1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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 2 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, MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 /* Define a symbol in a dynamic linkage section. */
32 struct elf_link_hash_entry
*
33 _bfd_elf_define_linkage_sym (bfd
*abfd
,
34 struct bfd_link_info
*info
,
38 struct elf_link_hash_entry
*h
;
39 struct bfd_link_hash_entry
*bh
;
40 const struct elf_backend_data
*bed
;
42 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
45 /* Zap symbol defined in an as-needed lib that wasn't linked.
46 This is a symptom of a larger problem: Absolute symbols
47 defined in shared libraries can't be overridden, because we
48 lose the link to the bfd which is via the symbol section. */
49 h
->root
.type
= bfd_link_hash_new
;
53 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
55 get_elf_backend_data (abfd
)->collect
,
58 h
= (struct elf_link_hash_entry
*) bh
;
61 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
63 bed
= get_elf_backend_data (abfd
);
64 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
69 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
73 struct elf_link_hash_entry
*h
;
74 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
77 /* This function may be called more than once. */
78 s
= bfd_get_section_by_name (abfd
, ".got");
79 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
82 switch (bed
->s
->arch_size
)
93 bfd_set_error (bfd_error_bad_value
);
97 flags
= bed
->dynamic_sec_flags
;
99 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
101 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
104 if (bed
->want_got_plt
)
106 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
108 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
112 if (bed
->want_got_sym
)
114 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
115 (or .got.plt) section. We don't do this in the linker script
116 because we don't want to define the symbol if we are not creating
117 a global offset table. */
118 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
119 elf_hash_table (info
)->hgot
= h
;
124 /* The first bit of the global offset table is the header. */
125 s
->size
+= bed
->got_header_size
;
130 /* Create a strtab to hold the dynamic symbol names. */
132 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
134 struct elf_link_hash_table
*hash_table
;
136 hash_table
= elf_hash_table (info
);
137 if (hash_table
->dynobj
== NULL
)
138 hash_table
->dynobj
= abfd
;
140 if (hash_table
->dynstr
== NULL
)
142 hash_table
->dynstr
= _bfd_elf_strtab_init ();
143 if (hash_table
->dynstr
== NULL
)
149 /* Create some sections which will be filled in with dynamic linking
150 information. ABFD is an input file which requires dynamic sections
151 to be created. The dynamic sections take up virtual memory space
152 when the final executable is run, so we need to create them before
153 addresses are assigned to the output sections. We work out the
154 actual contents and size of these sections later. */
157 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
160 register asection
*s
;
161 const struct elf_backend_data
*bed
;
163 if (! is_elf_hash_table (info
->hash
))
166 if (elf_hash_table (info
)->dynamic_sections_created
)
169 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
172 abfd
= elf_hash_table (info
)->dynobj
;
173 bed
= get_elf_backend_data (abfd
);
175 flags
= bed
->dynamic_sec_flags
;
177 /* A dynamically linked executable has a .interp section, but a
178 shared library does not. */
179 if (info
->executable
)
181 s
= bfd_make_section_with_flags (abfd
, ".interp",
182 flags
| SEC_READONLY
);
187 if (! info
->traditional_format
)
189 s
= bfd_make_section_with_flags (abfd
, ".eh_frame_hdr",
190 flags
| SEC_READONLY
);
192 || ! bfd_set_section_alignment (abfd
, s
, 2))
194 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
197 /* Create sections to hold version informations. These are removed
198 if they are not needed. */
199 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
200 flags
| SEC_READONLY
);
202 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
205 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
206 flags
| SEC_READONLY
);
208 || ! bfd_set_section_alignment (abfd
, s
, 1))
211 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
212 flags
| SEC_READONLY
);
214 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
217 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
218 flags
| SEC_READONLY
);
220 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
223 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
224 flags
| SEC_READONLY
);
228 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
230 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
233 /* The special symbol _DYNAMIC is always set to the start of the
234 .dynamic section. We could set _DYNAMIC in a linker script, but we
235 only want to define it if we are, in fact, creating a .dynamic
236 section. We don't want to define it if there is no .dynamic
237 section, since on some ELF platforms the start up code examines it
238 to decide how to initialize the process. */
239 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
242 s
= bfd_make_section_with_flags (abfd
, ".hash",
243 flags
| SEC_READONLY
);
245 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
247 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
249 /* Let the backend create the rest of the sections. This lets the
250 backend set the right flags. The backend will normally create
251 the .got and .plt sections. */
252 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
255 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
260 /* Create dynamic sections when linking against a dynamic object. */
263 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
265 flagword flags
, pltflags
;
267 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
269 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270 .rel[a].bss sections. */
271 flags
= bed
->dynamic_sec_flags
;
274 if (bed
->plt_not_loaded
)
275 /* We do not clear SEC_ALLOC here because we still want the OS to
276 allocate space for the section; it's just that there's nothing
277 to read in from the object file. */
278 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
280 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
281 if (bed
->plt_readonly
)
282 pltflags
|= SEC_READONLY
;
284 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
286 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
289 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
291 if (bed
->want_plt_sym
292 && !_bfd_elf_define_linkage_sym (abfd
, info
, s
,
293 "_PROCEDURE_LINKAGE_TABLE_"))
296 s
= bfd_make_section_with_flags (abfd
,
297 (bed
->default_use_rela_p
298 ? ".rela.plt" : ".rel.plt"),
299 flags
| SEC_READONLY
);
301 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
304 if (! _bfd_elf_create_got_section (abfd
, info
))
307 if (bed
->want_dynbss
)
309 /* The .dynbss section is a place to put symbols which are defined
310 by dynamic objects, are referenced by regular objects, and are
311 not functions. We must allocate space for them in the process
312 image and use a R_*_COPY reloc to tell the dynamic linker to
313 initialize them at run time. The linker script puts the .dynbss
314 section into the .bss section of the final image. */
315 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
317 | SEC_LINKER_CREATED
));
321 /* The .rel[a].bss section holds copy relocs. This section is not
322 normally needed. We need to create it here, though, so that the
323 linker will map it to an output section. We can't just create it
324 only if we need it, because we will not know whether we need it
325 until we have seen all the input files, and the first time the
326 main linker code calls BFD after examining all the input files
327 (size_dynamic_sections) the input sections have already been
328 mapped to the output sections. If the section turns out not to
329 be needed, we can discard it later. We will never need this
330 section when generating a shared object, since they do not use
334 s
= bfd_make_section_with_flags (abfd
,
335 (bed
->default_use_rela_p
336 ? ".rela.bss" : ".rel.bss"),
337 flags
| SEC_READONLY
);
339 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
347 /* Record a new dynamic symbol. We record the dynamic symbols as we
348 read the input files, since we need to have a list of all of them
349 before we can determine the final sizes of the output sections.
350 Note that we may actually call this function even though we are not
351 going to output any dynamic symbols; in some cases we know that a
352 symbol should be in the dynamic symbol table, but only if there is
356 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
357 struct elf_link_hash_entry
*h
)
359 if (h
->dynindx
== -1)
361 struct elf_strtab_hash
*dynstr
;
366 /* XXX: The ABI draft says the linker must turn hidden and
367 internal symbols into STB_LOCAL symbols when producing the
368 DSO. However, if ld.so honors st_other in the dynamic table,
369 this would not be necessary. */
370 switch (ELF_ST_VISIBILITY (h
->other
))
374 if (h
->root
.type
!= bfd_link_hash_undefined
375 && h
->root
.type
!= bfd_link_hash_undefweak
)
378 if (!elf_hash_table (info
)->is_relocatable_executable
)
386 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
387 ++elf_hash_table (info
)->dynsymcount
;
389 dynstr
= elf_hash_table (info
)->dynstr
;
392 /* Create a strtab to hold the dynamic symbol names. */
393 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
398 /* We don't put any version information in the dynamic string
400 name
= h
->root
.root
.string
;
401 p
= strchr (name
, ELF_VER_CHR
);
403 /* We know that the p points into writable memory. In fact,
404 there are only a few symbols that have read-only names, being
405 those like _GLOBAL_OFFSET_TABLE_ that are created specially
406 by the backends. Most symbols will have names pointing into
407 an ELF string table read from a file, or to objalloc memory. */
410 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
415 if (indx
== (bfd_size_type
) -1)
417 h
->dynstr_index
= indx
;
423 /* Record an assignment to a symbol made by a linker script. We need
424 this in case some dynamic object refers to this symbol. */
427 bfd_elf_record_link_assignment (struct bfd_link_info
*info
,
431 struct elf_link_hash_entry
*h
;
432 struct elf_link_hash_table
*htab
;
434 if (!is_elf_hash_table (info
->hash
))
437 htab
= elf_hash_table (info
);
438 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
442 /* Since we're defining the symbol, don't let it seem to have not
443 been defined. record_dynamic_symbol and size_dynamic_sections
444 may depend on this. */
445 if (h
->root
.type
== bfd_link_hash_undefweak
446 || h
->root
.type
== bfd_link_hash_undefined
)
448 h
->root
.type
= bfd_link_hash_new
;
449 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
450 bfd_link_repair_undef_list (&htab
->root
);
453 if (h
->root
.type
== bfd_link_hash_new
)
456 /* If this symbol is being provided by the linker script, and it is
457 currently defined by a dynamic object, but not by a regular
458 object, then mark it as undefined so that the generic linker will
459 force the correct value. */
463 h
->root
.type
= bfd_link_hash_undefined
;
465 /* If this symbol is not being provided by the linker script, and it is
466 currently defined by a dynamic object, but not by a regular object,
467 then clear out any version information because the symbol will not be
468 associated with the dynamic object any more. */
472 h
->verinfo
.verdef
= NULL
;
476 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
478 if (!info
->relocatable
480 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
481 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
487 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
490 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
493 /* If this is a weak defined symbol, and we know a corresponding
494 real symbol from the same dynamic object, make sure the real
495 symbol is also made into a dynamic symbol. */
496 if (h
->u
.weakdef
!= NULL
497 && h
->u
.weakdef
->dynindx
== -1)
499 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
507 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
508 success, and 2 on a failure caused by attempting to record a symbol
509 in a discarded section, eg. a discarded link-once section symbol. */
512 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
517 struct elf_link_local_dynamic_entry
*entry
;
518 struct elf_link_hash_table
*eht
;
519 struct elf_strtab_hash
*dynstr
;
520 unsigned long dynstr_index
;
522 Elf_External_Sym_Shndx eshndx
;
523 char esym
[sizeof (Elf64_External_Sym
)];
525 if (! is_elf_hash_table (info
->hash
))
528 /* See if the entry exists already. */
529 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
530 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
533 amt
= sizeof (*entry
);
534 entry
= bfd_alloc (input_bfd
, amt
);
538 /* Go find the symbol, so that we can find it's name. */
539 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
540 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
542 bfd_release (input_bfd
, entry
);
546 if (entry
->isym
.st_shndx
!= SHN_UNDEF
547 && (entry
->isym
.st_shndx
< SHN_LORESERVE
548 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
552 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
553 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
555 /* We can still bfd_release here as nothing has done another
556 bfd_alloc. We can't do this later in this function. */
557 bfd_release (input_bfd
, entry
);
562 name
= (bfd_elf_string_from_elf_section
563 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
564 entry
->isym
.st_name
));
566 dynstr
= elf_hash_table (info
)->dynstr
;
569 /* Create a strtab to hold the dynamic symbol names. */
570 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
575 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
576 if (dynstr_index
== (unsigned long) -1)
578 entry
->isym
.st_name
= dynstr_index
;
580 eht
= elf_hash_table (info
);
582 entry
->next
= eht
->dynlocal
;
583 eht
->dynlocal
= entry
;
584 entry
->input_bfd
= input_bfd
;
585 entry
->input_indx
= input_indx
;
588 /* Whatever binding the symbol had before, it's now local. */
590 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
592 /* The dynindx will be set at the end of size_dynamic_sections. */
597 /* Return the dynindex of a local dynamic symbol. */
600 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
604 struct elf_link_local_dynamic_entry
*e
;
606 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
607 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
612 /* This function is used to renumber the dynamic symbols, if some of
613 them are removed because they are marked as local. This is called
614 via elf_link_hash_traverse. */
617 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
620 size_t *count
= data
;
622 if (h
->root
.type
== bfd_link_hash_warning
)
623 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
628 if (h
->dynindx
!= -1)
629 h
->dynindx
= ++(*count
);
635 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
636 STB_LOCAL binding. */
639 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
642 size_t *count
= data
;
644 if (h
->root
.type
== bfd_link_hash_warning
)
645 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
647 if (!h
->forced_local
)
650 if (h
->dynindx
!= -1)
651 h
->dynindx
= ++(*count
);
656 /* Return true if the dynamic symbol for a given section should be
657 omitted when creating a shared library. */
659 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
660 struct bfd_link_info
*info
,
663 switch (elf_section_data (p
)->this_hdr
.sh_type
)
667 /* If sh_type is yet undecided, assume it could be
668 SHT_PROGBITS/SHT_NOBITS. */
670 if (strcmp (p
->name
, ".got") == 0
671 || strcmp (p
->name
, ".got.plt") == 0
672 || strcmp (p
->name
, ".plt") == 0)
675 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
678 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
679 && (ip
->flags
& SEC_LINKER_CREATED
)
680 && ip
->output_section
== p
)
685 /* There shouldn't be section relative relocations
686 against any other section. */
692 /* Assign dynsym indices. In a shared library we generate a section
693 symbol for each output section, which come first. Next come symbols
694 which have been forced to local binding. Then all of the back-end
695 allocated local dynamic syms, followed by the rest of the global
699 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
700 struct bfd_link_info
*info
,
701 unsigned long *section_sym_count
)
703 unsigned long dynsymcount
= 0;
705 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
707 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
709 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
710 if ((p
->flags
& SEC_EXCLUDE
) == 0
711 && (p
->flags
& SEC_ALLOC
) != 0
712 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
713 elf_section_data (p
)->dynindx
= ++dynsymcount
;
715 *section_sym_count
= dynsymcount
;
717 elf_link_hash_traverse (elf_hash_table (info
),
718 elf_link_renumber_local_hash_table_dynsyms
,
721 if (elf_hash_table (info
)->dynlocal
)
723 struct elf_link_local_dynamic_entry
*p
;
724 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
725 p
->dynindx
= ++dynsymcount
;
728 elf_link_hash_traverse (elf_hash_table (info
),
729 elf_link_renumber_hash_table_dynsyms
,
732 /* There is an unused NULL entry at the head of the table which
733 we must account for in our count. Unless there weren't any
734 symbols, which means we'll have no table at all. */
735 if (dynsymcount
!= 0)
738 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
742 /* This function is called when we want to define a new symbol. It
743 handles the various cases which arise when we find a definition in
744 a dynamic object, or when there is already a definition in a
745 dynamic object. The new symbol is described by NAME, SYM, PSEC,
746 and PVALUE. We set SYM_HASH to the hash table entry. We set
747 OVERRIDE if the old symbol is overriding a new definition. We set
748 TYPE_CHANGE_OK if it is OK for the type to change. We set
749 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
750 change, we mean that we shouldn't warn if the type or size does
751 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
752 object is overridden by a regular object. */
755 _bfd_elf_merge_symbol (bfd
*abfd
,
756 struct bfd_link_info
*info
,
758 Elf_Internal_Sym
*sym
,
761 unsigned int *pold_alignment
,
762 struct elf_link_hash_entry
**sym_hash
,
764 bfd_boolean
*override
,
765 bfd_boolean
*type_change_ok
,
766 bfd_boolean
*size_change_ok
)
768 asection
*sec
, *oldsec
;
769 struct elf_link_hash_entry
*h
;
770 struct elf_link_hash_entry
*flip
;
773 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
774 bfd_boolean newweak
, oldweak
;
775 const struct elf_backend_data
*bed
;
781 bind
= ELF_ST_BIND (sym
->st_info
);
783 if (! bfd_is_und_section (sec
))
784 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
786 h
= ((struct elf_link_hash_entry
*)
787 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
792 /* This code is for coping with dynamic objects, and is only useful
793 if we are doing an ELF link. */
794 if (info
->hash
->creator
!= abfd
->xvec
)
797 /* For merging, we only care about real symbols. */
799 while (h
->root
.type
== bfd_link_hash_indirect
800 || h
->root
.type
== bfd_link_hash_warning
)
801 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
803 /* If we just created the symbol, mark it as being an ELF symbol.
804 Other than that, there is nothing to do--there is no merge issue
805 with a newly defined symbol--so we just return. */
807 if (h
->root
.type
== bfd_link_hash_new
)
813 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
816 switch (h
->root
.type
)
823 case bfd_link_hash_undefined
:
824 case bfd_link_hash_undefweak
:
825 oldbfd
= h
->root
.u
.undef
.abfd
;
829 case bfd_link_hash_defined
:
830 case bfd_link_hash_defweak
:
831 oldbfd
= h
->root
.u
.def
.section
->owner
;
832 oldsec
= h
->root
.u
.def
.section
;
835 case bfd_link_hash_common
:
836 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
837 oldsec
= h
->root
.u
.c
.p
->section
;
841 /* In cases involving weak versioned symbols, we may wind up trying
842 to merge a symbol with itself. Catch that here, to avoid the
843 confusion that results if we try to override a symbol with
844 itself. The additional tests catch cases like
845 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
846 dynamic object, which we do want to handle here. */
848 && ((abfd
->flags
& DYNAMIC
) == 0
852 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
853 respectively, is from a dynamic object. */
855 if ((abfd
->flags
& DYNAMIC
) != 0)
861 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
866 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
867 indices used by MIPS ELF. */
868 switch (h
->root
.type
)
874 case bfd_link_hash_defined
:
875 case bfd_link_hash_defweak
:
876 hsec
= h
->root
.u
.def
.section
;
879 case bfd_link_hash_common
:
880 hsec
= h
->root
.u
.c
.p
->section
;
887 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
890 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
891 respectively, appear to be a definition rather than reference. */
893 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
898 if (h
->root
.type
== bfd_link_hash_undefined
899 || h
->root
.type
== bfd_link_hash_undefweak
900 || h
->root
.type
== bfd_link_hash_common
)
905 /* Check TLS symbol. */
906 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
907 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
910 bfd_boolean ntdef
, tdef
;
911 asection
*ntsec
, *tsec
;
913 if (h
->type
== STT_TLS
)
933 (*_bfd_error_handler
)
934 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
935 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
936 else if (!tdef
&& !ntdef
)
937 (*_bfd_error_handler
)
938 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
939 tbfd
, ntbfd
, h
->root
.root
.string
);
941 (*_bfd_error_handler
)
942 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
943 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
945 (*_bfd_error_handler
)
946 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
947 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
949 bfd_set_error (bfd_error_bad_value
);
953 /* We need to remember if a symbol has a definition in a dynamic
954 object or is weak in all dynamic objects. Internal and hidden
955 visibility will make it unavailable to dynamic objects. */
956 if (newdyn
&& !h
->dynamic_def
)
958 if (!bfd_is_und_section (sec
))
962 /* Check if this symbol is weak in all dynamic objects. If it
963 is the first time we see it in a dynamic object, we mark
964 if it is weak. Otherwise, we clear it. */
967 if (bind
== STB_WEAK
)
970 else if (bind
!= STB_WEAK
)
975 /* If the old symbol has non-default visibility, we ignore the new
976 definition from a dynamic object. */
978 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
979 && !bfd_is_und_section (sec
))
982 /* Make sure this symbol is dynamic. */
984 /* A protected symbol has external availability. Make sure it is
987 FIXME: Should we check type and size for protected symbol? */
988 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
989 return bfd_elf_link_record_dynamic_symbol (info
, h
);
994 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
997 /* If the new symbol with non-default visibility comes from a
998 relocatable file and the old definition comes from a dynamic
999 object, we remove the old definition. */
1000 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1003 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1004 && bfd_is_und_section (sec
))
1006 /* If the new symbol is undefined and the old symbol was
1007 also undefined before, we need to make sure
1008 _bfd_generic_link_add_one_symbol doesn't mess
1009 up the linker hash table undefs list. Since the old
1010 definition came from a dynamic object, it is still on the
1012 h
->root
.type
= bfd_link_hash_undefined
;
1013 h
->root
.u
.undef
.abfd
= abfd
;
1017 h
->root
.type
= bfd_link_hash_new
;
1018 h
->root
.u
.undef
.abfd
= NULL
;
1027 /* FIXME: Should we check type and size for protected symbol? */
1033 /* Differentiate strong and weak symbols. */
1034 newweak
= bind
== STB_WEAK
;
1035 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1036 || h
->root
.type
== bfd_link_hash_undefweak
);
1038 /* If a new weak symbol definition comes from a regular file and the
1039 old symbol comes from a dynamic library, we treat the new one as
1040 strong. Similarly, an old weak symbol definition from a regular
1041 file is treated as strong when the new symbol comes from a dynamic
1042 library. Further, an old weak symbol from a dynamic library is
1043 treated as strong if the new symbol is from a dynamic library.
1044 This reflects the way glibc's ld.so works.
1046 Do this before setting *type_change_ok or *size_change_ok so that
1047 we warn properly when dynamic library symbols are overridden. */
1049 if (newdef
&& !newdyn
&& olddyn
)
1051 if (olddef
&& newdyn
)
1054 /* It's OK to change the type if either the existing symbol or the
1055 new symbol is weak. A type change is also OK if the old symbol
1056 is undefined and the new symbol is defined. */
1061 && h
->root
.type
== bfd_link_hash_undefined
))
1062 *type_change_ok
= TRUE
;
1064 /* It's OK to change the size if either the existing symbol or the
1065 new symbol is weak, or if the old symbol is undefined. */
1068 || h
->root
.type
== bfd_link_hash_undefined
)
1069 *size_change_ok
= TRUE
;
1071 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1072 symbol, respectively, appears to be a common symbol in a dynamic
1073 object. If a symbol appears in an uninitialized section, and is
1074 not weak, and is not a function, then it may be a common symbol
1075 which was resolved when the dynamic object was created. We want
1076 to treat such symbols specially, because they raise special
1077 considerations when setting the symbol size: if the symbol
1078 appears as a common symbol in a regular object, and the size in
1079 the regular object is larger, we must make sure that we use the
1080 larger size. This problematic case can always be avoided in C,
1081 but it must be handled correctly when using Fortran shared
1084 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1085 likewise for OLDDYNCOMMON and OLDDEF.
1087 Note that this test is just a heuristic, and that it is quite
1088 possible to have an uninitialized symbol in a shared object which
1089 is really a definition, rather than a common symbol. This could
1090 lead to some minor confusion when the symbol really is a common
1091 symbol in some regular object. However, I think it will be
1097 && (sec
->flags
& SEC_ALLOC
) != 0
1098 && (sec
->flags
& SEC_LOAD
) == 0
1100 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1101 newdyncommon
= TRUE
;
1103 newdyncommon
= FALSE
;
1107 && h
->root
.type
== bfd_link_hash_defined
1109 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1110 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1112 && h
->type
!= STT_FUNC
)
1113 olddyncommon
= TRUE
;
1115 olddyncommon
= FALSE
;
1117 /* We now know everything about the old and new symbols. We ask the
1118 backend to check if we can merge them. */
1119 bed
= get_elf_backend_data (abfd
);
1120 if (bed
->merge_symbol
1121 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1122 pold_alignment
, skip
, override
,
1123 type_change_ok
, size_change_ok
,
1124 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1126 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1130 /* If both the old and the new symbols look like common symbols in a
1131 dynamic object, set the size of the symbol to the larger of the
1136 && sym
->st_size
!= h
->size
)
1138 /* Since we think we have two common symbols, issue a multiple
1139 common warning if desired. Note that we only warn if the
1140 size is different. If the size is the same, we simply let
1141 the old symbol override the new one as normally happens with
1142 symbols defined in dynamic objects. */
1144 if (! ((*info
->callbacks
->multiple_common
)
1145 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1146 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1149 if (sym
->st_size
> h
->size
)
1150 h
->size
= sym
->st_size
;
1152 *size_change_ok
= TRUE
;
1155 /* If we are looking at a dynamic object, and we have found a
1156 definition, we need to see if the symbol was already defined by
1157 some other object. If so, we want to use the existing
1158 definition, and we do not want to report a multiple symbol
1159 definition error; we do this by clobbering *PSEC to be
1160 bfd_und_section_ptr.
1162 We treat a common symbol as a definition if the symbol in the
1163 shared library is a function, since common symbols always
1164 represent variables; this can cause confusion in principle, but
1165 any such confusion would seem to indicate an erroneous program or
1166 shared library. We also permit a common symbol in a regular
1167 object to override a weak symbol in a shared object. */
1172 || (h
->root
.type
== bfd_link_hash_common
1174 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1178 newdyncommon
= FALSE
;
1180 *psec
= sec
= bfd_und_section_ptr
;
1181 *size_change_ok
= TRUE
;
1183 /* If we get here when the old symbol is a common symbol, then
1184 we are explicitly letting it override a weak symbol or
1185 function in a dynamic object, and we don't want to warn about
1186 a type change. If the old symbol is a defined symbol, a type
1187 change warning may still be appropriate. */
1189 if (h
->root
.type
== bfd_link_hash_common
)
1190 *type_change_ok
= TRUE
;
1193 /* Handle the special case of an old common symbol merging with a
1194 new symbol which looks like a common symbol in a shared object.
1195 We change *PSEC and *PVALUE to make the new symbol look like a
1196 common symbol, and let _bfd_generic_link_add_one_symbol do the
1200 && h
->root
.type
== bfd_link_hash_common
)
1204 newdyncommon
= FALSE
;
1205 *pvalue
= sym
->st_size
;
1206 *psec
= sec
= bed
->common_section (oldsec
);
1207 *size_change_ok
= TRUE
;
1210 /* Skip weak definitions of symbols that are already defined. */
1211 if (newdef
&& olddef
&& newweak
)
1214 /* If the old symbol is from a dynamic object, and the new symbol is
1215 a definition which is not from a dynamic object, then the new
1216 symbol overrides the old symbol. Symbols from regular files
1217 always take precedence over symbols from dynamic objects, even if
1218 they are defined after the dynamic object in the link.
1220 As above, we again permit a common symbol in a regular object to
1221 override a definition in a shared object if the shared object
1222 symbol is a function or is weak. */
1227 || (bfd_is_com_section (sec
)
1229 || h
->type
== STT_FUNC
)))
1234 /* Change the hash table entry to undefined, and let
1235 _bfd_generic_link_add_one_symbol do the right thing with the
1238 h
->root
.type
= bfd_link_hash_undefined
;
1239 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1240 *size_change_ok
= TRUE
;
1243 olddyncommon
= FALSE
;
1245 /* We again permit a type change when a common symbol may be
1246 overriding a function. */
1248 if (bfd_is_com_section (sec
))
1249 *type_change_ok
= TRUE
;
1251 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1254 /* This union may have been set to be non-NULL when this symbol
1255 was seen in a dynamic object. We must force the union to be
1256 NULL, so that it is correct for a regular symbol. */
1257 h
->verinfo
.vertree
= NULL
;
1260 /* Handle the special case of a new common symbol merging with an
1261 old symbol that looks like it might be a common symbol defined in
1262 a shared object. Note that we have already handled the case in
1263 which a new common symbol should simply override the definition
1264 in the shared library. */
1267 && bfd_is_com_section (sec
)
1270 /* It would be best if we could set the hash table entry to a
1271 common symbol, but we don't know what to use for the section
1272 or the alignment. */
1273 if (! ((*info
->callbacks
->multiple_common
)
1274 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1275 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1278 /* If the presumed common symbol in the dynamic object is
1279 larger, pretend that the new symbol has its size. */
1281 if (h
->size
> *pvalue
)
1284 /* We need to remember the alignment required by the symbol
1285 in the dynamic object. */
1286 BFD_ASSERT (pold_alignment
);
1287 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1290 olddyncommon
= FALSE
;
1292 h
->root
.type
= bfd_link_hash_undefined
;
1293 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1295 *size_change_ok
= TRUE
;
1296 *type_change_ok
= TRUE
;
1298 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1301 h
->verinfo
.vertree
= NULL
;
1306 /* Handle the case where we had a versioned symbol in a dynamic
1307 library and now find a definition in a normal object. In this
1308 case, we make the versioned symbol point to the normal one. */
1309 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1310 flip
->root
.type
= h
->root
.type
;
1311 h
->root
.type
= bfd_link_hash_indirect
;
1312 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1313 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1314 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1318 flip
->ref_dynamic
= 1;
1325 /* This function is called to create an indirect symbol from the
1326 default for the symbol with the default version if needed. The
1327 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1328 set DYNSYM if the new indirect symbol is dynamic. */
1331 _bfd_elf_add_default_symbol (bfd
*abfd
,
1332 struct bfd_link_info
*info
,
1333 struct elf_link_hash_entry
*h
,
1335 Elf_Internal_Sym
*sym
,
1338 bfd_boolean
*dynsym
,
1339 bfd_boolean override
)
1341 bfd_boolean type_change_ok
;
1342 bfd_boolean size_change_ok
;
1345 struct elf_link_hash_entry
*hi
;
1346 struct bfd_link_hash_entry
*bh
;
1347 const struct elf_backend_data
*bed
;
1348 bfd_boolean collect
;
1349 bfd_boolean dynamic
;
1351 size_t len
, shortlen
;
1354 /* If this symbol has a version, and it is the default version, we
1355 create an indirect symbol from the default name to the fully
1356 decorated name. This will cause external references which do not
1357 specify a version to be bound to this version of the symbol. */
1358 p
= strchr (name
, ELF_VER_CHR
);
1359 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1364 /* We are overridden by an old definition. We need to check if we
1365 need to create the indirect symbol from the default name. */
1366 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1368 BFD_ASSERT (hi
!= NULL
);
1371 while (hi
->root
.type
== bfd_link_hash_indirect
1372 || hi
->root
.type
== bfd_link_hash_warning
)
1374 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1380 bed
= get_elf_backend_data (abfd
);
1381 collect
= bed
->collect
;
1382 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1384 shortlen
= p
- name
;
1385 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1386 if (shortname
== NULL
)
1388 memcpy (shortname
, name
, shortlen
);
1389 shortname
[shortlen
] = '\0';
1391 /* We are going to create a new symbol. Merge it with any existing
1392 symbol with this name. For the purposes of the merge, act as
1393 though we were defining the symbol we just defined, although we
1394 actually going to define an indirect symbol. */
1395 type_change_ok
= FALSE
;
1396 size_change_ok
= FALSE
;
1398 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1399 NULL
, &hi
, &skip
, &override
,
1400 &type_change_ok
, &size_change_ok
))
1409 if (! (_bfd_generic_link_add_one_symbol
1410 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1411 0, name
, FALSE
, collect
, &bh
)))
1413 hi
= (struct elf_link_hash_entry
*) bh
;
1417 /* In this case the symbol named SHORTNAME is overriding the
1418 indirect symbol we want to add. We were planning on making
1419 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1420 is the name without a version. NAME is the fully versioned
1421 name, and it is the default version.
1423 Overriding means that we already saw a definition for the
1424 symbol SHORTNAME in a regular object, and it is overriding
1425 the symbol defined in the dynamic object.
1427 When this happens, we actually want to change NAME, the
1428 symbol we just added, to refer to SHORTNAME. This will cause
1429 references to NAME in the shared object to become references
1430 to SHORTNAME in the regular object. This is what we expect
1431 when we override a function in a shared object: that the
1432 references in the shared object will be mapped to the
1433 definition in the regular object. */
1435 while (hi
->root
.type
== bfd_link_hash_indirect
1436 || hi
->root
.type
== bfd_link_hash_warning
)
1437 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1439 h
->root
.type
= bfd_link_hash_indirect
;
1440 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1444 hi
->ref_dynamic
= 1;
1448 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1453 /* Now set HI to H, so that the following code will set the
1454 other fields correctly. */
1458 /* If there is a duplicate definition somewhere, then HI may not
1459 point to an indirect symbol. We will have reported an error to
1460 the user in that case. */
1462 if (hi
->root
.type
== bfd_link_hash_indirect
)
1464 struct elf_link_hash_entry
*ht
;
1466 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1467 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1469 /* See if the new flags lead us to realize that the symbol must
1481 if (hi
->ref_regular
)
1487 /* We also need to define an indirection from the nondefault version
1491 len
= strlen (name
);
1492 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1493 if (shortname
== NULL
)
1495 memcpy (shortname
, name
, shortlen
);
1496 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1498 /* Once again, merge with any existing symbol. */
1499 type_change_ok
= FALSE
;
1500 size_change_ok
= FALSE
;
1502 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1503 NULL
, &hi
, &skip
, &override
,
1504 &type_change_ok
, &size_change_ok
))
1512 /* Here SHORTNAME is a versioned name, so we don't expect to see
1513 the type of override we do in the case above unless it is
1514 overridden by a versioned definition. */
1515 if (hi
->root
.type
!= bfd_link_hash_defined
1516 && hi
->root
.type
!= bfd_link_hash_defweak
)
1517 (*_bfd_error_handler
)
1518 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1524 if (! (_bfd_generic_link_add_one_symbol
1525 (info
, abfd
, shortname
, BSF_INDIRECT
,
1526 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1528 hi
= (struct elf_link_hash_entry
*) bh
;
1530 /* If there is a duplicate definition somewhere, then HI may not
1531 point to an indirect symbol. We will have reported an error
1532 to the user in that case. */
1534 if (hi
->root
.type
== bfd_link_hash_indirect
)
1536 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1538 /* See if the new flags lead us to realize that the symbol
1550 if (hi
->ref_regular
)
1560 /* This routine is used to export all defined symbols into the dynamic
1561 symbol table. It is called via elf_link_hash_traverse. */
1564 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1566 struct elf_info_failed
*eif
= data
;
1568 /* Ignore indirect symbols. These are added by the versioning code. */
1569 if (h
->root
.type
== bfd_link_hash_indirect
)
1572 if (h
->root
.type
== bfd_link_hash_warning
)
1573 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1575 if (h
->dynindx
== -1
1579 struct bfd_elf_version_tree
*t
;
1580 struct bfd_elf_version_expr
*d
;
1582 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1584 if (t
->globals
.list
!= NULL
)
1586 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1591 if (t
->locals
.list
!= NULL
)
1593 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1602 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1613 /* Look through the symbols which are defined in other shared
1614 libraries and referenced here. Update the list of version
1615 dependencies. This will be put into the .gnu.version_r section.
1616 This function is called via elf_link_hash_traverse. */
1619 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1622 struct elf_find_verdep_info
*rinfo
= data
;
1623 Elf_Internal_Verneed
*t
;
1624 Elf_Internal_Vernaux
*a
;
1627 if (h
->root
.type
== bfd_link_hash_warning
)
1628 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1630 /* We only care about symbols defined in shared objects with version
1635 || h
->verinfo
.verdef
== NULL
)
1638 /* See if we already know about this version. */
1639 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1641 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1644 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1645 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1651 /* This is a new version. Add it to tree we are building. */
1656 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1659 rinfo
->failed
= TRUE
;
1663 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1664 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1665 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1669 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1671 /* Note that we are copying a string pointer here, and testing it
1672 above. If bfd_elf_string_from_elf_section is ever changed to
1673 discard the string data when low in memory, this will have to be
1675 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1677 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1678 a
->vna_nextptr
= t
->vn_auxptr
;
1680 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1683 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1690 /* Figure out appropriate versions for all the symbols. We may not
1691 have the version number script until we have read all of the input
1692 files, so until that point we don't know which symbols should be
1693 local. This function is called via elf_link_hash_traverse. */
1696 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1698 struct elf_assign_sym_version_info
*sinfo
;
1699 struct bfd_link_info
*info
;
1700 const struct elf_backend_data
*bed
;
1701 struct elf_info_failed eif
;
1708 if (h
->root
.type
== bfd_link_hash_warning
)
1709 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1711 /* Fix the symbol flags. */
1714 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1717 sinfo
->failed
= TRUE
;
1721 /* We only need version numbers for symbols defined in regular
1723 if (!h
->def_regular
)
1726 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1727 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1728 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1730 struct bfd_elf_version_tree
*t
;
1735 /* There are two consecutive ELF_VER_CHR characters if this is
1736 not a hidden symbol. */
1738 if (*p
== ELF_VER_CHR
)
1744 /* If there is no version string, we can just return out. */
1752 /* Look for the version. If we find it, it is no longer weak. */
1753 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1755 if (strcmp (t
->name
, p
) == 0)
1759 struct bfd_elf_version_expr
*d
;
1761 len
= p
- h
->root
.root
.string
;
1762 alc
= bfd_malloc (len
);
1765 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1766 alc
[len
- 1] = '\0';
1767 if (alc
[len
- 2] == ELF_VER_CHR
)
1768 alc
[len
- 2] = '\0';
1770 h
->verinfo
.vertree
= t
;
1774 if (t
->globals
.list
!= NULL
)
1775 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1777 /* See if there is anything to force this symbol to
1779 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1781 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1784 && ! info
->export_dynamic
)
1785 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1793 /* If we are building an application, we need to create a
1794 version node for this version. */
1795 if (t
== NULL
&& info
->executable
)
1797 struct bfd_elf_version_tree
**pp
;
1800 /* If we aren't going to export this symbol, we don't need
1801 to worry about it. */
1802 if (h
->dynindx
== -1)
1806 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1809 sinfo
->failed
= TRUE
;
1814 t
->name_indx
= (unsigned int) -1;
1818 /* Don't count anonymous version tag. */
1819 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1821 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1823 t
->vernum
= version_index
;
1827 h
->verinfo
.vertree
= t
;
1831 /* We could not find the version for a symbol when
1832 generating a shared archive. Return an error. */
1833 (*_bfd_error_handler
)
1834 (_("%B: undefined versioned symbol name %s"),
1835 sinfo
->output_bfd
, h
->root
.root
.string
);
1836 bfd_set_error (bfd_error_bad_value
);
1837 sinfo
->failed
= TRUE
;
1845 /* If we don't have a version for this symbol, see if we can find
1847 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1849 struct bfd_elf_version_tree
*t
;
1850 struct bfd_elf_version_tree
*local_ver
;
1851 struct bfd_elf_version_expr
*d
;
1853 /* See if can find what version this symbol is in. If the
1854 symbol is supposed to be local, then don't actually register
1857 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1859 if (t
->globals
.list
!= NULL
)
1861 bfd_boolean matched
;
1865 while ((d
= (*t
->match
) (&t
->globals
, d
,
1866 h
->root
.root
.string
)) != NULL
)
1871 /* There is a version without definition. Make
1872 the symbol the default definition for this
1874 h
->verinfo
.vertree
= t
;
1882 /* There is no undefined version for this symbol. Hide the
1884 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1887 if (t
->locals
.list
!= NULL
)
1890 while ((d
= (*t
->match
) (&t
->locals
, d
,
1891 h
->root
.root
.string
)) != NULL
)
1894 /* If the match is "*", keep looking for a more
1895 explicit, perhaps even global, match.
1896 XXX: Shouldn't this be !d->wildcard instead? */
1897 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1906 if (local_ver
!= NULL
)
1908 h
->verinfo
.vertree
= local_ver
;
1909 if (h
->dynindx
!= -1
1910 && ! info
->export_dynamic
)
1912 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1920 /* Read and swap the relocs from the section indicated by SHDR. This
1921 may be either a REL or a RELA section. The relocations are
1922 translated into RELA relocations and stored in INTERNAL_RELOCS,
1923 which should have already been allocated to contain enough space.
1924 The EXTERNAL_RELOCS are a buffer where the external form of the
1925 relocations should be stored.
1927 Returns FALSE if something goes wrong. */
1930 elf_link_read_relocs_from_section (bfd
*abfd
,
1932 Elf_Internal_Shdr
*shdr
,
1933 void *external_relocs
,
1934 Elf_Internal_Rela
*internal_relocs
)
1936 const struct elf_backend_data
*bed
;
1937 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1938 const bfd_byte
*erela
;
1939 const bfd_byte
*erelaend
;
1940 Elf_Internal_Rela
*irela
;
1941 Elf_Internal_Shdr
*symtab_hdr
;
1944 /* Position ourselves at the start of the section. */
1945 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1948 /* Read the relocations. */
1949 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1952 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1953 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1955 bed
= get_elf_backend_data (abfd
);
1957 /* Convert the external relocations to the internal format. */
1958 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1959 swap_in
= bed
->s
->swap_reloc_in
;
1960 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1961 swap_in
= bed
->s
->swap_reloca_in
;
1964 bfd_set_error (bfd_error_wrong_format
);
1968 erela
= external_relocs
;
1969 erelaend
= erela
+ shdr
->sh_size
;
1970 irela
= internal_relocs
;
1971 while (erela
< erelaend
)
1975 (*swap_in
) (abfd
, erela
, irela
);
1976 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1977 if (bed
->s
->arch_size
== 64)
1979 if ((size_t) r_symndx
>= nsyms
)
1981 (*_bfd_error_handler
)
1982 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1983 " for offset 0x%lx in section `%A'"),
1985 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1986 bfd_set_error (bfd_error_bad_value
);
1989 irela
+= bed
->s
->int_rels_per_ext_rel
;
1990 erela
+= shdr
->sh_entsize
;
1996 /* Read and swap the relocs for a section O. They may have been
1997 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1998 not NULL, they are used as buffers to read into. They are known to
1999 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2000 the return value is allocated using either malloc or bfd_alloc,
2001 according to the KEEP_MEMORY argument. If O has two relocation
2002 sections (both REL and RELA relocations), then the REL_HDR
2003 relocations will appear first in INTERNAL_RELOCS, followed by the
2004 REL_HDR2 relocations. */
2007 _bfd_elf_link_read_relocs (bfd
*abfd
,
2009 void *external_relocs
,
2010 Elf_Internal_Rela
*internal_relocs
,
2011 bfd_boolean keep_memory
)
2013 Elf_Internal_Shdr
*rel_hdr
;
2014 void *alloc1
= NULL
;
2015 Elf_Internal_Rela
*alloc2
= NULL
;
2016 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2018 if (elf_section_data (o
)->relocs
!= NULL
)
2019 return elf_section_data (o
)->relocs
;
2021 if (o
->reloc_count
== 0)
2024 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2026 if (internal_relocs
== NULL
)
2030 size
= o
->reloc_count
;
2031 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2033 internal_relocs
= bfd_alloc (abfd
, size
);
2035 internal_relocs
= alloc2
= bfd_malloc (size
);
2036 if (internal_relocs
== NULL
)
2040 if (external_relocs
== NULL
)
2042 bfd_size_type size
= rel_hdr
->sh_size
;
2044 if (elf_section_data (o
)->rel_hdr2
)
2045 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2046 alloc1
= bfd_malloc (size
);
2049 external_relocs
= alloc1
;
2052 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2056 if (elf_section_data (o
)->rel_hdr2
2057 && (!elf_link_read_relocs_from_section
2059 elf_section_data (o
)->rel_hdr2
,
2060 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2061 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2062 * bed
->s
->int_rels_per_ext_rel
))))
2065 /* Cache the results for next time, if we can. */
2067 elf_section_data (o
)->relocs
= internal_relocs
;
2072 /* Don't free alloc2, since if it was allocated we are passing it
2073 back (under the name of internal_relocs). */
2075 return internal_relocs
;
2085 /* Compute the size of, and allocate space for, REL_HDR which is the
2086 section header for a section containing relocations for O. */
2089 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2090 Elf_Internal_Shdr
*rel_hdr
,
2093 bfd_size_type reloc_count
;
2094 bfd_size_type num_rel_hashes
;
2096 /* Figure out how many relocations there will be. */
2097 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2098 reloc_count
= elf_section_data (o
)->rel_count
;
2100 reloc_count
= elf_section_data (o
)->rel_count2
;
2102 num_rel_hashes
= o
->reloc_count
;
2103 if (num_rel_hashes
< reloc_count
)
2104 num_rel_hashes
= reloc_count
;
2106 /* That allows us to calculate the size of the section. */
2107 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2109 /* The contents field must last into write_object_contents, so we
2110 allocate it with bfd_alloc rather than malloc. Also since we
2111 cannot be sure that the contents will actually be filled in,
2112 we zero the allocated space. */
2113 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2114 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2117 /* We only allocate one set of hash entries, so we only do it the
2118 first time we are called. */
2119 if (elf_section_data (o
)->rel_hashes
== NULL
2122 struct elf_link_hash_entry
**p
;
2124 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2128 elf_section_data (o
)->rel_hashes
= p
;
2134 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2135 originated from the section given by INPUT_REL_HDR) to the
2139 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2140 asection
*input_section
,
2141 Elf_Internal_Shdr
*input_rel_hdr
,
2142 Elf_Internal_Rela
*internal_relocs
,
2143 struct elf_link_hash_entry
**rel_hash
2146 Elf_Internal_Rela
*irela
;
2147 Elf_Internal_Rela
*irelaend
;
2149 Elf_Internal_Shdr
*output_rel_hdr
;
2150 asection
*output_section
;
2151 unsigned int *rel_countp
= NULL
;
2152 const struct elf_backend_data
*bed
;
2153 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2155 output_section
= input_section
->output_section
;
2156 output_rel_hdr
= NULL
;
2158 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2159 == input_rel_hdr
->sh_entsize
)
2161 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2162 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2164 else if (elf_section_data (output_section
)->rel_hdr2
2165 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2166 == input_rel_hdr
->sh_entsize
))
2168 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2169 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2173 (*_bfd_error_handler
)
2174 (_("%B: relocation size mismatch in %B section %A"),
2175 output_bfd
, input_section
->owner
, input_section
);
2176 bfd_set_error (bfd_error_wrong_object_format
);
2180 bed
= get_elf_backend_data (output_bfd
);
2181 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2182 swap_out
= bed
->s
->swap_reloc_out
;
2183 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2184 swap_out
= bed
->s
->swap_reloca_out
;
2188 erel
= output_rel_hdr
->contents
;
2189 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2190 irela
= internal_relocs
;
2191 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2192 * bed
->s
->int_rels_per_ext_rel
);
2193 while (irela
< irelaend
)
2195 (*swap_out
) (output_bfd
, irela
, erel
);
2196 irela
+= bed
->s
->int_rels_per_ext_rel
;
2197 erel
+= input_rel_hdr
->sh_entsize
;
2200 /* Bump the counter, so that we know where to add the next set of
2202 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2207 /* Fix up the flags for a symbol. This handles various cases which
2208 can only be fixed after all the input files are seen. This is
2209 currently called by both adjust_dynamic_symbol and
2210 assign_sym_version, which is unnecessary but perhaps more robust in
2211 the face of future changes. */
2214 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2215 struct elf_info_failed
*eif
)
2217 /* If this symbol was mentioned in a non-ELF file, try to set
2218 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2219 permit a non-ELF file to correctly refer to a symbol defined in
2220 an ELF dynamic object. */
2223 while (h
->root
.type
== bfd_link_hash_indirect
)
2224 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2226 if (h
->root
.type
!= bfd_link_hash_defined
2227 && h
->root
.type
!= bfd_link_hash_defweak
)
2230 h
->ref_regular_nonweak
= 1;
2234 if (h
->root
.u
.def
.section
->owner
!= NULL
2235 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2236 == bfd_target_elf_flavour
))
2239 h
->ref_regular_nonweak
= 1;
2245 if (h
->dynindx
== -1
2249 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2258 /* Unfortunately, NON_ELF is only correct if the symbol
2259 was first seen in a non-ELF file. Fortunately, if the symbol
2260 was first seen in an ELF file, we're probably OK unless the
2261 symbol was defined in a non-ELF file. Catch that case here.
2262 FIXME: We're still in trouble if the symbol was first seen in
2263 a dynamic object, and then later in a non-ELF regular object. */
2264 if ((h
->root
.type
== bfd_link_hash_defined
2265 || h
->root
.type
== bfd_link_hash_defweak
)
2267 && (h
->root
.u
.def
.section
->owner
!= NULL
2268 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2269 != bfd_target_elf_flavour
)
2270 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2271 && !h
->def_dynamic
)))
2275 /* If this is a final link, and the symbol was defined as a common
2276 symbol in a regular object file, and there was no definition in
2277 any dynamic object, then the linker will have allocated space for
2278 the symbol in a common section but the DEF_REGULAR
2279 flag will not have been set. */
2280 if (h
->root
.type
== bfd_link_hash_defined
2284 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2287 /* If -Bsymbolic was used (which means to bind references to global
2288 symbols to the definition within the shared object), and this
2289 symbol was defined in a regular object, then it actually doesn't
2290 need a PLT entry. Likewise, if the symbol has non-default
2291 visibility. If the symbol has hidden or internal visibility, we
2292 will force it local. */
2294 && eif
->info
->shared
2295 && is_elf_hash_table (eif
->info
->hash
)
2296 && (eif
->info
->symbolic
2297 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2300 const struct elf_backend_data
*bed
;
2301 bfd_boolean force_local
;
2303 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2305 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2306 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2307 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2310 /* If a weak undefined symbol has non-default visibility, we also
2311 hide it from the dynamic linker. */
2312 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2313 && h
->root
.type
== bfd_link_hash_undefweak
)
2315 const struct elf_backend_data
*bed
;
2316 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2317 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2320 /* If this is a weak defined symbol in a dynamic object, and we know
2321 the real definition in the dynamic object, copy interesting flags
2322 over to the real definition. */
2323 if (h
->u
.weakdef
!= NULL
)
2325 struct elf_link_hash_entry
*weakdef
;
2327 weakdef
= h
->u
.weakdef
;
2328 if (h
->root
.type
== bfd_link_hash_indirect
)
2329 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2331 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2332 || h
->root
.type
== bfd_link_hash_defweak
);
2333 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2334 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2335 BFD_ASSERT (weakdef
->def_dynamic
);
2337 /* If the real definition is defined by a regular object file,
2338 don't do anything special. See the longer description in
2339 _bfd_elf_adjust_dynamic_symbol, below. */
2340 if (weakdef
->def_regular
)
2341 h
->u
.weakdef
= NULL
;
2344 const struct elf_backend_data
*bed
;
2346 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2347 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2354 /* Make the backend pick a good value for a dynamic symbol. This is
2355 called via elf_link_hash_traverse, and also calls itself
2359 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2361 struct elf_info_failed
*eif
= data
;
2363 const struct elf_backend_data
*bed
;
2365 if (! is_elf_hash_table (eif
->info
->hash
))
2368 if (h
->root
.type
== bfd_link_hash_warning
)
2370 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2371 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2373 /* When warning symbols are created, they **replace** the "real"
2374 entry in the hash table, thus we never get to see the real
2375 symbol in a hash traversal. So look at it now. */
2376 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2379 /* Ignore indirect symbols. These are added by the versioning code. */
2380 if (h
->root
.type
== bfd_link_hash_indirect
)
2383 /* Fix the symbol flags. */
2384 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2387 /* If this symbol does not require a PLT entry, and it is not
2388 defined by a dynamic object, or is not referenced by a regular
2389 object, ignore it. We do have to handle a weak defined symbol,
2390 even if no regular object refers to it, if we decided to add it
2391 to the dynamic symbol table. FIXME: Do we normally need to worry
2392 about symbols which are defined by one dynamic object and
2393 referenced by another one? */
2398 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2400 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2404 /* If we've already adjusted this symbol, don't do it again. This
2405 can happen via a recursive call. */
2406 if (h
->dynamic_adjusted
)
2409 /* Don't look at this symbol again. Note that we must set this
2410 after checking the above conditions, because we may look at a
2411 symbol once, decide not to do anything, and then get called
2412 recursively later after REF_REGULAR is set below. */
2413 h
->dynamic_adjusted
= 1;
2415 /* If this is a weak definition, and we know a real definition, and
2416 the real symbol is not itself defined by a regular object file,
2417 then get a good value for the real definition. We handle the
2418 real symbol first, for the convenience of the backend routine.
2420 Note that there is a confusing case here. If the real definition
2421 is defined by a regular object file, we don't get the real symbol
2422 from the dynamic object, but we do get the weak symbol. If the
2423 processor backend uses a COPY reloc, then if some routine in the
2424 dynamic object changes the real symbol, we will not see that
2425 change in the corresponding weak symbol. This is the way other
2426 ELF linkers work as well, and seems to be a result of the shared
2429 I will clarify this issue. Most SVR4 shared libraries define the
2430 variable _timezone and define timezone as a weak synonym. The
2431 tzset call changes _timezone. If you write
2432 extern int timezone;
2434 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2435 you might expect that, since timezone is a synonym for _timezone,
2436 the same number will print both times. However, if the processor
2437 backend uses a COPY reloc, then actually timezone will be copied
2438 into your process image, and, since you define _timezone
2439 yourself, _timezone will not. Thus timezone and _timezone will
2440 wind up at different memory locations. The tzset call will set
2441 _timezone, leaving timezone unchanged. */
2443 if (h
->u
.weakdef
!= NULL
)
2445 /* If we get to this point, we know there is an implicit
2446 reference by a regular object file via the weak symbol H.
2447 FIXME: Is this really true? What if the traversal finds
2448 H->U.WEAKDEF before it finds H? */
2449 h
->u
.weakdef
->ref_regular
= 1;
2451 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2455 /* If a symbol has no type and no size and does not require a PLT
2456 entry, then we are probably about to do the wrong thing here: we
2457 are probably going to create a COPY reloc for an empty object.
2458 This case can arise when a shared object is built with assembly
2459 code, and the assembly code fails to set the symbol type. */
2461 && h
->type
== STT_NOTYPE
2463 (*_bfd_error_handler
)
2464 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2465 h
->root
.root
.string
);
2467 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2468 bed
= get_elf_backend_data (dynobj
);
2469 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2478 /* Adjust all external symbols pointing into SEC_MERGE sections
2479 to reflect the object merging within the sections. */
2482 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2486 if (h
->root
.type
== bfd_link_hash_warning
)
2487 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2489 if ((h
->root
.type
== bfd_link_hash_defined
2490 || h
->root
.type
== bfd_link_hash_defweak
)
2491 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2492 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2494 bfd
*output_bfd
= data
;
2496 h
->root
.u
.def
.value
=
2497 _bfd_merged_section_offset (output_bfd
,
2498 &h
->root
.u
.def
.section
,
2499 elf_section_data (sec
)->sec_info
,
2500 h
->root
.u
.def
.value
);
2506 /* Returns false if the symbol referred to by H should be considered
2507 to resolve local to the current module, and true if it should be
2508 considered to bind dynamically. */
2511 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2512 struct bfd_link_info
*info
,
2513 bfd_boolean ignore_protected
)
2515 bfd_boolean binding_stays_local_p
;
2520 while (h
->root
.type
== bfd_link_hash_indirect
2521 || h
->root
.type
== bfd_link_hash_warning
)
2522 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2524 /* If it was forced local, then clearly it's not dynamic. */
2525 if (h
->dynindx
== -1)
2527 if (h
->forced_local
)
2530 /* Identify the cases where name binding rules say that a
2531 visible symbol resolves locally. */
2532 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2534 switch (ELF_ST_VISIBILITY (h
->other
))
2541 /* Proper resolution for function pointer equality may require
2542 that these symbols perhaps be resolved dynamically, even though
2543 we should be resolving them to the current module. */
2544 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2545 binding_stays_local_p
= TRUE
;
2552 /* If it isn't defined locally, then clearly it's dynamic. */
2553 if (!h
->def_regular
)
2556 /* Otherwise, the symbol is dynamic if binding rules don't tell
2557 us that it remains local. */
2558 return !binding_stays_local_p
;
2561 /* Return true if the symbol referred to by H should be considered
2562 to resolve local to the current module, and false otherwise. Differs
2563 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2564 undefined symbols and weak symbols. */
2567 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2568 struct bfd_link_info
*info
,
2569 bfd_boolean local_protected
)
2571 /* If it's a local sym, of course we resolve locally. */
2575 /* Common symbols that become definitions don't get the DEF_REGULAR
2576 flag set, so test it first, and don't bail out. */
2577 if (ELF_COMMON_DEF_P (h
))
2579 /* If we don't have a definition in a regular file, then we can't
2580 resolve locally. The sym is either undefined or dynamic. */
2581 else if (!h
->def_regular
)
2584 /* Forced local symbols resolve locally. */
2585 if (h
->forced_local
)
2588 /* As do non-dynamic symbols. */
2589 if (h
->dynindx
== -1)
2592 /* At this point, we know the symbol is defined and dynamic. In an
2593 executable it must resolve locally, likewise when building symbolic
2594 shared libraries. */
2595 if (info
->executable
|| info
->symbolic
)
2598 /* Now deal with defined dynamic symbols in shared libraries. Ones
2599 with default visibility might not resolve locally. */
2600 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2603 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2604 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2607 /* STV_PROTECTED non-function symbols are local. */
2608 if (h
->type
!= STT_FUNC
)
2611 /* Function pointer equality tests may require that STV_PROTECTED
2612 symbols be treated as dynamic symbols, even when we know that the
2613 dynamic linker will resolve them locally. */
2614 return local_protected
;
2617 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2618 aligned. Returns the first TLS output section. */
2620 struct bfd_section
*
2621 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2623 struct bfd_section
*sec
, *tls
;
2624 unsigned int align
= 0;
2626 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2627 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2631 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2632 if (sec
->alignment_power
> align
)
2633 align
= sec
->alignment_power
;
2635 elf_hash_table (info
)->tls_sec
= tls
;
2637 /* Ensure the alignment of the first section is the largest alignment,
2638 so that the tls segment starts aligned. */
2640 tls
->alignment_power
= align
;
2645 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2647 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2648 Elf_Internal_Sym
*sym
)
2650 const struct elf_backend_data
*bed
;
2652 /* Local symbols do not count, but target specific ones might. */
2653 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2654 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2657 /* Function symbols do not count. */
2658 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2661 /* If the section is undefined, then so is the symbol. */
2662 if (sym
->st_shndx
== SHN_UNDEF
)
2665 /* If the symbol is defined in the common section, then
2666 it is a common definition and so does not count. */
2667 bed
= get_elf_backend_data (abfd
);
2668 if (bed
->common_definition (sym
))
2671 /* If the symbol is in a target specific section then we
2672 must rely upon the backend to tell us what it is. */
2673 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2674 /* FIXME - this function is not coded yet:
2676 return _bfd_is_global_symbol_definition (abfd, sym);
2678 Instead for now assume that the definition is not global,
2679 Even if this is wrong, at least the linker will behave
2680 in the same way that it used to do. */
2686 /* Search the symbol table of the archive element of the archive ABFD
2687 whose archive map contains a mention of SYMDEF, and determine if
2688 the symbol is defined in this element. */
2690 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2692 Elf_Internal_Shdr
* hdr
;
2693 bfd_size_type symcount
;
2694 bfd_size_type extsymcount
;
2695 bfd_size_type extsymoff
;
2696 Elf_Internal_Sym
*isymbuf
;
2697 Elf_Internal_Sym
*isym
;
2698 Elf_Internal_Sym
*isymend
;
2701 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2705 if (! bfd_check_format (abfd
, bfd_object
))
2708 /* If we have already included the element containing this symbol in the
2709 link then we do not need to include it again. Just claim that any symbol
2710 it contains is not a definition, so that our caller will not decide to
2711 (re)include this element. */
2712 if (abfd
->archive_pass
)
2715 /* Select the appropriate symbol table. */
2716 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2717 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2719 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2721 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2723 /* The sh_info field of the symtab header tells us where the
2724 external symbols start. We don't care about the local symbols. */
2725 if (elf_bad_symtab (abfd
))
2727 extsymcount
= symcount
;
2732 extsymcount
= symcount
- hdr
->sh_info
;
2733 extsymoff
= hdr
->sh_info
;
2736 if (extsymcount
== 0)
2739 /* Read in the symbol table. */
2740 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2742 if (isymbuf
== NULL
)
2745 /* Scan the symbol table looking for SYMDEF. */
2747 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2751 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2756 if (strcmp (name
, symdef
->name
) == 0)
2758 result
= is_global_data_symbol_definition (abfd
, isym
);
2768 /* Add an entry to the .dynamic table. */
2771 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2775 struct elf_link_hash_table
*hash_table
;
2776 const struct elf_backend_data
*bed
;
2778 bfd_size_type newsize
;
2779 bfd_byte
*newcontents
;
2780 Elf_Internal_Dyn dyn
;
2782 hash_table
= elf_hash_table (info
);
2783 if (! is_elf_hash_table (hash_table
))
2786 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2788 (_("warning: creating a DT_TEXTREL in a shared object."));
2790 bed
= get_elf_backend_data (hash_table
->dynobj
);
2791 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2792 BFD_ASSERT (s
!= NULL
);
2794 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2795 newcontents
= bfd_realloc (s
->contents
, newsize
);
2796 if (newcontents
== NULL
)
2800 dyn
.d_un
.d_val
= val
;
2801 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2804 s
->contents
= newcontents
;
2809 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2810 otherwise just check whether one already exists. Returns -1 on error,
2811 1 if a DT_NEEDED tag already exists, and 0 on success. */
2814 elf_add_dt_needed_tag (bfd
*abfd
,
2815 struct bfd_link_info
*info
,
2819 struct elf_link_hash_table
*hash_table
;
2820 bfd_size_type oldsize
;
2821 bfd_size_type strindex
;
2823 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2826 hash_table
= elf_hash_table (info
);
2827 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2828 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2829 if (strindex
== (bfd_size_type
) -1)
2832 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2835 const struct elf_backend_data
*bed
;
2838 bed
= get_elf_backend_data (hash_table
->dynobj
);
2839 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2841 for (extdyn
= sdyn
->contents
;
2842 extdyn
< sdyn
->contents
+ sdyn
->size
;
2843 extdyn
+= bed
->s
->sizeof_dyn
)
2845 Elf_Internal_Dyn dyn
;
2847 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2848 if (dyn
.d_tag
== DT_NEEDED
2849 && dyn
.d_un
.d_val
== strindex
)
2851 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2859 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2862 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2866 /* We were just checking for existence of the tag. */
2867 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2872 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2873 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2874 references from regular objects to these symbols.
2876 ??? Should we do something about references from other dynamic
2877 obects? If not, we potentially lose some warnings about undefined
2878 symbols. But how can we recover the initial undefined / undefweak
2881 struct elf_smash_syms_data
2884 struct elf_link_hash_table
*htab
;
2885 bfd_boolean twiddled
;
2889 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2891 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2892 struct bfd_link_hash_entry
*bh
;
2894 switch (h
->root
.type
)
2897 case bfd_link_hash_new
:
2900 case bfd_link_hash_undefined
:
2901 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2903 if (h
->root
.u
.undef
.weak
!= NULL
2904 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2906 /* Symbol was undefweak in u.undef.weak bfd, and has become
2907 undefined in as-needed lib. Restore weak. */
2908 h
->root
.type
= bfd_link_hash_undefweak
;
2909 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2910 if (h
->root
.u
.undef
.next
!= NULL
2911 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2912 inf
->twiddled
= TRUE
;
2917 case bfd_link_hash_undefweak
:
2918 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2922 case bfd_link_hash_defined
:
2923 case bfd_link_hash_defweak
:
2924 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2928 case bfd_link_hash_common
:
2929 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2933 case bfd_link_hash_warning
:
2934 case bfd_link_hash_indirect
:
2935 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2936 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2938 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2943 /* There is no way we can undo symbol table state from defined or
2944 defweak back to undefined. */
2948 /* Set sym back to newly created state, but keep undef.next if it is
2949 being used as a list pointer. */
2950 bh
= h
->root
.u
.undef
.next
;
2953 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2954 inf
->twiddled
= TRUE
;
2955 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2956 &inf
->htab
->root
.table
,
2957 h
->root
.root
.string
);
2958 h
->root
.u
.undef
.next
= bh
;
2959 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2964 /* Sort symbol by value and section. */
2966 elf_sort_symbol (const void *arg1
, const void *arg2
)
2968 const struct elf_link_hash_entry
*h1
;
2969 const struct elf_link_hash_entry
*h2
;
2970 bfd_signed_vma vdiff
;
2972 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2973 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2974 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2976 return vdiff
> 0 ? 1 : -1;
2979 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2981 return sdiff
> 0 ? 1 : -1;
2986 /* This function is used to adjust offsets into .dynstr for
2987 dynamic symbols. This is called via elf_link_hash_traverse. */
2990 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2992 struct elf_strtab_hash
*dynstr
= data
;
2994 if (h
->root
.type
== bfd_link_hash_warning
)
2995 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2997 if (h
->dynindx
!= -1)
2998 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3002 /* Assign string offsets in .dynstr, update all structures referencing
3006 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3008 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3009 struct elf_link_local_dynamic_entry
*entry
;
3010 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3011 bfd
*dynobj
= hash_table
->dynobj
;
3014 const struct elf_backend_data
*bed
;
3017 _bfd_elf_strtab_finalize (dynstr
);
3018 size
= _bfd_elf_strtab_size (dynstr
);
3020 bed
= get_elf_backend_data (dynobj
);
3021 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3022 BFD_ASSERT (sdyn
!= NULL
);
3024 /* Update all .dynamic entries referencing .dynstr strings. */
3025 for (extdyn
= sdyn
->contents
;
3026 extdyn
< sdyn
->contents
+ sdyn
->size
;
3027 extdyn
+= bed
->s
->sizeof_dyn
)
3029 Elf_Internal_Dyn dyn
;
3031 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3035 dyn
.d_un
.d_val
= size
;
3043 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3048 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3051 /* Now update local dynamic symbols. */
3052 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3053 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3054 entry
->isym
.st_name
);
3056 /* And the rest of dynamic symbols. */
3057 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3059 /* Adjust version definitions. */
3060 if (elf_tdata (output_bfd
)->cverdefs
)
3065 Elf_Internal_Verdef def
;
3066 Elf_Internal_Verdaux defaux
;
3068 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3072 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3074 p
+= sizeof (Elf_External_Verdef
);
3075 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3077 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3079 _bfd_elf_swap_verdaux_in (output_bfd
,
3080 (Elf_External_Verdaux
*) p
, &defaux
);
3081 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3083 _bfd_elf_swap_verdaux_out (output_bfd
,
3084 &defaux
, (Elf_External_Verdaux
*) p
);
3085 p
+= sizeof (Elf_External_Verdaux
);
3088 while (def
.vd_next
);
3091 /* Adjust version references. */
3092 if (elf_tdata (output_bfd
)->verref
)
3097 Elf_Internal_Verneed need
;
3098 Elf_Internal_Vernaux needaux
;
3100 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3104 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3106 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3107 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3108 (Elf_External_Verneed
*) p
);
3109 p
+= sizeof (Elf_External_Verneed
);
3110 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3112 _bfd_elf_swap_vernaux_in (output_bfd
,
3113 (Elf_External_Vernaux
*) p
, &needaux
);
3114 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3116 _bfd_elf_swap_vernaux_out (output_bfd
,
3118 (Elf_External_Vernaux
*) p
);
3119 p
+= sizeof (Elf_External_Vernaux
);
3122 while (need
.vn_next
);
3128 /* Add symbols from an ELF object file to the linker hash table. */
3131 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3133 bfd_boolean (*add_symbol_hook
)
3134 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3135 const char **, flagword
*, asection
**, bfd_vma
*);
3136 bfd_boolean (*check_relocs
)
3137 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3138 bfd_boolean (*check_directives
)
3139 (bfd
*, struct bfd_link_info
*);
3140 bfd_boolean collect
;
3141 Elf_Internal_Shdr
*hdr
;
3142 bfd_size_type symcount
;
3143 bfd_size_type extsymcount
;
3144 bfd_size_type extsymoff
;
3145 struct elf_link_hash_entry
**sym_hash
;
3146 bfd_boolean dynamic
;
3147 Elf_External_Versym
*extversym
= NULL
;
3148 Elf_External_Versym
*ever
;
3149 struct elf_link_hash_entry
*weaks
;
3150 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3151 bfd_size_type nondeflt_vers_cnt
= 0;
3152 Elf_Internal_Sym
*isymbuf
= NULL
;
3153 Elf_Internal_Sym
*isym
;
3154 Elf_Internal_Sym
*isymend
;
3155 const struct elf_backend_data
*bed
;
3156 bfd_boolean add_needed
;
3157 struct elf_link_hash_table
* hash_table
;
3160 hash_table
= elf_hash_table (info
);
3162 bed
= get_elf_backend_data (abfd
);
3163 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3164 collect
= bed
->collect
;
3166 if ((abfd
->flags
& DYNAMIC
) == 0)
3172 /* You can't use -r against a dynamic object. Also, there's no
3173 hope of using a dynamic object which does not exactly match
3174 the format of the output file. */
3175 if (info
->relocatable
3176 || !is_elf_hash_table (hash_table
)
3177 || hash_table
->root
.creator
!= abfd
->xvec
)
3179 if (info
->relocatable
)
3180 bfd_set_error (bfd_error_invalid_operation
);
3182 bfd_set_error (bfd_error_wrong_format
);
3187 /* As a GNU extension, any input sections which are named
3188 .gnu.warning.SYMBOL are treated as warning symbols for the given
3189 symbol. This differs from .gnu.warning sections, which generate
3190 warnings when they are included in an output file. */
3191 if (info
->executable
)
3195 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3199 name
= bfd_get_section_name (abfd
, s
);
3200 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3205 name
+= sizeof ".gnu.warning." - 1;
3207 /* If this is a shared object, then look up the symbol
3208 in the hash table. If it is there, and it is already
3209 been defined, then we will not be using the entry
3210 from this shared object, so we don't need to warn.
3211 FIXME: If we see the definition in a regular object
3212 later on, we will warn, but we shouldn't. The only
3213 fix is to keep track of what warnings we are supposed
3214 to emit, and then handle them all at the end of the
3218 struct elf_link_hash_entry
*h
;
3220 h
= elf_link_hash_lookup (hash_table
, name
,
3221 FALSE
, FALSE
, TRUE
);
3223 /* FIXME: What about bfd_link_hash_common? */
3225 && (h
->root
.type
== bfd_link_hash_defined
3226 || h
->root
.type
== bfd_link_hash_defweak
))
3228 /* We don't want to issue this warning. Clobber
3229 the section size so that the warning does not
3230 get copied into the output file. */
3237 msg
= bfd_alloc (abfd
, sz
+ 1);
3241 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3246 if (! (_bfd_generic_link_add_one_symbol
3247 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3248 FALSE
, collect
, NULL
)))
3251 if (! info
->relocatable
)
3253 /* Clobber the section size so that the warning does
3254 not get copied into the output file. */
3257 /* Also set SEC_EXCLUDE, so that symbols defined in
3258 the warning section don't get copied to the output. */
3259 s
->flags
|= SEC_EXCLUDE
;
3268 /* If we are creating a shared library, create all the dynamic
3269 sections immediately. We need to attach them to something,
3270 so we attach them to this BFD, provided it is the right
3271 format. FIXME: If there are no input BFD's of the same
3272 format as the output, we can't make a shared library. */
3274 && is_elf_hash_table (hash_table
)
3275 && hash_table
->root
.creator
== abfd
->xvec
3276 && ! hash_table
->dynamic_sections_created
)
3278 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3282 else if (!is_elf_hash_table (hash_table
))
3287 const char *soname
= NULL
;
3288 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3291 /* ld --just-symbols and dynamic objects don't mix very well.
3292 Test for --just-symbols by looking at info set up by
3293 _bfd_elf_link_just_syms. */
3294 if ((s
= abfd
->sections
) != NULL
3295 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3298 /* If this dynamic lib was specified on the command line with
3299 --as-needed in effect, then we don't want to add a DT_NEEDED
3300 tag unless the lib is actually used. Similary for libs brought
3301 in by another lib's DT_NEEDED. When --no-add-needed is used
3302 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3303 any dynamic library in DT_NEEDED tags in the dynamic lib at
3305 add_needed
= (elf_dyn_lib_class (abfd
)
3306 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3307 | DYN_NO_NEEDED
)) == 0;
3309 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3315 unsigned long shlink
;
3317 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3318 goto error_free_dyn
;
3320 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3322 goto error_free_dyn
;
3323 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3325 for (extdyn
= dynbuf
;
3326 extdyn
< dynbuf
+ s
->size
;
3327 extdyn
+= bed
->s
->sizeof_dyn
)
3329 Elf_Internal_Dyn dyn
;
3331 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3332 if (dyn
.d_tag
== DT_SONAME
)
3334 unsigned int tagv
= dyn
.d_un
.d_val
;
3335 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3337 goto error_free_dyn
;
3339 if (dyn
.d_tag
== DT_NEEDED
)
3341 struct bfd_link_needed_list
*n
, **pn
;
3343 unsigned int tagv
= dyn
.d_un
.d_val
;
3345 amt
= sizeof (struct bfd_link_needed_list
);
3346 n
= bfd_alloc (abfd
, amt
);
3347 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3348 if (n
== NULL
|| fnm
== NULL
)
3349 goto error_free_dyn
;
3350 amt
= strlen (fnm
) + 1;
3351 anm
= bfd_alloc (abfd
, amt
);
3353 goto error_free_dyn
;
3354 memcpy (anm
, fnm
, amt
);
3358 for (pn
= & hash_table
->needed
;
3364 if (dyn
.d_tag
== DT_RUNPATH
)
3366 struct bfd_link_needed_list
*n
, **pn
;
3368 unsigned int tagv
= dyn
.d_un
.d_val
;
3370 amt
= sizeof (struct bfd_link_needed_list
);
3371 n
= bfd_alloc (abfd
, amt
);
3372 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3373 if (n
== NULL
|| fnm
== NULL
)
3374 goto error_free_dyn
;
3375 amt
= strlen (fnm
) + 1;
3376 anm
= bfd_alloc (abfd
, amt
);
3378 goto error_free_dyn
;
3379 memcpy (anm
, fnm
, amt
);
3383 for (pn
= & runpath
;
3389 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3390 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3392 struct bfd_link_needed_list
*n
, **pn
;
3394 unsigned int tagv
= dyn
.d_un
.d_val
;
3396 amt
= sizeof (struct bfd_link_needed_list
);
3397 n
= bfd_alloc (abfd
, amt
);
3398 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3399 if (n
== NULL
|| fnm
== NULL
)
3400 goto error_free_dyn
;
3401 amt
= strlen (fnm
) + 1;
3402 anm
= bfd_alloc (abfd
, amt
);
3409 memcpy (anm
, fnm
, amt
);
3424 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3425 frees all more recently bfd_alloc'd blocks as well. */
3431 struct bfd_link_needed_list
**pn
;
3432 for (pn
= & hash_table
->runpath
;
3439 /* We do not want to include any of the sections in a dynamic
3440 object in the output file. We hack by simply clobbering the
3441 list of sections in the BFD. This could be handled more
3442 cleanly by, say, a new section flag; the existing
3443 SEC_NEVER_LOAD flag is not the one we want, because that one
3444 still implies that the section takes up space in the output
3446 bfd_section_list_clear (abfd
);
3448 /* Find the name to use in a DT_NEEDED entry that refers to this
3449 object. If the object has a DT_SONAME entry, we use it.
3450 Otherwise, if the generic linker stuck something in
3451 elf_dt_name, we use that. Otherwise, we just use the file
3453 if (soname
== NULL
|| *soname
== '\0')
3455 soname
= elf_dt_name (abfd
);
3456 if (soname
== NULL
|| *soname
== '\0')
3457 soname
= bfd_get_filename (abfd
);
3460 /* Save the SONAME because sometimes the linker emulation code
3461 will need to know it. */
3462 elf_dt_name (abfd
) = soname
;
3464 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3468 /* If we have already included this dynamic object in the
3469 link, just ignore it. There is no reason to include a
3470 particular dynamic object more than once. */
3475 /* If this is a dynamic object, we always link against the .dynsym
3476 symbol table, not the .symtab symbol table. The dynamic linker
3477 will only see the .dynsym symbol table, so there is no reason to
3478 look at .symtab for a dynamic object. */
3480 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3481 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3483 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3485 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3487 /* The sh_info field of the symtab header tells us where the
3488 external symbols start. We don't care about the local symbols at
3490 if (elf_bad_symtab (abfd
))
3492 extsymcount
= symcount
;
3497 extsymcount
= symcount
- hdr
->sh_info
;
3498 extsymoff
= hdr
->sh_info
;
3502 if (extsymcount
!= 0)
3504 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3506 if (isymbuf
== NULL
)
3509 /* We store a pointer to the hash table entry for each external
3511 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3512 sym_hash
= bfd_alloc (abfd
, amt
);
3513 if (sym_hash
== NULL
)
3514 goto error_free_sym
;
3515 elf_sym_hashes (abfd
) = sym_hash
;
3520 /* Read in any version definitions. */
3521 if (!_bfd_elf_slurp_version_tables (abfd
,
3522 info
->default_imported_symver
))
3523 goto error_free_sym
;
3525 /* Read in the symbol versions, but don't bother to convert them
3526 to internal format. */
3527 if (elf_dynversym (abfd
) != 0)
3529 Elf_Internal_Shdr
*versymhdr
;
3531 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3532 extversym
= bfd_malloc (versymhdr
->sh_size
);
3533 if (extversym
== NULL
)
3534 goto error_free_sym
;
3535 amt
= versymhdr
->sh_size
;
3536 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3537 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3538 goto error_free_vers
;
3544 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3545 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3547 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3551 asection
*sec
, *new_sec
;
3554 struct elf_link_hash_entry
*h
;
3555 bfd_boolean definition
;
3556 bfd_boolean size_change_ok
;
3557 bfd_boolean type_change_ok
;
3558 bfd_boolean new_weakdef
;
3559 bfd_boolean override
;
3561 unsigned int old_alignment
;
3566 flags
= BSF_NO_FLAGS
;
3568 value
= isym
->st_value
;
3570 common
= bed
->common_definition (isym
);
3572 bind
= ELF_ST_BIND (isym
->st_info
);
3573 if (bind
== STB_LOCAL
)
3575 /* This should be impossible, since ELF requires that all
3576 global symbols follow all local symbols, and that sh_info
3577 point to the first global symbol. Unfortunately, Irix 5
3581 else if (bind
== STB_GLOBAL
)
3583 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3586 else if (bind
== STB_WEAK
)
3590 /* Leave it up to the processor backend. */
3593 if (isym
->st_shndx
== SHN_UNDEF
)
3594 sec
= bfd_und_section_ptr
;
3595 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3597 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3599 sec
= bfd_abs_section_ptr
;
3600 else if (sec
->kept_section
)
3602 /* Symbols from discarded section are undefined, and have
3603 default visibility. */
3604 sec
= bfd_und_section_ptr
;
3605 isym
->st_shndx
= SHN_UNDEF
;
3606 isym
->st_other
= STV_DEFAULT
3607 | (isym
->st_other
& ~ ELF_ST_VISIBILITY(-1));
3609 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3612 else if (isym
->st_shndx
== SHN_ABS
)
3613 sec
= bfd_abs_section_ptr
;
3614 else if (isym
->st_shndx
== SHN_COMMON
)
3616 sec
= bfd_com_section_ptr
;
3617 /* What ELF calls the size we call the value. What ELF
3618 calls the value we call the alignment. */
3619 value
= isym
->st_size
;
3623 /* Leave it up to the processor backend. */
3626 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3629 goto error_free_vers
;
3631 if (isym
->st_shndx
== SHN_COMMON
3632 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3634 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3638 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3641 | SEC_LINKER_CREATED
3642 | SEC_THREAD_LOCAL
));
3644 goto error_free_vers
;
3648 else if (add_symbol_hook
)
3650 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3652 goto error_free_vers
;
3654 /* The hook function sets the name to NULL if this symbol
3655 should be skipped for some reason. */
3660 /* Sanity check that all possibilities were handled. */
3663 bfd_set_error (bfd_error_bad_value
);
3664 goto error_free_vers
;
3667 if (bfd_is_und_section (sec
)
3668 || bfd_is_com_section (sec
))
3673 size_change_ok
= FALSE
;
3674 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3679 if (is_elf_hash_table (hash_table
))
3681 Elf_Internal_Versym iver
;
3682 unsigned int vernum
= 0;
3687 if (info
->default_imported_symver
)
3688 /* Use the default symbol version created earlier. */
3689 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3694 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3696 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3698 /* If this is a hidden symbol, or if it is not version
3699 1, we append the version name to the symbol name.
3700 However, we do not modify a non-hidden absolute symbol
3701 if it is not a function, because it might be the version
3702 symbol itself. FIXME: What if it isn't? */
3703 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3704 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3705 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3708 size_t namelen
, verlen
, newlen
;
3711 if (isym
->st_shndx
!= SHN_UNDEF
)
3713 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3715 else if (vernum
> 1)
3717 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3723 (*_bfd_error_handler
)
3724 (_("%B: %s: invalid version %u (max %d)"),
3726 elf_tdata (abfd
)->cverdefs
);
3727 bfd_set_error (bfd_error_bad_value
);
3728 goto error_free_vers
;
3733 /* We cannot simply test for the number of
3734 entries in the VERNEED section since the
3735 numbers for the needed versions do not start
3737 Elf_Internal_Verneed
*t
;
3740 for (t
= elf_tdata (abfd
)->verref
;
3744 Elf_Internal_Vernaux
*a
;
3746 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3748 if (a
->vna_other
== vernum
)
3750 verstr
= a
->vna_nodename
;
3759 (*_bfd_error_handler
)
3760 (_("%B: %s: invalid needed version %d"),
3761 abfd
, name
, vernum
);
3762 bfd_set_error (bfd_error_bad_value
);
3763 goto error_free_vers
;
3767 namelen
= strlen (name
);
3768 verlen
= strlen (verstr
);
3769 newlen
= namelen
+ verlen
+ 2;
3770 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3771 && isym
->st_shndx
!= SHN_UNDEF
)
3774 newname
= bfd_alloc (abfd
, newlen
);
3775 if (newname
== NULL
)
3776 goto error_free_vers
;
3777 memcpy (newname
, name
, namelen
);
3778 p
= newname
+ namelen
;
3780 /* If this is a defined non-hidden version symbol,
3781 we add another @ to the name. This indicates the
3782 default version of the symbol. */
3783 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3784 && isym
->st_shndx
!= SHN_UNDEF
)
3786 memcpy (p
, verstr
, verlen
+ 1);
3791 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3792 &value
, &old_alignment
,
3793 sym_hash
, &skip
, &override
,
3794 &type_change_ok
, &size_change_ok
))
3795 goto error_free_vers
;
3804 while (h
->root
.type
== bfd_link_hash_indirect
3805 || h
->root
.type
== bfd_link_hash_warning
)
3806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3808 /* Remember the old alignment if this is a common symbol, so
3809 that we don't reduce the alignment later on. We can't
3810 check later, because _bfd_generic_link_add_one_symbol
3811 will set a default for the alignment which we want to
3812 override. We also remember the old bfd where the existing
3813 definition comes from. */
3814 switch (h
->root
.type
)
3819 case bfd_link_hash_defined
:
3820 case bfd_link_hash_defweak
:
3821 old_bfd
= h
->root
.u
.def
.section
->owner
;
3824 case bfd_link_hash_common
:
3825 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3826 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3830 if (elf_tdata (abfd
)->verdef
!= NULL
3834 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3837 if (! (_bfd_generic_link_add_one_symbol
3838 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3839 (struct bfd_link_hash_entry
**) sym_hash
)))
3840 goto error_free_vers
;
3843 while (h
->root
.type
== bfd_link_hash_indirect
3844 || h
->root
.type
== bfd_link_hash_warning
)
3845 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3848 new_weakdef
= FALSE
;
3851 && (flags
& BSF_WEAK
) != 0
3852 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3853 && is_elf_hash_table (hash_table
)
3854 && h
->u
.weakdef
== NULL
)
3856 /* Keep a list of all weak defined non function symbols from
3857 a dynamic object, using the weakdef field. Later in this
3858 function we will set the weakdef field to the correct
3859 value. We only put non-function symbols from dynamic
3860 objects on this list, because that happens to be the only
3861 time we need to know the normal symbol corresponding to a
3862 weak symbol, and the information is time consuming to
3863 figure out. If the weakdef field is not already NULL,
3864 then this symbol was already defined by some previous
3865 dynamic object, and we will be using that previous
3866 definition anyhow. */
3868 h
->u
.weakdef
= weaks
;
3873 /* Set the alignment of a common symbol. */
3874 if ((common
|| bfd_is_com_section (sec
))
3875 && h
->root
.type
== bfd_link_hash_common
)
3880 align
= bfd_log2 (isym
->st_value
);
3883 /* The new symbol is a common symbol in a shared object.
3884 We need to get the alignment from the section. */
3885 align
= new_sec
->alignment_power
;
3887 if (align
> old_alignment
3888 /* Permit an alignment power of zero if an alignment of one
3889 is specified and no other alignments have been specified. */
3890 || (isym
->st_value
== 1 && old_alignment
== 0))
3891 h
->root
.u
.c
.p
->alignment_power
= align
;
3893 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3896 if (is_elf_hash_table (hash_table
))
3900 /* Check the alignment when a common symbol is involved. This
3901 can change when a common symbol is overridden by a normal
3902 definition or a common symbol is ignored due to the old
3903 normal definition. We need to make sure the maximum
3904 alignment is maintained. */
3905 if ((old_alignment
|| common
)
3906 && h
->root
.type
!= bfd_link_hash_common
)
3908 unsigned int common_align
;
3909 unsigned int normal_align
;
3910 unsigned int symbol_align
;
3914 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3915 if (h
->root
.u
.def
.section
->owner
!= NULL
3916 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3918 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3919 if (normal_align
> symbol_align
)
3920 normal_align
= symbol_align
;
3923 normal_align
= symbol_align
;
3927 common_align
= old_alignment
;
3928 common_bfd
= old_bfd
;
3933 common_align
= bfd_log2 (isym
->st_value
);
3935 normal_bfd
= old_bfd
;
3938 if (normal_align
< common_align
)
3939 (*_bfd_error_handler
)
3940 (_("Warning: alignment %u of symbol `%s' in %B"
3941 " is smaller than %u in %B"),
3942 normal_bfd
, common_bfd
,
3943 1 << normal_align
, name
, 1 << common_align
);
3946 /* Remember the symbol size and type. */
3947 if (isym
->st_size
!= 0
3948 && (definition
|| h
->size
== 0))
3950 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3951 (*_bfd_error_handler
)
3952 (_("Warning: size of symbol `%s' changed"
3953 " from %lu in %B to %lu in %B"),
3955 name
, (unsigned long) h
->size
,
3956 (unsigned long) isym
->st_size
);
3958 h
->size
= isym
->st_size
;
3961 /* If this is a common symbol, then we always want H->SIZE
3962 to be the size of the common symbol. The code just above
3963 won't fix the size if a common symbol becomes larger. We
3964 don't warn about a size change here, because that is
3965 covered by --warn-common. */
3966 if (h
->root
.type
== bfd_link_hash_common
)
3967 h
->size
= h
->root
.u
.c
.size
;
3969 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3970 && (definition
|| h
->type
== STT_NOTYPE
))
3972 if (h
->type
!= STT_NOTYPE
3973 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3974 && ! type_change_ok
)
3975 (*_bfd_error_handler
)
3976 (_("Warning: type of symbol `%s' changed"
3977 " from %d to %d in %B"),
3978 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3980 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3983 /* If st_other has a processor-specific meaning, specific
3984 code might be needed here. We never merge the visibility
3985 attribute with the one from a dynamic object. */
3986 if (bed
->elf_backend_merge_symbol_attribute
)
3987 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3990 /* If this symbol has default visibility and the user has requested
3991 we not re-export it, then mark it as hidden. */
3992 if (definition
&& !dynamic
3994 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3995 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3996 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3998 if (isym
->st_other
!= 0 && !dynamic
)
4000 unsigned char hvis
, symvis
, other
, nvis
;
4002 /* Take the balance of OTHER from the definition. */
4003 other
= (definition
? isym
->st_other
: h
->other
);
4004 other
&= ~ ELF_ST_VISIBILITY (-1);
4006 /* Combine visibilities, using the most constraining one. */
4007 hvis
= ELF_ST_VISIBILITY (h
->other
);
4008 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4014 nvis
= hvis
< symvis
? hvis
: symvis
;
4016 h
->other
= other
| nvis
;
4019 /* Set a flag in the hash table entry indicating the type of
4020 reference or definition we just found. Keep a count of
4021 the number of dynamic symbols we find. A dynamic symbol
4022 is one which is referenced or defined by both a regular
4023 object and a shared object. */
4030 if (bind
!= STB_WEAK
)
4031 h
->ref_regular_nonweak
= 1;
4035 if (! info
->executable
4048 || (h
->u
.weakdef
!= NULL
4050 && h
->u
.weakdef
->dynindx
!= -1))
4054 /* Check to see if we need to add an indirect symbol for
4055 the default name. */
4056 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4057 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4058 &sec
, &value
, &dynsym
,
4060 goto error_free_vers
;
4062 if (definition
&& !dynamic
)
4064 char *p
= strchr (name
, ELF_VER_CHR
);
4065 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4067 /* Queue non-default versions so that .symver x, x@FOO
4068 aliases can be checked. */
4069 if (! nondeflt_vers
)
4071 amt
= (isymend
- isym
+ 1)
4072 * sizeof (struct elf_link_hash_entry
*);
4073 nondeflt_vers
= bfd_malloc (amt
);
4075 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4079 if (dynsym
&& h
->dynindx
== -1)
4081 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4082 goto error_free_vers
;
4083 if (h
->u
.weakdef
!= NULL
4085 && h
->u
.weakdef
->dynindx
== -1)
4087 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4088 goto error_free_vers
;
4091 else if (dynsym
&& h
->dynindx
!= -1)
4092 /* If the symbol already has a dynamic index, but
4093 visibility says it should not be visible, turn it into
4095 switch (ELF_ST_VISIBILITY (h
->other
))
4099 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4110 const char *soname
= elf_dt_name (abfd
);
4112 /* A symbol from a library loaded via DT_NEEDED of some
4113 other library is referenced by a regular object.
4114 Add a DT_NEEDED entry for it. Issue an error if
4115 --no-add-needed is used. */
4116 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4118 (*_bfd_error_handler
)
4119 (_("%s: invalid DSO for symbol `%s' definition"),
4121 bfd_set_error (bfd_error_bad_value
);
4122 goto error_free_vers
;
4125 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4128 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4130 goto error_free_vers
;
4132 BFD_ASSERT (ret
== 0);
4137 /* Now that all the symbols from this input file are created, handle
4138 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4139 if (nondeflt_vers
!= NULL
)
4141 bfd_size_type cnt
, symidx
;
4143 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4145 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4146 char *shortname
, *p
;
4148 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4150 || (h
->root
.type
!= bfd_link_hash_defined
4151 && h
->root
.type
!= bfd_link_hash_defweak
))
4154 amt
= p
- h
->root
.root
.string
;
4155 shortname
= bfd_malloc (amt
+ 1);
4156 memcpy (shortname
, h
->root
.root
.string
, amt
);
4157 shortname
[amt
] = '\0';
4159 hi
= (struct elf_link_hash_entry
*)
4160 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4161 FALSE
, FALSE
, FALSE
);
4163 && hi
->root
.type
== h
->root
.type
4164 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4165 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4167 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4168 hi
->root
.type
= bfd_link_hash_indirect
;
4169 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4170 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4171 sym_hash
= elf_sym_hashes (abfd
);
4173 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4174 if (sym_hash
[symidx
] == hi
)
4176 sym_hash
[symidx
] = h
;
4182 free (nondeflt_vers
);
4183 nondeflt_vers
= NULL
;
4186 if (extversym
!= NULL
)
4192 if (isymbuf
!= NULL
)
4197 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4199 /* Remove symbols defined in an as-needed shared lib that wasn't
4201 struct elf_smash_syms_data inf
;
4202 inf
.not_needed
= abfd
;
4203 inf
.htab
= hash_table
;
4204 inf
.twiddled
= FALSE
;
4205 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4207 bfd_link_repair_undef_list (&hash_table
->root
);
4211 /* Now set the weakdefs field correctly for all the weak defined
4212 symbols we found. The only way to do this is to search all the
4213 symbols. Since we only need the information for non functions in
4214 dynamic objects, that's the only time we actually put anything on
4215 the list WEAKS. We need this information so that if a regular
4216 object refers to a symbol defined weakly in a dynamic object, the
4217 real symbol in the dynamic object is also put in the dynamic
4218 symbols; we also must arrange for both symbols to point to the
4219 same memory location. We could handle the general case of symbol
4220 aliasing, but a general symbol alias can only be generated in
4221 assembler code, handling it correctly would be very time
4222 consuming, and other ELF linkers don't handle general aliasing
4226 struct elf_link_hash_entry
**hpp
;
4227 struct elf_link_hash_entry
**hppend
;
4228 struct elf_link_hash_entry
**sorted_sym_hash
;
4229 struct elf_link_hash_entry
*h
;
4232 /* Since we have to search the whole symbol list for each weak
4233 defined symbol, search time for N weak defined symbols will be
4234 O(N^2). Binary search will cut it down to O(NlogN). */
4235 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4236 sorted_sym_hash
= bfd_malloc (amt
);
4237 if (sorted_sym_hash
== NULL
)
4239 sym_hash
= sorted_sym_hash
;
4240 hpp
= elf_sym_hashes (abfd
);
4241 hppend
= hpp
+ extsymcount
;
4243 for (; hpp
< hppend
; hpp
++)
4247 && h
->root
.type
== bfd_link_hash_defined
4248 && h
->type
!= STT_FUNC
)
4256 qsort (sorted_sym_hash
, sym_count
,
4257 sizeof (struct elf_link_hash_entry
*),
4260 while (weaks
!= NULL
)
4262 struct elf_link_hash_entry
*hlook
;
4269 weaks
= hlook
->u
.weakdef
;
4270 hlook
->u
.weakdef
= NULL
;
4272 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4273 || hlook
->root
.type
== bfd_link_hash_defweak
4274 || hlook
->root
.type
== bfd_link_hash_common
4275 || hlook
->root
.type
== bfd_link_hash_indirect
);
4276 slook
= hlook
->root
.u
.def
.section
;
4277 vlook
= hlook
->root
.u
.def
.value
;
4284 bfd_signed_vma vdiff
;
4286 h
= sorted_sym_hash
[idx
];
4287 vdiff
= vlook
- h
->root
.u
.def
.value
;
4294 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4307 /* We didn't find a value/section match. */
4311 for (i
= ilook
; i
< sym_count
; i
++)
4313 h
= sorted_sym_hash
[i
];
4315 /* Stop if value or section doesn't match. */
4316 if (h
->root
.u
.def
.value
!= vlook
4317 || h
->root
.u
.def
.section
!= slook
)
4319 else if (h
!= hlook
)
4321 hlook
->u
.weakdef
= h
;
4323 /* If the weak definition is in the list of dynamic
4324 symbols, make sure the real definition is put
4326 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4328 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4332 /* If the real definition is in the list of dynamic
4333 symbols, make sure the weak definition is put
4334 there as well. If we don't do this, then the
4335 dynamic loader might not merge the entries for the
4336 real definition and the weak definition. */
4337 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4339 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4347 free (sorted_sym_hash
);
4350 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4351 if (check_directives
)
4352 check_directives (abfd
, info
);
4354 /* If this object is the same format as the output object, and it is
4355 not a shared library, then let the backend look through the
4358 This is required to build global offset table entries and to
4359 arrange for dynamic relocs. It is not required for the
4360 particular common case of linking non PIC code, even when linking
4361 against shared libraries, but unfortunately there is no way of
4362 knowing whether an object file has been compiled PIC or not.
4363 Looking through the relocs is not particularly time consuming.
4364 The problem is that we must either (1) keep the relocs in memory,
4365 which causes the linker to require additional runtime memory or
4366 (2) read the relocs twice from the input file, which wastes time.
4367 This would be a good case for using mmap.
4369 I have no idea how to handle linking PIC code into a file of a
4370 different format. It probably can't be done. */
4371 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4373 && is_elf_hash_table (hash_table
)
4374 && hash_table
->root
.creator
== abfd
->xvec
4375 && check_relocs
!= NULL
)
4379 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4381 Elf_Internal_Rela
*internal_relocs
;
4384 if ((o
->flags
& SEC_RELOC
) == 0
4385 || o
->reloc_count
== 0
4386 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4387 && (o
->flags
& SEC_DEBUGGING
) != 0)
4388 || bfd_is_abs_section (o
->output_section
))
4391 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4393 if (internal_relocs
== NULL
)
4396 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4398 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4399 free (internal_relocs
);
4406 /* If this is a non-traditional link, try to optimize the handling
4407 of the .stab/.stabstr sections. */
4409 && ! info
->traditional_format
4410 && is_elf_hash_table (hash_table
)
4411 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4415 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4416 if (stabstr
!= NULL
)
4418 bfd_size_type string_offset
= 0;
4421 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4422 if (strncmp (".stab", stab
->name
, 5) == 0
4423 && (!stab
->name
[5] ||
4424 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4425 && (stab
->flags
& SEC_MERGE
) == 0
4426 && !bfd_is_abs_section (stab
->output_section
))
4428 struct bfd_elf_section_data
*secdata
;
4430 secdata
= elf_section_data (stab
);
4431 if (! _bfd_link_section_stabs (abfd
,
4432 &hash_table
->stab_info
,
4437 if (secdata
->sec_info
)
4438 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4443 if (is_elf_hash_table (hash_table
) && add_needed
)
4445 /* Add this bfd to the loaded list. */
4446 struct elf_link_loaded_list
*n
;
4448 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4452 n
->next
= hash_table
->loaded
;
4453 hash_table
->loaded
= n
;
4459 if (nondeflt_vers
!= NULL
)
4460 free (nondeflt_vers
);
4461 if (extversym
!= NULL
)
4464 if (isymbuf
!= NULL
)
4470 /* Return the linker hash table entry of a symbol that might be
4471 satisfied by an archive symbol. Return -1 on error. */
4473 struct elf_link_hash_entry
*
4474 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4475 struct bfd_link_info
*info
,
4478 struct elf_link_hash_entry
*h
;
4482 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4486 /* If this is a default version (the name contains @@), look up the
4487 symbol again with only one `@' as well as without the version.
4488 The effect is that references to the symbol with and without the
4489 version will be matched by the default symbol in the archive. */
4491 p
= strchr (name
, ELF_VER_CHR
);
4492 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4495 /* First check with only one `@'. */
4496 len
= strlen (name
);
4497 copy
= bfd_alloc (abfd
, len
);
4499 return (struct elf_link_hash_entry
*) 0 - 1;
4501 first
= p
- name
+ 1;
4502 memcpy (copy
, name
, first
);
4503 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4505 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4508 /* We also need to check references to the symbol without the
4510 copy
[first
- 1] = '\0';
4511 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4512 FALSE
, FALSE
, FALSE
);
4515 bfd_release (abfd
, copy
);
4519 /* Add symbols from an ELF archive file to the linker hash table. We
4520 don't use _bfd_generic_link_add_archive_symbols because of a
4521 problem which arises on UnixWare. The UnixWare libc.so is an
4522 archive which includes an entry libc.so.1 which defines a bunch of
4523 symbols. The libc.so archive also includes a number of other
4524 object files, which also define symbols, some of which are the same
4525 as those defined in libc.so.1. Correct linking requires that we
4526 consider each object file in turn, and include it if it defines any
4527 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4528 this; it looks through the list of undefined symbols, and includes
4529 any object file which defines them. When this algorithm is used on
4530 UnixWare, it winds up pulling in libc.so.1 early and defining a
4531 bunch of symbols. This means that some of the other objects in the
4532 archive are not included in the link, which is incorrect since they
4533 precede libc.so.1 in the archive.
4535 Fortunately, ELF archive handling is simpler than that done by
4536 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4537 oddities. In ELF, if we find a symbol in the archive map, and the
4538 symbol is currently undefined, we know that we must pull in that
4541 Unfortunately, we do have to make multiple passes over the symbol
4542 table until nothing further is resolved. */
4545 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4548 bfd_boolean
*defined
= NULL
;
4549 bfd_boolean
*included
= NULL
;
4553 const struct elf_backend_data
*bed
;
4554 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4555 (bfd
*, struct bfd_link_info
*, const char *);
4557 if (! bfd_has_map (abfd
))
4559 /* An empty archive is a special case. */
4560 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4562 bfd_set_error (bfd_error_no_armap
);
4566 /* Keep track of all symbols we know to be already defined, and all
4567 files we know to be already included. This is to speed up the
4568 second and subsequent passes. */
4569 c
= bfd_ardata (abfd
)->symdef_count
;
4573 amt
*= sizeof (bfd_boolean
);
4574 defined
= bfd_zmalloc (amt
);
4575 included
= bfd_zmalloc (amt
);
4576 if (defined
== NULL
|| included
== NULL
)
4579 symdefs
= bfd_ardata (abfd
)->symdefs
;
4580 bed
= get_elf_backend_data (abfd
);
4581 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4594 symdefend
= symdef
+ c
;
4595 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4597 struct elf_link_hash_entry
*h
;
4599 struct bfd_link_hash_entry
*undefs_tail
;
4602 if (defined
[i
] || included
[i
])
4604 if (symdef
->file_offset
== last
)
4610 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4611 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4617 if (h
->root
.type
== bfd_link_hash_common
)
4619 /* We currently have a common symbol. The archive map contains
4620 a reference to this symbol, so we may want to include it. We
4621 only want to include it however, if this archive element
4622 contains a definition of the symbol, not just another common
4625 Unfortunately some archivers (including GNU ar) will put
4626 declarations of common symbols into their archive maps, as
4627 well as real definitions, so we cannot just go by the archive
4628 map alone. Instead we must read in the element's symbol
4629 table and check that to see what kind of symbol definition
4631 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4634 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4636 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4641 /* We need to include this archive member. */
4642 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4643 if (element
== NULL
)
4646 if (! bfd_check_format (element
, bfd_object
))
4649 /* Doublecheck that we have not included this object
4650 already--it should be impossible, but there may be
4651 something wrong with the archive. */
4652 if (element
->archive_pass
!= 0)
4654 bfd_set_error (bfd_error_bad_value
);
4657 element
->archive_pass
= 1;
4659 undefs_tail
= info
->hash
->undefs_tail
;
4661 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4664 if (! bfd_link_add_symbols (element
, info
))
4667 /* If there are any new undefined symbols, we need to make
4668 another pass through the archive in order to see whether
4669 they can be defined. FIXME: This isn't perfect, because
4670 common symbols wind up on undefs_tail and because an
4671 undefined symbol which is defined later on in this pass
4672 does not require another pass. This isn't a bug, but it
4673 does make the code less efficient than it could be. */
4674 if (undefs_tail
!= info
->hash
->undefs_tail
)
4677 /* Look backward to mark all symbols from this object file
4678 which we have already seen in this pass. */
4682 included
[mark
] = TRUE
;
4687 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4689 /* We mark subsequent symbols from this object file as we go
4690 on through the loop. */
4691 last
= symdef
->file_offset
;
4702 if (defined
!= NULL
)
4704 if (included
!= NULL
)
4709 /* Given an ELF BFD, add symbols to the global hash table as
4713 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4715 switch (bfd_get_format (abfd
))
4718 return elf_link_add_object_symbols (abfd
, info
);
4720 return elf_link_add_archive_symbols (abfd
, info
);
4722 bfd_set_error (bfd_error_wrong_format
);
4727 /* This function will be called though elf_link_hash_traverse to store
4728 all hash value of the exported symbols in an array. */
4731 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4733 unsigned long **valuep
= data
;
4739 if (h
->root
.type
== bfd_link_hash_warning
)
4740 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4742 /* Ignore indirect symbols. These are added by the versioning code. */
4743 if (h
->dynindx
== -1)
4746 name
= h
->root
.root
.string
;
4747 p
= strchr (name
, ELF_VER_CHR
);
4750 alc
= bfd_malloc (p
- name
+ 1);
4751 memcpy (alc
, name
, p
- name
);
4752 alc
[p
- name
] = '\0';
4756 /* Compute the hash value. */
4757 ha
= bfd_elf_hash (name
);
4759 /* Store the found hash value in the array given as the argument. */
4762 /* And store it in the struct so that we can put it in the hash table
4764 h
->u
.elf_hash_value
= ha
;
4772 /* Array used to determine the number of hash table buckets to use
4773 based on the number of symbols there are. If there are fewer than
4774 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4775 fewer than 37 we use 17 buckets, and so forth. We never use more
4776 than 32771 buckets. */
4778 static const size_t elf_buckets
[] =
4780 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4784 /* Compute bucket count for hashing table. We do not use a static set
4785 of possible tables sizes anymore. Instead we determine for all
4786 possible reasonable sizes of the table the outcome (i.e., the
4787 number of collisions etc) and choose the best solution. The
4788 weighting functions are not too simple to allow the table to grow
4789 without bounds. Instead one of the weighting factors is the size.
4790 Therefore the result is always a good payoff between few collisions
4791 (= short chain lengths) and table size. */
4793 compute_bucket_count (struct bfd_link_info
*info
)
4795 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4796 size_t best_size
= 0;
4797 unsigned long int *hashcodes
;
4798 unsigned long int *hashcodesp
;
4799 unsigned long int i
;
4802 /* Compute the hash values for all exported symbols. At the same
4803 time store the values in an array so that we could use them for
4806 amt
*= sizeof (unsigned long int);
4807 hashcodes
= bfd_malloc (amt
);
4808 if (hashcodes
== NULL
)
4810 hashcodesp
= hashcodes
;
4812 /* Put all hash values in HASHCODES. */
4813 elf_link_hash_traverse (elf_hash_table (info
),
4814 elf_collect_hash_codes
, &hashcodesp
);
4816 /* We have a problem here. The following code to optimize the table
4817 size requires an integer type with more the 32 bits. If
4818 BFD_HOST_U_64_BIT is set we know about such a type. */
4819 #ifdef BFD_HOST_U_64_BIT
4822 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4825 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4826 unsigned long int *counts
;
4827 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4828 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4830 /* Possible optimization parameters: if we have NSYMS symbols we say
4831 that the hashing table must at least have NSYMS/4 and at most
4833 minsize
= nsyms
/ 4;
4836 best_size
= maxsize
= nsyms
* 2;
4838 /* Create array where we count the collisions in. We must use bfd_malloc
4839 since the size could be large. */
4841 amt
*= sizeof (unsigned long int);
4842 counts
= bfd_malloc (amt
);
4849 /* Compute the "optimal" size for the hash table. The criteria is a
4850 minimal chain length. The minor criteria is (of course) the size
4852 for (i
= minsize
; i
< maxsize
; ++i
)
4854 /* Walk through the array of hashcodes and count the collisions. */
4855 BFD_HOST_U_64_BIT max
;
4856 unsigned long int j
;
4857 unsigned long int fact
;
4859 memset (counts
, '\0', i
* sizeof (unsigned long int));
4861 /* Determine how often each hash bucket is used. */
4862 for (j
= 0; j
< nsyms
; ++j
)
4863 ++counts
[hashcodes
[j
] % i
];
4865 /* For the weight function we need some information about the
4866 pagesize on the target. This is information need not be 100%
4867 accurate. Since this information is not available (so far) we
4868 define it here to a reasonable default value. If it is crucial
4869 to have a better value some day simply define this value. */
4870 # ifndef BFD_TARGET_PAGESIZE
4871 # define BFD_TARGET_PAGESIZE (4096)
4874 /* We in any case need 2 + NSYMS entries for the size values and
4876 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4879 /* Variant 1: optimize for short chains. We add the squares
4880 of all the chain lengths (which favors many small chain
4881 over a few long chains). */
4882 for (j
= 0; j
< i
; ++j
)
4883 max
+= counts
[j
] * counts
[j
];
4885 /* This adds penalties for the overall size of the table. */
4886 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4889 /* Variant 2: Optimize a lot more for small table. Here we
4890 also add squares of the size but we also add penalties for
4891 empty slots (the +1 term). */
4892 for (j
= 0; j
< i
; ++j
)
4893 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4895 /* The overall size of the table is considered, but not as
4896 strong as in variant 1, where it is squared. */
4897 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4901 /* Compare with current best results. */
4902 if (max
< best_chlen
)
4912 #endif /* defined (BFD_HOST_U_64_BIT) */
4914 /* This is the fallback solution if no 64bit type is available or if we
4915 are not supposed to spend much time on optimizations. We select the
4916 bucket count using a fixed set of numbers. */
4917 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4919 best_size
= elf_buckets
[i
];
4920 if (dynsymcount
< elf_buckets
[i
+ 1])
4925 /* Free the arrays we needed. */
4931 /* Set up the sizes and contents of the ELF dynamic sections. This is
4932 called by the ELF linker emulation before_allocation routine. We
4933 must set the sizes of the sections before the linker sets the
4934 addresses of the various sections. */
4937 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4940 const char *filter_shlib
,
4941 const char * const *auxiliary_filters
,
4942 struct bfd_link_info
*info
,
4943 asection
**sinterpptr
,
4944 struct bfd_elf_version_tree
*verdefs
)
4946 bfd_size_type soname_indx
;
4948 const struct elf_backend_data
*bed
;
4949 struct elf_assign_sym_version_info asvinfo
;
4953 soname_indx
= (bfd_size_type
) -1;
4955 if (!is_elf_hash_table (info
->hash
))
4958 elf_tdata (output_bfd
)->relro
= info
->relro
;
4959 if (info
->execstack
)
4960 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4961 else if (info
->noexecstack
)
4962 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4966 asection
*notesec
= NULL
;
4969 for (inputobj
= info
->input_bfds
;
4971 inputobj
= inputobj
->link_next
)
4975 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4977 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4980 if (s
->flags
& SEC_CODE
)
4989 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4990 if (exec
&& info
->relocatable
4991 && notesec
->output_section
!= bfd_abs_section_ptr
)
4992 notesec
->output_section
->flags
|= SEC_CODE
;
4996 /* Any syms created from now on start with -1 in
4997 got.refcount/offset and plt.refcount/offset. */
4998 elf_hash_table (info
)->init_got_refcount
4999 = elf_hash_table (info
)->init_got_offset
;
5000 elf_hash_table (info
)->init_plt_refcount
5001 = elf_hash_table (info
)->init_plt_offset
;
5003 /* The backend may have to create some sections regardless of whether
5004 we're dynamic or not. */
5005 bed
= get_elf_backend_data (output_bfd
);
5006 if (bed
->elf_backend_always_size_sections
5007 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5010 dynobj
= elf_hash_table (info
)->dynobj
;
5012 /* If there were no dynamic objects in the link, there is nothing to
5017 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5020 if (elf_hash_table (info
)->dynamic_sections_created
)
5022 struct elf_info_failed eif
;
5023 struct elf_link_hash_entry
*h
;
5025 struct bfd_elf_version_tree
*t
;
5026 struct bfd_elf_version_expr
*d
;
5028 bfd_boolean all_defined
;
5030 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5031 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5035 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5037 if (soname_indx
== (bfd_size_type
) -1
5038 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5044 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5046 info
->flags
|= DF_SYMBOLIC
;
5053 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5055 if (indx
== (bfd_size_type
) -1
5056 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5059 if (info
->new_dtags
)
5061 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5062 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5067 if (filter_shlib
!= NULL
)
5071 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5072 filter_shlib
, TRUE
);
5073 if (indx
== (bfd_size_type
) -1
5074 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5078 if (auxiliary_filters
!= NULL
)
5080 const char * const *p
;
5082 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5086 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5088 if (indx
== (bfd_size_type
) -1
5089 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5095 eif
.verdefs
= verdefs
;
5098 /* If we are supposed to export all symbols into the dynamic symbol
5099 table (this is not the normal case), then do so. */
5100 if (info
->export_dynamic
)
5102 elf_link_hash_traverse (elf_hash_table (info
),
5103 _bfd_elf_export_symbol
,
5109 /* Make all global versions with definition. */
5110 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5111 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5112 if (!d
->symver
&& d
->symbol
)
5114 const char *verstr
, *name
;
5115 size_t namelen
, verlen
, newlen
;
5117 struct elf_link_hash_entry
*newh
;
5120 namelen
= strlen (name
);
5122 verlen
= strlen (verstr
);
5123 newlen
= namelen
+ verlen
+ 3;
5125 newname
= bfd_malloc (newlen
);
5126 if (newname
== NULL
)
5128 memcpy (newname
, name
, namelen
);
5130 /* Check the hidden versioned definition. */
5131 p
= newname
+ namelen
;
5133 memcpy (p
, verstr
, verlen
+ 1);
5134 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5135 newname
, FALSE
, FALSE
,
5138 || (newh
->root
.type
!= bfd_link_hash_defined
5139 && newh
->root
.type
!= bfd_link_hash_defweak
))
5141 /* Check the default versioned definition. */
5143 memcpy (p
, verstr
, verlen
+ 1);
5144 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5145 newname
, FALSE
, FALSE
,
5150 /* Mark this version if there is a definition and it is
5151 not defined in a shared object. */
5153 && !newh
->def_dynamic
5154 && (newh
->root
.type
== bfd_link_hash_defined
5155 || newh
->root
.type
== bfd_link_hash_defweak
))
5159 /* Attach all the symbols to their version information. */
5160 asvinfo
.output_bfd
= output_bfd
;
5161 asvinfo
.info
= info
;
5162 asvinfo
.verdefs
= verdefs
;
5163 asvinfo
.failed
= FALSE
;
5165 elf_link_hash_traverse (elf_hash_table (info
),
5166 _bfd_elf_link_assign_sym_version
,
5171 if (!info
->allow_undefined_version
)
5173 /* Check if all global versions have a definition. */
5175 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5176 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5177 if (!d
->symver
&& !d
->script
)
5179 (*_bfd_error_handler
)
5180 (_("%s: undefined version: %s"),
5181 d
->pattern
, t
->name
);
5182 all_defined
= FALSE
;
5187 bfd_set_error (bfd_error_bad_value
);
5192 /* Find all symbols which were defined in a dynamic object and make
5193 the backend pick a reasonable value for them. */
5194 elf_link_hash_traverse (elf_hash_table (info
),
5195 _bfd_elf_adjust_dynamic_symbol
,
5200 /* Add some entries to the .dynamic section. We fill in some of the
5201 values later, in bfd_elf_final_link, but we must add the entries
5202 now so that we know the final size of the .dynamic section. */
5204 /* If there are initialization and/or finalization functions to
5205 call then add the corresponding DT_INIT/DT_FINI entries. */
5206 h
= (info
->init_function
5207 ? elf_link_hash_lookup (elf_hash_table (info
),
5208 info
->init_function
, FALSE
,
5215 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5218 h
= (info
->fini_function
5219 ? elf_link_hash_lookup (elf_hash_table (info
),
5220 info
->fini_function
, FALSE
,
5227 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5231 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5232 if (s
!= NULL
&& s
->linker_has_input
)
5234 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5235 if (! info
->executable
)
5240 for (sub
= info
->input_bfds
; sub
!= NULL
;
5241 sub
= sub
->link_next
)
5242 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5243 if (elf_section_data (o
)->this_hdr
.sh_type
5244 == SHT_PREINIT_ARRAY
)
5246 (*_bfd_error_handler
)
5247 (_("%B: .preinit_array section is not allowed in DSO"),
5252 bfd_set_error (bfd_error_nonrepresentable_section
);
5256 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5257 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5260 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5261 if (s
!= NULL
&& s
->linker_has_input
)
5263 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5264 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5267 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5268 if (s
!= NULL
&& s
->linker_has_input
)
5270 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5271 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5275 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5276 /* If .dynstr is excluded from the link, we don't want any of
5277 these tags. Strictly, we should be checking each section
5278 individually; This quick check covers for the case where
5279 someone does a /DISCARD/ : { *(*) }. */
5280 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5282 bfd_size_type strsize
;
5284 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5285 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5286 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5287 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5288 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5289 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5290 bed
->s
->sizeof_sym
))
5295 /* The backend must work out the sizes of all the other dynamic
5297 if (bed
->elf_backend_size_dynamic_sections
5298 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5301 if (elf_hash_table (info
)->dynamic_sections_created
)
5303 unsigned long section_sym_count
;
5306 /* Set up the version definition section. */
5307 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5308 BFD_ASSERT (s
!= NULL
);
5310 /* We may have created additional version definitions if we are
5311 just linking a regular application. */
5312 verdefs
= asvinfo
.verdefs
;
5314 /* Skip anonymous version tag. */
5315 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5316 verdefs
= verdefs
->next
;
5318 if (verdefs
== NULL
&& !info
->create_default_symver
)
5319 s
->flags
|= SEC_EXCLUDE
;
5324 struct bfd_elf_version_tree
*t
;
5326 Elf_Internal_Verdef def
;
5327 Elf_Internal_Verdaux defaux
;
5328 struct bfd_link_hash_entry
*bh
;
5329 struct elf_link_hash_entry
*h
;
5335 /* Make space for the base version. */
5336 size
+= sizeof (Elf_External_Verdef
);
5337 size
+= sizeof (Elf_External_Verdaux
);
5340 /* Make space for the default version. */
5341 if (info
->create_default_symver
)
5343 size
+= sizeof (Elf_External_Verdef
);
5347 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5349 struct bfd_elf_version_deps
*n
;
5351 size
+= sizeof (Elf_External_Verdef
);
5352 size
+= sizeof (Elf_External_Verdaux
);
5355 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5356 size
+= sizeof (Elf_External_Verdaux
);
5360 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5361 if (s
->contents
== NULL
&& s
->size
!= 0)
5364 /* Fill in the version definition section. */
5368 def
.vd_version
= VER_DEF_CURRENT
;
5369 def
.vd_flags
= VER_FLG_BASE
;
5372 if (info
->create_default_symver
)
5374 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5375 def
.vd_next
= sizeof (Elf_External_Verdef
);
5379 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5380 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5381 + sizeof (Elf_External_Verdaux
));
5384 if (soname_indx
!= (bfd_size_type
) -1)
5386 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5388 def
.vd_hash
= bfd_elf_hash (soname
);
5389 defaux
.vda_name
= soname_indx
;
5396 name
= lbasename (output_bfd
->filename
);
5397 def
.vd_hash
= bfd_elf_hash (name
);
5398 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5400 if (indx
== (bfd_size_type
) -1)
5402 defaux
.vda_name
= indx
;
5404 defaux
.vda_next
= 0;
5406 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5407 (Elf_External_Verdef
*) p
);
5408 p
+= sizeof (Elf_External_Verdef
);
5409 if (info
->create_default_symver
)
5411 /* Add a symbol representing this version. */
5413 if (! (_bfd_generic_link_add_one_symbol
5414 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5416 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5418 h
= (struct elf_link_hash_entry
*) bh
;
5421 h
->type
= STT_OBJECT
;
5422 h
->verinfo
.vertree
= NULL
;
5424 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5427 /* Create a duplicate of the base version with the same
5428 aux block, but different flags. */
5431 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5433 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5434 + sizeof (Elf_External_Verdaux
));
5437 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5438 (Elf_External_Verdef
*) p
);
5439 p
+= sizeof (Elf_External_Verdef
);
5441 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5442 (Elf_External_Verdaux
*) p
);
5443 p
+= sizeof (Elf_External_Verdaux
);
5445 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5448 struct bfd_elf_version_deps
*n
;
5451 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5454 /* Add a symbol representing this version. */
5456 if (! (_bfd_generic_link_add_one_symbol
5457 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5459 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5461 h
= (struct elf_link_hash_entry
*) bh
;
5464 h
->type
= STT_OBJECT
;
5465 h
->verinfo
.vertree
= t
;
5467 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5470 def
.vd_version
= VER_DEF_CURRENT
;
5472 if (t
->globals
.list
== NULL
5473 && t
->locals
.list
== NULL
5475 def
.vd_flags
|= VER_FLG_WEAK
;
5476 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5477 def
.vd_cnt
= cdeps
+ 1;
5478 def
.vd_hash
= bfd_elf_hash (t
->name
);
5479 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5481 if (t
->next
!= NULL
)
5482 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5483 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5485 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5486 (Elf_External_Verdef
*) p
);
5487 p
+= sizeof (Elf_External_Verdef
);
5489 defaux
.vda_name
= h
->dynstr_index
;
5490 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5492 defaux
.vda_next
= 0;
5493 if (t
->deps
!= NULL
)
5494 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5495 t
->name_indx
= defaux
.vda_name
;
5497 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5498 (Elf_External_Verdaux
*) p
);
5499 p
+= sizeof (Elf_External_Verdaux
);
5501 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5503 if (n
->version_needed
== NULL
)
5505 /* This can happen if there was an error in the
5507 defaux
.vda_name
= 0;
5511 defaux
.vda_name
= n
->version_needed
->name_indx
;
5512 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5515 if (n
->next
== NULL
)
5516 defaux
.vda_next
= 0;
5518 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5520 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5521 (Elf_External_Verdaux
*) p
);
5522 p
+= sizeof (Elf_External_Verdaux
);
5526 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5527 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5530 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5533 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5535 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5538 else if (info
->flags
& DF_BIND_NOW
)
5540 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5546 if (info
->executable
)
5547 info
->flags_1
&= ~ (DF_1_INITFIRST
5550 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5554 /* Work out the size of the version reference section. */
5556 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5557 BFD_ASSERT (s
!= NULL
);
5559 struct elf_find_verdep_info sinfo
;
5561 sinfo
.output_bfd
= output_bfd
;
5563 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5564 if (sinfo
.vers
== 0)
5566 sinfo
.failed
= FALSE
;
5568 elf_link_hash_traverse (elf_hash_table (info
),
5569 _bfd_elf_link_find_version_dependencies
,
5572 if (elf_tdata (output_bfd
)->verref
== NULL
)
5573 s
->flags
|= SEC_EXCLUDE
;
5576 Elf_Internal_Verneed
*t
;
5581 /* Build the version definition section. */
5584 for (t
= elf_tdata (output_bfd
)->verref
;
5588 Elf_Internal_Vernaux
*a
;
5590 size
+= sizeof (Elf_External_Verneed
);
5592 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5593 size
+= sizeof (Elf_External_Vernaux
);
5597 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5598 if (s
->contents
== NULL
)
5602 for (t
= elf_tdata (output_bfd
)->verref
;
5607 Elf_Internal_Vernaux
*a
;
5611 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5614 t
->vn_version
= VER_NEED_CURRENT
;
5616 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5617 elf_dt_name (t
->vn_bfd
) != NULL
5618 ? elf_dt_name (t
->vn_bfd
)
5619 : lbasename (t
->vn_bfd
->filename
),
5621 if (indx
== (bfd_size_type
) -1)
5624 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5625 if (t
->vn_nextref
== NULL
)
5628 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5629 + caux
* sizeof (Elf_External_Vernaux
));
5631 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5632 (Elf_External_Verneed
*) p
);
5633 p
+= sizeof (Elf_External_Verneed
);
5635 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5637 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5638 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5639 a
->vna_nodename
, FALSE
);
5640 if (indx
== (bfd_size_type
) -1)
5643 if (a
->vna_nextptr
== NULL
)
5646 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5648 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5649 (Elf_External_Vernaux
*) p
);
5650 p
+= sizeof (Elf_External_Vernaux
);
5654 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5655 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5658 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5662 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5663 && elf_tdata (output_bfd
)->cverdefs
== 0)
5664 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5665 §ion_sym_count
) == 0)
5667 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5668 s
->flags
|= SEC_EXCLUDE
;
5675 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5677 if (!is_elf_hash_table (info
->hash
))
5680 if (elf_hash_table (info
)->dynamic_sections_created
)
5683 const struct elf_backend_data
*bed
;
5685 bfd_size_type dynsymcount
;
5686 unsigned long section_sym_count
;
5687 size_t bucketcount
= 0;
5688 size_t hash_entry_size
;
5689 unsigned int dtagcount
;
5691 dynobj
= elf_hash_table (info
)->dynobj
;
5693 /* Assign dynsym indicies. In a shared library we generate a
5694 section symbol for each output section, which come first.
5695 Next come all of the back-end allocated local dynamic syms,
5696 followed by the rest of the global symbols. */
5698 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5699 §ion_sym_count
);
5701 /* Work out the size of the symbol version section. */
5702 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5703 BFD_ASSERT (s
!= NULL
);
5704 if (dynsymcount
!= 0
5705 && (s
->flags
& SEC_EXCLUDE
) == 0)
5707 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5708 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5709 if (s
->contents
== NULL
)
5712 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5716 /* Set the size of the .dynsym and .hash sections. We counted
5717 the number of dynamic symbols in elf_link_add_object_symbols.
5718 We will build the contents of .dynsym and .hash when we build
5719 the final symbol table, because until then we do not know the
5720 correct value to give the symbols. We built the .dynstr
5721 section as we went along in elf_link_add_object_symbols. */
5722 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5723 BFD_ASSERT (s
!= NULL
);
5724 bed
= get_elf_backend_data (output_bfd
);
5725 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5727 if (dynsymcount
!= 0)
5729 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5730 if (s
->contents
== NULL
)
5733 /* The first entry in .dynsym is a dummy symbol.
5734 Clear all the section syms, in case we don't output them all. */
5735 ++section_sym_count
;
5736 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5739 /* Compute the size of the hashing table. As a side effect this
5740 computes the hash values for all the names we export. */
5741 bucketcount
= compute_bucket_count (info
);
5743 s
= bfd_get_section_by_name (dynobj
, ".hash");
5744 BFD_ASSERT (s
!= NULL
);
5745 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5746 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5747 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5748 if (s
->contents
== NULL
)
5751 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5752 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5753 s
->contents
+ hash_entry_size
);
5755 elf_hash_table (info
)->bucketcount
= bucketcount
;
5757 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5758 BFD_ASSERT (s
!= NULL
);
5760 elf_finalize_dynstr (output_bfd
, info
);
5762 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5764 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5765 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5772 /* Final phase of ELF linker. */
5774 /* A structure we use to avoid passing large numbers of arguments. */
5776 struct elf_final_link_info
5778 /* General link information. */
5779 struct bfd_link_info
*info
;
5782 /* Symbol string table. */
5783 struct bfd_strtab_hash
*symstrtab
;
5784 /* .dynsym section. */
5785 asection
*dynsym_sec
;
5786 /* .hash section. */
5788 /* symbol version section (.gnu.version). */
5789 asection
*symver_sec
;
5790 /* Buffer large enough to hold contents of any section. */
5792 /* Buffer large enough to hold external relocs of any section. */
5793 void *external_relocs
;
5794 /* Buffer large enough to hold internal relocs of any section. */
5795 Elf_Internal_Rela
*internal_relocs
;
5796 /* Buffer large enough to hold external local symbols of any input
5798 bfd_byte
*external_syms
;
5799 /* And a buffer for symbol section indices. */
5800 Elf_External_Sym_Shndx
*locsym_shndx
;
5801 /* Buffer large enough to hold internal local symbols of any input
5803 Elf_Internal_Sym
*internal_syms
;
5804 /* Array large enough to hold a symbol index for each local symbol
5805 of any input BFD. */
5807 /* Array large enough to hold a section pointer for each local
5808 symbol of any input BFD. */
5809 asection
**sections
;
5810 /* Buffer to hold swapped out symbols. */
5812 /* And one for symbol section indices. */
5813 Elf_External_Sym_Shndx
*symshndxbuf
;
5814 /* Number of swapped out symbols in buffer. */
5815 size_t symbuf_count
;
5816 /* Number of symbols which fit in symbuf. */
5818 /* And same for symshndxbuf. */
5819 size_t shndxbuf_size
;
5822 /* This struct is used to pass information to elf_link_output_extsym. */
5824 struct elf_outext_info
5827 bfd_boolean localsyms
;
5828 struct elf_final_link_info
*finfo
;
5831 /* When performing a relocatable link, the input relocations are
5832 preserved. But, if they reference global symbols, the indices
5833 referenced must be updated. Update all the relocations in
5834 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5837 elf_link_adjust_relocs (bfd
*abfd
,
5838 Elf_Internal_Shdr
*rel_hdr
,
5840 struct elf_link_hash_entry
**rel_hash
)
5843 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5845 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5846 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5847 bfd_vma r_type_mask
;
5850 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5852 swap_in
= bed
->s
->swap_reloc_in
;
5853 swap_out
= bed
->s
->swap_reloc_out
;
5855 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5857 swap_in
= bed
->s
->swap_reloca_in
;
5858 swap_out
= bed
->s
->swap_reloca_out
;
5863 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5866 if (bed
->s
->arch_size
== 32)
5873 r_type_mask
= 0xffffffff;
5877 erela
= rel_hdr
->contents
;
5878 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5880 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5883 if (*rel_hash
== NULL
)
5886 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5888 (*swap_in
) (abfd
, erela
, irela
);
5889 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5890 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5891 | (irela
[j
].r_info
& r_type_mask
));
5892 (*swap_out
) (abfd
, irela
, erela
);
5896 struct elf_link_sort_rela
5902 enum elf_reloc_type_class type
;
5903 /* We use this as an array of size int_rels_per_ext_rel. */
5904 Elf_Internal_Rela rela
[1];
5908 elf_link_sort_cmp1 (const void *A
, const void *B
)
5910 const struct elf_link_sort_rela
*a
= A
;
5911 const struct elf_link_sort_rela
*b
= B
;
5912 int relativea
, relativeb
;
5914 relativea
= a
->type
== reloc_class_relative
;
5915 relativeb
= b
->type
== reloc_class_relative
;
5917 if (relativea
< relativeb
)
5919 if (relativea
> relativeb
)
5921 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5923 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5925 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5927 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5933 elf_link_sort_cmp2 (const void *A
, const void *B
)
5935 const struct elf_link_sort_rela
*a
= A
;
5936 const struct elf_link_sort_rela
*b
= B
;
5939 if (a
->u
.offset
< b
->u
.offset
)
5941 if (a
->u
.offset
> b
->u
.offset
)
5943 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5944 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5949 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5951 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5957 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5960 bfd_size_type count
, size
;
5961 size_t i
, ret
, sort_elt
, ext_size
;
5962 bfd_byte
*sort
, *s_non_relative
, *p
;
5963 struct elf_link_sort_rela
*sq
;
5964 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5965 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5966 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5967 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5968 struct bfd_link_order
*lo
;
5971 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5972 if (reldyn
== NULL
|| reldyn
->size
== 0)
5974 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5975 if (reldyn
== NULL
|| reldyn
->size
== 0)
5977 ext_size
= bed
->s
->sizeof_rel
;
5978 swap_in
= bed
->s
->swap_reloc_in
;
5979 swap_out
= bed
->s
->swap_reloc_out
;
5983 ext_size
= bed
->s
->sizeof_rela
;
5984 swap_in
= bed
->s
->swap_reloca_in
;
5985 swap_out
= bed
->s
->swap_reloca_out
;
5987 count
= reldyn
->size
/ ext_size
;
5990 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5991 if (lo
->type
== bfd_indirect_link_order
)
5993 asection
*o
= lo
->u
.indirect
.section
;
5997 if (size
!= reldyn
->size
)
6000 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6001 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6002 sort
= bfd_zmalloc (sort_elt
* count
);
6005 (*info
->callbacks
->warning
)
6006 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6010 if (bed
->s
->arch_size
== 32)
6011 r_sym_mask
= ~(bfd_vma
) 0xff;
6013 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6015 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6016 if (lo
->type
== bfd_indirect_link_order
)
6018 bfd_byte
*erel
, *erelend
;
6019 asection
*o
= lo
->u
.indirect
.section
;
6021 if (o
->contents
== NULL
&& o
->size
!= 0)
6023 /* This is a reloc section that is being handled as a normal
6024 section. See bfd_section_from_shdr. We can't combine
6025 relocs in this case. */
6030 erelend
= o
->contents
+ o
->size
;
6031 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6032 while (erel
< erelend
)
6034 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6035 (*swap_in
) (abfd
, erel
, s
->rela
);
6036 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6037 s
->u
.sym_mask
= r_sym_mask
;
6043 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6045 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6047 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6048 if (s
->type
!= reloc_class_relative
)
6054 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6055 for (; i
< count
; i
++, p
+= sort_elt
)
6057 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6058 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6060 sp
->u
.offset
= sq
->rela
->r_offset
;
6063 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6065 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6066 if (lo
->type
== bfd_indirect_link_order
)
6068 bfd_byte
*erel
, *erelend
;
6069 asection
*o
= lo
->u
.indirect
.section
;
6072 erelend
= o
->contents
+ o
->size
;
6073 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6074 while (erel
< erelend
)
6076 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6077 (*swap_out
) (abfd
, s
->rela
, erel
);
6088 /* Flush the output symbols to the file. */
6091 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6092 const struct elf_backend_data
*bed
)
6094 if (finfo
->symbuf_count
> 0)
6096 Elf_Internal_Shdr
*hdr
;
6100 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6101 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6102 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6103 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6104 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6107 hdr
->sh_size
+= amt
;
6108 finfo
->symbuf_count
= 0;
6114 /* Add a symbol to the output symbol table. */
6117 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6119 Elf_Internal_Sym
*elfsym
,
6120 asection
*input_sec
,
6121 struct elf_link_hash_entry
*h
)
6124 Elf_External_Sym_Shndx
*destshndx
;
6125 bfd_boolean (*output_symbol_hook
)
6126 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6127 struct elf_link_hash_entry
*);
6128 const struct elf_backend_data
*bed
;
6130 bed
= get_elf_backend_data (finfo
->output_bfd
);
6131 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6132 if (output_symbol_hook
!= NULL
)
6134 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6138 if (name
== NULL
|| *name
== '\0')
6139 elfsym
->st_name
= 0;
6140 else if (input_sec
->flags
& SEC_EXCLUDE
)
6141 elfsym
->st_name
= 0;
6144 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6146 if (elfsym
->st_name
== (unsigned long) -1)
6150 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6152 if (! elf_link_flush_output_syms (finfo
, bed
))
6156 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6157 destshndx
= finfo
->symshndxbuf
;
6158 if (destshndx
!= NULL
)
6160 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6164 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6165 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6166 if (destshndx
== NULL
)
6168 memset ((char *) destshndx
+ amt
, 0, amt
);
6169 finfo
->shndxbuf_size
*= 2;
6171 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6174 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6175 finfo
->symbuf_count
+= 1;
6176 bfd_get_symcount (finfo
->output_bfd
) += 1;
6181 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6182 allowing an unsatisfied unversioned symbol in the DSO to match a
6183 versioned symbol that would normally require an explicit version.
6184 We also handle the case that a DSO references a hidden symbol
6185 which may be satisfied by a versioned symbol in another DSO. */
6188 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6189 const struct elf_backend_data
*bed
,
6190 struct elf_link_hash_entry
*h
)
6193 struct elf_link_loaded_list
*loaded
;
6195 if (!is_elf_hash_table (info
->hash
))
6198 switch (h
->root
.type
)
6204 case bfd_link_hash_undefined
:
6205 case bfd_link_hash_undefweak
:
6206 abfd
= h
->root
.u
.undef
.abfd
;
6207 if ((abfd
->flags
& DYNAMIC
) == 0
6208 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6212 case bfd_link_hash_defined
:
6213 case bfd_link_hash_defweak
:
6214 abfd
= h
->root
.u
.def
.section
->owner
;
6217 case bfd_link_hash_common
:
6218 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6221 BFD_ASSERT (abfd
!= NULL
);
6223 for (loaded
= elf_hash_table (info
)->loaded
;
6225 loaded
= loaded
->next
)
6228 Elf_Internal_Shdr
*hdr
;
6229 bfd_size_type symcount
;
6230 bfd_size_type extsymcount
;
6231 bfd_size_type extsymoff
;
6232 Elf_Internal_Shdr
*versymhdr
;
6233 Elf_Internal_Sym
*isym
;
6234 Elf_Internal_Sym
*isymend
;
6235 Elf_Internal_Sym
*isymbuf
;
6236 Elf_External_Versym
*ever
;
6237 Elf_External_Versym
*extversym
;
6239 input
= loaded
->abfd
;
6241 /* We check each DSO for a possible hidden versioned definition. */
6243 || (input
->flags
& DYNAMIC
) == 0
6244 || elf_dynversym (input
) == 0)
6247 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6249 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6250 if (elf_bad_symtab (input
))
6252 extsymcount
= symcount
;
6257 extsymcount
= symcount
- hdr
->sh_info
;
6258 extsymoff
= hdr
->sh_info
;
6261 if (extsymcount
== 0)
6264 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6266 if (isymbuf
== NULL
)
6269 /* Read in any version definitions. */
6270 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6271 extversym
= bfd_malloc (versymhdr
->sh_size
);
6272 if (extversym
== NULL
)
6275 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6276 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6277 != versymhdr
->sh_size
))
6285 ever
= extversym
+ extsymoff
;
6286 isymend
= isymbuf
+ extsymcount
;
6287 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6290 Elf_Internal_Versym iver
;
6291 unsigned short version_index
;
6293 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6294 || isym
->st_shndx
== SHN_UNDEF
)
6297 name
= bfd_elf_string_from_elf_section (input
,
6300 if (strcmp (name
, h
->root
.root
.string
) != 0)
6303 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6305 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6307 /* If we have a non-hidden versioned sym, then it should
6308 have provided a definition for the undefined sym. */
6312 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6313 if (version_index
== 1 || version_index
== 2)
6315 /* This is the base or first version. We can use it. */
6329 /* Add an external symbol to the symbol table. This is called from
6330 the hash table traversal routine. When generating a shared object,
6331 we go through the symbol table twice. The first time we output
6332 anything that might have been forced to local scope in a version
6333 script. The second time we output the symbols that are still
6337 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6339 struct elf_outext_info
*eoinfo
= data
;
6340 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6342 Elf_Internal_Sym sym
;
6343 asection
*input_sec
;
6344 const struct elf_backend_data
*bed
;
6346 if (h
->root
.type
== bfd_link_hash_warning
)
6348 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6349 if (h
->root
.type
== bfd_link_hash_new
)
6353 /* Decide whether to output this symbol in this pass. */
6354 if (eoinfo
->localsyms
)
6356 if (!h
->forced_local
)
6361 if (h
->forced_local
)
6365 bed
= get_elf_backend_data (finfo
->output_bfd
);
6367 /* If we have an undefined symbol reference here then it must have
6368 come from a shared library that is being linked in. (Undefined
6369 references in regular files have already been handled). If we
6370 are reporting errors for this situation then do so now. */
6371 if (h
->root
.type
== bfd_link_hash_undefined
6374 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6375 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6377 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6378 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6379 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6381 eoinfo
->failed
= TRUE
;
6386 /* We should also warn if a forced local symbol is referenced from
6387 shared libraries. */
6388 if (! finfo
->info
->relocatable
6389 && (! finfo
->info
->shared
)
6394 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6396 (*_bfd_error_handler
)
6397 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6399 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6400 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6401 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6403 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6404 ? "hidden" : "local",
6405 h
->root
.root
.string
);
6406 eoinfo
->failed
= TRUE
;
6410 /* We don't want to output symbols that have never been mentioned by
6411 a regular file, or that we have been told to strip. However, if
6412 h->indx is set to -2, the symbol is used by a reloc and we must
6416 else if ((h
->def_dynamic
6418 || h
->root
.type
== bfd_link_hash_new
)
6422 else if (finfo
->info
->strip
== strip_all
)
6424 else if (finfo
->info
->strip
== strip_some
6425 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6426 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6428 else if (finfo
->info
->strip_discarded
6429 && (h
->root
.type
== bfd_link_hash_defined
6430 || h
->root
.type
== bfd_link_hash_defweak
)
6431 && elf_discarded_section (h
->root
.u
.def
.section
))
6436 /* If we're stripping it, and it's not a dynamic symbol, there's
6437 nothing else to do unless it is a forced local symbol. */
6440 && !h
->forced_local
)
6444 sym
.st_size
= h
->size
;
6445 sym
.st_other
= h
->other
;
6446 if (h
->forced_local
)
6447 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6448 else if (h
->root
.type
== bfd_link_hash_undefweak
6449 || h
->root
.type
== bfd_link_hash_defweak
)
6450 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6452 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6454 switch (h
->root
.type
)
6457 case bfd_link_hash_new
:
6458 case bfd_link_hash_warning
:
6462 case bfd_link_hash_undefined
:
6463 case bfd_link_hash_undefweak
:
6464 input_sec
= bfd_und_section_ptr
;
6465 sym
.st_shndx
= SHN_UNDEF
;
6468 case bfd_link_hash_defined
:
6469 case bfd_link_hash_defweak
:
6471 input_sec
= h
->root
.u
.def
.section
;
6472 if (input_sec
->output_section
!= NULL
)
6475 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6476 input_sec
->output_section
);
6477 if (sym
.st_shndx
== SHN_BAD
)
6479 (*_bfd_error_handler
)
6480 (_("%B: could not find output section %A for input section %A"),
6481 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6482 eoinfo
->failed
= TRUE
;
6486 /* ELF symbols in relocatable files are section relative,
6487 but in nonrelocatable files they are virtual
6489 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6490 if (! finfo
->info
->relocatable
)
6492 sym
.st_value
+= input_sec
->output_section
->vma
;
6493 if (h
->type
== STT_TLS
)
6495 /* STT_TLS symbols are relative to PT_TLS segment
6497 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6498 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6504 BFD_ASSERT (input_sec
->owner
== NULL
6505 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6506 sym
.st_shndx
= SHN_UNDEF
;
6507 input_sec
= bfd_und_section_ptr
;
6512 case bfd_link_hash_common
:
6513 input_sec
= h
->root
.u
.c
.p
->section
;
6514 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6515 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6518 case bfd_link_hash_indirect
:
6519 /* These symbols are created by symbol versioning. They point
6520 to the decorated version of the name. For example, if the
6521 symbol foo@@GNU_1.2 is the default, which should be used when
6522 foo is used with no version, then we add an indirect symbol
6523 foo which points to foo@@GNU_1.2. We ignore these symbols,
6524 since the indirected symbol is already in the hash table. */
6528 /* Give the processor backend a chance to tweak the symbol value,
6529 and also to finish up anything that needs to be done for this
6530 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6531 forced local syms when non-shared is due to a historical quirk. */
6532 if ((h
->dynindx
!= -1
6534 && ((finfo
->info
->shared
6535 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6536 || h
->root
.type
!= bfd_link_hash_undefweak
))
6537 || !h
->forced_local
)
6538 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6540 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6541 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6543 eoinfo
->failed
= TRUE
;
6548 /* If we are marking the symbol as undefined, and there are no
6549 non-weak references to this symbol from a regular object, then
6550 mark the symbol as weak undefined; if there are non-weak
6551 references, mark the symbol as strong. We can't do this earlier,
6552 because it might not be marked as undefined until the
6553 finish_dynamic_symbol routine gets through with it. */
6554 if (sym
.st_shndx
== SHN_UNDEF
6556 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6557 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6561 if (h
->ref_regular_nonweak
)
6562 bindtype
= STB_GLOBAL
;
6564 bindtype
= STB_WEAK
;
6565 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6568 /* If a non-weak symbol with non-default visibility is not defined
6569 locally, it is a fatal error. */
6570 if (! finfo
->info
->relocatable
6571 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6572 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6573 && h
->root
.type
== bfd_link_hash_undefined
6576 (*_bfd_error_handler
)
6577 (_("%B: %s symbol `%s' isn't defined"),
6579 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6581 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6582 ? "internal" : "hidden",
6583 h
->root
.root
.string
);
6584 eoinfo
->failed
= TRUE
;
6588 /* If this symbol should be put in the .dynsym section, then put it
6589 there now. We already know the symbol index. We also fill in
6590 the entry in the .hash section. */
6591 if (h
->dynindx
!= -1
6592 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6596 size_t hash_entry_size
;
6597 bfd_byte
*bucketpos
;
6601 sym
.st_name
= h
->dynstr_index
;
6602 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6603 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6605 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6606 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6608 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6609 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6610 + (bucket
+ 2) * hash_entry_size
);
6611 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6612 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6613 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6614 ((bfd_byte
*) finfo
->hash_sec
->contents
6615 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6617 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6619 Elf_Internal_Versym iversym
;
6620 Elf_External_Versym
*eversym
;
6622 if (!h
->def_regular
)
6624 if (h
->verinfo
.verdef
== NULL
)
6625 iversym
.vs_vers
= 0;
6627 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6631 if (h
->verinfo
.vertree
== NULL
)
6632 iversym
.vs_vers
= 1;
6634 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6635 if (finfo
->info
->create_default_symver
)
6640 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6642 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6643 eversym
+= h
->dynindx
;
6644 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6648 /* If we're stripping it, then it was just a dynamic symbol, and
6649 there's nothing else to do. */
6650 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6653 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6655 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6657 eoinfo
->failed
= TRUE
;
6664 /* Return TRUE if special handling is done for relocs in SEC against
6665 symbols defined in discarded sections. */
6668 elf_section_ignore_discarded_relocs (asection
*sec
)
6670 const struct elf_backend_data
*bed
;
6672 switch (sec
->sec_info_type
)
6674 case ELF_INFO_TYPE_STABS
:
6675 case ELF_INFO_TYPE_EH_FRAME
:
6681 bed
= get_elf_backend_data (sec
->owner
);
6682 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6683 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6689 /* Return a mask saying how ld should treat relocations in SEC against
6690 symbols defined in discarded sections. If this function returns
6691 COMPLAIN set, ld will issue a warning message. If this function
6692 returns PRETEND set, and the discarded section was link-once and the
6693 same size as the kept link-once section, ld will pretend that the
6694 symbol was actually defined in the kept section. Otherwise ld will
6695 zero the reloc (at least that is the intent, but some cooperation by
6696 the target dependent code is needed, particularly for REL targets). */
6699 _bfd_elf_default_action_discarded (asection
*sec
)
6701 if (sec
->flags
& SEC_DEBUGGING
)
6704 if (strcmp (".eh_frame", sec
->name
) == 0)
6707 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6710 return COMPLAIN
| PRETEND
;
6713 /* Find a match between a section and a member of a section group. */
6716 match_group_member (asection
*sec
, asection
*group
)
6718 asection
*first
= elf_next_in_group (group
);
6719 asection
*s
= first
;
6723 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6733 /* Check if the kept section of a discarded section SEC can be used
6734 to replace it. Return the replacement if it is OK. Otherwise return
6738 _bfd_elf_check_kept_section (asection
*sec
)
6742 kept
= sec
->kept_section
;
6745 if (elf_sec_group (sec
) != NULL
)
6746 kept
= match_group_member (sec
, kept
);
6747 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6753 /* Link an input file into the linker output file. This function
6754 handles all the sections and relocations of the input file at once.
6755 This is so that we only have to read the local symbols once, and
6756 don't have to keep them in memory. */
6759 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6761 bfd_boolean (*relocate_section
)
6762 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6763 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6765 Elf_Internal_Shdr
*symtab_hdr
;
6768 Elf_Internal_Sym
*isymbuf
;
6769 Elf_Internal_Sym
*isym
;
6770 Elf_Internal_Sym
*isymend
;
6772 asection
**ppsection
;
6774 const struct elf_backend_data
*bed
;
6775 bfd_boolean emit_relocs
;
6776 struct elf_link_hash_entry
**sym_hashes
;
6778 output_bfd
= finfo
->output_bfd
;
6779 bed
= get_elf_backend_data (output_bfd
);
6780 relocate_section
= bed
->elf_backend_relocate_section
;
6782 /* If this is a dynamic object, we don't want to do anything here:
6783 we don't want the local symbols, and we don't want the section
6785 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6788 emit_relocs
= (finfo
->info
->relocatable
6789 || finfo
->info
->emitrelocations
);
6791 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6792 if (elf_bad_symtab (input_bfd
))
6794 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6799 locsymcount
= symtab_hdr
->sh_info
;
6800 extsymoff
= symtab_hdr
->sh_info
;
6803 /* Read the local symbols. */
6804 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6805 if (isymbuf
== NULL
&& locsymcount
!= 0)
6807 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6808 finfo
->internal_syms
,
6809 finfo
->external_syms
,
6810 finfo
->locsym_shndx
);
6811 if (isymbuf
== NULL
)
6815 /* Find local symbol sections and adjust values of symbols in
6816 SEC_MERGE sections. Write out those local symbols we know are
6817 going into the output file. */
6818 isymend
= isymbuf
+ locsymcount
;
6819 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6821 isym
++, pindex
++, ppsection
++)
6825 Elf_Internal_Sym osym
;
6829 if (elf_bad_symtab (input_bfd
))
6831 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6838 if (isym
->st_shndx
== SHN_UNDEF
)
6839 isec
= bfd_und_section_ptr
;
6840 else if (isym
->st_shndx
< SHN_LORESERVE
6841 || isym
->st_shndx
> SHN_HIRESERVE
)
6843 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6845 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6846 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6848 _bfd_merged_section_offset (output_bfd
, &isec
,
6849 elf_section_data (isec
)->sec_info
,
6852 else if (isym
->st_shndx
== SHN_ABS
)
6853 isec
= bfd_abs_section_ptr
;
6854 else if (isym
->st_shndx
== SHN_COMMON
)
6855 isec
= bfd_com_section_ptr
;
6864 /* Don't output the first, undefined, symbol. */
6865 if (ppsection
== finfo
->sections
)
6868 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6870 /* We never output section symbols. Instead, we use the
6871 section symbol of the corresponding section in the output
6876 /* If we are stripping all symbols, we don't want to output this
6878 if (finfo
->info
->strip
== strip_all
)
6881 /* If we are discarding all local symbols, we don't want to
6882 output this one. If we are generating a relocatable output
6883 file, then some of the local symbols may be required by
6884 relocs; we output them below as we discover that they are
6886 if (finfo
->info
->discard
== discard_all
)
6889 /* If this symbol is defined in a section which we are
6890 discarding, we don't need to keep it, but note that
6891 linker_mark is only reliable for sections that have contents.
6892 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6893 as well as linker_mark. */
6894 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6896 || (! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6897 || (! finfo
->info
->relocatable
6898 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6901 /* If the section is not in the output BFD's section list, it is not
6903 if (bfd_section_removed_from_list (output_bfd
, isec
->output_section
))
6906 /* Get the name of the symbol. */
6907 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6912 /* See if we are discarding symbols with this name. */
6913 if ((finfo
->info
->strip
== strip_some
6914 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6916 || (((finfo
->info
->discard
== discard_sec_merge
6917 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6918 || finfo
->info
->discard
== discard_l
)
6919 && bfd_is_local_label_name (input_bfd
, name
)))
6922 /* If we get here, we are going to output this symbol. */
6926 /* Adjust the section index for the output file. */
6927 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6928 isec
->output_section
);
6929 if (osym
.st_shndx
== SHN_BAD
)
6932 *pindex
= bfd_get_symcount (output_bfd
);
6934 /* ELF symbols in relocatable files are section relative, but
6935 in executable files they are virtual addresses. Note that
6936 this code assumes that all ELF sections have an associated
6937 BFD section with a reasonable value for output_offset; below
6938 we assume that they also have a reasonable value for
6939 output_section. Any special sections must be set up to meet
6940 these requirements. */
6941 osym
.st_value
+= isec
->output_offset
;
6942 if (! finfo
->info
->relocatable
)
6944 osym
.st_value
+= isec
->output_section
->vma
;
6945 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6947 /* STT_TLS symbols are relative to PT_TLS segment base. */
6948 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6949 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6953 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6957 /* Relocate the contents of each section. */
6958 sym_hashes
= elf_sym_hashes (input_bfd
);
6959 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6963 if (! o
->linker_mark
)
6965 /* This section was omitted from the link. */
6969 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6970 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6973 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6975 /* Section was created by _bfd_elf_link_create_dynamic_sections
6980 /* Get the contents of the section. They have been cached by a
6981 relaxation routine. Note that o is a section in an input
6982 file, so the contents field will not have been set by any of
6983 the routines which work on output files. */
6984 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6985 contents
= elf_section_data (o
)->this_hdr
.contents
;
6988 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6990 contents
= finfo
->contents
;
6991 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6995 if ((o
->flags
& SEC_RELOC
) != 0)
6997 Elf_Internal_Rela
*internal_relocs
;
6998 bfd_vma r_type_mask
;
7001 /* Get the swapped relocs. */
7003 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7004 finfo
->internal_relocs
, FALSE
);
7005 if (internal_relocs
== NULL
7006 && o
->reloc_count
> 0)
7009 if (bed
->s
->arch_size
== 32)
7016 r_type_mask
= 0xffffffff;
7020 /* Run through the relocs looking for any against symbols
7021 from discarded sections and section symbols from
7022 removed link-once sections. Complain about relocs
7023 against discarded sections. Zero relocs against removed
7024 link-once sections. Preserve debug information as much
7026 if (!elf_section_ignore_discarded_relocs (o
))
7028 Elf_Internal_Rela
*rel
, *relend
;
7029 unsigned int action
= (*bed
->action_discarded
) (o
);
7031 rel
= internal_relocs
;
7032 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7033 for ( ; rel
< relend
; rel
++)
7035 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7036 asection
**ps
, *sec
;
7037 struct elf_link_hash_entry
*h
= NULL
;
7038 const char *sym_name
;
7040 if (r_symndx
== STN_UNDEF
)
7043 if (r_symndx
>= locsymcount
7044 || (elf_bad_symtab (input_bfd
)
7045 && finfo
->sections
[r_symndx
] == NULL
))
7047 h
= sym_hashes
[r_symndx
- extsymoff
];
7049 /* Badly formatted input files can contain relocs that
7050 reference non-existant symbols. Check here so that
7051 we do not seg fault. */
7056 sprintf_vma (buffer
, rel
->r_info
);
7057 (*_bfd_error_handler
)
7058 (_("error: %B contains a reloc (0x%s) for section %A "
7059 "that references a non-existent global symbol"),
7060 input_bfd
, o
, buffer
);
7061 bfd_set_error (bfd_error_bad_value
);
7065 while (h
->root
.type
== bfd_link_hash_indirect
7066 || h
->root
.type
== bfd_link_hash_warning
)
7067 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7069 if (h
->root
.type
!= bfd_link_hash_defined
7070 && h
->root
.type
!= bfd_link_hash_defweak
)
7073 ps
= &h
->root
.u
.def
.section
;
7074 sym_name
= h
->root
.root
.string
;
7078 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7079 ps
= &finfo
->sections
[r_symndx
];
7080 sym_name
= bfd_elf_sym_name (input_bfd
,
7085 /* Complain if the definition comes from a
7086 discarded section. */
7087 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7089 BFD_ASSERT (r_symndx
!= 0);
7090 if (action
& COMPLAIN
)
7091 (*finfo
->info
->callbacks
->einfo
)
7092 (_("%X`%s' referenced in section `%A' of %B: "
7093 "defined in discarded section `%A' of %B\n"),
7094 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7096 /* Try to do the best we can to support buggy old
7097 versions of gcc. If we've warned, or this is
7098 debugging info, pretend that the symbol is
7099 really defined in the kept linkonce section.
7100 FIXME: This is quite broken. Modifying the
7101 symbol here means we will be changing all later
7102 uses of the symbol, not just in this section.
7103 The only thing that makes this half reasonable
7104 is that we warn in non-debug sections, and
7105 debug sections tend to come after other
7107 if (action
& PRETEND
)
7111 kept
= _bfd_elf_check_kept_section (sec
);
7119 /* Remove the symbol reference from the reloc, but
7120 don't kill the reloc completely. This is so that
7121 a zero value will be written into the section,
7122 which may have non-zero contents put there by the
7123 assembler. Zero in things like an eh_frame fde
7124 pc_begin allows stack unwinders to recognize the
7126 rel
->r_info
&= r_type_mask
;
7132 /* Relocate the section by invoking a back end routine.
7134 The back end routine is responsible for adjusting the
7135 section contents as necessary, and (if using Rela relocs
7136 and generating a relocatable output file) adjusting the
7137 reloc addend as necessary.
7139 The back end routine does not have to worry about setting
7140 the reloc address or the reloc symbol index.
7142 The back end routine is given a pointer to the swapped in
7143 internal symbols, and can access the hash table entries
7144 for the external symbols via elf_sym_hashes (input_bfd).
7146 When generating relocatable output, the back end routine
7147 must handle STB_LOCAL/STT_SECTION symbols specially. The
7148 output symbol is going to be a section symbol
7149 corresponding to the output section, which will require
7150 the addend to be adjusted. */
7152 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7153 input_bfd
, o
, contents
,
7161 Elf_Internal_Rela
*irela
;
7162 Elf_Internal_Rela
*irelaend
;
7163 bfd_vma last_offset
;
7164 struct elf_link_hash_entry
**rel_hash
;
7165 struct elf_link_hash_entry
**rel_hash_list
;
7166 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7167 unsigned int next_erel
;
7168 bfd_boolean rela_normal
;
7170 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7171 rela_normal
= (bed
->rela_normal
7172 && (input_rel_hdr
->sh_entsize
7173 == bed
->s
->sizeof_rela
));
7175 /* Adjust the reloc addresses and symbol indices. */
7177 irela
= internal_relocs
;
7178 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7179 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7180 + elf_section_data (o
->output_section
)->rel_count
7181 + elf_section_data (o
->output_section
)->rel_count2
);
7182 rel_hash_list
= rel_hash
;
7183 last_offset
= o
->output_offset
;
7184 if (!finfo
->info
->relocatable
)
7185 last_offset
+= o
->output_section
->vma
;
7186 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7188 unsigned long r_symndx
;
7190 Elf_Internal_Sym sym
;
7192 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7198 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7201 if (irela
->r_offset
>= (bfd_vma
) -2)
7203 /* This is a reloc for a deleted entry or somesuch.
7204 Turn it into an R_*_NONE reloc, at the same
7205 offset as the last reloc. elf_eh_frame.c and
7206 elf_bfd_discard_info rely on reloc offsets
7208 irela
->r_offset
= last_offset
;
7210 irela
->r_addend
= 0;
7214 irela
->r_offset
+= o
->output_offset
;
7216 /* Relocs in an executable have to be virtual addresses. */
7217 if (!finfo
->info
->relocatable
)
7218 irela
->r_offset
+= o
->output_section
->vma
;
7220 last_offset
= irela
->r_offset
;
7222 r_symndx
= irela
->r_info
>> r_sym_shift
;
7223 if (r_symndx
== STN_UNDEF
)
7226 if (r_symndx
>= locsymcount
7227 || (elf_bad_symtab (input_bfd
)
7228 && finfo
->sections
[r_symndx
] == NULL
))
7230 struct elf_link_hash_entry
*rh
;
7233 /* This is a reloc against a global symbol. We
7234 have not yet output all the local symbols, so
7235 we do not know the symbol index of any global
7236 symbol. We set the rel_hash entry for this
7237 reloc to point to the global hash table entry
7238 for this symbol. The symbol index is then
7239 set at the end of bfd_elf_final_link. */
7240 indx
= r_symndx
- extsymoff
;
7241 rh
= elf_sym_hashes (input_bfd
)[indx
];
7242 while (rh
->root
.type
== bfd_link_hash_indirect
7243 || rh
->root
.type
== bfd_link_hash_warning
)
7244 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7246 /* Setting the index to -2 tells
7247 elf_link_output_extsym that this symbol is
7249 BFD_ASSERT (rh
->indx
< 0);
7257 /* This is a reloc against a local symbol. */
7260 sym
= isymbuf
[r_symndx
];
7261 sec
= finfo
->sections
[r_symndx
];
7262 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7264 /* I suppose the backend ought to fill in the
7265 section of any STT_SECTION symbol against a
7266 processor specific section. */
7268 if (bfd_is_abs_section (sec
))
7270 else if (sec
== NULL
|| sec
->owner
== NULL
)
7272 bfd_set_error (bfd_error_bad_value
);
7277 asection
*osec
= sec
->output_section
;
7279 /* If we have discarded a section, the output
7280 section will be the absolute section. In
7281 case of discarded link-once and discarded
7282 SEC_MERGE sections, use the kept section. */
7283 if (bfd_is_abs_section (osec
)
7284 && sec
->kept_section
!= NULL
7285 && sec
->kept_section
->output_section
!= NULL
)
7287 osec
= sec
->kept_section
->output_section
;
7288 irela
->r_addend
-= osec
->vma
;
7291 if (!bfd_is_abs_section (osec
))
7293 r_symndx
= osec
->target_index
;
7294 BFD_ASSERT (r_symndx
!= 0);
7298 /* Adjust the addend according to where the
7299 section winds up in the output section. */
7301 irela
->r_addend
+= sec
->output_offset
;
7305 if (finfo
->indices
[r_symndx
] == -1)
7307 unsigned long shlink
;
7311 if (finfo
->info
->strip
== strip_all
)
7313 /* You can't do ld -r -s. */
7314 bfd_set_error (bfd_error_invalid_operation
);
7318 /* This symbol was skipped earlier, but
7319 since it is needed by a reloc, we
7320 must output it now. */
7321 shlink
= symtab_hdr
->sh_link
;
7322 name
= (bfd_elf_string_from_elf_section
7323 (input_bfd
, shlink
, sym
.st_name
));
7327 osec
= sec
->output_section
;
7329 _bfd_elf_section_from_bfd_section (output_bfd
,
7331 if (sym
.st_shndx
== SHN_BAD
)
7334 sym
.st_value
+= sec
->output_offset
;
7335 if (! finfo
->info
->relocatable
)
7337 sym
.st_value
+= osec
->vma
;
7338 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7340 /* STT_TLS symbols are relative to PT_TLS
7342 BFD_ASSERT (elf_hash_table (finfo
->info
)
7344 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7349 finfo
->indices
[r_symndx
]
7350 = bfd_get_symcount (output_bfd
);
7352 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7357 r_symndx
= finfo
->indices
[r_symndx
];
7360 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7361 | (irela
->r_info
& r_type_mask
));
7364 /* Swap out the relocs. */
7365 if (input_rel_hdr
->sh_size
!= 0
7366 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7372 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7373 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7375 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7376 * bed
->s
->int_rels_per_ext_rel
);
7377 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7378 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7387 /* Write out the modified section contents. */
7388 if (bed
->elf_backend_write_section
7389 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7391 /* Section written out. */
7393 else switch (o
->sec_info_type
)
7395 case ELF_INFO_TYPE_STABS
:
7396 if (! (_bfd_write_section_stabs
7398 &elf_hash_table (finfo
->info
)->stab_info
,
7399 o
, &elf_section_data (o
)->sec_info
, contents
)))
7402 case ELF_INFO_TYPE_MERGE
:
7403 if (! _bfd_write_merged_section (output_bfd
, o
,
7404 elf_section_data (o
)->sec_info
))
7407 case ELF_INFO_TYPE_EH_FRAME
:
7409 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7416 if (! (o
->flags
& SEC_EXCLUDE
)
7417 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7419 (file_ptr
) o
->output_offset
,
7430 /* Generate a reloc when linking an ELF file. This is a reloc
7431 requested by the linker, and does come from any input file. This
7432 is used to build constructor and destructor tables when linking
7436 elf_reloc_link_order (bfd
*output_bfd
,
7437 struct bfd_link_info
*info
,
7438 asection
*output_section
,
7439 struct bfd_link_order
*link_order
)
7441 reloc_howto_type
*howto
;
7445 struct elf_link_hash_entry
**rel_hash_ptr
;
7446 Elf_Internal_Shdr
*rel_hdr
;
7447 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7448 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7452 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7455 bfd_set_error (bfd_error_bad_value
);
7459 addend
= link_order
->u
.reloc
.p
->addend
;
7461 /* Figure out the symbol index. */
7462 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7463 + elf_section_data (output_section
)->rel_count
7464 + elf_section_data (output_section
)->rel_count2
);
7465 if (link_order
->type
== bfd_section_reloc_link_order
)
7467 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7468 BFD_ASSERT (indx
!= 0);
7469 *rel_hash_ptr
= NULL
;
7473 struct elf_link_hash_entry
*h
;
7475 /* Treat a reloc against a defined symbol as though it were
7476 actually against the section. */
7477 h
= ((struct elf_link_hash_entry
*)
7478 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7479 link_order
->u
.reloc
.p
->u
.name
,
7480 FALSE
, FALSE
, TRUE
));
7482 && (h
->root
.type
== bfd_link_hash_defined
7483 || h
->root
.type
== bfd_link_hash_defweak
))
7487 section
= h
->root
.u
.def
.section
;
7488 indx
= section
->output_section
->target_index
;
7489 *rel_hash_ptr
= NULL
;
7490 /* It seems that we ought to add the symbol value to the
7491 addend here, but in practice it has already been added
7492 because it was passed to constructor_callback. */
7493 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7497 /* Setting the index to -2 tells elf_link_output_extsym that
7498 this symbol is used by a reloc. */
7505 if (! ((*info
->callbacks
->unattached_reloc
)
7506 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7512 /* If this is an inplace reloc, we must write the addend into the
7514 if (howto
->partial_inplace
&& addend
!= 0)
7517 bfd_reloc_status_type rstat
;
7520 const char *sym_name
;
7522 size
= bfd_get_reloc_size (howto
);
7523 buf
= bfd_zmalloc (size
);
7526 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7533 case bfd_reloc_outofrange
:
7536 case bfd_reloc_overflow
:
7537 if (link_order
->type
== bfd_section_reloc_link_order
)
7538 sym_name
= bfd_section_name (output_bfd
,
7539 link_order
->u
.reloc
.p
->u
.section
);
7541 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7542 if (! ((*info
->callbacks
->reloc_overflow
)
7543 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7544 NULL
, (bfd_vma
) 0)))
7551 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7552 link_order
->offset
, size
);
7558 /* The address of a reloc is relative to the section in a
7559 relocatable file, and is a virtual address in an executable
7561 offset
= link_order
->offset
;
7562 if (! info
->relocatable
)
7563 offset
+= output_section
->vma
;
7565 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7567 irel
[i
].r_offset
= offset
;
7569 irel
[i
].r_addend
= 0;
7571 if (bed
->s
->arch_size
== 32)
7572 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7574 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7576 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7577 erel
= rel_hdr
->contents
;
7578 if (rel_hdr
->sh_type
== SHT_REL
)
7580 erel
+= (elf_section_data (output_section
)->rel_count
7581 * bed
->s
->sizeof_rel
);
7582 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7586 irel
[0].r_addend
= addend
;
7587 erel
+= (elf_section_data (output_section
)->rel_count
7588 * bed
->s
->sizeof_rela
);
7589 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7592 ++elf_section_data (output_section
)->rel_count
;
7598 /* Get the output vma of the section pointed to by the sh_link field. */
7601 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7603 Elf_Internal_Shdr
**elf_shdrp
;
7607 s
= p
->u
.indirect
.section
;
7608 elf_shdrp
= elf_elfsections (s
->owner
);
7609 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7610 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7612 The Intel C compiler generates SHT_IA_64_UNWIND with
7613 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7614 sh_info fields. Hence we could get the situation
7615 where elfsec is 0. */
7618 const struct elf_backend_data
*bed
7619 = get_elf_backend_data (s
->owner
);
7620 if (bed
->link_order_error_handler
)
7621 bed
->link_order_error_handler
7622 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7627 s
= elf_shdrp
[elfsec
]->bfd_section
;
7628 return s
->output_section
->vma
+ s
->output_offset
;
7633 /* Compare two sections based on the locations of the sections they are
7634 linked to. Used by elf_fixup_link_order. */
7637 compare_link_order (const void * a
, const void * b
)
7642 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7643 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7650 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7651 order as their linked sections. Returns false if this could not be done
7652 because an output section includes both ordered and unordered
7653 sections. Ideally we'd do this in the linker proper. */
7656 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7661 struct bfd_link_order
*p
;
7663 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7665 struct bfd_link_order
**sections
;
7671 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7673 if (p
->type
== bfd_indirect_link_order
7674 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7675 == bfd_target_elf_flavour
)
7676 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7678 s
= p
->u
.indirect
.section
;
7679 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7681 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7690 if (!seen_linkorder
)
7693 if (seen_other
&& seen_linkorder
)
7695 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7697 bfd_set_error (bfd_error_bad_value
);
7701 sections
= (struct bfd_link_order
**)
7702 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7705 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7707 sections
[seen_linkorder
++] = p
;
7709 /* Sort the input sections in the order of their linked section. */
7710 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7711 compare_link_order
);
7713 /* Change the offsets of the sections. */
7715 for (n
= 0; n
< seen_linkorder
; n
++)
7717 s
= sections
[n
]->u
.indirect
.section
;
7718 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7719 s
->output_offset
= offset
;
7720 sections
[n
]->offset
= offset
;
7721 offset
+= sections
[n
]->size
;
7728 /* Do the final step of an ELF link. */
7731 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7733 bfd_boolean dynamic
;
7734 bfd_boolean emit_relocs
;
7736 struct elf_final_link_info finfo
;
7737 register asection
*o
;
7738 register struct bfd_link_order
*p
;
7740 bfd_size_type max_contents_size
;
7741 bfd_size_type max_external_reloc_size
;
7742 bfd_size_type max_internal_reloc_count
;
7743 bfd_size_type max_sym_count
;
7744 bfd_size_type max_sym_shndx_count
;
7746 Elf_Internal_Sym elfsym
;
7748 Elf_Internal_Shdr
*symtab_hdr
;
7749 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7750 Elf_Internal_Shdr
*symstrtab_hdr
;
7751 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7752 struct elf_outext_info eoinfo
;
7754 size_t relativecount
= 0;
7755 asection
*reldyn
= 0;
7758 if (! is_elf_hash_table (info
->hash
))
7762 abfd
->flags
|= DYNAMIC
;
7764 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7765 dynobj
= elf_hash_table (info
)->dynobj
;
7767 emit_relocs
= (info
->relocatable
7768 || info
->emitrelocations
7769 || bed
->elf_backend_emit_relocs
);
7772 finfo
.output_bfd
= abfd
;
7773 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7774 if (finfo
.symstrtab
== NULL
)
7779 finfo
.dynsym_sec
= NULL
;
7780 finfo
.hash_sec
= NULL
;
7781 finfo
.symver_sec
= NULL
;
7785 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7786 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7787 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7788 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7789 /* Note that it is OK if symver_sec is NULL. */
7792 finfo
.contents
= NULL
;
7793 finfo
.external_relocs
= NULL
;
7794 finfo
.internal_relocs
= NULL
;
7795 finfo
.external_syms
= NULL
;
7796 finfo
.locsym_shndx
= NULL
;
7797 finfo
.internal_syms
= NULL
;
7798 finfo
.indices
= NULL
;
7799 finfo
.sections
= NULL
;
7800 finfo
.symbuf
= NULL
;
7801 finfo
.symshndxbuf
= NULL
;
7802 finfo
.symbuf_count
= 0;
7803 finfo
.shndxbuf_size
= 0;
7805 /* Count up the number of relocations we will output for each output
7806 section, so that we know the sizes of the reloc sections. We
7807 also figure out some maximum sizes. */
7808 max_contents_size
= 0;
7809 max_external_reloc_size
= 0;
7810 max_internal_reloc_count
= 0;
7812 max_sym_shndx_count
= 0;
7814 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7816 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7819 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7821 unsigned int reloc_count
= 0;
7822 struct bfd_elf_section_data
*esdi
= NULL
;
7823 unsigned int *rel_count1
;
7825 if (p
->type
== bfd_section_reloc_link_order
7826 || p
->type
== bfd_symbol_reloc_link_order
)
7828 else if (p
->type
== bfd_indirect_link_order
)
7832 sec
= p
->u
.indirect
.section
;
7833 esdi
= elf_section_data (sec
);
7835 /* Mark all sections which are to be included in the
7836 link. This will normally be every section. We need
7837 to do this so that we can identify any sections which
7838 the linker has decided to not include. */
7839 sec
->linker_mark
= TRUE
;
7841 if (sec
->flags
& SEC_MERGE
)
7844 if (info
->relocatable
|| info
->emitrelocations
)
7845 reloc_count
= sec
->reloc_count
;
7846 else if (bed
->elf_backend_count_relocs
)
7848 Elf_Internal_Rela
* relocs
;
7850 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7853 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7855 if (elf_section_data (o
)->relocs
!= relocs
)
7859 if (sec
->rawsize
> max_contents_size
)
7860 max_contents_size
= sec
->rawsize
;
7861 if (sec
->size
> max_contents_size
)
7862 max_contents_size
= sec
->size
;
7864 /* We are interested in just local symbols, not all
7866 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7867 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7871 if (elf_bad_symtab (sec
->owner
))
7872 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7873 / bed
->s
->sizeof_sym
);
7875 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7877 if (sym_count
> max_sym_count
)
7878 max_sym_count
= sym_count
;
7880 if (sym_count
> max_sym_shndx_count
7881 && elf_symtab_shndx (sec
->owner
) != 0)
7882 max_sym_shndx_count
= sym_count
;
7884 if ((sec
->flags
& SEC_RELOC
) != 0)
7888 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7889 if (ext_size
> max_external_reloc_size
)
7890 max_external_reloc_size
= ext_size
;
7891 if (sec
->reloc_count
> max_internal_reloc_count
)
7892 max_internal_reloc_count
= sec
->reloc_count
;
7897 if (reloc_count
== 0)
7900 o
->reloc_count
+= reloc_count
;
7902 /* MIPS may have a mix of REL and RELA relocs on sections.
7903 To support this curious ABI we keep reloc counts in
7904 elf_section_data too. We must be careful to add the
7905 relocations from the input section to the right output
7906 count. FIXME: Get rid of one count. We have
7907 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7908 rel_count1
= &esdo
->rel_count
;
7911 bfd_boolean same_size
;
7912 bfd_size_type entsize1
;
7914 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7915 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7916 || entsize1
== bed
->s
->sizeof_rela
);
7917 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7920 rel_count1
= &esdo
->rel_count2
;
7922 if (esdi
->rel_hdr2
!= NULL
)
7924 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7925 unsigned int alt_count
;
7926 unsigned int *rel_count2
;
7928 BFD_ASSERT (entsize2
!= entsize1
7929 && (entsize2
== bed
->s
->sizeof_rel
7930 || entsize2
== bed
->s
->sizeof_rela
));
7932 rel_count2
= &esdo
->rel_count2
;
7934 rel_count2
= &esdo
->rel_count
;
7936 /* The following is probably too simplistic if the
7937 backend counts output relocs unusually. */
7938 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7939 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7940 *rel_count2
+= alt_count
;
7941 reloc_count
-= alt_count
;
7944 *rel_count1
+= reloc_count
;
7947 if (o
->reloc_count
> 0)
7948 o
->flags
|= SEC_RELOC
;
7951 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7952 set it (this is probably a bug) and if it is set
7953 assign_section_numbers will create a reloc section. */
7954 o
->flags
&=~ SEC_RELOC
;
7957 /* If the SEC_ALLOC flag is not set, force the section VMA to
7958 zero. This is done in elf_fake_sections as well, but forcing
7959 the VMA to 0 here will ensure that relocs against these
7960 sections are handled correctly. */
7961 if ((o
->flags
& SEC_ALLOC
) == 0
7962 && ! o
->user_set_vma
)
7966 if (! info
->relocatable
&& merged
)
7967 elf_link_hash_traverse (elf_hash_table (info
),
7968 _bfd_elf_link_sec_merge_syms
, abfd
);
7970 /* Figure out the file positions for everything but the symbol table
7971 and the relocs. We set symcount to force assign_section_numbers
7972 to create a symbol table. */
7973 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7974 BFD_ASSERT (! abfd
->output_has_begun
);
7975 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7978 /* Set sizes, and assign file positions for reloc sections. */
7979 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7981 if ((o
->flags
& SEC_RELOC
) != 0)
7983 if (!(_bfd_elf_link_size_reloc_section
7984 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7987 if (elf_section_data (o
)->rel_hdr2
7988 && !(_bfd_elf_link_size_reloc_section
7989 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7993 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7994 to count upwards while actually outputting the relocations. */
7995 elf_section_data (o
)->rel_count
= 0;
7996 elf_section_data (o
)->rel_count2
= 0;
7999 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8001 /* We have now assigned file positions for all the sections except
8002 .symtab and .strtab. We start the .symtab section at the current
8003 file position, and write directly to it. We build the .strtab
8004 section in memory. */
8005 bfd_get_symcount (abfd
) = 0;
8006 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8007 /* sh_name is set in prep_headers. */
8008 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8009 /* sh_flags, sh_addr and sh_size all start off zero. */
8010 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8011 /* sh_link is set in assign_section_numbers. */
8012 /* sh_info is set below. */
8013 /* sh_offset is set just below. */
8014 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8016 off
= elf_tdata (abfd
)->next_file_pos
;
8017 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8019 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8020 incorrect. We do not yet know the size of the .symtab section.
8021 We correct next_file_pos below, after we do know the size. */
8023 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8024 continuously seeking to the right position in the file. */
8025 if (! info
->keep_memory
|| max_sym_count
< 20)
8026 finfo
.symbuf_size
= 20;
8028 finfo
.symbuf_size
= max_sym_count
;
8029 amt
= finfo
.symbuf_size
;
8030 amt
*= bed
->s
->sizeof_sym
;
8031 finfo
.symbuf
= bfd_malloc (amt
);
8032 if (finfo
.symbuf
== NULL
)
8034 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8036 /* Wild guess at number of output symbols. realloc'd as needed. */
8037 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8038 finfo
.shndxbuf_size
= amt
;
8039 amt
*= sizeof (Elf_External_Sym_Shndx
);
8040 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8041 if (finfo
.symshndxbuf
== NULL
)
8045 /* Start writing out the symbol table. The first symbol is always a
8047 if (info
->strip
!= strip_all
8050 elfsym
.st_value
= 0;
8053 elfsym
.st_other
= 0;
8054 elfsym
.st_shndx
= SHN_UNDEF
;
8055 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8060 /* Output a symbol for each section. We output these even if we are
8061 discarding local symbols, since they are used for relocs. These
8062 symbols have no names. We store the index of each one in the
8063 index field of the section, so that we can find it again when
8064 outputting relocs. */
8065 if (info
->strip
!= strip_all
8069 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8070 elfsym
.st_other
= 0;
8071 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8073 o
= bfd_section_from_elf_index (abfd
, i
);
8075 o
->target_index
= bfd_get_symcount (abfd
);
8076 elfsym
.st_shndx
= i
;
8077 if (info
->relocatable
|| o
== NULL
)
8078 elfsym
.st_value
= 0;
8080 elfsym
.st_value
= o
->vma
;
8081 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8083 if (i
== SHN_LORESERVE
- 1)
8084 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8088 /* Allocate some memory to hold information read in from the input
8090 if (max_contents_size
!= 0)
8092 finfo
.contents
= bfd_malloc (max_contents_size
);
8093 if (finfo
.contents
== NULL
)
8097 if (max_external_reloc_size
!= 0)
8099 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8100 if (finfo
.external_relocs
== NULL
)
8104 if (max_internal_reloc_count
!= 0)
8106 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8107 amt
*= sizeof (Elf_Internal_Rela
);
8108 finfo
.internal_relocs
= bfd_malloc (amt
);
8109 if (finfo
.internal_relocs
== NULL
)
8113 if (max_sym_count
!= 0)
8115 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8116 finfo
.external_syms
= bfd_malloc (amt
);
8117 if (finfo
.external_syms
== NULL
)
8120 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8121 finfo
.internal_syms
= bfd_malloc (amt
);
8122 if (finfo
.internal_syms
== NULL
)
8125 amt
= max_sym_count
* sizeof (long);
8126 finfo
.indices
= bfd_malloc (amt
);
8127 if (finfo
.indices
== NULL
)
8130 amt
= max_sym_count
* sizeof (asection
*);
8131 finfo
.sections
= bfd_malloc (amt
);
8132 if (finfo
.sections
== NULL
)
8136 if (max_sym_shndx_count
!= 0)
8138 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8139 finfo
.locsym_shndx
= bfd_malloc (amt
);
8140 if (finfo
.locsym_shndx
== NULL
)
8144 if (elf_hash_table (info
)->tls_sec
)
8146 bfd_vma base
, end
= 0;
8149 for (sec
= elf_hash_table (info
)->tls_sec
;
8150 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8153 bfd_vma size
= sec
->size
;
8155 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8157 struct bfd_link_order
*o
;
8159 for (o
= sec
->map_head
.link_order
; o
!= NULL
; o
= o
->next
)
8160 if (size
< o
->offset
+ o
->size
)
8161 size
= o
->offset
+ o
->size
;
8163 end
= sec
->vma
+ size
;
8165 base
= elf_hash_table (info
)->tls_sec
->vma
;
8166 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8167 elf_hash_table (info
)->tls_size
= end
- base
;
8170 /* Reorder SHF_LINK_ORDER sections. */
8171 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8173 if (!elf_fixup_link_order (abfd
, o
))
8177 /* Since ELF permits relocations to be against local symbols, we
8178 must have the local symbols available when we do the relocations.
8179 Since we would rather only read the local symbols once, and we
8180 would rather not keep them in memory, we handle all the
8181 relocations for a single input file at the same time.
8183 Unfortunately, there is no way to know the total number of local
8184 symbols until we have seen all of them, and the local symbol
8185 indices precede the global symbol indices. This means that when
8186 we are generating relocatable output, and we see a reloc against
8187 a global symbol, we can not know the symbol index until we have
8188 finished examining all the local symbols to see which ones we are
8189 going to output. To deal with this, we keep the relocations in
8190 memory, and don't output them until the end of the link. This is
8191 an unfortunate waste of memory, but I don't see a good way around
8192 it. Fortunately, it only happens when performing a relocatable
8193 link, which is not the common case. FIXME: If keep_memory is set
8194 we could write the relocs out and then read them again; I don't
8195 know how bad the memory loss will be. */
8197 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8198 sub
->output_has_begun
= FALSE
;
8199 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8201 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8203 if (p
->type
== bfd_indirect_link_order
8204 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8205 == bfd_target_elf_flavour
)
8206 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8208 if (! sub
->output_has_begun
)
8210 if (! elf_link_input_bfd (&finfo
, sub
))
8212 sub
->output_has_begun
= TRUE
;
8215 else if (p
->type
== bfd_section_reloc_link_order
8216 || p
->type
== bfd_symbol_reloc_link_order
)
8218 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8223 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8229 /* Output any global symbols that got converted to local in a
8230 version script or due to symbol visibility. We do this in a
8231 separate step since ELF requires all local symbols to appear
8232 prior to any global symbols. FIXME: We should only do this if
8233 some global symbols were, in fact, converted to become local.
8234 FIXME: Will this work correctly with the Irix 5 linker? */
8235 eoinfo
.failed
= FALSE
;
8236 eoinfo
.finfo
= &finfo
;
8237 eoinfo
.localsyms
= TRUE
;
8238 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8243 /* That wrote out all the local symbols. Finish up the symbol table
8244 with the global symbols. Even if we want to strip everything we
8245 can, we still need to deal with those global symbols that got
8246 converted to local in a version script. */
8248 /* The sh_info field records the index of the first non local symbol. */
8249 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8252 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8254 Elf_Internal_Sym sym
;
8255 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8256 long last_local
= 0;
8258 /* Write out the section symbols for the output sections. */
8259 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8265 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8268 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8274 dynindx
= elf_section_data (s
)->dynindx
;
8277 indx
= elf_section_data (s
)->this_idx
;
8278 BFD_ASSERT (indx
> 0);
8279 sym
.st_shndx
= indx
;
8280 sym
.st_value
= s
->vma
;
8281 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8282 if (last_local
< dynindx
)
8283 last_local
= dynindx
;
8284 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8288 /* Write out the local dynsyms. */
8289 if (elf_hash_table (info
)->dynlocal
)
8291 struct elf_link_local_dynamic_entry
*e
;
8292 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8297 sym
.st_size
= e
->isym
.st_size
;
8298 sym
.st_other
= e
->isym
.st_other
;
8300 /* Copy the internal symbol as is.
8301 Note that we saved a word of storage and overwrote
8302 the original st_name with the dynstr_index. */
8305 if (e
->isym
.st_shndx
!= SHN_UNDEF
8306 && (e
->isym
.st_shndx
< SHN_LORESERVE
8307 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8309 s
= bfd_section_from_elf_index (e
->input_bfd
,
8313 elf_section_data (s
->output_section
)->this_idx
;
8314 sym
.st_value
= (s
->output_section
->vma
8316 + e
->isym
.st_value
);
8319 if (last_local
< e
->dynindx
)
8320 last_local
= e
->dynindx
;
8322 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8323 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8327 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8331 /* We get the global symbols from the hash table. */
8332 eoinfo
.failed
= FALSE
;
8333 eoinfo
.localsyms
= FALSE
;
8334 eoinfo
.finfo
= &finfo
;
8335 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8340 /* If backend needs to output some symbols not present in the hash
8341 table, do it now. */
8342 if (bed
->elf_backend_output_arch_syms
)
8344 typedef bfd_boolean (*out_sym_func
)
8345 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8346 struct elf_link_hash_entry
*);
8348 if (! ((*bed
->elf_backend_output_arch_syms
)
8349 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8353 /* Flush all symbols to the file. */
8354 if (! elf_link_flush_output_syms (&finfo
, bed
))
8357 /* Now we know the size of the symtab section. */
8358 off
+= symtab_hdr
->sh_size
;
8360 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8361 if (symtab_shndx_hdr
->sh_name
!= 0)
8363 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8364 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8365 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8366 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8367 symtab_shndx_hdr
->sh_size
= amt
;
8369 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8372 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8373 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8378 /* Finish up and write out the symbol string table (.strtab)
8380 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8381 /* sh_name was set in prep_headers. */
8382 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8383 symstrtab_hdr
->sh_flags
= 0;
8384 symstrtab_hdr
->sh_addr
= 0;
8385 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8386 symstrtab_hdr
->sh_entsize
= 0;
8387 symstrtab_hdr
->sh_link
= 0;
8388 symstrtab_hdr
->sh_info
= 0;
8389 /* sh_offset is set just below. */
8390 symstrtab_hdr
->sh_addralign
= 1;
8392 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8393 elf_tdata (abfd
)->next_file_pos
= off
;
8395 if (bfd_get_symcount (abfd
) > 0)
8397 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8398 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8402 /* Adjust the relocs to have the correct symbol indices. */
8403 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8405 if ((o
->flags
& SEC_RELOC
) == 0)
8408 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8409 elf_section_data (o
)->rel_count
,
8410 elf_section_data (o
)->rel_hashes
);
8411 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8412 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8413 elf_section_data (o
)->rel_count2
,
8414 (elf_section_data (o
)->rel_hashes
8415 + elf_section_data (o
)->rel_count
));
8417 /* Set the reloc_count field to 0 to prevent write_relocs from
8418 trying to swap the relocs out itself. */
8422 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8423 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8425 /* If we are linking against a dynamic object, or generating a
8426 shared library, finish up the dynamic linking information. */
8429 bfd_byte
*dyncon
, *dynconend
;
8431 /* Fix up .dynamic entries. */
8432 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8433 BFD_ASSERT (o
!= NULL
);
8435 dyncon
= o
->contents
;
8436 dynconend
= o
->contents
+ o
->size
;
8437 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8439 Elf_Internal_Dyn dyn
;
8443 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8450 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8452 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8454 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8455 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8458 dyn
.d_un
.d_val
= relativecount
;
8465 name
= info
->init_function
;
8468 name
= info
->fini_function
;
8471 struct elf_link_hash_entry
*h
;
8473 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8474 FALSE
, FALSE
, TRUE
);
8476 && (h
->root
.type
== bfd_link_hash_defined
8477 || h
->root
.type
== bfd_link_hash_defweak
))
8479 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8480 o
= h
->root
.u
.def
.section
;
8481 if (o
->output_section
!= NULL
)
8482 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8483 + o
->output_offset
);
8486 /* The symbol is imported from another shared
8487 library and does not apply to this one. */
8495 case DT_PREINIT_ARRAYSZ
:
8496 name
= ".preinit_array";
8498 case DT_INIT_ARRAYSZ
:
8499 name
= ".init_array";
8501 case DT_FINI_ARRAYSZ
:
8502 name
= ".fini_array";
8504 o
= bfd_get_section_by_name (abfd
, name
);
8507 (*_bfd_error_handler
)
8508 (_("%B: could not find output section %s"), abfd
, name
);
8512 (*_bfd_error_handler
)
8513 (_("warning: %s section has zero size"), name
);
8514 dyn
.d_un
.d_val
= o
->size
;
8517 case DT_PREINIT_ARRAY
:
8518 name
= ".preinit_array";
8521 name
= ".init_array";
8524 name
= ".fini_array";
8537 name
= ".gnu.version_d";
8540 name
= ".gnu.version_r";
8543 name
= ".gnu.version";
8545 o
= bfd_get_section_by_name (abfd
, name
);
8548 (*_bfd_error_handler
)
8549 (_("%B: could not find output section %s"), abfd
, name
);
8552 dyn
.d_un
.d_ptr
= o
->vma
;
8559 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8564 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8566 Elf_Internal_Shdr
*hdr
;
8568 hdr
= elf_elfsections (abfd
)[i
];
8569 if (hdr
->sh_type
== type
8570 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8572 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8573 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8576 if (dyn
.d_un
.d_val
== 0
8577 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8578 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8584 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8588 /* If we have created any dynamic sections, then output them. */
8591 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8594 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8596 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8598 || o
->output_section
== bfd_abs_section_ptr
)
8600 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8602 /* At this point, we are only interested in sections
8603 created by _bfd_elf_link_create_dynamic_sections. */
8606 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8608 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8610 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8612 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8614 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8616 (file_ptr
) o
->output_offset
,
8622 /* The contents of the .dynstr section are actually in a
8624 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8625 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8626 || ! _bfd_elf_strtab_emit (abfd
,
8627 elf_hash_table (info
)->dynstr
))
8633 if (info
->relocatable
)
8635 bfd_boolean failed
= FALSE
;
8637 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8642 /* If we have optimized stabs strings, output them. */
8643 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8645 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8649 if (info
->eh_frame_hdr
)
8651 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8655 if (finfo
.symstrtab
!= NULL
)
8656 _bfd_stringtab_free (finfo
.symstrtab
);
8657 if (finfo
.contents
!= NULL
)
8658 free (finfo
.contents
);
8659 if (finfo
.external_relocs
!= NULL
)
8660 free (finfo
.external_relocs
);
8661 if (finfo
.internal_relocs
!= NULL
)
8662 free (finfo
.internal_relocs
);
8663 if (finfo
.external_syms
!= NULL
)
8664 free (finfo
.external_syms
);
8665 if (finfo
.locsym_shndx
!= NULL
)
8666 free (finfo
.locsym_shndx
);
8667 if (finfo
.internal_syms
!= NULL
)
8668 free (finfo
.internal_syms
);
8669 if (finfo
.indices
!= NULL
)
8670 free (finfo
.indices
);
8671 if (finfo
.sections
!= NULL
)
8672 free (finfo
.sections
);
8673 if (finfo
.symbuf
!= NULL
)
8674 free (finfo
.symbuf
);
8675 if (finfo
.symshndxbuf
!= NULL
)
8676 free (finfo
.symshndxbuf
);
8677 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8679 if ((o
->flags
& SEC_RELOC
) != 0
8680 && elf_section_data (o
)->rel_hashes
!= NULL
)
8681 free (elf_section_data (o
)->rel_hashes
);
8684 elf_tdata (abfd
)->linker
= TRUE
;
8689 if (finfo
.symstrtab
!= NULL
)
8690 _bfd_stringtab_free (finfo
.symstrtab
);
8691 if (finfo
.contents
!= NULL
)
8692 free (finfo
.contents
);
8693 if (finfo
.external_relocs
!= NULL
)
8694 free (finfo
.external_relocs
);
8695 if (finfo
.internal_relocs
!= NULL
)
8696 free (finfo
.internal_relocs
);
8697 if (finfo
.external_syms
!= NULL
)
8698 free (finfo
.external_syms
);
8699 if (finfo
.locsym_shndx
!= NULL
)
8700 free (finfo
.locsym_shndx
);
8701 if (finfo
.internal_syms
!= NULL
)
8702 free (finfo
.internal_syms
);
8703 if (finfo
.indices
!= NULL
)
8704 free (finfo
.indices
);
8705 if (finfo
.sections
!= NULL
)
8706 free (finfo
.sections
);
8707 if (finfo
.symbuf
!= NULL
)
8708 free (finfo
.symbuf
);
8709 if (finfo
.symshndxbuf
!= NULL
)
8710 free (finfo
.symshndxbuf
);
8711 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8713 if ((o
->flags
& SEC_RELOC
) != 0
8714 && elf_section_data (o
)->rel_hashes
!= NULL
)
8715 free (elf_section_data (o
)->rel_hashes
);
8721 /* Garbage collect unused sections. */
8723 /* The mark phase of garbage collection. For a given section, mark
8724 it and any sections in this section's group, and all the sections
8725 which define symbols to which it refers. */
8727 typedef asection
* (*gc_mark_hook_fn
)
8728 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8729 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8732 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8734 gc_mark_hook_fn gc_mark_hook
)
8738 asection
*group_sec
;
8742 /* Mark all the sections in the group. */
8743 group_sec
= elf_section_data (sec
)->next_in_group
;
8744 if (group_sec
&& !group_sec
->gc_mark
)
8745 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8748 /* Look through the section relocs. */
8750 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8751 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8753 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8754 Elf_Internal_Shdr
*symtab_hdr
;
8755 struct elf_link_hash_entry
**sym_hashes
;
8758 bfd
*input_bfd
= sec
->owner
;
8759 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8760 Elf_Internal_Sym
*isym
= NULL
;
8763 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8764 sym_hashes
= elf_sym_hashes (input_bfd
);
8766 /* Read the local symbols. */
8767 if (elf_bad_symtab (input_bfd
))
8769 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8773 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8775 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8776 if (isym
== NULL
&& nlocsyms
!= 0)
8778 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8784 /* Read the relocations. */
8785 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8787 if (relstart
== NULL
)
8792 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8794 if (bed
->s
->arch_size
== 32)
8799 for (rel
= relstart
; rel
< relend
; rel
++)
8801 unsigned long r_symndx
;
8803 struct elf_link_hash_entry
*h
;
8805 r_symndx
= rel
->r_info
>> r_sym_shift
;
8809 if (r_symndx
>= nlocsyms
8810 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8812 h
= sym_hashes
[r_symndx
- extsymoff
];
8813 while (h
->root
.type
== bfd_link_hash_indirect
8814 || h
->root
.type
== bfd_link_hash_warning
)
8815 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8816 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8820 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8823 if (rsec
&& !rsec
->gc_mark
)
8825 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8828 rsec
->gc_mark_from_eh
= 1;
8829 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8838 if (elf_section_data (sec
)->relocs
!= relstart
)
8841 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8843 if (! info
->keep_memory
)
8846 symtab_hdr
->contents
= (unsigned char *) isym
;
8853 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8855 struct elf_gc_sweep_symbol_info
{
8856 struct bfd_link_info
*info
;
8857 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8862 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8864 if (h
->root
.type
== bfd_link_hash_warning
)
8865 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8867 if ((h
->root
.type
== bfd_link_hash_defined
8868 || h
->root
.type
== bfd_link_hash_defweak
)
8869 && !h
->root
.u
.def
.section
->gc_mark
8870 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8872 struct elf_gc_sweep_symbol_info
*inf
= data
;
8873 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8879 /* The sweep phase of garbage collection. Remove all garbage sections. */
8881 typedef bfd_boolean (*gc_sweep_hook_fn
)
8882 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8885 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
8888 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8889 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
8890 unsigned long section_sym_count
;
8891 struct elf_gc_sweep_symbol_info sweep_info
;
8893 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8897 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8900 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8902 /* Keep debug and special sections. */
8903 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8904 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8910 /* Skip sweeping sections already excluded. */
8911 if (o
->flags
& SEC_EXCLUDE
)
8914 /* Since this is early in the link process, it is simple
8915 to remove a section from the output. */
8916 o
->flags
|= SEC_EXCLUDE
;
8918 /* But we also have to update some of the relocation
8919 info we collected before. */
8921 && (o
->flags
& SEC_RELOC
) != 0
8922 && o
->reloc_count
> 0
8923 && !bfd_is_abs_section (o
->output_section
))
8925 Elf_Internal_Rela
*internal_relocs
;
8929 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8931 if (internal_relocs
== NULL
)
8934 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8936 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8937 free (internal_relocs
);
8945 /* Remove the symbols that were in the swept sections from the dynamic
8946 symbol table. GCFIXME: Anyone know how to get them out of the
8947 static symbol table as well? */
8948 sweep_info
.info
= info
;
8949 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
8950 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
8953 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
8957 /* Propagate collected vtable information. This is called through
8958 elf_link_hash_traverse. */
8961 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8963 if (h
->root
.type
== bfd_link_hash_warning
)
8964 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8966 /* Those that are not vtables. */
8967 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8970 /* Those vtables that do not have parents, we cannot merge. */
8971 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8974 /* If we've already been done, exit. */
8975 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8978 /* Make sure the parent's table is up to date. */
8979 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8981 if (h
->vtable
->used
== NULL
)
8983 /* None of this table's entries were referenced. Re-use the
8985 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8986 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8991 bfd_boolean
*cu
, *pu
;
8993 /* Or the parent's entries into ours. */
8994 cu
= h
->vtable
->used
;
8996 pu
= h
->vtable
->parent
->vtable
->used
;
8999 const struct elf_backend_data
*bed
;
9000 unsigned int log_file_align
;
9002 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9003 log_file_align
= bed
->s
->log_file_align
;
9004 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9019 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9022 bfd_vma hstart
, hend
;
9023 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9024 const struct elf_backend_data
*bed
;
9025 unsigned int log_file_align
;
9027 if (h
->root
.type
== bfd_link_hash_warning
)
9028 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9030 /* Take care of both those symbols that do not describe vtables as
9031 well as those that are not loaded. */
9032 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9035 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9036 || h
->root
.type
== bfd_link_hash_defweak
);
9038 sec
= h
->root
.u
.def
.section
;
9039 hstart
= h
->root
.u
.def
.value
;
9040 hend
= hstart
+ h
->size
;
9042 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9044 return *(bfd_boolean
*) okp
= FALSE
;
9045 bed
= get_elf_backend_data (sec
->owner
);
9046 log_file_align
= bed
->s
->log_file_align
;
9048 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9050 for (rel
= relstart
; rel
< relend
; ++rel
)
9051 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9053 /* If the entry is in use, do nothing. */
9055 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9057 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9058 if (h
->vtable
->used
[entry
])
9061 /* Otherwise, kill it. */
9062 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9068 /* Mark sections containing dynamically referenced symbols. When
9069 building shared libraries, we must assume that any visible symbol is
9073 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9075 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9077 if (h
->root
.type
== bfd_link_hash_warning
)
9078 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9080 if ((h
->root
.type
== bfd_link_hash_defined
9081 || h
->root
.type
== bfd_link_hash_defweak
)
9085 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9086 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9087 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9092 /* Do mark and sweep of unused sections. */
9095 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9097 bfd_boolean ok
= TRUE
;
9099 asection
* (*gc_mark_hook
)
9100 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9101 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9103 if (!get_elf_backend_data (abfd
)->can_gc_sections
9104 || info
->relocatable
9105 || info
->emitrelocations
9106 || !is_elf_hash_table (info
->hash
))
9108 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9112 /* Apply transitive closure to the vtable entry usage info. */
9113 elf_link_hash_traverse (elf_hash_table (info
),
9114 elf_gc_propagate_vtable_entries_used
,
9119 /* Kill the vtable relocations that were not used. */
9120 elf_link_hash_traverse (elf_hash_table (info
),
9121 elf_gc_smash_unused_vtentry_relocs
,
9126 /* Mark dynamically referenced symbols. */
9127 if (elf_hash_table (info
)->dynamic_sections_created
)
9128 elf_link_hash_traverse (elf_hash_table (info
),
9129 elf_gc_mark_dynamic_ref_symbol
,
9132 /* Grovel through relocs to find out who stays ... */
9133 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9134 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9138 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9141 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9142 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9143 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9147 /* ... again for sections marked from eh_frame. */
9148 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9152 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9155 /* Keep .gcc_except_table.* if the associated .text.* is
9156 marked. This isn't very nice, but the proper solution,
9157 splitting .eh_frame up and using comdat doesn't pan out
9158 easily due to needing special relocs to handle the
9159 difference of two symbols in separate sections.
9160 Don't keep code sections referenced by .eh_frame. */
9161 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9162 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9164 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9170 len
= strlen (o
->name
+ 18) + 1;
9171 fn_name
= bfd_malloc (len
+ 6);
9172 if (fn_name
== NULL
)
9174 memcpy (fn_name
, ".text.", 6);
9175 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9176 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9178 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9182 /* If not using specially named exception table section,
9183 then keep whatever we are using. */
9184 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9189 /* ... and mark SEC_EXCLUDE for those that go. */
9190 return elf_gc_sweep (abfd
, info
);
9193 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9196 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9198 struct elf_link_hash_entry
*h
,
9201 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9202 struct elf_link_hash_entry
**search
, *child
;
9203 bfd_size_type extsymcount
;
9204 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9206 /* The sh_info field of the symtab header tells us where the
9207 external symbols start. We don't care about the local symbols at
9209 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9210 if (!elf_bad_symtab (abfd
))
9211 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9213 sym_hashes
= elf_sym_hashes (abfd
);
9214 sym_hashes_end
= sym_hashes
+ extsymcount
;
9216 /* Hunt down the child symbol, which is in this section at the same
9217 offset as the relocation. */
9218 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9220 if ((child
= *search
) != NULL
9221 && (child
->root
.type
== bfd_link_hash_defined
9222 || child
->root
.type
== bfd_link_hash_defweak
)
9223 && child
->root
.u
.def
.section
== sec
9224 && child
->root
.u
.def
.value
== offset
)
9228 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9229 abfd
, sec
, (unsigned long) offset
);
9230 bfd_set_error (bfd_error_invalid_operation
);
9236 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9242 /* This *should* only be the absolute section. It could potentially
9243 be that someone has defined a non-global vtable though, which
9244 would be bad. It isn't worth paging in the local symbols to be
9245 sure though; that case should simply be handled by the assembler. */
9247 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9250 child
->vtable
->parent
= h
;
9255 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9258 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9259 asection
*sec ATTRIBUTE_UNUSED
,
9260 struct elf_link_hash_entry
*h
,
9263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9264 unsigned int log_file_align
= bed
->s
->log_file_align
;
9268 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9273 if (addend
>= h
->vtable
->size
)
9275 size_t size
, bytes
, file_align
;
9276 bfd_boolean
*ptr
= h
->vtable
->used
;
9278 /* While the symbol is undefined, we have to be prepared to handle
9280 file_align
= 1 << log_file_align
;
9281 if (h
->root
.type
== bfd_link_hash_undefined
)
9282 size
= addend
+ file_align
;
9288 /* Oops! We've got a reference past the defined end of
9289 the table. This is probably a bug -- shall we warn? */
9290 size
= addend
+ file_align
;
9293 size
= (size
+ file_align
- 1) & -file_align
;
9295 /* Allocate one extra entry for use as a "done" flag for the
9296 consolidation pass. */
9297 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9301 ptr
= bfd_realloc (ptr
- 1, bytes
);
9307 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9308 * sizeof (bfd_boolean
));
9309 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9313 ptr
= bfd_zmalloc (bytes
);
9318 /* And arrange for that done flag to be at index -1. */
9319 h
->vtable
->used
= ptr
+ 1;
9320 h
->vtable
->size
= size
;
9323 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9328 struct alloc_got_off_arg
{
9330 unsigned int got_elt_size
;
9333 /* We need a special top-level link routine to convert got reference counts
9334 to real got offsets. */
9337 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9339 struct alloc_got_off_arg
*gofarg
= arg
;
9341 if (h
->root
.type
== bfd_link_hash_warning
)
9342 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9344 if (h
->got
.refcount
> 0)
9346 h
->got
.offset
= gofarg
->gotoff
;
9347 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9350 h
->got
.offset
= (bfd_vma
) -1;
9355 /* And an accompanying bit to work out final got entry offsets once
9356 we're done. Should be called from final_link. */
9359 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9360 struct bfd_link_info
*info
)
9363 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9365 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9366 struct alloc_got_off_arg gofarg
;
9368 if (! is_elf_hash_table (info
->hash
))
9371 /* The GOT offset is relative to the .got section, but the GOT header is
9372 put into the .got.plt section, if the backend uses it. */
9373 if (bed
->want_got_plt
)
9376 gotoff
= bed
->got_header_size
;
9378 /* Do the local .got entries first. */
9379 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9381 bfd_signed_vma
*local_got
;
9382 bfd_size_type j
, locsymcount
;
9383 Elf_Internal_Shdr
*symtab_hdr
;
9385 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9388 local_got
= elf_local_got_refcounts (i
);
9392 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9393 if (elf_bad_symtab (i
))
9394 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9396 locsymcount
= symtab_hdr
->sh_info
;
9398 for (j
= 0; j
< locsymcount
; ++j
)
9400 if (local_got
[j
] > 0)
9402 local_got
[j
] = gotoff
;
9403 gotoff
+= got_elt_size
;
9406 local_got
[j
] = (bfd_vma
) -1;
9410 /* Then the global .got entries. .plt refcounts are handled by
9411 adjust_dynamic_symbol */
9412 gofarg
.gotoff
= gotoff
;
9413 gofarg
.got_elt_size
= got_elt_size
;
9414 elf_link_hash_traverse (elf_hash_table (info
),
9415 elf_gc_allocate_got_offsets
,
9420 /* Many folk need no more in the way of final link than this, once
9421 got entry reference counting is enabled. */
9424 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9426 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9429 /* Invoke the regular ELF backend linker to do all the work. */
9430 return bfd_elf_final_link (abfd
, info
);
9434 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9436 struct elf_reloc_cookie
*rcookie
= cookie
;
9438 if (rcookie
->bad_symtab
)
9439 rcookie
->rel
= rcookie
->rels
;
9441 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9443 unsigned long r_symndx
;
9445 if (! rcookie
->bad_symtab
)
9446 if (rcookie
->rel
->r_offset
> offset
)
9448 if (rcookie
->rel
->r_offset
!= offset
)
9451 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9452 if (r_symndx
== SHN_UNDEF
)
9455 if (r_symndx
>= rcookie
->locsymcount
9456 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9458 struct elf_link_hash_entry
*h
;
9460 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9462 while (h
->root
.type
== bfd_link_hash_indirect
9463 || h
->root
.type
== bfd_link_hash_warning
)
9464 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9466 if ((h
->root
.type
== bfd_link_hash_defined
9467 || h
->root
.type
== bfd_link_hash_defweak
)
9468 && elf_discarded_section (h
->root
.u
.def
.section
))
9475 /* It's not a relocation against a global symbol,
9476 but it could be a relocation against a local
9477 symbol for a discarded section. */
9479 Elf_Internal_Sym
*isym
;
9481 /* Need to: get the symbol; get the section. */
9482 isym
= &rcookie
->locsyms
[r_symndx
];
9483 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9485 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9486 if (isec
!= NULL
&& elf_discarded_section (isec
))
9495 /* Discard unneeded references to discarded sections.
9496 Returns TRUE if any section's size was changed. */
9497 /* This function assumes that the relocations are in sorted order,
9498 which is true for all known assemblers. */
9501 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9503 struct elf_reloc_cookie cookie
;
9504 asection
*stab
, *eh
;
9505 Elf_Internal_Shdr
*symtab_hdr
;
9506 const struct elf_backend_data
*bed
;
9509 bfd_boolean ret
= FALSE
;
9511 if (info
->traditional_format
9512 || !is_elf_hash_table (info
->hash
))
9515 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9517 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9520 bed
= get_elf_backend_data (abfd
);
9522 if ((abfd
->flags
& DYNAMIC
) != 0)
9525 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9526 if (info
->relocatable
9529 || bfd_is_abs_section (eh
->output_section
))))
9532 stab
= bfd_get_section_by_name (abfd
, ".stab");
9535 || bfd_is_abs_section (stab
->output_section
)
9536 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9541 && bed
->elf_backend_discard_info
== NULL
)
9544 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9546 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9547 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9548 if (cookie
.bad_symtab
)
9550 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9551 cookie
.extsymoff
= 0;
9555 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9556 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9559 if (bed
->s
->arch_size
== 32)
9560 cookie
.r_sym_shift
= 8;
9562 cookie
.r_sym_shift
= 32;
9564 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9565 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9567 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9568 cookie
.locsymcount
, 0,
9570 if (cookie
.locsyms
== NULL
)
9577 count
= stab
->reloc_count
;
9579 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9581 if (cookie
.rels
!= NULL
)
9583 cookie
.rel
= cookie
.rels
;
9584 cookie
.relend
= cookie
.rels
;
9585 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9586 if (_bfd_discard_section_stabs (abfd
, stab
,
9587 elf_section_data (stab
)->sec_info
,
9588 bfd_elf_reloc_symbol_deleted_p
,
9591 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9599 count
= eh
->reloc_count
;
9601 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9603 cookie
.rel
= cookie
.rels
;
9604 cookie
.relend
= cookie
.rels
;
9605 if (cookie
.rels
!= NULL
)
9606 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9608 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9609 bfd_elf_reloc_symbol_deleted_p
,
9613 if (cookie
.rels
!= NULL
9614 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9618 if (bed
->elf_backend_discard_info
!= NULL
9619 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9622 if (cookie
.locsyms
!= NULL
9623 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9625 if (! info
->keep_memory
)
9626 free (cookie
.locsyms
);
9628 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9632 if (info
->eh_frame_hdr
9633 && !info
->relocatable
9634 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9641 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9644 const char *name
, *p
;
9645 struct bfd_section_already_linked
*l
;
9646 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9649 /* A single member comdat group section may be discarded by a
9650 linkonce section. See below. */
9651 if (sec
->output_section
== bfd_abs_section_ptr
)
9656 /* Check if it belongs to a section group. */
9657 group
= elf_sec_group (sec
);
9659 /* Return if it isn't a linkonce section nor a member of a group. A
9660 comdat group section also has SEC_LINK_ONCE set. */
9661 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9666 /* If this is the member of a single member comdat group, check if
9667 the group should be discarded. */
9668 if (elf_next_in_group (sec
) == sec
9669 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9675 /* FIXME: When doing a relocatable link, we may have trouble
9676 copying relocations in other sections that refer to local symbols
9677 in the section being discarded. Those relocations will have to
9678 be converted somehow; as of this writing I'm not sure that any of
9679 the backends handle that correctly.
9681 It is tempting to instead not discard link once sections when
9682 doing a relocatable link (technically, they should be discarded
9683 whenever we are building constructors). However, that fails,
9684 because the linker winds up combining all the link once sections
9685 into a single large link once section, which defeats the purpose
9686 of having link once sections in the first place.
9688 Also, not merging link once sections in a relocatable link
9689 causes trouble for MIPS ELF, which relies on link once semantics
9690 to handle the .reginfo section correctly. */
9692 name
= bfd_get_section_name (abfd
, sec
);
9694 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9695 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9700 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9702 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9704 /* We may have 3 different sections on the list: group section,
9705 comdat section and linkonce section. SEC may be a linkonce or
9706 group section. We match a group section with a group section,
9707 a linkonce section with a linkonce section, and ignore comdat
9709 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9710 && strcmp (name
, l
->sec
->name
) == 0
9711 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9713 /* The section has already been linked. See if we should
9715 switch (flags
& SEC_LINK_DUPLICATES
)
9720 case SEC_LINK_DUPLICATES_DISCARD
:
9723 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9724 (*_bfd_error_handler
)
9725 (_("%B: ignoring duplicate section `%A'"),
9729 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9730 if (sec
->size
!= l
->sec
->size
)
9731 (*_bfd_error_handler
)
9732 (_("%B: duplicate section `%A' has different size"),
9736 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9737 if (sec
->size
!= l
->sec
->size
)
9738 (*_bfd_error_handler
)
9739 (_("%B: duplicate section `%A' has different size"),
9741 else if (sec
->size
!= 0)
9743 bfd_byte
*sec_contents
, *l_sec_contents
;
9745 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9746 (*_bfd_error_handler
)
9747 (_("%B: warning: could not read contents of section `%A'"),
9749 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9751 (*_bfd_error_handler
)
9752 (_("%B: warning: could not read contents of section `%A'"),
9753 l
->sec
->owner
, l
->sec
);
9754 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9755 (*_bfd_error_handler
)
9756 (_("%B: warning: duplicate section `%A' has different contents"),
9760 free (sec_contents
);
9762 free (l_sec_contents
);
9767 /* Set the output_section field so that lang_add_section
9768 does not create a lang_input_section structure for this
9769 section. Since there might be a symbol in the section
9770 being discarded, we must retain a pointer to the section
9771 which we are really going to use. */
9772 sec
->output_section
= bfd_abs_section_ptr
;
9773 sec
->kept_section
= l
->sec
;
9775 if (flags
& SEC_GROUP
)
9777 asection
*first
= elf_next_in_group (sec
);
9778 asection
*s
= first
;
9782 s
->output_section
= bfd_abs_section_ptr
;
9783 /* Record which group discards it. */
9784 s
->kept_section
= l
->sec
;
9785 s
= elf_next_in_group (s
);
9786 /* These lists are circular. */
9798 /* If this is the member of a single member comdat group and the
9799 group hasn't be discarded, we check if it matches a linkonce
9800 section. We only record the discarded comdat group. Otherwise
9801 the undiscarded group will be discarded incorrectly later since
9802 itself has been recorded. */
9803 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9804 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9805 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9806 && bfd_elf_match_symbols_in_sections (l
->sec
,
9807 elf_next_in_group (sec
)))
9809 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9810 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9811 group
->output_section
= bfd_abs_section_ptr
;
9818 /* There is no direct match. But for linkonce section, we should
9819 check if there is a match with comdat group member. We always
9820 record the linkonce section, discarded or not. */
9821 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9822 if (l
->sec
->flags
& SEC_GROUP
)
9824 asection
*first
= elf_next_in_group (l
->sec
);
9827 && elf_next_in_group (first
) == first
9828 && bfd_elf_match_symbols_in_sections (first
, sec
))
9830 sec
->output_section
= bfd_abs_section_ptr
;
9831 sec
->kept_section
= l
->sec
;
9836 /* This is the first section with this name. Record it. */
9837 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9841 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9843 return sym
->st_shndx
== SHN_COMMON
;
9847 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9853 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9855 return bfd_com_section_ptr
;