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
;
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
2020 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2022 if (t
->globals
.list
!= NULL
)
2024 bfd_boolean matched
;
2028 while ((d
= (*t
->match
) (&t
->globals
, d
,
2029 h
->root
.root
.string
)) != NULL
)
2034 /* There is a version without definition. Make
2035 the symbol the default definition for this
2037 h
->verinfo
.vertree
= t
;
2045 /* There is no undefined version for this symbol. Hide the
2047 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2050 if (t
->locals
.list
!= NULL
)
2053 while ((d
= (*t
->match
) (&t
->locals
, d
,
2054 h
->root
.root
.string
)) != NULL
)
2057 /* If the match is "*", keep looking for a more
2058 explicit, perhaps even global, match.
2059 XXX: Shouldn't this be !d->wildcard instead? */
2060 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2069 if (local_ver
!= NULL
)
2071 h
->verinfo
.vertree
= local_ver
;
2072 if (h
->dynindx
!= -1
2073 && ! info
->export_dynamic
)
2075 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2083 /* Read and swap the relocs from the section indicated by SHDR. This
2084 may be either a REL or a RELA section. The relocations are
2085 translated into RELA relocations and stored in INTERNAL_RELOCS,
2086 which should have already been allocated to contain enough space.
2087 The EXTERNAL_RELOCS are a buffer where the external form of the
2088 relocations should be stored.
2090 Returns FALSE if something goes wrong. */
2093 elf_link_read_relocs_from_section (bfd
*abfd
,
2095 Elf_Internal_Shdr
*shdr
,
2096 void *external_relocs
,
2097 Elf_Internal_Rela
*internal_relocs
)
2099 const struct elf_backend_data
*bed
;
2100 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2101 const bfd_byte
*erela
;
2102 const bfd_byte
*erelaend
;
2103 Elf_Internal_Rela
*irela
;
2104 Elf_Internal_Shdr
*symtab_hdr
;
2107 /* Position ourselves at the start of the section. */
2108 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2111 /* Read the relocations. */
2112 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2115 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2116 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2118 bed
= get_elf_backend_data (abfd
);
2120 /* Convert the external relocations to the internal format. */
2121 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2122 swap_in
= bed
->s
->swap_reloc_in
;
2123 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2124 swap_in
= bed
->s
->swap_reloca_in
;
2127 bfd_set_error (bfd_error_wrong_format
);
2131 erela
= external_relocs
;
2132 erelaend
= erela
+ shdr
->sh_size
;
2133 irela
= internal_relocs
;
2134 while (erela
< erelaend
)
2138 (*swap_in
) (abfd
, erela
, irela
);
2139 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2140 if (bed
->s
->arch_size
== 64)
2142 if ((size_t) r_symndx
>= nsyms
)
2144 (*_bfd_error_handler
)
2145 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2146 " for offset 0x%lx in section `%A'"),
2148 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2149 bfd_set_error (bfd_error_bad_value
);
2152 irela
+= bed
->s
->int_rels_per_ext_rel
;
2153 erela
+= shdr
->sh_entsize
;
2159 /* Read and swap the relocs for a section O. They may have been
2160 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2161 not NULL, they are used as buffers to read into. They are known to
2162 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2163 the return value is allocated using either malloc or bfd_alloc,
2164 according to the KEEP_MEMORY argument. If O has two relocation
2165 sections (both REL and RELA relocations), then the REL_HDR
2166 relocations will appear first in INTERNAL_RELOCS, followed by the
2167 REL_HDR2 relocations. */
2170 _bfd_elf_link_read_relocs (bfd
*abfd
,
2172 void *external_relocs
,
2173 Elf_Internal_Rela
*internal_relocs
,
2174 bfd_boolean keep_memory
)
2176 Elf_Internal_Shdr
*rel_hdr
;
2177 void *alloc1
= NULL
;
2178 Elf_Internal_Rela
*alloc2
= NULL
;
2179 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2181 if (elf_section_data (o
)->relocs
!= NULL
)
2182 return elf_section_data (o
)->relocs
;
2184 if (o
->reloc_count
== 0)
2187 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2189 if (internal_relocs
== NULL
)
2193 size
= o
->reloc_count
;
2194 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2196 internal_relocs
= alloc2
= bfd_alloc (abfd
, size
);
2198 internal_relocs
= alloc2
= bfd_malloc (size
);
2199 if (internal_relocs
== NULL
)
2203 if (external_relocs
== NULL
)
2205 bfd_size_type size
= rel_hdr
->sh_size
;
2207 if (elf_section_data (o
)->rel_hdr2
)
2208 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2209 alloc1
= bfd_malloc (size
);
2212 external_relocs
= alloc1
;
2215 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2219 if (elf_section_data (o
)->rel_hdr2
2220 && (!elf_link_read_relocs_from_section
2222 elf_section_data (o
)->rel_hdr2
,
2223 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2224 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2225 * bed
->s
->int_rels_per_ext_rel
))))
2228 /* Cache the results for next time, if we can. */
2230 elf_section_data (o
)->relocs
= internal_relocs
;
2235 /* Don't free alloc2, since if it was allocated we are passing it
2236 back (under the name of internal_relocs). */
2238 return internal_relocs
;
2246 bfd_release (abfd
, alloc2
);
2253 /* Compute the size of, and allocate space for, REL_HDR which is the
2254 section header for a section containing relocations for O. */
2257 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2258 Elf_Internal_Shdr
*rel_hdr
,
2261 bfd_size_type reloc_count
;
2262 bfd_size_type num_rel_hashes
;
2264 /* Figure out how many relocations there will be. */
2265 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2266 reloc_count
= elf_section_data (o
)->rel_count
;
2268 reloc_count
= elf_section_data (o
)->rel_count2
;
2270 num_rel_hashes
= o
->reloc_count
;
2271 if (num_rel_hashes
< reloc_count
)
2272 num_rel_hashes
= reloc_count
;
2274 /* That allows us to calculate the size of the section. */
2275 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2277 /* The contents field must last into write_object_contents, so we
2278 allocate it with bfd_alloc rather than malloc. Also since we
2279 cannot be sure that the contents will actually be filled in,
2280 we zero the allocated space. */
2281 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2282 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2285 /* We only allocate one set of hash entries, so we only do it the
2286 first time we are called. */
2287 if (elf_section_data (o
)->rel_hashes
== NULL
2290 struct elf_link_hash_entry
**p
;
2292 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2296 elf_section_data (o
)->rel_hashes
= p
;
2302 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2303 originated from the section given by INPUT_REL_HDR) to the
2307 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2308 asection
*input_section
,
2309 Elf_Internal_Shdr
*input_rel_hdr
,
2310 Elf_Internal_Rela
*internal_relocs
,
2311 struct elf_link_hash_entry
**rel_hash
2314 Elf_Internal_Rela
*irela
;
2315 Elf_Internal_Rela
*irelaend
;
2317 Elf_Internal_Shdr
*output_rel_hdr
;
2318 asection
*output_section
;
2319 unsigned int *rel_countp
= NULL
;
2320 const struct elf_backend_data
*bed
;
2321 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2323 output_section
= input_section
->output_section
;
2324 output_rel_hdr
= NULL
;
2326 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2327 == input_rel_hdr
->sh_entsize
)
2329 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2330 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2332 else if (elf_section_data (output_section
)->rel_hdr2
2333 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2334 == input_rel_hdr
->sh_entsize
))
2336 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2337 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2341 (*_bfd_error_handler
)
2342 (_("%B: relocation size mismatch in %B section %A"),
2343 output_bfd
, input_section
->owner
, input_section
);
2344 bfd_set_error (bfd_error_wrong_format
);
2348 bed
= get_elf_backend_data (output_bfd
);
2349 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2350 swap_out
= bed
->s
->swap_reloc_out
;
2351 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2352 swap_out
= bed
->s
->swap_reloca_out
;
2356 erel
= output_rel_hdr
->contents
;
2357 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2358 irela
= internal_relocs
;
2359 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2360 * bed
->s
->int_rels_per_ext_rel
);
2361 while (irela
< irelaend
)
2363 (*swap_out
) (output_bfd
, irela
, erel
);
2364 irela
+= bed
->s
->int_rels_per_ext_rel
;
2365 erel
+= input_rel_hdr
->sh_entsize
;
2368 /* Bump the counter, so that we know where to add the next set of
2370 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2375 /* Make weak undefined symbols in PIE dynamic. */
2378 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2379 struct elf_link_hash_entry
*h
)
2383 && h
->root
.type
== bfd_link_hash_undefweak
)
2384 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2389 /* Fix up the flags for a symbol. This handles various cases which
2390 can only be fixed after all the input files are seen. This is
2391 currently called by both adjust_dynamic_symbol and
2392 assign_sym_version, which is unnecessary but perhaps more robust in
2393 the face of future changes. */
2396 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2397 struct elf_info_failed
*eif
)
2399 const struct elf_backend_data
*bed
;
2401 /* If this symbol was mentioned in a non-ELF file, try to set
2402 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2403 permit a non-ELF file to correctly refer to a symbol defined in
2404 an ELF dynamic object. */
2407 while (h
->root
.type
== bfd_link_hash_indirect
)
2408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2410 if (h
->root
.type
!= bfd_link_hash_defined
2411 && h
->root
.type
!= bfd_link_hash_defweak
)
2414 h
->ref_regular_nonweak
= 1;
2418 if (h
->root
.u
.def
.section
->owner
!= NULL
2419 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2420 == bfd_target_elf_flavour
))
2423 h
->ref_regular_nonweak
= 1;
2429 if (h
->dynindx
== -1
2433 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2442 /* Unfortunately, NON_ELF is only correct if the symbol
2443 was first seen in a non-ELF file. Fortunately, if the symbol
2444 was first seen in an ELF file, we're probably OK unless the
2445 symbol was defined in a non-ELF file. Catch that case here.
2446 FIXME: We're still in trouble if the symbol was first seen in
2447 a dynamic object, and then later in a non-ELF regular object. */
2448 if ((h
->root
.type
== bfd_link_hash_defined
2449 || h
->root
.type
== bfd_link_hash_defweak
)
2451 && (h
->root
.u
.def
.section
->owner
!= NULL
2452 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2453 != bfd_target_elf_flavour
)
2454 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2455 && !h
->def_dynamic
)))
2459 /* Backend specific symbol fixup. */
2460 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2461 if (bed
->elf_backend_fixup_symbol
2462 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2465 /* If this is a final link, and the symbol was defined as a common
2466 symbol in a regular object file, and there was no definition in
2467 any dynamic object, then the linker will have allocated space for
2468 the symbol in a common section but the DEF_REGULAR
2469 flag will not have been set. */
2470 if (h
->root
.type
== bfd_link_hash_defined
2474 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2477 /* If -Bsymbolic was used (which means to bind references to global
2478 symbols to the definition within the shared object), and this
2479 symbol was defined in a regular object, then it actually doesn't
2480 need a PLT entry. Likewise, if the symbol has non-default
2481 visibility. If the symbol has hidden or internal visibility, we
2482 will force it local. */
2484 && eif
->info
->shared
2485 && is_elf_hash_table (eif
->info
->hash
)
2486 && (SYMBOLIC_BIND (eif
->info
, h
)
2487 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2490 bfd_boolean force_local
;
2492 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2493 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2494 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2497 /* If a weak undefined symbol has non-default visibility, we also
2498 hide it from the dynamic linker. */
2499 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2500 && h
->root
.type
== bfd_link_hash_undefweak
)
2501 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2503 /* If this is a weak defined symbol in a dynamic object, and we know
2504 the real definition in the dynamic object, copy interesting flags
2505 over to the real definition. */
2506 if (h
->u
.weakdef
!= NULL
)
2508 struct elf_link_hash_entry
*weakdef
;
2510 weakdef
= h
->u
.weakdef
;
2511 if (h
->root
.type
== bfd_link_hash_indirect
)
2512 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2514 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2515 || h
->root
.type
== bfd_link_hash_defweak
);
2516 BFD_ASSERT (weakdef
->def_dynamic
);
2518 /* If the real definition is defined by a regular object file,
2519 don't do anything special. See the longer description in
2520 _bfd_elf_adjust_dynamic_symbol, below. */
2521 if (weakdef
->def_regular
)
2522 h
->u
.weakdef
= NULL
;
2525 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2526 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2527 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2534 /* Make the backend pick a good value for a dynamic symbol. This is
2535 called via elf_link_hash_traverse, and also calls itself
2539 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2541 struct elf_info_failed
*eif
= data
;
2543 const struct elf_backend_data
*bed
;
2545 if (! is_elf_hash_table (eif
->info
->hash
))
2548 if (h
->root
.type
== bfd_link_hash_warning
)
2550 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2551 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2553 /* When warning symbols are created, they **replace** the "real"
2554 entry in the hash table, thus we never get to see the real
2555 symbol in a hash traversal. So look at it now. */
2556 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2559 /* Ignore indirect symbols. These are added by the versioning code. */
2560 if (h
->root
.type
== bfd_link_hash_indirect
)
2563 /* Fix the symbol flags. */
2564 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2567 /* If this symbol does not require a PLT entry, and it is not
2568 defined by a dynamic object, or is not referenced by a regular
2569 object, ignore it. We do have to handle a weak defined symbol,
2570 even if no regular object refers to it, if we decided to add it
2571 to the dynamic symbol table. FIXME: Do we normally need to worry
2572 about symbols which are defined by one dynamic object and
2573 referenced by another one? */
2578 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2580 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2584 /* If we've already adjusted this symbol, don't do it again. This
2585 can happen via a recursive call. */
2586 if (h
->dynamic_adjusted
)
2589 /* Don't look at this symbol again. Note that we must set this
2590 after checking the above conditions, because we may look at a
2591 symbol once, decide not to do anything, and then get called
2592 recursively later after REF_REGULAR is set below. */
2593 h
->dynamic_adjusted
= 1;
2595 /* If this is a weak definition, and we know a real definition, and
2596 the real symbol is not itself defined by a regular object file,
2597 then get a good value for the real definition. We handle the
2598 real symbol first, for the convenience of the backend routine.
2600 Note that there is a confusing case here. If the real definition
2601 is defined by a regular object file, we don't get the real symbol
2602 from the dynamic object, but we do get the weak symbol. If the
2603 processor backend uses a COPY reloc, then if some routine in the
2604 dynamic object changes the real symbol, we will not see that
2605 change in the corresponding weak symbol. This is the way other
2606 ELF linkers work as well, and seems to be a result of the shared
2609 I will clarify this issue. Most SVR4 shared libraries define the
2610 variable _timezone and define timezone as a weak synonym. The
2611 tzset call changes _timezone. If you write
2612 extern int timezone;
2614 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2615 you might expect that, since timezone is a synonym for _timezone,
2616 the same number will print both times. However, if the processor
2617 backend uses a COPY reloc, then actually timezone will be copied
2618 into your process image, and, since you define _timezone
2619 yourself, _timezone will not. Thus timezone and _timezone will
2620 wind up at different memory locations. The tzset call will set
2621 _timezone, leaving timezone unchanged. */
2623 if (h
->u
.weakdef
!= NULL
)
2625 /* If we get to this point, we know there is an implicit
2626 reference by a regular object file via the weak symbol H.
2627 FIXME: Is this really true? What if the traversal finds
2628 H->U.WEAKDEF before it finds H? */
2629 h
->u
.weakdef
->ref_regular
= 1;
2631 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2635 /* If a symbol has no type and no size and does not require a PLT
2636 entry, then we are probably about to do the wrong thing here: we
2637 are probably going to create a COPY reloc for an empty object.
2638 This case can arise when a shared object is built with assembly
2639 code, and the assembly code fails to set the symbol type. */
2641 && h
->type
== STT_NOTYPE
2643 (*_bfd_error_handler
)
2644 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2645 h
->root
.root
.string
);
2647 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2648 bed
= get_elf_backend_data (dynobj
);
2649 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2658 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2662 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2665 unsigned int power_of_two
;
2667 asection
*sec
= h
->root
.u
.def
.section
;
2669 /* The section aligment of definition is the maximum alignment
2670 requirement of symbols defined in the section. Since we don't
2671 know the symbol alignment requirement, we start with the
2672 maximum alignment and check low bits of the symbol address
2673 for the minimum alignment. */
2674 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2675 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2676 while ((h
->root
.u
.def
.value
& mask
) != 0)
2682 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2685 /* Adjust the section alignment if needed. */
2686 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2691 /* We make sure that the symbol will be aligned properly. */
2692 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2694 /* Define the symbol as being at this point in DYNBSS. */
2695 h
->root
.u
.def
.section
= dynbss
;
2696 h
->root
.u
.def
.value
= dynbss
->size
;
2698 /* Increment the size of DYNBSS to make room for the symbol. */
2699 dynbss
->size
+= h
->size
;
2704 /* Adjust all external symbols pointing into SEC_MERGE sections
2705 to reflect the object merging within the sections. */
2708 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2712 if (h
->root
.type
== bfd_link_hash_warning
)
2713 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2715 if ((h
->root
.type
== bfd_link_hash_defined
2716 || h
->root
.type
== bfd_link_hash_defweak
)
2717 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2718 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2720 bfd
*output_bfd
= data
;
2722 h
->root
.u
.def
.value
=
2723 _bfd_merged_section_offset (output_bfd
,
2724 &h
->root
.u
.def
.section
,
2725 elf_section_data (sec
)->sec_info
,
2726 h
->root
.u
.def
.value
);
2732 /* Returns false if the symbol referred to by H should be considered
2733 to resolve local to the current module, and true if it should be
2734 considered to bind dynamically. */
2737 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2738 struct bfd_link_info
*info
,
2739 bfd_boolean ignore_protected
)
2741 bfd_boolean binding_stays_local_p
;
2742 const struct elf_backend_data
*bed
;
2743 struct elf_link_hash_table
*hash_table
;
2748 while (h
->root
.type
== bfd_link_hash_indirect
2749 || h
->root
.type
== bfd_link_hash_warning
)
2750 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2752 /* If it was forced local, then clearly it's not dynamic. */
2753 if (h
->dynindx
== -1)
2755 if (h
->forced_local
)
2758 /* Identify the cases where name binding rules say that a
2759 visible symbol resolves locally. */
2760 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2762 switch (ELF_ST_VISIBILITY (h
->other
))
2769 hash_table
= elf_hash_table (info
);
2770 if (!is_elf_hash_table (hash_table
))
2773 bed
= get_elf_backend_data (hash_table
->dynobj
);
2775 /* Proper resolution for function pointer equality may require
2776 that these symbols perhaps be resolved dynamically, even though
2777 we should be resolving them to the current module. */
2778 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2779 binding_stays_local_p
= TRUE
;
2786 /* If it isn't defined locally, then clearly it's dynamic. */
2787 if (!h
->def_regular
)
2790 /* Otherwise, the symbol is dynamic if binding rules don't tell
2791 us that it remains local. */
2792 return !binding_stays_local_p
;
2795 /* Return true if the symbol referred to by H should be considered
2796 to resolve local to the current module, and false otherwise. Differs
2797 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2798 undefined symbols and weak symbols. */
2801 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2802 struct bfd_link_info
*info
,
2803 bfd_boolean local_protected
)
2805 const struct elf_backend_data
*bed
;
2806 struct elf_link_hash_table
*hash_table
;
2808 /* If it's a local sym, of course we resolve locally. */
2812 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2813 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2814 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2817 /* Common symbols that become definitions don't get the DEF_REGULAR
2818 flag set, so test it first, and don't bail out. */
2819 if (ELF_COMMON_DEF_P (h
))
2821 /* If we don't have a definition in a regular file, then we can't
2822 resolve locally. The sym is either undefined or dynamic. */
2823 else if (!h
->def_regular
)
2826 /* Forced local symbols resolve locally. */
2827 if (h
->forced_local
)
2830 /* As do non-dynamic symbols. */
2831 if (h
->dynindx
== -1)
2834 /* At this point, we know the symbol is defined and dynamic. In an
2835 executable it must resolve locally, likewise when building symbolic
2836 shared libraries. */
2837 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2840 /* Now deal with defined dynamic symbols in shared libraries. Ones
2841 with default visibility might not resolve locally. */
2842 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2845 hash_table
= elf_hash_table (info
);
2846 if (!is_elf_hash_table (hash_table
))
2849 bed
= get_elf_backend_data (hash_table
->dynobj
);
2851 /* STV_PROTECTED non-function symbols are local. */
2852 if (!bed
->is_function_type (h
->type
))
2855 /* Function pointer equality tests may require that STV_PROTECTED
2856 symbols be treated as dynamic symbols, even when we know that the
2857 dynamic linker will resolve them locally. */
2858 return local_protected
;
2861 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2862 aligned. Returns the first TLS output section. */
2864 struct bfd_section
*
2865 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2867 struct bfd_section
*sec
, *tls
;
2868 unsigned int align
= 0;
2870 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2871 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2875 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2876 if (sec
->alignment_power
> align
)
2877 align
= sec
->alignment_power
;
2879 elf_hash_table (info
)->tls_sec
= tls
;
2881 /* Ensure the alignment of the first section is the largest alignment,
2882 so that the tls segment starts aligned. */
2884 tls
->alignment_power
= align
;
2889 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2891 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2892 Elf_Internal_Sym
*sym
)
2894 const struct elf_backend_data
*bed
;
2896 /* Local symbols do not count, but target specific ones might. */
2897 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2898 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2901 bed
= get_elf_backend_data (abfd
);
2902 /* Function symbols do not count. */
2903 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2906 /* If the section is undefined, then so is the symbol. */
2907 if (sym
->st_shndx
== SHN_UNDEF
)
2910 /* If the symbol is defined in the common section, then
2911 it is a common definition and so does not count. */
2912 if (bed
->common_definition (sym
))
2915 /* If the symbol is in a target specific section then we
2916 must rely upon the backend to tell us what it is. */
2917 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2918 /* FIXME - this function is not coded yet:
2920 return _bfd_is_global_symbol_definition (abfd, sym);
2922 Instead for now assume that the definition is not global,
2923 Even if this is wrong, at least the linker will behave
2924 in the same way that it used to do. */
2930 /* Search the symbol table of the archive element of the archive ABFD
2931 whose archive map contains a mention of SYMDEF, and determine if
2932 the symbol is defined in this element. */
2934 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2936 Elf_Internal_Shdr
* hdr
;
2937 bfd_size_type symcount
;
2938 bfd_size_type extsymcount
;
2939 bfd_size_type extsymoff
;
2940 Elf_Internal_Sym
*isymbuf
;
2941 Elf_Internal_Sym
*isym
;
2942 Elf_Internal_Sym
*isymend
;
2945 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2949 if (! bfd_check_format (abfd
, bfd_object
))
2952 /* If we have already included the element containing this symbol in the
2953 link then we do not need to include it again. Just claim that any symbol
2954 it contains is not a definition, so that our caller will not decide to
2955 (re)include this element. */
2956 if (abfd
->archive_pass
)
2959 /* Select the appropriate symbol table. */
2960 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2961 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2963 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2965 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2967 /* The sh_info field of the symtab header tells us where the
2968 external symbols start. We don't care about the local symbols. */
2969 if (elf_bad_symtab (abfd
))
2971 extsymcount
= symcount
;
2976 extsymcount
= symcount
- hdr
->sh_info
;
2977 extsymoff
= hdr
->sh_info
;
2980 if (extsymcount
== 0)
2983 /* Read in the symbol table. */
2984 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2986 if (isymbuf
== NULL
)
2989 /* Scan the symbol table looking for SYMDEF. */
2991 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2995 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3000 if (strcmp (name
, symdef
->name
) == 0)
3002 result
= is_global_data_symbol_definition (abfd
, isym
);
3012 /* Add an entry to the .dynamic table. */
3015 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3019 struct elf_link_hash_table
*hash_table
;
3020 const struct elf_backend_data
*bed
;
3022 bfd_size_type newsize
;
3023 bfd_byte
*newcontents
;
3024 Elf_Internal_Dyn dyn
;
3026 hash_table
= elf_hash_table (info
);
3027 if (! is_elf_hash_table (hash_table
))
3030 bed
= get_elf_backend_data (hash_table
->dynobj
);
3031 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3032 BFD_ASSERT (s
!= NULL
);
3034 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3035 newcontents
= bfd_realloc (s
->contents
, newsize
);
3036 if (newcontents
== NULL
)
3040 dyn
.d_un
.d_val
= val
;
3041 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3044 s
->contents
= newcontents
;
3049 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3050 otherwise just check whether one already exists. Returns -1 on error,
3051 1 if a DT_NEEDED tag already exists, and 0 on success. */
3054 elf_add_dt_needed_tag (bfd
*abfd
,
3055 struct bfd_link_info
*info
,
3059 struct elf_link_hash_table
*hash_table
;
3060 bfd_size_type oldsize
;
3061 bfd_size_type strindex
;
3063 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3066 hash_table
= elf_hash_table (info
);
3067 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3068 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3069 if (strindex
== (bfd_size_type
) -1)
3072 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3075 const struct elf_backend_data
*bed
;
3078 bed
= get_elf_backend_data (hash_table
->dynobj
);
3079 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3081 for (extdyn
= sdyn
->contents
;
3082 extdyn
< sdyn
->contents
+ sdyn
->size
;
3083 extdyn
+= bed
->s
->sizeof_dyn
)
3085 Elf_Internal_Dyn dyn
;
3087 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3088 if (dyn
.d_tag
== DT_NEEDED
3089 && dyn
.d_un
.d_val
== strindex
)
3091 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3099 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3102 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3106 /* We were just checking for existence of the tag. */
3107 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3112 /* Sort symbol by value and section. */
3114 elf_sort_symbol (const void *arg1
, const void *arg2
)
3116 const struct elf_link_hash_entry
*h1
;
3117 const struct elf_link_hash_entry
*h2
;
3118 bfd_signed_vma vdiff
;
3120 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3121 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3122 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3124 return vdiff
> 0 ? 1 : -1;
3127 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3129 return sdiff
> 0 ? 1 : -1;
3134 /* This function is used to adjust offsets into .dynstr for
3135 dynamic symbols. This is called via elf_link_hash_traverse. */
3138 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3140 struct elf_strtab_hash
*dynstr
= data
;
3142 if (h
->root
.type
== bfd_link_hash_warning
)
3143 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3145 if (h
->dynindx
!= -1)
3146 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3150 /* Assign string offsets in .dynstr, update all structures referencing
3154 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3156 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3157 struct elf_link_local_dynamic_entry
*entry
;
3158 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3159 bfd
*dynobj
= hash_table
->dynobj
;
3162 const struct elf_backend_data
*bed
;
3165 _bfd_elf_strtab_finalize (dynstr
);
3166 size
= _bfd_elf_strtab_size (dynstr
);
3168 bed
= get_elf_backend_data (dynobj
);
3169 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3170 BFD_ASSERT (sdyn
!= NULL
);
3172 /* Update all .dynamic entries referencing .dynstr strings. */
3173 for (extdyn
= sdyn
->contents
;
3174 extdyn
< sdyn
->contents
+ sdyn
->size
;
3175 extdyn
+= bed
->s
->sizeof_dyn
)
3177 Elf_Internal_Dyn dyn
;
3179 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3183 dyn
.d_un
.d_val
= size
;
3191 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3196 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3199 /* Now update local dynamic symbols. */
3200 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3201 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3202 entry
->isym
.st_name
);
3204 /* And the rest of dynamic symbols. */
3205 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3207 /* Adjust version definitions. */
3208 if (elf_tdata (output_bfd
)->cverdefs
)
3213 Elf_Internal_Verdef def
;
3214 Elf_Internal_Verdaux defaux
;
3216 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3220 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3222 p
+= sizeof (Elf_External_Verdef
);
3223 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3225 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3227 _bfd_elf_swap_verdaux_in (output_bfd
,
3228 (Elf_External_Verdaux
*) p
, &defaux
);
3229 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3231 _bfd_elf_swap_verdaux_out (output_bfd
,
3232 &defaux
, (Elf_External_Verdaux
*) p
);
3233 p
+= sizeof (Elf_External_Verdaux
);
3236 while (def
.vd_next
);
3239 /* Adjust version references. */
3240 if (elf_tdata (output_bfd
)->verref
)
3245 Elf_Internal_Verneed need
;
3246 Elf_Internal_Vernaux needaux
;
3248 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3252 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3254 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3255 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3256 (Elf_External_Verneed
*) p
);
3257 p
+= sizeof (Elf_External_Verneed
);
3258 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3260 _bfd_elf_swap_vernaux_in (output_bfd
,
3261 (Elf_External_Vernaux
*) p
, &needaux
);
3262 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3264 _bfd_elf_swap_vernaux_out (output_bfd
,
3266 (Elf_External_Vernaux
*) p
);
3267 p
+= sizeof (Elf_External_Vernaux
);
3270 while (need
.vn_next
);
3276 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3277 The default is to only match when the INPUT and OUTPUT are exactly
3281 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3282 const bfd_target
*output
)
3284 return input
== output
;
3287 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3288 This version is used when different targets for the same architecture
3289 are virtually identical. */
3292 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3293 const bfd_target
*output
)
3295 const struct elf_backend_data
*obed
, *ibed
;
3297 if (input
== output
)
3300 ibed
= xvec_get_elf_backend_data (input
);
3301 obed
= xvec_get_elf_backend_data (output
);
3303 if (ibed
->arch
!= obed
->arch
)
3306 /* If both backends are using this function, deem them compatible. */
3307 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3310 /* Add symbols from an ELF object file to the linker hash table. */
3313 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3315 Elf_Internal_Shdr
*hdr
;
3316 bfd_size_type symcount
;
3317 bfd_size_type extsymcount
;
3318 bfd_size_type extsymoff
;
3319 struct elf_link_hash_entry
**sym_hash
;
3320 bfd_boolean dynamic
;
3321 Elf_External_Versym
*extversym
= NULL
;
3322 Elf_External_Versym
*ever
;
3323 struct elf_link_hash_entry
*weaks
;
3324 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3325 bfd_size_type nondeflt_vers_cnt
= 0;
3326 Elf_Internal_Sym
*isymbuf
= NULL
;
3327 Elf_Internal_Sym
*isym
;
3328 Elf_Internal_Sym
*isymend
;
3329 const struct elf_backend_data
*bed
;
3330 bfd_boolean add_needed
;
3331 struct elf_link_hash_table
*htab
;
3333 void *alloc_mark
= NULL
;
3334 struct bfd_hash_entry
**old_table
= NULL
;
3335 unsigned int old_size
= 0;
3336 unsigned int old_count
= 0;
3337 void *old_tab
= NULL
;
3340 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3341 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3342 long old_dynsymcount
= 0;
3344 size_t hashsize
= 0;
3346 htab
= elf_hash_table (info
);
3347 bed
= get_elf_backend_data (abfd
);
3349 if ((abfd
->flags
& DYNAMIC
) == 0)
3355 /* You can't use -r against a dynamic object. Also, there's no
3356 hope of using a dynamic object which does not exactly match
3357 the format of the output file. */
3358 if (info
->relocatable
3359 || !is_elf_hash_table (htab
)
3360 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3362 if (info
->relocatable
)
3363 bfd_set_error (bfd_error_invalid_operation
);
3365 bfd_set_error (bfd_error_wrong_format
);
3370 /* As a GNU extension, any input sections which are named
3371 .gnu.warning.SYMBOL are treated as warning symbols for the given
3372 symbol. This differs from .gnu.warning sections, which generate
3373 warnings when they are included in an output file. */
3374 if (info
->executable
)
3378 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3382 name
= bfd_get_section_name (abfd
, s
);
3383 if (CONST_STRNEQ (name
, ".gnu.warning."))
3388 name
+= sizeof ".gnu.warning." - 1;
3390 /* If this is a shared object, then look up the symbol
3391 in the hash table. If it is there, and it is already
3392 been defined, then we will not be using the entry
3393 from this shared object, so we don't need to warn.
3394 FIXME: If we see the definition in a regular object
3395 later on, we will warn, but we shouldn't. The only
3396 fix is to keep track of what warnings we are supposed
3397 to emit, and then handle them all at the end of the
3401 struct elf_link_hash_entry
*h
;
3403 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3405 /* FIXME: What about bfd_link_hash_common? */
3407 && (h
->root
.type
== bfd_link_hash_defined
3408 || h
->root
.type
== bfd_link_hash_defweak
))
3410 /* We don't want to issue this warning. Clobber
3411 the section size so that the warning does not
3412 get copied into the output file. */
3419 msg
= bfd_alloc (abfd
, sz
+ 1);
3423 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3428 if (! (_bfd_generic_link_add_one_symbol
3429 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3430 FALSE
, bed
->collect
, NULL
)))
3433 if (! info
->relocatable
)
3435 /* Clobber the section size so that the warning does
3436 not get copied into the output file. */
3439 /* Also set SEC_EXCLUDE, so that symbols defined in
3440 the warning section don't get copied to the output. */
3441 s
->flags
|= SEC_EXCLUDE
;
3450 /* If we are creating a shared library, create all the dynamic
3451 sections immediately. We need to attach them to something,
3452 so we attach them to this BFD, provided it is the right
3453 format. FIXME: If there are no input BFD's of the same
3454 format as the output, we can't make a shared library. */
3456 && is_elf_hash_table (htab
)
3457 && info
->output_bfd
->xvec
== abfd
->xvec
3458 && !htab
->dynamic_sections_created
)
3460 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3464 else if (!is_elf_hash_table (htab
))
3469 const char *soname
= NULL
;
3470 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3473 /* ld --just-symbols and dynamic objects don't mix very well.
3474 ld shouldn't allow it. */
3475 if ((s
= abfd
->sections
) != NULL
3476 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3479 /* If this dynamic lib was specified on the command line with
3480 --as-needed in effect, then we don't want to add a DT_NEEDED
3481 tag unless the lib is actually used. Similary for libs brought
3482 in by another lib's DT_NEEDED. When --no-add-needed is used
3483 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3484 any dynamic library in DT_NEEDED tags in the dynamic lib at
3486 add_needed
= (elf_dyn_lib_class (abfd
)
3487 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3488 | DYN_NO_NEEDED
)) == 0;
3490 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3495 unsigned int elfsec
;
3496 unsigned long shlink
;
3498 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3499 goto error_free_dyn
;
3501 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3502 if (elfsec
== SHN_BAD
)
3503 goto error_free_dyn
;
3504 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3506 for (extdyn
= dynbuf
;
3507 extdyn
< dynbuf
+ s
->size
;
3508 extdyn
+= bed
->s
->sizeof_dyn
)
3510 Elf_Internal_Dyn dyn
;
3512 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3513 if (dyn
.d_tag
== DT_SONAME
)
3515 unsigned int tagv
= dyn
.d_un
.d_val
;
3516 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3518 goto error_free_dyn
;
3520 if (dyn
.d_tag
== DT_NEEDED
)
3522 struct bfd_link_needed_list
*n
, **pn
;
3524 unsigned int tagv
= dyn
.d_un
.d_val
;
3526 amt
= sizeof (struct bfd_link_needed_list
);
3527 n
= bfd_alloc (abfd
, amt
);
3528 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3529 if (n
== NULL
|| fnm
== NULL
)
3530 goto error_free_dyn
;
3531 amt
= strlen (fnm
) + 1;
3532 anm
= bfd_alloc (abfd
, amt
);
3534 goto error_free_dyn
;
3535 memcpy (anm
, fnm
, amt
);
3539 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3543 if (dyn
.d_tag
== DT_RUNPATH
)
3545 struct bfd_link_needed_list
*n
, **pn
;
3547 unsigned int tagv
= dyn
.d_un
.d_val
;
3549 amt
= sizeof (struct bfd_link_needed_list
);
3550 n
= bfd_alloc (abfd
, amt
);
3551 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3552 if (n
== NULL
|| fnm
== NULL
)
3553 goto error_free_dyn
;
3554 amt
= strlen (fnm
) + 1;
3555 anm
= bfd_alloc (abfd
, amt
);
3557 goto error_free_dyn
;
3558 memcpy (anm
, fnm
, amt
);
3562 for (pn
= & runpath
;
3568 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3569 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3571 struct bfd_link_needed_list
*n
, **pn
;
3573 unsigned int tagv
= dyn
.d_un
.d_val
;
3575 amt
= sizeof (struct bfd_link_needed_list
);
3576 n
= bfd_alloc (abfd
, amt
);
3577 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3578 if (n
== NULL
|| fnm
== NULL
)
3579 goto error_free_dyn
;
3580 amt
= strlen (fnm
) + 1;
3581 anm
= bfd_alloc (abfd
, amt
);
3588 memcpy (anm
, fnm
, amt
);
3603 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3604 frees all more recently bfd_alloc'd blocks as well. */
3610 struct bfd_link_needed_list
**pn
;
3611 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3616 /* We do not want to include any of the sections in a dynamic
3617 object in the output file. We hack by simply clobbering the
3618 list of sections in the BFD. This could be handled more
3619 cleanly by, say, a new section flag; the existing
3620 SEC_NEVER_LOAD flag is not the one we want, because that one
3621 still implies that the section takes up space in the output
3623 bfd_section_list_clear (abfd
);
3625 /* Find the name to use in a DT_NEEDED entry that refers to this
3626 object. If the object has a DT_SONAME entry, we use it.
3627 Otherwise, if the generic linker stuck something in
3628 elf_dt_name, we use that. Otherwise, we just use the file
3630 if (soname
== NULL
|| *soname
== '\0')
3632 soname
= elf_dt_name (abfd
);
3633 if (soname
== NULL
|| *soname
== '\0')
3634 soname
= bfd_get_filename (abfd
);
3637 /* Save the SONAME because sometimes the linker emulation code
3638 will need to know it. */
3639 elf_dt_name (abfd
) = soname
;
3641 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3645 /* If we have already included this dynamic object in the
3646 link, just ignore it. There is no reason to include a
3647 particular dynamic object more than once. */
3652 /* If this is a dynamic object, we always link against the .dynsym
3653 symbol table, not the .symtab symbol table. The dynamic linker
3654 will only see the .dynsym symbol table, so there is no reason to
3655 look at .symtab for a dynamic object. */
3657 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3658 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3660 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3662 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3664 /* The sh_info field of the symtab header tells us where the
3665 external symbols start. We don't care about the local symbols at
3667 if (elf_bad_symtab (abfd
))
3669 extsymcount
= symcount
;
3674 extsymcount
= symcount
- hdr
->sh_info
;
3675 extsymoff
= hdr
->sh_info
;
3679 if (extsymcount
!= 0)
3681 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3683 if (isymbuf
== NULL
)
3686 /* We store a pointer to the hash table entry for each external
3688 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3689 sym_hash
= bfd_alloc (abfd
, amt
);
3690 if (sym_hash
== NULL
)
3691 goto error_free_sym
;
3692 elf_sym_hashes (abfd
) = sym_hash
;
3697 /* Read in any version definitions. */
3698 if (!_bfd_elf_slurp_version_tables (abfd
,
3699 info
->default_imported_symver
))
3700 goto error_free_sym
;
3702 /* Read in the symbol versions, but don't bother to convert them
3703 to internal format. */
3704 if (elf_dynversym (abfd
) != 0)
3706 Elf_Internal_Shdr
*versymhdr
;
3708 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3709 extversym
= bfd_malloc (versymhdr
->sh_size
);
3710 if (extversym
== NULL
)
3711 goto error_free_sym
;
3712 amt
= versymhdr
->sh_size
;
3713 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3714 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3715 goto error_free_vers
;
3719 /* If we are loading an as-needed shared lib, save the symbol table
3720 state before we start adding symbols. If the lib turns out
3721 to be unneeded, restore the state. */
3722 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3727 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3729 struct bfd_hash_entry
*p
;
3730 struct elf_link_hash_entry
*h
;
3732 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3734 h
= (struct elf_link_hash_entry
*) p
;
3735 entsize
+= htab
->root
.table
.entsize
;
3736 if (h
->root
.type
== bfd_link_hash_warning
)
3737 entsize
+= htab
->root
.table
.entsize
;
3741 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3742 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3743 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3744 if (old_tab
== NULL
)
3745 goto error_free_vers
;
3747 /* Remember the current objalloc pointer, so that all mem for
3748 symbols added can later be reclaimed. */
3749 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3750 if (alloc_mark
== NULL
)
3751 goto error_free_vers
;
3753 /* Make a special call to the linker "notice" function to
3754 tell it that we are about to handle an as-needed lib. */
3755 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3757 goto error_free_vers
;
3759 /* Clone the symbol table and sym hashes. Remember some
3760 pointers into the symbol table, and dynamic symbol count. */
3761 old_hash
= (char *) old_tab
+ tabsize
;
3762 old_ent
= (char *) old_hash
+ hashsize
;
3763 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3764 memcpy (old_hash
, sym_hash
, hashsize
);
3765 old_undefs
= htab
->root
.undefs
;
3766 old_undefs_tail
= htab
->root
.undefs_tail
;
3767 old_table
= htab
->root
.table
.table
;
3768 old_size
= htab
->root
.table
.size
;
3769 old_count
= htab
->root
.table
.count
;
3770 old_dynsymcount
= htab
->dynsymcount
;
3772 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3774 struct bfd_hash_entry
*p
;
3775 struct elf_link_hash_entry
*h
;
3777 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3779 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3780 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3781 h
= (struct elf_link_hash_entry
*) p
;
3782 if (h
->root
.type
== bfd_link_hash_warning
)
3784 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3785 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3792 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3793 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3795 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3799 asection
*sec
, *new_sec
;
3802 struct elf_link_hash_entry
*h
;
3803 bfd_boolean definition
;
3804 bfd_boolean size_change_ok
;
3805 bfd_boolean type_change_ok
;
3806 bfd_boolean new_weakdef
;
3807 bfd_boolean override
;
3809 unsigned int old_alignment
;
3814 flags
= BSF_NO_FLAGS
;
3816 value
= isym
->st_value
;
3818 common
= bed
->common_definition (isym
);
3820 bind
= ELF_ST_BIND (isym
->st_info
);
3821 if (bind
== STB_LOCAL
)
3823 /* This should be impossible, since ELF requires that all
3824 global symbols follow all local symbols, and that sh_info
3825 point to the first global symbol. Unfortunately, Irix 5
3829 else if (bind
== STB_GLOBAL
)
3831 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3834 else if (bind
== STB_WEAK
)
3838 /* Leave it up to the processor backend. */
3841 if (isym
->st_shndx
== SHN_UNDEF
)
3842 sec
= bfd_und_section_ptr
;
3843 else if (isym
->st_shndx
== SHN_ABS
)
3844 sec
= bfd_abs_section_ptr
;
3845 else if (isym
->st_shndx
== SHN_COMMON
)
3847 sec
= bfd_com_section_ptr
;
3848 /* What ELF calls the size we call the value. What ELF
3849 calls the value we call the alignment. */
3850 value
= isym
->st_size
;
3854 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3856 sec
= bfd_abs_section_ptr
;
3857 else if (sec
->kept_section
)
3859 /* Symbols from discarded section are undefined. We keep
3861 sec
= bfd_und_section_ptr
;
3862 isym
->st_shndx
= SHN_UNDEF
;
3864 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3868 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3871 goto error_free_vers
;
3873 if (isym
->st_shndx
== SHN_COMMON
3874 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3875 && !info
->relocatable
)
3877 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3881 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3884 | SEC_LINKER_CREATED
3885 | SEC_THREAD_LOCAL
));
3887 goto error_free_vers
;
3891 else if (bed
->elf_add_symbol_hook
)
3893 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3895 goto error_free_vers
;
3897 /* The hook function sets the name to NULL if this symbol
3898 should be skipped for some reason. */
3903 /* Sanity check that all possibilities were handled. */
3906 bfd_set_error (bfd_error_bad_value
);
3907 goto error_free_vers
;
3910 if (bfd_is_und_section (sec
)
3911 || bfd_is_com_section (sec
))
3916 size_change_ok
= FALSE
;
3917 type_change_ok
= bed
->type_change_ok
;
3922 if (is_elf_hash_table (htab
))
3924 Elf_Internal_Versym iver
;
3925 unsigned int vernum
= 0;
3930 if (info
->default_imported_symver
)
3931 /* Use the default symbol version created earlier. */
3932 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3937 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3939 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3941 /* If this is a hidden symbol, or if it is not version
3942 1, we append the version name to the symbol name.
3943 However, we do not modify a non-hidden absolute symbol
3944 if it is not a function, because it might be the version
3945 symbol itself. FIXME: What if it isn't? */
3946 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3948 && (!bfd_is_abs_section (sec
)
3949 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3952 size_t namelen
, verlen
, newlen
;
3955 if (isym
->st_shndx
!= SHN_UNDEF
)
3957 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3959 else if (vernum
> 1)
3961 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3967 (*_bfd_error_handler
)
3968 (_("%B: %s: invalid version %u (max %d)"),
3970 elf_tdata (abfd
)->cverdefs
);
3971 bfd_set_error (bfd_error_bad_value
);
3972 goto error_free_vers
;
3977 /* We cannot simply test for the number of
3978 entries in the VERNEED section since the
3979 numbers for the needed versions do not start
3981 Elf_Internal_Verneed
*t
;
3984 for (t
= elf_tdata (abfd
)->verref
;
3988 Elf_Internal_Vernaux
*a
;
3990 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3992 if (a
->vna_other
== vernum
)
3994 verstr
= a
->vna_nodename
;
4003 (*_bfd_error_handler
)
4004 (_("%B: %s: invalid needed version %d"),
4005 abfd
, name
, vernum
);
4006 bfd_set_error (bfd_error_bad_value
);
4007 goto error_free_vers
;
4011 namelen
= strlen (name
);
4012 verlen
= strlen (verstr
);
4013 newlen
= namelen
+ verlen
+ 2;
4014 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4015 && isym
->st_shndx
!= SHN_UNDEF
)
4018 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
4019 if (newname
== NULL
)
4020 goto error_free_vers
;
4021 memcpy (newname
, name
, namelen
);
4022 p
= newname
+ namelen
;
4024 /* If this is a defined non-hidden version symbol,
4025 we add another @ to the name. This indicates the
4026 default version of the symbol. */
4027 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4028 && isym
->st_shndx
!= SHN_UNDEF
)
4030 memcpy (p
, verstr
, verlen
+ 1);
4035 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
4036 &value
, &old_alignment
,
4037 sym_hash
, &skip
, &override
,
4038 &type_change_ok
, &size_change_ok
))
4039 goto error_free_vers
;
4048 while (h
->root
.type
== bfd_link_hash_indirect
4049 || h
->root
.type
== bfd_link_hash_warning
)
4050 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4052 /* Remember the old alignment if this is a common symbol, so
4053 that we don't reduce the alignment later on. We can't
4054 check later, because _bfd_generic_link_add_one_symbol
4055 will set a default for the alignment which we want to
4056 override. We also remember the old bfd where the existing
4057 definition comes from. */
4058 switch (h
->root
.type
)
4063 case bfd_link_hash_defined
:
4064 case bfd_link_hash_defweak
:
4065 old_bfd
= h
->root
.u
.def
.section
->owner
;
4068 case bfd_link_hash_common
:
4069 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4070 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4074 if (elf_tdata (abfd
)->verdef
!= NULL
4078 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4081 if (! (_bfd_generic_link_add_one_symbol
4082 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4083 (struct bfd_link_hash_entry
**) sym_hash
)))
4084 goto error_free_vers
;
4087 while (h
->root
.type
== bfd_link_hash_indirect
4088 || h
->root
.type
== bfd_link_hash_warning
)
4089 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4092 new_weakdef
= FALSE
;
4095 && (flags
& BSF_WEAK
) != 0
4096 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4097 && is_elf_hash_table (htab
)
4098 && h
->u
.weakdef
== NULL
)
4100 /* Keep a list of all weak defined non function symbols from
4101 a dynamic object, using the weakdef field. Later in this
4102 function we will set the weakdef field to the correct
4103 value. We only put non-function symbols from dynamic
4104 objects on this list, because that happens to be the only
4105 time we need to know the normal symbol corresponding to a
4106 weak symbol, and the information is time consuming to
4107 figure out. If the weakdef field is not already NULL,
4108 then this symbol was already defined by some previous
4109 dynamic object, and we will be using that previous
4110 definition anyhow. */
4112 h
->u
.weakdef
= weaks
;
4117 /* Set the alignment of a common symbol. */
4118 if ((common
|| bfd_is_com_section (sec
))
4119 && h
->root
.type
== bfd_link_hash_common
)
4124 align
= bfd_log2 (isym
->st_value
);
4127 /* The new symbol is a common symbol in a shared object.
4128 We need to get the alignment from the section. */
4129 align
= new_sec
->alignment_power
;
4131 if (align
> old_alignment
4132 /* Permit an alignment power of zero if an alignment of one
4133 is specified and no other alignments have been specified. */
4134 || (isym
->st_value
== 1 && old_alignment
== 0))
4135 h
->root
.u
.c
.p
->alignment_power
= align
;
4137 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4140 if (is_elf_hash_table (htab
))
4144 /* Check the alignment when a common symbol is involved. This
4145 can change when a common symbol is overridden by a normal
4146 definition or a common symbol is ignored due to the old
4147 normal definition. We need to make sure the maximum
4148 alignment is maintained. */
4149 if ((old_alignment
|| common
)
4150 && h
->root
.type
!= bfd_link_hash_common
)
4152 unsigned int common_align
;
4153 unsigned int normal_align
;
4154 unsigned int symbol_align
;
4158 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4159 if (h
->root
.u
.def
.section
->owner
!= NULL
4160 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4162 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4163 if (normal_align
> symbol_align
)
4164 normal_align
= symbol_align
;
4167 normal_align
= symbol_align
;
4171 common_align
= old_alignment
;
4172 common_bfd
= old_bfd
;
4177 common_align
= bfd_log2 (isym
->st_value
);
4179 normal_bfd
= old_bfd
;
4182 if (normal_align
< common_align
)
4184 /* PR binutils/2735 */
4185 if (normal_bfd
== NULL
)
4186 (*_bfd_error_handler
)
4187 (_("Warning: alignment %u of common symbol `%s' in %B"
4188 " is greater than the alignment (%u) of its section %A"),
4189 common_bfd
, h
->root
.u
.def
.section
,
4190 1 << common_align
, name
, 1 << normal_align
);
4192 (*_bfd_error_handler
)
4193 (_("Warning: alignment %u of symbol `%s' in %B"
4194 " is smaller than %u in %B"),
4195 normal_bfd
, common_bfd
,
4196 1 << normal_align
, name
, 1 << common_align
);
4200 /* Remember the symbol size if it isn't undefined. */
4201 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4202 && (definition
|| h
->size
== 0))
4205 && h
->size
!= isym
->st_size
4206 && ! size_change_ok
)
4207 (*_bfd_error_handler
)
4208 (_("Warning: size of symbol `%s' changed"
4209 " from %lu in %B to %lu in %B"),
4211 name
, (unsigned long) h
->size
,
4212 (unsigned long) isym
->st_size
);
4214 h
->size
= isym
->st_size
;
4217 /* If this is a common symbol, then we always want H->SIZE
4218 to be the size of the common symbol. The code just above
4219 won't fix the size if a common symbol becomes larger. We
4220 don't warn about a size change here, because that is
4221 covered by --warn-common. Allow changed between different
4223 if (h
->root
.type
== bfd_link_hash_common
)
4224 h
->size
= h
->root
.u
.c
.size
;
4226 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4227 && (definition
|| h
->type
== STT_NOTYPE
))
4229 if (h
->type
!= STT_NOTYPE
4230 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4231 && ! type_change_ok
)
4232 (*_bfd_error_handler
)
4233 (_("Warning: type of symbol `%s' changed"
4234 " from %d to %d in %B"),
4235 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4237 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4240 /* If st_other has a processor-specific meaning, specific
4241 code might be needed here. We never merge the visibility
4242 attribute with the one from a dynamic object. */
4243 if (bed
->elf_backend_merge_symbol_attribute
)
4244 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4247 /* If this symbol has default visibility and the user has requested
4248 we not re-export it, then mark it as hidden. */
4249 if (definition
&& !dynamic
4251 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4252 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4253 isym
->st_other
= (STV_HIDDEN
4254 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4256 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4258 unsigned char hvis
, symvis
, other
, nvis
;
4260 /* Only merge the visibility. Leave the remainder of the
4261 st_other field to elf_backend_merge_symbol_attribute. */
4262 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4264 /* Combine visibilities, using the most constraining one. */
4265 hvis
= ELF_ST_VISIBILITY (h
->other
);
4266 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4272 nvis
= hvis
< symvis
? hvis
: symvis
;
4274 h
->other
= other
| nvis
;
4277 /* Set a flag in the hash table entry indicating the type of
4278 reference or definition we just found. Keep a count of
4279 the number of dynamic symbols we find. A dynamic symbol
4280 is one which is referenced or defined by both a regular
4281 object and a shared object. */
4288 if (bind
!= STB_WEAK
)
4289 h
->ref_regular_nonweak
= 1;
4293 if (! info
->executable
4306 || (h
->u
.weakdef
!= NULL
4308 && h
->u
.weakdef
->dynindx
!= -1))
4312 if (definition
&& (sec
->flags
& SEC_DEBUGGING
) && !info
->relocatable
)
4314 /* We don't want to make debug symbol dynamic. */
4315 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4319 /* Check to see if we need to add an indirect symbol for
4320 the default name. */
4321 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4322 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4323 &sec
, &value
, &dynsym
,
4325 goto error_free_vers
;
4327 if (definition
&& !dynamic
)
4329 char *p
= strchr (name
, ELF_VER_CHR
);
4330 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4332 /* Queue non-default versions so that .symver x, x@FOO
4333 aliases can be checked. */
4336 amt
= ((isymend
- isym
+ 1)
4337 * sizeof (struct elf_link_hash_entry
*));
4338 nondeflt_vers
= bfd_malloc (amt
);
4340 goto error_free_vers
;
4342 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4346 if (dynsym
&& h
->dynindx
== -1)
4348 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4349 goto error_free_vers
;
4350 if (h
->u
.weakdef
!= NULL
4352 && h
->u
.weakdef
->dynindx
== -1)
4354 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4355 goto error_free_vers
;
4358 else if (dynsym
&& h
->dynindx
!= -1)
4359 /* If the symbol already has a dynamic index, but
4360 visibility says it should not be visible, turn it into
4362 switch (ELF_ST_VISIBILITY (h
->other
))
4366 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4377 const char *soname
= elf_dt_name (abfd
);
4379 /* A symbol from a library loaded via DT_NEEDED of some
4380 other library is referenced by a regular object.
4381 Add a DT_NEEDED entry for it. Issue an error if
4382 --no-add-needed is used. */
4383 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4385 (*_bfd_error_handler
)
4386 (_("%s: invalid DSO for symbol `%s' definition"),
4388 bfd_set_error (bfd_error_bad_value
);
4389 goto error_free_vers
;
4392 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4395 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4397 goto error_free_vers
;
4399 BFD_ASSERT (ret
== 0);
4404 if (extversym
!= NULL
)
4410 if (isymbuf
!= NULL
)
4416 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4420 /* Restore the symbol table. */
4421 if (bed
->as_needed_cleanup
)
4422 (*bed
->as_needed_cleanup
) (abfd
, info
);
4423 old_hash
= (char *) old_tab
+ tabsize
;
4424 old_ent
= (char *) old_hash
+ hashsize
;
4425 sym_hash
= elf_sym_hashes (abfd
);
4426 htab
->root
.table
.table
= old_table
;
4427 htab
->root
.table
.size
= old_size
;
4428 htab
->root
.table
.count
= old_count
;
4429 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4430 memcpy (sym_hash
, old_hash
, hashsize
);
4431 htab
->root
.undefs
= old_undefs
;
4432 htab
->root
.undefs_tail
= old_undefs_tail
;
4433 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4435 struct bfd_hash_entry
*p
;
4436 struct elf_link_hash_entry
*h
;
4438 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4440 h
= (struct elf_link_hash_entry
*) p
;
4441 if (h
->root
.type
== bfd_link_hash_warning
)
4442 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4443 if (h
->dynindx
>= old_dynsymcount
)
4444 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4446 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4447 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4448 h
= (struct elf_link_hash_entry
*) p
;
4449 if (h
->root
.type
== bfd_link_hash_warning
)
4451 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4452 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4457 /* Make a special call to the linker "notice" function to
4458 tell it that symbols added for crefs may need to be removed. */
4459 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4461 goto error_free_vers
;
4464 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4466 if (nondeflt_vers
!= NULL
)
4467 free (nondeflt_vers
);
4471 if (old_tab
!= NULL
)
4473 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4475 goto error_free_vers
;
4480 /* Now that all the symbols from this input file are created, handle
4481 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4482 if (nondeflt_vers
!= NULL
)
4484 bfd_size_type cnt
, symidx
;
4486 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4488 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4489 char *shortname
, *p
;
4491 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4493 || (h
->root
.type
!= bfd_link_hash_defined
4494 && h
->root
.type
!= bfd_link_hash_defweak
))
4497 amt
= p
- h
->root
.root
.string
;
4498 shortname
= bfd_malloc (amt
+ 1);
4500 goto error_free_vers
;
4501 memcpy (shortname
, h
->root
.root
.string
, amt
);
4502 shortname
[amt
] = '\0';
4504 hi
= (struct elf_link_hash_entry
*)
4505 bfd_link_hash_lookup (&htab
->root
, shortname
,
4506 FALSE
, FALSE
, FALSE
);
4508 && hi
->root
.type
== h
->root
.type
4509 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4510 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4512 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4513 hi
->root
.type
= bfd_link_hash_indirect
;
4514 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4515 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4516 sym_hash
= elf_sym_hashes (abfd
);
4518 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4519 if (sym_hash
[symidx
] == hi
)
4521 sym_hash
[symidx
] = h
;
4527 free (nondeflt_vers
);
4528 nondeflt_vers
= NULL
;
4531 /* Now set the weakdefs field correctly for all the weak defined
4532 symbols we found. The only way to do this is to search all the
4533 symbols. Since we only need the information for non functions in
4534 dynamic objects, that's the only time we actually put anything on
4535 the list WEAKS. We need this information so that if a regular
4536 object refers to a symbol defined weakly in a dynamic object, the
4537 real symbol in the dynamic object is also put in the dynamic
4538 symbols; we also must arrange for both symbols to point to the
4539 same memory location. We could handle the general case of symbol
4540 aliasing, but a general symbol alias can only be generated in
4541 assembler code, handling it correctly would be very time
4542 consuming, and other ELF linkers don't handle general aliasing
4546 struct elf_link_hash_entry
**hpp
;
4547 struct elf_link_hash_entry
**hppend
;
4548 struct elf_link_hash_entry
**sorted_sym_hash
;
4549 struct elf_link_hash_entry
*h
;
4552 /* Since we have to search the whole symbol list for each weak
4553 defined symbol, search time for N weak defined symbols will be
4554 O(N^2). Binary search will cut it down to O(NlogN). */
4555 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4556 sorted_sym_hash
= bfd_malloc (amt
);
4557 if (sorted_sym_hash
== NULL
)
4559 sym_hash
= sorted_sym_hash
;
4560 hpp
= elf_sym_hashes (abfd
);
4561 hppend
= hpp
+ extsymcount
;
4563 for (; hpp
< hppend
; hpp
++)
4567 && h
->root
.type
== bfd_link_hash_defined
4568 && !bed
->is_function_type (h
->type
))
4576 qsort (sorted_sym_hash
, sym_count
,
4577 sizeof (struct elf_link_hash_entry
*),
4580 while (weaks
!= NULL
)
4582 struct elf_link_hash_entry
*hlook
;
4589 weaks
= hlook
->u
.weakdef
;
4590 hlook
->u
.weakdef
= NULL
;
4592 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4593 || hlook
->root
.type
== bfd_link_hash_defweak
4594 || hlook
->root
.type
== bfd_link_hash_common
4595 || hlook
->root
.type
== bfd_link_hash_indirect
);
4596 slook
= hlook
->root
.u
.def
.section
;
4597 vlook
= hlook
->root
.u
.def
.value
;
4604 bfd_signed_vma vdiff
;
4606 h
= sorted_sym_hash
[idx
];
4607 vdiff
= vlook
- h
->root
.u
.def
.value
;
4614 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4627 /* We didn't find a value/section match. */
4631 for (i
= ilook
; i
< sym_count
; i
++)
4633 h
= sorted_sym_hash
[i
];
4635 /* Stop if value or section doesn't match. */
4636 if (h
->root
.u
.def
.value
!= vlook
4637 || h
->root
.u
.def
.section
!= slook
)
4639 else if (h
!= hlook
)
4641 hlook
->u
.weakdef
= h
;
4643 /* If the weak definition is in the list of dynamic
4644 symbols, make sure the real definition is put
4646 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4648 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4651 free (sorted_sym_hash
);
4656 /* If the real definition is in the list of dynamic
4657 symbols, make sure the weak definition is put
4658 there as well. If we don't do this, then the
4659 dynamic loader might not merge the entries for the
4660 real definition and the weak definition. */
4661 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4663 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4664 goto err_free_sym_hash
;
4671 free (sorted_sym_hash
);
4674 if (bed
->check_directives
4675 && !(*bed
->check_directives
) (abfd
, info
))
4678 /* If this object is the same format as the output object, and it is
4679 not a shared library, then let the backend look through the
4682 This is required to build global offset table entries and to
4683 arrange for dynamic relocs. It is not required for the
4684 particular common case of linking non PIC code, even when linking
4685 against shared libraries, but unfortunately there is no way of
4686 knowing whether an object file has been compiled PIC or not.
4687 Looking through the relocs is not particularly time consuming.
4688 The problem is that we must either (1) keep the relocs in memory,
4689 which causes the linker to require additional runtime memory or
4690 (2) read the relocs twice from the input file, which wastes time.
4691 This would be a good case for using mmap.
4693 I have no idea how to handle linking PIC code into a file of a
4694 different format. It probably can't be done. */
4696 && is_elf_hash_table (htab
)
4697 && bed
->check_relocs
!= NULL
4698 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4702 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4704 Elf_Internal_Rela
*internal_relocs
;
4707 if ((o
->flags
& SEC_RELOC
) == 0
4708 || o
->reloc_count
== 0
4709 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4710 && (o
->flags
& SEC_DEBUGGING
) != 0)
4711 || bfd_is_abs_section (o
->output_section
))
4714 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4716 if (internal_relocs
== NULL
)
4719 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4721 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4722 free (internal_relocs
);
4729 /* If this is a non-traditional link, try to optimize the handling
4730 of the .stab/.stabstr sections. */
4732 && ! info
->traditional_format
4733 && is_elf_hash_table (htab
)
4734 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4738 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4739 if (stabstr
!= NULL
)
4741 bfd_size_type string_offset
= 0;
4744 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4745 if (CONST_STRNEQ (stab
->name
, ".stab")
4746 && (!stab
->name
[5] ||
4747 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4748 && (stab
->flags
& SEC_MERGE
) == 0
4749 && !bfd_is_abs_section (stab
->output_section
))
4751 struct bfd_elf_section_data
*secdata
;
4753 secdata
= elf_section_data (stab
);
4754 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4755 stabstr
, &secdata
->sec_info
,
4758 if (secdata
->sec_info
)
4759 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4764 if (is_elf_hash_table (htab
) && add_needed
)
4766 /* Add this bfd to the loaded list. */
4767 struct elf_link_loaded_list
*n
;
4769 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4773 n
->next
= htab
->loaded
;
4780 if (old_tab
!= NULL
)
4782 if (nondeflt_vers
!= NULL
)
4783 free (nondeflt_vers
);
4784 if (extversym
!= NULL
)
4787 if (isymbuf
!= NULL
)
4793 /* Return the linker hash table entry of a symbol that might be
4794 satisfied by an archive symbol. Return -1 on error. */
4796 struct elf_link_hash_entry
*
4797 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4798 struct bfd_link_info
*info
,
4801 struct elf_link_hash_entry
*h
;
4805 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4809 /* If this is a default version (the name contains @@), look up the
4810 symbol again with only one `@' as well as without the version.
4811 The effect is that references to the symbol with and without the
4812 version will be matched by the default symbol in the archive. */
4814 p
= strchr (name
, ELF_VER_CHR
);
4815 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4818 /* First check with only one `@'. */
4819 len
= strlen (name
);
4820 copy
= bfd_alloc (abfd
, len
);
4822 return (struct elf_link_hash_entry
*) 0 - 1;
4824 first
= p
- name
+ 1;
4825 memcpy (copy
, name
, first
);
4826 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4828 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4831 /* We also need to check references to the symbol without the
4833 copy
[first
- 1] = '\0';
4834 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4835 FALSE
, FALSE
, FALSE
);
4838 bfd_release (abfd
, copy
);
4842 /* Add symbols from an ELF archive file to the linker hash table. We
4843 don't use _bfd_generic_link_add_archive_symbols because of a
4844 problem which arises on UnixWare. The UnixWare libc.so is an
4845 archive which includes an entry libc.so.1 which defines a bunch of
4846 symbols. The libc.so archive also includes a number of other
4847 object files, which also define symbols, some of which are the same
4848 as those defined in libc.so.1. Correct linking requires that we
4849 consider each object file in turn, and include it if it defines any
4850 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4851 this; it looks through the list of undefined symbols, and includes
4852 any object file which defines them. When this algorithm is used on
4853 UnixWare, it winds up pulling in libc.so.1 early and defining a
4854 bunch of symbols. This means that some of the other objects in the
4855 archive are not included in the link, which is incorrect since they
4856 precede libc.so.1 in the archive.
4858 Fortunately, ELF archive handling is simpler than that done by
4859 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4860 oddities. In ELF, if we find a symbol in the archive map, and the
4861 symbol is currently undefined, we know that we must pull in that
4864 Unfortunately, we do have to make multiple passes over the symbol
4865 table until nothing further is resolved. */
4868 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4871 bfd_boolean
*defined
= NULL
;
4872 bfd_boolean
*included
= NULL
;
4876 const struct elf_backend_data
*bed
;
4877 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4878 (bfd
*, struct bfd_link_info
*, const char *);
4880 if (! bfd_has_map (abfd
))
4882 /* An empty archive is a special case. */
4883 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4885 bfd_set_error (bfd_error_no_armap
);
4889 /* Keep track of all symbols we know to be already defined, and all
4890 files we know to be already included. This is to speed up the
4891 second and subsequent passes. */
4892 c
= bfd_ardata (abfd
)->symdef_count
;
4896 amt
*= sizeof (bfd_boolean
);
4897 defined
= bfd_zmalloc (amt
);
4898 included
= bfd_zmalloc (amt
);
4899 if (defined
== NULL
|| included
== NULL
)
4902 symdefs
= bfd_ardata (abfd
)->symdefs
;
4903 bed
= get_elf_backend_data (abfd
);
4904 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4917 symdefend
= symdef
+ c
;
4918 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4920 struct elf_link_hash_entry
*h
;
4922 struct bfd_link_hash_entry
*undefs_tail
;
4925 if (defined
[i
] || included
[i
])
4927 if (symdef
->file_offset
== last
)
4933 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4934 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4940 if (h
->root
.type
== bfd_link_hash_common
)
4942 /* We currently have a common symbol. The archive map contains
4943 a reference to this symbol, so we may want to include it. We
4944 only want to include it however, if this archive element
4945 contains a definition of the symbol, not just another common
4948 Unfortunately some archivers (including GNU ar) will put
4949 declarations of common symbols into their archive maps, as
4950 well as real definitions, so we cannot just go by the archive
4951 map alone. Instead we must read in the element's symbol
4952 table and check that to see what kind of symbol definition
4954 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4957 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4959 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4964 /* We need to include this archive member. */
4965 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4966 if (element
== NULL
)
4969 if (! bfd_check_format (element
, bfd_object
))
4972 /* Doublecheck that we have not included this object
4973 already--it should be impossible, but there may be
4974 something wrong with the archive. */
4975 if (element
->archive_pass
!= 0)
4977 bfd_set_error (bfd_error_bad_value
);
4980 element
->archive_pass
= 1;
4982 undefs_tail
= info
->hash
->undefs_tail
;
4984 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4987 if (! bfd_link_add_symbols (element
, info
))
4990 /* If there are any new undefined symbols, we need to make
4991 another pass through the archive in order to see whether
4992 they can be defined. FIXME: This isn't perfect, because
4993 common symbols wind up on undefs_tail and because an
4994 undefined symbol which is defined later on in this pass
4995 does not require another pass. This isn't a bug, but it
4996 does make the code less efficient than it could be. */
4997 if (undefs_tail
!= info
->hash
->undefs_tail
)
5000 /* Look backward to mark all symbols from this object file
5001 which we have already seen in this pass. */
5005 included
[mark
] = TRUE
;
5010 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5012 /* We mark subsequent symbols from this object file as we go
5013 on through the loop. */
5014 last
= symdef
->file_offset
;
5025 if (defined
!= NULL
)
5027 if (included
!= NULL
)
5032 /* Given an ELF BFD, add symbols to the global hash table as
5036 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5038 switch (bfd_get_format (abfd
))
5041 return elf_link_add_object_symbols (abfd
, info
);
5043 return elf_link_add_archive_symbols (abfd
, info
);
5045 bfd_set_error (bfd_error_wrong_format
);
5050 struct hash_codes_info
5052 unsigned long *hashcodes
;
5056 /* This function will be called though elf_link_hash_traverse to store
5057 all hash value of the exported symbols in an array. */
5060 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5062 struct hash_codes_info
*inf
= data
;
5068 if (h
->root
.type
== bfd_link_hash_warning
)
5069 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5071 /* Ignore indirect symbols. These are added by the versioning code. */
5072 if (h
->dynindx
== -1)
5075 name
= h
->root
.root
.string
;
5076 p
= strchr (name
, ELF_VER_CHR
);
5079 alc
= bfd_malloc (p
- name
+ 1);
5085 memcpy (alc
, name
, p
- name
);
5086 alc
[p
- name
] = '\0';
5090 /* Compute the hash value. */
5091 ha
= bfd_elf_hash (name
);
5093 /* Store the found hash value in the array given as the argument. */
5094 *(inf
->hashcodes
)++ = ha
;
5096 /* And store it in the struct so that we can put it in the hash table
5098 h
->u
.elf_hash_value
= ha
;
5106 struct collect_gnu_hash_codes
5109 const struct elf_backend_data
*bed
;
5110 unsigned long int nsyms
;
5111 unsigned long int maskbits
;
5112 unsigned long int *hashcodes
;
5113 unsigned long int *hashval
;
5114 unsigned long int *indx
;
5115 unsigned long int *counts
;
5118 long int min_dynindx
;
5119 unsigned long int bucketcount
;
5120 unsigned long int symindx
;
5121 long int local_indx
;
5122 long int shift1
, shift2
;
5123 unsigned long int mask
;
5127 /* This function will be called though elf_link_hash_traverse to store
5128 all hash value of the exported symbols in an array. */
5131 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5133 struct collect_gnu_hash_codes
*s
= data
;
5139 if (h
->root
.type
== bfd_link_hash_warning
)
5140 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5142 /* Ignore indirect symbols. These are added by the versioning code. */
5143 if (h
->dynindx
== -1)
5146 /* Ignore also local symbols and undefined symbols. */
5147 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5150 name
= h
->root
.root
.string
;
5151 p
= strchr (name
, ELF_VER_CHR
);
5154 alc
= bfd_malloc (p
- name
+ 1);
5160 memcpy (alc
, name
, p
- name
);
5161 alc
[p
- name
] = '\0';
5165 /* Compute the hash value. */
5166 ha
= bfd_elf_gnu_hash (name
);
5168 /* Store the found hash value in the array for compute_bucket_count,
5169 and also for .dynsym reordering purposes. */
5170 s
->hashcodes
[s
->nsyms
] = ha
;
5171 s
->hashval
[h
->dynindx
] = ha
;
5173 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5174 s
->min_dynindx
= h
->dynindx
;
5182 /* This function will be called though elf_link_hash_traverse to do
5183 final dynaminc symbol renumbering. */
5186 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5188 struct collect_gnu_hash_codes
*s
= data
;
5189 unsigned long int bucket
;
5190 unsigned long int val
;
5192 if (h
->root
.type
== bfd_link_hash_warning
)
5193 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5195 /* Ignore indirect symbols. */
5196 if (h
->dynindx
== -1)
5199 /* Ignore also local symbols and undefined symbols. */
5200 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5202 if (h
->dynindx
>= s
->min_dynindx
)
5203 h
->dynindx
= s
->local_indx
++;
5207 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5208 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5209 & ((s
->maskbits
>> s
->shift1
) - 1);
5210 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5212 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5213 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5214 if (s
->counts
[bucket
] == 1)
5215 /* Last element terminates the chain. */
5217 bfd_put_32 (s
->output_bfd
, val
,
5218 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5219 --s
->counts
[bucket
];
5220 h
->dynindx
= s
->indx
[bucket
]++;
5224 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5227 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5229 return !(h
->forced_local
5230 || h
->root
.type
== bfd_link_hash_undefined
5231 || h
->root
.type
== bfd_link_hash_undefweak
5232 || ((h
->root
.type
== bfd_link_hash_defined
5233 || h
->root
.type
== bfd_link_hash_defweak
)
5234 && h
->root
.u
.def
.section
->output_section
== NULL
));
5237 /* Array used to determine the number of hash table buckets to use
5238 based on the number of symbols there are. If there are fewer than
5239 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5240 fewer than 37 we use 17 buckets, and so forth. We never use more
5241 than 32771 buckets. */
5243 static const size_t elf_buckets
[] =
5245 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5249 /* Compute bucket count for hashing table. We do not use a static set
5250 of possible tables sizes anymore. Instead we determine for all
5251 possible reasonable sizes of the table the outcome (i.e., the
5252 number of collisions etc) and choose the best solution. The
5253 weighting functions are not too simple to allow the table to grow
5254 without bounds. Instead one of the weighting factors is the size.
5255 Therefore the result is always a good payoff between few collisions
5256 (= short chain lengths) and table size. */
5258 compute_bucket_count (struct bfd_link_info
*info
,
5259 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5260 unsigned long int nsyms
,
5263 size_t best_size
= 0;
5264 unsigned long int i
;
5266 /* We have a problem here. The following code to optimize the table
5267 size requires an integer type with more the 32 bits. If
5268 BFD_HOST_U_64_BIT is set we know about such a type. */
5269 #ifdef BFD_HOST_U_64_BIT
5274 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5275 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5276 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5277 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5278 unsigned long int *counts
;
5281 /* Possible optimization parameters: if we have NSYMS symbols we say
5282 that the hashing table must at least have NSYMS/4 and at most
5284 minsize
= nsyms
/ 4;
5287 best_size
= maxsize
= nsyms
* 2;
5292 if ((best_size
& 31) == 0)
5296 /* Create array where we count the collisions in. We must use bfd_malloc
5297 since the size could be large. */
5299 amt
*= sizeof (unsigned long int);
5300 counts
= bfd_malloc (amt
);
5304 /* Compute the "optimal" size for the hash table. The criteria is a
5305 minimal chain length. The minor criteria is (of course) the size
5307 for (i
= minsize
; i
< maxsize
; ++i
)
5309 /* Walk through the array of hashcodes and count the collisions. */
5310 BFD_HOST_U_64_BIT max
;
5311 unsigned long int j
;
5312 unsigned long int fact
;
5314 if (gnu_hash
&& (i
& 31) == 0)
5317 memset (counts
, '\0', i
* sizeof (unsigned long int));
5319 /* Determine how often each hash bucket is used. */
5320 for (j
= 0; j
< nsyms
; ++j
)
5321 ++counts
[hashcodes
[j
] % i
];
5323 /* For the weight function we need some information about the
5324 pagesize on the target. This is information need not be 100%
5325 accurate. Since this information is not available (so far) we
5326 define it here to a reasonable default value. If it is crucial
5327 to have a better value some day simply define this value. */
5328 # ifndef BFD_TARGET_PAGESIZE
5329 # define BFD_TARGET_PAGESIZE (4096)
5332 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5334 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5337 /* Variant 1: optimize for short chains. We add the squares
5338 of all the chain lengths (which favors many small chain
5339 over a few long chains). */
5340 for (j
= 0; j
< i
; ++j
)
5341 max
+= counts
[j
] * counts
[j
];
5343 /* This adds penalties for the overall size of the table. */
5344 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5347 /* Variant 2: Optimize a lot more for small table. Here we
5348 also add squares of the size but we also add penalties for
5349 empty slots (the +1 term). */
5350 for (j
= 0; j
< i
; ++j
)
5351 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5353 /* The overall size of the table is considered, but not as
5354 strong as in variant 1, where it is squared. */
5355 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5359 /* Compare with current best results. */
5360 if (max
< best_chlen
)
5370 #endif /* defined (BFD_HOST_U_64_BIT) */
5372 /* This is the fallback solution if no 64bit type is available or if we
5373 are not supposed to spend much time on optimizations. We select the
5374 bucket count using a fixed set of numbers. */
5375 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5377 best_size
= elf_buckets
[i
];
5378 if (nsyms
< elf_buckets
[i
+ 1])
5381 if (gnu_hash
&& best_size
< 2)
5388 /* Set up the sizes and contents of the ELF dynamic sections. This is
5389 called by the ELF linker emulation before_allocation routine. We
5390 must set the sizes of the sections before the linker sets the
5391 addresses of the various sections. */
5394 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5397 const char *filter_shlib
,
5398 const char * const *auxiliary_filters
,
5399 struct bfd_link_info
*info
,
5400 asection
**sinterpptr
,
5401 struct bfd_elf_version_tree
*verdefs
)
5403 bfd_size_type soname_indx
;
5405 const struct elf_backend_data
*bed
;
5406 struct elf_assign_sym_version_info asvinfo
;
5410 soname_indx
= (bfd_size_type
) -1;
5412 if (!is_elf_hash_table (info
->hash
))
5415 bed
= get_elf_backend_data (output_bfd
);
5416 if (info
->execstack
)
5417 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5418 else if (info
->noexecstack
)
5419 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5423 asection
*notesec
= NULL
;
5426 for (inputobj
= info
->input_bfds
;
5428 inputobj
= inputobj
->link_next
)
5432 if (inputobj
->flags
& (DYNAMIC
| EXEC_P
| BFD_LINKER_CREATED
))
5434 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5437 if (s
->flags
& SEC_CODE
)
5441 else if (bed
->default_execstack
)
5446 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5447 if (exec
&& info
->relocatable
5448 && notesec
->output_section
!= bfd_abs_section_ptr
)
5449 notesec
->output_section
->flags
|= SEC_CODE
;
5453 /* Any syms created from now on start with -1 in
5454 got.refcount/offset and plt.refcount/offset. */
5455 elf_hash_table (info
)->init_got_refcount
5456 = elf_hash_table (info
)->init_got_offset
;
5457 elf_hash_table (info
)->init_plt_refcount
5458 = elf_hash_table (info
)->init_plt_offset
;
5460 /* The backend may have to create some sections regardless of whether
5461 we're dynamic or not. */
5462 if (bed
->elf_backend_always_size_sections
5463 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5466 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5469 dynobj
= elf_hash_table (info
)->dynobj
;
5471 /* If there were no dynamic objects in the link, there is nothing to
5476 if (elf_hash_table (info
)->dynamic_sections_created
)
5478 struct elf_info_failed eif
;
5479 struct elf_link_hash_entry
*h
;
5481 struct bfd_elf_version_tree
*t
;
5482 struct bfd_elf_version_expr
*d
;
5484 bfd_boolean all_defined
;
5486 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5487 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5491 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5493 if (soname_indx
== (bfd_size_type
) -1
5494 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5500 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5502 info
->flags
|= DF_SYMBOLIC
;
5509 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5511 if (indx
== (bfd_size_type
) -1
5512 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5515 if (info
->new_dtags
)
5517 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5518 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5523 if (filter_shlib
!= NULL
)
5527 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5528 filter_shlib
, TRUE
);
5529 if (indx
== (bfd_size_type
) -1
5530 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5534 if (auxiliary_filters
!= NULL
)
5536 const char * const *p
;
5538 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5542 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5544 if (indx
== (bfd_size_type
) -1
5545 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5551 eif
.verdefs
= verdefs
;
5554 /* If we are supposed to export all symbols into the dynamic symbol
5555 table (this is not the normal case), then do so. */
5556 if (info
->export_dynamic
5557 || (info
->executable
&& info
->dynamic
))
5559 elf_link_hash_traverse (elf_hash_table (info
),
5560 _bfd_elf_export_symbol
,
5566 /* Make all global versions with definition. */
5567 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5568 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5569 if (!d
->symver
&& d
->symbol
)
5571 const char *verstr
, *name
;
5572 size_t namelen
, verlen
, newlen
;
5574 struct elf_link_hash_entry
*newh
;
5577 namelen
= strlen (name
);
5579 verlen
= strlen (verstr
);
5580 newlen
= namelen
+ verlen
+ 3;
5582 newname
= bfd_malloc (newlen
);
5583 if (newname
== NULL
)
5585 memcpy (newname
, name
, namelen
);
5587 /* Check the hidden versioned definition. */
5588 p
= newname
+ namelen
;
5590 memcpy (p
, verstr
, verlen
+ 1);
5591 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5592 newname
, FALSE
, FALSE
,
5595 || (newh
->root
.type
!= bfd_link_hash_defined
5596 && newh
->root
.type
!= bfd_link_hash_defweak
))
5598 /* Check the default versioned definition. */
5600 memcpy (p
, verstr
, verlen
+ 1);
5601 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5602 newname
, FALSE
, FALSE
,
5607 /* Mark this version if there is a definition and it is
5608 not defined in a shared object. */
5610 && !newh
->def_dynamic
5611 && (newh
->root
.type
== bfd_link_hash_defined
5612 || newh
->root
.type
== bfd_link_hash_defweak
))
5616 /* Attach all the symbols to their version information. */
5617 asvinfo
.output_bfd
= output_bfd
;
5618 asvinfo
.info
= info
;
5619 asvinfo
.verdefs
= verdefs
;
5620 asvinfo
.failed
= FALSE
;
5622 elf_link_hash_traverse (elf_hash_table (info
),
5623 _bfd_elf_link_assign_sym_version
,
5628 if (!info
->allow_undefined_version
)
5630 /* Check if all global versions have a definition. */
5632 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5633 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5634 if (!d
->symver
&& !d
->script
)
5636 (*_bfd_error_handler
)
5637 (_("%s: undefined version: %s"),
5638 d
->pattern
, t
->name
);
5639 all_defined
= FALSE
;
5644 bfd_set_error (bfd_error_bad_value
);
5649 /* Find all symbols which were defined in a dynamic object and make
5650 the backend pick a reasonable value for them. */
5651 elf_link_hash_traverse (elf_hash_table (info
),
5652 _bfd_elf_adjust_dynamic_symbol
,
5657 /* Add some entries to the .dynamic section. We fill in some of the
5658 values later, in bfd_elf_final_link, but we must add the entries
5659 now so that we know the final size of the .dynamic section. */
5661 /* If there are initialization and/or finalization functions to
5662 call then add the corresponding DT_INIT/DT_FINI entries. */
5663 h
= (info
->init_function
5664 ? elf_link_hash_lookup (elf_hash_table (info
),
5665 info
->init_function
, FALSE
,
5672 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5675 h
= (info
->fini_function
5676 ? elf_link_hash_lookup (elf_hash_table (info
),
5677 info
->fini_function
, FALSE
,
5684 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5688 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5689 if (s
!= NULL
&& s
->linker_has_input
)
5691 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5692 if (! info
->executable
)
5697 for (sub
= info
->input_bfds
; sub
!= NULL
;
5698 sub
= sub
->link_next
)
5699 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5700 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5701 if (elf_section_data (o
)->this_hdr
.sh_type
5702 == SHT_PREINIT_ARRAY
)
5704 (*_bfd_error_handler
)
5705 (_("%B: .preinit_array section is not allowed in DSO"),
5710 bfd_set_error (bfd_error_nonrepresentable_section
);
5714 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5715 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5718 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5719 if (s
!= NULL
&& s
->linker_has_input
)
5721 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5722 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5725 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5726 if (s
!= NULL
&& s
->linker_has_input
)
5728 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5729 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5733 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5734 /* If .dynstr is excluded from the link, we don't want any of
5735 these tags. Strictly, we should be checking each section
5736 individually; This quick check covers for the case where
5737 someone does a /DISCARD/ : { *(*) }. */
5738 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5740 bfd_size_type strsize
;
5742 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5743 if ((info
->emit_hash
5744 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5745 || (info
->emit_gnu_hash
5746 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5747 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5748 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5749 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5750 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5751 bed
->s
->sizeof_sym
))
5756 /* The backend must work out the sizes of all the other dynamic
5758 if (bed
->elf_backend_size_dynamic_sections
5759 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5762 if (elf_hash_table (info
)->dynamic_sections_created
)
5764 unsigned long section_sym_count
;
5767 /* Set up the version definition section. */
5768 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5769 BFD_ASSERT (s
!= NULL
);
5771 /* We may have created additional version definitions if we are
5772 just linking a regular application. */
5773 verdefs
= asvinfo
.verdefs
;
5775 /* Skip anonymous version tag. */
5776 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5777 verdefs
= verdefs
->next
;
5779 if (verdefs
== NULL
&& !info
->create_default_symver
)
5780 s
->flags
|= SEC_EXCLUDE
;
5785 struct bfd_elf_version_tree
*t
;
5787 Elf_Internal_Verdef def
;
5788 Elf_Internal_Verdaux defaux
;
5789 struct bfd_link_hash_entry
*bh
;
5790 struct elf_link_hash_entry
*h
;
5796 /* Make space for the base version. */
5797 size
+= sizeof (Elf_External_Verdef
);
5798 size
+= sizeof (Elf_External_Verdaux
);
5801 /* Make space for the default version. */
5802 if (info
->create_default_symver
)
5804 size
+= sizeof (Elf_External_Verdef
);
5808 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5810 struct bfd_elf_version_deps
*n
;
5812 size
+= sizeof (Elf_External_Verdef
);
5813 size
+= sizeof (Elf_External_Verdaux
);
5816 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5817 size
+= sizeof (Elf_External_Verdaux
);
5821 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5822 if (s
->contents
== NULL
&& s
->size
!= 0)
5825 /* Fill in the version definition section. */
5829 def
.vd_version
= VER_DEF_CURRENT
;
5830 def
.vd_flags
= VER_FLG_BASE
;
5833 if (info
->create_default_symver
)
5835 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5836 def
.vd_next
= sizeof (Elf_External_Verdef
);
5840 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5841 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5842 + sizeof (Elf_External_Verdaux
));
5845 if (soname_indx
!= (bfd_size_type
) -1)
5847 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5849 def
.vd_hash
= bfd_elf_hash (soname
);
5850 defaux
.vda_name
= soname_indx
;
5857 name
= lbasename (output_bfd
->filename
);
5858 def
.vd_hash
= bfd_elf_hash (name
);
5859 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5861 if (indx
== (bfd_size_type
) -1)
5863 defaux
.vda_name
= indx
;
5865 defaux
.vda_next
= 0;
5867 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5868 (Elf_External_Verdef
*) p
);
5869 p
+= sizeof (Elf_External_Verdef
);
5870 if (info
->create_default_symver
)
5872 /* Add a symbol representing this version. */
5874 if (! (_bfd_generic_link_add_one_symbol
5875 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5877 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5879 h
= (struct elf_link_hash_entry
*) bh
;
5882 h
->type
= STT_OBJECT
;
5883 h
->verinfo
.vertree
= NULL
;
5885 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5888 /* Create a duplicate of the base version with the same
5889 aux block, but different flags. */
5892 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5894 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5895 + sizeof (Elf_External_Verdaux
));
5898 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5899 (Elf_External_Verdef
*) p
);
5900 p
+= sizeof (Elf_External_Verdef
);
5902 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5903 (Elf_External_Verdaux
*) p
);
5904 p
+= sizeof (Elf_External_Verdaux
);
5906 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5909 struct bfd_elf_version_deps
*n
;
5912 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5915 /* Add a symbol representing this version. */
5917 if (! (_bfd_generic_link_add_one_symbol
5918 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5920 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5922 h
= (struct elf_link_hash_entry
*) bh
;
5925 h
->type
= STT_OBJECT
;
5926 h
->verinfo
.vertree
= t
;
5928 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5931 def
.vd_version
= VER_DEF_CURRENT
;
5933 if (t
->globals
.list
== NULL
5934 && t
->locals
.list
== NULL
5936 def
.vd_flags
|= VER_FLG_WEAK
;
5937 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5938 def
.vd_cnt
= cdeps
+ 1;
5939 def
.vd_hash
= bfd_elf_hash (t
->name
);
5940 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5942 if (t
->next
!= NULL
)
5943 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5944 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5946 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5947 (Elf_External_Verdef
*) p
);
5948 p
+= sizeof (Elf_External_Verdef
);
5950 defaux
.vda_name
= h
->dynstr_index
;
5951 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5953 defaux
.vda_next
= 0;
5954 if (t
->deps
!= NULL
)
5955 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5956 t
->name_indx
= defaux
.vda_name
;
5958 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5959 (Elf_External_Verdaux
*) p
);
5960 p
+= sizeof (Elf_External_Verdaux
);
5962 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5964 if (n
->version_needed
== NULL
)
5966 /* This can happen if there was an error in the
5968 defaux
.vda_name
= 0;
5972 defaux
.vda_name
= n
->version_needed
->name_indx
;
5973 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5976 if (n
->next
== NULL
)
5977 defaux
.vda_next
= 0;
5979 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5981 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5982 (Elf_External_Verdaux
*) p
);
5983 p
+= sizeof (Elf_External_Verdaux
);
5987 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5988 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5991 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5994 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5996 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5999 else if (info
->flags
& DF_BIND_NOW
)
6001 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6007 if (info
->executable
)
6008 info
->flags_1
&= ~ (DF_1_INITFIRST
6011 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6015 /* Work out the size of the version reference section. */
6017 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
6018 BFD_ASSERT (s
!= NULL
);
6020 struct elf_find_verdep_info sinfo
;
6022 sinfo
.output_bfd
= output_bfd
;
6024 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6025 if (sinfo
.vers
== 0)
6027 sinfo
.failed
= FALSE
;
6029 elf_link_hash_traverse (elf_hash_table (info
),
6030 _bfd_elf_link_find_version_dependencies
,
6035 if (elf_tdata (output_bfd
)->verref
== NULL
)
6036 s
->flags
|= SEC_EXCLUDE
;
6039 Elf_Internal_Verneed
*t
;
6044 /* Build the version definition section. */
6047 for (t
= elf_tdata (output_bfd
)->verref
;
6051 Elf_Internal_Vernaux
*a
;
6053 size
+= sizeof (Elf_External_Verneed
);
6055 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6056 size
+= sizeof (Elf_External_Vernaux
);
6060 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6061 if (s
->contents
== NULL
)
6065 for (t
= elf_tdata (output_bfd
)->verref
;
6070 Elf_Internal_Vernaux
*a
;
6074 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6077 t
->vn_version
= VER_NEED_CURRENT
;
6079 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6080 elf_dt_name (t
->vn_bfd
) != NULL
6081 ? elf_dt_name (t
->vn_bfd
)
6082 : lbasename (t
->vn_bfd
->filename
),
6084 if (indx
== (bfd_size_type
) -1)
6087 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6088 if (t
->vn_nextref
== NULL
)
6091 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6092 + caux
* sizeof (Elf_External_Vernaux
));
6094 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6095 (Elf_External_Verneed
*) p
);
6096 p
+= sizeof (Elf_External_Verneed
);
6098 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6100 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6101 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6102 a
->vna_nodename
, FALSE
);
6103 if (indx
== (bfd_size_type
) -1)
6106 if (a
->vna_nextptr
== NULL
)
6109 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6111 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6112 (Elf_External_Vernaux
*) p
);
6113 p
+= sizeof (Elf_External_Vernaux
);
6117 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6118 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6121 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6125 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6126 && elf_tdata (output_bfd
)->cverdefs
== 0)
6127 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6128 §ion_sym_count
) == 0)
6130 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6131 s
->flags
|= SEC_EXCLUDE
;
6137 /* Find the first non-excluded output section. We'll use its
6138 section symbol for some emitted relocs. */
6140 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6144 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6145 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6146 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6148 elf_hash_table (info
)->text_index_section
= s
;
6153 /* Find two non-excluded output sections, one for code, one for data.
6154 We'll use their section symbols for some emitted relocs. */
6156 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6160 /* Data first, since setting text_index_section changes
6161 _bfd_elf_link_omit_section_dynsym. */
6162 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6163 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6164 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6166 elf_hash_table (info
)->data_index_section
= s
;
6170 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6171 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6172 == (SEC_ALLOC
| SEC_READONLY
))
6173 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6175 elf_hash_table (info
)->text_index_section
= s
;
6179 if (elf_hash_table (info
)->text_index_section
== NULL
)
6180 elf_hash_table (info
)->text_index_section
6181 = elf_hash_table (info
)->data_index_section
;
6185 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6187 const struct elf_backend_data
*bed
;
6189 if (!is_elf_hash_table (info
->hash
))
6192 bed
= get_elf_backend_data (output_bfd
);
6193 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6195 if (elf_hash_table (info
)->dynamic_sections_created
)
6199 bfd_size_type dynsymcount
;
6200 unsigned long section_sym_count
;
6201 unsigned int dtagcount
;
6203 dynobj
= elf_hash_table (info
)->dynobj
;
6205 /* Assign dynsym indicies. In a shared library we generate a
6206 section symbol for each output section, which come first.
6207 Next come all of the back-end allocated local dynamic syms,
6208 followed by the rest of the global symbols. */
6210 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6211 §ion_sym_count
);
6213 /* Work out the size of the symbol version section. */
6214 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6215 BFD_ASSERT (s
!= NULL
);
6216 if (dynsymcount
!= 0
6217 && (s
->flags
& SEC_EXCLUDE
) == 0)
6219 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6220 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6221 if (s
->contents
== NULL
)
6224 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6228 /* Set the size of the .dynsym and .hash sections. We counted
6229 the number of dynamic symbols in elf_link_add_object_symbols.
6230 We will build the contents of .dynsym and .hash when we build
6231 the final symbol table, because until then we do not know the
6232 correct value to give the symbols. We built the .dynstr
6233 section as we went along in elf_link_add_object_symbols. */
6234 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6235 BFD_ASSERT (s
!= NULL
);
6236 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6238 if (dynsymcount
!= 0)
6240 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6241 if (s
->contents
== NULL
)
6244 /* The first entry in .dynsym is a dummy symbol.
6245 Clear all the section syms, in case we don't output them all. */
6246 ++section_sym_count
;
6247 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6250 elf_hash_table (info
)->bucketcount
= 0;
6252 /* Compute the size of the hashing table. As a side effect this
6253 computes the hash values for all the names we export. */
6254 if (info
->emit_hash
)
6256 unsigned long int *hashcodes
;
6257 struct hash_codes_info hashinf
;
6259 unsigned long int nsyms
;
6261 size_t hash_entry_size
;
6263 /* Compute the hash values for all exported symbols. At the same
6264 time store the values in an array so that we could use them for
6266 amt
= dynsymcount
* sizeof (unsigned long int);
6267 hashcodes
= bfd_malloc (amt
);
6268 if (hashcodes
== NULL
)
6270 hashinf
.hashcodes
= hashcodes
;
6271 hashinf
.error
= FALSE
;
6273 /* Put all hash values in HASHCODES. */
6274 elf_link_hash_traverse (elf_hash_table (info
),
6275 elf_collect_hash_codes
, &hashinf
);
6282 nsyms
= hashinf
.hashcodes
- hashcodes
;
6284 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6287 if (bucketcount
== 0)
6290 elf_hash_table (info
)->bucketcount
= bucketcount
;
6292 s
= bfd_get_section_by_name (dynobj
, ".hash");
6293 BFD_ASSERT (s
!= NULL
);
6294 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6295 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6296 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6297 if (s
->contents
== NULL
)
6300 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6301 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6302 s
->contents
+ hash_entry_size
);
6305 if (info
->emit_gnu_hash
)
6308 unsigned char *contents
;
6309 struct collect_gnu_hash_codes cinfo
;
6313 memset (&cinfo
, 0, sizeof (cinfo
));
6315 /* Compute the hash values for all exported symbols. At the same
6316 time store the values in an array so that we could use them for
6318 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6319 cinfo
.hashcodes
= bfd_malloc (amt
);
6320 if (cinfo
.hashcodes
== NULL
)
6323 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6324 cinfo
.min_dynindx
= -1;
6325 cinfo
.output_bfd
= output_bfd
;
6328 /* Put all hash values in HASHCODES. */
6329 elf_link_hash_traverse (elf_hash_table (info
),
6330 elf_collect_gnu_hash_codes
, &cinfo
);
6333 free (cinfo
.hashcodes
);
6338 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6340 if (bucketcount
== 0)
6342 free (cinfo
.hashcodes
);
6346 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6347 BFD_ASSERT (s
!= NULL
);
6349 if (cinfo
.nsyms
== 0)
6351 /* Empty .gnu.hash section is special. */
6352 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6353 free (cinfo
.hashcodes
);
6354 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6355 contents
= bfd_zalloc (output_bfd
, s
->size
);
6356 if (contents
== NULL
)
6358 s
->contents
= contents
;
6359 /* 1 empty bucket. */
6360 bfd_put_32 (output_bfd
, 1, contents
);
6361 /* SYMIDX above the special symbol 0. */
6362 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6363 /* Just one word for bitmask. */
6364 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6365 /* Only hash fn bloom filter. */
6366 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6367 /* No hashes are valid - empty bitmask. */
6368 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6369 /* No hashes in the only bucket. */
6370 bfd_put_32 (output_bfd
, 0,
6371 contents
+ 16 + bed
->s
->arch_size
/ 8);
6375 unsigned long int maskwords
, maskbitslog2
;
6376 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6378 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6379 if (maskbitslog2
< 3)
6381 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6382 maskbitslog2
= maskbitslog2
+ 3;
6384 maskbitslog2
= maskbitslog2
+ 2;
6385 if (bed
->s
->arch_size
== 64)
6387 if (maskbitslog2
== 5)
6393 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6394 cinfo
.shift2
= maskbitslog2
;
6395 cinfo
.maskbits
= 1 << maskbitslog2
;
6396 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6397 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6398 amt
+= maskwords
* sizeof (bfd_vma
);
6399 cinfo
.bitmask
= bfd_malloc (amt
);
6400 if (cinfo
.bitmask
== NULL
)
6402 free (cinfo
.hashcodes
);
6406 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6407 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6408 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6409 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6411 /* Determine how often each hash bucket is used. */
6412 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6413 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6414 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6416 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6417 if (cinfo
.counts
[i
] != 0)
6419 cinfo
.indx
[i
] = cnt
;
6420 cnt
+= cinfo
.counts
[i
];
6422 BFD_ASSERT (cnt
== dynsymcount
);
6423 cinfo
.bucketcount
= bucketcount
;
6424 cinfo
.local_indx
= cinfo
.min_dynindx
;
6426 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6427 s
->size
+= cinfo
.maskbits
/ 8;
6428 contents
= bfd_zalloc (output_bfd
, s
->size
);
6429 if (contents
== NULL
)
6431 free (cinfo
.bitmask
);
6432 free (cinfo
.hashcodes
);
6436 s
->contents
= contents
;
6437 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6438 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6439 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6440 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6441 contents
+= 16 + cinfo
.maskbits
/ 8;
6443 for (i
= 0; i
< bucketcount
; ++i
)
6445 if (cinfo
.counts
[i
] == 0)
6446 bfd_put_32 (output_bfd
, 0, contents
);
6448 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6452 cinfo
.contents
= contents
;
6454 /* Renumber dynamic symbols, populate .gnu.hash section. */
6455 elf_link_hash_traverse (elf_hash_table (info
),
6456 elf_renumber_gnu_hash_syms
, &cinfo
);
6458 contents
= s
->contents
+ 16;
6459 for (i
= 0; i
< maskwords
; ++i
)
6461 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6463 contents
+= bed
->s
->arch_size
/ 8;
6466 free (cinfo
.bitmask
);
6467 free (cinfo
.hashcodes
);
6471 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6472 BFD_ASSERT (s
!= NULL
);
6474 elf_finalize_dynstr (output_bfd
, info
);
6476 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6478 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6479 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6486 /* Indicate that we are only retrieving symbol values from this
6490 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
6492 if (is_elf_hash_table (info
->hash
))
6493 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
6494 _bfd_generic_link_just_syms (sec
, info
);
6497 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6500 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6503 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
6504 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
6507 /* Finish SHF_MERGE section merging. */
6510 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6515 if (!is_elf_hash_table (info
->hash
))
6518 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6519 if ((ibfd
->flags
& DYNAMIC
) == 0)
6520 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6521 if ((sec
->flags
& SEC_MERGE
) != 0
6522 && !bfd_is_abs_section (sec
->output_section
))
6524 struct bfd_elf_section_data
*secdata
;
6526 secdata
= elf_section_data (sec
);
6527 if (! _bfd_add_merge_section (abfd
,
6528 &elf_hash_table (info
)->merge_info
,
6529 sec
, &secdata
->sec_info
))
6531 else if (secdata
->sec_info
)
6532 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
6535 if (elf_hash_table (info
)->merge_info
!= NULL
)
6536 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6537 merge_sections_remove_hook
);
6541 /* Create an entry in an ELF linker hash table. */
6543 struct bfd_hash_entry
*
6544 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6545 struct bfd_hash_table
*table
,
6548 /* Allocate the structure if it has not already been allocated by a
6552 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6557 /* Call the allocation method of the superclass. */
6558 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6561 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6562 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6564 /* Set local fields. */
6567 ret
->got
= htab
->init_got_refcount
;
6568 ret
->plt
= htab
->init_plt_refcount
;
6569 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6570 - offsetof (struct elf_link_hash_entry
, size
)));
6571 /* Assume that we have been called by a non-ELF symbol reader.
6572 This flag is then reset by the code which reads an ELF input
6573 file. This ensures that a symbol created by a non-ELF symbol
6574 reader will have the flag set correctly. */
6581 /* Copy data from an indirect symbol to its direct symbol, hiding the
6582 old indirect symbol. Also used for copying flags to a weakdef. */
6585 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6586 struct elf_link_hash_entry
*dir
,
6587 struct elf_link_hash_entry
*ind
)
6589 struct elf_link_hash_table
*htab
;
6591 /* Copy down any references that we may have already seen to the
6592 symbol which just became indirect. */
6594 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6595 dir
->ref_regular
|= ind
->ref_regular
;
6596 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6597 dir
->non_got_ref
|= ind
->non_got_ref
;
6598 dir
->needs_plt
|= ind
->needs_plt
;
6599 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6601 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6604 /* Copy over the global and procedure linkage table refcount entries.
6605 These may have been already set up by a check_relocs routine. */
6606 htab
= elf_hash_table (info
);
6607 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6609 if (dir
->got
.refcount
< 0)
6610 dir
->got
.refcount
= 0;
6611 dir
->got
.refcount
+= ind
->got
.refcount
;
6612 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6615 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6617 if (dir
->plt
.refcount
< 0)
6618 dir
->plt
.refcount
= 0;
6619 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6620 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6623 if (ind
->dynindx
!= -1)
6625 if (dir
->dynindx
!= -1)
6626 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6627 dir
->dynindx
= ind
->dynindx
;
6628 dir
->dynstr_index
= ind
->dynstr_index
;
6630 ind
->dynstr_index
= 0;
6635 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6636 struct elf_link_hash_entry
*h
,
6637 bfd_boolean force_local
)
6639 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6643 h
->forced_local
= 1;
6644 if (h
->dynindx
!= -1)
6647 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6653 /* Initialize an ELF linker hash table. */
6656 _bfd_elf_link_hash_table_init
6657 (struct elf_link_hash_table
*table
,
6659 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6660 struct bfd_hash_table
*,
6662 unsigned int entsize
)
6665 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6667 memset (table
, 0, sizeof * table
);
6668 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6669 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6670 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6671 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6672 /* The first dynamic symbol is a dummy. */
6673 table
->dynsymcount
= 1;
6675 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6676 table
->root
.type
= bfd_link_elf_hash_table
;
6681 /* Create an ELF linker hash table. */
6683 struct bfd_link_hash_table
*
6684 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6686 struct elf_link_hash_table
*ret
;
6687 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6689 ret
= bfd_malloc (amt
);
6693 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6694 sizeof (struct elf_link_hash_entry
)))
6703 /* This is a hook for the ELF emulation code in the generic linker to
6704 tell the backend linker what file name to use for the DT_NEEDED
6705 entry for a dynamic object. */
6708 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6710 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6711 && bfd_get_format (abfd
) == bfd_object
)
6712 elf_dt_name (abfd
) = name
;
6716 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6719 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6720 && bfd_get_format (abfd
) == bfd_object
)
6721 lib_class
= elf_dyn_lib_class (abfd
);
6728 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6730 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6731 && bfd_get_format (abfd
) == bfd_object
)
6732 elf_dyn_lib_class (abfd
) = lib_class
;
6735 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6736 the linker ELF emulation code. */
6738 struct bfd_link_needed_list
*
6739 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6740 struct bfd_link_info
*info
)
6742 if (! is_elf_hash_table (info
->hash
))
6744 return elf_hash_table (info
)->needed
;
6747 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6748 hook for the linker ELF emulation code. */
6750 struct bfd_link_needed_list
*
6751 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6752 struct bfd_link_info
*info
)
6754 if (! is_elf_hash_table (info
->hash
))
6756 return elf_hash_table (info
)->runpath
;
6759 /* Get the name actually used for a dynamic object for a link. This
6760 is the SONAME entry if there is one. Otherwise, it is the string
6761 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6764 bfd_elf_get_dt_soname (bfd
*abfd
)
6766 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6767 && bfd_get_format (abfd
) == bfd_object
)
6768 return elf_dt_name (abfd
);
6772 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6773 the ELF linker emulation code. */
6776 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6777 struct bfd_link_needed_list
**pneeded
)
6780 bfd_byte
*dynbuf
= NULL
;
6781 unsigned int elfsec
;
6782 unsigned long shlink
;
6783 bfd_byte
*extdyn
, *extdynend
;
6785 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
6789 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
6790 || bfd_get_format (abfd
) != bfd_object
)
6793 s
= bfd_get_section_by_name (abfd
, ".dynamic");
6794 if (s
== NULL
|| s
->size
== 0)
6797 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
6800 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
6801 if (elfsec
== SHN_BAD
)
6804 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
6806 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
6807 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
6810 extdynend
= extdyn
+ s
->size
;
6811 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
6813 Elf_Internal_Dyn dyn
;
6815 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
6817 if (dyn
.d_tag
== DT_NULL
)
6820 if (dyn
.d_tag
== DT_NEEDED
)
6823 struct bfd_link_needed_list
*l
;
6824 unsigned int tagv
= dyn
.d_un
.d_val
;
6827 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
6832 l
= bfd_alloc (abfd
, amt
);
6853 struct elf_symbuf_symbol
6855 unsigned long st_name
; /* Symbol name, index in string tbl */
6856 unsigned char st_info
; /* Type and binding attributes */
6857 unsigned char st_other
; /* Visibilty, and target specific */
6860 struct elf_symbuf_head
6862 struct elf_symbuf_symbol
*ssym
;
6863 bfd_size_type count
;
6864 unsigned int st_shndx
;
6871 Elf_Internal_Sym
*isym
;
6872 struct elf_symbuf_symbol
*ssym
;
6877 /* Sort references to symbols by ascending section number. */
6880 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
6882 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
6883 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
6885 return s1
->st_shndx
- s2
->st_shndx
;
6889 elf_sym_name_compare (const void *arg1
, const void *arg2
)
6891 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
6892 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
6893 return strcmp (s1
->name
, s2
->name
);
6896 static struct elf_symbuf_head
*
6897 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
6899 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
6900 struct elf_symbuf_symbol
*ssym
;
6901 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
6902 bfd_size_type i
, shndx_count
, total_size
;
6904 indbuf
= bfd_malloc2 (symcount
, sizeof (*indbuf
));
6908 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
6909 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
6910 *ind
++ = &isymbuf
[i
];
6913 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
6914 elf_sort_elf_symbol
);
6917 if (indbufend
> indbuf
)
6918 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
6919 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
6922 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
6923 + (indbufend
- indbuf
) * sizeof (*ssym
));
6924 ssymbuf
= bfd_malloc (total_size
);
6925 if (ssymbuf
== NULL
)
6931 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
6932 ssymbuf
->ssym
= NULL
;
6933 ssymbuf
->count
= shndx_count
;
6934 ssymbuf
->st_shndx
= 0;
6935 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
6937 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
6940 ssymhead
->ssym
= ssym
;
6941 ssymhead
->count
= 0;
6942 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
6944 ssym
->st_name
= (*ind
)->st_name
;
6945 ssym
->st_info
= (*ind
)->st_info
;
6946 ssym
->st_other
= (*ind
)->st_other
;
6949 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
6950 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
6957 /* Check if 2 sections define the same set of local and global
6961 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
6962 struct bfd_link_info
*info
)
6965 const struct elf_backend_data
*bed1
, *bed2
;
6966 Elf_Internal_Shdr
*hdr1
, *hdr2
;
6967 bfd_size_type symcount1
, symcount2
;
6968 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
6969 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
6970 Elf_Internal_Sym
*isym
, *isymend
;
6971 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
6972 bfd_size_type count1
, count2
, i
;
6973 unsigned int shndx1
, shndx2
;
6979 /* Both sections have to be in ELF. */
6980 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
6981 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
6984 if (elf_section_type (sec1
) != elf_section_type (sec2
))
6987 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
6988 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
6989 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
6992 bed1
= get_elf_backend_data (bfd1
);
6993 bed2
= get_elf_backend_data (bfd2
);
6994 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
6995 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
6996 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
6997 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
6999 if (symcount1
== 0 || symcount2
== 0)
7005 ssymbuf1
= elf_tdata (bfd1
)->symbuf
;
7006 ssymbuf2
= elf_tdata (bfd2
)->symbuf
;
7008 if (ssymbuf1
== NULL
)
7010 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7012 if (isymbuf1
== NULL
)
7015 if (!info
->reduce_memory_overheads
)
7016 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7017 = elf_create_symbuf (symcount1
, isymbuf1
);
7020 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7022 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7024 if (isymbuf2
== NULL
)
7027 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7028 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7029 = elf_create_symbuf (symcount2
, isymbuf2
);
7032 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7034 /* Optimized faster version. */
7035 bfd_size_type lo
, hi
, mid
;
7036 struct elf_symbol
*symp
;
7037 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7040 hi
= ssymbuf1
->count
;
7045 mid
= (lo
+ hi
) / 2;
7046 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7048 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7052 count1
= ssymbuf1
[mid
].count
;
7059 hi
= ssymbuf2
->count
;
7064 mid
= (lo
+ hi
) / 2;
7065 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7067 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7071 count2
= ssymbuf2
[mid
].count
;
7077 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7080 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
7081 symtable2
= bfd_malloc (count2
* sizeof (struct elf_symbol
));
7082 if (symtable1
== NULL
|| symtable2
== NULL
)
7086 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7087 ssym
< ssymend
; ssym
++, symp
++)
7089 symp
->u
.ssym
= ssym
;
7090 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7096 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7097 ssym
< ssymend
; ssym
++, symp
++)
7099 symp
->u
.ssym
= ssym
;
7100 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7105 /* Sort symbol by name. */
7106 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7107 elf_sym_name_compare
);
7108 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7109 elf_sym_name_compare
);
7111 for (i
= 0; i
< count1
; i
++)
7112 /* Two symbols must have the same binding, type and name. */
7113 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7114 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7115 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7122 symtable1
= bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7123 symtable2
= bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7124 if (symtable1
== NULL
|| symtable2
== NULL
)
7127 /* Count definitions in the section. */
7129 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7130 if (isym
->st_shndx
== shndx1
)
7131 symtable1
[count1
++].u
.isym
= isym
;
7134 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7135 if (isym
->st_shndx
== shndx2
)
7136 symtable2
[count2
++].u
.isym
= isym
;
7138 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7141 for (i
= 0; i
< count1
; i
++)
7143 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7144 symtable1
[i
].u
.isym
->st_name
);
7146 for (i
= 0; i
< count2
; i
++)
7148 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7149 symtable2
[i
].u
.isym
->st_name
);
7151 /* Sort symbol by name. */
7152 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7153 elf_sym_name_compare
);
7154 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7155 elf_sym_name_compare
);
7157 for (i
= 0; i
< count1
; i
++)
7158 /* Two symbols must have the same binding, type and name. */
7159 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7160 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7161 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7179 /* Return TRUE if 2 section types are compatible. */
7182 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7183 bfd
*bbfd
, const asection
*bsec
)
7187 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7188 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7191 return elf_section_type (asec
) == elf_section_type (bsec
);
7194 /* Final phase of ELF linker. */
7196 /* A structure we use to avoid passing large numbers of arguments. */
7198 struct elf_final_link_info
7200 /* General link information. */
7201 struct bfd_link_info
*info
;
7204 /* Symbol string table. */
7205 struct bfd_strtab_hash
*symstrtab
;
7206 /* .dynsym section. */
7207 asection
*dynsym_sec
;
7208 /* .hash section. */
7210 /* symbol version section (.gnu.version). */
7211 asection
*symver_sec
;
7212 /* Buffer large enough to hold contents of any section. */
7214 /* Buffer large enough to hold external relocs of any section. */
7215 void *external_relocs
;
7216 /* Buffer large enough to hold internal relocs of any section. */
7217 Elf_Internal_Rela
*internal_relocs
;
7218 /* Buffer large enough to hold external local symbols of any input
7220 bfd_byte
*external_syms
;
7221 /* And a buffer for symbol section indices. */
7222 Elf_External_Sym_Shndx
*locsym_shndx
;
7223 /* Buffer large enough to hold internal local symbols of any input
7225 Elf_Internal_Sym
*internal_syms
;
7226 /* Array large enough to hold a symbol index for each local symbol
7227 of any input BFD. */
7229 /* Array large enough to hold a section pointer for each local
7230 symbol of any input BFD. */
7231 asection
**sections
;
7232 /* Buffer to hold swapped out symbols. */
7234 /* And one for symbol section indices. */
7235 Elf_External_Sym_Shndx
*symshndxbuf
;
7236 /* Number of swapped out symbols in buffer. */
7237 size_t symbuf_count
;
7238 /* Number of symbols which fit in symbuf. */
7240 /* And same for symshndxbuf. */
7241 size_t shndxbuf_size
;
7244 /* This struct is used to pass information to elf_link_output_extsym. */
7246 struct elf_outext_info
7249 bfd_boolean localsyms
;
7250 struct elf_final_link_info
*finfo
;
7254 /* Support for evaluating a complex relocation.
7256 Complex relocations are generalized, self-describing relocations. The
7257 implementation of them consists of two parts: complex symbols, and the
7258 relocations themselves.
7260 The relocations are use a reserved elf-wide relocation type code (R_RELC
7261 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7262 information (start bit, end bit, word width, etc) into the addend. This
7263 information is extracted from CGEN-generated operand tables within gas.
7265 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7266 internal) representing prefix-notation expressions, including but not
7267 limited to those sorts of expressions normally encoded as addends in the
7268 addend field. The symbol mangling format is:
7271 | <unary-operator> ':' <node>
7272 | <binary-operator> ':' <node> ':' <node>
7275 <literal> := 's' <digits=N> ':' <N character symbol name>
7276 | 'S' <digits=N> ':' <N character section name>
7280 <binary-operator> := as in C
7281 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7284 set_symbol_value (bfd
*bfd_with_globals
,
7285 Elf_Internal_Sym
*isymbuf
,
7290 struct elf_link_hash_entry
**sym_hashes
;
7291 struct elf_link_hash_entry
*h
;
7292 size_t extsymoff
= locsymcount
;
7294 if (symidx
< locsymcount
)
7296 Elf_Internal_Sym
*sym
;
7298 sym
= isymbuf
+ symidx
;
7299 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7301 /* It is a local symbol: move it to the
7302 "absolute" section and give it a value. */
7303 sym
->st_shndx
= SHN_ABS
;
7304 sym
->st_value
= val
;
7307 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7311 /* It is a global symbol: set its link type
7312 to "defined" and give it a value. */
7314 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7315 h
= sym_hashes
[symidx
- extsymoff
];
7316 while (h
->root
.type
== bfd_link_hash_indirect
7317 || h
->root
.type
== bfd_link_hash_warning
)
7318 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7319 h
->root
.type
= bfd_link_hash_defined
;
7320 h
->root
.u
.def
.value
= val
;
7321 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7325 resolve_symbol (const char *name
,
7327 struct elf_final_link_info
*finfo
,
7329 Elf_Internal_Sym
*isymbuf
,
7332 Elf_Internal_Sym
*sym
;
7333 struct bfd_link_hash_entry
*global_entry
;
7334 const char *candidate
= NULL
;
7335 Elf_Internal_Shdr
*symtab_hdr
;
7338 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7340 for (i
= 0; i
< locsymcount
; ++ i
)
7344 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7347 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7348 symtab_hdr
->sh_link
,
7351 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7352 name
, candidate
, (unsigned long) sym
->st_value
);
7354 if (candidate
&& strcmp (candidate
, name
) == 0)
7356 asection
*sec
= finfo
->sections
[i
];
7358 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7359 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7361 printf ("Found symbol with value %8.8lx\n",
7362 (unsigned long) *result
);
7368 /* Hmm, haven't found it yet. perhaps it is a global. */
7369 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
,
7370 FALSE
, FALSE
, TRUE
);
7374 if (global_entry
->type
== bfd_link_hash_defined
7375 || global_entry
->type
== bfd_link_hash_defweak
)
7377 *result
= (global_entry
->u
.def
.value
7378 + global_entry
->u
.def
.section
->output_section
->vma
7379 + global_entry
->u
.def
.section
->output_offset
);
7381 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7382 global_entry
->root
.string
, (unsigned long) *result
);
7391 resolve_section (const char *name
,
7398 for (curr
= sections
; curr
; curr
= curr
->next
)
7399 if (strcmp (curr
->name
, name
) == 0)
7401 *result
= curr
->vma
;
7405 /* Hmm. still haven't found it. try pseudo-section names. */
7406 for (curr
= sections
; curr
; curr
= curr
->next
)
7408 len
= strlen (curr
->name
);
7409 if (len
> strlen (name
))
7412 if (strncmp (curr
->name
, name
, len
) == 0)
7414 if (strncmp (".end", name
+ len
, 4) == 0)
7416 *result
= curr
->vma
+ curr
->size
;
7420 /* Insert more pseudo-section names here, if you like. */
7428 undefined_reference (const char *reftype
, const char *name
)
7430 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7435 eval_symbol (bfd_vma
*result
,
7438 struct elf_final_link_info
*finfo
,
7440 Elf_Internal_Sym
*isymbuf
,
7449 const char *sym
= *symp
;
7451 bfd_boolean symbol_is_section
= FALSE
;
7456 if (len
< 1 || len
> sizeof (symbuf
))
7458 bfd_set_error (bfd_error_invalid_operation
);
7471 *result
= strtoul (sym
, (char **) symp
, 16);
7475 symbol_is_section
= TRUE
;
7478 symlen
= strtol (sym
, (char **) symp
, 10);
7479 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7481 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7483 bfd_set_error (bfd_error_invalid_operation
);
7487 memcpy (symbuf
, sym
, symlen
);
7488 symbuf
[symlen
] = '\0';
7489 *symp
= sym
+ symlen
;
7491 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7492 the symbol as a section, or vice-versa. so we're pretty liberal in our
7493 interpretation here; section means "try section first", not "must be a
7494 section", and likewise with symbol. */
7496 if (symbol_is_section
)
7498 if (!resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
)
7499 && !resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7500 isymbuf
, locsymcount
))
7502 undefined_reference ("section", symbuf
);
7508 if (!resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7509 isymbuf
, locsymcount
)
7510 && !resolve_section (symbuf
, finfo
->output_bfd
->sections
,
7513 undefined_reference ("symbol", symbuf
);
7520 /* All that remains are operators. */
7522 #define UNARY_OP(op) \
7523 if (strncmp (sym, #op, strlen (#op)) == 0) \
7525 sym += strlen (#op); \
7529 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7530 isymbuf, locsymcount, signed_p)) \
7533 *result = op ((bfd_signed_vma) a); \
7539 #define BINARY_OP(op) \
7540 if (strncmp (sym, #op, strlen (#op)) == 0) \
7542 sym += strlen (#op); \
7546 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7547 isymbuf, locsymcount, signed_p)) \
7550 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7551 isymbuf, locsymcount, signed_p)) \
7554 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7584 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7585 bfd_set_error (bfd_error_invalid_operation
);
7591 put_value (bfd_vma size
,
7592 unsigned long chunksz
,
7597 location
+= (size
- chunksz
);
7599 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7607 bfd_put_8 (input_bfd
, x
, location
);
7610 bfd_put_16 (input_bfd
, x
, location
);
7613 bfd_put_32 (input_bfd
, x
, location
);
7617 bfd_put_64 (input_bfd
, x
, location
);
7627 get_value (bfd_vma size
,
7628 unsigned long chunksz
,
7634 for (; size
; size
-= chunksz
, location
+= chunksz
)
7642 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7645 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7648 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7652 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7663 decode_complex_addend (unsigned long *start
, /* in bits */
7664 unsigned long *oplen
, /* in bits */
7665 unsigned long *len
, /* in bits */
7666 unsigned long *wordsz
, /* in bytes */
7667 unsigned long *chunksz
, /* in bytes */
7668 unsigned long *lsb0_p
,
7669 unsigned long *signed_p
,
7670 unsigned long *trunc_p
,
7671 unsigned long encoded
)
7673 * start
= encoded
& 0x3F;
7674 * len
= (encoded
>> 6) & 0x3F;
7675 * oplen
= (encoded
>> 12) & 0x3F;
7676 * wordsz
= (encoded
>> 18) & 0xF;
7677 * chunksz
= (encoded
>> 22) & 0xF;
7678 * lsb0_p
= (encoded
>> 27) & 1;
7679 * signed_p
= (encoded
>> 28) & 1;
7680 * trunc_p
= (encoded
>> 29) & 1;
7683 bfd_reloc_status_type
7684 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
7685 asection
*input_section ATTRIBUTE_UNUSED
,
7687 Elf_Internal_Rela
*rel
,
7690 bfd_vma shift
, x
, mask
;
7691 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
7692 bfd_reloc_status_type r
;
7694 /* Perform this reloc, since it is complex.
7695 (this is not to say that it necessarily refers to a complex
7696 symbol; merely that it is a self-describing CGEN based reloc.
7697 i.e. the addend has the complete reloc information (bit start, end,
7698 word size, etc) encoded within it.). */
7700 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
7701 &chunksz
, &lsb0_p
, &signed_p
,
7702 &trunc_p
, rel
->r_addend
);
7704 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7707 shift
= (start
+ 1) - len
;
7709 shift
= (8 * wordsz
) - (start
+ len
);
7711 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7714 printf ("Doing complex reloc: "
7715 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7716 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7717 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7718 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7719 oplen
, x
, mask
, relocation
);
7724 /* Now do an overflow check. */
7725 r
= bfd_check_overflow ((signed_p
7726 ? complain_overflow_signed
7727 : complain_overflow_unsigned
),
7728 len
, 0, (8 * wordsz
),
7732 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7735 printf (" relocation: %8.8lx\n"
7736 " shifted mask: %8.8lx\n"
7737 " shifted/masked reloc: %8.8lx\n"
7738 " result: %8.8lx\n",
7739 relocation
, (mask
<< shift
),
7740 ((relocation
& mask
) << shift
), x
);
7742 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7746 /* When performing a relocatable link, the input relocations are
7747 preserved. But, if they reference global symbols, the indices
7748 referenced must be updated. Update all the relocations in
7749 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7752 elf_link_adjust_relocs (bfd
*abfd
,
7753 Elf_Internal_Shdr
*rel_hdr
,
7755 struct elf_link_hash_entry
**rel_hash
)
7758 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7760 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7761 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7762 bfd_vma r_type_mask
;
7765 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7767 swap_in
= bed
->s
->swap_reloc_in
;
7768 swap_out
= bed
->s
->swap_reloc_out
;
7770 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7772 swap_in
= bed
->s
->swap_reloca_in
;
7773 swap_out
= bed
->s
->swap_reloca_out
;
7778 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7781 if (bed
->s
->arch_size
== 32)
7788 r_type_mask
= 0xffffffff;
7792 erela
= rel_hdr
->contents
;
7793 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7795 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7798 if (*rel_hash
== NULL
)
7801 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7803 (*swap_in
) (abfd
, erela
, irela
);
7804 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7805 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7806 | (irela
[j
].r_info
& r_type_mask
));
7807 (*swap_out
) (abfd
, irela
, erela
);
7811 struct elf_link_sort_rela
7817 enum elf_reloc_type_class type
;
7818 /* We use this as an array of size int_rels_per_ext_rel. */
7819 Elf_Internal_Rela rela
[1];
7823 elf_link_sort_cmp1 (const void *A
, const void *B
)
7825 const struct elf_link_sort_rela
*a
= A
;
7826 const struct elf_link_sort_rela
*b
= B
;
7827 int relativea
, relativeb
;
7829 relativea
= a
->type
== reloc_class_relative
;
7830 relativeb
= b
->type
== reloc_class_relative
;
7832 if (relativea
< relativeb
)
7834 if (relativea
> relativeb
)
7836 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7838 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7840 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7842 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7848 elf_link_sort_cmp2 (const void *A
, const void *B
)
7850 const struct elf_link_sort_rela
*a
= A
;
7851 const struct elf_link_sort_rela
*b
= B
;
7854 if (a
->u
.offset
< b
->u
.offset
)
7856 if (a
->u
.offset
> b
->u
.offset
)
7858 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7859 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7864 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7866 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7872 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7874 asection
*dynamic_relocs
;
7877 bfd_size_type count
, size
;
7878 size_t i
, ret
, sort_elt
, ext_size
;
7879 bfd_byte
*sort
, *s_non_relative
, *p
;
7880 struct elf_link_sort_rela
*sq
;
7881 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7882 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7883 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7884 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7885 struct bfd_link_order
*lo
;
7887 bfd_boolean use_rela
;
7889 /* Find a dynamic reloc section. */
7890 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7891 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7892 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
7893 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7895 bfd_boolean use_rela_initialised
= FALSE
;
7897 /* This is just here to stop gcc from complaining.
7898 It's initialization checking code is not perfect. */
7901 /* Both sections are present. Examine the sizes
7902 of the indirect sections to help us choose. */
7903 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7904 if (lo
->type
== bfd_indirect_link_order
)
7906 asection
*o
= lo
->u
.indirect
.section
;
7908 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7910 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7911 /* Section size is divisible by both rel and rela sizes.
7912 It is of no help to us. */
7916 /* Section size is only divisible by rela. */
7917 if (use_rela_initialised
&& (use_rela
== FALSE
))
7920 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7921 bfd_set_error (bfd_error_invalid_operation
);
7927 use_rela_initialised
= TRUE
;
7931 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7933 /* Section size is only divisible by rel. */
7934 if (use_rela_initialised
&& (use_rela
== TRUE
))
7937 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7938 bfd_set_error (bfd_error_invalid_operation
);
7944 use_rela_initialised
= TRUE
;
7949 /* The section size is not divisible by either - something is wrong. */
7951 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7952 bfd_set_error (bfd_error_invalid_operation
);
7957 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7958 if (lo
->type
== bfd_indirect_link_order
)
7960 asection
*o
= lo
->u
.indirect
.section
;
7962 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7964 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7965 /* Section size is divisible by both rel and rela sizes.
7966 It is of no help to us. */
7970 /* Section size is only divisible by rela. */
7971 if (use_rela_initialised
&& (use_rela
== FALSE
))
7974 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7975 bfd_set_error (bfd_error_invalid_operation
);
7981 use_rela_initialised
= TRUE
;
7985 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7987 /* Section size is only divisible by rel. */
7988 if (use_rela_initialised
&& (use_rela
== TRUE
))
7991 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7992 bfd_set_error (bfd_error_invalid_operation
);
7998 use_rela_initialised
= TRUE
;
8003 /* The section size is not divisible by either - something is wrong. */
8005 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8006 bfd_set_error (bfd_error_invalid_operation
);
8011 if (! use_rela_initialised
)
8015 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8017 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8024 dynamic_relocs
= rela_dyn
;
8025 ext_size
= bed
->s
->sizeof_rela
;
8026 swap_in
= bed
->s
->swap_reloca_in
;
8027 swap_out
= bed
->s
->swap_reloca_out
;
8031 dynamic_relocs
= rel_dyn
;
8032 ext_size
= bed
->s
->sizeof_rel
;
8033 swap_in
= bed
->s
->swap_reloc_in
;
8034 swap_out
= bed
->s
->swap_reloc_out
;
8038 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8039 if (lo
->type
== bfd_indirect_link_order
)
8040 size
+= lo
->u
.indirect
.section
->size
;
8042 if (size
!= dynamic_relocs
->size
)
8045 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8046 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8048 count
= dynamic_relocs
->size
/ ext_size
;
8049 sort
= bfd_zmalloc (sort_elt
* count
);
8053 (*info
->callbacks
->warning
)
8054 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8058 if (bed
->s
->arch_size
== 32)
8059 r_sym_mask
= ~(bfd_vma
) 0xff;
8061 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8063 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8064 if (lo
->type
== bfd_indirect_link_order
)
8066 bfd_byte
*erel
, *erelend
;
8067 asection
*o
= lo
->u
.indirect
.section
;
8069 if (o
->contents
== NULL
&& o
->size
!= 0)
8071 /* This is a reloc section that is being handled as a normal
8072 section. See bfd_section_from_shdr. We can't combine
8073 relocs in this case. */
8078 erelend
= o
->contents
+ o
->size
;
8079 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8081 while (erel
< erelend
)
8083 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8085 (*swap_in
) (abfd
, erel
, s
->rela
);
8086 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8087 s
->u
.sym_mask
= r_sym_mask
;
8093 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8095 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8097 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8098 if (s
->type
!= reloc_class_relative
)
8104 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8105 for (; i
< count
; i
++, p
+= sort_elt
)
8107 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8108 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8110 sp
->u
.offset
= sq
->rela
->r_offset
;
8113 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8115 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8116 if (lo
->type
== bfd_indirect_link_order
)
8118 bfd_byte
*erel
, *erelend
;
8119 asection
*o
= lo
->u
.indirect
.section
;
8122 erelend
= o
->contents
+ o
->size
;
8123 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8124 while (erel
< erelend
)
8126 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8127 (*swap_out
) (abfd
, s
->rela
, erel
);
8134 *psec
= dynamic_relocs
;
8138 /* Flush the output symbols to the file. */
8141 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
8142 const struct elf_backend_data
*bed
)
8144 if (finfo
->symbuf_count
> 0)
8146 Elf_Internal_Shdr
*hdr
;
8150 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
8151 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8152 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8153 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
8154 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
8157 hdr
->sh_size
+= amt
;
8158 finfo
->symbuf_count
= 0;
8164 /* Add a symbol to the output symbol table. */
8167 elf_link_output_sym (struct elf_final_link_info
*finfo
,
8169 Elf_Internal_Sym
*elfsym
,
8170 asection
*input_sec
,
8171 struct elf_link_hash_entry
*h
)
8174 Elf_External_Sym_Shndx
*destshndx
;
8175 bfd_boolean (*output_symbol_hook
)
8176 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8177 struct elf_link_hash_entry
*);
8178 const struct elf_backend_data
*bed
;
8180 bed
= get_elf_backend_data (finfo
->output_bfd
);
8181 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8182 if (output_symbol_hook
!= NULL
)
8184 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
8188 if (name
== NULL
|| *name
== '\0')
8189 elfsym
->st_name
= 0;
8190 else if (input_sec
->flags
& SEC_EXCLUDE
)
8191 elfsym
->st_name
= 0;
8194 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
8196 if (elfsym
->st_name
== (unsigned long) -1)
8200 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
8202 if (! elf_link_flush_output_syms (finfo
, bed
))
8206 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8207 destshndx
= finfo
->symshndxbuf
;
8208 if (destshndx
!= NULL
)
8210 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
8214 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8215 destshndx
= bfd_realloc (destshndx
, amt
* 2);
8216 if (destshndx
== NULL
)
8218 finfo
->symshndxbuf
= destshndx
;
8219 memset ((char *) destshndx
+ amt
, 0, amt
);
8220 finfo
->shndxbuf_size
*= 2;
8222 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
8225 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
8226 finfo
->symbuf_count
+= 1;
8227 bfd_get_symcount (finfo
->output_bfd
) += 1;
8232 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8235 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8237 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
8238 && sym
->st_shndx
< SHN_LORESERVE
)
8240 /* The gABI doesn't support dynamic symbols in output sections
8242 (*_bfd_error_handler
)
8243 (_("%B: Too many sections: %d (>= %d)"),
8244 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
8245 bfd_set_error (bfd_error_nonrepresentable_section
);
8251 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8252 allowing an unsatisfied unversioned symbol in the DSO to match a
8253 versioned symbol that would normally require an explicit version.
8254 We also handle the case that a DSO references a hidden symbol
8255 which may be satisfied by a versioned symbol in another DSO. */
8258 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8259 const struct elf_backend_data
*bed
,
8260 struct elf_link_hash_entry
*h
)
8263 struct elf_link_loaded_list
*loaded
;
8265 if (!is_elf_hash_table (info
->hash
))
8268 switch (h
->root
.type
)
8274 case bfd_link_hash_undefined
:
8275 case bfd_link_hash_undefweak
:
8276 abfd
= h
->root
.u
.undef
.abfd
;
8277 if ((abfd
->flags
& DYNAMIC
) == 0
8278 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8282 case bfd_link_hash_defined
:
8283 case bfd_link_hash_defweak
:
8284 abfd
= h
->root
.u
.def
.section
->owner
;
8287 case bfd_link_hash_common
:
8288 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8291 BFD_ASSERT (abfd
!= NULL
);
8293 for (loaded
= elf_hash_table (info
)->loaded
;
8295 loaded
= loaded
->next
)
8298 Elf_Internal_Shdr
*hdr
;
8299 bfd_size_type symcount
;
8300 bfd_size_type extsymcount
;
8301 bfd_size_type extsymoff
;
8302 Elf_Internal_Shdr
*versymhdr
;
8303 Elf_Internal_Sym
*isym
;
8304 Elf_Internal_Sym
*isymend
;
8305 Elf_Internal_Sym
*isymbuf
;
8306 Elf_External_Versym
*ever
;
8307 Elf_External_Versym
*extversym
;
8309 input
= loaded
->abfd
;
8311 /* We check each DSO for a possible hidden versioned definition. */
8313 || (input
->flags
& DYNAMIC
) == 0
8314 || elf_dynversym (input
) == 0)
8317 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8319 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8320 if (elf_bad_symtab (input
))
8322 extsymcount
= symcount
;
8327 extsymcount
= symcount
- hdr
->sh_info
;
8328 extsymoff
= hdr
->sh_info
;
8331 if (extsymcount
== 0)
8334 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8336 if (isymbuf
== NULL
)
8339 /* Read in any version definitions. */
8340 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8341 extversym
= bfd_malloc (versymhdr
->sh_size
);
8342 if (extversym
== NULL
)
8345 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8346 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8347 != versymhdr
->sh_size
))
8355 ever
= extversym
+ extsymoff
;
8356 isymend
= isymbuf
+ extsymcount
;
8357 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8360 Elf_Internal_Versym iver
;
8361 unsigned short version_index
;
8363 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8364 || isym
->st_shndx
== SHN_UNDEF
)
8367 name
= bfd_elf_string_from_elf_section (input
,
8370 if (strcmp (name
, h
->root
.root
.string
) != 0)
8373 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8375 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
8377 /* If we have a non-hidden versioned sym, then it should
8378 have provided a definition for the undefined sym. */
8382 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8383 if (version_index
== 1 || version_index
== 2)
8385 /* This is the base or first version. We can use it. */
8399 /* Add an external symbol to the symbol table. This is called from
8400 the hash table traversal routine. When generating a shared object,
8401 we go through the symbol table twice. The first time we output
8402 anything that might have been forced to local scope in a version
8403 script. The second time we output the symbols that are still
8407 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
8409 struct elf_outext_info
*eoinfo
= data
;
8410 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
8412 Elf_Internal_Sym sym
;
8413 asection
*input_sec
;
8414 const struct elf_backend_data
*bed
;
8416 if (h
->root
.type
== bfd_link_hash_warning
)
8418 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8419 if (h
->root
.type
== bfd_link_hash_new
)
8423 /* Decide whether to output this symbol in this pass. */
8424 if (eoinfo
->localsyms
)
8426 if (!h
->forced_local
)
8431 if (h
->forced_local
)
8435 bed
= get_elf_backend_data (finfo
->output_bfd
);
8437 if (h
->root
.type
== bfd_link_hash_undefined
)
8439 /* If we have an undefined symbol reference here then it must have
8440 come from a shared library that is being linked in. (Undefined
8441 references in regular files have already been handled). */
8442 bfd_boolean ignore_undef
= FALSE
;
8444 /* Some symbols may be special in that the fact that they're
8445 undefined can be safely ignored - let backend determine that. */
8446 if (bed
->elf_backend_ignore_undef_symbol
)
8447 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8449 /* If we are reporting errors for this situation then do so now. */
8450 if (ignore_undef
== FALSE
8453 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
8454 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8456 if (! (finfo
->info
->callbacks
->undefined_symbol
8457 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
8458 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
8460 eoinfo
->failed
= TRUE
;
8466 /* We should also warn if a forced local symbol is referenced from
8467 shared libraries. */
8468 if (! finfo
->info
->relocatable
8469 && (! finfo
->info
->shared
)
8474 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
8476 (*_bfd_error_handler
)
8477 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8479 h
->root
.u
.def
.section
== bfd_abs_section_ptr
8480 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
8481 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
8483 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
8484 ? "hidden" : "local",
8485 h
->root
.root
.string
);
8486 eoinfo
->failed
= TRUE
;
8490 /* We don't want to output symbols that have never been mentioned by
8491 a regular file, or that we have been told to strip. However, if
8492 h->indx is set to -2, the symbol is used by a reloc and we must
8496 else if ((h
->def_dynamic
8498 || h
->root
.type
== bfd_link_hash_new
)
8502 else if (finfo
->info
->strip
== strip_all
)
8504 else if (finfo
->info
->strip
== strip_some
8505 && bfd_hash_lookup (finfo
->info
->keep_hash
,
8506 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8508 else if (finfo
->info
->strip_discarded
8509 && (h
->root
.type
== bfd_link_hash_defined
8510 || h
->root
.type
== bfd_link_hash_defweak
)
8511 && elf_discarded_section (h
->root
.u
.def
.section
))
8516 /* If we're stripping it, and it's not a dynamic symbol, there's
8517 nothing else to do unless it is a forced local symbol. */
8520 && !h
->forced_local
)
8524 sym
.st_size
= h
->size
;
8525 sym
.st_other
= h
->other
;
8526 if (h
->forced_local
)
8527 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8528 else if (h
->root
.type
== bfd_link_hash_undefweak
8529 || h
->root
.type
== bfd_link_hash_defweak
)
8530 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8532 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8534 switch (h
->root
.type
)
8537 case bfd_link_hash_new
:
8538 case bfd_link_hash_warning
:
8542 case bfd_link_hash_undefined
:
8543 case bfd_link_hash_undefweak
:
8544 input_sec
= bfd_und_section_ptr
;
8545 sym
.st_shndx
= SHN_UNDEF
;
8548 case bfd_link_hash_defined
:
8549 case bfd_link_hash_defweak
:
8551 input_sec
= h
->root
.u
.def
.section
;
8552 if (input_sec
->output_section
!= NULL
)
8555 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
8556 input_sec
->output_section
);
8557 if (sym
.st_shndx
== SHN_BAD
)
8559 (*_bfd_error_handler
)
8560 (_("%B: could not find output section %A for input section %A"),
8561 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8562 eoinfo
->failed
= TRUE
;
8566 /* ELF symbols in relocatable files are section relative,
8567 but in nonrelocatable files they are virtual
8569 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8570 if (! finfo
->info
->relocatable
)
8572 sym
.st_value
+= input_sec
->output_section
->vma
;
8573 if (h
->type
== STT_TLS
)
8575 asection
*tls_sec
= elf_hash_table (finfo
->info
)->tls_sec
;
8576 if (tls_sec
!= NULL
)
8577 sym
.st_value
-= tls_sec
->vma
;
8580 /* The TLS section may have been garbage collected. */
8581 BFD_ASSERT (finfo
->info
->gc_sections
8582 && !input_sec
->gc_mark
);
8589 BFD_ASSERT (input_sec
->owner
== NULL
8590 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8591 sym
.st_shndx
= SHN_UNDEF
;
8592 input_sec
= bfd_und_section_ptr
;
8597 case bfd_link_hash_common
:
8598 input_sec
= h
->root
.u
.c
.p
->section
;
8599 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8600 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8603 case bfd_link_hash_indirect
:
8604 /* These symbols are created by symbol versioning. They point
8605 to the decorated version of the name. For example, if the
8606 symbol foo@@GNU_1.2 is the default, which should be used when
8607 foo is used with no version, then we add an indirect symbol
8608 foo which points to foo@@GNU_1.2. We ignore these symbols,
8609 since the indirected symbol is already in the hash table. */
8613 /* Give the processor backend a chance to tweak the symbol value,
8614 and also to finish up anything that needs to be done for this
8615 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8616 forced local syms when non-shared is due to a historical quirk. */
8617 if ((h
->dynindx
!= -1
8619 && ((finfo
->info
->shared
8620 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8621 || h
->root
.type
!= bfd_link_hash_undefweak
))
8622 || !h
->forced_local
)
8623 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8625 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8626 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8628 eoinfo
->failed
= TRUE
;
8633 /* If we are marking the symbol as undefined, and there are no
8634 non-weak references to this symbol from a regular object, then
8635 mark the symbol as weak undefined; if there are non-weak
8636 references, mark the symbol as strong. We can't do this earlier,
8637 because it might not be marked as undefined until the
8638 finish_dynamic_symbol routine gets through with it. */
8639 if (sym
.st_shndx
== SHN_UNDEF
8641 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8642 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8646 if (h
->ref_regular_nonweak
)
8647 bindtype
= STB_GLOBAL
;
8649 bindtype
= STB_WEAK
;
8650 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8653 /* If this is a symbol defined in a dynamic library, don't use the
8654 symbol size from the dynamic library. Relinking an executable
8655 against a new library may introduce gratuitous changes in the
8656 executable's symbols if we keep the size. */
8657 if (sym
.st_shndx
== SHN_UNDEF
8662 /* If a non-weak symbol with non-default visibility is not defined
8663 locally, it is a fatal error. */
8664 if (! finfo
->info
->relocatable
8665 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8666 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8667 && h
->root
.type
== bfd_link_hash_undefined
8670 (*_bfd_error_handler
)
8671 (_("%B: %s symbol `%s' isn't defined"),
8673 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8675 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8676 ? "internal" : "hidden",
8677 h
->root
.root
.string
);
8678 eoinfo
->failed
= TRUE
;
8682 /* If this symbol should be put in the .dynsym section, then put it
8683 there now. We already know the symbol index. We also fill in
8684 the entry in the .hash section. */
8685 if (h
->dynindx
!= -1
8686 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8690 sym
.st_name
= h
->dynstr_index
;
8691 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8692 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8694 eoinfo
->failed
= TRUE
;
8697 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8699 if (finfo
->hash_sec
!= NULL
)
8701 size_t hash_entry_size
;
8702 bfd_byte
*bucketpos
;
8707 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8708 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8711 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8712 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8713 + (bucket
+ 2) * hash_entry_size
);
8714 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8715 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8716 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8717 ((bfd_byte
*) finfo
->hash_sec
->contents
8718 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8721 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8723 Elf_Internal_Versym iversym
;
8724 Elf_External_Versym
*eversym
;
8726 if (!h
->def_regular
)
8728 if (h
->verinfo
.verdef
== NULL
)
8729 iversym
.vs_vers
= 0;
8731 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8735 if (h
->verinfo
.vertree
== NULL
)
8736 iversym
.vs_vers
= 1;
8738 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8739 if (finfo
->info
->create_default_symver
)
8744 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8746 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8747 eversym
+= h
->dynindx
;
8748 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8752 /* If we're stripping it, then it was just a dynamic symbol, and
8753 there's nothing else to do. */
8754 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8757 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8759 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8761 eoinfo
->failed
= TRUE
;
8768 /* Return TRUE if special handling is done for relocs in SEC against
8769 symbols defined in discarded sections. */
8772 elf_section_ignore_discarded_relocs (asection
*sec
)
8774 const struct elf_backend_data
*bed
;
8776 switch (sec
->sec_info_type
)
8778 case ELF_INFO_TYPE_STABS
:
8779 case ELF_INFO_TYPE_EH_FRAME
:
8785 bed
= get_elf_backend_data (sec
->owner
);
8786 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8787 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8793 /* Return a mask saying how ld should treat relocations in SEC against
8794 symbols defined in discarded sections. If this function returns
8795 COMPLAIN set, ld will issue a warning message. If this function
8796 returns PRETEND set, and the discarded section was link-once and the
8797 same size as the kept link-once section, ld will pretend that the
8798 symbol was actually defined in the kept section. Otherwise ld will
8799 zero the reloc (at least that is the intent, but some cooperation by
8800 the target dependent code is needed, particularly for REL targets). */
8803 _bfd_elf_default_action_discarded (asection
*sec
)
8805 if (sec
->flags
& SEC_DEBUGGING
)
8808 if (strcmp (".eh_frame", sec
->name
) == 0)
8811 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8814 return COMPLAIN
| PRETEND
;
8817 /* Find a match between a section and a member of a section group. */
8820 match_group_member (asection
*sec
, asection
*group
,
8821 struct bfd_link_info
*info
)
8823 asection
*first
= elf_next_in_group (group
);
8824 asection
*s
= first
;
8828 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8831 s
= elf_next_in_group (s
);
8839 /* Check if the kept section of a discarded section SEC can be used
8840 to replace it. Return the replacement if it is OK. Otherwise return
8844 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8848 kept
= sec
->kept_section
;
8851 if ((kept
->flags
& SEC_GROUP
) != 0)
8852 kept
= match_group_member (sec
, kept
, info
);
8854 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
8855 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
8857 sec
->kept_section
= kept
;
8862 /* Link an input file into the linker output file. This function
8863 handles all the sections and relocations of the input file at once.
8864 This is so that we only have to read the local symbols once, and
8865 don't have to keep them in memory. */
8868 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8870 int (*relocate_section
)
8871 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8872 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8874 Elf_Internal_Shdr
*symtab_hdr
;
8877 Elf_Internal_Sym
*isymbuf
;
8878 Elf_Internal_Sym
*isym
;
8879 Elf_Internal_Sym
*isymend
;
8881 asection
**ppsection
;
8883 const struct elf_backend_data
*bed
;
8884 struct elf_link_hash_entry
**sym_hashes
;
8886 output_bfd
= finfo
->output_bfd
;
8887 bed
= get_elf_backend_data (output_bfd
);
8888 relocate_section
= bed
->elf_backend_relocate_section
;
8890 /* If this is a dynamic object, we don't want to do anything here:
8891 we don't want the local symbols, and we don't want the section
8893 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8896 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8897 if (elf_bad_symtab (input_bfd
))
8899 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8904 locsymcount
= symtab_hdr
->sh_info
;
8905 extsymoff
= symtab_hdr
->sh_info
;
8908 /* Read the local symbols. */
8909 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8910 if (isymbuf
== NULL
&& locsymcount
!= 0)
8912 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8913 finfo
->internal_syms
,
8914 finfo
->external_syms
,
8915 finfo
->locsym_shndx
);
8916 if (isymbuf
== NULL
)
8920 /* Find local symbol sections and adjust values of symbols in
8921 SEC_MERGE sections. Write out those local symbols we know are
8922 going into the output file. */
8923 isymend
= isymbuf
+ locsymcount
;
8924 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8926 isym
++, pindex
++, ppsection
++)
8930 Elf_Internal_Sym osym
;
8934 if (elf_bad_symtab (input_bfd
))
8936 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8943 if (isym
->st_shndx
== SHN_UNDEF
)
8944 isec
= bfd_und_section_ptr
;
8945 else if (isym
->st_shndx
== SHN_ABS
)
8946 isec
= bfd_abs_section_ptr
;
8947 else if (isym
->st_shndx
== SHN_COMMON
)
8948 isec
= bfd_com_section_ptr
;
8951 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8954 /* Don't attempt to output symbols with st_shnx in the
8955 reserved range other than SHN_ABS and SHN_COMMON. */
8959 else if (isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8960 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8962 _bfd_merged_section_offset (output_bfd
, &isec
,
8963 elf_section_data (isec
)->sec_info
,
8969 /* Don't output the first, undefined, symbol. */
8970 if (ppsection
== finfo
->sections
)
8973 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8975 /* We never output section symbols. Instead, we use the
8976 section symbol of the corresponding section in the output
8981 /* If we are stripping all symbols, we don't want to output this
8983 if (finfo
->info
->strip
== strip_all
)
8986 /* If we are discarding all local symbols, we don't want to
8987 output this one. If we are generating a relocatable output
8988 file, then some of the local symbols may be required by
8989 relocs; we output them below as we discover that they are
8991 if (finfo
->info
->discard
== discard_all
)
8994 /* If this symbol is defined in a section which we are
8995 discarding, we don't need to keep it. */
8996 if (isym
->st_shndx
!= SHN_UNDEF
8997 && isym
->st_shndx
< SHN_LORESERVE
8998 && bfd_section_removed_from_list (output_bfd
,
8999 isec
->output_section
))
9002 /* Get the name of the symbol. */
9003 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9008 /* See if we are discarding symbols with this name. */
9009 if ((finfo
->info
->strip
== strip_some
9010 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9012 || (((finfo
->info
->discard
== discard_sec_merge
9013 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
9014 || finfo
->info
->discard
== discard_l
)
9015 && bfd_is_local_label_name (input_bfd
, name
)))
9018 /* If we get here, we are going to output this symbol. */
9022 /* Adjust the section index for the output file. */
9023 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9024 isec
->output_section
);
9025 if (osym
.st_shndx
== SHN_BAD
)
9028 *pindex
= bfd_get_symcount (output_bfd
);
9030 /* ELF symbols in relocatable files are section relative, but
9031 in executable files they are virtual addresses. Note that
9032 this code assumes that all ELF sections have an associated
9033 BFD section with a reasonable value for output_offset; below
9034 we assume that they also have a reasonable value for
9035 output_section. Any special sections must be set up to meet
9036 these requirements. */
9037 osym
.st_value
+= isec
->output_offset
;
9038 if (! finfo
->info
->relocatable
)
9040 osym
.st_value
+= isec
->output_section
->vma
;
9041 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9043 /* STT_TLS symbols are relative to PT_TLS segment base. */
9044 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
9045 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
9049 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
9053 /* Relocate the contents of each section. */
9054 sym_hashes
= elf_sym_hashes (input_bfd
);
9055 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9059 if (! o
->linker_mark
)
9061 /* This section was omitted from the link. */
9065 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9066 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9069 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9071 /* Section was created by _bfd_elf_link_create_dynamic_sections
9076 /* Get the contents of the section. They have been cached by a
9077 relaxation routine. Note that o is a section in an input
9078 file, so the contents field will not have been set by any of
9079 the routines which work on output files. */
9080 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9081 contents
= elf_section_data (o
)->this_hdr
.contents
;
9084 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
9086 contents
= finfo
->contents
;
9087 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
9091 if ((o
->flags
& SEC_RELOC
) != 0)
9093 Elf_Internal_Rela
*internal_relocs
;
9094 Elf_Internal_Rela
*rel
, *relend
;
9095 bfd_vma r_type_mask
;
9097 int action_discarded
;
9100 /* Get the swapped relocs. */
9102 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
9103 finfo
->internal_relocs
, FALSE
);
9104 if (internal_relocs
== NULL
9105 && o
->reloc_count
> 0)
9108 if (bed
->s
->arch_size
== 32)
9115 r_type_mask
= 0xffffffff;
9119 action_discarded
= -1;
9120 if (!elf_section_ignore_discarded_relocs (o
))
9121 action_discarded
= (*bed
->action_discarded
) (o
);
9123 /* Run through the relocs evaluating complex reloc symbols and
9124 looking for relocs against symbols from discarded sections
9125 or section symbols from removed link-once sections.
9126 Complain about relocs against discarded sections. Zero
9127 relocs against removed link-once sections. */
9129 rel
= internal_relocs
;
9130 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9131 for ( ; rel
< relend
; rel
++)
9133 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9134 unsigned int s_type
;
9135 asection
**ps
, *sec
;
9136 struct elf_link_hash_entry
*h
= NULL
;
9137 const char *sym_name
;
9139 if (r_symndx
== STN_UNDEF
)
9142 if (r_symndx
>= locsymcount
9143 || (elf_bad_symtab (input_bfd
)
9144 && finfo
->sections
[r_symndx
] == NULL
))
9146 h
= sym_hashes
[r_symndx
- extsymoff
];
9148 /* Badly formatted input files can contain relocs that
9149 reference non-existant symbols. Check here so that
9150 we do not seg fault. */
9155 sprintf_vma (buffer
, rel
->r_info
);
9156 (*_bfd_error_handler
)
9157 (_("error: %B contains a reloc (0x%s) for section %A "
9158 "that references a non-existent global symbol"),
9159 input_bfd
, o
, buffer
);
9160 bfd_set_error (bfd_error_bad_value
);
9164 while (h
->root
.type
== bfd_link_hash_indirect
9165 || h
->root
.type
== bfd_link_hash_warning
)
9166 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9171 if (h
->root
.type
== bfd_link_hash_defined
9172 || h
->root
.type
== bfd_link_hash_defweak
)
9173 ps
= &h
->root
.u
.def
.section
;
9175 sym_name
= h
->root
.root
.string
;
9179 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9181 s_type
= ELF_ST_TYPE (sym
->st_info
);
9182 ps
= &finfo
->sections
[r_symndx
];
9183 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
9187 if (s_type
== STT_RELC
|| s_type
== STT_SRELC
)
9190 bfd_vma dot
= (rel
->r_offset
9191 + o
->output_offset
+ o
->output_section
->vma
);
9193 printf ("Encountered a complex symbol!");
9194 printf (" (input_bfd %s, section %s, reloc %ld\n",
9195 input_bfd
->filename
, o
->name
, rel
- internal_relocs
);
9196 printf (" symbol: idx %8.8lx, name %s\n",
9197 r_symndx
, sym_name
);
9198 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9199 (unsigned long) rel
->r_info
,
9200 (unsigned long) rel
->r_offset
);
9202 if (!eval_symbol (&val
, &sym_name
, input_bfd
, finfo
, dot
,
9203 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
9206 /* Symbol evaluated OK. Update to absolute value. */
9207 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
9212 if (action_discarded
!= -1 && ps
!= NULL
)
9214 /* Complain if the definition comes from a
9215 discarded section. */
9216 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
9218 BFD_ASSERT (r_symndx
!= 0);
9219 if (action_discarded
& COMPLAIN
)
9220 (*finfo
->info
->callbacks
->einfo
)
9221 (_("%X`%s' referenced in section `%A' of %B: "
9222 "defined in discarded section `%A' of %B\n"),
9223 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9225 /* Try to do the best we can to support buggy old
9226 versions of gcc. Pretend that the symbol is
9227 really defined in the kept linkonce section.
9228 FIXME: This is quite broken. Modifying the
9229 symbol here means we will be changing all later
9230 uses of the symbol, not just in this section. */
9231 if (action_discarded
& PRETEND
)
9235 kept
= _bfd_elf_check_kept_section (sec
,
9247 /* Relocate the section by invoking a back end routine.
9249 The back end routine is responsible for adjusting the
9250 section contents as necessary, and (if using Rela relocs
9251 and generating a relocatable output file) adjusting the
9252 reloc addend as necessary.
9254 The back end routine does not have to worry about setting
9255 the reloc address or the reloc symbol index.
9257 The back end routine is given a pointer to the swapped in
9258 internal symbols, and can access the hash table entries
9259 for the external symbols via elf_sym_hashes (input_bfd).
9261 When generating relocatable output, the back end routine
9262 must handle STB_LOCAL/STT_SECTION symbols specially. The
9263 output symbol is going to be a section symbol
9264 corresponding to the output section, which will require
9265 the addend to be adjusted. */
9267 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
9268 input_bfd
, o
, contents
,
9276 || finfo
->info
->relocatable
9277 || finfo
->info
->emitrelocations
)
9279 Elf_Internal_Rela
*irela
;
9280 Elf_Internal_Rela
*irelaend
;
9281 bfd_vma last_offset
;
9282 struct elf_link_hash_entry
**rel_hash
;
9283 struct elf_link_hash_entry
**rel_hash_list
;
9284 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
9285 unsigned int next_erel
;
9286 bfd_boolean rela_normal
;
9288 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
9289 rela_normal
= (bed
->rela_normal
9290 && (input_rel_hdr
->sh_entsize
9291 == bed
->s
->sizeof_rela
));
9293 /* Adjust the reloc addresses and symbol indices. */
9295 irela
= internal_relocs
;
9296 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9297 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
9298 + elf_section_data (o
->output_section
)->rel_count
9299 + elf_section_data (o
->output_section
)->rel_count2
);
9300 rel_hash_list
= rel_hash
;
9301 last_offset
= o
->output_offset
;
9302 if (!finfo
->info
->relocatable
)
9303 last_offset
+= o
->output_section
->vma
;
9304 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9306 unsigned long r_symndx
;
9308 Elf_Internal_Sym sym
;
9310 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9316 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9319 if (irela
->r_offset
>= (bfd_vma
) -2)
9321 /* This is a reloc for a deleted entry or somesuch.
9322 Turn it into an R_*_NONE reloc, at the same
9323 offset as the last reloc. elf_eh_frame.c and
9324 bfd_elf_discard_info rely on reloc offsets
9326 irela
->r_offset
= last_offset
;
9328 irela
->r_addend
= 0;
9332 irela
->r_offset
+= o
->output_offset
;
9334 /* Relocs in an executable have to be virtual addresses. */
9335 if (!finfo
->info
->relocatable
)
9336 irela
->r_offset
+= o
->output_section
->vma
;
9338 last_offset
= irela
->r_offset
;
9340 r_symndx
= irela
->r_info
>> r_sym_shift
;
9341 if (r_symndx
== STN_UNDEF
)
9344 if (r_symndx
>= locsymcount
9345 || (elf_bad_symtab (input_bfd
)
9346 && finfo
->sections
[r_symndx
] == NULL
))
9348 struct elf_link_hash_entry
*rh
;
9351 /* This is a reloc against a global symbol. We
9352 have not yet output all the local symbols, so
9353 we do not know the symbol index of any global
9354 symbol. We set the rel_hash entry for this
9355 reloc to point to the global hash table entry
9356 for this symbol. The symbol index is then
9357 set at the end of bfd_elf_final_link. */
9358 indx
= r_symndx
- extsymoff
;
9359 rh
= elf_sym_hashes (input_bfd
)[indx
];
9360 while (rh
->root
.type
== bfd_link_hash_indirect
9361 || rh
->root
.type
== bfd_link_hash_warning
)
9362 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9364 /* Setting the index to -2 tells
9365 elf_link_output_extsym that this symbol is
9367 BFD_ASSERT (rh
->indx
< 0);
9375 /* This is a reloc against a local symbol. */
9378 sym
= isymbuf
[r_symndx
];
9379 sec
= finfo
->sections
[r_symndx
];
9380 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9382 /* I suppose the backend ought to fill in the
9383 section of any STT_SECTION symbol against a
9384 processor specific section. */
9386 if (bfd_is_abs_section (sec
))
9388 else if (sec
== NULL
|| sec
->owner
== NULL
)
9390 bfd_set_error (bfd_error_bad_value
);
9395 asection
*osec
= sec
->output_section
;
9397 /* If we have discarded a section, the output
9398 section will be the absolute section. In
9399 case of discarded SEC_MERGE sections, use
9400 the kept section. relocate_section should
9401 have already handled discarded linkonce
9403 if (bfd_is_abs_section (osec
)
9404 && sec
->kept_section
!= NULL
9405 && sec
->kept_section
->output_section
!= NULL
)
9407 osec
= sec
->kept_section
->output_section
;
9408 irela
->r_addend
-= osec
->vma
;
9411 if (!bfd_is_abs_section (osec
))
9413 r_symndx
= osec
->target_index
;
9416 struct elf_link_hash_table
*htab
;
9419 htab
= elf_hash_table (finfo
->info
);
9420 oi
= htab
->text_index_section
;
9421 if ((osec
->flags
& SEC_READONLY
) == 0
9422 && htab
->data_index_section
!= NULL
)
9423 oi
= htab
->data_index_section
;
9427 irela
->r_addend
+= osec
->vma
- oi
->vma
;
9428 r_symndx
= oi
->target_index
;
9432 BFD_ASSERT (r_symndx
!= 0);
9436 /* Adjust the addend according to where the
9437 section winds up in the output section. */
9439 irela
->r_addend
+= sec
->output_offset
;
9443 if (finfo
->indices
[r_symndx
] == -1)
9445 unsigned long shlink
;
9449 if (finfo
->info
->strip
== strip_all
)
9451 /* You can't do ld -r -s. */
9452 bfd_set_error (bfd_error_invalid_operation
);
9456 /* This symbol was skipped earlier, but
9457 since it is needed by a reloc, we
9458 must output it now. */
9459 shlink
= symtab_hdr
->sh_link
;
9460 name
= (bfd_elf_string_from_elf_section
9461 (input_bfd
, shlink
, sym
.st_name
));
9465 osec
= sec
->output_section
;
9467 _bfd_elf_section_from_bfd_section (output_bfd
,
9469 if (sym
.st_shndx
== SHN_BAD
)
9472 sym
.st_value
+= sec
->output_offset
;
9473 if (! finfo
->info
->relocatable
)
9475 sym
.st_value
+= osec
->vma
;
9476 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9478 /* STT_TLS symbols are relative to PT_TLS
9480 BFD_ASSERT (elf_hash_table (finfo
->info
)
9482 sym
.st_value
-= (elf_hash_table (finfo
->info
)
9487 finfo
->indices
[r_symndx
]
9488 = bfd_get_symcount (output_bfd
);
9490 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
9495 r_symndx
= finfo
->indices
[r_symndx
];
9498 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9499 | (irela
->r_info
& r_type_mask
));
9502 /* Swap out the relocs. */
9503 if (input_rel_hdr
->sh_size
!= 0
9504 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
9510 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
9511 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
9513 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9514 * bed
->s
->int_rels_per_ext_rel
);
9515 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9516 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9525 /* Write out the modified section contents. */
9526 if (bed
->elf_backend_write_section
9527 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
9530 /* Section written out. */
9532 else switch (o
->sec_info_type
)
9534 case ELF_INFO_TYPE_STABS
:
9535 if (! (_bfd_write_section_stabs
9537 &elf_hash_table (finfo
->info
)->stab_info
,
9538 o
, &elf_section_data (o
)->sec_info
, contents
)))
9541 case ELF_INFO_TYPE_MERGE
:
9542 if (! _bfd_write_merged_section (output_bfd
, o
,
9543 elf_section_data (o
)->sec_info
))
9546 case ELF_INFO_TYPE_EH_FRAME
:
9548 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
9555 if (! (o
->flags
& SEC_EXCLUDE
)
9556 && ! (o
->output_section
->flags
& SEC_NEVER_LOAD
)
9557 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
9559 (file_ptr
) o
->output_offset
,
9570 /* Generate a reloc when linking an ELF file. This is a reloc
9571 requested by the linker, and does not come from any input file. This
9572 is used to build constructor and destructor tables when linking
9576 elf_reloc_link_order (bfd
*output_bfd
,
9577 struct bfd_link_info
*info
,
9578 asection
*output_section
,
9579 struct bfd_link_order
*link_order
)
9581 reloc_howto_type
*howto
;
9585 struct elf_link_hash_entry
**rel_hash_ptr
;
9586 Elf_Internal_Shdr
*rel_hdr
;
9587 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
9588 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
9592 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
9595 bfd_set_error (bfd_error_bad_value
);
9599 addend
= link_order
->u
.reloc
.p
->addend
;
9601 /* Figure out the symbol index. */
9602 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
9603 + elf_section_data (output_section
)->rel_count
9604 + elf_section_data (output_section
)->rel_count2
);
9605 if (link_order
->type
== bfd_section_reloc_link_order
)
9607 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
9608 BFD_ASSERT (indx
!= 0);
9609 *rel_hash_ptr
= NULL
;
9613 struct elf_link_hash_entry
*h
;
9615 /* Treat a reloc against a defined symbol as though it were
9616 actually against the section. */
9617 h
= ((struct elf_link_hash_entry
*)
9618 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
9619 link_order
->u
.reloc
.p
->u
.name
,
9620 FALSE
, FALSE
, TRUE
));
9622 && (h
->root
.type
== bfd_link_hash_defined
9623 || h
->root
.type
== bfd_link_hash_defweak
))
9627 section
= h
->root
.u
.def
.section
;
9628 indx
= section
->output_section
->target_index
;
9629 *rel_hash_ptr
= NULL
;
9630 /* It seems that we ought to add the symbol value to the
9631 addend here, but in practice it has already been added
9632 because it was passed to constructor_callback. */
9633 addend
+= section
->output_section
->vma
+ section
->output_offset
;
9637 /* Setting the index to -2 tells elf_link_output_extsym that
9638 this symbol is used by a reloc. */
9645 if (! ((*info
->callbacks
->unattached_reloc
)
9646 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9652 /* If this is an inplace reloc, we must write the addend into the
9654 if (howto
->partial_inplace
&& addend
!= 0)
9657 bfd_reloc_status_type rstat
;
9660 const char *sym_name
;
9662 size
= bfd_get_reloc_size (howto
);
9663 buf
= bfd_zmalloc (size
);
9666 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9673 case bfd_reloc_outofrange
:
9676 case bfd_reloc_overflow
:
9677 if (link_order
->type
== bfd_section_reloc_link_order
)
9678 sym_name
= bfd_section_name (output_bfd
,
9679 link_order
->u
.reloc
.p
->u
.section
);
9681 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9682 if (! ((*info
->callbacks
->reloc_overflow
)
9683 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9684 NULL
, (bfd_vma
) 0)))
9691 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9692 link_order
->offset
, size
);
9698 /* The address of a reloc is relative to the section in a
9699 relocatable file, and is a virtual address in an executable
9701 offset
= link_order
->offset
;
9702 if (! info
->relocatable
)
9703 offset
+= output_section
->vma
;
9705 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9707 irel
[i
].r_offset
= offset
;
9709 irel
[i
].r_addend
= 0;
9711 if (bed
->s
->arch_size
== 32)
9712 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9714 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9716 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9717 erel
= rel_hdr
->contents
;
9718 if (rel_hdr
->sh_type
== SHT_REL
)
9720 erel
+= (elf_section_data (output_section
)->rel_count
9721 * bed
->s
->sizeof_rel
);
9722 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9726 irel
[0].r_addend
= addend
;
9727 erel
+= (elf_section_data (output_section
)->rel_count
9728 * bed
->s
->sizeof_rela
);
9729 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9732 ++elf_section_data (output_section
)->rel_count
;
9738 /* Get the output vma of the section pointed to by the sh_link field. */
9741 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9743 Elf_Internal_Shdr
**elf_shdrp
;
9747 s
= p
->u
.indirect
.section
;
9748 elf_shdrp
= elf_elfsections (s
->owner
);
9749 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9750 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9752 The Intel C compiler generates SHT_IA_64_UNWIND with
9753 SHF_LINK_ORDER. But it doesn't set the sh_link or
9754 sh_info fields. Hence we could get the situation
9755 where elfsec is 0. */
9758 const struct elf_backend_data
*bed
9759 = get_elf_backend_data (s
->owner
);
9760 if (bed
->link_order_error_handler
)
9761 bed
->link_order_error_handler
9762 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9767 s
= elf_shdrp
[elfsec
]->bfd_section
;
9768 return s
->output_section
->vma
+ s
->output_offset
;
9773 /* Compare two sections based on the locations of the sections they are
9774 linked to. Used by elf_fixup_link_order. */
9777 compare_link_order (const void * a
, const void * b
)
9782 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9783 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9790 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9791 order as their linked sections. Returns false if this could not be done
9792 because an output section includes both ordered and unordered
9793 sections. Ideally we'd do this in the linker proper. */
9796 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9801 struct bfd_link_order
*p
;
9803 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9805 struct bfd_link_order
**sections
;
9806 asection
*s
, *other_sec
, *linkorder_sec
;
9810 linkorder_sec
= NULL
;
9813 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9815 if (p
->type
== bfd_indirect_link_order
)
9817 s
= p
->u
.indirect
.section
;
9819 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9820 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9821 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9822 && elfsec
< elf_numsections (sub
)
9823 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
9824 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
9838 if (seen_other
&& seen_linkorder
)
9840 if (other_sec
&& linkorder_sec
)
9841 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9843 linkorder_sec
->owner
, other_sec
,
9846 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9848 bfd_set_error (bfd_error_bad_value
);
9853 if (!seen_linkorder
)
9856 sections
= (struct bfd_link_order
**)
9857 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9858 if (sections
== NULL
)
9862 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9864 sections
[seen_linkorder
++] = p
;
9866 /* Sort the input sections in the order of their linked section. */
9867 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9868 compare_link_order
);
9870 /* Change the offsets of the sections. */
9872 for (n
= 0; n
< seen_linkorder
; n
++)
9874 s
= sections
[n
]->u
.indirect
.section
;
9875 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
9876 s
->output_offset
= offset
;
9877 sections
[n
]->offset
= offset
;
9878 offset
+= sections
[n
]->size
;
9886 /* Do the final step of an ELF link. */
9889 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9891 bfd_boolean dynamic
;
9892 bfd_boolean emit_relocs
;
9894 struct elf_final_link_info finfo
;
9895 register asection
*o
;
9896 register struct bfd_link_order
*p
;
9898 bfd_size_type max_contents_size
;
9899 bfd_size_type max_external_reloc_size
;
9900 bfd_size_type max_internal_reloc_count
;
9901 bfd_size_type max_sym_count
;
9902 bfd_size_type max_sym_shndx_count
;
9904 Elf_Internal_Sym elfsym
;
9906 Elf_Internal_Shdr
*symtab_hdr
;
9907 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9908 Elf_Internal_Shdr
*symstrtab_hdr
;
9909 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9910 struct elf_outext_info eoinfo
;
9912 size_t relativecount
= 0;
9913 asection
*reldyn
= 0;
9915 asection
*attr_section
= NULL
;
9916 bfd_vma attr_size
= 0;
9917 const char *std_attrs_section
;
9919 if (! is_elf_hash_table (info
->hash
))
9923 abfd
->flags
|= DYNAMIC
;
9925 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9926 dynobj
= elf_hash_table (info
)->dynobj
;
9928 emit_relocs
= (info
->relocatable
9929 || info
->emitrelocations
);
9932 finfo
.output_bfd
= abfd
;
9933 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9934 if (finfo
.symstrtab
== NULL
)
9939 finfo
.dynsym_sec
= NULL
;
9940 finfo
.hash_sec
= NULL
;
9941 finfo
.symver_sec
= NULL
;
9945 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9946 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9947 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9948 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9949 /* Note that it is OK if symver_sec is NULL. */
9952 finfo
.contents
= NULL
;
9953 finfo
.external_relocs
= NULL
;
9954 finfo
.internal_relocs
= NULL
;
9955 finfo
.external_syms
= NULL
;
9956 finfo
.locsym_shndx
= NULL
;
9957 finfo
.internal_syms
= NULL
;
9958 finfo
.indices
= NULL
;
9959 finfo
.sections
= NULL
;
9960 finfo
.symbuf
= NULL
;
9961 finfo
.symshndxbuf
= NULL
;
9962 finfo
.symbuf_count
= 0;
9963 finfo
.shndxbuf_size
= 0;
9965 /* The object attributes have been merged. Remove the input
9966 sections from the link, and set the contents of the output
9968 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
9969 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9971 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
9972 || strcmp (o
->name
, ".gnu.attributes") == 0)
9974 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9976 asection
*input_section
;
9978 if (p
->type
!= bfd_indirect_link_order
)
9980 input_section
= p
->u
.indirect
.section
;
9981 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9982 elf_link_input_bfd ignores this section. */
9983 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
9986 attr_size
= bfd_elf_obj_attr_size (abfd
);
9989 bfd_set_section_size (abfd
, o
, attr_size
);
9991 /* Skip this section later on. */
9992 o
->map_head
.link_order
= NULL
;
9995 o
->flags
|= SEC_EXCLUDE
;
9999 /* Count up the number of relocations we will output for each output
10000 section, so that we know the sizes of the reloc sections. We
10001 also figure out some maximum sizes. */
10002 max_contents_size
= 0;
10003 max_external_reloc_size
= 0;
10004 max_internal_reloc_count
= 0;
10006 max_sym_shndx_count
= 0;
10008 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10010 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10011 o
->reloc_count
= 0;
10013 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10015 unsigned int reloc_count
= 0;
10016 struct bfd_elf_section_data
*esdi
= NULL
;
10017 unsigned int *rel_count1
;
10019 if (p
->type
== bfd_section_reloc_link_order
10020 || p
->type
== bfd_symbol_reloc_link_order
)
10022 else if (p
->type
== bfd_indirect_link_order
)
10026 sec
= p
->u
.indirect
.section
;
10027 esdi
= elf_section_data (sec
);
10029 /* Mark all sections which are to be included in the
10030 link. This will normally be every section. We need
10031 to do this so that we can identify any sections which
10032 the linker has decided to not include. */
10033 sec
->linker_mark
= TRUE
;
10035 if (sec
->flags
& SEC_MERGE
)
10038 if (info
->relocatable
|| info
->emitrelocations
)
10039 reloc_count
= sec
->reloc_count
;
10040 else if (bed
->elf_backend_count_relocs
)
10042 Elf_Internal_Rela
* relocs
;
10044 relocs
= _bfd_elf_link_read_relocs (sec
->owner
, sec
,
10046 info
->keep_memory
);
10048 if (relocs
!= NULL
)
10051 = (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
10053 if (elf_section_data (sec
)->relocs
!= relocs
)
10058 if (sec
->rawsize
> max_contents_size
)
10059 max_contents_size
= sec
->rawsize
;
10060 if (sec
->size
> max_contents_size
)
10061 max_contents_size
= sec
->size
;
10063 /* We are interested in just local symbols, not all
10065 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10066 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10070 if (elf_bad_symtab (sec
->owner
))
10071 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10072 / bed
->s
->sizeof_sym
);
10074 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10076 if (sym_count
> max_sym_count
)
10077 max_sym_count
= sym_count
;
10079 if (sym_count
> max_sym_shndx_count
10080 && elf_symtab_shndx (sec
->owner
) != 0)
10081 max_sym_shndx_count
= sym_count
;
10083 if ((sec
->flags
& SEC_RELOC
) != 0)
10087 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
10088 if (ext_size
> max_external_reloc_size
)
10089 max_external_reloc_size
= ext_size
;
10090 if (sec
->reloc_count
> max_internal_reloc_count
)
10091 max_internal_reloc_count
= sec
->reloc_count
;
10096 if (reloc_count
== 0)
10099 o
->reloc_count
+= reloc_count
;
10101 /* MIPS may have a mix of REL and RELA relocs on sections.
10102 To support this curious ABI we keep reloc counts in
10103 elf_section_data too. We must be careful to add the
10104 relocations from the input section to the right output
10105 count. FIXME: Get rid of one count. We have
10106 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10107 rel_count1
= &esdo
->rel_count
;
10110 bfd_boolean same_size
;
10111 bfd_size_type entsize1
;
10113 entsize1
= esdi
->rel_hdr
.sh_entsize
;
10114 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
10115 || entsize1
== bed
->s
->sizeof_rela
);
10116 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
10119 rel_count1
= &esdo
->rel_count2
;
10121 if (esdi
->rel_hdr2
!= NULL
)
10123 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
10124 unsigned int alt_count
;
10125 unsigned int *rel_count2
;
10127 BFD_ASSERT (entsize2
!= entsize1
10128 && (entsize2
== bed
->s
->sizeof_rel
10129 || entsize2
== bed
->s
->sizeof_rela
));
10131 rel_count2
= &esdo
->rel_count2
;
10133 rel_count2
= &esdo
->rel_count
;
10135 /* The following is probably too simplistic if the
10136 backend counts output relocs unusually. */
10137 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
10138 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
10139 *rel_count2
+= alt_count
;
10140 reloc_count
-= alt_count
;
10143 *rel_count1
+= reloc_count
;
10146 if (o
->reloc_count
> 0)
10147 o
->flags
|= SEC_RELOC
;
10150 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10151 set it (this is probably a bug) and if it is set
10152 assign_section_numbers will create a reloc section. */
10153 o
->flags
&=~ SEC_RELOC
;
10156 /* If the SEC_ALLOC flag is not set, force the section VMA to
10157 zero. This is done in elf_fake_sections as well, but forcing
10158 the VMA to 0 here will ensure that relocs against these
10159 sections are handled correctly. */
10160 if ((o
->flags
& SEC_ALLOC
) == 0
10161 && ! o
->user_set_vma
)
10165 if (! info
->relocatable
&& merged
)
10166 elf_link_hash_traverse (elf_hash_table (info
),
10167 _bfd_elf_link_sec_merge_syms
, abfd
);
10169 /* Figure out the file positions for everything but the symbol table
10170 and the relocs. We set symcount to force assign_section_numbers
10171 to create a symbol table. */
10172 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10173 BFD_ASSERT (! abfd
->output_has_begun
);
10174 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10177 /* Set sizes, and assign file positions for reloc sections. */
10178 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10180 if ((o
->flags
& SEC_RELOC
) != 0)
10182 if (!(_bfd_elf_link_size_reloc_section
10183 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
10186 if (elf_section_data (o
)->rel_hdr2
10187 && !(_bfd_elf_link_size_reloc_section
10188 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
10192 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10193 to count upwards while actually outputting the relocations. */
10194 elf_section_data (o
)->rel_count
= 0;
10195 elf_section_data (o
)->rel_count2
= 0;
10198 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10200 /* We have now assigned file positions for all the sections except
10201 .symtab and .strtab. We start the .symtab section at the current
10202 file position, and write directly to it. We build the .strtab
10203 section in memory. */
10204 bfd_get_symcount (abfd
) = 0;
10205 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10206 /* sh_name is set in prep_headers. */
10207 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10208 /* sh_flags, sh_addr and sh_size all start off zero. */
10209 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10210 /* sh_link is set in assign_section_numbers. */
10211 /* sh_info is set below. */
10212 /* sh_offset is set just below. */
10213 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
10215 off
= elf_tdata (abfd
)->next_file_pos
;
10216 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10218 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10219 incorrect. We do not yet know the size of the .symtab section.
10220 We correct next_file_pos below, after we do know the size. */
10222 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10223 continuously seeking to the right position in the file. */
10224 if (! info
->keep_memory
|| max_sym_count
< 20)
10225 finfo
.symbuf_size
= 20;
10227 finfo
.symbuf_size
= max_sym_count
;
10228 amt
= finfo
.symbuf_size
;
10229 amt
*= bed
->s
->sizeof_sym
;
10230 finfo
.symbuf
= bfd_malloc (amt
);
10231 if (finfo
.symbuf
== NULL
)
10233 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
10235 /* Wild guess at number of output symbols. realloc'd as needed. */
10236 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10237 finfo
.shndxbuf_size
= amt
;
10238 amt
*= sizeof (Elf_External_Sym_Shndx
);
10239 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
10240 if (finfo
.symshndxbuf
== NULL
)
10244 /* Start writing out the symbol table. The first symbol is always a
10246 if (info
->strip
!= strip_all
10249 elfsym
.st_value
= 0;
10250 elfsym
.st_size
= 0;
10251 elfsym
.st_info
= 0;
10252 elfsym
.st_other
= 0;
10253 elfsym
.st_shndx
= SHN_UNDEF
;
10254 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10259 /* Output a symbol for each section. We output these even if we are
10260 discarding local symbols, since they are used for relocs. These
10261 symbols have no names. We store the index of each one in the
10262 index field of the section, so that we can find it again when
10263 outputting relocs. */
10264 if (info
->strip
!= strip_all
10267 elfsym
.st_size
= 0;
10268 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10269 elfsym
.st_other
= 0;
10270 elfsym
.st_value
= 0;
10271 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10273 o
= bfd_section_from_elf_index (abfd
, i
);
10276 o
->target_index
= bfd_get_symcount (abfd
);
10277 elfsym
.st_shndx
= i
;
10278 if (!info
->relocatable
)
10279 elfsym
.st_value
= o
->vma
;
10280 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
10286 /* Allocate some memory to hold information read in from the input
10288 if (max_contents_size
!= 0)
10290 finfo
.contents
= bfd_malloc (max_contents_size
);
10291 if (finfo
.contents
== NULL
)
10295 if (max_external_reloc_size
!= 0)
10297 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10298 if (finfo
.external_relocs
== NULL
)
10302 if (max_internal_reloc_count
!= 0)
10304 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10305 amt
*= sizeof (Elf_Internal_Rela
);
10306 finfo
.internal_relocs
= bfd_malloc (amt
);
10307 if (finfo
.internal_relocs
== NULL
)
10311 if (max_sym_count
!= 0)
10313 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10314 finfo
.external_syms
= bfd_malloc (amt
);
10315 if (finfo
.external_syms
== NULL
)
10318 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10319 finfo
.internal_syms
= bfd_malloc (amt
);
10320 if (finfo
.internal_syms
== NULL
)
10323 amt
= max_sym_count
* sizeof (long);
10324 finfo
.indices
= bfd_malloc (amt
);
10325 if (finfo
.indices
== NULL
)
10328 amt
= max_sym_count
* sizeof (asection
*);
10329 finfo
.sections
= bfd_malloc (amt
);
10330 if (finfo
.sections
== NULL
)
10334 if (max_sym_shndx_count
!= 0)
10336 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10337 finfo
.locsym_shndx
= bfd_malloc (amt
);
10338 if (finfo
.locsym_shndx
== NULL
)
10342 if (elf_hash_table (info
)->tls_sec
)
10344 bfd_vma base
, end
= 0;
10347 for (sec
= elf_hash_table (info
)->tls_sec
;
10348 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10351 bfd_size_type size
= sec
->size
;
10354 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10356 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
10358 size
= o
->offset
+ o
->size
;
10360 end
= sec
->vma
+ size
;
10362 base
= elf_hash_table (info
)->tls_sec
->vma
;
10363 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
10364 elf_hash_table (info
)->tls_size
= end
- base
;
10367 /* Reorder SHF_LINK_ORDER sections. */
10368 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10370 if (!elf_fixup_link_order (abfd
, o
))
10374 /* Since ELF permits relocations to be against local symbols, we
10375 must have the local symbols available when we do the relocations.
10376 Since we would rather only read the local symbols once, and we
10377 would rather not keep them in memory, we handle all the
10378 relocations for a single input file at the same time.
10380 Unfortunately, there is no way to know the total number of local
10381 symbols until we have seen all of them, and the local symbol
10382 indices precede the global symbol indices. This means that when
10383 we are generating relocatable output, and we see a reloc against
10384 a global symbol, we can not know the symbol index until we have
10385 finished examining all the local symbols to see which ones we are
10386 going to output. To deal with this, we keep the relocations in
10387 memory, and don't output them until the end of the link. This is
10388 an unfortunate waste of memory, but I don't see a good way around
10389 it. Fortunately, it only happens when performing a relocatable
10390 link, which is not the common case. FIXME: If keep_memory is set
10391 we could write the relocs out and then read them again; I don't
10392 know how bad the memory loss will be. */
10394 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10395 sub
->output_has_begun
= FALSE
;
10396 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10398 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10400 if (p
->type
== bfd_indirect_link_order
10401 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10402 == bfd_target_elf_flavour
)
10403 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10405 if (! sub
->output_has_begun
)
10407 if (! elf_link_input_bfd (&finfo
, sub
))
10409 sub
->output_has_begun
= TRUE
;
10412 else if (p
->type
== bfd_section_reloc_link_order
10413 || p
->type
== bfd_symbol_reloc_link_order
)
10415 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10420 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10426 /* Free symbol buffer if needed. */
10427 if (!info
->reduce_memory_overheads
)
10429 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10430 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10431 && elf_tdata (sub
)->symbuf
)
10433 free (elf_tdata (sub
)->symbuf
);
10434 elf_tdata (sub
)->symbuf
= NULL
;
10438 /* Output any global symbols that got converted to local in a
10439 version script or due to symbol visibility. We do this in a
10440 separate step since ELF requires all local symbols to appear
10441 prior to any global symbols. FIXME: We should only do this if
10442 some global symbols were, in fact, converted to become local.
10443 FIXME: Will this work correctly with the Irix 5 linker? */
10444 eoinfo
.failed
= FALSE
;
10445 eoinfo
.finfo
= &finfo
;
10446 eoinfo
.localsyms
= TRUE
;
10447 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10452 /* If backend needs to output some local symbols not present in the hash
10453 table, do it now. */
10454 if (bed
->elf_backend_output_arch_local_syms
)
10456 typedef bfd_boolean (*out_sym_func
)
10457 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10458 struct elf_link_hash_entry
*);
10460 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10461 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10465 /* That wrote out all the local symbols. Finish up the symbol table
10466 with the global symbols. Even if we want to strip everything we
10467 can, we still need to deal with those global symbols that got
10468 converted to local in a version script. */
10470 /* The sh_info field records the index of the first non local symbol. */
10471 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10474 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10476 Elf_Internal_Sym sym
;
10477 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
10478 long last_local
= 0;
10480 /* Write out the section symbols for the output sections. */
10481 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10487 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10490 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
10496 dynindx
= elf_section_data (s
)->dynindx
;
10499 indx
= elf_section_data (s
)->this_idx
;
10500 BFD_ASSERT (indx
> 0);
10501 sym
.st_shndx
= indx
;
10502 if (! check_dynsym (abfd
, &sym
))
10504 sym
.st_value
= s
->vma
;
10505 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
10506 if (last_local
< dynindx
)
10507 last_local
= dynindx
;
10508 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10512 /* Write out the local dynsyms. */
10513 if (elf_hash_table (info
)->dynlocal
)
10515 struct elf_link_local_dynamic_entry
*e
;
10516 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
10521 sym
.st_size
= e
->isym
.st_size
;
10522 sym
.st_other
= e
->isym
.st_other
;
10524 /* Copy the internal symbol as is.
10525 Note that we saved a word of storage and overwrote
10526 the original st_name with the dynstr_index. */
10529 s
= bfd_section_from_elf_index (e
->input_bfd
,
10534 elf_section_data (s
->output_section
)->this_idx
;
10535 if (! check_dynsym (abfd
, &sym
))
10537 sym
.st_value
= (s
->output_section
->vma
10539 + e
->isym
.st_value
);
10542 if (last_local
< e
->dynindx
)
10543 last_local
= e
->dynindx
;
10545 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
10546 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10550 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
10554 /* We get the global symbols from the hash table. */
10555 eoinfo
.failed
= FALSE
;
10556 eoinfo
.localsyms
= FALSE
;
10557 eoinfo
.finfo
= &finfo
;
10558 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10563 /* If backend needs to output some symbols not present in the hash
10564 table, do it now. */
10565 if (bed
->elf_backend_output_arch_syms
)
10567 typedef bfd_boolean (*out_sym_func
)
10568 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10569 struct elf_link_hash_entry
*);
10571 if (! ((*bed
->elf_backend_output_arch_syms
)
10572 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10576 /* Flush all symbols to the file. */
10577 if (! elf_link_flush_output_syms (&finfo
, bed
))
10580 /* Now we know the size of the symtab section. */
10581 off
+= symtab_hdr
->sh_size
;
10583 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
10584 if (symtab_shndx_hdr
->sh_name
!= 0)
10586 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
10587 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
10588 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
10589 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
10590 symtab_shndx_hdr
->sh_size
= amt
;
10592 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
10595 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
10596 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
10601 /* Finish up and write out the symbol string table (.strtab)
10603 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
10604 /* sh_name was set in prep_headers. */
10605 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
10606 symstrtab_hdr
->sh_flags
= 0;
10607 symstrtab_hdr
->sh_addr
= 0;
10608 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
10609 symstrtab_hdr
->sh_entsize
= 0;
10610 symstrtab_hdr
->sh_link
= 0;
10611 symstrtab_hdr
->sh_info
= 0;
10612 /* sh_offset is set just below. */
10613 symstrtab_hdr
->sh_addralign
= 1;
10615 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
10616 elf_tdata (abfd
)->next_file_pos
= off
;
10618 if (bfd_get_symcount (abfd
) > 0)
10620 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
10621 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
10625 /* Adjust the relocs to have the correct symbol indices. */
10626 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10628 if ((o
->flags
& SEC_RELOC
) == 0)
10631 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
10632 elf_section_data (o
)->rel_count
,
10633 elf_section_data (o
)->rel_hashes
);
10634 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
10635 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
10636 elf_section_data (o
)->rel_count2
,
10637 (elf_section_data (o
)->rel_hashes
10638 + elf_section_data (o
)->rel_count
));
10640 /* Set the reloc_count field to 0 to prevent write_relocs from
10641 trying to swap the relocs out itself. */
10642 o
->reloc_count
= 0;
10645 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10646 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10648 /* If we are linking against a dynamic object, or generating a
10649 shared library, finish up the dynamic linking information. */
10652 bfd_byte
*dyncon
, *dynconend
;
10654 /* Fix up .dynamic entries. */
10655 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10656 BFD_ASSERT (o
!= NULL
);
10658 dyncon
= o
->contents
;
10659 dynconend
= o
->contents
+ o
->size
;
10660 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10662 Elf_Internal_Dyn dyn
;
10666 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10673 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10675 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10677 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10678 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10681 dyn
.d_un
.d_val
= relativecount
;
10688 name
= info
->init_function
;
10691 name
= info
->fini_function
;
10694 struct elf_link_hash_entry
*h
;
10696 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10697 FALSE
, FALSE
, TRUE
);
10699 && (h
->root
.type
== bfd_link_hash_defined
10700 || h
->root
.type
== bfd_link_hash_defweak
))
10702 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
10703 o
= h
->root
.u
.def
.section
;
10704 if (o
->output_section
!= NULL
)
10705 dyn
.d_un
.d_val
+= (o
->output_section
->vma
10706 + o
->output_offset
);
10709 /* The symbol is imported from another shared
10710 library and does not apply to this one. */
10711 dyn
.d_un
.d_val
= 0;
10718 case DT_PREINIT_ARRAYSZ
:
10719 name
= ".preinit_array";
10721 case DT_INIT_ARRAYSZ
:
10722 name
= ".init_array";
10724 case DT_FINI_ARRAYSZ
:
10725 name
= ".fini_array";
10727 o
= bfd_get_section_by_name (abfd
, name
);
10730 (*_bfd_error_handler
)
10731 (_("%B: could not find output section %s"), abfd
, name
);
10735 (*_bfd_error_handler
)
10736 (_("warning: %s section has zero size"), name
);
10737 dyn
.d_un
.d_val
= o
->size
;
10740 case DT_PREINIT_ARRAY
:
10741 name
= ".preinit_array";
10743 case DT_INIT_ARRAY
:
10744 name
= ".init_array";
10746 case DT_FINI_ARRAY
:
10747 name
= ".fini_array";
10754 name
= ".gnu.hash";
10763 name
= ".gnu.version_d";
10766 name
= ".gnu.version_r";
10769 name
= ".gnu.version";
10771 o
= bfd_get_section_by_name (abfd
, name
);
10774 (*_bfd_error_handler
)
10775 (_("%B: could not find output section %s"), abfd
, name
);
10778 dyn
.d_un
.d_ptr
= o
->vma
;
10785 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10789 dyn
.d_un
.d_val
= 0;
10790 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10792 Elf_Internal_Shdr
*hdr
;
10794 hdr
= elf_elfsections (abfd
)[i
];
10795 if (hdr
->sh_type
== type
10796 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10798 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10799 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10802 if (dyn
.d_un
.d_val
== 0
10803 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
10804 dyn
.d_un
.d_val
= hdr
->sh_addr
;
10810 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10814 /* If we have created any dynamic sections, then output them. */
10815 if (dynobj
!= NULL
)
10817 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10820 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10821 if (info
->warn_shared_textrel
&& info
->shared
)
10823 bfd_byte
*dyncon
, *dynconend
;
10825 /* Fix up .dynamic entries. */
10826 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10827 BFD_ASSERT (o
!= NULL
);
10829 dyncon
= o
->contents
;
10830 dynconend
= o
->contents
+ o
->size
;
10831 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10833 Elf_Internal_Dyn dyn
;
10835 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10837 if (dyn
.d_tag
== DT_TEXTREL
)
10839 info
->callbacks
->einfo
10840 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10846 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10848 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10850 || o
->output_section
== bfd_abs_section_ptr
)
10852 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10854 /* At this point, we are only interested in sections
10855 created by _bfd_elf_link_create_dynamic_sections. */
10858 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10860 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10862 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10864 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10866 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10868 (file_ptr
) o
->output_offset
,
10874 /* The contents of the .dynstr section are actually in a
10876 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10877 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10878 || ! _bfd_elf_strtab_emit (abfd
,
10879 elf_hash_table (info
)->dynstr
))
10885 if (info
->relocatable
)
10887 bfd_boolean failed
= FALSE
;
10889 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10894 /* If we have optimized stabs strings, output them. */
10895 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10897 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10901 if (info
->eh_frame_hdr
)
10903 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10907 if (finfo
.symstrtab
!= NULL
)
10908 _bfd_stringtab_free (finfo
.symstrtab
);
10909 if (finfo
.contents
!= NULL
)
10910 free (finfo
.contents
);
10911 if (finfo
.external_relocs
!= NULL
)
10912 free (finfo
.external_relocs
);
10913 if (finfo
.internal_relocs
!= NULL
)
10914 free (finfo
.internal_relocs
);
10915 if (finfo
.external_syms
!= NULL
)
10916 free (finfo
.external_syms
);
10917 if (finfo
.locsym_shndx
!= NULL
)
10918 free (finfo
.locsym_shndx
);
10919 if (finfo
.internal_syms
!= NULL
)
10920 free (finfo
.internal_syms
);
10921 if (finfo
.indices
!= NULL
)
10922 free (finfo
.indices
);
10923 if (finfo
.sections
!= NULL
)
10924 free (finfo
.sections
);
10925 if (finfo
.symbuf
!= NULL
)
10926 free (finfo
.symbuf
);
10927 if (finfo
.symshndxbuf
!= NULL
)
10928 free (finfo
.symshndxbuf
);
10929 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10931 if ((o
->flags
& SEC_RELOC
) != 0
10932 && elf_section_data (o
)->rel_hashes
!= NULL
)
10933 free (elf_section_data (o
)->rel_hashes
);
10936 elf_tdata (abfd
)->linker
= TRUE
;
10940 bfd_byte
*contents
= bfd_malloc (attr_size
);
10941 if (contents
== NULL
)
10942 return FALSE
; /* Bail out and fail. */
10943 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
10944 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
10951 if (finfo
.symstrtab
!= NULL
)
10952 _bfd_stringtab_free (finfo
.symstrtab
);
10953 if (finfo
.contents
!= NULL
)
10954 free (finfo
.contents
);
10955 if (finfo
.external_relocs
!= NULL
)
10956 free (finfo
.external_relocs
);
10957 if (finfo
.internal_relocs
!= NULL
)
10958 free (finfo
.internal_relocs
);
10959 if (finfo
.external_syms
!= NULL
)
10960 free (finfo
.external_syms
);
10961 if (finfo
.locsym_shndx
!= NULL
)
10962 free (finfo
.locsym_shndx
);
10963 if (finfo
.internal_syms
!= NULL
)
10964 free (finfo
.internal_syms
);
10965 if (finfo
.indices
!= NULL
)
10966 free (finfo
.indices
);
10967 if (finfo
.sections
!= NULL
)
10968 free (finfo
.sections
);
10969 if (finfo
.symbuf
!= NULL
)
10970 free (finfo
.symbuf
);
10971 if (finfo
.symshndxbuf
!= NULL
)
10972 free (finfo
.symshndxbuf
);
10973 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10975 if ((o
->flags
& SEC_RELOC
) != 0
10976 && elf_section_data (o
)->rel_hashes
!= NULL
)
10977 free (elf_section_data (o
)->rel_hashes
);
10983 /* Initialize COOKIE for input bfd ABFD. */
10986 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
10987 struct bfd_link_info
*info
, bfd
*abfd
)
10989 Elf_Internal_Shdr
*symtab_hdr
;
10990 const struct elf_backend_data
*bed
;
10992 bed
= get_elf_backend_data (abfd
);
10993 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10995 cookie
->abfd
= abfd
;
10996 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
10997 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
10998 if (cookie
->bad_symtab
)
11000 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11001 cookie
->extsymoff
= 0;
11005 cookie
->locsymcount
= symtab_hdr
->sh_info
;
11006 cookie
->extsymoff
= symtab_hdr
->sh_info
;
11009 if (bed
->s
->arch_size
== 32)
11010 cookie
->r_sym_shift
= 8;
11012 cookie
->r_sym_shift
= 32;
11014 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11015 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
11017 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11018 cookie
->locsymcount
, 0,
11020 if (cookie
->locsyms
== NULL
)
11022 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11025 if (info
->keep_memory
)
11026 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
11031 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11034 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
11036 Elf_Internal_Shdr
*symtab_hdr
;
11038 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11039 if (cookie
->locsyms
!= NULL
11040 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
11041 free (cookie
->locsyms
);
11044 /* Initialize the relocation information in COOKIE for input section SEC
11045 of input bfd ABFD. */
11048 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11049 struct bfd_link_info
*info
, bfd
*abfd
,
11052 const struct elf_backend_data
*bed
;
11054 if (sec
->reloc_count
== 0)
11056 cookie
->rels
= NULL
;
11057 cookie
->relend
= NULL
;
11061 bed
= get_elf_backend_data (abfd
);
11063 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
11064 info
->keep_memory
);
11065 if (cookie
->rels
== NULL
)
11067 cookie
->rel
= cookie
->rels
;
11068 cookie
->relend
= (cookie
->rels
11069 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
11071 cookie
->rel
= cookie
->rels
;
11075 /* Free the memory allocated by init_reloc_cookie_rels,
11079 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11082 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
11083 free (cookie
->rels
);
11086 /* Initialize the whole of COOKIE for input section SEC. */
11089 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11090 struct bfd_link_info
*info
,
11093 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
11095 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
11100 fini_reloc_cookie (cookie
, sec
->owner
);
11105 /* Free the memory allocated by init_reloc_cookie_for_section,
11109 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11112 fini_reloc_cookie_rels (cookie
, sec
);
11113 fini_reloc_cookie (cookie
, sec
->owner
);
11116 /* Garbage collect unused sections. */
11118 /* Default gc_mark_hook. */
11121 _bfd_elf_gc_mark_hook (asection
*sec
,
11122 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11123 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11124 struct elf_link_hash_entry
*h
,
11125 Elf_Internal_Sym
*sym
)
11129 switch (h
->root
.type
)
11131 case bfd_link_hash_defined
:
11132 case bfd_link_hash_defweak
:
11133 return h
->root
.u
.def
.section
;
11135 case bfd_link_hash_common
:
11136 return h
->root
.u
.c
.p
->section
;
11143 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11148 /* COOKIE->rel describes a relocation against section SEC, which is
11149 a section we've decided to keep. Return the section that contains
11150 the relocation symbol, or NULL if no section contains it. */
11153 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
11154 elf_gc_mark_hook_fn gc_mark_hook
,
11155 struct elf_reloc_cookie
*cookie
)
11157 unsigned long r_symndx
;
11158 struct elf_link_hash_entry
*h
;
11160 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
11164 if (r_symndx
>= cookie
->locsymcount
11165 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11167 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
11168 while (h
->root
.type
== bfd_link_hash_indirect
11169 || h
->root
.type
== bfd_link_hash_warning
)
11170 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11171 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
11174 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
11175 &cookie
->locsyms
[r_symndx
]);
11178 /* COOKIE->rel describes a relocation against section SEC, which is
11179 a section we've decided to keep. Mark the section that contains
11180 the relocation symbol. */
11183 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
11185 elf_gc_mark_hook_fn gc_mark_hook
,
11186 struct elf_reloc_cookie
*cookie
)
11190 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
11191 if (rsec
&& !rsec
->gc_mark
)
11193 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
11195 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11201 /* The mark phase of garbage collection. For a given section, mark
11202 it and any sections in this section's group, and all the sections
11203 which define symbols to which it refers. */
11206 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11208 elf_gc_mark_hook_fn gc_mark_hook
)
11211 asection
*group_sec
, *eh_frame
;
11215 /* Mark all the sections in the group. */
11216 group_sec
= elf_section_data (sec
)->next_in_group
;
11217 if (group_sec
&& !group_sec
->gc_mark
)
11218 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11221 /* Look through the section relocs. */
11223 eh_frame
= elf_eh_frame_section (sec
->owner
);
11224 if ((sec
->flags
& SEC_RELOC
) != 0
11225 && sec
->reloc_count
> 0
11226 && sec
!= eh_frame
)
11228 struct elf_reloc_cookie cookie
;
11230 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
11234 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
11235 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
11240 fini_reloc_cookie_for_section (&cookie
, sec
);
11244 if (ret
&& eh_frame
&& elf_fde_list (sec
))
11246 struct elf_reloc_cookie cookie
;
11248 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
11252 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
11253 gc_mark_hook
, &cookie
))
11255 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
11262 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11264 struct elf_gc_sweep_symbol_info
11266 struct bfd_link_info
*info
;
11267 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11272 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11274 if (h
->root
.type
== bfd_link_hash_warning
)
11275 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11277 if ((h
->root
.type
== bfd_link_hash_defined
11278 || h
->root
.type
== bfd_link_hash_defweak
)
11279 && !h
->root
.u
.def
.section
->gc_mark
11280 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
11282 struct elf_gc_sweep_symbol_info
*inf
= data
;
11283 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11289 /* The sweep phase of garbage collection. Remove all garbage sections. */
11291 typedef bfd_boolean (*gc_sweep_hook_fn
)
11292 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11295 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11298 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11299 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11300 unsigned long section_sym_count
;
11301 struct elf_gc_sweep_symbol_info sweep_info
;
11303 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11307 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11310 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11312 /* Keep debug and special sections. */
11313 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
11314 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
11320 /* Skip sweeping sections already excluded. */
11321 if (o
->flags
& SEC_EXCLUDE
)
11324 /* Since this is early in the link process, it is simple
11325 to remove a section from the output. */
11326 o
->flags
|= SEC_EXCLUDE
;
11328 if (info
->print_gc_sections
&& o
->size
!= 0)
11329 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11331 /* But we also have to update some of the relocation
11332 info we collected before. */
11334 && (o
->flags
& SEC_RELOC
) != 0
11335 && o
->reloc_count
> 0
11336 && !bfd_is_abs_section (o
->output_section
))
11338 Elf_Internal_Rela
*internal_relocs
;
11342 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11343 info
->keep_memory
);
11344 if (internal_relocs
== NULL
)
11347 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11349 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11350 free (internal_relocs
);
11358 /* Remove the symbols that were in the swept sections from the dynamic
11359 symbol table. GCFIXME: Anyone know how to get them out of the
11360 static symbol table as well? */
11361 sweep_info
.info
= info
;
11362 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11363 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11366 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11370 /* Propagate collected vtable information. This is called through
11371 elf_link_hash_traverse. */
11374 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11376 if (h
->root
.type
== bfd_link_hash_warning
)
11377 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11379 /* Those that are not vtables. */
11380 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11383 /* Those vtables that do not have parents, we cannot merge. */
11384 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11387 /* If we've already been done, exit. */
11388 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11391 /* Make sure the parent's table is up to date. */
11392 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11394 if (h
->vtable
->used
== NULL
)
11396 /* None of this table's entries were referenced. Re-use the
11398 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
11399 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
11404 bfd_boolean
*cu
, *pu
;
11406 /* Or the parent's entries into ours. */
11407 cu
= h
->vtable
->used
;
11409 pu
= h
->vtable
->parent
->vtable
->used
;
11412 const struct elf_backend_data
*bed
;
11413 unsigned int log_file_align
;
11415 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
11416 log_file_align
= bed
->s
->log_file_align
;
11417 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
11432 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
11435 bfd_vma hstart
, hend
;
11436 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
11437 const struct elf_backend_data
*bed
;
11438 unsigned int log_file_align
;
11440 if (h
->root
.type
== bfd_link_hash_warning
)
11441 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11443 /* Take care of both those symbols that do not describe vtables as
11444 well as those that are not loaded. */
11445 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11448 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
11449 || h
->root
.type
== bfd_link_hash_defweak
);
11451 sec
= h
->root
.u
.def
.section
;
11452 hstart
= h
->root
.u
.def
.value
;
11453 hend
= hstart
+ h
->size
;
11455 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
11457 return *(bfd_boolean
*) okp
= FALSE
;
11458 bed
= get_elf_backend_data (sec
->owner
);
11459 log_file_align
= bed
->s
->log_file_align
;
11461 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11463 for (rel
= relstart
; rel
< relend
; ++rel
)
11464 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
11466 /* If the entry is in use, do nothing. */
11467 if (h
->vtable
->used
11468 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
11470 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
11471 if (h
->vtable
->used
[entry
])
11474 /* Otherwise, kill it. */
11475 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
11481 /* Mark sections containing dynamically referenced symbols. When
11482 building shared libraries, we must assume that any visible symbol is
11486 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
11488 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
11490 if (h
->root
.type
== bfd_link_hash_warning
)
11491 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11493 if ((h
->root
.type
== bfd_link_hash_defined
11494 || h
->root
.type
== bfd_link_hash_defweak
)
11496 || (!info
->executable
11498 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
11499 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
11500 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11505 /* Keep all sections containing symbols undefined on the command-line,
11506 and the section containing the entry symbol. */
11509 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
11511 struct bfd_sym_chain
*sym
;
11513 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
11515 struct elf_link_hash_entry
*h
;
11517 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
11518 FALSE
, FALSE
, FALSE
);
11521 && (h
->root
.type
== bfd_link_hash_defined
11522 || h
->root
.type
== bfd_link_hash_defweak
)
11523 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
11524 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11528 /* Do mark and sweep of unused sections. */
11531 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
11533 bfd_boolean ok
= TRUE
;
11535 elf_gc_mark_hook_fn gc_mark_hook
;
11536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11538 if (!bed
->can_gc_sections
11539 || !is_elf_hash_table (info
->hash
))
11541 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
11545 bed
->gc_keep (info
);
11547 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11548 at the .eh_frame section if we can mark the FDEs individually. */
11549 _bfd_elf_begin_eh_frame_parsing (info
);
11550 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11553 struct elf_reloc_cookie cookie
;
11555 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
11556 if (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
11558 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
11559 if (elf_section_data (sec
)->sec_info
)
11560 elf_eh_frame_section (sub
) = sec
;
11561 fini_reloc_cookie_for_section (&cookie
, sec
);
11564 _bfd_elf_end_eh_frame_parsing (info
);
11566 /* Apply transitive closure to the vtable entry usage info. */
11567 elf_link_hash_traverse (elf_hash_table (info
),
11568 elf_gc_propagate_vtable_entries_used
,
11573 /* Kill the vtable relocations that were not used. */
11574 elf_link_hash_traverse (elf_hash_table (info
),
11575 elf_gc_smash_unused_vtentry_relocs
,
11580 /* Mark dynamically referenced symbols. */
11581 if (elf_hash_table (info
)->dynamic_sections_created
)
11582 elf_link_hash_traverse (elf_hash_table (info
),
11583 bed
->gc_mark_dynamic_ref
,
11586 /* Grovel through relocs to find out who stays ... */
11587 gc_mark_hook
= bed
->gc_mark_hook
;
11588 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11592 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11595 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11596 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
11597 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11601 /* Allow the backend to mark additional target specific sections. */
11602 if (bed
->gc_mark_extra_sections
)
11603 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
11605 /* ... and mark SEC_EXCLUDE for those that go. */
11606 return elf_gc_sweep (abfd
, info
);
11609 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11612 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
11614 struct elf_link_hash_entry
*h
,
11617 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
11618 struct elf_link_hash_entry
**search
, *child
;
11619 bfd_size_type extsymcount
;
11620 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11622 /* The sh_info field of the symtab header tells us where the
11623 external symbols start. We don't care about the local symbols at
11625 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
11626 if (!elf_bad_symtab (abfd
))
11627 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
11629 sym_hashes
= elf_sym_hashes (abfd
);
11630 sym_hashes_end
= sym_hashes
+ extsymcount
;
11632 /* Hunt down the child symbol, which is in this section at the same
11633 offset as the relocation. */
11634 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
11636 if ((child
= *search
) != NULL
11637 && (child
->root
.type
== bfd_link_hash_defined
11638 || child
->root
.type
== bfd_link_hash_defweak
)
11639 && child
->root
.u
.def
.section
== sec
11640 && child
->root
.u
.def
.value
== offset
)
11644 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
11645 abfd
, sec
, (unsigned long) offset
);
11646 bfd_set_error (bfd_error_invalid_operation
);
11650 if (!child
->vtable
)
11652 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
11653 if (!child
->vtable
)
11658 /* This *should* only be the absolute section. It could potentially
11659 be that someone has defined a non-global vtable though, which
11660 would be bad. It isn't worth paging in the local symbols to be
11661 sure though; that case should simply be handled by the assembler. */
11663 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
11666 child
->vtable
->parent
= h
;
11671 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11674 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
11675 asection
*sec ATTRIBUTE_UNUSED
,
11676 struct elf_link_hash_entry
*h
,
11679 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11680 unsigned int log_file_align
= bed
->s
->log_file_align
;
11684 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
11689 if (addend
>= h
->vtable
->size
)
11691 size_t size
, bytes
, file_align
;
11692 bfd_boolean
*ptr
= h
->vtable
->used
;
11694 /* While the symbol is undefined, we have to be prepared to handle
11696 file_align
= 1 << log_file_align
;
11697 if (h
->root
.type
== bfd_link_hash_undefined
)
11698 size
= addend
+ file_align
;
11702 if (addend
>= size
)
11704 /* Oops! We've got a reference past the defined end of
11705 the table. This is probably a bug -- shall we warn? */
11706 size
= addend
+ file_align
;
11709 size
= (size
+ file_align
- 1) & -file_align
;
11711 /* Allocate one extra entry for use as a "done" flag for the
11712 consolidation pass. */
11713 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
11717 ptr
= bfd_realloc (ptr
- 1, bytes
);
11723 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
11724 * sizeof (bfd_boolean
));
11725 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
11729 ptr
= bfd_zmalloc (bytes
);
11734 /* And arrange for that done flag to be at index -1. */
11735 h
->vtable
->used
= ptr
+ 1;
11736 h
->vtable
->size
= size
;
11739 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11744 struct alloc_got_off_arg
{
11746 unsigned int got_elt_size
;
11749 /* We need a special top-level link routine to convert got reference counts
11750 to real got offsets. */
11753 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11755 struct alloc_got_off_arg
*gofarg
= arg
;
11757 if (h
->root
.type
== bfd_link_hash_warning
)
11758 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11760 if (h
->got
.refcount
> 0)
11762 h
->got
.offset
= gofarg
->gotoff
;
11763 gofarg
->gotoff
+= gofarg
->got_elt_size
;
11766 h
->got
.offset
= (bfd_vma
) -1;
11771 /* And an accompanying bit to work out final got entry offsets once
11772 we're done. Should be called from final_link. */
11775 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11776 struct bfd_link_info
*info
)
11779 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11781 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
11782 struct alloc_got_off_arg gofarg
;
11784 if (! is_elf_hash_table (info
->hash
))
11787 /* The GOT offset is relative to the .got section, but the GOT header is
11788 put into the .got.plt section, if the backend uses it. */
11789 if (bed
->want_got_plt
)
11792 gotoff
= bed
->got_header_size
;
11794 /* Do the local .got entries first. */
11795 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11797 bfd_signed_vma
*local_got
;
11798 bfd_size_type j
, locsymcount
;
11799 Elf_Internal_Shdr
*symtab_hdr
;
11801 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11804 local_got
= elf_local_got_refcounts (i
);
11808 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11809 if (elf_bad_symtab (i
))
11810 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11812 locsymcount
= symtab_hdr
->sh_info
;
11814 for (j
= 0; j
< locsymcount
; ++j
)
11816 if (local_got
[j
] > 0)
11818 local_got
[j
] = gotoff
;
11819 gotoff
+= got_elt_size
;
11822 local_got
[j
] = (bfd_vma
) -1;
11826 /* Then the global .got entries. .plt refcounts are handled by
11827 adjust_dynamic_symbol */
11828 gofarg
.gotoff
= gotoff
;
11829 gofarg
.got_elt_size
= got_elt_size
;
11830 elf_link_hash_traverse (elf_hash_table (info
),
11831 elf_gc_allocate_got_offsets
,
11836 /* Many folk need no more in the way of final link than this, once
11837 got entry reference counting is enabled. */
11840 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11842 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11845 /* Invoke the regular ELF backend linker to do all the work. */
11846 return bfd_elf_final_link (abfd
, info
);
11850 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11852 struct elf_reloc_cookie
*rcookie
= cookie
;
11854 if (rcookie
->bad_symtab
)
11855 rcookie
->rel
= rcookie
->rels
;
11857 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11859 unsigned long r_symndx
;
11861 if (! rcookie
->bad_symtab
)
11862 if (rcookie
->rel
->r_offset
> offset
)
11864 if (rcookie
->rel
->r_offset
!= offset
)
11867 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11868 if (r_symndx
== SHN_UNDEF
)
11871 if (r_symndx
>= rcookie
->locsymcount
11872 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11874 struct elf_link_hash_entry
*h
;
11876 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11878 while (h
->root
.type
== bfd_link_hash_indirect
11879 || h
->root
.type
== bfd_link_hash_warning
)
11880 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11882 if ((h
->root
.type
== bfd_link_hash_defined
11883 || h
->root
.type
== bfd_link_hash_defweak
)
11884 && elf_discarded_section (h
->root
.u
.def
.section
))
11891 /* It's not a relocation against a global symbol,
11892 but it could be a relocation against a local
11893 symbol for a discarded section. */
11895 Elf_Internal_Sym
*isym
;
11897 /* Need to: get the symbol; get the section. */
11898 isym
= &rcookie
->locsyms
[r_symndx
];
11899 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11900 if (isec
!= NULL
&& elf_discarded_section (isec
))
11908 /* Discard unneeded references to discarded sections.
11909 Returns TRUE if any section's size was changed. */
11910 /* This function assumes that the relocations are in sorted order,
11911 which is true for all known assemblers. */
11914 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11916 struct elf_reloc_cookie cookie
;
11917 asection
*stab
, *eh
;
11918 const struct elf_backend_data
*bed
;
11920 bfd_boolean ret
= FALSE
;
11922 if (info
->traditional_format
11923 || !is_elf_hash_table (info
->hash
))
11926 _bfd_elf_begin_eh_frame_parsing (info
);
11927 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11929 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11932 bed
= get_elf_backend_data (abfd
);
11934 if ((abfd
->flags
& DYNAMIC
) != 0)
11938 if (!info
->relocatable
)
11940 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
11943 || bfd_is_abs_section (eh
->output_section
)))
11947 stab
= bfd_get_section_by_name (abfd
, ".stab");
11949 && (stab
->size
== 0
11950 || bfd_is_abs_section (stab
->output_section
)
11951 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
11956 && bed
->elf_backend_discard_info
== NULL
)
11959 if (!init_reloc_cookie (&cookie
, info
, abfd
))
11963 && stab
->reloc_count
> 0
11964 && init_reloc_cookie_rels (&cookie
, info
, abfd
, stab
))
11966 if (_bfd_discard_section_stabs (abfd
, stab
,
11967 elf_section_data (stab
)->sec_info
,
11968 bfd_elf_reloc_symbol_deleted_p
,
11971 fini_reloc_cookie_rels (&cookie
, stab
);
11975 && init_reloc_cookie_rels (&cookie
, info
, abfd
, eh
))
11977 _bfd_elf_parse_eh_frame (abfd
, info
, eh
, &cookie
);
11978 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11979 bfd_elf_reloc_symbol_deleted_p
,
11982 fini_reloc_cookie_rels (&cookie
, eh
);
11985 if (bed
->elf_backend_discard_info
!= NULL
11986 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11989 fini_reloc_cookie (&cookie
, abfd
);
11991 _bfd_elf_end_eh_frame_parsing (info
);
11993 if (info
->eh_frame_hdr
11994 && !info
->relocatable
11995 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12001 /* For a SHT_GROUP section, return the group signature. For other
12002 sections, return the normal section name. */
12004 static const char *
12005 section_signature (asection
*sec
)
12007 if ((sec
->flags
& SEC_GROUP
) != 0
12008 && elf_next_in_group (sec
) != NULL
12009 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
12010 return elf_group_name (elf_next_in_group (sec
));
12015 _bfd_elf_section_already_linked (bfd
*abfd
, asection
*sec
,
12016 struct bfd_link_info
*info
)
12019 const char *name
, *p
;
12020 struct bfd_section_already_linked
*l
;
12021 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12023 if (sec
->output_section
== bfd_abs_section_ptr
)
12026 flags
= sec
->flags
;
12028 /* Return if it isn't a linkonce section. A comdat group section
12029 also has SEC_LINK_ONCE set. */
12030 if ((flags
& SEC_LINK_ONCE
) == 0)
12033 /* Don't put group member sections on our list of already linked
12034 sections. They are handled as a group via their group section. */
12035 if (elf_sec_group (sec
) != NULL
)
12038 /* FIXME: When doing a relocatable link, we may have trouble
12039 copying relocations in other sections that refer to local symbols
12040 in the section being discarded. Those relocations will have to
12041 be converted somehow; as of this writing I'm not sure that any of
12042 the backends handle that correctly.
12044 It is tempting to instead not discard link once sections when
12045 doing a relocatable link (technically, they should be discarded
12046 whenever we are building constructors). However, that fails,
12047 because the linker winds up combining all the link once sections
12048 into a single large link once section, which defeats the purpose
12049 of having link once sections in the first place.
12051 Also, not merging link once sections in a relocatable link
12052 causes trouble for MIPS ELF, which relies on link once semantics
12053 to handle the .reginfo section correctly. */
12055 name
= section_signature (sec
);
12057 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12058 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12063 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
12065 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12067 /* We may have 2 different types of sections on the list: group
12068 sections and linkonce sections. Match like sections. */
12069 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12070 && strcmp (name
, section_signature (l
->sec
)) == 0
12071 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
12073 /* The section has already been linked. See if we should
12074 issue a warning. */
12075 switch (flags
& SEC_LINK_DUPLICATES
)
12080 case SEC_LINK_DUPLICATES_DISCARD
:
12083 case SEC_LINK_DUPLICATES_ONE_ONLY
:
12084 (*_bfd_error_handler
)
12085 (_("%B: ignoring duplicate section `%A'"),
12089 case SEC_LINK_DUPLICATES_SAME_SIZE
:
12090 if (sec
->size
!= l
->sec
->size
)
12091 (*_bfd_error_handler
)
12092 (_("%B: duplicate section `%A' has different size"),
12096 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
12097 if (sec
->size
!= l
->sec
->size
)
12098 (*_bfd_error_handler
)
12099 (_("%B: duplicate section `%A' has different size"),
12101 else if (sec
->size
!= 0)
12103 bfd_byte
*sec_contents
, *l_sec_contents
;
12105 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
12106 (*_bfd_error_handler
)
12107 (_("%B: warning: could not read contents of section `%A'"),
12109 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
12111 (*_bfd_error_handler
)
12112 (_("%B: warning: could not read contents of section `%A'"),
12113 l
->sec
->owner
, l
->sec
);
12114 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
12115 (*_bfd_error_handler
)
12116 (_("%B: warning: duplicate section `%A' has different contents"),
12120 free (sec_contents
);
12121 if (l_sec_contents
)
12122 free (l_sec_contents
);
12127 /* Set the output_section field so that lang_add_section
12128 does not create a lang_input_section structure for this
12129 section. Since there might be a symbol in the section
12130 being discarded, we must retain a pointer to the section
12131 which we are really going to use. */
12132 sec
->output_section
= bfd_abs_section_ptr
;
12133 sec
->kept_section
= l
->sec
;
12135 if (flags
& SEC_GROUP
)
12137 asection
*first
= elf_next_in_group (sec
);
12138 asection
*s
= first
;
12142 s
->output_section
= bfd_abs_section_ptr
;
12143 /* Record which group discards it. */
12144 s
->kept_section
= l
->sec
;
12145 s
= elf_next_in_group (s
);
12146 /* These lists are circular. */
12156 /* A single member comdat group section may be discarded by a
12157 linkonce section and vice versa. */
12159 if ((flags
& SEC_GROUP
) != 0)
12161 asection
*first
= elf_next_in_group (sec
);
12163 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12164 /* Check this single member group against linkonce sections. */
12165 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12166 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12167 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
12168 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12170 first
->output_section
= bfd_abs_section_ptr
;
12171 first
->kept_section
= l
->sec
;
12172 sec
->output_section
= bfd_abs_section_ptr
;
12177 /* Check this linkonce section against single member groups. */
12178 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12179 if (l
->sec
->flags
& SEC_GROUP
)
12181 asection
*first
= elf_next_in_group (l
->sec
);
12184 && elf_next_in_group (first
) == first
12185 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12187 sec
->output_section
= bfd_abs_section_ptr
;
12188 sec
->kept_section
= first
;
12193 /* This is the first section with this name. Record it. */
12194 if (! bfd_section_already_linked_table_insert (already_linked_list
, sec
))
12195 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E"));
12199 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12201 return sym
->st_shndx
== SHN_COMMON
;
12205 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12211 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12213 return bfd_com_section_ptr
;