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 (bfd
*output_bfd
,
428 struct bfd_link_info
*info
,
433 struct elf_link_hash_entry
*h
;
434 struct elf_link_hash_table
*htab
;
436 if (!is_elf_hash_table (info
->hash
))
439 htab
= elf_hash_table (info
);
440 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
444 /* Since we're defining the symbol, don't let it seem to have not
445 been defined. record_dynamic_symbol and size_dynamic_sections
446 may depend on this. */
447 if (h
->root
.type
== bfd_link_hash_undefweak
448 || h
->root
.type
== bfd_link_hash_undefined
)
450 h
->root
.type
= bfd_link_hash_new
;
451 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
452 bfd_link_repair_undef_list (&htab
->root
);
455 if (h
->root
.type
== bfd_link_hash_new
)
458 /* If this symbol is being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular
460 object, then mark it as undefined so that the generic linker will
461 force the correct value. */
465 h
->root
.type
= bfd_link_hash_undefined
;
467 /* If this symbol is not being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular object,
469 then clear out any version information because the symbol will not be
470 associated with the dynamic object any more. */
474 h
->verinfo
.verdef
= NULL
;
478 if (provide
&& hidden
)
480 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
482 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
483 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
486 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
488 if (!info
->relocatable
490 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
491 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
497 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
500 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
503 /* If this is a weak defined symbol, and we know a corresponding
504 real symbol from the same dynamic object, make sure the real
505 symbol is also made into a dynamic symbol. */
506 if (h
->u
.weakdef
!= NULL
507 && h
->u
.weakdef
->dynindx
== -1)
509 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
517 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
518 success, and 2 on a failure caused by attempting to record a symbol
519 in a discarded section, eg. a discarded link-once section symbol. */
522 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
527 struct elf_link_local_dynamic_entry
*entry
;
528 struct elf_link_hash_table
*eht
;
529 struct elf_strtab_hash
*dynstr
;
530 unsigned long dynstr_index
;
532 Elf_External_Sym_Shndx eshndx
;
533 char esym
[sizeof (Elf64_External_Sym
)];
535 if (! is_elf_hash_table (info
->hash
))
538 /* See if the entry exists already. */
539 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
540 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
543 amt
= sizeof (*entry
);
544 entry
= bfd_alloc (input_bfd
, amt
);
548 /* Go find the symbol, so that we can find it's name. */
549 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
550 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
552 bfd_release (input_bfd
, entry
);
556 if (entry
->isym
.st_shndx
!= SHN_UNDEF
557 && (entry
->isym
.st_shndx
< SHN_LORESERVE
558 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
562 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
563 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
565 /* We can still bfd_release here as nothing has done another
566 bfd_alloc. We can't do this later in this function. */
567 bfd_release (input_bfd
, entry
);
572 name
= (bfd_elf_string_from_elf_section
573 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
574 entry
->isym
.st_name
));
576 dynstr
= elf_hash_table (info
)->dynstr
;
579 /* Create a strtab to hold the dynamic symbol names. */
580 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
585 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
586 if (dynstr_index
== (unsigned long) -1)
588 entry
->isym
.st_name
= dynstr_index
;
590 eht
= elf_hash_table (info
);
592 entry
->next
= eht
->dynlocal
;
593 eht
->dynlocal
= entry
;
594 entry
->input_bfd
= input_bfd
;
595 entry
->input_indx
= input_indx
;
598 /* Whatever binding the symbol had before, it's now local. */
600 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
602 /* The dynindx will be set at the end of size_dynamic_sections. */
607 /* Return the dynindex of a local dynamic symbol. */
610 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
614 struct elf_link_local_dynamic_entry
*e
;
616 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
617 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
622 /* This function is used to renumber the dynamic symbols, if some of
623 them are removed because they are marked as local. This is called
624 via elf_link_hash_traverse. */
627 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
630 size_t *count
= data
;
632 if (h
->root
.type
== bfd_link_hash_warning
)
633 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
638 if (h
->dynindx
!= -1)
639 h
->dynindx
= ++(*count
);
645 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
646 STB_LOCAL binding. */
649 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
652 size_t *count
= data
;
654 if (h
->root
.type
== bfd_link_hash_warning
)
655 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
657 if (!h
->forced_local
)
660 if (h
->dynindx
!= -1)
661 h
->dynindx
= ++(*count
);
666 /* Return true if the dynamic symbol for a given section should be
667 omitted when creating a shared library. */
669 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
670 struct bfd_link_info
*info
,
673 switch (elf_section_data (p
)->this_hdr
.sh_type
)
677 /* If sh_type is yet undecided, assume it could be
678 SHT_PROGBITS/SHT_NOBITS. */
680 if (strcmp (p
->name
, ".got") == 0
681 || strcmp (p
->name
, ".got.plt") == 0
682 || strcmp (p
->name
, ".plt") == 0)
685 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
688 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
689 && (ip
->flags
& SEC_LINKER_CREATED
)
690 && ip
->output_section
== p
)
695 /* There shouldn't be section relative relocations
696 against any other section. */
702 /* Assign dynsym indices. In a shared library we generate a section
703 symbol for each output section, which come first. Next come symbols
704 which have been forced to local binding. Then all of the back-end
705 allocated local dynamic syms, followed by the rest of the global
709 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
710 struct bfd_link_info
*info
,
711 unsigned long *section_sym_count
)
713 unsigned long dynsymcount
= 0;
715 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
717 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
719 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
720 if ((p
->flags
& SEC_EXCLUDE
) == 0
721 && (p
->flags
& SEC_ALLOC
) != 0
722 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
723 elf_section_data (p
)->dynindx
= ++dynsymcount
;
725 *section_sym_count
= dynsymcount
;
727 elf_link_hash_traverse (elf_hash_table (info
),
728 elf_link_renumber_local_hash_table_dynsyms
,
731 if (elf_hash_table (info
)->dynlocal
)
733 struct elf_link_local_dynamic_entry
*p
;
734 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
735 p
->dynindx
= ++dynsymcount
;
738 elf_link_hash_traverse (elf_hash_table (info
),
739 elf_link_renumber_hash_table_dynsyms
,
742 /* There is an unused NULL entry at the head of the table which
743 we must account for in our count. Unless there weren't any
744 symbols, which means we'll have no table at all. */
745 if (dynsymcount
!= 0)
748 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
752 /* This function is called when we want to define a new symbol. It
753 handles the various cases which arise when we find a definition in
754 a dynamic object, or when there is already a definition in a
755 dynamic object. The new symbol is described by NAME, SYM, PSEC,
756 and PVALUE. We set SYM_HASH to the hash table entry. We set
757 OVERRIDE if the old symbol is overriding a new definition. We set
758 TYPE_CHANGE_OK if it is OK for the type to change. We set
759 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
760 change, we mean that we shouldn't warn if the type or size does
761 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
762 object is overridden by a regular object. */
765 _bfd_elf_merge_symbol (bfd
*abfd
,
766 struct bfd_link_info
*info
,
768 Elf_Internal_Sym
*sym
,
771 unsigned int *pold_alignment
,
772 struct elf_link_hash_entry
**sym_hash
,
774 bfd_boolean
*override
,
775 bfd_boolean
*type_change_ok
,
776 bfd_boolean
*size_change_ok
)
778 asection
*sec
, *oldsec
;
779 struct elf_link_hash_entry
*h
;
780 struct elf_link_hash_entry
*flip
;
783 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
784 bfd_boolean newweak
, oldweak
;
785 const struct elf_backend_data
*bed
;
791 bind
= ELF_ST_BIND (sym
->st_info
);
793 if (! bfd_is_und_section (sec
))
794 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
796 h
= ((struct elf_link_hash_entry
*)
797 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
802 /* This code is for coping with dynamic objects, and is only useful
803 if we are doing an ELF link. */
804 if (info
->hash
->creator
!= abfd
->xvec
)
807 /* For merging, we only care about real symbols. */
809 while (h
->root
.type
== bfd_link_hash_indirect
810 || h
->root
.type
== bfd_link_hash_warning
)
811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
813 /* If we just created the symbol, mark it as being an ELF symbol.
814 Other than that, there is nothing to do--there is no merge issue
815 with a newly defined symbol--so we just return. */
817 if (h
->root
.type
== bfd_link_hash_new
)
823 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
826 switch (h
->root
.type
)
833 case bfd_link_hash_undefined
:
834 case bfd_link_hash_undefweak
:
835 oldbfd
= h
->root
.u
.undef
.abfd
;
839 case bfd_link_hash_defined
:
840 case bfd_link_hash_defweak
:
841 oldbfd
= h
->root
.u
.def
.section
->owner
;
842 oldsec
= h
->root
.u
.def
.section
;
845 case bfd_link_hash_common
:
846 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
847 oldsec
= h
->root
.u
.c
.p
->section
;
851 /* In cases involving weak versioned symbols, we may wind up trying
852 to merge a symbol with itself. Catch that here, to avoid the
853 confusion that results if we try to override a symbol with
854 itself. The additional tests catch cases like
855 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
856 dynamic object, which we do want to handle here. */
858 && ((abfd
->flags
& DYNAMIC
) == 0
862 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
863 respectively, is from a dynamic object. */
865 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
869 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
870 else if (oldsec
!= NULL
)
872 /* This handles the special SHN_MIPS_{TEXT,DATA} section
873 indices used by MIPS ELF. */
874 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
877 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
878 respectively, appear to be a definition rather than reference. */
880 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
882 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
883 && h
->root
.type
!= bfd_link_hash_undefweak
884 && h
->root
.type
!= bfd_link_hash_common
);
886 /* Check TLS symbol. We don't check undefined symbol introduced by
888 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
889 && ELF_ST_TYPE (sym
->st_info
) != h
->type
893 bfd_boolean ntdef
, tdef
;
894 asection
*ntsec
, *tsec
;
896 if (h
->type
== STT_TLS
)
916 (*_bfd_error_handler
)
917 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
918 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
919 else if (!tdef
&& !ntdef
)
920 (*_bfd_error_handler
)
921 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
922 tbfd
, ntbfd
, h
->root
.root
.string
);
924 (*_bfd_error_handler
)
925 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
926 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
928 (*_bfd_error_handler
)
929 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
930 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
932 bfd_set_error (bfd_error_bad_value
);
936 /* We need to remember if a symbol has a definition in a dynamic
937 object or is weak in all dynamic objects. Internal and hidden
938 visibility will make it unavailable to dynamic objects. */
939 if (newdyn
&& !h
->dynamic_def
)
941 if (!bfd_is_und_section (sec
))
945 /* Check if this symbol is weak in all dynamic objects. If it
946 is the first time we see it in a dynamic object, we mark
947 if it is weak. Otherwise, we clear it. */
950 if (bind
== STB_WEAK
)
953 else if (bind
!= STB_WEAK
)
958 /* If the old symbol has non-default visibility, we ignore the new
959 definition from a dynamic object. */
961 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
962 && !bfd_is_und_section (sec
))
965 /* Make sure this symbol is dynamic. */
967 /* A protected symbol has external availability. Make sure it is
970 FIXME: Should we check type and size for protected symbol? */
971 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
972 return bfd_elf_link_record_dynamic_symbol (info
, h
);
977 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
980 /* If the new symbol with non-default visibility comes from a
981 relocatable file and the old definition comes from a dynamic
982 object, we remove the old definition. */
983 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
986 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
987 && bfd_is_und_section (sec
))
989 /* If the new symbol is undefined and the old symbol was
990 also undefined before, we need to make sure
991 _bfd_generic_link_add_one_symbol doesn't mess
992 up the linker hash table undefs list. Since the old
993 definition came from a dynamic object, it is still on the
995 h
->root
.type
= bfd_link_hash_undefined
;
996 h
->root
.u
.undef
.abfd
= abfd
;
1000 h
->root
.type
= bfd_link_hash_new
;
1001 h
->root
.u
.undef
.abfd
= NULL
;
1010 /* FIXME: Should we check type and size for protected symbol? */
1016 /* Differentiate strong and weak symbols. */
1017 newweak
= bind
== STB_WEAK
;
1018 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1019 || h
->root
.type
== bfd_link_hash_undefweak
);
1021 /* If a new weak symbol definition comes from a regular file and the
1022 old symbol comes from a dynamic library, we treat the new one as
1023 strong. Similarly, an old weak symbol definition from a regular
1024 file is treated as strong when the new symbol comes from a dynamic
1025 library. Further, an old weak symbol from a dynamic library is
1026 treated as strong if the new symbol is from a dynamic library.
1027 This reflects the way glibc's ld.so works.
1029 Do this before setting *type_change_ok or *size_change_ok so that
1030 we warn properly when dynamic library symbols are overridden. */
1032 if (newdef
&& !newdyn
&& olddyn
)
1034 if (olddef
&& newdyn
)
1037 /* It's OK to change the type if either the existing symbol or the
1038 new symbol is weak. A type change is also OK if the old symbol
1039 is undefined and the new symbol is defined. */
1044 && h
->root
.type
== bfd_link_hash_undefined
))
1045 *type_change_ok
= TRUE
;
1047 /* It's OK to change the size if either the existing symbol or the
1048 new symbol is weak, or if the old symbol is undefined. */
1051 || h
->root
.type
== bfd_link_hash_undefined
)
1052 *size_change_ok
= TRUE
;
1054 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1055 symbol, respectively, appears to be a common symbol in a dynamic
1056 object. If a symbol appears in an uninitialized section, and is
1057 not weak, and is not a function, then it may be a common symbol
1058 which was resolved when the dynamic object was created. We want
1059 to treat such symbols specially, because they raise special
1060 considerations when setting the symbol size: if the symbol
1061 appears as a common symbol in a regular object, and the size in
1062 the regular object is larger, we must make sure that we use the
1063 larger size. This problematic case can always be avoided in C,
1064 but it must be handled correctly when using Fortran shared
1067 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1068 likewise for OLDDYNCOMMON and OLDDEF.
1070 Note that this test is just a heuristic, and that it is quite
1071 possible to have an uninitialized symbol in a shared object which
1072 is really a definition, rather than a common symbol. This could
1073 lead to some minor confusion when the symbol really is a common
1074 symbol in some regular object. However, I think it will be
1080 && (sec
->flags
& SEC_ALLOC
) != 0
1081 && (sec
->flags
& SEC_LOAD
) == 0
1083 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1084 newdyncommon
= TRUE
;
1086 newdyncommon
= FALSE
;
1090 && h
->root
.type
== bfd_link_hash_defined
1092 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1093 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1095 && h
->type
!= STT_FUNC
)
1096 olddyncommon
= TRUE
;
1098 olddyncommon
= FALSE
;
1100 /* We now know everything about the old and new symbols. We ask the
1101 backend to check if we can merge them. */
1102 bed
= get_elf_backend_data (abfd
);
1103 if (bed
->merge_symbol
1104 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1105 pold_alignment
, skip
, override
,
1106 type_change_ok
, size_change_ok
,
1107 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1109 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1113 /* If both the old and the new symbols look like common symbols in a
1114 dynamic object, set the size of the symbol to the larger of the
1119 && sym
->st_size
!= h
->size
)
1121 /* Since we think we have two common symbols, issue a multiple
1122 common warning if desired. Note that we only warn if the
1123 size is different. If the size is the same, we simply let
1124 the old symbol override the new one as normally happens with
1125 symbols defined in dynamic objects. */
1127 if (! ((*info
->callbacks
->multiple_common
)
1128 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1129 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1132 if (sym
->st_size
> h
->size
)
1133 h
->size
= sym
->st_size
;
1135 *size_change_ok
= TRUE
;
1138 /* If we are looking at a dynamic object, and we have found a
1139 definition, we need to see if the symbol was already defined by
1140 some other object. If so, we want to use the existing
1141 definition, and we do not want to report a multiple symbol
1142 definition error; we do this by clobbering *PSEC to be
1143 bfd_und_section_ptr.
1145 We treat a common symbol as a definition if the symbol in the
1146 shared library is a function, since common symbols always
1147 represent variables; this can cause confusion in principle, but
1148 any such confusion would seem to indicate an erroneous program or
1149 shared library. We also permit a common symbol in a regular
1150 object to override a weak symbol in a shared object. */
1155 || (h
->root
.type
== bfd_link_hash_common
1157 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1161 newdyncommon
= FALSE
;
1163 *psec
= sec
= bfd_und_section_ptr
;
1164 *size_change_ok
= TRUE
;
1166 /* If we get here when the old symbol is a common symbol, then
1167 we are explicitly letting it override a weak symbol or
1168 function in a dynamic object, and we don't want to warn about
1169 a type change. If the old symbol is a defined symbol, a type
1170 change warning may still be appropriate. */
1172 if (h
->root
.type
== bfd_link_hash_common
)
1173 *type_change_ok
= TRUE
;
1176 /* Handle the special case of an old common symbol merging with a
1177 new symbol which looks like a common symbol in a shared object.
1178 We change *PSEC and *PVALUE to make the new symbol look like a
1179 common symbol, and let _bfd_generic_link_add_one_symbol do the
1183 && h
->root
.type
== bfd_link_hash_common
)
1187 newdyncommon
= FALSE
;
1188 *pvalue
= sym
->st_size
;
1189 *psec
= sec
= bed
->common_section (oldsec
);
1190 *size_change_ok
= TRUE
;
1193 /* Skip weak definitions of symbols that are already defined. */
1194 if (newdef
&& olddef
&& newweak
)
1197 /* If the old symbol is from a dynamic object, and the new symbol is
1198 a definition which is not from a dynamic object, then the new
1199 symbol overrides the old symbol. Symbols from regular files
1200 always take precedence over symbols from dynamic objects, even if
1201 they are defined after the dynamic object in the link.
1203 As above, we again permit a common symbol in a regular object to
1204 override a definition in a shared object if the shared object
1205 symbol is a function or is weak. */
1210 || (bfd_is_com_section (sec
)
1212 || h
->type
== STT_FUNC
)))
1217 /* Change the hash table entry to undefined, and let
1218 _bfd_generic_link_add_one_symbol do the right thing with the
1221 h
->root
.type
= bfd_link_hash_undefined
;
1222 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1223 *size_change_ok
= TRUE
;
1226 olddyncommon
= FALSE
;
1228 /* We again permit a type change when a common symbol may be
1229 overriding a function. */
1231 if (bfd_is_com_section (sec
))
1232 *type_change_ok
= TRUE
;
1234 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1237 /* This union may have been set to be non-NULL when this symbol
1238 was seen in a dynamic object. We must force the union to be
1239 NULL, so that it is correct for a regular symbol. */
1240 h
->verinfo
.vertree
= NULL
;
1243 /* Handle the special case of a new common symbol merging with an
1244 old symbol that looks like it might be a common symbol defined in
1245 a shared object. Note that we have already handled the case in
1246 which a new common symbol should simply override the definition
1247 in the shared library. */
1250 && bfd_is_com_section (sec
)
1253 /* It would be best if we could set the hash table entry to a
1254 common symbol, but we don't know what to use for the section
1255 or the alignment. */
1256 if (! ((*info
->callbacks
->multiple_common
)
1257 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1258 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1261 /* If the presumed common symbol in the dynamic object is
1262 larger, pretend that the new symbol has its size. */
1264 if (h
->size
> *pvalue
)
1267 /* We need to remember the alignment required by the symbol
1268 in the dynamic object. */
1269 BFD_ASSERT (pold_alignment
);
1270 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1273 olddyncommon
= FALSE
;
1275 h
->root
.type
= bfd_link_hash_undefined
;
1276 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1278 *size_change_ok
= TRUE
;
1279 *type_change_ok
= TRUE
;
1281 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1284 h
->verinfo
.vertree
= NULL
;
1289 /* Handle the case where we had a versioned symbol in a dynamic
1290 library and now find a definition in a normal object. In this
1291 case, we make the versioned symbol point to the normal one. */
1292 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1293 flip
->root
.type
= h
->root
.type
;
1294 h
->root
.type
= bfd_link_hash_indirect
;
1295 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1296 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1297 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1301 flip
->ref_dynamic
= 1;
1308 /* This function is called to create an indirect symbol from the
1309 default for the symbol with the default version if needed. The
1310 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1311 set DYNSYM if the new indirect symbol is dynamic. */
1314 _bfd_elf_add_default_symbol (bfd
*abfd
,
1315 struct bfd_link_info
*info
,
1316 struct elf_link_hash_entry
*h
,
1318 Elf_Internal_Sym
*sym
,
1321 bfd_boolean
*dynsym
,
1322 bfd_boolean override
)
1324 bfd_boolean type_change_ok
;
1325 bfd_boolean size_change_ok
;
1328 struct elf_link_hash_entry
*hi
;
1329 struct bfd_link_hash_entry
*bh
;
1330 const struct elf_backend_data
*bed
;
1331 bfd_boolean collect
;
1332 bfd_boolean dynamic
;
1334 size_t len
, shortlen
;
1337 /* If this symbol has a version, and it is the default version, we
1338 create an indirect symbol from the default name to the fully
1339 decorated name. This will cause external references which do not
1340 specify a version to be bound to this version of the symbol. */
1341 p
= strchr (name
, ELF_VER_CHR
);
1342 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1347 /* We are overridden by an old definition. We need to check if we
1348 need to create the indirect symbol from the default name. */
1349 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1351 BFD_ASSERT (hi
!= NULL
);
1354 while (hi
->root
.type
== bfd_link_hash_indirect
1355 || hi
->root
.type
== bfd_link_hash_warning
)
1357 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1363 bed
= get_elf_backend_data (abfd
);
1364 collect
= bed
->collect
;
1365 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1367 shortlen
= p
- name
;
1368 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1369 if (shortname
== NULL
)
1371 memcpy (shortname
, name
, shortlen
);
1372 shortname
[shortlen
] = '\0';
1374 /* We are going to create a new symbol. Merge it with any existing
1375 symbol with this name. For the purposes of the merge, act as
1376 though we were defining the symbol we just defined, although we
1377 actually going to define an indirect symbol. */
1378 type_change_ok
= FALSE
;
1379 size_change_ok
= FALSE
;
1381 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1382 NULL
, &hi
, &skip
, &override
,
1383 &type_change_ok
, &size_change_ok
))
1392 if (! (_bfd_generic_link_add_one_symbol
1393 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1394 0, name
, FALSE
, collect
, &bh
)))
1396 hi
= (struct elf_link_hash_entry
*) bh
;
1400 /* In this case the symbol named SHORTNAME is overriding the
1401 indirect symbol we want to add. We were planning on making
1402 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1403 is the name without a version. NAME is the fully versioned
1404 name, and it is the default version.
1406 Overriding means that we already saw a definition for the
1407 symbol SHORTNAME in a regular object, and it is overriding
1408 the symbol defined in the dynamic object.
1410 When this happens, we actually want to change NAME, the
1411 symbol we just added, to refer to SHORTNAME. This will cause
1412 references to NAME in the shared object to become references
1413 to SHORTNAME in the regular object. This is what we expect
1414 when we override a function in a shared object: that the
1415 references in the shared object will be mapped to the
1416 definition in the regular object. */
1418 while (hi
->root
.type
== bfd_link_hash_indirect
1419 || hi
->root
.type
== bfd_link_hash_warning
)
1420 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1422 h
->root
.type
= bfd_link_hash_indirect
;
1423 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1427 hi
->ref_dynamic
= 1;
1431 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1436 /* Now set HI to H, so that the following code will set the
1437 other fields correctly. */
1441 /* If there is a duplicate definition somewhere, then HI may not
1442 point to an indirect symbol. We will have reported an error to
1443 the user in that case. */
1445 if (hi
->root
.type
== bfd_link_hash_indirect
)
1447 struct elf_link_hash_entry
*ht
;
1449 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1450 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1452 /* See if the new flags lead us to realize that the symbol must
1464 if (hi
->ref_regular
)
1470 /* We also need to define an indirection from the nondefault version
1474 len
= strlen (name
);
1475 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1476 if (shortname
== NULL
)
1478 memcpy (shortname
, name
, shortlen
);
1479 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1481 /* Once again, merge with any existing symbol. */
1482 type_change_ok
= FALSE
;
1483 size_change_ok
= FALSE
;
1485 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1486 NULL
, &hi
, &skip
, &override
,
1487 &type_change_ok
, &size_change_ok
))
1495 /* Here SHORTNAME is a versioned name, so we don't expect to see
1496 the type of override we do in the case above unless it is
1497 overridden by a versioned definition. */
1498 if (hi
->root
.type
!= bfd_link_hash_defined
1499 && hi
->root
.type
!= bfd_link_hash_defweak
)
1500 (*_bfd_error_handler
)
1501 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1507 if (! (_bfd_generic_link_add_one_symbol
1508 (info
, abfd
, shortname
, BSF_INDIRECT
,
1509 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1511 hi
= (struct elf_link_hash_entry
*) bh
;
1513 /* If there is a duplicate definition somewhere, then HI may not
1514 point to an indirect symbol. We will have reported an error
1515 to the user in that case. */
1517 if (hi
->root
.type
== bfd_link_hash_indirect
)
1519 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1521 /* See if the new flags lead us to realize that the symbol
1533 if (hi
->ref_regular
)
1543 /* This routine is used to export all defined symbols into the dynamic
1544 symbol table. It is called via elf_link_hash_traverse. */
1547 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1549 struct elf_info_failed
*eif
= data
;
1551 /* Ignore indirect symbols. These are added by the versioning code. */
1552 if (h
->root
.type
== bfd_link_hash_indirect
)
1555 if (h
->root
.type
== bfd_link_hash_warning
)
1556 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1558 if (h
->dynindx
== -1
1562 struct bfd_elf_version_tree
*t
;
1563 struct bfd_elf_version_expr
*d
;
1565 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1567 if (t
->globals
.list
!= NULL
)
1569 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1574 if (t
->locals
.list
!= NULL
)
1576 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1585 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1596 /* Look through the symbols which are defined in other shared
1597 libraries and referenced here. Update the list of version
1598 dependencies. This will be put into the .gnu.version_r section.
1599 This function is called via elf_link_hash_traverse. */
1602 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1605 struct elf_find_verdep_info
*rinfo
= data
;
1606 Elf_Internal_Verneed
*t
;
1607 Elf_Internal_Vernaux
*a
;
1610 if (h
->root
.type
== bfd_link_hash_warning
)
1611 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1613 /* We only care about symbols defined in shared objects with version
1618 || h
->verinfo
.verdef
== NULL
)
1621 /* See if we already know about this version. */
1622 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1624 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1627 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1628 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1634 /* This is a new version. Add it to tree we are building. */
1639 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1642 rinfo
->failed
= TRUE
;
1646 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1647 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1648 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1652 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1654 /* Note that we are copying a string pointer here, and testing it
1655 above. If bfd_elf_string_from_elf_section is ever changed to
1656 discard the string data when low in memory, this will have to be
1658 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1660 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1661 a
->vna_nextptr
= t
->vn_auxptr
;
1663 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1666 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1673 /* Figure out appropriate versions for all the symbols. We may not
1674 have the version number script until we have read all of the input
1675 files, so until that point we don't know which symbols should be
1676 local. This function is called via elf_link_hash_traverse. */
1679 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1681 struct elf_assign_sym_version_info
*sinfo
;
1682 struct bfd_link_info
*info
;
1683 const struct elf_backend_data
*bed
;
1684 struct elf_info_failed eif
;
1691 if (h
->root
.type
== bfd_link_hash_warning
)
1692 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1694 /* Fix the symbol flags. */
1697 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1700 sinfo
->failed
= TRUE
;
1704 /* We only need version numbers for symbols defined in regular
1706 if (!h
->def_regular
)
1709 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1710 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1711 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1713 struct bfd_elf_version_tree
*t
;
1718 /* There are two consecutive ELF_VER_CHR characters if this is
1719 not a hidden symbol. */
1721 if (*p
== ELF_VER_CHR
)
1727 /* If there is no version string, we can just return out. */
1735 /* Look for the version. If we find it, it is no longer weak. */
1736 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1738 if (strcmp (t
->name
, p
) == 0)
1742 struct bfd_elf_version_expr
*d
;
1744 len
= p
- h
->root
.root
.string
;
1745 alc
= bfd_malloc (len
);
1748 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1749 alc
[len
- 1] = '\0';
1750 if (alc
[len
- 2] == ELF_VER_CHR
)
1751 alc
[len
- 2] = '\0';
1753 h
->verinfo
.vertree
= t
;
1757 if (t
->globals
.list
!= NULL
)
1758 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1760 /* See if there is anything to force this symbol to
1762 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1764 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1767 && ! info
->export_dynamic
)
1768 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1776 /* If we are building an application, we need to create a
1777 version node for this version. */
1778 if (t
== NULL
&& info
->executable
)
1780 struct bfd_elf_version_tree
**pp
;
1783 /* If we aren't going to export this symbol, we don't need
1784 to worry about it. */
1785 if (h
->dynindx
== -1)
1789 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1792 sinfo
->failed
= TRUE
;
1797 t
->name_indx
= (unsigned int) -1;
1801 /* Don't count anonymous version tag. */
1802 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1804 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1806 t
->vernum
= version_index
;
1810 h
->verinfo
.vertree
= t
;
1814 /* We could not find the version for a symbol when
1815 generating a shared archive. Return an error. */
1816 (*_bfd_error_handler
)
1817 (_("%B: undefined versioned symbol name %s"),
1818 sinfo
->output_bfd
, h
->root
.root
.string
);
1819 bfd_set_error (bfd_error_bad_value
);
1820 sinfo
->failed
= TRUE
;
1828 /* If we don't have a version for this symbol, see if we can find
1830 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1832 struct bfd_elf_version_tree
*t
;
1833 struct bfd_elf_version_tree
*local_ver
;
1834 struct bfd_elf_version_expr
*d
;
1836 /* See if can find what version this symbol is in. If the
1837 symbol is supposed to be local, then don't actually register
1840 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1842 if (t
->globals
.list
!= NULL
)
1844 bfd_boolean matched
;
1848 while ((d
= (*t
->match
) (&t
->globals
, d
,
1849 h
->root
.root
.string
)) != NULL
)
1854 /* There is a version without definition. Make
1855 the symbol the default definition for this
1857 h
->verinfo
.vertree
= t
;
1865 /* There is no undefined version for this symbol. Hide the
1867 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1870 if (t
->locals
.list
!= NULL
)
1873 while ((d
= (*t
->match
) (&t
->locals
, d
,
1874 h
->root
.root
.string
)) != NULL
)
1877 /* If the match is "*", keep looking for a more
1878 explicit, perhaps even global, match.
1879 XXX: Shouldn't this be !d->wildcard instead? */
1880 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1889 if (local_ver
!= NULL
)
1891 h
->verinfo
.vertree
= local_ver
;
1892 if (h
->dynindx
!= -1
1893 && ! info
->export_dynamic
)
1895 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1903 /* Read and swap the relocs from the section indicated by SHDR. This
1904 may be either a REL or a RELA section. The relocations are
1905 translated into RELA relocations and stored in INTERNAL_RELOCS,
1906 which should have already been allocated to contain enough space.
1907 The EXTERNAL_RELOCS are a buffer where the external form of the
1908 relocations should be stored.
1910 Returns FALSE if something goes wrong. */
1913 elf_link_read_relocs_from_section (bfd
*abfd
,
1915 Elf_Internal_Shdr
*shdr
,
1916 void *external_relocs
,
1917 Elf_Internal_Rela
*internal_relocs
)
1919 const struct elf_backend_data
*bed
;
1920 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1921 const bfd_byte
*erela
;
1922 const bfd_byte
*erelaend
;
1923 Elf_Internal_Rela
*irela
;
1924 Elf_Internal_Shdr
*symtab_hdr
;
1927 /* Position ourselves at the start of the section. */
1928 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1931 /* Read the relocations. */
1932 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1935 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1936 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1938 bed
= get_elf_backend_data (abfd
);
1940 /* Convert the external relocations to the internal format. */
1941 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1942 swap_in
= bed
->s
->swap_reloc_in
;
1943 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1944 swap_in
= bed
->s
->swap_reloca_in
;
1947 bfd_set_error (bfd_error_wrong_format
);
1951 erela
= external_relocs
;
1952 erelaend
= erela
+ shdr
->sh_size
;
1953 irela
= internal_relocs
;
1954 while (erela
< erelaend
)
1958 (*swap_in
) (abfd
, erela
, irela
);
1959 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1960 if (bed
->s
->arch_size
== 64)
1962 if ((size_t) r_symndx
>= nsyms
)
1964 (*_bfd_error_handler
)
1965 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1966 " for offset 0x%lx in section `%A'"),
1968 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1969 bfd_set_error (bfd_error_bad_value
);
1972 irela
+= bed
->s
->int_rels_per_ext_rel
;
1973 erela
+= shdr
->sh_entsize
;
1979 /* Read and swap the relocs for a section O. They may have been
1980 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1981 not NULL, they are used as buffers to read into. They are known to
1982 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1983 the return value is allocated using either malloc or bfd_alloc,
1984 according to the KEEP_MEMORY argument. If O has two relocation
1985 sections (both REL and RELA relocations), then the REL_HDR
1986 relocations will appear first in INTERNAL_RELOCS, followed by the
1987 REL_HDR2 relocations. */
1990 _bfd_elf_link_read_relocs (bfd
*abfd
,
1992 void *external_relocs
,
1993 Elf_Internal_Rela
*internal_relocs
,
1994 bfd_boolean keep_memory
)
1996 Elf_Internal_Shdr
*rel_hdr
;
1997 void *alloc1
= NULL
;
1998 Elf_Internal_Rela
*alloc2
= NULL
;
1999 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2001 if (elf_section_data (o
)->relocs
!= NULL
)
2002 return elf_section_data (o
)->relocs
;
2004 if (o
->reloc_count
== 0)
2007 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2009 if (internal_relocs
== NULL
)
2013 size
= o
->reloc_count
;
2014 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2016 internal_relocs
= bfd_alloc (abfd
, size
);
2018 internal_relocs
= alloc2
= bfd_malloc (size
);
2019 if (internal_relocs
== NULL
)
2023 if (external_relocs
== NULL
)
2025 bfd_size_type size
= rel_hdr
->sh_size
;
2027 if (elf_section_data (o
)->rel_hdr2
)
2028 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2029 alloc1
= bfd_malloc (size
);
2032 external_relocs
= alloc1
;
2035 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2039 if (elf_section_data (o
)->rel_hdr2
2040 && (!elf_link_read_relocs_from_section
2042 elf_section_data (o
)->rel_hdr2
,
2043 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2044 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2045 * bed
->s
->int_rels_per_ext_rel
))))
2048 /* Cache the results for next time, if we can. */
2050 elf_section_data (o
)->relocs
= internal_relocs
;
2055 /* Don't free alloc2, since if it was allocated we are passing it
2056 back (under the name of internal_relocs). */
2058 return internal_relocs
;
2068 /* Compute the size of, and allocate space for, REL_HDR which is the
2069 section header for a section containing relocations for O. */
2072 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2073 Elf_Internal_Shdr
*rel_hdr
,
2076 bfd_size_type reloc_count
;
2077 bfd_size_type num_rel_hashes
;
2079 /* Figure out how many relocations there will be. */
2080 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2081 reloc_count
= elf_section_data (o
)->rel_count
;
2083 reloc_count
= elf_section_data (o
)->rel_count2
;
2085 num_rel_hashes
= o
->reloc_count
;
2086 if (num_rel_hashes
< reloc_count
)
2087 num_rel_hashes
= reloc_count
;
2089 /* That allows us to calculate the size of the section. */
2090 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2092 /* The contents field must last into write_object_contents, so we
2093 allocate it with bfd_alloc rather than malloc. Also since we
2094 cannot be sure that the contents will actually be filled in,
2095 we zero the allocated space. */
2096 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2097 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2100 /* We only allocate one set of hash entries, so we only do it the
2101 first time we are called. */
2102 if (elf_section_data (o
)->rel_hashes
== NULL
2105 struct elf_link_hash_entry
**p
;
2107 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2111 elf_section_data (o
)->rel_hashes
= p
;
2117 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2118 originated from the section given by INPUT_REL_HDR) to the
2122 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2123 asection
*input_section
,
2124 Elf_Internal_Shdr
*input_rel_hdr
,
2125 Elf_Internal_Rela
*internal_relocs
,
2126 struct elf_link_hash_entry
**rel_hash
2129 Elf_Internal_Rela
*irela
;
2130 Elf_Internal_Rela
*irelaend
;
2132 Elf_Internal_Shdr
*output_rel_hdr
;
2133 asection
*output_section
;
2134 unsigned int *rel_countp
= NULL
;
2135 const struct elf_backend_data
*bed
;
2136 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2138 output_section
= input_section
->output_section
;
2139 output_rel_hdr
= NULL
;
2141 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2142 == input_rel_hdr
->sh_entsize
)
2144 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2145 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2147 else if (elf_section_data (output_section
)->rel_hdr2
2148 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2149 == input_rel_hdr
->sh_entsize
))
2151 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2152 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2156 (*_bfd_error_handler
)
2157 (_("%B: relocation size mismatch in %B section %A"),
2158 output_bfd
, input_section
->owner
, input_section
);
2159 bfd_set_error (bfd_error_wrong_object_format
);
2163 bed
= get_elf_backend_data (output_bfd
);
2164 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2165 swap_out
= bed
->s
->swap_reloc_out
;
2166 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2167 swap_out
= bed
->s
->swap_reloca_out
;
2171 erel
= output_rel_hdr
->contents
;
2172 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2173 irela
= internal_relocs
;
2174 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2175 * bed
->s
->int_rels_per_ext_rel
);
2176 while (irela
< irelaend
)
2178 (*swap_out
) (output_bfd
, irela
, erel
);
2179 irela
+= bed
->s
->int_rels_per_ext_rel
;
2180 erel
+= input_rel_hdr
->sh_entsize
;
2183 /* Bump the counter, so that we know where to add the next set of
2185 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2190 /* Fix up the flags for a symbol. This handles various cases which
2191 can only be fixed after all the input files are seen. This is
2192 currently called by both adjust_dynamic_symbol and
2193 assign_sym_version, which is unnecessary but perhaps more robust in
2194 the face of future changes. */
2197 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2198 struct elf_info_failed
*eif
)
2200 /* If this symbol was mentioned in a non-ELF file, try to set
2201 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2202 permit a non-ELF file to correctly refer to a symbol defined in
2203 an ELF dynamic object. */
2206 while (h
->root
.type
== bfd_link_hash_indirect
)
2207 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2209 if (h
->root
.type
!= bfd_link_hash_defined
2210 && h
->root
.type
!= bfd_link_hash_defweak
)
2213 h
->ref_regular_nonweak
= 1;
2217 if (h
->root
.u
.def
.section
->owner
!= NULL
2218 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2219 == bfd_target_elf_flavour
))
2222 h
->ref_regular_nonweak
= 1;
2228 if (h
->dynindx
== -1
2232 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2241 /* Unfortunately, NON_ELF is only correct if the symbol
2242 was first seen in a non-ELF file. Fortunately, if the symbol
2243 was first seen in an ELF file, we're probably OK unless the
2244 symbol was defined in a non-ELF file. Catch that case here.
2245 FIXME: We're still in trouble if the symbol was first seen in
2246 a dynamic object, and then later in a non-ELF regular object. */
2247 if ((h
->root
.type
== bfd_link_hash_defined
2248 || h
->root
.type
== bfd_link_hash_defweak
)
2250 && (h
->root
.u
.def
.section
->owner
!= NULL
2251 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2252 != bfd_target_elf_flavour
)
2253 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2254 && !h
->def_dynamic
)))
2258 /* If this is a final link, and the symbol was defined as a common
2259 symbol in a regular object file, and there was no definition in
2260 any dynamic object, then the linker will have allocated space for
2261 the symbol in a common section but the DEF_REGULAR
2262 flag will not have been set. */
2263 if (h
->root
.type
== bfd_link_hash_defined
2267 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2270 /* If -Bsymbolic was used (which means to bind references to global
2271 symbols to the definition within the shared object), and this
2272 symbol was defined in a regular object, then it actually doesn't
2273 need a PLT entry. Likewise, if the symbol has non-default
2274 visibility. If the symbol has hidden or internal visibility, we
2275 will force it local. */
2277 && eif
->info
->shared
2278 && is_elf_hash_table (eif
->info
->hash
)
2279 && (eif
->info
->symbolic
2280 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2283 const struct elf_backend_data
*bed
;
2284 bfd_boolean force_local
;
2286 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2288 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2289 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2290 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2293 /* If a weak undefined symbol has non-default visibility, we also
2294 hide it from the dynamic linker. */
2295 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2296 && h
->root
.type
== bfd_link_hash_undefweak
)
2298 const struct elf_backend_data
*bed
;
2299 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2300 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2303 /* If this is a weak defined symbol in a dynamic object, and we know
2304 the real definition in the dynamic object, copy interesting flags
2305 over to the real definition. */
2306 if (h
->u
.weakdef
!= NULL
)
2308 struct elf_link_hash_entry
*weakdef
;
2310 weakdef
= h
->u
.weakdef
;
2311 if (h
->root
.type
== bfd_link_hash_indirect
)
2312 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2314 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2315 || h
->root
.type
== bfd_link_hash_defweak
);
2316 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2317 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2318 BFD_ASSERT (weakdef
->def_dynamic
);
2320 /* If the real definition is defined by a regular object file,
2321 don't do anything special. See the longer description in
2322 _bfd_elf_adjust_dynamic_symbol, below. */
2323 if (weakdef
->def_regular
)
2324 h
->u
.weakdef
= NULL
;
2327 const struct elf_backend_data
*bed
;
2329 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2330 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2337 /* Make the backend pick a good value for a dynamic symbol. This is
2338 called via elf_link_hash_traverse, and also calls itself
2342 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2344 struct elf_info_failed
*eif
= data
;
2346 const struct elf_backend_data
*bed
;
2348 if (! is_elf_hash_table (eif
->info
->hash
))
2351 if (h
->root
.type
== bfd_link_hash_warning
)
2353 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2354 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2356 /* When warning symbols are created, they **replace** the "real"
2357 entry in the hash table, thus we never get to see the real
2358 symbol in a hash traversal. So look at it now. */
2359 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2362 /* Ignore indirect symbols. These are added by the versioning code. */
2363 if (h
->root
.type
== bfd_link_hash_indirect
)
2366 /* Fix the symbol flags. */
2367 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2370 /* If this symbol does not require a PLT entry, and it is not
2371 defined by a dynamic object, or is not referenced by a regular
2372 object, ignore it. We do have to handle a weak defined symbol,
2373 even if no regular object refers to it, if we decided to add it
2374 to the dynamic symbol table. FIXME: Do we normally need to worry
2375 about symbols which are defined by one dynamic object and
2376 referenced by another one? */
2381 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2383 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2387 /* If we've already adjusted this symbol, don't do it again. This
2388 can happen via a recursive call. */
2389 if (h
->dynamic_adjusted
)
2392 /* Don't look at this symbol again. Note that we must set this
2393 after checking the above conditions, because we may look at a
2394 symbol once, decide not to do anything, and then get called
2395 recursively later after REF_REGULAR is set below. */
2396 h
->dynamic_adjusted
= 1;
2398 /* If this is a weak definition, and we know a real definition, and
2399 the real symbol is not itself defined by a regular object file,
2400 then get a good value for the real definition. We handle the
2401 real symbol first, for the convenience of the backend routine.
2403 Note that there is a confusing case here. If the real definition
2404 is defined by a regular object file, we don't get the real symbol
2405 from the dynamic object, but we do get the weak symbol. If the
2406 processor backend uses a COPY reloc, then if some routine in the
2407 dynamic object changes the real symbol, we will not see that
2408 change in the corresponding weak symbol. This is the way other
2409 ELF linkers work as well, and seems to be a result of the shared
2412 I will clarify this issue. Most SVR4 shared libraries define the
2413 variable _timezone and define timezone as a weak synonym. The
2414 tzset call changes _timezone. If you write
2415 extern int timezone;
2417 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2418 you might expect that, since timezone is a synonym for _timezone,
2419 the same number will print both times. However, if the processor
2420 backend uses a COPY reloc, then actually timezone will be copied
2421 into your process image, and, since you define _timezone
2422 yourself, _timezone will not. Thus timezone and _timezone will
2423 wind up at different memory locations. The tzset call will set
2424 _timezone, leaving timezone unchanged. */
2426 if (h
->u
.weakdef
!= NULL
)
2428 /* If we get to this point, we know there is an implicit
2429 reference by a regular object file via the weak symbol H.
2430 FIXME: Is this really true? What if the traversal finds
2431 H->U.WEAKDEF before it finds H? */
2432 h
->u
.weakdef
->ref_regular
= 1;
2434 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2438 /* If a symbol has no type and no size and does not require a PLT
2439 entry, then we are probably about to do the wrong thing here: we
2440 are probably going to create a COPY reloc for an empty object.
2441 This case can arise when a shared object is built with assembly
2442 code, and the assembly code fails to set the symbol type. */
2444 && h
->type
== STT_NOTYPE
2446 (*_bfd_error_handler
)
2447 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2448 h
->root
.root
.string
);
2450 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2451 bed
= get_elf_backend_data (dynobj
);
2452 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2461 /* Adjust all external symbols pointing into SEC_MERGE sections
2462 to reflect the object merging within the sections. */
2465 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2469 if (h
->root
.type
== bfd_link_hash_warning
)
2470 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2472 if ((h
->root
.type
== bfd_link_hash_defined
2473 || h
->root
.type
== bfd_link_hash_defweak
)
2474 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2475 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2477 bfd
*output_bfd
= data
;
2479 h
->root
.u
.def
.value
=
2480 _bfd_merged_section_offset (output_bfd
,
2481 &h
->root
.u
.def
.section
,
2482 elf_section_data (sec
)->sec_info
,
2483 h
->root
.u
.def
.value
);
2489 /* Returns false if the symbol referred to by H should be considered
2490 to resolve local to the current module, and true if it should be
2491 considered to bind dynamically. */
2494 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2495 struct bfd_link_info
*info
,
2496 bfd_boolean ignore_protected
)
2498 bfd_boolean binding_stays_local_p
;
2503 while (h
->root
.type
== bfd_link_hash_indirect
2504 || h
->root
.type
== bfd_link_hash_warning
)
2505 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2507 /* If it was forced local, then clearly it's not dynamic. */
2508 if (h
->dynindx
== -1)
2510 if (h
->forced_local
)
2513 /* Identify the cases where name binding rules say that a
2514 visible symbol resolves locally. */
2515 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2517 switch (ELF_ST_VISIBILITY (h
->other
))
2524 /* Proper resolution for function pointer equality may require
2525 that these symbols perhaps be resolved dynamically, even though
2526 we should be resolving them to the current module. */
2527 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2528 binding_stays_local_p
= TRUE
;
2535 /* If it isn't defined locally, then clearly it's dynamic. */
2536 if (!h
->def_regular
)
2539 /* Otherwise, the symbol is dynamic if binding rules don't tell
2540 us that it remains local. */
2541 return !binding_stays_local_p
;
2544 /* Return true if the symbol referred to by H should be considered
2545 to resolve local to the current module, and false otherwise. Differs
2546 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2547 undefined symbols and weak symbols. */
2550 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2551 struct bfd_link_info
*info
,
2552 bfd_boolean local_protected
)
2554 /* If it's a local sym, of course we resolve locally. */
2558 /* Common symbols that become definitions don't get the DEF_REGULAR
2559 flag set, so test it first, and don't bail out. */
2560 if (ELF_COMMON_DEF_P (h
))
2562 /* If we don't have a definition in a regular file, then we can't
2563 resolve locally. The sym is either undefined or dynamic. */
2564 else if (!h
->def_regular
)
2567 /* Forced local symbols resolve locally. */
2568 if (h
->forced_local
)
2571 /* As do non-dynamic symbols. */
2572 if (h
->dynindx
== -1)
2575 /* At this point, we know the symbol is defined and dynamic. In an
2576 executable it must resolve locally, likewise when building symbolic
2577 shared libraries. */
2578 if (info
->executable
|| info
->symbolic
)
2581 /* Now deal with defined dynamic symbols in shared libraries. Ones
2582 with default visibility might not resolve locally. */
2583 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2586 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2587 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2590 /* STV_PROTECTED non-function symbols are local. */
2591 if (h
->type
!= STT_FUNC
)
2594 /* Function pointer equality tests may require that STV_PROTECTED
2595 symbols be treated as dynamic symbols, even when we know that the
2596 dynamic linker will resolve them locally. */
2597 return local_protected
;
2600 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2601 aligned. Returns the first TLS output section. */
2603 struct bfd_section
*
2604 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2606 struct bfd_section
*sec
, *tls
;
2607 unsigned int align
= 0;
2609 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2610 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2614 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2615 if (sec
->alignment_power
> align
)
2616 align
= sec
->alignment_power
;
2618 elf_hash_table (info
)->tls_sec
= tls
;
2620 /* Ensure the alignment of the first section is the largest alignment,
2621 so that the tls segment starts aligned. */
2623 tls
->alignment_power
= align
;
2628 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2630 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2631 Elf_Internal_Sym
*sym
)
2633 const struct elf_backend_data
*bed
;
2635 /* Local symbols do not count, but target specific ones might. */
2636 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2637 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2640 /* Function symbols do not count. */
2641 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2644 /* If the section is undefined, then so is the symbol. */
2645 if (sym
->st_shndx
== SHN_UNDEF
)
2648 /* If the symbol is defined in the common section, then
2649 it is a common definition and so does not count. */
2650 bed
= get_elf_backend_data (abfd
);
2651 if (bed
->common_definition (sym
))
2654 /* If the symbol is in a target specific section then we
2655 must rely upon the backend to tell us what it is. */
2656 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2657 /* FIXME - this function is not coded yet:
2659 return _bfd_is_global_symbol_definition (abfd, sym);
2661 Instead for now assume that the definition is not global,
2662 Even if this is wrong, at least the linker will behave
2663 in the same way that it used to do. */
2669 /* Search the symbol table of the archive element of the archive ABFD
2670 whose archive map contains a mention of SYMDEF, and determine if
2671 the symbol is defined in this element. */
2673 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2675 Elf_Internal_Shdr
* hdr
;
2676 bfd_size_type symcount
;
2677 bfd_size_type extsymcount
;
2678 bfd_size_type extsymoff
;
2679 Elf_Internal_Sym
*isymbuf
;
2680 Elf_Internal_Sym
*isym
;
2681 Elf_Internal_Sym
*isymend
;
2684 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2688 if (! bfd_check_format (abfd
, bfd_object
))
2691 /* If we have already included the element containing this symbol in the
2692 link then we do not need to include it again. Just claim that any symbol
2693 it contains is not a definition, so that our caller will not decide to
2694 (re)include this element. */
2695 if (abfd
->archive_pass
)
2698 /* Select the appropriate symbol table. */
2699 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2700 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2702 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2704 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2706 /* The sh_info field of the symtab header tells us where the
2707 external symbols start. We don't care about the local symbols. */
2708 if (elf_bad_symtab (abfd
))
2710 extsymcount
= symcount
;
2715 extsymcount
= symcount
- hdr
->sh_info
;
2716 extsymoff
= hdr
->sh_info
;
2719 if (extsymcount
== 0)
2722 /* Read in the symbol table. */
2723 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2725 if (isymbuf
== NULL
)
2728 /* Scan the symbol table looking for SYMDEF. */
2730 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2734 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2739 if (strcmp (name
, symdef
->name
) == 0)
2741 result
= is_global_data_symbol_definition (abfd
, isym
);
2751 /* Add an entry to the .dynamic table. */
2754 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2758 struct elf_link_hash_table
*hash_table
;
2759 const struct elf_backend_data
*bed
;
2761 bfd_size_type newsize
;
2762 bfd_byte
*newcontents
;
2763 Elf_Internal_Dyn dyn
;
2765 hash_table
= elf_hash_table (info
);
2766 if (! is_elf_hash_table (hash_table
))
2769 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2771 (_("warning: creating a DT_TEXTREL in a shared object."));
2773 bed
= get_elf_backend_data (hash_table
->dynobj
);
2774 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2775 BFD_ASSERT (s
!= NULL
);
2777 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2778 newcontents
= bfd_realloc (s
->contents
, newsize
);
2779 if (newcontents
== NULL
)
2783 dyn
.d_un
.d_val
= val
;
2784 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2787 s
->contents
= newcontents
;
2792 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2793 otherwise just check whether one already exists. Returns -1 on error,
2794 1 if a DT_NEEDED tag already exists, and 0 on success. */
2797 elf_add_dt_needed_tag (bfd
*abfd
,
2798 struct bfd_link_info
*info
,
2802 struct elf_link_hash_table
*hash_table
;
2803 bfd_size_type oldsize
;
2804 bfd_size_type strindex
;
2806 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2809 hash_table
= elf_hash_table (info
);
2810 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2811 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2812 if (strindex
== (bfd_size_type
) -1)
2815 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2818 const struct elf_backend_data
*bed
;
2821 bed
= get_elf_backend_data (hash_table
->dynobj
);
2822 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2824 for (extdyn
= sdyn
->contents
;
2825 extdyn
< sdyn
->contents
+ sdyn
->size
;
2826 extdyn
+= bed
->s
->sizeof_dyn
)
2828 Elf_Internal_Dyn dyn
;
2830 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2831 if (dyn
.d_tag
== DT_NEEDED
2832 && dyn
.d_un
.d_val
== strindex
)
2834 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2842 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2845 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2849 /* We were just checking for existence of the tag. */
2850 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2855 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2856 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2857 references from regular objects to these symbols.
2859 ??? Should we do something about references from other dynamic
2860 obects? If not, we potentially lose some warnings about undefined
2861 symbols. But how can we recover the initial undefined / undefweak
2864 struct elf_smash_syms_data
2867 struct elf_link_hash_table
*htab
;
2868 bfd_boolean twiddled
;
2872 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2874 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2875 struct bfd_link_hash_entry
*bh
;
2877 switch (h
->root
.type
)
2880 case bfd_link_hash_new
:
2883 case bfd_link_hash_undefined
:
2884 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2886 if (h
->root
.u
.undef
.weak
!= NULL
2887 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2889 /* Symbol was undefweak in u.undef.weak bfd, and has become
2890 undefined in as-needed lib. Restore weak. */
2891 h
->root
.type
= bfd_link_hash_undefweak
;
2892 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2893 if (h
->root
.u
.undef
.next
!= NULL
2894 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2895 inf
->twiddled
= TRUE
;
2900 case bfd_link_hash_undefweak
:
2901 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2905 case bfd_link_hash_defined
:
2906 case bfd_link_hash_defweak
:
2907 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2911 case bfd_link_hash_common
:
2912 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2916 case bfd_link_hash_warning
:
2917 case bfd_link_hash_indirect
:
2918 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2919 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2921 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2926 /* There is no way we can undo symbol table state from defined or
2927 defweak back to undefined. */
2931 /* Set sym back to newly created state, but keep undef.next if it is
2932 being used as a list pointer. */
2933 bh
= h
->root
.u
.undef
.next
;
2936 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2937 inf
->twiddled
= TRUE
;
2938 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2939 &inf
->htab
->root
.table
,
2940 h
->root
.root
.string
);
2941 h
->root
.u
.undef
.next
= bh
;
2942 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2947 /* Sort symbol by value and section. */
2949 elf_sort_symbol (const void *arg1
, const void *arg2
)
2951 const struct elf_link_hash_entry
*h1
;
2952 const struct elf_link_hash_entry
*h2
;
2953 bfd_signed_vma vdiff
;
2955 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2956 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2957 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2959 return vdiff
> 0 ? 1 : -1;
2962 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2964 return sdiff
> 0 ? 1 : -1;
2969 /* This function is used to adjust offsets into .dynstr for
2970 dynamic symbols. This is called via elf_link_hash_traverse. */
2973 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2975 struct elf_strtab_hash
*dynstr
= data
;
2977 if (h
->root
.type
== bfd_link_hash_warning
)
2978 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2980 if (h
->dynindx
!= -1)
2981 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2985 /* Assign string offsets in .dynstr, update all structures referencing
2989 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2991 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2992 struct elf_link_local_dynamic_entry
*entry
;
2993 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2994 bfd
*dynobj
= hash_table
->dynobj
;
2997 const struct elf_backend_data
*bed
;
3000 _bfd_elf_strtab_finalize (dynstr
);
3001 size
= _bfd_elf_strtab_size (dynstr
);
3003 bed
= get_elf_backend_data (dynobj
);
3004 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3005 BFD_ASSERT (sdyn
!= NULL
);
3007 /* Update all .dynamic entries referencing .dynstr strings. */
3008 for (extdyn
= sdyn
->contents
;
3009 extdyn
< sdyn
->contents
+ sdyn
->size
;
3010 extdyn
+= bed
->s
->sizeof_dyn
)
3012 Elf_Internal_Dyn dyn
;
3014 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3018 dyn
.d_un
.d_val
= size
;
3026 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3031 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3034 /* Now update local dynamic symbols. */
3035 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3036 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3037 entry
->isym
.st_name
);
3039 /* And the rest of dynamic symbols. */
3040 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3042 /* Adjust version definitions. */
3043 if (elf_tdata (output_bfd
)->cverdefs
)
3048 Elf_Internal_Verdef def
;
3049 Elf_Internal_Verdaux defaux
;
3051 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3055 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3057 p
+= sizeof (Elf_External_Verdef
);
3058 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3060 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3062 _bfd_elf_swap_verdaux_in (output_bfd
,
3063 (Elf_External_Verdaux
*) p
, &defaux
);
3064 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3066 _bfd_elf_swap_verdaux_out (output_bfd
,
3067 &defaux
, (Elf_External_Verdaux
*) p
);
3068 p
+= sizeof (Elf_External_Verdaux
);
3071 while (def
.vd_next
);
3074 /* Adjust version references. */
3075 if (elf_tdata (output_bfd
)->verref
)
3080 Elf_Internal_Verneed need
;
3081 Elf_Internal_Vernaux needaux
;
3083 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3087 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3089 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3090 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3091 (Elf_External_Verneed
*) p
);
3092 p
+= sizeof (Elf_External_Verneed
);
3093 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3095 _bfd_elf_swap_vernaux_in (output_bfd
,
3096 (Elf_External_Vernaux
*) p
, &needaux
);
3097 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3099 _bfd_elf_swap_vernaux_out (output_bfd
,
3101 (Elf_External_Vernaux
*) p
);
3102 p
+= sizeof (Elf_External_Vernaux
);
3105 while (need
.vn_next
);
3111 /* Add symbols from an ELF object file to the linker hash table. */
3114 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3116 bfd_boolean (*add_symbol_hook
)
3117 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3118 const char **, flagword
*, asection
**, bfd_vma
*);
3119 bfd_boolean (*check_relocs
)
3120 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3121 bfd_boolean (*check_directives
)
3122 (bfd
*, struct bfd_link_info
*);
3123 bfd_boolean collect
;
3124 Elf_Internal_Shdr
*hdr
;
3125 bfd_size_type symcount
;
3126 bfd_size_type extsymcount
;
3127 bfd_size_type extsymoff
;
3128 struct elf_link_hash_entry
**sym_hash
;
3129 bfd_boolean dynamic
;
3130 Elf_External_Versym
*extversym
= NULL
;
3131 Elf_External_Versym
*ever
;
3132 struct elf_link_hash_entry
*weaks
;
3133 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3134 bfd_size_type nondeflt_vers_cnt
= 0;
3135 Elf_Internal_Sym
*isymbuf
= NULL
;
3136 Elf_Internal_Sym
*isym
;
3137 Elf_Internal_Sym
*isymend
;
3138 const struct elf_backend_data
*bed
;
3139 bfd_boolean add_needed
;
3140 struct elf_link_hash_table
* hash_table
;
3143 hash_table
= elf_hash_table (info
);
3145 bed
= get_elf_backend_data (abfd
);
3146 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3147 collect
= bed
->collect
;
3149 if ((abfd
->flags
& DYNAMIC
) == 0)
3155 /* You can't use -r against a dynamic object. Also, there's no
3156 hope of using a dynamic object which does not exactly match
3157 the format of the output file. */
3158 if (info
->relocatable
3159 || !is_elf_hash_table (hash_table
)
3160 || hash_table
->root
.creator
!= abfd
->xvec
)
3162 if (info
->relocatable
)
3163 bfd_set_error (bfd_error_invalid_operation
);
3165 bfd_set_error (bfd_error_wrong_format
);
3170 /* As a GNU extension, any input sections which are named
3171 .gnu.warning.SYMBOL are treated as warning symbols for the given
3172 symbol. This differs from .gnu.warning sections, which generate
3173 warnings when they are included in an output file. */
3174 if (info
->executable
)
3178 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3182 name
= bfd_get_section_name (abfd
, s
);
3183 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3188 name
+= sizeof ".gnu.warning." - 1;
3190 /* If this is a shared object, then look up the symbol
3191 in the hash table. If it is there, and it is already
3192 been defined, then we will not be using the entry
3193 from this shared object, so we don't need to warn.
3194 FIXME: If we see the definition in a regular object
3195 later on, we will warn, but we shouldn't. The only
3196 fix is to keep track of what warnings we are supposed
3197 to emit, and then handle them all at the end of the
3201 struct elf_link_hash_entry
*h
;
3203 h
= elf_link_hash_lookup (hash_table
, name
,
3204 FALSE
, FALSE
, TRUE
);
3206 /* FIXME: What about bfd_link_hash_common? */
3208 && (h
->root
.type
== bfd_link_hash_defined
3209 || h
->root
.type
== bfd_link_hash_defweak
))
3211 /* We don't want to issue this warning. Clobber
3212 the section size so that the warning does not
3213 get copied into the output file. */
3220 msg
= bfd_alloc (abfd
, sz
+ 1);
3224 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3229 if (! (_bfd_generic_link_add_one_symbol
3230 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3231 FALSE
, collect
, NULL
)))
3234 if (! info
->relocatable
)
3236 /* Clobber the section size so that the warning does
3237 not get copied into the output file. */
3240 /* Also set SEC_EXCLUDE, so that symbols defined in
3241 the warning section don't get copied to the output. */
3242 s
->flags
|= SEC_EXCLUDE
;
3251 /* If we are creating a shared library, create all the dynamic
3252 sections immediately. We need to attach them to something,
3253 so we attach them to this BFD, provided it is the right
3254 format. FIXME: If there are no input BFD's of the same
3255 format as the output, we can't make a shared library. */
3257 && is_elf_hash_table (hash_table
)
3258 && hash_table
->root
.creator
== abfd
->xvec
3259 && ! hash_table
->dynamic_sections_created
)
3261 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3265 else if (!is_elf_hash_table (hash_table
))
3270 const char *soname
= NULL
;
3271 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3274 /* ld --just-symbols and dynamic objects don't mix very well.
3275 ld shouldn't allow it. */
3276 if ((s
= abfd
->sections
) != NULL
3277 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3280 /* If this dynamic lib was specified on the command line with
3281 --as-needed in effect, then we don't want to add a DT_NEEDED
3282 tag unless the lib is actually used. Similary for libs brought
3283 in by another lib's DT_NEEDED. When --no-add-needed is used
3284 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3285 any dynamic library in DT_NEEDED tags in the dynamic lib at
3287 add_needed
= (elf_dyn_lib_class (abfd
)
3288 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3289 | DYN_NO_NEEDED
)) == 0;
3291 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3297 unsigned long shlink
;
3299 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3300 goto error_free_dyn
;
3302 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3304 goto error_free_dyn
;
3305 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3307 for (extdyn
= dynbuf
;
3308 extdyn
< dynbuf
+ s
->size
;
3309 extdyn
+= bed
->s
->sizeof_dyn
)
3311 Elf_Internal_Dyn dyn
;
3313 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3314 if (dyn
.d_tag
== DT_SONAME
)
3316 unsigned int tagv
= dyn
.d_un
.d_val
;
3317 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3319 goto error_free_dyn
;
3321 if (dyn
.d_tag
== DT_NEEDED
)
3323 struct bfd_link_needed_list
*n
, **pn
;
3325 unsigned int tagv
= dyn
.d_un
.d_val
;
3327 amt
= sizeof (struct bfd_link_needed_list
);
3328 n
= bfd_alloc (abfd
, amt
);
3329 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3330 if (n
== NULL
|| fnm
== NULL
)
3331 goto error_free_dyn
;
3332 amt
= strlen (fnm
) + 1;
3333 anm
= bfd_alloc (abfd
, amt
);
3335 goto error_free_dyn
;
3336 memcpy (anm
, fnm
, amt
);
3340 for (pn
= & hash_table
->needed
;
3346 if (dyn
.d_tag
== DT_RUNPATH
)
3348 struct bfd_link_needed_list
*n
, **pn
;
3350 unsigned int tagv
= dyn
.d_un
.d_val
;
3352 amt
= sizeof (struct bfd_link_needed_list
);
3353 n
= bfd_alloc (abfd
, amt
);
3354 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3355 if (n
== NULL
|| fnm
== NULL
)
3356 goto error_free_dyn
;
3357 amt
= strlen (fnm
) + 1;
3358 anm
= bfd_alloc (abfd
, amt
);
3360 goto error_free_dyn
;
3361 memcpy (anm
, fnm
, amt
);
3365 for (pn
= & runpath
;
3371 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3372 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3374 struct bfd_link_needed_list
*n
, **pn
;
3376 unsigned int tagv
= dyn
.d_un
.d_val
;
3378 amt
= sizeof (struct bfd_link_needed_list
);
3379 n
= bfd_alloc (abfd
, amt
);
3380 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3381 if (n
== NULL
|| fnm
== NULL
)
3382 goto error_free_dyn
;
3383 amt
= strlen (fnm
) + 1;
3384 anm
= bfd_alloc (abfd
, amt
);
3391 memcpy (anm
, fnm
, amt
);
3406 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3407 frees all more recently bfd_alloc'd blocks as well. */
3413 struct bfd_link_needed_list
**pn
;
3414 for (pn
= & hash_table
->runpath
;
3421 /* We do not want to include any of the sections in a dynamic
3422 object in the output file. We hack by simply clobbering the
3423 list of sections in the BFD. This could be handled more
3424 cleanly by, say, a new section flag; the existing
3425 SEC_NEVER_LOAD flag is not the one we want, because that one
3426 still implies that the section takes up space in the output
3428 bfd_section_list_clear (abfd
);
3430 /* Find the name to use in a DT_NEEDED entry that refers to this
3431 object. If the object has a DT_SONAME entry, we use it.
3432 Otherwise, if the generic linker stuck something in
3433 elf_dt_name, we use that. Otherwise, we just use the file
3435 if (soname
== NULL
|| *soname
== '\0')
3437 soname
= elf_dt_name (abfd
);
3438 if (soname
== NULL
|| *soname
== '\0')
3439 soname
= bfd_get_filename (abfd
);
3442 /* Save the SONAME because sometimes the linker emulation code
3443 will need to know it. */
3444 elf_dt_name (abfd
) = soname
;
3446 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3450 /* If we have already included this dynamic object in the
3451 link, just ignore it. There is no reason to include a
3452 particular dynamic object more than once. */
3457 /* If this is a dynamic object, we always link against the .dynsym
3458 symbol table, not the .symtab symbol table. The dynamic linker
3459 will only see the .dynsym symbol table, so there is no reason to
3460 look at .symtab for a dynamic object. */
3462 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3463 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3465 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3467 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3469 /* The sh_info field of the symtab header tells us where the
3470 external symbols start. We don't care about the local symbols at
3472 if (elf_bad_symtab (abfd
))
3474 extsymcount
= symcount
;
3479 extsymcount
= symcount
- hdr
->sh_info
;
3480 extsymoff
= hdr
->sh_info
;
3484 if (extsymcount
!= 0)
3486 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3488 if (isymbuf
== NULL
)
3491 /* We store a pointer to the hash table entry for each external
3493 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3494 sym_hash
= bfd_alloc (abfd
, amt
);
3495 if (sym_hash
== NULL
)
3496 goto error_free_sym
;
3497 elf_sym_hashes (abfd
) = sym_hash
;
3502 /* Read in any version definitions. */
3503 if (!_bfd_elf_slurp_version_tables (abfd
,
3504 info
->default_imported_symver
))
3505 goto error_free_sym
;
3507 /* Read in the symbol versions, but don't bother to convert them
3508 to internal format. */
3509 if (elf_dynversym (abfd
) != 0)
3511 Elf_Internal_Shdr
*versymhdr
;
3513 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3514 extversym
= bfd_malloc (versymhdr
->sh_size
);
3515 if (extversym
== NULL
)
3516 goto error_free_sym
;
3517 amt
= versymhdr
->sh_size
;
3518 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3519 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3520 goto error_free_vers
;
3526 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3527 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3529 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3533 asection
*sec
, *new_sec
;
3536 struct elf_link_hash_entry
*h
;
3537 bfd_boolean definition
;
3538 bfd_boolean size_change_ok
;
3539 bfd_boolean type_change_ok
;
3540 bfd_boolean new_weakdef
;
3541 bfd_boolean override
;
3543 unsigned int old_alignment
;
3548 flags
= BSF_NO_FLAGS
;
3550 value
= isym
->st_value
;
3552 common
= bed
->common_definition (isym
);
3554 bind
= ELF_ST_BIND (isym
->st_info
);
3555 if (bind
== STB_LOCAL
)
3557 /* This should be impossible, since ELF requires that all
3558 global symbols follow all local symbols, and that sh_info
3559 point to the first global symbol. Unfortunately, Irix 5
3563 else if (bind
== STB_GLOBAL
)
3565 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3568 else if (bind
== STB_WEAK
)
3572 /* Leave it up to the processor backend. */
3575 if (isym
->st_shndx
== SHN_UNDEF
)
3576 sec
= bfd_und_section_ptr
;
3577 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3579 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3581 sec
= bfd_abs_section_ptr
;
3582 else if (sec
->kept_section
)
3584 /* Symbols from discarded section are undefined, and have
3585 default visibility. */
3586 sec
= bfd_und_section_ptr
;
3587 isym
->st_shndx
= SHN_UNDEF
;
3588 isym
->st_other
= STV_DEFAULT
3589 | (isym
->st_other
& ~ ELF_ST_VISIBILITY(-1));
3591 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3594 else if (isym
->st_shndx
== SHN_ABS
)
3595 sec
= bfd_abs_section_ptr
;
3596 else if (isym
->st_shndx
== SHN_COMMON
)
3598 sec
= bfd_com_section_ptr
;
3599 /* What ELF calls the size we call the value. What ELF
3600 calls the value we call the alignment. */
3601 value
= isym
->st_size
;
3605 /* Leave it up to the processor backend. */
3608 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3611 goto error_free_vers
;
3613 if (isym
->st_shndx
== SHN_COMMON
3614 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3616 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3620 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3623 | SEC_LINKER_CREATED
3624 | SEC_THREAD_LOCAL
));
3626 goto error_free_vers
;
3630 else if (add_symbol_hook
)
3632 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3634 goto error_free_vers
;
3636 /* The hook function sets the name to NULL if this symbol
3637 should be skipped for some reason. */
3642 /* Sanity check that all possibilities were handled. */
3645 bfd_set_error (bfd_error_bad_value
);
3646 goto error_free_vers
;
3649 if (bfd_is_und_section (sec
)
3650 || bfd_is_com_section (sec
))
3655 size_change_ok
= FALSE
;
3656 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3661 if (is_elf_hash_table (hash_table
))
3663 Elf_Internal_Versym iver
;
3664 unsigned int vernum
= 0;
3669 if (info
->default_imported_symver
)
3670 /* Use the default symbol version created earlier. */
3671 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3676 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3678 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3680 /* If this is a hidden symbol, or if it is not version
3681 1, we append the version name to the symbol name.
3682 However, we do not modify a non-hidden absolute symbol
3683 if it is not a function, because it might be the version
3684 symbol itself. FIXME: What if it isn't? */
3685 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3686 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3687 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3690 size_t namelen
, verlen
, newlen
;
3693 if (isym
->st_shndx
!= SHN_UNDEF
)
3695 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3697 else if (vernum
> 1)
3699 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3705 (*_bfd_error_handler
)
3706 (_("%B: %s: invalid version %u (max %d)"),
3708 elf_tdata (abfd
)->cverdefs
);
3709 bfd_set_error (bfd_error_bad_value
);
3710 goto error_free_vers
;
3715 /* We cannot simply test for the number of
3716 entries in the VERNEED section since the
3717 numbers for the needed versions do not start
3719 Elf_Internal_Verneed
*t
;
3722 for (t
= elf_tdata (abfd
)->verref
;
3726 Elf_Internal_Vernaux
*a
;
3728 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3730 if (a
->vna_other
== vernum
)
3732 verstr
= a
->vna_nodename
;
3741 (*_bfd_error_handler
)
3742 (_("%B: %s: invalid needed version %d"),
3743 abfd
, name
, vernum
);
3744 bfd_set_error (bfd_error_bad_value
);
3745 goto error_free_vers
;
3749 namelen
= strlen (name
);
3750 verlen
= strlen (verstr
);
3751 newlen
= namelen
+ verlen
+ 2;
3752 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3753 && isym
->st_shndx
!= SHN_UNDEF
)
3756 newname
= bfd_alloc (abfd
, newlen
);
3757 if (newname
== NULL
)
3758 goto error_free_vers
;
3759 memcpy (newname
, name
, namelen
);
3760 p
= newname
+ namelen
;
3762 /* If this is a defined non-hidden version symbol,
3763 we add another @ to the name. This indicates the
3764 default version of the symbol. */
3765 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3766 && isym
->st_shndx
!= SHN_UNDEF
)
3768 memcpy (p
, verstr
, verlen
+ 1);
3773 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3774 &value
, &old_alignment
,
3775 sym_hash
, &skip
, &override
,
3776 &type_change_ok
, &size_change_ok
))
3777 goto error_free_vers
;
3786 while (h
->root
.type
== bfd_link_hash_indirect
3787 || h
->root
.type
== bfd_link_hash_warning
)
3788 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3790 /* Remember the old alignment if this is a common symbol, so
3791 that we don't reduce the alignment later on. We can't
3792 check later, because _bfd_generic_link_add_one_symbol
3793 will set a default for the alignment which we want to
3794 override. We also remember the old bfd where the existing
3795 definition comes from. */
3796 switch (h
->root
.type
)
3801 case bfd_link_hash_defined
:
3802 case bfd_link_hash_defweak
:
3803 old_bfd
= h
->root
.u
.def
.section
->owner
;
3806 case bfd_link_hash_common
:
3807 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3808 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3812 if (elf_tdata (abfd
)->verdef
!= NULL
3816 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3819 if (! (_bfd_generic_link_add_one_symbol
3820 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3821 (struct bfd_link_hash_entry
**) sym_hash
)))
3822 goto error_free_vers
;
3825 while (h
->root
.type
== bfd_link_hash_indirect
3826 || h
->root
.type
== bfd_link_hash_warning
)
3827 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3830 new_weakdef
= FALSE
;
3833 && (flags
& BSF_WEAK
) != 0
3834 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3835 && is_elf_hash_table (hash_table
)
3836 && h
->u
.weakdef
== NULL
)
3838 /* Keep a list of all weak defined non function symbols from
3839 a dynamic object, using the weakdef field. Later in this
3840 function we will set the weakdef field to the correct
3841 value. We only put non-function symbols from dynamic
3842 objects on this list, because that happens to be the only
3843 time we need to know the normal symbol corresponding to a
3844 weak symbol, and the information is time consuming to
3845 figure out. If the weakdef field is not already NULL,
3846 then this symbol was already defined by some previous
3847 dynamic object, and we will be using that previous
3848 definition anyhow. */
3850 h
->u
.weakdef
= weaks
;
3855 /* Set the alignment of a common symbol. */
3856 if ((common
|| bfd_is_com_section (sec
))
3857 && h
->root
.type
== bfd_link_hash_common
)
3862 align
= bfd_log2 (isym
->st_value
);
3865 /* The new symbol is a common symbol in a shared object.
3866 We need to get the alignment from the section. */
3867 align
= new_sec
->alignment_power
;
3869 if (align
> old_alignment
3870 /* Permit an alignment power of zero if an alignment of one
3871 is specified and no other alignments have been specified. */
3872 || (isym
->st_value
== 1 && old_alignment
== 0))
3873 h
->root
.u
.c
.p
->alignment_power
= align
;
3875 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3878 if (is_elf_hash_table (hash_table
))
3882 /* Check the alignment when a common symbol is involved. This
3883 can change when a common symbol is overridden by a normal
3884 definition or a common symbol is ignored due to the old
3885 normal definition. We need to make sure the maximum
3886 alignment is maintained. */
3887 if ((old_alignment
|| common
)
3888 && h
->root
.type
!= bfd_link_hash_common
)
3890 unsigned int common_align
;
3891 unsigned int normal_align
;
3892 unsigned int symbol_align
;
3896 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3897 if (h
->root
.u
.def
.section
->owner
!= NULL
3898 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3900 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3901 if (normal_align
> symbol_align
)
3902 normal_align
= symbol_align
;
3905 normal_align
= symbol_align
;
3909 common_align
= old_alignment
;
3910 common_bfd
= old_bfd
;
3915 common_align
= bfd_log2 (isym
->st_value
);
3917 normal_bfd
= old_bfd
;
3920 if (normal_align
< common_align
)
3921 (*_bfd_error_handler
)
3922 (_("Warning: alignment %u of symbol `%s' in %B"
3923 " is smaller than %u in %B"),
3924 normal_bfd
, common_bfd
,
3925 1 << normal_align
, name
, 1 << common_align
);
3928 /* Remember the symbol size and type. */
3929 if (isym
->st_size
!= 0
3930 && (definition
|| h
->size
== 0))
3932 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3933 (*_bfd_error_handler
)
3934 (_("Warning: size of symbol `%s' changed"
3935 " from %lu in %B to %lu in %B"),
3937 name
, (unsigned long) h
->size
,
3938 (unsigned long) isym
->st_size
);
3940 h
->size
= isym
->st_size
;
3943 /* If this is a common symbol, then we always want H->SIZE
3944 to be the size of the common symbol. The code just above
3945 won't fix the size if a common symbol becomes larger. We
3946 don't warn about a size change here, because that is
3947 covered by --warn-common. */
3948 if (h
->root
.type
== bfd_link_hash_common
)
3949 h
->size
= h
->root
.u
.c
.size
;
3951 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3952 && (definition
|| h
->type
== STT_NOTYPE
))
3954 if (h
->type
!= STT_NOTYPE
3955 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3956 && ! type_change_ok
)
3957 (*_bfd_error_handler
)
3958 (_("Warning: type of symbol `%s' changed"
3959 " from %d to %d in %B"),
3960 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3962 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3965 /* If st_other has a processor-specific meaning, specific
3966 code might be needed here. We never merge the visibility
3967 attribute with the one from a dynamic object. */
3968 if (bed
->elf_backend_merge_symbol_attribute
)
3969 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3972 /* If this symbol has default visibility and the user has requested
3973 we not re-export it, then mark it as hidden. */
3974 if (definition
&& !dynamic
3976 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3977 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3978 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3980 if (isym
->st_other
!= 0 && !dynamic
)
3982 unsigned char hvis
, symvis
, other
, nvis
;
3984 /* Take the balance of OTHER from the definition. */
3985 other
= (definition
? isym
->st_other
: h
->other
);
3986 other
&= ~ ELF_ST_VISIBILITY (-1);
3988 /* Combine visibilities, using the most constraining one. */
3989 hvis
= ELF_ST_VISIBILITY (h
->other
);
3990 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3996 nvis
= hvis
< symvis
? hvis
: symvis
;
3998 h
->other
= other
| nvis
;
4001 /* Set a flag in the hash table entry indicating the type of
4002 reference or definition we just found. Keep a count of
4003 the number of dynamic symbols we find. A dynamic symbol
4004 is one which is referenced or defined by both a regular
4005 object and a shared object. */
4012 if (bind
!= STB_WEAK
)
4013 h
->ref_regular_nonweak
= 1;
4017 if (! info
->executable
4030 || (h
->u
.weakdef
!= NULL
4032 && h
->u
.weakdef
->dynindx
!= -1))
4036 /* Check to see if we need to add an indirect symbol for
4037 the default name. */
4038 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4039 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4040 &sec
, &value
, &dynsym
,
4042 goto error_free_vers
;
4044 if (definition
&& !dynamic
)
4046 char *p
= strchr (name
, ELF_VER_CHR
);
4047 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4049 /* Queue non-default versions so that .symver x, x@FOO
4050 aliases can be checked. */
4051 if (! nondeflt_vers
)
4053 amt
= (isymend
- isym
+ 1)
4054 * sizeof (struct elf_link_hash_entry
*);
4055 nondeflt_vers
= bfd_malloc (amt
);
4057 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4061 if (dynsym
&& h
->dynindx
== -1)
4063 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4064 goto error_free_vers
;
4065 if (h
->u
.weakdef
!= NULL
4067 && h
->u
.weakdef
->dynindx
== -1)
4069 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4070 goto error_free_vers
;
4073 else if (dynsym
&& h
->dynindx
!= -1)
4074 /* If the symbol already has a dynamic index, but
4075 visibility says it should not be visible, turn it into
4077 switch (ELF_ST_VISIBILITY (h
->other
))
4081 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4092 const char *soname
= elf_dt_name (abfd
);
4094 /* A symbol from a library loaded via DT_NEEDED of some
4095 other library is referenced by a regular object.
4096 Add a DT_NEEDED entry for it. Issue an error if
4097 --no-add-needed is used. */
4098 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4100 (*_bfd_error_handler
)
4101 (_("%s: invalid DSO for symbol `%s' definition"),
4103 bfd_set_error (bfd_error_bad_value
);
4104 goto error_free_vers
;
4107 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4110 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4112 goto error_free_vers
;
4114 BFD_ASSERT (ret
== 0);
4119 /* Now that all the symbols from this input file are created, handle
4120 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4121 if (nondeflt_vers
!= NULL
)
4123 bfd_size_type cnt
, symidx
;
4125 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4127 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4128 char *shortname
, *p
;
4130 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4132 || (h
->root
.type
!= bfd_link_hash_defined
4133 && h
->root
.type
!= bfd_link_hash_defweak
))
4136 amt
= p
- h
->root
.root
.string
;
4137 shortname
= bfd_malloc (amt
+ 1);
4138 memcpy (shortname
, h
->root
.root
.string
, amt
);
4139 shortname
[amt
] = '\0';
4141 hi
= (struct elf_link_hash_entry
*)
4142 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4143 FALSE
, FALSE
, FALSE
);
4145 && hi
->root
.type
== h
->root
.type
4146 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4147 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4149 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4150 hi
->root
.type
= bfd_link_hash_indirect
;
4151 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4152 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4153 sym_hash
= elf_sym_hashes (abfd
);
4155 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4156 if (sym_hash
[symidx
] == hi
)
4158 sym_hash
[symidx
] = h
;
4164 free (nondeflt_vers
);
4165 nondeflt_vers
= NULL
;
4168 if (extversym
!= NULL
)
4174 if (isymbuf
!= NULL
)
4179 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4181 /* Remove symbols defined in an as-needed shared lib that wasn't
4183 struct elf_smash_syms_data inf
;
4184 inf
.not_needed
= abfd
;
4185 inf
.htab
= hash_table
;
4186 inf
.twiddled
= FALSE
;
4187 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4189 bfd_link_repair_undef_list (&hash_table
->root
);
4193 /* Now set the weakdefs field correctly for all the weak defined
4194 symbols we found. The only way to do this is to search all the
4195 symbols. Since we only need the information for non functions in
4196 dynamic objects, that's the only time we actually put anything on
4197 the list WEAKS. We need this information so that if a regular
4198 object refers to a symbol defined weakly in a dynamic object, the
4199 real symbol in the dynamic object is also put in the dynamic
4200 symbols; we also must arrange for both symbols to point to the
4201 same memory location. We could handle the general case of symbol
4202 aliasing, but a general symbol alias can only be generated in
4203 assembler code, handling it correctly would be very time
4204 consuming, and other ELF linkers don't handle general aliasing
4208 struct elf_link_hash_entry
**hpp
;
4209 struct elf_link_hash_entry
**hppend
;
4210 struct elf_link_hash_entry
**sorted_sym_hash
;
4211 struct elf_link_hash_entry
*h
;
4214 /* Since we have to search the whole symbol list for each weak
4215 defined symbol, search time for N weak defined symbols will be
4216 O(N^2). Binary search will cut it down to O(NlogN). */
4217 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4218 sorted_sym_hash
= bfd_malloc (amt
);
4219 if (sorted_sym_hash
== NULL
)
4221 sym_hash
= sorted_sym_hash
;
4222 hpp
= elf_sym_hashes (abfd
);
4223 hppend
= hpp
+ extsymcount
;
4225 for (; hpp
< hppend
; hpp
++)
4229 && h
->root
.type
== bfd_link_hash_defined
4230 && h
->type
!= STT_FUNC
)
4238 qsort (sorted_sym_hash
, sym_count
,
4239 sizeof (struct elf_link_hash_entry
*),
4242 while (weaks
!= NULL
)
4244 struct elf_link_hash_entry
*hlook
;
4251 weaks
= hlook
->u
.weakdef
;
4252 hlook
->u
.weakdef
= NULL
;
4254 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4255 || hlook
->root
.type
== bfd_link_hash_defweak
4256 || hlook
->root
.type
== bfd_link_hash_common
4257 || hlook
->root
.type
== bfd_link_hash_indirect
);
4258 slook
= hlook
->root
.u
.def
.section
;
4259 vlook
= hlook
->root
.u
.def
.value
;
4266 bfd_signed_vma vdiff
;
4268 h
= sorted_sym_hash
[idx
];
4269 vdiff
= vlook
- h
->root
.u
.def
.value
;
4276 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4289 /* We didn't find a value/section match. */
4293 for (i
= ilook
; i
< sym_count
; i
++)
4295 h
= sorted_sym_hash
[i
];
4297 /* Stop if value or section doesn't match. */
4298 if (h
->root
.u
.def
.value
!= vlook
4299 || h
->root
.u
.def
.section
!= slook
)
4301 else if (h
!= hlook
)
4303 hlook
->u
.weakdef
= h
;
4305 /* If the weak definition is in the list of dynamic
4306 symbols, make sure the real definition is put
4308 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4310 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4314 /* If the real definition is in the list of dynamic
4315 symbols, make sure the weak definition is put
4316 there as well. If we don't do this, then the
4317 dynamic loader might not merge the entries for the
4318 real definition and the weak definition. */
4319 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4321 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4329 free (sorted_sym_hash
);
4332 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4333 if (check_directives
)
4334 check_directives (abfd
, info
);
4336 /* If this object is the same format as the output object, and it is
4337 not a shared library, then let the backend look through the
4340 This is required to build global offset table entries and to
4341 arrange for dynamic relocs. It is not required for the
4342 particular common case of linking non PIC code, even when linking
4343 against shared libraries, but unfortunately there is no way of
4344 knowing whether an object file has been compiled PIC or not.
4345 Looking through the relocs is not particularly time consuming.
4346 The problem is that we must either (1) keep the relocs in memory,
4347 which causes the linker to require additional runtime memory or
4348 (2) read the relocs twice from the input file, which wastes time.
4349 This would be a good case for using mmap.
4351 I have no idea how to handle linking PIC code into a file of a
4352 different format. It probably can't be done. */
4353 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4355 && is_elf_hash_table (hash_table
)
4356 && hash_table
->root
.creator
== abfd
->xvec
4357 && check_relocs
!= NULL
)
4361 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4363 Elf_Internal_Rela
*internal_relocs
;
4366 if ((o
->flags
& SEC_RELOC
) == 0
4367 || o
->reloc_count
== 0
4368 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4369 && (o
->flags
& SEC_DEBUGGING
) != 0)
4370 || bfd_is_abs_section (o
->output_section
))
4373 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4375 if (internal_relocs
== NULL
)
4378 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4380 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4381 free (internal_relocs
);
4388 /* If this is a non-traditional link, try to optimize the handling
4389 of the .stab/.stabstr sections. */
4391 && ! info
->traditional_format
4392 && is_elf_hash_table (hash_table
)
4393 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4397 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4398 if (stabstr
!= NULL
)
4400 bfd_size_type string_offset
= 0;
4403 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4404 if (strncmp (".stab", stab
->name
, 5) == 0
4405 && (!stab
->name
[5] ||
4406 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4407 && (stab
->flags
& SEC_MERGE
) == 0
4408 && !bfd_is_abs_section (stab
->output_section
))
4410 struct bfd_elf_section_data
*secdata
;
4412 secdata
= elf_section_data (stab
);
4413 if (! _bfd_link_section_stabs (abfd
,
4414 &hash_table
->stab_info
,
4419 if (secdata
->sec_info
)
4420 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4425 if (is_elf_hash_table (hash_table
) && add_needed
)
4427 /* Add this bfd to the loaded list. */
4428 struct elf_link_loaded_list
*n
;
4430 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4434 n
->next
= hash_table
->loaded
;
4435 hash_table
->loaded
= n
;
4441 if (nondeflt_vers
!= NULL
)
4442 free (nondeflt_vers
);
4443 if (extversym
!= NULL
)
4446 if (isymbuf
!= NULL
)
4452 /* Return the linker hash table entry of a symbol that might be
4453 satisfied by an archive symbol. Return -1 on error. */
4455 struct elf_link_hash_entry
*
4456 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4457 struct bfd_link_info
*info
,
4460 struct elf_link_hash_entry
*h
;
4464 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4468 /* If this is a default version (the name contains @@), look up the
4469 symbol again with only one `@' as well as without the version.
4470 The effect is that references to the symbol with and without the
4471 version will be matched by the default symbol in the archive. */
4473 p
= strchr (name
, ELF_VER_CHR
);
4474 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4477 /* First check with only one `@'. */
4478 len
= strlen (name
);
4479 copy
= bfd_alloc (abfd
, len
);
4481 return (struct elf_link_hash_entry
*) 0 - 1;
4483 first
= p
- name
+ 1;
4484 memcpy (copy
, name
, first
);
4485 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4487 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4490 /* We also need to check references to the symbol without the
4492 copy
[first
- 1] = '\0';
4493 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4494 FALSE
, FALSE
, FALSE
);
4497 bfd_release (abfd
, copy
);
4501 /* Add symbols from an ELF archive file to the linker hash table. We
4502 don't use _bfd_generic_link_add_archive_symbols because of a
4503 problem which arises on UnixWare. The UnixWare libc.so is an
4504 archive which includes an entry libc.so.1 which defines a bunch of
4505 symbols. The libc.so archive also includes a number of other
4506 object files, which also define symbols, some of which are the same
4507 as those defined in libc.so.1. Correct linking requires that we
4508 consider each object file in turn, and include it if it defines any
4509 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4510 this; it looks through the list of undefined symbols, and includes
4511 any object file which defines them. When this algorithm is used on
4512 UnixWare, it winds up pulling in libc.so.1 early and defining a
4513 bunch of symbols. This means that some of the other objects in the
4514 archive are not included in the link, which is incorrect since they
4515 precede libc.so.1 in the archive.
4517 Fortunately, ELF archive handling is simpler than that done by
4518 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4519 oddities. In ELF, if we find a symbol in the archive map, and the
4520 symbol is currently undefined, we know that we must pull in that
4523 Unfortunately, we do have to make multiple passes over the symbol
4524 table until nothing further is resolved. */
4527 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4530 bfd_boolean
*defined
= NULL
;
4531 bfd_boolean
*included
= NULL
;
4535 const struct elf_backend_data
*bed
;
4536 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4537 (bfd
*, struct bfd_link_info
*, const char *);
4539 if (! bfd_has_map (abfd
))
4541 /* An empty archive is a special case. */
4542 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4544 bfd_set_error (bfd_error_no_armap
);
4548 /* Keep track of all symbols we know to be already defined, and all
4549 files we know to be already included. This is to speed up the
4550 second and subsequent passes. */
4551 c
= bfd_ardata (abfd
)->symdef_count
;
4555 amt
*= sizeof (bfd_boolean
);
4556 defined
= bfd_zmalloc (amt
);
4557 included
= bfd_zmalloc (amt
);
4558 if (defined
== NULL
|| included
== NULL
)
4561 symdefs
= bfd_ardata (abfd
)->symdefs
;
4562 bed
= get_elf_backend_data (abfd
);
4563 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4576 symdefend
= symdef
+ c
;
4577 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4579 struct elf_link_hash_entry
*h
;
4581 struct bfd_link_hash_entry
*undefs_tail
;
4584 if (defined
[i
] || included
[i
])
4586 if (symdef
->file_offset
== last
)
4592 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4593 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4599 if (h
->root
.type
== bfd_link_hash_common
)
4601 /* We currently have a common symbol. The archive map contains
4602 a reference to this symbol, so we may want to include it. We
4603 only want to include it however, if this archive element
4604 contains a definition of the symbol, not just another common
4607 Unfortunately some archivers (including GNU ar) will put
4608 declarations of common symbols into their archive maps, as
4609 well as real definitions, so we cannot just go by the archive
4610 map alone. Instead we must read in the element's symbol
4611 table and check that to see what kind of symbol definition
4613 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4616 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4618 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4623 /* We need to include this archive member. */
4624 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4625 if (element
== NULL
)
4628 if (! bfd_check_format (element
, bfd_object
))
4631 /* Doublecheck that we have not included this object
4632 already--it should be impossible, but there may be
4633 something wrong with the archive. */
4634 if (element
->archive_pass
!= 0)
4636 bfd_set_error (bfd_error_bad_value
);
4639 element
->archive_pass
= 1;
4641 undefs_tail
= info
->hash
->undefs_tail
;
4643 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4646 if (! bfd_link_add_symbols (element
, info
))
4649 /* If there are any new undefined symbols, we need to make
4650 another pass through the archive in order to see whether
4651 they can be defined. FIXME: This isn't perfect, because
4652 common symbols wind up on undefs_tail and because an
4653 undefined symbol which is defined later on in this pass
4654 does not require another pass. This isn't a bug, but it
4655 does make the code less efficient than it could be. */
4656 if (undefs_tail
!= info
->hash
->undefs_tail
)
4659 /* Look backward to mark all symbols from this object file
4660 which we have already seen in this pass. */
4664 included
[mark
] = TRUE
;
4669 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4671 /* We mark subsequent symbols from this object file as we go
4672 on through the loop. */
4673 last
= symdef
->file_offset
;
4684 if (defined
!= NULL
)
4686 if (included
!= NULL
)
4691 /* Given an ELF BFD, add symbols to the global hash table as
4695 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4697 switch (bfd_get_format (abfd
))
4700 return elf_link_add_object_symbols (abfd
, info
);
4702 return elf_link_add_archive_symbols (abfd
, info
);
4704 bfd_set_error (bfd_error_wrong_format
);
4709 /* This function will be called though elf_link_hash_traverse to store
4710 all hash value of the exported symbols in an array. */
4713 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4715 unsigned long **valuep
= data
;
4721 if (h
->root
.type
== bfd_link_hash_warning
)
4722 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4724 /* Ignore indirect symbols. These are added by the versioning code. */
4725 if (h
->dynindx
== -1)
4728 name
= h
->root
.root
.string
;
4729 p
= strchr (name
, ELF_VER_CHR
);
4732 alc
= bfd_malloc (p
- name
+ 1);
4733 memcpy (alc
, name
, p
- name
);
4734 alc
[p
- name
] = '\0';
4738 /* Compute the hash value. */
4739 ha
= bfd_elf_hash (name
);
4741 /* Store the found hash value in the array given as the argument. */
4744 /* And store it in the struct so that we can put it in the hash table
4746 h
->u
.elf_hash_value
= ha
;
4754 /* Array used to determine the number of hash table buckets to use
4755 based on the number of symbols there are. If there are fewer than
4756 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4757 fewer than 37 we use 17 buckets, and so forth. We never use more
4758 than 32771 buckets. */
4760 static const size_t elf_buckets
[] =
4762 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4766 /* Compute bucket count for hashing table. We do not use a static set
4767 of possible tables sizes anymore. Instead we determine for all
4768 possible reasonable sizes of the table the outcome (i.e., the
4769 number of collisions etc) and choose the best solution. The
4770 weighting functions are not too simple to allow the table to grow
4771 without bounds. Instead one of the weighting factors is the size.
4772 Therefore the result is always a good payoff between few collisions
4773 (= short chain lengths) and table size. */
4775 compute_bucket_count (struct bfd_link_info
*info
)
4777 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4778 size_t best_size
= 0;
4779 unsigned long int *hashcodes
;
4780 unsigned long int *hashcodesp
;
4781 unsigned long int i
;
4784 /* Compute the hash values for all exported symbols. At the same
4785 time store the values in an array so that we could use them for
4788 amt
*= sizeof (unsigned long int);
4789 hashcodes
= bfd_malloc (amt
);
4790 if (hashcodes
== NULL
)
4792 hashcodesp
= hashcodes
;
4794 /* Put all hash values in HASHCODES. */
4795 elf_link_hash_traverse (elf_hash_table (info
),
4796 elf_collect_hash_codes
, &hashcodesp
);
4798 /* We have a problem here. The following code to optimize the table
4799 size requires an integer type with more the 32 bits. If
4800 BFD_HOST_U_64_BIT is set we know about such a type. */
4801 #ifdef BFD_HOST_U_64_BIT
4804 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4807 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4808 unsigned long int *counts
;
4809 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4810 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4812 /* Possible optimization parameters: if we have NSYMS symbols we say
4813 that the hashing table must at least have NSYMS/4 and at most
4815 minsize
= nsyms
/ 4;
4818 best_size
= maxsize
= nsyms
* 2;
4820 /* Create array where we count the collisions in. We must use bfd_malloc
4821 since the size could be large. */
4823 amt
*= sizeof (unsigned long int);
4824 counts
= bfd_malloc (amt
);
4831 /* Compute the "optimal" size for the hash table. The criteria is a
4832 minimal chain length. The minor criteria is (of course) the size
4834 for (i
= minsize
; i
< maxsize
; ++i
)
4836 /* Walk through the array of hashcodes and count the collisions. */
4837 BFD_HOST_U_64_BIT max
;
4838 unsigned long int j
;
4839 unsigned long int fact
;
4841 memset (counts
, '\0', i
* sizeof (unsigned long int));
4843 /* Determine how often each hash bucket is used. */
4844 for (j
= 0; j
< nsyms
; ++j
)
4845 ++counts
[hashcodes
[j
] % i
];
4847 /* For the weight function we need some information about the
4848 pagesize on the target. This is information need not be 100%
4849 accurate. Since this information is not available (so far) we
4850 define it here to a reasonable default value. If it is crucial
4851 to have a better value some day simply define this value. */
4852 # ifndef BFD_TARGET_PAGESIZE
4853 # define BFD_TARGET_PAGESIZE (4096)
4856 /* We in any case need 2 + NSYMS entries for the size values and
4858 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4861 /* Variant 1: optimize for short chains. We add the squares
4862 of all the chain lengths (which favors many small chain
4863 over a few long chains). */
4864 for (j
= 0; j
< i
; ++j
)
4865 max
+= counts
[j
] * counts
[j
];
4867 /* This adds penalties for the overall size of the table. */
4868 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4871 /* Variant 2: Optimize a lot more for small table. Here we
4872 also add squares of the size but we also add penalties for
4873 empty slots (the +1 term). */
4874 for (j
= 0; j
< i
; ++j
)
4875 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4877 /* The overall size of the table is considered, but not as
4878 strong as in variant 1, where it is squared. */
4879 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4883 /* Compare with current best results. */
4884 if (max
< best_chlen
)
4894 #endif /* defined (BFD_HOST_U_64_BIT) */
4896 /* This is the fallback solution if no 64bit type is available or if we
4897 are not supposed to spend much time on optimizations. We select the
4898 bucket count using a fixed set of numbers. */
4899 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4901 best_size
= elf_buckets
[i
];
4902 if (dynsymcount
< elf_buckets
[i
+ 1])
4907 /* Free the arrays we needed. */
4913 /* Set up the sizes and contents of the ELF dynamic sections. This is
4914 called by the ELF linker emulation before_allocation routine. We
4915 must set the sizes of the sections before the linker sets the
4916 addresses of the various sections. */
4919 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4922 const char *filter_shlib
,
4923 const char * const *auxiliary_filters
,
4924 struct bfd_link_info
*info
,
4925 asection
**sinterpptr
,
4926 struct bfd_elf_version_tree
*verdefs
)
4928 bfd_size_type soname_indx
;
4930 const struct elf_backend_data
*bed
;
4931 struct elf_assign_sym_version_info asvinfo
;
4935 soname_indx
= (bfd_size_type
) -1;
4937 if (!is_elf_hash_table (info
->hash
))
4940 elf_tdata (output_bfd
)->relro
= info
->relro
;
4941 if (info
->execstack
)
4942 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4943 else if (info
->noexecstack
)
4944 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4948 asection
*notesec
= NULL
;
4951 for (inputobj
= info
->input_bfds
;
4953 inputobj
= inputobj
->link_next
)
4957 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4959 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4962 if (s
->flags
& SEC_CODE
)
4971 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4972 if (exec
&& info
->relocatable
4973 && notesec
->output_section
!= bfd_abs_section_ptr
)
4974 notesec
->output_section
->flags
|= SEC_CODE
;
4978 /* Any syms created from now on start with -1 in
4979 got.refcount/offset and plt.refcount/offset. */
4980 elf_hash_table (info
)->init_got_refcount
4981 = elf_hash_table (info
)->init_got_offset
;
4982 elf_hash_table (info
)->init_plt_refcount
4983 = elf_hash_table (info
)->init_plt_offset
;
4985 /* The backend may have to create some sections regardless of whether
4986 we're dynamic or not. */
4987 bed
= get_elf_backend_data (output_bfd
);
4988 if (bed
->elf_backend_always_size_sections
4989 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4992 dynobj
= elf_hash_table (info
)->dynobj
;
4994 /* If there were no dynamic objects in the link, there is nothing to
4999 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5002 if (elf_hash_table (info
)->dynamic_sections_created
)
5004 struct elf_info_failed eif
;
5005 struct elf_link_hash_entry
*h
;
5007 struct bfd_elf_version_tree
*t
;
5008 struct bfd_elf_version_expr
*d
;
5010 bfd_boolean all_defined
;
5012 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5013 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5017 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5019 if (soname_indx
== (bfd_size_type
) -1
5020 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5026 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5028 info
->flags
|= DF_SYMBOLIC
;
5035 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5037 if (indx
== (bfd_size_type
) -1
5038 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5041 if (info
->new_dtags
)
5043 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5044 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5049 if (filter_shlib
!= NULL
)
5053 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5054 filter_shlib
, TRUE
);
5055 if (indx
== (bfd_size_type
) -1
5056 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5060 if (auxiliary_filters
!= NULL
)
5062 const char * const *p
;
5064 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5068 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5070 if (indx
== (bfd_size_type
) -1
5071 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5077 eif
.verdefs
= verdefs
;
5080 /* If we are supposed to export all symbols into the dynamic symbol
5081 table (this is not the normal case), then do so. */
5082 if (info
->export_dynamic
)
5084 elf_link_hash_traverse (elf_hash_table (info
),
5085 _bfd_elf_export_symbol
,
5091 /* Make all global versions with definition. */
5092 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5093 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5094 if (!d
->symver
&& d
->symbol
)
5096 const char *verstr
, *name
;
5097 size_t namelen
, verlen
, newlen
;
5099 struct elf_link_hash_entry
*newh
;
5102 namelen
= strlen (name
);
5104 verlen
= strlen (verstr
);
5105 newlen
= namelen
+ verlen
+ 3;
5107 newname
= bfd_malloc (newlen
);
5108 if (newname
== NULL
)
5110 memcpy (newname
, name
, namelen
);
5112 /* Check the hidden versioned definition. */
5113 p
= newname
+ namelen
;
5115 memcpy (p
, verstr
, verlen
+ 1);
5116 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5117 newname
, FALSE
, FALSE
,
5120 || (newh
->root
.type
!= bfd_link_hash_defined
5121 && newh
->root
.type
!= bfd_link_hash_defweak
))
5123 /* Check the default versioned definition. */
5125 memcpy (p
, verstr
, verlen
+ 1);
5126 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5127 newname
, FALSE
, FALSE
,
5132 /* Mark this version if there is a definition and it is
5133 not defined in a shared object. */
5135 && !newh
->def_dynamic
5136 && (newh
->root
.type
== bfd_link_hash_defined
5137 || newh
->root
.type
== bfd_link_hash_defweak
))
5141 /* Attach all the symbols to their version information. */
5142 asvinfo
.output_bfd
= output_bfd
;
5143 asvinfo
.info
= info
;
5144 asvinfo
.verdefs
= verdefs
;
5145 asvinfo
.failed
= FALSE
;
5147 elf_link_hash_traverse (elf_hash_table (info
),
5148 _bfd_elf_link_assign_sym_version
,
5153 if (!info
->allow_undefined_version
)
5155 /* Check if all global versions have a definition. */
5157 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5158 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5159 if (!d
->symver
&& !d
->script
)
5161 (*_bfd_error_handler
)
5162 (_("%s: undefined version: %s"),
5163 d
->pattern
, t
->name
);
5164 all_defined
= FALSE
;
5169 bfd_set_error (bfd_error_bad_value
);
5174 /* Find all symbols which were defined in a dynamic object and make
5175 the backend pick a reasonable value for them. */
5176 elf_link_hash_traverse (elf_hash_table (info
),
5177 _bfd_elf_adjust_dynamic_symbol
,
5182 /* Add some entries to the .dynamic section. We fill in some of the
5183 values later, in bfd_elf_final_link, but we must add the entries
5184 now so that we know the final size of the .dynamic section. */
5186 /* If there are initialization and/or finalization functions to
5187 call then add the corresponding DT_INIT/DT_FINI entries. */
5188 h
= (info
->init_function
5189 ? elf_link_hash_lookup (elf_hash_table (info
),
5190 info
->init_function
, FALSE
,
5197 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5200 h
= (info
->fini_function
5201 ? elf_link_hash_lookup (elf_hash_table (info
),
5202 info
->fini_function
, FALSE
,
5209 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5213 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5214 if (s
!= NULL
&& s
->linker_has_input
)
5216 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5217 if (! info
->executable
)
5222 for (sub
= info
->input_bfds
; sub
!= NULL
;
5223 sub
= sub
->link_next
)
5224 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5225 if (elf_section_data (o
)->this_hdr
.sh_type
5226 == SHT_PREINIT_ARRAY
)
5228 (*_bfd_error_handler
)
5229 (_("%B: .preinit_array section is not allowed in DSO"),
5234 bfd_set_error (bfd_error_nonrepresentable_section
);
5238 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5239 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5242 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5243 if (s
!= NULL
&& s
->linker_has_input
)
5245 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5246 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5249 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5250 if (s
!= NULL
&& s
->linker_has_input
)
5252 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5253 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5257 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5258 /* If .dynstr is excluded from the link, we don't want any of
5259 these tags. Strictly, we should be checking each section
5260 individually; This quick check covers for the case where
5261 someone does a /DISCARD/ : { *(*) }. */
5262 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5264 bfd_size_type strsize
;
5266 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5267 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5268 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5269 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5270 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5271 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5272 bed
->s
->sizeof_sym
))
5277 /* The backend must work out the sizes of all the other dynamic
5279 if (bed
->elf_backend_size_dynamic_sections
5280 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5283 if (elf_hash_table (info
)->dynamic_sections_created
)
5285 unsigned long section_sym_count
;
5288 /* Set up the version definition section. */
5289 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5290 BFD_ASSERT (s
!= NULL
);
5292 /* We may have created additional version definitions if we are
5293 just linking a regular application. */
5294 verdefs
= asvinfo
.verdefs
;
5296 /* Skip anonymous version tag. */
5297 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5298 verdefs
= verdefs
->next
;
5300 if (verdefs
== NULL
&& !info
->create_default_symver
)
5301 s
->flags
|= SEC_EXCLUDE
;
5306 struct bfd_elf_version_tree
*t
;
5308 Elf_Internal_Verdef def
;
5309 Elf_Internal_Verdaux defaux
;
5310 struct bfd_link_hash_entry
*bh
;
5311 struct elf_link_hash_entry
*h
;
5317 /* Make space for the base version. */
5318 size
+= sizeof (Elf_External_Verdef
);
5319 size
+= sizeof (Elf_External_Verdaux
);
5322 /* Make space for the default version. */
5323 if (info
->create_default_symver
)
5325 size
+= sizeof (Elf_External_Verdef
);
5329 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5331 struct bfd_elf_version_deps
*n
;
5333 size
+= sizeof (Elf_External_Verdef
);
5334 size
+= sizeof (Elf_External_Verdaux
);
5337 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5338 size
+= sizeof (Elf_External_Verdaux
);
5342 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5343 if (s
->contents
== NULL
&& s
->size
!= 0)
5346 /* Fill in the version definition section. */
5350 def
.vd_version
= VER_DEF_CURRENT
;
5351 def
.vd_flags
= VER_FLG_BASE
;
5354 if (info
->create_default_symver
)
5356 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5357 def
.vd_next
= sizeof (Elf_External_Verdef
);
5361 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5362 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5363 + sizeof (Elf_External_Verdaux
));
5366 if (soname_indx
!= (bfd_size_type
) -1)
5368 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5370 def
.vd_hash
= bfd_elf_hash (soname
);
5371 defaux
.vda_name
= soname_indx
;
5378 name
= lbasename (output_bfd
->filename
);
5379 def
.vd_hash
= bfd_elf_hash (name
);
5380 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5382 if (indx
== (bfd_size_type
) -1)
5384 defaux
.vda_name
= indx
;
5386 defaux
.vda_next
= 0;
5388 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5389 (Elf_External_Verdef
*) p
);
5390 p
+= sizeof (Elf_External_Verdef
);
5391 if (info
->create_default_symver
)
5393 /* Add a symbol representing this version. */
5395 if (! (_bfd_generic_link_add_one_symbol
5396 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5398 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5400 h
= (struct elf_link_hash_entry
*) bh
;
5403 h
->type
= STT_OBJECT
;
5404 h
->verinfo
.vertree
= NULL
;
5406 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5409 /* Create a duplicate of the base version with the same
5410 aux block, but different flags. */
5413 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5415 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5416 + sizeof (Elf_External_Verdaux
));
5419 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5420 (Elf_External_Verdef
*) p
);
5421 p
+= sizeof (Elf_External_Verdef
);
5423 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5424 (Elf_External_Verdaux
*) p
);
5425 p
+= sizeof (Elf_External_Verdaux
);
5427 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5430 struct bfd_elf_version_deps
*n
;
5433 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5436 /* Add a symbol representing this version. */
5438 if (! (_bfd_generic_link_add_one_symbol
5439 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5441 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5443 h
= (struct elf_link_hash_entry
*) bh
;
5446 h
->type
= STT_OBJECT
;
5447 h
->verinfo
.vertree
= t
;
5449 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5452 def
.vd_version
= VER_DEF_CURRENT
;
5454 if (t
->globals
.list
== NULL
5455 && t
->locals
.list
== NULL
5457 def
.vd_flags
|= VER_FLG_WEAK
;
5458 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5459 def
.vd_cnt
= cdeps
+ 1;
5460 def
.vd_hash
= bfd_elf_hash (t
->name
);
5461 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5463 if (t
->next
!= NULL
)
5464 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5465 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5467 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5468 (Elf_External_Verdef
*) p
);
5469 p
+= sizeof (Elf_External_Verdef
);
5471 defaux
.vda_name
= h
->dynstr_index
;
5472 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5474 defaux
.vda_next
= 0;
5475 if (t
->deps
!= NULL
)
5476 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5477 t
->name_indx
= defaux
.vda_name
;
5479 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5480 (Elf_External_Verdaux
*) p
);
5481 p
+= sizeof (Elf_External_Verdaux
);
5483 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5485 if (n
->version_needed
== NULL
)
5487 /* This can happen if there was an error in the
5489 defaux
.vda_name
= 0;
5493 defaux
.vda_name
= n
->version_needed
->name_indx
;
5494 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5497 if (n
->next
== NULL
)
5498 defaux
.vda_next
= 0;
5500 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5502 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5503 (Elf_External_Verdaux
*) p
);
5504 p
+= sizeof (Elf_External_Verdaux
);
5508 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5509 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5512 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5515 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5517 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5520 else if (info
->flags
& DF_BIND_NOW
)
5522 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5528 if (info
->executable
)
5529 info
->flags_1
&= ~ (DF_1_INITFIRST
5532 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5536 /* Work out the size of the version reference section. */
5538 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5539 BFD_ASSERT (s
!= NULL
);
5541 struct elf_find_verdep_info sinfo
;
5543 sinfo
.output_bfd
= output_bfd
;
5545 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5546 if (sinfo
.vers
== 0)
5548 sinfo
.failed
= FALSE
;
5550 elf_link_hash_traverse (elf_hash_table (info
),
5551 _bfd_elf_link_find_version_dependencies
,
5554 if (elf_tdata (output_bfd
)->verref
== NULL
)
5555 s
->flags
|= SEC_EXCLUDE
;
5558 Elf_Internal_Verneed
*t
;
5563 /* Build the version definition section. */
5566 for (t
= elf_tdata (output_bfd
)->verref
;
5570 Elf_Internal_Vernaux
*a
;
5572 size
+= sizeof (Elf_External_Verneed
);
5574 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5575 size
+= sizeof (Elf_External_Vernaux
);
5579 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5580 if (s
->contents
== NULL
)
5584 for (t
= elf_tdata (output_bfd
)->verref
;
5589 Elf_Internal_Vernaux
*a
;
5593 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5596 t
->vn_version
= VER_NEED_CURRENT
;
5598 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5599 elf_dt_name (t
->vn_bfd
) != NULL
5600 ? elf_dt_name (t
->vn_bfd
)
5601 : lbasename (t
->vn_bfd
->filename
),
5603 if (indx
== (bfd_size_type
) -1)
5606 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5607 if (t
->vn_nextref
== NULL
)
5610 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5611 + caux
* sizeof (Elf_External_Vernaux
));
5613 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5614 (Elf_External_Verneed
*) p
);
5615 p
+= sizeof (Elf_External_Verneed
);
5617 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5619 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5620 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5621 a
->vna_nodename
, FALSE
);
5622 if (indx
== (bfd_size_type
) -1)
5625 if (a
->vna_nextptr
== NULL
)
5628 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5630 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5631 (Elf_External_Vernaux
*) p
);
5632 p
+= sizeof (Elf_External_Vernaux
);
5636 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5637 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5640 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5644 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5645 && elf_tdata (output_bfd
)->cverdefs
== 0)
5646 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5647 §ion_sym_count
) == 0)
5649 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5650 s
->flags
|= SEC_EXCLUDE
;
5657 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5659 if (!is_elf_hash_table (info
->hash
))
5662 if (elf_hash_table (info
)->dynamic_sections_created
)
5665 const struct elf_backend_data
*bed
;
5667 bfd_size_type dynsymcount
;
5668 unsigned long section_sym_count
;
5669 size_t bucketcount
= 0;
5670 size_t hash_entry_size
;
5671 unsigned int dtagcount
;
5673 dynobj
= elf_hash_table (info
)->dynobj
;
5675 /* Assign dynsym indicies. In a shared library we generate a
5676 section symbol for each output section, which come first.
5677 Next come all of the back-end allocated local dynamic syms,
5678 followed by the rest of the global symbols. */
5680 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5681 §ion_sym_count
);
5683 /* Work out the size of the symbol version section. */
5684 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5685 BFD_ASSERT (s
!= NULL
);
5686 if (dynsymcount
!= 0
5687 && (s
->flags
& SEC_EXCLUDE
) == 0)
5689 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5690 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5691 if (s
->contents
== NULL
)
5694 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5698 /* Set the size of the .dynsym and .hash sections. We counted
5699 the number of dynamic symbols in elf_link_add_object_symbols.
5700 We will build the contents of .dynsym and .hash when we build
5701 the final symbol table, because until then we do not know the
5702 correct value to give the symbols. We built the .dynstr
5703 section as we went along in elf_link_add_object_symbols. */
5704 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5705 BFD_ASSERT (s
!= NULL
);
5706 bed
= get_elf_backend_data (output_bfd
);
5707 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5709 if (dynsymcount
!= 0)
5711 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5712 if (s
->contents
== NULL
)
5715 /* The first entry in .dynsym is a dummy symbol.
5716 Clear all the section syms, in case we don't output them all. */
5717 ++section_sym_count
;
5718 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5721 /* Compute the size of the hashing table. As a side effect this
5722 computes the hash values for all the names we export. */
5723 bucketcount
= compute_bucket_count (info
);
5725 s
= bfd_get_section_by_name (dynobj
, ".hash");
5726 BFD_ASSERT (s
!= NULL
);
5727 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5728 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5729 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5730 if (s
->contents
== NULL
)
5733 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5734 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5735 s
->contents
+ hash_entry_size
);
5737 elf_hash_table (info
)->bucketcount
= bucketcount
;
5739 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5740 BFD_ASSERT (s
!= NULL
);
5742 elf_finalize_dynstr (output_bfd
, info
);
5744 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5746 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5747 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5754 /* Final phase of ELF linker. */
5756 /* A structure we use to avoid passing large numbers of arguments. */
5758 struct elf_final_link_info
5760 /* General link information. */
5761 struct bfd_link_info
*info
;
5764 /* Symbol string table. */
5765 struct bfd_strtab_hash
*symstrtab
;
5766 /* .dynsym section. */
5767 asection
*dynsym_sec
;
5768 /* .hash section. */
5770 /* symbol version section (.gnu.version). */
5771 asection
*symver_sec
;
5772 /* Buffer large enough to hold contents of any section. */
5774 /* Buffer large enough to hold external relocs of any section. */
5775 void *external_relocs
;
5776 /* Buffer large enough to hold internal relocs of any section. */
5777 Elf_Internal_Rela
*internal_relocs
;
5778 /* Buffer large enough to hold external local symbols of any input
5780 bfd_byte
*external_syms
;
5781 /* And a buffer for symbol section indices. */
5782 Elf_External_Sym_Shndx
*locsym_shndx
;
5783 /* Buffer large enough to hold internal local symbols of any input
5785 Elf_Internal_Sym
*internal_syms
;
5786 /* Array large enough to hold a symbol index for each local symbol
5787 of any input BFD. */
5789 /* Array large enough to hold a section pointer for each local
5790 symbol of any input BFD. */
5791 asection
**sections
;
5792 /* Buffer to hold swapped out symbols. */
5794 /* And one for symbol section indices. */
5795 Elf_External_Sym_Shndx
*symshndxbuf
;
5796 /* Number of swapped out symbols in buffer. */
5797 size_t symbuf_count
;
5798 /* Number of symbols which fit in symbuf. */
5800 /* And same for symshndxbuf. */
5801 size_t shndxbuf_size
;
5804 /* This struct is used to pass information to elf_link_output_extsym. */
5806 struct elf_outext_info
5809 bfd_boolean localsyms
;
5810 struct elf_final_link_info
*finfo
;
5813 /* When performing a relocatable link, the input relocations are
5814 preserved. But, if they reference global symbols, the indices
5815 referenced must be updated. Update all the relocations in
5816 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5819 elf_link_adjust_relocs (bfd
*abfd
,
5820 Elf_Internal_Shdr
*rel_hdr
,
5822 struct elf_link_hash_entry
**rel_hash
)
5825 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5827 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5828 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5829 bfd_vma r_type_mask
;
5832 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5834 swap_in
= bed
->s
->swap_reloc_in
;
5835 swap_out
= bed
->s
->swap_reloc_out
;
5837 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5839 swap_in
= bed
->s
->swap_reloca_in
;
5840 swap_out
= bed
->s
->swap_reloca_out
;
5845 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5848 if (bed
->s
->arch_size
== 32)
5855 r_type_mask
= 0xffffffff;
5859 erela
= rel_hdr
->contents
;
5860 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5862 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5865 if (*rel_hash
== NULL
)
5868 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5870 (*swap_in
) (abfd
, erela
, irela
);
5871 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5872 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5873 | (irela
[j
].r_info
& r_type_mask
));
5874 (*swap_out
) (abfd
, irela
, erela
);
5878 struct elf_link_sort_rela
5884 enum elf_reloc_type_class type
;
5885 /* We use this as an array of size int_rels_per_ext_rel. */
5886 Elf_Internal_Rela rela
[1];
5890 elf_link_sort_cmp1 (const void *A
, const void *B
)
5892 const struct elf_link_sort_rela
*a
= A
;
5893 const struct elf_link_sort_rela
*b
= B
;
5894 int relativea
, relativeb
;
5896 relativea
= a
->type
== reloc_class_relative
;
5897 relativeb
= b
->type
== reloc_class_relative
;
5899 if (relativea
< relativeb
)
5901 if (relativea
> relativeb
)
5903 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5905 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5907 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5909 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5915 elf_link_sort_cmp2 (const void *A
, const void *B
)
5917 const struct elf_link_sort_rela
*a
= A
;
5918 const struct elf_link_sort_rela
*b
= B
;
5921 if (a
->u
.offset
< b
->u
.offset
)
5923 if (a
->u
.offset
> b
->u
.offset
)
5925 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5926 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5931 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5933 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5939 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5942 bfd_size_type count
, size
;
5943 size_t i
, ret
, sort_elt
, ext_size
;
5944 bfd_byte
*sort
, *s_non_relative
, *p
;
5945 struct elf_link_sort_rela
*sq
;
5946 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5947 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5948 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5949 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5950 struct bfd_link_order
*lo
;
5953 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5954 if (reldyn
== NULL
|| reldyn
->size
== 0)
5956 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5957 if (reldyn
== NULL
|| reldyn
->size
== 0)
5959 ext_size
= bed
->s
->sizeof_rel
;
5960 swap_in
= bed
->s
->swap_reloc_in
;
5961 swap_out
= bed
->s
->swap_reloc_out
;
5965 ext_size
= bed
->s
->sizeof_rela
;
5966 swap_in
= bed
->s
->swap_reloca_in
;
5967 swap_out
= bed
->s
->swap_reloca_out
;
5969 count
= reldyn
->size
/ ext_size
;
5972 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5973 if (lo
->type
== bfd_indirect_link_order
)
5975 asection
*o
= lo
->u
.indirect
.section
;
5979 if (size
!= reldyn
->size
)
5982 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5983 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5984 sort
= bfd_zmalloc (sort_elt
* count
);
5987 (*info
->callbacks
->warning
)
5988 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5992 if (bed
->s
->arch_size
== 32)
5993 r_sym_mask
= ~(bfd_vma
) 0xff;
5995 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5997 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5998 if (lo
->type
== bfd_indirect_link_order
)
6000 bfd_byte
*erel
, *erelend
;
6001 asection
*o
= lo
->u
.indirect
.section
;
6003 if (o
->contents
== NULL
&& o
->size
!= 0)
6005 /* This is a reloc section that is being handled as a normal
6006 section. See bfd_section_from_shdr. We can't combine
6007 relocs in this case. */
6012 erelend
= o
->contents
+ o
->size
;
6013 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6014 while (erel
< erelend
)
6016 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6017 (*swap_in
) (abfd
, erel
, s
->rela
);
6018 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6019 s
->u
.sym_mask
= r_sym_mask
;
6025 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6027 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6029 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6030 if (s
->type
!= reloc_class_relative
)
6036 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6037 for (; i
< count
; i
++, p
+= sort_elt
)
6039 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6040 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6042 sp
->u
.offset
= sq
->rela
->r_offset
;
6045 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6047 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6048 if (lo
->type
== bfd_indirect_link_order
)
6050 bfd_byte
*erel
, *erelend
;
6051 asection
*o
= lo
->u
.indirect
.section
;
6054 erelend
= o
->contents
+ o
->size
;
6055 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6056 while (erel
< erelend
)
6058 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6059 (*swap_out
) (abfd
, s
->rela
, erel
);
6070 /* Flush the output symbols to the file. */
6073 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6074 const struct elf_backend_data
*bed
)
6076 if (finfo
->symbuf_count
> 0)
6078 Elf_Internal_Shdr
*hdr
;
6082 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6083 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6084 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6085 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6086 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6089 hdr
->sh_size
+= amt
;
6090 finfo
->symbuf_count
= 0;
6096 /* Add a symbol to the output symbol table. */
6099 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6101 Elf_Internal_Sym
*elfsym
,
6102 asection
*input_sec
,
6103 struct elf_link_hash_entry
*h
)
6106 Elf_External_Sym_Shndx
*destshndx
;
6107 bfd_boolean (*output_symbol_hook
)
6108 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6109 struct elf_link_hash_entry
*);
6110 const struct elf_backend_data
*bed
;
6112 bed
= get_elf_backend_data (finfo
->output_bfd
);
6113 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6114 if (output_symbol_hook
!= NULL
)
6116 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6120 if (name
== NULL
|| *name
== '\0')
6121 elfsym
->st_name
= 0;
6122 else if (input_sec
->flags
& SEC_EXCLUDE
)
6123 elfsym
->st_name
= 0;
6126 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6128 if (elfsym
->st_name
== (unsigned long) -1)
6132 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6134 if (! elf_link_flush_output_syms (finfo
, bed
))
6138 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6139 destshndx
= finfo
->symshndxbuf
;
6140 if (destshndx
!= NULL
)
6142 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6146 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6147 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6148 if (destshndx
== NULL
)
6150 memset ((char *) destshndx
+ amt
, 0, amt
);
6151 finfo
->shndxbuf_size
*= 2;
6153 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6156 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6157 finfo
->symbuf_count
+= 1;
6158 bfd_get_symcount (finfo
->output_bfd
) += 1;
6163 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6164 allowing an unsatisfied unversioned symbol in the DSO to match a
6165 versioned symbol that would normally require an explicit version.
6166 We also handle the case that a DSO references a hidden symbol
6167 which may be satisfied by a versioned symbol in another DSO. */
6170 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6171 const struct elf_backend_data
*bed
,
6172 struct elf_link_hash_entry
*h
)
6175 struct elf_link_loaded_list
*loaded
;
6177 if (!is_elf_hash_table (info
->hash
))
6180 switch (h
->root
.type
)
6186 case bfd_link_hash_undefined
:
6187 case bfd_link_hash_undefweak
:
6188 abfd
= h
->root
.u
.undef
.abfd
;
6189 if ((abfd
->flags
& DYNAMIC
) == 0
6190 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6194 case bfd_link_hash_defined
:
6195 case bfd_link_hash_defweak
:
6196 abfd
= h
->root
.u
.def
.section
->owner
;
6199 case bfd_link_hash_common
:
6200 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6203 BFD_ASSERT (abfd
!= NULL
);
6205 for (loaded
= elf_hash_table (info
)->loaded
;
6207 loaded
= loaded
->next
)
6210 Elf_Internal_Shdr
*hdr
;
6211 bfd_size_type symcount
;
6212 bfd_size_type extsymcount
;
6213 bfd_size_type extsymoff
;
6214 Elf_Internal_Shdr
*versymhdr
;
6215 Elf_Internal_Sym
*isym
;
6216 Elf_Internal_Sym
*isymend
;
6217 Elf_Internal_Sym
*isymbuf
;
6218 Elf_External_Versym
*ever
;
6219 Elf_External_Versym
*extversym
;
6221 input
= loaded
->abfd
;
6223 /* We check each DSO for a possible hidden versioned definition. */
6225 || (input
->flags
& DYNAMIC
) == 0
6226 || elf_dynversym (input
) == 0)
6229 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6231 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6232 if (elf_bad_symtab (input
))
6234 extsymcount
= symcount
;
6239 extsymcount
= symcount
- hdr
->sh_info
;
6240 extsymoff
= hdr
->sh_info
;
6243 if (extsymcount
== 0)
6246 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6248 if (isymbuf
== NULL
)
6251 /* Read in any version definitions. */
6252 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6253 extversym
= bfd_malloc (versymhdr
->sh_size
);
6254 if (extversym
== NULL
)
6257 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6258 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6259 != versymhdr
->sh_size
))
6267 ever
= extversym
+ extsymoff
;
6268 isymend
= isymbuf
+ extsymcount
;
6269 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6272 Elf_Internal_Versym iver
;
6273 unsigned short version_index
;
6275 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6276 || isym
->st_shndx
== SHN_UNDEF
)
6279 name
= bfd_elf_string_from_elf_section (input
,
6282 if (strcmp (name
, h
->root
.root
.string
) != 0)
6285 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6287 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6289 /* If we have a non-hidden versioned sym, then it should
6290 have provided a definition for the undefined sym. */
6294 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6295 if (version_index
== 1 || version_index
== 2)
6297 /* This is the base or first version. We can use it. */
6311 /* Add an external symbol to the symbol table. This is called from
6312 the hash table traversal routine. When generating a shared object,
6313 we go through the symbol table twice. The first time we output
6314 anything that might have been forced to local scope in a version
6315 script. The second time we output the symbols that are still
6319 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6321 struct elf_outext_info
*eoinfo
= data
;
6322 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6324 Elf_Internal_Sym sym
;
6325 asection
*input_sec
;
6326 const struct elf_backend_data
*bed
;
6328 if (h
->root
.type
== bfd_link_hash_warning
)
6330 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6331 if (h
->root
.type
== bfd_link_hash_new
)
6335 /* Decide whether to output this symbol in this pass. */
6336 if (eoinfo
->localsyms
)
6338 if (!h
->forced_local
)
6343 if (h
->forced_local
)
6347 bed
= get_elf_backend_data (finfo
->output_bfd
);
6349 /* If we have an undefined symbol reference here then it must have
6350 come from a shared library that is being linked in. (Undefined
6351 references in regular files have already been handled). If we
6352 are reporting errors for this situation then do so now. */
6353 if (h
->root
.type
== bfd_link_hash_undefined
6356 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6357 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6359 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6360 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6361 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6363 eoinfo
->failed
= TRUE
;
6368 /* We should also warn if a forced local symbol is referenced from
6369 shared libraries. */
6370 if (! finfo
->info
->relocatable
6371 && (! finfo
->info
->shared
)
6376 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6378 (*_bfd_error_handler
)
6379 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6381 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6382 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6383 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6385 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6386 ? "hidden" : "local",
6387 h
->root
.root
.string
);
6388 eoinfo
->failed
= TRUE
;
6392 /* We don't want to output symbols that have never been mentioned by
6393 a regular file, or that we have been told to strip. However, if
6394 h->indx is set to -2, the symbol is used by a reloc and we must
6398 else if ((h
->def_dynamic
6400 || h
->root
.type
== bfd_link_hash_new
)
6404 else if (finfo
->info
->strip
== strip_all
)
6406 else if (finfo
->info
->strip
== strip_some
6407 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6408 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6410 else if (finfo
->info
->strip_discarded
6411 && (h
->root
.type
== bfd_link_hash_defined
6412 || h
->root
.type
== bfd_link_hash_defweak
)
6413 && elf_discarded_section (h
->root
.u
.def
.section
))
6418 /* If we're stripping it, and it's not a dynamic symbol, there's
6419 nothing else to do unless it is a forced local symbol. */
6422 && !h
->forced_local
)
6426 sym
.st_size
= h
->size
;
6427 sym
.st_other
= h
->other
;
6428 if (h
->forced_local
)
6429 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6430 else if (h
->root
.type
== bfd_link_hash_undefweak
6431 || h
->root
.type
== bfd_link_hash_defweak
)
6432 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6434 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6436 switch (h
->root
.type
)
6439 case bfd_link_hash_new
:
6440 case bfd_link_hash_warning
:
6444 case bfd_link_hash_undefined
:
6445 case bfd_link_hash_undefweak
:
6446 input_sec
= bfd_und_section_ptr
;
6447 sym
.st_shndx
= SHN_UNDEF
;
6450 case bfd_link_hash_defined
:
6451 case bfd_link_hash_defweak
:
6453 input_sec
= h
->root
.u
.def
.section
;
6454 if (input_sec
->output_section
!= NULL
)
6457 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6458 input_sec
->output_section
);
6459 if (sym
.st_shndx
== SHN_BAD
)
6461 (*_bfd_error_handler
)
6462 (_("%B: could not find output section %A for input section %A"),
6463 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6464 eoinfo
->failed
= TRUE
;
6468 /* ELF symbols in relocatable files are section relative,
6469 but in nonrelocatable files they are virtual
6471 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6472 if (! finfo
->info
->relocatable
)
6474 sym
.st_value
+= input_sec
->output_section
->vma
;
6475 if (h
->type
== STT_TLS
)
6477 /* STT_TLS symbols are relative to PT_TLS segment
6479 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6480 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6486 BFD_ASSERT (input_sec
->owner
== NULL
6487 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6488 sym
.st_shndx
= SHN_UNDEF
;
6489 input_sec
= bfd_und_section_ptr
;
6494 case bfd_link_hash_common
:
6495 input_sec
= h
->root
.u
.c
.p
->section
;
6496 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6497 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6500 case bfd_link_hash_indirect
:
6501 /* These symbols are created by symbol versioning. They point
6502 to the decorated version of the name. For example, if the
6503 symbol foo@@GNU_1.2 is the default, which should be used when
6504 foo is used with no version, then we add an indirect symbol
6505 foo which points to foo@@GNU_1.2. We ignore these symbols,
6506 since the indirected symbol is already in the hash table. */
6510 /* Give the processor backend a chance to tweak the symbol value,
6511 and also to finish up anything that needs to be done for this
6512 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6513 forced local syms when non-shared is due to a historical quirk. */
6514 if ((h
->dynindx
!= -1
6516 && ((finfo
->info
->shared
6517 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6518 || h
->root
.type
!= bfd_link_hash_undefweak
))
6519 || !h
->forced_local
)
6520 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6522 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6523 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6525 eoinfo
->failed
= TRUE
;
6530 /* If we are marking the symbol as undefined, and there are no
6531 non-weak references to this symbol from a regular object, then
6532 mark the symbol as weak undefined; if there are non-weak
6533 references, mark the symbol as strong. We can't do this earlier,
6534 because it might not be marked as undefined until the
6535 finish_dynamic_symbol routine gets through with it. */
6536 if (sym
.st_shndx
== SHN_UNDEF
6538 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6539 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6543 if (h
->ref_regular_nonweak
)
6544 bindtype
= STB_GLOBAL
;
6546 bindtype
= STB_WEAK
;
6547 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6550 /* If a non-weak symbol with non-default visibility is not defined
6551 locally, it is a fatal error. */
6552 if (! finfo
->info
->relocatable
6553 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6554 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6555 && h
->root
.type
== bfd_link_hash_undefined
6558 (*_bfd_error_handler
)
6559 (_("%B: %s symbol `%s' isn't defined"),
6561 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6563 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6564 ? "internal" : "hidden",
6565 h
->root
.root
.string
);
6566 eoinfo
->failed
= TRUE
;
6570 /* If this symbol should be put in the .dynsym section, then put it
6571 there now. We already know the symbol index. We also fill in
6572 the entry in the .hash section. */
6573 if (h
->dynindx
!= -1
6574 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6578 size_t hash_entry_size
;
6579 bfd_byte
*bucketpos
;
6583 sym
.st_name
= h
->dynstr_index
;
6584 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6585 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6587 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6588 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6590 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6591 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6592 + (bucket
+ 2) * hash_entry_size
);
6593 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6594 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6595 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6596 ((bfd_byte
*) finfo
->hash_sec
->contents
6597 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6599 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6601 Elf_Internal_Versym iversym
;
6602 Elf_External_Versym
*eversym
;
6604 if (!h
->def_regular
)
6606 if (h
->verinfo
.verdef
== NULL
)
6607 iversym
.vs_vers
= 0;
6609 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6613 if (h
->verinfo
.vertree
== NULL
)
6614 iversym
.vs_vers
= 1;
6616 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6617 if (finfo
->info
->create_default_symver
)
6622 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6624 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6625 eversym
+= h
->dynindx
;
6626 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6630 /* If we're stripping it, then it was just a dynamic symbol, and
6631 there's nothing else to do. */
6632 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6635 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6637 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6639 eoinfo
->failed
= TRUE
;
6646 /* Return TRUE if special handling is done for relocs in SEC against
6647 symbols defined in discarded sections. */
6650 elf_section_ignore_discarded_relocs (asection
*sec
)
6652 const struct elf_backend_data
*bed
;
6654 switch (sec
->sec_info_type
)
6656 case ELF_INFO_TYPE_STABS
:
6657 case ELF_INFO_TYPE_EH_FRAME
:
6663 bed
= get_elf_backend_data (sec
->owner
);
6664 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6665 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6671 /* Return a mask saying how ld should treat relocations in SEC against
6672 symbols defined in discarded sections. If this function returns
6673 COMPLAIN set, ld will issue a warning message. If this function
6674 returns PRETEND set, and the discarded section was link-once and the
6675 same size as the kept link-once section, ld will pretend that the
6676 symbol was actually defined in the kept section. Otherwise ld will
6677 zero the reloc (at least that is the intent, but some cooperation by
6678 the target dependent code is needed, particularly for REL targets). */
6681 _bfd_elf_default_action_discarded (asection
*sec
)
6683 if (sec
->flags
& SEC_DEBUGGING
)
6686 if (strcmp (".eh_frame", sec
->name
) == 0)
6689 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6692 return COMPLAIN
| PRETEND
;
6695 /* Find a match between a section and a member of a section group. */
6698 match_group_member (asection
*sec
, asection
*group
)
6700 asection
*first
= elf_next_in_group (group
);
6701 asection
*s
= first
;
6705 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6715 /* Check if the kept section of a discarded section SEC can be used
6716 to replace it. Return the replacement if it is OK. Otherwise return
6720 _bfd_elf_check_kept_section (asection
*sec
)
6724 kept
= sec
->kept_section
;
6727 if (elf_sec_group (sec
) != NULL
)
6728 kept
= match_group_member (sec
, kept
);
6729 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6735 /* Link an input file into the linker output file. This function
6736 handles all the sections and relocations of the input file at once.
6737 This is so that we only have to read the local symbols once, and
6738 don't have to keep them in memory. */
6741 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6743 bfd_boolean (*relocate_section
)
6744 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6745 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6747 Elf_Internal_Shdr
*symtab_hdr
;
6750 Elf_Internal_Sym
*isymbuf
;
6751 Elf_Internal_Sym
*isym
;
6752 Elf_Internal_Sym
*isymend
;
6754 asection
**ppsection
;
6756 const struct elf_backend_data
*bed
;
6757 bfd_boolean emit_relocs
;
6758 struct elf_link_hash_entry
**sym_hashes
;
6760 output_bfd
= finfo
->output_bfd
;
6761 bed
= get_elf_backend_data (output_bfd
);
6762 relocate_section
= bed
->elf_backend_relocate_section
;
6764 /* If this is a dynamic object, we don't want to do anything here:
6765 we don't want the local symbols, and we don't want the section
6767 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6770 emit_relocs
= (finfo
->info
->relocatable
6771 || finfo
->info
->emitrelocations
);
6773 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6774 if (elf_bad_symtab (input_bfd
))
6776 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6781 locsymcount
= symtab_hdr
->sh_info
;
6782 extsymoff
= symtab_hdr
->sh_info
;
6785 /* Read the local symbols. */
6786 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6787 if (isymbuf
== NULL
&& locsymcount
!= 0)
6789 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6790 finfo
->internal_syms
,
6791 finfo
->external_syms
,
6792 finfo
->locsym_shndx
);
6793 if (isymbuf
== NULL
)
6797 /* Find local symbol sections and adjust values of symbols in
6798 SEC_MERGE sections. Write out those local symbols we know are
6799 going into the output file. */
6800 isymend
= isymbuf
+ locsymcount
;
6801 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6803 isym
++, pindex
++, ppsection
++)
6807 Elf_Internal_Sym osym
;
6811 if (elf_bad_symtab (input_bfd
))
6813 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6820 if (isym
->st_shndx
== SHN_UNDEF
)
6821 isec
= bfd_und_section_ptr
;
6822 else if (isym
->st_shndx
< SHN_LORESERVE
6823 || isym
->st_shndx
> SHN_HIRESERVE
)
6825 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6827 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6828 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6830 _bfd_merged_section_offset (output_bfd
, &isec
,
6831 elf_section_data (isec
)->sec_info
,
6834 else if (isym
->st_shndx
== SHN_ABS
)
6835 isec
= bfd_abs_section_ptr
;
6836 else if (isym
->st_shndx
== SHN_COMMON
)
6837 isec
= bfd_com_section_ptr
;
6840 /* Don't attempt to output symbols with st_shnx in the
6841 reserved range other than SHN_ABS and SHN_COMMON. */
6848 /* Don't output the first, undefined, symbol. */
6849 if (ppsection
== finfo
->sections
)
6852 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6854 /* We never output section symbols. Instead, we use the
6855 section symbol of the corresponding section in the output
6860 /* If we are stripping all symbols, we don't want to output this
6862 if (finfo
->info
->strip
== strip_all
)
6865 /* If we are discarding all local symbols, we don't want to
6866 output this one. If we are generating a relocatable output
6867 file, then some of the local symbols may be required by
6868 relocs; we output them below as we discover that they are
6870 if (finfo
->info
->discard
== discard_all
)
6873 /* If this symbol is defined in a section which we are
6874 discarding, we don't need to keep it. */
6875 if (isym
->st_shndx
!= SHN_UNDEF
6876 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6878 || bfd_section_removed_from_list (output_bfd
,
6879 isec
->output_section
)))
6882 /* Get the name of the symbol. */
6883 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6888 /* See if we are discarding symbols with this name. */
6889 if ((finfo
->info
->strip
== strip_some
6890 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6892 || (((finfo
->info
->discard
== discard_sec_merge
6893 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6894 || finfo
->info
->discard
== discard_l
)
6895 && bfd_is_local_label_name (input_bfd
, name
)))
6898 /* If we get here, we are going to output this symbol. */
6902 /* Adjust the section index for the output file. */
6903 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6904 isec
->output_section
);
6905 if (osym
.st_shndx
== SHN_BAD
)
6908 *pindex
= bfd_get_symcount (output_bfd
);
6910 /* ELF symbols in relocatable files are section relative, but
6911 in executable files they are virtual addresses. Note that
6912 this code assumes that all ELF sections have an associated
6913 BFD section with a reasonable value for output_offset; below
6914 we assume that they also have a reasonable value for
6915 output_section. Any special sections must be set up to meet
6916 these requirements. */
6917 osym
.st_value
+= isec
->output_offset
;
6918 if (! finfo
->info
->relocatable
)
6920 osym
.st_value
+= isec
->output_section
->vma
;
6921 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6923 /* STT_TLS symbols are relative to PT_TLS segment base. */
6924 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6925 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6929 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6933 /* Relocate the contents of each section. */
6934 sym_hashes
= elf_sym_hashes (input_bfd
);
6935 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6939 if (! o
->linker_mark
)
6941 /* This section was omitted from the link. */
6945 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6946 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6949 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6951 /* Section was created by _bfd_elf_link_create_dynamic_sections
6956 /* Get the contents of the section. They have been cached by a
6957 relaxation routine. Note that o is a section in an input
6958 file, so the contents field will not have been set by any of
6959 the routines which work on output files. */
6960 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6961 contents
= elf_section_data (o
)->this_hdr
.contents
;
6964 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6966 contents
= finfo
->contents
;
6967 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6971 if ((o
->flags
& SEC_RELOC
) != 0)
6973 Elf_Internal_Rela
*internal_relocs
;
6974 bfd_vma r_type_mask
;
6977 /* Get the swapped relocs. */
6979 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6980 finfo
->internal_relocs
, FALSE
);
6981 if (internal_relocs
== NULL
6982 && o
->reloc_count
> 0)
6985 if (bed
->s
->arch_size
== 32)
6992 r_type_mask
= 0xffffffff;
6996 /* Run through the relocs looking for any against symbols
6997 from discarded sections and section symbols from
6998 removed link-once sections. Complain about relocs
6999 against discarded sections. Zero relocs against removed
7000 link-once sections. Preserve debug information as much
7002 if (!elf_section_ignore_discarded_relocs (o
))
7004 Elf_Internal_Rela
*rel
, *relend
;
7005 unsigned int action
= (*bed
->action_discarded
) (o
);
7007 rel
= internal_relocs
;
7008 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7009 for ( ; rel
< relend
; rel
++)
7011 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7012 asection
**ps
, *sec
;
7013 struct elf_link_hash_entry
*h
= NULL
;
7014 const char *sym_name
;
7016 if (r_symndx
== STN_UNDEF
)
7019 if (r_symndx
>= locsymcount
7020 || (elf_bad_symtab (input_bfd
)
7021 && finfo
->sections
[r_symndx
] == NULL
))
7023 h
= sym_hashes
[r_symndx
- extsymoff
];
7025 /* Badly formatted input files can contain relocs that
7026 reference non-existant symbols. Check here so that
7027 we do not seg fault. */
7032 sprintf_vma (buffer
, rel
->r_info
);
7033 (*_bfd_error_handler
)
7034 (_("error: %B contains a reloc (0x%s) for section %A "
7035 "that references a non-existent global symbol"),
7036 input_bfd
, o
, buffer
);
7037 bfd_set_error (bfd_error_bad_value
);
7041 while (h
->root
.type
== bfd_link_hash_indirect
7042 || h
->root
.type
== bfd_link_hash_warning
)
7043 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7045 if (h
->root
.type
!= bfd_link_hash_defined
7046 && h
->root
.type
!= bfd_link_hash_defweak
)
7049 ps
= &h
->root
.u
.def
.section
;
7050 sym_name
= h
->root
.root
.string
;
7054 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7055 ps
= &finfo
->sections
[r_symndx
];
7056 sym_name
= bfd_elf_sym_name (input_bfd
,
7061 /* Complain if the definition comes from a
7062 discarded section. */
7063 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7065 BFD_ASSERT (r_symndx
!= 0);
7066 if (action
& COMPLAIN
)
7067 (*finfo
->info
->callbacks
->einfo
)
7068 (_("%X`%s' referenced in section `%A' of %B: "
7069 "defined in discarded section `%A' of %B\n"),
7070 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7072 /* Try to do the best we can to support buggy old
7073 versions of gcc. If we've warned, or this is
7074 debugging info, pretend that the symbol is
7075 really defined in the kept linkonce section.
7076 FIXME: This is quite broken. Modifying the
7077 symbol here means we will be changing all later
7078 uses of the symbol, not just in this section.
7079 The only thing that makes this half reasonable
7080 is that we warn in non-debug sections, and
7081 debug sections tend to come after other
7083 if (action
& PRETEND
)
7087 kept
= _bfd_elf_check_kept_section (sec
);
7095 /* Remove the symbol reference from the reloc, but
7096 don't kill the reloc completely. This is so that
7097 a zero value will be written into the section,
7098 which may have non-zero contents put there by the
7099 assembler. Zero in things like an eh_frame fde
7100 pc_begin allows stack unwinders to recognize the
7102 rel
->r_info
&= r_type_mask
;
7108 /* Relocate the section by invoking a back end routine.
7110 The back end routine is responsible for adjusting the
7111 section contents as necessary, and (if using Rela relocs
7112 and generating a relocatable output file) adjusting the
7113 reloc addend as necessary.
7115 The back end routine does not have to worry about setting
7116 the reloc address or the reloc symbol index.
7118 The back end routine is given a pointer to the swapped in
7119 internal symbols, and can access the hash table entries
7120 for the external symbols via elf_sym_hashes (input_bfd).
7122 When generating relocatable output, the back end routine
7123 must handle STB_LOCAL/STT_SECTION symbols specially. The
7124 output symbol is going to be a section symbol
7125 corresponding to the output section, which will require
7126 the addend to be adjusted. */
7128 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7129 input_bfd
, o
, contents
,
7137 Elf_Internal_Rela
*irela
;
7138 Elf_Internal_Rela
*irelaend
;
7139 bfd_vma last_offset
;
7140 struct elf_link_hash_entry
**rel_hash
;
7141 struct elf_link_hash_entry
**rel_hash_list
;
7142 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7143 unsigned int next_erel
;
7144 bfd_boolean rela_normal
;
7146 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7147 rela_normal
= (bed
->rela_normal
7148 && (input_rel_hdr
->sh_entsize
7149 == bed
->s
->sizeof_rela
));
7151 /* Adjust the reloc addresses and symbol indices. */
7153 irela
= internal_relocs
;
7154 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7155 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7156 + elf_section_data (o
->output_section
)->rel_count
7157 + elf_section_data (o
->output_section
)->rel_count2
);
7158 rel_hash_list
= rel_hash
;
7159 last_offset
= o
->output_offset
;
7160 if (!finfo
->info
->relocatable
)
7161 last_offset
+= o
->output_section
->vma
;
7162 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7164 unsigned long r_symndx
;
7166 Elf_Internal_Sym sym
;
7168 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7174 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7177 if (irela
->r_offset
>= (bfd_vma
) -2)
7179 /* This is a reloc for a deleted entry or somesuch.
7180 Turn it into an R_*_NONE reloc, at the same
7181 offset as the last reloc. elf_eh_frame.c and
7182 elf_bfd_discard_info rely on reloc offsets
7184 irela
->r_offset
= last_offset
;
7186 irela
->r_addend
= 0;
7190 irela
->r_offset
+= o
->output_offset
;
7192 /* Relocs in an executable have to be virtual addresses. */
7193 if (!finfo
->info
->relocatable
)
7194 irela
->r_offset
+= o
->output_section
->vma
;
7196 last_offset
= irela
->r_offset
;
7198 r_symndx
= irela
->r_info
>> r_sym_shift
;
7199 if (r_symndx
== STN_UNDEF
)
7202 if (r_symndx
>= locsymcount
7203 || (elf_bad_symtab (input_bfd
)
7204 && finfo
->sections
[r_symndx
] == NULL
))
7206 struct elf_link_hash_entry
*rh
;
7209 /* This is a reloc against a global symbol. We
7210 have not yet output all the local symbols, so
7211 we do not know the symbol index of any global
7212 symbol. We set the rel_hash entry for this
7213 reloc to point to the global hash table entry
7214 for this symbol. The symbol index is then
7215 set at the end of bfd_elf_final_link. */
7216 indx
= r_symndx
- extsymoff
;
7217 rh
= elf_sym_hashes (input_bfd
)[indx
];
7218 while (rh
->root
.type
== bfd_link_hash_indirect
7219 || rh
->root
.type
== bfd_link_hash_warning
)
7220 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7222 /* Setting the index to -2 tells
7223 elf_link_output_extsym that this symbol is
7225 BFD_ASSERT (rh
->indx
< 0);
7233 /* This is a reloc against a local symbol. */
7236 sym
= isymbuf
[r_symndx
];
7237 sec
= finfo
->sections
[r_symndx
];
7238 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7240 /* I suppose the backend ought to fill in the
7241 section of any STT_SECTION symbol against a
7242 processor specific section. */
7244 if (bfd_is_abs_section (sec
))
7246 else if (sec
== NULL
|| sec
->owner
== NULL
)
7248 bfd_set_error (bfd_error_bad_value
);
7253 asection
*osec
= sec
->output_section
;
7255 /* If we have discarded a section, the output
7256 section will be the absolute section. In
7257 case of discarded link-once and discarded
7258 SEC_MERGE sections, use the kept section. */
7259 if (bfd_is_abs_section (osec
)
7260 && sec
->kept_section
!= NULL
7261 && sec
->kept_section
->output_section
!= NULL
)
7263 osec
= sec
->kept_section
->output_section
;
7264 irela
->r_addend
-= osec
->vma
;
7267 if (!bfd_is_abs_section (osec
))
7269 r_symndx
= osec
->target_index
;
7270 BFD_ASSERT (r_symndx
!= 0);
7274 /* Adjust the addend according to where the
7275 section winds up in the output section. */
7277 irela
->r_addend
+= sec
->output_offset
;
7281 if (finfo
->indices
[r_symndx
] == -1)
7283 unsigned long shlink
;
7287 if (finfo
->info
->strip
== strip_all
)
7289 /* You can't do ld -r -s. */
7290 bfd_set_error (bfd_error_invalid_operation
);
7294 /* This symbol was skipped earlier, but
7295 since it is needed by a reloc, we
7296 must output it now. */
7297 shlink
= symtab_hdr
->sh_link
;
7298 name
= (bfd_elf_string_from_elf_section
7299 (input_bfd
, shlink
, sym
.st_name
));
7303 osec
= sec
->output_section
;
7305 _bfd_elf_section_from_bfd_section (output_bfd
,
7307 if (sym
.st_shndx
== SHN_BAD
)
7310 sym
.st_value
+= sec
->output_offset
;
7311 if (! finfo
->info
->relocatable
)
7313 sym
.st_value
+= osec
->vma
;
7314 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7316 /* STT_TLS symbols are relative to PT_TLS
7318 BFD_ASSERT (elf_hash_table (finfo
->info
)
7320 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7325 finfo
->indices
[r_symndx
]
7326 = bfd_get_symcount (output_bfd
);
7328 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7333 r_symndx
= finfo
->indices
[r_symndx
];
7336 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7337 | (irela
->r_info
& r_type_mask
));
7340 /* Swap out the relocs. */
7341 if (input_rel_hdr
->sh_size
!= 0
7342 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7348 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7349 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7351 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7352 * bed
->s
->int_rels_per_ext_rel
);
7353 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7354 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7363 /* Write out the modified section contents. */
7364 if (bed
->elf_backend_write_section
7365 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7367 /* Section written out. */
7369 else switch (o
->sec_info_type
)
7371 case ELF_INFO_TYPE_STABS
:
7372 if (! (_bfd_write_section_stabs
7374 &elf_hash_table (finfo
->info
)->stab_info
,
7375 o
, &elf_section_data (o
)->sec_info
, contents
)))
7378 case ELF_INFO_TYPE_MERGE
:
7379 if (! _bfd_write_merged_section (output_bfd
, o
,
7380 elf_section_data (o
)->sec_info
))
7383 case ELF_INFO_TYPE_EH_FRAME
:
7385 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7392 if (! (o
->flags
& SEC_EXCLUDE
)
7393 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7395 (file_ptr
) o
->output_offset
,
7406 /* Generate a reloc when linking an ELF file. This is a reloc
7407 requested by the linker, and does not come from any input file. This
7408 is used to build constructor and destructor tables when linking
7412 elf_reloc_link_order (bfd
*output_bfd
,
7413 struct bfd_link_info
*info
,
7414 asection
*output_section
,
7415 struct bfd_link_order
*link_order
)
7417 reloc_howto_type
*howto
;
7421 struct elf_link_hash_entry
**rel_hash_ptr
;
7422 Elf_Internal_Shdr
*rel_hdr
;
7423 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7424 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7428 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7431 bfd_set_error (bfd_error_bad_value
);
7435 addend
= link_order
->u
.reloc
.p
->addend
;
7437 /* Figure out the symbol index. */
7438 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7439 + elf_section_data (output_section
)->rel_count
7440 + elf_section_data (output_section
)->rel_count2
);
7441 if (link_order
->type
== bfd_section_reloc_link_order
)
7443 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7444 BFD_ASSERT (indx
!= 0);
7445 *rel_hash_ptr
= NULL
;
7449 struct elf_link_hash_entry
*h
;
7451 /* Treat a reloc against a defined symbol as though it were
7452 actually against the section. */
7453 h
= ((struct elf_link_hash_entry
*)
7454 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7455 link_order
->u
.reloc
.p
->u
.name
,
7456 FALSE
, FALSE
, TRUE
));
7458 && (h
->root
.type
== bfd_link_hash_defined
7459 || h
->root
.type
== bfd_link_hash_defweak
))
7463 section
= h
->root
.u
.def
.section
;
7464 indx
= section
->output_section
->target_index
;
7465 *rel_hash_ptr
= NULL
;
7466 /* It seems that we ought to add the symbol value to the
7467 addend here, but in practice it has already been added
7468 because it was passed to constructor_callback. */
7469 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7473 /* Setting the index to -2 tells elf_link_output_extsym that
7474 this symbol is used by a reloc. */
7481 if (! ((*info
->callbacks
->unattached_reloc
)
7482 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7488 /* If this is an inplace reloc, we must write the addend into the
7490 if (howto
->partial_inplace
&& addend
!= 0)
7493 bfd_reloc_status_type rstat
;
7496 const char *sym_name
;
7498 size
= bfd_get_reloc_size (howto
);
7499 buf
= bfd_zmalloc (size
);
7502 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7509 case bfd_reloc_outofrange
:
7512 case bfd_reloc_overflow
:
7513 if (link_order
->type
== bfd_section_reloc_link_order
)
7514 sym_name
= bfd_section_name (output_bfd
,
7515 link_order
->u
.reloc
.p
->u
.section
);
7517 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7518 if (! ((*info
->callbacks
->reloc_overflow
)
7519 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7520 NULL
, (bfd_vma
) 0)))
7527 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7528 link_order
->offset
, size
);
7534 /* The address of a reloc is relative to the section in a
7535 relocatable file, and is a virtual address in an executable
7537 offset
= link_order
->offset
;
7538 if (! info
->relocatable
)
7539 offset
+= output_section
->vma
;
7541 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7543 irel
[i
].r_offset
= offset
;
7545 irel
[i
].r_addend
= 0;
7547 if (bed
->s
->arch_size
== 32)
7548 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7550 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7552 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7553 erel
= rel_hdr
->contents
;
7554 if (rel_hdr
->sh_type
== SHT_REL
)
7556 erel
+= (elf_section_data (output_section
)->rel_count
7557 * bed
->s
->sizeof_rel
);
7558 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7562 irel
[0].r_addend
= addend
;
7563 erel
+= (elf_section_data (output_section
)->rel_count
7564 * bed
->s
->sizeof_rela
);
7565 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7568 ++elf_section_data (output_section
)->rel_count
;
7574 /* Get the output vma of the section pointed to by the sh_link field. */
7577 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7579 Elf_Internal_Shdr
**elf_shdrp
;
7583 s
= p
->u
.indirect
.section
;
7584 elf_shdrp
= elf_elfsections (s
->owner
);
7585 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7586 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7588 The Intel C compiler generates SHT_IA_64_UNWIND with
7589 SHF_LINK_ORDER. But it doesn't set the sh_link or
7590 sh_info fields. Hence we could get the situation
7591 where elfsec is 0. */
7594 const struct elf_backend_data
*bed
7595 = get_elf_backend_data (s
->owner
);
7596 if (bed
->link_order_error_handler
)
7597 bed
->link_order_error_handler
7598 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7603 s
= elf_shdrp
[elfsec
]->bfd_section
;
7604 return s
->output_section
->vma
+ s
->output_offset
;
7609 /* Compare two sections based on the locations of the sections they are
7610 linked to. Used by elf_fixup_link_order. */
7613 compare_link_order (const void * a
, const void * b
)
7618 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7619 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7626 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7627 order as their linked sections. Returns false if this could not be done
7628 because an output section includes both ordered and unordered
7629 sections. Ideally we'd do this in the linker proper. */
7632 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7637 struct bfd_link_order
*p
;
7639 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7641 struct bfd_link_order
**sections
;
7642 asection
*s
, *other_sec
, *linkorder_sec
;
7646 linkorder_sec
= NULL
;
7649 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7651 if (p
->type
== bfd_indirect_link_order
)
7653 s
= p
->u
.indirect
.section
;
7655 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7656 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
7657 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
)) != -1
7658 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7672 if (seen_other
&& seen_linkorder
)
7674 if (other_sec
&& linkorder_sec
)
7675 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7677 linkorder_sec
->owner
, other_sec
,
7680 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7682 bfd_set_error (bfd_error_bad_value
);
7687 if (!seen_linkorder
)
7690 sections
= (struct bfd_link_order
**)
7691 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7694 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7696 sections
[seen_linkorder
++] = p
;
7698 /* Sort the input sections in the order of their linked section. */
7699 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7700 compare_link_order
);
7702 /* Change the offsets of the sections. */
7704 for (n
= 0; n
< seen_linkorder
; n
++)
7706 s
= sections
[n
]->u
.indirect
.section
;
7707 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7708 s
->output_offset
= offset
;
7709 sections
[n
]->offset
= offset
;
7710 offset
+= sections
[n
]->size
;
7717 /* Do the final step of an ELF link. */
7720 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7722 bfd_boolean dynamic
;
7723 bfd_boolean emit_relocs
;
7725 struct elf_final_link_info finfo
;
7726 register asection
*o
;
7727 register struct bfd_link_order
*p
;
7729 bfd_size_type max_contents_size
;
7730 bfd_size_type max_external_reloc_size
;
7731 bfd_size_type max_internal_reloc_count
;
7732 bfd_size_type max_sym_count
;
7733 bfd_size_type max_sym_shndx_count
;
7735 Elf_Internal_Sym elfsym
;
7737 Elf_Internal_Shdr
*symtab_hdr
;
7738 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7739 Elf_Internal_Shdr
*symstrtab_hdr
;
7740 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7741 struct elf_outext_info eoinfo
;
7743 size_t relativecount
= 0;
7744 asection
*reldyn
= 0;
7747 if (! is_elf_hash_table (info
->hash
))
7751 abfd
->flags
|= DYNAMIC
;
7753 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7754 dynobj
= elf_hash_table (info
)->dynobj
;
7756 emit_relocs
= (info
->relocatable
7757 || info
->emitrelocations
7758 || bed
->elf_backend_emit_relocs
);
7761 finfo
.output_bfd
= abfd
;
7762 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7763 if (finfo
.symstrtab
== NULL
)
7768 finfo
.dynsym_sec
= NULL
;
7769 finfo
.hash_sec
= NULL
;
7770 finfo
.symver_sec
= NULL
;
7774 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7775 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7776 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7777 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7778 /* Note that it is OK if symver_sec is NULL. */
7781 finfo
.contents
= NULL
;
7782 finfo
.external_relocs
= NULL
;
7783 finfo
.internal_relocs
= NULL
;
7784 finfo
.external_syms
= NULL
;
7785 finfo
.locsym_shndx
= NULL
;
7786 finfo
.internal_syms
= NULL
;
7787 finfo
.indices
= NULL
;
7788 finfo
.sections
= NULL
;
7789 finfo
.symbuf
= NULL
;
7790 finfo
.symshndxbuf
= NULL
;
7791 finfo
.symbuf_count
= 0;
7792 finfo
.shndxbuf_size
= 0;
7794 /* Count up the number of relocations we will output for each output
7795 section, so that we know the sizes of the reloc sections. We
7796 also figure out some maximum sizes. */
7797 max_contents_size
= 0;
7798 max_external_reloc_size
= 0;
7799 max_internal_reloc_count
= 0;
7801 max_sym_shndx_count
= 0;
7803 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7805 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7808 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7810 unsigned int reloc_count
= 0;
7811 struct bfd_elf_section_data
*esdi
= NULL
;
7812 unsigned int *rel_count1
;
7814 if (p
->type
== bfd_section_reloc_link_order
7815 || p
->type
== bfd_symbol_reloc_link_order
)
7817 else if (p
->type
== bfd_indirect_link_order
)
7821 sec
= p
->u
.indirect
.section
;
7822 esdi
= elf_section_data (sec
);
7824 /* Mark all sections which are to be included in the
7825 link. This will normally be every section. We need
7826 to do this so that we can identify any sections which
7827 the linker has decided to not include. */
7828 sec
->linker_mark
= TRUE
;
7830 if (sec
->flags
& SEC_MERGE
)
7833 if (info
->relocatable
|| info
->emitrelocations
)
7834 reloc_count
= sec
->reloc_count
;
7835 else if (bed
->elf_backend_count_relocs
)
7837 Elf_Internal_Rela
* relocs
;
7839 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7842 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7844 if (elf_section_data (o
)->relocs
!= relocs
)
7848 if (sec
->rawsize
> max_contents_size
)
7849 max_contents_size
= sec
->rawsize
;
7850 if (sec
->size
> max_contents_size
)
7851 max_contents_size
= sec
->size
;
7853 /* We are interested in just local symbols, not all
7855 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7856 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7860 if (elf_bad_symtab (sec
->owner
))
7861 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7862 / bed
->s
->sizeof_sym
);
7864 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7866 if (sym_count
> max_sym_count
)
7867 max_sym_count
= sym_count
;
7869 if (sym_count
> max_sym_shndx_count
7870 && elf_symtab_shndx (sec
->owner
) != 0)
7871 max_sym_shndx_count
= sym_count
;
7873 if ((sec
->flags
& SEC_RELOC
) != 0)
7877 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7878 if (ext_size
> max_external_reloc_size
)
7879 max_external_reloc_size
= ext_size
;
7880 if (sec
->reloc_count
> max_internal_reloc_count
)
7881 max_internal_reloc_count
= sec
->reloc_count
;
7886 if (reloc_count
== 0)
7889 o
->reloc_count
+= reloc_count
;
7891 /* MIPS may have a mix of REL and RELA relocs on sections.
7892 To support this curious ABI we keep reloc counts in
7893 elf_section_data too. We must be careful to add the
7894 relocations from the input section to the right output
7895 count. FIXME: Get rid of one count. We have
7896 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7897 rel_count1
= &esdo
->rel_count
;
7900 bfd_boolean same_size
;
7901 bfd_size_type entsize1
;
7903 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7904 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7905 || entsize1
== bed
->s
->sizeof_rela
);
7906 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7909 rel_count1
= &esdo
->rel_count2
;
7911 if (esdi
->rel_hdr2
!= NULL
)
7913 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7914 unsigned int alt_count
;
7915 unsigned int *rel_count2
;
7917 BFD_ASSERT (entsize2
!= entsize1
7918 && (entsize2
== bed
->s
->sizeof_rel
7919 || entsize2
== bed
->s
->sizeof_rela
));
7921 rel_count2
= &esdo
->rel_count2
;
7923 rel_count2
= &esdo
->rel_count
;
7925 /* The following is probably too simplistic if the
7926 backend counts output relocs unusually. */
7927 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7928 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7929 *rel_count2
+= alt_count
;
7930 reloc_count
-= alt_count
;
7933 *rel_count1
+= reloc_count
;
7936 if (o
->reloc_count
> 0)
7937 o
->flags
|= SEC_RELOC
;
7940 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7941 set it (this is probably a bug) and if it is set
7942 assign_section_numbers will create a reloc section. */
7943 o
->flags
&=~ SEC_RELOC
;
7946 /* If the SEC_ALLOC flag is not set, force the section VMA to
7947 zero. This is done in elf_fake_sections as well, but forcing
7948 the VMA to 0 here will ensure that relocs against these
7949 sections are handled correctly. */
7950 if ((o
->flags
& SEC_ALLOC
) == 0
7951 && ! o
->user_set_vma
)
7955 if (! info
->relocatable
&& merged
)
7956 elf_link_hash_traverse (elf_hash_table (info
),
7957 _bfd_elf_link_sec_merge_syms
, abfd
);
7959 /* Figure out the file positions for everything but the symbol table
7960 and the relocs. We set symcount to force assign_section_numbers
7961 to create a symbol table. */
7962 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7963 BFD_ASSERT (! abfd
->output_has_begun
);
7964 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7967 /* Set sizes, and assign file positions for reloc sections. */
7968 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7970 if ((o
->flags
& SEC_RELOC
) != 0)
7972 if (!(_bfd_elf_link_size_reloc_section
7973 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7976 if (elf_section_data (o
)->rel_hdr2
7977 && !(_bfd_elf_link_size_reloc_section
7978 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7982 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7983 to count upwards while actually outputting the relocations. */
7984 elf_section_data (o
)->rel_count
= 0;
7985 elf_section_data (o
)->rel_count2
= 0;
7988 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7990 /* We have now assigned file positions for all the sections except
7991 .symtab and .strtab. We start the .symtab section at the current
7992 file position, and write directly to it. We build the .strtab
7993 section in memory. */
7994 bfd_get_symcount (abfd
) = 0;
7995 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7996 /* sh_name is set in prep_headers. */
7997 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7998 /* sh_flags, sh_addr and sh_size all start off zero. */
7999 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8000 /* sh_link is set in assign_section_numbers. */
8001 /* sh_info is set below. */
8002 /* sh_offset is set just below. */
8003 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8005 off
= elf_tdata (abfd
)->next_file_pos
;
8006 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8008 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8009 incorrect. We do not yet know the size of the .symtab section.
8010 We correct next_file_pos below, after we do know the size. */
8012 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8013 continuously seeking to the right position in the file. */
8014 if (! info
->keep_memory
|| max_sym_count
< 20)
8015 finfo
.symbuf_size
= 20;
8017 finfo
.symbuf_size
= max_sym_count
;
8018 amt
= finfo
.symbuf_size
;
8019 amt
*= bed
->s
->sizeof_sym
;
8020 finfo
.symbuf
= bfd_malloc (amt
);
8021 if (finfo
.symbuf
== NULL
)
8023 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8025 /* Wild guess at number of output symbols. realloc'd as needed. */
8026 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8027 finfo
.shndxbuf_size
= amt
;
8028 amt
*= sizeof (Elf_External_Sym_Shndx
);
8029 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8030 if (finfo
.symshndxbuf
== NULL
)
8034 /* Start writing out the symbol table. The first symbol is always a
8036 if (info
->strip
!= strip_all
8039 elfsym
.st_value
= 0;
8042 elfsym
.st_other
= 0;
8043 elfsym
.st_shndx
= SHN_UNDEF
;
8044 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8049 /* Output a symbol for each section. We output these even if we are
8050 discarding local symbols, since they are used for relocs. These
8051 symbols have no names. We store the index of each one in the
8052 index field of the section, so that we can find it again when
8053 outputting relocs. */
8054 if (info
->strip
!= strip_all
8058 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8059 elfsym
.st_other
= 0;
8060 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8062 o
= bfd_section_from_elf_index (abfd
, i
);
8064 o
->target_index
= bfd_get_symcount (abfd
);
8065 elfsym
.st_shndx
= i
;
8066 if (info
->relocatable
|| o
== NULL
)
8067 elfsym
.st_value
= 0;
8069 elfsym
.st_value
= o
->vma
;
8070 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8072 if (i
== SHN_LORESERVE
- 1)
8073 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8077 /* Allocate some memory to hold information read in from the input
8079 if (max_contents_size
!= 0)
8081 finfo
.contents
= bfd_malloc (max_contents_size
);
8082 if (finfo
.contents
== NULL
)
8086 if (max_external_reloc_size
!= 0)
8088 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8089 if (finfo
.external_relocs
== NULL
)
8093 if (max_internal_reloc_count
!= 0)
8095 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8096 amt
*= sizeof (Elf_Internal_Rela
);
8097 finfo
.internal_relocs
= bfd_malloc (amt
);
8098 if (finfo
.internal_relocs
== NULL
)
8102 if (max_sym_count
!= 0)
8104 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8105 finfo
.external_syms
= bfd_malloc (amt
);
8106 if (finfo
.external_syms
== NULL
)
8109 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8110 finfo
.internal_syms
= bfd_malloc (amt
);
8111 if (finfo
.internal_syms
== NULL
)
8114 amt
= max_sym_count
* sizeof (long);
8115 finfo
.indices
= bfd_malloc (amt
);
8116 if (finfo
.indices
== NULL
)
8119 amt
= max_sym_count
* sizeof (asection
*);
8120 finfo
.sections
= bfd_malloc (amt
);
8121 if (finfo
.sections
== NULL
)
8125 if (max_sym_shndx_count
!= 0)
8127 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8128 finfo
.locsym_shndx
= bfd_malloc (amt
);
8129 if (finfo
.locsym_shndx
== NULL
)
8133 if (elf_hash_table (info
)->tls_sec
)
8135 bfd_vma base
, end
= 0;
8138 for (sec
= elf_hash_table (info
)->tls_sec
;
8139 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8142 bfd_size_type size
= sec
->size
;
8145 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8147 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8149 size
= o
->offset
+ o
->size
;
8151 end
= sec
->vma
+ size
;
8153 base
= elf_hash_table (info
)->tls_sec
->vma
;
8154 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8155 elf_hash_table (info
)->tls_size
= end
- base
;
8158 /* Reorder SHF_LINK_ORDER sections. */
8159 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8161 if (!elf_fixup_link_order (abfd
, o
))
8165 /* Since ELF permits relocations to be against local symbols, we
8166 must have the local symbols available when we do the relocations.
8167 Since we would rather only read the local symbols once, and we
8168 would rather not keep them in memory, we handle all the
8169 relocations for a single input file at the same time.
8171 Unfortunately, there is no way to know the total number of local
8172 symbols until we have seen all of them, and the local symbol
8173 indices precede the global symbol indices. This means that when
8174 we are generating relocatable output, and we see a reloc against
8175 a global symbol, we can not know the symbol index until we have
8176 finished examining all the local symbols to see which ones we are
8177 going to output. To deal with this, we keep the relocations in
8178 memory, and don't output them until the end of the link. This is
8179 an unfortunate waste of memory, but I don't see a good way around
8180 it. Fortunately, it only happens when performing a relocatable
8181 link, which is not the common case. FIXME: If keep_memory is set
8182 we could write the relocs out and then read them again; I don't
8183 know how bad the memory loss will be. */
8185 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8186 sub
->output_has_begun
= FALSE
;
8187 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8189 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8191 if (p
->type
== bfd_indirect_link_order
8192 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8193 == bfd_target_elf_flavour
)
8194 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8196 if (! sub
->output_has_begun
)
8198 if (! elf_link_input_bfd (&finfo
, sub
))
8200 sub
->output_has_begun
= TRUE
;
8203 else if (p
->type
== bfd_section_reloc_link_order
8204 || p
->type
== bfd_symbol_reloc_link_order
)
8206 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8211 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8217 /* Output any global symbols that got converted to local in a
8218 version script or due to symbol visibility. We do this in a
8219 separate step since ELF requires all local symbols to appear
8220 prior to any global symbols. FIXME: We should only do this if
8221 some global symbols were, in fact, converted to become local.
8222 FIXME: Will this work correctly with the Irix 5 linker? */
8223 eoinfo
.failed
= FALSE
;
8224 eoinfo
.finfo
= &finfo
;
8225 eoinfo
.localsyms
= TRUE
;
8226 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8231 /* That wrote out all the local symbols. Finish up the symbol table
8232 with the global symbols. Even if we want to strip everything we
8233 can, we still need to deal with those global symbols that got
8234 converted to local in a version script. */
8236 /* The sh_info field records the index of the first non local symbol. */
8237 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8240 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8242 Elf_Internal_Sym sym
;
8243 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8244 long last_local
= 0;
8246 /* Write out the section symbols for the output sections. */
8247 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8253 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8256 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8262 dynindx
= elf_section_data (s
)->dynindx
;
8265 indx
= elf_section_data (s
)->this_idx
;
8266 BFD_ASSERT (indx
> 0);
8267 sym
.st_shndx
= indx
;
8268 sym
.st_value
= s
->vma
;
8269 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8270 if (last_local
< dynindx
)
8271 last_local
= dynindx
;
8272 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8276 /* Write out the local dynsyms. */
8277 if (elf_hash_table (info
)->dynlocal
)
8279 struct elf_link_local_dynamic_entry
*e
;
8280 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8285 sym
.st_size
= e
->isym
.st_size
;
8286 sym
.st_other
= e
->isym
.st_other
;
8288 /* Copy the internal symbol as is.
8289 Note that we saved a word of storage and overwrote
8290 the original st_name with the dynstr_index. */
8293 if (e
->isym
.st_shndx
!= SHN_UNDEF
8294 && (e
->isym
.st_shndx
< SHN_LORESERVE
8295 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8297 s
= bfd_section_from_elf_index (e
->input_bfd
,
8301 elf_section_data (s
->output_section
)->this_idx
;
8302 sym
.st_value
= (s
->output_section
->vma
8304 + e
->isym
.st_value
);
8307 if (last_local
< e
->dynindx
)
8308 last_local
= e
->dynindx
;
8310 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8311 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8315 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8319 /* We get the global symbols from the hash table. */
8320 eoinfo
.failed
= FALSE
;
8321 eoinfo
.localsyms
= FALSE
;
8322 eoinfo
.finfo
= &finfo
;
8323 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8328 /* If backend needs to output some symbols not present in the hash
8329 table, do it now. */
8330 if (bed
->elf_backend_output_arch_syms
)
8332 typedef bfd_boolean (*out_sym_func
)
8333 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8334 struct elf_link_hash_entry
*);
8336 if (! ((*bed
->elf_backend_output_arch_syms
)
8337 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8341 /* Flush all symbols to the file. */
8342 if (! elf_link_flush_output_syms (&finfo
, bed
))
8345 /* Now we know the size of the symtab section. */
8346 off
+= symtab_hdr
->sh_size
;
8348 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8349 if (symtab_shndx_hdr
->sh_name
!= 0)
8351 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8352 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8353 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8354 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8355 symtab_shndx_hdr
->sh_size
= amt
;
8357 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8360 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8361 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8366 /* Finish up and write out the symbol string table (.strtab)
8368 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8369 /* sh_name was set in prep_headers. */
8370 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8371 symstrtab_hdr
->sh_flags
= 0;
8372 symstrtab_hdr
->sh_addr
= 0;
8373 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8374 symstrtab_hdr
->sh_entsize
= 0;
8375 symstrtab_hdr
->sh_link
= 0;
8376 symstrtab_hdr
->sh_info
= 0;
8377 /* sh_offset is set just below. */
8378 symstrtab_hdr
->sh_addralign
= 1;
8380 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8381 elf_tdata (abfd
)->next_file_pos
= off
;
8383 if (bfd_get_symcount (abfd
) > 0)
8385 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8386 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8390 /* Adjust the relocs to have the correct symbol indices. */
8391 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8393 if ((o
->flags
& SEC_RELOC
) == 0)
8396 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8397 elf_section_data (o
)->rel_count
,
8398 elf_section_data (o
)->rel_hashes
);
8399 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8400 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8401 elf_section_data (o
)->rel_count2
,
8402 (elf_section_data (o
)->rel_hashes
8403 + elf_section_data (o
)->rel_count
));
8405 /* Set the reloc_count field to 0 to prevent write_relocs from
8406 trying to swap the relocs out itself. */
8410 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8411 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8413 /* If we are linking against a dynamic object, or generating a
8414 shared library, finish up the dynamic linking information. */
8417 bfd_byte
*dyncon
, *dynconend
;
8419 /* Fix up .dynamic entries. */
8420 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8421 BFD_ASSERT (o
!= NULL
);
8423 dyncon
= o
->contents
;
8424 dynconend
= o
->contents
+ o
->size
;
8425 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8427 Elf_Internal_Dyn dyn
;
8431 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8438 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8440 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8442 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8443 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8446 dyn
.d_un
.d_val
= relativecount
;
8453 name
= info
->init_function
;
8456 name
= info
->fini_function
;
8459 struct elf_link_hash_entry
*h
;
8461 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8462 FALSE
, FALSE
, TRUE
);
8464 && (h
->root
.type
== bfd_link_hash_defined
8465 || h
->root
.type
== bfd_link_hash_defweak
))
8467 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8468 o
= h
->root
.u
.def
.section
;
8469 if (o
->output_section
!= NULL
)
8470 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8471 + o
->output_offset
);
8474 /* The symbol is imported from another shared
8475 library and does not apply to this one. */
8483 case DT_PREINIT_ARRAYSZ
:
8484 name
= ".preinit_array";
8486 case DT_INIT_ARRAYSZ
:
8487 name
= ".init_array";
8489 case DT_FINI_ARRAYSZ
:
8490 name
= ".fini_array";
8492 o
= bfd_get_section_by_name (abfd
, name
);
8495 (*_bfd_error_handler
)
8496 (_("%B: could not find output section %s"), abfd
, name
);
8500 (*_bfd_error_handler
)
8501 (_("warning: %s section has zero size"), name
);
8502 dyn
.d_un
.d_val
= o
->size
;
8505 case DT_PREINIT_ARRAY
:
8506 name
= ".preinit_array";
8509 name
= ".init_array";
8512 name
= ".fini_array";
8525 name
= ".gnu.version_d";
8528 name
= ".gnu.version_r";
8531 name
= ".gnu.version";
8533 o
= bfd_get_section_by_name (abfd
, name
);
8536 (*_bfd_error_handler
)
8537 (_("%B: could not find output section %s"), abfd
, name
);
8540 dyn
.d_un
.d_ptr
= o
->vma
;
8547 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8552 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8554 Elf_Internal_Shdr
*hdr
;
8556 hdr
= elf_elfsections (abfd
)[i
];
8557 if (hdr
->sh_type
== type
8558 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8560 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8561 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8564 if (dyn
.d_un
.d_val
== 0
8565 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8566 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8572 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8576 /* If we have created any dynamic sections, then output them. */
8579 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8582 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8584 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8586 || o
->output_section
== bfd_abs_section_ptr
)
8588 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8590 /* At this point, we are only interested in sections
8591 created by _bfd_elf_link_create_dynamic_sections. */
8594 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8596 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8598 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8600 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8602 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8604 (file_ptr
) o
->output_offset
,
8610 /* The contents of the .dynstr section are actually in a
8612 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8613 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8614 || ! _bfd_elf_strtab_emit (abfd
,
8615 elf_hash_table (info
)->dynstr
))
8621 if (info
->relocatable
)
8623 bfd_boolean failed
= FALSE
;
8625 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8630 /* If we have optimized stabs strings, output them. */
8631 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8633 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8637 if (info
->eh_frame_hdr
)
8639 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8643 if (finfo
.symstrtab
!= NULL
)
8644 _bfd_stringtab_free (finfo
.symstrtab
);
8645 if (finfo
.contents
!= NULL
)
8646 free (finfo
.contents
);
8647 if (finfo
.external_relocs
!= NULL
)
8648 free (finfo
.external_relocs
);
8649 if (finfo
.internal_relocs
!= NULL
)
8650 free (finfo
.internal_relocs
);
8651 if (finfo
.external_syms
!= NULL
)
8652 free (finfo
.external_syms
);
8653 if (finfo
.locsym_shndx
!= NULL
)
8654 free (finfo
.locsym_shndx
);
8655 if (finfo
.internal_syms
!= NULL
)
8656 free (finfo
.internal_syms
);
8657 if (finfo
.indices
!= NULL
)
8658 free (finfo
.indices
);
8659 if (finfo
.sections
!= NULL
)
8660 free (finfo
.sections
);
8661 if (finfo
.symbuf
!= NULL
)
8662 free (finfo
.symbuf
);
8663 if (finfo
.symshndxbuf
!= NULL
)
8664 free (finfo
.symshndxbuf
);
8665 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8667 if ((o
->flags
& SEC_RELOC
) != 0
8668 && elf_section_data (o
)->rel_hashes
!= NULL
)
8669 free (elf_section_data (o
)->rel_hashes
);
8672 elf_tdata (abfd
)->linker
= TRUE
;
8677 if (finfo
.symstrtab
!= NULL
)
8678 _bfd_stringtab_free (finfo
.symstrtab
);
8679 if (finfo
.contents
!= NULL
)
8680 free (finfo
.contents
);
8681 if (finfo
.external_relocs
!= NULL
)
8682 free (finfo
.external_relocs
);
8683 if (finfo
.internal_relocs
!= NULL
)
8684 free (finfo
.internal_relocs
);
8685 if (finfo
.external_syms
!= NULL
)
8686 free (finfo
.external_syms
);
8687 if (finfo
.locsym_shndx
!= NULL
)
8688 free (finfo
.locsym_shndx
);
8689 if (finfo
.internal_syms
!= NULL
)
8690 free (finfo
.internal_syms
);
8691 if (finfo
.indices
!= NULL
)
8692 free (finfo
.indices
);
8693 if (finfo
.sections
!= NULL
)
8694 free (finfo
.sections
);
8695 if (finfo
.symbuf
!= NULL
)
8696 free (finfo
.symbuf
);
8697 if (finfo
.symshndxbuf
!= NULL
)
8698 free (finfo
.symshndxbuf
);
8699 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8701 if ((o
->flags
& SEC_RELOC
) != 0
8702 && elf_section_data (o
)->rel_hashes
!= NULL
)
8703 free (elf_section_data (o
)->rel_hashes
);
8709 /* Garbage collect unused sections. */
8711 /* The mark phase of garbage collection. For a given section, mark
8712 it and any sections in this section's group, and all the sections
8713 which define symbols to which it refers. */
8715 typedef asection
* (*gc_mark_hook_fn
)
8716 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8717 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8720 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8722 gc_mark_hook_fn gc_mark_hook
)
8726 asection
*group_sec
;
8730 /* Mark all the sections in the group. */
8731 group_sec
= elf_section_data (sec
)->next_in_group
;
8732 if (group_sec
&& !group_sec
->gc_mark
)
8733 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8736 /* Look through the section relocs. */
8738 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8739 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8741 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8742 Elf_Internal_Shdr
*symtab_hdr
;
8743 struct elf_link_hash_entry
**sym_hashes
;
8746 bfd
*input_bfd
= sec
->owner
;
8747 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8748 Elf_Internal_Sym
*isym
= NULL
;
8751 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8752 sym_hashes
= elf_sym_hashes (input_bfd
);
8754 /* Read the local symbols. */
8755 if (elf_bad_symtab (input_bfd
))
8757 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8761 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8763 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8764 if (isym
== NULL
&& nlocsyms
!= 0)
8766 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8772 /* Read the relocations. */
8773 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8775 if (relstart
== NULL
)
8780 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8782 if (bed
->s
->arch_size
== 32)
8787 for (rel
= relstart
; rel
< relend
; rel
++)
8789 unsigned long r_symndx
;
8791 struct elf_link_hash_entry
*h
;
8793 r_symndx
= rel
->r_info
>> r_sym_shift
;
8797 if (r_symndx
>= nlocsyms
8798 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8800 h
= sym_hashes
[r_symndx
- extsymoff
];
8801 while (h
->root
.type
== bfd_link_hash_indirect
8802 || h
->root
.type
== bfd_link_hash_warning
)
8803 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8804 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8808 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8811 if (rsec
&& !rsec
->gc_mark
)
8813 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8816 rsec
->gc_mark_from_eh
= 1;
8817 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8826 if (elf_section_data (sec
)->relocs
!= relstart
)
8829 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8831 if (! info
->keep_memory
)
8834 symtab_hdr
->contents
= (unsigned char *) isym
;
8841 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8843 struct elf_gc_sweep_symbol_info
{
8844 struct bfd_link_info
*info
;
8845 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8850 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8852 if (h
->root
.type
== bfd_link_hash_warning
)
8853 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8855 if ((h
->root
.type
== bfd_link_hash_defined
8856 || h
->root
.type
== bfd_link_hash_defweak
)
8857 && !h
->root
.u
.def
.section
->gc_mark
8858 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8860 struct elf_gc_sweep_symbol_info
*inf
= data
;
8861 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8867 /* The sweep phase of garbage collection. Remove all garbage sections. */
8869 typedef bfd_boolean (*gc_sweep_hook_fn
)
8870 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8873 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
8876 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8877 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
8878 unsigned long section_sym_count
;
8879 struct elf_gc_sweep_symbol_info sweep_info
;
8881 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8885 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8888 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8890 /* Keep debug and special sections. */
8891 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8892 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8898 /* Skip sweeping sections already excluded. */
8899 if (o
->flags
& SEC_EXCLUDE
)
8902 /* Since this is early in the link process, it is simple
8903 to remove a section from the output. */
8904 o
->flags
|= SEC_EXCLUDE
;
8906 /* But we also have to update some of the relocation
8907 info we collected before. */
8909 && (o
->flags
& SEC_RELOC
) != 0
8910 && o
->reloc_count
> 0
8911 && !bfd_is_abs_section (o
->output_section
))
8913 Elf_Internal_Rela
*internal_relocs
;
8917 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8919 if (internal_relocs
== NULL
)
8922 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8924 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8925 free (internal_relocs
);
8933 /* Remove the symbols that were in the swept sections from the dynamic
8934 symbol table. GCFIXME: Anyone know how to get them out of the
8935 static symbol table as well? */
8936 sweep_info
.info
= info
;
8937 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
8938 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
8941 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
8945 /* Propagate collected vtable information. This is called through
8946 elf_link_hash_traverse. */
8949 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8951 if (h
->root
.type
== bfd_link_hash_warning
)
8952 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8954 /* Those that are not vtables. */
8955 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8958 /* Those vtables that do not have parents, we cannot merge. */
8959 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8962 /* If we've already been done, exit. */
8963 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8966 /* Make sure the parent's table is up to date. */
8967 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8969 if (h
->vtable
->used
== NULL
)
8971 /* None of this table's entries were referenced. Re-use the
8973 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8974 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8979 bfd_boolean
*cu
, *pu
;
8981 /* Or the parent's entries into ours. */
8982 cu
= h
->vtable
->used
;
8984 pu
= h
->vtable
->parent
->vtable
->used
;
8987 const struct elf_backend_data
*bed
;
8988 unsigned int log_file_align
;
8990 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8991 log_file_align
= bed
->s
->log_file_align
;
8992 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9007 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9010 bfd_vma hstart
, hend
;
9011 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9012 const struct elf_backend_data
*bed
;
9013 unsigned int log_file_align
;
9015 if (h
->root
.type
== bfd_link_hash_warning
)
9016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9018 /* Take care of both those symbols that do not describe vtables as
9019 well as those that are not loaded. */
9020 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9023 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9024 || h
->root
.type
== bfd_link_hash_defweak
);
9026 sec
= h
->root
.u
.def
.section
;
9027 hstart
= h
->root
.u
.def
.value
;
9028 hend
= hstart
+ h
->size
;
9030 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9032 return *(bfd_boolean
*) okp
= FALSE
;
9033 bed
= get_elf_backend_data (sec
->owner
);
9034 log_file_align
= bed
->s
->log_file_align
;
9036 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9038 for (rel
= relstart
; rel
< relend
; ++rel
)
9039 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9041 /* If the entry is in use, do nothing. */
9043 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9045 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9046 if (h
->vtable
->used
[entry
])
9049 /* Otherwise, kill it. */
9050 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9056 /* Mark sections containing dynamically referenced symbols. When
9057 building shared libraries, we must assume that any visible symbol is
9061 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9063 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9065 if (h
->root
.type
== bfd_link_hash_warning
)
9066 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9068 if ((h
->root
.type
== bfd_link_hash_defined
9069 || h
->root
.type
== bfd_link_hash_defweak
)
9071 || (!info
->executable
9073 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9074 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9075 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9080 /* Do mark and sweep of unused sections. */
9083 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9085 bfd_boolean ok
= TRUE
;
9087 asection
* (*gc_mark_hook
)
9088 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9089 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9091 if (!get_elf_backend_data (abfd
)->can_gc_sections
9092 || info
->relocatable
9093 || info
->emitrelocations
9094 || !is_elf_hash_table (info
->hash
))
9096 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9100 /* Apply transitive closure to the vtable entry usage info. */
9101 elf_link_hash_traverse (elf_hash_table (info
),
9102 elf_gc_propagate_vtable_entries_used
,
9107 /* Kill the vtable relocations that were not used. */
9108 elf_link_hash_traverse (elf_hash_table (info
),
9109 elf_gc_smash_unused_vtentry_relocs
,
9114 /* Mark dynamically referenced symbols. */
9115 if (elf_hash_table (info
)->dynamic_sections_created
)
9116 elf_link_hash_traverse (elf_hash_table (info
),
9117 elf_gc_mark_dynamic_ref_symbol
,
9120 /* Grovel through relocs to find out who stays ... */
9121 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9122 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9126 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9129 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9130 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9131 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9135 /* ... again for sections marked from eh_frame. */
9136 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9140 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9143 /* Keep .gcc_except_table.* if the associated .text.* is
9144 marked. This isn't very nice, but the proper solution,
9145 splitting .eh_frame up and using comdat doesn't pan out
9146 easily due to needing special relocs to handle the
9147 difference of two symbols in separate sections.
9148 Don't keep code sections referenced by .eh_frame. */
9149 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9150 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9152 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9158 len
= strlen (o
->name
+ 18) + 1;
9159 fn_name
= bfd_malloc (len
+ 6);
9160 if (fn_name
== NULL
)
9162 memcpy (fn_name
, ".text.", 6);
9163 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9164 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9166 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9170 /* If not using specially named exception table section,
9171 then keep whatever we are using. */
9172 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9177 /* ... and mark SEC_EXCLUDE for those that go. */
9178 return elf_gc_sweep (abfd
, info
);
9181 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9184 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9186 struct elf_link_hash_entry
*h
,
9189 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9190 struct elf_link_hash_entry
**search
, *child
;
9191 bfd_size_type extsymcount
;
9192 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9194 /* The sh_info field of the symtab header tells us where the
9195 external symbols start. We don't care about the local symbols at
9197 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9198 if (!elf_bad_symtab (abfd
))
9199 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9201 sym_hashes
= elf_sym_hashes (abfd
);
9202 sym_hashes_end
= sym_hashes
+ extsymcount
;
9204 /* Hunt down the child symbol, which is in this section at the same
9205 offset as the relocation. */
9206 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9208 if ((child
= *search
) != NULL
9209 && (child
->root
.type
== bfd_link_hash_defined
9210 || child
->root
.type
== bfd_link_hash_defweak
)
9211 && child
->root
.u
.def
.section
== sec
9212 && child
->root
.u
.def
.value
== offset
)
9216 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9217 abfd
, sec
, (unsigned long) offset
);
9218 bfd_set_error (bfd_error_invalid_operation
);
9224 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9230 /* This *should* only be the absolute section. It could potentially
9231 be that someone has defined a non-global vtable though, which
9232 would be bad. It isn't worth paging in the local symbols to be
9233 sure though; that case should simply be handled by the assembler. */
9235 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9238 child
->vtable
->parent
= h
;
9243 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9246 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9247 asection
*sec ATTRIBUTE_UNUSED
,
9248 struct elf_link_hash_entry
*h
,
9251 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9252 unsigned int log_file_align
= bed
->s
->log_file_align
;
9256 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9261 if (addend
>= h
->vtable
->size
)
9263 size_t size
, bytes
, file_align
;
9264 bfd_boolean
*ptr
= h
->vtable
->used
;
9266 /* While the symbol is undefined, we have to be prepared to handle
9268 file_align
= 1 << log_file_align
;
9269 if (h
->root
.type
== bfd_link_hash_undefined
)
9270 size
= addend
+ file_align
;
9276 /* Oops! We've got a reference past the defined end of
9277 the table. This is probably a bug -- shall we warn? */
9278 size
= addend
+ file_align
;
9281 size
= (size
+ file_align
- 1) & -file_align
;
9283 /* Allocate one extra entry for use as a "done" flag for the
9284 consolidation pass. */
9285 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9289 ptr
= bfd_realloc (ptr
- 1, bytes
);
9295 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9296 * sizeof (bfd_boolean
));
9297 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9301 ptr
= bfd_zmalloc (bytes
);
9306 /* And arrange for that done flag to be at index -1. */
9307 h
->vtable
->used
= ptr
+ 1;
9308 h
->vtable
->size
= size
;
9311 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9316 struct alloc_got_off_arg
{
9318 unsigned int got_elt_size
;
9321 /* We need a special top-level link routine to convert got reference counts
9322 to real got offsets. */
9325 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9327 struct alloc_got_off_arg
*gofarg
= arg
;
9329 if (h
->root
.type
== bfd_link_hash_warning
)
9330 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9332 if (h
->got
.refcount
> 0)
9334 h
->got
.offset
= gofarg
->gotoff
;
9335 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9338 h
->got
.offset
= (bfd_vma
) -1;
9343 /* And an accompanying bit to work out final got entry offsets once
9344 we're done. Should be called from final_link. */
9347 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9348 struct bfd_link_info
*info
)
9351 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9353 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9354 struct alloc_got_off_arg gofarg
;
9356 if (! is_elf_hash_table (info
->hash
))
9359 /* The GOT offset is relative to the .got section, but the GOT header is
9360 put into the .got.plt section, if the backend uses it. */
9361 if (bed
->want_got_plt
)
9364 gotoff
= bed
->got_header_size
;
9366 /* Do the local .got entries first. */
9367 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9369 bfd_signed_vma
*local_got
;
9370 bfd_size_type j
, locsymcount
;
9371 Elf_Internal_Shdr
*symtab_hdr
;
9373 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9376 local_got
= elf_local_got_refcounts (i
);
9380 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9381 if (elf_bad_symtab (i
))
9382 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9384 locsymcount
= symtab_hdr
->sh_info
;
9386 for (j
= 0; j
< locsymcount
; ++j
)
9388 if (local_got
[j
] > 0)
9390 local_got
[j
] = gotoff
;
9391 gotoff
+= got_elt_size
;
9394 local_got
[j
] = (bfd_vma
) -1;
9398 /* Then the global .got entries. .plt refcounts are handled by
9399 adjust_dynamic_symbol */
9400 gofarg
.gotoff
= gotoff
;
9401 gofarg
.got_elt_size
= got_elt_size
;
9402 elf_link_hash_traverse (elf_hash_table (info
),
9403 elf_gc_allocate_got_offsets
,
9408 /* Many folk need no more in the way of final link than this, once
9409 got entry reference counting is enabled. */
9412 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9414 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9417 /* Invoke the regular ELF backend linker to do all the work. */
9418 return bfd_elf_final_link (abfd
, info
);
9422 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9424 struct elf_reloc_cookie
*rcookie
= cookie
;
9426 if (rcookie
->bad_symtab
)
9427 rcookie
->rel
= rcookie
->rels
;
9429 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9431 unsigned long r_symndx
;
9433 if (! rcookie
->bad_symtab
)
9434 if (rcookie
->rel
->r_offset
> offset
)
9436 if (rcookie
->rel
->r_offset
!= offset
)
9439 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9440 if (r_symndx
== SHN_UNDEF
)
9443 if (r_symndx
>= rcookie
->locsymcount
9444 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9446 struct elf_link_hash_entry
*h
;
9448 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9450 while (h
->root
.type
== bfd_link_hash_indirect
9451 || h
->root
.type
== bfd_link_hash_warning
)
9452 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9454 if ((h
->root
.type
== bfd_link_hash_defined
9455 || h
->root
.type
== bfd_link_hash_defweak
)
9456 && elf_discarded_section (h
->root
.u
.def
.section
))
9463 /* It's not a relocation against a global symbol,
9464 but it could be a relocation against a local
9465 symbol for a discarded section. */
9467 Elf_Internal_Sym
*isym
;
9469 /* Need to: get the symbol; get the section. */
9470 isym
= &rcookie
->locsyms
[r_symndx
];
9471 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9473 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9474 if (isec
!= NULL
&& elf_discarded_section (isec
))
9483 /* Discard unneeded references to discarded sections.
9484 Returns TRUE if any section's size was changed. */
9485 /* This function assumes that the relocations are in sorted order,
9486 which is true for all known assemblers. */
9489 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9491 struct elf_reloc_cookie cookie
;
9492 asection
*stab
, *eh
;
9493 Elf_Internal_Shdr
*symtab_hdr
;
9494 const struct elf_backend_data
*bed
;
9497 bfd_boolean ret
= FALSE
;
9499 if (info
->traditional_format
9500 || !is_elf_hash_table (info
->hash
))
9503 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9505 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9508 bed
= get_elf_backend_data (abfd
);
9510 if ((abfd
->flags
& DYNAMIC
) != 0)
9513 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9514 if (info
->relocatable
9517 || bfd_is_abs_section (eh
->output_section
))))
9520 stab
= bfd_get_section_by_name (abfd
, ".stab");
9523 || bfd_is_abs_section (stab
->output_section
)
9524 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9529 && bed
->elf_backend_discard_info
== NULL
)
9532 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9534 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9535 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9536 if (cookie
.bad_symtab
)
9538 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9539 cookie
.extsymoff
= 0;
9543 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9544 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9547 if (bed
->s
->arch_size
== 32)
9548 cookie
.r_sym_shift
= 8;
9550 cookie
.r_sym_shift
= 32;
9552 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9553 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9555 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9556 cookie
.locsymcount
, 0,
9558 if (cookie
.locsyms
== NULL
)
9565 count
= stab
->reloc_count
;
9567 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9569 if (cookie
.rels
!= NULL
)
9571 cookie
.rel
= cookie
.rels
;
9572 cookie
.relend
= cookie
.rels
;
9573 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9574 if (_bfd_discard_section_stabs (abfd
, stab
,
9575 elf_section_data (stab
)->sec_info
,
9576 bfd_elf_reloc_symbol_deleted_p
,
9579 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9587 count
= eh
->reloc_count
;
9589 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9591 cookie
.rel
= cookie
.rels
;
9592 cookie
.relend
= cookie
.rels
;
9593 if (cookie
.rels
!= NULL
)
9594 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9596 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9597 bfd_elf_reloc_symbol_deleted_p
,
9601 if (cookie
.rels
!= NULL
9602 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9606 if (bed
->elf_backend_discard_info
!= NULL
9607 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9610 if (cookie
.locsyms
!= NULL
9611 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9613 if (! info
->keep_memory
)
9614 free (cookie
.locsyms
);
9616 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9620 if (info
->eh_frame_hdr
9621 && !info
->relocatable
9622 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9629 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9632 const char *name
, *p
;
9633 struct bfd_section_already_linked
*l
;
9634 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9637 /* A single member comdat group section may be discarded by a
9638 linkonce section. See below. */
9639 if (sec
->output_section
== bfd_abs_section_ptr
)
9644 /* Check if it belongs to a section group. */
9645 group
= elf_sec_group (sec
);
9647 /* Return if it isn't a linkonce section nor a member of a group. A
9648 comdat group section also has SEC_LINK_ONCE set. */
9649 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9654 /* If this is the member of a single member comdat group, check if
9655 the group should be discarded. */
9656 if (elf_next_in_group (sec
) == sec
9657 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9663 /* FIXME: When doing a relocatable link, we may have trouble
9664 copying relocations in other sections that refer to local symbols
9665 in the section being discarded. Those relocations will have to
9666 be converted somehow; as of this writing I'm not sure that any of
9667 the backends handle that correctly.
9669 It is tempting to instead not discard link once sections when
9670 doing a relocatable link (technically, they should be discarded
9671 whenever we are building constructors). However, that fails,
9672 because the linker winds up combining all the link once sections
9673 into a single large link once section, which defeats the purpose
9674 of having link once sections in the first place.
9676 Also, not merging link once sections in a relocatable link
9677 causes trouble for MIPS ELF, which relies on link once semantics
9678 to handle the .reginfo section correctly. */
9680 name
= bfd_get_section_name (abfd
, sec
);
9682 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9683 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9688 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9690 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9692 /* We may have 3 different sections on the list: group section,
9693 comdat section and linkonce section. SEC may be a linkonce or
9694 group section. We match a group section with a group section,
9695 a linkonce section with a linkonce section, and ignore comdat
9697 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9698 && strcmp (name
, l
->sec
->name
) == 0
9699 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9701 /* The section has already been linked. See if we should
9703 switch (flags
& SEC_LINK_DUPLICATES
)
9708 case SEC_LINK_DUPLICATES_DISCARD
:
9711 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9712 (*_bfd_error_handler
)
9713 (_("%B: ignoring duplicate section `%A'"),
9717 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9718 if (sec
->size
!= l
->sec
->size
)
9719 (*_bfd_error_handler
)
9720 (_("%B: duplicate section `%A' has different size"),
9724 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9725 if (sec
->size
!= l
->sec
->size
)
9726 (*_bfd_error_handler
)
9727 (_("%B: duplicate section `%A' has different size"),
9729 else if (sec
->size
!= 0)
9731 bfd_byte
*sec_contents
, *l_sec_contents
;
9733 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9734 (*_bfd_error_handler
)
9735 (_("%B: warning: could not read contents of section `%A'"),
9737 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9739 (*_bfd_error_handler
)
9740 (_("%B: warning: could not read contents of section `%A'"),
9741 l
->sec
->owner
, l
->sec
);
9742 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9743 (*_bfd_error_handler
)
9744 (_("%B: warning: duplicate section `%A' has different contents"),
9748 free (sec_contents
);
9750 free (l_sec_contents
);
9755 /* Set the output_section field so that lang_add_section
9756 does not create a lang_input_section structure for this
9757 section. Since there might be a symbol in the section
9758 being discarded, we must retain a pointer to the section
9759 which we are really going to use. */
9760 sec
->output_section
= bfd_abs_section_ptr
;
9761 sec
->kept_section
= l
->sec
;
9763 if (flags
& SEC_GROUP
)
9765 asection
*first
= elf_next_in_group (sec
);
9766 asection
*s
= first
;
9770 s
->output_section
= bfd_abs_section_ptr
;
9771 /* Record which group discards it. */
9772 s
->kept_section
= l
->sec
;
9773 s
= elf_next_in_group (s
);
9774 /* These lists are circular. */
9786 /* If this is the member of a single member comdat group and the
9787 group hasn't be discarded, we check if it matches a linkonce
9788 section. We only record the discarded comdat group. Otherwise
9789 the undiscarded group will be discarded incorrectly later since
9790 itself has been recorded. */
9791 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9792 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9793 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9794 && bfd_elf_match_symbols_in_sections (l
->sec
,
9795 elf_next_in_group (sec
)))
9797 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9798 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9799 group
->output_section
= bfd_abs_section_ptr
;
9806 /* There is no direct match. But for linkonce section, we should
9807 check if there is a match with comdat group member. We always
9808 record the linkonce section, discarded or not. */
9809 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9810 if (l
->sec
->flags
& SEC_GROUP
)
9812 asection
*first
= elf_next_in_group (l
->sec
);
9815 && elf_next_in_group (first
) == first
9816 && bfd_elf_match_symbols_in_sections (first
, sec
))
9818 sec
->output_section
= bfd_abs_section_ptr
;
9819 sec
->kept_section
= l
->sec
;
9824 /* This is the first section with this name. Record it. */
9825 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9829 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9831 return sym
->st_shndx
== SHN_COMMON
;
9835 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9841 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9843 return bfd_com_section_ptr
;