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
);
1811 /* Note that we are copying a string pointer here, and testing it
1812 above. If bfd_elf_string_from_elf_section is ever changed to
1813 discard the string data when low in memory, this will have to be
1815 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1817 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1818 a
->vna_nextptr
= t
->vn_auxptr
;
1820 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1823 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1830 /* Figure out appropriate versions for all the symbols. We may not
1831 have the version number script until we have read all of the input
1832 files, so until that point we don't know which symbols should be
1833 local. This function is called via elf_link_hash_traverse. */
1836 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1838 struct elf_assign_sym_version_info
*sinfo
;
1839 struct bfd_link_info
*info
;
1840 const struct elf_backend_data
*bed
;
1841 struct elf_info_failed eif
;
1848 if (h
->root
.type
== bfd_link_hash_warning
)
1849 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1851 /* Fix the symbol flags. */
1854 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1857 sinfo
->failed
= TRUE
;
1861 /* We only need version numbers for symbols defined in regular
1863 if (!h
->def_regular
)
1866 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1867 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1868 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1870 struct bfd_elf_version_tree
*t
;
1875 /* There are two consecutive ELF_VER_CHR characters if this is
1876 not a hidden symbol. */
1878 if (*p
== ELF_VER_CHR
)
1884 /* If there is no version string, we can just return out. */
1892 /* Look for the version. If we find it, it is no longer weak. */
1893 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1895 if (strcmp (t
->name
, p
) == 0)
1899 struct bfd_elf_version_expr
*d
;
1901 len
= p
- h
->root
.root
.string
;
1902 alc
= bfd_malloc (len
);
1905 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1906 alc
[len
- 1] = '\0';
1907 if (alc
[len
- 2] == ELF_VER_CHR
)
1908 alc
[len
- 2] = '\0';
1910 h
->verinfo
.vertree
= t
;
1914 if (t
->globals
.list
!= NULL
)
1915 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1917 /* See if there is anything to force this symbol to
1919 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1921 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1924 && ! info
->export_dynamic
)
1925 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1933 /* If we are building an application, we need to create a
1934 version node for this version. */
1935 if (t
== NULL
&& info
->executable
)
1937 struct bfd_elf_version_tree
**pp
;
1940 /* If we aren't going to export this symbol, we don't need
1941 to worry about it. */
1942 if (h
->dynindx
== -1)
1946 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1949 sinfo
->failed
= TRUE
;
1954 t
->name_indx
= (unsigned int) -1;
1958 /* Don't count anonymous version tag. */
1959 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1961 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1963 t
->vernum
= version_index
;
1967 h
->verinfo
.vertree
= t
;
1971 /* We could not find the version for a symbol when
1972 generating a shared archive. Return an error. */
1973 (*_bfd_error_handler
)
1974 (_("%B: version node not found for symbol %s"),
1975 sinfo
->output_bfd
, h
->root
.root
.string
);
1976 bfd_set_error (bfd_error_bad_value
);
1977 sinfo
->failed
= TRUE
;
1985 /* If we don't have a version for this symbol, see if we can find
1987 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1989 struct bfd_elf_version_tree
*t
;
1990 struct bfd_elf_version_tree
*local_ver
;
1991 struct bfd_elf_version_expr
*d
;
1993 /* See if can find what version this symbol is in. If the
1994 symbol is supposed to be local, then don't actually register
1997 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1999 if (t
->globals
.list
!= NULL
)
2001 bfd_boolean matched
;
2005 while ((d
= (*t
->match
) (&t
->globals
, d
,
2006 h
->root
.root
.string
)) != NULL
)
2011 /* There is a version without definition. Make
2012 the symbol the default definition for this
2014 h
->verinfo
.vertree
= t
;
2022 /* There is no undefined version for this symbol. Hide the
2024 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2027 if (t
->locals
.list
!= NULL
)
2030 while ((d
= (*t
->match
) (&t
->locals
, d
,
2031 h
->root
.root
.string
)) != NULL
)
2034 /* If the match is "*", keep looking for a more
2035 explicit, perhaps even global, match.
2036 XXX: Shouldn't this be !d->wildcard instead? */
2037 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2046 if (local_ver
!= NULL
)
2048 h
->verinfo
.vertree
= local_ver
;
2049 if (h
->dynindx
!= -1
2050 && ! info
->export_dynamic
)
2052 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2060 /* Read and swap the relocs from the section indicated by SHDR. This
2061 may be either a REL or a RELA section. The relocations are
2062 translated into RELA relocations and stored in INTERNAL_RELOCS,
2063 which should have already been allocated to contain enough space.
2064 The EXTERNAL_RELOCS are a buffer where the external form of the
2065 relocations should be stored.
2067 Returns FALSE if something goes wrong. */
2070 elf_link_read_relocs_from_section (bfd
*abfd
,
2072 Elf_Internal_Shdr
*shdr
,
2073 void *external_relocs
,
2074 Elf_Internal_Rela
*internal_relocs
)
2076 const struct elf_backend_data
*bed
;
2077 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2078 const bfd_byte
*erela
;
2079 const bfd_byte
*erelaend
;
2080 Elf_Internal_Rela
*irela
;
2081 Elf_Internal_Shdr
*symtab_hdr
;
2084 /* Position ourselves at the start of the section. */
2085 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2088 /* Read the relocations. */
2089 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2092 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2093 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2095 bed
= get_elf_backend_data (abfd
);
2097 /* Convert the external relocations to the internal format. */
2098 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2099 swap_in
= bed
->s
->swap_reloc_in
;
2100 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2101 swap_in
= bed
->s
->swap_reloca_in
;
2104 bfd_set_error (bfd_error_wrong_format
);
2108 erela
= external_relocs
;
2109 erelaend
= erela
+ shdr
->sh_size
;
2110 irela
= internal_relocs
;
2111 while (erela
< erelaend
)
2115 (*swap_in
) (abfd
, erela
, irela
);
2116 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2117 if (bed
->s
->arch_size
== 64)
2119 if ((size_t) r_symndx
>= nsyms
)
2121 (*_bfd_error_handler
)
2122 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2123 " for offset 0x%lx in section `%A'"),
2125 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2126 bfd_set_error (bfd_error_bad_value
);
2129 irela
+= bed
->s
->int_rels_per_ext_rel
;
2130 erela
+= shdr
->sh_entsize
;
2136 /* Read and swap the relocs for a section O. They may have been
2137 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2138 not NULL, they are used as buffers to read into. They are known to
2139 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2140 the return value is allocated using either malloc or bfd_alloc,
2141 according to the KEEP_MEMORY argument. If O has two relocation
2142 sections (both REL and RELA relocations), then the REL_HDR
2143 relocations will appear first in INTERNAL_RELOCS, followed by the
2144 REL_HDR2 relocations. */
2147 _bfd_elf_link_read_relocs (bfd
*abfd
,
2149 void *external_relocs
,
2150 Elf_Internal_Rela
*internal_relocs
,
2151 bfd_boolean keep_memory
)
2153 Elf_Internal_Shdr
*rel_hdr
;
2154 void *alloc1
= NULL
;
2155 Elf_Internal_Rela
*alloc2
= NULL
;
2156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2158 if (elf_section_data (o
)->relocs
!= NULL
)
2159 return elf_section_data (o
)->relocs
;
2161 if (o
->reloc_count
== 0)
2164 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2166 if (internal_relocs
== NULL
)
2170 size
= o
->reloc_count
;
2171 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2173 internal_relocs
= bfd_alloc (abfd
, size
);
2175 internal_relocs
= alloc2
= bfd_malloc (size
);
2176 if (internal_relocs
== NULL
)
2180 if (external_relocs
== NULL
)
2182 bfd_size_type size
= rel_hdr
->sh_size
;
2184 if (elf_section_data (o
)->rel_hdr2
)
2185 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2186 alloc1
= bfd_malloc (size
);
2189 external_relocs
= alloc1
;
2192 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2196 if (elf_section_data (o
)->rel_hdr2
2197 && (!elf_link_read_relocs_from_section
2199 elf_section_data (o
)->rel_hdr2
,
2200 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2201 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2202 * bed
->s
->int_rels_per_ext_rel
))))
2205 /* Cache the results for next time, if we can. */
2207 elf_section_data (o
)->relocs
= internal_relocs
;
2212 /* Don't free alloc2, since if it was allocated we are passing it
2213 back (under the name of internal_relocs). */
2215 return internal_relocs
;
2225 /* Compute the size of, and allocate space for, REL_HDR which is the
2226 section header for a section containing relocations for O. */
2229 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2230 Elf_Internal_Shdr
*rel_hdr
,
2233 bfd_size_type reloc_count
;
2234 bfd_size_type num_rel_hashes
;
2236 /* Figure out how many relocations there will be. */
2237 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2238 reloc_count
= elf_section_data (o
)->rel_count
;
2240 reloc_count
= elf_section_data (o
)->rel_count2
;
2242 num_rel_hashes
= o
->reloc_count
;
2243 if (num_rel_hashes
< reloc_count
)
2244 num_rel_hashes
= reloc_count
;
2246 /* That allows us to calculate the size of the section. */
2247 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2249 /* The contents field must last into write_object_contents, so we
2250 allocate it with bfd_alloc rather than malloc. Also since we
2251 cannot be sure that the contents will actually be filled in,
2252 we zero the allocated space. */
2253 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2254 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2257 /* We only allocate one set of hash entries, so we only do it the
2258 first time we are called. */
2259 if (elf_section_data (o
)->rel_hashes
== NULL
2262 struct elf_link_hash_entry
**p
;
2264 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2268 elf_section_data (o
)->rel_hashes
= p
;
2274 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2275 originated from the section given by INPUT_REL_HDR) to the
2279 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2280 asection
*input_section
,
2281 Elf_Internal_Shdr
*input_rel_hdr
,
2282 Elf_Internal_Rela
*internal_relocs
,
2283 struct elf_link_hash_entry
**rel_hash
2286 Elf_Internal_Rela
*irela
;
2287 Elf_Internal_Rela
*irelaend
;
2289 Elf_Internal_Shdr
*output_rel_hdr
;
2290 asection
*output_section
;
2291 unsigned int *rel_countp
= NULL
;
2292 const struct elf_backend_data
*bed
;
2293 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2295 output_section
= input_section
->output_section
;
2296 output_rel_hdr
= NULL
;
2298 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2299 == input_rel_hdr
->sh_entsize
)
2301 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2302 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2304 else if (elf_section_data (output_section
)->rel_hdr2
2305 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2306 == input_rel_hdr
->sh_entsize
))
2308 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2309 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2313 (*_bfd_error_handler
)
2314 (_("%B: relocation size mismatch in %B section %A"),
2315 output_bfd
, input_section
->owner
, input_section
);
2316 bfd_set_error (bfd_error_wrong_object_format
);
2320 bed
= get_elf_backend_data (output_bfd
);
2321 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2322 swap_out
= bed
->s
->swap_reloc_out
;
2323 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2324 swap_out
= bed
->s
->swap_reloca_out
;
2328 erel
= output_rel_hdr
->contents
;
2329 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2330 irela
= internal_relocs
;
2331 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2332 * bed
->s
->int_rels_per_ext_rel
);
2333 while (irela
< irelaend
)
2335 (*swap_out
) (output_bfd
, irela
, erel
);
2336 irela
+= bed
->s
->int_rels_per_ext_rel
;
2337 erel
+= input_rel_hdr
->sh_entsize
;
2340 /* Bump the counter, so that we know where to add the next set of
2342 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2347 /* Make weak undefined symbols in PIE dynamic. */
2350 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2351 struct elf_link_hash_entry
*h
)
2355 && h
->root
.type
== bfd_link_hash_undefweak
)
2356 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2361 /* Fix up the flags for a symbol. This handles various cases which
2362 can only be fixed after all the input files are seen. This is
2363 currently called by both adjust_dynamic_symbol and
2364 assign_sym_version, which is unnecessary but perhaps more robust in
2365 the face of future changes. */
2368 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2369 struct elf_info_failed
*eif
)
2371 const struct elf_backend_data
*bed
= NULL
;
2373 /* If this symbol was mentioned in a non-ELF file, try to set
2374 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2375 permit a non-ELF file to correctly refer to a symbol defined in
2376 an ELF dynamic object. */
2379 while (h
->root
.type
== bfd_link_hash_indirect
)
2380 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2382 if (h
->root
.type
!= bfd_link_hash_defined
2383 && h
->root
.type
!= bfd_link_hash_defweak
)
2386 h
->ref_regular_nonweak
= 1;
2390 if (h
->root
.u
.def
.section
->owner
!= NULL
2391 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2392 == bfd_target_elf_flavour
))
2395 h
->ref_regular_nonweak
= 1;
2401 if (h
->dynindx
== -1
2405 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2414 /* Unfortunately, NON_ELF is only correct if the symbol
2415 was first seen in a non-ELF file. Fortunately, if the symbol
2416 was first seen in an ELF file, we're probably OK unless the
2417 symbol was defined in a non-ELF file. Catch that case here.
2418 FIXME: We're still in trouble if the symbol was first seen in
2419 a dynamic object, and then later in a non-ELF regular object. */
2420 if ((h
->root
.type
== bfd_link_hash_defined
2421 || h
->root
.type
== bfd_link_hash_defweak
)
2423 && (h
->root
.u
.def
.section
->owner
!= NULL
2424 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2425 != bfd_target_elf_flavour
)
2426 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2427 && !h
->def_dynamic
)))
2431 /* Backend specific symbol fixup. */
2432 if (elf_hash_table (eif
->info
)->dynobj
)
2434 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2435 if (bed
->elf_backend_fixup_symbol
2436 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2440 /* If this is a final link, and the symbol was defined as a common
2441 symbol in a regular object file, and there was no definition in
2442 any dynamic object, then the linker will have allocated space for
2443 the symbol in a common section but the DEF_REGULAR
2444 flag will not have been set. */
2445 if (h
->root
.type
== bfd_link_hash_defined
2449 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2452 /* If -Bsymbolic was used (which means to bind references to global
2453 symbols to the definition within the shared object), and this
2454 symbol was defined in a regular object, then it actually doesn't
2455 need a PLT entry. Likewise, if the symbol has non-default
2456 visibility. If the symbol has hidden or internal visibility, we
2457 will force it local. */
2459 && eif
->info
->shared
2460 && is_elf_hash_table (eif
->info
->hash
)
2461 && (SYMBOLIC_BIND (eif
->info
, h
)
2462 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2465 bfd_boolean force_local
;
2467 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2468 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2469 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2472 /* If a weak undefined symbol has non-default visibility, we also
2473 hide it from the dynamic linker. */
2474 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2475 && h
->root
.type
== bfd_link_hash_undefweak
)
2477 const struct elf_backend_data
*bed
;
2478 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2479 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2482 /* If this is a weak defined symbol in a dynamic object, and we know
2483 the real definition in the dynamic object, copy interesting flags
2484 over to the real definition. */
2485 if (h
->u
.weakdef
!= NULL
)
2487 struct elf_link_hash_entry
*weakdef
;
2489 weakdef
= h
->u
.weakdef
;
2490 if (h
->root
.type
== bfd_link_hash_indirect
)
2491 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2493 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2494 || h
->root
.type
== bfd_link_hash_defweak
);
2495 BFD_ASSERT (weakdef
->def_dynamic
);
2497 /* If the real definition is defined by a regular object file,
2498 don't do anything special. See the longer description in
2499 _bfd_elf_adjust_dynamic_symbol, below. */
2500 if (weakdef
->def_regular
)
2501 h
->u
.weakdef
= NULL
;
2504 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2505 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2506 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2513 /* Make the backend pick a good value for a dynamic symbol. This is
2514 called via elf_link_hash_traverse, and also calls itself
2518 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2520 struct elf_info_failed
*eif
= data
;
2522 const struct elf_backend_data
*bed
;
2524 if (! is_elf_hash_table (eif
->info
->hash
))
2527 if (h
->root
.type
== bfd_link_hash_warning
)
2529 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2530 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2532 /* When warning symbols are created, they **replace** the "real"
2533 entry in the hash table, thus we never get to see the real
2534 symbol in a hash traversal. So look at it now. */
2535 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2538 /* Ignore indirect symbols. These are added by the versioning code. */
2539 if (h
->root
.type
== bfd_link_hash_indirect
)
2542 /* Fix the symbol flags. */
2543 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2546 /* If this symbol does not require a PLT entry, and it is not
2547 defined by a dynamic object, or is not referenced by a regular
2548 object, ignore it. We do have to handle a weak defined symbol,
2549 even if no regular object refers to it, if we decided to add it
2550 to the dynamic symbol table. FIXME: Do we normally need to worry
2551 about symbols which are defined by one dynamic object and
2552 referenced by another one? */
2557 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2559 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2563 /* If we've already adjusted this symbol, don't do it again. This
2564 can happen via a recursive call. */
2565 if (h
->dynamic_adjusted
)
2568 /* Don't look at this symbol again. Note that we must set this
2569 after checking the above conditions, because we may look at a
2570 symbol once, decide not to do anything, and then get called
2571 recursively later after REF_REGULAR is set below. */
2572 h
->dynamic_adjusted
= 1;
2574 /* If this is a weak definition, and we know a real definition, and
2575 the real symbol is not itself defined by a regular object file,
2576 then get a good value for the real definition. We handle the
2577 real symbol first, for the convenience of the backend routine.
2579 Note that there is a confusing case here. If the real definition
2580 is defined by a regular object file, we don't get the real symbol
2581 from the dynamic object, but we do get the weak symbol. If the
2582 processor backend uses a COPY reloc, then if some routine in the
2583 dynamic object changes the real symbol, we will not see that
2584 change in the corresponding weak symbol. This is the way other
2585 ELF linkers work as well, and seems to be a result of the shared
2588 I will clarify this issue. Most SVR4 shared libraries define the
2589 variable _timezone and define timezone as a weak synonym. The
2590 tzset call changes _timezone. If you write
2591 extern int timezone;
2593 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2594 you might expect that, since timezone is a synonym for _timezone,
2595 the same number will print both times. However, if the processor
2596 backend uses a COPY reloc, then actually timezone will be copied
2597 into your process image, and, since you define _timezone
2598 yourself, _timezone will not. Thus timezone and _timezone will
2599 wind up at different memory locations. The tzset call will set
2600 _timezone, leaving timezone unchanged. */
2602 if (h
->u
.weakdef
!= NULL
)
2604 /* If we get to this point, we know there is an implicit
2605 reference by a regular object file via the weak symbol H.
2606 FIXME: Is this really true? What if the traversal finds
2607 H->U.WEAKDEF before it finds H? */
2608 h
->u
.weakdef
->ref_regular
= 1;
2610 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2614 /* If a symbol has no type and no size and does not require a PLT
2615 entry, then we are probably about to do the wrong thing here: we
2616 are probably going to create a COPY reloc for an empty object.
2617 This case can arise when a shared object is built with assembly
2618 code, and the assembly code fails to set the symbol type. */
2620 && h
->type
== STT_NOTYPE
2622 (*_bfd_error_handler
)
2623 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2624 h
->root
.root
.string
);
2626 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2627 bed
= get_elf_backend_data (dynobj
);
2628 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2637 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2641 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2644 unsigned int power_of_two
;
2646 asection
*sec
= h
->root
.u
.def
.section
;
2648 /* The section aligment of definition is the maximum alignment
2649 requirement of symbols defined in the section. Since we don't
2650 know the symbol alignment requirement, we start with the
2651 maximum alignment and check low bits of the symbol address
2652 for the minimum alignment. */
2653 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2654 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2655 while ((h
->root
.u
.def
.value
& mask
) != 0)
2661 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2664 /* Adjust the section alignment if needed. */
2665 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2670 /* We make sure that the symbol will be aligned properly. */
2671 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2673 /* Define the symbol as being at this point in DYNBSS. */
2674 h
->root
.u
.def
.section
= dynbss
;
2675 h
->root
.u
.def
.value
= dynbss
->size
;
2677 /* Increment the size of DYNBSS to make room for the symbol. */
2678 dynbss
->size
+= h
->size
;
2683 /* Adjust all external symbols pointing into SEC_MERGE sections
2684 to reflect the object merging within the sections. */
2687 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2691 if (h
->root
.type
== bfd_link_hash_warning
)
2692 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2694 if ((h
->root
.type
== bfd_link_hash_defined
2695 || h
->root
.type
== bfd_link_hash_defweak
)
2696 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2697 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2699 bfd
*output_bfd
= data
;
2701 h
->root
.u
.def
.value
=
2702 _bfd_merged_section_offset (output_bfd
,
2703 &h
->root
.u
.def
.section
,
2704 elf_section_data (sec
)->sec_info
,
2705 h
->root
.u
.def
.value
);
2711 /* Returns false if the symbol referred to by H should be considered
2712 to resolve local to the current module, and true if it should be
2713 considered to bind dynamically. */
2716 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2717 struct bfd_link_info
*info
,
2718 bfd_boolean ignore_protected
)
2720 bfd_boolean binding_stays_local_p
;
2721 const struct elf_backend_data
*bed
;
2722 struct elf_link_hash_table
*hash_table
;
2727 while (h
->root
.type
== bfd_link_hash_indirect
2728 || h
->root
.type
== bfd_link_hash_warning
)
2729 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2731 /* If it was forced local, then clearly it's not dynamic. */
2732 if (h
->dynindx
== -1)
2734 if (h
->forced_local
)
2737 /* Identify the cases where name binding rules say that a
2738 visible symbol resolves locally. */
2739 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2741 switch (ELF_ST_VISIBILITY (h
->other
))
2748 hash_table
= elf_hash_table (info
);
2749 if (!is_elf_hash_table (hash_table
))
2752 bed
= get_elf_backend_data (hash_table
->dynobj
);
2754 /* Proper resolution for function pointer equality may require
2755 that these symbols perhaps be resolved dynamically, even though
2756 we should be resolving them to the current module. */
2757 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2758 binding_stays_local_p
= TRUE
;
2765 /* If it isn't defined locally, then clearly it's dynamic. */
2766 if (!h
->def_regular
)
2769 /* Otherwise, the symbol is dynamic if binding rules don't tell
2770 us that it remains local. */
2771 return !binding_stays_local_p
;
2774 /* Return true if the symbol referred to by H should be considered
2775 to resolve local to the current module, and false otherwise. Differs
2776 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2777 undefined symbols and weak symbols. */
2780 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2781 struct bfd_link_info
*info
,
2782 bfd_boolean local_protected
)
2784 const struct elf_backend_data
*bed
;
2785 struct elf_link_hash_table
*hash_table
;
2787 /* If it's a local sym, of course we resolve locally. */
2791 /* Common symbols that become definitions don't get the DEF_REGULAR
2792 flag set, so test it first, and don't bail out. */
2793 if (ELF_COMMON_DEF_P (h
))
2795 /* If we don't have a definition in a regular file, then we can't
2796 resolve locally. The sym is either undefined or dynamic. */
2797 else if (!h
->def_regular
)
2800 /* Forced local symbols resolve locally. */
2801 if (h
->forced_local
)
2804 /* As do non-dynamic symbols. */
2805 if (h
->dynindx
== -1)
2808 /* At this point, we know the symbol is defined and dynamic. In an
2809 executable it must resolve locally, likewise when building symbolic
2810 shared libraries. */
2811 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2814 /* Now deal with defined dynamic symbols in shared libraries. Ones
2815 with default visibility might not resolve locally. */
2816 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2819 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2820 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2823 hash_table
= elf_hash_table (info
);
2824 if (!is_elf_hash_table (hash_table
))
2827 bed
= get_elf_backend_data (hash_table
->dynobj
);
2829 /* STV_PROTECTED non-function symbols are local. */
2830 if (!bed
->is_function_type (h
->type
))
2833 /* Function pointer equality tests may require that STV_PROTECTED
2834 symbols be treated as dynamic symbols, even when we know that the
2835 dynamic linker will resolve them locally. */
2836 return local_protected
;
2839 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2840 aligned. Returns the first TLS output section. */
2842 struct bfd_section
*
2843 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2845 struct bfd_section
*sec
, *tls
;
2846 unsigned int align
= 0;
2848 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2849 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2853 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2854 if (sec
->alignment_power
> align
)
2855 align
= sec
->alignment_power
;
2857 elf_hash_table (info
)->tls_sec
= tls
;
2859 /* Ensure the alignment of the first section is the largest alignment,
2860 so that the tls segment starts aligned. */
2862 tls
->alignment_power
= align
;
2867 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2869 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2870 Elf_Internal_Sym
*sym
)
2872 const struct elf_backend_data
*bed
;
2874 /* Local symbols do not count, but target specific ones might. */
2875 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2876 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2879 bed
= get_elf_backend_data (abfd
);
2880 /* Function symbols do not count. */
2881 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2884 /* If the section is undefined, then so is the symbol. */
2885 if (sym
->st_shndx
== SHN_UNDEF
)
2888 /* If the symbol is defined in the common section, then
2889 it is a common definition and so does not count. */
2890 if (bed
->common_definition (sym
))
2893 /* If the symbol is in a target specific section then we
2894 must rely upon the backend to tell us what it is. */
2895 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2896 /* FIXME - this function is not coded yet:
2898 return _bfd_is_global_symbol_definition (abfd, sym);
2900 Instead for now assume that the definition is not global,
2901 Even if this is wrong, at least the linker will behave
2902 in the same way that it used to do. */
2908 /* Search the symbol table of the archive element of the archive ABFD
2909 whose archive map contains a mention of SYMDEF, and determine if
2910 the symbol is defined in this element. */
2912 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2914 Elf_Internal_Shdr
* hdr
;
2915 bfd_size_type symcount
;
2916 bfd_size_type extsymcount
;
2917 bfd_size_type extsymoff
;
2918 Elf_Internal_Sym
*isymbuf
;
2919 Elf_Internal_Sym
*isym
;
2920 Elf_Internal_Sym
*isymend
;
2923 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2927 if (! bfd_check_format (abfd
, bfd_object
))
2930 /* If we have already included the element containing this symbol in the
2931 link then we do not need to include it again. Just claim that any symbol
2932 it contains is not a definition, so that our caller will not decide to
2933 (re)include this element. */
2934 if (abfd
->archive_pass
)
2937 /* Select the appropriate symbol table. */
2938 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2939 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2941 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2943 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2945 /* The sh_info field of the symtab header tells us where the
2946 external symbols start. We don't care about the local symbols. */
2947 if (elf_bad_symtab (abfd
))
2949 extsymcount
= symcount
;
2954 extsymcount
= symcount
- hdr
->sh_info
;
2955 extsymoff
= hdr
->sh_info
;
2958 if (extsymcount
== 0)
2961 /* Read in the symbol table. */
2962 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2964 if (isymbuf
== NULL
)
2967 /* Scan the symbol table looking for SYMDEF. */
2969 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2973 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2978 if (strcmp (name
, symdef
->name
) == 0)
2980 result
= is_global_data_symbol_definition (abfd
, isym
);
2990 /* Add an entry to the .dynamic table. */
2993 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2997 struct elf_link_hash_table
*hash_table
;
2998 const struct elf_backend_data
*bed
;
3000 bfd_size_type newsize
;
3001 bfd_byte
*newcontents
;
3002 Elf_Internal_Dyn dyn
;
3004 hash_table
= elf_hash_table (info
);
3005 if (! is_elf_hash_table (hash_table
))
3008 bed
= get_elf_backend_data (hash_table
->dynobj
);
3009 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3010 BFD_ASSERT (s
!= NULL
);
3012 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3013 newcontents
= bfd_realloc (s
->contents
, newsize
);
3014 if (newcontents
== NULL
)
3018 dyn
.d_un
.d_val
= val
;
3019 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3022 s
->contents
= newcontents
;
3027 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3028 otherwise just check whether one already exists. Returns -1 on error,
3029 1 if a DT_NEEDED tag already exists, and 0 on success. */
3032 elf_add_dt_needed_tag (bfd
*abfd
,
3033 struct bfd_link_info
*info
,
3037 struct elf_link_hash_table
*hash_table
;
3038 bfd_size_type oldsize
;
3039 bfd_size_type strindex
;
3041 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3044 hash_table
= elf_hash_table (info
);
3045 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3046 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3047 if (strindex
== (bfd_size_type
) -1)
3050 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3053 const struct elf_backend_data
*bed
;
3056 bed
= get_elf_backend_data (hash_table
->dynobj
);
3057 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3059 for (extdyn
= sdyn
->contents
;
3060 extdyn
< sdyn
->contents
+ sdyn
->size
;
3061 extdyn
+= bed
->s
->sizeof_dyn
)
3063 Elf_Internal_Dyn dyn
;
3065 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3066 if (dyn
.d_tag
== DT_NEEDED
3067 && dyn
.d_un
.d_val
== strindex
)
3069 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3077 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3080 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3084 /* We were just checking for existence of the tag. */
3085 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3090 /* Sort symbol by value and section. */
3092 elf_sort_symbol (const void *arg1
, const void *arg2
)
3094 const struct elf_link_hash_entry
*h1
;
3095 const struct elf_link_hash_entry
*h2
;
3096 bfd_signed_vma vdiff
;
3098 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3099 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3100 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3102 return vdiff
> 0 ? 1 : -1;
3105 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3107 return sdiff
> 0 ? 1 : -1;
3112 /* This function is used to adjust offsets into .dynstr for
3113 dynamic symbols. This is called via elf_link_hash_traverse. */
3116 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3118 struct elf_strtab_hash
*dynstr
= data
;
3120 if (h
->root
.type
== bfd_link_hash_warning
)
3121 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3123 if (h
->dynindx
!= -1)
3124 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3128 /* Assign string offsets in .dynstr, update all structures referencing
3132 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3134 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3135 struct elf_link_local_dynamic_entry
*entry
;
3136 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3137 bfd
*dynobj
= hash_table
->dynobj
;
3140 const struct elf_backend_data
*bed
;
3143 _bfd_elf_strtab_finalize (dynstr
);
3144 size
= _bfd_elf_strtab_size (dynstr
);
3146 bed
= get_elf_backend_data (dynobj
);
3147 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3148 BFD_ASSERT (sdyn
!= NULL
);
3150 /* Update all .dynamic entries referencing .dynstr strings. */
3151 for (extdyn
= sdyn
->contents
;
3152 extdyn
< sdyn
->contents
+ sdyn
->size
;
3153 extdyn
+= bed
->s
->sizeof_dyn
)
3155 Elf_Internal_Dyn dyn
;
3157 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3161 dyn
.d_un
.d_val
= size
;
3169 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3174 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3177 /* Now update local dynamic symbols. */
3178 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3179 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3180 entry
->isym
.st_name
);
3182 /* And the rest of dynamic symbols. */
3183 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3185 /* Adjust version definitions. */
3186 if (elf_tdata (output_bfd
)->cverdefs
)
3191 Elf_Internal_Verdef def
;
3192 Elf_Internal_Verdaux defaux
;
3194 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3198 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3200 p
+= sizeof (Elf_External_Verdef
);
3201 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3203 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3205 _bfd_elf_swap_verdaux_in (output_bfd
,
3206 (Elf_External_Verdaux
*) p
, &defaux
);
3207 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3209 _bfd_elf_swap_verdaux_out (output_bfd
,
3210 &defaux
, (Elf_External_Verdaux
*) p
);
3211 p
+= sizeof (Elf_External_Verdaux
);
3214 while (def
.vd_next
);
3217 /* Adjust version references. */
3218 if (elf_tdata (output_bfd
)->verref
)
3223 Elf_Internal_Verneed need
;
3224 Elf_Internal_Vernaux needaux
;
3226 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3230 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3232 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3233 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3234 (Elf_External_Verneed
*) p
);
3235 p
+= sizeof (Elf_External_Verneed
);
3236 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3238 _bfd_elf_swap_vernaux_in (output_bfd
,
3239 (Elf_External_Vernaux
*) p
, &needaux
);
3240 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3242 _bfd_elf_swap_vernaux_out (output_bfd
,
3244 (Elf_External_Vernaux
*) p
);
3245 p
+= sizeof (Elf_External_Vernaux
);
3248 while (need
.vn_next
);
3254 /* Add symbols from an ELF object file to the linker hash table. */
3257 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3259 Elf_Internal_Shdr
*hdr
;
3260 bfd_size_type symcount
;
3261 bfd_size_type extsymcount
;
3262 bfd_size_type extsymoff
;
3263 struct elf_link_hash_entry
**sym_hash
;
3264 bfd_boolean dynamic
;
3265 Elf_External_Versym
*extversym
= NULL
;
3266 Elf_External_Versym
*ever
;
3267 struct elf_link_hash_entry
*weaks
;
3268 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3269 bfd_size_type nondeflt_vers_cnt
= 0;
3270 Elf_Internal_Sym
*isymbuf
= NULL
;
3271 Elf_Internal_Sym
*isym
;
3272 Elf_Internal_Sym
*isymend
;
3273 const struct elf_backend_data
*bed
;
3274 bfd_boolean add_needed
;
3275 struct elf_link_hash_table
*htab
;
3277 void *alloc_mark
= NULL
;
3278 struct bfd_hash_entry
**old_table
= NULL
;
3279 unsigned int old_size
= 0;
3280 unsigned int old_count
= 0;
3281 void *old_tab
= NULL
;
3284 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3285 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3286 long old_dynsymcount
= 0;
3288 size_t hashsize
= 0;
3290 htab
= elf_hash_table (info
);
3291 bed
= get_elf_backend_data (abfd
);
3293 if ((abfd
->flags
& DYNAMIC
) == 0)
3299 /* You can't use -r against a dynamic object. Also, there's no
3300 hope of using a dynamic object which does not exactly match
3301 the format of the output file. */
3302 if (info
->relocatable
3303 || !is_elf_hash_table (htab
)
3304 || htab
->root
.creator
!= abfd
->xvec
)
3306 if (info
->relocatable
)
3307 bfd_set_error (bfd_error_invalid_operation
);
3309 bfd_set_error (bfd_error_wrong_format
);
3314 /* As a GNU extension, any input sections which are named
3315 .gnu.warning.SYMBOL are treated as warning symbols for the given
3316 symbol. This differs from .gnu.warning sections, which generate
3317 warnings when they are included in an output file. */
3318 if (info
->executable
)
3322 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3326 name
= bfd_get_section_name (abfd
, s
);
3327 if (CONST_STRNEQ (name
, ".gnu.warning."))
3332 name
+= sizeof ".gnu.warning." - 1;
3334 /* If this is a shared object, then look up the symbol
3335 in the hash table. If it is there, and it is already
3336 been defined, then we will not be using the entry
3337 from this shared object, so we don't need to warn.
3338 FIXME: If we see the definition in a regular object
3339 later on, we will warn, but we shouldn't. The only
3340 fix is to keep track of what warnings we are supposed
3341 to emit, and then handle them all at the end of the
3345 struct elf_link_hash_entry
*h
;
3347 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3349 /* FIXME: What about bfd_link_hash_common? */
3351 && (h
->root
.type
== bfd_link_hash_defined
3352 || h
->root
.type
== bfd_link_hash_defweak
))
3354 /* We don't want to issue this warning. Clobber
3355 the section size so that the warning does not
3356 get copied into the output file. */
3363 msg
= bfd_alloc (abfd
, sz
+ 1);
3367 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3372 if (! (_bfd_generic_link_add_one_symbol
3373 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3374 FALSE
, bed
->collect
, NULL
)))
3377 if (! info
->relocatable
)
3379 /* Clobber the section size so that the warning does
3380 not get copied into the output file. */
3383 /* Also set SEC_EXCLUDE, so that symbols defined in
3384 the warning section don't get copied to the output. */
3385 s
->flags
|= SEC_EXCLUDE
;
3394 /* If we are creating a shared library, create all the dynamic
3395 sections immediately. We need to attach them to something,
3396 so we attach them to this BFD, provided it is the right
3397 format. FIXME: If there are no input BFD's of the same
3398 format as the output, we can't make a shared library. */
3400 && is_elf_hash_table (htab
)
3401 && htab
->root
.creator
== abfd
->xvec
3402 && !htab
->dynamic_sections_created
)
3404 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3408 else if (!is_elf_hash_table (htab
))
3413 const char *soname
= NULL
;
3414 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3417 /* ld --just-symbols and dynamic objects don't mix very well.
3418 ld shouldn't allow it. */
3419 if ((s
= abfd
->sections
) != NULL
3420 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3423 /* If this dynamic lib was specified on the command line with
3424 --as-needed in effect, then we don't want to add a DT_NEEDED
3425 tag unless the lib is actually used. Similary for libs brought
3426 in by another lib's DT_NEEDED. When --no-add-needed is used
3427 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3428 any dynamic library in DT_NEEDED tags in the dynamic lib at
3430 add_needed
= (elf_dyn_lib_class (abfd
)
3431 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3432 | DYN_NO_NEEDED
)) == 0;
3434 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3440 unsigned long shlink
;
3442 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3443 goto error_free_dyn
;
3445 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3447 goto error_free_dyn
;
3448 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3450 for (extdyn
= dynbuf
;
3451 extdyn
< dynbuf
+ s
->size
;
3452 extdyn
+= bed
->s
->sizeof_dyn
)
3454 Elf_Internal_Dyn dyn
;
3456 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3457 if (dyn
.d_tag
== DT_SONAME
)
3459 unsigned int tagv
= dyn
.d_un
.d_val
;
3460 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3462 goto error_free_dyn
;
3464 if (dyn
.d_tag
== DT_NEEDED
)
3466 struct bfd_link_needed_list
*n
, **pn
;
3468 unsigned int tagv
= dyn
.d_un
.d_val
;
3470 amt
= sizeof (struct bfd_link_needed_list
);
3471 n
= bfd_alloc (abfd
, amt
);
3472 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3473 if (n
== NULL
|| fnm
== NULL
)
3474 goto error_free_dyn
;
3475 amt
= strlen (fnm
) + 1;
3476 anm
= bfd_alloc (abfd
, amt
);
3478 goto error_free_dyn
;
3479 memcpy (anm
, fnm
, amt
);
3483 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3487 if (dyn
.d_tag
== DT_RUNPATH
)
3489 struct bfd_link_needed_list
*n
, **pn
;
3491 unsigned int tagv
= dyn
.d_un
.d_val
;
3493 amt
= sizeof (struct bfd_link_needed_list
);
3494 n
= bfd_alloc (abfd
, amt
);
3495 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3496 if (n
== NULL
|| fnm
== NULL
)
3497 goto error_free_dyn
;
3498 amt
= strlen (fnm
) + 1;
3499 anm
= bfd_alloc (abfd
, amt
);
3501 goto error_free_dyn
;
3502 memcpy (anm
, fnm
, amt
);
3506 for (pn
= & runpath
;
3512 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3513 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3515 struct bfd_link_needed_list
*n
, **pn
;
3517 unsigned int tagv
= dyn
.d_un
.d_val
;
3519 amt
= sizeof (struct bfd_link_needed_list
);
3520 n
= bfd_alloc (abfd
, amt
);
3521 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3522 if (n
== NULL
|| fnm
== NULL
)
3523 goto error_free_dyn
;
3524 amt
= strlen (fnm
) + 1;
3525 anm
= bfd_alloc (abfd
, amt
);
3532 memcpy (anm
, fnm
, amt
);
3547 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3548 frees all more recently bfd_alloc'd blocks as well. */
3554 struct bfd_link_needed_list
**pn
;
3555 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3560 /* We do not want to include any of the sections in a dynamic
3561 object in the output file. We hack by simply clobbering the
3562 list of sections in the BFD. This could be handled more
3563 cleanly by, say, a new section flag; the existing
3564 SEC_NEVER_LOAD flag is not the one we want, because that one
3565 still implies that the section takes up space in the output
3567 bfd_section_list_clear (abfd
);
3569 /* Find the name to use in a DT_NEEDED entry that refers to this
3570 object. If the object has a DT_SONAME entry, we use it.
3571 Otherwise, if the generic linker stuck something in
3572 elf_dt_name, we use that. Otherwise, we just use the file
3574 if (soname
== NULL
|| *soname
== '\0')
3576 soname
= elf_dt_name (abfd
);
3577 if (soname
== NULL
|| *soname
== '\0')
3578 soname
= bfd_get_filename (abfd
);
3581 /* Save the SONAME because sometimes the linker emulation code
3582 will need to know it. */
3583 elf_dt_name (abfd
) = soname
;
3585 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3589 /* If we have already included this dynamic object in the
3590 link, just ignore it. There is no reason to include a
3591 particular dynamic object more than once. */
3596 /* If this is a dynamic object, we always link against the .dynsym
3597 symbol table, not the .symtab symbol table. The dynamic linker
3598 will only see the .dynsym symbol table, so there is no reason to
3599 look at .symtab for a dynamic object. */
3601 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3602 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3604 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3606 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3608 /* The sh_info field of the symtab header tells us where the
3609 external symbols start. We don't care about the local symbols at
3611 if (elf_bad_symtab (abfd
))
3613 extsymcount
= symcount
;
3618 extsymcount
= symcount
- hdr
->sh_info
;
3619 extsymoff
= hdr
->sh_info
;
3623 if (extsymcount
!= 0)
3625 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3627 if (isymbuf
== NULL
)
3630 /* We store a pointer to the hash table entry for each external
3632 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3633 sym_hash
= bfd_alloc (abfd
, amt
);
3634 if (sym_hash
== NULL
)
3635 goto error_free_sym
;
3636 elf_sym_hashes (abfd
) = sym_hash
;
3641 /* Read in any version definitions. */
3642 if (!_bfd_elf_slurp_version_tables (abfd
,
3643 info
->default_imported_symver
))
3644 goto error_free_sym
;
3646 /* Read in the symbol versions, but don't bother to convert them
3647 to internal format. */
3648 if (elf_dynversym (abfd
) != 0)
3650 Elf_Internal_Shdr
*versymhdr
;
3652 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3653 extversym
= bfd_malloc (versymhdr
->sh_size
);
3654 if (extversym
== NULL
)
3655 goto error_free_sym
;
3656 amt
= versymhdr
->sh_size
;
3657 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3658 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3659 goto error_free_vers
;
3663 /* If we are loading an as-needed shared lib, save the symbol table
3664 state before we start adding symbols. If the lib turns out
3665 to be unneeded, restore the state. */
3666 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3671 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3673 struct bfd_hash_entry
*p
;
3674 struct elf_link_hash_entry
*h
;
3676 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3678 h
= (struct elf_link_hash_entry
*) p
;
3679 entsize
+= htab
->root
.table
.entsize
;
3680 if (h
->root
.type
== bfd_link_hash_warning
)
3681 entsize
+= htab
->root
.table
.entsize
;
3685 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3686 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3687 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3688 if (old_tab
== NULL
)
3689 goto error_free_vers
;
3691 /* Remember the current objalloc pointer, so that all mem for
3692 symbols added can later be reclaimed. */
3693 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3694 if (alloc_mark
== NULL
)
3695 goto error_free_vers
;
3697 /* Make a special call to the linker "notice" function to
3698 tell it that we are about to handle an as-needed lib. */
3699 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3701 goto error_free_vers
;
3703 /* Clone the symbol table and sym hashes. Remember some
3704 pointers into the symbol table, and dynamic symbol count. */
3705 old_hash
= (char *) old_tab
+ tabsize
;
3706 old_ent
= (char *) old_hash
+ hashsize
;
3707 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3708 memcpy (old_hash
, sym_hash
, hashsize
);
3709 old_undefs
= htab
->root
.undefs
;
3710 old_undefs_tail
= htab
->root
.undefs_tail
;
3711 old_table
= htab
->root
.table
.table
;
3712 old_size
= htab
->root
.table
.size
;
3713 old_count
= htab
->root
.table
.count
;
3714 old_dynsymcount
= htab
->dynsymcount
;
3716 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3718 struct bfd_hash_entry
*p
;
3719 struct elf_link_hash_entry
*h
;
3721 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3723 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3724 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3725 h
= (struct elf_link_hash_entry
*) p
;
3726 if (h
->root
.type
== bfd_link_hash_warning
)
3728 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3729 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3736 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3737 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3739 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3743 asection
*sec
, *new_sec
;
3746 struct elf_link_hash_entry
*h
;
3747 bfd_boolean definition
;
3748 bfd_boolean size_change_ok
;
3749 bfd_boolean type_change_ok
;
3750 bfd_boolean new_weakdef
;
3751 bfd_boolean override
;
3753 unsigned int old_alignment
;
3758 flags
= BSF_NO_FLAGS
;
3760 value
= isym
->st_value
;
3762 common
= bed
->common_definition (isym
);
3764 bind
= ELF_ST_BIND (isym
->st_info
);
3765 if (bind
== STB_LOCAL
)
3767 /* This should be impossible, since ELF requires that all
3768 global symbols follow all local symbols, and that sh_info
3769 point to the first global symbol. Unfortunately, Irix 5
3773 else if (bind
== STB_GLOBAL
)
3775 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3778 else if (bind
== STB_WEAK
)
3782 /* Leave it up to the processor backend. */
3785 if (isym
->st_shndx
== SHN_UNDEF
)
3786 sec
= bfd_und_section_ptr
;
3787 else if (isym
->st_shndx
< SHN_LORESERVE
3788 || isym
->st_shndx
> SHN_HIRESERVE
)
3790 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3792 sec
= bfd_abs_section_ptr
;
3793 else if (sec
->kept_section
)
3795 /* Symbols from discarded section are undefined. We keep
3797 sec
= bfd_und_section_ptr
;
3798 isym
->st_shndx
= SHN_UNDEF
;
3800 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3803 else if (isym
->st_shndx
== SHN_ABS
)
3804 sec
= bfd_abs_section_ptr
;
3805 else if (isym
->st_shndx
== SHN_COMMON
)
3807 sec
= bfd_com_section_ptr
;
3808 /* What ELF calls the size we call the value. What ELF
3809 calls the value we call the alignment. */
3810 value
= isym
->st_size
;
3814 /* Leave it up to the processor backend. */
3817 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3820 goto error_free_vers
;
3822 if (isym
->st_shndx
== SHN_COMMON
3823 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3824 && !info
->relocatable
)
3826 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3830 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3833 | SEC_LINKER_CREATED
3834 | SEC_THREAD_LOCAL
));
3836 goto error_free_vers
;
3840 else if (bed
->elf_add_symbol_hook
)
3842 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3844 goto error_free_vers
;
3846 /* The hook function sets the name to NULL if this symbol
3847 should be skipped for some reason. */
3852 /* Sanity check that all possibilities were handled. */
3855 bfd_set_error (bfd_error_bad_value
);
3856 goto error_free_vers
;
3859 if (bfd_is_und_section (sec
)
3860 || bfd_is_com_section (sec
))
3865 size_change_ok
= FALSE
;
3866 type_change_ok
= bed
->type_change_ok
;
3871 if (is_elf_hash_table (htab
))
3873 Elf_Internal_Versym iver
;
3874 unsigned int vernum
= 0;
3879 if (info
->default_imported_symver
)
3880 /* Use the default symbol version created earlier. */
3881 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3886 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3888 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3890 /* If this is a hidden symbol, or if it is not version
3891 1, we append the version name to the symbol name.
3892 However, we do not modify a non-hidden absolute symbol
3893 if it is not a function, because it might be the version
3894 symbol itself. FIXME: What if it isn't? */
3895 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3897 && (!bfd_is_abs_section (sec
)
3898 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3901 size_t namelen
, verlen
, newlen
;
3904 if (isym
->st_shndx
!= SHN_UNDEF
)
3906 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3908 else if (vernum
> 1)
3910 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3916 (*_bfd_error_handler
)
3917 (_("%B: %s: invalid version %u (max %d)"),
3919 elf_tdata (abfd
)->cverdefs
);
3920 bfd_set_error (bfd_error_bad_value
);
3921 goto error_free_vers
;
3926 /* We cannot simply test for the number of
3927 entries in the VERNEED section since the
3928 numbers for the needed versions do not start
3930 Elf_Internal_Verneed
*t
;
3933 for (t
= elf_tdata (abfd
)->verref
;
3937 Elf_Internal_Vernaux
*a
;
3939 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3941 if (a
->vna_other
== vernum
)
3943 verstr
= a
->vna_nodename
;
3952 (*_bfd_error_handler
)
3953 (_("%B: %s: invalid needed version %d"),
3954 abfd
, name
, vernum
);
3955 bfd_set_error (bfd_error_bad_value
);
3956 goto error_free_vers
;
3960 namelen
= strlen (name
);
3961 verlen
= strlen (verstr
);
3962 newlen
= namelen
+ verlen
+ 2;
3963 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3964 && isym
->st_shndx
!= SHN_UNDEF
)
3967 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3968 if (newname
== NULL
)
3969 goto error_free_vers
;
3970 memcpy (newname
, name
, namelen
);
3971 p
= newname
+ namelen
;
3973 /* If this is a defined non-hidden version symbol,
3974 we add another @ to the name. This indicates the
3975 default version of the symbol. */
3976 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3977 && isym
->st_shndx
!= SHN_UNDEF
)
3979 memcpy (p
, verstr
, verlen
+ 1);
3984 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3985 &value
, &old_alignment
,
3986 sym_hash
, &skip
, &override
,
3987 &type_change_ok
, &size_change_ok
))
3988 goto error_free_vers
;
3997 while (h
->root
.type
== bfd_link_hash_indirect
3998 || h
->root
.type
== bfd_link_hash_warning
)
3999 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4001 /* Remember the old alignment if this is a common symbol, so
4002 that we don't reduce the alignment later on. We can't
4003 check later, because _bfd_generic_link_add_one_symbol
4004 will set a default for the alignment which we want to
4005 override. We also remember the old bfd where the existing
4006 definition comes from. */
4007 switch (h
->root
.type
)
4012 case bfd_link_hash_defined
:
4013 case bfd_link_hash_defweak
:
4014 old_bfd
= h
->root
.u
.def
.section
->owner
;
4017 case bfd_link_hash_common
:
4018 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4019 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4023 if (elf_tdata (abfd
)->verdef
!= NULL
4027 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4030 if (! (_bfd_generic_link_add_one_symbol
4031 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4032 (struct bfd_link_hash_entry
**) sym_hash
)))
4033 goto error_free_vers
;
4036 while (h
->root
.type
== bfd_link_hash_indirect
4037 || h
->root
.type
== bfd_link_hash_warning
)
4038 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4041 new_weakdef
= FALSE
;
4044 && (flags
& BSF_WEAK
) != 0
4045 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4046 && is_elf_hash_table (htab
)
4047 && h
->u
.weakdef
== NULL
)
4049 /* Keep a list of all weak defined non function symbols from
4050 a dynamic object, using the weakdef field. Later in this
4051 function we will set the weakdef field to the correct
4052 value. We only put non-function symbols from dynamic
4053 objects on this list, because that happens to be the only
4054 time we need to know the normal symbol corresponding to a
4055 weak symbol, and the information is time consuming to
4056 figure out. If the weakdef field is not already NULL,
4057 then this symbol was already defined by some previous
4058 dynamic object, and we will be using that previous
4059 definition anyhow. */
4061 h
->u
.weakdef
= weaks
;
4066 /* Set the alignment of a common symbol. */
4067 if ((common
|| bfd_is_com_section (sec
))
4068 && h
->root
.type
== bfd_link_hash_common
)
4073 align
= bfd_log2 (isym
->st_value
);
4076 /* The new symbol is a common symbol in a shared object.
4077 We need to get the alignment from the section. */
4078 align
= new_sec
->alignment_power
;
4080 if (align
> old_alignment
4081 /* Permit an alignment power of zero if an alignment of one
4082 is specified and no other alignments have been specified. */
4083 || (isym
->st_value
== 1 && old_alignment
== 0))
4084 h
->root
.u
.c
.p
->alignment_power
= align
;
4086 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4089 if (is_elf_hash_table (htab
))
4093 /* Check the alignment when a common symbol is involved. This
4094 can change when a common symbol is overridden by a normal
4095 definition or a common symbol is ignored due to the old
4096 normal definition. We need to make sure the maximum
4097 alignment is maintained. */
4098 if ((old_alignment
|| common
)
4099 && h
->root
.type
!= bfd_link_hash_common
)
4101 unsigned int common_align
;
4102 unsigned int normal_align
;
4103 unsigned int symbol_align
;
4107 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4108 if (h
->root
.u
.def
.section
->owner
!= NULL
4109 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4111 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4112 if (normal_align
> symbol_align
)
4113 normal_align
= symbol_align
;
4116 normal_align
= symbol_align
;
4120 common_align
= old_alignment
;
4121 common_bfd
= old_bfd
;
4126 common_align
= bfd_log2 (isym
->st_value
);
4128 normal_bfd
= old_bfd
;
4131 if (normal_align
< common_align
)
4133 /* PR binutils/2735 */
4134 if (normal_bfd
== NULL
)
4135 (*_bfd_error_handler
)
4136 (_("Warning: alignment %u of common symbol `%s' in %B"
4137 " is greater than the alignment (%u) of its section %A"),
4138 common_bfd
, h
->root
.u
.def
.section
,
4139 1 << common_align
, name
, 1 << normal_align
);
4141 (*_bfd_error_handler
)
4142 (_("Warning: alignment %u of symbol `%s' in %B"
4143 " is smaller than %u in %B"),
4144 normal_bfd
, common_bfd
,
4145 1 << normal_align
, name
, 1 << common_align
);
4149 /* Remember the symbol size if it isn't undefined. */
4150 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4151 && (definition
|| h
->size
== 0))
4154 && h
->size
!= isym
->st_size
4155 && ! size_change_ok
)
4156 (*_bfd_error_handler
)
4157 (_("Warning: size of symbol `%s' changed"
4158 " from %lu in %B to %lu in %B"),
4160 name
, (unsigned long) h
->size
,
4161 (unsigned long) isym
->st_size
);
4163 h
->size
= isym
->st_size
;
4166 /* If this is a common symbol, then we always want H->SIZE
4167 to be the size of the common symbol. The code just above
4168 won't fix the size if a common symbol becomes larger. We
4169 don't warn about a size change here, because that is
4170 covered by --warn-common. Allow changed between different
4172 if (h
->root
.type
== bfd_link_hash_common
)
4173 h
->size
= h
->root
.u
.c
.size
;
4175 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4176 && (definition
|| h
->type
== STT_NOTYPE
))
4178 if (h
->type
!= STT_NOTYPE
4179 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4180 && ! type_change_ok
)
4181 (*_bfd_error_handler
)
4182 (_("Warning: type of symbol `%s' changed"
4183 " from %d to %d in %B"),
4184 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4186 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4189 /* If st_other has a processor-specific meaning, specific
4190 code might be needed here. We never merge the visibility
4191 attribute with the one from a dynamic object. */
4192 if (bed
->elf_backend_merge_symbol_attribute
)
4193 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4196 /* If this symbol has default visibility and the user has requested
4197 we not re-export it, then mark it as hidden. */
4198 if (definition
&& !dynamic
4200 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4201 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4202 isym
->st_other
= (STV_HIDDEN
4203 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4205 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4207 unsigned char hvis
, symvis
, other
, nvis
;
4209 /* Only merge the visibility. Leave the remainder of the
4210 st_other field to elf_backend_merge_symbol_attribute. */
4211 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4213 /* Combine visibilities, using the most constraining one. */
4214 hvis
= ELF_ST_VISIBILITY (h
->other
);
4215 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4221 nvis
= hvis
< symvis
? hvis
: symvis
;
4223 h
->other
= other
| nvis
;
4226 /* Set a flag in the hash table entry indicating the type of
4227 reference or definition we just found. Keep a count of
4228 the number of dynamic symbols we find. A dynamic symbol
4229 is one which is referenced or defined by both a regular
4230 object and a shared object. */
4237 if (bind
!= STB_WEAK
)
4238 h
->ref_regular_nonweak
= 1;
4242 if (! info
->executable
4255 || (h
->u
.weakdef
!= NULL
4257 && h
->u
.weakdef
->dynindx
!= -1))
4261 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4263 /* We don't want to make debug symbol dynamic. */
4264 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4268 /* Check to see if we need to add an indirect symbol for
4269 the default name. */
4270 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4271 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4272 &sec
, &value
, &dynsym
,
4274 goto error_free_vers
;
4276 if (definition
&& !dynamic
)
4278 char *p
= strchr (name
, ELF_VER_CHR
);
4279 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4281 /* Queue non-default versions so that .symver x, x@FOO
4282 aliases can be checked. */
4285 amt
= ((isymend
- isym
+ 1)
4286 * sizeof (struct elf_link_hash_entry
*));
4287 nondeflt_vers
= bfd_malloc (amt
);
4289 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4293 if (dynsym
&& h
->dynindx
== -1)
4295 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4296 goto error_free_vers
;
4297 if (h
->u
.weakdef
!= NULL
4299 && h
->u
.weakdef
->dynindx
== -1)
4301 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4302 goto error_free_vers
;
4305 else if (dynsym
&& h
->dynindx
!= -1)
4306 /* If the symbol already has a dynamic index, but
4307 visibility says it should not be visible, turn it into
4309 switch (ELF_ST_VISIBILITY (h
->other
))
4313 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4324 const char *soname
= elf_dt_name (abfd
);
4326 /* A symbol from a library loaded via DT_NEEDED of some
4327 other library is referenced by a regular object.
4328 Add a DT_NEEDED entry for it. Issue an error if
4329 --no-add-needed is used. */
4330 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4332 (*_bfd_error_handler
)
4333 (_("%s: invalid DSO for symbol `%s' definition"),
4335 bfd_set_error (bfd_error_bad_value
);
4336 goto error_free_vers
;
4339 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4342 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4344 goto error_free_vers
;
4346 BFD_ASSERT (ret
== 0);
4351 if (extversym
!= NULL
)
4357 if (isymbuf
!= NULL
)
4363 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4367 /* Restore the symbol table. */
4368 if (bed
->as_needed_cleanup
)
4369 (*bed
->as_needed_cleanup
) (abfd
, info
);
4370 old_hash
= (char *) old_tab
+ tabsize
;
4371 old_ent
= (char *) old_hash
+ hashsize
;
4372 sym_hash
= elf_sym_hashes (abfd
);
4373 htab
->root
.table
.table
= old_table
;
4374 htab
->root
.table
.size
= old_size
;
4375 htab
->root
.table
.count
= old_count
;
4376 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4377 memcpy (sym_hash
, old_hash
, hashsize
);
4378 htab
->root
.undefs
= old_undefs
;
4379 htab
->root
.undefs_tail
= old_undefs_tail
;
4380 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4382 struct bfd_hash_entry
*p
;
4383 struct elf_link_hash_entry
*h
;
4385 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4387 h
= (struct elf_link_hash_entry
*) p
;
4388 if (h
->root
.type
== bfd_link_hash_warning
)
4389 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4390 if (h
->dynindx
>= old_dynsymcount
)
4391 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4393 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4394 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4395 h
= (struct elf_link_hash_entry
*) p
;
4396 if (h
->root
.type
== bfd_link_hash_warning
)
4398 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4399 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4404 /* Make a special call to the linker "notice" function to
4405 tell it that symbols added for crefs may need to be removed. */
4406 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4408 goto error_free_vers
;
4411 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4413 if (nondeflt_vers
!= NULL
)
4414 free (nondeflt_vers
);
4418 if (old_tab
!= NULL
)
4420 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4422 goto error_free_vers
;
4427 /* Now that all the symbols from this input file are created, handle
4428 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4429 if (nondeflt_vers
!= NULL
)
4431 bfd_size_type cnt
, symidx
;
4433 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4435 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4436 char *shortname
, *p
;
4438 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4440 || (h
->root
.type
!= bfd_link_hash_defined
4441 && h
->root
.type
!= bfd_link_hash_defweak
))
4444 amt
= p
- h
->root
.root
.string
;
4445 shortname
= bfd_malloc (amt
+ 1);
4446 memcpy (shortname
, h
->root
.root
.string
, amt
);
4447 shortname
[amt
] = '\0';
4449 hi
= (struct elf_link_hash_entry
*)
4450 bfd_link_hash_lookup (&htab
->root
, shortname
,
4451 FALSE
, FALSE
, FALSE
);
4453 && hi
->root
.type
== h
->root
.type
4454 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4455 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4457 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4458 hi
->root
.type
= bfd_link_hash_indirect
;
4459 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4460 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4461 sym_hash
= elf_sym_hashes (abfd
);
4463 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4464 if (sym_hash
[symidx
] == hi
)
4466 sym_hash
[symidx
] = h
;
4472 free (nondeflt_vers
);
4473 nondeflt_vers
= NULL
;
4476 /* Now set the weakdefs field correctly for all the weak defined
4477 symbols we found. The only way to do this is to search all the
4478 symbols. Since we only need the information for non functions in
4479 dynamic objects, that's the only time we actually put anything on
4480 the list WEAKS. We need this information so that if a regular
4481 object refers to a symbol defined weakly in a dynamic object, the
4482 real symbol in the dynamic object is also put in the dynamic
4483 symbols; we also must arrange for both symbols to point to the
4484 same memory location. We could handle the general case of symbol
4485 aliasing, but a general symbol alias can only be generated in
4486 assembler code, handling it correctly would be very time
4487 consuming, and other ELF linkers don't handle general aliasing
4491 struct elf_link_hash_entry
**hpp
;
4492 struct elf_link_hash_entry
**hppend
;
4493 struct elf_link_hash_entry
**sorted_sym_hash
;
4494 struct elf_link_hash_entry
*h
;
4497 /* Since we have to search the whole symbol list for each weak
4498 defined symbol, search time for N weak defined symbols will be
4499 O(N^2). Binary search will cut it down to O(NlogN). */
4500 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4501 sorted_sym_hash
= bfd_malloc (amt
);
4502 if (sorted_sym_hash
== NULL
)
4504 sym_hash
= sorted_sym_hash
;
4505 hpp
= elf_sym_hashes (abfd
);
4506 hppend
= hpp
+ extsymcount
;
4508 for (; hpp
< hppend
; hpp
++)
4512 && h
->root
.type
== bfd_link_hash_defined
4513 && !bed
->is_function_type (h
->type
))
4521 qsort (sorted_sym_hash
, sym_count
,
4522 sizeof (struct elf_link_hash_entry
*),
4525 while (weaks
!= NULL
)
4527 struct elf_link_hash_entry
*hlook
;
4534 weaks
= hlook
->u
.weakdef
;
4535 hlook
->u
.weakdef
= NULL
;
4537 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4538 || hlook
->root
.type
== bfd_link_hash_defweak
4539 || hlook
->root
.type
== bfd_link_hash_common
4540 || hlook
->root
.type
== bfd_link_hash_indirect
);
4541 slook
= hlook
->root
.u
.def
.section
;
4542 vlook
= hlook
->root
.u
.def
.value
;
4549 bfd_signed_vma vdiff
;
4551 h
= sorted_sym_hash
[idx
];
4552 vdiff
= vlook
- h
->root
.u
.def
.value
;
4559 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4572 /* We didn't find a value/section match. */
4576 for (i
= ilook
; i
< sym_count
; i
++)
4578 h
= sorted_sym_hash
[i
];
4580 /* Stop if value or section doesn't match. */
4581 if (h
->root
.u
.def
.value
!= vlook
4582 || h
->root
.u
.def
.section
!= slook
)
4584 else if (h
!= hlook
)
4586 hlook
->u
.weakdef
= h
;
4588 /* If the weak definition is in the list of dynamic
4589 symbols, make sure the real definition is put
4591 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4593 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4597 /* If the real definition is in the list of dynamic
4598 symbols, make sure the weak definition is put
4599 there as well. If we don't do this, then the
4600 dynamic loader might not merge the entries for the
4601 real definition and the weak definition. */
4602 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4604 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4612 free (sorted_sym_hash
);
4615 if (bed
->check_directives
)
4616 (*bed
->check_directives
) (abfd
, info
);
4618 /* If this object is the same format as the output object, and it is
4619 not a shared library, then let the backend look through the
4622 This is required to build global offset table entries and to
4623 arrange for dynamic relocs. It is not required for the
4624 particular common case of linking non PIC code, even when linking
4625 against shared libraries, but unfortunately there is no way of
4626 knowing whether an object file has been compiled PIC or not.
4627 Looking through the relocs is not particularly time consuming.
4628 The problem is that we must either (1) keep the relocs in memory,
4629 which causes the linker to require additional runtime memory or
4630 (2) read the relocs twice from the input file, which wastes time.
4631 This would be a good case for using mmap.
4633 I have no idea how to handle linking PIC code into a file of a
4634 different format. It probably can't be done. */
4636 && is_elf_hash_table (htab
)
4637 && htab
->root
.creator
== abfd
->xvec
4638 && bed
->check_relocs
!= NULL
)
4642 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4644 Elf_Internal_Rela
*internal_relocs
;
4647 if ((o
->flags
& SEC_RELOC
) == 0
4648 || o
->reloc_count
== 0
4649 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4650 && (o
->flags
& SEC_DEBUGGING
) != 0)
4651 || bfd_is_abs_section (o
->output_section
))
4654 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4656 if (internal_relocs
== NULL
)
4659 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4661 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4662 free (internal_relocs
);
4669 /* If this is a non-traditional link, try to optimize the handling
4670 of the .stab/.stabstr sections. */
4672 && ! info
->traditional_format
4673 && is_elf_hash_table (htab
)
4674 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4678 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4679 if (stabstr
!= NULL
)
4681 bfd_size_type string_offset
= 0;
4684 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4685 if (CONST_STRNEQ (stab
->name
, ".stab")
4686 && (!stab
->name
[5] ||
4687 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4688 && (stab
->flags
& SEC_MERGE
) == 0
4689 && !bfd_is_abs_section (stab
->output_section
))
4691 struct bfd_elf_section_data
*secdata
;
4693 secdata
= elf_section_data (stab
);
4694 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4695 stabstr
, &secdata
->sec_info
,
4698 if (secdata
->sec_info
)
4699 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4704 if (is_elf_hash_table (htab
) && add_needed
)
4706 /* Add this bfd to the loaded list. */
4707 struct elf_link_loaded_list
*n
;
4709 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4713 n
->next
= htab
->loaded
;
4720 if (old_tab
!= NULL
)
4722 if (nondeflt_vers
!= NULL
)
4723 free (nondeflt_vers
);
4724 if (extversym
!= NULL
)
4727 if (isymbuf
!= NULL
)
4733 /* Return the linker hash table entry of a symbol that might be
4734 satisfied by an archive symbol. Return -1 on error. */
4736 struct elf_link_hash_entry
*
4737 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4738 struct bfd_link_info
*info
,
4741 struct elf_link_hash_entry
*h
;
4745 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4749 /* If this is a default version (the name contains @@), look up the
4750 symbol again with only one `@' as well as without the version.
4751 The effect is that references to the symbol with and without the
4752 version will be matched by the default symbol in the archive. */
4754 p
= strchr (name
, ELF_VER_CHR
);
4755 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4758 /* First check with only one `@'. */
4759 len
= strlen (name
);
4760 copy
= bfd_alloc (abfd
, len
);
4762 return (struct elf_link_hash_entry
*) 0 - 1;
4764 first
= p
- name
+ 1;
4765 memcpy (copy
, name
, first
);
4766 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4768 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4771 /* We also need to check references to the symbol without the
4773 copy
[first
- 1] = '\0';
4774 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4775 FALSE
, FALSE
, FALSE
);
4778 bfd_release (abfd
, copy
);
4782 /* Add symbols from an ELF archive file to the linker hash table. We
4783 don't use _bfd_generic_link_add_archive_symbols because of a
4784 problem which arises on UnixWare. The UnixWare libc.so is an
4785 archive which includes an entry libc.so.1 which defines a bunch of
4786 symbols. The libc.so archive also includes a number of other
4787 object files, which also define symbols, some of which are the same
4788 as those defined in libc.so.1. Correct linking requires that we
4789 consider each object file in turn, and include it if it defines any
4790 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4791 this; it looks through the list of undefined symbols, and includes
4792 any object file which defines them. When this algorithm is used on
4793 UnixWare, it winds up pulling in libc.so.1 early and defining a
4794 bunch of symbols. This means that some of the other objects in the
4795 archive are not included in the link, which is incorrect since they
4796 precede libc.so.1 in the archive.
4798 Fortunately, ELF archive handling is simpler than that done by
4799 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4800 oddities. In ELF, if we find a symbol in the archive map, and the
4801 symbol is currently undefined, we know that we must pull in that
4804 Unfortunately, we do have to make multiple passes over the symbol
4805 table until nothing further is resolved. */
4808 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4811 bfd_boolean
*defined
= NULL
;
4812 bfd_boolean
*included
= NULL
;
4816 const struct elf_backend_data
*bed
;
4817 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4818 (bfd
*, struct bfd_link_info
*, const char *);
4820 if (! bfd_has_map (abfd
))
4822 /* An empty archive is a special case. */
4823 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4825 bfd_set_error (bfd_error_no_armap
);
4829 /* Keep track of all symbols we know to be already defined, and all
4830 files we know to be already included. This is to speed up the
4831 second and subsequent passes. */
4832 c
= bfd_ardata (abfd
)->symdef_count
;
4836 amt
*= sizeof (bfd_boolean
);
4837 defined
= bfd_zmalloc (amt
);
4838 included
= bfd_zmalloc (amt
);
4839 if (defined
== NULL
|| included
== NULL
)
4842 symdefs
= bfd_ardata (abfd
)->symdefs
;
4843 bed
= get_elf_backend_data (abfd
);
4844 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4857 symdefend
= symdef
+ c
;
4858 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4860 struct elf_link_hash_entry
*h
;
4862 struct bfd_link_hash_entry
*undefs_tail
;
4865 if (defined
[i
] || included
[i
])
4867 if (symdef
->file_offset
== last
)
4873 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4874 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4880 if (h
->root
.type
== bfd_link_hash_common
)
4882 /* We currently have a common symbol. The archive map contains
4883 a reference to this symbol, so we may want to include it. We
4884 only want to include it however, if this archive element
4885 contains a definition of the symbol, not just another common
4888 Unfortunately some archivers (including GNU ar) will put
4889 declarations of common symbols into their archive maps, as
4890 well as real definitions, so we cannot just go by the archive
4891 map alone. Instead we must read in the element's symbol
4892 table and check that to see what kind of symbol definition
4894 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4897 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4899 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4904 /* We need to include this archive member. */
4905 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4906 if (element
== NULL
)
4909 if (! bfd_check_format (element
, bfd_object
))
4912 /* Doublecheck that we have not included this object
4913 already--it should be impossible, but there may be
4914 something wrong with the archive. */
4915 if (element
->archive_pass
!= 0)
4917 bfd_set_error (bfd_error_bad_value
);
4920 element
->archive_pass
= 1;
4922 undefs_tail
= info
->hash
->undefs_tail
;
4924 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4927 if (! bfd_link_add_symbols (element
, info
))
4930 /* If there are any new undefined symbols, we need to make
4931 another pass through the archive in order to see whether
4932 they can be defined. FIXME: This isn't perfect, because
4933 common symbols wind up on undefs_tail and because an
4934 undefined symbol which is defined later on in this pass
4935 does not require another pass. This isn't a bug, but it
4936 does make the code less efficient than it could be. */
4937 if (undefs_tail
!= info
->hash
->undefs_tail
)
4940 /* Look backward to mark all symbols from this object file
4941 which we have already seen in this pass. */
4945 included
[mark
] = TRUE
;
4950 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4952 /* We mark subsequent symbols from this object file as we go
4953 on through the loop. */
4954 last
= symdef
->file_offset
;
4965 if (defined
!= NULL
)
4967 if (included
!= NULL
)
4972 /* Given an ELF BFD, add symbols to the global hash table as
4976 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4978 switch (bfd_get_format (abfd
))
4981 return elf_link_add_object_symbols (abfd
, info
);
4983 return elf_link_add_archive_symbols (abfd
, info
);
4985 bfd_set_error (bfd_error_wrong_format
);
4990 /* This function will be called though elf_link_hash_traverse to store
4991 all hash value of the exported symbols in an array. */
4994 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4996 unsigned long **valuep
= data
;
5002 if (h
->root
.type
== bfd_link_hash_warning
)
5003 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5005 /* Ignore indirect symbols. These are added by the versioning code. */
5006 if (h
->dynindx
== -1)
5009 name
= h
->root
.root
.string
;
5010 p
= strchr (name
, ELF_VER_CHR
);
5013 alc
= bfd_malloc (p
- name
+ 1);
5014 memcpy (alc
, name
, p
- name
);
5015 alc
[p
- name
] = '\0';
5019 /* Compute the hash value. */
5020 ha
= bfd_elf_hash (name
);
5022 /* Store the found hash value in the array given as the argument. */
5025 /* And store it in the struct so that we can put it in the hash table
5027 h
->u
.elf_hash_value
= ha
;
5035 struct collect_gnu_hash_codes
5038 const struct elf_backend_data
*bed
;
5039 unsigned long int nsyms
;
5040 unsigned long int maskbits
;
5041 unsigned long int *hashcodes
;
5042 unsigned long int *hashval
;
5043 unsigned long int *indx
;
5044 unsigned long int *counts
;
5047 long int min_dynindx
;
5048 unsigned long int bucketcount
;
5049 unsigned long int symindx
;
5050 long int local_indx
;
5051 long int shift1
, shift2
;
5052 unsigned long int mask
;
5055 /* This function will be called though elf_link_hash_traverse to store
5056 all hash value of the exported symbols in an array. */
5059 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5061 struct collect_gnu_hash_codes
*s
= data
;
5067 if (h
->root
.type
== bfd_link_hash_warning
)
5068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5070 /* Ignore indirect symbols. These are added by the versioning code. */
5071 if (h
->dynindx
== -1)
5074 /* Ignore also local symbols and undefined symbols. */
5075 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5078 name
= h
->root
.root
.string
;
5079 p
= strchr (name
, ELF_VER_CHR
);
5082 alc
= bfd_malloc (p
- name
+ 1);
5083 memcpy (alc
, name
, p
- name
);
5084 alc
[p
- name
] = '\0';
5088 /* Compute the hash value. */
5089 ha
= bfd_elf_gnu_hash (name
);
5091 /* Store the found hash value in the array for compute_bucket_count,
5092 and also for .dynsym reordering purposes. */
5093 s
->hashcodes
[s
->nsyms
] = ha
;
5094 s
->hashval
[h
->dynindx
] = ha
;
5096 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5097 s
->min_dynindx
= h
->dynindx
;
5105 /* This function will be called though elf_link_hash_traverse to do
5106 final dynaminc symbol renumbering. */
5109 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5111 struct collect_gnu_hash_codes
*s
= data
;
5112 unsigned long int bucket
;
5113 unsigned long int val
;
5115 if (h
->root
.type
== bfd_link_hash_warning
)
5116 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5118 /* Ignore indirect symbols. */
5119 if (h
->dynindx
== -1)
5122 /* Ignore also local symbols and undefined symbols. */
5123 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5125 if (h
->dynindx
>= s
->min_dynindx
)
5126 h
->dynindx
= s
->local_indx
++;
5130 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5131 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5132 & ((s
->maskbits
>> s
->shift1
) - 1);
5133 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5135 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5136 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5137 if (s
->counts
[bucket
] == 1)
5138 /* Last element terminates the chain. */
5140 bfd_put_32 (s
->output_bfd
, val
,
5141 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5142 --s
->counts
[bucket
];
5143 h
->dynindx
= s
->indx
[bucket
]++;
5147 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5150 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5152 return !(h
->forced_local
5153 || h
->root
.type
== bfd_link_hash_undefined
5154 || h
->root
.type
== bfd_link_hash_undefweak
5155 || ((h
->root
.type
== bfd_link_hash_defined
5156 || h
->root
.type
== bfd_link_hash_defweak
)
5157 && h
->root
.u
.def
.section
->output_section
== NULL
));
5160 /* Array used to determine the number of hash table buckets to use
5161 based on the number of symbols there are. If there are fewer than
5162 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5163 fewer than 37 we use 17 buckets, and so forth. We never use more
5164 than 32771 buckets. */
5166 static const size_t elf_buckets
[] =
5168 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5172 /* Compute bucket count for hashing table. We do not use a static set
5173 of possible tables sizes anymore. Instead we determine for all
5174 possible reasonable sizes of the table the outcome (i.e., the
5175 number of collisions etc) and choose the best solution. The
5176 weighting functions are not too simple to allow the table to grow
5177 without bounds. Instead one of the weighting factors is the size.
5178 Therefore the result is always a good payoff between few collisions
5179 (= short chain lengths) and table size. */
5181 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5182 unsigned long int nsyms
, int gnu_hash
)
5184 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5185 size_t best_size
= 0;
5186 unsigned long int i
;
5189 /* We have a problem here. The following code to optimize the table
5190 size requires an integer type with more the 32 bits. If
5191 BFD_HOST_U_64_BIT is set we know about such a type. */
5192 #ifdef BFD_HOST_U_64_BIT
5197 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5198 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5199 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5200 unsigned long int *counts
;
5202 /* Possible optimization parameters: if we have NSYMS symbols we say
5203 that the hashing table must at least have NSYMS/4 and at most
5205 minsize
= nsyms
/ 4;
5208 best_size
= maxsize
= nsyms
* 2;
5213 if ((best_size
& 31) == 0)
5217 /* Create array where we count the collisions in. We must use bfd_malloc
5218 since the size could be large. */
5220 amt
*= sizeof (unsigned long int);
5221 counts
= bfd_malloc (amt
);
5225 /* Compute the "optimal" size for the hash table. The criteria is a
5226 minimal chain length. The minor criteria is (of course) the size
5228 for (i
= minsize
; i
< maxsize
; ++i
)
5230 /* Walk through the array of hashcodes and count the collisions. */
5231 BFD_HOST_U_64_BIT max
;
5232 unsigned long int j
;
5233 unsigned long int fact
;
5235 if (gnu_hash
&& (i
& 31) == 0)
5238 memset (counts
, '\0', i
* sizeof (unsigned long int));
5240 /* Determine how often each hash bucket is used. */
5241 for (j
= 0; j
< nsyms
; ++j
)
5242 ++counts
[hashcodes
[j
] % i
];
5244 /* For the weight function we need some information about the
5245 pagesize on the target. This is information need not be 100%
5246 accurate. Since this information is not available (so far) we
5247 define it here to a reasonable default value. If it is crucial
5248 to have a better value some day simply define this value. */
5249 # ifndef BFD_TARGET_PAGESIZE
5250 # define BFD_TARGET_PAGESIZE (4096)
5253 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5255 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5258 /* Variant 1: optimize for short chains. We add the squares
5259 of all the chain lengths (which favors many small chain
5260 over a few long chains). */
5261 for (j
= 0; j
< i
; ++j
)
5262 max
+= counts
[j
] * counts
[j
];
5264 /* This adds penalties for the overall size of the table. */
5265 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5268 /* Variant 2: Optimize a lot more for small table. Here we
5269 also add squares of the size but we also add penalties for
5270 empty slots (the +1 term). */
5271 for (j
= 0; j
< i
; ++j
)
5272 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5274 /* The overall size of the table is considered, but not as
5275 strong as in variant 1, where it is squared. */
5276 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5280 /* Compare with current best results. */
5281 if (max
< best_chlen
)
5291 #endif /* defined (BFD_HOST_U_64_BIT) */
5293 /* This is the fallback solution if no 64bit type is available or if we
5294 are not supposed to spend much time on optimizations. We select the
5295 bucket count using a fixed set of numbers. */
5296 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5298 best_size
= elf_buckets
[i
];
5299 if (nsyms
< elf_buckets
[i
+ 1])
5302 if (gnu_hash
&& best_size
< 2)
5309 /* Set up the sizes and contents of the ELF dynamic sections. This is
5310 called by the ELF linker emulation before_allocation routine. We
5311 must set the sizes of the sections before the linker sets the
5312 addresses of the various sections. */
5315 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5318 const char *filter_shlib
,
5319 const char * const *auxiliary_filters
,
5320 struct bfd_link_info
*info
,
5321 asection
**sinterpptr
,
5322 struct bfd_elf_version_tree
*verdefs
)
5324 bfd_size_type soname_indx
;
5326 const struct elf_backend_data
*bed
;
5327 struct elf_assign_sym_version_info asvinfo
;
5331 soname_indx
= (bfd_size_type
) -1;
5333 if (!is_elf_hash_table (info
->hash
))
5336 bed
= get_elf_backend_data (output_bfd
);
5337 elf_tdata (output_bfd
)->relro
= info
->relro
;
5338 if (info
->execstack
)
5339 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5340 else if (info
->noexecstack
)
5341 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5345 asection
*notesec
= NULL
;
5348 for (inputobj
= info
->input_bfds
;
5350 inputobj
= inputobj
->link_next
)
5354 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5356 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5359 if (s
->flags
& SEC_CODE
)
5363 else if (bed
->default_execstack
)
5368 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5369 if (exec
&& info
->relocatable
5370 && notesec
->output_section
!= bfd_abs_section_ptr
)
5371 notesec
->output_section
->flags
|= SEC_CODE
;
5375 /* Any syms created from now on start with -1 in
5376 got.refcount/offset and plt.refcount/offset. */
5377 elf_hash_table (info
)->init_got_refcount
5378 = elf_hash_table (info
)->init_got_offset
;
5379 elf_hash_table (info
)->init_plt_refcount
5380 = elf_hash_table (info
)->init_plt_offset
;
5382 /* The backend may have to create some sections regardless of whether
5383 we're dynamic or not. */
5384 if (bed
->elf_backend_always_size_sections
5385 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5388 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5391 dynobj
= elf_hash_table (info
)->dynobj
;
5393 /* If there were no dynamic objects in the link, there is nothing to
5398 if (elf_hash_table (info
)->dynamic_sections_created
)
5400 struct elf_info_failed eif
;
5401 struct elf_link_hash_entry
*h
;
5403 struct bfd_elf_version_tree
*t
;
5404 struct bfd_elf_version_expr
*d
;
5406 bfd_boolean all_defined
;
5408 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5409 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5413 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5415 if (soname_indx
== (bfd_size_type
) -1
5416 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5422 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5424 info
->flags
|= DF_SYMBOLIC
;
5431 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5433 if (indx
== (bfd_size_type
) -1
5434 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5437 if (info
->new_dtags
)
5439 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5440 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5445 if (filter_shlib
!= NULL
)
5449 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5450 filter_shlib
, TRUE
);
5451 if (indx
== (bfd_size_type
) -1
5452 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5456 if (auxiliary_filters
!= NULL
)
5458 const char * const *p
;
5460 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5464 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5466 if (indx
== (bfd_size_type
) -1
5467 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5473 eif
.verdefs
= verdefs
;
5476 /* If we are supposed to export all symbols into the dynamic symbol
5477 table (this is not the normal case), then do so. */
5478 if (info
->export_dynamic
5479 || (info
->executable
&& info
->dynamic
))
5481 elf_link_hash_traverse (elf_hash_table (info
),
5482 _bfd_elf_export_symbol
,
5488 /* Make all global versions with definition. */
5489 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5490 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5491 if (!d
->symver
&& d
->symbol
)
5493 const char *verstr
, *name
;
5494 size_t namelen
, verlen
, newlen
;
5496 struct elf_link_hash_entry
*newh
;
5499 namelen
= strlen (name
);
5501 verlen
= strlen (verstr
);
5502 newlen
= namelen
+ verlen
+ 3;
5504 newname
= bfd_malloc (newlen
);
5505 if (newname
== NULL
)
5507 memcpy (newname
, name
, namelen
);
5509 /* Check the hidden versioned definition. */
5510 p
= newname
+ namelen
;
5512 memcpy (p
, verstr
, verlen
+ 1);
5513 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5514 newname
, FALSE
, FALSE
,
5517 || (newh
->root
.type
!= bfd_link_hash_defined
5518 && newh
->root
.type
!= bfd_link_hash_defweak
))
5520 /* Check the default versioned definition. */
5522 memcpy (p
, verstr
, verlen
+ 1);
5523 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5524 newname
, FALSE
, FALSE
,
5529 /* Mark this version if there is a definition and it is
5530 not defined in a shared object. */
5532 && !newh
->def_dynamic
5533 && (newh
->root
.type
== bfd_link_hash_defined
5534 || newh
->root
.type
== bfd_link_hash_defweak
))
5538 /* Attach all the symbols to their version information. */
5539 asvinfo
.output_bfd
= output_bfd
;
5540 asvinfo
.info
= info
;
5541 asvinfo
.verdefs
= verdefs
;
5542 asvinfo
.failed
= FALSE
;
5544 elf_link_hash_traverse (elf_hash_table (info
),
5545 _bfd_elf_link_assign_sym_version
,
5550 if (!info
->allow_undefined_version
)
5552 /* Check if all global versions have a definition. */
5554 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5555 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5556 if (!d
->symver
&& !d
->script
)
5558 (*_bfd_error_handler
)
5559 (_("%s: undefined version: %s"),
5560 d
->pattern
, t
->name
);
5561 all_defined
= FALSE
;
5566 bfd_set_error (bfd_error_bad_value
);
5571 /* Find all symbols which were defined in a dynamic object and make
5572 the backend pick a reasonable value for them. */
5573 elf_link_hash_traverse (elf_hash_table (info
),
5574 _bfd_elf_adjust_dynamic_symbol
,
5579 /* Add some entries to the .dynamic section. We fill in some of the
5580 values later, in bfd_elf_final_link, but we must add the entries
5581 now so that we know the final size of the .dynamic section. */
5583 /* If there are initialization and/or finalization functions to
5584 call then add the corresponding DT_INIT/DT_FINI entries. */
5585 h
= (info
->init_function
5586 ? elf_link_hash_lookup (elf_hash_table (info
),
5587 info
->init_function
, FALSE
,
5594 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5597 h
= (info
->fini_function
5598 ? elf_link_hash_lookup (elf_hash_table (info
),
5599 info
->fini_function
, FALSE
,
5606 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5610 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5611 if (s
!= NULL
&& s
->linker_has_input
)
5613 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5614 if (! info
->executable
)
5619 for (sub
= info
->input_bfds
; sub
!= NULL
;
5620 sub
= sub
->link_next
)
5621 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5622 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5623 if (elf_section_data (o
)->this_hdr
.sh_type
5624 == SHT_PREINIT_ARRAY
)
5626 (*_bfd_error_handler
)
5627 (_("%B: .preinit_array section is not allowed in DSO"),
5632 bfd_set_error (bfd_error_nonrepresentable_section
);
5636 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5637 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5640 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5641 if (s
!= NULL
&& s
->linker_has_input
)
5643 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5644 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5647 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5648 if (s
!= NULL
&& s
->linker_has_input
)
5650 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5651 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5655 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5656 /* If .dynstr is excluded from the link, we don't want any of
5657 these tags. Strictly, we should be checking each section
5658 individually; This quick check covers for the case where
5659 someone does a /DISCARD/ : { *(*) }. */
5660 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5662 bfd_size_type strsize
;
5664 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5665 if ((info
->emit_hash
5666 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5667 || (info
->emit_gnu_hash
5668 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5669 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5670 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5671 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5672 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5673 bed
->s
->sizeof_sym
))
5678 /* The backend must work out the sizes of all the other dynamic
5680 if (bed
->elf_backend_size_dynamic_sections
5681 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5684 if (elf_hash_table (info
)->dynamic_sections_created
)
5686 unsigned long section_sym_count
;
5689 /* Set up the version definition section. */
5690 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5691 BFD_ASSERT (s
!= NULL
);
5693 /* We may have created additional version definitions if we are
5694 just linking a regular application. */
5695 verdefs
= asvinfo
.verdefs
;
5697 /* Skip anonymous version tag. */
5698 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5699 verdefs
= verdefs
->next
;
5701 if (verdefs
== NULL
&& !info
->create_default_symver
)
5702 s
->flags
|= SEC_EXCLUDE
;
5707 struct bfd_elf_version_tree
*t
;
5709 Elf_Internal_Verdef def
;
5710 Elf_Internal_Verdaux defaux
;
5711 struct bfd_link_hash_entry
*bh
;
5712 struct elf_link_hash_entry
*h
;
5718 /* Make space for the base version. */
5719 size
+= sizeof (Elf_External_Verdef
);
5720 size
+= sizeof (Elf_External_Verdaux
);
5723 /* Make space for the default version. */
5724 if (info
->create_default_symver
)
5726 size
+= sizeof (Elf_External_Verdef
);
5730 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5732 struct bfd_elf_version_deps
*n
;
5734 size
+= sizeof (Elf_External_Verdef
);
5735 size
+= sizeof (Elf_External_Verdaux
);
5738 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5739 size
+= sizeof (Elf_External_Verdaux
);
5743 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5744 if (s
->contents
== NULL
&& s
->size
!= 0)
5747 /* Fill in the version definition section. */
5751 def
.vd_version
= VER_DEF_CURRENT
;
5752 def
.vd_flags
= VER_FLG_BASE
;
5755 if (info
->create_default_symver
)
5757 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5758 def
.vd_next
= sizeof (Elf_External_Verdef
);
5762 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5763 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5764 + sizeof (Elf_External_Verdaux
));
5767 if (soname_indx
!= (bfd_size_type
) -1)
5769 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5771 def
.vd_hash
= bfd_elf_hash (soname
);
5772 defaux
.vda_name
= soname_indx
;
5779 name
= lbasename (output_bfd
->filename
);
5780 def
.vd_hash
= bfd_elf_hash (name
);
5781 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5783 if (indx
== (bfd_size_type
) -1)
5785 defaux
.vda_name
= indx
;
5787 defaux
.vda_next
= 0;
5789 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5790 (Elf_External_Verdef
*) p
);
5791 p
+= sizeof (Elf_External_Verdef
);
5792 if (info
->create_default_symver
)
5794 /* Add a symbol representing this version. */
5796 if (! (_bfd_generic_link_add_one_symbol
5797 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5799 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5801 h
= (struct elf_link_hash_entry
*) bh
;
5804 h
->type
= STT_OBJECT
;
5805 h
->verinfo
.vertree
= NULL
;
5807 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5810 /* Create a duplicate of the base version with the same
5811 aux block, but different flags. */
5814 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5816 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5817 + sizeof (Elf_External_Verdaux
));
5820 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5821 (Elf_External_Verdef
*) p
);
5822 p
+= sizeof (Elf_External_Verdef
);
5824 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5825 (Elf_External_Verdaux
*) p
);
5826 p
+= sizeof (Elf_External_Verdaux
);
5828 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5831 struct bfd_elf_version_deps
*n
;
5834 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5837 /* Add a symbol representing this version. */
5839 if (! (_bfd_generic_link_add_one_symbol
5840 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5842 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5844 h
= (struct elf_link_hash_entry
*) bh
;
5847 h
->type
= STT_OBJECT
;
5848 h
->verinfo
.vertree
= t
;
5850 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5853 def
.vd_version
= VER_DEF_CURRENT
;
5855 if (t
->globals
.list
== NULL
5856 && t
->locals
.list
== NULL
5858 def
.vd_flags
|= VER_FLG_WEAK
;
5859 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5860 def
.vd_cnt
= cdeps
+ 1;
5861 def
.vd_hash
= bfd_elf_hash (t
->name
);
5862 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5864 if (t
->next
!= NULL
)
5865 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5866 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5868 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5869 (Elf_External_Verdef
*) p
);
5870 p
+= sizeof (Elf_External_Verdef
);
5872 defaux
.vda_name
= h
->dynstr_index
;
5873 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5875 defaux
.vda_next
= 0;
5876 if (t
->deps
!= NULL
)
5877 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5878 t
->name_indx
= defaux
.vda_name
;
5880 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5881 (Elf_External_Verdaux
*) p
);
5882 p
+= sizeof (Elf_External_Verdaux
);
5884 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5886 if (n
->version_needed
== NULL
)
5888 /* This can happen if there was an error in the
5890 defaux
.vda_name
= 0;
5894 defaux
.vda_name
= n
->version_needed
->name_indx
;
5895 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5898 if (n
->next
== NULL
)
5899 defaux
.vda_next
= 0;
5901 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5903 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5904 (Elf_External_Verdaux
*) p
);
5905 p
+= sizeof (Elf_External_Verdaux
);
5909 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5910 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5913 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5916 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5918 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5921 else if (info
->flags
& DF_BIND_NOW
)
5923 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5929 if (info
->executable
)
5930 info
->flags_1
&= ~ (DF_1_INITFIRST
5933 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5937 /* Work out the size of the version reference section. */
5939 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5940 BFD_ASSERT (s
!= NULL
);
5942 struct elf_find_verdep_info sinfo
;
5944 sinfo
.output_bfd
= output_bfd
;
5946 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5947 if (sinfo
.vers
== 0)
5949 sinfo
.failed
= FALSE
;
5951 elf_link_hash_traverse (elf_hash_table (info
),
5952 _bfd_elf_link_find_version_dependencies
,
5955 if (elf_tdata (output_bfd
)->verref
== NULL
)
5956 s
->flags
|= SEC_EXCLUDE
;
5959 Elf_Internal_Verneed
*t
;
5964 /* Build the version definition section. */
5967 for (t
= elf_tdata (output_bfd
)->verref
;
5971 Elf_Internal_Vernaux
*a
;
5973 size
+= sizeof (Elf_External_Verneed
);
5975 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5976 size
+= sizeof (Elf_External_Vernaux
);
5980 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5981 if (s
->contents
== NULL
)
5985 for (t
= elf_tdata (output_bfd
)->verref
;
5990 Elf_Internal_Vernaux
*a
;
5994 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5997 t
->vn_version
= VER_NEED_CURRENT
;
5999 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6000 elf_dt_name (t
->vn_bfd
) != NULL
6001 ? elf_dt_name (t
->vn_bfd
)
6002 : lbasename (t
->vn_bfd
->filename
),
6004 if (indx
== (bfd_size_type
) -1)
6007 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6008 if (t
->vn_nextref
== NULL
)
6011 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6012 + caux
* sizeof (Elf_External_Vernaux
));
6014 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6015 (Elf_External_Verneed
*) p
);
6016 p
+= sizeof (Elf_External_Verneed
);
6018 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6020 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6021 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6022 a
->vna_nodename
, FALSE
);
6023 if (indx
== (bfd_size_type
) -1)
6026 if (a
->vna_nextptr
== NULL
)
6029 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6031 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6032 (Elf_External_Vernaux
*) p
);
6033 p
+= sizeof (Elf_External_Vernaux
);
6037 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6038 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6041 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6045 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6046 && elf_tdata (output_bfd
)->cverdefs
== 0)
6047 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6048 §ion_sym_count
) == 0)
6050 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6051 s
->flags
|= SEC_EXCLUDE
;
6057 /* Find the first non-excluded output section. We'll use its
6058 section symbol for some emitted relocs. */
6060 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6064 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6065 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6066 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6068 elf_hash_table (info
)->text_index_section
= s
;
6073 /* Find two non-excluded output sections, one for code, one for data.
6074 We'll use their section symbols for some emitted relocs. */
6076 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6080 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6081 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6082 == (SEC_ALLOC
| SEC_READONLY
))
6083 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6085 elf_hash_table (info
)->text_index_section
= s
;
6089 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6090 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6091 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6093 elf_hash_table (info
)->data_index_section
= s
;
6097 if (elf_hash_table (info
)->text_index_section
== NULL
)
6098 elf_hash_table (info
)->text_index_section
6099 = elf_hash_table (info
)->data_index_section
;
6103 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6105 const struct elf_backend_data
*bed
;
6107 if (!is_elf_hash_table (info
->hash
))
6110 bed
= get_elf_backend_data (output_bfd
);
6111 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6113 if (elf_hash_table (info
)->dynamic_sections_created
)
6117 bfd_size_type dynsymcount
;
6118 unsigned long section_sym_count
;
6119 unsigned int dtagcount
;
6121 dynobj
= elf_hash_table (info
)->dynobj
;
6123 /* Assign dynsym indicies. In a shared library we generate a
6124 section symbol for each output section, which come first.
6125 Next come all of the back-end allocated local dynamic syms,
6126 followed by the rest of the global symbols. */
6128 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6129 §ion_sym_count
);
6131 /* Work out the size of the symbol version section. */
6132 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6133 BFD_ASSERT (s
!= NULL
);
6134 if (dynsymcount
!= 0
6135 && (s
->flags
& SEC_EXCLUDE
) == 0)
6137 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6138 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6139 if (s
->contents
== NULL
)
6142 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6146 /* Set the size of the .dynsym and .hash sections. We counted
6147 the number of dynamic symbols in elf_link_add_object_symbols.
6148 We will build the contents of .dynsym and .hash when we build
6149 the final symbol table, because until then we do not know the
6150 correct value to give the symbols. We built the .dynstr
6151 section as we went along in elf_link_add_object_symbols. */
6152 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6153 BFD_ASSERT (s
!= NULL
);
6154 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6156 if (dynsymcount
!= 0)
6158 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6159 if (s
->contents
== NULL
)
6162 /* The first entry in .dynsym is a dummy symbol.
6163 Clear all the section syms, in case we don't output them all. */
6164 ++section_sym_count
;
6165 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6168 elf_hash_table (info
)->bucketcount
= 0;
6170 /* Compute the size of the hashing table. As a side effect this
6171 computes the hash values for all the names we export. */
6172 if (info
->emit_hash
)
6174 unsigned long int *hashcodes
;
6175 unsigned long int *hashcodesp
;
6177 unsigned long int nsyms
;
6179 size_t hash_entry_size
;
6181 /* Compute the hash values for all exported symbols. At the same
6182 time store the values in an array so that we could use them for
6184 amt
= dynsymcount
* sizeof (unsigned long int);
6185 hashcodes
= bfd_malloc (amt
);
6186 if (hashcodes
== NULL
)
6188 hashcodesp
= hashcodes
;
6190 /* Put all hash values in HASHCODES. */
6191 elf_link_hash_traverse (elf_hash_table (info
),
6192 elf_collect_hash_codes
, &hashcodesp
);
6194 nsyms
= hashcodesp
- hashcodes
;
6196 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6199 if (bucketcount
== 0)
6202 elf_hash_table (info
)->bucketcount
= bucketcount
;
6204 s
= bfd_get_section_by_name (dynobj
, ".hash");
6205 BFD_ASSERT (s
!= NULL
);
6206 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6207 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6208 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6209 if (s
->contents
== NULL
)
6212 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6213 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6214 s
->contents
+ hash_entry_size
);
6217 if (info
->emit_gnu_hash
)
6220 unsigned char *contents
;
6221 struct collect_gnu_hash_codes cinfo
;
6225 memset (&cinfo
, 0, sizeof (cinfo
));
6227 /* Compute the hash values for all exported symbols. At the same
6228 time store the values in an array so that we could use them for
6230 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6231 cinfo
.hashcodes
= bfd_malloc (amt
);
6232 if (cinfo
.hashcodes
== NULL
)
6235 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6236 cinfo
.min_dynindx
= -1;
6237 cinfo
.output_bfd
= output_bfd
;
6240 /* Put all hash values in HASHCODES. */
6241 elf_link_hash_traverse (elf_hash_table (info
),
6242 elf_collect_gnu_hash_codes
, &cinfo
);
6245 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6247 if (bucketcount
== 0)
6249 free (cinfo
.hashcodes
);
6253 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6254 BFD_ASSERT (s
!= NULL
);
6256 if (cinfo
.nsyms
== 0)
6258 /* Empty .gnu.hash section is special. */
6259 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6260 free (cinfo
.hashcodes
);
6261 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6262 contents
= bfd_zalloc (output_bfd
, s
->size
);
6263 if (contents
== NULL
)
6265 s
->contents
= contents
;
6266 /* 1 empty bucket. */
6267 bfd_put_32 (output_bfd
, 1, contents
);
6268 /* SYMIDX above the special symbol 0. */
6269 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6270 /* Just one word for bitmask. */
6271 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6272 /* Only hash fn bloom filter. */
6273 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6274 /* No hashes are valid - empty bitmask. */
6275 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6276 /* No hashes in the only bucket. */
6277 bfd_put_32 (output_bfd
, 0,
6278 contents
+ 16 + bed
->s
->arch_size
/ 8);
6282 unsigned long int maskwords
, maskbitslog2
;
6283 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6285 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6286 if (maskbitslog2
< 3)
6288 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6289 maskbitslog2
= maskbitslog2
+ 3;
6291 maskbitslog2
= maskbitslog2
+ 2;
6292 if (bed
->s
->arch_size
== 64)
6294 if (maskbitslog2
== 5)
6300 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6301 cinfo
.shift2
= maskbitslog2
;
6302 cinfo
.maskbits
= 1 << maskbitslog2
;
6303 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6304 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6305 amt
+= maskwords
* sizeof (bfd_vma
);
6306 cinfo
.bitmask
= bfd_malloc (amt
);
6307 if (cinfo
.bitmask
== NULL
)
6309 free (cinfo
.hashcodes
);
6313 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6314 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6315 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6316 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6318 /* Determine how often each hash bucket is used. */
6319 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6320 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6321 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6323 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6324 if (cinfo
.counts
[i
] != 0)
6326 cinfo
.indx
[i
] = cnt
;
6327 cnt
+= cinfo
.counts
[i
];
6329 BFD_ASSERT (cnt
== dynsymcount
);
6330 cinfo
.bucketcount
= bucketcount
;
6331 cinfo
.local_indx
= cinfo
.min_dynindx
;
6333 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6334 s
->size
+= cinfo
.maskbits
/ 8;
6335 contents
= bfd_zalloc (output_bfd
, s
->size
);
6336 if (contents
== NULL
)
6338 free (cinfo
.bitmask
);
6339 free (cinfo
.hashcodes
);
6343 s
->contents
= contents
;
6344 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6345 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6346 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6347 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6348 contents
+= 16 + cinfo
.maskbits
/ 8;
6350 for (i
= 0; i
< bucketcount
; ++i
)
6352 if (cinfo
.counts
[i
] == 0)
6353 bfd_put_32 (output_bfd
, 0, contents
);
6355 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6359 cinfo
.contents
= contents
;
6361 /* Renumber dynamic symbols, populate .gnu.hash section. */
6362 elf_link_hash_traverse (elf_hash_table (info
),
6363 elf_renumber_gnu_hash_syms
, &cinfo
);
6365 contents
= s
->contents
+ 16;
6366 for (i
= 0; i
< maskwords
; ++i
)
6368 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6370 contents
+= bed
->s
->arch_size
/ 8;
6373 free (cinfo
.bitmask
);
6374 free (cinfo
.hashcodes
);
6378 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6379 BFD_ASSERT (s
!= NULL
);
6381 elf_finalize_dynstr (output_bfd
, info
);
6383 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6385 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6386 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6393 /* Indicate that we are only retrieving symbol values from this
6397 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
6399 if (is_elf_hash_table (info
->hash
))
6400 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
6401 _bfd_generic_link_just_syms (sec
, info
);
6404 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6407 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6410 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
6411 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
6414 /* Finish SHF_MERGE section merging. */
6417 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6422 if (!is_elf_hash_table (info
->hash
))
6425 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6426 if ((ibfd
->flags
& DYNAMIC
) == 0)
6427 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6428 if ((sec
->flags
& SEC_MERGE
) != 0
6429 && !bfd_is_abs_section (sec
->output_section
))
6431 struct bfd_elf_section_data
*secdata
;
6433 secdata
= elf_section_data (sec
);
6434 if (! _bfd_add_merge_section (abfd
,
6435 &elf_hash_table (info
)->merge_info
,
6436 sec
, &secdata
->sec_info
))
6438 else if (secdata
->sec_info
)
6439 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
6442 if (elf_hash_table (info
)->merge_info
!= NULL
)
6443 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6444 merge_sections_remove_hook
);
6448 /* Create an entry in an ELF linker hash table. */
6450 struct bfd_hash_entry
*
6451 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6452 struct bfd_hash_table
*table
,
6455 /* Allocate the structure if it has not already been allocated by a
6459 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6464 /* Call the allocation method of the superclass. */
6465 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6468 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6469 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6471 /* Set local fields. */
6474 ret
->got
= htab
->init_got_refcount
;
6475 ret
->plt
= htab
->init_plt_refcount
;
6476 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6477 - offsetof (struct elf_link_hash_entry
, size
)));
6478 /* Assume that we have been called by a non-ELF symbol reader.
6479 This flag is then reset by the code which reads an ELF input
6480 file. This ensures that a symbol created by a non-ELF symbol
6481 reader will have the flag set correctly. */
6488 /* Copy data from an indirect symbol to its direct symbol, hiding the
6489 old indirect symbol. Also used for copying flags to a weakdef. */
6492 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6493 struct elf_link_hash_entry
*dir
,
6494 struct elf_link_hash_entry
*ind
)
6496 struct elf_link_hash_table
*htab
;
6498 /* Copy down any references that we may have already seen to the
6499 symbol which just became indirect. */
6501 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6502 dir
->ref_regular
|= ind
->ref_regular
;
6503 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6504 dir
->non_got_ref
|= ind
->non_got_ref
;
6505 dir
->needs_plt
|= ind
->needs_plt
;
6506 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6508 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6511 /* Copy over the global and procedure linkage table refcount entries.
6512 These may have been already set up by a check_relocs routine. */
6513 htab
= elf_hash_table (info
);
6514 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6516 if (dir
->got
.refcount
< 0)
6517 dir
->got
.refcount
= 0;
6518 dir
->got
.refcount
+= ind
->got
.refcount
;
6519 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6522 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6524 if (dir
->plt
.refcount
< 0)
6525 dir
->plt
.refcount
= 0;
6526 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6527 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6530 if (ind
->dynindx
!= -1)
6532 if (dir
->dynindx
!= -1)
6533 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6534 dir
->dynindx
= ind
->dynindx
;
6535 dir
->dynstr_index
= ind
->dynstr_index
;
6537 ind
->dynstr_index
= 0;
6542 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6543 struct elf_link_hash_entry
*h
,
6544 bfd_boolean force_local
)
6546 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6550 h
->forced_local
= 1;
6551 if (h
->dynindx
!= -1)
6554 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6560 /* Initialize an ELF linker hash table. */
6563 _bfd_elf_link_hash_table_init
6564 (struct elf_link_hash_table
*table
,
6566 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6567 struct bfd_hash_table
*,
6569 unsigned int entsize
)
6572 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6574 memset (table
, 0, sizeof * table
);
6575 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6576 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6577 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6578 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6579 /* The first dynamic symbol is a dummy. */
6580 table
->dynsymcount
= 1;
6582 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6583 table
->root
.type
= bfd_link_elf_hash_table
;
6588 /* Create an ELF linker hash table. */
6590 struct bfd_link_hash_table
*
6591 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6593 struct elf_link_hash_table
*ret
;
6594 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6596 ret
= bfd_malloc (amt
);
6600 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6601 sizeof (struct elf_link_hash_entry
)))
6610 /* This is a hook for the ELF emulation code in the generic linker to
6611 tell the backend linker what file name to use for the DT_NEEDED
6612 entry for a dynamic object. */
6615 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6617 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6618 && bfd_get_format (abfd
) == bfd_object
)
6619 elf_dt_name (abfd
) = name
;
6623 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6626 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6627 && bfd_get_format (abfd
) == bfd_object
)
6628 lib_class
= elf_dyn_lib_class (abfd
);
6635 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6637 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6638 && bfd_get_format (abfd
) == bfd_object
)
6639 elf_dyn_lib_class (abfd
) = lib_class
;
6642 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6643 the linker ELF emulation code. */
6645 struct bfd_link_needed_list
*
6646 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6647 struct bfd_link_info
*info
)
6649 if (! is_elf_hash_table (info
->hash
))
6651 return elf_hash_table (info
)->needed
;
6654 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6655 hook for the linker ELF emulation code. */
6657 struct bfd_link_needed_list
*
6658 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6659 struct bfd_link_info
*info
)
6661 if (! is_elf_hash_table (info
->hash
))
6663 return elf_hash_table (info
)->runpath
;
6666 /* Get the name actually used for a dynamic object for a link. This
6667 is the SONAME entry if there is one. Otherwise, it is the string
6668 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6671 bfd_elf_get_dt_soname (bfd
*abfd
)
6673 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6674 && bfd_get_format (abfd
) == bfd_object
)
6675 return elf_dt_name (abfd
);
6679 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6680 the ELF linker emulation code. */
6683 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6684 struct bfd_link_needed_list
**pneeded
)
6687 bfd_byte
*dynbuf
= NULL
;
6689 unsigned long shlink
;
6690 bfd_byte
*extdyn
, *extdynend
;
6692 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
6696 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
6697 || bfd_get_format (abfd
) != bfd_object
)
6700 s
= bfd_get_section_by_name (abfd
, ".dynamic");
6701 if (s
== NULL
|| s
->size
== 0)
6704 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
6707 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
6711 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
6713 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
6714 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
6717 extdynend
= extdyn
+ s
->size
;
6718 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
6720 Elf_Internal_Dyn dyn
;
6722 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
6724 if (dyn
.d_tag
== DT_NULL
)
6727 if (dyn
.d_tag
== DT_NEEDED
)
6730 struct bfd_link_needed_list
*l
;
6731 unsigned int tagv
= dyn
.d_un
.d_val
;
6734 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
6739 l
= bfd_alloc (abfd
, amt
);
6760 struct elf_symbuf_symbol
6762 unsigned long st_name
; /* Symbol name, index in string tbl */
6763 unsigned char st_info
; /* Type and binding attributes */
6764 unsigned char st_other
; /* Visibilty, and target specific */
6767 struct elf_symbuf_head
6769 struct elf_symbuf_symbol
*ssym
;
6770 bfd_size_type count
;
6771 unsigned int st_shndx
;
6778 Elf_Internal_Sym
*isym
;
6779 struct elf_symbuf_symbol
*ssym
;
6784 /* Sort references to symbols by ascending section number. */
6787 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
6789 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
6790 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
6792 return s1
->st_shndx
- s2
->st_shndx
;
6796 elf_sym_name_compare (const void *arg1
, const void *arg2
)
6798 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
6799 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
6800 return strcmp (s1
->name
, s2
->name
);
6803 static struct elf_symbuf_head
*
6804 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
6806 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
6807 = bfd_malloc2 (symcount
, sizeof (*indbuf
));
6808 struct elf_symbuf_symbol
*ssym
;
6809 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
6810 bfd_size_type i
, shndx_count
;
6815 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
6816 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
6817 *ind
++ = &isymbuf
[i
];
6820 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
6821 elf_sort_elf_symbol
);
6824 if (indbufend
> indbuf
)
6825 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
6826 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
6829 ssymbuf
= bfd_malloc ((shndx_count
+ 1) * sizeof (*ssymbuf
)
6830 + (indbufend
- indbuf
) * sizeof (*ssymbuf
));
6831 if (ssymbuf
== NULL
)
6837 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
);
6838 ssymbuf
->ssym
= NULL
;
6839 ssymbuf
->count
= shndx_count
;
6840 ssymbuf
->st_shndx
= 0;
6841 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
6843 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
6846 ssymhead
->ssym
= ssym
;
6847 ssymhead
->count
= 0;
6848 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
6850 ssym
->st_name
= (*ind
)->st_name
;
6851 ssym
->st_info
= (*ind
)->st_info
;
6852 ssym
->st_other
= (*ind
)->st_other
;
6855 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
);
6861 /* Check if 2 sections define the same set of local and global
6865 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
6866 struct bfd_link_info
*info
)
6869 const struct elf_backend_data
*bed1
, *bed2
;
6870 Elf_Internal_Shdr
*hdr1
, *hdr2
;
6871 bfd_size_type symcount1
, symcount2
;
6872 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
6873 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
6874 Elf_Internal_Sym
*isym
, *isymend
;
6875 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
6876 bfd_size_type count1
, count2
, i
;
6883 /* If both are .gnu.linkonce sections, they have to have the same
6885 if (CONST_STRNEQ (sec1
->name
, ".gnu.linkonce")
6886 && CONST_STRNEQ (sec2
->name
, ".gnu.linkonce"))
6887 return strcmp (sec1
->name
+ sizeof ".gnu.linkonce",
6888 sec2
->name
+ sizeof ".gnu.linkonce") == 0;
6890 /* Both sections have to be in ELF. */
6891 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
6892 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
6895 if (elf_section_type (sec1
) != elf_section_type (sec2
))
6898 if ((elf_section_flags (sec1
) & SHF_GROUP
) != 0
6899 && (elf_section_flags (sec2
) & SHF_GROUP
) != 0)
6901 /* If both are members of section groups, they have to have the
6903 if (strcmp (elf_group_name (sec1
), elf_group_name (sec2
)) != 0)
6907 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
6908 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
6909 if (shndx1
== -1 || shndx2
== -1)
6912 bed1
= get_elf_backend_data (bfd1
);
6913 bed2
= get_elf_backend_data (bfd2
);
6914 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
6915 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
6916 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
6917 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
6919 if (symcount1
== 0 || symcount2
== 0)
6925 ssymbuf1
= elf_tdata (bfd1
)->symbuf
;
6926 ssymbuf2
= elf_tdata (bfd2
)->symbuf
;
6928 if (ssymbuf1
== NULL
)
6930 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
6932 if (isymbuf1
== NULL
)
6935 if (!info
->reduce_memory_overheads
)
6936 elf_tdata (bfd1
)->symbuf
= ssymbuf1
6937 = elf_create_symbuf (symcount1
, isymbuf1
);
6940 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
6942 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
6944 if (isymbuf2
== NULL
)
6947 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
6948 elf_tdata (bfd2
)->symbuf
= ssymbuf2
6949 = elf_create_symbuf (symcount2
, isymbuf2
);
6952 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
6954 /* Optimized faster version. */
6955 bfd_size_type lo
, hi
, mid
;
6956 struct elf_symbol
*symp
;
6957 struct elf_symbuf_symbol
*ssym
, *ssymend
;
6960 hi
= ssymbuf1
->count
;
6965 mid
= (lo
+ hi
) / 2;
6966 if ((unsigned int) shndx1
< ssymbuf1
[mid
].st_shndx
)
6968 else if ((unsigned int) shndx1
> ssymbuf1
[mid
].st_shndx
)
6972 count1
= ssymbuf1
[mid
].count
;
6979 hi
= ssymbuf2
->count
;
6984 mid
= (lo
+ hi
) / 2;
6985 if ((unsigned int) shndx2
< ssymbuf2
[mid
].st_shndx
)
6987 else if ((unsigned int) shndx2
> ssymbuf2
[mid
].st_shndx
)
6991 count2
= ssymbuf2
[mid
].count
;
6997 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7000 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
7001 symtable2
= bfd_malloc (count2
* sizeof (struct elf_symbol
));
7002 if (symtable1
== NULL
|| symtable2
== NULL
)
7006 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7007 ssym
< ssymend
; ssym
++, symp
++)
7009 symp
->u
.ssym
= ssym
;
7010 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7016 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7017 ssym
< ssymend
; ssym
++, symp
++)
7019 symp
->u
.ssym
= ssym
;
7020 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7025 /* Sort symbol by name. */
7026 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7027 elf_sym_name_compare
);
7028 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7029 elf_sym_name_compare
);
7031 for (i
= 0; i
< count1
; i
++)
7032 /* Two symbols must have the same binding, type and name. */
7033 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7034 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7035 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7042 symtable1
= bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7043 symtable2
= bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7044 if (symtable1
== NULL
|| symtable2
== NULL
)
7047 /* Count definitions in the section. */
7049 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7050 if (isym
->st_shndx
== (unsigned int) shndx1
)
7051 symtable1
[count1
++].u
.isym
= isym
;
7054 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7055 if (isym
->st_shndx
== (unsigned int) shndx2
)
7056 symtable2
[count2
++].u
.isym
= isym
;
7058 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7061 for (i
= 0; i
< count1
; i
++)
7063 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7064 symtable1
[i
].u
.isym
->st_name
);
7066 for (i
= 0; i
< count2
; i
++)
7068 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7069 symtable2
[i
].u
.isym
->st_name
);
7071 /* Sort symbol by name. */
7072 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7073 elf_sym_name_compare
);
7074 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7075 elf_sym_name_compare
);
7077 for (i
= 0; i
< count1
; i
++)
7078 /* Two symbols must have the same binding, type and name. */
7079 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7080 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7081 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7099 /* Return TRUE if 2 section types are compatible. */
7102 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7103 bfd
*bbfd
, const asection
*bsec
)
7107 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7108 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7111 return elf_section_type (asec
) == elf_section_type (bsec
);
7114 /* Final phase of ELF linker. */
7116 /* A structure we use to avoid passing large numbers of arguments. */
7118 struct elf_final_link_info
7120 /* General link information. */
7121 struct bfd_link_info
*info
;
7124 /* Symbol string table. */
7125 struct bfd_strtab_hash
*symstrtab
;
7126 /* .dynsym section. */
7127 asection
*dynsym_sec
;
7128 /* .hash section. */
7130 /* symbol version section (.gnu.version). */
7131 asection
*symver_sec
;
7132 /* Buffer large enough to hold contents of any section. */
7134 /* Buffer large enough to hold external relocs of any section. */
7135 void *external_relocs
;
7136 /* Buffer large enough to hold internal relocs of any section. */
7137 Elf_Internal_Rela
*internal_relocs
;
7138 /* Buffer large enough to hold external local symbols of any input
7140 bfd_byte
*external_syms
;
7141 /* And a buffer for symbol section indices. */
7142 Elf_External_Sym_Shndx
*locsym_shndx
;
7143 /* Buffer large enough to hold internal local symbols of any input
7145 Elf_Internal_Sym
*internal_syms
;
7146 /* Array large enough to hold a symbol index for each local symbol
7147 of any input BFD. */
7149 /* Array large enough to hold a section pointer for each local
7150 symbol of any input BFD. */
7151 asection
**sections
;
7152 /* Buffer to hold swapped out symbols. */
7154 /* And one for symbol section indices. */
7155 Elf_External_Sym_Shndx
*symshndxbuf
;
7156 /* Number of swapped out symbols in buffer. */
7157 size_t symbuf_count
;
7158 /* Number of symbols which fit in symbuf. */
7160 /* And same for symshndxbuf. */
7161 size_t shndxbuf_size
;
7164 /* This struct is used to pass information to elf_link_output_extsym. */
7166 struct elf_outext_info
7169 bfd_boolean localsyms
;
7170 struct elf_final_link_info
*finfo
;
7174 /* Support for evaluating a complex relocation.
7176 Complex relocations are generalized, self-describing relocations. The
7177 implementation of them consists of two parts: complex symbols, and the
7178 relocations themselves.
7180 The relocations are use a reserved elf-wide relocation type code (R_RELC
7181 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7182 information (start bit, end bit, word width, etc) into the addend. This
7183 information is extracted from CGEN-generated operand tables within gas.
7185 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7186 internal) representing prefix-notation expressions, including but not
7187 limited to those sorts of expressions normally encoded as addends in the
7188 addend field. The symbol mangling format is:
7191 | <unary-operator> ':' <node>
7192 | <binary-operator> ':' <node> ':' <node>
7195 <literal> := 's' <digits=N> ':' <N character symbol name>
7196 | 'S' <digits=N> ':' <N character section name>
7200 <binary-operator> := as in C
7201 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7204 set_symbol_value (bfd
* bfd_with_globals
,
7205 struct elf_final_link_info
* finfo
,
7209 bfd_boolean is_local
;
7210 Elf_Internal_Sym
* sym
;
7211 struct elf_link_hash_entry
** sym_hashes
;
7212 struct elf_link_hash_entry
* h
;
7214 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7215 sym
= finfo
->internal_syms
+ symidx
;
7216 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
7220 /* It is a local symbol: move it to the
7221 "absolute" section and give it a value. */
7222 sym
->st_shndx
= SHN_ABS
;
7223 sym
->st_value
= val
;
7227 /* It is a global symbol: set its link type
7228 to "defined" and give it a value. */
7229 h
= sym_hashes
[symidx
];
7230 while (h
->root
.type
== bfd_link_hash_indirect
7231 || h
->root
.type
== bfd_link_hash_warning
)
7232 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7233 h
->root
.type
= bfd_link_hash_defined
;
7234 h
->root
.u
.def
.value
= val
;
7235 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7240 resolve_symbol (const char * name
,
7242 struct elf_final_link_info
* finfo
,
7246 Elf_Internal_Sym
* sym
;
7247 struct bfd_link_hash_entry
* global_entry
;
7248 const char * candidate
= NULL
;
7249 Elf_Internal_Shdr
* symtab_hdr
;
7250 asection
* sec
= NULL
;
7253 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7255 for (i
= 0; i
< locsymcount
; ++ i
)
7257 sym
= finfo
->internal_syms
+ i
;
7258 sec
= finfo
->sections
[i
];
7260 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7263 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7264 symtab_hdr
->sh_link
,
7267 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
7268 name
, candidate
, (unsigned int)sym
->st_value
);
7270 if (candidate
&& strcmp (candidate
, name
) == 0)
7272 * result
= sym
->st_value
;
7274 if (sym
->st_shndx
> SHN_UNDEF
&&
7275 sym
->st_shndx
< SHN_LORESERVE
)
7278 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
7279 sec
->output_section
->name
,
7280 (unsigned int)sec
->output_section
->vma
,
7281 (unsigned int)sec
->output_offset
);
7283 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7286 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
7292 /* Hmm, haven't found it yet. perhaps it is a global. */
7293 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
7297 if (global_entry
->type
== bfd_link_hash_defined
7298 || global_entry
->type
== bfd_link_hash_defweak
)
7300 * result
= global_entry
->u
.def
.value
7301 + global_entry
->u
.def
.section
->output_section
->vma
7302 + global_entry
->u
.def
.section
->output_offset
;
7304 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
7305 global_entry
->root
.string
, (unsigned int)*result
);
7310 if (global_entry
->type
== bfd_link_hash_common
)
7312 *result
= global_entry
->u
.def
.value
+
7313 bfd_com_section_ptr
->output_section
->vma
+
7314 bfd_com_section_ptr
->output_offset
;
7316 printf ("Found COMMON symbol '%s' with value %8.8x\n",
7317 global_entry
->root
.string
, (unsigned int)*result
);
7326 resolve_section (const char * name
,
7327 asection
* sections
,
7333 for (curr
= sections
; curr
; curr
= curr
->next
)
7334 if (strcmp (curr
->name
, name
) == 0)
7336 *result
= curr
->vma
;
7340 /* Hmm. still haven't found it. try pseudo-section names. */
7341 for (curr
= sections
; curr
; curr
= curr
->next
)
7343 len
= strlen (curr
->name
);
7344 if (len
> strlen (name
))
7347 if (strncmp (curr
->name
, name
, len
) == 0)
7349 if (strncmp (".end", name
+ len
, 4) == 0)
7351 *result
= curr
->vma
+ curr
->size
;
7355 /* Insert more pseudo-section names here, if you like. */
7363 undefined_reference (const char * reftype
,
7366 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
7370 eval_symbol (bfd_vma
* result
,
7374 struct elf_final_link_info
* finfo
,
7376 bfd_vma section_offset
,
7384 const int bufsz
= 4096;
7385 char symbuf
[bufsz
];
7386 const char * symend
;
7387 bfd_boolean symbol_is_section
= FALSE
;
7392 if (len
< 1 || len
> bufsz
)
7394 bfd_set_error (bfd_error_invalid_operation
);
7401 * result
= addr
+ section_offset
;
7402 * advanced
= sym
+ 1;
7407 * result
= strtoul (sym
, advanced
, 16);
7411 symbol_is_section
= TRUE
;
7414 symlen
= strtol (sym
, &sym
, 10);
7415 ++ sym
; /* Skip the trailing ':'. */
7417 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
7419 bfd_set_error (bfd_error_invalid_operation
);
7423 memcpy (symbuf
, sym
, symlen
);
7424 symbuf
[symlen
] = '\0';
7425 * advanced
= sym
+ symlen
;
7427 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7428 the symbol as a section, or vice-versa. so we're pretty liberal in our
7429 interpretation here; section means "try section first", not "must be a
7430 section", and likewise with symbol. */
7432 if (symbol_is_section
)
7434 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
7435 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
7437 undefined_reference ("section", symbuf
);
7443 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
7444 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
7447 undefined_reference ("symbol", symbuf
);
7454 /* All that remains are operators. */
7456 #define UNARY_OP(op) \
7457 if (strncmp (sym, #op, strlen (#op)) == 0) \
7459 sym += strlen (#op); \
7462 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
7463 section_offset, locsymcount, signed_p) \
7467 * result = op ((signed)a); \
7474 #define BINARY_OP(op) \
7475 if (strncmp (sym, #op, strlen (#op)) == 0) \
7477 sym += strlen (#op); \
7480 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
7481 section_offset, locsymcount, signed_p) \
7485 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
7486 section_offset, locsymcount, signed_p) \
7490 * result = ((signed) a) op ((signed) b); \
7492 * result = a op b; \
7521 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7522 bfd_set_error (bfd_error_invalid_operation
);
7527 /* Entry point to evaluator, called from elf_link_input_bfd. */
7530 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
7531 struct elf_final_link_info
* finfo
,
7534 const struct elf_backend_data
* bed
;
7535 Elf_Internal_Shdr
* symtab_hdr
;
7536 struct elf_link_hash_entry
** sym_hashes
;
7537 asection
* reloc_sec
;
7538 bfd_boolean result
= TRUE
;
7540 /* For each section, we're going to check and see if it has any
7541 complex relocations, and we're going to evaluate any of them
7544 if (finfo
->info
->relocatable
)
7547 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7548 sym_hashes
= elf_sym_hashes (input_bfd
);
7549 bed
= get_elf_backend_data (input_bfd
);
7551 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
7553 Elf_Internal_Rela
* internal_relocs
;
7556 /* This section was omitted from the link. */
7557 if (! reloc_sec
->linker_mark
)
7560 /* Only process sections containing relocs. */
7561 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
7564 if (reloc_sec
->reloc_count
== 0)
7567 /* Read in the relocs for this section. */
7569 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
7570 (Elf_Internal_Rela
*) NULL
,
7572 if (internal_relocs
== NULL
)
7575 for (i
= reloc_sec
->reloc_count
; i
--;)
7577 Elf_Internal_Rela
* rel
;
7580 Elf_Internal_Sym
* sym
;
7582 bfd_vma section_offset
;
7586 rel
= internal_relocs
+ i
;
7587 section_offset
= reloc_sec
->output_section
->vma
7588 + reloc_sec
->output_offset
;
7589 addr
= rel
->r_offset
;
7591 index
= ELF32_R_SYM (rel
->r_info
);
7592 if (bed
->s
->arch_size
== 64)
7595 if (index
== STN_UNDEF
)
7598 if (index
< locsymcount
)
7600 /* The symbol is local. */
7601 sym
= finfo
->internal_syms
+ index
;
7603 /* We're only processing STT_RELC or STT_SRELC type symbols. */
7604 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
7605 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
7608 sym_name
= bfd_elf_string_from_elf_section
7609 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
7611 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
7615 /* The symbol is global. */
7616 struct elf_link_hash_entry
* h
;
7618 if (elf_bad_symtab (input_bfd
))
7621 h
= sym_hashes
[index
- locsymcount
];
7622 while ( h
->root
.type
== bfd_link_hash_indirect
7623 || h
->root
.type
== bfd_link_hash_warning
)
7624 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7626 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
7629 signed_p
= (h
->type
== STT_SRELC
);
7630 sym_name
= (char *) h
->root
.root
.string
;
7633 printf ("Encountered a complex symbol!");
7634 printf (" (input_bfd %s, section %s, reloc %ld\n",
7635 input_bfd
->filename
, reloc_sec
->name
, i
);
7636 printf (" symbol: idx %8.8lx, name %s\n",
7638 printf (" reloc : info %8.8lx, addr %8.8lx\n",
7640 printf (" Evaluating '%s' ...\n ", sym_name
);
7642 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
7643 finfo
, addr
, section_offset
, locsymcount
,
7645 /* Symbol evaluated OK. Update to absolute value. */
7646 set_symbol_value (input_bfd
, finfo
, index
, result
);
7652 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
7653 free (internal_relocs
);
7656 /* If nothing went wrong, then we adjusted
7657 everything we wanted to adjust. */
7662 put_value (bfd_vma size
,
7663 unsigned long chunksz
,
7666 bfd_byte
* location
)
7668 location
+= (size
- chunksz
);
7670 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7678 bfd_put_8 (input_bfd
, x
, location
);
7681 bfd_put_16 (input_bfd
, x
, location
);
7684 bfd_put_32 (input_bfd
, x
, location
);
7688 bfd_put_64 (input_bfd
, x
, location
);
7698 get_value (bfd_vma size
,
7699 unsigned long chunksz
,
7701 bfd_byte
* location
)
7705 for (; size
; size
-= chunksz
, location
+= chunksz
)
7713 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7716 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7719 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7723 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7734 decode_complex_addend
7735 (unsigned long * start
, /* in bits */
7736 unsigned long * oplen
, /* in bits */
7737 unsigned long * len
, /* in bits */
7738 unsigned long * wordsz
, /* in bytes */
7739 unsigned long * chunksz
, /* in bytes */
7740 unsigned long * lsb0_p
,
7741 unsigned long * signed_p
,
7742 unsigned long * trunc_p
,
7743 unsigned long encoded
)
7745 * start
= encoded
& 0x3F;
7746 * len
= (encoded
>> 6) & 0x3F;
7747 * oplen
= (encoded
>> 12) & 0x3F;
7748 * wordsz
= (encoded
>> 18) & 0xF;
7749 * chunksz
= (encoded
>> 22) & 0xF;
7750 * lsb0_p
= (encoded
>> 27) & 1;
7751 * signed_p
= (encoded
>> 28) & 1;
7752 * trunc_p
= (encoded
>> 29) & 1;
7756 bfd_elf_perform_complex_relocation
7757 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
7758 struct bfd_link_info
* info
,
7760 asection
* input_section
,
7761 bfd_byte
* contents
,
7762 Elf_Internal_Rela
* rel
,
7763 Elf_Internal_Sym
* local_syms
,
7764 asection
** local_sections
)
7766 const struct elf_backend_data
* bed
;
7767 Elf_Internal_Shdr
* symtab_hdr
;
7769 bfd_vma relocation
= 0, shift
, x
;
7772 unsigned long start
, oplen
, len
, wordsz
,
7773 chunksz
, lsb0_p
, signed_p
, trunc_p
;
7775 /* Perform this reloc, since it is complex.
7776 (this is not to say that it necessarily refers to a complex
7777 symbol; merely that it is a self-describing CGEN based reloc.
7778 i.e. the addend has the complete reloc information (bit start, end,
7779 word size, etc) encoded within it.). */
7780 r_symndx
= ELF32_R_SYM (rel
->r_info
);
7781 bed
= get_elf_backend_data (input_bfd
);
7782 if (bed
->s
->arch_size
== 64)
7786 printf ("Performing complex relocation %ld...\n", r_symndx
);
7789 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7790 if (r_symndx
< symtab_hdr
->sh_info
)
7792 /* The symbol is local. */
7793 Elf_Internal_Sym
* sym
;
7795 sym
= local_syms
+ r_symndx
;
7796 sec
= local_sections
[r_symndx
];
7797 relocation
= sym
->st_value
;
7798 if (sym
->st_shndx
> SHN_UNDEF
&&
7799 sym
->st_shndx
< SHN_LORESERVE
)
7800 relocation
+= (sec
->output_offset
+
7801 sec
->output_section
->vma
);
7805 /* The symbol is global. */
7806 struct elf_link_hash_entry
**sym_hashes
;
7807 struct elf_link_hash_entry
* h
;
7809 sym_hashes
= elf_sym_hashes (input_bfd
);
7810 h
= sym_hashes
[r_symndx
];
7812 while (h
->root
.type
== bfd_link_hash_indirect
7813 || h
->root
.type
== bfd_link_hash_warning
)
7814 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7816 if (h
->root
.type
== bfd_link_hash_defined
7817 || h
->root
.type
== bfd_link_hash_defweak
)
7819 sec
= h
->root
.u
.def
.section
;
7820 relocation
= h
->root
.u
.def
.value
;
7822 if (! bfd_is_abs_section (sec
))
7823 relocation
+= (sec
->output_section
->vma
7824 + sec
->output_offset
);
7826 if (h
->root
.type
== bfd_link_hash_undefined
7827 && !((*info
->callbacks
->undefined_symbol
)
7828 (info
, h
->root
.root
.string
, input_bfd
,
7829 input_section
, rel
->r_offset
,
7830 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7831 || ELF_ST_VISIBILITY (h
->other
))))
7835 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7836 & chunksz
, & lsb0_p
, & signed_p
,
7837 & trunc_p
, rel
->r_addend
);
7839 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7842 shift
= (start
+ 1) - len
;
7844 shift
= (8 * wordsz
) - (start
+ len
);
7846 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7849 printf ("Doing complex reloc: "
7850 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7851 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7852 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7853 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7854 oplen
, x
, mask
, relocation
);
7859 /* Now do an overflow check. */
7860 if (bfd_check_overflow ((signed_p
?
7861 complain_overflow_signed
:
7862 complain_overflow_unsigned
),
7863 len
, 0, (8 * wordsz
),
7864 relocation
) == bfd_reloc_overflow
)
7865 (*_bfd_error_handler
)
7866 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7868 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7869 relocation
, (signed_p
? "(signed) " : ""), mask
);
7873 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7876 printf (" relocation: %8.8lx\n"
7877 " shifted mask: %8.8lx\n"
7878 " shifted/masked reloc: %8.8lx\n"
7879 " result: %8.8lx\n",
7880 relocation
, (mask
<< shift
),
7881 ((relocation
& mask
) << shift
), x
);
7883 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7886 /* When performing a relocatable link, the input relocations are
7887 preserved. But, if they reference global symbols, the indices
7888 referenced must be updated. Update all the relocations in
7889 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7892 elf_link_adjust_relocs (bfd
*abfd
,
7893 Elf_Internal_Shdr
*rel_hdr
,
7895 struct elf_link_hash_entry
**rel_hash
)
7898 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7900 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7901 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7902 bfd_vma r_type_mask
;
7905 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7907 swap_in
= bed
->s
->swap_reloc_in
;
7908 swap_out
= bed
->s
->swap_reloc_out
;
7910 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7912 swap_in
= bed
->s
->swap_reloca_in
;
7913 swap_out
= bed
->s
->swap_reloca_out
;
7918 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7921 if (bed
->s
->arch_size
== 32)
7928 r_type_mask
= 0xffffffff;
7932 erela
= rel_hdr
->contents
;
7933 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7935 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7938 if (*rel_hash
== NULL
)
7941 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7943 (*swap_in
) (abfd
, erela
, irela
);
7944 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7945 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7946 | (irela
[j
].r_info
& r_type_mask
));
7947 (*swap_out
) (abfd
, irela
, erela
);
7951 struct elf_link_sort_rela
7957 enum elf_reloc_type_class type
;
7958 /* We use this as an array of size int_rels_per_ext_rel. */
7959 Elf_Internal_Rela rela
[1];
7963 elf_link_sort_cmp1 (const void *A
, const void *B
)
7965 const struct elf_link_sort_rela
*a
= A
;
7966 const struct elf_link_sort_rela
*b
= B
;
7967 int relativea
, relativeb
;
7969 relativea
= a
->type
== reloc_class_relative
;
7970 relativeb
= b
->type
== reloc_class_relative
;
7972 if (relativea
< relativeb
)
7974 if (relativea
> relativeb
)
7976 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7978 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7980 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7982 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7988 elf_link_sort_cmp2 (const void *A
, const void *B
)
7990 const struct elf_link_sort_rela
*a
= A
;
7991 const struct elf_link_sort_rela
*b
= B
;
7994 if (a
->u
.offset
< b
->u
.offset
)
7996 if (a
->u
.offset
> b
->u
.offset
)
7998 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7999 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
8004 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8006 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8012 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8014 asection
*dynamic_relocs
;
8017 bfd_size_type count
, size
;
8018 size_t i
, ret
, sort_elt
, ext_size
;
8019 bfd_byte
*sort
, *s_non_relative
, *p
;
8020 struct elf_link_sort_rela
*sq
;
8021 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8022 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8023 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8024 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8025 struct bfd_link_order
*lo
;
8027 bfd_boolean use_rela
;
8029 /* Find a dynamic reloc section. */
8030 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8031 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8032 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8033 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8035 bfd_boolean use_rela_initialised
= FALSE
;
8037 /* This is just here to stop gcc from complaining.
8038 It's initialization checking code is not perfect. */
8041 /* Both sections are present. Examine the sizes
8042 of the indirect sections to help us choose. */
8043 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8044 if (lo
->type
== bfd_indirect_link_order
)
8046 asection
*o
= lo
->u
.indirect
.section
;
8048 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8050 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8051 /* Section size is divisible by both rel and rela sizes.
8052 It is of no help to us. */
8056 /* Section size is only divisible by rela. */
8057 if (use_rela_initialised
&& (use_rela
== FALSE
))
8060 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8061 bfd_set_error (bfd_error_invalid_operation
);
8067 use_rela_initialised
= TRUE
;
8071 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8073 /* Section size is only divisible by rel. */
8074 if (use_rela_initialised
&& (use_rela
== TRUE
))
8077 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8078 bfd_set_error (bfd_error_invalid_operation
);
8084 use_rela_initialised
= TRUE
;
8089 /* The section size is not divisible by either - something is wrong. */
8091 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8092 bfd_set_error (bfd_error_invalid_operation
);
8097 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8098 if (lo
->type
== bfd_indirect_link_order
)
8100 asection
*o
= lo
->u
.indirect
.section
;
8102 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8104 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8105 /* Section size is divisible by both rel and rela sizes.
8106 It is of no help to us. */
8110 /* Section size is only divisible by rela. */
8111 if (use_rela_initialised
&& (use_rela
== FALSE
))
8114 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8115 bfd_set_error (bfd_error_invalid_operation
);
8121 use_rela_initialised
= TRUE
;
8125 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8127 /* Section size is only divisible by rel. */
8128 if (use_rela_initialised
&& (use_rela
== TRUE
))
8131 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8132 bfd_set_error (bfd_error_invalid_operation
);
8138 use_rela_initialised
= TRUE
;
8143 /* The section size is not divisible by either - something is wrong. */
8145 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8146 bfd_set_error (bfd_error_invalid_operation
);
8151 if (! use_rela_initialised
)
8155 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8157 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8164 dynamic_relocs
= rela_dyn
;
8165 ext_size
= bed
->s
->sizeof_rela
;
8166 swap_in
= bed
->s
->swap_reloca_in
;
8167 swap_out
= bed
->s
->swap_reloca_out
;
8171 dynamic_relocs
= rel_dyn
;
8172 ext_size
= bed
->s
->sizeof_rel
;
8173 swap_in
= bed
->s
->swap_reloc_in
;
8174 swap_out
= bed
->s
->swap_reloc_out
;
8178 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8179 if (lo
->type
== bfd_indirect_link_order
)
8180 size
+= lo
->u
.indirect
.section
->size
;
8182 if (size
!= dynamic_relocs
->size
)
8185 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8186 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8188 count
= dynamic_relocs
->size
/ ext_size
;
8189 sort
= bfd_zmalloc (sort_elt
* count
);
8193 (*info
->callbacks
->warning
)
8194 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8198 if (bed
->s
->arch_size
== 32)
8199 r_sym_mask
= ~(bfd_vma
) 0xff;
8201 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8203 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8204 if (lo
->type
== bfd_indirect_link_order
)
8206 bfd_byte
*erel
, *erelend
;
8207 asection
*o
= lo
->u
.indirect
.section
;
8209 if (o
->contents
== NULL
&& o
->size
!= 0)
8211 /* This is a reloc section that is being handled as a normal
8212 section. See bfd_section_from_shdr. We can't combine
8213 relocs in this case. */
8218 erelend
= o
->contents
+ o
->size
;
8219 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8221 while (erel
< erelend
)
8223 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8225 (*swap_in
) (abfd
, erel
, s
->rela
);
8226 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8227 s
->u
.sym_mask
= r_sym_mask
;
8233 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8235 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8237 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8238 if (s
->type
!= reloc_class_relative
)
8244 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8245 for (; i
< count
; i
++, p
+= sort_elt
)
8247 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8248 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8250 sp
->u
.offset
= sq
->rela
->r_offset
;
8253 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8255 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8256 if (lo
->type
== bfd_indirect_link_order
)
8258 bfd_byte
*erel
, *erelend
;
8259 asection
*o
= lo
->u
.indirect
.section
;
8262 erelend
= o
->contents
+ o
->size
;
8263 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8264 while (erel
< erelend
)
8266 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8267 (*swap_out
) (abfd
, s
->rela
, erel
);
8274 *psec
= dynamic_relocs
;
8278 /* Flush the output symbols to the file. */
8281 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
8282 const struct elf_backend_data
*bed
)
8284 if (finfo
->symbuf_count
> 0)
8286 Elf_Internal_Shdr
*hdr
;
8290 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
8291 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8292 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8293 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
8294 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
8297 hdr
->sh_size
+= amt
;
8298 finfo
->symbuf_count
= 0;
8304 /* Add a symbol to the output symbol table. */
8307 elf_link_output_sym (struct elf_final_link_info
*finfo
,
8309 Elf_Internal_Sym
*elfsym
,
8310 asection
*input_sec
,
8311 struct elf_link_hash_entry
*h
)
8314 Elf_External_Sym_Shndx
*destshndx
;
8315 bfd_boolean (*output_symbol_hook
)
8316 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8317 struct elf_link_hash_entry
*);
8318 const struct elf_backend_data
*bed
;
8320 bed
= get_elf_backend_data (finfo
->output_bfd
);
8321 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8322 if (output_symbol_hook
!= NULL
)
8324 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
8328 if (name
== NULL
|| *name
== '\0')
8329 elfsym
->st_name
= 0;
8330 else if (input_sec
->flags
& SEC_EXCLUDE
)
8331 elfsym
->st_name
= 0;
8334 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
8336 if (elfsym
->st_name
== (unsigned long) -1)
8340 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
8342 if (! elf_link_flush_output_syms (finfo
, bed
))
8346 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8347 destshndx
= finfo
->symshndxbuf
;
8348 if (destshndx
!= NULL
)
8350 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
8354 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8355 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
8356 if (destshndx
== NULL
)
8358 memset ((char *) destshndx
+ amt
, 0, amt
);
8359 finfo
->shndxbuf_size
*= 2;
8361 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
8364 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
8365 finfo
->symbuf_count
+= 1;
8366 bfd_get_symcount (finfo
->output_bfd
) += 1;
8371 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8374 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8376 if (sym
->st_shndx
> SHN_HIRESERVE
)
8378 /* The gABI doesn't support dynamic symbols in output sections
8380 (*_bfd_error_handler
)
8381 (_("%B: Too many sections: %d (>= %d)"),
8382 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
8383 bfd_set_error (bfd_error_nonrepresentable_section
);
8389 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8390 allowing an unsatisfied unversioned symbol in the DSO to match a
8391 versioned symbol that would normally require an explicit version.
8392 We also handle the case that a DSO references a hidden symbol
8393 which may be satisfied by a versioned symbol in another DSO. */
8396 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8397 const struct elf_backend_data
*bed
,
8398 struct elf_link_hash_entry
*h
)
8401 struct elf_link_loaded_list
*loaded
;
8403 if (!is_elf_hash_table (info
->hash
))
8406 switch (h
->root
.type
)
8412 case bfd_link_hash_undefined
:
8413 case bfd_link_hash_undefweak
:
8414 abfd
= h
->root
.u
.undef
.abfd
;
8415 if ((abfd
->flags
& DYNAMIC
) == 0
8416 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8420 case bfd_link_hash_defined
:
8421 case bfd_link_hash_defweak
:
8422 abfd
= h
->root
.u
.def
.section
->owner
;
8425 case bfd_link_hash_common
:
8426 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8429 BFD_ASSERT (abfd
!= NULL
);
8431 for (loaded
= elf_hash_table (info
)->loaded
;
8433 loaded
= loaded
->next
)
8436 Elf_Internal_Shdr
*hdr
;
8437 bfd_size_type symcount
;
8438 bfd_size_type extsymcount
;
8439 bfd_size_type extsymoff
;
8440 Elf_Internal_Shdr
*versymhdr
;
8441 Elf_Internal_Sym
*isym
;
8442 Elf_Internal_Sym
*isymend
;
8443 Elf_Internal_Sym
*isymbuf
;
8444 Elf_External_Versym
*ever
;
8445 Elf_External_Versym
*extversym
;
8447 input
= loaded
->abfd
;
8449 /* We check each DSO for a possible hidden versioned definition. */
8451 || (input
->flags
& DYNAMIC
) == 0
8452 || elf_dynversym (input
) == 0)
8455 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8457 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8458 if (elf_bad_symtab (input
))
8460 extsymcount
= symcount
;
8465 extsymcount
= symcount
- hdr
->sh_info
;
8466 extsymoff
= hdr
->sh_info
;
8469 if (extsymcount
== 0)
8472 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8474 if (isymbuf
== NULL
)
8477 /* Read in any version definitions. */
8478 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8479 extversym
= bfd_malloc (versymhdr
->sh_size
);
8480 if (extversym
== NULL
)
8483 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8484 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8485 != versymhdr
->sh_size
))
8493 ever
= extversym
+ extsymoff
;
8494 isymend
= isymbuf
+ extsymcount
;
8495 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8498 Elf_Internal_Versym iver
;
8499 unsigned short version_index
;
8501 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8502 || isym
->st_shndx
== SHN_UNDEF
)
8505 name
= bfd_elf_string_from_elf_section (input
,
8508 if (strcmp (name
, h
->root
.root
.string
) != 0)
8511 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8513 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
8515 /* If we have a non-hidden versioned sym, then it should
8516 have provided a definition for the undefined sym. */
8520 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8521 if (version_index
== 1 || version_index
== 2)
8523 /* This is the base or first version. We can use it. */
8537 /* Add an external symbol to the symbol table. This is called from
8538 the hash table traversal routine. When generating a shared object,
8539 we go through the symbol table twice. The first time we output
8540 anything that might have been forced to local scope in a version
8541 script. The second time we output the symbols that are still
8545 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
8547 struct elf_outext_info
*eoinfo
= data
;
8548 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
8550 Elf_Internal_Sym sym
;
8551 asection
*input_sec
;
8552 const struct elf_backend_data
*bed
;
8554 if (h
->root
.type
== bfd_link_hash_warning
)
8556 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8557 if (h
->root
.type
== bfd_link_hash_new
)
8561 /* Decide whether to output this symbol in this pass. */
8562 if (eoinfo
->localsyms
)
8564 if (!h
->forced_local
)
8569 if (h
->forced_local
)
8573 bed
= get_elf_backend_data (finfo
->output_bfd
);
8575 if (h
->root
.type
== bfd_link_hash_undefined
)
8577 /* If we have an undefined symbol reference here then it must have
8578 come from a shared library that is being linked in. (Undefined
8579 references in regular files have already been handled). */
8580 bfd_boolean ignore_undef
= FALSE
;
8582 /* Some symbols may be special in that the fact that they're
8583 undefined can be safely ignored - let backend determine that. */
8584 if (bed
->elf_backend_ignore_undef_symbol
)
8585 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8587 /* If we are reporting errors for this situation then do so now. */
8588 if (ignore_undef
== FALSE
8591 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
8592 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8594 if (! (finfo
->info
->callbacks
->undefined_symbol
8595 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
8596 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
8598 eoinfo
->failed
= TRUE
;
8604 /* We should also warn if a forced local symbol is referenced from
8605 shared libraries. */
8606 if (! finfo
->info
->relocatable
8607 && (! finfo
->info
->shared
)
8612 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
8614 (*_bfd_error_handler
)
8615 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8617 h
->root
.u
.def
.section
== bfd_abs_section_ptr
8618 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
8619 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
8621 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
8622 ? "hidden" : "local",
8623 h
->root
.root
.string
);
8624 eoinfo
->failed
= TRUE
;
8628 /* We don't want to output symbols that have never been mentioned by
8629 a regular file, or that we have been told to strip. However, if
8630 h->indx is set to -2, the symbol is used by a reloc and we must
8634 else if ((h
->def_dynamic
8636 || h
->root
.type
== bfd_link_hash_new
)
8640 else if (finfo
->info
->strip
== strip_all
)
8642 else if (finfo
->info
->strip
== strip_some
8643 && bfd_hash_lookup (finfo
->info
->keep_hash
,
8644 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8646 else if (finfo
->info
->strip_discarded
8647 && (h
->root
.type
== bfd_link_hash_defined
8648 || h
->root
.type
== bfd_link_hash_defweak
)
8649 && elf_discarded_section (h
->root
.u
.def
.section
))
8654 /* If we're stripping it, and it's not a dynamic symbol, there's
8655 nothing else to do unless it is a forced local symbol. */
8658 && !h
->forced_local
)
8662 sym
.st_size
= h
->size
;
8663 sym
.st_other
= h
->other
;
8664 if (h
->forced_local
)
8665 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8666 else if (h
->root
.type
== bfd_link_hash_undefweak
8667 || h
->root
.type
== bfd_link_hash_defweak
)
8668 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8670 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8672 switch (h
->root
.type
)
8675 case bfd_link_hash_new
:
8676 case bfd_link_hash_warning
:
8680 case bfd_link_hash_undefined
:
8681 case bfd_link_hash_undefweak
:
8682 input_sec
= bfd_und_section_ptr
;
8683 sym
.st_shndx
= SHN_UNDEF
;
8686 case bfd_link_hash_defined
:
8687 case bfd_link_hash_defweak
:
8689 input_sec
= h
->root
.u
.def
.section
;
8690 if (input_sec
->output_section
!= NULL
)
8693 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
8694 input_sec
->output_section
);
8695 if (sym
.st_shndx
== SHN_BAD
)
8697 (*_bfd_error_handler
)
8698 (_("%B: could not find output section %A for input section %A"),
8699 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8700 eoinfo
->failed
= TRUE
;
8704 /* ELF symbols in relocatable files are section relative,
8705 but in nonrelocatable files they are virtual
8707 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8708 if (! finfo
->info
->relocatable
)
8710 sym
.st_value
+= input_sec
->output_section
->vma
;
8711 if (h
->type
== STT_TLS
)
8713 /* STT_TLS symbols are relative to PT_TLS segment
8715 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8716 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8722 BFD_ASSERT (input_sec
->owner
== NULL
8723 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8724 sym
.st_shndx
= SHN_UNDEF
;
8725 input_sec
= bfd_und_section_ptr
;
8730 case bfd_link_hash_common
:
8731 input_sec
= h
->root
.u
.c
.p
->section
;
8732 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8733 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8736 case bfd_link_hash_indirect
:
8737 /* These symbols are created by symbol versioning. They point
8738 to the decorated version of the name. For example, if the
8739 symbol foo@@GNU_1.2 is the default, which should be used when
8740 foo is used with no version, then we add an indirect symbol
8741 foo which points to foo@@GNU_1.2. We ignore these symbols,
8742 since the indirected symbol is already in the hash table. */
8746 /* Give the processor backend a chance to tweak the symbol value,
8747 and also to finish up anything that needs to be done for this
8748 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8749 forced local syms when non-shared is due to a historical quirk. */
8750 if ((h
->dynindx
!= -1
8752 && ((finfo
->info
->shared
8753 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8754 || h
->root
.type
!= bfd_link_hash_undefweak
))
8755 || !h
->forced_local
)
8756 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8758 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8759 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8761 eoinfo
->failed
= TRUE
;
8766 /* If we are marking the symbol as undefined, and there are no
8767 non-weak references to this symbol from a regular object, then
8768 mark the symbol as weak undefined; if there are non-weak
8769 references, mark the symbol as strong. We can't do this earlier,
8770 because it might not be marked as undefined until the
8771 finish_dynamic_symbol routine gets through with it. */
8772 if (sym
.st_shndx
== SHN_UNDEF
8774 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8775 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8779 if (h
->ref_regular_nonweak
)
8780 bindtype
= STB_GLOBAL
;
8782 bindtype
= STB_WEAK
;
8783 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8786 /* If a non-weak symbol with non-default visibility is not defined
8787 locally, it is a fatal error. */
8788 if (! finfo
->info
->relocatable
8789 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8790 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8791 && h
->root
.type
== bfd_link_hash_undefined
8794 (*_bfd_error_handler
)
8795 (_("%B: %s symbol `%s' isn't defined"),
8797 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8799 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8800 ? "internal" : "hidden",
8801 h
->root
.root
.string
);
8802 eoinfo
->failed
= TRUE
;
8806 /* If this symbol should be put in the .dynsym section, then put it
8807 there now. We already know the symbol index. We also fill in
8808 the entry in the .hash section. */
8809 if (h
->dynindx
!= -1
8810 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8814 sym
.st_name
= h
->dynstr_index
;
8815 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8816 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8818 eoinfo
->failed
= TRUE
;
8821 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8823 if (finfo
->hash_sec
!= NULL
)
8825 size_t hash_entry_size
;
8826 bfd_byte
*bucketpos
;
8831 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8832 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8835 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8836 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8837 + (bucket
+ 2) * hash_entry_size
);
8838 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8839 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8840 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8841 ((bfd_byte
*) finfo
->hash_sec
->contents
8842 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8845 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8847 Elf_Internal_Versym iversym
;
8848 Elf_External_Versym
*eversym
;
8850 if (!h
->def_regular
)
8852 if (h
->verinfo
.verdef
== NULL
)
8853 iversym
.vs_vers
= 0;
8855 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8859 if (h
->verinfo
.vertree
== NULL
)
8860 iversym
.vs_vers
= 1;
8862 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8863 if (finfo
->info
->create_default_symver
)
8868 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8870 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8871 eversym
+= h
->dynindx
;
8872 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8876 /* If we're stripping it, then it was just a dynamic symbol, and
8877 there's nothing else to do. */
8878 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8881 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8883 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8885 eoinfo
->failed
= TRUE
;
8892 /* Return TRUE if special handling is done for relocs in SEC against
8893 symbols defined in discarded sections. */
8896 elf_section_ignore_discarded_relocs (asection
*sec
)
8898 const struct elf_backend_data
*bed
;
8900 switch (sec
->sec_info_type
)
8902 case ELF_INFO_TYPE_STABS
:
8903 case ELF_INFO_TYPE_EH_FRAME
:
8909 bed
= get_elf_backend_data (sec
->owner
);
8910 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8911 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8917 /* Return a mask saying how ld should treat relocations in SEC against
8918 symbols defined in discarded sections. If this function returns
8919 COMPLAIN set, ld will issue a warning message. If this function
8920 returns PRETEND set, and the discarded section was link-once and the
8921 same size as the kept link-once section, ld will pretend that the
8922 symbol was actually defined in the kept section. Otherwise ld will
8923 zero the reloc (at least that is the intent, but some cooperation by
8924 the target dependent code is needed, particularly for REL targets). */
8927 _bfd_elf_default_action_discarded (asection
*sec
)
8929 if (sec
->flags
& SEC_DEBUGGING
)
8932 if (strcmp (".eh_frame", sec
->name
) == 0)
8935 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8938 return COMPLAIN
| PRETEND
;
8941 /* Find a match between a section and a member of a section group. */
8944 match_group_member (asection
*sec
, asection
*group
,
8945 struct bfd_link_info
*info
)
8947 asection
*first
= elf_next_in_group (group
);
8948 asection
*s
= first
;
8952 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8955 s
= elf_next_in_group (s
);
8963 /* Check if the kept section of a discarded section SEC can be used
8964 to replace it. Return the replacement if it is OK. Otherwise return
8968 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8972 kept
= sec
->kept_section
;
8975 if ((kept
->flags
& SEC_GROUP
) != 0)
8976 kept
= match_group_member (sec
, kept
, info
);
8977 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8979 sec
->kept_section
= kept
;
8984 /* Link an input file into the linker output file. This function
8985 handles all the sections and relocations of the input file at once.
8986 This is so that we only have to read the local symbols once, and
8987 don't have to keep them in memory. */
8990 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8992 int (*relocate_section
)
8993 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8994 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8996 Elf_Internal_Shdr
*symtab_hdr
;
8999 Elf_Internal_Sym
*isymbuf
;
9000 Elf_Internal_Sym
*isym
;
9001 Elf_Internal_Sym
*isymend
;
9003 asection
**ppsection
;
9005 const struct elf_backend_data
*bed
;
9006 struct elf_link_hash_entry
**sym_hashes
;
9008 output_bfd
= finfo
->output_bfd
;
9009 bed
= get_elf_backend_data (output_bfd
);
9010 relocate_section
= bed
->elf_backend_relocate_section
;
9012 /* If this is a dynamic object, we don't want to do anything here:
9013 we don't want the local symbols, and we don't want the section
9015 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9018 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9019 if (elf_bad_symtab (input_bfd
))
9021 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9026 locsymcount
= symtab_hdr
->sh_info
;
9027 extsymoff
= symtab_hdr
->sh_info
;
9030 /* Read the local symbols. */
9031 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9032 if (isymbuf
== NULL
&& locsymcount
!= 0)
9034 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9035 finfo
->internal_syms
,
9036 finfo
->external_syms
,
9037 finfo
->locsym_shndx
);
9038 if (isymbuf
== NULL
)
9041 /* evaluate_complex_relocation_symbols looks for symbols in
9042 finfo->internal_syms. */
9043 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
9045 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9046 finfo
->internal_syms
,
9047 finfo
->external_syms
,
9048 finfo
->locsym_shndx
);
9051 /* Find local symbol sections and adjust values of symbols in
9052 SEC_MERGE sections. Write out those local symbols we know are
9053 going into the output file. */
9054 isymend
= isymbuf
+ locsymcount
;
9055 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
9057 isym
++, pindex
++, ppsection
++)
9061 Elf_Internal_Sym osym
;
9065 if (elf_bad_symtab (input_bfd
))
9067 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9074 if (isym
->st_shndx
== SHN_UNDEF
)
9075 isec
= bfd_und_section_ptr
;
9076 else if (isym
->st_shndx
< SHN_LORESERVE
9077 || isym
->st_shndx
> SHN_HIRESERVE
)
9079 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9081 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
9082 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9084 _bfd_merged_section_offset (output_bfd
, &isec
,
9085 elf_section_data (isec
)->sec_info
,
9088 else if (isym
->st_shndx
== SHN_ABS
)
9089 isec
= bfd_abs_section_ptr
;
9090 else if (isym
->st_shndx
== SHN_COMMON
)
9091 isec
= bfd_com_section_ptr
;
9094 /* Don't attempt to output symbols with st_shnx in the
9095 reserved range other than SHN_ABS and SHN_COMMON. */
9102 /* Don't output the first, undefined, symbol. */
9103 if (ppsection
== finfo
->sections
)
9106 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9108 /* We never output section symbols. Instead, we use the
9109 section symbol of the corresponding section in the output
9114 /* If we are stripping all symbols, we don't want to output this
9116 if (finfo
->info
->strip
== strip_all
)
9119 /* If we are discarding all local symbols, we don't want to
9120 output this one. If we are generating a relocatable output
9121 file, then some of the local symbols may be required by
9122 relocs; we output them below as we discover that they are
9124 if (finfo
->info
->discard
== discard_all
)
9127 /* If this symbol is defined in a section which we are
9128 discarding, we don't need to keep it. */
9129 if (isym
->st_shndx
!= SHN_UNDEF
9130 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9132 || bfd_section_removed_from_list (output_bfd
,
9133 isec
->output_section
)))
9136 /* Get the name of the symbol. */
9137 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9142 /* See if we are discarding symbols with this name. */
9143 if ((finfo
->info
->strip
== strip_some
9144 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9146 || (((finfo
->info
->discard
== discard_sec_merge
9147 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
9148 || finfo
->info
->discard
== discard_l
)
9149 && bfd_is_local_label_name (input_bfd
, name
)))
9152 /* If we get here, we are going to output this symbol. */
9156 /* Adjust the section index for the output file. */
9157 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9158 isec
->output_section
);
9159 if (osym
.st_shndx
== SHN_BAD
)
9162 *pindex
= bfd_get_symcount (output_bfd
);
9164 /* ELF symbols in relocatable files are section relative, but
9165 in executable files they are virtual addresses. Note that
9166 this code assumes that all ELF sections have an associated
9167 BFD section with a reasonable value for output_offset; below
9168 we assume that they also have a reasonable value for
9169 output_section. Any special sections must be set up to meet
9170 these requirements. */
9171 osym
.st_value
+= isec
->output_offset
;
9172 if (! finfo
->info
->relocatable
)
9174 osym
.st_value
+= isec
->output_section
->vma
;
9175 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9177 /* STT_TLS symbols are relative to PT_TLS segment base. */
9178 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
9179 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
9183 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
9187 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
9190 /* Relocate the contents of each section. */
9191 sym_hashes
= elf_sym_hashes (input_bfd
);
9192 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9196 if (! o
->linker_mark
)
9198 /* This section was omitted from the link. */
9202 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9203 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9206 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9208 /* Section was created by _bfd_elf_link_create_dynamic_sections
9213 /* Get the contents of the section. They have been cached by a
9214 relaxation routine. Note that o is a section in an input
9215 file, so the contents field will not have been set by any of
9216 the routines which work on output files. */
9217 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9218 contents
= elf_section_data (o
)->this_hdr
.contents
;
9221 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
9223 contents
= finfo
->contents
;
9224 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
9228 if ((o
->flags
& SEC_RELOC
) != 0)
9230 Elf_Internal_Rela
*internal_relocs
;
9231 bfd_vma r_type_mask
;
9235 /* Get the swapped relocs. */
9237 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
9238 finfo
->internal_relocs
, FALSE
);
9239 if (internal_relocs
== NULL
9240 && o
->reloc_count
> 0)
9243 if (bed
->s
->arch_size
== 32)
9250 r_type_mask
= 0xffffffff;
9254 /* Run through the relocs looking for any against symbols
9255 from discarded sections and section symbols from
9256 removed link-once sections. Complain about relocs
9257 against discarded sections. Zero relocs against removed
9258 link-once sections. */
9259 if (!elf_section_ignore_discarded_relocs (o
))
9261 Elf_Internal_Rela
*rel
, *relend
;
9262 unsigned int action
= (*bed
->action_discarded
) (o
);
9264 rel
= internal_relocs
;
9265 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9266 for ( ; rel
< relend
; rel
++)
9268 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9269 asection
**ps
, *sec
;
9270 struct elf_link_hash_entry
*h
= NULL
;
9271 const char *sym_name
;
9273 if (r_symndx
== STN_UNDEF
)
9276 if (r_symndx
>= locsymcount
9277 || (elf_bad_symtab (input_bfd
)
9278 && finfo
->sections
[r_symndx
] == NULL
))
9280 h
= sym_hashes
[r_symndx
- extsymoff
];
9282 /* Badly formatted input files can contain relocs that
9283 reference non-existant symbols. Check here so that
9284 we do not seg fault. */
9289 sprintf_vma (buffer
, rel
->r_info
);
9290 (*_bfd_error_handler
)
9291 (_("error: %B contains a reloc (0x%s) for section %A "
9292 "that references a non-existent global symbol"),
9293 input_bfd
, o
, buffer
);
9294 bfd_set_error (bfd_error_bad_value
);
9298 while (h
->root
.type
== bfd_link_hash_indirect
9299 || h
->root
.type
== bfd_link_hash_warning
)
9300 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9302 if (h
->root
.type
!= bfd_link_hash_defined
9303 && h
->root
.type
!= bfd_link_hash_defweak
)
9306 ps
= &h
->root
.u
.def
.section
;
9307 sym_name
= h
->root
.root
.string
;
9311 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9312 ps
= &finfo
->sections
[r_symndx
];
9313 sym_name
= bfd_elf_sym_name (input_bfd
,
9318 /* Complain if the definition comes from a
9319 discarded section. */
9320 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
9322 BFD_ASSERT (r_symndx
!= 0);
9323 if (action
& COMPLAIN
)
9324 (*finfo
->info
->callbacks
->einfo
)
9325 (_("%X`%s' referenced in section `%A' of %B: "
9326 "defined in discarded section `%A' of %B\n"),
9327 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9329 /* Try to do the best we can to support buggy old
9330 versions of gcc. Pretend that the symbol is
9331 really defined in the kept linkonce section.
9332 FIXME: This is quite broken. Modifying the
9333 symbol here means we will be changing all later
9334 uses of the symbol, not just in this section. */
9335 if (action
& PRETEND
)
9339 kept
= _bfd_elf_check_kept_section (sec
,
9351 /* Relocate the section by invoking a back end routine.
9353 The back end routine is responsible for adjusting the
9354 section contents as necessary, and (if using Rela relocs
9355 and generating a relocatable output file) adjusting the
9356 reloc addend as necessary.
9358 The back end routine does not have to worry about setting
9359 the reloc address or the reloc symbol index.
9361 The back end routine is given a pointer to the swapped in
9362 internal symbols, and can access the hash table entries
9363 for the external symbols via elf_sym_hashes (input_bfd).
9365 When generating relocatable output, the back end routine
9366 must handle STB_LOCAL/STT_SECTION symbols specially. The
9367 output symbol is going to be a section symbol
9368 corresponding to the output section, which will require
9369 the addend to be adjusted. */
9371 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
9372 input_bfd
, o
, contents
,
9380 || finfo
->info
->relocatable
9381 || finfo
->info
->emitrelocations
)
9383 Elf_Internal_Rela
*irela
;
9384 Elf_Internal_Rela
*irelaend
;
9385 bfd_vma last_offset
;
9386 struct elf_link_hash_entry
**rel_hash
;
9387 struct elf_link_hash_entry
**rel_hash_list
;
9388 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
9389 unsigned int next_erel
;
9390 bfd_boolean rela_normal
;
9392 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
9393 rela_normal
= (bed
->rela_normal
9394 && (input_rel_hdr
->sh_entsize
9395 == bed
->s
->sizeof_rela
));
9397 /* Adjust the reloc addresses and symbol indices. */
9399 irela
= internal_relocs
;
9400 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9401 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
9402 + elf_section_data (o
->output_section
)->rel_count
9403 + elf_section_data (o
->output_section
)->rel_count2
);
9404 rel_hash_list
= rel_hash
;
9405 last_offset
= o
->output_offset
;
9406 if (!finfo
->info
->relocatable
)
9407 last_offset
+= o
->output_section
->vma
;
9408 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9410 unsigned long r_symndx
;
9412 Elf_Internal_Sym sym
;
9414 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9420 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9423 if (irela
->r_offset
>= (bfd_vma
) -2)
9425 /* This is a reloc for a deleted entry or somesuch.
9426 Turn it into an R_*_NONE reloc, at the same
9427 offset as the last reloc. elf_eh_frame.c and
9428 bfd_elf_discard_info rely on reloc offsets
9430 irela
->r_offset
= last_offset
;
9432 irela
->r_addend
= 0;
9436 irela
->r_offset
+= o
->output_offset
;
9438 /* Relocs in an executable have to be virtual addresses. */
9439 if (!finfo
->info
->relocatable
)
9440 irela
->r_offset
+= o
->output_section
->vma
;
9442 last_offset
= irela
->r_offset
;
9444 r_symndx
= irela
->r_info
>> r_sym_shift
;
9445 if (r_symndx
== STN_UNDEF
)
9448 if (r_symndx
>= locsymcount
9449 || (elf_bad_symtab (input_bfd
)
9450 && finfo
->sections
[r_symndx
] == NULL
))
9452 struct elf_link_hash_entry
*rh
;
9455 /* This is a reloc against a global symbol. We
9456 have not yet output all the local symbols, so
9457 we do not know the symbol index of any global
9458 symbol. We set the rel_hash entry for this
9459 reloc to point to the global hash table entry
9460 for this symbol. The symbol index is then
9461 set at the end of bfd_elf_final_link. */
9462 indx
= r_symndx
- extsymoff
;
9463 rh
= elf_sym_hashes (input_bfd
)[indx
];
9464 while (rh
->root
.type
== bfd_link_hash_indirect
9465 || rh
->root
.type
== bfd_link_hash_warning
)
9466 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9468 /* Setting the index to -2 tells
9469 elf_link_output_extsym that this symbol is
9471 BFD_ASSERT (rh
->indx
< 0);
9479 /* This is a reloc against a local symbol. */
9482 sym
= isymbuf
[r_symndx
];
9483 sec
= finfo
->sections
[r_symndx
];
9484 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9486 /* I suppose the backend ought to fill in the
9487 section of any STT_SECTION symbol against a
9488 processor specific section. */
9490 if (bfd_is_abs_section (sec
))
9492 else if (sec
== NULL
|| sec
->owner
== NULL
)
9494 bfd_set_error (bfd_error_bad_value
);
9499 asection
*osec
= sec
->output_section
;
9501 /* If we have discarded a section, the output
9502 section will be the absolute section. In
9503 case of discarded SEC_MERGE sections, use
9504 the kept section. relocate_section should
9505 have already handled discarded linkonce
9507 if (bfd_is_abs_section (osec
)
9508 && sec
->kept_section
!= NULL
9509 && sec
->kept_section
->output_section
!= NULL
)
9511 osec
= sec
->kept_section
->output_section
;
9512 irela
->r_addend
-= osec
->vma
;
9515 if (!bfd_is_abs_section (osec
))
9517 r_symndx
= osec
->target_index
;
9520 struct elf_link_hash_table
*htab
;
9523 htab
= elf_hash_table (finfo
->info
);
9524 oi
= htab
->text_index_section
;
9525 if ((osec
->flags
& SEC_READONLY
) == 0
9526 && htab
->data_index_section
!= NULL
)
9527 oi
= htab
->data_index_section
;
9531 irela
->r_addend
+= osec
->vma
- oi
->vma
;
9532 r_symndx
= oi
->target_index
;
9536 BFD_ASSERT (r_symndx
!= 0);
9540 /* Adjust the addend according to where the
9541 section winds up in the output section. */
9543 irela
->r_addend
+= sec
->output_offset
;
9547 if (finfo
->indices
[r_symndx
] == -1)
9549 unsigned long shlink
;
9553 if (finfo
->info
->strip
== strip_all
)
9555 /* You can't do ld -r -s. */
9556 bfd_set_error (bfd_error_invalid_operation
);
9560 /* This symbol was skipped earlier, but
9561 since it is needed by a reloc, we
9562 must output it now. */
9563 shlink
= symtab_hdr
->sh_link
;
9564 name
= (bfd_elf_string_from_elf_section
9565 (input_bfd
, shlink
, sym
.st_name
));
9569 osec
= sec
->output_section
;
9571 _bfd_elf_section_from_bfd_section (output_bfd
,
9573 if (sym
.st_shndx
== SHN_BAD
)
9576 sym
.st_value
+= sec
->output_offset
;
9577 if (! finfo
->info
->relocatable
)
9579 sym
.st_value
+= osec
->vma
;
9580 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9582 /* STT_TLS symbols are relative to PT_TLS
9584 BFD_ASSERT (elf_hash_table (finfo
->info
)
9586 sym
.st_value
-= (elf_hash_table (finfo
->info
)
9591 finfo
->indices
[r_symndx
]
9592 = bfd_get_symcount (output_bfd
);
9594 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
9599 r_symndx
= finfo
->indices
[r_symndx
];
9602 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9603 | (irela
->r_info
& r_type_mask
));
9606 /* Swap out the relocs. */
9607 if (input_rel_hdr
->sh_size
!= 0
9608 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
9614 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
9615 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
9617 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9618 * bed
->s
->int_rels_per_ext_rel
);
9619 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9620 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9629 /* Write out the modified section contents. */
9630 if (bed
->elf_backend_write_section
9631 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
9634 /* Section written out. */
9636 else switch (o
->sec_info_type
)
9638 case ELF_INFO_TYPE_STABS
:
9639 if (! (_bfd_write_section_stabs
9641 &elf_hash_table (finfo
->info
)->stab_info
,
9642 o
, &elf_section_data (o
)->sec_info
, contents
)))
9645 case ELF_INFO_TYPE_MERGE
:
9646 if (! _bfd_write_merged_section (output_bfd
, o
,
9647 elf_section_data (o
)->sec_info
))
9650 case ELF_INFO_TYPE_EH_FRAME
:
9652 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
9659 if (! (o
->flags
& SEC_EXCLUDE
)
9660 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
9662 (file_ptr
) o
->output_offset
,
9673 /* Generate a reloc when linking an ELF file. This is a reloc
9674 requested by the linker, and does not come from any input file. This
9675 is used to build constructor and destructor tables when linking
9679 elf_reloc_link_order (bfd
*output_bfd
,
9680 struct bfd_link_info
*info
,
9681 asection
*output_section
,
9682 struct bfd_link_order
*link_order
)
9684 reloc_howto_type
*howto
;
9688 struct elf_link_hash_entry
**rel_hash_ptr
;
9689 Elf_Internal_Shdr
*rel_hdr
;
9690 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
9691 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
9695 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
9698 bfd_set_error (bfd_error_bad_value
);
9702 addend
= link_order
->u
.reloc
.p
->addend
;
9704 /* Figure out the symbol index. */
9705 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
9706 + elf_section_data (output_section
)->rel_count
9707 + elf_section_data (output_section
)->rel_count2
);
9708 if (link_order
->type
== bfd_section_reloc_link_order
)
9710 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
9711 BFD_ASSERT (indx
!= 0);
9712 *rel_hash_ptr
= NULL
;
9716 struct elf_link_hash_entry
*h
;
9718 /* Treat a reloc against a defined symbol as though it were
9719 actually against the section. */
9720 h
= ((struct elf_link_hash_entry
*)
9721 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
9722 link_order
->u
.reloc
.p
->u
.name
,
9723 FALSE
, FALSE
, TRUE
));
9725 && (h
->root
.type
== bfd_link_hash_defined
9726 || h
->root
.type
== bfd_link_hash_defweak
))
9730 section
= h
->root
.u
.def
.section
;
9731 indx
= section
->output_section
->target_index
;
9732 *rel_hash_ptr
= NULL
;
9733 /* It seems that we ought to add the symbol value to the
9734 addend here, but in practice it has already been added
9735 because it was passed to constructor_callback. */
9736 addend
+= section
->output_section
->vma
+ section
->output_offset
;
9740 /* Setting the index to -2 tells elf_link_output_extsym that
9741 this symbol is used by a reloc. */
9748 if (! ((*info
->callbacks
->unattached_reloc
)
9749 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9755 /* If this is an inplace reloc, we must write the addend into the
9757 if (howto
->partial_inplace
&& addend
!= 0)
9760 bfd_reloc_status_type rstat
;
9763 const char *sym_name
;
9765 size
= bfd_get_reloc_size (howto
);
9766 buf
= bfd_zmalloc (size
);
9769 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9776 case bfd_reloc_outofrange
:
9779 case bfd_reloc_overflow
:
9780 if (link_order
->type
== bfd_section_reloc_link_order
)
9781 sym_name
= bfd_section_name (output_bfd
,
9782 link_order
->u
.reloc
.p
->u
.section
);
9784 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9785 if (! ((*info
->callbacks
->reloc_overflow
)
9786 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9787 NULL
, (bfd_vma
) 0)))
9794 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9795 link_order
->offset
, size
);
9801 /* The address of a reloc is relative to the section in a
9802 relocatable file, and is a virtual address in an executable
9804 offset
= link_order
->offset
;
9805 if (! info
->relocatable
)
9806 offset
+= output_section
->vma
;
9808 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9810 irel
[i
].r_offset
= offset
;
9812 irel
[i
].r_addend
= 0;
9814 if (bed
->s
->arch_size
== 32)
9815 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9817 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9819 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9820 erel
= rel_hdr
->contents
;
9821 if (rel_hdr
->sh_type
== SHT_REL
)
9823 erel
+= (elf_section_data (output_section
)->rel_count
9824 * bed
->s
->sizeof_rel
);
9825 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9829 irel
[0].r_addend
= addend
;
9830 erel
+= (elf_section_data (output_section
)->rel_count
9831 * bed
->s
->sizeof_rela
);
9832 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9835 ++elf_section_data (output_section
)->rel_count
;
9841 /* Get the output vma of the section pointed to by the sh_link field. */
9844 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9846 Elf_Internal_Shdr
**elf_shdrp
;
9850 s
= p
->u
.indirect
.section
;
9851 elf_shdrp
= elf_elfsections (s
->owner
);
9852 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9853 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9855 The Intel C compiler generates SHT_IA_64_UNWIND with
9856 SHF_LINK_ORDER. But it doesn't set the sh_link or
9857 sh_info fields. Hence we could get the situation
9858 where elfsec is 0. */
9861 const struct elf_backend_data
*bed
9862 = get_elf_backend_data (s
->owner
);
9863 if (bed
->link_order_error_handler
)
9864 bed
->link_order_error_handler
9865 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9870 s
= elf_shdrp
[elfsec
]->bfd_section
;
9871 return s
->output_section
->vma
+ s
->output_offset
;
9876 /* Compare two sections based on the locations of the sections they are
9877 linked to. Used by elf_fixup_link_order. */
9880 compare_link_order (const void * a
, const void * b
)
9885 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9886 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9893 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9894 order as their linked sections. Returns false if this could not be done
9895 because an output section includes both ordered and unordered
9896 sections. Ideally we'd do this in the linker proper. */
9899 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9904 struct bfd_link_order
*p
;
9906 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9908 struct bfd_link_order
**sections
;
9909 asection
*s
, *other_sec
, *linkorder_sec
;
9913 linkorder_sec
= NULL
;
9916 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9918 if (p
->type
== bfd_indirect_link_order
)
9920 s
= p
->u
.indirect
.section
;
9922 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9923 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9924 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9925 && elfsec
< elf_numsections (sub
)
9926 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
9940 if (seen_other
&& seen_linkorder
)
9942 if (other_sec
&& linkorder_sec
)
9943 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9945 linkorder_sec
->owner
, other_sec
,
9948 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9950 bfd_set_error (bfd_error_bad_value
);
9955 if (!seen_linkorder
)
9958 sections
= (struct bfd_link_order
**)
9959 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9962 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9964 sections
[seen_linkorder
++] = p
;
9966 /* Sort the input sections in the order of their linked section. */
9967 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9968 compare_link_order
);
9970 /* Change the offsets of the sections. */
9972 for (n
= 0; n
< seen_linkorder
; n
++)
9974 s
= sections
[n
]->u
.indirect
.section
;
9975 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9976 s
->output_offset
= offset
;
9977 sections
[n
]->offset
= offset
;
9978 offset
+= sections
[n
]->size
;
9985 /* Do the final step of an ELF link. */
9988 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9990 bfd_boolean dynamic
;
9991 bfd_boolean emit_relocs
;
9993 struct elf_final_link_info finfo
;
9994 register asection
*o
;
9995 register struct bfd_link_order
*p
;
9997 bfd_size_type max_contents_size
;
9998 bfd_size_type max_external_reloc_size
;
9999 bfd_size_type max_internal_reloc_count
;
10000 bfd_size_type max_sym_count
;
10001 bfd_size_type max_sym_shndx_count
;
10003 Elf_Internal_Sym elfsym
;
10005 Elf_Internal_Shdr
*symtab_hdr
;
10006 Elf_Internal_Shdr
*symtab_shndx_hdr
;
10007 Elf_Internal_Shdr
*symstrtab_hdr
;
10008 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10009 struct elf_outext_info eoinfo
;
10010 bfd_boolean merged
;
10011 size_t relativecount
= 0;
10012 asection
*reldyn
= 0;
10014 asection
*attr_section
= NULL
;
10015 bfd_vma attr_size
= 0;
10016 const char *std_attrs_section
;
10018 if (! is_elf_hash_table (info
->hash
))
10022 abfd
->flags
|= DYNAMIC
;
10024 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
10025 dynobj
= elf_hash_table (info
)->dynobj
;
10027 emit_relocs
= (info
->relocatable
10028 || info
->emitrelocations
);
10031 finfo
.output_bfd
= abfd
;
10032 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
10033 if (finfo
.symstrtab
== NULL
)
10038 finfo
.dynsym_sec
= NULL
;
10039 finfo
.hash_sec
= NULL
;
10040 finfo
.symver_sec
= NULL
;
10044 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
10045 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
10046 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
10047 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
10048 /* Note that it is OK if symver_sec is NULL. */
10051 finfo
.contents
= NULL
;
10052 finfo
.external_relocs
= NULL
;
10053 finfo
.internal_relocs
= NULL
;
10054 finfo
.external_syms
= NULL
;
10055 finfo
.locsym_shndx
= NULL
;
10056 finfo
.internal_syms
= NULL
;
10057 finfo
.indices
= NULL
;
10058 finfo
.sections
= NULL
;
10059 finfo
.symbuf
= NULL
;
10060 finfo
.symshndxbuf
= NULL
;
10061 finfo
.symbuf_count
= 0;
10062 finfo
.shndxbuf_size
= 0;
10064 /* The object attributes have been merged. Remove the input
10065 sections from the link, and set the contents of the output
10067 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10068 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10070 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10071 || strcmp (o
->name
, ".gnu.attributes") == 0)
10073 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10075 asection
*input_section
;
10077 if (p
->type
!= bfd_indirect_link_order
)
10079 input_section
= p
->u
.indirect
.section
;
10080 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10081 elf_link_input_bfd ignores this section. */
10082 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10085 attr_size
= bfd_elf_obj_attr_size (abfd
);
10088 bfd_set_section_size (abfd
, o
, attr_size
);
10090 /* Skip this section later on. */
10091 o
->map_head
.link_order
= NULL
;
10094 o
->flags
|= SEC_EXCLUDE
;
10098 /* Count up the number of relocations we will output for each output
10099 section, so that we know the sizes of the reloc sections. We
10100 also figure out some maximum sizes. */
10101 max_contents_size
= 0;
10102 max_external_reloc_size
= 0;
10103 max_internal_reloc_count
= 0;
10105 max_sym_shndx_count
= 0;
10107 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10109 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10110 o
->reloc_count
= 0;
10112 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10114 unsigned int reloc_count
= 0;
10115 struct bfd_elf_section_data
*esdi
= NULL
;
10116 unsigned int *rel_count1
;
10118 if (p
->type
== bfd_section_reloc_link_order
10119 || p
->type
== bfd_symbol_reloc_link_order
)
10121 else if (p
->type
== bfd_indirect_link_order
)
10125 sec
= p
->u
.indirect
.section
;
10126 esdi
= elf_section_data (sec
);
10128 /* Mark all sections which are to be included in the
10129 link. This will normally be every section. We need
10130 to do this so that we can identify any sections which
10131 the linker has decided to not include. */
10132 sec
->linker_mark
= TRUE
;
10134 if (sec
->flags
& SEC_MERGE
)
10137 if (info
->relocatable
|| info
->emitrelocations
)
10138 reloc_count
= sec
->reloc_count
;
10139 else if (bed
->elf_backend_count_relocs
)
10141 Elf_Internal_Rela
* relocs
;
10143 relocs
= _bfd_elf_link_read_relocs (sec
->owner
, sec
,
10145 info
->keep_memory
);
10147 if (relocs
!= NULL
)
10150 = (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
10152 if (elf_section_data (sec
)->relocs
!= relocs
)
10157 if (sec
->rawsize
> max_contents_size
)
10158 max_contents_size
= sec
->rawsize
;
10159 if (sec
->size
> max_contents_size
)
10160 max_contents_size
= sec
->size
;
10162 /* We are interested in just local symbols, not all
10164 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10165 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10169 if (elf_bad_symtab (sec
->owner
))
10170 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10171 / bed
->s
->sizeof_sym
);
10173 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10175 if (sym_count
> max_sym_count
)
10176 max_sym_count
= sym_count
;
10178 if (sym_count
> max_sym_shndx_count
10179 && elf_symtab_shndx (sec
->owner
) != 0)
10180 max_sym_shndx_count
= sym_count
;
10182 if ((sec
->flags
& SEC_RELOC
) != 0)
10186 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
10187 if (ext_size
> max_external_reloc_size
)
10188 max_external_reloc_size
= ext_size
;
10189 if (sec
->reloc_count
> max_internal_reloc_count
)
10190 max_internal_reloc_count
= sec
->reloc_count
;
10195 if (reloc_count
== 0)
10198 o
->reloc_count
+= reloc_count
;
10200 /* MIPS may have a mix of REL and RELA relocs on sections.
10201 To support this curious ABI we keep reloc counts in
10202 elf_section_data too. We must be careful to add the
10203 relocations from the input section to the right output
10204 count. FIXME: Get rid of one count. We have
10205 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10206 rel_count1
= &esdo
->rel_count
;
10209 bfd_boolean same_size
;
10210 bfd_size_type entsize1
;
10212 entsize1
= esdi
->rel_hdr
.sh_entsize
;
10213 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
10214 || entsize1
== bed
->s
->sizeof_rela
);
10215 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
10218 rel_count1
= &esdo
->rel_count2
;
10220 if (esdi
->rel_hdr2
!= NULL
)
10222 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
10223 unsigned int alt_count
;
10224 unsigned int *rel_count2
;
10226 BFD_ASSERT (entsize2
!= entsize1
10227 && (entsize2
== bed
->s
->sizeof_rel
10228 || entsize2
== bed
->s
->sizeof_rela
));
10230 rel_count2
= &esdo
->rel_count2
;
10232 rel_count2
= &esdo
->rel_count
;
10234 /* The following is probably too simplistic if the
10235 backend counts output relocs unusually. */
10236 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
10237 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
10238 *rel_count2
+= alt_count
;
10239 reloc_count
-= alt_count
;
10242 *rel_count1
+= reloc_count
;
10245 if (o
->reloc_count
> 0)
10246 o
->flags
|= SEC_RELOC
;
10249 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10250 set it (this is probably a bug) and if it is set
10251 assign_section_numbers will create a reloc section. */
10252 o
->flags
&=~ SEC_RELOC
;
10255 /* If the SEC_ALLOC flag is not set, force the section VMA to
10256 zero. This is done in elf_fake_sections as well, but forcing
10257 the VMA to 0 here will ensure that relocs against these
10258 sections are handled correctly. */
10259 if ((o
->flags
& SEC_ALLOC
) == 0
10260 && ! o
->user_set_vma
)
10264 if (! info
->relocatable
&& merged
)
10265 elf_link_hash_traverse (elf_hash_table (info
),
10266 _bfd_elf_link_sec_merge_syms
, abfd
);
10268 /* Figure out the file positions for everything but the symbol table
10269 and the relocs. We set symcount to force assign_section_numbers
10270 to create a symbol table. */
10271 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10272 BFD_ASSERT (! abfd
->output_has_begun
);
10273 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10276 /* Set sizes, and assign file positions for reloc sections. */
10277 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10279 if ((o
->flags
& SEC_RELOC
) != 0)
10281 if (!(_bfd_elf_link_size_reloc_section
10282 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
10285 if (elf_section_data (o
)->rel_hdr2
10286 && !(_bfd_elf_link_size_reloc_section
10287 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
10291 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10292 to count upwards while actually outputting the relocations. */
10293 elf_section_data (o
)->rel_count
= 0;
10294 elf_section_data (o
)->rel_count2
= 0;
10297 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10299 /* We have now assigned file positions for all the sections except
10300 .symtab and .strtab. We start the .symtab section at the current
10301 file position, and write directly to it. We build the .strtab
10302 section in memory. */
10303 bfd_get_symcount (abfd
) = 0;
10304 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10305 /* sh_name is set in prep_headers. */
10306 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10307 /* sh_flags, sh_addr and sh_size all start off zero. */
10308 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10309 /* sh_link is set in assign_section_numbers. */
10310 /* sh_info is set below. */
10311 /* sh_offset is set just below. */
10312 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
10314 off
= elf_tdata (abfd
)->next_file_pos
;
10315 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10317 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10318 incorrect. We do not yet know the size of the .symtab section.
10319 We correct next_file_pos below, after we do know the size. */
10321 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10322 continuously seeking to the right position in the file. */
10323 if (! info
->keep_memory
|| max_sym_count
< 20)
10324 finfo
.symbuf_size
= 20;
10326 finfo
.symbuf_size
= max_sym_count
;
10327 amt
= finfo
.symbuf_size
;
10328 amt
*= bed
->s
->sizeof_sym
;
10329 finfo
.symbuf
= bfd_malloc (amt
);
10330 if (finfo
.symbuf
== NULL
)
10332 if (elf_numsections (abfd
) > SHN_LORESERVE
)
10334 /* Wild guess at number of output symbols. realloc'd as needed. */
10335 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10336 finfo
.shndxbuf_size
= amt
;
10337 amt
*= sizeof (Elf_External_Sym_Shndx
);
10338 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
10339 if (finfo
.symshndxbuf
== NULL
)
10343 /* Start writing out the symbol table. The first symbol is always a
10345 if (info
->strip
!= strip_all
10348 elfsym
.st_value
= 0;
10349 elfsym
.st_size
= 0;
10350 elfsym
.st_info
= 0;
10351 elfsym
.st_other
= 0;
10352 elfsym
.st_shndx
= SHN_UNDEF
;
10353 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10358 /* Output a symbol for each section. We output these even if we are
10359 discarding local symbols, since they are used for relocs. These
10360 symbols have no names. We store the index of each one in the
10361 index field of the section, so that we can find it again when
10362 outputting relocs. */
10363 if (info
->strip
!= strip_all
10366 elfsym
.st_size
= 0;
10367 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10368 elfsym
.st_other
= 0;
10369 elfsym
.st_value
= 0;
10370 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10372 o
= bfd_section_from_elf_index (abfd
, i
);
10375 o
->target_index
= bfd_get_symcount (abfd
);
10376 elfsym
.st_shndx
= i
;
10377 if (!info
->relocatable
)
10378 elfsym
.st_value
= o
->vma
;
10379 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
10382 if (i
== SHN_LORESERVE
- 1)
10383 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
10387 /* Allocate some memory to hold information read in from the input
10389 if (max_contents_size
!= 0)
10391 finfo
.contents
= bfd_malloc (max_contents_size
);
10392 if (finfo
.contents
== NULL
)
10396 if (max_external_reloc_size
!= 0)
10398 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10399 if (finfo
.external_relocs
== NULL
)
10403 if (max_internal_reloc_count
!= 0)
10405 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10406 amt
*= sizeof (Elf_Internal_Rela
);
10407 finfo
.internal_relocs
= bfd_malloc (amt
);
10408 if (finfo
.internal_relocs
== NULL
)
10412 if (max_sym_count
!= 0)
10414 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10415 finfo
.external_syms
= bfd_malloc (amt
);
10416 if (finfo
.external_syms
== NULL
)
10419 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10420 finfo
.internal_syms
= bfd_malloc (amt
);
10421 if (finfo
.internal_syms
== NULL
)
10424 amt
= max_sym_count
* sizeof (long);
10425 finfo
.indices
= bfd_malloc (amt
);
10426 if (finfo
.indices
== NULL
)
10429 amt
= max_sym_count
* sizeof (asection
*);
10430 finfo
.sections
= bfd_malloc (amt
);
10431 if (finfo
.sections
== NULL
)
10435 if (max_sym_shndx_count
!= 0)
10437 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10438 finfo
.locsym_shndx
= bfd_malloc (amt
);
10439 if (finfo
.locsym_shndx
== NULL
)
10443 if (elf_hash_table (info
)->tls_sec
)
10445 bfd_vma base
, end
= 0;
10448 for (sec
= elf_hash_table (info
)->tls_sec
;
10449 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10452 bfd_size_type size
= sec
->size
;
10455 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10457 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
10459 size
= o
->offset
+ o
->size
;
10461 end
= sec
->vma
+ size
;
10463 base
= elf_hash_table (info
)->tls_sec
->vma
;
10464 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
10465 elf_hash_table (info
)->tls_size
= end
- base
;
10468 /* Reorder SHF_LINK_ORDER sections. */
10469 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10471 if (!elf_fixup_link_order (abfd
, o
))
10475 /* Since ELF permits relocations to be against local symbols, we
10476 must have the local symbols available when we do the relocations.
10477 Since we would rather only read the local symbols once, and we
10478 would rather not keep them in memory, we handle all the
10479 relocations for a single input file at the same time.
10481 Unfortunately, there is no way to know the total number of local
10482 symbols until we have seen all of them, and the local symbol
10483 indices precede the global symbol indices. This means that when
10484 we are generating relocatable output, and we see a reloc against
10485 a global symbol, we can not know the symbol index until we have
10486 finished examining all the local symbols to see which ones we are
10487 going to output. To deal with this, we keep the relocations in
10488 memory, and don't output them until the end of the link. This is
10489 an unfortunate waste of memory, but I don't see a good way around
10490 it. Fortunately, it only happens when performing a relocatable
10491 link, which is not the common case. FIXME: If keep_memory is set
10492 we could write the relocs out and then read them again; I don't
10493 know how bad the memory loss will be. */
10495 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10496 sub
->output_has_begun
= FALSE
;
10497 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10499 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10501 if (p
->type
== bfd_indirect_link_order
10502 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10503 == bfd_target_elf_flavour
)
10504 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10506 if (! sub
->output_has_begun
)
10508 if (! elf_link_input_bfd (&finfo
, sub
))
10510 sub
->output_has_begun
= TRUE
;
10513 else if (p
->type
== bfd_section_reloc_link_order
10514 || p
->type
== bfd_symbol_reloc_link_order
)
10516 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10521 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10527 /* Free symbol buffer if needed. */
10528 if (!info
->reduce_memory_overheads
)
10530 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10531 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10532 && elf_tdata (sub
)->symbuf
)
10534 free (elf_tdata (sub
)->symbuf
);
10535 elf_tdata (sub
)->symbuf
= NULL
;
10539 /* Output any global symbols that got converted to local in a
10540 version script or due to symbol visibility. We do this in a
10541 separate step since ELF requires all local symbols to appear
10542 prior to any global symbols. FIXME: We should only do this if
10543 some global symbols were, in fact, converted to become local.
10544 FIXME: Will this work correctly with the Irix 5 linker? */
10545 eoinfo
.failed
= FALSE
;
10546 eoinfo
.finfo
= &finfo
;
10547 eoinfo
.localsyms
= TRUE
;
10548 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10553 /* If backend needs to output some local symbols not present in the hash
10554 table, do it now. */
10555 if (bed
->elf_backend_output_arch_local_syms
)
10557 typedef bfd_boolean (*out_sym_func
)
10558 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10559 struct elf_link_hash_entry
*);
10561 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10562 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10566 /* That wrote out all the local symbols. Finish up the symbol table
10567 with the global symbols. Even if we want to strip everything we
10568 can, we still need to deal with those global symbols that got
10569 converted to local in a version script. */
10571 /* The sh_info field records the index of the first non local symbol. */
10572 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10575 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10577 Elf_Internal_Sym sym
;
10578 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
10579 long last_local
= 0;
10581 /* Write out the section symbols for the output sections. */
10582 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10588 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10591 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
10597 dynindx
= elf_section_data (s
)->dynindx
;
10600 indx
= elf_section_data (s
)->this_idx
;
10601 BFD_ASSERT (indx
> 0);
10602 sym
.st_shndx
= indx
;
10603 if (! check_dynsym (abfd
, &sym
))
10605 sym
.st_value
= s
->vma
;
10606 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
10607 if (last_local
< dynindx
)
10608 last_local
= dynindx
;
10609 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10613 /* Write out the local dynsyms. */
10614 if (elf_hash_table (info
)->dynlocal
)
10616 struct elf_link_local_dynamic_entry
*e
;
10617 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
10622 sym
.st_size
= e
->isym
.st_size
;
10623 sym
.st_other
= e
->isym
.st_other
;
10625 /* Copy the internal symbol as is.
10626 Note that we saved a word of storage and overwrote
10627 the original st_name with the dynstr_index. */
10630 if (e
->isym
.st_shndx
!= SHN_UNDEF
10631 && (e
->isym
.st_shndx
< SHN_LORESERVE
10632 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
10634 s
= bfd_section_from_elf_index (e
->input_bfd
,
10638 elf_section_data (s
->output_section
)->this_idx
;
10639 if (! check_dynsym (abfd
, &sym
))
10641 sym
.st_value
= (s
->output_section
->vma
10643 + e
->isym
.st_value
);
10646 if (last_local
< e
->dynindx
)
10647 last_local
= e
->dynindx
;
10649 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
10650 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10654 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
10658 /* We get the global symbols from the hash table. */
10659 eoinfo
.failed
= FALSE
;
10660 eoinfo
.localsyms
= FALSE
;
10661 eoinfo
.finfo
= &finfo
;
10662 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10667 /* If backend needs to output some symbols not present in the hash
10668 table, do it now. */
10669 if (bed
->elf_backend_output_arch_syms
)
10671 typedef bfd_boolean (*out_sym_func
)
10672 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10673 struct elf_link_hash_entry
*);
10675 if (! ((*bed
->elf_backend_output_arch_syms
)
10676 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10680 /* Flush all symbols to the file. */
10681 if (! elf_link_flush_output_syms (&finfo
, bed
))
10684 /* Now we know the size of the symtab section. */
10685 off
+= symtab_hdr
->sh_size
;
10687 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
10688 if (symtab_shndx_hdr
->sh_name
!= 0)
10690 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
10691 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
10692 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
10693 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
10694 symtab_shndx_hdr
->sh_size
= amt
;
10696 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
10699 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
10700 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
10705 /* Finish up and write out the symbol string table (.strtab)
10707 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
10708 /* sh_name was set in prep_headers. */
10709 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
10710 symstrtab_hdr
->sh_flags
= 0;
10711 symstrtab_hdr
->sh_addr
= 0;
10712 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
10713 symstrtab_hdr
->sh_entsize
= 0;
10714 symstrtab_hdr
->sh_link
= 0;
10715 symstrtab_hdr
->sh_info
= 0;
10716 /* sh_offset is set just below. */
10717 symstrtab_hdr
->sh_addralign
= 1;
10719 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
10720 elf_tdata (abfd
)->next_file_pos
= off
;
10722 if (bfd_get_symcount (abfd
) > 0)
10724 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
10725 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
10729 /* Adjust the relocs to have the correct symbol indices. */
10730 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10732 if ((o
->flags
& SEC_RELOC
) == 0)
10735 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
10736 elf_section_data (o
)->rel_count
,
10737 elf_section_data (o
)->rel_hashes
);
10738 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
10739 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
10740 elf_section_data (o
)->rel_count2
,
10741 (elf_section_data (o
)->rel_hashes
10742 + elf_section_data (o
)->rel_count
));
10744 /* Set the reloc_count field to 0 to prevent write_relocs from
10745 trying to swap the relocs out itself. */
10746 o
->reloc_count
= 0;
10749 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10750 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10752 /* If we are linking against a dynamic object, or generating a
10753 shared library, finish up the dynamic linking information. */
10756 bfd_byte
*dyncon
, *dynconend
;
10758 /* Fix up .dynamic entries. */
10759 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10760 BFD_ASSERT (o
!= NULL
);
10762 dyncon
= o
->contents
;
10763 dynconend
= o
->contents
+ o
->size
;
10764 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10766 Elf_Internal_Dyn dyn
;
10770 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10777 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10779 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10781 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10782 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10785 dyn
.d_un
.d_val
= relativecount
;
10792 name
= info
->init_function
;
10795 name
= info
->fini_function
;
10798 struct elf_link_hash_entry
*h
;
10800 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10801 FALSE
, FALSE
, TRUE
);
10803 && (h
->root
.type
== bfd_link_hash_defined
10804 || h
->root
.type
== bfd_link_hash_defweak
))
10806 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
10807 o
= h
->root
.u
.def
.section
;
10808 if (o
->output_section
!= NULL
)
10809 dyn
.d_un
.d_val
+= (o
->output_section
->vma
10810 + o
->output_offset
);
10813 /* The symbol is imported from another shared
10814 library and does not apply to this one. */
10815 dyn
.d_un
.d_val
= 0;
10822 case DT_PREINIT_ARRAYSZ
:
10823 name
= ".preinit_array";
10825 case DT_INIT_ARRAYSZ
:
10826 name
= ".init_array";
10828 case DT_FINI_ARRAYSZ
:
10829 name
= ".fini_array";
10831 o
= bfd_get_section_by_name (abfd
, name
);
10834 (*_bfd_error_handler
)
10835 (_("%B: could not find output section %s"), abfd
, name
);
10839 (*_bfd_error_handler
)
10840 (_("warning: %s section has zero size"), name
);
10841 dyn
.d_un
.d_val
= o
->size
;
10844 case DT_PREINIT_ARRAY
:
10845 name
= ".preinit_array";
10847 case DT_INIT_ARRAY
:
10848 name
= ".init_array";
10850 case DT_FINI_ARRAY
:
10851 name
= ".fini_array";
10858 name
= ".gnu.hash";
10867 name
= ".gnu.version_d";
10870 name
= ".gnu.version_r";
10873 name
= ".gnu.version";
10875 o
= bfd_get_section_by_name (abfd
, name
);
10878 (*_bfd_error_handler
)
10879 (_("%B: could not find output section %s"), abfd
, name
);
10882 dyn
.d_un
.d_ptr
= o
->vma
;
10889 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10893 dyn
.d_un
.d_val
= 0;
10894 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10896 Elf_Internal_Shdr
*hdr
;
10898 hdr
= elf_elfsections (abfd
)[i
];
10899 if (hdr
->sh_type
== type
10900 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10902 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10903 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10906 if (dyn
.d_un
.d_val
== 0
10907 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
10908 dyn
.d_un
.d_val
= hdr
->sh_addr
;
10914 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10918 /* If we have created any dynamic sections, then output them. */
10919 if (dynobj
!= NULL
)
10921 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10924 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10925 if (info
->warn_shared_textrel
&& info
->shared
)
10927 bfd_byte
*dyncon
, *dynconend
;
10929 /* Fix up .dynamic entries. */
10930 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10931 BFD_ASSERT (o
!= NULL
);
10933 dyncon
= o
->contents
;
10934 dynconend
= o
->contents
+ o
->size
;
10935 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10937 Elf_Internal_Dyn dyn
;
10939 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10941 if (dyn
.d_tag
== DT_TEXTREL
)
10943 info
->callbacks
->einfo
10944 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10950 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10952 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10954 || o
->output_section
== bfd_abs_section_ptr
)
10956 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10958 /* At this point, we are only interested in sections
10959 created by _bfd_elf_link_create_dynamic_sections. */
10962 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10964 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10966 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10968 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10970 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10972 (file_ptr
) o
->output_offset
,
10978 /* The contents of the .dynstr section are actually in a
10980 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10981 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10982 || ! _bfd_elf_strtab_emit (abfd
,
10983 elf_hash_table (info
)->dynstr
))
10989 if (info
->relocatable
)
10991 bfd_boolean failed
= FALSE
;
10993 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10998 /* If we have optimized stabs strings, output them. */
10999 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
11001 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
11005 if (info
->eh_frame_hdr
)
11007 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
11011 if (finfo
.symstrtab
!= NULL
)
11012 _bfd_stringtab_free (finfo
.symstrtab
);
11013 if (finfo
.contents
!= NULL
)
11014 free (finfo
.contents
);
11015 if (finfo
.external_relocs
!= NULL
)
11016 free (finfo
.external_relocs
);
11017 if (finfo
.internal_relocs
!= NULL
)
11018 free (finfo
.internal_relocs
);
11019 if (finfo
.external_syms
!= NULL
)
11020 free (finfo
.external_syms
);
11021 if (finfo
.locsym_shndx
!= NULL
)
11022 free (finfo
.locsym_shndx
);
11023 if (finfo
.internal_syms
!= NULL
)
11024 free (finfo
.internal_syms
);
11025 if (finfo
.indices
!= NULL
)
11026 free (finfo
.indices
);
11027 if (finfo
.sections
!= NULL
)
11028 free (finfo
.sections
);
11029 if (finfo
.symbuf
!= NULL
)
11030 free (finfo
.symbuf
);
11031 if (finfo
.symshndxbuf
!= NULL
)
11032 free (finfo
.symshndxbuf
);
11033 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11035 if ((o
->flags
& SEC_RELOC
) != 0
11036 && elf_section_data (o
)->rel_hashes
!= NULL
)
11037 free (elf_section_data (o
)->rel_hashes
);
11040 elf_tdata (abfd
)->linker
= TRUE
;
11044 bfd_byte
*contents
= bfd_malloc (attr_size
);
11045 if (contents
== NULL
)
11047 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11048 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11055 if (finfo
.symstrtab
!= NULL
)
11056 _bfd_stringtab_free (finfo
.symstrtab
);
11057 if (finfo
.contents
!= NULL
)
11058 free (finfo
.contents
);
11059 if (finfo
.external_relocs
!= NULL
)
11060 free (finfo
.external_relocs
);
11061 if (finfo
.internal_relocs
!= NULL
)
11062 free (finfo
.internal_relocs
);
11063 if (finfo
.external_syms
!= NULL
)
11064 free (finfo
.external_syms
);
11065 if (finfo
.locsym_shndx
!= NULL
)
11066 free (finfo
.locsym_shndx
);
11067 if (finfo
.internal_syms
!= NULL
)
11068 free (finfo
.internal_syms
);
11069 if (finfo
.indices
!= NULL
)
11070 free (finfo
.indices
);
11071 if (finfo
.sections
!= NULL
)
11072 free (finfo
.sections
);
11073 if (finfo
.symbuf
!= NULL
)
11074 free (finfo
.symbuf
);
11075 if (finfo
.symshndxbuf
!= NULL
)
11076 free (finfo
.symshndxbuf
);
11077 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11079 if ((o
->flags
& SEC_RELOC
) != 0
11080 && elf_section_data (o
)->rel_hashes
!= NULL
)
11081 free (elf_section_data (o
)->rel_hashes
);
11087 /* Garbage collect unused sections. */
11089 /* Default gc_mark_hook. */
11092 _bfd_elf_gc_mark_hook (asection
*sec
,
11093 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11094 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11095 struct elf_link_hash_entry
*h
,
11096 Elf_Internal_Sym
*sym
)
11100 switch (h
->root
.type
)
11102 case bfd_link_hash_defined
:
11103 case bfd_link_hash_defweak
:
11104 return h
->root
.u
.def
.section
;
11106 case bfd_link_hash_common
:
11107 return h
->root
.u
.c
.p
->section
;
11114 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11119 /* The mark phase of garbage collection. For a given section, mark
11120 it and any sections in this section's group, and all the sections
11121 which define symbols to which it refers. */
11124 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11126 elf_gc_mark_hook_fn gc_mark_hook
)
11130 asection
*group_sec
;
11134 /* Mark all the sections in the group. */
11135 group_sec
= elf_section_data (sec
)->next_in_group
;
11136 if (group_sec
&& !group_sec
->gc_mark
)
11137 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11140 /* Look through the section relocs. */
11142 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
11143 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
11145 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
11146 Elf_Internal_Shdr
*symtab_hdr
;
11147 struct elf_link_hash_entry
**sym_hashes
;
11150 bfd
*input_bfd
= sec
->owner
;
11151 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
11152 Elf_Internal_Sym
*isym
= NULL
;
11155 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11156 sym_hashes
= elf_sym_hashes (input_bfd
);
11158 /* Read the local symbols. */
11159 if (elf_bad_symtab (input_bfd
))
11161 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11165 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
11167 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11168 if (isym
== NULL
&& nlocsyms
!= 0)
11170 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
11176 /* Read the relocations. */
11177 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
11178 info
->keep_memory
);
11179 if (relstart
== NULL
)
11184 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11186 if (bed
->s
->arch_size
== 32)
11191 for (rel
= relstart
; rel
< relend
; rel
++)
11193 unsigned long r_symndx
;
11195 struct elf_link_hash_entry
*h
;
11197 r_symndx
= rel
->r_info
>> r_sym_shift
;
11201 if (r_symndx
>= nlocsyms
11202 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
11204 h
= sym_hashes
[r_symndx
- extsymoff
];
11205 while (h
->root
.type
== bfd_link_hash_indirect
11206 || h
->root
.type
== bfd_link_hash_warning
)
11207 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11208 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
11212 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
11215 if (rsec
&& !rsec
->gc_mark
)
11217 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
11220 rsec
->gc_mark_from_eh
= 1;
11221 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11230 if (elf_section_data (sec
)->relocs
!= relstart
)
11233 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
11235 if (! info
->keep_memory
)
11238 symtab_hdr
->contents
= (unsigned char *) isym
;
11245 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11247 struct elf_gc_sweep_symbol_info
11249 struct bfd_link_info
*info
;
11250 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11255 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11257 if (h
->root
.type
== bfd_link_hash_warning
)
11258 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11260 if ((h
->root
.type
== bfd_link_hash_defined
11261 || h
->root
.type
== bfd_link_hash_defweak
)
11262 && !h
->root
.u
.def
.section
->gc_mark
11263 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
11265 struct elf_gc_sweep_symbol_info
*inf
= data
;
11266 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11272 /* The sweep phase of garbage collection. Remove all garbage sections. */
11274 typedef bfd_boolean (*gc_sweep_hook_fn
)
11275 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11278 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11281 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11282 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11283 unsigned long section_sym_count
;
11284 struct elf_gc_sweep_symbol_info sweep_info
;
11286 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11290 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11293 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11295 /* Keep debug and special sections. */
11296 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
11297 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
11303 /* Skip sweeping sections already excluded. */
11304 if (o
->flags
& SEC_EXCLUDE
)
11307 /* Since this is early in the link process, it is simple
11308 to remove a section from the output. */
11309 o
->flags
|= SEC_EXCLUDE
;
11311 if (info
->print_gc_sections
&& o
->size
!= 0)
11312 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11314 /* But we also have to update some of the relocation
11315 info we collected before. */
11317 && (o
->flags
& SEC_RELOC
) != 0
11318 && o
->reloc_count
> 0
11319 && !bfd_is_abs_section (o
->output_section
))
11321 Elf_Internal_Rela
*internal_relocs
;
11325 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11326 info
->keep_memory
);
11327 if (internal_relocs
== NULL
)
11330 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11332 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11333 free (internal_relocs
);
11341 /* Remove the symbols that were in the swept sections from the dynamic
11342 symbol table. GCFIXME: Anyone know how to get them out of the
11343 static symbol table as well? */
11344 sweep_info
.info
= info
;
11345 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11346 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11349 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11353 /* Propagate collected vtable information. This is called through
11354 elf_link_hash_traverse. */
11357 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11359 if (h
->root
.type
== bfd_link_hash_warning
)
11360 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11362 /* Those that are not vtables. */
11363 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11366 /* Those vtables that do not have parents, we cannot merge. */
11367 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11370 /* If we've already been done, exit. */
11371 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11374 /* Make sure the parent's table is up to date. */
11375 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11377 if (h
->vtable
->used
== NULL
)
11379 /* None of this table's entries were referenced. Re-use the
11381 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
11382 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
11387 bfd_boolean
*cu
, *pu
;
11389 /* Or the parent's entries into ours. */
11390 cu
= h
->vtable
->used
;
11392 pu
= h
->vtable
->parent
->vtable
->used
;
11395 const struct elf_backend_data
*bed
;
11396 unsigned int log_file_align
;
11398 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
11399 log_file_align
= bed
->s
->log_file_align
;
11400 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
11415 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
11418 bfd_vma hstart
, hend
;
11419 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
11420 const struct elf_backend_data
*bed
;
11421 unsigned int log_file_align
;
11423 if (h
->root
.type
== bfd_link_hash_warning
)
11424 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11426 /* Take care of both those symbols that do not describe vtables as
11427 well as those that are not loaded. */
11428 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11431 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
11432 || h
->root
.type
== bfd_link_hash_defweak
);
11434 sec
= h
->root
.u
.def
.section
;
11435 hstart
= h
->root
.u
.def
.value
;
11436 hend
= hstart
+ h
->size
;
11438 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
11440 return *(bfd_boolean
*) okp
= FALSE
;
11441 bed
= get_elf_backend_data (sec
->owner
);
11442 log_file_align
= bed
->s
->log_file_align
;
11444 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11446 for (rel
= relstart
; rel
< relend
; ++rel
)
11447 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
11449 /* If the entry is in use, do nothing. */
11450 if (h
->vtable
->used
11451 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
11453 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
11454 if (h
->vtable
->used
[entry
])
11457 /* Otherwise, kill it. */
11458 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
11464 /* Mark sections containing dynamically referenced symbols. When
11465 building shared libraries, we must assume that any visible symbol is
11469 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
11471 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
11473 if (h
->root
.type
== bfd_link_hash_warning
)
11474 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11476 if ((h
->root
.type
== bfd_link_hash_defined
11477 || h
->root
.type
== bfd_link_hash_defweak
)
11479 || (!info
->executable
11481 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
11482 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
11483 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11488 /* Do mark and sweep of unused sections. */
11491 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
11493 bfd_boolean ok
= TRUE
;
11495 elf_gc_mark_hook_fn gc_mark_hook
;
11496 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11498 if (!bed
->can_gc_sections
11499 || info
->relocatable
11500 || info
->emitrelocations
11501 || !is_elf_hash_table (info
->hash
))
11503 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
11507 /* Apply transitive closure to the vtable entry usage info. */
11508 elf_link_hash_traverse (elf_hash_table (info
),
11509 elf_gc_propagate_vtable_entries_used
,
11514 /* Kill the vtable relocations that were not used. */
11515 elf_link_hash_traverse (elf_hash_table (info
),
11516 elf_gc_smash_unused_vtentry_relocs
,
11521 /* Mark dynamically referenced symbols. */
11522 if (elf_hash_table (info
)->dynamic_sections_created
)
11523 elf_link_hash_traverse (elf_hash_table (info
),
11524 bed
->gc_mark_dynamic_ref
,
11527 /* Grovel through relocs to find out who stays ... */
11528 gc_mark_hook
= bed
->gc_mark_hook
;
11529 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11533 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11536 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11537 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
11538 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11542 /* Allow the backend to mark additional target specific sections. */
11543 if (bed
->gc_mark_extra_sections
)
11544 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
11546 /* ... again for sections marked from eh_frame. */
11547 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11551 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11554 /* Keep .gcc_except_table.* if the associated .text.* (or the
11555 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
11556 marked. This isn't very nice, but the proper solution,
11557 splitting .eh_frame up and using comdat doesn't pan out
11558 easily due to needing special relocs to handle the
11559 difference of two symbols in separate sections.
11560 Don't keep code sections referenced by .eh_frame. */
11561 #define TEXT_PREFIX ".text."
11562 #define TEXT_PREFIX2 ".gnu.linkonce.t."
11563 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
11564 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11565 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
11567 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
11570 const char *sec_name
;
11572 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
11574 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
11575 sec_name
= o
->name
+ o_name_prefix_len
;
11576 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
11577 tmp
= strlen (TEXT_PREFIX2
);
11578 if (tmp
> fn_name_prefix_len
)
11579 fn_name_prefix_len
= tmp
;
11581 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
11582 if (fn_name
== NULL
)
11585 /* Try the first prefix. */
11586 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
11587 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
11589 /* Try the second prefix. */
11590 if (fn_text
== NULL
)
11592 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
11593 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
11597 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
11601 /* If not using specially named exception table section,
11602 then keep whatever we are using. */
11603 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11608 /* ... and mark SEC_EXCLUDE for those that go. */
11609 return elf_gc_sweep (abfd
, info
);
11612 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11615 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
11617 struct elf_link_hash_entry
*h
,
11620 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
11621 struct elf_link_hash_entry
**search
, *child
;
11622 bfd_size_type extsymcount
;
11623 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11625 /* The sh_info field of the symtab header tells us where the
11626 external symbols start. We don't care about the local symbols at
11628 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
11629 if (!elf_bad_symtab (abfd
))
11630 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
11632 sym_hashes
= elf_sym_hashes (abfd
);
11633 sym_hashes_end
= sym_hashes
+ extsymcount
;
11635 /* Hunt down the child symbol, which is in this section at the same
11636 offset as the relocation. */
11637 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
11639 if ((child
= *search
) != NULL
11640 && (child
->root
.type
== bfd_link_hash_defined
11641 || child
->root
.type
== bfd_link_hash_defweak
)
11642 && child
->root
.u
.def
.section
== sec
11643 && child
->root
.u
.def
.value
== offset
)
11647 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
11648 abfd
, sec
, (unsigned long) offset
);
11649 bfd_set_error (bfd_error_invalid_operation
);
11653 if (!child
->vtable
)
11655 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
11656 if (!child
->vtable
)
11661 /* This *should* only be the absolute section. It could potentially
11662 be that someone has defined a non-global vtable though, which
11663 would be bad. It isn't worth paging in the local symbols to be
11664 sure though; that case should simply be handled by the assembler. */
11666 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
11669 child
->vtable
->parent
= h
;
11674 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11677 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
11678 asection
*sec ATTRIBUTE_UNUSED
,
11679 struct elf_link_hash_entry
*h
,
11682 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11683 unsigned int log_file_align
= bed
->s
->log_file_align
;
11687 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
11692 if (addend
>= h
->vtable
->size
)
11694 size_t size
, bytes
, file_align
;
11695 bfd_boolean
*ptr
= h
->vtable
->used
;
11697 /* While the symbol is undefined, we have to be prepared to handle
11699 file_align
= 1 << log_file_align
;
11700 if (h
->root
.type
== bfd_link_hash_undefined
)
11701 size
= addend
+ file_align
;
11705 if (addend
>= size
)
11707 /* Oops! We've got a reference past the defined end of
11708 the table. This is probably a bug -- shall we warn? */
11709 size
= addend
+ file_align
;
11712 size
= (size
+ file_align
- 1) & -file_align
;
11714 /* Allocate one extra entry for use as a "done" flag for the
11715 consolidation pass. */
11716 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
11720 ptr
= bfd_realloc (ptr
- 1, bytes
);
11726 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
11727 * sizeof (bfd_boolean
));
11728 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
11732 ptr
= bfd_zmalloc (bytes
);
11737 /* And arrange for that done flag to be at index -1. */
11738 h
->vtable
->used
= ptr
+ 1;
11739 h
->vtable
->size
= size
;
11742 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11747 struct alloc_got_off_arg
{
11749 unsigned int got_elt_size
;
11752 /* We need a special top-level link routine to convert got reference counts
11753 to real got offsets. */
11756 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11758 struct alloc_got_off_arg
*gofarg
= arg
;
11760 if (h
->root
.type
== bfd_link_hash_warning
)
11761 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11763 if (h
->got
.refcount
> 0)
11765 h
->got
.offset
= gofarg
->gotoff
;
11766 gofarg
->gotoff
+= gofarg
->got_elt_size
;
11769 h
->got
.offset
= (bfd_vma
) -1;
11774 /* And an accompanying bit to work out final got entry offsets once
11775 we're done. Should be called from final_link. */
11778 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11779 struct bfd_link_info
*info
)
11782 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11784 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
11785 struct alloc_got_off_arg gofarg
;
11787 if (! is_elf_hash_table (info
->hash
))
11790 /* The GOT offset is relative to the .got section, but the GOT header is
11791 put into the .got.plt section, if the backend uses it. */
11792 if (bed
->want_got_plt
)
11795 gotoff
= bed
->got_header_size
;
11797 /* Do the local .got entries first. */
11798 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11800 bfd_signed_vma
*local_got
;
11801 bfd_size_type j
, locsymcount
;
11802 Elf_Internal_Shdr
*symtab_hdr
;
11804 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11807 local_got
= elf_local_got_refcounts (i
);
11811 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11812 if (elf_bad_symtab (i
))
11813 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11815 locsymcount
= symtab_hdr
->sh_info
;
11817 for (j
= 0; j
< locsymcount
; ++j
)
11819 if (local_got
[j
] > 0)
11821 local_got
[j
] = gotoff
;
11822 gotoff
+= got_elt_size
;
11825 local_got
[j
] = (bfd_vma
) -1;
11829 /* Then the global .got entries. .plt refcounts are handled by
11830 adjust_dynamic_symbol */
11831 gofarg
.gotoff
= gotoff
;
11832 gofarg
.got_elt_size
= got_elt_size
;
11833 elf_link_hash_traverse (elf_hash_table (info
),
11834 elf_gc_allocate_got_offsets
,
11839 /* Many folk need no more in the way of final link than this, once
11840 got entry reference counting is enabled. */
11843 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11845 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11848 /* Invoke the regular ELF backend linker to do all the work. */
11849 return bfd_elf_final_link (abfd
, info
);
11853 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11855 struct elf_reloc_cookie
*rcookie
= cookie
;
11857 if (rcookie
->bad_symtab
)
11858 rcookie
->rel
= rcookie
->rels
;
11860 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11862 unsigned long r_symndx
;
11864 if (! rcookie
->bad_symtab
)
11865 if (rcookie
->rel
->r_offset
> offset
)
11867 if (rcookie
->rel
->r_offset
!= offset
)
11870 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11871 if (r_symndx
== SHN_UNDEF
)
11874 if (r_symndx
>= rcookie
->locsymcount
11875 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11877 struct elf_link_hash_entry
*h
;
11879 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11881 while (h
->root
.type
== bfd_link_hash_indirect
11882 || h
->root
.type
== bfd_link_hash_warning
)
11883 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11885 if ((h
->root
.type
== bfd_link_hash_defined
11886 || h
->root
.type
== bfd_link_hash_defweak
)
11887 && elf_discarded_section (h
->root
.u
.def
.section
))
11894 /* It's not a relocation against a global symbol,
11895 but it could be a relocation against a local
11896 symbol for a discarded section. */
11898 Elf_Internal_Sym
*isym
;
11900 /* Need to: get the symbol; get the section. */
11901 isym
= &rcookie
->locsyms
[r_symndx
];
11902 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
11904 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11905 if (isec
!= NULL
&& elf_discarded_section (isec
))
11914 /* Discard unneeded references to discarded sections.
11915 Returns TRUE if any section's size was changed. */
11916 /* This function assumes that the relocations are in sorted order,
11917 which is true for all known assemblers. */
11920 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11922 struct elf_reloc_cookie cookie
;
11923 asection
*stab
, *eh
;
11924 Elf_Internal_Shdr
*symtab_hdr
;
11925 const struct elf_backend_data
*bed
;
11927 unsigned int count
;
11928 bfd_boolean ret
= FALSE
;
11930 if (info
->traditional_format
11931 || !is_elf_hash_table (info
->hash
))
11934 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11936 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11939 bed
= get_elf_backend_data (abfd
);
11941 if ((abfd
->flags
& DYNAMIC
) != 0)
11945 if (!info
->relocatable
)
11947 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
11950 || bfd_is_abs_section (eh
->output_section
)))
11954 stab
= bfd_get_section_by_name (abfd
, ".stab");
11956 && (stab
->size
== 0
11957 || bfd_is_abs_section (stab
->output_section
)
11958 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
11963 && bed
->elf_backend_discard_info
== NULL
)
11966 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11967 cookie
.abfd
= abfd
;
11968 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
11969 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
11970 if (cookie
.bad_symtab
)
11972 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11973 cookie
.extsymoff
= 0;
11977 cookie
.locsymcount
= symtab_hdr
->sh_info
;
11978 cookie
.extsymoff
= symtab_hdr
->sh_info
;
11981 if (bed
->s
->arch_size
== 32)
11982 cookie
.r_sym_shift
= 8;
11984 cookie
.r_sym_shift
= 32;
11986 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11987 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
11989 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11990 cookie
.locsymcount
, 0,
11992 if (cookie
.locsyms
== NULL
)
11994 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12001 cookie
.rels
= NULL
;
12002 count
= stab
->reloc_count
;
12004 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
12005 info
->keep_memory
);
12006 if (cookie
.rels
!= NULL
)
12008 cookie
.rel
= cookie
.rels
;
12009 cookie
.relend
= cookie
.rels
;
12010 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
12011 if (_bfd_discard_section_stabs (abfd
, stab
,
12012 elf_section_data (stab
)->sec_info
,
12013 bfd_elf_reloc_symbol_deleted_p
,
12016 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
12017 free (cookie
.rels
);
12023 cookie
.rels
= NULL
;
12024 count
= eh
->reloc_count
;
12026 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
12027 info
->keep_memory
);
12028 cookie
.rel
= cookie
.rels
;
12029 cookie
.relend
= cookie
.rels
;
12030 if (cookie
.rels
!= NULL
)
12031 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
12033 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
12034 bfd_elf_reloc_symbol_deleted_p
,
12038 if (cookie
.rels
!= NULL
12039 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
12040 free (cookie
.rels
);
12043 if (bed
->elf_backend_discard_info
!= NULL
12044 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
12047 if (cookie
.locsyms
!= NULL
12048 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
12050 if (! info
->keep_memory
)
12051 free (cookie
.locsyms
);
12053 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
12057 if (info
->eh_frame_hdr
12058 && !info
->relocatable
12059 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12066 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
12067 struct bfd_link_info
*info
)
12070 const char *name
, *p
;
12071 struct bfd_section_already_linked
*l
;
12072 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12074 if (sec
->output_section
== bfd_abs_section_ptr
)
12077 flags
= sec
->flags
;
12079 /* Return if it isn't a linkonce section. A comdat group section
12080 also has SEC_LINK_ONCE set. */
12081 if ((flags
& SEC_LINK_ONCE
) == 0)
12084 /* Don't put group member sections on our list of already linked
12085 sections. They are handled as a group via their group section. */
12086 if (elf_sec_group (sec
) != NULL
)
12089 /* FIXME: When doing a relocatable link, we may have trouble
12090 copying relocations in other sections that refer to local symbols
12091 in the section being discarded. Those relocations will have to
12092 be converted somehow; as of this writing I'm not sure that any of
12093 the backends handle that correctly.
12095 It is tempting to instead not discard link once sections when
12096 doing a relocatable link (technically, they should be discarded
12097 whenever we are building constructors). However, that fails,
12098 because the linker winds up combining all the link once sections
12099 into a single large link once section, which defeats the purpose
12100 of having link once sections in the first place.
12102 Also, not merging link once sections in a relocatable link
12103 causes trouble for MIPS ELF, which relies on link once semantics
12104 to handle the .reginfo section correctly. */
12106 name
= bfd_get_section_name (abfd
, sec
);
12108 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12109 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12114 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
12116 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12118 /* We may have 2 different types of sections on the list: group
12119 sections and linkonce sections. Match like sections. */
12120 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12121 && strcmp (name
, l
->sec
->name
) == 0
12122 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
12124 /* The section has already been linked. See if we should
12125 issue a warning. */
12126 switch (flags
& SEC_LINK_DUPLICATES
)
12131 case SEC_LINK_DUPLICATES_DISCARD
:
12134 case SEC_LINK_DUPLICATES_ONE_ONLY
:
12135 (*_bfd_error_handler
)
12136 (_("%B: ignoring duplicate section `%A'"),
12140 case SEC_LINK_DUPLICATES_SAME_SIZE
:
12141 if (sec
->size
!= l
->sec
->size
)
12142 (*_bfd_error_handler
)
12143 (_("%B: duplicate section `%A' has different size"),
12147 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
12148 if (sec
->size
!= l
->sec
->size
)
12149 (*_bfd_error_handler
)
12150 (_("%B: duplicate section `%A' has different size"),
12152 else if (sec
->size
!= 0)
12154 bfd_byte
*sec_contents
, *l_sec_contents
;
12156 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
12157 (*_bfd_error_handler
)
12158 (_("%B: warning: could not read contents of section `%A'"),
12160 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
12162 (*_bfd_error_handler
)
12163 (_("%B: warning: could not read contents of section `%A'"),
12164 l
->sec
->owner
, l
->sec
);
12165 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
12166 (*_bfd_error_handler
)
12167 (_("%B: warning: duplicate section `%A' has different contents"),
12171 free (sec_contents
);
12172 if (l_sec_contents
)
12173 free (l_sec_contents
);
12178 /* Set the output_section field so that lang_add_section
12179 does not create a lang_input_section structure for this
12180 section. Since there might be a symbol in the section
12181 being discarded, we must retain a pointer to the section
12182 which we are really going to use. */
12183 sec
->output_section
= bfd_abs_section_ptr
;
12184 sec
->kept_section
= l
->sec
;
12186 if (flags
& SEC_GROUP
)
12188 asection
*first
= elf_next_in_group (sec
);
12189 asection
*s
= first
;
12193 s
->output_section
= bfd_abs_section_ptr
;
12194 /* Record which group discards it. */
12195 s
->kept_section
= l
->sec
;
12196 s
= elf_next_in_group (s
);
12197 /* These lists are circular. */
12207 /* A single member comdat group section may be discarded by a
12208 linkonce section and vice versa. */
12210 if ((flags
& SEC_GROUP
) != 0)
12212 asection
*first
= elf_next_in_group (sec
);
12214 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12215 /* Check this single member group against linkonce sections. */
12216 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12217 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12218 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
12219 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12221 first
->output_section
= bfd_abs_section_ptr
;
12222 first
->kept_section
= l
->sec
;
12223 sec
->output_section
= bfd_abs_section_ptr
;
12228 /* Check this linkonce section against single member groups. */
12229 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12230 if (l
->sec
->flags
& SEC_GROUP
)
12232 asection
*first
= elf_next_in_group (l
->sec
);
12235 && elf_next_in_group (first
) == first
12236 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12238 sec
->output_section
= bfd_abs_section_ptr
;
12239 sec
->kept_section
= first
;
12244 /* This is the first section with this name. Record it. */
12245 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
12249 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12251 return sym
->st_shndx
== SHN_COMMON
;
12255 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12261 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12263 return bfd_com_section_ptr
;