1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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
->default_use_rela_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
->default_use_rela_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
622 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
626 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
627 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
629 /* We can still bfd_release here as nothing has done another
630 bfd_alloc. We can't do this later in this function. */
631 bfd_release (input_bfd
, entry
);
636 name
= (bfd_elf_string_from_elf_section
637 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
638 entry
->isym
.st_name
));
640 dynstr
= elf_hash_table (info
)->dynstr
;
643 /* Create a strtab to hold the dynamic symbol names. */
644 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
649 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
650 if (dynstr_index
== (unsigned long) -1)
652 entry
->isym
.st_name
= dynstr_index
;
654 eht
= elf_hash_table (info
);
656 entry
->next
= eht
->dynlocal
;
657 eht
->dynlocal
= entry
;
658 entry
->input_bfd
= input_bfd
;
659 entry
->input_indx
= input_indx
;
662 /* Whatever binding the symbol had before, it's now local. */
664 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
666 /* The dynindx will be set at the end of size_dynamic_sections. */
671 /* Return the dynindex of a local dynamic symbol. */
674 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
678 struct elf_link_local_dynamic_entry
*e
;
680 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
681 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
686 /* This function is used to renumber the dynamic symbols, if some of
687 them are removed because they are marked as local. This is called
688 via elf_link_hash_traverse. */
691 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
694 size_t *count
= data
;
696 if (h
->root
.type
== bfd_link_hash_warning
)
697 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
702 if (h
->dynindx
!= -1)
703 h
->dynindx
= ++(*count
);
709 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
710 STB_LOCAL binding. */
713 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
716 size_t *count
= data
;
718 if (h
->root
.type
== bfd_link_hash_warning
)
719 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
721 if (!h
->forced_local
)
724 if (h
->dynindx
!= -1)
725 h
->dynindx
= ++(*count
);
730 /* Return true if the dynamic symbol for a given section should be
731 omitted when creating a shared library. */
733 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
734 struct bfd_link_info
*info
,
737 struct elf_link_hash_table
*htab
;
739 switch (elf_section_data (p
)->this_hdr
.sh_type
)
743 /* If sh_type is yet undecided, assume it could be
744 SHT_PROGBITS/SHT_NOBITS. */
746 htab
= elf_hash_table (info
);
747 if (p
== htab
->tls_sec
)
750 if (htab
->text_index_section
!= NULL
)
751 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
753 if (strcmp (p
->name
, ".got") == 0
754 || strcmp (p
->name
, ".got.plt") == 0
755 || strcmp (p
->name
, ".plt") == 0)
759 if (htab
->dynobj
!= NULL
760 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
761 && (ip
->flags
& SEC_LINKER_CREATED
)
762 && ip
->output_section
== p
)
767 /* There shouldn't be section relative relocations
768 against any other section. */
774 /* Assign dynsym indices. In a shared library we generate a section
775 symbol for each output section, which come first. Next come symbols
776 which have been forced to local binding. Then all of the back-end
777 allocated local dynamic syms, followed by the rest of the global
781 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
782 struct bfd_link_info
*info
,
783 unsigned long *section_sym_count
)
785 unsigned long dynsymcount
= 0;
787 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
789 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
791 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
792 if ((p
->flags
& SEC_EXCLUDE
) == 0
793 && (p
->flags
& SEC_ALLOC
) != 0
794 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
795 elf_section_data (p
)->dynindx
= ++dynsymcount
;
797 elf_section_data (p
)->dynindx
= 0;
799 *section_sym_count
= dynsymcount
;
801 elf_link_hash_traverse (elf_hash_table (info
),
802 elf_link_renumber_local_hash_table_dynsyms
,
805 if (elf_hash_table (info
)->dynlocal
)
807 struct elf_link_local_dynamic_entry
*p
;
808 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
809 p
->dynindx
= ++dynsymcount
;
812 elf_link_hash_traverse (elf_hash_table (info
),
813 elf_link_renumber_hash_table_dynsyms
,
816 /* There is an unused NULL entry at the head of the table which
817 we must account for in our count. Unless there weren't any
818 symbols, which means we'll have no table at all. */
819 if (dynsymcount
!= 0)
822 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
826 /* This function is called when we want to define a new symbol. It
827 handles the various cases which arise when we find a definition in
828 a dynamic object, or when there is already a definition in a
829 dynamic object. The new symbol is described by NAME, SYM, PSEC,
830 and PVALUE. We set SYM_HASH to the hash table entry. We set
831 OVERRIDE if the old symbol is overriding a new definition. We set
832 TYPE_CHANGE_OK if it is OK for the type to change. We set
833 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
834 change, we mean that we shouldn't warn if the type or size does
835 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
836 object is overridden by a regular object. */
839 _bfd_elf_merge_symbol (bfd
*abfd
,
840 struct bfd_link_info
*info
,
842 Elf_Internal_Sym
*sym
,
845 unsigned int *pold_alignment
,
846 struct elf_link_hash_entry
**sym_hash
,
848 bfd_boolean
*override
,
849 bfd_boolean
*type_change_ok
,
850 bfd_boolean
*size_change_ok
)
852 asection
*sec
, *oldsec
;
853 struct elf_link_hash_entry
*h
;
854 struct elf_link_hash_entry
*flip
;
857 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
858 bfd_boolean newweak
, oldweak
;
859 const struct elf_backend_data
*bed
;
865 bind
= ELF_ST_BIND (sym
->st_info
);
867 /* Silently discard TLS symbols from --just-syms. There's no way to
868 combine a static TLS block with a new TLS block for this executable. */
869 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
870 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
876 if (! bfd_is_und_section (sec
))
877 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
879 h
= ((struct elf_link_hash_entry
*)
880 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
885 /* This code is for coping with dynamic objects, and is only useful
886 if we are doing an ELF link. */
887 if (info
->hash
->creator
!= abfd
->xvec
)
890 /* For merging, we only care about real symbols. */
892 while (h
->root
.type
== bfd_link_hash_indirect
893 || h
->root
.type
== bfd_link_hash_warning
)
894 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
896 /* We have to check it for every instance since the first few may be
897 refereences and not all compilers emit symbol type for undefined
899 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
901 /* If we just created the symbol, mark it as being an ELF symbol.
902 Other than that, there is nothing to do--there is no merge issue
903 with a newly defined symbol--so we just return. */
905 if (h
->root
.type
== bfd_link_hash_new
)
911 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
914 switch (h
->root
.type
)
921 case bfd_link_hash_undefined
:
922 case bfd_link_hash_undefweak
:
923 oldbfd
= h
->root
.u
.undef
.abfd
;
927 case bfd_link_hash_defined
:
928 case bfd_link_hash_defweak
:
929 oldbfd
= h
->root
.u
.def
.section
->owner
;
930 oldsec
= h
->root
.u
.def
.section
;
933 case bfd_link_hash_common
:
934 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
935 oldsec
= h
->root
.u
.c
.p
->section
;
939 /* In cases involving weak versioned symbols, we may wind up trying
940 to merge a symbol with itself. Catch that here, to avoid the
941 confusion that results if we try to override a symbol with
942 itself. The additional tests catch cases like
943 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
944 dynamic object, which we do want to handle here. */
946 && ((abfd
->flags
& DYNAMIC
) == 0
950 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
951 respectively, is from a dynamic object. */
953 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
957 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
958 else if (oldsec
!= NULL
)
960 /* This handles the special SHN_MIPS_{TEXT,DATA} section
961 indices used by MIPS ELF. */
962 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
965 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
966 respectively, appear to be a definition rather than reference. */
968 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
970 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
971 && h
->root
.type
!= bfd_link_hash_undefweak
972 && h
->root
.type
!= bfd_link_hash_common
);
974 bed
= get_elf_backend_data (abfd
);
975 /* When we try to create a default indirect symbol from the dynamic
976 definition with the default version, we skip it if its type and
977 the type of existing regular definition mismatch. We only do it
978 if the existing regular definition won't be dynamic. */
979 if (pold_alignment
== NULL
981 && !info
->export_dynamic
986 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
987 && ELF_ST_TYPE (sym
->st_info
) != h
->type
988 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
989 && h
->type
!= STT_NOTYPE
990 && !(bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
991 && bed
->is_function_type (h
->type
)))
997 /* Check TLS symbol. We don't check undefined symbol introduced by
999 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
1000 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1004 bfd_boolean ntdef
, tdef
;
1005 asection
*ntsec
, *tsec
;
1007 if (h
->type
== STT_TLS
)
1027 (*_bfd_error_handler
)
1028 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1029 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1030 else if (!tdef
&& !ntdef
)
1031 (*_bfd_error_handler
)
1032 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1033 tbfd
, ntbfd
, h
->root
.root
.string
);
1035 (*_bfd_error_handler
)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1037 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1039 (*_bfd_error_handler
)
1040 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1041 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1043 bfd_set_error (bfd_error_bad_value
);
1047 /* We need to remember if a symbol has a definition in a dynamic
1048 object or is weak in all dynamic objects. Internal and hidden
1049 visibility will make it unavailable to dynamic objects. */
1050 if (newdyn
&& !h
->dynamic_def
)
1052 if (!bfd_is_und_section (sec
))
1056 /* Check if this symbol is weak in all dynamic objects. If it
1057 is the first time we see it in a dynamic object, we mark
1058 if it is weak. Otherwise, we clear it. */
1059 if (!h
->ref_dynamic
)
1061 if (bind
== STB_WEAK
)
1062 h
->dynamic_weak
= 1;
1064 else if (bind
!= STB_WEAK
)
1065 h
->dynamic_weak
= 0;
1069 /* If the old symbol has non-default visibility, we ignore the new
1070 definition from a dynamic object. */
1072 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1073 && !bfd_is_und_section (sec
))
1076 /* Make sure this symbol is dynamic. */
1078 /* A protected symbol has external availability. Make sure it is
1079 recorded as dynamic.
1081 FIXME: Should we check type and size for protected symbol? */
1082 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1083 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1088 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1091 /* If the new symbol with non-default visibility comes from a
1092 relocatable file and the old definition comes from a dynamic
1093 object, we remove the old definition. */
1094 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1096 /* Handle the case where the old dynamic definition is
1097 default versioned. We need to copy the symbol info from
1098 the symbol with default version to the normal one if it
1099 was referenced before. */
1102 const struct elf_backend_data
*bed
1103 = get_elf_backend_data (abfd
);
1104 struct elf_link_hash_entry
*vh
= *sym_hash
;
1105 vh
->root
.type
= h
->root
.type
;
1106 h
->root
.type
= bfd_link_hash_indirect
;
1107 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1108 /* Protected symbols will override the dynamic definition
1109 with default version. */
1110 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1112 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1113 vh
->dynamic_def
= 1;
1114 vh
->ref_dynamic
= 1;
1118 h
->root
.type
= vh
->root
.type
;
1119 vh
->ref_dynamic
= 0;
1120 /* We have to hide it here since it was made dynamic
1121 global with extra bits when the symbol info was
1122 copied from the old dynamic definition. */
1123 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1131 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1132 && bfd_is_und_section (sec
))
1134 /* If the new symbol is undefined and the old symbol was
1135 also undefined before, we need to make sure
1136 _bfd_generic_link_add_one_symbol doesn't mess
1137 up the linker hash table undefs list. Since the old
1138 definition came from a dynamic object, it is still on the
1140 h
->root
.type
= bfd_link_hash_undefined
;
1141 h
->root
.u
.undef
.abfd
= abfd
;
1145 h
->root
.type
= bfd_link_hash_new
;
1146 h
->root
.u
.undef
.abfd
= NULL
;
1155 /* FIXME: Should we check type and size for protected symbol? */
1161 /* Differentiate strong and weak symbols. */
1162 newweak
= bind
== STB_WEAK
;
1163 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1164 || h
->root
.type
== bfd_link_hash_undefweak
);
1166 /* If a new weak symbol definition comes from a regular file and the
1167 old symbol comes from a dynamic library, we treat the new one as
1168 strong. Similarly, an old weak symbol definition from a regular
1169 file is treated as strong when the new symbol comes from a dynamic
1170 library. Further, an old weak symbol from a dynamic library is
1171 treated as strong if the new symbol is from a dynamic library.
1172 This reflects the way glibc's ld.so works.
1174 Do this before setting *type_change_ok or *size_change_ok so that
1175 we warn properly when dynamic library symbols are overridden. */
1177 if (newdef
&& !newdyn
&& olddyn
)
1179 if (olddef
&& newdyn
)
1182 /* Allow changes between different types of funciton symbol. */
1183 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
1184 && bed
->is_function_type (h
->type
))
1185 *type_change_ok
= TRUE
;
1187 /* It's OK to change the type if either the existing symbol or the
1188 new symbol is weak. A type change is also OK if the old symbol
1189 is undefined and the new symbol is defined. */
1194 && h
->root
.type
== bfd_link_hash_undefined
))
1195 *type_change_ok
= TRUE
;
1197 /* It's OK to change the size if either the existing symbol or the
1198 new symbol is weak, or if the old symbol is undefined. */
1201 || h
->root
.type
== bfd_link_hash_undefined
)
1202 *size_change_ok
= TRUE
;
1204 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1205 symbol, respectively, appears to be a common symbol in a dynamic
1206 object. If a symbol appears in an uninitialized section, and is
1207 not weak, and is not a function, then it may be a common symbol
1208 which was resolved when the dynamic object was created. We want
1209 to treat such symbols specially, because they raise special
1210 considerations when setting the symbol size: if the symbol
1211 appears as a common symbol in a regular object, and the size in
1212 the regular object is larger, we must make sure that we use the
1213 larger size. This problematic case can always be avoided in C,
1214 but it must be handled correctly when using Fortran shared
1217 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1218 likewise for OLDDYNCOMMON and OLDDEF.
1220 Note that this test is just a heuristic, and that it is quite
1221 possible to have an uninitialized symbol in a shared object which
1222 is really a definition, rather than a common symbol. This could
1223 lead to some minor confusion when the symbol really is a common
1224 symbol in some regular object. However, I think it will be
1230 && (sec
->flags
& SEC_ALLOC
) != 0
1231 && (sec
->flags
& SEC_LOAD
) == 0
1233 && !bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
1234 newdyncommon
= TRUE
;
1236 newdyncommon
= FALSE
;
1240 && h
->root
.type
== bfd_link_hash_defined
1242 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1243 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1245 && !bed
->is_function_type (h
->type
))
1246 olddyncommon
= TRUE
;
1248 olddyncommon
= FALSE
;
1250 /* We now know everything about the old and new symbols. We ask the
1251 backend to check if we can merge them. */
1252 if (bed
->merge_symbol
1253 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1254 pold_alignment
, skip
, override
,
1255 type_change_ok
, size_change_ok
,
1256 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1258 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1262 /* If both the old and the new symbols look like common symbols in a
1263 dynamic object, set the size of the symbol to the larger of the
1268 && sym
->st_size
!= h
->size
)
1270 /* Since we think we have two common symbols, issue a multiple
1271 common warning if desired. Note that we only warn if the
1272 size is different. If the size is the same, we simply let
1273 the old symbol override the new one as normally happens with
1274 symbols defined in dynamic objects. */
1276 if (! ((*info
->callbacks
->multiple_common
)
1277 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1278 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1281 if (sym
->st_size
> h
->size
)
1282 h
->size
= sym
->st_size
;
1284 *size_change_ok
= TRUE
;
1287 /* If we are looking at a dynamic object, and we have found a
1288 definition, we need to see if the symbol was already defined by
1289 some other object. If so, we want to use the existing
1290 definition, and we do not want to report a multiple symbol
1291 definition error; we do this by clobbering *PSEC to be
1292 bfd_und_section_ptr.
1294 We treat a common symbol as a definition if the symbol in the
1295 shared library is a function, since common symbols always
1296 represent variables; this can cause confusion in principle, but
1297 any such confusion would seem to indicate an erroneous program or
1298 shared library. We also permit a common symbol in a regular
1299 object to override a weak symbol in a shared object. */
1304 || (h
->root
.type
== bfd_link_hash_common
1306 || bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))))))
1310 newdyncommon
= FALSE
;
1312 *psec
= sec
= bfd_und_section_ptr
;
1313 *size_change_ok
= TRUE
;
1315 /* If we get here when the old symbol is a common symbol, then
1316 we are explicitly letting it override a weak symbol or
1317 function in a dynamic object, and we don't want to warn about
1318 a type change. If the old symbol is a defined symbol, a type
1319 change warning may still be appropriate. */
1321 if (h
->root
.type
== bfd_link_hash_common
)
1322 *type_change_ok
= TRUE
;
1325 /* Handle the special case of an old common symbol merging with a
1326 new symbol which looks like a common symbol in a shared object.
1327 We change *PSEC and *PVALUE to make the new symbol look like a
1328 common symbol, and let _bfd_generic_link_add_one_symbol do the
1332 && h
->root
.type
== bfd_link_hash_common
)
1336 newdyncommon
= FALSE
;
1337 *pvalue
= sym
->st_size
;
1338 *psec
= sec
= bed
->common_section (oldsec
);
1339 *size_change_ok
= TRUE
;
1342 /* Skip weak definitions of symbols that are already defined. */
1343 if (newdef
&& olddef
&& newweak
)
1346 /* If the old symbol is from a dynamic object, and the new symbol is
1347 a definition which is not from a dynamic object, then the new
1348 symbol overrides the old symbol. Symbols from regular files
1349 always take precedence over symbols from dynamic objects, even if
1350 they are defined after the dynamic object in the link.
1352 As above, we again permit a common symbol in a regular object to
1353 override a definition in a shared object if the shared object
1354 symbol is a function or is weak. */
1359 || (bfd_is_com_section (sec
)
1361 || bed
->is_function_type (h
->type
))))
1366 /* Change the hash table entry to undefined, and let
1367 _bfd_generic_link_add_one_symbol do the right thing with the
1370 h
->root
.type
= bfd_link_hash_undefined
;
1371 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1372 *size_change_ok
= TRUE
;
1375 olddyncommon
= FALSE
;
1377 /* We again permit a type change when a common symbol may be
1378 overriding a function. */
1380 if (bfd_is_com_section (sec
))
1381 *type_change_ok
= TRUE
;
1383 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1386 /* This union may have been set to be non-NULL when this symbol
1387 was seen in a dynamic object. We must force the union to be
1388 NULL, so that it is correct for a regular symbol. */
1389 h
->verinfo
.vertree
= NULL
;
1392 /* Handle the special case of a new common symbol merging with an
1393 old symbol that looks like it might be a common symbol defined in
1394 a shared object. Note that we have already handled the case in
1395 which a new common symbol should simply override the definition
1396 in the shared library. */
1399 && bfd_is_com_section (sec
)
1402 /* It would be best if we could set the hash table entry to a
1403 common symbol, but we don't know what to use for the section
1404 or the alignment. */
1405 if (! ((*info
->callbacks
->multiple_common
)
1406 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1407 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1410 /* If the presumed common symbol in the dynamic object is
1411 larger, pretend that the new symbol has its size. */
1413 if (h
->size
> *pvalue
)
1416 /* We need to remember the alignment required by the symbol
1417 in the dynamic object. */
1418 BFD_ASSERT (pold_alignment
);
1419 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1422 olddyncommon
= FALSE
;
1424 h
->root
.type
= bfd_link_hash_undefined
;
1425 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1427 *size_change_ok
= TRUE
;
1428 *type_change_ok
= TRUE
;
1430 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1433 h
->verinfo
.vertree
= NULL
;
1438 /* Handle the case where we had a versioned symbol in a dynamic
1439 library and now find a definition in a normal object. In this
1440 case, we make the versioned symbol point to the normal one. */
1441 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1442 flip
->root
.type
= h
->root
.type
;
1443 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1444 h
->root
.type
= bfd_link_hash_indirect
;
1445 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1446 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1450 flip
->ref_dynamic
= 1;
1457 /* This function is called to create an indirect symbol from the
1458 default for the symbol with the default version if needed. The
1459 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1460 set DYNSYM if the new indirect symbol is dynamic. */
1463 _bfd_elf_add_default_symbol (bfd
*abfd
,
1464 struct bfd_link_info
*info
,
1465 struct elf_link_hash_entry
*h
,
1467 Elf_Internal_Sym
*sym
,
1470 bfd_boolean
*dynsym
,
1471 bfd_boolean override
)
1473 bfd_boolean type_change_ok
;
1474 bfd_boolean size_change_ok
;
1477 struct elf_link_hash_entry
*hi
;
1478 struct bfd_link_hash_entry
*bh
;
1479 const struct elf_backend_data
*bed
;
1480 bfd_boolean collect
;
1481 bfd_boolean dynamic
;
1483 size_t len
, shortlen
;
1486 /* If this symbol has a version, and it is the default version, we
1487 create an indirect symbol from the default name to the fully
1488 decorated name. This will cause external references which do not
1489 specify a version to be bound to this version of the symbol. */
1490 p
= strchr (name
, ELF_VER_CHR
);
1491 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1496 /* We are overridden by an old definition. We need to check if we
1497 need to create the indirect symbol from the default name. */
1498 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1500 BFD_ASSERT (hi
!= NULL
);
1503 while (hi
->root
.type
== bfd_link_hash_indirect
1504 || hi
->root
.type
== bfd_link_hash_warning
)
1506 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1512 bed
= get_elf_backend_data (abfd
);
1513 collect
= bed
->collect
;
1514 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1516 shortlen
= p
- name
;
1517 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1518 if (shortname
== NULL
)
1520 memcpy (shortname
, name
, shortlen
);
1521 shortname
[shortlen
] = '\0';
1523 /* We are going to create a new symbol. Merge it with any existing
1524 symbol with this name. For the purposes of the merge, act as
1525 though we were defining the symbol we just defined, although we
1526 actually going to define an indirect symbol. */
1527 type_change_ok
= FALSE
;
1528 size_change_ok
= FALSE
;
1530 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1531 NULL
, &hi
, &skip
, &override
,
1532 &type_change_ok
, &size_change_ok
))
1541 if (! (_bfd_generic_link_add_one_symbol
1542 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1543 0, name
, FALSE
, collect
, &bh
)))
1545 hi
= (struct elf_link_hash_entry
*) bh
;
1549 /* In this case the symbol named SHORTNAME is overriding the
1550 indirect symbol we want to add. We were planning on making
1551 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1552 is the name without a version. NAME is the fully versioned
1553 name, and it is the default version.
1555 Overriding means that we already saw a definition for the
1556 symbol SHORTNAME in a regular object, and it is overriding
1557 the symbol defined in the dynamic object.
1559 When this happens, we actually want to change NAME, the
1560 symbol we just added, to refer to SHORTNAME. This will cause
1561 references to NAME in the shared object to become references
1562 to SHORTNAME in the regular object. This is what we expect
1563 when we override a function in a shared object: that the
1564 references in the shared object will be mapped to the
1565 definition in the regular object. */
1567 while (hi
->root
.type
== bfd_link_hash_indirect
1568 || hi
->root
.type
== bfd_link_hash_warning
)
1569 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1571 h
->root
.type
= bfd_link_hash_indirect
;
1572 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1576 hi
->ref_dynamic
= 1;
1580 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1585 /* Now set HI to H, so that the following code will set the
1586 other fields correctly. */
1590 /* Check if HI is a warning symbol. */
1591 if (hi
->root
.type
== bfd_link_hash_warning
)
1592 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1594 /* If there is a duplicate definition somewhere, then HI may not
1595 point to an indirect symbol. We will have reported an error to
1596 the user in that case. */
1598 if (hi
->root
.type
== bfd_link_hash_indirect
)
1600 struct elf_link_hash_entry
*ht
;
1602 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1603 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1605 /* See if the new flags lead us to realize that the symbol must
1617 if (hi
->ref_regular
)
1623 /* We also need to define an indirection from the nondefault version
1627 len
= strlen (name
);
1628 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1629 if (shortname
== NULL
)
1631 memcpy (shortname
, name
, shortlen
);
1632 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1634 /* Once again, merge with any existing symbol. */
1635 type_change_ok
= FALSE
;
1636 size_change_ok
= FALSE
;
1638 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1639 NULL
, &hi
, &skip
, &override
,
1640 &type_change_ok
, &size_change_ok
))
1648 /* Here SHORTNAME is a versioned name, so we don't expect to see
1649 the type of override we do in the case above unless it is
1650 overridden by a versioned definition. */
1651 if (hi
->root
.type
!= bfd_link_hash_defined
1652 && hi
->root
.type
!= bfd_link_hash_defweak
)
1653 (*_bfd_error_handler
)
1654 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1660 if (! (_bfd_generic_link_add_one_symbol
1661 (info
, abfd
, shortname
, BSF_INDIRECT
,
1662 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1664 hi
= (struct elf_link_hash_entry
*) bh
;
1666 /* If there is a duplicate definition somewhere, then HI may not
1667 point to an indirect symbol. We will have reported an error
1668 to the user in that case. */
1670 if (hi
->root
.type
== bfd_link_hash_indirect
)
1672 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1674 /* See if the new flags lead us to realize that the symbol
1686 if (hi
->ref_regular
)
1696 /* This routine is used to export all defined symbols into the dynamic
1697 symbol table. It is called via elf_link_hash_traverse. */
1700 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1702 struct elf_info_failed
*eif
= data
;
1704 /* Ignore this if we won't export it. */
1705 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1708 /* Ignore indirect symbols. These are added by the versioning code. */
1709 if (h
->root
.type
== bfd_link_hash_indirect
)
1712 if (h
->root
.type
== bfd_link_hash_warning
)
1713 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1715 if (h
->dynindx
== -1
1719 struct bfd_elf_version_tree
*t
;
1720 struct bfd_elf_version_expr
*d
;
1722 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1724 if (t
->globals
.list
!= NULL
)
1726 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1731 if (t
->locals
.list
!= NULL
)
1733 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1742 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1753 /* Look through the symbols which are defined in other shared
1754 libraries and referenced here. Update the list of version
1755 dependencies. This will be put into the .gnu.version_r section.
1756 This function is called via elf_link_hash_traverse. */
1759 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1762 struct elf_find_verdep_info
*rinfo
= data
;
1763 Elf_Internal_Verneed
*t
;
1764 Elf_Internal_Vernaux
*a
;
1767 if (h
->root
.type
== bfd_link_hash_warning
)
1768 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1770 /* We only care about symbols defined in shared objects with version
1775 || h
->verinfo
.verdef
== NULL
)
1778 /* See if we already know about this version. */
1779 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1781 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1784 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1785 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1791 /* This is a new version. Add it to tree we are building. */
1796 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1799 rinfo
->failed
= TRUE
;
1803 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1804 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1805 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1809 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1812 rinfo
->failed
= TRUE
;
1816 /* Note that we are copying a string pointer here, and testing it
1817 above. If bfd_elf_string_from_elf_section is ever changed to
1818 discard the string data when low in memory, this will have to be
1820 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1822 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1823 a
->vna_nextptr
= t
->vn_auxptr
;
1825 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1828 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1835 /* Figure out appropriate versions for all the symbols. We may not
1836 have the version number script until we have read all of the input
1837 files, so until that point we don't know which symbols should be
1838 local. This function is called via elf_link_hash_traverse. */
1841 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1843 struct elf_assign_sym_version_info
*sinfo
;
1844 struct bfd_link_info
*info
;
1845 const struct elf_backend_data
*bed
;
1846 struct elf_info_failed eif
;
1853 if (h
->root
.type
== bfd_link_hash_warning
)
1854 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1856 /* Fix the symbol flags. */
1859 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1862 sinfo
->failed
= TRUE
;
1866 /* We only need version numbers for symbols defined in regular
1868 if (!h
->def_regular
)
1871 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1872 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1873 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1875 struct bfd_elf_version_tree
*t
;
1880 /* There are two consecutive ELF_VER_CHR characters if this is
1881 not a hidden symbol. */
1883 if (*p
== ELF_VER_CHR
)
1889 /* If there is no version string, we can just return out. */
1897 /* Look for the version. If we find it, it is no longer weak. */
1898 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1900 if (strcmp (t
->name
, p
) == 0)
1904 struct bfd_elf_version_expr
*d
;
1906 len
= p
- h
->root
.root
.string
;
1907 alc
= bfd_malloc (len
);
1910 sinfo
->failed
= TRUE
;
1913 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1914 alc
[len
- 1] = '\0';
1915 if (alc
[len
- 2] == ELF_VER_CHR
)
1916 alc
[len
- 2] = '\0';
1918 h
->verinfo
.vertree
= t
;
1922 if (t
->globals
.list
!= NULL
)
1923 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1925 /* See if there is anything to force this symbol to
1927 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1929 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1932 && ! info
->export_dynamic
)
1933 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1941 /* If we are building an application, we need to create a
1942 version node for this version. */
1943 if (t
== NULL
&& info
->executable
)
1945 struct bfd_elf_version_tree
**pp
;
1948 /* If we aren't going to export this symbol, we don't need
1949 to worry about it. */
1950 if (h
->dynindx
== -1)
1954 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1957 sinfo
->failed
= TRUE
;
1962 t
->name_indx
= (unsigned int) -1;
1966 /* Don't count anonymous version tag. */
1967 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1969 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1971 t
->vernum
= version_index
;
1975 h
->verinfo
.vertree
= t
;
1979 /* We could not find the version for a symbol when
1980 generating a shared archive. Return an error. */
1981 (*_bfd_error_handler
)
1982 (_("%B: version node not found for symbol %s"),
1983 sinfo
->output_bfd
, h
->root
.root
.string
);
1984 bfd_set_error (bfd_error_bad_value
);
1985 sinfo
->failed
= TRUE
;
1993 /* If we don't have a version for this symbol, see if we can find
1995 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1997 struct bfd_elf_version_tree
*t
;
1998 struct bfd_elf_version_tree
*local_ver
;
1999 struct bfd_elf_version_expr
*d
;
2001 /* See if can find what version this symbol is in. If the
2002 symbol is supposed to be local, then don't actually register
2005 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2007 if (t
->globals
.list
!= NULL
)
2009 bfd_boolean matched
;
2013 while ((d
= (*t
->match
) (&t
->globals
, d
,
2014 h
->root
.root
.string
)) != NULL
)
2019 /* There is a version without definition. Make
2020 the symbol the default definition for this
2022 h
->verinfo
.vertree
= t
;
2030 /* There is no undefined version for this symbol. Hide the
2032 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2035 if (t
->locals
.list
!= NULL
)
2038 while ((d
= (*t
->match
) (&t
->locals
, d
,
2039 h
->root
.root
.string
)) != NULL
)
2042 /* If the match is "*", keep looking for a more
2043 explicit, perhaps even global, match.
2044 XXX: Shouldn't this be !d->wildcard instead? */
2045 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2054 if (local_ver
!= NULL
)
2056 h
->verinfo
.vertree
= local_ver
;
2057 if (h
->dynindx
!= -1
2058 && ! info
->export_dynamic
)
2060 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2068 /* Read and swap the relocs from the section indicated by SHDR. This
2069 may be either a REL or a RELA section. The relocations are
2070 translated into RELA relocations and stored in INTERNAL_RELOCS,
2071 which should have already been allocated to contain enough space.
2072 The EXTERNAL_RELOCS are a buffer where the external form of the
2073 relocations should be stored.
2075 Returns FALSE if something goes wrong. */
2078 elf_link_read_relocs_from_section (bfd
*abfd
,
2080 Elf_Internal_Shdr
*shdr
,
2081 void *external_relocs
,
2082 Elf_Internal_Rela
*internal_relocs
)
2084 const struct elf_backend_data
*bed
;
2085 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2086 const bfd_byte
*erela
;
2087 const bfd_byte
*erelaend
;
2088 Elf_Internal_Rela
*irela
;
2089 Elf_Internal_Shdr
*symtab_hdr
;
2092 /* Position ourselves at the start of the section. */
2093 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2096 /* Read the relocations. */
2097 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2100 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2101 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2103 bed
= get_elf_backend_data (abfd
);
2105 /* Convert the external relocations to the internal format. */
2106 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2107 swap_in
= bed
->s
->swap_reloc_in
;
2108 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2109 swap_in
= bed
->s
->swap_reloca_in
;
2112 bfd_set_error (bfd_error_wrong_format
);
2116 erela
= external_relocs
;
2117 erelaend
= erela
+ shdr
->sh_size
;
2118 irela
= internal_relocs
;
2119 while (erela
< erelaend
)
2123 (*swap_in
) (abfd
, erela
, irela
);
2124 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2125 if (bed
->s
->arch_size
== 64)
2127 if ((size_t) r_symndx
>= nsyms
)
2129 (*_bfd_error_handler
)
2130 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2131 " for offset 0x%lx in section `%A'"),
2133 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2134 bfd_set_error (bfd_error_bad_value
);
2137 irela
+= bed
->s
->int_rels_per_ext_rel
;
2138 erela
+= shdr
->sh_entsize
;
2144 /* Read and swap the relocs for a section O. They may have been
2145 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2146 not NULL, they are used as buffers to read into. They are known to
2147 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2148 the return value is allocated using either malloc or bfd_alloc,
2149 according to the KEEP_MEMORY argument. If O has two relocation
2150 sections (both REL and RELA relocations), then the REL_HDR
2151 relocations will appear first in INTERNAL_RELOCS, followed by the
2152 REL_HDR2 relocations. */
2155 _bfd_elf_link_read_relocs (bfd
*abfd
,
2157 void *external_relocs
,
2158 Elf_Internal_Rela
*internal_relocs
,
2159 bfd_boolean keep_memory
)
2161 Elf_Internal_Shdr
*rel_hdr
;
2162 void *alloc1
= NULL
;
2163 Elf_Internal_Rela
*alloc2
= NULL
;
2164 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2166 if (elf_section_data (o
)->relocs
!= NULL
)
2167 return elf_section_data (o
)->relocs
;
2169 if (o
->reloc_count
== 0)
2172 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2174 if (internal_relocs
== NULL
)
2178 size
= o
->reloc_count
;
2179 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2181 internal_relocs
= bfd_alloc (abfd
, size
);
2183 internal_relocs
= alloc2
= bfd_malloc (size
);
2184 if (internal_relocs
== NULL
)
2188 if (external_relocs
== NULL
)
2190 bfd_size_type size
= rel_hdr
->sh_size
;
2192 if (elf_section_data (o
)->rel_hdr2
)
2193 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2194 alloc1
= bfd_malloc (size
);
2197 external_relocs
= alloc1
;
2200 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2204 if (elf_section_data (o
)->rel_hdr2
2205 && (!elf_link_read_relocs_from_section
2207 elf_section_data (o
)->rel_hdr2
,
2208 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2209 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2210 * bed
->s
->int_rels_per_ext_rel
))))
2213 /* Cache the results for next time, if we can. */
2215 elf_section_data (o
)->relocs
= internal_relocs
;
2220 /* Don't free alloc2, since if it was allocated we are passing it
2221 back (under the name of internal_relocs). */
2223 return internal_relocs
;
2233 /* Compute the size of, and allocate space for, REL_HDR which is the
2234 section header for a section containing relocations for O. */
2237 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2238 Elf_Internal_Shdr
*rel_hdr
,
2241 bfd_size_type reloc_count
;
2242 bfd_size_type num_rel_hashes
;
2244 /* Figure out how many relocations there will be. */
2245 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2246 reloc_count
= elf_section_data (o
)->rel_count
;
2248 reloc_count
= elf_section_data (o
)->rel_count2
;
2250 num_rel_hashes
= o
->reloc_count
;
2251 if (num_rel_hashes
< reloc_count
)
2252 num_rel_hashes
= reloc_count
;
2254 /* That allows us to calculate the size of the section. */
2255 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2257 /* The contents field must last into write_object_contents, so we
2258 allocate it with bfd_alloc rather than malloc. Also since we
2259 cannot be sure that the contents will actually be filled in,
2260 we zero the allocated space. */
2261 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2262 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2265 /* We only allocate one set of hash entries, so we only do it the
2266 first time we are called. */
2267 if (elf_section_data (o
)->rel_hashes
== NULL
2270 struct elf_link_hash_entry
**p
;
2272 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2276 elf_section_data (o
)->rel_hashes
= p
;
2282 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2283 originated from the section given by INPUT_REL_HDR) to the
2287 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2288 asection
*input_section
,
2289 Elf_Internal_Shdr
*input_rel_hdr
,
2290 Elf_Internal_Rela
*internal_relocs
,
2291 struct elf_link_hash_entry
**rel_hash
2294 Elf_Internal_Rela
*irela
;
2295 Elf_Internal_Rela
*irelaend
;
2297 Elf_Internal_Shdr
*output_rel_hdr
;
2298 asection
*output_section
;
2299 unsigned int *rel_countp
= NULL
;
2300 const struct elf_backend_data
*bed
;
2301 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2303 output_section
= input_section
->output_section
;
2304 output_rel_hdr
= NULL
;
2306 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2307 == input_rel_hdr
->sh_entsize
)
2309 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2310 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2312 else if (elf_section_data (output_section
)->rel_hdr2
2313 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2314 == input_rel_hdr
->sh_entsize
))
2316 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2317 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2321 (*_bfd_error_handler
)
2322 (_("%B: relocation size mismatch in %B section %A"),
2323 output_bfd
, input_section
->owner
, input_section
);
2324 bfd_set_error (bfd_error_wrong_object_format
);
2328 bed
= get_elf_backend_data (output_bfd
);
2329 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2330 swap_out
= bed
->s
->swap_reloc_out
;
2331 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2332 swap_out
= bed
->s
->swap_reloca_out
;
2336 erel
= output_rel_hdr
->contents
;
2337 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2338 irela
= internal_relocs
;
2339 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2340 * bed
->s
->int_rels_per_ext_rel
);
2341 while (irela
< irelaend
)
2343 (*swap_out
) (output_bfd
, irela
, erel
);
2344 irela
+= bed
->s
->int_rels_per_ext_rel
;
2345 erel
+= input_rel_hdr
->sh_entsize
;
2348 /* Bump the counter, so that we know where to add the next set of
2350 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2355 /* Make weak undefined symbols in PIE dynamic. */
2358 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2359 struct elf_link_hash_entry
*h
)
2363 && h
->root
.type
== bfd_link_hash_undefweak
)
2364 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2369 /* Fix up the flags for a symbol. This handles various cases which
2370 can only be fixed after all the input files are seen. This is
2371 currently called by both adjust_dynamic_symbol and
2372 assign_sym_version, which is unnecessary but perhaps more robust in
2373 the face of future changes. */
2376 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2377 struct elf_info_failed
*eif
)
2379 const struct elf_backend_data
*bed
;
2381 /* If this symbol was mentioned in a non-ELF file, try to set
2382 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2383 permit a non-ELF file to correctly refer to a symbol defined in
2384 an ELF dynamic object. */
2387 while (h
->root
.type
== bfd_link_hash_indirect
)
2388 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2390 if (h
->root
.type
!= bfd_link_hash_defined
2391 && h
->root
.type
!= bfd_link_hash_defweak
)
2394 h
->ref_regular_nonweak
= 1;
2398 if (h
->root
.u
.def
.section
->owner
!= NULL
2399 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2400 == bfd_target_elf_flavour
))
2403 h
->ref_regular_nonweak
= 1;
2409 if (h
->dynindx
== -1
2413 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2422 /* Unfortunately, NON_ELF is only correct if the symbol
2423 was first seen in a non-ELF file. Fortunately, if the symbol
2424 was first seen in an ELF file, we're probably OK unless the
2425 symbol was defined in a non-ELF file. Catch that case here.
2426 FIXME: We're still in trouble if the symbol was first seen in
2427 a dynamic object, and then later in a non-ELF regular object. */
2428 if ((h
->root
.type
== bfd_link_hash_defined
2429 || h
->root
.type
== bfd_link_hash_defweak
)
2431 && (h
->root
.u
.def
.section
->owner
!= NULL
2432 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2433 != bfd_target_elf_flavour
)
2434 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2435 && !h
->def_dynamic
)))
2439 /* Backend specific symbol fixup. */
2440 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2441 if (bed
->elf_backend_fixup_symbol
2442 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2445 /* If this is a final link, and the symbol was defined as a common
2446 symbol in a regular object file, and there was no definition in
2447 any dynamic object, then the linker will have allocated space for
2448 the symbol in a common section but the DEF_REGULAR
2449 flag will not have been set. */
2450 if (h
->root
.type
== bfd_link_hash_defined
2454 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2457 /* If -Bsymbolic was used (which means to bind references to global
2458 symbols to the definition within the shared object), and this
2459 symbol was defined in a regular object, then it actually doesn't
2460 need a PLT entry. Likewise, if the symbol has non-default
2461 visibility. If the symbol has hidden or internal visibility, we
2462 will force it local. */
2464 && eif
->info
->shared
2465 && is_elf_hash_table (eif
->info
->hash
)
2466 && (SYMBOLIC_BIND (eif
->info
, h
)
2467 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2470 bfd_boolean force_local
;
2472 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2473 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2474 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2477 /* If a weak undefined symbol has non-default visibility, we also
2478 hide it from the dynamic linker. */
2479 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2480 && h
->root
.type
== bfd_link_hash_undefweak
)
2481 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2483 /* If this is a weak defined symbol in a dynamic object, and we know
2484 the real definition in the dynamic object, copy interesting flags
2485 over to the real definition. */
2486 if (h
->u
.weakdef
!= NULL
)
2488 struct elf_link_hash_entry
*weakdef
;
2490 weakdef
= h
->u
.weakdef
;
2491 if (h
->root
.type
== bfd_link_hash_indirect
)
2492 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2494 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2495 || h
->root
.type
== bfd_link_hash_defweak
);
2496 BFD_ASSERT (weakdef
->def_dynamic
);
2498 /* If the real definition is defined by a regular object file,
2499 don't do anything special. See the longer description in
2500 _bfd_elf_adjust_dynamic_symbol, below. */
2501 if (weakdef
->def_regular
)
2502 h
->u
.weakdef
= NULL
;
2505 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2506 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2507 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2514 /* Make the backend pick a good value for a dynamic symbol. This is
2515 called via elf_link_hash_traverse, and also calls itself
2519 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2521 struct elf_info_failed
*eif
= data
;
2523 const struct elf_backend_data
*bed
;
2525 if (! is_elf_hash_table (eif
->info
->hash
))
2528 if (h
->root
.type
== bfd_link_hash_warning
)
2530 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2531 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2533 /* When warning symbols are created, they **replace** the "real"
2534 entry in the hash table, thus we never get to see the real
2535 symbol in a hash traversal. So look at it now. */
2536 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2539 /* Ignore indirect symbols. These are added by the versioning code. */
2540 if (h
->root
.type
== bfd_link_hash_indirect
)
2543 /* Fix the symbol flags. */
2544 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2547 /* If this symbol does not require a PLT entry, and it is not
2548 defined by a dynamic object, or is not referenced by a regular
2549 object, ignore it. We do have to handle a weak defined symbol,
2550 even if no regular object refers to it, if we decided to add it
2551 to the dynamic symbol table. FIXME: Do we normally need to worry
2552 about symbols which are defined by one dynamic object and
2553 referenced by another one? */
2558 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2560 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2564 /* If we've already adjusted this symbol, don't do it again. This
2565 can happen via a recursive call. */
2566 if (h
->dynamic_adjusted
)
2569 /* Don't look at this symbol again. Note that we must set this
2570 after checking the above conditions, because we may look at a
2571 symbol once, decide not to do anything, and then get called
2572 recursively later after REF_REGULAR is set below. */
2573 h
->dynamic_adjusted
= 1;
2575 /* If this is a weak definition, and we know a real definition, and
2576 the real symbol is not itself defined by a regular object file,
2577 then get a good value for the real definition. We handle the
2578 real symbol first, for the convenience of the backend routine.
2580 Note that there is a confusing case here. If the real definition
2581 is defined by a regular object file, we don't get the real symbol
2582 from the dynamic object, but we do get the weak symbol. If the
2583 processor backend uses a COPY reloc, then if some routine in the
2584 dynamic object changes the real symbol, we will not see that
2585 change in the corresponding weak symbol. This is the way other
2586 ELF linkers work as well, and seems to be a result of the shared
2589 I will clarify this issue. Most SVR4 shared libraries define the
2590 variable _timezone and define timezone as a weak synonym. The
2591 tzset call changes _timezone. If you write
2592 extern int timezone;
2594 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2595 you might expect that, since timezone is a synonym for _timezone,
2596 the same number will print both times. However, if the processor
2597 backend uses a COPY reloc, then actually timezone will be copied
2598 into your process image, and, since you define _timezone
2599 yourself, _timezone will not. Thus timezone and _timezone will
2600 wind up at different memory locations. The tzset call will set
2601 _timezone, leaving timezone unchanged. */
2603 if (h
->u
.weakdef
!= NULL
)
2605 /* If we get to this point, we know there is an implicit
2606 reference by a regular object file via the weak symbol H.
2607 FIXME: Is this really true? What if the traversal finds
2608 H->U.WEAKDEF before it finds H? */
2609 h
->u
.weakdef
->ref_regular
= 1;
2611 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2615 /* If a symbol has no type and no size and does not require a PLT
2616 entry, then we are probably about to do the wrong thing here: we
2617 are probably going to create a COPY reloc for an empty object.
2618 This case can arise when a shared object is built with assembly
2619 code, and the assembly code fails to set the symbol type. */
2621 && h
->type
== STT_NOTYPE
2623 (*_bfd_error_handler
)
2624 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2625 h
->root
.root
.string
);
2627 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2628 bed
= get_elf_backend_data (dynobj
);
2629 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2638 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2642 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2645 unsigned int power_of_two
;
2647 asection
*sec
= h
->root
.u
.def
.section
;
2649 /* The section aligment of definition is the maximum alignment
2650 requirement of symbols defined in the section. Since we don't
2651 know the symbol alignment requirement, we start with the
2652 maximum alignment and check low bits of the symbol address
2653 for the minimum alignment. */
2654 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2655 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2656 while ((h
->root
.u
.def
.value
& mask
) != 0)
2662 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2665 /* Adjust the section alignment if needed. */
2666 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2671 /* We make sure that the symbol will be aligned properly. */
2672 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2674 /* Define the symbol as being at this point in DYNBSS. */
2675 h
->root
.u
.def
.section
= dynbss
;
2676 h
->root
.u
.def
.value
= dynbss
->size
;
2678 /* Increment the size of DYNBSS to make room for the symbol. */
2679 dynbss
->size
+= h
->size
;
2684 /* Adjust all external symbols pointing into SEC_MERGE sections
2685 to reflect the object merging within the sections. */
2688 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2692 if (h
->root
.type
== bfd_link_hash_warning
)
2693 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2695 if ((h
->root
.type
== bfd_link_hash_defined
2696 || h
->root
.type
== bfd_link_hash_defweak
)
2697 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2698 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2700 bfd
*output_bfd
= data
;
2702 h
->root
.u
.def
.value
=
2703 _bfd_merged_section_offset (output_bfd
,
2704 &h
->root
.u
.def
.section
,
2705 elf_section_data (sec
)->sec_info
,
2706 h
->root
.u
.def
.value
);
2712 /* Returns false if the symbol referred to by H should be considered
2713 to resolve local to the current module, and true if it should be
2714 considered to bind dynamically. */
2717 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2718 struct bfd_link_info
*info
,
2719 bfd_boolean ignore_protected
)
2721 bfd_boolean binding_stays_local_p
;
2722 const struct elf_backend_data
*bed
;
2723 struct elf_link_hash_table
*hash_table
;
2728 while (h
->root
.type
== bfd_link_hash_indirect
2729 || h
->root
.type
== bfd_link_hash_warning
)
2730 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2732 /* If it was forced local, then clearly it's not dynamic. */
2733 if (h
->dynindx
== -1)
2735 if (h
->forced_local
)
2738 /* Identify the cases where name binding rules say that a
2739 visible symbol resolves locally. */
2740 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2742 switch (ELF_ST_VISIBILITY (h
->other
))
2749 hash_table
= elf_hash_table (info
);
2750 if (!is_elf_hash_table (hash_table
))
2753 bed
= get_elf_backend_data (hash_table
->dynobj
);
2755 /* Proper resolution for function pointer equality may require
2756 that these symbols perhaps be resolved dynamically, even though
2757 we should be resolving them to the current module. */
2758 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2759 binding_stays_local_p
= TRUE
;
2766 /* If it isn't defined locally, then clearly it's dynamic. */
2767 if (!h
->def_regular
)
2770 /* Otherwise, the symbol is dynamic if binding rules don't tell
2771 us that it remains local. */
2772 return !binding_stays_local_p
;
2775 /* Return true if the symbol referred to by H should be considered
2776 to resolve local to the current module, and false otherwise. Differs
2777 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2778 undefined symbols and weak symbols. */
2781 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2782 struct bfd_link_info
*info
,
2783 bfd_boolean local_protected
)
2785 const struct elf_backend_data
*bed
;
2786 struct elf_link_hash_table
*hash_table
;
2788 /* If it's a local sym, of course we resolve locally. */
2792 /* Common symbols that become definitions don't get the DEF_REGULAR
2793 flag set, so test it first, and don't bail out. */
2794 if (ELF_COMMON_DEF_P (h
))
2796 /* If we don't have a definition in a regular file, then we can't
2797 resolve locally. The sym is either undefined or dynamic. */
2798 else if (!h
->def_regular
)
2801 /* Forced local symbols resolve locally. */
2802 if (h
->forced_local
)
2805 /* As do non-dynamic symbols. */
2806 if (h
->dynindx
== -1)
2809 /* At this point, we know the symbol is defined and dynamic. In an
2810 executable it must resolve locally, likewise when building symbolic
2811 shared libraries. */
2812 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2815 /* Now deal with defined dynamic symbols in shared libraries. Ones
2816 with default visibility might not resolve locally. */
2817 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2820 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2821 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2824 hash_table
= elf_hash_table (info
);
2825 if (!is_elf_hash_table (hash_table
))
2828 bed
= get_elf_backend_data (hash_table
->dynobj
);
2830 /* STV_PROTECTED non-function symbols are local. */
2831 if (!bed
->is_function_type (h
->type
))
2834 /* Function pointer equality tests may require that STV_PROTECTED
2835 symbols be treated as dynamic symbols, even when we know that the
2836 dynamic linker will resolve them locally. */
2837 return local_protected
;
2840 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2841 aligned. Returns the first TLS output section. */
2843 struct bfd_section
*
2844 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2846 struct bfd_section
*sec
, *tls
;
2847 unsigned int align
= 0;
2849 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2850 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2854 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2855 if (sec
->alignment_power
> align
)
2856 align
= sec
->alignment_power
;
2858 elf_hash_table (info
)->tls_sec
= tls
;
2860 /* Ensure the alignment of the first section is the largest alignment,
2861 so that the tls segment starts aligned. */
2863 tls
->alignment_power
= align
;
2868 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2870 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2871 Elf_Internal_Sym
*sym
)
2873 const struct elf_backend_data
*bed
;
2875 /* Local symbols do not count, but target specific ones might. */
2876 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2877 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2880 bed
= get_elf_backend_data (abfd
);
2881 /* Function symbols do not count. */
2882 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2885 /* If the section is undefined, then so is the symbol. */
2886 if (sym
->st_shndx
== SHN_UNDEF
)
2889 /* If the symbol is defined in the common section, then
2890 it is a common definition and so does not count. */
2891 if (bed
->common_definition (sym
))
2894 /* If the symbol is in a target specific section then we
2895 must rely upon the backend to tell us what it is. */
2896 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2897 /* FIXME - this function is not coded yet:
2899 return _bfd_is_global_symbol_definition (abfd, sym);
2901 Instead for now assume that the definition is not global,
2902 Even if this is wrong, at least the linker will behave
2903 in the same way that it used to do. */
2909 /* Search the symbol table of the archive element of the archive ABFD
2910 whose archive map contains a mention of SYMDEF, and determine if
2911 the symbol is defined in this element. */
2913 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2915 Elf_Internal_Shdr
* hdr
;
2916 bfd_size_type symcount
;
2917 bfd_size_type extsymcount
;
2918 bfd_size_type extsymoff
;
2919 Elf_Internal_Sym
*isymbuf
;
2920 Elf_Internal_Sym
*isym
;
2921 Elf_Internal_Sym
*isymend
;
2924 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2928 if (! bfd_check_format (abfd
, bfd_object
))
2931 /* If we have already included the element containing this symbol in the
2932 link then we do not need to include it again. Just claim that any symbol
2933 it contains is not a definition, so that our caller will not decide to
2934 (re)include this element. */
2935 if (abfd
->archive_pass
)
2938 /* Select the appropriate symbol table. */
2939 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2940 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2942 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2944 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2946 /* The sh_info field of the symtab header tells us where the
2947 external symbols start. We don't care about the local symbols. */
2948 if (elf_bad_symtab (abfd
))
2950 extsymcount
= symcount
;
2955 extsymcount
= symcount
- hdr
->sh_info
;
2956 extsymoff
= hdr
->sh_info
;
2959 if (extsymcount
== 0)
2962 /* Read in the symbol table. */
2963 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2965 if (isymbuf
== NULL
)
2968 /* Scan the symbol table looking for SYMDEF. */
2970 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2974 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2979 if (strcmp (name
, symdef
->name
) == 0)
2981 result
= is_global_data_symbol_definition (abfd
, isym
);
2991 /* Add an entry to the .dynamic table. */
2994 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2998 struct elf_link_hash_table
*hash_table
;
2999 const struct elf_backend_data
*bed
;
3001 bfd_size_type newsize
;
3002 bfd_byte
*newcontents
;
3003 Elf_Internal_Dyn dyn
;
3005 hash_table
= elf_hash_table (info
);
3006 if (! is_elf_hash_table (hash_table
))
3009 bed
= get_elf_backend_data (hash_table
->dynobj
);
3010 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3011 BFD_ASSERT (s
!= NULL
);
3013 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3014 newcontents
= bfd_realloc (s
->contents
, newsize
);
3015 if (newcontents
== NULL
)
3019 dyn
.d_un
.d_val
= val
;
3020 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3023 s
->contents
= newcontents
;
3028 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3029 otherwise just check whether one already exists. Returns -1 on error,
3030 1 if a DT_NEEDED tag already exists, and 0 on success. */
3033 elf_add_dt_needed_tag (bfd
*abfd
,
3034 struct bfd_link_info
*info
,
3038 struct elf_link_hash_table
*hash_table
;
3039 bfd_size_type oldsize
;
3040 bfd_size_type strindex
;
3042 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3045 hash_table
= elf_hash_table (info
);
3046 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3047 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3048 if (strindex
== (bfd_size_type
) -1)
3051 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3054 const struct elf_backend_data
*bed
;
3057 bed
= get_elf_backend_data (hash_table
->dynobj
);
3058 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3060 for (extdyn
= sdyn
->contents
;
3061 extdyn
< sdyn
->contents
+ sdyn
->size
;
3062 extdyn
+= bed
->s
->sizeof_dyn
)
3064 Elf_Internal_Dyn dyn
;
3066 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3067 if (dyn
.d_tag
== DT_NEEDED
3068 && dyn
.d_un
.d_val
== strindex
)
3070 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3078 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3081 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3085 /* We were just checking for existence of the tag. */
3086 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3091 /* Sort symbol by value and section. */
3093 elf_sort_symbol (const void *arg1
, const void *arg2
)
3095 const struct elf_link_hash_entry
*h1
;
3096 const struct elf_link_hash_entry
*h2
;
3097 bfd_signed_vma vdiff
;
3099 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3100 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3101 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3103 return vdiff
> 0 ? 1 : -1;
3106 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3108 return sdiff
> 0 ? 1 : -1;
3113 /* This function is used to adjust offsets into .dynstr for
3114 dynamic symbols. This is called via elf_link_hash_traverse. */
3117 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3119 struct elf_strtab_hash
*dynstr
= data
;
3121 if (h
->root
.type
== bfd_link_hash_warning
)
3122 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3124 if (h
->dynindx
!= -1)
3125 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3129 /* Assign string offsets in .dynstr, update all structures referencing
3133 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3135 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3136 struct elf_link_local_dynamic_entry
*entry
;
3137 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3138 bfd
*dynobj
= hash_table
->dynobj
;
3141 const struct elf_backend_data
*bed
;
3144 _bfd_elf_strtab_finalize (dynstr
);
3145 size
= _bfd_elf_strtab_size (dynstr
);
3147 bed
= get_elf_backend_data (dynobj
);
3148 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3149 BFD_ASSERT (sdyn
!= NULL
);
3151 /* Update all .dynamic entries referencing .dynstr strings. */
3152 for (extdyn
= sdyn
->contents
;
3153 extdyn
< sdyn
->contents
+ sdyn
->size
;
3154 extdyn
+= bed
->s
->sizeof_dyn
)
3156 Elf_Internal_Dyn dyn
;
3158 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3162 dyn
.d_un
.d_val
= size
;
3170 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3175 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3178 /* Now update local dynamic symbols. */
3179 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3180 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3181 entry
->isym
.st_name
);
3183 /* And the rest of dynamic symbols. */
3184 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3186 /* Adjust version definitions. */
3187 if (elf_tdata (output_bfd
)->cverdefs
)
3192 Elf_Internal_Verdef def
;
3193 Elf_Internal_Verdaux defaux
;
3195 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3199 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3201 p
+= sizeof (Elf_External_Verdef
);
3202 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3204 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3206 _bfd_elf_swap_verdaux_in (output_bfd
,
3207 (Elf_External_Verdaux
*) p
, &defaux
);
3208 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3210 _bfd_elf_swap_verdaux_out (output_bfd
,
3211 &defaux
, (Elf_External_Verdaux
*) p
);
3212 p
+= sizeof (Elf_External_Verdaux
);
3215 while (def
.vd_next
);
3218 /* Adjust version references. */
3219 if (elf_tdata (output_bfd
)->verref
)
3224 Elf_Internal_Verneed need
;
3225 Elf_Internal_Vernaux needaux
;
3227 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3231 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3233 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3234 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3235 (Elf_External_Verneed
*) p
);
3236 p
+= sizeof (Elf_External_Verneed
);
3237 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3239 _bfd_elf_swap_vernaux_in (output_bfd
,
3240 (Elf_External_Vernaux
*) p
, &needaux
);
3241 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3243 _bfd_elf_swap_vernaux_out (output_bfd
,
3245 (Elf_External_Vernaux
*) p
);
3246 p
+= sizeof (Elf_External_Vernaux
);
3249 while (need
.vn_next
);
3255 /* Add symbols from an ELF object file to the linker hash table. */
3258 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3260 Elf_Internal_Shdr
*hdr
;
3261 bfd_size_type symcount
;
3262 bfd_size_type extsymcount
;
3263 bfd_size_type extsymoff
;
3264 struct elf_link_hash_entry
**sym_hash
;
3265 bfd_boolean dynamic
;
3266 Elf_External_Versym
*extversym
= NULL
;
3267 Elf_External_Versym
*ever
;
3268 struct elf_link_hash_entry
*weaks
;
3269 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3270 bfd_size_type nondeflt_vers_cnt
= 0;
3271 Elf_Internal_Sym
*isymbuf
= NULL
;
3272 Elf_Internal_Sym
*isym
;
3273 Elf_Internal_Sym
*isymend
;
3274 const struct elf_backend_data
*bed
;
3275 bfd_boolean add_needed
;
3276 struct elf_link_hash_table
*htab
;
3278 void *alloc_mark
= NULL
;
3279 struct bfd_hash_entry
**old_table
= NULL
;
3280 unsigned int old_size
= 0;
3281 unsigned int old_count
= 0;
3282 void *old_tab
= NULL
;
3285 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3286 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3287 long old_dynsymcount
= 0;
3289 size_t hashsize
= 0;
3291 htab
= elf_hash_table (info
);
3292 bed
= get_elf_backend_data (abfd
);
3294 if ((abfd
->flags
& DYNAMIC
) == 0)
3300 /* You can't use -r against a dynamic object. Also, there's no
3301 hope of using a dynamic object which does not exactly match
3302 the format of the output file. */
3303 if (info
->relocatable
3304 || !is_elf_hash_table (htab
)
3305 || htab
->root
.creator
!= abfd
->xvec
)
3307 if (info
->relocatable
)
3308 bfd_set_error (bfd_error_invalid_operation
);
3310 bfd_set_error (bfd_error_wrong_format
);
3315 /* As a GNU extension, any input sections which are named
3316 .gnu.warning.SYMBOL are treated as warning symbols for the given
3317 symbol. This differs from .gnu.warning sections, which generate
3318 warnings when they are included in an output file. */
3319 if (info
->executable
)
3323 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3327 name
= bfd_get_section_name (abfd
, s
);
3328 if (CONST_STRNEQ (name
, ".gnu.warning."))
3333 name
+= sizeof ".gnu.warning." - 1;
3335 /* If this is a shared object, then look up the symbol
3336 in the hash table. If it is there, and it is already
3337 been defined, then we will not be using the entry
3338 from this shared object, so we don't need to warn.
3339 FIXME: If we see the definition in a regular object
3340 later on, we will warn, but we shouldn't. The only
3341 fix is to keep track of what warnings we are supposed
3342 to emit, and then handle them all at the end of the
3346 struct elf_link_hash_entry
*h
;
3348 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3350 /* FIXME: What about bfd_link_hash_common? */
3352 && (h
->root
.type
== bfd_link_hash_defined
3353 || h
->root
.type
== bfd_link_hash_defweak
))
3355 /* We don't want to issue this warning. Clobber
3356 the section size so that the warning does not
3357 get copied into the output file. */
3364 msg
= bfd_alloc (abfd
, sz
+ 1);
3368 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3373 if (! (_bfd_generic_link_add_one_symbol
3374 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3375 FALSE
, bed
->collect
, NULL
)))
3378 if (! info
->relocatable
)
3380 /* Clobber the section size so that the warning does
3381 not get copied into the output file. */
3384 /* Also set SEC_EXCLUDE, so that symbols defined in
3385 the warning section don't get copied to the output. */
3386 s
->flags
|= SEC_EXCLUDE
;
3395 /* If we are creating a shared library, create all the dynamic
3396 sections immediately. We need to attach them to something,
3397 so we attach them to this BFD, provided it is the right
3398 format. FIXME: If there are no input BFD's of the same
3399 format as the output, we can't make a shared library. */
3401 && is_elf_hash_table (htab
)
3402 && htab
->root
.creator
== abfd
->xvec
3403 && !htab
->dynamic_sections_created
)
3405 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3409 else if (!is_elf_hash_table (htab
))
3414 const char *soname
= NULL
;
3415 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3418 /* ld --just-symbols and dynamic objects don't mix very well.
3419 ld shouldn't allow it. */
3420 if ((s
= abfd
->sections
) != NULL
3421 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3424 /* If this dynamic lib was specified on the command line with
3425 --as-needed in effect, then we don't want to add a DT_NEEDED
3426 tag unless the lib is actually used. Similary for libs brought
3427 in by another lib's DT_NEEDED. When --no-add-needed is used
3428 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3429 any dynamic library in DT_NEEDED tags in the dynamic lib at
3431 add_needed
= (elf_dyn_lib_class (abfd
)
3432 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3433 | DYN_NO_NEEDED
)) == 0;
3435 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3441 unsigned long shlink
;
3443 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3444 goto error_free_dyn
;
3446 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3448 goto error_free_dyn
;
3449 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3451 for (extdyn
= dynbuf
;
3452 extdyn
< dynbuf
+ s
->size
;
3453 extdyn
+= bed
->s
->sizeof_dyn
)
3455 Elf_Internal_Dyn dyn
;
3457 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3458 if (dyn
.d_tag
== DT_SONAME
)
3460 unsigned int tagv
= dyn
.d_un
.d_val
;
3461 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3463 goto error_free_dyn
;
3465 if (dyn
.d_tag
== DT_NEEDED
)
3467 struct bfd_link_needed_list
*n
, **pn
;
3469 unsigned int tagv
= dyn
.d_un
.d_val
;
3471 amt
= sizeof (struct bfd_link_needed_list
);
3472 n
= bfd_alloc (abfd
, amt
);
3473 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3474 if (n
== NULL
|| fnm
== NULL
)
3475 goto error_free_dyn
;
3476 amt
= strlen (fnm
) + 1;
3477 anm
= bfd_alloc (abfd
, amt
);
3479 goto error_free_dyn
;
3480 memcpy (anm
, fnm
, amt
);
3484 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3488 if (dyn
.d_tag
== DT_RUNPATH
)
3490 struct bfd_link_needed_list
*n
, **pn
;
3492 unsigned int tagv
= dyn
.d_un
.d_val
;
3494 amt
= sizeof (struct bfd_link_needed_list
);
3495 n
= bfd_alloc (abfd
, amt
);
3496 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3497 if (n
== NULL
|| fnm
== NULL
)
3498 goto error_free_dyn
;
3499 amt
= strlen (fnm
) + 1;
3500 anm
= bfd_alloc (abfd
, amt
);
3502 goto error_free_dyn
;
3503 memcpy (anm
, fnm
, amt
);
3507 for (pn
= & runpath
;
3513 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3514 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3516 struct bfd_link_needed_list
*n
, **pn
;
3518 unsigned int tagv
= dyn
.d_un
.d_val
;
3520 amt
= sizeof (struct bfd_link_needed_list
);
3521 n
= bfd_alloc (abfd
, amt
);
3522 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3523 if (n
== NULL
|| fnm
== NULL
)
3524 goto error_free_dyn
;
3525 amt
= strlen (fnm
) + 1;
3526 anm
= bfd_alloc (abfd
, amt
);
3533 memcpy (anm
, fnm
, amt
);
3548 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3549 frees all more recently bfd_alloc'd blocks as well. */
3555 struct bfd_link_needed_list
**pn
;
3556 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3561 /* We do not want to include any of the sections in a dynamic
3562 object in the output file. We hack by simply clobbering the
3563 list of sections in the BFD. This could be handled more
3564 cleanly by, say, a new section flag; the existing
3565 SEC_NEVER_LOAD flag is not the one we want, because that one
3566 still implies that the section takes up space in the output
3568 bfd_section_list_clear (abfd
);
3570 /* Find the name to use in a DT_NEEDED entry that refers to this
3571 object. If the object has a DT_SONAME entry, we use it.
3572 Otherwise, if the generic linker stuck something in
3573 elf_dt_name, we use that. Otherwise, we just use the file
3575 if (soname
== NULL
|| *soname
== '\0')
3577 soname
= elf_dt_name (abfd
);
3578 if (soname
== NULL
|| *soname
== '\0')
3579 soname
= bfd_get_filename (abfd
);
3582 /* Save the SONAME because sometimes the linker emulation code
3583 will need to know it. */
3584 elf_dt_name (abfd
) = soname
;
3586 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3590 /* If we have already included this dynamic object in the
3591 link, just ignore it. There is no reason to include a
3592 particular dynamic object more than once. */
3597 /* If this is a dynamic object, we always link against the .dynsym
3598 symbol table, not the .symtab symbol table. The dynamic linker
3599 will only see the .dynsym symbol table, so there is no reason to
3600 look at .symtab for a dynamic object. */
3602 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3603 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3605 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3607 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3609 /* The sh_info field of the symtab header tells us where the
3610 external symbols start. We don't care about the local symbols at
3612 if (elf_bad_symtab (abfd
))
3614 extsymcount
= symcount
;
3619 extsymcount
= symcount
- hdr
->sh_info
;
3620 extsymoff
= hdr
->sh_info
;
3624 if (extsymcount
!= 0)
3626 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3628 if (isymbuf
== NULL
)
3631 /* We store a pointer to the hash table entry for each external
3633 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3634 sym_hash
= bfd_alloc (abfd
, amt
);
3635 if (sym_hash
== NULL
)
3636 goto error_free_sym
;
3637 elf_sym_hashes (abfd
) = sym_hash
;
3642 /* Read in any version definitions. */
3643 if (!_bfd_elf_slurp_version_tables (abfd
,
3644 info
->default_imported_symver
))
3645 goto error_free_sym
;
3647 /* Read in the symbol versions, but don't bother to convert them
3648 to internal format. */
3649 if (elf_dynversym (abfd
) != 0)
3651 Elf_Internal_Shdr
*versymhdr
;
3653 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3654 extversym
= bfd_malloc (versymhdr
->sh_size
);
3655 if (extversym
== NULL
)
3656 goto error_free_sym
;
3657 amt
= versymhdr
->sh_size
;
3658 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3659 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3660 goto error_free_vers
;
3664 /* If we are loading an as-needed shared lib, save the symbol table
3665 state before we start adding symbols. If the lib turns out
3666 to be unneeded, restore the state. */
3667 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3672 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3674 struct bfd_hash_entry
*p
;
3675 struct elf_link_hash_entry
*h
;
3677 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3679 h
= (struct elf_link_hash_entry
*) p
;
3680 entsize
+= htab
->root
.table
.entsize
;
3681 if (h
->root
.type
== bfd_link_hash_warning
)
3682 entsize
+= htab
->root
.table
.entsize
;
3686 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3687 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3688 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3689 if (old_tab
== NULL
)
3690 goto error_free_vers
;
3692 /* Remember the current objalloc pointer, so that all mem for
3693 symbols added can later be reclaimed. */
3694 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3695 if (alloc_mark
== NULL
)
3696 goto error_free_vers
;
3698 /* Make a special call to the linker "notice" function to
3699 tell it that we are about to handle an as-needed lib. */
3700 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3702 goto error_free_vers
;
3704 /* Clone the symbol table and sym hashes. Remember some
3705 pointers into the symbol table, and dynamic symbol count. */
3706 old_hash
= (char *) old_tab
+ tabsize
;
3707 old_ent
= (char *) old_hash
+ hashsize
;
3708 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3709 memcpy (old_hash
, sym_hash
, hashsize
);
3710 old_undefs
= htab
->root
.undefs
;
3711 old_undefs_tail
= htab
->root
.undefs_tail
;
3712 old_table
= htab
->root
.table
.table
;
3713 old_size
= htab
->root
.table
.size
;
3714 old_count
= htab
->root
.table
.count
;
3715 old_dynsymcount
= htab
->dynsymcount
;
3717 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3719 struct bfd_hash_entry
*p
;
3720 struct elf_link_hash_entry
*h
;
3722 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3724 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3725 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3726 h
= (struct elf_link_hash_entry
*) p
;
3727 if (h
->root
.type
== bfd_link_hash_warning
)
3729 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3730 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3737 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3738 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3740 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3744 asection
*sec
, *new_sec
;
3747 struct elf_link_hash_entry
*h
;
3748 bfd_boolean definition
;
3749 bfd_boolean size_change_ok
;
3750 bfd_boolean type_change_ok
;
3751 bfd_boolean new_weakdef
;
3752 bfd_boolean override
;
3754 unsigned int old_alignment
;
3759 flags
= BSF_NO_FLAGS
;
3761 value
= isym
->st_value
;
3763 common
= bed
->common_definition (isym
);
3765 bind
= ELF_ST_BIND (isym
->st_info
);
3766 if (bind
== STB_LOCAL
)
3768 /* This should be impossible, since ELF requires that all
3769 global symbols follow all local symbols, and that sh_info
3770 point to the first global symbol. Unfortunately, Irix 5
3774 else if (bind
== STB_GLOBAL
)
3776 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3779 else if (bind
== STB_WEAK
)
3783 /* Leave it up to the processor backend. */
3786 if (isym
->st_shndx
== SHN_UNDEF
)
3787 sec
= bfd_und_section_ptr
;
3788 else if (isym
->st_shndx
< SHN_LORESERVE
3789 || isym
->st_shndx
> SHN_HIRESERVE
)
3791 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3793 sec
= bfd_abs_section_ptr
;
3794 else if (sec
->kept_section
)
3796 /* Symbols from discarded section are undefined. We keep
3798 sec
= bfd_und_section_ptr
;
3799 isym
->st_shndx
= SHN_UNDEF
;
3801 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3804 else if (isym
->st_shndx
== SHN_ABS
)
3805 sec
= bfd_abs_section_ptr
;
3806 else if (isym
->st_shndx
== SHN_COMMON
)
3808 sec
= bfd_com_section_ptr
;
3809 /* What ELF calls the size we call the value. What ELF
3810 calls the value we call the alignment. */
3811 value
= isym
->st_size
;
3815 /* Leave it up to the processor backend. */
3818 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3821 goto error_free_vers
;
3823 if (isym
->st_shndx
== SHN_COMMON
3824 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3825 && !info
->relocatable
)
3827 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3831 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3834 | SEC_LINKER_CREATED
3835 | SEC_THREAD_LOCAL
));
3837 goto error_free_vers
;
3841 else if (bed
->elf_add_symbol_hook
)
3843 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3845 goto error_free_vers
;
3847 /* The hook function sets the name to NULL if this symbol
3848 should be skipped for some reason. */
3853 /* Sanity check that all possibilities were handled. */
3856 bfd_set_error (bfd_error_bad_value
);
3857 goto error_free_vers
;
3860 if (bfd_is_und_section (sec
)
3861 || bfd_is_com_section (sec
))
3866 size_change_ok
= FALSE
;
3867 type_change_ok
= bed
->type_change_ok
;
3872 if (is_elf_hash_table (htab
))
3874 Elf_Internal_Versym iver
;
3875 unsigned int vernum
= 0;
3880 if (info
->default_imported_symver
)
3881 /* Use the default symbol version created earlier. */
3882 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3887 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3889 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3891 /* If this is a hidden symbol, or if it is not version
3892 1, we append the version name to the symbol name.
3893 However, we do not modify a non-hidden absolute symbol
3894 if it is not a function, because it might be the version
3895 symbol itself. FIXME: What if it isn't? */
3896 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3898 && (!bfd_is_abs_section (sec
)
3899 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3902 size_t namelen
, verlen
, newlen
;
3905 if (isym
->st_shndx
!= SHN_UNDEF
)
3907 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3909 else if (vernum
> 1)
3911 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3917 (*_bfd_error_handler
)
3918 (_("%B: %s: invalid version %u (max %d)"),
3920 elf_tdata (abfd
)->cverdefs
);
3921 bfd_set_error (bfd_error_bad_value
);
3922 goto error_free_vers
;
3927 /* We cannot simply test for the number of
3928 entries in the VERNEED section since the
3929 numbers for the needed versions do not start
3931 Elf_Internal_Verneed
*t
;
3934 for (t
= elf_tdata (abfd
)->verref
;
3938 Elf_Internal_Vernaux
*a
;
3940 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3942 if (a
->vna_other
== vernum
)
3944 verstr
= a
->vna_nodename
;
3953 (*_bfd_error_handler
)
3954 (_("%B: %s: invalid needed version %d"),
3955 abfd
, name
, vernum
);
3956 bfd_set_error (bfd_error_bad_value
);
3957 goto error_free_vers
;
3961 namelen
= strlen (name
);
3962 verlen
= strlen (verstr
);
3963 newlen
= namelen
+ verlen
+ 2;
3964 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3965 && isym
->st_shndx
!= SHN_UNDEF
)
3968 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3969 if (newname
== NULL
)
3970 goto error_free_vers
;
3971 memcpy (newname
, name
, namelen
);
3972 p
= newname
+ namelen
;
3974 /* If this is a defined non-hidden version symbol,
3975 we add another @ to the name. This indicates the
3976 default version of the symbol. */
3977 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3978 && isym
->st_shndx
!= SHN_UNDEF
)
3980 memcpy (p
, verstr
, verlen
+ 1);
3985 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3986 &value
, &old_alignment
,
3987 sym_hash
, &skip
, &override
,
3988 &type_change_ok
, &size_change_ok
))
3989 goto error_free_vers
;
3998 while (h
->root
.type
== bfd_link_hash_indirect
3999 || h
->root
.type
== bfd_link_hash_warning
)
4000 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4002 /* Remember the old alignment if this is a common symbol, so
4003 that we don't reduce the alignment later on. We can't
4004 check later, because _bfd_generic_link_add_one_symbol
4005 will set a default for the alignment which we want to
4006 override. We also remember the old bfd where the existing
4007 definition comes from. */
4008 switch (h
->root
.type
)
4013 case bfd_link_hash_defined
:
4014 case bfd_link_hash_defweak
:
4015 old_bfd
= h
->root
.u
.def
.section
->owner
;
4018 case bfd_link_hash_common
:
4019 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4020 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4024 if (elf_tdata (abfd
)->verdef
!= NULL
4028 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4031 if (! (_bfd_generic_link_add_one_symbol
4032 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4033 (struct bfd_link_hash_entry
**) sym_hash
)))
4034 goto error_free_vers
;
4037 while (h
->root
.type
== bfd_link_hash_indirect
4038 || h
->root
.type
== bfd_link_hash_warning
)
4039 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4042 new_weakdef
= FALSE
;
4045 && (flags
& BSF_WEAK
) != 0
4046 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4047 && is_elf_hash_table (htab
)
4048 && h
->u
.weakdef
== NULL
)
4050 /* Keep a list of all weak defined non function symbols from
4051 a dynamic object, using the weakdef field. Later in this
4052 function we will set the weakdef field to the correct
4053 value. We only put non-function symbols from dynamic
4054 objects on this list, because that happens to be the only
4055 time we need to know the normal symbol corresponding to a
4056 weak symbol, and the information is time consuming to
4057 figure out. If the weakdef field is not already NULL,
4058 then this symbol was already defined by some previous
4059 dynamic object, and we will be using that previous
4060 definition anyhow. */
4062 h
->u
.weakdef
= weaks
;
4067 /* Set the alignment of a common symbol. */
4068 if ((common
|| bfd_is_com_section (sec
))
4069 && h
->root
.type
== bfd_link_hash_common
)
4074 align
= bfd_log2 (isym
->st_value
);
4077 /* The new symbol is a common symbol in a shared object.
4078 We need to get the alignment from the section. */
4079 align
= new_sec
->alignment_power
;
4081 if (align
> old_alignment
4082 /* Permit an alignment power of zero if an alignment of one
4083 is specified and no other alignments have been specified. */
4084 || (isym
->st_value
== 1 && old_alignment
== 0))
4085 h
->root
.u
.c
.p
->alignment_power
= align
;
4087 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4090 if (is_elf_hash_table (htab
))
4094 /* Check the alignment when a common symbol is involved. This
4095 can change when a common symbol is overridden by a normal
4096 definition or a common symbol is ignored due to the old
4097 normal definition. We need to make sure the maximum
4098 alignment is maintained. */
4099 if ((old_alignment
|| common
)
4100 && h
->root
.type
!= bfd_link_hash_common
)
4102 unsigned int common_align
;
4103 unsigned int normal_align
;
4104 unsigned int symbol_align
;
4108 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4109 if (h
->root
.u
.def
.section
->owner
!= NULL
4110 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4112 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4113 if (normal_align
> symbol_align
)
4114 normal_align
= symbol_align
;
4117 normal_align
= symbol_align
;
4121 common_align
= old_alignment
;
4122 common_bfd
= old_bfd
;
4127 common_align
= bfd_log2 (isym
->st_value
);
4129 normal_bfd
= old_bfd
;
4132 if (normal_align
< common_align
)
4134 /* PR binutils/2735 */
4135 if (normal_bfd
== NULL
)
4136 (*_bfd_error_handler
)
4137 (_("Warning: alignment %u of common symbol `%s' in %B"
4138 " is greater than the alignment (%u) of its section %A"),
4139 common_bfd
, h
->root
.u
.def
.section
,
4140 1 << common_align
, name
, 1 << normal_align
);
4142 (*_bfd_error_handler
)
4143 (_("Warning: alignment %u of symbol `%s' in %B"
4144 " is smaller than %u in %B"),
4145 normal_bfd
, common_bfd
,
4146 1 << normal_align
, name
, 1 << common_align
);
4150 /* Remember the symbol size if it isn't undefined. */
4151 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4152 && (definition
|| h
->size
== 0))
4155 && h
->size
!= isym
->st_size
4156 && ! size_change_ok
)
4157 (*_bfd_error_handler
)
4158 (_("Warning: size of symbol `%s' changed"
4159 " from %lu in %B to %lu in %B"),
4161 name
, (unsigned long) h
->size
,
4162 (unsigned long) isym
->st_size
);
4164 h
->size
= isym
->st_size
;
4167 /* If this is a common symbol, then we always want H->SIZE
4168 to be the size of the common symbol. The code just above
4169 won't fix the size if a common symbol becomes larger. We
4170 don't warn about a size change here, because that is
4171 covered by --warn-common. Allow changed between different
4173 if (h
->root
.type
== bfd_link_hash_common
)
4174 h
->size
= h
->root
.u
.c
.size
;
4176 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4177 && (definition
|| h
->type
== STT_NOTYPE
))
4179 if (h
->type
!= STT_NOTYPE
4180 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4181 && ! type_change_ok
)
4182 (*_bfd_error_handler
)
4183 (_("Warning: type of symbol `%s' changed"
4184 " from %d to %d in %B"),
4185 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4187 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4190 /* If st_other has a processor-specific meaning, specific
4191 code might be needed here. We never merge the visibility
4192 attribute with the one from a dynamic object. */
4193 if (bed
->elf_backend_merge_symbol_attribute
)
4194 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4197 /* If this symbol has default visibility and the user has requested
4198 we not re-export it, then mark it as hidden. */
4199 if (definition
&& !dynamic
4201 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4202 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4203 isym
->st_other
= (STV_HIDDEN
4204 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4206 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4208 unsigned char hvis
, symvis
, other
, nvis
;
4210 /* Only merge the visibility. Leave the remainder of the
4211 st_other field to elf_backend_merge_symbol_attribute. */
4212 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4214 /* Combine visibilities, using the most constraining one. */
4215 hvis
= ELF_ST_VISIBILITY (h
->other
);
4216 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4222 nvis
= hvis
< symvis
? hvis
: symvis
;
4224 h
->other
= other
| nvis
;
4227 /* Set a flag in the hash table entry indicating the type of
4228 reference or definition we just found. Keep a count of
4229 the number of dynamic symbols we find. A dynamic symbol
4230 is one which is referenced or defined by both a regular
4231 object and a shared object. */
4238 if (bind
!= STB_WEAK
)
4239 h
->ref_regular_nonweak
= 1;
4243 if (! info
->executable
4256 || (h
->u
.weakdef
!= NULL
4258 && h
->u
.weakdef
->dynindx
!= -1))
4262 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4264 /* We don't want to make debug symbol dynamic. */
4265 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4269 /* Check to see if we need to add an indirect symbol for
4270 the default name. */
4271 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4272 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4273 &sec
, &value
, &dynsym
,
4275 goto error_free_vers
;
4277 if (definition
&& !dynamic
)
4279 char *p
= strchr (name
, ELF_VER_CHR
);
4280 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4282 /* Queue non-default versions so that .symver x, x@FOO
4283 aliases can be checked. */
4286 amt
= ((isymend
- isym
+ 1)
4287 * sizeof (struct elf_link_hash_entry
*));
4288 nondeflt_vers
= bfd_malloc (amt
);
4290 goto error_free_vers
;
4292 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4296 if (dynsym
&& h
->dynindx
== -1)
4298 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4299 goto error_free_vers
;
4300 if (h
->u
.weakdef
!= NULL
4302 && h
->u
.weakdef
->dynindx
== -1)
4304 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4305 goto error_free_vers
;
4308 else if (dynsym
&& h
->dynindx
!= -1)
4309 /* If the symbol already has a dynamic index, but
4310 visibility says it should not be visible, turn it into
4312 switch (ELF_ST_VISIBILITY (h
->other
))
4316 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4327 const char *soname
= elf_dt_name (abfd
);
4329 /* A symbol from a library loaded via DT_NEEDED of some
4330 other library is referenced by a regular object.
4331 Add a DT_NEEDED entry for it. Issue an error if
4332 --no-add-needed is used. */
4333 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4335 (*_bfd_error_handler
)
4336 (_("%s: invalid DSO for symbol `%s' definition"),
4338 bfd_set_error (bfd_error_bad_value
);
4339 goto error_free_vers
;
4342 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4345 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4347 goto error_free_vers
;
4349 BFD_ASSERT (ret
== 0);
4354 if (extversym
!= NULL
)
4360 if (isymbuf
!= NULL
)
4366 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4370 /* Restore the symbol table. */
4371 if (bed
->as_needed_cleanup
)
4372 (*bed
->as_needed_cleanup
) (abfd
, info
);
4373 old_hash
= (char *) old_tab
+ tabsize
;
4374 old_ent
= (char *) old_hash
+ hashsize
;
4375 sym_hash
= elf_sym_hashes (abfd
);
4376 htab
->root
.table
.table
= old_table
;
4377 htab
->root
.table
.size
= old_size
;
4378 htab
->root
.table
.count
= old_count
;
4379 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4380 memcpy (sym_hash
, old_hash
, hashsize
);
4381 htab
->root
.undefs
= old_undefs
;
4382 htab
->root
.undefs_tail
= old_undefs_tail
;
4383 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4385 struct bfd_hash_entry
*p
;
4386 struct elf_link_hash_entry
*h
;
4388 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4390 h
= (struct elf_link_hash_entry
*) p
;
4391 if (h
->root
.type
== bfd_link_hash_warning
)
4392 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4393 if (h
->dynindx
>= old_dynsymcount
)
4394 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4396 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4397 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4398 h
= (struct elf_link_hash_entry
*) p
;
4399 if (h
->root
.type
== bfd_link_hash_warning
)
4401 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4402 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4407 /* Make a special call to the linker "notice" function to
4408 tell it that symbols added for crefs may need to be removed. */
4409 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4411 goto error_free_vers
;
4414 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4416 if (nondeflt_vers
!= NULL
)
4417 free (nondeflt_vers
);
4421 if (old_tab
!= NULL
)
4423 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4425 goto error_free_vers
;
4430 /* Now that all the symbols from this input file are created, handle
4431 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4432 if (nondeflt_vers
!= NULL
)
4434 bfd_size_type cnt
, symidx
;
4436 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4438 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4439 char *shortname
, *p
;
4441 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4443 || (h
->root
.type
!= bfd_link_hash_defined
4444 && h
->root
.type
!= bfd_link_hash_defweak
))
4447 amt
= p
- h
->root
.root
.string
;
4448 shortname
= bfd_malloc (amt
+ 1);
4450 goto error_free_vers
;
4451 memcpy (shortname
, h
->root
.root
.string
, amt
);
4452 shortname
[amt
] = '\0';
4454 hi
= (struct elf_link_hash_entry
*)
4455 bfd_link_hash_lookup (&htab
->root
, shortname
,
4456 FALSE
, FALSE
, FALSE
);
4458 && hi
->root
.type
== h
->root
.type
4459 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4460 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4462 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4463 hi
->root
.type
= bfd_link_hash_indirect
;
4464 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4465 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4466 sym_hash
= elf_sym_hashes (abfd
);
4468 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4469 if (sym_hash
[symidx
] == hi
)
4471 sym_hash
[symidx
] = h
;
4477 free (nondeflt_vers
);
4478 nondeflt_vers
= NULL
;
4481 /* Now set the weakdefs field correctly for all the weak defined
4482 symbols we found. The only way to do this is to search all the
4483 symbols. Since we only need the information for non functions in
4484 dynamic objects, that's the only time we actually put anything on
4485 the list WEAKS. We need this information so that if a regular
4486 object refers to a symbol defined weakly in a dynamic object, the
4487 real symbol in the dynamic object is also put in the dynamic
4488 symbols; we also must arrange for both symbols to point to the
4489 same memory location. We could handle the general case of symbol
4490 aliasing, but a general symbol alias can only be generated in
4491 assembler code, handling it correctly would be very time
4492 consuming, and other ELF linkers don't handle general aliasing
4496 struct elf_link_hash_entry
**hpp
;
4497 struct elf_link_hash_entry
**hppend
;
4498 struct elf_link_hash_entry
**sorted_sym_hash
;
4499 struct elf_link_hash_entry
*h
;
4502 /* Since we have to search the whole symbol list for each weak
4503 defined symbol, search time for N weak defined symbols will be
4504 O(N^2). Binary search will cut it down to O(NlogN). */
4505 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4506 sorted_sym_hash
= bfd_malloc (amt
);
4507 if (sorted_sym_hash
== NULL
)
4509 sym_hash
= sorted_sym_hash
;
4510 hpp
= elf_sym_hashes (abfd
);
4511 hppend
= hpp
+ extsymcount
;
4513 for (; hpp
< hppend
; hpp
++)
4517 && h
->root
.type
== bfd_link_hash_defined
4518 && !bed
->is_function_type (h
->type
))
4526 qsort (sorted_sym_hash
, sym_count
,
4527 sizeof (struct elf_link_hash_entry
*),
4530 while (weaks
!= NULL
)
4532 struct elf_link_hash_entry
*hlook
;
4539 weaks
= hlook
->u
.weakdef
;
4540 hlook
->u
.weakdef
= NULL
;
4542 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4543 || hlook
->root
.type
== bfd_link_hash_defweak
4544 || hlook
->root
.type
== bfd_link_hash_common
4545 || hlook
->root
.type
== bfd_link_hash_indirect
);
4546 slook
= hlook
->root
.u
.def
.section
;
4547 vlook
= hlook
->root
.u
.def
.value
;
4554 bfd_signed_vma vdiff
;
4556 h
= sorted_sym_hash
[idx
];
4557 vdiff
= vlook
- h
->root
.u
.def
.value
;
4564 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4577 /* We didn't find a value/section match. */
4581 for (i
= ilook
; i
< sym_count
; i
++)
4583 h
= sorted_sym_hash
[i
];
4585 /* Stop if value or section doesn't match. */
4586 if (h
->root
.u
.def
.value
!= vlook
4587 || h
->root
.u
.def
.section
!= slook
)
4589 else if (h
!= hlook
)
4591 hlook
->u
.weakdef
= h
;
4593 /* If the weak definition is in the list of dynamic
4594 symbols, make sure the real definition is put
4596 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4598 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4602 /* If the real definition is in the list of dynamic
4603 symbols, make sure the weak definition is put
4604 there as well. If we don't do this, then the
4605 dynamic loader might not merge the entries for the
4606 real definition and the weak definition. */
4607 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4609 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4617 free (sorted_sym_hash
);
4620 if (bed
->check_directives
)
4621 (*bed
->check_directives
) (abfd
, info
);
4623 /* If this object is the same format as the output object, and it is
4624 not a shared library, then let the backend look through the
4627 This is required to build global offset table entries and to
4628 arrange for dynamic relocs. It is not required for the
4629 particular common case of linking non PIC code, even when linking
4630 against shared libraries, but unfortunately there is no way of
4631 knowing whether an object file has been compiled PIC or not.
4632 Looking through the relocs is not particularly time consuming.
4633 The problem is that we must either (1) keep the relocs in memory,
4634 which causes the linker to require additional runtime memory or
4635 (2) read the relocs twice from the input file, which wastes time.
4636 This would be a good case for using mmap.
4638 I have no idea how to handle linking PIC code into a file of a
4639 different format. It probably can't be done. */
4641 && is_elf_hash_table (htab
)
4642 && htab
->root
.creator
== abfd
->xvec
4643 && bed
->check_relocs
!= NULL
)
4647 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4649 Elf_Internal_Rela
*internal_relocs
;
4652 if ((o
->flags
& SEC_RELOC
) == 0
4653 || o
->reloc_count
== 0
4654 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4655 && (o
->flags
& SEC_DEBUGGING
) != 0)
4656 || bfd_is_abs_section (o
->output_section
))
4659 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4661 if (internal_relocs
== NULL
)
4664 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4666 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4667 free (internal_relocs
);
4674 /* If this is a non-traditional link, try to optimize the handling
4675 of the .stab/.stabstr sections. */
4677 && ! info
->traditional_format
4678 && is_elf_hash_table (htab
)
4679 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4683 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4684 if (stabstr
!= NULL
)
4686 bfd_size_type string_offset
= 0;
4689 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4690 if (CONST_STRNEQ (stab
->name
, ".stab")
4691 && (!stab
->name
[5] ||
4692 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4693 && (stab
->flags
& SEC_MERGE
) == 0
4694 && !bfd_is_abs_section (stab
->output_section
))
4696 struct bfd_elf_section_data
*secdata
;
4698 secdata
= elf_section_data (stab
);
4699 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4700 stabstr
, &secdata
->sec_info
,
4703 if (secdata
->sec_info
)
4704 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4709 if (is_elf_hash_table (htab
) && add_needed
)
4711 /* Add this bfd to the loaded list. */
4712 struct elf_link_loaded_list
*n
;
4714 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4718 n
->next
= htab
->loaded
;
4725 if (old_tab
!= NULL
)
4727 if (nondeflt_vers
!= NULL
)
4728 free (nondeflt_vers
);
4729 if (extversym
!= NULL
)
4732 if (isymbuf
!= NULL
)
4738 /* Return the linker hash table entry of a symbol that might be
4739 satisfied by an archive symbol. Return -1 on error. */
4741 struct elf_link_hash_entry
*
4742 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4743 struct bfd_link_info
*info
,
4746 struct elf_link_hash_entry
*h
;
4750 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4754 /* If this is a default version (the name contains @@), look up the
4755 symbol again with only one `@' as well as without the version.
4756 The effect is that references to the symbol with and without the
4757 version will be matched by the default symbol in the archive. */
4759 p
= strchr (name
, ELF_VER_CHR
);
4760 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4763 /* First check with only one `@'. */
4764 len
= strlen (name
);
4765 copy
= bfd_alloc (abfd
, len
);
4767 return (struct elf_link_hash_entry
*) 0 - 1;
4769 first
= p
- name
+ 1;
4770 memcpy (copy
, name
, first
);
4771 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4773 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4776 /* We also need to check references to the symbol without the
4778 copy
[first
- 1] = '\0';
4779 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4780 FALSE
, FALSE
, FALSE
);
4783 bfd_release (abfd
, copy
);
4787 /* Add symbols from an ELF archive file to the linker hash table. We
4788 don't use _bfd_generic_link_add_archive_symbols because of a
4789 problem which arises on UnixWare. The UnixWare libc.so is an
4790 archive which includes an entry libc.so.1 which defines a bunch of
4791 symbols. The libc.so archive also includes a number of other
4792 object files, which also define symbols, some of which are the same
4793 as those defined in libc.so.1. Correct linking requires that we
4794 consider each object file in turn, and include it if it defines any
4795 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4796 this; it looks through the list of undefined symbols, and includes
4797 any object file which defines them. When this algorithm is used on
4798 UnixWare, it winds up pulling in libc.so.1 early and defining a
4799 bunch of symbols. This means that some of the other objects in the
4800 archive are not included in the link, which is incorrect since they
4801 precede libc.so.1 in the archive.
4803 Fortunately, ELF archive handling is simpler than that done by
4804 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4805 oddities. In ELF, if we find a symbol in the archive map, and the
4806 symbol is currently undefined, we know that we must pull in that
4809 Unfortunately, we do have to make multiple passes over the symbol
4810 table until nothing further is resolved. */
4813 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4816 bfd_boolean
*defined
= NULL
;
4817 bfd_boolean
*included
= NULL
;
4821 const struct elf_backend_data
*bed
;
4822 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4823 (bfd
*, struct bfd_link_info
*, const char *);
4825 if (! bfd_has_map (abfd
))
4827 /* An empty archive is a special case. */
4828 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4830 bfd_set_error (bfd_error_no_armap
);
4834 /* Keep track of all symbols we know to be already defined, and all
4835 files we know to be already included. This is to speed up the
4836 second and subsequent passes. */
4837 c
= bfd_ardata (abfd
)->symdef_count
;
4841 amt
*= sizeof (bfd_boolean
);
4842 defined
= bfd_zmalloc (amt
);
4843 included
= bfd_zmalloc (amt
);
4844 if (defined
== NULL
|| included
== NULL
)
4847 symdefs
= bfd_ardata (abfd
)->symdefs
;
4848 bed
= get_elf_backend_data (abfd
);
4849 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4862 symdefend
= symdef
+ c
;
4863 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4865 struct elf_link_hash_entry
*h
;
4867 struct bfd_link_hash_entry
*undefs_tail
;
4870 if (defined
[i
] || included
[i
])
4872 if (symdef
->file_offset
== last
)
4878 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4879 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4885 if (h
->root
.type
== bfd_link_hash_common
)
4887 /* We currently have a common symbol. The archive map contains
4888 a reference to this symbol, so we may want to include it. We
4889 only want to include it however, if this archive element
4890 contains a definition of the symbol, not just another common
4893 Unfortunately some archivers (including GNU ar) will put
4894 declarations of common symbols into their archive maps, as
4895 well as real definitions, so we cannot just go by the archive
4896 map alone. Instead we must read in the element's symbol
4897 table and check that to see what kind of symbol definition
4899 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4902 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4904 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4909 /* We need to include this archive member. */
4910 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4911 if (element
== NULL
)
4914 if (! bfd_check_format (element
, bfd_object
))
4917 /* Doublecheck that we have not included this object
4918 already--it should be impossible, but there may be
4919 something wrong with the archive. */
4920 if (element
->archive_pass
!= 0)
4922 bfd_set_error (bfd_error_bad_value
);
4925 element
->archive_pass
= 1;
4927 undefs_tail
= info
->hash
->undefs_tail
;
4929 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4932 if (! bfd_link_add_symbols (element
, info
))
4935 /* If there are any new undefined symbols, we need to make
4936 another pass through the archive in order to see whether
4937 they can be defined. FIXME: This isn't perfect, because
4938 common symbols wind up on undefs_tail and because an
4939 undefined symbol which is defined later on in this pass
4940 does not require another pass. This isn't a bug, but it
4941 does make the code less efficient than it could be. */
4942 if (undefs_tail
!= info
->hash
->undefs_tail
)
4945 /* Look backward to mark all symbols from this object file
4946 which we have already seen in this pass. */
4950 included
[mark
] = TRUE
;
4955 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4957 /* We mark subsequent symbols from this object file as we go
4958 on through the loop. */
4959 last
= symdef
->file_offset
;
4970 if (defined
!= NULL
)
4972 if (included
!= NULL
)
4977 /* Given an ELF BFD, add symbols to the global hash table as
4981 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4983 switch (bfd_get_format (abfd
))
4986 return elf_link_add_object_symbols (abfd
, info
);
4988 return elf_link_add_archive_symbols (abfd
, info
);
4990 bfd_set_error (bfd_error_wrong_format
);
4995 struct hash_codes_info
4997 unsigned long *hashcodes
;
5001 /* This function will be called though elf_link_hash_traverse to store
5002 all hash value of the exported symbols in an array. */
5005 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5007 struct hash_codes_info
*inf
= data
;
5013 if (h
->root
.type
== bfd_link_hash_warning
)
5014 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5016 /* Ignore indirect symbols. These are added by the versioning code. */
5017 if (h
->dynindx
== -1)
5020 name
= h
->root
.root
.string
;
5021 p
= strchr (name
, ELF_VER_CHR
);
5024 alc
= bfd_malloc (p
- name
+ 1);
5030 memcpy (alc
, name
, p
- name
);
5031 alc
[p
- name
] = '\0';
5035 /* Compute the hash value. */
5036 ha
= bfd_elf_hash (name
);
5038 /* Store the found hash value in the array given as the argument. */
5039 *(inf
->hashcodes
)++ = ha
;
5041 /* And store it in the struct so that we can put it in the hash table
5043 h
->u
.elf_hash_value
= ha
;
5051 struct collect_gnu_hash_codes
5054 const struct elf_backend_data
*bed
;
5055 unsigned long int nsyms
;
5056 unsigned long int maskbits
;
5057 unsigned long int *hashcodes
;
5058 unsigned long int *hashval
;
5059 unsigned long int *indx
;
5060 unsigned long int *counts
;
5063 long int min_dynindx
;
5064 unsigned long int bucketcount
;
5065 unsigned long int symindx
;
5066 long int local_indx
;
5067 long int shift1
, shift2
;
5068 unsigned long int mask
;
5072 /* This function will be called though elf_link_hash_traverse to store
5073 all hash value of the exported symbols in an array. */
5076 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5078 struct collect_gnu_hash_codes
*s
= data
;
5084 if (h
->root
.type
== bfd_link_hash_warning
)
5085 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5087 /* Ignore indirect symbols. These are added by the versioning code. */
5088 if (h
->dynindx
== -1)
5091 /* Ignore also local symbols and undefined symbols. */
5092 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5095 name
= h
->root
.root
.string
;
5096 p
= strchr (name
, ELF_VER_CHR
);
5099 alc
= bfd_malloc (p
- name
+ 1);
5105 memcpy (alc
, name
, p
- name
);
5106 alc
[p
- name
] = '\0';
5110 /* Compute the hash value. */
5111 ha
= bfd_elf_gnu_hash (name
);
5113 /* Store the found hash value in the array for compute_bucket_count,
5114 and also for .dynsym reordering purposes. */
5115 s
->hashcodes
[s
->nsyms
] = ha
;
5116 s
->hashval
[h
->dynindx
] = ha
;
5118 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5119 s
->min_dynindx
= h
->dynindx
;
5127 /* This function will be called though elf_link_hash_traverse to do
5128 final dynaminc symbol renumbering. */
5131 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5133 struct collect_gnu_hash_codes
*s
= data
;
5134 unsigned long int bucket
;
5135 unsigned long int val
;
5137 if (h
->root
.type
== bfd_link_hash_warning
)
5138 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5140 /* Ignore indirect symbols. */
5141 if (h
->dynindx
== -1)
5144 /* Ignore also local symbols and undefined symbols. */
5145 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5147 if (h
->dynindx
>= s
->min_dynindx
)
5148 h
->dynindx
= s
->local_indx
++;
5152 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5153 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5154 & ((s
->maskbits
>> s
->shift1
) - 1);
5155 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5157 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5158 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5159 if (s
->counts
[bucket
] == 1)
5160 /* Last element terminates the chain. */
5162 bfd_put_32 (s
->output_bfd
, val
,
5163 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5164 --s
->counts
[bucket
];
5165 h
->dynindx
= s
->indx
[bucket
]++;
5169 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5172 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5174 return !(h
->forced_local
5175 || h
->root
.type
== bfd_link_hash_undefined
5176 || h
->root
.type
== bfd_link_hash_undefweak
5177 || ((h
->root
.type
== bfd_link_hash_defined
5178 || h
->root
.type
== bfd_link_hash_defweak
)
5179 && h
->root
.u
.def
.section
->output_section
== NULL
));
5182 /* Array used to determine the number of hash table buckets to use
5183 based on the number of symbols there are. If there are fewer than
5184 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5185 fewer than 37 we use 17 buckets, and so forth. We never use more
5186 than 32771 buckets. */
5188 static const size_t elf_buckets
[] =
5190 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5194 /* Compute bucket count for hashing table. We do not use a static set
5195 of possible tables sizes anymore. Instead we determine for all
5196 possible reasonable sizes of the table the outcome (i.e., the
5197 number of collisions etc) and choose the best solution. The
5198 weighting functions are not too simple to allow the table to grow
5199 without bounds. Instead one of the weighting factors is the size.
5200 Therefore the result is always a good payoff between few collisions
5201 (= short chain lengths) and table size. */
5203 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5204 unsigned long int nsyms
, int gnu_hash
)
5206 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5207 size_t best_size
= 0;
5208 unsigned long int i
;
5211 /* We have a problem here. The following code to optimize the table
5212 size requires an integer type with more the 32 bits. If
5213 BFD_HOST_U_64_BIT is set we know about such a type. */
5214 #ifdef BFD_HOST_U_64_BIT
5219 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5220 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5221 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5222 unsigned long int *counts
;
5224 /* Possible optimization parameters: if we have NSYMS symbols we say
5225 that the hashing table must at least have NSYMS/4 and at most
5227 minsize
= nsyms
/ 4;
5230 best_size
= maxsize
= nsyms
* 2;
5235 if ((best_size
& 31) == 0)
5239 /* Create array where we count the collisions in. We must use bfd_malloc
5240 since the size could be large. */
5242 amt
*= sizeof (unsigned long int);
5243 counts
= bfd_malloc (amt
);
5247 /* Compute the "optimal" size for the hash table. The criteria is a
5248 minimal chain length. The minor criteria is (of course) the size
5250 for (i
= minsize
; i
< maxsize
; ++i
)
5252 /* Walk through the array of hashcodes and count the collisions. */
5253 BFD_HOST_U_64_BIT max
;
5254 unsigned long int j
;
5255 unsigned long int fact
;
5257 if (gnu_hash
&& (i
& 31) == 0)
5260 memset (counts
, '\0', i
* sizeof (unsigned long int));
5262 /* Determine how often each hash bucket is used. */
5263 for (j
= 0; j
< nsyms
; ++j
)
5264 ++counts
[hashcodes
[j
] % i
];
5266 /* For the weight function we need some information about the
5267 pagesize on the target. This is information need not be 100%
5268 accurate. Since this information is not available (so far) we
5269 define it here to a reasonable default value. If it is crucial
5270 to have a better value some day simply define this value. */
5271 # ifndef BFD_TARGET_PAGESIZE
5272 # define BFD_TARGET_PAGESIZE (4096)
5275 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5277 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5280 /* Variant 1: optimize for short chains. We add the squares
5281 of all the chain lengths (which favors many small chain
5282 over a few long chains). */
5283 for (j
= 0; j
< i
; ++j
)
5284 max
+= counts
[j
] * counts
[j
];
5286 /* This adds penalties for the overall size of the table. */
5287 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5290 /* Variant 2: Optimize a lot more for small table. Here we
5291 also add squares of the size but we also add penalties for
5292 empty slots (the +1 term). */
5293 for (j
= 0; j
< i
; ++j
)
5294 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5296 /* The overall size of the table is considered, but not as
5297 strong as in variant 1, where it is squared. */
5298 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5302 /* Compare with current best results. */
5303 if (max
< best_chlen
)
5313 #endif /* defined (BFD_HOST_U_64_BIT) */
5315 /* This is the fallback solution if no 64bit type is available or if we
5316 are not supposed to spend much time on optimizations. We select the
5317 bucket count using a fixed set of numbers. */
5318 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5320 best_size
= elf_buckets
[i
];
5321 if (nsyms
< elf_buckets
[i
+ 1])
5324 if (gnu_hash
&& best_size
< 2)
5331 /* Set up the sizes and contents of the ELF dynamic sections. This is
5332 called by the ELF linker emulation before_allocation routine. We
5333 must set the sizes of the sections before the linker sets the
5334 addresses of the various sections. */
5337 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5340 const char *filter_shlib
,
5341 const char * const *auxiliary_filters
,
5342 struct bfd_link_info
*info
,
5343 asection
**sinterpptr
,
5344 struct bfd_elf_version_tree
*verdefs
)
5346 bfd_size_type soname_indx
;
5348 const struct elf_backend_data
*bed
;
5349 struct elf_assign_sym_version_info asvinfo
;
5353 soname_indx
= (bfd_size_type
) -1;
5355 if (!is_elf_hash_table (info
->hash
))
5358 bed
= get_elf_backend_data (output_bfd
);
5359 if (info
->execstack
)
5360 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5361 else if (info
->noexecstack
)
5362 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5366 asection
*notesec
= NULL
;
5369 for (inputobj
= info
->input_bfds
;
5371 inputobj
= inputobj
->link_next
)
5375 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5377 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5380 if (s
->flags
& SEC_CODE
)
5384 else if (bed
->default_execstack
)
5389 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5390 if (exec
&& info
->relocatable
5391 && notesec
->output_section
!= bfd_abs_section_ptr
)
5392 notesec
->output_section
->flags
|= SEC_CODE
;
5396 /* Any syms created from now on start with -1 in
5397 got.refcount/offset and plt.refcount/offset. */
5398 elf_hash_table (info
)->init_got_refcount
5399 = elf_hash_table (info
)->init_got_offset
;
5400 elf_hash_table (info
)->init_plt_refcount
5401 = elf_hash_table (info
)->init_plt_offset
;
5403 /* The backend may have to create some sections regardless of whether
5404 we're dynamic or not. */
5405 if (bed
->elf_backend_always_size_sections
5406 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5409 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5412 dynobj
= elf_hash_table (info
)->dynobj
;
5414 /* If there were no dynamic objects in the link, there is nothing to
5419 if (elf_hash_table (info
)->dynamic_sections_created
)
5421 struct elf_info_failed eif
;
5422 struct elf_link_hash_entry
*h
;
5424 struct bfd_elf_version_tree
*t
;
5425 struct bfd_elf_version_expr
*d
;
5427 bfd_boolean all_defined
;
5429 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5430 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5434 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5436 if (soname_indx
== (bfd_size_type
) -1
5437 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5443 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5445 info
->flags
|= DF_SYMBOLIC
;
5452 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5454 if (indx
== (bfd_size_type
) -1
5455 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5458 if (info
->new_dtags
)
5460 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5461 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5466 if (filter_shlib
!= NULL
)
5470 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5471 filter_shlib
, TRUE
);
5472 if (indx
== (bfd_size_type
) -1
5473 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5477 if (auxiliary_filters
!= NULL
)
5479 const char * const *p
;
5481 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5485 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5487 if (indx
== (bfd_size_type
) -1
5488 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5494 eif
.verdefs
= verdefs
;
5497 /* If we are supposed to export all symbols into the dynamic symbol
5498 table (this is not the normal case), then do so. */
5499 if (info
->export_dynamic
5500 || (info
->executable
&& info
->dynamic
))
5502 elf_link_hash_traverse (elf_hash_table (info
),
5503 _bfd_elf_export_symbol
,
5509 /* Make all global versions with definition. */
5510 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5511 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5512 if (!d
->symver
&& d
->symbol
)
5514 const char *verstr
, *name
;
5515 size_t namelen
, verlen
, newlen
;
5517 struct elf_link_hash_entry
*newh
;
5520 namelen
= strlen (name
);
5522 verlen
= strlen (verstr
);
5523 newlen
= namelen
+ verlen
+ 3;
5525 newname
= bfd_malloc (newlen
);
5526 if (newname
== NULL
)
5528 memcpy (newname
, name
, namelen
);
5530 /* Check the hidden versioned definition. */
5531 p
= newname
+ namelen
;
5533 memcpy (p
, verstr
, verlen
+ 1);
5534 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5535 newname
, FALSE
, FALSE
,
5538 || (newh
->root
.type
!= bfd_link_hash_defined
5539 && newh
->root
.type
!= bfd_link_hash_defweak
))
5541 /* Check the default versioned definition. */
5543 memcpy (p
, verstr
, verlen
+ 1);
5544 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5545 newname
, FALSE
, FALSE
,
5550 /* Mark this version if there is a definition and it is
5551 not defined in a shared object. */
5553 && !newh
->def_dynamic
5554 && (newh
->root
.type
== bfd_link_hash_defined
5555 || newh
->root
.type
== bfd_link_hash_defweak
))
5559 /* Attach all the symbols to their version information. */
5560 asvinfo
.output_bfd
= output_bfd
;
5561 asvinfo
.info
= info
;
5562 asvinfo
.verdefs
= verdefs
;
5563 asvinfo
.failed
= FALSE
;
5565 elf_link_hash_traverse (elf_hash_table (info
),
5566 _bfd_elf_link_assign_sym_version
,
5571 if (!info
->allow_undefined_version
)
5573 /* Check if all global versions have a definition. */
5575 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5576 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5577 if (!d
->symver
&& !d
->script
)
5579 (*_bfd_error_handler
)
5580 (_("%s: undefined version: %s"),
5581 d
->pattern
, t
->name
);
5582 all_defined
= FALSE
;
5587 bfd_set_error (bfd_error_bad_value
);
5592 /* Find all symbols which were defined in a dynamic object and make
5593 the backend pick a reasonable value for them. */
5594 elf_link_hash_traverse (elf_hash_table (info
),
5595 _bfd_elf_adjust_dynamic_symbol
,
5600 /* Add some entries to the .dynamic section. We fill in some of the
5601 values later, in bfd_elf_final_link, but we must add the entries
5602 now so that we know the final size of the .dynamic section. */
5604 /* If there are initialization and/or finalization functions to
5605 call then add the corresponding DT_INIT/DT_FINI entries. */
5606 h
= (info
->init_function
5607 ? elf_link_hash_lookup (elf_hash_table (info
),
5608 info
->init_function
, FALSE
,
5615 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5618 h
= (info
->fini_function
5619 ? elf_link_hash_lookup (elf_hash_table (info
),
5620 info
->fini_function
, FALSE
,
5627 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5631 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5632 if (s
!= NULL
&& s
->linker_has_input
)
5634 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5635 if (! info
->executable
)
5640 for (sub
= info
->input_bfds
; sub
!= NULL
;
5641 sub
= sub
->link_next
)
5642 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5643 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5644 if (elf_section_data (o
)->this_hdr
.sh_type
5645 == SHT_PREINIT_ARRAY
)
5647 (*_bfd_error_handler
)
5648 (_("%B: .preinit_array section is not allowed in DSO"),
5653 bfd_set_error (bfd_error_nonrepresentable_section
);
5657 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5658 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5661 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5662 if (s
!= NULL
&& s
->linker_has_input
)
5664 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5665 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5668 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5669 if (s
!= NULL
&& s
->linker_has_input
)
5671 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5672 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5676 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5677 /* If .dynstr is excluded from the link, we don't want any of
5678 these tags. Strictly, we should be checking each section
5679 individually; This quick check covers for the case where
5680 someone does a /DISCARD/ : { *(*) }. */
5681 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5683 bfd_size_type strsize
;
5685 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5686 if ((info
->emit_hash
5687 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5688 || (info
->emit_gnu_hash
5689 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5690 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5691 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5692 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5693 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5694 bed
->s
->sizeof_sym
))
5699 /* The backend must work out the sizes of all the other dynamic
5701 if (bed
->elf_backend_size_dynamic_sections
5702 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5705 if (elf_hash_table (info
)->dynamic_sections_created
)
5707 unsigned long section_sym_count
;
5710 /* Set up the version definition section. */
5711 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5712 BFD_ASSERT (s
!= NULL
);
5714 /* We may have created additional version definitions if we are
5715 just linking a regular application. */
5716 verdefs
= asvinfo
.verdefs
;
5718 /* Skip anonymous version tag. */
5719 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5720 verdefs
= verdefs
->next
;
5722 if (verdefs
== NULL
&& !info
->create_default_symver
)
5723 s
->flags
|= SEC_EXCLUDE
;
5728 struct bfd_elf_version_tree
*t
;
5730 Elf_Internal_Verdef def
;
5731 Elf_Internal_Verdaux defaux
;
5732 struct bfd_link_hash_entry
*bh
;
5733 struct elf_link_hash_entry
*h
;
5739 /* Make space for the base version. */
5740 size
+= sizeof (Elf_External_Verdef
);
5741 size
+= sizeof (Elf_External_Verdaux
);
5744 /* Make space for the default version. */
5745 if (info
->create_default_symver
)
5747 size
+= sizeof (Elf_External_Verdef
);
5751 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5753 struct bfd_elf_version_deps
*n
;
5755 size
+= sizeof (Elf_External_Verdef
);
5756 size
+= sizeof (Elf_External_Verdaux
);
5759 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5760 size
+= sizeof (Elf_External_Verdaux
);
5764 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5765 if (s
->contents
== NULL
&& s
->size
!= 0)
5768 /* Fill in the version definition section. */
5772 def
.vd_version
= VER_DEF_CURRENT
;
5773 def
.vd_flags
= VER_FLG_BASE
;
5776 if (info
->create_default_symver
)
5778 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5779 def
.vd_next
= sizeof (Elf_External_Verdef
);
5783 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5784 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5785 + sizeof (Elf_External_Verdaux
));
5788 if (soname_indx
!= (bfd_size_type
) -1)
5790 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5792 def
.vd_hash
= bfd_elf_hash (soname
);
5793 defaux
.vda_name
= soname_indx
;
5800 name
= lbasename (output_bfd
->filename
);
5801 def
.vd_hash
= bfd_elf_hash (name
);
5802 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5804 if (indx
== (bfd_size_type
) -1)
5806 defaux
.vda_name
= indx
;
5808 defaux
.vda_next
= 0;
5810 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5811 (Elf_External_Verdef
*) p
);
5812 p
+= sizeof (Elf_External_Verdef
);
5813 if (info
->create_default_symver
)
5815 /* Add a symbol representing this version. */
5817 if (! (_bfd_generic_link_add_one_symbol
5818 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5820 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5822 h
= (struct elf_link_hash_entry
*) bh
;
5825 h
->type
= STT_OBJECT
;
5826 h
->verinfo
.vertree
= NULL
;
5828 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5831 /* Create a duplicate of the base version with the same
5832 aux block, but different flags. */
5835 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5837 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5838 + sizeof (Elf_External_Verdaux
));
5841 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5842 (Elf_External_Verdef
*) p
);
5843 p
+= sizeof (Elf_External_Verdef
);
5845 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5846 (Elf_External_Verdaux
*) p
);
5847 p
+= sizeof (Elf_External_Verdaux
);
5849 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5852 struct bfd_elf_version_deps
*n
;
5855 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5858 /* Add a symbol representing this version. */
5860 if (! (_bfd_generic_link_add_one_symbol
5861 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5863 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5865 h
= (struct elf_link_hash_entry
*) bh
;
5868 h
->type
= STT_OBJECT
;
5869 h
->verinfo
.vertree
= t
;
5871 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5874 def
.vd_version
= VER_DEF_CURRENT
;
5876 if (t
->globals
.list
== NULL
5877 && t
->locals
.list
== NULL
5879 def
.vd_flags
|= VER_FLG_WEAK
;
5880 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5881 def
.vd_cnt
= cdeps
+ 1;
5882 def
.vd_hash
= bfd_elf_hash (t
->name
);
5883 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5885 if (t
->next
!= NULL
)
5886 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5887 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5889 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5890 (Elf_External_Verdef
*) p
);
5891 p
+= sizeof (Elf_External_Verdef
);
5893 defaux
.vda_name
= h
->dynstr_index
;
5894 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5896 defaux
.vda_next
= 0;
5897 if (t
->deps
!= NULL
)
5898 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5899 t
->name_indx
= defaux
.vda_name
;
5901 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5902 (Elf_External_Verdaux
*) p
);
5903 p
+= sizeof (Elf_External_Verdaux
);
5905 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5907 if (n
->version_needed
== NULL
)
5909 /* This can happen if there was an error in the
5911 defaux
.vda_name
= 0;
5915 defaux
.vda_name
= n
->version_needed
->name_indx
;
5916 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5919 if (n
->next
== NULL
)
5920 defaux
.vda_next
= 0;
5922 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5924 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5925 (Elf_External_Verdaux
*) p
);
5926 p
+= sizeof (Elf_External_Verdaux
);
5930 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5931 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5934 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5937 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5939 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5942 else if (info
->flags
& DF_BIND_NOW
)
5944 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5950 if (info
->executable
)
5951 info
->flags_1
&= ~ (DF_1_INITFIRST
5954 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5958 /* Work out the size of the version reference section. */
5960 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5961 BFD_ASSERT (s
!= NULL
);
5963 struct elf_find_verdep_info sinfo
;
5965 sinfo
.output_bfd
= output_bfd
;
5967 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5968 if (sinfo
.vers
== 0)
5970 sinfo
.failed
= FALSE
;
5972 elf_link_hash_traverse (elf_hash_table (info
),
5973 _bfd_elf_link_find_version_dependencies
,
5978 if (elf_tdata (output_bfd
)->verref
== NULL
)
5979 s
->flags
|= SEC_EXCLUDE
;
5982 Elf_Internal_Verneed
*t
;
5987 /* Build the version definition section. */
5990 for (t
= elf_tdata (output_bfd
)->verref
;
5994 Elf_Internal_Vernaux
*a
;
5996 size
+= sizeof (Elf_External_Verneed
);
5998 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5999 size
+= sizeof (Elf_External_Vernaux
);
6003 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6004 if (s
->contents
== NULL
)
6008 for (t
= elf_tdata (output_bfd
)->verref
;
6013 Elf_Internal_Vernaux
*a
;
6017 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6020 t
->vn_version
= VER_NEED_CURRENT
;
6022 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6023 elf_dt_name (t
->vn_bfd
) != NULL
6024 ? elf_dt_name (t
->vn_bfd
)
6025 : lbasename (t
->vn_bfd
->filename
),
6027 if (indx
== (bfd_size_type
) -1)
6030 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6031 if (t
->vn_nextref
== NULL
)
6034 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6035 + caux
* sizeof (Elf_External_Vernaux
));
6037 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6038 (Elf_External_Verneed
*) p
);
6039 p
+= sizeof (Elf_External_Verneed
);
6041 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6043 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6044 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6045 a
->vna_nodename
, FALSE
);
6046 if (indx
== (bfd_size_type
) -1)
6049 if (a
->vna_nextptr
== NULL
)
6052 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6054 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6055 (Elf_External_Vernaux
*) p
);
6056 p
+= sizeof (Elf_External_Vernaux
);
6060 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6061 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6064 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6068 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6069 && elf_tdata (output_bfd
)->cverdefs
== 0)
6070 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6071 §ion_sym_count
) == 0)
6073 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6074 s
->flags
|= SEC_EXCLUDE
;
6080 /* Find the first non-excluded output section. We'll use its
6081 section symbol for some emitted relocs. */
6083 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6087 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6088 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6089 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6091 elf_hash_table (info
)->text_index_section
= s
;
6096 /* Find two non-excluded output sections, one for code, one for data.
6097 We'll use their section symbols for some emitted relocs. */
6099 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6103 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6104 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6105 == (SEC_ALLOC
| SEC_READONLY
))
6106 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6108 elf_hash_table (info
)->text_index_section
= s
;
6112 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6113 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6114 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6116 elf_hash_table (info
)->data_index_section
= s
;
6120 if (elf_hash_table (info
)->text_index_section
== NULL
)
6121 elf_hash_table (info
)->text_index_section
6122 = elf_hash_table (info
)->data_index_section
;
6126 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6128 const struct elf_backend_data
*bed
;
6130 if (!is_elf_hash_table (info
->hash
))
6133 bed
= get_elf_backend_data (output_bfd
);
6134 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6136 if (elf_hash_table (info
)->dynamic_sections_created
)
6140 bfd_size_type dynsymcount
;
6141 unsigned long section_sym_count
;
6142 unsigned int dtagcount
;
6144 dynobj
= elf_hash_table (info
)->dynobj
;
6146 /* Assign dynsym indicies. In a shared library we generate a
6147 section symbol for each output section, which come first.
6148 Next come all of the back-end allocated local dynamic syms,
6149 followed by the rest of the global symbols. */
6151 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6152 §ion_sym_count
);
6154 /* Work out the size of the symbol version section. */
6155 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6156 BFD_ASSERT (s
!= NULL
);
6157 if (dynsymcount
!= 0
6158 && (s
->flags
& SEC_EXCLUDE
) == 0)
6160 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6161 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6162 if (s
->contents
== NULL
)
6165 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6169 /* Set the size of the .dynsym and .hash sections. We counted
6170 the number of dynamic symbols in elf_link_add_object_symbols.
6171 We will build the contents of .dynsym and .hash when we build
6172 the final symbol table, because until then we do not know the
6173 correct value to give the symbols. We built the .dynstr
6174 section as we went along in elf_link_add_object_symbols. */
6175 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6176 BFD_ASSERT (s
!= NULL
);
6177 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6179 if (dynsymcount
!= 0)
6181 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6182 if (s
->contents
== NULL
)
6185 /* The first entry in .dynsym is a dummy symbol.
6186 Clear all the section syms, in case we don't output them all. */
6187 ++section_sym_count
;
6188 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6191 elf_hash_table (info
)->bucketcount
= 0;
6193 /* Compute the size of the hashing table. As a side effect this
6194 computes the hash values for all the names we export. */
6195 if (info
->emit_hash
)
6197 unsigned long int *hashcodes
;
6198 struct hash_codes_info hashinf
;
6200 unsigned long int nsyms
;
6202 size_t hash_entry_size
;
6204 /* Compute the hash values for all exported symbols. At the same
6205 time store the values in an array so that we could use them for
6207 amt
= dynsymcount
* sizeof (unsigned long int);
6208 hashcodes
= bfd_malloc (amt
);
6209 if (hashcodes
== NULL
)
6211 hashinf
.hashcodes
= hashcodes
;
6212 hashinf
.error
= FALSE
;
6214 /* Put all hash values in HASHCODES. */
6215 elf_link_hash_traverse (elf_hash_table (info
),
6216 elf_collect_hash_codes
, &hashinf
);
6220 nsyms
= hashinf
.hashcodes
- hashcodes
;
6222 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6225 if (bucketcount
== 0)
6228 elf_hash_table (info
)->bucketcount
= bucketcount
;
6230 s
= bfd_get_section_by_name (dynobj
, ".hash");
6231 BFD_ASSERT (s
!= NULL
);
6232 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6233 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6234 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6235 if (s
->contents
== NULL
)
6238 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6239 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6240 s
->contents
+ hash_entry_size
);
6243 if (info
->emit_gnu_hash
)
6246 unsigned char *contents
;
6247 struct collect_gnu_hash_codes cinfo
;
6251 memset (&cinfo
, 0, sizeof (cinfo
));
6253 /* Compute the hash values for all exported symbols. At the same
6254 time store the values in an array so that we could use them for
6256 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6257 cinfo
.hashcodes
= bfd_malloc (amt
);
6258 if (cinfo
.hashcodes
== NULL
)
6261 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6262 cinfo
.min_dynindx
= -1;
6263 cinfo
.output_bfd
= output_bfd
;
6266 /* Put all hash values in HASHCODES. */
6267 elf_link_hash_traverse (elf_hash_table (info
),
6268 elf_collect_gnu_hash_codes
, &cinfo
);
6273 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6275 if (bucketcount
== 0)
6277 free (cinfo
.hashcodes
);
6281 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6282 BFD_ASSERT (s
!= NULL
);
6284 if (cinfo
.nsyms
== 0)
6286 /* Empty .gnu.hash section is special. */
6287 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6288 free (cinfo
.hashcodes
);
6289 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6290 contents
= bfd_zalloc (output_bfd
, s
->size
);
6291 if (contents
== NULL
)
6293 s
->contents
= contents
;
6294 /* 1 empty bucket. */
6295 bfd_put_32 (output_bfd
, 1, contents
);
6296 /* SYMIDX above the special symbol 0. */
6297 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6298 /* Just one word for bitmask. */
6299 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6300 /* Only hash fn bloom filter. */
6301 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6302 /* No hashes are valid - empty bitmask. */
6303 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6304 /* No hashes in the only bucket. */
6305 bfd_put_32 (output_bfd
, 0,
6306 contents
+ 16 + bed
->s
->arch_size
/ 8);
6310 unsigned long int maskwords
, maskbitslog2
;
6311 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6313 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6314 if (maskbitslog2
< 3)
6316 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6317 maskbitslog2
= maskbitslog2
+ 3;
6319 maskbitslog2
= maskbitslog2
+ 2;
6320 if (bed
->s
->arch_size
== 64)
6322 if (maskbitslog2
== 5)
6328 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6329 cinfo
.shift2
= maskbitslog2
;
6330 cinfo
.maskbits
= 1 << maskbitslog2
;
6331 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6332 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6333 amt
+= maskwords
* sizeof (bfd_vma
);
6334 cinfo
.bitmask
= bfd_malloc (amt
);
6335 if (cinfo
.bitmask
== NULL
)
6337 free (cinfo
.hashcodes
);
6341 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6342 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6343 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6344 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6346 /* Determine how often each hash bucket is used. */
6347 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6348 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6349 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6351 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6352 if (cinfo
.counts
[i
] != 0)
6354 cinfo
.indx
[i
] = cnt
;
6355 cnt
+= cinfo
.counts
[i
];
6357 BFD_ASSERT (cnt
== dynsymcount
);
6358 cinfo
.bucketcount
= bucketcount
;
6359 cinfo
.local_indx
= cinfo
.min_dynindx
;
6361 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6362 s
->size
+= cinfo
.maskbits
/ 8;
6363 contents
= bfd_zalloc (output_bfd
, s
->size
);
6364 if (contents
== NULL
)
6366 free (cinfo
.bitmask
);
6367 free (cinfo
.hashcodes
);
6371 s
->contents
= contents
;
6372 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6373 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6374 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6375 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6376 contents
+= 16 + cinfo
.maskbits
/ 8;
6378 for (i
= 0; i
< bucketcount
; ++i
)
6380 if (cinfo
.counts
[i
] == 0)
6381 bfd_put_32 (output_bfd
, 0, contents
);
6383 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6387 cinfo
.contents
= contents
;
6389 /* Renumber dynamic symbols, populate .gnu.hash section. */
6390 elf_link_hash_traverse (elf_hash_table (info
),
6391 elf_renumber_gnu_hash_syms
, &cinfo
);
6393 contents
= s
->contents
+ 16;
6394 for (i
= 0; i
< maskwords
; ++i
)
6396 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6398 contents
+= bed
->s
->arch_size
/ 8;
6401 free (cinfo
.bitmask
);
6402 free (cinfo
.hashcodes
);
6406 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6407 BFD_ASSERT (s
!= NULL
);
6409 elf_finalize_dynstr (output_bfd
, info
);
6411 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6413 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6414 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6421 /* Indicate that we are only retrieving symbol values from this
6425 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
6427 if (is_elf_hash_table (info
->hash
))
6428 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
6429 _bfd_generic_link_just_syms (sec
, info
);
6432 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6435 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6438 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
6439 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
6442 /* Finish SHF_MERGE section merging. */
6445 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6450 if (!is_elf_hash_table (info
->hash
))
6453 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6454 if ((ibfd
->flags
& DYNAMIC
) == 0)
6455 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6456 if ((sec
->flags
& SEC_MERGE
) != 0
6457 && !bfd_is_abs_section (sec
->output_section
))
6459 struct bfd_elf_section_data
*secdata
;
6461 secdata
= elf_section_data (sec
);
6462 if (! _bfd_add_merge_section (abfd
,
6463 &elf_hash_table (info
)->merge_info
,
6464 sec
, &secdata
->sec_info
))
6466 else if (secdata
->sec_info
)
6467 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
6470 if (elf_hash_table (info
)->merge_info
!= NULL
)
6471 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6472 merge_sections_remove_hook
);
6476 /* Create an entry in an ELF linker hash table. */
6478 struct bfd_hash_entry
*
6479 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6480 struct bfd_hash_table
*table
,
6483 /* Allocate the structure if it has not already been allocated by a
6487 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6492 /* Call the allocation method of the superclass. */
6493 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6496 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6497 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6499 /* Set local fields. */
6502 ret
->got
= htab
->init_got_refcount
;
6503 ret
->plt
= htab
->init_plt_refcount
;
6504 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6505 - offsetof (struct elf_link_hash_entry
, size
)));
6506 /* Assume that we have been called by a non-ELF symbol reader.
6507 This flag is then reset by the code which reads an ELF input
6508 file. This ensures that a symbol created by a non-ELF symbol
6509 reader will have the flag set correctly. */
6516 /* Copy data from an indirect symbol to its direct symbol, hiding the
6517 old indirect symbol. Also used for copying flags to a weakdef. */
6520 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6521 struct elf_link_hash_entry
*dir
,
6522 struct elf_link_hash_entry
*ind
)
6524 struct elf_link_hash_table
*htab
;
6526 /* Copy down any references that we may have already seen to the
6527 symbol which just became indirect. */
6529 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6530 dir
->ref_regular
|= ind
->ref_regular
;
6531 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6532 dir
->non_got_ref
|= ind
->non_got_ref
;
6533 dir
->needs_plt
|= ind
->needs_plt
;
6534 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6536 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6539 /* Copy over the global and procedure linkage table refcount entries.
6540 These may have been already set up by a check_relocs routine. */
6541 htab
= elf_hash_table (info
);
6542 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6544 if (dir
->got
.refcount
< 0)
6545 dir
->got
.refcount
= 0;
6546 dir
->got
.refcount
+= ind
->got
.refcount
;
6547 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6550 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6552 if (dir
->plt
.refcount
< 0)
6553 dir
->plt
.refcount
= 0;
6554 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6555 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6558 if (ind
->dynindx
!= -1)
6560 if (dir
->dynindx
!= -1)
6561 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6562 dir
->dynindx
= ind
->dynindx
;
6563 dir
->dynstr_index
= ind
->dynstr_index
;
6565 ind
->dynstr_index
= 0;
6570 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6571 struct elf_link_hash_entry
*h
,
6572 bfd_boolean force_local
)
6574 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6578 h
->forced_local
= 1;
6579 if (h
->dynindx
!= -1)
6582 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6588 /* Initialize an ELF linker hash table. */
6591 _bfd_elf_link_hash_table_init
6592 (struct elf_link_hash_table
*table
,
6594 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6595 struct bfd_hash_table
*,
6597 unsigned int entsize
)
6600 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6602 memset (table
, 0, sizeof * table
);
6603 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6604 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6605 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6606 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6607 /* The first dynamic symbol is a dummy. */
6608 table
->dynsymcount
= 1;
6610 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6611 table
->root
.type
= bfd_link_elf_hash_table
;
6616 /* Create an ELF linker hash table. */
6618 struct bfd_link_hash_table
*
6619 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6621 struct elf_link_hash_table
*ret
;
6622 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6624 ret
= bfd_malloc (amt
);
6628 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6629 sizeof (struct elf_link_hash_entry
)))
6638 /* This is a hook for the ELF emulation code in the generic linker to
6639 tell the backend linker what file name to use for the DT_NEEDED
6640 entry for a dynamic object. */
6643 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6645 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6646 && bfd_get_format (abfd
) == bfd_object
)
6647 elf_dt_name (abfd
) = name
;
6651 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6654 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6655 && bfd_get_format (abfd
) == bfd_object
)
6656 lib_class
= elf_dyn_lib_class (abfd
);
6663 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6665 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6666 && bfd_get_format (abfd
) == bfd_object
)
6667 elf_dyn_lib_class (abfd
) = lib_class
;
6670 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6671 the linker ELF emulation code. */
6673 struct bfd_link_needed_list
*
6674 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6675 struct bfd_link_info
*info
)
6677 if (! is_elf_hash_table (info
->hash
))
6679 return elf_hash_table (info
)->needed
;
6682 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6683 hook for the linker ELF emulation code. */
6685 struct bfd_link_needed_list
*
6686 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6687 struct bfd_link_info
*info
)
6689 if (! is_elf_hash_table (info
->hash
))
6691 return elf_hash_table (info
)->runpath
;
6694 /* Get the name actually used for a dynamic object for a link. This
6695 is the SONAME entry if there is one. Otherwise, it is the string
6696 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6699 bfd_elf_get_dt_soname (bfd
*abfd
)
6701 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6702 && bfd_get_format (abfd
) == bfd_object
)
6703 return elf_dt_name (abfd
);
6707 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6708 the ELF linker emulation code. */
6711 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6712 struct bfd_link_needed_list
**pneeded
)
6715 bfd_byte
*dynbuf
= NULL
;
6717 unsigned long shlink
;
6718 bfd_byte
*extdyn
, *extdynend
;
6720 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
6724 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
6725 || bfd_get_format (abfd
) != bfd_object
)
6728 s
= bfd_get_section_by_name (abfd
, ".dynamic");
6729 if (s
== NULL
|| s
->size
== 0)
6732 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
6735 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
6739 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
6741 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
6742 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
6745 extdynend
= extdyn
+ s
->size
;
6746 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
6748 Elf_Internal_Dyn dyn
;
6750 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
6752 if (dyn
.d_tag
== DT_NULL
)
6755 if (dyn
.d_tag
== DT_NEEDED
)
6758 struct bfd_link_needed_list
*l
;
6759 unsigned int tagv
= dyn
.d_un
.d_val
;
6762 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
6767 l
= bfd_alloc (abfd
, amt
);
6788 struct elf_symbuf_symbol
6790 unsigned long st_name
; /* Symbol name, index in string tbl */
6791 unsigned char st_info
; /* Type and binding attributes */
6792 unsigned char st_other
; /* Visibilty, and target specific */
6795 struct elf_symbuf_head
6797 struct elf_symbuf_symbol
*ssym
;
6798 bfd_size_type count
;
6799 unsigned int st_shndx
;
6806 Elf_Internal_Sym
*isym
;
6807 struct elf_symbuf_symbol
*ssym
;
6812 /* Sort references to symbols by ascending section number. */
6815 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
6817 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
6818 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
6820 return s1
->st_shndx
- s2
->st_shndx
;
6824 elf_sym_name_compare (const void *arg1
, const void *arg2
)
6826 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
6827 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
6828 return strcmp (s1
->name
, s2
->name
);
6831 static struct elf_symbuf_head
*
6832 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
6834 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
6835 struct elf_symbuf_symbol
*ssym
;
6836 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
6837 bfd_size_type i
, shndx_count
;
6839 indbuf
= bfd_malloc2 (symcount
, sizeof (*indbuf
));
6843 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
6844 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
6845 *ind
++ = &isymbuf
[i
];
6848 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
6849 elf_sort_elf_symbol
);
6852 if (indbufend
> indbuf
)
6853 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
6854 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
6857 ssymbuf
= bfd_malloc ((shndx_count
+ 1) * sizeof (*ssymbuf
)
6858 + (indbufend
- indbuf
) * sizeof (*ssymbuf
));
6859 if (ssymbuf
== NULL
)
6865 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
);
6866 ssymbuf
->ssym
= NULL
;
6867 ssymbuf
->count
= shndx_count
;
6868 ssymbuf
->st_shndx
= 0;
6869 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
6871 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
6874 ssymhead
->ssym
= ssym
;
6875 ssymhead
->count
= 0;
6876 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
6878 ssym
->st_name
= (*ind
)->st_name
;
6879 ssym
->st_info
= (*ind
)->st_info
;
6880 ssym
->st_other
= (*ind
)->st_other
;
6883 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
);
6889 /* Check if 2 sections define the same set of local and global
6893 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
6894 struct bfd_link_info
*info
)
6897 const struct elf_backend_data
*bed1
, *bed2
;
6898 Elf_Internal_Shdr
*hdr1
, *hdr2
;
6899 bfd_size_type symcount1
, symcount2
;
6900 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
6901 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
6902 Elf_Internal_Sym
*isym
, *isymend
;
6903 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
6904 bfd_size_type count1
, count2
, i
;
6911 /* If both are .gnu.linkonce sections, they have to have the same
6913 if (CONST_STRNEQ (sec1
->name
, ".gnu.linkonce")
6914 && CONST_STRNEQ (sec2
->name
, ".gnu.linkonce"))
6915 return strcmp (sec1
->name
+ sizeof ".gnu.linkonce",
6916 sec2
->name
+ sizeof ".gnu.linkonce") == 0;
6918 /* Both sections have to be in ELF. */
6919 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
6920 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
6923 if (elf_section_type (sec1
) != elf_section_type (sec2
))
6926 if ((elf_section_flags (sec1
) & SHF_GROUP
) != 0
6927 && (elf_section_flags (sec2
) & SHF_GROUP
) != 0)
6929 /* If both are members of section groups, they have to have the
6931 if (strcmp (elf_group_name (sec1
), elf_group_name (sec2
)) != 0)
6935 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
6936 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
6937 if (shndx1
== -1 || shndx2
== -1)
6940 bed1
= get_elf_backend_data (bfd1
);
6941 bed2
= get_elf_backend_data (bfd2
);
6942 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
6943 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
6944 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
6945 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
6947 if (symcount1
== 0 || symcount2
== 0)
6953 ssymbuf1
= elf_tdata (bfd1
)->symbuf
;
6954 ssymbuf2
= elf_tdata (bfd2
)->symbuf
;
6956 if (ssymbuf1
== NULL
)
6958 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
6960 if (isymbuf1
== NULL
)
6963 if (!info
->reduce_memory_overheads
)
6964 elf_tdata (bfd1
)->symbuf
= ssymbuf1
6965 = elf_create_symbuf (symcount1
, isymbuf1
);
6968 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
6970 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
6972 if (isymbuf2
== NULL
)
6975 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
6976 elf_tdata (bfd2
)->symbuf
= ssymbuf2
6977 = elf_create_symbuf (symcount2
, isymbuf2
);
6980 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
6982 /* Optimized faster version. */
6983 bfd_size_type lo
, hi
, mid
;
6984 struct elf_symbol
*symp
;
6985 struct elf_symbuf_symbol
*ssym
, *ssymend
;
6988 hi
= ssymbuf1
->count
;
6993 mid
= (lo
+ hi
) / 2;
6994 if ((unsigned int) shndx1
< ssymbuf1
[mid
].st_shndx
)
6996 else if ((unsigned int) shndx1
> ssymbuf1
[mid
].st_shndx
)
7000 count1
= ssymbuf1
[mid
].count
;
7007 hi
= ssymbuf2
->count
;
7012 mid
= (lo
+ hi
) / 2;
7013 if ((unsigned int) shndx2
< ssymbuf2
[mid
].st_shndx
)
7015 else if ((unsigned int) shndx2
> ssymbuf2
[mid
].st_shndx
)
7019 count2
= ssymbuf2
[mid
].count
;
7025 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7028 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
7029 symtable2
= bfd_malloc (count2
* sizeof (struct elf_symbol
));
7030 if (symtable1
== NULL
|| symtable2
== NULL
)
7034 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7035 ssym
< ssymend
; ssym
++, symp
++)
7037 symp
->u
.ssym
= ssym
;
7038 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7044 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7045 ssym
< ssymend
; ssym
++, symp
++)
7047 symp
->u
.ssym
= ssym
;
7048 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7053 /* Sort symbol by name. */
7054 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7055 elf_sym_name_compare
);
7056 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7057 elf_sym_name_compare
);
7059 for (i
= 0; i
< count1
; i
++)
7060 /* Two symbols must have the same binding, type and name. */
7061 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7062 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7063 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7070 symtable1
= bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7071 symtable2
= bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7072 if (symtable1
== NULL
|| symtable2
== NULL
)
7075 /* Count definitions in the section. */
7077 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7078 if (isym
->st_shndx
== (unsigned int) shndx1
)
7079 symtable1
[count1
++].u
.isym
= isym
;
7082 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7083 if (isym
->st_shndx
== (unsigned int) shndx2
)
7084 symtable2
[count2
++].u
.isym
= isym
;
7086 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7089 for (i
= 0; i
< count1
; i
++)
7091 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7092 symtable1
[i
].u
.isym
->st_name
);
7094 for (i
= 0; i
< count2
; i
++)
7096 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7097 symtable2
[i
].u
.isym
->st_name
);
7099 /* Sort symbol by name. */
7100 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7101 elf_sym_name_compare
);
7102 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7103 elf_sym_name_compare
);
7105 for (i
= 0; i
< count1
; i
++)
7106 /* Two symbols must have the same binding, type and name. */
7107 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7108 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7109 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7127 /* Return TRUE if 2 section types are compatible. */
7130 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7131 bfd
*bbfd
, const asection
*bsec
)
7135 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7136 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7139 return elf_section_type (asec
) == elf_section_type (bsec
);
7142 /* Final phase of ELF linker. */
7144 /* A structure we use to avoid passing large numbers of arguments. */
7146 struct elf_final_link_info
7148 /* General link information. */
7149 struct bfd_link_info
*info
;
7152 /* Symbol string table. */
7153 struct bfd_strtab_hash
*symstrtab
;
7154 /* .dynsym section. */
7155 asection
*dynsym_sec
;
7156 /* .hash section. */
7158 /* symbol version section (.gnu.version). */
7159 asection
*symver_sec
;
7160 /* Buffer large enough to hold contents of any section. */
7162 /* Buffer large enough to hold external relocs of any section. */
7163 void *external_relocs
;
7164 /* Buffer large enough to hold internal relocs of any section. */
7165 Elf_Internal_Rela
*internal_relocs
;
7166 /* Buffer large enough to hold external local symbols of any input
7168 bfd_byte
*external_syms
;
7169 /* And a buffer for symbol section indices. */
7170 Elf_External_Sym_Shndx
*locsym_shndx
;
7171 /* Buffer large enough to hold internal local symbols of any input
7173 Elf_Internal_Sym
*internal_syms
;
7174 /* Array large enough to hold a symbol index for each local symbol
7175 of any input BFD. */
7177 /* Array large enough to hold a section pointer for each local
7178 symbol of any input BFD. */
7179 asection
**sections
;
7180 /* Buffer to hold swapped out symbols. */
7182 /* And one for symbol section indices. */
7183 Elf_External_Sym_Shndx
*symshndxbuf
;
7184 /* Number of swapped out symbols in buffer. */
7185 size_t symbuf_count
;
7186 /* Number of symbols which fit in symbuf. */
7188 /* And same for symshndxbuf. */
7189 size_t shndxbuf_size
;
7192 /* This struct is used to pass information to elf_link_output_extsym. */
7194 struct elf_outext_info
7197 bfd_boolean localsyms
;
7198 struct elf_final_link_info
*finfo
;
7202 /* Support for evaluating a complex relocation.
7204 Complex relocations are generalized, self-describing relocations. The
7205 implementation of them consists of two parts: complex symbols, and the
7206 relocations themselves.
7208 The relocations are use a reserved elf-wide relocation type code (R_RELC
7209 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7210 information (start bit, end bit, word width, etc) into the addend. This
7211 information is extracted from CGEN-generated operand tables within gas.
7213 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7214 internal) representing prefix-notation expressions, including but not
7215 limited to those sorts of expressions normally encoded as addends in the
7216 addend field. The symbol mangling format is:
7219 | <unary-operator> ':' <node>
7220 | <binary-operator> ':' <node> ':' <node>
7223 <literal> := 's' <digits=N> ':' <N character symbol name>
7224 | 'S' <digits=N> ':' <N character section name>
7228 <binary-operator> := as in C
7229 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7232 set_symbol_value (bfd
* bfd_with_globals
,
7233 Elf_Internal_Sym
* isymbuf
,
7238 struct elf_link_hash_entry
**sym_hashes
;
7239 struct elf_link_hash_entry
*h
;
7240 size_t extsymoff
= locsymcount
;
7242 if (symidx
< locsymcount
)
7244 Elf_Internal_Sym
*sym
;
7246 sym
= isymbuf
+ symidx
;
7247 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7249 /* It is a local symbol: move it to the
7250 "absolute" section and give it a value. */
7251 sym
->st_shndx
= SHN_ABS
;
7252 sym
->st_value
= val
;
7255 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7259 /* It is a global symbol: set its link type
7260 to "defined" and give it a value. */
7262 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7263 h
= sym_hashes
[symidx
- extsymoff
];
7264 while (h
->root
.type
== bfd_link_hash_indirect
7265 || h
->root
.type
== bfd_link_hash_warning
)
7266 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7267 h
->root
.type
= bfd_link_hash_defined
;
7268 h
->root
.u
.def
.value
= val
;
7269 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7273 resolve_symbol (const char * name
,
7275 struct elf_final_link_info
* finfo
,
7277 Elf_Internal_Sym
* isymbuf
,
7280 Elf_Internal_Sym
* sym
;
7281 struct bfd_link_hash_entry
* global_entry
;
7282 const char * candidate
= NULL
;
7283 Elf_Internal_Shdr
* symtab_hdr
;
7284 asection
* sec
= NULL
;
7287 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7289 for (i
= 0; i
< locsymcount
; ++ i
)
7292 sec
= finfo
->sections
[i
];
7294 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7297 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7298 symtab_hdr
->sh_link
,
7301 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
7302 name
, candidate
, (unsigned int)sym
->st_value
);
7304 if (candidate
&& strcmp (candidate
, name
) == 0)
7306 * result
= sym
->st_value
;
7308 if (sym
->st_shndx
> SHN_UNDEF
&&
7309 sym
->st_shndx
< SHN_LORESERVE
)
7312 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
7313 sec
->output_section
->name
,
7314 (unsigned int)sec
->output_section
->vma
,
7315 (unsigned int)sec
->output_offset
);
7317 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7320 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
7326 /* Hmm, haven't found it yet. perhaps it is a global. */
7327 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
7331 if (global_entry
->type
== bfd_link_hash_defined
7332 || global_entry
->type
== bfd_link_hash_defweak
)
7334 * result
= global_entry
->u
.def
.value
7335 + global_entry
->u
.def
.section
->output_section
->vma
7336 + global_entry
->u
.def
.section
->output_offset
;
7338 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
7339 global_entry
->root
.string
, (unsigned int)*result
);
7344 if (global_entry
->type
== bfd_link_hash_common
)
7346 *result
= global_entry
->u
.def
.value
+
7347 bfd_com_section_ptr
->output_section
->vma
+
7348 bfd_com_section_ptr
->output_offset
;
7350 printf ("Found COMMON symbol '%s' with value %8.8x\n",
7351 global_entry
->root
.string
, (unsigned int)*result
);
7360 resolve_section (const char * name
,
7361 asection
* sections
,
7367 for (curr
= sections
; curr
; curr
= curr
->next
)
7368 if (strcmp (curr
->name
, name
) == 0)
7370 *result
= curr
->vma
;
7374 /* Hmm. still haven't found it. try pseudo-section names. */
7375 for (curr
= sections
; curr
; curr
= curr
->next
)
7377 len
= strlen (curr
->name
);
7378 if (len
> strlen (name
))
7381 if (strncmp (curr
->name
, name
, len
) == 0)
7383 if (strncmp (".end", name
+ len
, 4) == 0)
7385 *result
= curr
->vma
+ curr
->size
;
7389 /* Insert more pseudo-section names here, if you like. */
7397 undefined_reference (const char * reftype
,
7400 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
7404 eval_symbol (bfd_vma
* result
,
7408 struct elf_final_link_info
* finfo
,
7410 bfd_vma section_offset
,
7411 Elf_Internal_Sym
* isymbuf
,
7419 const int bufsz
= 4096;
7420 char symbuf
[bufsz
];
7421 const char * symend
;
7422 bfd_boolean symbol_is_section
= FALSE
;
7427 if (len
< 1 || len
> bufsz
)
7429 bfd_set_error (bfd_error_invalid_operation
);
7436 * result
= addr
+ section_offset
;
7437 * advanced
= sym
+ 1;
7442 * result
= strtoul (sym
, advanced
, 16);
7446 symbol_is_section
= TRUE
;
7449 symlen
= strtol (sym
, &sym
, 10);
7450 ++ sym
; /* Skip the trailing ':'. */
7452 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
7454 bfd_set_error (bfd_error_invalid_operation
);
7458 memcpy (symbuf
, sym
, symlen
);
7459 symbuf
[symlen
] = '\0';
7460 * advanced
= sym
+ symlen
;
7462 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7463 the symbol as a section, or vice-versa. so we're pretty liberal in our
7464 interpretation here; section means "try section first", not "must be a
7465 section", and likewise with symbol. */
7467 if (symbol_is_section
)
7469 if (!resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
)
7470 && !resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7471 isymbuf
, locsymcount
))
7473 undefined_reference ("section", symbuf
);
7479 if (!resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7480 isymbuf
, locsymcount
)
7481 && !resolve_section (symbuf
, finfo
->output_bfd
->sections
,
7484 undefined_reference ("symbol", symbuf
);
7491 /* All that remains are operators. */
7493 #define UNARY_OP(op) \
7494 if (strncmp (sym, #op, strlen (#op)) == 0) \
7496 sym += strlen (#op); \
7499 if (!eval_symbol (&a, sym, &sym, input_bfd, finfo, addr, \
7500 section_offset, isymbuf, locsymcount, \
7504 * result = op ((signed)a); \
7511 #define BINARY_OP(op) \
7512 if (strncmp (sym, #op, strlen (#op)) == 0) \
7514 sym += strlen (#op); \
7517 if (!eval_symbol (&a, sym, &sym, input_bfd, finfo, addr, \
7518 section_offset, isymbuf, locsymcount, \
7522 if (!eval_symbol (&b, sym, &sym, input_bfd, finfo, addr, \
7523 section_offset, isymbuf, locsymcount, \
7527 * result = ((signed) a) op ((signed) b); \
7529 * result = a op b; \
7558 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7559 bfd_set_error (bfd_error_invalid_operation
);
7564 /* Entry point to evaluator, called from elf_link_input_bfd. */
7567 evaluate_complex_relocation_symbols (bfd
*input_bfd
,
7568 struct elf_final_link_info
*finfo
,
7569 Elf_Internal_Sym
*isymbuf
,
7572 const struct elf_backend_data
* bed
;
7573 Elf_Internal_Shdr
* symtab_hdr
;
7574 struct elf_link_hash_entry
** sym_hashes
;
7575 asection
* reloc_sec
;
7576 bfd_boolean result
= TRUE
;
7578 /* For each section, we're going to check and see if it has any
7579 complex relocations, and we're going to evaluate any of them
7582 if (finfo
->info
->relocatable
)
7585 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7586 sym_hashes
= elf_sym_hashes (input_bfd
);
7587 bed
= get_elf_backend_data (input_bfd
);
7589 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
7591 Elf_Internal_Rela
* internal_relocs
;
7594 /* This section was omitted from the link. */
7595 if (! reloc_sec
->linker_mark
)
7598 /* Only process sections containing relocs. */
7599 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
7602 if (reloc_sec
->reloc_count
== 0)
7605 /* Read in the relocs for this section. */
7607 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
7608 (Elf_Internal_Rela
*) NULL
,
7610 if (internal_relocs
== NULL
)
7613 for (i
= reloc_sec
->reloc_count
; i
--;)
7615 Elf_Internal_Rela
* rel
;
7618 Elf_Internal_Sym
* sym
;
7620 bfd_vma section_offset
;
7624 rel
= internal_relocs
+ i
;
7625 section_offset
= reloc_sec
->output_section
->vma
7626 + reloc_sec
->output_offset
;
7627 addr
= rel
->r_offset
;
7629 index
= ELF32_R_SYM (rel
->r_info
);
7630 if (bed
->s
->arch_size
== 64)
7633 if (index
== STN_UNDEF
)
7636 if (index
< locsymcount
)
7638 /* The symbol is local. */
7639 sym
= isymbuf
+ index
;
7641 /* We're only processing STT_RELC or STT_SRELC type symbols. */
7642 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
7643 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
7646 sym_name
= bfd_elf_string_from_elf_section
7647 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
7649 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
7653 /* The symbol is global. */
7654 struct elf_link_hash_entry
* h
;
7656 if (elf_bad_symtab (input_bfd
))
7659 h
= sym_hashes
[index
- locsymcount
];
7660 while ( h
->root
.type
== bfd_link_hash_indirect
7661 || h
->root
.type
== bfd_link_hash_warning
)
7662 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7664 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
7667 signed_p
= (h
->type
== STT_SRELC
);
7668 sym_name
= (char *) h
->root
.root
.string
;
7671 printf ("Encountered a complex symbol!");
7672 printf (" (input_bfd %s, section %s, reloc %ld\n",
7673 input_bfd
->filename
, reloc_sec
->name
, i
);
7674 printf (" symbol: idx %8.8lx, name %s\n",
7676 printf (" reloc : info %8.8lx, addr %8.8lx\n",
7678 printf (" Evaluating '%s' ...\n ", sym_name
);
7680 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
7681 finfo
, addr
, section_offset
, isymbuf
, locsymcount
,
7683 /* Symbol evaluated OK. Update to absolute value. */
7684 set_symbol_value (input_bfd
, isymbuf
, locsymcount
, index
, result
);
7690 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
7691 free (internal_relocs
);
7694 /* If nothing went wrong, then we adjusted
7695 everything we wanted to adjust. */
7700 put_value (bfd_vma size
,
7701 unsigned long chunksz
,
7704 bfd_byte
* location
)
7706 location
+= (size
- chunksz
);
7708 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7716 bfd_put_8 (input_bfd
, x
, location
);
7719 bfd_put_16 (input_bfd
, x
, location
);
7722 bfd_put_32 (input_bfd
, x
, location
);
7726 bfd_put_64 (input_bfd
, x
, location
);
7736 get_value (bfd_vma size
,
7737 unsigned long chunksz
,
7739 bfd_byte
* location
)
7743 for (; size
; size
-= chunksz
, location
+= chunksz
)
7751 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7754 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7757 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7761 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7772 decode_complex_addend
7773 (unsigned long * start
, /* in bits */
7774 unsigned long * oplen
, /* in bits */
7775 unsigned long * len
, /* in bits */
7776 unsigned long * wordsz
, /* in bytes */
7777 unsigned long * chunksz
, /* in bytes */
7778 unsigned long * lsb0_p
,
7779 unsigned long * signed_p
,
7780 unsigned long * trunc_p
,
7781 unsigned long encoded
)
7783 * start
= encoded
& 0x3F;
7784 * len
= (encoded
>> 6) & 0x3F;
7785 * oplen
= (encoded
>> 12) & 0x3F;
7786 * wordsz
= (encoded
>> 18) & 0xF;
7787 * chunksz
= (encoded
>> 22) & 0xF;
7788 * lsb0_p
= (encoded
>> 27) & 1;
7789 * signed_p
= (encoded
>> 28) & 1;
7790 * trunc_p
= (encoded
>> 29) & 1;
7794 bfd_elf_perform_complex_relocation
7795 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
7796 struct bfd_link_info
* info
,
7798 asection
* input_section
,
7799 bfd_byte
* contents
,
7800 Elf_Internal_Rela
* rel
,
7801 Elf_Internal_Sym
* local_syms
,
7802 asection
** local_sections
)
7804 const struct elf_backend_data
* bed
;
7805 Elf_Internal_Shdr
* symtab_hdr
;
7807 bfd_vma relocation
= 0, shift
, x
;
7810 unsigned long start
, oplen
, len
, wordsz
,
7811 chunksz
, lsb0_p
, signed_p
, trunc_p
;
7813 /* Perform this reloc, since it is complex.
7814 (this is not to say that it necessarily refers to a complex
7815 symbol; merely that it is a self-describing CGEN based reloc.
7816 i.e. the addend has the complete reloc information (bit start, end,
7817 word size, etc) encoded within it.). */
7818 r_symndx
= ELF32_R_SYM (rel
->r_info
);
7819 bed
= get_elf_backend_data (input_bfd
);
7820 if (bed
->s
->arch_size
== 64)
7824 printf ("Performing complex relocation %ld...\n", r_symndx
);
7827 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7828 if (r_symndx
< symtab_hdr
->sh_info
)
7830 /* The symbol is local. */
7831 Elf_Internal_Sym
* sym
;
7833 sym
= local_syms
+ r_symndx
;
7834 sec
= local_sections
[r_symndx
];
7835 relocation
= sym
->st_value
;
7836 if (sym
->st_shndx
> SHN_UNDEF
&&
7837 sym
->st_shndx
< SHN_LORESERVE
)
7838 relocation
+= (sec
->output_offset
+
7839 sec
->output_section
->vma
);
7843 /* The symbol is global. */
7844 struct elf_link_hash_entry
**sym_hashes
;
7845 struct elf_link_hash_entry
* h
;
7847 sym_hashes
= elf_sym_hashes (input_bfd
);
7848 h
= sym_hashes
[r_symndx
];
7850 while (h
->root
.type
== bfd_link_hash_indirect
7851 || h
->root
.type
== bfd_link_hash_warning
)
7852 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7854 if (h
->root
.type
== bfd_link_hash_defined
7855 || h
->root
.type
== bfd_link_hash_defweak
)
7857 sec
= h
->root
.u
.def
.section
;
7858 relocation
= h
->root
.u
.def
.value
;
7860 if (! bfd_is_abs_section (sec
))
7861 relocation
+= (sec
->output_section
->vma
7862 + sec
->output_offset
);
7864 if (h
->root
.type
== bfd_link_hash_undefined
7865 && !((*info
->callbacks
->undefined_symbol
)
7866 (info
, h
->root
.root
.string
, input_bfd
,
7867 input_section
, rel
->r_offset
,
7868 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7869 || ELF_ST_VISIBILITY (h
->other
))))
7873 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7874 & chunksz
, & lsb0_p
, & signed_p
,
7875 & trunc_p
, rel
->r_addend
);
7877 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7880 shift
= (start
+ 1) - len
;
7882 shift
= (8 * wordsz
) - (start
+ len
);
7884 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7887 printf ("Doing complex reloc: "
7888 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7889 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7890 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7891 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7892 oplen
, x
, mask
, relocation
);
7897 /* Now do an overflow check. */
7898 if (bfd_check_overflow ((signed_p
?
7899 complain_overflow_signed
:
7900 complain_overflow_unsigned
),
7901 len
, 0, (8 * wordsz
),
7902 relocation
) == bfd_reloc_overflow
)
7903 (*_bfd_error_handler
)
7904 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7906 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7907 relocation
, (signed_p
? "(signed) " : ""), mask
);
7911 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7914 printf (" relocation: %8.8lx\n"
7915 " shifted mask: %8.8lx\n"
7916 " shifted/masked reloc: %8.8lx\n"
7917 " result: %8.8lx\n",
7918 relocation
, (mask
<< shift
),
7919 ((relocation
& mask
) << shift
), x
);
7921 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7924 /* When performing a relocatable link, the input relocations are
7925 preserved. But, if they reference global symbols, the indices
7926 referenced must be updated. Update all the relocations in
7927 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7930 elf_link_adjust_relocs (bfd
*abfd
,
7931 Elf_Internal_Shdr
*rel_hdr
,
7933 struct elf_link_hash_entry
**rel_hash
)
7936 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7938 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7939 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7940 bfd_vma r_type_mask
;
7943 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7945 swap_in
= bed
->s
->swap_reloc_in
;
7946 swap_out
= bed
->s
->swap_reloc_out
;
7948 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7950 swap_in
= bed
->s
->swap_reloca_in
;
7951 swap_out
= bed
->s
->swap_reloca_out
;
7956 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7959 if (bed
->s
->arch_size
== 32)
7966 r_type_mask
= 0xffffffff;
7970 erela
= rel_hdr
->contents
;
7971 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7973 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7976 if (*rel_hash
== NULL
)
7979 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7981 (*swap_in
) (abfd
, erela
, irela
);
7982 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7983 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7984 | (irela
[j
].r_info
& r_type_mask
));
7985 (*swap_out
) (abfd
, irela
, erela
);
7989 struct elf_link_sort_rela
7995 enum elf_reloc_type_class type
;
7996 /* We use this as an array of size int_rels_per_ext_rel. */
7997 Elf_Internal_Rela rela
[1];
8001 elf_link_sort_cmp1 (const void *A
, const void *B
)
8003 const struct elf_link_sort_rela
*a
= A
;
8004 const struct elf_link_sort_rela
*b
= B
;
8005 int relativea
, relativeb
;
8007 relativea
= a
->type
== reloc_class_relative
;
8008 relativeb
= b
->type
== reloc_class_relative
;
8010 if (relativea
< relativeb
)
8012 if (relativea
> relativeb
)
8014 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8016 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8018 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8020 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8026 elf_link_sort_cmp2 (const void *A
, const void *B
)
8028 const struct elf_link_sort_rela
*a
= A
;
8029 const struct elf_link_sort_rela
*b
= B
;
8032 if (a
->u
.offset
< b
->u
.offset
)
8034 if (a
->u
.offset
> b
->u
.offset
)
8036 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
8037 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
8042 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8044 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8050 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8052 asection
*dynamic_relocs
;
8055 bfd_size_type count
, size
;
8056 size_t i
, ret
, sort_elt
, ext_size
;
8057 bfd_byte
*sort
, *s_non_relative
, *p
;
8058 struct elf_link_sort_rela
*sq
;
8059 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8060 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8061 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8062 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8063 struct bfd_link_order
*lo
;
8065 bfd_boolean use_rela
;
8067 /* Find a dynamic reloc section. */
8068 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8069 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8070 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8071 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8073 bfd_boolean use_rela_initialised
= FALSE
;
8075 /* This is just here to stop gcc from complaining.
8076 It's initialization checking code is not perfect. */
8079 /* Both sections are present. Examine the sizes
8080 of the indirect sections to help us choose. */
8081 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8082 if (lo
->type
== bfd_indirect_link_order
)
8084 asection
*o
= lo
->u
.indirect
.section
;
8086 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8088 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8089 /* Section size is divisible by both rel and rela sizes.
8090 It is of no help to us. */
8094 /* Section size is only divisible by rela. */
8095 if (use_rela_initialised
&& (use_rela
== FALSE
))
8098 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8099 bfd_set_error (bfd_error_invalid_operation
);
8105 use_rela_initialised
= TRUE
;
8109 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8111 /* Section size is only divisible by rel. */
8112 if (use_rela_initialised
&& (use_rela
== TRUE
))
8115 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8116 bfd_set_error (bfd_error_invalid_operation
);
8122 use_rela_initialised
= TRUE
;
8127 /* The section size is not divisible by either - something is wrong. */
8129 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8130 bfd_set_error (bfd_error_invalid_operation
);
8135 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8136 if (lo
->type
== bfd_indirect_link_order
)
8138 asection
*o
= lo
->u
.indirect
.section
;
8140 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8142 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8143 /* Section size is divisible by both rel and rela sizes.
8144 It is of no help to us. */
8148 /* Section size is only divisible by rela. */
8149 if (use_rela_initialised
&& (use_rela
== FALSE
))
8152 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8153 bfd_set_error (bfd_error_invalid_operation
);
8159 use_rela_initialised
= TRUE
;
8163 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8165 /* Section size is only divisible by rel. */
8166 if (use_rela_initialised
&& (use_rela
== TRUE
))
8169 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8170 bfd_set_error (bfd_error_invalid_operation
);
8176 use_rela_initialised
= TRUE
;
8181 /* The section size is not divisible by either - something is wrong. */
8183 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8184 bfd_set_error (bfd_error_invalid_operation
);
8189 if (! use_rela_initialised
)
8193 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8195 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8202 dynamic_relocs
= rela_dyn
;
8203 ext_size
= bed
->s
->sizeof_rela
;
8204 swap_in
= bed
->s
->swap_reloca_in
;
8205 swap_out
= bed
->s
->swap_reloca_out
;
8209 dynamic_relocs
= rel_dyn
;
8210 ext_size
= bed
->s
->sizeof_rel
;
8211 swap_in
= bed
->s
->swap_reloc_in
;
8212 swap_out
= bed
->s
->swap_reloc_out
;
8216 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8217 if (lo
->type
== bfd_indirect_link_order
)
8218 size
+= lo
->u
.indirect
.section
->size
;
8220 if (size
!= dynamic_relocs
->size
)
8223 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8224 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8226 count
= dynamic_relocs
->size
/ ext_size
;
8227 sort
= bfd_zmalloc (sort_elt
* count
);
8231 (*info
->callbacks
->warning
)
8232 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8236 if (bed
->s
->arch_size
== 32)
8237 r_sym_mask
= ~(bfd_vma
) 0xff;
8239 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8241 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8242 if (lo
->type
== bfd_indirect_link_order
)
8244 bfd_byte
*erel
, *erelend
;
8245 asection
*o
= lo
->u
.indirect
.section
;
8247 if (o
->contents
== NULL
&& o
->size
!= 0)
8249 /* This is a reloc section that is being handled as a normal
8250 section. See bfd_section_from_shdr. We can't combine
8251 relocs in this case. */
8256 erelend
= o
->contents
+ o
->size
;
8257 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8259 while (erel
< erelend
)
8261 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8263 (*swap_in
) (abfd
, erel
, s
->rela
);
8264 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8265 s
->u
.sym_mask
= r_sym_mask
;
8271 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8273 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8275 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8276 if (s
->type
!= reloc_class_relative
)
8282 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8283 for (; i
< count
; i
++, p
+= sort_elt
)
8285 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8286 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8288 sp
->u
.offset
= sq
->rela
->r_offset
;
8291 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8293 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8294 if (lo
->type
== bfd_indirect_link_order
)
8296 bfd_byte
*erel
, *erelend
;
8297 asection
*o
= lo
->u
.indirect
.section
;
8300 erelend
= o
->contents
+ o
->size
;
8301 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8302 while (erel
< erelend
)
8304 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8305 (*swap_out
) (abfd
, s
->rela
, erel
);
8312 *psec
= dynamic_relocs
;
8316 /* Flush the output symbols to the file. */
8319 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
8320 const struct elf_backend_data
*bed
)
8322 if (finfo
->symbuf_count
> 0)
8324 Elf_Internal_Shdr
*hdr
;
8328 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
8329 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8330 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8331 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
8332 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
8335 hdr
->sh_size
+= amt
;
8336 finfo
->symbuf_count
= 0;
8342 /* Add a symbol to the output symbol table. */
8345 elf_link_output_sym (struct elf_final_link_info
*finfo
,
8347 Elf_Internal_Sym
*elfsym
,
8348 asection
*input_sec
,
8349 struct elf_link_hash_entry
*h
)
8352 Elf_External_Sym_Shndx
*destshndx
;
8353 bfd_boolean (*output_symbol_hook
)
8354 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8355 struct elf_link_hash_entry
*);
8356 const struct elf_backend_data
*bed
;
8358 bed
= get_elf_backend_data (finfo
->output_bfd
);
8359 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8360 if (output_symbol_hook
!= NULL
)
8362 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
8366 if (name
== NULL
|| *name
== '\0')
8367 elfsym
->st_name
= 0;
8368 else if (input_sec
->flags
& SEC_EXCLUDE
)
8369 elfsym
->st_name
= 0;
8372 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
8374 if (elfsym
->st_name
== (unsigned long) -1)
8378 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
8380 if (! elf_link_flush_output_syms (finfo
, bed
))
8384 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8385 destshndx
= finfo
->symshndxbuf
;
8386 if (destshndx
!= NULL
)
8388 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
8392 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8393 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
8394 if (destshndx
== NULL
)
8396 memset ((char *) destshndx
+ amt
, 0, amt
);
8397 finfo
->shndxbuf_size
*= 2;
8399 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
8402 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
8403 finfo
->symbuf_count
+= 1;
8404 bfd_get_symcount (finfo
->output_bfd
) += 1;
8409 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8412 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8414 if (sym
->st_shndx
> SHN_HIRESERVE
)
8416 /* The gABI doesn't support dynamic symbols in output sections
8418 (*_bfd_error_handler
)
8419 (_("%B: Too many sections: %d (>= %d)"),
8420 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
8421 bfd_set_error (bfd_error_nonrepresentable_section
);
8427 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8428 allowing an unsatisfied unversioned symbol in the DSO to match a
8429 versioned symbol that would normally require an explicit version.
8430 We also handle the case that a DSO references a hidden symbol
8431 which may be satisfied by a versioned symbol in another DSO. */
8434 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8435 const struct elf_backend_data
*bed
,
8436 struct elf_link_hash_entry
*h
)
8439 struct elf_link_loaded_list
*loaded
;
8441 if (!is_elf_hash_table (info
->hash
))
8444 switch (h
->root
.type
)
8450 case bfd_link_hash_undefined
:
8451 case bfd_link_hash_undefweak
:
8452 abfd
= h
->root
.u
.undef
.abfd
;
8453 if ((abfd
->flags
& DYNAMIC
) == 0
8454 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8458 case bfd_link_hash_defined
:
8459 case bfd_link_hash_defweak
:
8460 abfd
= h
->root
.u
.def
.section
->owner
;
8463 case bfd_link_hash_common
:
8464 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8467 BFD_ASSERT (abfd
!= NULL
);
8469 for (loaded
= elf_hash_table (info
)->loaded
;
8471 loaded
= loaded
->next
)
8474 Elf_Internal_Shdr
*hdr
;
8475 bfd_size_type symcount
;
8476 bfd_size_type extsymcount
;
8477 bfd_size_type extsymoff
;
8478 Elf_Internal_Shdr
*versymhdr
;
8479 Elf_Internal_Sym
*isym
;
8480 Elf_Internal_Sym
*isymend
;
8481 Elf_Internal_Sym
*isymbuf
;
8482 Elf_External_Versym
*ever
;
8483 Elf_External_Versym
*extversym
;
8485 input
= loaded
->abfd
;
8487 /* We check each DSO for a possible hidden versioned definition. */
8489 || (input
->flags
& DYNAMIC
) == 0
8490 || elf_dynversym (input
) == 0)
8493 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8495 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8496 if (elf_bad_symtab (input
))
8498 extsymcount
= symcount
;
8503 extsymcount
= symcount
- hdr
->sh_info
;
8504 extsymoff
= hdr
->sh_info
;
8507 if (extsymcount
== 0)
8510 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8512 if (isymbuf
== NULL
)
8515 /* Read in any version definitions. */
8516 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8517 extversym
= bfd_malloc (versymhdr
->sh_size
);
8518 if (extversym
== NULL
)
8521 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8522 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8523 != versymhdr
->sh_size
))
8531 ever
= extversym
+ extsymoff
;
8532 isymend
= isymbuf
+ extsymcount
;
8533 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8536 Elf_Internal_Versym iver
;
8537 unsigned short version_index
;
8539 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8540 || isym
->st_shndx
== SHN_UNDEF
)
8543 name
= bfd_elf_string_from_elf_section (input
,
8546 if (strcmp (name
, h
->root
.root
.string
) != 0)
8549 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8551 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
8553 /* If we have a non-hidden versioned sym, then it should
8554 have provided a definition for the undefined sym. */
8558 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8559 if (version_index
== 1 || version_index
== 2)
8561 /* This is the base or first version. We can use it. */
8575 /* Add an external symbol to the symbol table. This is called from
8576 the hash table traversal routine. When generating a shared object,
8577 we go through the symbol table twice. The first time we output
8578 anything that might have been forced to local scope in a version
8579 script. The second time we output the symbols that are still
8583 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
8585 struct elf_outext_info
*eoinfo
= data
;
8586 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
8588 Elf_Internal_Sym sym
;
8589 asection
*input_sec
;
8590 const struct elf_backend_data
*bed
;
8592 if (h
->root
.type
== bfd_link_hash_warning
)
8594 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8595 if (h
->root
.type
== bfd_link_hash_new
)
8599 /* Decide whether to output this symbol in this pass. */
8600 if (eoinfo
->localsyms
)
8602 if (!h
->forced_local
)
8607 if (h
->forced_local
)
8611 bed
= get_elf_backend_data (finfo
->output_bfd
);
8613 if (h
->root
.type
== bfd_link_hash_undefined
)
8615 /* If we have an undefined symbol reference here then it must have
8616 come from a shared library that is being linked in. (Undefined
8617 references in regular files have already been handled). */
8618 bfd_boolean ignore_undef
= FALSE
;
8620 /* Some symbols may be special in that the fact that they're
8621 undefined can be safely ignored - let backend determine that. */
8622 if (bed
->elf_backend_ignore_undef_symbol
)
8623 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8625 /* If we are reporting errors for this situation then do so now. */
8626 if (ignore_undef
== FALSE
8629 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
8630 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8632 if (! (finfo
->info
->callbacks
->undefined_symbol
8633 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
8634 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
8636 eoinfo
->failed
= TRUE
;
8642 /* We should also warn if a forced local symbol is referenced from
8643 shared libraries. */
8644 if (! finfo
->info
->relocatable
8645 && (! finfo
->info
->shared
)
8650 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
8652 (*_bfd_error_handler
)
8653 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8655 h
->root
.u
.def
.section
== bfd_abs_section_ptr
8656 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
8657 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
8659 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
8660 ? "hidden" : "local",
8661 h
->root
.root
.string
);
8662 eoinfo
->failed
= TRUE
;
8666 /* We don't want to output symbols that have never been mentioned by
8667 a regular file, or that we have been told to strip. However, if
8668 h->indx is set to -2, the symbol is used by a reloc and we must
8672 else if ((h
->def_dynamic
8674 || h
->root
.type
== bfd_link_hash_new
)
8678 else if (finfo
->info
->strip
== strip_all
)
8680 else if (finfo
->info
->strip
== strip_some
8681 && bfd_hash_lookup (finfo
->info
->keep_hash
,
8682 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8684 else if (finfo
->info
->strip_discarded
8685 && (h
->root
.type
== bfd_link_hash_defined
8686 || h
->root
.type
== bfd_link_hash_defweak
)
8687 && elf_discarded_section (h
->root
.u
.def
.section
))
8692 /* If we're stripping it, and it's not a dynamic symbol, there's
8693 nothing else to do unless it is a forced local symbol. */
8696 && !h
->forced_local
)
8700 sym
.st_size
= h
->size
;
8701 sym
.st_other
= h
->other
;
8702 if (h
->forced_local
)
8703 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8704 else if (h
->root
.type
== bfd_link_hash_undefweak
8705 || h
->root
.type
== bfd_link_hash_defweak
)
8706 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8708 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8710 switch (h
->root
.type
)
8713 case bfd_link_hash_new
:
8714 case bfd_link_hash_warning
:
8718 case bfd_link_hash_undefined
:
8719 case bfd_link_hash_undefweak
:
8720 input_sec
= bfd_und_section_ptr
;
8721 sym
.st_shndx
= SHN_UNDEF
;
8724 case bfd_link_hash_defined
:
8725 case bfd_link_hash_defweak
:
8727 input_sec
= h
->root
.u
.def
.section
;
8728 if (input_sec
->output_section
!= NULL
)
8731 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
8732 input_sec
->output_section
);
8733 if (sym
.st_shndx
== SHN_BAD
)
8735 (*_bfd_error_handler
)
8736 (_("%B: could not find output section %A for input section %A"),
8737 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8738 eoinfo
->failed
= TRUE
;
8742 /* ELF symbols in relocatable files are section relative,
8743 but in nonrelocatable files they are virtual
8745 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8746 if (! finfo
->info
->relocatable
)
8748 sym
.st_value
+= input_sec
->output_section
->vma
;
8749 if (h
->type
== STT_TLS
)
8751 /* STT_TLS symbols are relative to PT_TLS segment
8753 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8754 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8760 BFD_ASSERT (input_sec
->owner
== NULL
8761 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8762 sym
.st_shndx
= SHN_UNDEF
;
8763 input_sec
= bfd_und_section_ptr
;
8768 case bfd_link_hash_common
:
8769 input_sec
= h
->root
.u
.c
.p
->section
;
8770 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8771 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8774 case bfd_link_hash_indirect
:
8775 /* These symbols are created by symbol versioning. They point
8776 to the decorated version of the name. For example, if the
8777 symbol foo@@GNU_1.2 is the default, which should be used when
8778 foo is used with no version, then we add an indirect symbol
8779 foo which points to foo@@GNU_1.2. We ignore these symbols,
8780 since the indirected symbol is already in the hash table. */
8784 /* Give the processor backend a chance to tweak the symbol value,
8785 and also to finish up anything that needs to be done for this
8786 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8787 forced local syms when non-shared is due to a historical quirk. */
8788 if ((h
->dynindx
!= -1
8790 && ((finfo
->info
->shared
8791 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8792 || h
->root
.type
!= bfd_link_hash_undefweak
))
8793 || !h
->forced_local
)
8794 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8796 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8797 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8799 eoinfo
->failed
= TRUE
;
8804 /* If we are marking the symbol as undefined, and there are no
8805 non-weak references to this symbol from a regular object, then
8806 mark the symbol as weak undefined; if there are non-weak
8807 references, mark the symbol as strong. We can't do this earlier,
8808 because it might not be marked as undefined until the
8809 finish_dynamic_symbol routine gets through with it. */
8810 if (sym
.st_shndx
== SHN_UNDEF
8812 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8813 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8817 if (h
->ref_regular_nonweak
)
8818 bindtype
= STB_GLOBAL
;
8820 bindtype
= STB_WEAK
;
8821 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8824 /* If a non-weak symbol with non-default visibility is not defined
8825 locally, it is a fatal error. */
8826 if (! finfo
->info
->relocatable
8827 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8828 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8829 && h
->root
.type
== bfd_link_hash_undefined
8832 (*_bfd_error_handler
)
8833 (_("%B: %s symbol `%s' isn't defined"),
8835 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8837 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8838 ? "internal" : "hidden",
8839 h
->root
.root
.string
);
8840 eoinfo
->failed
= TRUE
;
8844 /* If this symbol should be put in the .dynsym section, then put it
8845 there now. We already know the symbol index. We also fill in
8846 the entry in the .hash section. */
8847 if (h
->dynindx
!= -1
8848 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8852 sym
.st_name
= h
->dynstr_index
;
8853 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8854 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8856 eoinfo
->failed
= TRUE
;
8859 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8861 if (finfo
->hash_sec
!= NULL
)
8863 size_t hash_entry_size
;
8864 bfd_byte
*bucketpos
;
8869 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8870 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8873 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8874 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8875 + (bucket
+ 2) * hash_entry_size
);
8876 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8877 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8878 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8879 ((bfd_byte
*) finfo
->hash_sec
->contents
8880 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8883 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8885 Elf_Internal_Versym iversym
;
8886 Elf_External_Versym
*eversym
;
8888 if (!h
->def_regular
)
8890 if (h
->verinfo
.verdef
== NULL
)
8891 iversym
.vs_vers
= 0;
8893 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8897 if (h
->verinfo
.vertree
== NULL
)
8898 iversym
.vs_vers
= 1;
8900 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8901 if (finfo
->info
->create_default_symver
)
8906 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8908 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8909 eversym
+= h
->dynindx
;
8910 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8914 /* If we're stripping it, then it was just a dynamic symbol, and
8915 there's nothing else to do. */
8916 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8919 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8921 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8923 eoinfo
->failed
= TRUE
;
8930 /* Return TRUE if special handling is done for relocs in SEC against
8931 symbols defined in discarded sections. */
8934 elf_section_ignore_discarded_relocs (asection
*sec
)
8936 const struct elf_backend_data
*bed
;
8938 switch (sec
->sec_info_type
)
8940 case ELF_INFO_TYPE_STABS
:
8941 case ELF_INFO_TYPE_EH_FRAME
:
8947 bed
= get_elf_backend_data (sec
->owner
);
8948 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8949 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8955 /* Return a mask saying how ld should treat relocations in SEC against
8956 symbols defined in discarded sections. If this function returns
8957 COMPLAIN set, ld will issue a warning message. If this function
8958 returns PRETEND set, and the discarded section was link-once and the
8959 same size as the kept link-once section, ld will pretend that the
8960 symbol was actually defined in the kept section. Otherwise ld will
8961 zero the reloc (at least that is the intent, but some cooperation by
8962 the target dependent code is needed, particularly for REL targets). */
8965 _bfd_elf_default_action_discarded (asection
*sec
)
8967 if (sec
->flags
& SEC_DEBUGGING
)
8970 if (strcmp (".eh_frame", sec
->name
) == 0)
8973 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8976 return COMPLAIN
| PRETEND
;
8979 /* Find a match between a section and a member of a section group. */
8982 match_group_member (asection
*sec
, asection
*group
,
8983 struct bfd_link_info
*info
)
8985 asection
*first
= elf_next_in_group (group
);
8986 asection
*s
= first
;
8990 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8993 s
= elf_next_in_group (s
);
9001 /* Check if the kept section of a discarded section SEC can be used
9002 to replace it. Return the replacement if it is OK. Otherwise return
9006 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9010 kept
= sec
->kept_section
;
9013 if ((kept
->flags
& SEC_GROUP
) != 0)
9014 kept
= match_group_member (sec
, kept
, info
);
9015 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
9017 sec
->kept_section
= kept
;
9022 /* Link an input file into the linker output file. This function
9023 handles all the sections and relocations of the input file at once.
9024 This is so that we only have to read the local symbols once, and
9025 don't have to keep them in memory. */
9028 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
9030 int (*relocate_section
)
9031 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9032 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9034 Elf_Internal_Shdr
*symtab_hdr
;
9037 Elf_Internal_Sym
*isymbuf
;
9038 Elf_Internal_Sym
*isym
;
9039 Elf_Internal_Sym
*isymend
;
9041 asection
**ppsection
;
9043 const struct elf_backend_data
*bed
;
9044 struct elf_link_hash_entry
**sym_hashes
;
9046 output_bfd
= finfo
->output_bfd
;
9047 bed
= get_elf_backend_data (output_bfd
);
9048 relocate_section
= bed
->elf_backend_relocate_section
;
9050 /* If this is a dynamic object, we don't want to do anything here:
9051 we don't want the local symbols, and we don't want the section
9053 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9056 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9057 if (elf_bad_symtab (input_bfd
))
9059 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9064 locsymcount
= symtab_hdr
->sh_info
;
9065 extsymoff
= symtab_hdr
->sh_info
;
9068 /* Read the local symbols. */
9069 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9070 if (isymbuf
== NULL
&& locsymcount
!= 0)
9072 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9073 finfo
->internal_syms
,
9074 finfo
->external_syms
,
9075 finfo
->locsym_shndx
);
9076 if (isymbuf
== NULL
)
9080 /* Find local symbol sections and adjust values of symbols in
9081 SEC_MERGE sections. Write out those local symbols we know are
9082 going into the output file. */
9083 isymend
= isymbuf
+ locsymcount
;
9084 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
9086 isym
++, pindex
++, ppsection
++)
9090 Elf_Internal_Sym osym
;
9094 if (elf_bad_symtab (input_bfd
))
9096 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9103 if (isym
->st_shndx
== SHN_UNDEF
)
9104 isec
= bfd_und_section_ptr
;
9105 else if (isym
->st_shndx
< SHN_LORESERVE
9106 || isym
->st_shndx
> SHN_HIRESERVE
)
9108 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9110 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
9111 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9113 _bfd_merged_section_offset (output_bfd
, &isec
,
9114 elf_section_data (isec
)->sec_info
,
9117 else if (isym
->st_shndx
== SHN_ABS
)
9118 isec
= bfd_abs_section_ptr
;
9119 else if (isym
->st_shndx
== SHN_COMMON
)
9120 isec
= bfd_com_section_ptr
;
9123 /* Don't attempt to output symbols with st_shnx in the
9124 reserved range other than SHN_ABS and SHN_COMMON. */
9131 /* Don't output the first, undefined, symbol. */
9132 if (ppsection
== finfo
->sections
)
9135 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9137 /* We never output section symbols. Instead, we use the
9138 section symbol of the corresponding section in the output
9143 /* If we are stripping all symbols, we don't want to output this
9145 if (finfo
->info
->strip
== strip_all
)
9148 /* If we are discarding all local symbols, we don't want to
9149 output this one. If we are generating a relocatable output
9150 file, then some of the local symbols may be required by
9151 relocs; we output them below as we discover that they are
9153 if (finfo
->info
->discard
== discard_all
)
9156 /* If this symbol is defined in a section which we are
9157 discarding, we don't need to keep it. */
9158 if (isym
->st_shndx
!= SHN_UNDEF
9159 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9161 || bfd_section_removed_from_list (output_bfd
,
9162 isec
->output_section
)))
9165 /* Get the name of the symbol. */
9166 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9171 /* See if we are discarding symbols with this name. */
9172 if ((finfo
->info
->strip
== strip_some
9173 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9175 || (((finfo
->info
->discard
== discard_sec_merge
9176 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
9177 || finfo
->info
->discard
== discard_l
)
9178 && bfd_is_local_label_name (input_bfd
, name
)))
9181 /* If we get here, we are going to output this symbol. */
9185 /* Adjust the section index for the output file. */
9186 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9187 isec
->output_section
);
9188 if (osym
.st_shndx
== SHN_BAD
)
9191 *pindex
= bfd_get_symcount (output_bfd
);
9193 /* ELF symbols in relocatable files are section relative, but
9194 in executable files they are virtual addresses. Note that
9195 this code assumes that all ELF sections have an associated
9196 BFD section with a reasonable value for output_offset; below
9197 we assume that they also have a reasonable value for
9198 output_section. Any special sections must be set up to meet
9199 these requirements. */
9200 osym
.st_value
+= isec
->output_offset
;
9201 if (! finfo
->info
->relocatable
)
9203 osym
.st_value
+= isec
->output_section
->vma
;
9204 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9206 /* STT_TLS symbols are relative to PT_TLS segment base. */
9207 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
9208 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
9212 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
9216 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, isymbuf
,
9220 /* Relocate the contents of each section. */
9221 sym_hashes
= elf_sym_hashes (input_bfd
);
9222 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9226 if (! o
->linker_mark
)
9228 /* This section was omitted from the link. */
9232 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9233 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9236 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9238 /* Section was created by _bfd_elf_link_create_dynamic_sections
9243 /* Get the contents of the section. They have been cached by a
9244 relaxation routine. Note that o is a section in an input
9245 file, so the contents field will not have been set by any of
9246 the routines which work on output files. */
9247 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9248 contents
= elf_section_data (o
)->this_hdr
.contents
;
9251 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
9253 contents
= finfo
->contents
;
9254 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
9258 if ((o
->flags
& SEC_RELOC
) != 0)
9260 Elf_Internal_Rela
*internal_relocs
;
9261 bfd_vma r_type_mask
;
9265 /* Get the swapped relocs. */
9267 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
9268 finfo
->internal_relocs
, FALSE
);
9269 if (internal_relocs
== NULL
9270 && o
->reloc_count
> 0)
9273 if (bed
->s
->arch_size
== 32)
9280 r_type_mask
= 0xffffffff;
9284 /* Run through the relocs looking for any against symbols
9285 from discarded sections and section symbols from
9286 removed link-once sections. Complain about relocs
9287 against discarded sections. Zero relocs against removed
9288 link-once sections. */
9289 if (!elf_section_ignore_discarded_relocs (o
))
9291 Elf_Internal_Rela
*rel
, *relend
;
9292 unsigned int action
= (*bed
->action_discarded
) (o
);
9294 rel
= internal_relocs
;
9295 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9296 for ( ; rel
< relend
; rel
++)
9298 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9299 asection
**ps
, *sec
;
9300 struct elf_link_hash_entry
*h
= NULL
;
9301 const char *sym_name
;
9303 if (r_symndx
== STN_UNDEF
)
9306 if (r_symndx
>= locsymcount
9307 || (elf_bad_symtab (input_bfd
)
9308 && finfo
->sections
[r_symndx
] == NULL
))
9310 h
= sym_hashes
[r_symndx
- extsymoff
];
9312 /* Badly formatted input files can contain relocs that
9313 reference non-existant symbols. Check here so that
9314 we do not seg fault. */
9319 sprintf_vma (buffer
, rel
->r_info
);
9320 (*_bfd_error_handler
)
9321 (_("error: %B contains a reloc (0x%s) for section %A "
9322 "that references a non-existent global symbol"),
9323 input_bfd
, o
, buffer
);
9324 bfd_set_error (bfd_error_bad_value
);
9328 while (h
->root
.type
== bfd_link_hash_indirect
9329 || h
->root
.type
== bfd_link_hash_warning
)
9330 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9332 if (h
->root
.type
!= bfd_link_hash_defined
9333 && h
->root
.type
!= bfd_link_hash_defweak
)
9336 ps
= &h
->root
.u
.def
.section
;
9337 sym_name
= h
->root
.root
.string
;
9341 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9342 ps
= &finfo
->sections
[r_symndx
];
9343 sym_name
= bfd_elf_sym_name (input_bfd
,
9348 /* Complain if the definition comes from a
9349 discarded section. */
9350 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
9352 BFD_ASSERT (r_symndx
!= 0);
9353 if (action
& COMPLAIN
)
9354 (*finfo
->info
->callbacks
->einfo
)
9355 (_("%X`%s' referenced in section `%A' of %B: "
9356 "defined in discarded section `%A' of %B\n"),
9357 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9359 /* Try to do the best we can to support buggy old
9360 versions of gcc. Pretend that the symbol is
9361 really defined in the kept linkonce section.
9362 FIXME: This is quite broken. Modifying the
9363 symbol here means we will be changing all later
9364 uses of the symbol, not just in this section. */
9365 if (action
& PRETEND
)
9369 kept
= _bfd_elf_check_kept_section (sec
,
9381 /* Relocate the section by invoking a back end routine.
9383 The back end routine is responsible for adjusting the
9384 section contents as necessary, and (if using Rela relocs
9385 and generating a relocatable output file) adjusting the
9386 reloc addend as necessary.
9388 The back end routine does not have to worry about setting
9389 the reloc address or the reloc symbol index.
9391 The back end routine is given a pointer to the swapped in
9392 internal symbols, and can access the hash table entries
9393 for the external symbols via elf_sym_hashes (input_bfd).
9395 When generating relocatable output, the back end routine
9396 must handle STB_LOCAL/STT_SECTION symbols specially. The
9397 output symbol is going to be a section symbol
9398 corresponding to the output section, which will require
9399 the addend to be adjusted. */
9401 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
9402 input_bfd
, o
, contents
,
9410 || finfo
->info
->relocatable
9411 || finfo
->info
->emitrelocations
)
9413 Elf_Internal_Rela
*irela
;
9414 Elf_Internal_Rela
*irelaend
;
9415 bfd_vma last_offset
;
9416 struct elf_link_hash_entry
**rel_hash
;
9417 struct elf_link_hash_entry
**rel_hash_list
;
9418 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
9419 unsigned int next_erel
;
9420 bfd_boolean rela_normal
;
9422 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
9423 rela_normal
= (bed
->rela_normal
9424 && (input_rel_hdr
->sh_entsize
9425 == bed
->s
->sizeof_rela
));
9427 /* Adjust the reloc addresses and symbol indices. */
9429 irela
= internal_relocs
;
9430 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9431 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
9432 + elf_section_data (o
->output_section
)->rel_count
9433 + elf_section_data (o
->output_section
)->rel_count2
);
9434 rel_hash_list
= rel_hash
;
9435 last_offset
= o
->output_offset
;
9436 if (!finfo
->info
->relocatable
)
9437 last_offset
+= o
->output_section
->vma
;
9438 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9440 unsigned long r_symndx
;
9442 Elf_Internal_Sym sym
;
9444 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9450 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9453 if (irela
->r_offset
>= (bfd_vma
) -2)
9455 /* This is a reloc for a deleted entry or somesuch.
9456 Turn it into an R_*_NONE reloc, at the same
9457 offset as the last reloc. elf_eh_frame.c and
9458 bfd_elf_discard_info rely on reloc offsets
9460 irela
->r_offset
= last_offset
;
9462 irela
->r_addend
= 0;
9466 irela
->r_offset
+= o
->output_offset
;
9468 /* Relocs in an executable have to be virtual addresses. */
9469 if (!finfo
->info
->relocatable
)
9470 irela
->r_offset
+= o
->output_section
->vma
;
9472 last_offset
= irela
->r_offset
;
9474 r_symndx
= irela
->r_info
>> r_sym_shift
;
9475 if (r_symndx
== STN_UNDEF
)
9478 if (r_symndx
>= locsymcount
9479 || (elf_bad_symtab (input_bfd
)
9480 && finfo
->sections
[r_symndx
] == NULL
))
9482 struct elf_link_hash_entry
*rh
;
9485 /* This is a reloc against a global symbol. We
9486 have not yet output all the local symbols, so
9487 we do not know the symbol index of any global
9488 symbol. We set the rel_hash entry for this
9489 reloc to point to the global hash table entry
9490 for this symbol. The symbol index is then
9491 set at the end of bfd_elf_final_link. */
9492 indx
= r_symndx
- extsymoff
;
9493 rh
= elf_sym_hashes (input_bfd
)[indx
];
9494 while (rh
->root
.type
== bfd_link_hash_indirect
9495 || rh
->root
.type
== bfd_link_hash_warning
)
9496 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9498 /* Setting the index to -2 tells
9499 elf_link_output_extsym that this symbol is
9501 BFD_ASSERT (rh
->indx
< 0);
9509 /* This is a reloc against a local symbol. */
9512 sym
= isymbuf
[r_symndx
];
9513 sec
= finfo
->sections
[r_symndx
];
9514 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9516 /* I suppose the backend ought to fill in the
9517 section of any STT_SECTION symbol against a
9518 processor specific section. */
9520 if (bfd_is_abs_section (sec
))
9522 else if (sec
== NULL
|| sec
->owner
== NULL
)
9524 bfd_set_error (bfd_error_bad_value
);
9529 asection
*osec
= sec
->output_section
;
9531 /* If we have discarded a section, the output
9532 section will be the absolute section. In
9533 case of discarded SEC_MERGE sections, use
9534 the kept section. relocate_section should
9535 have already handled discarded linkonce
9537 if (bfd_is_abs_section (osec
)
9538 && sec
->kept_section
!= NULL
9539 && sec
->kept_section
->output_section
!= NULL
)
9541 osec
= sec
->kept_section
->output_section
;
9542 irela
->r_addend
-= osec
->vma
;
9545 if (!bfd_is_abs_section (osec
))
9547 r_symndx
= osec
->target_index
;
9550 struct elf_link_hash_table
*htab
;
9553 htab
= elf_hash_table (finfo
->info
);
9554 oi
= htab
->text_index_section
;
9555 if ((osec
->flags
& SEC_READONLY
) == 0
9556 && htab
->data_index_section
!= NULL
)
9557 oi
= htab
->data_index_section
;
9561 irela
->r_addend
+= osec
->vma
- oi
->vma
;
9562 r_symndx
= oi
->target_index
;
9566 BFD_ASSERT (r_symndx
!= 0);
9570 /* Adjust the addend according to where the
9571 section winds up in the output section. */
9573 irela
->r_addend
+= sec
->output_offset
;
9577 if (finfo
->indices
[r_symndx
] == -1)
9579 unsigned long shlink
;
9583 if (finfo
->info
->strip
== strip_all
)
9585 /* You can't do ld -r -s. */
9586 bfd_set_error (bfd_error_invalid_operation
);
9590 /* This symbol was skipped earlier, but
9591 since it is needed by a reloc, we
9592 must output it now. */
9593 shlink
= symtab_hdr
->sh_link
;
9594 name
= (bfd_elf_string_from_elf_section
9595 (input_bfd
, shlink
, sym
.st_name
));
9599 osec
= sec
->output_section
;
9601 _bfd_elf_section_from_bfd_section (output_bfd
,
9603 if (sym
.st_shndx
== SHN_BAD
)
9606 sym
.st_value
+= sec
->output_offset
;
9607 if (! finfo
->info
->relocatable
)
9609 sym
.st_value
+= osec
->vma
;
9610 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9612 /* STT_TLS symbols are relative to PT_TLS
9614 BFD_ASSERT (elf_hash_table (finfo
->info
)
9616 sym
.st_value
-= (elf_hash_table (finfo
->info
)
9621 finfo
->indices
[r_symndx
]
9622 = bfd_get_symcount (output_bfd
);
9624 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
9629 r_symndx
= finfo
->indices
[r_symndx
];
9632 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9633 | (irela
->r_info
& r_type_mask
));
9636 /* Swap out the relocs. */
9637 if (input_rel_hdr
->sh_size
!= 0
9638 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
9644 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
9645 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
9647 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9648 * bed
->s
->int_rels_per_ext_rel
);
9649 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9650 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9659 /* Write out the modified section contents. */
9660 if (bed
->elf_backend_write_section
9661 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
9664 /* Section written out. */
9666 else switch (o
->sec_info_type
)
9668 case ELF_INFO_TYPE_STABS
:
9669 if (! (_bfd_write_section_stabs
9671 &elf_hash_table (finfo
->info
)->stab_info
,
9672 o
, &elf_section_data (o
)->sec_info
, contents
)))
9675 case ELF_INFO_TYPE_MERGE
:
9676 if (! _bfd_write_merged_section (output_bfd
, o
,
9677 elf_section_data (o
)->sec_info
))
9680 case ELF_INFO_TYPE_EH_FRAME
:
9682 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
9689 if (! (o
->flags
& SEC_EXCLUDE
)
9690 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
9692 (file_ptr
) o
->output_offset
,
9703 /* Generate a reloc when linking an ELF file. This is a reloc
9704 requested by the linker, and does not come from any input file. This
9705 is used to build constructor and destructor tables when linking
9709 elf_reloc_link_order (bfd
*output_bfd
,
9710 struct bfd_link_info
*info
,
9711 asection
*output_section
,
9712 struct bfd_link_order
*link_order
)
9714 reloc_howto_type
*howto
;
9718 struct elf_link_hash_entry
**rel_hash_ptr
;
9719 Elf_Internal_Shdr
*rel_hdr
;
9720 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
9721 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
9725 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
9728 bfd_set_error (bfd_error_bad_value
);
9732 addend
= link_order
->u
.reloc
.p
->addend
;
9734 /* Figure out the symbol index. */
9735 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
9736 + elf_section_data (output_section
)->rel_count
9737 + elf_section_data (output_section
)->rel_count2
);
9738 if (link_order
->type
== bfd_section_reloc_link_order
)
9740 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
9741 BFD_ASSERT (indx
!= 0);
9742 *rel_hash_ptr
= NULL
;
9746 struct elf_link_hash_entry
*h
;
9748 /* Treat a reloc against a defined symbol as though it were
9749 actually against the section. */
9750 h
= ((struct elf_link_hash_entry
*)
9751 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
9752 link_order
->u
.reloc
.p
->u
.name
,
9753 FALSE
, FALSE
, TRUE
));
9755 && (h
->root
.type
== bfd_link_hash_defined
9756 || h
->root
.type
== bfd_link_hash_defweak
))
9760 section
= h
->root
.u
.def
.section
;
9761 indx
= section
->output_section
->target_index
;
9762 *rel_hash_ptr
= NULL
;
9763 /* It seems that we ought to add the symbol value to the
9764 addend here, but in practice it has already been added
9765 because it was passed to constructor_callback. */
9766 addend
+= section
->output_section
->vma
+ section
->output_offset
;
9770 /* Setting the index to -2 tells elf_link_output_extsym that
9771 this symbol is used by a reloc. */
9778 if (! ((*info
->callbacks
->unattached_reloc
)
9779 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9785 /* If this is an inplace reloc, we must write the addend into the
9787 if (howto
->partial_inplace
&& addend
!= 0)
9790 bfd_reloc_status_type rstat
;
9793 const char *sym_name
;
9795 size
= bfd_get_reloc_size (howto
);
9796 buf
= bfd_zmalloc (size
);
9799 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9806 case bfd_reloc_outofrange
:
9809 case bfd_reloc_overflow
:
9810 if (link_order
->type
== bfd_section_reloc_link_order
)
9811 sym_name
= bfd_section_name (output_bfd
,
9812 link_order
->u
.reloc
.p
->u
.section
);
9814 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9815 if (! ((*info
->callbacks
->reloc_overflow
)
9816 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9817 NULL
, (bfd_vma
) 0)))
9824 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9825 link_order
->offset
, size
);
9831 /* The address of a reloc is relative to the section in a
9832 relocatable file, and is a virtual address in an executable
9834 offset
= link_order
->offset
;
9835 if (! info
->relocatable
)
9836 offset
+= output_section
->vma
;
9838 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9840 irel
[i
].r_offset
= offset
;
9842 irel
[i
].r_addend
= 0;
9844 if (bed
->s
->arch_size
== 32)
9845 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9847 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9849 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9850 erel
= rel_hdr
->contents
;
9851 if (rel_hdr
->sh_type
== SHT_REL
)
9853 erel
+= (elf_section_data (output_section
)->rel_count
9854 * bed
->s
->sizeof_rel
);
9855 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9859 irel
[0].r_addend
= addend
;
9860 erel
+= (elf_section_data (output_section
)->rel_count
9861 * bed
->s
->sizeof_rela
);
9862 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9865 ++elf_section_data (output_section
)->rel_count
;
9871 /* Get the output vma of the section pointed to by the sh_link field. */
9874 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9876 Elf_Internal_Shdr
**elf_shdrp
;
9880 s
= p
->u
.indirect
.section
;
9881 elf_shdrp
= elf_elfsections (s
->owner
);
9882 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9883 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9885 The Intel C compiler generates SHT_IA_64_UNWIND with
9886 SHF_LINK_ORDER. But it doesn't set the sh_link or
9887 sh_info fields. Hence we could get the situation
9888 where elfsec is 0. */
9891 const struct elf_backend_data
*bed
9892 = get_elf_backend_data (s
->owner
);
9893 if (bed
->link_order_error_handler
)
9894 bed
->link_order_error_handler
9895 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9900 s
= elf_shdrp
[elfsec
]->bfd_section
;
9901 return s
->output_section
->vma
+ s
->output_offset
;
9906 /* Compare two sections based on the locations of the sections they are
9907 linked to. Used by elf_fixup_link_order. */
9910 compare_link_order (const void * a
, const void * b
)
9915 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9916 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9923 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9924 order as their linked sections. Returns false if this could not be done
9925 because an output section includes both ordered and unordered
9926 sections. Ideally we'd do this in the linker proper. */
9929 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9934 struct bfd_link_order
*p
;
9936 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9938 struct bfd_link_order
**sections
;
9939 asection
*s
, *other_sec
, *linkorder_sec
;
9943 linkorder_sec
= NULL
;
9946 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9948 if (p
->type
== bfd_indirect_link_order
)
9950 s
= p
->u
.indirect
.section
;
9952 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9953 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9954 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9955 && elfsec
< elf_numsections (sub
)
9956 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
9970 if (seen_other
&& seen_linkorder
)
9972 if (other_sec
&& linkorder_sec
)
9973 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9975 linkorder_sec
->owner
, other_sec
,
9978 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9980 bfd_set_error (bfd_error_bad_value
);
9985 if (!seen_linkorder
)
9988 sections
= (struct bfd_link_order
**)
9989 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9990 if (sections
== NULL
)
9994 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9996 sections
[seen_linkorder
++] = p
;
9998 /* Sort the input sections in the order of their linked section. */
9999 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
10000 compare_link_order
);
10002 /* Change the offsets of the sections. */
10004 for (n
= 0; n
< seen_linkorder
; n
++)
10006 s
= sections
[n
]->u
.indirect
.section
;
10007 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
10008 s
->output_offset
= offset
;
10009 sections
[n
]->offset
= offset
;
10010 offset
+= sections
[n
]->size
;
10017 /* Do the final step of an ELF link. */
10020 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10022 bfd_boolean dynamic
;
10023 bfd_boolean emit_relocs
;
10025 struct elf_final_link_info finfo
;
10026 register asection
*o
;
10027 register struct bfd_link_order
*p
;
10029 bfd_size_type max_contents_size
;
10030 bfd_size_type max_external_reloc_size
;
10031 bfd_size_type max_internal_reloc_count
;
10032 bfd_size_type max_sym_count
;
10033 bfd_size_type max_sym_shndx_count
;
10035 Elf_Internal_Sym elfsym
;
10037 Elf_Internal_Shdr
*symtab_hdr
;
10038 Elf_Internal_Shdr
*symtab_shndx_hdr
;
10039 Elf_Internal_Shdr
*symstrtab_hdr
;
10040 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10041 struct elf_outext_info eoinfo
;
10042 bfd_boolean merged
;
10043 size_t relativecount
= 0;
10044 asection
*reldyn
= 0;
10046 asection
*attr_section
= NULL
;
10047 bfd_vma attr_size
= 0;
10048 const char *std_attrs_section
;
10050 if (! is_elf_hash_table (info
->hash
))
10054 abfd
->flags
|= DYNAMIC
;
10056 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
10057 dynobj
= elf_hash_table (info
)->dynobj
;
10059 emit_relocs
= (info
->relocatable
10060 || info
->emitrelocations
);
10063 finfo
.output_bfd
= abfd
;
10064 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
10065 if (finfo
.symstrtab
== NULL
)
10070 finfo
.dynsym_sec
= NULL
;
10071 finfo
.hash_sec
= NULL
;
10072 finfo
.symver_sec
= NULL
;
10076 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
10077 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
10078 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
10079 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
10080 /* Note that it is OK if symver_sec is NULL. */
10083 finfo
.contents
= NULL
;
10084 finfo
.external_relocs
= NULL
;
10085 finfo
.internal_relocs
= NULL
;
10086 finfo
.external_syms
= NULL
;
10087 finfo
.locsym_shndx
= NULL
;
10088 finfo
.internal_syms
= NULL
;
10089 finfo
.indices
= NULL
;
10090 finfo
.sections
= NULL
;
10091 finfo
.symbuf
= NULL
;
10092 finfo
.symshndxbuf
= NULL
;
10093 finfo
.symbuf_count
= 0;
10094 finfo
.shndxbuf_size
= 0;
10096 /* The object attributes have been merged. Remove the input
10097 sections from the link, and set the contents of the output
10099 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10100 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10102 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10103 || strcmp (o
->name
, ".gnu.attributes") == 0)
10105 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10107 asection
*input_section
;
10109 if (p
->type
!= bfd_indirect_link_order
)
10111 input_section
= p
->u
.indirect
.section
;
10112 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10113 elf_link_input_bfd ignores this section. */
10114 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10117 attr_size
= bfd_elf_obj_attr_size (abfd
);
10120 bfd_set_section_size (abfd
, o
, attr_size
);
10122 /* Skip this section later on. */
10123 o
->map_head
.link_order
= NULL
;
10126 o
->flags
|= SEC_EXCLUDE
;
10130 /* Count up the number of relocations we will output for each output
10131 section, so that we know the sizes of the reloc sections. We
10132 also figure out some maximum sizes. */
10133 max_contents_size
= 0;
10134 max_external_reloc_size
= 0;
10135 max_internal_reloc_count
= 0;
10137 max_sym_shndx_count
= 0;
10139 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10141 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10142 o
->reloc_count
= 0;
10144 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10146 unsigned int reloc_count
= 0;
10147 struct bfd_elf_section_data
*esdi
= NULL
;
10148 unsigned int *rel_count1
;
10150 if (p
->type
== bfd_section_reloc_link_order
10151 || p
->type
== bfd_symbol_reloc_link_order
)
10153 else if (p
->type
== bfd_indirect_link_order
)
10157 sec
= p
->u
.indirect
.section
;
10158 esdi
= elf_section_data (sec
);
10160 /* Mark all sections which are to be included in the
10161 link. This will normally be every section. We need
10162 to do this so that we can identify any sections which
10163 the linker has decided to not include. */
10164 sec
->linker_mark
= TRUE
;
10166 if (sec
->flags
& SEC_MERGE
)
10169 if (info
->relocatable
|| info
->emitrelocations
)
10170 reloc_count
= sec
->reloc_count
;
10171 else if (bed
->elf_backend_count_relocs
)
10173 Elf_Internal_Rela
* relocs
;
10175 relocs
= _bfd_elf_link_read_relocs (sec
->owner
, sec
,
10177 info
->keep_memory
);
10179 if (relocs
!= NULL
)
10182 = (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
10184 if (elf_section_data (sec
)->relocs
!= relocs
)
10189 if (sec
->rawsize
> max_contents_size
)
10190 max_contents_size
= sec
->rawsize
;
10191 if (sec
->size
> max_contents_size
)
10192 max_contents_size
= sec
->size
;
10194 /* We are interested in just local symbols, not all
10196 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10197 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10201 if (elf_bad_symtab (sec
->owner
))
10202 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10203 / bed
->s
->sizeof_sym
);
10205 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10207 if (sym_count
> max_sym_count
)
10208 max_sym_count
= sym_count
;
10210 if (sym_count
> max_sym_shndx_count
10211 && elf_symtab_shndx (sec
->owner
) != 0)
10212 max_sym_shndx_count
= sym_count
;
10214 if ((sec
->flags
& SEC_RELOC
) != 0)
10218 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
10219 if (ext_size
> max_external_reloc_size
)
10220 max_external_reloc_size
= ext_size
;
10221 if (sec
->reloc_count
> max_internal_reloc_count
)
10222 max_internal_reloc_count
= sec
->reloc_count
;
10227 if (reloc_count
== 0)
10230 o
->reloc_count
+= reloc_count
;
10232 /* MIPS may have a mix of REL and RELA relocs on sections.
10233 To support this curious ABI we keep reloc counts in
10234 elf_section_data too. We must be careful to add the
10235 relocations from the input section to the right output
10236 count. FIXME: Get rid of one count. We have
10237 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10238 rel_count1
= &esdo
->rel_count
;
10241 bfd_boolean same_size
;
10242 bfd_size_type entsize1
;
10244 entsize1
= esdi
->rel_hdr
.sh_entsize
;
10245 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
10246 || entsize1
== bed
->s
->sizeof_rela
);
10247 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
10250 rel_count1
= &esdo
->rel_count2
;
10252 if (esdi
->rel_hdr2
!= NULL
)
10254 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
10255 unsigned int alt_count
;
10256 unsigned int *rel_count2
;
10258 BFD_ASSERT (entsize2
!= entsize1
10259 && (entsize2
== bed
->s
->sizeof_rel
10260 || entsize2
== bed
->s
->sizeof_rela
));
10262 rel_count2
= &esdo
->rel_count2
;
10264 rel_count2
= &esdo
->rel_count
;
10266 /* The following is probably too simplistic if the
10267 backend counts output relocs unusually. */
10268 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
10269 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
10270 *rel_count2
+= alt_count
;
10271 reloc_count
-= alt_count
;
10274 *rel_count1
+= reloc_count
;
10277 if (o
->reloc_count
> 0)
10278 o
->flags
|= SEC_RELOC
;
10281 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10282 set it (this is probably a bug) and if it is set
10283 assign_section_numbers will create a reloc section. */
10284 o
->flags
&=~ SEC_RELOC
;
10287 /* If the SEC_ALLOC flag is not set, force the section VMA to
10288 zero. This is done in elf_fake_sections as well, but forcing
10289 the VMA to 0 here will ensure that relocs against these
10290 sections are handled correctly. */
10291 if ((o
->flags
& SEC_ALLOC
) == 0
10292 && ! o
->user_set_vma
)
10296 if (! info
->relocatable
&& merged
)
10297 elf_link_hash_traverse (elf_hash_table (info
),
10298 _bfd_elf_link_sec_merge_syms
, abfd
);
10300 /* Figure out the file positions for everything but the symbol table
10301 and the relocs. We set symcount to force assign_section_numbers
10302 to create a symbol table. */
10303 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10304 BFD_ASSERT (! abfd
->output_has_begun
);
10305 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10308 /* Set sizes, and assign file positions for reloc sections. */
10309 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10311 if ((o
->flags
& SEC_RELOC
) != 0)
10313 if (!(_bfd_elf_link_size_reloc_section
10314 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
10317 if (elf_section_data (o
)->rel_hdr2
10318 && !(_bfd_elf_link_size_reloc_section
10319 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
10323 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10324 to count upwards while actually outputting the relocations. */
10325 elf_section_data (o
)->rel_count
= 0;
10326 elf_section_data (o
)->rel_count2
= 0;
10329 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10331 /* We have now assigned file positions for all the sections except
10332 .symtab and .strtab. We start the .symtab section at the current
10333 file position, and write directly to it. We build the .strtab
10334 section in memory. */
10335 bfd_get_symcount (abfd
) = 0;
10336 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10337 /* sh_name is set in prep_headers. */
10338 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10339 /* sh_flags, sh_addr and sh_size all start off zero. */
10340 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10341 /* sh_link is set in assign_section_numbers. */
10342 /* sh_info is set below. */
10343 /* sh_offset is set just below. */
10344 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
10346 off
= elf_tdata (abfd
)->next_file_pos
;
10347 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10349 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10350 incorrect. We do not yet know the size of the .symtab section.
10351 We correct next_file_pos below, after we do know the size. */
10353 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10354 continuously seeking to the right position in the file. */
10355 if (! info
->keep_memory
|| max_sym_count
< 20)
10356 finfo
.symbuf_size
= 20;
10358 finfo
.symbuf_size
= max_sym_count
;
10359 amt
= finfo
.symbuf_size
;
10360 amt
*= bed
->s
->sizeof_sym
;
10361 finfo
.symbuf
= bfd_malloc (amt
);
10362 if (finfo
.symbuf
== NULL
)
10364 if (elf_numsections (abfd
) > SHN_LORESERVE
)
10366 /* Wild guess at number of output symbols. realloc'd as needed. */
10367 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10368 finfo
.shndxbuf_size
= amt
;
10369 amt
*= sizeof (Elf_External_Sym_Shndx
);
10370 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
10371 if (finfo
.symshndxbuf
== NULL
)
10375 /* Start writing out the symbol table. The first symbol is always a
10377 if (info
->strip
!= strip_all
10380 elfsym
.st_value
= 0;
10381 elfsym
.st_size
= 0;
10382 elfsym
.st_info
= 0;
10383 elfsym
.st_other
= 0;
10384 elfsym
.st_shndx
= SHN_UNDEF
;
10385 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10390 /* Output a symbol for each section. We output these even if we are
10391 discarding local symbols, since they are used for relocs. These
10392 symbols have no names. We store the index of each one in the
10393 index field of the section, so that we can find it again when
10394 outputting relocs. */
10395 if (info
->strip
!= strip_all
10398 elfsym
.st_size
= 0;
10399 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10400 elfsym
.st_other
= 0;
10401 elfsym
.st_value
= 0;
10402 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10404 o
= bfd_section_from_elf_index (abfd
, i
);
10407 o
->target_index
= bfd_get_symcount (abfd
);
10408 elfsym
.st_shndx
= i
;
10409 if (!info
->relocatable
)
10410 elfsym
.st_value
= o
->vma
;
10411 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
10414 if (i
== SHN_LORESERVE
- 1)
10415 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
10419 /* Allocate some memory to hold information read in from the input
10421 if (max_contents_size
!= 0)
10423 finfo
.contents
= bfd_malloc (max_contents_size
);
10424 if (finfo
.contents
== NULL
)
10428 if (max_external_reloc_size
!= 0)
10430 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10431 if (finfo
.external_relocs
== NULL
)
10435 if (max_internal_reloc_count
!= 0)
10437 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10438 amt
*= sizeof (Elf_Internal_Rela
);
10439 finfo
.internal_relocs
= bfd_malloc (amt
);
10440 if (finfo
.internal_relocs
== NULL
)
10444 if (max_sym_count
!= 0)
10446 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10447 finfo
.external_syms
= bfd_malloc (amt
);
10448 if (finfo
.external_syms
== NULL
)
10451 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10452 finfo
.internal_syms
= bfd_malloc (amt
);
10453 if (finfo
.internal_syms
== NULL
)
10456 amt
= max_sym_count
* sizeof (long);
10457 finfo
.indices
= bfd_malloc (amt
);
10458 if (finfo
.indices
== NULL
)
10461 amt
= max_sym_count
* sizeof (asection
*);
10462 finfo
.sections
= bfd_malloc (amt
);
10463 if (finfo
.sections
== NULL
)
10467 if (max_sym_shndx_count
!= 0)
10469 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10470 finfo
.locsym_shndx
= bfd_malloc (amt
);
10471 if (finfo
.locsym_shndx
== NULL
)
10475 if (elf_hash_table (info
)->tls_sec
)
10477 bfd_vma base
, end
= 0;
10480 for (sec
= elf_hash_table (info
)->tls_sec
;
10481 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10484 bfd_size_type size
= sec
->size
;
10487 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10489 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
10491 size
= o
->offset
+ o
->size
;
10493 end
= sec
->vma
+ size
;
10495 base
= elf_hash_table (info
)->tls_sec
->vma
;
10496 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
10497 elf_hash_table (info
)->tls_size
= end
- base
;
10500 /* Reorder SHF_LINK_ORDER sections. */
10501 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10503 if (!elf_fixup_link_order (abfd
, o
))
10507 /* Since ELF permits relocations to be against local symbols, we
10508 must have the local symbols available when we do the relocations.
10509 Since we would rather only read the local symbols once, and we
10510 would rather not keep them in memory, we handle all the
10511 relocations for a single input file at the same time.
10513 Unfortunately, there is no way to know the total number of local
10514 symbols until we have seen all of them, and the local symbol
10515 indices precede the global symbol indices. This means that when
10516 we are generating relocatable output, and we see a reloc against
10517 a global symbol, we can not know the symbol index until we have
10518 finished examining all the local symbols to see which ones we are
10519 going to output. To deal with this, we keep the relocations in
10520 memory, and don't output them until the end of the link. This is
10521 an unfortunate waste of memory, but I don't see a good way around
10522 it. Fortunately, it only happens when performing a relocatable
10523 link, which is not the common case. FIXME: If keep_memory is set
10524 we could write the relocs out and then read them again; I don't
10525 know how bad the memory loss will be. */
10527 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10528 sub
->output_has_begun
= FALSE
;
10529 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10531 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10533 if (p
->type
== bfd_indirect_link_order
10534 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10535 == bfd_target_elf_flavour
)
10536 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10538 if (! sub
->output_has_begun
)
10540 if (! elf_link_input_bfd (&finfo
, sub
))
10542 sub
->output_has_begun
= TRUE
;
10545 else if (p
->type
== bfd_section_reloc_link_order
10546 || p
->type
== bfd_symbol_reloc_link_order
)
10548 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10553 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10559 /* Free symbol buffer if needed. */
10560 if (!info
->reduce_memory_overheads
)
10562 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10563 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10564 && elf_tdata (sub
)->symbuf
)
10566 free (elf_tdata (sub
)->symbuf
);
10567 elf_tdata (sub
)->symbuf
= NULL
;
10571 /* Output any global symbols that got converted to local in a
10572 version script or due to symbol visibility. We do this in a
10573 separate step since ELF requires all local symbols to appear
10574 prior to any global symbols. FIXME: We should only do this if
10575 some global symbols were, in fact, converted to become local.
10576 FIXME: Will this work correctly with the Irix 5 linker? */
10577 eoinfo
.failed
= FALSE
;
10578 eoinfo
.finfo
= &finfo
;
10579 eoinfo
.localsyms
= TRUE
;
10580 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10585 /* If backend needs to output some local symbols not present in the hash
10586 table, do it now. */
10587 if (bed
->elf_backend_output_arch_local_syms
)
10589 typedef bfd_boolean (*out_sym_func
)
10590 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10591 struct elf_link_hash_entry
*);
10593 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10594 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10598 /* That wrote out all the local symbols. Finish up the symbol table
10599 with the global symbols. Even if we want to strip everything we
10600 can, we still need to deal with those global symbols that got
10601 converted to local in a version script. */
10603 /* The sh_info field records the index of the first non local symbol. */
10604 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10607 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10609 Elf_Internal_Sym sym
;
10610 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
10611 long last_local
= 0;
10613 /* Write out the section symbols for the output sections. */
10614 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10620 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10623 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
10629 dynindx
= elf_section_data (s
)->dynindx
;
10632 indx
= elf_section_data (s
)->this_idx
;
10633 BFD_ASSERT (indx
> 0);
10634 sym
.st_shndx
= indx
;
10635 if (! check_dynsym (abfd
, &sym
))
10637 sym
.st_value
= s
->vma
;
10638 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
10639 if (last_local
< dynindx
)
10640 last_local
= dynindx
;
10641 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10645 /* Write out the local dynsyms. */
10646 if (elf_hash_table (info
)->dynlocal
)
10648 struct elf_link_local_dynamic_entry
*e
;
10649 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
10654 sym
.st_size
= e
->isym
.st_size
;
10655 sym
.st_other
= e
->isym
.st_other
;
10657 /* Copy the internal symbol as is.
10658 Note that we saved a word of storage and overwrote
10659 the original st_name with the dynstr_index. */
10662 if (e
->isym
.st_shndx
!= SHN_UNDEF
10663 && (e
->isym
.st_shndx
< SHN_LORESERVE
10664 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
10666 s
= bfd_section_from_elf_index (e
->input_bfd
,
10670 elf_section_data (s
->output_section
)->this_idx
;
10671 if (! check_dynsym (abfd
, &sym
))
10673 sym
.st_value
= (s
->output_section
->vma
10675 + e
->isym
.st_value
);
10678 if (last_local
< e
->dynindx
)
10679 last_local
= e
->dynindx
;
10681 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
10682 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10686 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
10690 /* We get the global symbols from the hash table. */
10691 eoinfo
.failed
= FALSE
;
10692 eoinfo
.localsyms
= FALSE
;
10693 eoinfo
.finfo
= &finfo
;
10694 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10699 /* If backend needs to output some symbols not present in the hash
10700 table, do it now. */
10701 if (bed
->elf_backend_output_arch_syms
)
10703 typedef bfd_boolean (*out_sym_func
)
10704 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10705 struct elf_link_hash_entry
*);
10707 if (! ((*bed
->elf_backend_output_arch_syms
)
10708 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10712 /* Flush all symbols to the file. */
10713 if (! elf_link_flush_output_syms (&finfo
, bed
))
10716 /* Now we know the size of the symtab section. */
10717 off
+= symtab_hdr
->sh_size
;
10719 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
10720 if (symtab_shndx_hdr
->sh_name
!= 0)
10722 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
10723 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
10724 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
10725 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
10726 symtab_shndx_hdr
->sh_size
= amt
;
10728 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
10731 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
10732 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
10737 /* Finish up and write out the symbol string table (.strtab)
10739 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
10740 /* sh_name was set in prep_headers. */
10741 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
10742 symstrtab_hdr
->sh_flags
= 0;
10743 symstrtab_hdr
->sh_addr
= 0;
10744 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
10745 symstrtab_hdr
->sh_entsize
= 0;
10746 symstrtab_hdr
->sh_link
= 0;
10747 symstrtab_hdr
->sh_info
= 0;
10748 /* sh_offset is set just below. */
10749 symstrtab_hdr
->sh_addralign
= 1;
10751 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
10752 elf_tdata (abfd
)->next_file_pos
= off
;
10754 if (bfd_get_symcount (abfd
) > 0)
10756 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
10757 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
10761 /* Adjust the relocs to have the correct symbol indices. */
10762 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10764 if ((o
->flags
& SEC_RELOC
) == 0)
10767 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
10768 elf_section_data (o
)->rel_count
,
10769 elf_section_data (o
)->rel_hashes
);
10770 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
10771 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
10772 elf_section_data (o
)->rel_count2
,
10773 (elf_section_data (o
)->rel_hashes
10774 + elf_section_data (o
)->rel_count
));
10776 /* Set the reloc_count field to 0 to prevent write_relocs from
10777 trying to swap the relocs out itself. */
10778 o
->reloc_count
= 0;
10781 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10782 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10784 /* If we are linking against a dynamic object, or generating a
10785 shared library, finish up the dynamic linking information. */
10788 bfd_byte
*dyncon
, *dynconend
;
10790 /* Fix up .dynamic entries. */
10791 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10792 BFD_ASSERT (o
!= NULL
);
10794 dyncon
= o
->contents
;
10795 dynconend
= o
->contents
+ o
->size
;
10796 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10798 Elf_Internal_Dyn dyn
;
10802 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10809 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10811 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10813 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10814 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10817 dyn
.d_un
.d_val
= relativecount
;
10824 name
= info
->init_function
;
10827 name
= info
->fini_function
;
10830 struct elf_link_hash_entry
*h
;
10832 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10833 FALSE
, FALSE
, TRUE
);
10835 && (h
->root
.type
== bfd_link_hash_defined
10836 || h
->root
.type
== bfd_link_hash_defweak
))
10838 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
10839 o
= h
->root
.u
.def
.section
;
10840 if (o
->output_section
!= NULL
)
10841 dyn
.d_un
.d_val
+= (o
->output_section
->vma
10842 + o
->output_offset
);
10845 /* The symbol is imported from another shared
10846 library and does not apply to this one. */
10847 dyn
.d_un
.d_val
= 0;
10854 case DT_PREINIT_ARRAYSZ
:
10855 name
= ".preinit_array";
10857 case DT_INIT_ARRAYSZ
:
10858 name
= ".init_array";
10860 case DT_FINI_ARRAYSZ
:
10861 name
= ".fini_array";
10863 o
= bfd_get_section_by_name (abfd
, name
);
10866 (*_bfd_error_handler
)
10867 (_("%B: could not find output section %s"), abfd
, name
);
10871 (*_bfd_error_handler
)
10872 (_("warning: %s section has zero size"), name
);
10873 dyn
.d_un
.d_val
= o
->size
;
10876 case DT_PREINIT_ARRAY
:
10877 name
= ".preinit_array";
10879 case DT_INIT_ARRAY
:
10880 name
= ".init_array";
10882 case DT_FINI_ARRAY
:
10883 name
= ".fini_array";
10890 name
= ".gnu.hash";
10899 name
= ".gnu.version_d";
10902 name
= ".gnu.version_r";
10905 name
= ".gnu.version";
10907 o
= bfd_get_section_by_name (abfd
, name
);
10910 (*_bfd_error_handler
)
10911 (_("%B: could not find output section %s"), abfd
, name
);
10914 dyn
.d_un
.d_ptr
= o
->vma
;
10921 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10925 dyn
.d_un
.d_val
= 0;
10926 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10928 Elf_Internal_Shdr
*hdr
;
10930 hdr
= elf_elfsections (abfd
)[i
];
10931 if (hdr
->sh_type
== type
10932 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10934 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10935 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10938 if (dyn
.d_un
.d_val
== 0
10939 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
10940 dyn
.d_un
.d_val
= hdr
->sh_addr
;
10946 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10950 /* If we have created any dynamic sections, then output them. */
10951 if (dynobj
!= NULL
)
10953 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10956 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10957 if (info
->warn_shared_textrel
&& info
->shared
)
10959 bfd_byte
*dyncon
, *dynconend
;
10961 /* Fix up .dynamic entries. */
10962 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10963 BFD_ASSERT (o
!= NULL
);
10965 dyncon
= o
->contents
;
10966 dynconend
= o
->contents
+ o
->size
;
10967 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10969 Elf_Internal_Dyn dyn
;
10971 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10973 if (dyn
.d_tag
== DT_TEXTREL
)
10975 info
->callbacks
->einfo
10976 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10982 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10984 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10986 || o
->output_section
== bfd_abs_section_ptr
)
10988 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10990 /* At this point, we are only interested in sections
10991 created by _bfd_elf_link_create_dynamic_sections. */
10994 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10996 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10998 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
11000 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
11002 if (! bfd_set_section_contents (abfd
, o
->output_section
,
11004 (file_ptr
) o
->output_offset
,
11010 /* The contents of the .dynstr section are actually in a
11012 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
11013 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
11014 || ! _bfd_elf_strtab_emit (abfd
,
11015 elf_hash_table (info
)->dynstr
))
11021 if (info
->relocatable
)
11023 bfd_boolean failed
= FALSE
;
11025 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
11030 /* If we have optimized stabs strings, output them. */
11031 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
11033 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
11037 if (info
->eh_frame_hdr
)
11039 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
11043 if (finfo
.symstrtab
!= NULL
)
11044 _bfd_stringtab_free (finfo
.symstrtab
);
11045 if (finfo
.contents
!= NULL
)
11046 free (finfo
.contents
);
11047 if (finfo
.external_relocs
!= NULL
)
11048 free (finfo
.external_relocs
);
11049 if (finfo
.internal_relocs
!= NULL
)
11050 free (finfo
.internal_relocs
);
11051 if (finfo
.external_syms
!= NULL
)
11052 free (finfo
.external_syms
);
11053 if (finfo
.locsym_shndx
!= NULL
)
11054 free (finfo
.locsym_shndx
);
11055 if (finfo
.internal_syms
!= NULL
)
11056 free (finfo
.internal_syms
);
11057 if (finfo
.indices
!= NULL
)
11058 free (finfo
.indices
);
11059 if (finfo
.sections
!= NULL
)
11060 free (finfo
.sections
);
11061 if (finfo
.symbuf
!= NULL
)
11062 free (finfo
.symbuf
);
11063 if (finfo
.symshndxbuf
!= NULL
)
11064 free (finfo
.symshndxbuf
);
11065 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11067 if ((o
->flags
& SEC_RELOC
) != 0
11068 && elf_section_data (o
)->rel_hashes
!= NULL
)
11069 free (elf_section_data (o
)->rel_hashes
);
11072 elf_tdata (abfd
)->linker
= TRUE
;
11076 bfd_byte
*contents
= bfd_malloc (attr_size
);
11077 if (contents
== NULL
)
11078 return FALSE
; /* Bail out and fail. */
11079 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11080 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11087 if (finfo
.symstrtab
!= NULL
)
11088 _bfd_stringtab_free (finfo
.symstrtab
);
11089 if (finfo
.contents
!= NULL
)
11090 free (finfo
.contents
);
11091 if (finfo
.external_relocs
!= NULL
)
11092 free (finfo
.external_relocs
);
11093 if (finfo
.internal_relocs
!= NULL
)
11094 free (finfo
.internal_relocs
);
11095 if (finfo
.external_syms
!= NULL
)
11096 free (finfo
.external_syms
);
11097 if (finfo
.locsym_shndx
!= NULL
)
11098 free (finfo
.locsym_shndx
);
11099 if (finfo
.internal_syms
!= NULL
)
11100 free (finfo
.internal_syms
);
11101 if (finfo
.indices
!= NULL
)
11102 free (finfo
.indices
);
11103 if (finfo
.sections
!= NULL
)
11104 free (finfo
.sections
);
11105 if (finfo
.symbuf
!= NULL
)
11106 free (finfo
.symbuf
);
11107 if (finfo
.symshndxbuf
!= NULL
)
11108 free (finfo
.symshndxbuf
);
11109 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11111 if ((o
->flags
& SEC_RELOC
) != 0
11112 && elf_section_data (o
)->rel_hashes
!= NULL
)
11113 free (elf_section_data (o
)->rel_hashes
);
11119 /* Garbage collect unused sections. */
11121 /* Default gc_mark_hook. */
11124 _bfd_elf_gc_mark_hook (asection
*sec
,
11125 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11126 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11127 struct elf_link_hash_entry
*h
,
11128 Elf_Internal_Sym
*sym
)
11132 switch (h
->root
.type
)
11134 case bfd_link_hash_defined
:
11135 case bfd_link_hash_defweak
:
11136 return h
->root
.u
.def
.section
;
11138 case bfd_link_hash_common
:
11139 return h
->root
.u
.c
.p
->section
;
11146 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11151 /* The mark phase of garbage collection. For a given section, mark
11152 it and any sections in this section's group, and all the sections
11153 which define symbols to which it refers. */
11156 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11158 elf_gc_mark_hook_fn gc_mark_hook
)
11162 asection
*group_sec
;
11166 /* Mark all the sections in the group. */
11167 group_sec
= elf_section_data (sec
)->next_in_group
;
11168 if (group_sec
&& !group_sec
->gc_mark
)
11169 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11172 /* Look through the section relocs. */
11174 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
11175 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
11177 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
11178 Elf_Internal_Shdr
*symtab_hdr
;
11179 struct elf_link_hash_entry
**sym_hashes
;
11182 bfd
*input_bfd
= sec
->owner
;
11183 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
11184 Elf_Internal_Sym
*isym
= NULL
;
11187 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11188 sym_hashes
= elf_sym_hashes (input_bfd
);
11190 /* Read the local symbols. */
11191 if (elf_bad_symtab (input_bfd
))
11193 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11197 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
11199 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11200 if (isym
== NULL
&& nlocsyms
!= 0)
11202 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
11208 /* Read the relocations. */
11209 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
11210 info
->keep_memory
);
11211 if (relstart
== NULL
)
11216 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11218 if (bed
->s
->arch_size
== 32)
11223 for (rel
= relstart
; rel
< relend
; rel
++)
11225 unsigned long r_symndx
;
11227 struct elf_link_hash_entry
*h
;
11229 r_symndx
= rel
->r_info
>> r_sym_shift
;
11233 if (r_symndx
>= nlocsyms
11234 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
11236 h
= sym_hashes
[r_symndx
- extsymoff
];
11237 while (h
->root
.type
== bfd_link_hash_indirect
11238 || h
->root
.type
== bfd_link_hash_warning
)
11239 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11240 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
11244 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
11247 if (rsec
&& !rsec
->gc_mark
)
11249 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
11252 rsec
->gc_mark_from_eh
= 1;
11253 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11262 if (elf_section_data (sec
)->relocs
!= relstart
)
11265 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
11267 if (! info
->keep_memory
)
11270 symtab_hdr
->contents
= (unsigned char *) isym
;
11277 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11279 struct elf_gc_sweep_symbol_info
11281 struct bfd_link_info
*info
;
11282 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11287 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11289 if (h
->root
.type
== bfd_link_hash_warning
)
11290 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11292 if ((h
->root
.type
== bfd_link_hash_defined
11293 || h
->root
.type
== bfd_link_hash_defweak
)
11294 && !h
->root
.u
.def
.section
->gc_mark
11295 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
11297 struct elf_gc_sweep_symbol_info
*inf
= data
;
11298 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11304 /* The sweep phase of garbage collection. Remove all garbage sections. */
11306 typedef bfd_boolean (*gc_sweep_hook_fn
)
11307 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11310 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11313 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11314 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11315 unsigned long section_sym_count
;
11316 struct elf_gc_sweep_symbol_info sweep_info
;
11318 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11322 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11325 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11327 /* Keep debug and special sections. */
11328 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
11329 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
11335 /* Skip sweeping sections already excluded. */
11336 if (o
->flags
& SEC_EXCLUDE
)
11339 /* Since this is early in the link process, it is simple
11340 to remove a section from the output. */
11341 o
->flags
|= SEC_EXCLUDE
;
11343 if (info
->print_gc_sections
&& o
->size
!= 0)
11344 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11346 /* But we also have to update some of the relocation
11347 info we collected before. */
11349 && (o
->flags
& SEC_RELOC
) != 0
11350 && o
->reloc_count
> 0
11351 && !bfd_is_abs_section (o
->output_section
))
11353 Elf_Internal_Rela
*internal_relocs
;
11357 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11358 info
->keep_memory
);
11359 if (internal_relocs
== NULL
)
11362 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11364 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11365 free (internal_relocs
);
11373 /* Remove the symbols that were in the swept sections from the dynamic
11374 symbol table. GCFIXME: Anyone know how to get them out of the
11375 static symbol table as well? */
11376 sweep_info
.info
= info
;
11377 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11378 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11381 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11385 /* Propagate collected vtable information. This is called through
11386 elf_link_hash_traverse. */
11389 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11391 if (h
->root
.type
== bfd_link_hash_warning
)
11392 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11394 /* Those that are not vtables. */
11395 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11398 /* Those vtables that do not have parents, we cannot merge. */
11399 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11402 /* If we've already been done, exit. */
11403 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11406 /* Make sure the parent's table is up to date. */
11407 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11409 if (h
->vtable
->used
== NULL
)
11411 /* None of this table's entries were referenced. Re-use the
11413 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
11414 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
11419 bfd_boolean
*cu
, *pu
;
11421 /* Or the parent's entries into ours. */
11422 cu
= h
->vtable
->used
;
11424 pu
= h
->vtable
->parent
->vtable
->used
;
11427 const struct elf_backend_data
*bed
;
11428 unsigned int log_file_align
;
11430 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
11431 log_file_align
= bed
->s
->log_file_align
;
11432 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
11447 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
11450 bfd_vma hstart
, hend
;
11451 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
11452 const struct elf_backend_data
*bed
;
11453 unsigned int log_file_align
;
11455 if (h
->root
.type
== bfd_link_hash_warning
)
11456 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11458 /* Take care of both those symbols that do not describe vtables as
11459 well as those that are not loaded. */
11460 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11463 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
11464 || h
->root
.type
== bfd_link_hash_defweak
);
11466 sec
= h
->root
.u
.def
.section
;
11467 hstart
= h
->root
.u
.def
.value
;
11468 hend
= hstart
+ h
->size
;
11470 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
11472 return *(bfd_boolean
*) okp
= FALSE
;
11473 bed
= get_elf_backend_data (sec
->owner
);
11474 log_file_align
= bed
->s
->log_file_align
;
11476 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11478 for (rel
= relstart
; rel
< relend
; ++rel
)
11479 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
11481 /* If the entry is in use, do nothing. */
11482 if (h
->vtable
->used
11483 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
11485 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
11486 if (h
->vtable
->used
[entry
])
11489 /* Otherwise, kill it. */
11490 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
11496 /* Mark sections containing dynamically referenced symbols. When
11497 building shared libraries, we must assume that any visible symbol is
11501 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
11503 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
11505 if (h
->root
.type
== bfd_link_hash_warning
)
11506 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11508 if ((h
->root
.type
== bfd_link_hash_defined
11509 || h
->root
.type
== bfd_link_hash_defweak
)
11511 || (!info
->executable
11513 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
11514 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
11515 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11520 /* Do mark and sweep of unused sections. */
11523 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
11525 bfd_boolean ok
= TRUE
;
11527 elf_gc_mark_hook_fn gc_mark_hook
;
11528 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11530 if (!bed
->can_gc_sections
11531 || info
->relocatable
11532 || info
->emitrelocations
11533 || !is_elf_hash_table (info
->hash
))
11535 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
11539 /* Apply transitive closure to the vtable entry usage info. */
11540 elf_link_hash_traverse (elf_hash_table (info
),
11541 elf_gc_propagate_vtable_entries_used
,
11546 /* Kill the vtable relocations that were not used. */
11547 elf_link_hash_traverse (elf_hash_table (info
),
11548 elf_gc_smash_unused_vtentry_relocs
,
11553 /* Mark dynamically referenced symbols. */
11554 if (elf_hash_table (info
)->dynamic_sections_created
)
11555 elf_link_hash_traverse (elf_hash_table (info
),
11556 bed
->gc_mark_dynamic_ref
,
11559 /* Grovel through relocs to find out who stays ... */
11560 gc_mark_hook
= bed
->gc_mark_hook
;
11561 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11565 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11568 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11569 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
11570 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11574 /* Allow the backend to mark additional target specific sections. */
11575 if (bed
->gc_mark_extra_sections
)
11576 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
11578 /* ... again for sections marked from eh_frame. */
11579 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11583 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11586 /* Keep .gcc_except_table.* if the associated .text.* (or the
11587 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
11588 marked. This isn't very nice, but the proper solution,
11589 splitting .eh_frame up and using comdat doesn't pan out
11590 easily due to needing special relocs to handle the
11591 difference of two symbols in separate sections.
11592 Don't keep code sections referenced by .eh_frame. */
11593 #define TEXT_PREFIX ".text."
11594 #define TEXT_PREFIX2 ".gnu.linkonce.t."
11595 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
11596 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11597 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
11599 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
11602 const char *sec_name
;
11604 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
11606 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
11607 sec_name
= o
->name
+ o_name_prefix_len
;
11608 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
11609 tmp
= strlen (TEXT_PREFIX2
);
11610 if (tmp
> fn_name_prefix_len
)
11611 fn_name_prefix_len
= tmp
;
11613 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
11614 if (fn_name
== NULL
)
11617 /* Try the first prefix. */
11618 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
11619 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
11621 /* Try the second prefix. */
11622 if (fn_text
== NULL
)
11624 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
11625 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
11629 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
11633 /* If not using specially named exception table section,
11634 then keep whatever we are using. */
11635 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11640 /* ... and mark SEC_EXCLUDE for those that go. */
11641 return elf_gc_sweep (abfd
, info
);
11644 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11647 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
11649 struct elf_link_hash_entry
*h
,
11652 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
11653 struct elf_link_hash_entry
**search
, *child
;
11654 bfd_size_type extsymcount
;
11655 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11657 /* The sh_info field of the symtab header tells us where the
11658 external symbols start. We don't care about the local symbols at
11660 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
11661 if (!elf_bad_symtab (abfd
))
11662 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
11664 sym_hashes
= elf_sym_hashes (abfd
);
11665 sym_hashes_end
= sym_hashes
+ extsymcount
;
11667 /* Hunt down the child symbol, which is in this section at the same
11668 offset as the relocation. */
11669 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
11671 if ((child
= *search
) != NULL
11672 && (child
->root
.type
== bfd_link_hash_defined
11673 || child
->root
.type
== bfd_link_hash_defweak
)
11674 && child
->root
.u
.def
.section
== sec
11675 && child
->root
.u
.def
.value
== offset
)
11679 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
11680 abfd
, sec
, (unsigned long) offset
);
11681 bfd_set_error (bfd_error_invalid_operation
);
11685 if (!child
->vtable
)
11687 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
11688 if (!child
->vtable
)
11693 /* This *should* only be the absolute section. It could potentially
11694 be that someone has defined a non-global vtable though, which
11695 would be bad. It isn't worth paging in the local symbols to be
11696 sure though; that case should simply be handled by the assembler. */
11698 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
11701 child
->vtable
->parent
= h
;
11706 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11709 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
11710 asection
*sec ATTRIBUTE_UNUSED
,
11711 struct elf_link_hash_entry
*h
,
11714 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11715 unsigned int log_file_align
= bed
->s
->log_file_align
;
11719 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
11724 if (addend
>= h
->vtable
->size
)
11726 size_t size
, bytes
, file_align
;
11727 bfd_boolean
*ptr
= h
->vtable
->used
;
11729 /* While the symbol is undefined, we have to be prepared to handle
11731 file_align
= 1 << log_file_align
;
11732 if (h
->root
.type
== bfd_link_hash_undefined
)
11733 size
= addend
+ file_align
;
11737 if (addend
>= size
)
11739 /* Oops! We've got a reference past the defined end of
11740 the table. This is probably a bug -- shall we warn? */
11741 size
= addend
+ file_align
;
11744 size
= (size
+ file_align
- 1) & -file_align
;
11746 /* Allocate one extra entry for use as a "done" flag for the
11747 consolidation pass. */
11748 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
11752 ptr
= bfd_realloc (ptr
- 1, bytes
);
11758 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
11759 * sizeof (bfd_boolean
));
11760 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
11764 ptr
= bfd_zmalloc (bytes
);
11769 /* And arrange for that done flag to be at index -1. */
11770 h
->vtable
->used
= ptr
+ 1;
11771 h
->vtable
->size
= size
;
11774 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11779 struct alloc_got_off_arg
{
11781 unsigned int got_elt_size
;
11784 /* We need a special top-level link routine to convert got reference counts
11785 to real got offsets. */
11788 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11790 struct alloc_got_off_arg
*gofarg
= arg
;
11792 if (h
->root
.type
== bfd_link_hash_warning
)
11793 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11795 if (h
->got
.refcount
> 0)
11797 h
->got
.offset
= gofarg
->gotoff
;
11798 gofarg
->gotoff
+= gofarg
->got_elt_size
;
11801 h
->got
.offset
= (bfd_vma
) -1;
11806 /* And an accompanying bit to work out final got entry offsets once
11807 we're done. Should be called from final_link. */
11810 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11811 struct bfd_link_info
*info
)
11814 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11816 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
11817 struct alloc_got_off_arg gofarg
;
11819 if (! is_elf_hash_table (info
->hash
))
11822 /* The GOT offset is relative to the .got section, but the GOT header is
11823 put into the .got.plt section, if the backend uses it. */
11824 if (bed
->want_got_plt
)
11827 gotoff
= bed
->got_header_size
;
11829 /* Do the local .got entries first. */
11830 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11832 bfd_signed_vma
*local_got
;
11833 bfd_size_type j
, locsymcount
;
11834 Elf_Internal_Shdr
*symtab_hdr
;
11836 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11839 local_got
= elf_local_got_refcounts (i
);
11843 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11844 if (elf_bad_symtab (i
))
11845 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11847 locsymcount
= symtab_hdr
->sh_info
;
11849 for (j
= 0; j
< locsymcount
; ++j
)
11851 if (local_got
[j
] > 0)
11853 local_got
[j
] = gotoff
;
11854 gotoff
+= got_elt_size
;
11857 local_got
[j
] = (bfd_vma
) -1;
11861 /* Then the global .got entries. .plt refcounts are handled by
11862 adjust_dynamic_symbol */
11863 gofarg
.gotoff
= gotoff
;
11864 gofarg
.got_elt_size
= got_elt_size
;
11865 elf_link_hash_traverse (elf_hash_table (info
),
11866 elf_gc_allocate_got_offsets
,
11871 /* Many folk need no more in the way of final link than this, once
11872 got entry reference counting is enabled. */
11875 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11877 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11880 /* Invoke the regular ELF backend linker to do all the work. */
11881 return bfd_elf_final_link (abfd
, info
);
11885 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11887 struct elf_reloc_cookie
*rcookie
= cookie
;
11889 if (rcookie
->bad_symtab
)
11890 rcookie
->rel
= rcookie
->rels
;
11892 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11894 unsigned long r_symndx
;
11896 if (! rcookie
->bad_symtab
)
11897 if (rcookie
->rel
->r_offset
> offset
)
11899 if (rcookie
->rel
->r_offset
!= offset
)
11902 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11903 if (r_symndx
== SHN_UNDEF
)
11906 if (r_symndx
>= rcookie
->locsymcount
11907 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11909 struct elf_link_hash_entry
*h
;
11911 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11913 while (h
->root
.type
== bfd_link_hash_indirect
11914 || h
->root
.type
== bfd_link_hash_warning
)
11915 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11917 if ((h
->root
.type
== bfd_link_hash_defined
11918 || h
->root
.type
== bfd_link_hash_defweak
)
11919 && elf_discarded_section (h
->root
.u
.def
.section
))
11926 /* It's not a relocation against a global symbol,
11927 but it could be a relocation against a local
11928 symbol for a discarded section. */
11930 Elf_Internal_Sym
*isym
;
11932 /* Need to: get the symbol; get the section. */
11933 isym
= &rcookie
->locsyms
[r_symndx
];
11934 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
11936 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11937 if (isec
!= NULL
&& elf_discarded_section (isec
))
11946 /* Discard unneeded references to discarded sections.
11947 Returns TRUE if any section's size was changed. */
11948 /* This function assumes that the relocations are in sorted order,
11949 which is true for all known assemblers. */
11952 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11954 struct elf_reloc_cookie cookie
;
11955 asection
*stab
, *eh
;
11956 Elf_Internal_Shdr
*symtab_hdr
;
11957 const struct elf_backend_data
*bed
;
11959 unsigned int count
;
11960 bfd_boolean ret
= FALSE
;
11962 if (info
->traditional_format
11963 || !is_elf_hash_table (info
->hash
))
11966 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11968 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11971 bed
= get_elf_backend_data (abfd
);
11973 if ((abfd
->flags
& DYNAMIC
) != 0)
11977 if (!info
->relocatable
)
11979 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
11982 || bfd_is_abs_section (eh
->output_section
)))
11986 stab
= bfd_get_section_by_name (abfd
, ".stab");
11988 && (stab
->size
== 0
11989 || bfd_is_abs_section (stab
->output_section
)
11990 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
11995 && bed
->elf_backend_discard_info
== NULL
)
11998 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11999 cookie
.abfd
= abfd
;
12000 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
12001 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
12002 if (cookie
.bad_symtab
)
12004 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12005 cookie
.extsymoff
= 0;
12009 cookie
.locsymcount
= symtab_hdr
->sh_info
;
12010 cookie
.extsymoff
= symtab_hdr
->sh_info
;
12013 if (bed
->s
->arch_size
== 32)
12014 cookie
.r_sym_shift
= 8;
12016 cookie
.r_sym_shift
= 32;
12018 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12019 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
12021 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12022 cookie
.locsymcount
, 0,
12024 if (cookie
.locsyms
== NULL
)
12026 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12033 cookie
.rels
= NULL
;
12034 count
= stab
->reloc_count
;
12036 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
12037 info
->keep_memory
);
12038 if (cookie
.rels
!= NULL
)
12040 cookie
.rel
= cookie
.rels
;
12041 cookie
.relend
= cookie
.rels
;
12042 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
12043 if (_bfd_discard_section_stabs (abfd
, stab
,
12044 elf_section_data (stab
)->sec_info
,
12045 bfd_elf_reloc_symbol_deleted_p
,
12048 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
12049 free (cookie
.rels
);
12055 cookie
.rels
= NULL
;
12056 count
= eh
->reloc_count
;
12058 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
12059 info
->keep_memory
);
12060 cookie
.rel
= cookie
.rels
;
12061 cookie
.relend
= cookie
.rels
;
12062 if (cookie
.rels
!= NULL
)
12063 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
12065 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
12066 bfd_elf_reloc_symbol_deleted_p
,
12070 if (cookie
.rels
!= NULL
12071 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
12072 free (cookie
.rels
);
12075 if (bed
->elf_backend_discard_info
!= NULL
12076 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
12079 if (cookie
.locsyms
!= NULL
12080 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
12082 if (! info
->keep_memory
)
12083 free (cookie
.locsyms
);
12085 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
12089 if (info
->eh_frame_hdr
12090 && !info
->relocatable
12091 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12098 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
12099 struct bfd_link_info
*info
)
12102 const char *name
, *p
;
12103 struct bfd_section_already_linked
*l
;
12104 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12106 if (sec
->output_section
== bfd_abs_section_ptr
)
12109 flags
= sec
->flags
;
12111 /* Return if it isn't a linkonce section. A comdat group section
12112 also has SEC_LINK_ONCE set. */
12113 if ((flags
& SEC_LINK_ONCE
) == 0)
12116 /* Don't put group member sections on our list of already linked
12117 sections. They are handled as a group via their group section. */
12118 if (elf_sec_group (sec
) != NULL
)
12121 /* FIXME: When doing a relocatable link, we may have trouble
12122 copying relocations in other sections that refer to local symbols
12123 in the section being discarded. Those relocations will have to
12124 be converted somehow; as of this writing I'm not sure that any of
12125 the backends handle that correctly.
12127 It is tempting to instead not discard link once sections when
12128 doing a relocatable link (technically, they should be discarded
12129 whenever we are building constructors). However, that fails,
12130 because the linker winds up combining all the link once sections
12131 into a single large link once section, which defeats the purpose
12132 of having link once sections in the first place.
12134 Also, not merging link once sections in a relocatable link
12135 causes trouble for MIPS ELF, which relies on link once semantics
12136 to handle the .reginfo section correctly. */
12138 name
= bfd_get_section_name (abfd
, sec
);
12140 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12141 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12146 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
12148 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12150 /* We may have 2 different types of sections on the list: group
12151 sections and linkonce sections. Match like sections. */
12152 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12153 && strcmp (name
, l
->sec
->name
) == 0
12154 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
12156 /* The section has already been linked. See if we should
12157 issue a warning. */
12158 switch (flags
& SEC_LINK_DUPLICATES
)
12163 case SEC_LINK_DUPLICATES_DISCARD
:
12166 case SEC_LINK_DUPLICATES_ONE_ONLY
:
12167 (*_bfd_error_handler
)
12168 (_("%B: ignoring duplicate section `%A'"),
12172 case SEC_LINK_DUPLICATES_SAME_SIZE
:
12173 if (sec
->size
!= l
->sec
->size
)
12174 (*_bfd_error_handler
)
12175 (_("%B: duplicate section `%A' has different size"),
12179 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
12180 if (sec
->size
!= l
->sec
->size
)
12181 (*_bfd_error_handler
)
12182 (_("%B: duplicate section `%A' has different size"),
12184 else if (sec
->size
!= 0)
12186 bfd_byte
*sec_contents
, *l_sec_contents
;
12188 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
12189 (*_bfd_error_handler
)
12190 (_("%B: warning: could not read contents of section `%A'"),
12192 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
12194 (*_bfd_error_handler
)
12195 (_("%B: warning: could not read contents of section `%A'"),
12196 l
->sec
->owner
, l
->sec
);
12197 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
12198 (*_bfd_error_handler
)
12199 (_("%B: warning: duplicate section `%A' has different contents"),
12203 free (sec_contents
);
12204 if (l_sec_contents
)
12205 free (l_sec_contents
);
12210 /* Set the output_section field so that lang_add_section
12211 does not create a lang_input_section structure for this
12212 section. Since there might be a symbol in the section
12213 being discarded, we must retain a pointer to the section
12214 which we are really going to use. */
12215 sec
->output_section
= bfd_abs_section_ptr
;
12216 sec
->kept_section
= l
->sec
;
12218 if (flags
& SEC_GROUP
)
12220 asection
*first
= elf_next_in_group (sec
);
12221 asection
*s
= first
;
12225 s
->output_section
= bfd_abs_section_ptr
;
12226 /* Record which group discards it. */
12227 s
->kept_section
= l
->sec
;
12228 s
= elf_next_in_group (s
);
12229 /* These lists are circular. */
12239 /* A single member comdat group section may be discarded by a
12240 linkonce section and vice versa. */
12242 if ((flags
& SEC_GROUP
) != 0)
12244 asection
*first
= elf_next_in_group (sec
);
12246 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12247 /* Check this single member group against linkonce sections. */
12248 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12249 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12250 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
12251 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12253 first
->output_section
= bfd_abs_section_ptr
;
12254 first
->kept_section
= l
->sec
;
12255 sec
->output_section
= bfd_abs_section_ptr
;
12260 /* Check this linkonce section against single member groups. */
12261 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12262 if (l
->sec
->flags
& SEC_GROUP
)
12264 asection
*first
= elf_next_in_group (l
->sec
);
12267 && elf_next_in_group (first
) == first
12268 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12270 sec
->output_section
= bfd_abs_section_ptr
;
12271 sec
->kept_section
= first
;
12276 /* This is the first section with this name. Record it. */
12277 if (! bfd_section_already_linked_table_insert (already_linked_list
, sec
))
12278 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E"));
12282 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12284 return sym
->st_shndx
== SHN_COMMON
;
12288 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12294 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12296 return bfd_com_section_ptr
;