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. We don't check undefined symbol introduced by
907 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
908 && ELF_ST_TYPE (sym
->st_info
) != h
->type
912 bfd_boolean ntdef
, tdef
;
913 asection
*ntsec
, *tsec
;
915 if (h
->type
== STT_TLS
)
935 (*_bfd_error_handler
)
936 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
937 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
938 else if (!tdef
&& !ntdef
)
939 (*_bfd_error_handler
)
940 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
941 tbfd
, ntbfd
, h
->root
.root
.string
);
943 (*_bfd_error_handler
)
944 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
945 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
947 (*_bfd_error_handler
)
948 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
949 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
951 bfd_set_error (bfd_error_bad_value
);
955 /* We need to remember if a symbol has a definition in a dynamic
956 object or is weak in all dynamic objects. Internal and hidden
957 visibility will make it unavailable to dynamic objects. */
958 if (newdyn
&& !h
->dynamic_def
)
960 if (!bfd_is_und_section (sec
))
964 /* Check if this symbol is weak in all dynamic objects. If it
965 is the first time we see it in a dynamic object, we mark
966 if it is weak. Otherwise, we clear it. */
969 if (bind
== STB_WEAK
)
972 else if (bind
!= STB_WEAK
)
977 /* If the old symbol has non-default visibility, we ignore the new
978 definition from a dynamic object. */
980 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
981 && !bfd_is_und_section (sec
))
984 /* Make sure this symbol is dynamic. */
986 /* A protected symbol has external availability. Make sure it is
989 FIXME: Should we check type and size for protected symbol? */
990 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
991 return bfd_elf_link_record_dynamic_symbol (info
, h
);
996 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
999 /* If the new symbol with non-default visibility comes from a
1000 relocatable file and the old definition comes from a dynamic
1001 object, we remove the old definition. */
1002 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1005 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1006 && bfd_is_und_section (sec
))
1008 /* If the new symbol is undefined and the old symbol was
1009 also undefined before, we need to make sure
1010 _bfd_generic_link_add_one_symbol doesn't mess
1011 up the linker hash table undefs list. Since the old
1012 definition came from a dynamic object, it is still on the
1014 h
->root
.type
= bfd_link_hash_undefined
;
1015 h
->root
.u
.undef
.abfd
= abfd
;
1019 h
->root
.type
= bfd_link_hash_new
;
1020 h
->root
.u
.undef
.abfd
= NULL
;
1029 /* FIXME: Should we check type and size for protected symbol? */
1035 /* Differentiate strong and weak symbols. */
1036 newweak
= bind
== STB_WEAK
;
1037 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1038 || h
->root
.type
== bfd_link_hash_undefweak
);
1040 /* If a new weak symbol definition comes from a regular file and the
1041 old symbol comes from a dynamic library, we treat the new one as
1042 strong. Similarly, an old weak symbol definition from a regular
1043 file is treated as strong when the new symbol comes from a dynamic
1044 library. Further, an old weak symbol from a dynamic library is
1045 treated as strong if the new symbol is from a dynamic library.
1046 This reflects the way glibc's ld.so works.
1048 Do this before setting *type_change_ok or *size_change_ok so that
1049 we warn properly when dynamic library symbols are overridden. */
1051 if (newdef
&& !newdyn
&& olddyn
)
1053 if (olddef
&& newdyn
)
1056 /* It's OK to change the type if either the existing symbol or the
1057 new symbol is weak. A type change is also OK if the old symbol
1058 is undefined and the new symbol is defined. */
1063 && h
->root
.type
== bfd_link_hash_undefined
))
1064 *type_change_ok
= TRUE
;
1066 /* It's OK to change the size if either the existing symbol or the
1067 new symbol is weak, or if the old symbol is undefined. */
1070 || h
->root
.type
== bfd_link_hash_undefined
)
1071 *size_change_ok
= TRUE
;
1073 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1074 symbol, respectively, appears to be a common symbol in a dynamic
1075 object. If a symbol appears in an uninitialized section, and is
1076 not weak, and is not a function, then it may be a common symbol
1077 which was resolved when the dynamic object was created. We want
1078 to treat such symbols specially, because they raise special
1079 considerations when setting the symbol size: if the symbol
1080 appears as a common symbol in a regular object, and the size in
1081 the regular object is larger, we must make sure that we use the
1082 larger size. This problematic case can always be avoided in C,
1083 but it must be handled correctly when using Fortran shared
1086 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1087 likewise for OLDDYNCOMMON and OLDDEF.
1089 Note that this test is just a heuristic, and that it is quite
1090 possible to have an uninitialized symbol in a shared object which
1091 is really a definition, rather than a common symbol. This could
1092 lead to some minor confusion when the symbol really is a common
1093 symbol in some regular object. However, I think it will be
1099 && (sec
->flags
& SEC_ALLOC
) != 0
1100 && (sec
->flags
& SEC_LOAD
) == 0
1102 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1103 newdyncommon
= TRUE
;
1105 newdyncommon
= FALSE
;
1109 && h
->root
.type
== bfd_link_hash_defined
1111 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1112 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1114 && h
->type
!= STT_FUNC
)
1115 olddyncommon
= TRUE
;
1117 olddyncommon
= FALSE
;
1119 /* We now know everything about the old and new symbols. We ask the
1120 backend to check if we can merge them. */
1121 bed
= get_elf_backend_data (abfd
);
1122 if (bed
->merge_symbol
1123 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1124 pold_alignment
, skip
, override
,
1125 type_change_ok
, size_change_ok
,
1126 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1128 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1132 /* If both the old and the new symbols look like common symbols in a
1133 dynamic object, set the size of the symbol to the larger of the
1138 && sym
->st_size
!= h
->size
)
1140 /* Since we think we have two common symbols, issue a multiple
1141 common warning if desired. Note that we only warn if the
1142 size is different. If the size is the same, we simply let
1143 the old symbol override the new one as normally happens with
1144 symbols defined in dynamic objects. */
1146 if (! ((*info
->callbacks
->multiple_common
)
1147 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1148 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1151 if (sym
->st_size
> h
->size
)
1152 h
->size
= sym
->st_size
;
1154 *size_change_ok
= TRUE
;
1157 /* If we are looking at a dynamic object, and we have found a
1158 definition, we need to see if the symbol was already defined by
1159 some other object. If so, we want to use the existing
1160 definition, and we do not want to report a multiple symbol
1161 definition error; we do this by clobbering *PSEC to be
1162 bfd_und_section_ptr.
1164 We treat a common symbol as a definition if the symbol in the
1165 shared library is a function, since common symbols always
1166 represent variables; this can cause confusion in principle, but
1167 any such confusion would seem to indicate an erroneous program or
1168 shared library. We also permit a common symbol in a regular
1169 object to override a weak symbol in a shared object. */
1174 || (h
->root
.type
== bfd_link_hash_common
1176 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1180 newdyncommon
= FALSE
;
1182 *psec
= sec
= bfd_und_section_ptr
;
1183 *size_change_ok
= TRUE
;
1185 /* If we get here when the old symbol is a common symbol, then
1186 we are explicitly letting it override a weak symbol or
1187 function in a dynamic object, and we don't want to warn about
1188 a type change. If the old symbol is a defined symbol, a type
1189 change warning may still be appropriate. */
1191 if (h
->root
.type
== bfd_link_hash_common
)
1192 *type_change_ok
= TRUE
;
1195 /* Handle the special case of an old common symbol merging with a
1196 new symbol which looks like a common symbol in a shared object.
1197 We change *PSEC and *PVALUE to make the new symbol look like a
1198 common symbol, and let _bfd_generic_link_add_one_symbol do the
1202 && h
->root
.type
== bfd_link_hash_common
)
1206 newdyncommon
= FALSE
;
1207 *pvalue
= sym
->st_size
;
1208 *psec
= sec
= bed
->common_section (oldsec
);
1209 *size_change_ok
= TRUE
;
1212 /* Skip weak definitions of symbols that are already defined. */
1213 if (newdef
&& olddef
&& newweak
)
1216 /* If the old symbol is from a dynamic object, and the new symbol is
1217 a definition which is not from a dynamic object, then the new
1218 symbol overrides the old symbol. Symbols from regular files
1219 always take precedence over symbols from dynamic objects, even if
1220 they are defined after the dynamic object in the link.
1222 As above, we again permit a common symbol in a regular object to
1223 override a definition in a shared object if the shared object
1224 symbol is a function or is weak. */
1229 || (bfd_is_com_section (sec
)
1231 || h
->type
== STT_FUNC
)))
1236 /* Change the hash table entry to undefined, and let
1237 _bfd_generic_link_add_one_symbol do the right thing with the
1240 h
->root
.type
= bfd_link_hash_undefined
;
1241 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1242 *size_change_ok
= TRUE
;
1245 olddyncommon
= FALSE
;
1247 /* We again permit a type change when a common symbol may be
1248 overriding a function. */
1250 if (bfd_is_com_section (sec
))
1251 *type_change_ok
= TRUE
;
1253 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1256 /* This union may have been set to be non-NULL when this symbol
1257 was seen in a dynamic object. We must force the union to be
1258 NULL, so that it is correct for a regular symbol. */
1259 h
->verinfo
.vertree
= NULL
;
1262 /* Handle the special case of a new common symbol merging with an
1263 old symbol that looks like it might be a common symbol defined in
1264 a shared object. Note that we have already handled the case in
1265 which a new common symbol should simply override the definition
1266 in the shared library. */
1269 && bfd_is_com_section (sec
)
1272 /* It would be best if we could set the hash table entry to a
1273 common symbol, but we don't know what to use for the section
1274 or the alignment. */
1275 if (! ((*info
->callbacks
->multiple_common
)
1276 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1277 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1280 /* If the presumed common symbol in the dynamic object is
1281 larger, pretend that the new symbol has its size. */
1283 if (h
->size
> *pvalue
)
1286 /* We need to remember the alignment required by the symbol
1287 in the dynamic object. */
1288 BFD_ASSERT (pold_alignment
);
1289 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1292 olddyncommon
= FALSE
;
1294 h
->root
.type
= bfd_link_hash_undefined
;
1295 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1297 *size_change_ok
= TRUE
;
1298 *type_change_ok
= TRUE
;
1300 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1303 h
->verinfo
.vertree
= NULL
;
1308 /* Handle the case where we had a versioned symbol in a dynamic
1309 library and now find a definition in a normal object. In this
1310 case, we make the versioned symbol point to the normal one. */
1311 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1312 flip
->root
.type
= h
->root
.type
;
1313 h
->root
.type
= bfd_link_hash_indirect
;
1314 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1315 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1316 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1320 flip
->ref_dynamic
= 1;
1327 /* This function is called to create an indirect symbol from the
1328 default for the symbol with the default version if needed. The
1329 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1330 set DYNSYM if the new indirect symbol is dynamic. */
1333 _bfd_elf_add_default_symbol (bfd
*abfd
,
1334 struct bfd_link_info
*info
,
1335 struct elf_link_hash_entry
*h
,
1337 Elf_Internal_Sym
*sym
,
1340 bfd_boolean
*dynsym
,
1341 bfd_boolean override
)
1343 bfd_boolean type_change_ok
;
1344 bfd_boolean size_change_ok
;
1347 struct elf_link_hash_entry
*hi
;
1348 struct bfd_link_hash_entry
*bh
;
1349 const struct elf_backend_data
*bed
;
1350 bfd_boolean collect
;
1351 bfd_boolean dynamic
;
1353 size_t len
, shortlen
;
1356 /* If this symbol has a version, and it is the default version, we
1357 create an indirect symbol from the default name to the fully
1358 decorated name. This will cause external references which do not
1359 specify a version to be bound to this version of the symbol. */
1360 p
= strchr (name
, ELF_VER_CHR
);
1361 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1366 /* We are overridden by an old definition. We need to check if we
1367 need to create the indirect symbol from the default name. */
1368 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1370 BFD_ASSERT (hi
!= NULL
);
1373 while (hi
->root
.type
== bfd_link_hash_indirect
1374 || hi
->root
.type
== bfd_link_hash_warning
)
1376 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1382 bed
= get_elf_backend_data (abfd
);
1383 collect
= bed
->collect
;
1384 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1386 shortlen
= p
- name
;
1387 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1388 if (shortname
== NULL
)
1390 memcpy (shortname
, name
, shortlen
);
1391 shortname
[shortlen
] = '\0';
1393 /* We are going to create a new symbol. Merge it with any existing
1394 symbol with this name. For the purposes of the merge, act as
1395 though we were defining the symbol we just defined, although we
1396 actually going to define an indirect symbol. */
1397 type_change_ok
= FALSE
;
1398 size_change_ok
= FALSE
;
1400 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1401 NULL
, &hi
, &skip
, &override
,
1402 &type_change_ok
, &size_change_ok
))
1411 if (! (_bfd_generic_link_add_one_symbol
1412 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1413 0, name
, FALSE
, collect
, &bh
)))
1415 hi
= (struct elf_link_hash_entry
*) bh
;
1419 /* In this case the symbol named SHORTNAME is overriding the
1420 indirect symbol we want to add. We were planning on making
1421 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1422 is the name without a version. NAME is the fully versioned
1423 name, and it is the default version.
1425 Overriding means that we already saw a definition for the
1426 symbol SHORTNAME in a regular object, and it is overriding
1427 the symbol defined in the dynamic object.
1429 When this happens, we actually want to change NAME, the
1430 symbol we just added, to refer to SHORTNAME. This will cause
1431 references to NAME in the shared object to become references
1432 to SHORTNAME in the regular object. This is what we expect
1433 when we override a function in a shared object: that the
1434 references in the shared object will be mapped to the
1435 definition in the regular object. */
1437 while (hi
->root
.type
== bfd_link_hash_indirect
1438 || hi
->root
.type
== bfd_link_hash_warning
)
1439 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1441 h
->root
.type
= bfd_link_hash_indirect
;
1442 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1446 hi
->ref_dynamic
= 1;
1450 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1455 /* Now set HI to H, so that the following code will set the
1456 other fields correctly. */
1460 /* If there is a duplicate definition somewhere, then HI may not
1461 point to an indirect symbol. We will have reported an error to
1462 the user in that case. */
1464 if (hi
->root
.type
== bfd_link_hash_indirect
)
1466 struct elf_link_hash_entry
*ht
;
1468 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1469 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1471 /* See if the new flags lead us to realize that the symbol must
1483 if (hi
->ref_regular
)
1489 /* We also need to define an indirection from the nondefault version
1493 len
= strlen (name
);
1494 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1495 if (shortname
== NULL
)
1497 memcpy (shortname
, name
, shortlen
);
1498 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1500 /* Once again, merge with any existing symbol. */
1501 type_change_ok
= FALSE
;
1502 size_change_ok
= FALSE
;
1504 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1505 NULL
, &hi
, &skip
, &override
,
1506 &type_change_ok
, &size_change_ok
))
1514 /* Here SHORTNAME is a versioned name, so we don't expect to see
1515 the type of override we do in the case above unless it is
1516 overridden by a versioned definition. */
1517 if (hi
->root
.type
!= bfd_link_hash_defined
1518 && hi
->root
.type
!= bfd_link_hash_defweak
)
1519 (*_bfd_error_handler
)
1520 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1526 if (! (_bfd_generic_link_add_one_symbol
1527 (info
, abfd
, shortname
, BSF_INDIRECT
,
1528 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1530 hi
= (struct elf_link_hash_entry
*) bh
;
1532 /* If there is a duplicate definition somewhere, then HI may not
1533 point to an indirect symbol. We will have reported an error
1534 to the user in that case. */
1536 if (hi
->root
.type
== bfd_link_hash_indirect
)
1538 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1540 /* See if the new flags lead us to realize that the symbol
1552 if (hi
->ref_regular
)
1562 /* This routine is used to export all defined symbols into the dynamic
1563 symbol table. It is called via elf_link_hash_traverse. */
1566 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1568 struct elf_info_failed
*eif
= data
;
1570 /* Ignore indirect symbols. These are added by the versioning code. */
1571 if (h
->root
.type
== bfd_link_hash_indirect
)
1574 if (h
->root
.type
== bfd_link_hash_warning
)
1575 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1577 if (h
->dynindx
== -1
1581 struct bfd_elf_version_tree
*t
;
1582 struct bfd_elf_version_expr
*d
;
1584 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1586 if (t
->globals
.list
!= NULL
)
1588 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1593 if (t
->locals
.list
!= NULL
)
1595 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1604 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1615 /* Look through the symbols which are defined in other shared
1616 libraries and referenced here. Update the list of version
1617 dependencies. This will be put into the .gnu.version_r section.
1618 This function is called via elf_link_hash_traverse. */
1621 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1624 struct elf_find_verdep_info
*rinfo
= data
;
1625 Elf_Internal_Verneed
*t
;
1626 Elf_Internal_Vernaux
*a
;
1629 if (h
->root
.type
== bfd_link_hash_warning
)
1630 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1632 /* We only care about symbols defined in shared objects with version
1637 || h
->verinfo
.verdef
== NULL
)
1640 /* See if we already know about this version. */
1641 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1643 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1646 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1647 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1653 /* This is a new version. Add it to tree we are building. */
1658 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1661 rinfo
->failed
= TRUE
;
1665 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1666 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1667 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1671 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1673 /* Note that we are copying a string pointer here, and testing it
1674 above. If bfd_elf_string_from_elf_section is ever changed to
1675 discard the string data when low in memory, this will have to be
1677 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1679 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1680 a
->vna_nextptr
= t
->vn_auxptr
;
1682 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1685 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1692 /* Figure out appropriate versions for all the symbols. We may not
1693 have the version number script until we have read all of the input
1694 files, so until that point we don't know which symbols should be
1695 local. This function is called via elf_link_hash_traverse. */
1698 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1700 struct elf_assign_sym_version_info
*sinfo
;
1701 struct bfd_link_info
*info
;
1702 const struct elf_backend_data
*bed
;
1703 struct elf_info_failed eif
;
1710 if (h
->root
.type
== bfd_link_hash_warning
)
1711 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1713 /* Fix the symbol flags. */
1716 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1719 sinfo
->failed
= TRUE
;
1723 /* We only need version numbers for symbols defined in regular
1725 if (!h
->def_regular
)
1728 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1729 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1730 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1732 struct bfd_elf_version_tree
*t
;
1737 /* There are two consecutive ELF_VER_CHR characters if this is
1738 not a hidden symbol. */
1740 if (*p
== ELF_VER_CHR
)
1746 /* If there is no version string, we can just return out. */
1754 /* Look for the version. If we find it, it is no longer weak. */
1755 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1757 if (strcmp (t
->name
, p
) == 0)
1761 struct bfd_elf_version_expr
*d
;
1763 len
= p
- h
->root
.root
.string
;
1764 alc
= bfd_malloc (len
);
1767 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1768 alc
[len
- 1] = '\0';
1769 if (alc
[len
- 2] == ELF_VER_CHR
)
1770 alc
[len
- 2] = '\0';
1772 h
->verinfo
.vertree
= t
;
1776 if (t
->globals
.list
!= NULL
)
1777 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1779 /* See if there is anything to force this symbol to
1781 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1783 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1786 && ! info
->export_dynamic
)
1787 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1795 /* If we are building an application, we need to create a
1796 version node for this version. */
1797 if (t
== NULL
&& info
->executable
)
1799 struct bfd_elf_version_tree
**pp
;
1802 /* If we aren't going to export this symbol, we don't need
1803 to worry about it. */
1804 if (h
->dynindx
== -1)
1808 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1811 sinfo
->failed
= TRUE
;
1816 t
->name_indx
= (unsigned int) -1;
1820 /* Don't count anonymous version tag. */
1821 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1823 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1825 t
->vernum
= version_index
;
1829 h
->verinfo
.vertree
= t
;
1833 /* We could not find the version for a symbol when
1834 generating a shared archive. Return an error. */
1835 (*_bfd_error_handler
)
1836 (_("%B: undefined versioned symbol name %s"),
1837 sinfo
->output_bfd
, h
->root
.root
.string
);
1838 bfd_set_error (bfd_error_bad_value
);
1839 sinfo
->failed
= TRUE
;
1847 /* If we don't have a version for this symbol, see if we can find
1849 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1851 struct bfd_elf_version_tree
*t
;
1852 struct bfd_elf_version_tree
*local_ver
;
1853 struct bfd_elf_version_expr
*d
;
1855 /* See if can find what version this symbol is in. If the
1856 symbol is supposed to be local, then don't actually register
1859 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1861 if (t
->globals
.list
!= NULL
)
1863 bfd_boolean matched
;
1867 while ((d
= (*t
->match
) (&t
->globals
, d
,
1868 h
->root
.root
.string
)) != NULL
)
1873 /* There is a version without definition. Make
1874 the symbol the default definition for this
1876 h
->verinfo
.vertree
= t
;
1884 /* There is no undefined version for this symbol. Hide the
1886 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1889 if (t
->locals
.list
!= NULL
)
1892 while ((d
= (*t
->match
) (&t
->locals
, d
,
1893 h
->root
.root
.string
)) != NULL
)
1896 /* If the match is "*", keep looking for a more
1897 explicit, perhaps even global, match.
1898 XXX: Shouldn't this be !d->wildcard instead? */
1899 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1908 if (local_ver
!= NULL
)
1910 h
->verinfo
.vertree
= local_ver
;
1911 if (h
->dynindx
!= -1
1912 && ! info
->export_dynamic
)
1914 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1922 /* Read and swap the relocs from the section indicated by SHDR. This
1923 may be either a REL or a RELA section. The relocations are
1924 translated into RELA relocations and stored in INTERNAL_RELOCS,
1925 which should have already been allocated to contain enough space.
1926 The EXTERNAL_RELOCS are a buffer where the external form of the
1927 relocations should be stored.
1929 Returns FALSE if something goes wrong. */
1932 elf_link_read_relocs_from_section (bfd
*abfd
,
1934 Elf_Internal_Shdr
*shdr
,
1935 void *external_relocs
,
1936 Elf_Internal_Rela
*internal_relocs
)
1938 const struct elf_backend_data
*bed
;
1939 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1940 const bfd_byte
*erela
;
1941 const bfd_byte
*erelaend
;
1942 Elf_Internal_Rela
*irela
;
1943 Elf_Internal_Shdr
*symtab_hdr
;
1946 /* Position ourselves at the start of the section. */
1947 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1950 /* Read the relocations. */
1951 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1954 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1955 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1957 bed
= get_elf_backend_data (abfd
);
1959 /* Convert the external relocations to the internal format. */
1960 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1961 swap_in
= bed
->s
->swap_reloc_in
;
1962 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1963 swap_in
= bed
->s
->swap_reloca_in
;
1966 bfd_set_error (bfd_error_wrong_format
);
1970 erela
= external_relocs
;
1971 erelaend
= erela
+ shdr
->sh_size
;
1972 irela
= internal_relocs
;
1973 while (erela
< erelaend
)
1977 (*swap_in
) (abfd
, erela
, irela
);
1978 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1979 if (bed
->s
->arch_size
== 64)
1981 if ((size_t) r_symndx
>= nsyms
)
1983 (*_bfd_error_handler
)
1984 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1985 " for offset 0x%lx in section `%A'"),
1987 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1988 bfd_set_error (bfd_error_bad_value
);
1991 irela
+= bed
->s
->int_rels_per_ext_rel
;
1992 erela
+= shdr
->sh_entsize
;
1998 /* Read and swap the relocs for a section O. They may have been
1999 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2000 not NULL, they are used as buffers to read into. They are known to
2001 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2002 the return value is allocated using either malloc or bfd_alloc,
2003 according to the KEEP_MEMORY argument. If O has two relocation
2004 sections (both REL and RELA relocations), then the REL_HDR
2005 relocations will appear first in INTERNAL_RELOCS, followed by the
2006 REL_HDR2 relocations. */
2009 _bfd_elf_link_read_relocs (bfd
*abfd
,
2011 void *external_relocs
,
2012 Elf_Internal_Rela
*internal_relocs
,
2013 bfd_boolean keep_memory
)
2015 Elf_Internal_Shdr
*rel_hdr
;
2016 void *alloc1
= NULL
;
2017 Elf_Internal_Rela
*alloc2
= NULL
;
2018 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2020 if (elf_section_data (o
)->relocs
!= NULL
)
2021 return elf_section_data (o
)->relocs
;
2023 if (o
->reloc_count
== 0)
2026 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2028 if (internal_relocs
== NULL
)
2032 size
= o
->reloc_count
;
2033 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2035 internal_relocs
= bfd_alloc (abfd
, size
);
2037 internal_relocs
= alloc2
= bfd_malloc (size
);
2038 if (internal_relocs
== NULL
)
2042 if (external_relocs
== NULL
)
2044 bfd_size_type size
= rel_hdr
->sh_size
;
2046 if (elf_section_data (o
)->rel_hdr2
)
2047 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2048 alloc1
= bfd_malloc (size
);
2051 external_relocs
= alloc1
;
2054 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2058 if (elf_section_data (o
)->rel_hdr2
2059 && (!elf_link_read_relocs_from_section
2061 elf_section_data (o
)->rel_hdr2
,
2062 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2063 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2064 * bed
->s
->int_rels_per_ext_rel
))))
2067 /* Cache the results for next time, if we can. */
2069 elf_section_data (o
)->relocs
= internal_relocs
;
2074 /* Don't free alloc2, since if it was allocated we are passing it
2075 back (under the name of internal_relocs). */
2077 return internal_relocs
;
2087 /* Compute the size of, and allocate space for, REL_HDR which is the
2088 section header for a section containing relocations for O. */
2091 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2092 Elf_Internal_Shdr
*rel_hdr
,
2095 bfd_size_type reloc_count
;
2096 bfd_size_type num_rel_hashes
;
2098 /* Figure out how many relocations there will be. */
2099 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2100 reloc_count
= elf_section_data (o
)->rel_count
;
2102 reloc_count
= elf_section_data (o
)->rel_count2
;
2104 num_rel_hashes
= o
->reloc_count
;
2105 if (num_rel_hashes
< reloc_count
)
2106 num_rel_hashes
= reloc_count
;
2108 /* That allows us to calculate the size of the section. */
2109 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2111 /* The contents field must last into write_object_contents, so we
2112 allocate it with bfd_alloc rather than malloc. Also since we
2113 cannot be sure that the contents will actually be filled in,
2114 we zero the allocated space. */
2115 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2116 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2119 /* We only allocate one set of hash entries, so we only do it the
2120 first time we are called. */
2121 if (elf_section_data (o
)->rel_hashes
== NULL
2124 struct elf_link_hash_entry
**p
;
2126 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2130 elf_section_data (o
)->rel_hashes
= p
;
2136 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2137 originated from the section given by INPUT_REL_HDR) to the
2141 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2142 asection
*input_section
,
2143 Elf_Internal_Shdr
*input_rel_hdr
,
2144 Elf_Internal_Rela
*internal_relocs
,
2145 struct elf_link_hash_entry
**rel_hash
2148 Elf_Internal_Rela
*irela
;
2149 Elf_Internal_Rela
*irelaend
;
2151 Elf_Internal_Shdr
*output_rel_hdr
;
2152 asection
*output_section
;
2153 unsigned int *rel_countp
= NULL
;
2154 const struct elf_backend_data
*bed
;
2155 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2157 output_section
= input_section
->output_section
;
2158 output_rel_hdr
= NULL
;
2160 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2161 == input_rel_hdr
->sh_entsize
)
2163 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2164 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2166 else if (elf_section_data (output_section
)->rel_hdr2
2167 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2168 == input_rel_hdr
->sh_entsize
))
2170 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2171 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2175 (*_bfd_error_handler
)
2176 (_("%B: relocation size mismatch in %B section %A"),
2177 output_bfd
, input_section
->owner
, input_section
);
2178 bfd_set_error (bfd_error_wrong_object_format
);
2182 bed
= get_elf_backend_data (output_bfd
);
2183 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2184 swap_out
= bed
->s
->swap_reloc_out
;
2185 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2186 swap_out
= bed
->s
->swap_reloca_out
;
2190 erel
= output_rel_hdr
->contents
;
2191 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2192 irela
= internal_relocs
;
2193 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2194 * bed
->s
->int_rels_per_ext_rel
);
2195 while (irela
< irelaend
)
2197 (*swap_out
) (output_bfd
, irela
, erel
);
2198 irela
+= bed
->s
->int_rels_per_ext_rel
;
2199 erel
+= input_rel_hdr
->sh_entsize
;
2202 /* Bump the counter, so that we know where to add the next set of
2204 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2209 /* Fix up the flags for a symbol. This handles various cases which
2210 can only be fixed after all the input files are seen. This is
2211 currently called by both adjust_dynamic_symbol and
2212 assign_sym_version, which is unnecessary but perhaps more robust in
2213 the face of future changes. */
2216 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2217 struct elf_info_failed
*eif
)
2219 /* If this symbol was mentioned in a non-ELF file, try to set
2220 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2221 permit a non-ELF file to correctly refer to a symbol defined in
2222 an ELF dynamic object. */
2225 while (h
->root
.type
== bfd_link_hash_indirect
)
2226 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2228 if (h
->root
.type
!= bfd_link_hash_defined
2229 && h
->root
.type
!= bfd_link_hash_defweak
)
2232 h
->ref_regular_nonweak
= 1;
2236 if (h
->root
.u
.def
.section
->owner
!= NULL
2237 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2238 == bfd_target_elf_flavour
))
2241 h
->ref_regular_nonweak
= 1;
2247 if (h
->dynindx
== -1
2251 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2260 /* Unfortunately, NON_ELF is only correct if the symbol
2261 was first seen in a non-ELF file. Fortunately, if the symbol
2262 was first seen in an ELF file, we're probably OK unless the
2263 symbol was defined in a non-ELF file. Catch that case here.
2264 FIXME: We're still in trouble if the symbol was first seen in
2265 a dynamic object, and then later in a non-ELF regular object. */
2266 if ((h
->root
.type
== bfd_link_hash_defined
2267 || h
->root
.type
== bfd_link_hash_defweak
)
2269 && (h
->root
.u
.def
.section
->owner
!= NULL
2270 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2271 != bfd_target_elf_flavour
)
2272 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2273 && !h
->def_dynamic
)))
2277 /* If this is a final link, and the symbol was defined as a common
2278 symbol in a regular object file, and there was no definition in
2279 any dynamic object, then the linker will have allocated space for
2280 the symbol in a common section but the DEF_REGULAR
2281 flag will not have been set. */
2282 if (h
->root
.type
== bfd_link_hash_defined
2286 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2289 /* If -Bsymbolic was used (which means to bind references to global
2290 symbols to the definition within the shared object), and this
2291 symbol was defined in a regular object, then it actually doesn't
2292 need a PLT entry. Likewise, if the symbol has non-default
2293 visibility. If the symbol has hidden or internal visibility, we
2294 will force it local. */
2296 && eif
->info
->shared
2297 && is_elf_hash_table (eif
->info
->hash
)
2298 && (eif
->info
->symbolic
2299 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2302 const struct elf_backend_data
*bed
;
2303 bfd_boolean force_local
;
2305 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2307 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2308 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2309 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2312 /* If a weak undefined symbol has non-default visibility, we also
2313 hide it from the dynamic linker. */
2314 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2315 && h
->root
.type
== bfd_link_hash_undefweak
)
2317 const struct elf_backend_data
*bed
;
2318 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2319 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2322 /* If this is a weak defined symbol in a dynamic object, and we know
2323 the real definition in the dynamic object, copy interesting flags
2324 over to the real definition. */
2325 if (h
->u
.weakdef
!= NULL
)
2327 struct elf_link_hash_entry
*weakdef
;
2329 weakdef
= h
->u
.weakdef
;
2330 if (h
->root
.type
== bfd_link_hash_indirect
)
2331 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2333 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2334 || h
->root
.type
== bfd_link_hash_defweak
);
2335 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2336 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2337 BFD_ASSERT (weakdef
->def_dynamic
);
2339 /* If the real definition is defined by a regular object file,
2340 don't do anything special. See the longer description in
2341 _bfd_elf_adjust_dynamic_symbol, below. */
2342 if (weakdef
->def_regular
)
2343 h
->u
.weakdef
= NULL
;
2346 const struct elf_backend_data
*bed
;
2348 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2349 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2356 /* Make the backend pick a good value for a dynamic symbol. This is
2357 called via elf_link_hash_traverse, and also calls itself
2361 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2363 struct elf_info_failed
*eif
= data
;
2365 const struct elf_backend_data
*bed
;
2367 if (! is_elf_hash_table (eif
->info
->hash
))
2370 if (h
->root
.type
== bfd_link_hash_warning
)
2372 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2373 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2375 /* When warning symbols are created, they **replace** the "real"
2376 entry in the hash table, thus we never get to see the real
2377 symbol in a hash traversal. So look at it now. */
2378 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2381 /* Ignore indirect symbols. These are added by the versioning code. */
2382 if (h
->root
.type
== bfd_link_hash_indirect
)
2385 /* Fix the symbol flags. */
2386 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2389 /* If this symbol does not require a PLT entry, and it is not
2390 defined by a dynamic object, or is not referenced by a regular
2391 object, ignore it. We do have to handle a weak defined symbol,
2392 even if no regular object refers to it, if we decided to add it
2393 to the dynamic symbol table. FIXME: Do we normally need to worry
2394 about symbols which are defined by one dynamic object and
2395 referenced by another one? */
2400 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2402 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2406 /* If we've already adjusted this symbol, don't do it again. This
2407 can happen via a recursive call. */
2408 if (h
->dynamic_adjusted
)
2411 /* Don't look at this symbol again. Note that we must set this
2412 after checking the above conditions, because we may look at a
2413 symbol once, decide not to do anything, and then get called
2414 recursively later after REF_REGULAR is set below. */
2415 h
->dynamic_adjusted
= 1;
2417 /* If this is a weak definition, and we know a real definition, and
2418 the real symbol is not itself defined by a regular object file,
2419 then get a good value for the real definition. We handle the
2420 real symbol first, for the convenience of the backend routine.
2422 Note that there is a confusing case here. If the real definition
2423 is defined by a regular object file, we don't get the real symbol
2424 from the dynamic object, but we do get the weak symbol. If the
2425 processor backend uses a COPY reloc, then if some routine in the
2426 dynamic object changes the real symbol, we will not see that
2427 change in the corresponding weak symbol. This is the way other
2428 ELF linkers work as well, and seems to be a result of the shared
2431 I will clarify this issue. Most SVR4 shared libraries define the
2432 variable _timezone and define timezone as a weak synonym. The
2433 tzset call changes _timezone. If you write
2434 extern int timezone;
2436 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2437 you might expect that, since timezone is a synonym for _timezone,
2438 the same number will print both times. However, if the processor
2439 backend uses a COPY reloc, then actually timezone will be copied
2440 into your process image, and, since you define _timezone
2441 yourself, _timezone will not. Thus timezone and _timezone will
2442 wind up at different memory locations. The tzset call will set
2443 _timezone, leaving timezone unchanged. */
2445 if (h
->u
.weakdef
!= NULL
)
2447 /* If we get to this point, we know there is an implicit
2448 reference by a regular object file via the weak symbol H.
2449 FIXME: Is this really true? What if the traversal finds
2450 H->U.WEAKDEF before it finds H? */
2451 h
->u
.weakdef
->ref_regular
= 1;
2453 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2457 /* If a symbol has no type and no size and does not require a PLT
2458 entry, then we are probably about to do the wrong thing here: we
2459 are probably going to create a COPY reloc for an empty object.
2460 This case can arise when a shared object is built with assembly
2461 code, and the assembly code fails to set the symbol type. */
2463 && h
->type
== STT_NOTYPE
2465 (*_bfd_error_handler
)
2466 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2467 h
->root
.root
.string
);
2469 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2470 bed
= get_elf_backend_data (dynobj
);
2471 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2480 /* Adjust all external symbols pointing into SEC_MERGE sections
2481 to reflect the object merging within the sections. */
2484 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2488 if (h
->root
.type
== bfd_link_hash_warning
)
2489 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2491 if ((h
->root
.type
== bfd_link_hash_defined
2492 || h
->root
.type
== bfd_link_hash_defweak
)
2493 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2494 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2496 bfd
*output_bfd
= data
;
2498 h
->root
.u
.def
.value
=
2499 _bfd_merged_section_offset (output_bfd
,
2500 &h
->root
.u
.def
.section
,
2501 elf_section_data (sec
)->sec_info
,
2502 h
->root
.u
.def
.value
);
2508 /* Returns false if the symbol referred to by H should be considered
2509 to resolve local to the current module, and true if it should be
2510 considered to bind dynamically. */
2513 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2514 struct bfd_link_info
*info
,
2515 bfd_boolean ignore_protected
)
2517 bfd_boolean binding_stays_local_p
;
2522 while (h
->root
.type
== bfd_link_hash_indirect
2523 || h
->root
.type
== bfd_link_hash_warning
)
2524 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2526 /* If it was forced local, then clearly it's not dynamic. */
2527 if (h
->dynindx
== -1)
2529 if (h
->forced_local
)
2532 /* Identify the cases where name binding rules say that a
2533 visible symbol resolves locally. */
2534 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2536 switch (ELF_ST_VISIBILITY (h
->other
))
2543 /* Proper resolution for function pointer equality may require
2544 that these symbols perhaps be resolved dynamically, even though
2545 we should be resolving them to the current module. */
2546 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2547 binding_stays_local_p
= TRUE
;
2554 /* If it isn't defined locally, then clearly it's dynamic. */
2555 if (!h
->def_regular
)
2558 /* Otherwise, the symbol is dynamic if binding rules don't tell
2559 us that it remains local. */
2560 return !binding_stays_local_p
;
2563 /* Return true if the symbol referred to by H should be considered
2564 to resolve local to the current module, and false otherwise. Differs
2565 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2566 undefined symbols and weak symbols. */
2569 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2570 struct bfd_link_info
*info
,
2571 bfd_boolean local_protected
)
2573 /* If it's a local sym, of course we resolve locally. */
2577 /* Common symbols that become definitions don't get the DEF_REGULAR
2578 flag set, so test it first, and don't bail out. */
2579 if (ELF_COMMON_DEF_P (h
))
2581 /* If we don't have a definition in a regular file, then we can't
2582 resolve locally. The sym is either undefined or dynamic. */
2583 else if (!h
->def_regular
)
2586 /* Forced local symbols resolve locally. */
2587 if (h
->forced_local
)
2590 /* As do non-dynamic symbols. */
2591 if (h
->dynindx
== -1)
2594 /* At this point, we know the symbol is defined and dynamic. In an
2595 executable it must resolve locally, likewise when building symbolic
2596 shared libraries. */
2597 if (info
->executable
|| info
->symbolic
)
2600 /* Now deal with defined dynamic symbols in shared libraries. Ones
2601 with default visibility might not resolve locally. */
2602 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2605 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2606 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2609 /* STV_PROTECTED non-function symbols are local. */
2610 if (h
->type
!= STT_FUNC
)
2613 /* Function pointer equality tests may require that STV_PROTECTED
2614 symbols be treated as dynamic symbols, even when we know that the
2615 dynamic linker will resolve them locally. */
2616 return local_protected
;
2619 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2620 aligned. Returns the first TLS output section. */
2622 struct bfd_section
*
2623 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2625 struct bfd_section
*sec
, *tls
;
2626 unsigned int align
= 0;
2628 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2629 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2633 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2634 if (sec
->alignment_power
> align
)
2635 align
= sec
->alignment_power
;
2637 elf_hash_table (info
)->tls_sec
= tls
;
2639 /* Ensure the alignment of the first section is the largest alignment,
2640 so that the tls segment starts aligned. */
2642 tls
->alignment_power
= align
;
2647 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2649 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2650 Elf_Internal_Sym
*sym
)
2652 const struct elf_backend_data
*bed
;
2654 /* Local symbols do not count, but target specific ones might. */
2655 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2656 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2659 /* Function symbols do not count. */
2660 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2663 /* If the section is undefined, then so is the symbol. */
2664 if (sym
->st_shndx
== SHN_UNDEF
)
2667 /* If the symbol is defined in the common section, then
2668 it is a common definition and so does not count. */
2669 bed
= get_elf_backend_data (abfd
);
2670 if (bed
->common_definition (sym
))
2673 /* If the symbol is in a target specific section then we
2674 must rely upon the backend to tell us what it is. */
2675 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2676 /* FIXME - this function is not coded yet:
2678 return _bfd_is_global_symbol_definition (abfd, sym);
2680 Instead for now assume that the definition is not global,
2681 Even if this is wrong, at least the linker will behave
2682 in the same way that it used to do. */
2688 /* Search the symbol table of the archive element of the archive ABFD
2689 whose archive map contains a mention of SYMDEF, and determine if
2690 the symbol is defined in this element. */
2692 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2694 Elf_Internal_Shdr
* hdr
;
2695 bfd_size_type symcount
;
2696 bfd_size_type extsymcount
;
2697 bfd_size_type extsymoff
;
2698 Elf_Internal_Sym
*isymbuf
;
2699 Elf_Internal_Sym
*isym
;
2700 Elf_Internal_Sym
*isymend
;
2703 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2707 if (! bfd_check_format (abfd
, bfd_object
))
2710 /* If we have already included the element containing this symbol in the
2711 link then we do not need to include it again. Just claim that any symbol
2712 it contains is not a definition, so that our caller will not decide to
2713 (re)include this element. */
2714 if (abfd
->archive_pass
)
2717 /* Select the appropriate symbol table. */
2718 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2719 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2721 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2723 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2725 /* The sh_info field of the symtab header tells us where the
2726 external symbols start. We don't care about the local symbols. */
2727 if (elf_bad_symtab (abfd
))
2729 extsymcount
= symcount
;
2734 extsymcount
= symcount
- hdr
->sh_info
;
2735 extsymoff
= hdr
->sh_info
;
2738 if (extsymcount
== 0)
2741 /* Read in the symbol table. */
2742 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2744 if (isymbuf
== NULL
)
2747 /* Scan the symbol table looking for SYMDEF. */
2749 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2753 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2758 if (strcmp (name
, symdef
->name
) == 0)
2760 result
= is_global_data_symbol_definition (abfd
, isym
);
2770 /* Add an entry to the .dynamic table. */
2773 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2777 struct elf_link_hash_table
*hash_table
;
2778 const struct elf_backend_data
*bed
;
2780 bfd_size_type newsize
;
2781 bfd_byte
*newcontents
;
2782 Elf_Internal_Dyn dyn
;
2784 hash_table
= elf_hash_table (info
);
2785 if (! is_elf_hash_table (hash_table
))
2788 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2790 (_("warning: creating a DT_TEXTREL in a shared object."));
2792 bed
= get_elf_backend_data (hash_table
->dynobj
);
2793 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2794 BFD_ASSERT (s
!= NULL
);
2796 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2797 newcontents
= bfd_realloc (s
->contents
, newsize
);
2798 if (newcontents
== NULL
)
2802 dyn
.d_un
.d_val
= val
;
2803 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2806 s
->contents
= newcontents
;
2811 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2812 otherwise just check whether one already exists. Returns -1 on error,
2813 1 if a DT_NEEDED tag already exists, and 0 on success. */
2816 elf_add_dt_needed_tag (bfd
*abfd
,
2817 struct bfd_link_info
*info
,
2821 struct elf_link_hash_table
*hash_table
;
2822 bfd_size_type oldsize
;
2823 bfd_size_type strindex
;
2825 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2828 hash_table
= elf_hash_table (info
);
2829 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2830 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2831 if (strindex
== (bfd_size_type
) -1)
2834 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2837 const struct elf_backend_data
*bed
;
2840 bed
= get_elf_backend_data (hash_table
->dynobj
);
2841 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2843 for (extdyn
= sdyn
->contents
;
2844 extdyn
< sdyn
->contents
+ sdyn
->size
;
2845 extdyn
+= bed
->s
->sizeof_dyn
)
2847 Elf_Internal_Dyn dyn
;
2849 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2850 if (dyn
.d_tag
== DT_NEEDED
2851 && dyn
.d_un
.d_val
== strindex
)
2853 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2861 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2864 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2868 /* We were just checking for existence of the tag. */
2869 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2874 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2875 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2876 references from regular objects to these symbols.
2878 ??? Should we do something about references from other dynamic
2879 obects? If not, we potentially lose some warnings about undefined
2880 symbols. But how can we recover the initial undefined / undefweak
2883 struct elf_smash_syms_data
2886 struct elf_link_hash_table
*htab
;
2887 bfd_boolean twiddled
;
2891 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2893 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2894 struct bfd_link_hash_entry
*bh
;
2896 switch (h
->root
.type
)
2899 case bfd_link_hash_new
:
2902 case bfd_link_hash_undefined
:
2903 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2905 if (h
->root
.u
.undef
.weak
!= NULL
2906 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2908 /* Symbol was undefweak in u.undef.weak bfd, and has become
2909 undefined in as-needed lib. Restore weak. */
2910 h
->root
.type
= bfd_link_hash_undefweak
;
2911 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2912 if (h
->root
.u
.undef
.next
!= NULL
2913 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2914 inf
->twiddled
= TRUE
;
2919 case bfd_link_hash_undefweak
:
2920 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2924 case bfd_link_hash_defined
:
2925 case bfd_link_hash_defweak
:
2926 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2930 case bfd_link_hash_common
:
2931 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2935 case bfd_link_hash_warning
:
2936 case bfd_link_hash_indirect
:
2937 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2938 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2940 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2945 /* There is no way we can undo symbol table state from defined or
2946 defweak back to undefined. */
2950 /* Set sym back to newly created state, but keep undef.next if it is
2951 being used as a list pointer. */
2952 bh
= h
->root
.u
.undef
.next
;
2955 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2956 inf
->twiddled
= TRUE
;
2957 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2958 &inf
->htab
->root
.table
,
2959 h
->root
.root
.string
);
2960 h
->root
.u
.undef
.next
= bh
;
2961 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2966 /* Sort symbol by value and section. */
2968 elf_sort_symbol (const void *arg1
, const void *arg2
)
2970 const struct elf_link_hash_entry
*h1
;
2971 const struct elf_link_hash_entry
*h2
;
2972 bfd_signed_vma vdiff
;
2974 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2975 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2976 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2978 return vdiff
> 0 ? 1 : -1;
2981 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2983 return sdiff
> 0 ? 1 : -1;
2988 /* This function is used to adjust offsets into .dynstr for
2989 dynamic symbols. This is called via elf_link_hash_traverse. */
2992 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2994 struct elf_strtab_hash
*dynstr
= data
;
2996 if (h
->root
.type
== bfd_link_hash_warning
)
2997 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2999 if (h
->dynindx
!= -1)
3000 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3004 /* Assign string offsets in .dynstr, update all structures referencing
3008 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3010 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3011 struct elf_link_local_dynamic_entry
*entry
;
3012 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3013 bfd
*dynobj
= hash_table
->dynobj
;
3016 const struct elf_backend_data
*bed
;
3019 _bfd_elf_strtab_finalize (dynstr
);
3020 size
= _bfd_elf_strtab_size (dynstr
);
3022 bed
= get_elf_backend_data (dynobj
);
3023 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3024 BFD_ASSERT (sdyn
!= NULL
);
3026 /* Update all .dynamic entries referencing .dynstr strings. */
3027 for (extdyn
= sdyn
->contents
;
3028 extdyn
< sdyn
->contents
+ sdyn
->size
;
3029 extdyn
+= bed
->s
->sizeof_dyn
)
3031 Elf_Internal_Dyn dyn
;
3033 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3037 dyn
.d_un
.d_val
= size
;
3045 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3050 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3053 /* Now update local dynamic symbols. */
3054 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3055 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3056 entry
->isym
.st_name
);
3058 /* And the rest of dynamic symbols. */
3059 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3061 /* Adjust version definitions. */
3062 if (elf_tdata (output_bfd
)->cverdefs
)
3067 Elf_Internal_Verdef def
;
3068 Elf_Internal_Verdaux defaux
;
3070 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3074 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3076 p
+= sizeof (Elf_External_Verdef
);
3077 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3079 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3081 _bfd_elf_swap_verdaux_in (output_bfd
,
3082 (Elf_External_Verdaux
*) p
, &defaux
);
3083 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3085 _bfd_elf_swap_verdaux_out (output_bfd
,
3086 &defaux
, (Elf_External_Verdaux
*) p
);
3087 p
+= sizeof (Elf_External_Verdaux
);
3090 while (def
.vd_next
);
3093 /* Adjust version references. */
3094 if (elf_tdata (output_bfd
)->verref
)
3099 Elf_Internal_Verneed need
;
3100 Elf_Internal_Vernaux needaux
;
3102 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3106 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3108 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3109 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3110 (Elf_External_Verneed
*) p
);
3111 p
+= sizeof (Elf_External_Verneed
);
3112 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3114 _bfd_elf_swap_vernaux_in (output_bfd
,
3115 (Elf_External_Vernaux
*) p
, &needaux
);
3116 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3118 _bfd_elf_swap_vernaux_out (output_bfd
,
3120 (Elf_External_Vernaux
*) p
);
3121 p
+= sizeof (Elf_External_Vernaux
);
3124 while (need
.vn_next
);
3130 /* Add symbols from an ELF object file to the linker hash table. */
3133 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3135 bfd_boolean (*add_symbol_hook
)
3136 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3137 const char **, flagword
*, asection
**, bfd_vma
*);
3138 bfd_boolean (*check_relocs
)
3139 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3140 bfd_boolean (*check_directives
)
3141 (bfd
*, struct bfd_link_info
*);
3142 bfd_boolean collect
;
3143 Elf_Internal_Shdr
*hdr
;
3144 bfd_size_type symcount
;
3145 bfd_size_type extsymcount
;
3146 bfd_size_type extsymoff
;
3147 struct elf_link_hash_entry
**sym_hash
;
3148 bfd_boolean dynamic
;
3149 Elf_External_Versym
*extversym
= NULL
;
3150 Elf_External_Versym
*ever
;
3151 struct elf_link_hash_entry
*weaks
;
3152 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3153 bfd_size_type nondeflt_vers_cnt
= 0;
3154 Elf_Internal_Sym
*isymbuf
= NULL
;
3155 Elf_Internal_Sym
*isym
;
3156 Elf_Internal_Sym
*isymend
;
3157 const struct elf_backend_data
*bed
;
3158 bfd_boolean add_needed
;
3159 struct elf_link_hash_table
* hash_table
;
3162 hash_table
= elf_hash_table (info
);
3164 bed
= get_elf_backend_data (abfd
);
3165 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3166 collect
= bed
->collect
;
3168 if ((abfd
->flags
& DYNAMIC
) == 0)
3174 /* You can't use -r against a dynamic object. Also, there's no
3175 hope of using a dynamic object which does not exactly match
3176 the format of the output file. */
3177 if (info
->relocatable
3178 || !is_elf_hash_table (hash_table
)
3179 || hash_table
->root
.creator
!= abfd
->xvec
)
3181 if (info
->relocatable
)
3182 bfd_set_error (bfd_error_invalid_operation
);
3184 bfd_set_error (bfd_error_wrong_format
);
3189 /* As a GNU extension, any input sections which are named
3190 .gnu.warning.SYMBOL are treated as warning symbols for the given
3191 symbol. This differs from .gnu.warning sections, which generate
3192 warnings when they are included in an output file. */
3193 if (info
->executable
)
3197 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3201 name
= bfd_get_section_name (abfd
, s
);
3202 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3207 name
+= sizeof ".gnu.warning." - 1;
3209 /* If this is a shared object, then look up the symbol
3210 in the hash table. If it is there, and it is already
3211 been defined, then we will not be using the entry
3212 from this shared object, so we don't need to warn.
3213 FIXME: If we see the definition in a regular object
3214 later on, we will warn, but we shouldn't. The only
3215 fix is to keep track of what warnings we are supposed
3216 to emit, and then handle them all at the end of the
3220 struct elf_link_hash_entry
*h
;
3222 h
= elf_link_hash_lookup (hash_table
, name
,
3223 FALSE
, FALSE
, TRUE
);
3225 /* FIXME: What about bfd_link_hash_common? */
3227 && (h
->root
.type
== bfd_link_hash_defined
3228 || h
->root
.type
== bfd_link_hash_defweak
))
3230 /* We don't want to issue this warning. Clobber
3231 the section size so that the warning does not
3232 get copied into the output file. */
3239 msg
= bfd_alloc (abfd
, sz
+ 1);
3243 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3248 if (! (_bfd_generic_link_add_one_symbol
3249 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3250 FALSE
, collect
, NULL
)))
3253 if (! info
->relocatable
)
3255 /* Clobber the section size so that the warning does
3256 not get copied into the output file. */
3259 /* Also set SEC_EXCLUDE, so that symbols defined in
3260 the warning section don't get copied to the output. */
3261 s
->flags
|= SEC_EXCLUDE
;
3270 /* If we are creating a shared library, create all the dynamic
3271 sections immediately. We need to attach them to something,
3272 so we attach them to this BFD, provided it is the right
3273 format. FIXME: If there are no input BFD's of the same
3274 format as the output, we can't make a shared library. */
3276 && is_elf_hash_table (hash_table
)
3277 && hash_table
->root
.creator
== abfd
->xvec
3278 && ! hash_table
->dynamic_sections_created
)
3280 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3284 else if (!is_elf_hash_table (hash_table
))
3289 const char *soname
= NULL
;
3290 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3293 /* ld --just-symbols and dynamic objects don't mix very well.
3294 ld shouldn't allow it. */
3295 if ((s
= abfd
->sections
) != NULL
3296 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3299 /* If this dynamic lib was specified on the command line with
3300 --as-needed in effect, then we don't want to add a DT_NEEDED
3301 tag unless the lib is actually used. Similary for libs brought
3302 in by another lib's DT_NEEDED. When --no-add-needed is used
3303 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3304 any dynamic library in DT_NEEDED tags in the dynamic lib at
3306 add_needed
= (elf_dyn_lib_class (abfd
)
3307 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3308 | DYN_NO_NEEDED
)) == 0;
3310 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3316 unsigned long shlink
;
3318 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3319 goto error_free_dyn
;
3321 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3323 goto error_free_dyn
;
3324 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3326 for (extdyn
= dynbuf
;
3327 extdyn
< dynbuf
+ s
->size
;
3328 extdyn
+= bed
->s
->sizeof_dyn
)
3330 Elf_Internal_Dyn dyn
;
3332 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3333 if (dyn
.d_tag
== DT_SONAME
)
3335 unsigned int tagv
= dyn
.d_un
.d_val
;
3336 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3338 goto error_free_dyn
;
3340 if (dyn
.d_tag
== DT_NEEDED
)
3342 struct bfd_link_needed_list
*n
, **pn
;
3344 unsigned int tagv
= dyn
.d_un
.d_val
;
3346 amt
= sizeof (struct bfd_link_needed_list
);
3347 n
= bfd_alloc (abfd
, amt
);
3348 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3349 if (n
== NULL
|| fnm
== NULL
)
3350 goto error_free_dyn
;
3351 amt
= strlen (fnm
) + 1;
3352 anm
= bfd_alloc (abfd
, amt
);
3354 goto error_free_dyn
;
3355 memcpy (anm
, fnm
, amt
);
3359 for (pn
= & hash_table
->needed
;
3365 if (dyn
.d_tag
== DT_RUNPATH
)
3367 struct bfd_link_needed_list
*n
, **pn
;
3369 unsigned int tagv
= dyn
.d_un
.d_val
;
3371 amt
= sizeof (struct bfd_link_needed_list
);
3372 n
= bfd_alloc (abfd
, amt
);
3373 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3374 if (n
== NULL
|| fnm
== NULL
)
3375 goto error_free_dyn
;
3376 amt
= strlen (fnm
) + 1;
3377 anm
= bfd_alloc (abfd
, amt
);
3379 goto error_free_dyn
;
3380 memcpy (anm
, fnm
, amt
);
3384 for (pn
= & runpath
;
3390 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3391 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3393 struct bfd_link_needed_list
*n
, **pn
;
3395 unsigned int tagv
= dyn
.d_un
.d_val
;
3397 amt
= sizeof (struct bfd_link_needed_list
);
3398 n
= bfd_alloc (abfd
, amt
);
3399 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3400 if (n
== NULL
|| fnm
== NULL
)
3401 goto error_free_dyn
;
3402 amt
= strlen (fnm
) + 1;
3403 anm
= bfd_alloc (abfd
, amt
);
3410 memcpy (anm
, fnm
, amt
);
3425 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3426 frees all more recently bfd_alloc'd blocks as well. */
3432 struct bfd_link_needed_list
**pn
;
3433 for (pn
= & hash_table
->runpath
;
3440 /* We do not want to include any of the sections in a dynamic
3441 object in the output file. We hack by simply clobbering the
3442 list of sections in the BFD. This could be handled more
3443 cleanly by, say, a new section flag; the existing
3444 SEC_NEVER_LOAD flag is not the one we want, because that one
3445 still implies that the section takes up space in the output
3447 bfd_section_list_clear (abfd
);
3449 /* Find the name to use in a DT_NEEDED entry that refers to this
3450 object. If the object has a DT_SONAME entry, we use it.
3451 Otherwise, if the generic linker stuck something in
3452 elf_dt_name, we use that. Otherwise, we just use the file
3454 if (soname
== NULL
|| *soname
== '\0')
3456 soname
= elf_dt_name (abfd
);
3457 if (soname
== NULL
|| *soname
== '\0')
3458 soname
= bfd_get_filename (abfd
);
3461 /* Save the SONAME because sometimes the linker emulation code
3462 will need to know it. */
3463 elf_dt_name (abfd
) = soname
;
3465 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3469 /* If we have already included this dynamic object in the
3470 link, just ignore it. There is no reason to include a
3471 particular dynamic object more than once. */
3476 /* If this is a dynamic object, we always link against the .dynsym
3477 symbol table, not the .symtab symbol table. The dynamic linker
3478 will only see the .dynsym symbol table, so there is no reason to
3479 look at .symtab for a dynamic object. */
3481 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3482 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3484 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3486 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3488 /* The sh_info field of the symtab header tells us where the
3489 external symbols start. We don't care about the local symbols at
3491 if (elf_bad_symtab (abfd
))
3493 extsymcount
= symcount
;
3498 extsymcount
= symcount
- hdr
->sh_info
;
3499 extsymoff
= hdr
->sh_info
;
3503 if (extsymcount
!= 0)
3505 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3507 if (isymbuf
== NULL
)
3510 /* We store a pointer to the hash table entry for each external
3512 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3513 sym_hash
= bfd_alloc (abfd
, amt
);
3514 if (sym_hash
== NULL
)
3515 goto error_free_sym
;
3516 elf_sym_hashes (abfd
) = sym_hash
;
3521 /* Read in any version definitions. */
3522 if (!_bfd_elf_slurp_version_tables (abfd
,
3523 info
->default_imported_symver
))
3524 goto error_free_sym
;
3526 /* Read in the symbol versions, but don't bother to convert them
3527 to internal format. */
3528 if (elf_dynversym (abfd
) != 0)
3530 Elf_Internal_Shdr
*versymhdr
;
3532 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3533 extversym
= bfd_malloc (versymhdr
->sh_size
);
3534 if (extversym
== NULL
)
3535 goto error_free_sym
;
3536 amt
= versymhdr
->sh_size
;
3537 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3538 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3539 goto error_free_vers
;
3545 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3546 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3548 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3552 asection
*sec
, *new_sec
;
3555 struct elf_link_hash_entry
*h
;
3556 bfd_boolean definition
;
3557 bfd_boolean size_change_ok
;
3558 bfd_boolean type_change_ok
;
3559 bfd_boolean new_weakdef
;
3560 bfd_boolean override
;
3562 unsigned int old_alignment
;
3567 flags
= BSF_NO_FLAGS
;
3569 value
= isym
->st_value
;
3571 common
= bed
->common_definition (isym
);
3573 bind
= ELF_ST_BIND (isym
->st_info
);
3574 if (bind
== STB_LOCAL
)
3576 /* This should be impossible, since ELF requires that all
3577 global symbols follow all local symbols, and that sh_info
3578 point to the first global symbol. Unfortunately, Irix 5
3582 else if (bind
== STB_GLOBAL
)
3584 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3587 else if (bind
== STB_WEAK
)
3591 /* Leave it up to the processor backend. */
3594 if (isym
->st_shndx
== SHN_UNDEF
)
3595 sec
= bfd_und_section_ptr
;
3596 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3598 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3600 sec
= bfd_abs_section_ptr
;
3601 else if (sec
->kept_section
)
3603 /* Symbols from discarded section are undefined, and have
3604 default visibility. */
3605 sec
= bfd_und_section_ptr
;
3606 isym
->st_shndx
= SHN_UNDEF
;
3607 isym
->st_other
= STV_DEFAULT
3608 | (isym
->st_other
& ~ ELF_ST_VISIBILITY(-1));
3610 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3613 else if (isym
->st_shndx
== SHN_ABS
)
3614 sec
= bfd_abs_section_ptr
;
3615 else if (isym
->st_shndx
== SHN_COMMON
)
3617 sec
= bfd_com_section_ptr
;
3618 /* What ELF calls the size we call the value. What ELF
3619 calls the value we call the alignment. */
3620 value
= isym
->st_size
;
3624 /* Leave it up to the processor backend. */
3627 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3630 goto error_free_vers
;
3632 if (isym
->st_shndx
== SHN_COMMON
3633 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3635 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3639 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3642 | SEC_LINKER_CREATED
3643 | SEC_THREAD_LOCAL
));
3645 goto error_free_vers
;
3649 else if (add_symbol_hook
)
3651 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3653 goto error_free_vers
;
3655 /* The hook function sets the name to NULL if this symbol
3656 should be skipped for some reason. */
3661 /* Sanity check that all possibilities were handled. */
3664 bfd_set_error (bfd_error_bad_value
);
3665 goto error_free_vers
;
3668 if (bfd_is_und_section (sec
)
3669 || bfd_is_com_section (sec
))
3674 size_change_ok
= FALSE
;
3675 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3680 if (is_elf_hash_table (hash_table
))
3682 Elf_Internal_Versym iver
;
3683 unsigned int vernum
= 0;
3688 if (info
->default_imported_symver
)
3689 /* Use the default symbol version created earlier. */
3690 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3695 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3697 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3699 /* If this is a hidden symbol, or if it is not version
3700 1, we append the version name to the symbol name.
3701 However, we do not modify a non-hidden absolute symbol
3702 if it is not a function, because it might be the version
3703 symbol itself. FIXME: What if it isn't? */
3704 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3705 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3706 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3709 size_t namelen
, verlen
, newlen
;
3712 if (isym
->st_shndx
!= SHN_UNDEF
)
3714 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3716 else if (vernum
> 1)
3718 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3724 (*_bfd_error_handler
)
3725 (_("%B: %s: invalid version %u (max %d)"),
3727 elf_tdata (abfd
)->cverdefs
);
3728 bfd_set_error (bfd_error_bad_value
);
3729 goto error_free_vers
;
3734 /* We cannot simply test for the number of
3735 entries in the VERNEED section since the
3736 numbers for the needed versions do not start
3738 Elf_Internal_Verneed
*t
;
3741 for (t
= elf_tdata (abfd
)->verref
;
3745 Elf_Internal_Vernaux
*a
;
3747 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3749 if (a
->vna_other
== vernum
)
3751 verstr
= a
->vna_nodename
;
3760 (*_bfd_error_handler
)
3761 (_("%B: %s: invalid needed version %d"),
3762 abfd
, name
, vernum
);
3763 bfd_set_error (bfd_error_bad_value
);
3764 goto error_free_vers
;
3768 namelen
= strlen (name
);
3769 verlen
= strlen (verstr
);
3770 newlen
= namelen
+ verlen
+ 2;
3771 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3772 && isym
->st_shndx
!= SHN_UNDEF
)
3775 newname
= bfd_alloc (abfd
, newlen
);
3776 if (newname
== NULL
)
3777 goto error_free_vers
;
3778 memcpy (newname
, name
, namelen
);
3779 p
= newname
+ namelen
;
3781 /* If this is a defined non-hidden version symbol,
3782 we add another @ to the name. This indicates the
3783 default version of the symbol. */
3784 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3785 && isym
->st_shndx
!= SHN_UNDEF
)
3787 memcpy (p
, verstr
, verlen
+ 1);
3792 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3793 &value
, &old_alignment
,
3794 sym_hash
, &skip
, &override
,
3795 &type_change_ok
, &size_change_ok
))
3796 goto error_free_vers
;
3805 while (h
->root
.type
== bfd_link_hash_indirect
3806 || h
->root
.type
== bfd_link_hash_warning
)
3807 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3809 /* Remember the old alignment if this is a common symbol, so
3810 that we don't reduce the alignment later on. We can't
3811 check later, because _bfd_generic_link_add_one_symbol
3812 will set a default for the alignment which we want to
3813 override. We also remember the old bfd where the existing
3814 definition comes from. */
3815 switch (h
->root
.type
)
3820 case bfd_link_hash_defined
:
3821 case bfd_link_hash_defweak
:
3822 old_bfd
= h
->root
.u
.def
.section
->owner
;
3825 case bfd_link_hash_common
:
3826 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3827 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3831 if (elf_tdata (abfd
)->verdef
!= NULL
3835 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3838 if (! (_bfd_generic_link_add_one_symbol
3839 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3840 (struct bfd_link_hash_entry
**) sym_hash
)))
3841 goto error_free_vers
;
3844 while (h
->root
.type
== bfd_link_hash_indirect
3845 || h
->root
.type
== bfd_link_hash_warning
)
3846 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3849 new_weakdef
= FALSE
;
3852 && (flags
& BSF_WEAK
) != 0
3853 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3854 && is_elf_hash_table (hash_table
)
3855 && h
->u
.weakdef
== NULL
)
3857 /* Keep a list of all weak defined non function symbols from
3858 a dynamic object, using the weakdef field. Later in this
3859 function we will set the weakdef field to the correct
3860 value. We only put non-function symbols from dynamic
3861 objects on this list, because that happens to be the only
3862 time we need to know the normal symbol corresponding to a
3863 weak symbol, and the information is time consuming to
3864 figure out. If the weakdef field is not already NULL,
3865 then this symbol was already defined by some previous
3866 dynamic object, and we will be using that previous
3867 definition anyhow. */
3869 h
->u
.weakdef
= weaks
;
3874 /* Set the alignment of a common symbol. */
3875 if ((common
|| bfd_is_com_section (sec
))
3876 && h
->root
.type
== bfd_link_hash_common
)
3881 align
= bfd_log2 (isym
->st_value
);
3884 /* The new symbol is a common symbol in a shared object.
3885 We need to get the alignment from the section. */
3886 align
= new_sec
->alignment_power
;
3888 if (align
> old_alignment
3889 /* Permit an alignment power of zero if an alignment of one
3890 is specified and no other alignments have been specified. */
3891 || (isym
->st_value
== 1 && old_alignment
== 0))
3892 h
->root
.u
.c
.p
->alignment_power
= align
;
3894 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3897 if (is_elf_hash_table (hash_table
))
3901 /* Check the alignment when a common symbol is involved. This
3902 can change when a common symbol is overridden by a normal
3903 definition or a common symbol is ignored due to the old
3904 normal definition. We need to make sure the maximum
3905 alignment is maintained. */
3906 if ((old_alignment
|| common
)
3907 && h
->root
.type
!= bfd_link_hash_common
)
3909 unsigned int common_align
;
3910 unsigned int normal_align
;
3911 unsigned int symbol_align
;
3915 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3916 if (h
->root
.u
.def
.section
->owner
!= NULL
3917 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3919 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3920 if (normal_align
> symbol_align
)
3921 normal_align
= symbol_align
;
3924 normal_align
= symbol_align
;
3928 common_align
= old_alignment
;
3929 common_bfd
= old_bfd
;
3934 common_align
= bfd_log2 (isym
->st_value
);
3936 normal_bfd
= old_bfd
;
3939 if (normal_align
< common_align
)
3940 (*_bfd_error_handler
)
3941 (_("Warning: alignment %u of symbol `%s' in %B"
3942 " is smaller than %u in %B"),
3943 normal_bfd
, common_bfd
,
3944 1 << normal_align
, name
, 1 << common_align
);
3947 /* Remember the symbol size and type. */
3948 if (isym
->st_size
!= 0
3949 && (definition
|| h
->size
== 0))
3951 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3952 (*_bfd_error_handler
)
3953 (_("Warning: size of symbol `%s' changed"
3954 " from %lu in %B to %lu in %B"),
3956 name
, (unsigned long) h
->size
,
3957 (unsigned long) isym
->st_size
);
3959 h
->size
= isym
->st_size
;
3962 /* If this is a common symbol, then we always want H->SIZE
3963 to be the size of the common symbol. The code just above
3964 won't fix the size if a common symbol becomes larger. We
3965 don't warn about a size change here, because that is
3966 covered by --warn-common. */
3967 if (h
->root
.type
== bfd_link_hash_common
)
3968 h
->size
= h
->root
.u
.c
.size
;
3970 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3971 && (definition
|| h
->type
== STT_NOTYPE
))
3973 if (h
->type
!= STT_NOTYPE
3974 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3975 && ! type_change_ok
)
3976 (*_bfd_error_handler
)
3977 (_("Warning: type of symbol `%s' changed"
3978 " from %d to %d in %B"),
3979 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3981 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3984 /* If st_other has a processor-specific meaning, specific
3985 code might be needed here. We never merge the visibility
3986 attribute with the one from a dynamic object. */
3987 if (bed
->elf_backend_merge_symbol_attribute
)
3988 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3991 /* If this symbol has default visibility and the user has requested
3992 we not re-export it, then mark it as hidden. */
3993 if (definition
&& !dynamic
3995 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3996 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3997 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3999 if (isym
->st_other
!= 0 && !dynamic
)
4001 unsigned char hvis
, symvis
, other
, nvis
;
4003 /* Take the balance of OTHER from the definition. */
4004 other
= (definition
? isym
->st_other
: h
->other
);
4005 other
&= ~ ELF_ST_VISIBILITY (-1);
4007 /* Combine visibilities, using the most constraining one. */
4008 hvis
= ELF_ST_VISIBILITY (h
->other
);
4009 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4015 nvis
= hvis
< symvis
? hvis
: symvis
;
4017 h
->other
= other
| nvis
;
4020 /* Set a flag in the hash table entry indicating the type of
4021 reference or definition we just found. Keep a count of
4022 the number of dynamic symbols we find. A dynamic symbol
4023 is one which is referenced or defined by both a regular
4024 object and a shared object. */
4031 if (bind
!= STB_WEAK
)
4032 h
->ref_regular_nonweak
= 1;
4036 if (! info
->executable
4049 || (h
->u
.weakdef
!= NULL
4051 && h
->u
.weakdef
->dynindx
!= -1))
4055 /* Check to see if we need to add an indirect symbol for
4056 the default name. */
4057 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4058 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4059 &sec
, &value
, &dynsym
,
4061 goto error_free_vers
;
4063 if (definition
&& !dynamic
)
4065 char *p
= strchr (name
, ELF_VER_CHR
);
4066 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4068 /* Queue non-default versions so that .symver x, x@FOO
4069 aliases can be checked. */
4070 if (! nondeflt_vers
)
4072 amt
= (isymend
- isym
+ 1)
4073 * sizeof (struct elf_link_hash_entry
*);
4074 nondeflt_vers
= bfd_malloc (amt
);
4076 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4080 if (dynsym
&& h
->dynindx
== -1)
4082 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4083 goto error_free_vers
;
4084 if (h
->u
.weakdef
!= NULL
4086 && h
->u
.weakdef
->dynindx
== -1)
4088 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4089 goto error_free_vers
;
4092 else if (dynsym
&& h
->dynindx
!= -1)
4093 /* If the symbol already has a dynamic index, but
4094 visibility says it should not be visible, turn it into
4096 switch (ELF_ST_VISIBILITY (h
->other
))
4100 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4111 const char *soname
= elf_dt_name (abfd
);
4113 /* A symbol from a library loaded via DT_NEEDED of some
4114 other library is referenced by a regular object.
4115 Add a DT_NEEDED entry for it. Issue an error if
4116 --no-add-needed is used. */
4117 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4119 (*_bfd_error_handler
)
4120 (_("%s: invalid DSO for symbol `%s' definition"),
4122 bfd_set_error (bfd_error_bad_value
);
4123 goto error_free_vers
;
4126 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4129 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4131 goto error_free_vers
;
4133 BFD_ASSERT (ret
== 0);
4138 /* Now that all the symbols from this input file are created, handle
4139 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4140 if (nondeflt_vers
!= NULL
)
4142 bfd_size_type cnt
, symidx
;
4144 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4146 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4147 char *shortname
, *p
;
4149 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4151 || (h
->root
.type
!= bfd_link_hash_defined
4152 && h
->root
.type
!= bfd_link_hash_defweak
))
4155 amt
= p
- h
->root
.root
.string
;
4156 shortname
= bfd_malloc (amt
+ 1);
4157 memcpy (shortname
, h
->root
.root
.string
, amt
);
4158 shortname
[amt
] = '\0';
4160 hi
= (struct elf_link_hash_entry
*)
4161 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4162 FALSE
, FALSE
, FALSE
);
4164 && hi
->root
.type
== h
->root
.type
4165 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4166 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4168 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4169 hi
->root
.type
= bfd_link_hash_indirect
;
4170 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4171 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4172 sym_hash
= elf_sym_hashes (abfd
);
4174 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4175 if (sym_hash
[symidx
] == hi
)
4177 sym_hash
[symidx
] = h
;
4183 free (nondeflt_vers
);
4184 nondeflt_vers
= NULL
;
4187 if (extversym
!= NULL
)
4193 if (isymbuf
!= NULL
)
4198 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4200 /* Remove symbols defined in an as-needed shared lib that wasn't
4202 struct elf_smash_syms_data inf
;
4203 inf
.not_needed
= abfd
;
4204 inf
.htab
= hash_table
;
4205 inf
.twiddled
= FALSE
;
4206 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4208 bfd_link_repair_undef_list (&hash_table
->root
);
4212 /* Now set the weakdefs field correctly for all the weak defined
4213 symbols we found. The only way to do this is to search all the
4214 symbols. Since we only need the information for non functions in
4215 dynamic objects, that's the only time we actually put anything on
4216 the list WEAKS. We need this information so that if a regular
4217 object refers to a symbol defined weakly in a dynamic object, the
4218 real symbol in the dynamic object is also put in the dynamic
4219 symbols; we also must arrange for both symbols to point to the
4220 same memory location. We could handle the general case of symbol
4221 aliasing, but a general symbol alias can only be generated in
4222 assembler code, handling it correctly would be very time
4223 consuming, and other ELF linkers don't handle general aliasing
4227 struct elf_link_hash_entry
**hpp
;
4228 struct elf_link_hash_entry
**hppend
;
4229 struct elf_link_hash_entry
**sorted_sym_hash
;
4230 struct elf_link_hash_entry
*h
;
4233 /* Since we have to search the whole symbol list for each weak
4234 defined symbol, search time for N weak defined symbols will be
4235 O(N^2). Binary search will cut it down to O(NlogN). */
4236 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4237 sorted_sym_hash
= bfd_malloc (amt
);
4238 if (sorted_sym_hash
== NULL
)
4240 sym_hash
= sorted_sym_hash
;
4241 hpp
= elf_sym_hashes (abfd
);
4242 hppend
= hpp
+ extsymcount
;
4244 for (; hpp
< hppend
; hpp
++)
4248 && h
->root
.type
== bfd_link_hash_defined
4249 && h
->type
!= STT_FUNC
)
4257 qsort (sorted_sym_hash
, sym_count
,
4258 sizeof (struct elf_link_hash_entry
*),
4261 while (weaks
!= NULL
)
4263 struct elf_link_hash_entry
*hlook
;
4270 weaks
= hlook
->u
.weakdef
;
4271 hlook
->u
.weakdef
= NULL
;
4273 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4274 || hlook
->root
.type
== bfd_link_hash_defweak
4275 || hlook
->root
.type
== bfd_link_hash_common
4276 || hlook
->root
.type
== bfd_link_hash_indirect
);
4277 slook
= hlook
->root
.u
.def
.section
;
4278 vlook
= hlook
->root
.u
.def
.value
;
4285 bfd_signed_vma vdiff
;
4287 h
= sorted_sym_hash
[idx
];
4288 vdiff
= vlook
- h
->root
.u
.def
.value
;
4295 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4308 /* We didn't find a value/section match. */
4312 for (i
= ilook
; i
< sym_count
; i
++)
4314 h
= sorted_sym_hash
[i
];
4316 /* Stop if value or section doesn't match. */
4317 if (h
->root
.u
.def
.value
!= vlook
4318 || h
->root
.u
.def
.section
!= slook
)
4320 else if (h
!= hlook
)
4322 hlook
->u
.weakdef
= h
;
4324 /* If the weak definition is in the list of dynamic
4325 symbols, make sure the real definition is put
4327 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4329 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4333 /* If the real definition is in the list of dynamic
4334 symbols, make sure the weak definition is put
4335 there as well. If we don't do this, then the
4336 dynamic loader might not merge the entries for the
4337 real definition and the weak definition. */
4338 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4340 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4348 free (sorted_sym_hash
);
4351 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4352 if (check_directives
)
4353 check_directives (abfd
, info
);
4355 /* If this object is the same format as the output object, and it is
4356 not a shared library, then let the backend look through the
4359 This is required to build global offset table entries and to
4360 arrange for dynamic relocs. It is not required for the
4361 particular common case of linking non PIC code, even when linking
4362 against shared libraries, but unfortunately there is no way of
4363 knowing whether an object file has been compiled PIC or not.
4364 Looking through the relocs is not particularly time consuming.
4365 The problem is that we must either (1) keep the relocs in memory,
4366 which causes the linker to require additional runtime memory or
4367 (2) read the relocs twice from the input file, which wastes time.
4368 This would be a good case for using mmap.
4370 I have no idea how to handle linking PIC code into a file of a
4371 different format. It probably can't be done. */
4372 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4374 && is_elf_hash_table (hash_table
)
4375 && hash_table
->root
.creator
== abfd
->xvec
4376 && check_relocs
!= NULL
)
4380 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4382 Elf_Internal_Rela
*internal_relocs
;
4385 if ((o
->flags
& SEC_RELOC
) == 0
4386 || o
->reloc_count
== 0
4387 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4388 && (o
->flags
& SEC_DEBUGGING
) != 0)
4389 || bfd_is_abs_section (o
->output_section
))
4392 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4394 if (internal_relocs
== NULL
)
4397 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4399 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4400 free (internal_relocs
);
4407 /* If this is a non-traditional link, try to optimize the handling
4408 of the .stab/.stabstr sections. */
4410 && ! info
->traditional_format
4411 && is_elf_hash_table (hash_table
)
4412 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4416 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4417 if (stabstr
!= NULL
)
4419 bfd_size_type string_offset
= 0;
4422 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4423 if (strncmp (".stab", stab
->name
, 5) == 0
4424 && (!stab
->name
[5] ||
4425 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4426 && (stab
->flags
& SEC_MERGE
) == 0
4427 && !bfd_is_abs_section (stab
->output_section
))
4429 struct bfd_elf_section_data
*secdata
;
4431 secdata
= elf_section_data (stab
);
4432 if (! _bfd_link_section_stabs (abfd
,
4433 &hash_table
->stab_info
,
4438 if (secdata
->sec_info
)
4439 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4444 if (is_elf_hash_table (hash_table
) && add_needed
)
4446 /* Add this bfd to the loaded list. */
4447 struct elf_link_loaded_list
*n
;
4449 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4453 n
->next
= hash_table
->loaded
;
4454 hash_table
->loaded
= n
;
4460 if (nondeflt_vers
!= NULL
)
4461 free (nondeflt_vers
);
4462 if (extversym
!= NULL
)
4465 if (isymbuf
!= NULL
)
4471 /* Return the linker hash table entry of a symbol that might be
4472 satisfied by an archive symbol. Return -1 on error. */
4474 struct elf_link_hash_entry
*
4475 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4476 struct bfd_link_info
*info
,
4479 struct elf_link_hash_entry
*h
;
4483 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4487 /* If this is a default version (the name contains @@), look up the
4488 symbol again with only one `@' as well as without the version.
4489 The effect is that references to the symbol with and without the
4490 version will be matched by the default symbol in the archive. */
4492 p
= strchr (name
, ELF_VER_CHR
);
4493 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4496 /* First check with only one `@'. */
4497 len
= strlen (name
);
4498 copy
= bfd_alloc (abfd
, len
);
4500 return (struct elf_link_hash_entry
*) 0 - 1;
4502 first
= p
- name
+ 1;
4503 memcpy (copy
, name
, first
);
4504 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4506 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4509 /* We also need to check references to the symbol without the
4511 copy
[first
- 1] = '\0';
4512 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4513 FALSE
, FALSE
, FALSE
);
4516 bfd_release (abfd
, copy
);
4520 /* Add symbols from an ELF archive file to the linker hash table. We
4521 don't use _bfd_generic_link_add_archive_symbols because of a
4522 problem which arises on UnixWare. The UnixWare libc.so is an
4523 archive which includes an entry libc.so.1 which defines a bunch of
4524 symbols. The libc.so archive also includes a number of other
4525 object files, which also define symbols, some of which are the same
4526 as those defined in libc.so.1. Correct linking requires that we
4527 consider each object file in turn, and include it if it defines any
4528 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4529 this; it looks through the list of undefined symbols, and includes
4530 any object file which defines them. When this algorithm is used on
4531 UnixWare, it winds up pulling in libc.so.1 early and defining a
4532 bunch of symbols. This means that some of the other objects in the
4533 archive are not included in the link, which is incorrect since they
4534 precede libc.so.1 in the archive.
4536 Fortunately, ELF archive handling is simpler than that done by
4537 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4538 oddities. In ELF, if we find a symbol in the archive map, and the
4539 symbol is currently undefined, we know that we must pull in that
4542 Unfortunately, we do have to make multiple passes over the symbol
4543 table until nothing further is resolved. */
4546 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4549 bfd_boolean
*defined
= NULL
;
4550 bfd_boolean
*included
= NULL
;
4554 const struct elf_backend_data
*bed
;
4555 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4556 (bfd
*, struct bfd_link_info
*, const char *);
4558 if (! bfd_has_map (abfd
))
4560 /* An empty archive is a special case. */
4561 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4563 bfd_set_error (bfd_error_no_armap
);
4567 /* Keep track of all symbols we know to be already defined, and all
4568 files we know to be already included. This is to speed up the
4569 second and subsequent passes. */
4570 c
= bfd_ardata (abfd
)->symdef_count
;
4574 amt
*= sizeof (bfd_boolean
);
4575 defined
= bfd_zmalloc (amt
);
4576 included
= bfd_zmalloc (amt
);
4577 if (defined
== NULL
|| included
== NULL
)
4580 symdefs
= bfd_ardata (abfd
)->symdefs
;
4581 bed
= get_elf_backend_data (abfd
);
4582 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4595 symdefend
= symdef
+ c
;
4596 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4598 struct elf_link_hash_entry
*h
;
4600 struct bfd_link_hash_entry
*undefs_tail
;
4603 if (defined
[i
] || included
[i
])
4605 if (symdef
->file_offset
== last
)
4611 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4612 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4618 if (h
->root
.type
== bfd_link_hash_common
)
4620 /* We currently have a common symbol. The archive map contains
4621 a reference to this symbol, so we may want to include it. We
4622 only want to include it however, if this archive element
4623 contains a definition of the symbol, not just another common
4626 Unfortunately some archivers (including GNU ar) will put
4627 declarations of common symbols into their archive maps, as
4628 well as real definitions, so we cannot just go by the archive
4629 map alone. Instead we must read in the element's symbol
4630 table and check that to see what kind of symbol definition
4632 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4635 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4637 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4642 /* We need to include this archive member. */
4643 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4644 if (element
== NULL
)
4647 if (! bfd_check_format (element
, bfd_object
))
4650 /* Doublecheck that we have not included this object
4651 already--it should be impossible, but there may be
4652 something wrong with the archive. */
4653 if (element
->archive_pass
!= 0)
4655 bfd_set_error (bfd_error_bad_value
);
4658 element
->archive_pass
= 1;
4660 undefs_tail
= info
->hash
->undefs_tail
;
4662 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4665 if (! bfd_link_add_symbols (element
, info
))
4668 /* If there are any new undefined symbols, we need to make
4669 another pass through the archive in order to see whether
4670 they can be defined. FIXME: This isn't perfect, because
4671 common symbols wind up on undefs_tail and because an
4672 undefined symbol which is defined later on in this pass
4673 does not require another pass. This isn't a bug, but it
4674 does make the code less efficient than it could be. */
4675 if (undefs_tail
!= info
->hash
->undefs_tail
)
4678 /* Look backward to mark all symbols from this object file
4679 which we have already seen in this pass. */
4683 included
[mark
] = TRUE
;
4688 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4690 /* We mark subsequent symbols from this object file as we go
4691 on through the loop. */
4692 last
= symdef
->file_offset
;
4703 if (defined
!= NULL
)
4705 if (included
!= NULL
)
4710 /* Given an ELF BFD, add symbols to the global hash table as
4714 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4716 switch (bfd_get_format (abfd
))
4719 return elf_link_add_object_symbols (abfd
, info
);
4721 return elf_link_add_archive_symbols (abfd
, info
);
4723 bfd_set_error (bfd_error_wrong_format
);
4728 /* This function will be called though elf_link_hash_traverse to store
4729 all hash value of the exported symbols in an array. */
4732 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4734 unsigned long **valuep
= data
;
4740 if (h
->root
.type
== bfd_link_hash_warning
)
4741 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4743 /* Ignore indirect symbols. These are added by the versioning code. */
4744 if (h
->dynindx
== -1)
4747 name
= h
->root
.root
.string
;
4748 p
= strchr (name
, ELF_VER_CHR
);
4751 alc
= bfd_malloc (p
- name
+ 1);
4752 memcpy (alc
, name
, p
- name
);
4753 alc
[p
- name
] = '\0';
4757 /* Compute the hash value. */
4758 ha
= bfd_elf_hash (name
);
4760 /* Store the found hash value in the array given as the argument. */
4763 /* And store it in the struct so that we can put it in the hash table
4765 h
->u
.elf_hash_value
= ha
;
4773 /* Array used to determine the number of hash table buckets to use
4774 based on the number of symbols there are. If there are fewer than
4775 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4776 fewer than 37 we use 17 buckets, and so forth. We never use more
4777 than 32771 buckets. */
4779 static const size_t elf_buckets
[] =
4781 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4785 /* Compute bucket count for hashing table. We do not use a static set
4786 of possible tables sizes anymore. Instead we determine for all
4787 possible reasonable sizes of the table the outcome (i.e., the
4788 number of collisions etc) and choose the best solution. The
4789 weighting functions are not too simple to allow the table to grow
4790 without bounds. Instead one of the weighting factors is the size.
4791 Therefore the result is always a good payoff between few collisions
4792 (= short chain lengths) and table size. */
4794 compute_bucket_count (struct bfd_link_info
*info
)
4796 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4797 size_t best_size
= 0;
4798 unsigned long int *hashcodes
;
4799 unsigned long int *hashcodesp
;
4800 unsigned long int i
;
4803 /* Compute the hash values for all exported symbols. At the same
4804 time store the values in an array so that we could use them for
4807 amt
*= sizeof (unsigned long int);
4808 hashcodes
= bfd_malloc (amt
);
4809 if (hashcodes
== NULL
)
4811 hashcodesp
= hashcodes
;
4813 /* Put all hash values in HASHCODES. */
4814 elf_link_hash_traverse (elf_hash_table (info
),
4815 elf_collect_hash_codes
, &hashcodesp
);
4817 /* We have a problem here. The following code to optimize the table
4818 size requires an integer type with more the 32 bits. If
4819 BFD_HOST_U_64_BIT is set we know about such a type. */
4820 #ifdef BFD_HOST_U_64_BIT
4823 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4826 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4827 unsigned long int *counts
;
4828 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4829 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4831 /* Possible optimization parameters: if we have NSYMS symbols we say
4832 that the hashing table must at least have NSYMS/4 and at most
4834 minsize
= nsyms
/ 4;
4837 best_size
= maxsize
= nsyms
* 2;
4839 /* Create array where we count the collisions in. We must use bfd_malloc
4840 since the size could be large. */
4842 amt
*= sizeof (unsigned long int);
4843 counts
= bfd_malloc (amt
);
4850 /* Compute the "optimal" size for the hash table. The criteria is a
4851 minimal chain length. The minor criteria is (of course) the size
4853 for (i
= minsize
; i
< maxsize
; ++i
)
4855 /* Walk through the array of hashcodes and count the collisions. */
4856 BFD_HOST_U_64_BIT max
;
4857 unsigned long int j
;
4858 unsigned long int fact
;
4860 memset (counts
, '\0', i
* sizeof (unsigned long int));
4862 /* Determine how often each hash bucket is used. */
4863 for (j
= 0; j
< nsyms
; ++j
)
4864 ++counts
[hashcodes
[j
] % i
];
4866 /* For the weight function we need some information about the
4867 pagesize on the target. This is information need not be 100%
4868 accurate. Since this information is not available (so far) we
4869 define it here to a reasonable default value. If it is crucial
4870 to have a better value some day simply define this value. */
4871 # ifndef BFD_TARGET_PAGESIZE
4872 # define BFD_TARGET_PAGESIZE (4096)
4875 /* We in any case need 2 + NSYMS entries for the size values and
4877 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4880 /* Variant 1: optimize for short chains. We add the squares
4881 of all the chain lengths (which favors many small chain
4882 over a few long chains). */
4883 for (j
= 0; j
< i
; ++j
)
4884 max
+= counts
[j
] * counts
[j
];
4886 /* This adds penalties for the overall size of the table. */
4887 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4890 /* Variant 2: Optimize a lot more for small table. Here we
4891 also add squares of the size but we also add penalties for
4892 empty slots (the +1 term). */
4893 for (j
= 0; j
< i
; ++j
)
4894 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4896 /* The overall size of the table is considered, but not as
4897 strong as in variant 1, where it is squared. */
4898 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4902 /* Compare with current best results. */
4903 if (max
< best_chlen
)
4913 #endif /* defined (BFD_HOST_U_64_BIT) */
4915 /* This is the fallback solution if no 64bit type is available or if we
4916 are not supposed to spend much time on optimizations. We select the
4917 bucket count using a fixed set of numbers. */
4918 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4920 best_size
= elf_buckets
[i
];
4921 if (dynsymcount
< elf_buckets
[i
+ 1])
4926 /* Free the arrays we needed. */
4932 /* Set up the sizes and contents of the ELF dynamic sections. This is
4933 called by the ELF linker emulation before_allocation routine. We
4934 must set the sizes of the sections before the linker sets the
4935 addresses of the various sections. */
4938 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4941 const char *filter_shlib
,
4942 const char * const *auxiliary_filters
,
4943 struct bfd_link_info
*info
,
4944 asection
**sinterpptr
,
4945 struct bfd_elf_version_tree
*verdefs
)
4947 bfd_size_type soname_indx
;
4949 const struct elf_backend_data
*bed
;
4950 struct elf_assign_sym_version_info asvinfo
;
4954 soname_indx
= (bfd_size_type
) -1;
4956 if (!is_elf_hash_table (info
->hash
))
4959 elf_tdata (output_bfd
)->relro
= info
->relro
;
4960 if (info
->execstack
)
4961 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4962 else if (info
->noexecstack
)
4963 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4967 asection
*notesec
= NULL
;
4970 for (inputobj
= info
->input_bfds
;
4972 inputobj
= inputobj
->link_next
)
4976 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4978 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4981 if (s
->flags
& SEC_CODE
)
4990 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4991 if (exec
&& info
->relocatable
4992 && notesec
->output_section
!= bfd_abs_section_ptr
)
4993 notesec
->output_section
->flags
|= SEC_CODE
;
4997 /* Any syms created from now on start with -1 in
4998 got.refcount/offset and plt.refcount/offset. */
4999 elf_hash_table (info
)->init_got_refcount
5000 = elf_hash_table (info
)->init_got_offset
;
5001 elf_hash_table (info
)->init_plt_refcount
5002 = elf_hash_table (info
)->init_plt_offset
;
5004 /* The backend may have to create some sections regardless of whether
5005 we're dynamic or not. */
5006 bed
= get_elf_backend_data (output_bfd
);
5007 if (bed
->elf_backend_always_size_sections
5008 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5011 dynobj
= elf_hash_table (info
)->dynobj
;
5013 /* If there were no dynamic objects in the link, there is nothing to
5018 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5021 if (elf_hash_table (info
)->dynamic_sections_created
)
5023 struct elf_info_failed eif
;
5024 struct elf_link_hash_entry
*h
;
5026 struct bfd_elf_version_tree
*t
;
5027 struct bfd_elf_version_expr
*d
;
5029 bfd_boolean all_defined
;
5031 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5032 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5036 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5038 if (soname_indx
== (bfd_size_type
) -1
5039 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5045 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5047 info
->flags
|= DF_SYMBOLIC
;
5054 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5056 if (indx
== (bfd_size_type
) -1
5057 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5060 if (info
->new_dtags
)
5062 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5063 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5068 if (filter_shlib
!= NULL
)
5072 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5073 filter_shlib
, TRUE
);
5074 if (indx
== (bfd_size_type
) -1
5075 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5079 if (auxiliary_filters
!= NULL
)
5081 const char * const *p
;
5083 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5087 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5089 if (indx
== (bfd_size_type
) -1
5090 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5096 eif
.verdefs
= verdefs
;
5099 /* If we are supposed to export all symbols into the dynamic symbol
5100 table (this is not the normal case), then do so. */
5101 if (info
->export_dynamic
)
5103 elf_link_hash_traverse (elf_hash_table (info
),
5104 _bfd_elf_export_symbol
,
5110 /* Make all global versions with definition. */
5111 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5112 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5113 if (!d
->symver
&& d
->symbol
)
5115 const char *verstr
, *name
;
5116 size_t namelen
, verlen
, newlen
;
5118 struct elf_link_hash_entry
*newh
;
5121 namelen
= strlen (name
);
5123 verlen
= strlen (verstr
);
5124 newlen
= namelen
+ verlen
+ 3;
5126 newname
= bfd_malloc (newlen
);
5127 if (newname
== NULL
)
5129 memcpy (newname
, name
, namelen
);
5131 /* Check the hidden versioned definition. */
5132 p
= newname
+ namelen
;
5134 memcpy (p
, verstr
, verlen
+ 1);
5135 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5136 newname
, FALSE
, FALSE
,
5139 || (newh
->root
.type
!= bfd_link_hash_defined
5140 && newh
->root
.type
!= bfd_link_hash_defweak
))
5142 /* Check the default versioned definition. */
5144 memcpy (p
, verstr
, verlen
+ 1);
5145 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5146 newname
, FALSE
, FALSE
,
5151 /* Mark this version if there is a definition and it is
5152 not defined in a shared object. */
5154 && !newh
->def_dynamic
5155 && (newh
->root
.type
== bfd_link_hash_defined
5156 || newh
->root
.type
== bfd_link_hash_defweak
))
5160 /* Attach all the symbols to their version information. */
5161 asvinfo
.output_bfd
= output_bfd
;
5162 asvinfo
.info
= info
;
5163 asvinfo
.verdefs
= verdefs
;
5164 asvinfo
.failed
= FALSE
;
5166 elf_link_hash_traverse (elf_hash_table (info
),
5167 _bfd_elf_link_assign_sym_version
,
5172 if (!info
->allow_undefined_version
)
5174 /* Check if all global versions have a definition. */
5176 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5177 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5178 if (!d
->symver
&& !d
->script
)
5180 (*_bfd_error_handler
)
5181 (_("%s: undefined version: %s"),
5182 d
->pattern
, t
->name
);
5183 all_defined
= FALSE
;
5188 bfd_set_error (bfd_error_bad_value
);
5193 /* Find all symbols which were defined in a dynamic object and make
5194 the backend pick a reasonable value for them. */
5195 elf_link_hash_traverse (elf_hash_table (info
),
5196 _bfd_elf_adjust_dynamic_symbol
,
5201 /* Add some entries to the .dynamic section. We fill in some of the
5202 values later, in bfd_elf_final_link, but we must add the entries
5203 now so that we know the final size of the .dynamic section. */
5205 /* If there are initialization and/or finalization functions to
5206 call then add the corresponding DT_INIT/DT_FINI entries. */
5207 h
= (info
->init_function
5208 ? elf_link_hash_lookup (elf_hash_table (info
),
5209 info
->init_function
, FALSE
,
5216 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5219 h
= (info
->fini_function
5220 ? elf_link_hash_lookup (elf_hash_table (info
),
5221 info
->fini_function
, FALSE
,
5228 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5232 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5233 if (s
!= NULL
&& s
->linker_has_input
)
5235 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5236 if (! info
->executable
)
5241 for (sub
= info
->input_bfds
; sub
!= NULL
;
5242 sub
= sub
->link_next
)
5243 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5244 if (elf_section_data (o
)->this_hdr
.sh_type
5245 == SHT_PREINIT_ARRAY
)
5247 (*_bfd_error_handler
)
5248 (_("%B: .preinit_array section is not allowed in DSO"),
5253 bfd_set_error (bfd_error_nonrepresentable_section
);
5257 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5258 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5261 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5262 if (s
!= NULL
&& s
->linker_has_input
)
5264 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5265 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5268 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5269 if (s
!= NULL
&& s
->linker_has_input
)
5271 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5272 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5276 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5277 /* If .dynstr is excluded from the link, we don't want any of
5278 these tags. Strictly, we should be checking each section
5279 individually; This quick check covers for the case where
5280 someone does a /DISCARD/ : { *(*) }. */
5281 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5283 bfd_size_type strsize
;
5285 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5286 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5287 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5288 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5289 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5290 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5291 bed
->s
->sizeof_sym
))
5296 /* The backend must work out the sizes of all the other dynamic
5298 if (bed
->elf_backend_size_dynamic_sections
5299 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5302 if (elf_hash_table (info
)->dynamic_sections_created
)
5304 unsigned long section_sym_count
;
5307 /* Set up the version definition section. */
5308 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5309 BFD_ASSERT (s
!= NULL
);
5311 /* We may have created additional version definitions if we are
5312 just linking a regular application. */
5313 verdefs
= asvinfo
.verdefs
;
5315 /* Skip anonymous version tag. */
5316 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5317 verdefs
= verdefs
->next
;
5319 if (verdefs
== NULL
&& !info
->create_default_symver
)
5320 s
->flags
|= SEC_EXCLUDE
;
5325 struct bfd_elf_version_tree
*t
;
5327 Elf_Internal_Verdef def
;
5328 Elf_Internal_Verdaux defaux
;
5329 struct bfd_link_hash_entry
*bh
;
5330 struct elf_link_hash_entry
*h
;
5336 /* Make space for the base version. */
5337 size
+= sizeof (Elf_External_Verdef
);
5338 size
+= sizeof (Elf_External_Verdaux
);
5341 /* Make space for the default version. */
5342 if (info
->create_default_symver
)
5344 size
+= sizeof (Elf_External_Verdef
);
5348 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5350 struct bfd_elf_version_deps
*n
;
5352 size
+= sizeof (Elf_External_Verdef
);
5353 size
+= sizeof (Elf_External_Verdaux
);
5356 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5357 size
+= sizeof (Elf_External_Verdaux
);
5361 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5362 if (s
->contents
== NULL
&& s
->size
!= 0)
5365 /* Fill in the version definition section. */
5369 def
.vd_version
= VER_DEF_CURRENT
;
5370 def
.vd_flags
= VER_FLG_BASE
;
5373 if (info
->create_default_symver
)
5375 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5376 def
.vd_next
= sizeof (Elf_External_Verdef
);
5380 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5381 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5382 + sizeof (Elf_External_Verdaux
));
5385 if (soname_indx
!= (bfd_size_type
) -1)
5387 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5389 def
.vd_hash
= bfd_elf_hash (soname
);
5390 defaux
.vda_name
= soname_indx
;
5397 name
= lbasename (output_bfd
->filename
);
5398 def
.vd_hash
= bfd_elf_hash (name
);
5399 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5401 if (indx
== (bfd_size_type
) -1)
5403 defaux
.vda_name
= indx
;
5405 defaux
.vda_next
= 0;
5407 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5408 (Elf_External_Verdef
*) p
);
5409 p
+= sizeof (Elf_External_Verdef
);
5410 if (info
->create_default_symver
)
5412 /* Add a symbol representing this version. */
5414 if (! (_bfd_generic_link_add_one_symbol
5415 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5417 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5419 h
= (struct elf_link_hash_entry
*) bh
;
5422 h
->type
= STT_OBJECT
;
5423 h
->verinfo
.vertree
= NULL
;
5425 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5428 /* Create a duplicate of the base version with the same
5429 aux block, but different flags. */
5432 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5434 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5435 + sizeof (Elf_External_Verdaux
));
5438 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5439 (Elf_External_Verdef
*) p
);
5440 p
+= sizeof (Elf_External_Verdef
);
5442 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5443 (Elf_External_Verdaux
*) p
);
5444 p
+= sizeof (Elf_External_Verdaux
);
5446 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5449 struct bfd_elf_version_deps
*n
;
5452 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5455 /* Add a symbol representing this version. */
5457 if (! (_bfd_generic_link_add_one_symbol
5458 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5460 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5462 h
= (struct elf_link_hash_entry
*) bh
;
5465 h
->type
= STT_OBJECT
;
5466 h
->verinfo
.vertree
= t
;
5468 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5471 def
.vd_version
= VER_DEF_CURRENT
;
5473 if (t
->globals
.list
== NULL
5474 && t
->locals
.list
== NULL
5476 def
.vd_flags
|= VER_FLG_WEAK
;
5477 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5478 def
.vd_cnt
= cdeps
+ 1;
5479 def
.vd_hash
= bfd_elf_hash (t
->name
);
5480 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5482 if (t
->next
!= NULL
)
5483 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5484 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5486 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5487 (Elf_External_Verdef
*) p
);
5488 p
+= sizeof (Elf_External_Verdef
);
5490 defaux
.vda_name
= h
->dynstr_index
;
5491 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5493 defaux
.vda_next
= 0;
5494 if (t
->deps
!= NULL
)
5495 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5496 t
->name_indx
= defaux
.vda_name
;
5498 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5499 (Elf_External_Verdaux
*) p
);
5500 p
+= sizeof (Elf_External_Verdaux
);
5502 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5504 if (n
->version_needed
== NULL
)
5506 /* This can happen if there was an error in the
5508 defaux
.vda_name
= 0;
5512 defaux
.vda_name
= n
->version_needed
->name_indx
;
5513 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5516 if (n
->next
== NULL
)
5517 defaux
.vda_next
= 0;
5519 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5521 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5522 (Elf_External_Verdaux
*) p
);
5523 p
+= sizeof (Elf_External_Verdaux
);
5527 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5528 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5531 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5534 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5536 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5539 else if (info
->flags
& DF_BIND_NOW
)
5541 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5547 if (info
->executable
)
5548 info
->flags_1
&= ~ (DF_1_INITFIRST
5551 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5555 /* Work out the size of the version reference section. */
5557 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5558 BFD_ASSERT (s
!= NULL
);
5560 struct elf_find_verdep_info sinfo
;
5562 sinfo
.output_bfd
= output_bfd
;
5564 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5565 if (sinfo
.vers
== 0)
5567 sinfo
.failed
= FALSE
;
5569 elf_link_hash_traverse (elf_hash_table (info
),
5570 _bfd_elf_link_find_version_dependencies
,
5573 if (elf_tdata (output_bfd
)->verref
== NULL
)
5574 s
->flags
|= SEC_EXCLUDE
;
5577 Elf_Internal_Verneed
*t
;
5582 /* Build the version definition section. */
5585 for (t
= elf_tdata (output_bfd
)->verref
;
5589 Elf_Internal_Vernaux
*a
;
5591 size
+= sizeof (Elf_External_Verneed
);
5593 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5594 size
+= sizeof (Elf_External_Vernaux
);
5598 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5599 if (s
->contents
== NULL
)
5603 for (t
= elf_tdata (output_bfd
)->verref
;
5608 Elf_Internal_Vernaux
*a
;
5612 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5615 t
->vn_version
= VER_NEED_CURRENT
;
5617 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5618 elf_dt_name (t
->vn_bfd
) != NULL
5619 ? elf_dt_name (t
->vn_bfd
)
5620 : lbasename (t
->vn_bfd
->filename
),
5622 if (indx
== (bfd_size_type
) -1)
5625 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5626 if (t
->vn_nextref
== NULL
)
5629 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5630 + caux
* sizeof (Elf_External_Vernaux
));
5632 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5633 (Elf_External_Verneed
*) p
);
5634 p
+= sizeof (Elf_External_Verneed
);
5636 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5638 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5639 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5640 a
->vna_nodename
, FALSE
);
5641 if (indx
== (bfd_size_type
) -1)
5644 if (a
->vna_nextptr
== NULL
)
5647 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5649 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5650 (Elf_External_Vernaux
*) p
);
5651 p
+= sizeof (Elf_External_Vernaux
);
5655 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5656 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5659 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5663 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5664 && elf_tdata (output_bfd
)->cverdefs
== 0)
5665 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5666 §ion_sym_count
) == 0)
5668 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5669 s
->flags
|= SEC_EXCLUDE
;
5676 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5678 if (!is_elf_hash_table (info
->hash
))
5681 if (elf_hash_table (info
)->dynamic_sections_created
)
5684 const struct elf_backend_data
*bed
;
5686 bfd_size_type dynsymcount
;
5687 unsigned long section_sym_count
;
5688 size_t bucketcount
= 0;
5689 size_t hash_entry_size
;
5690 unsigned int dtagcount
;
5692 dynobj
= elf_hash_table (info
)->dynobj
;
5694 /* Assign dynsym indicies. In a shared library we generate a
5695 section symbol for each output section, which come first.
5696 Next come all of the back-end allocated local dynamic syms,
5697 followed by the rest of the global symbols. */
5699 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5700 §ion_sym_count
);
5702 /* Work out the size of the symbol version section. */
5703 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5704 BFD_ASSERT (s
!= NULL
);
5705 if (dynsymcount
!= 0
5706 && (s
->flags
& SEC_EXCLUDE
) == 0)
5708 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5709 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5710 if (s
->contents
== NULL
)
5713 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5717 /* Set the size of the .dynsym and .hash sections. We counted
5718 the number of dynamic symbols in elf_link_add_object_symbols.
5719 We will build the contents of .dynsym and .hash when we build
5720 the final symbol table, because until then we do not know the
5721 correct value to give the symbols. We built the .dynstr
5722 section as we went along in elf_link_add_object_symbols. */
5723 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5724 BFD_ASSERT (s
!= NULL
);
5725 bed
= get_elf_backend_data (output_bfd
);
5726 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5728 if (dynsymcount
!= 0)
5730 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5731 if (s
->contents
== NULL
)
5734 /* The first entry in .dynsym is a dummy symbol.
5735 Clear all the section syms, in case we don't output them all. */
5736 ++section_sym_count
;
5737 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5740 /* Compute the size of the hashing table. As a side effect this
5741 computes the hash values for all the names we export. */
5742 bucketcount
= compute_bucket_count (info
);
5744 s
= bfd_get_section_by_name (dynobj
, ".hash");
5745 BFD_ASSERT (s
!= NULL
);
5746 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5747 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5748 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5749 if (s
->contents
== NULL
)
5752 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5753 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5754 s
->contents
+ hash_entry_size
);
5756 elf_hash_table (info
)->bucketcount
= bucketcount
;
5758 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5759 BFD_ASSERT (s
!= NULL
);
5761 elf_finalize_dynstr (output_bfd
, info
);
5763 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5765 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5766 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5773 /* Final phase of ELF linker. */
5775 /* A structure we use to avoid passing large numbers of arguments. */
5777 struct elf_final_link_info
5779 /* General link information. */
5780 struct bfd_link_info
*info
;
5783 /* Symbol string table. */
5784 struct bfd_strtab_hash
*symstrtab
;
5785 /* .dynsym section. */
5786 asection
*dynsym_sec
;
5787 /* .hash section. */
5789 /* symbol version section (.gnu.version). */
5790 asection
*symver_sec
;
5791 /* Buffer large enough to hold contents of any section. */
5793 /* Buffer large enough to hold external relocs of any section. */
5794 void *external_relocs
;
5795 /* Buffer large enough to hold internal relocs of any section. */
5796 Elf_Internal_Rela
*internal_relocs
;
5797 /* Buffer large enough to hold external local symbols of any input
5799 bfd_byte
*external_syms
;
5800 /* And a buffer for symbol section indices. */
5801 Elf_External_Sym_Shndx
*locsym_shndx
;
5802 /* Buffer large enough to hold internal local symbols of any input
5804 Elf_Internal_Sym
*internal_syms
;
5805 /* Array large enough to hold a symbol index for each local symbol
5806 of any input BFD. */
5808 /* Array large enough to hold a section pointer for each local
5809 symbol of any input BFD. */
5810 asection
**sections
;
5811 /* Buffer to hold swapped out symbols. */
5813 /* And one for symbol section indices. */
5814 Elf_External_Sym_Shndx
*symshndxbuf
;
5815 /* Number of swapped out symbols in buffer. */
5816 size_t symbuf_count
;
5817 /* Number of symbols which fit in symbuf. */
5819 /* And same for symshndxbuf. */
5820 size_t shndxbuf_size
;
5823 /* This struct is used to pass information to elf_link_output_extsym. */
5825 struct elf_outext_info
5828 bfd_boolean localsyms
;
5829 struct elf_final_link_info
*finfo
;
5832 /* When performing a relocatable link, the input relocations are
5833 preserved. But, if they reference global symbols, the indices
5834 referenced must be updated. Update all the relocations in
5835 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5838 elf_link_adjust_relocs (bfd
*abfd
,
5839 Elf_Internal_Shdr
*rel_hdr
,
5841 struct elf_link_hash_entry
**rel_hash
)
5844 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5846 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5847 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5848 bfd_vma r_type_mask
;
5851 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5853 swap_in
= bed
->s
->swap_reloc_in
;
5854 swap_out
= bed
->s
->swap_reloc_out
;
5856 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5858 swap_in
= bed
->s
->swap_reloca_in
;
5859 swap_out
= bed
->s
->swap_reloca_out
;
5864 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5867 if (bed
->s
->arch_size
== 32)
5874 r_type_mask
= 0xffffffff;
5878 erela
= rel_hdr
->contents
;
5879 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5881 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5884 if (*rel_hash
== NULL
)
5887 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5889 (*swap_in
) (abfd
, erela
, irela
);
5890 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5891 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5892 | (irela
[j
].r_info
& r_type_mask
));
5893 (*swap_out
) (abfd
, irela
, erela
);
5897 struct elf_link_sort_rela
5903 enum elf_reloc_type_class type
;
5904 /* We use this as an array of size int_rels_per_ext_rel. */
5905 Elf_Internal_Rela rela
[1];
5909 elf_link_sort_cmp1 (const void *A
, const void *B
)
5911 const struct elf_link_sort_rela
*a
= A
;
5912 const struct elf_link_sort_rela
*b
= B
;
5913 int relativea
, relativeb
;
5915 relativea
= a
->type
== reloc_class_relative
;
5916 relativeb
= b
->type
== reloc_class_relative
;
5918 if (relativea
< relativeb
)
5920 if (relativea
> relativeb
)
5922 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5924 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5926 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5928 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5934 elf_link_sort_cmp2 (const void *A
, const void *B
)
5936 const struct elf_link_sort_rela
*a
= A
;
5937 const struct elf_link_sort_rela
*b
= B
;
5940 if (a
->u
.offset
< b
->u
.offset
)
5942 if (a
->u
.offset
> b
->u
.offset
)
5944 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5945 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5950 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5952 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5958 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5961 bfd_size_type count
, size
;
5962 size_t i
, ret
, sort_elt
, ext_size
;
5963 bfd_byte
*sort
, *s_non_relative
, *p
;
5964 struct elf_link_sort_rela
*sq
;
5965 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5966 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5967 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5968 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5969 struct bfd_link_order
*lo
;
5972 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5973 if (reldyn
== NULL
|| reldyn
->size
== 0)
5975 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5976 if (reldyn
== NULL
|| reldyn
->size
== 0)
5978 ext_size
= bed
->s
->sizeof_rel
;
5979 swap_in
= bed
->s
->swap_reloc_in
;
5980 swap_out
= bed
->s
->swap_reloc_out
;
5984 ext_size
= bed
->s
->sizeof_rela
;
5985 swap_in
= bed
->s
->swap_reloca_in
;
5986 swap_out
= bed
->s
->swap_reloca_out
;
5988 count
= reldyn
->size
/ ext_size
;
5991 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5992 if (lo
->type
== bfd_indirect_link_order
)
5994 asection
*o
= lo
->u
.indirect
.section
;
5998 if (size
!= reldyn
->size
)
6001 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6002 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6003 sort
= bfd_zmalloc (sort_elt
* count
);
6006 (*info
->callbacks
->warning
)
6007 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6011 if (bed
->s
->arch_size
== 32)
6012 r_sym_mask
= ~(bfd_vma
) 0xff;
6014 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6016 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6017 if (lo
->type
== bfd_indirect_link_order
)
6019 bfd_byte
*erel
, *erelend
;
6020 asection
*o
= lo
->u
.indirect
.section
;
6022 if (o
->contents
== NULL
&& o
->size
!= 0)
6024 /* This is a reloc section that is being handled as a normal
6025 section. See bfd_section_from_shdr. We can't combine
6026 relocs in this case. */
6031 erelend
= o
->contents
+ o
->size
;
6032 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6033 while (erel
< erelend
)
6035 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6036 (*swap_in
) (abfd
, erel
, s
->rela
);
6037 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6038 s
->u
.sym_mask
= r_sym_mask
;
6044 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6046 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6048 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6049 if (s
->type
!= reloc_class_relative
)
6055 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6056 for (; i
< count
; i
++, p
+= sort_elt
)
6058 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6059 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6061 sp
->u
.offset
= sq
->rela
->r_offset
;
6064 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6066 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6067 if (lo
->type
== bfd_indirect_link_order
)
6069 bfd_byte
*erel
, *erelend
;
6070 asection
*o
= lo
->u
.indirect
.section
;
6073 erelend
= o
->contents
+ o
->size
;
6074 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6075 while (erel
< erelend
)
6077 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6078 (*swap_out
) (abfd
, s
->rela
, erel
);
6089 /* Flush the output symbols to the file. */
6092 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6093 const struct elf_backend_data
*bed
)
6095 if (finfo
->symbuf_count
> 0)
6097 Elf_Internal_Shdr
*hdr
;
6101 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6102 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6103 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6104 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6105 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6108 hdr
->sh_size
+= amt
;
6109 finfo
->symbuf_count
= 0;
6115 /* Add a symbol to the output symbol table. */
6118 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6120 Elf_Internal_Sym
*elfsym
,
6121 asection
*input_sec
,
6122 struct elf_link_hash_entry
*h
)
6125 Elf_External_Sym_Shndx
*destshndx
;
6126 bfd_boolean (*output_symbol_hook
)
6127 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6128 struct elf_link_hash_entry
*);
6129 const struct elf_backend_data
*bed
;
6131 bed
= get_elf_backend_data (finfo
->output_bfd
);
6132 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6133 if (output_symbol_hook
!= NULL
)
6135 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6139 if (name
== NULL
|| *name
== '\0')
6140 elfsym
->st_name
= 0;
6141 else if (input_sec
->flags
& SEC_EXCLUDE
)
6142 elfsym
->st_name
= 0;
6145 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6147 if (elfsym
->st_name
== (unsigned long) -1)
6151 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6153 if (! elf_link_flush_output_syms (finfo
, bed
))
6157 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6158 destshndx
= finfo
->symshndxbuf
;
6159 if (destshndx
!= NULL
)
6161 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6165 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6166 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6167 if (destshndx
== NULL
)
6169 memset ((char *) destshndx
+ amt
, 0, amt
);
6170 finfo
->shndxbuf_size
*= 2;
6172 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6175 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6176 finfo
->symbuf_count
+= 1;
6177 bfd_get_symcount (finfo
->output_bfd
) += 1;
6182 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6183 allowing an unsatisfied unversioned symbol in the DSO to match a
6184 versioned symbol that would normally require an explicit version.
6185 We also handle the case that a DSO references a hidden symbol
6186 which may be satisfied by a versioned symbol in another DSO. */
6189 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6190 const struct elf_backend_data
*bed
,
6191 struct elf_link_hash_entry
*h
)
6194 struct elf_link_loaded_list
*loaded
;
6196 if (!is_elf_hash_table (info
->hash
))
6199 switch (h
->root
.type
)
6205 case bfd_link_hash_undefined
:
6206 case bfd_link_hash_undefweak
:
6207 abfd
= h
->root
.u
.undef
.abfd
;
6208 if ((abfd
->flags
& DYNAMIC
) == 0
6209 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6213 case bfd_link_hash_defined
:
6214 case bfd_link_hash_defweak
:
6215 abfd
= h
->root
.u
.def
.section
->owner
;
6218 case bfd_link_hash_common
:
6219 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6222 BFD_ASSERT (abfd
!= NULL
);
6224 for (loaded
= elf_hash_table (info
)->loaded
;
6226 loaded
= loaded
->next
)
6229 Elf_Internal_Shdr
*hdr
;
6230 bfd_size_type symcount
;
6231 bfd_size_type extsymcount
;
6232 bfd_size_type extsymoff
;
6233 Elf_Internal_Shdr
*versymhdr
;
6234 Elf_Internal_Sym
*isym
;
6235 Elf_Internal_Sym
*isymend
;
6236 Elf_Internal_Sym
*isymbuf
;
6237 Elf_External_Versym
*ever
;
6238 Elf_External_Versym
*extversym
;
6240 input
= loaded
->abfd
;
6242 /* We check each DSO for a possible hidden versioned definition. */
6244 || (input
->flags
& DYNAMIC
) == 0
6245 || elf_dynversym (input
) == 0)
6248 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6250 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6251 if (elf_bad_symtab (input
))
6253 extsymcount
= symcount
;
6258 extsymcount
= symcount
- hdr
->sh_info
;
6259 extsymoff
= hdr
->sh_info
;
6262 if (extsymcount
== 0)
6265 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6267 if (isymbuf
== NULL
)
6270 /* Read in any version definitions. */
6271 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6272 extversym
= bfd_malloc (versymhdr
->sh_size
);
6273 if (extversym
== NULL
)
6276 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6277 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6278 != versymhdr
->sh_size
))
6286 ever
= extversym
+ extsymoff
;
6287 isymend
= isymbuf
+ extsymcount
;
6288 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6291 Elf_Internal_Versym iver
;
6292 unsigned short version_index
;
6294 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6295 || isym
->st_shndx
== SHN_UNDEF
)
6298 name
= bfd_elf_string_from_elf_section (input
,
6301 if (strcmp (name
, h
->root
.root
.string
) != 0)
6304 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6306 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6308 /* If we have a non-hidden versioned sym, then it should
6309 have provided a definition for the undefined sym. */
6313 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6314 if (version_index
== 1 || version_index
== 2)
6316 /* This is the base or first version. We can use it. */
6330 /* Add an external symbol to the symbol table. This is called from
6331 the hash table traversal routine. When generating a shared object,
6332 we go through the symbol table twice. The first time we output
6333 anything that might have been forced to local scope in a version
6334 script. The second time we output the symbols that are still
6338 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6340 struct elf_outext_info
*eoinfo
= data
;
6341 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6343 Elf_Internal_Sym sym
;
6344 asection
*input_sec
;
6345 const struct elf_backend_data
*bed
;
6347 if (h
->root
.type
== bfd_link_hash_warning
)
6349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6350 if (h
->root
.type
== bfd_link_hash_new
)
6354 /* Decide whether to output this symbol in this pass. */
6355 if (eoinfo
->localsyms
)
6357 if (!h
->forced_local
)
6362 if (h
->forced_local
)
6366 bed
= get_elf_backend_data (finfo
->output_bfd
);
6368 /* If we have an undefined symbol reference here then it must have
6369 come from a shared library that is being linked in. (Undefined
6370 references in regular files have already been handled). If we
6371 are reporting errors for this situation then do so now. */
6372 if (h
->root
.type
== bfd_link_hash_undefined
6375 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6376 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6378 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6379 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6380 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6382 eoinfo
->failed
= TRUE
;
6387 /* We should also warn if a forced local symbol is referenced from
6388 shared libraries. */
6389 if (! finfo
->info
->relocatable
6390 && (! finfo
->info
->shared
)
6395 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6397 (*_bfd_error_handler
)
6398 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6400 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6401 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6402 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6404 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6405 ? "hidden" : "local",
6406 h
->root
.root
.string
);
6407 eoinfo
->failed
= TRUE
;
6411 /* We don't want to output symbols that have never been mentioned by
6412 a regular file, or that we have been told to strip. However, if
6413 h->indx is set to -2, the symbol is used by a reloc and we must
6417 else if ((h
->def_dynamic
6419 || h
->root
.type
== bfd_link_hash_new
)
6423 else if (finfo
->info
->strip
== strip_all
)
6425 else if (finfo
->info
->strip
== strip_some
6426 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6427 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6429 else if (finfo
->info
->strip_discarded
6430 && (h
->root
.type
== bfd_link_hash_defined
6431 || h
->root
.type
== bfd_link_hash_defweak
)
6432 && elf_discarded_section (h
->root
.u
.def
.section
))
6437 /* If we're stripping it, and it's not a dynamic symbol, there's
6438 nothing else to do unless it is a forced local symbol. */
6441 && !h
->forced_local
)
6445 sym
.st_size
= h
->size
;
6446 sym
.st_other
= h
->other
;
6447 if (h
->forced_local
)
6448 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6449 else if (h
->root
.type
== bfd_link_hash_undefweak
6450 || h
->root
.type
== bfd_link_hash_defweak
)
6451 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6453 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6455 switch (h
->root
.type
)
6458 case bfd_link_hash_new
:
6459 case bfd_link_hash_warning
:
6463 case bfd_link_hash_undefined
:
6464 case bfd_link_hash_undefweak
:
6465 input_sec
= bfd_und_section_ptr
;
6466 sym
.st_shndx
= SHN_UNDEF
;
6469 case bfd_link_hash_defined
:
6470 case bfd_link_hash_defweak
:
6472 input_sec
= h
->root
.u
.def
.section
;
6473 if (input_sec
->output_section
!= NULL
)
6476 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6477 input_sec
->output_section
);
6478 if (sym
.st_shndx
== SHN_BAD
)
6480 (*_bfd_error_handler
)
6481 (_("%B: could not find output section %A for input section %A"),
6482 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6483 eoinfo
->failed
= TRUE
;
6487 /* ELF symbols in relocatable files are section relative,
6488 but in nonrelocatable files they are virtual
6490 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6491 if (! finfo
->info
->relocatable
)
6493 sym
.st_value
+= input_sec
->output_section
->vma
;
6494 if (h
->type
== STT_TLS
)
6496 /* STT_TLS symbols are relative to PT_TLS segment
6498 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6499 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6505 BFD_ASSERT (input_sec
->owner
== NULL
6506 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6507 sym
.st_shndx
= SHN_UNDEF
;
6508 input_sec
= bfd_und_section_ptr
;
6513 case bfd_link_hash_common
:
6514 input_sec
= h
->root
.u
.c
.p
->section
;
6515 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6516 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6519 case bfd_link_hash_indirect
:
6520 /* These symbols are created by symbol versioning. They point
6521 to the decorated version of the name. For example, if the
6522 symbol foo@@GNU_1.2 is the default, which should be used when
6523 foo is used with no version, then we add an indirect symbol
6524 foo which points to foo@@GNU_1.2. We ignore these symbols,
6525 since the indirected symbol is already in the hash table. */
6529 /* Give the processor backend a chance to tweak the symbol value,
6530 and also to finish up anything that needs to be done for this
6531 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6532 forced local syms when non-shared is due to a historical quirk. */
6533 if ((h
->dynindx
!= -1
6535 && ((finfo
->info
->shared
6536 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6537 || h
->root
.type
!= bfd_link_hash_undefweak
))
6538 || !h
->forced_local
)
6539 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6541 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6542 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6544 eoinfo
->failed
= TRUE
;
6549 /* If we are marking the symbol as undefined, and there are no
6550 non-weak references to this symbol from a regular object, then
6551 mark the symbol as weak undefined; if there are non-weak
6552 references, mark the symbol as strong. We can't do this earlier,
6553 because it might not be marked as undefined until the
6554 finish_dynamic_symbol routine gets through with it. */
6555 if (sym
.st_shndx
== SHN_UNDEF
6557 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6558 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6562 if (h
->ref_regular_nonweak
)
6563 bindtype
= STB_GLOBAL
;
6565 bindtype
= STB_WEAK
;
6566 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6569 /* If a non-weak symbol with non-default visibility is not defined
6570 locally, it is a fatal error. */
6571 if (! finfo
->info
->relocatable
6572 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6573 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6574 && h
->root
.type
== bfd_link_hash_undefined
6577 (*_bfd_error_handler
)
6578 (_("%B: %s symbol `%s' isn't defined"),
6580 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6582 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6583 ? "internal" : "hidden",
6584 h
->root
.root
.string
);
6585 eoinfo
->failed
= TRUE
;
6589 /* If this symbol should be put in the .dynsym section, then put it
6590 there now. We already know the symbol index. We also fill in
6591 the entry in the .hash section. */
6592 if (h
->dynindx
!= -1
6593 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6597 size_t hash_entry_size
;
6598 bfd_byte
*bucketpos
;
6602 sym
.st_name
= h
->dynstr_index
;
6603 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6604 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6606 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6607 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6609 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6610 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6611 + (bucket
+ 2) * hash_entry_size
);
6612 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6613 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6614 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6615 ((bfd_byte
*) finfo
->hash_sec
->contents
6616 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6618 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6620 Elf_Internal_Versym iversym
;
6621 Elf_External_Versym
*eversym
;
6623 if (!h
->def_regular
)
6625 if (h
->verinfo
.verdef
== NULL
)
6626 iversym
.vs_vers
= 0;
6628 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6632 if (h
->verinfo
.vertree
== NULL
)
6633 iversym
.vs_vers
= 1;
6635 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6636 if (finfo
->info
->create_default_symver
)
6641 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6643 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6644 eversym
+= h
->dynindx
;
6645 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6649 /* If we're stripping it, then it was just a dynamic symbol, and
6650 there's nothing else to do. */
6651 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6654 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6656 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6658 eoinfo
->failed
= TRUE
;
6665 /* Return TRUE if special handling is done for relocs in SEC against
6666 symbols defined in discarded sections. */
6669 elf_section_ignore_discarded_relocs (asection
*sec
)
6671 const struct elf_backend_data
*bed
;
6673 switch (sec
->sec_info_type
)
6675 case ELF_INFO_TYPE_STABS
:
6676 case ELF_INFO_TYPE_EH_FRAME
:
6682 bed
= get_elf_backend_data (sec
->owner
);
6683 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6684 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6690 /* Return a mask saying how ld should treat relocations in SEC against
6691 symbols defined in discarded sections. If this function returns
6692 COMPLAIN set, ld will issue a warning message. If this function
6693 returns PRETEND set, and the discarded section was link-once and the
6694 same size as the kept link-once section, ld will pretend that the
6695 symbol was actually defined in the kept section. Otherwise ld will
6696 zero the reloc (at least that is the intent, but some cooperation by
6697 the target dependent code is needed, particularly for REL targets). */
6700 _bfd_elf_default_action_discarded (asection
*sec
)
6702 if (sec
->flags
& SEC_DEBUGGING
)
6705 if (strcmp (".eh_frame", sec
->name
) == 0)
6708 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6711 return COMPLAIN
| PRETEND
;
6714 /* Find a match between a section and a member of a section group. */
6717 match_group_member (asection
*sec
, asection
*group
)
6719 asection
*first
= elf_next_in_group (group
);
6720 asection
*s
= first
;
6724 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6734 /* Check if the kept section of a discarded section SEC can be used
6735 to replace it. Return the replacement if it is OK. Otherwise return
6739 _bfd_elf_check_kept_section (asection
*sec
)
6743 kept
= sec
->kept_section
;
6746 if (elf_sec_group (sec
) != NULL
)
6747 kept
= match_group_member (sec
, kept
);
6748 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6754 /* Link an input file into the linker output file. This function
6755 handles all the sections and relocations of the input file at once.
6756 This is so that we only have to read the local symbols once, and
6757 don't have to keep them in memory. */
6760 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6762 bfd_boolean (*relocate_section
)
6763 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6764 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6766 Elf_Internal_Shdr
*symtab_hdr
;
6769 Elf_Internal_Sym
*isymbuf
;
6770 Elf_Internal_Sym
*isym
;
6771 Elf_Internal_Sym
*isymend
;
6773 asection
**ppsection
;
6775 const struct elf_backend_data
*bed
;
6776 bfd_boolean emit_relocs
;
6777 struct elf_link_hash_entry
**sym_hashes
;
6779 output_bfd
= finfo
->output_bfd
;
6780 bed
= get_elf_backend_data (output_bfd
);
6781 relocate_section
= bed
->elf_backend_relocate_section
;
6783 /* If this is a dynamic object, we don't want to do anything here:
6784 we don't want the local symbols, and we don't want the section
6786 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6789 emit_relocs
= (finfo
->info
->relocatable
6790 || finfo
->info
->emitrelocations
);
6792 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6793 if (elf_bad_symtab (input_bfd
))
6795 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6800 locsymcount
= symtab_hdr
->sh_info
;
6801 extsymoff
= symtab_hdr
->sh_info
;
6804 /* Read the local symbols. */
6805 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6806 if (isymbuf
== NULL
&& locsymcount
!= 0)
6808 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6809 finfo
->internal_syms
,
6810 finfo
->external_syms
,
6811 finfo
->locsym_shndx
);
6812 if (isymbuf
== NULL
)
6816 /* Find local symbol sections and adjust values of symbols in
6817 SEC_MERGE sections. Write out those local symbols we know are
6818 going into the output file. */
6819 isymend
= isymbuf
+ locsymcount
;
6820 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6822 isym
++, pindex
++, ppsection
++)
6826 Elf_Internal_Sym osym
;
6830 if (elf_bad_symtab (input_bfd
))
6832 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6839 if (isym
->st_shndx
== SHN_UNDEF
)
6840 isec
= bfd_und_section_ptr
;
6841 else if (isym
->st_shndx
< SHN_LORESERVE
6842 || isym
->st_shndx
> SHN_HIRESERVE
)
6844 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6846 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6847 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6849 _bfd_merged_section_offset (output_bfd
, &isec
,
6850 elf_section_data (isec
)->sec_info
,
6853 else if (isym
->st_shndx
== SHN_ABS
)
6854 isec
= bfd_abs_section_ptr
;
6855 else if (isym
->st_shndx
== SHN_COMMON
)
6856 isec
= bfd_com_section_ptr
;
6865 /* Don't output the first, undefined, symbol. */
6866 if (ppsection
== finfo
->sections
)
6869 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6871 /* We never output section symbols. Instead, we use the
6872 section symbol of the corresponding section in the output
6877 /* If we are stripping all symbols, we don't want to output this
6879 if (finfo
->info
->strip
== strip_all
)
6882 /* If we are discarding all local symbols, we don't want to
6883 output this one. If we are generating a relocatable output
6884 file, then some of the local symbols may be required by
6885 relocs; we output them below as we discover that they are
6887 if (finfo
->info
->discard
== discard_all
)
6890 /* If this symbol is defined in a section which we are
6891 discarding, we don't need to keep it, but note that
6892 linker_mark is only reliable for sections that have contents.
6893 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6894 as well as linker_mark. */
6895 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6897 || (! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6898 || (! finfo
->info
->relocatable
6899 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6902 /* If the section is not in the output BFD's section list, it is not
6904 if (bfd_section_removed_from_list (output_bfd
, isec
->output_section
))
6907 /* Get the name of the symbol. */
6908 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6913 /* See if we are discarding symbols with this name. */
6914 if ((finfo
->info
->strip
== strip_some
6915 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6917 || (((finfo
->info
->discard
== discard_sec_merge
6918 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6919 || finfo
->info
->discard
== discard_l
)
6920 && bfd_is_local_label_name (input_bfd
, name
)))
6923 /* If we get here, we are going to output this symbol. */
6927 /* Adjust the section index for the output file. */
6928 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6929 isec
->output_section
);
6930 if (osym
.st_shndx
== SHN_BAD
)
6933 *pindex
= bfd_get_symcount (output_bfd
);
6935 /* ELF symbols in relocatable files are section relative, but
6936 in executable files they are virtual addresses. Note that
6937 this code assumes that all ELF sections have an associated
6938 BFD section with a reasonable value for output_offset; below
6939 we assume that they also have a reasonable value for
6940 output_section. Any special sections must be set up to meet
6941 these requirements. */
6942 osym
.st_value
+= isec
->output_offset
;
6943 if (! finfo
->info
->relocatable
)
6945 osym
.st_value
+= isec
->output_section
->vma
;
6946 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6948 /* STT_TLS symbols are relative to PT_TLS segment base. */
6949 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6950 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6954 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6958 /* Relocate the contents of each section. */
6959 sym_hashes
= elf_sym_hashes (input_bfd
);
6960 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6964 if (! o
->linker_mark
)
6966 /* This section was omitted from the link. */
6970 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6971 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6974 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6976 /* Section was created by _bfd_elf_link_create_dynamic_sections
6981 /* Get the contents of the section. They have been cached by a
6982 relaxation routine. Note that o is a section in an input
6983 file, so the contents field will not have been set by any of
6984 the routines which work on output files. */
6985 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6986 contents
= elf_section_data (o
)->this_hdr
.contents
;
6989 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6991 contents
= finfo
->contents
;
6992 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6996 if ((o
->flags
& SEC_RELOC
) != 0)
6998 Elf_Internal_Rela
*internal_relocs
;
6999 bfd_vma r_type_mask
;
7002 /* Get the swapped relocs. */
7004 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7005 finfo
->internal_relocs
, FALSE
);
7006 if (internal_relocs
== NULL
7007 && o
->reloc_count
> 0)
7010 if (bed
->s
->arch_size
== 32)
7017 r_type_mask
= 0xffffffff;
7021 /* Run through the relocs looking for any against symbols
7022 from discarded sections and section symbols from
7023 removed link-once sections. Complain about relocs
7024 against discarded sections. Zero relocs against removed
7025 link-once sections. Preserve debug information as much
7027 if (!elf_section_ignore_discarded_relocs (o
))
7029 Elf_Internal_Rela
*rel
, *relend
;
7030 unsigned int action
= (*bed
->action_discarded
) (o
);
7032 rel
= internal_relocs
;
7033 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7034 for ( ; rel
< relend
; rel
++)
7036 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7037 asection
**ps
, *sec
;
7038 struct elf_link_hash_entry
*h
= NULL
;
7039 const char *sym_name
;
7041 if (r_symndx
== STN_UNDEF
)
7044 if (r_symndx
>= locsymcount
7045 || (elf_bad_symtab (input_bfd
)
7046 && finfo
->sections
[r_symndx
] == NULL
))
7048 h
= sym_hashes
[r_symndx
- extsymoff
];
7050 /* Badly formatted input files can contain relocs that
7051 reference non-existant symbols. Check here so that
7052 we do not seg fault. */
7057 sprintf_vma (buffer
, rel
->r_info
);
7058 (*_bfd_error_handler
)
7059 (_("error: %B contains a reloc (0x%s) for section %A "
7060 "that references a non-existent global symbol"),
7061 input_bfd
, o
, buffer
);
7062 bfd_set_error (bfd_error_bad_value
);
7066 while (h
->root
.type
== bfd_link_hash_indirect
7067 || h
->root
.type
== bfd_link_hash_warning
)
7068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7070 if (h
->root
.type
!= bfd_link_hash_defined
7071 && h
->root
.type
!= bfd_link_hash_defweak
)
7074 ps
= &h
->root
.u
.def
.section
;
7075 sym_name
= h
->root
.root
.string
;
7079 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7080 ps
= &finfo
->sections
[r_symndx
];
7081 sym_name
= bfd_elf_sym_name (input_bfd
,
7086 /* Complain if the definition comes from a
7087 discarded section. */
7088 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7090 BFD_ASSERT (r_symndx
!= 0);
7091 if (action
& COMPLAIN
)
7092 (*finfo
->info
->callbacks
->einfo
)
7093 (_("%X`%s' referenced in section `%A' of %B: "
7094 "defined in discarded section `%A' of %B\n"),
7095 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7097 /* Try to do the best we can to support buggy old
7098 versions of gcc. If we've warned, or this is
7099 debugging info, pretend that the symbol is
7100 really defined in the kept linkonce section.
7101 FIXME: This is quite broken. Modifying the
7102 symbol here means we will be changing all later
7103 uses of the symbol, not just in this section.
7104 The only thing that makes this half reasonable
7105 is that we warn in non-debug sections, and
7106 debug sections tend to come after other
7108 if (action
& PRETEND
)
7112 kept
= _bfd_elf_check_kept_section (sec
);
7120 /* Remove the symbol reference from the reloc, but
7121 don't kill the reloc completely. This is so that
7122 a zero value will be written into the section,
7123 which may have non-zero contents put there by the
7124 assembler. Zero in things like an eh_frame fde
7125 pc_begin allows stack unwinders to recognize the
7127 rel
->r_info
&= r_type_mask
;
7133 /* Relocate the section by invoking a back end routine.
7135 The back end routine is responsible for adjusting the
7136 section contents as necessary, and (if using Rela relocs
7137 and generating a relocatable output file) adjusting the
7138 reloc addend as necessary.
7140 The back end routine does not have to worry about setting
7141 the reloc address or the reloc symbol index.
7143 The back end routine is given a pointer to the swapped in
7144 internal symbols, and can access the hash table entries
7145 for the external symbols via elf_sym_hashes (input_bfd).
7147 When generating relocatable output, the back end routine
7148 must handle STB_LOCAL/STT_SECTION symbols specially. The
7149 output symbol is going to be a section symbol
7150 corresponding to the output section, which will require
7151 the addend to be adjusted. */
7153 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7154 input_bfd
, o
, contents
,
7162 Elf_Internal_Rela
*irela
;
7163 Elf_Internal_Rela
*irelaend
;
7164 bfd_vma last_offset
;
7165 struct elf_link_hash_entry
**rel_hash
;
7166 struct elf_link_hash_entry
**rel_hash_list
;
7167 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7168 unsigned int next_erel
;
7169 bfd_boolean rela_normal
;
7171 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7172 rela_normal
= (bed
->rela_normal
7173 && (input_rel_hdr
->sh_entsize
7174 == bed
->s
->sizeof_rela
));
7176 /* Adjust the reloc addresses and symbol indices. */
7178 irela
= internal_relocs
;
7179 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7180 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7181 + elf_section_data (o
->output_section
)->rel_count
7182 + elf_section_data (o
->output_section
)->rel_count2
);
7183 rel_hash_list
= rel_hash
;
7184 last_offset
= o
->output_offset
;
7185 if (!finfo
->info
->relocatable
)
7186 last_offset
+= o
->output_section
->vma
;
7187 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7189 unsigned long r_symndx
;
7191 Elf_Internal_Sym sym
;
7193 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7199 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7202 if (irela
->r_offset
>= (bfd_vma
) -2)
7204 /* This is a reloc for a deleted entry or somesuch.
7205 Turn it into an R_*_NONE reloc, at the same
7206 offset as the last reloc. elf_eh_frame.c and
7207 elf_bfd_discard_info rely on reloc offsets
7209 irela
->r_offset
= last_offset
;
7211 irela
->r_addend
= 0;
7215 irela
->r_offset
+= o
->output_offset
;
7217 /* Relocs in an executable have to be virtual addresses. */
7218 if (!finfo
->info
->relocatable
)
7219 irela
->r_offset
+= o
->output_section
->vma
;
7221 last_offset
= irela
->r_offset
;
7223 r_symndx
= irela
->r_info
>> r_sym_shift
;
7224 if (r_symndx
== STN_UNDEF
)
7227 if (r_symndx
>= locsymcount
7228 || (elf_bad_symtab (input_bfd
)
7229 && finfo
->sections
[r_symndx
] == NULL
))
7231 struct elf_link_hash_entry
*rh
;
7234 /* This is a reloc against a global symbol. We
7235 have not yet output all the local symbols, so
7236 we do not know the symbol index of any global
7237 symbol. We set the rel_hash entry for this
7238 reloc to point to the global hash table entry
7239 for this symbol. The symbol index is then
7240 set at the end of bfd_elf_final_link. */
7241 indx
= r_symndx
- extsymoff
;
7242 rh
= elf_sym_hashes (input_bfd
)[indx
];
7243 while (rh
->root
.type
== bfd_link_hash_indirect
7244 || rh
->root
.type
== bfd_link_hash_warning
)
7245 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7247 /* Setting the index to -2 tells
7248 elf_link_output_extsym that this symbol is
7250 BFD_ASSERT (rh
->indx
< 0);
7258 /* This is a reloc against a local symbol. */
7261 sym
= isymbuf
[r_symndx
];
7262 sec
= finfo
->sections
[r_symndx
];
7263 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7265 /* I suppose the backend ought to fill in the
7266 section of any STT_SECTION symbol against a
7267 processor specific section. */
7269 if (bfd_is_abs_section (sec
))
7271 else if (sec
== NULL
|| sec
->owner
== NULL
)
7273 bfd_set_error (bfd_error_bad_value
);
7278 asection
*osec
= sec
->output_section
;
7280 /* If we have discarded a section, the output
7281 section will be the absolute section. In
7282 case of discarded link-once and discarded
7283 SEC_MERGE sections, use the kept section. */
7284 if (bfd_is_abs_section (osec
)
7285 && sec
->kept_section
!= NULL
7286 && sec
->kept_section
->output_section
!= NULL
)
7288 osec
= sec
->kept_section
->output_section
;
7289 irela
->r_addend
-= osec
->vma
;
7292 if (!bfd_is_abs_section (osec
))
7294 r_symndx
= osec
->target_index
;
7295 BFD_ASSERT (r_symndx
!= 0);
7299 /* Adjust the addend according to where the
7300 section winds up in the output section. */
7302 irela
->r_addend
+= sec
->output_offset
;
7306 if (finfo
->indices
[r_symndx
] == -1)
7308 unsigned long shlink
;
7312 if (finfo
->info
->strip
== strip_all
)
7314 /* You can't do ld -r -s. */
7315 bfd_set_error (bfd_error_invalid_operation
);
7319 /* This symbol was skipped earlier, but
7320 since it is needed by a reloc, we
7321 must output it now. */
7322 shlink
= symtab_hdr
->sh_link
;
7323 name
= (bfd_elf_string_from_elf_section
7324 (input_bfd
, shlink
, sym
.st_name
));
7328 osec
= sec
->output_section
;
7330 _bfd_elf_section_from_bfd_section (output_bfd
,
7332 if (sym
.st_shndx
== SHN_BAD
)
7335 sym
.st_value
+= sec
->output_offset
;
7336 if (! finfo
->info
->relocatable
)
7338 sym
.st_value
+= osec
->vma
;
7339 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7341 /* STT_TLS symbols are relative to PT_TLS
7343 BFD_ASSERT (elf_hash_table (finfo
->info
)
7345 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7350 finfo
->indices
[r_symndx
]
7351 = bfd_get_symcount (output_bfd
);
7353 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7358 r_symndx
= finfo
->indices
[r_symndx
];
7361 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7362 | (irela
->r_info
& r_type_mask
));
7365 /* Swap out the relocs. */
7366 if (input_rel_hdr
->sh_size
!= 0
7367 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7373 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7374 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7376 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7377 * bed
->s
->int_rels_per_ext_rel
);
7378 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7379 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7388 /* Write out the modified section contents. */
7389 if (bed
->elf_backend_write_section
7390 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7392 /* Section written out. */
7394 else switch (o
->sec_info_type
)
7396 case ELF_INFO_TYPE_STABS
:
7397 if (! (_bfd_write_section_stabs
7399 &elf_hash_table (finfo
->info
)->stab_info
,
7400 o
, &elf_section_data (o
)->sec_info
, contents
)))
7403 case ELF_INFO_TYPE_MERGE
:
7404 if (! _bfd_write_merged_section (output_bfd
, o
,
7405 elf_section_data (o
)->sec_info
))
7408 case ELF_INFO_TYPE_EH_FRAME
:
7410 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7417 if (! (o
->flags
& SEC_EXCLUDE
)
7418 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7420 (file_ptr
) o
->output_offset
,
7431 /* Generate a reloc when linking an ELF file. This is a reloc
7432 requested by the linker, and does come from any input file. This
7433 is used to build constructor and destructor tables when linking
7437 elf_reloc_link_order (bfd
*output_bfd
,
7438 struct bfd_link_info
*info
,
7439 asection
*output_section
,
7440 struct bfd_link_order
*link_order
)
7442 reloc_howto_type
*howto
;
7446 struct elf_link_hash_entry
**rel_hash_ptr
;
7447 Elf_Internal_Shdr
*rel_hdr
;
7448 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7449 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7453 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7456 bfd_set_error (bfd_error_bad_value
);
7460 addend
= link_order
->u
.reloc
.p
->addend
;
7462 /* Figure out the symbol index. */
7463 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7464 + elf_section_data (output_section
)->rel_count
7465 + elf_section_data (output_section
)->rel_count2
);
7466 if (link_order
->type
== bfd_section_reloc_link_order
)
7468 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7469 BFD_ASSERT (indx
!= 0);
7470 *rel_hash_ptr
= NULL
;
7474 struct elf_link_hash_entry
*h
;
7476 /* Treat a reloc against a defined symbol as though it were
7477 actually against the section. */
7478 h
= ((struct elf_link_hash_entry
*)
7479 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7480 link_order
->u
.reloc
.p
->u
.name
,
7481 FALSE
, FALSE
, TRUE
));
7483 && (h
->root
.type
== bfd_link_hash_defined
7484 || h
->root
.type
== bfd_link_hash_defweak
))
7488 section
= h
->root
.u
.def
.section
;
7489 indx
= section
->output_section
->target_index
;
7490 *rel_hash_ptr
= NULL
;
7491 /* It seems that we ought to add the symbol value to the
7492 addend here, but in practice it has already been added
7493 because it was passed to constructor_callback. */
7494 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7498 /* Setting the index to -2 tells elf_link_output_extsym that
7499 this symbol is used by a reloc. */
7506 if (! ((*info
->callbacks
->unattached_reloc
)
7507 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7513 /* If this is an inplace reloc, we must write the addend into the
7515 if (howto
->partial_inplace
&& addend
!= 0)
7518 bfd_reloc_status_type rstat
;
7521 const char *sym_name
;
7523 size
= bfd_get_reloc_size (howto
);
7524 buf
= bfd_zmalloc (size
);
7527 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7534 case bfd_reloc_outofrange
:
7537 case bfd_reloc_overflow
:
7538 if (link_order
->type
== bfd_section_reloc_link_order
)
7539 sym_name
= bfd_section_name (output_bfd
,
7540 link_order
->u
.reloc
.p
->u
.section
);
7542 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7543 if (! ((*info
->callbacks
->reloc_overflow
)
7544 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7545 NULL
, (bfd_vma
) 0)))
7552 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7553 link_order
->offset
, size
);
7559 /* The address of a reloc is relative to the section in a
7560 relocatable file, and is a virtual address in an executable
7562 offset
= link_order
->offset
;
7563 if (! info
->relocatable
)
7564 offset
+= output_section
->vma
;
7566 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7568 irel
[i
].r_offset
= offset
;
7570 irel
[i
].r_addend
= 0;
7572 if (bed
->s
->arch_size
== 32)
7573 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7575 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7577 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7578 erel
= rel_hdr
->contents
;
7579 if (rel_hdr
->sh_type
== SHT_REL
)
7581 erel
+= (elf_section_data (output_section
)->rel_count
7582 * bed
->s
->sizeof_rel
);
7583 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7587 irel
[0].r_addend
= addend
;
7588 erel
+= (elf_section_data (output_section
)->rel_count
7589 * bed
->s
->sizeof_rela
);
7590 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7593 ++elf_section_data (output_section
)->rel_count
;
7599 /* Get the output vma of the section pointed to by the sh_link field. */
7602 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7604 Elf_Internal_Shdr
**elf_shdrp
;
7608 s
= p
->u
.indirect
.section
;
7609 elf_shdrp
= elf_elfsections (s
->owner
);
7610 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7611 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7613 The Intel C compiler generates SHT_IA_64_UNWIND with
7614 SHF_LINK_ORDER. But it doesn't set the sh_link or
7615 sh_info fields. Hence we could get the situation
7616 where elfsec is 0. */
7619 const struct elf_backend_data
*bed
7620 = get_elf_backend_data (s
->owner
);
7621 if (bed
->link_order_error_handler
)
7622 bed
->link_order_error_handler
7623 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7628 s
= elf_shdrp
[elfsec
]->bfd_section
;
7629 return s
->output_section
->vma
+ s
->output_offset
;
7634 /* Compare two sections based on the locations of the sections they are
7635 linked to. Used by elf_fixup_link_order. */
7638 compare_link_order (const void * a
, const void * b
)
7643 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7644 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7651 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7652 order as their linked sections. Returns false if this could not be done
7653 because an output section includes both ordered and unordered
7654 sections. Ideally we'd do this in the linker proper. */
7657 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7662 struct bfd_link_order
*p
;
7664 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7666 struct bfd_link_order
**sections
;
7672 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7674 if (p
->type
== bfd_indirect_link_order
7675 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7676 == bfd_target_elf_flavour
)
7677 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7679 s
= p
->u
.indirect
.section
;
7680 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7682 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7691 if (!seen_linkorder
)
7694 if (seen_other
&& seen_linkorder
)
7696 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7698 bfd_set_error (bfd_error_bad_value
);
7702 sections
= (struct bfd_link_order
**)
7703 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7706 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7708 sections
[seen_linkorder
++] = p
;
7710 /* Sort the input sections in the order of their linked section. */
7711 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7712 compare_link_order
);
7714 /* Change the offsets of the sections. */
7716 for (n
= 0; n
< seen_linkorder
; n
++)
7718 s
= sections
[n
]->u
.indirect
.section
;
7719 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7720 s
->output_offset
= offset
;
7721 sections
[n
]->offset
= offset
;
7722 offset
+= sections
[n
]->size
;
7729 /* Do the final step of an ELF link. */
7732 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7734 bfd_boolean dynamic
;
7735 bfd_boolean emit_relocs
;
7737 struct elf_final_link_info finfo
;
7738 register asection
*o
;
7739 register struct bfd_link_order
*p
;
7741 bfd_size_type max_contents_size
;
7742 bfd_size_type max_external_reloc_size
;
7743 bfd_size_type max_internal_reloc_count
;
7744 bfd_size_type max_sym_count
;
7745 bfd_size_type max_sym_shndx_count
;
7747 Elf_Internal_Sym elfsym
;
7749 Elf_Internal_Shdr
*symtab_hdr
;
7750 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7751 Elf_Internal_Shdr
*symstrtab_hdr
;
7752 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7753 struct elf_outext_info eoinfo
;
7755 size_t relativecount
= 0;
7756 asection
*reldyn
= 0;
7759 if (! is_elf_hash_table (info
->hash
))
7763 abfd
->flags
|= DYNAMIC
;
7765 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7766 dynobj
= elf_hash_table (info
)->dynobj
;
7768 emit_relocs
= (info
->relocatable
7769 || info
->emitrelocations
7770 || bed
->elf_backend_emit_relocs
);
7773 finfo
.output_bfd
= abfd
;
7774 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7775 if (finfo
.symstrtab
== NULL
)
7780 finfo
.dynsym_sec
= NULL
;
7781 finfo
.hash_sec
= NULL
;
7782 finfo
.symver_sec
= NULL
;
7786 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7787 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7788 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7789 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7790 /* Note that it is OK if symver_sec is NULL. */
7793 finfo
.contents
= NULL
;
7794 finfo
.external_relocs
= NULL
;
7795 finfo
.internal_relocs
= NULL
;
7796 finfo
.external_syms
= NULL
;
7797 finfo
.locsym_shndx
= NULL
;
7798 finfo
.internal_syms
= NULL
;
7799 finfo
.indices
= NULL
;
7800 finfo
.sections
= NULL
;
7801 finfo
.symbuf
= NULL
;
7802 finfo
.symshndxbuf
= NULL
;
7803 finfo
.symbuf_count
= 0;
7804 finfo
.shndxbuf_size
= 0;
7806 /* Count up the number of relocations we will output for each output
7807 section, so that we know the sizes of the reloc sections. We
7808 also figure out some maximum sizes. */
7809 max_contents_size
= 0;
7810 max_external_reloc_size
= 0;
7811 max_internal_reloc_count
= 0;
7813 max_sym_shndx_count
= 0;
7815 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7817 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7820 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7822 unsigned int reloc_count
= 0;
7823 struct bfd_elf_section_data
*esdi
= NULL
;
7824 unsigned int *rel_count1
;
7826 if (p
->type
== bfd_section_reloc_link_order
7827 || p
->type
== bfd_symbol_reloc_link_order
)
7829 else if (p
->type
== bfd_indirect_link_order
)
7833 sec
= p
->u
.indirect
.section
;
7834 esdi
= elf_section_data (sec
);
7836 /* Mark all sections which are to be included in the
7837 link. This will normally be every section. We need
7838 to do this so that we can identify any sections which
7839 the linker has decided to not include. */
7840 sec
->linker_mark
= TRUE
;
7842 if (sec
->flags
& SEC_MERGE
)
7845 if (info
->relocatable
|| info
->emitrelocations
)
7846 reloc_count
= sec
->reloc_count
;
7847 else if (bed
->elf_backend_count_relocs
)
7849 Elf_Internal_Rela
* relocs
;
7851 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7854 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7856 if (elf_section_data (o
)->relocs
!= relocs
)
7860 if (sec
->rawsize
> max_contents_size
)
7861 max_contents_size
= sec
->rawsize
;
7862 if (sec
->size
> max_contents_size
)
7863 max_contents_size
= sec
->size
;
7865 /* We are interested in just local symbols, not all
7867 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7868 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7872 if (elf_bad_symtab (sec
->owner
))
7873 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7874 / bed
->s
->sizeof_sym
);
7876 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7878 if (sym_count
> max_sym_count
)
7879 max_sym_count
= sym_count
;
7881 if (sym_count
> max_sym_shndx_count
7882 && elf_symtab_shndx (sec
->owner
) != 0)
7883 max_sym_shndx_count
= sym_count
;
7885 if ((sec
->flags
& SEC_RELOC
) != 0)
7889 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7890 if (ext_size
> max_external_reloc_size
)
7891 max_external_reloc_size
= ext_size
;
7892 if (sec
->reloc_count
> max_internal_reloc_count
)
7893 max_internal_reloc_count
= sec
->reloc_count
;
7898 if (reloc_count
== 0)
7901 o
->reloc_count
+= reloc_count
;
7903 /* MIPS may have a mix of REL and RELA relocs on sections.
7904 To support this curious ABI we keep reloc counts in
7905 elf_section_data too. We must be careful to add the
7906 relocations from the input section to the right output
7907 count. FIXME: Get rid of one count. We have
7908 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7909 rel_count1
= &esdo
->rel_count
;
7912 bfd_boolean same_size
;
7913 bfd_size_type entsize1
;
7915 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7916 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7917 || entsize1
== bed
->s
->sizeof_rela
);
7918 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7921 rel_count1
= &esdo
->rel_count2
;
7923 if (esdi
->rel_hdr2
!= NULL
)
7925 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7926 unsigned int alt_count
;
7927 unsigned int *rel_count2
;
7929 BFD_ASSERT (entsize2
!= entsize1
7930 && (entsize2
== bed
->s
->sizeof_rel
7931 || entsize2
== bed
->s
->sizeof_rela
));
7933 rel_count2
= &esdo
->rel_count2
;
7935 rel_count2
= &esdo
->rel_count
;
7937 /* The following is probably too simplistic if the
7938 backend counts output relocs unusually. */
7939 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7940 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7941 *rel_count2
+= alt_count
;
7942 reloc_count
-= alt_count
;
7945 *rel_count1
+= reloc_count
;
7948 if (o
->reloc_count
> 0)
7949 o
->flags
|= SEC_RELOC
;
7952 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7953 set it (this is probably a bug) and if it is set
7954 assign_section_numbers will create a reloc section. */
7955 o
->flags
&=~ SEC_RELOC
;
7958 /* If the SEC_ALLOC flag is not set, force the section VMA to
7959 zero. This is done in elf_fake_sections as well, but forcing
7960 the VMA to 0 here will ensure that relocs against these
7961 sections are handled correctly. */
7962 if ((o
->flags
& SEC_ALLOC
) == 0
7963 && ! o
->user_set_vma
)
7967 if (! info
->relocatable
&& merged
)
7968 elf_link_hash_traverse (elf_hash_table (info
),
7969 _bfd_elf_link_sec_merge_syms
, abfd
);
7971 /* Figure out the file positions for everything but the symbol table
7972 and the relocs. We set symcount to force assign_section_numbers
7973 to create a symbol table. */
7974 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7975 BFD_ASSERT (! abfd
->output_has_begun
);
7976 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7979 /* Set sizes, and assign file positions for reloc sections. */
7980 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7982 if ((o
->flags
& SEC_RELOC
) != 0)
7984 if (!(_bfd_elf_link_size_reloc_section
7985 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7988 if (elf_section_data (o
)->rel_hdr2
7989 && !(_bfd_elf_link_size_reloc_section
7990 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7994 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7995 to count upwards while actually outputting the relocations. */
7996 elf_section_data (o
)->rel_count
= 0;
7997 elf_section_data (o
)->rel_count2
= 0;
8000 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8002 /* We have now assigned file positions for all the sections except
8003 .symtab and .strtab. We start the .symtab section at the current
8004 file position, and write directly to it. We build the .strtab
8005 section in memory. */
8006 bfd_get_symcount (abfd
) = 0;
8007 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8008 /* sh_name is set in prep_headers. */
8009 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8010 /* sh_flags, sh_addr and sh_size all start off zero. */
8011 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8012 /* sh_link is set in assign_section_numbers. */
8013 /* sh_info is set below. */
8014 /* sh_offset is set just below. */
8015 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8017 off
= elf_tdata (abfd
)->next_file_pos
;
8018 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8020 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8021 incorrect. We do not yet know the size of the .symtab section.
8022 We correct next_file_pos below, after we do know the size. */
8024 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8025 continuously seeking to the right position in the file. */
8026 if (! info
->keep_memory
|| max_sym_count
< 20)
8027 finfo
.symbuf_size
= 20;
8029 finfo
.symbuf_size
= max_sym_count
;
8030 amt
= finfo
.symbuf_size
;
8031 amt
*= bed
->s
->sizeof_sym
;
8032 finfo
.symbuf
= bfd_malloc (amt
);
8033 if (finfo
.symbuf
== NULL
)
8035 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8037 /* Wild guess at number of output symbols. realloc'd as needed. */
8038 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8039 finfo
.shndxbuf_size
= amt
;
8040 amt
*= sizeof (Elf_External_Sym_Shndx
);
8041 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8042 if (finfo
.symshndxbuf
== NULL
)
8046 /* Start writing out the symbol table. The first symbol is always a
8048 if (info
->strip
!= strip_all
8051 elfsym
.st_value
= 0;
8054 elfsym
.st_other
= 0;
8055 elfsym
.st_shndx
= SHN_UNDEF
;
8056 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8061 /* Output a symbol for each section. We output these even if we are
8062 discarding local symbols, since they are used for relocs. These
8063 symbols have no names. We store the index of each one in the
8064 index field of the section, so that we can find it again when
8065 outputting relocs. */
8066 if (info
->strip
!= strip_all
8070 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8071 elfsym
.st_other
= 0;
8072 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8074 o
= bfd_section_from_elf_index (abfd
, i
);
8076 o
->target_index
= bfd_get_symcount (abfd
);
8077 elfsym
.st_shndx
= i
;
8078 if (info
->relocatable
|| o
== NULL
)
8079 elfsym
.st_value
= 0;
8081 elfsym
.st_value
= o
->vma
;
8082 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8084 if (i
== SHN_LORESERVE
- 1)
8085 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8089 /* Allocate some memory to hold information read in from the input
8091 if (max_contents_size
!= 0)
8093 finfo
.contents
= bfd_malloc (max_contents_size
);
8094 if (finfo
.contents
== NULL
)
8098 if (max_external_reloc_size
!= 0)
8100 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8101 if (finfo
.external_relocs
== NULL
)
8105 if (max_internal_reloc_count
!= 0)
8107 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8108 amt
*= sizeof (Elf_Internal_Rela
);
8109 finfo
.internal_relocs
= bfd_malloc (amt
);
8110 if (finfo
.internal_relocs
== NULL
)
8114 if (max_sym_count
!= 0)
8116 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8117 finfo
.external_syms
= bfd_malloc (amt
);
8118 if (finfo
.external_syms
== NULL
)
8121 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8122 finfo
.internal_syms
= bfd_malloc (amt
);
8123 if (finfo
.internal_syms
== NULL
)
8126 amt
= max_sym_count
* sizeof (long);
8127 finfo
.indices
= bfd_malloc (amt
);
8128 if (finfo
.indices
== NULL
)
8131 amt
= max_sym_count
* sizeof (asection
*);
8132 finfo
.sections
= bfd_malloc (amt
);
8133 if (finfo
.sections
== NULL
)
8137 if (max_sym_shndx_count
!= 0)
8139 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8140 finfo
.locsym_shndx
= bfd_malloc (amt
);
8141 if (finfo
.locsym_shndx
== NULL
)
8145 if (elf_hash_table (info
)->tls_sec
)
8147 bfd_vma base
, end
= 0;
8150 for (sec
= elf_hash_table (info
)->tls_sec
;
8151 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8154 bfd_vma size
= sec
->size
;
8156 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8158 struct bfd_link_order
*o
;
8160 for (o
= sec
->map_head
.link_order
; o
!= NULL
; o
= o
->next
)
8161 if (size
< o
->offset
+ o
->size
)
8162 size
= o
->offset
+ o
->size
;
8164 end
= sec
->vma
+ size
;
8166 base
= elf_hash_table (info
)->tls_sec
->vma
;
8167 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8168 elf_hash_table (info
)->tls_size
= end
- base
;
8171 /* Reorder SHF_LINK_ORDER sections. */
8172 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8174 if (!elf_fixup_link_order (abfd
, o
))
8178 /* Since ELF permits relocations to be against local symbols, we
8179 must have the local symbols available when we do the relocations.
8180 Since we would rather only read the local symbols once, and we
8181 would rather not keep them in memory, we handle all the
8182 relocations for a single input file at the same time.
8184 Unfortunately, there is no way to know the total number of local
8185 symbols until we have seen all of them, and the local symbol
8186 indices precede the global symbol indices. This means that when
8187 we are generating relocatable output, and we see a reloc against
8188 a global symbol, we can not know the symbol index until we have
8189 finished examining all the local symbols to see which ones we are
8190 going to output. To deal with this, we keep the relocations in
8191 memory, and don't output them until the end of the link. This is
8192 an unfortunate waste of memory, but I don't see a good way around
8193 it. Fortunately, it only happens when performing a relocatable
8194 link, which is not the common case. FIXME: If keep_memory is set
8195 we could write the relocs out and then read them again; I don't
8196 know how bad the memory loss will be. */
8198 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8199 sub
->output_has_begun
= FALSE
;
8200 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8202 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8204 if (p
->type
== bfd_indirect_link_order
8205 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8206 == bfd_target_elf_flavour
)
8207 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8209 if (! sub
->output_has_begun
)
8211 if (! elf_link_input_bfd (&finfo
, sub
))
8213 sub
->output_has_begun
= TRUE
;
8216 else if (p
->type
== bfd_section_reloc_link_order
8217 || p
->type
== bfd_symbol_reloc_link_order
)
8219 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8224 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8230 /* Output any global symbols that got converted to local in a
8231 version script or due to symbol visibility. We do this in a
8232 separate step since ELF requires all local symbols to appear
8233 prior to any global symbols. FIXME: We should only do this if
8234 some global symbols were, in fact, converted to become local.
8235 FIXME: Will this work correctly with the Irix 5 linker? */
8236 eoinfo
.failed
= FALSE
;
8237 eoinfo
.finfo
= &finfo
;
8238 eoinfo
.localsyms
= TRUE
;
8239 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8244 /* That wrote out all the local symbols. Finish up the symbol table
8245 with the global symbols. Even if we want to strip everything we
8246 can, we still need to deal with those global symbols that got
8247 converted to local in a version script. */
8249 /* The sh_info field records the index of the first non local symbol. */
8250 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8253 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8255 Elf_Internal_Sym sym
;
8256 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8257 long last_local
= 0;
8259 /* Write out the section symbols for the output sections. */
8260 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8266 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8269 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8275 dynindx
= elf_section_data (s
)->dynindx
;
8278 indx
= elf_section_data (s
)->this_idx
;
8279 BFD_ASSERT (indx
> 0);
8280 sym
.st_shndx
= indx
;
8281 sym
.st_value
= s
->vma
;
8282 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8283 if (last_local
< dynindx
)
8284 last_local
= dynindx
;
8285 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8289 /* Write out the local dynsyms. */
8290 if (elf_hash_table (info
)->dynlocal
)
8292 struct elf_link_local_dynamic_entry
*e
;
8293 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8298 sym
.st_size
= e
->isym
.st_size
;
8299 sym
.st_other
= e
->isym
.st_other
;
8301 /* Copy the internal symbol as is.
8302 Note that we saved a word of storage and overwrote
8303 the original st_name with the dynstr_index. */
8306 if (e
->isym
.st_shndx
!= SHN_UNDEF
8307 && (e
->isym
.st_shndx
< SHN_LORESERVE
8308 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8310 s
= bfd_section_from_elf_index (e
->input_bfd
,
8314 elf_section_data (s
->output_section
)->this_idx
;
8315 sym
.st_value
= (s
->output_section
->vma
8317 + e
->isym
.st_value
);
8320 if (last_local
< e
->dynindx
)
8321 last_local
= e
->dynindx
;
8323 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8324 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8328 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8332 /* We get the global symbols from the hash table. */
8333 eoinfo
.failed
= FALSE
;
8334 eoinfo
.localsyms
= FALSE
;
8335 eoinfo
.finfo
= &finfo
;
8336 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8341 /* If backend needs to output some symbols not present in the hash
8342 table, do it now. */
8343 if (bed
->elf_backend_output_arch_syms
)
8345 typedef bfd_boolean (*out_sym_func
)
8346 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8347 struct elf_link_hash_entry
*);
8349 if (! ((*bed
->elf_backend_output_arch_syms
)
8350 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8354 /* Flush all symbols to the file. */
8355 if (! elf_link_flush_output_syms (&finfo
, bed
))
8358 /* Now we know the size of the symtab section. */
8359 off
+= symtab_hdr
->sh_size
;
8361 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8362 if (symtab_shndx_hdr
->sh_name
!= 0)
8364 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8365 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8366 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8367 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8368 symtab_shndx_hdr
->sh_size
= amt
;
8370 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8373 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8374 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8379 /* Finish up and write out the symbol string table (.strtab)
8381 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8382 /* sh_name was set in prep_headers. */
8383 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8384 symstrtab_hdr
->sh_flags
= 0;
8385 symstrtab_hdr
->sh_addr
= 0;
8386 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8387 symstrtab_hdr
->sh_entsize
= 0;
8388 symstrtab_hdr
->sh_link
= 0;
8389 symstrtab_hdr
->sh_info
= 0;
8390 /* sh_offset is set just below. */
8391 symstrtab_hdr
->sh_addralign
= 1;
8393 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8394 elf_tdata (abfd
)->next_file_pos
= off
;
8396 if (bfd_get_symcount (abfd
) > 0)
8398 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8399 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8403 /* Adjust the relocs to have the correct symbol indices. */
8404 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8406 if ((o
->flags
& SEC_RELOC
) == 0)
8409 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8410 elf_section_data (o
)->rel_count
,
8411 elf_section_data (o
)->rel_hashes
);
8412 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8413 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8414 elf_section_data (o
)->rel_count2
,
8415 (elf_section_data (o
)->rel_hashes
8416 + elf_section_data (o
)->rel_count
));
8418 /* Set the reloc_count field to 0 to prevent write_relocs from
8419 trying to swap the relocs out itself. */
8423 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8424 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8426 /* If we are linking against a dynamic object, or generating a
8427 shared library, finish up the dynamic linking information. */
8430 bfd_byte
*dyncon
, *dynconend
;
8432 /* Fix up .dynamic entries. */
8433 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8434 BFD_ASSERT (o
!= NULL
);
8436 dyncon
= o
->contents
;
8437 dynconend
= o
->contents
+ o
->size
;
8438 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8440 Elf_Internal_Dyn dyn
;
8444 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8451 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8453 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8455 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8456 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8459 dyn
.d_un
.d_val
= relativecount
;
8466 name
= info
->init_function
;
8469 name
= info
->fini_function
;
8472 struct elf_link_hash_entry
*h
;
8474 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8475 FALSE
, FALSE
, TRUE
);
8477 && (h
->root
.type
== bfd_link_hash_defined
8478 || h
->root
.type
== bfd_link_hash_defweak
))
8480 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8481 o
= h
->root
.u
.def
.section
;
8482 if (o
->output_section
!= NULL
)
8483 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8484 + o
->output_offset
);
8487 /* The symbol is imported from another shared
8488 library and does not apply to this one. */
8496 case DT_PREINIT_ARRAYSZ
:
8497 name
= ".preinit_array";
8499 case DT_INIT_ARRAYSZ
:
8500 name
= ".init_array";
8502 case DT_FINI_ARRAYSZ
:
8503 name
= ".fini_array";
8505 o
= bfd_get_section_by_name (abfd
, name
);
8508 (*_bfd_error_handler
)
8509 (_("%B: could not find output section %s"), abfd
, name
);
8513 (*_bfd_error_handler
)
8514 (_("warning: %s section has zero size"), name
);
8515 dyn
.d_un
.d_val
= o
->size
;
8518 case DT_PREINIT_ARRAY
:
8519 name
= ".preinit_array";
8522 name
= ".init_array";
8525 name
= ".fini_array";
8538 name
= ".gnu.version_d";
8541 name
= ".gnu.version_r";
8544 name
= ".gnu.version";
8546 o
= bfd_get_section_by_name (abfd
, name
);
8549 (*_bfd_error_handler
)
8550 (_("%B: could not find output section %s"), abfd
, name
);
8553 dyn
.d_un
.d_ptr
= o
->vma
;
8560 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8565 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8567 Elf_Internal_Shdr
*hdr
;
8569 hdr
= elf_elfsections (abfd
)[i
];
8570 if (hdr
->sh_type
== type
8571 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8573 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8574 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8577 if (dyn
.d_un
.d_val
== 0
8578 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8579 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8585 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8589 /* If we have created any dynamic sections, then output them. */
8592 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8595 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8597 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8599 || o
->output_section
== bfd_abs_section_ptr
)
8601 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8603 /* At this point, we are only interested in sections
8604 created by _bfd_elf_link_create_dynamic_sections. */
8607 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8609 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8611 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8613 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8615 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8617 (file_ptr
) o
->output_offset
,
8623 /* The contents of the .dynstr section are actually in a
8625 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8626 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8627 || ! _bfd_elf_strtab_emit (abfd
,
8628 elf_hash_table (info
)->dynstr
))
8634 if (info
->relocatable
)
8636 bfd_boolean failed
= FALSE
;
8638 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8643 /* If we have optimized stabs strings, output them. */
8644 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8646 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8650 if (info
->eh_frame_hdr
)
8652 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8656 if (finfo
.symstrtab
!= NULL
)
8657 _bfd_stringtab_free (finfo
.symstrtab
);
8658 if (finfo
.contents
!= NULL
)
8659 free (finfo
.contents
);
8660 if (finfo
.external_relocs
!= NULL
)
8661 free (finfo
.external_relocs
);
8662 if (finfo
.internal_relocs
!= NULL
)
8663 free (finfo
.internal_relocs
);
8664 if (finfo
.external_syms
!= NULL
)
8665 free (finfo
.external_syms
);
8666 if (finfo
.locsym_shndx
!= NULL
)
8667 free (finfo
.locsym_shndx
);
8668 if (finfo
.internal_syms
!= NULL
)
8669 free (finfo
.internal_syms
);
8670 if (finfo
.indices
!= NULL
)
8671 free (finfo
.indices
);
8672 if (finfo
.sections
!= NULL
)
8673 free (finfo
.sections
);
8674 if (finfo
.symbuf
!= NULL
)
8675 free (finfo
.symbuf
);
8676 if (finfo
.symshndxbuf
!= NULL
)
8677 free (finfo
.symshndxbuf
);
8678 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8680 if ((o
->flags
& SEC_RELOC
) != 0
8681 && elf_section_data (o
)->rel_hashes
!= NULL
)
8682 free (elf_section_data (o
)->rel_hashes
);
8685 elf_tdata (abfd
)->linker
= TRUE
;
8690 if (finfo
.symstrtab
!= NULL
)
8691 _bfd_stringtab_free (finfo
.symstrtab
);
8692 if (finfo
.contents
!= NULL
)
8693 free (finfo
.contents
);
8694 if (finfo
.external_relocs
!= NULL
)
8695 free (finfo
.external_relocs
);
8696 if (finfo
.internal_relocs
!= NULL
)
8697 free (finfo
.internal_relocs
);
8698 if (finfo
.external_syms
!= NULL
)
8699 free (finfo
.external_syms
);
8700 if (finfo
.locsym_shndx
!= NULL
)
8701 free (finfo
.locsym_shndx
);
8702 if (finfo
.internal_syms
!= NULL
)
8703 free (finfo
.internal_syms
);
8704 if (finfo
.indices
!= NULL
)
8705 free (finfo
.indices
);
8706 if (finfo
.sections
!= NULL
)
8707 free (finfo
.sections
);
8708 if (finfo
.symbuf
!= NULL
)
8709 free (finfo
.symbuf
);
8710 if (finfo
.symshndxbuf
!= NULL
)
8711 free (finfo
.symshndxbuf
);
8712 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8714 if ((o
->flags
& SEC_RELOC
) != 0
8715 && elf_section_data (o
)->rel_hashes
!= NULL
)
8716 free (elf_section_data (o
)->rel_hashes
);
8722 /* Garbage collect unused sections. */
8724 /* The mark phase of garbage collection. For a given section, mark
8725 it and any sections in this section's group, and all the sections
8726 which define symbols to which it refers. */
8728 typedef asection
* (*gc_mark_hook_fn
)
8729 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8730 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8733 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8735 gc_mark_hook_fn gc_mark_hook
)
8739 asection
*group_sec
;
8743 /* Mark all the sections in the group. */
8744 group_sec
= elf_section_data (sec
)->next_in_group
;
8745 if (group_sec
&& !group_sec
->gc_mark
)
8746 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8749 /* Look through the section relocs. */
8751 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8752 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8754 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8755 Elf_Internal_Shdr
*symtab_hdr
;
8756 struct elf_link_hash_entry
**sym_hashes
;
8759 bfd
*input_bfd
= sec
->owner
;
8760 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8761 Elf_Internal_Sym
*isym
= NULL
;
8764 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8765 sym_hashes
= elf_sym_hashes (input_bfd
);
8767 /* Read the local symbols. */
8768 if (elf_bad_symtab (input_bfd
))
8770 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8774 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8776 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8777 if (isym
== NULL
&& nlocsyms
!= 0)
8779 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8785 /* Read the relocations. */
8786 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8788 if (relstart
== NULL
)
8793 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8795 if (bed
->s
->arch_size
== 32)
8800 for (rel
= relstart
; rel
< relend
; rel
++)
8802 unsigned long r_symndx
;
8804 struct elf_link_hash_entry
*h
;
8806 r_symndx
= rel
->r_info
>> r_sym_shift
;
8810 if (r_symndx
>= nlocsyms
8811 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8813 h
= sym_hashes
[r_symndx
- extsymoff
];
8814 while (h
->root
.type
== bfd_link_hash_indirect
8815 || h
->root
.type
== bfd_link_hash_warning
)
8816 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8817 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8821 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8824 if (rsec
&& !rsec
->gc_mark
)
8826 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8829 rsec
->gc_mark_from_eh
= 1;
8830 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8839 if (elf_section_data (sec
)->relocs
!= relstart
)
8842 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8844 if (! info
->keep_memory
)
8847 symtab_hdr
->contents
= (unsigned char *) isym
;
8854 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8856 struct elf_gc_sweep_symbol_info
{
8857 struct bfd_link_info
*info
;
8858 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8863 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8865 if (h
->root
.type
== bfd_link_hash_warning
)
8866 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8868 if ((h
->root
.type
== bfd_link_hash_defined
8869 || h
->root
.type
== bfd_link_hash_defweak
)
8870 && !h
->root
.u
.def
.section
->gc_mark
8871 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8873 struct elf_gc_sweep_symbol_info
*inf
= data
;
8874 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8880 /* The sweep phase of garbage collection. Remove all garbage sections. */
8882 typedef bfd_boolean (*gc_sweep_hook_fn
)
8883 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8886 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
8889 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8890 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
8891 unsigned long section_sym_count
;
8892 struct elf_gc_sweep_symbol_info sweep_info
;
8894 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8898 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8901 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8903 /* Keep debug and special sections. */
8904 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8905 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8911 /* Skip sweeping sections already excluded. */
8912 if (o
->flags
& SEC_EXCLUDE
)
8915 /* Since this is early in the link process, it is simple
8916 to remove a section from the output. */
8917 o
->flags
|= SEC_EXCLUDE
;
8919 /* But we also have to update some of the relocation
8920 info we collected before. */
8922 && (o
->flags
& SEC_RELOC
) != 0
8923 && o
->reloc_count
> 0
8924 && !bfd_is_abs_section (o
->output_section
))
8926 Elf_Internal_Rela
*internal_relocs
;
8930 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8932 if (internal_relocs
== NULL
)
8935 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8937 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8938 free (internal_relocs
);
8946 /* Remove the symbols that were in the swept sections from the dynamic
8947 symbol table. GCFIXME: Anyone know how to get them out of the
8948 static symbol table as well? */
8949 sweep_info
.info
= info
;
8950 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
8951 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
8954 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
8958 /* Propagate collected vtable information. This is called through
8959 elf_link_hash_traverse. */
8962 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8964 if (h
->root
.type
== bfd_link_hash_warning
)
8965 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8967 /* Those that are not vtables. */
8968 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8971 /* Those vtables that do not have parents, we cannot merge. */
8972 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8975 /* If we've already been done, exit. */
8976 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8979 /* Make sure the parent's table is up to date. */
8980 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8982 if (h
->vtable
->used
== NULL
)
8984 /* None of this table's entries were referenced. Re-use the
8986 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8987 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8992 bfd_boolean
*cu
, *pu
;
8994 /* Or the parent's entries into ours. */
8995 cu
= h
->vtable
->used
;
8997 pu
= h
->vtable
->parent
->vtable
->used
;
9000 const struct elf_backend_data
*bed
;
9001 unsigned int log_file_align
;
9003 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9004 log_file_align
= bed
->s
->log_file_align
;
9005 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9020 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9023 bfd_vma hstart
, hend
;
9024 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9025 const struct elf_backend_data
*bed
;
9026 unsigned int log_file_align
;
9028 if (h
->root
.type
== bfd_link_hash_warning
)
9029 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9031 /* Take care of both those symbols that do not describe vtables as
9032 well as those that are not loaded. */
9033 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9036 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9037 || h
->root
.type
== bfd_link_hash_defweak
);
9039 sec
= h
->root
.u
.def
.section
;
9040 hstart
= h
->root
.u
.def
.value
;
9041 hend
= hstart
+ h
->size
;
9043 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9045 return *(bfd_boolean
*) okp
= FALSE
;
9046 bed
= get_elf_backend_data (sec
->owner
);
9047 log_file_align
= bed
->s
->log_file_align
;
9049 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9051 for (rel
= relstart
; rel
< relend
; ++rel
)
9052 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9054 /* If the entry is in use, do nothing. */
9056 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9058 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9059 if (h
->vtable
->used
[entry
])
9062 /* Otherwise, kill it. */
9063 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9069 /* Mark sections containing dynamically referenced symbols. When
9070 building shared libraries, we must assume that any visible symbol is
9074 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9076 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9078 if (h
->root
.type
== bfd_link_hash_warning
)
9079 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9081 if ((h
->root
.type
== bfd_link_hash_defined
9082 || h
->root
.type
== bfd_link_hash_defweak
)
9086 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9087 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9088 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9093 /* Do mark and sweep of unused sections. */
9096 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9098 bfd_boolean ok
= TRUE
;
9100 asection
* (*gc_mark_hook
)
9101 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9102 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9104 if (!get_elf_backend_data (abfd
)->can_gc_sections
9105 || info
->relocatable
9106 || info
->emitrelocations
9107 || !is_elf_hash_table (info
->hash
))
9109 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9113 /* Apply transitive closure to the vtable entry usage info. */
9114 elf_link_hash_traverse (elf_hash_table (info
),
9115 elf_gc_propagate_vtable_entries_used
,
9120 /* Kill the vtable relocations that were not used. */
9121 elf_link_hash_traverse (elf_hash_table (info
),
9122 elf_gc_smash_unused_vtentry_relocs
,
9127 /* Mark dynamically referenced symbols. */
9128 if (elf_hash_table (info
)->dynamic_sections_created
)
9129 elf_link_hash_traverse (elf_hash_table (info
),
9130 elf_gc_mark_dynamic_ref_symbol
,
9133 /* Grovel through relocs to find out who stays ... */
9134 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9135 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9139 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9142 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9143 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9144 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9148 /* ... again for sections marked from eh_frame. */
9149 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9153 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9156 /* Keep .gcc_except_table.* if the associated .text.* is
9157 marked. This isn't very nice, but the proper solution,
9158 splitting .eh_frame up and using comdat doesn't pan out
9159 easily due to needing special relocs to handle the
9160 difference of two symbols in separate sections.
9161 Don't keep code sections referenced by .eh_frame. */
9162 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9163 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9165 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9171 len
= strlen (o
->name
+ 18) + 1;
9172 fn_name
= bfd_malloc (len
+ 6);
9173 if (fn_name
== NULL
)
9175 memcpy (fn_name
, ".text.", 6);
9176 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9177 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9179 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9183 /* If not using specially named exception table section,
9184 then keep whatever we are using. */
9185 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9190 /* ... and mark SEC_EXCLUDE for those that go. */
9191 return elf_gc_sweep (abfd
, info
);
9194 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9197 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9199 struct elf_link_hash_entry
*h
,
9202 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9203 struct elf_link_hash_entry
**search
, *child
;
9204 bfd_size_type extsymcount
;
9205 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9207 /* The sh_info field of the symtab header tells us where the
9208 external symbols start. We don't care about the local symbols at
9210 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9211 if (!elf_bad_symtab (abfd
))
9212 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9214 sym_hashes
= elf_sym_hashes (abfd
);
9215 sym_hashes_end
= sym_hashes
+ extsymcount
;
9217 /* Hunt down the child symbol, which is in this section at the same
9218 offset as the relocation. */
9219 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9221 if ((child
= *search
) != NULL
9222 && (child
->root
.type
== bfd_link_hash_defined
9223 || child
->root
.type
== bfd_link_hash_defweak
)
9224 && child
->root
.u
.def
.section
== sec
9225 && child
->root
.u
.def
.value
== offset
)
9229 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9230 abfd
, sec
, (unsigned long) offset
);
9231 bfd_set_error (bfd_error_invalid_operation
);
9237 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9243 /* This *should* only be the absolute section. It could potentially
9244 be that someone has defined a non-global vtable though, which
9245 would be bad. It isn't worth paging in the local symbols to be
9246 sure though; that case should simply be handled by the assembler. */
9248 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9251 child
->vtable
->parent
= h
;
9256 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9259 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9260 asection
*sec ATTRIBUTE_UNUSED
,
9261 struct elf_link_hash_entry
*h
,
9264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9265 unsigned int log_file_align
= bed
->s
->log_file_align
;
9269 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9274 if (addend
>= h
->vtable
->size
)
9276 size_t size
, bytes
, file_align
;
9277 bfd_boolean
*ptr
= h
->vtable
->used
;
9279 /* While the symbol is undefined, we have to be prepared to handle
9281 file_align
= 1 << log_file_align
;
9282 if (h
->root
.type
== bfd_link_hash_undefined
)
9283 size
= addend
+ file_align
;
9289 /* Oops! We've got a reference past the defined end of
9290 the table. This is probably a bug -- shall we warn? */
9291 size
= addend
+ file_align
;
9294 size
= (size
+ file_align
- 1) & -file_align
;
9296 /* Allocate one extra entry for use as a "done" flag for the
9297 consolidation pass. */
9298 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9302 ptr
= bfd_realloc (ptr
- 1, bytes
);
9308 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9309 * sizeof (bfd_boolean
));
9310 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9314 ptr
= bfd_zmalloc (bytes
);
9319 /* And arrange for that done flag to be at index -1. */
9320 h
->vtable
->used
= ptr
+ 1;
9321 h
->vtable
->size
= size
;
9324 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9329 struct alloc_got_off_arg
{
9331 unsigned int got_elt_size
;
9334 /* We need a special top-level link routine to convert got reference counts
9335 to real got offsets. */
9338 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9340 struct alloc_got_off_arg
*gofarg
= arg
;
9342 if (h
->root
.type
== bfd_link_hash_warning
)
9343 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9345 if (h
->got
.refcount
> 0)
9347 h
->got
.offset
= gofarg
->gotoff
;
9348 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9351 h
->got
.offset
= (bfd_vma
) -1;
9356 /* And an accompanying bit to work out final got entry offsets once
9357 we're done. Should be called from final_link. */
9360 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9361 struct bfd_link_info
*info
)
9364 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9366 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9367 struct alloc_got_off_arg gofarg
;
9369 if (! is_elf_hash_table (info
->hash
))
9372 /* The GOT offset is relative to the .got section, but the GOT header is
9373 put into the .got.plt section, if the backend uses it. */
9374 if (bed
->want_got_plt
)
9377 gotoff
= bed
->got_header_size
;
9379 /* Do the local .got entries first. */
9380 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9382 bfd_signed_vma
*local_got
;
9383 bfd_size_type j
, locsymcount
;
9384 Elf_Internal_Shdr
*symtab_hdr
;
9386 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9389 local_got
= elf_local_got_refcounts (i
);
9393 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9394 if (elf_bad_symtab (i
))
9395 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9397 locsymcount
= symtab_hdr
->sh_info
;
9399 for (j
= 0; j
< locsymcount
; ++j
)
9401 if (local_got
[j
] > 0)
9403 local_got
[j
] = gotoff
;
9404 gotoff
+= got_elt_size
;
9407 local_got
[j
] = (bfd_vma
) -1;
9411 /* Then the global .got entries. .plt refcounts are handled by
9412 adjust_dynamic_symbol */
9413 gofarg
.gotoff
= gotoff
;
9414 gofarg
.got_elt_size
= got_elt_size
;
9415 elf_link_hash_traverse (elf_hash_table (info
),
9416 elf_gc_allocate_got_offsets
,
9421 /* Many folk need no more in the way of final link than this, once
9422 got entry reference counting is enabled. */
9425 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9427 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9430 /* Invoke the regular ELF backend linker to do all the work. */
9431 return bfd_elf_final_link (abfd
, info
);
9435 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9437 struct elf_reloc_cookie
*rcookie
= cookie
;
9439 if (rcookie
->bad_symtab
)
9440 rcookie
->rel
= rcookie
->rels
;
9442 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9444 unsigned long r_symndx
;
9446 if (! rcookie
->bad_symtab
)
9447 if (rcookie
->rel
->r_offset
> offset
)
9449 if (rcookie
->rel
->r_offset
!= offset
)
9452 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9453 if (r_symndx
== SHN_UNDEF
)
9456 if (r_symndx
>= rcookie
->locsymcount
9457 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9459 struct elf_link_hash_entry
*h
;
9461 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9463 while (h
->root
.type
== bfd_link_hash_indirect
9464 || h
->root
.type
== bfd_link_hash_warning
)
9465 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9467 if ((h
->root
.type
== bfd_link_hash_defined
9468 || h
->root
.type
== bfd_link_hash_defweak
)
9469 && elf_discarded_section (h
->root
.u
.def
.section
))
9476 /* It's not a relocation against a global symbol,
9477 but it could be a relocation against a local
9478 symbol for a discarded section. */
9480 Elf_Internal_Sym
*isym
;
9482 /* Need to: get the symbol; get the section. */
9483 isym
= &rcookie
->locsyms
[r_symndx
];
9484 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9486 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9487 if (isec
!= NULL
&& elf_discarded_section (isec
))
9496 /* Discard unneeded references to discarded sections.
9497 Returns TRUE if any section's size was changed. */
9498 /* This function assumes that the relocations are in sorted order,
9499 which is true for all known assemblers. */
9502 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9504 struct elf_reloc_cookie cookie
;
9505 asection
*stab
, *eh
;
9506 Elf_Internal_Shdr
*symtab_hdr
;
9507 const struct elf_backend_data
*bed
;
9510 bfd_boolean ret
= FALSE
;
9512 if (info
->traditional_format
9513 || !is_elf_hash_table (info
->hash
))
9516 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9518 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9521 bed
= get_elf_backend_data (abfd
);
9523 if ((abfd
->flags
& DYNAMIC
) != 0)
9526 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9527 if (info
->relocatable
9530 || bfd_is_abs_section (eh
->output_section
))))
9533 stab
= bfd_get_section_by_name (abfd
, ".stab");
9536 || bfd_is_abs_section (stab
->output_section
)
9537 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9542 && bed
->elf_backend_discard_info
== NULL
)
9545 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9547 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9548 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9549 if (cookie
.bad_symtab
)
9551 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9552 cookie
.extsymoff
= 0;
9556 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9557 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9560 if (bed
->s
->arch_size
== 32)
9561 cookie
.r_sym_shift
= 8;
9563 cookie
.r_sym_shift
= 32;
9565 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9566 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9568 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9569 cookie
.locsymcount
, 0,
9571 if (cookie
.locsyms
== NULL
)
9578 count
= stab
->reloc_count
;
9580 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9582 if (cookie
.rels
!= NULL
)
9584 cookie
.rel
= cookie
.rels
;
9585 cookie
.relend
= cookie
.rels
;
9586 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9587 if (_bfd_discard_section_stabs (abfd
, stab
,
9588 elf_section_data (stab
)->sec_info
,
9589 bfd_elf_reloc_symbol_deleted_p
,
9592 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9600 count
= eh
->reloc_count
;
9602 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9604 cookie
.rel
= cookie
.rels
;
9605 cookie
.relend
= cookie
.rels
;
9606 if (cookie
.rels
!= NULL
)
9607 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9609 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9610 bfd_elf_reloc_symbol_deleted_p
,
9614 if (cookie
.rels
!= NULL
9615 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9619 if (bed
->elf_backend_discard_info
!= NULL
9620 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9623 if (cookie
.locsyms
!= NULL
9624 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9626 if (! info
->keep_memory
)
9627 free (cookie
.locsyms
);
9629 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9633 if (info
->eh_frame_hdr
9634 && !info
->relocatable
9635 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9642 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9645 const char *name
, *p
;
9646 struct bfd_section_already_linked
*l
;
9647 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9650 /* A single member comdat group section may be discarded by a
9651 linkonce section. See below. */
9652 if (sec
->output_section
== bfd_abs_section_ptr
)
9657 /* Check if it belongs to a section group. */
9658 group
= elf_sec_group (sec
);
9660 /* Return if it isn't a linkonce section nor a member of a group. A
9661 comdat group section also has SEC_LINK_ONCE set. */
9662 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9667 /* If this is the member of a single member comdat group, check if
9668 the group should be discarded. */
9669 if (elf_next_in_group (sec
) == sec
9670 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9676 /* FIXME: When doing a relocatable link, we may have trouble
9677 copying relocations in other sections that refer to local symbols
9678 in the section being discarded. Those relocations will have to
9679 be converted somehow; as of this writing I'm not sure that any of
9680 the backends handle that correctly.
9682 It is tempting to instead not discard link once sections when
9683 doing a relocatable link (technically, they should be discarded
9684 whenever we are building constructors). However, that fails,
9685 because the linker winds up combining all the link once sections
9686 into a single large link once section, which defeats the purpose
9687 of having link once sections in the first place.
9689 Also, not merging link once sections in a relocatable link
9690 causes trouble for MIPS ELF, which relies on link once semantics
9691 to handle the .reginfo section correctly. */
9693 name
= bfd_get_section_name (abfd
, sec
);
9695 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9696 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9701 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9703 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9705 /* We may have 3 different sections on the list: group section,
9706 comdat section and linkonce section. SEC may be a linkonce or
9707 group section. We match a group section with a group section,
9708 a linkonce section with a linkonce section, and ignore comdat
9710 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9711 && strcmp (name
, l
->sec
->name
) == 0
9712 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9714 /* The section has already been linked. See if we should
9716 switch (flags
& SEC_LINK_DUPLICATES
)
9721 case SEC_LINK_DUPLICATES_DISCARD
:
9724 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9725 (*_bfd_error_handler
)
9726 (_("%B: ignoring duplicate section `%A'"),
9730 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9731 if (sec
->size
!= l
->sec
->size
)
9732 (*_bfd_error_handler
)
9733 (_("%B: duplicate section `%A' has different size"),
9737 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9738 if (sec
->size
!= l
->sec
->size
)
9739 (*_bfd_error_handler
)
9740 (_("%B: duplicate section `%A' has different size"),
9742 else if (sec
->size
!= 0)
9744 bfd_byte
*sec_contents
, *l_sec_contents
;
9746 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9747 (*_bfd_error_handler
)
9748 (_("%B: warning: could not read contents of section `%A'"),
9750 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9752 (*_bfd_error_handler
)
9753 (_("%B: warning: could not read contents of section `%A'"),
9754 l
->sec
->owner
, l
->sec
);
9755 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9756 (*_bfd_error_handler
)
9757 (_("%B: warning: duplicate section `%A' has different contents"),
9761 free (sec_contents
);
9763 free (l_sec_contents
);
9768 /* Set the output_section field so that lang_add_section
9769 does not create a lang_input_section structure for this
9770 section. Since there might be a symbol in the section
9771 being discarded, we must retain a pointer to the section
9772 which we are really going to use. */
9773 sec
->output_section
= bfd_abs_section_ptr
;
9774 sec
->kept_section
= l
->sec
;
9776 if (flags
& SEC_GROUP
)
9778 asection
*first
= elf_next_in_group (sec
);
9779 asection
*s
= first
;
9783 s
->output_section
= bfd_abs_section_ptr
;
9784 /* Record which group discards it. */
9785 s
->kept_section
= l
->sec
;
9786 s
= elf_next_in_group (s
);
9787 /* These lists are circular. */
9799 /* If this is the member of a single member comdat group and the
9800 group hasn't be discarded, we check if it matches a linkonce
9801 section. We only record the discarded comdat group. Otherwise
9802 the undiscarded group will be discarded incorrectly later since
9803 itself has been recorded. */
9804 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9805 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9806 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9807 && bfd_elf_match_symbols_in_sections (l
->sec
,
9808 elf_next_in_group (sec
)))
9810 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9811 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9812 group
->output_section
= bfd_abs_section_ptr
;
9819 /* There is no direct match. But for linkonce section, we should
9820 check if there is a match with comdat group member. We always
9821 record the linkonce section, discarded or not. */
9822 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9823 if (l
->sec
->flags
& SEC_GROUP
)
9825 asection
*first
= elf_next_in_group (l
->sec
);
9828 && elf_next_in_group (first
) == first
9829 && bfd_elf_match_symbols_in_sections (first
, sec
))
9831 sec
->output_section
= bfd_abs_section_ptr
;
9832 sec
->kept_section
= l
->sec
;
9837 /* This is the first section with this name. Record it. */
9838 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9842 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9844 return sym
->st_shndx
== SHN_COMMON
;
9848 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9854 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9856 return bfd_com_section_ptr
;