1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 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"
31 /* Define a symbol in a dynamic linkage section. */
33 struct elf_link_hash_entry
*
34 _bfd_elf_define_linkage_sym (bfd
*abfd
,
35 struct bfd_link_info
*info
,
39 struct elf_link_hash_entry
*h
;
40 struct bfd_link_hash_entry
*bh
;
41 const struct elf_backend_data
*bed
;
43 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h
->root
.type
= bfd_link_hash_new
;
54 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
56 get_elf_backend_data (abfd
)->collect
,
59 h
= (struct elf_link_hash_entry
*) bh
;
62 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
64 bed
= get_elf_backend_data (abfd
);
65 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
70 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
74 struct elf_link_hash_entry
*h
;
75 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
78 /* This function may be called more than once. */
79 s
= bfd_get_section_by_name (abfd
, ".got");
80 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
83 switch (bed
->s
->arch_size
)
94 bfd_set_error (bfd_error_bad_value
);
98 flags
= bed
->dynamic_sec_flags
;
100 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
102 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
105 if (bed
->want_got_plt
)
107 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
109 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
113 if (bed
->want_got_sym
)
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info
)->hgot
= h
;
125 /* The first bit of the global offset table is the header. */
126 s
->size
+= bed
->got_header_size
;
131 /* Create a strtab to hold the dynamic symbol names. */
133 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
135 struct elf_link_hash_table
*hash_table
;
137 hash_table
= elf_hash_table (info
);
138 if (hash_table
->dynobj
== NULL
)
139 hash_table
->dynobj
= abfd
;
141 if (hash_table
->dynstr
== NULL
)
143 hash_table
->dynstr
= _bfd_elf_strtab_init ();
144 if (hash_table
->dynstr
== NULL
)
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
158 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
161 register asection
*s
;
162 const struct elf_backend_data
*bed
;
164 if (! is_elf_hash_table (info
->hash
))
167 if (elf_hash_table (info
)->dynamic_sections_created
)
170 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
173 abfd
= elf_hash_table (info
)->dynobj
;
174 bed
= get_elf_backend_data (abfd
);
176 flags
= bed
->dynamic_sec_flags
;
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info
->executable
)
182 s
= bfd_make_section_with_flags (abfd
, ".interp",
183 flags
| SEC_READONLY
);
188 if (! info
->traditional_format
)
190 s
= bfd_make_section_with_flags (abfd
, ".eh_frame_hdr",
191 flags
| SEC_READONLY
);
193 || ! bfd_set_section_alignment (abfd
, s
, 2))
195 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
198 /* Create sections to hold version informations. These are removed
199 if they are not needed. */
200 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
201 flags
| SEC_READONLY
);
203 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
206 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
207 flags
| SEC_READONLY
);
209 || ! bfd_set_section_alignment (abfd
, s
, 1))
212 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
213 flags
| SEC_READONLY
);
215 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
218 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
219 flags
| SEC_READONLY
);
221 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
224 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
225 flags
| SEC_READONLY
);
229 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
231 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
234 /* The special symbol _DYNAMIC is always set to the start of the
235 .dynamic section. We could set _DYNAMIC in a linker script, but we
236 only want to define it if we are, in fact, creating a .dynamic
237 section. We don't want to define it if there is no .dynamic
238 section, since on some ELF platforms the start up code examines it
239 to decide how to initialize the process. */
240 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
243 s
= bfd_make_section_with_flags (abfd
, ".hash",
244 flags
| SEC_READONLY
);
246 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
248 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
250 /* Let the backend create the rest of the sections. This lets the
251 backend set the right flags. The backend will normally create
252 the .got and .plt sections. */
253 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
256 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
261 /* Create dynamic sections when linking against a dynamic object. */
264 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
266 flagword flags
, pltflags
;
267 struct elf_link_hash_entry
*h
;
269 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
271 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
272 .rel[a].bss sections. */
273 flags
= bed
->dynamic_sec_flags
;
276 if (bed
->plt_not_loaded
)
277 /* We do not clear SEC_ALLOC here because we still want the OS to
278 allocate space for the section; it's just that there's nothing
279 to read in from the object file. */
280 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
282 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
283 if (bed
->plt_readonly
)
284 pltflags
|= SEC_READONLY
;
286 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
288 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
293 if (bed
->want_plt_sym
)
295 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
296 "_PROCEDURE_LINKAGE_TABLE_");
297 elf_hash_table (info
)->hplt
= h
;
302 s
= bfd_make_section_with_flags (abfd
,
303 (bed
->default_use_rela_p
304 ? ".rela.plt" : ".rel.plt"),
305 flags
| SEC_READONLY
);
307 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
310 if (! _bfd_elf_create_got_section (abfd
, info
))
313 if (bed
->want_dynbss
)
315 /* The .dynbss section is a place to put symbols which are defined
316 by dynamic objects, are referenced by regular objects, and are
317 not functions. We must allocate space for them in the process
318 image and use a R_*_COPY reloc to tell the dynamic linker to
319 initialize them at run time. The linker script puts the .dynbss
320 section into the .bss section of the final image. */
321 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
323 | SEC_LINKER_CREATED
));
327 /* The .rel[a].bss section holds copy relocs. This section is not
328 normally needed. We need to create it here, though, so that the
329 linker will map it to an output section. We can't just create it
330 only if we need it, because we will not know whether we need it
331 until we have seen all the input files, and the first time the
332 main linker code calls BFD after examining all the input files
333 (size_dynamic_sections) the input sections have already been
334 mapped to the output sections. If the section turns out not to
335 be needed, we can discard it later. We will never need this
336 section when generating a shared object, since they do not use
340 s
= bfd_make_section_with_flags (abfd
,
341 (bed
->default_use_rela_p
342 ? ".rela.bss" : ".rel.bss"),
343 flags
| SEC_READONLY
);
345 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
353 /* Record a new dynamic symbol. We record the dynamic symbols as we
354 read the input files, since we need to have a list of all of them
355 before we can determine the final sizes of the output sections.
356 Note that we may actually call this function even though we are not
357 going to output any dynamic symbols; in some cases we know that a
358 symbol should be in the dynamic symbol table, but only if there is
362 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
363 struct elf_link_hash_entry
*h
)
365 if (h
->dynindx
== -1)
367 struct elf_strtab_hash
*dynstr
;
372 /* XXX: The ABI draft says the linker must turn hidden and
373 internal symbols into STB_LOCAL symbols when producing the
374 DSO. However, if ld.so honors st_other in the dynamic table,
375 this would not be necessary. */
376 switch (ELF_ST_VISIBILITY (h
->other
))
380 if (h
->root
.type
!= bfd_link_hash_undefined
381 && h
->root
.type
!= bfd_link_hash_undefweak
)
384 if (!elf_hash_table (info
)->is_relocatable_executable
)
392 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
393 ++elf_hash_table (info
)->dynsymcount
;
395 dynstr
= elf_hash_table (info
)->dynstr
;
398 /* Create a strtab to hold the dynamic symbol names. */
399 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
404 /* We don't put any version information in the dynamic string
406 name
= h
->root
.root
.string
;
407 p
= strchr (name
, ELF_VER_CHR
);
409 /* We know that the p points into writable memory. In fact,
410 there are only a few symbols that have read-only names, being
411 those like _GLOBAL_OFFSET_TABLE_ that are created specially
412 by the backends. Most symbols will have names pointing into
413 an ELF string table read from a file, or to objalloc memory. */
416 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
421 if (indx
== (bfd_size_type
) -1)
423 h
->dynstr_index
= indx
;
429 /* Record an assignment to a symbol made by a linker script. We need
430 this in case some dynamic object refers to this symbol. */
433 bfd_elf_record_link_assignment (bfd
*output_bfd
,
434 struct bfd_link_info
*info
,
439 struct elf_link_hash_entry
*h
;
440 struct elf_link_hash_table
*htab
;
442 if (!is_elf_hash_table (info
->hash
))
445 htab
= elf_hash_table (info
);
446 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
450 /* Since we're defining the symbol, don't let it seem to have not
451 been defined. record_dynamic_symbol and size_dynamic_sections
452 may depend on this. */
453 if (h
->root
.type
== bfd_link_hash_undefweak
454 || h
->root
.type
== bfd_link_hash_undefined
)
456 h
->root
.type
= bfd_link_hash_new
;
457 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
458 bfd_link_repair_undef_list (&htab
->root
);
461 if (h
->root
.type
== bfd_link_hash_new
)
464 /* If this symbol is being provided by the linker script, and it is
465 currently defined by a dynamic object, but not by a regular
466 object, then mark it as undefined so that the generic linker will
467 force the correct value. */
471 h
->root
.type
= bfd_link_hash_undefined
;
473 /* If this symbol is not being provided by the linker script, and it is
474 currently defined by a dynamic object, but not by a regular object,
475 then clear out any version information because the symbol will not be
476 associated with the dynamic object any more. */
480 h
->verinfo
.verdef
= NULL
;
484 if (provide
&& hidden
)
486 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
488 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
489 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
492 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
494 if (!info
->relocatable
496 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
497 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
503 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
506 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
509 /* If this is a weak defined symbol, and we know a corresponding
510 real symbol from the same dynamic object, make sure the real
511 symbol is also made into a dynamic symbol. */
512 if (h
->u
.weakdef
!= NULL
513 && h
->u
.weakdef
->dynindx
== -1)
515 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
523 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
524 success, and 2 on a failure caused by attempting to record a symbol
525 in a discarded section, eg. a discarded link-once section symbol. */
528 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
533 struct elf_link_local_dynamic_entry
*entry
;
534 struct elf_link_hash_table
*eht
;
535 struct elf_strtab_hash
*dynstr
;
536 unsigned long dynstr_index
;
538 Elf_External_Sym_Shndx eshndx
;
539 char esym
[sizeof (Elf64_External_Sym
)];
541 if (! is_elf_hash_table (info
->hash
))
544 /* See if the entry exists already. */
545 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
546 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
549 amt
= sizeof (*entry
);
550 entry
= bfd_alloc (input_bfd
, amt
);
554 /* Go find the symbol, so that we can find it's name. */
555 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
556 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
558 bfd_release (input_bfd
, entry
);
562 if (entry
->isym
.st_shndx
!= SHN_UNDEF
563 && (entry
->isym
.st_shndx
< SHN_LORESERVE
564 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
568 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
569 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
571 /* We can still bfd_release here as nothing has done another
572 bfd_alloc. We can't do this later in this function. */
573 bfd_release (input_bfd
, entry
);
578 name
= (bfd_elf_string_from_elf_section
579 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
580 entry
->isym
.st_name
));
582 dynstr
= elf_hash_table (info
)->dynstr
;
585 /* Create a strtab to hold the dynamic symbol names. */
586 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
591 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
592 if (dynstr_index
== (unsigned long) -1)
594 entry
->isym
.st_name
= dynstr_index
;
596 eht
= elf_hash_table (info
);
598 entry
->next
= eht
->dynlocal
;
599 eht
->dynlocal
= entry
;
600 entry
->input_bfd
= input_bfd
;
601 entry
->input_indx
= input_indx
;
604 /* Whatever binding the symbol had before, it's now local. */
606 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
608 /* The dynindx will be set at the end of size_dynamic_sections. */
613 /* Return the dynindex of a local dynamic symbol. */
616 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
620 struct elf_link_local_dynamic_entry
*e
;
622 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
623 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
628 /* This function is used to renumber the dynamic symbols, if some of
629 them are removed because they are marked as local. This is called
630 via elf_link_hash_traverse. */
633 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
636 size_t *count
= data
;
638 if (h
->root
.type
== bfd_link_hash_warning
)
639 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
644 if (h
->dynindx
!= -1)
645 h
->dynindx
= ++(*count
);
651 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
652 STB_LOCAL binding. */
655 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
658 size_t *count
= data
;
660 if (h
->root
.type
== bfd_link_hash_warning
)
661 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
663 if (!h
->forced_local
)
666 if (h
->dynindx
!= -1)
667 h
->dynindx
= ++(*count
);
672 /* Return true if the dynamic symbol for a given section should be
673 omitted when creating a shared library. */
675 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
676 struct bfd_link_info
*info
,
679 switch (elf_section_data (p
)->this_hdr
.sh_type
)
683 /* If sh_type is yet undecided, assume it could be
684 SHT_PROGBITS/SHT_NOBITS. */
686 if (strcmp (p
->name
, ".got") == 0
687 || strcmp (p
->name
, ".got.plt") == 0
688 || strcmp (p
->name
, ".plt") == 0)
691 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
694 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
695 && (ip
->flags
& SEC_LINKER_CREATED
)
696 && ip
->output_section
== p
)
701 /* There shouldn't be section relative relocations
702 against any other section. */
708 /* Assign dynsym indices. In a shared library we generate a section
709 symbol for each output section, which come first. Next come symbols
710 which have been forced to local binding. Then all of the back-end
711 allocated local dynamic syms, followed by the rest of the global
715 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
716 struct bfd_link_info
*info
,
717 unsigned long *section_sym_count
)
719 unsigned long dynsymcount
= 0;
721 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
723 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
725 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
726 if ((p
->flags
& SEC_EXCLUDE
) == 0
727 && (p
->flags
& SEC_ALLOC
) != 0
728 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
729 elf_section_data (p
)->dynindx
= ++dynsymcount
;
731 *section_sym_count
= dynsymcount
;
733 elf_link_hash_traverse (elf_hash_table (info
),
734 elf_link_renumber_local_hash_table_dynsyms
,
737 if (elf_hash_table (info
)->dynlocal
)
739 struct elf_link_local_dynamic_entry
*p
;
740 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
741 p
->dynindx
= ++dynsymcount
;
744 elf_link_hash_traverse (elf_hash_table (info
),
745 elf_link_renumber_hash_table_dynsyms
,
748 /* There is an unused NULL entry at the head of the table which
749 we must account for in our count. Unless there weren't any
750 symbols, which means we'll have no table at all. */
751 if (dynsymcount
!= 0)
754 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
758 /* This function is called when we want to define a new symbol. It
759 handles the various cases which arise when we find a definition in
760 a dynamic object, or when there is already a definition in a
761 dynamic object. The new symbol is described by NAME, SYM, PSEC,
762 and PVALUE. We set SYM_HASH to the hash table entry. We set
763 OVERRIDE if the old symbol is overriding a new definition. We set
764 TYPE_CHANGE_OK if it is OK for the type to change. We set
765 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
766 change, we mean that we shouldn't warn if the type or size does
767 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
768 object is overridden by a regular object. */
771 _bfd_elf_merge_symbol (bfd
*abfd
,
772 struct bfd_link_info
*info
,
774 Elf_Internal_Sym
*sym
,
777 unsigned int *pold_alignment
,
778 struct elf_link_hash_entry
**sym_hash
,
780 bfd_boolean
*override
,
781 bfd_boolean
*type_change_ok
,
782 bfd_boolean
*size_change_ok
)
784 asection
*sec
, *oldsec
;
785 struct elf_link_hash_entry
*h
;
786 struct elf_link_hash_entry
*flip
;
789 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
790 bfd_boolean newweak
, oldweak
;
791 const struct elf_backend_data
*bed
;
797 bind
= ELF_ST_BIND (sym
->st_info
);
799 if (! bfd_is_und_section (sec
))
800 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
802 h
= ((struct elf_link_hash_entry
*)
803 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
808 /* This code is for coping with dynamic objects, and is only useful
809 if we are doing an ELF link. */
810 if (info
->hash
->creator
!= abfd
->xvec
)
813 /* For merging, we only care about real symbols. */
815 while (h
->root
.type
== bfd_link_hash_indirect
816 || h
->root
.type
== bfd_link_hash_warning
)
817 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
819 /* If we just created the symbol, mark it as being an ELF symbol.
820 Other than that, there is nothing to do--there is no merge issue
821 with a newly defined symbol--so we just return. */
823 if (h
->root
.type
== bfd_link_hash_new
)
829 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
832 switch (h
->root
.type
)
839 case bfd_link_hash_undefined
:
840 case bfd_link_hash_undefweak
:
841 oldbfd
= h
->root
.u
.undef
.abfd
;
845 case bfd_link_hash_defined
:
846 case bfd_link_hash_defweak
:
847 oldbfd
= h
->root
.u
.def
.section
->owner
;
848 oldsec
= h
->root
.u
.def
.section
;
851 case bfd_link_hash_common
:
852 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
853 oldsec
= h
->root
.u
.c
.p
->section
;
857 /* In cases involving weak versioned symbols, we may wind up trying
858 to merge a symbol with itself. Catch that here, to avoid the
859 confusion that results if we try to override a symbol with
860 itself. The additional tests catch cases like
861 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
862 dynamic object, which we do want to handle here. */
864 && ((abfd
->flags
& DYNAMIC
) == 0
868 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
869 respectively, is from a dynamic object. */
871 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
875 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
876 else if (oldsec
!= NULL
)
878 /* This handles the special SHN_MIPS_{TEXT,DATA} section
879 indices used by MIPS ELF. */
880 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
883 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
884 respectively, appear to be a definition rather than reference. */
886 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
888 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
889 && h
->root
.type
!= bfd_link_hash_undefweak
890 && h
->root
.type
!= bfd_link_hash_common
);
892 /* When we try to create a default indirect symbol from the dynamic
893 definition with the default version, we skip it if its type and
894 the type of existing regular definition mismatch. We only do it
895 if the existing regular definition won't be dynamic. */
896 if (pold_alignment
== NULL
898 && !info
->export_dynamic
903 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
904 && ELF_ST_TYPE (sym
->st_info
) != h
->type
905 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
906 && h
->type
!= STT_NOTYPE
)
912 /* Check TLS symbol. We don't check undefined symbol introduced by
914 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
915 && ELF_ST_TYPE (sym
->st_info
) != h
->type
919 bfd_boolean ntdef
, tdef
;
920 asection
*ntsec
, *tsec
;
922 if (h
->type
== STT_TLS
)
942 (*_bfd_error_handler
)
943 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
944 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
945 else if (!tdef
&& !ntdef
)
946 (*_bfd_error_handler
)
947 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
948 tbfd
, ntbfd
, h
->root
.root
.string
);
950 (*_bfd_error_handler
)
951 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
952 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
954 (*_bfd_error_handler
)
955 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
956 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
958 bfd_set_error (bfd_error_bad_value
);
962 /* We need to remember if a symbol has a definition in a dynamic
963 object or is weak in all dynamic objects. Internal and hidden
964 visibility will make it unavailable to dynamic objects. */
965 if (newdyn
&& !h
->dynamic_def
)
967 if (!bfd_is_und_section (sec
))
971 /* Check if this symbol is weak in all dynamic objects. If it
972 is the first time we see it in a dynamic object, we mark
973 if it is weak. Otherwise, we clear it. */
976 if (bind
== STB_WEAK
)
979 else if (bind
!= STB_WEAK
)
984 /* If the old symbol has non-default visibility, we ignore the new
985 definition from a dynamic object. */
987 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
988 && !bfd_is_und_section (sec
))
991 /* Make sure this symbol is dynamic. */
993 /* A protected symbol has external availability. Make sure it is
996 FIXME: Should we check type and size for protected symbol? */
997 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
998 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1003 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1006 /* If the new symbol with non-default visibility comes from a
1007 relocatable file and the old definition comes from a dynamic
1008 object, we remove the old definition. */
1009 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1012 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1013 && bfd_is_und_section (sec
))
1015 /* If the new symbol is undefined and the old symbol was
1016 also undefined before, we need to make sure
1017 _bfd_generic_link_add_one_symbol doesn't mess
1018 up the linker hash table undefs list. Since the old
1019 definition came from a dynamic object, it is still on the
1021 h
->root
.type
= bfd_link_hash_undefined
;
1022 h
->root
.u
.undef
.abfd
= abfd
;
1026 h
->root
.type
= bfd_link_hash_new
;
1027 h
->root
.u
.undef
.abfd
= NULL
;
1036 /* FIXME: Should we check type and size for protected symbol? */
1042 /* Differentiate strong and weak symbols. */
1043 newweak
= bind
== STB_WEAK
;
1044 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1045 || h
->root
.type
== bfd_link_hash_undefweak
);
1047 /* If a new weak symbol definition comes from a regular file and the
1048 old symbol comes from a dynamic library, we treat the new one as
1049 strong. Similarly, an old weak symbol definition from a regular
1050 file is treated as strong when the new symbol comes from a dynamic
1051 library. Further, an old weak symbol from a dynamic library is
1052 treated as strong if the new symbol is from a dynamic library.
1053 This reflects the way glibc's ld.so works.
1055 Do this before setting *type_change_ok or *size_change_ok so that
1056 we warn properly when dynamic library symbols are overridden. */
1058 if (newdef
&& !newdyn
&& olddyn
)
1060 if (olddef
&& newdyn
)
1063 /* It's OK to change the type if either the existing symbol or the
1064 new symbol is weak. A type change is also OK if the old symbol
1065 is undefined and the new symbol is defined. */
1070 && h
->root
.type
== bfd_link_hash_undefined
))
1071 *type_change_ok
= TRUE
;
1073 /* It's OK to change the size if either the existing symbol or the
1074 new symbol is weak, or if the old symbol is undefined. */
1077 || h
->root
.type
== bfd_link_hash_undefined
)
1078 *size_change_ok
= TRUE
;
1080 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1081 symbol, respectively, appears to be a common symbol in a dynamic
1082 object. If a symbol appears in an uninitialized section, and is
1083 not weak, and is not a function, then it may be a common symbol
1084 which was resolved when the dynamic object was created. We want
1085 to treat such symbols specially, because they raise special
1086 considerations when setting the symbol size: if the symbol
1087 appears as a common symbol in a regular object, and the size in
1088 the regular object is larger, we must make sure that we use the
1089 larger size. This problematic case can always be avoided in C,
1090 but it must be handled correctly when using Fortran shared
1093 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1094 likewise for OLDDYNCOMMON and OLDDEF.
1096 Note that this test is just a heuristic, and that it is quite
1097 possible to have an uninitialized symbol in a shared object which
1098 is really a definition, rather than a common symbol. This could
1099 lead to some minor confusion when the symbol really is a common
1100 symbol in some regular object. However, I think it will be
1106 && (sec
->flags
& SEC_ALLOC
) != 0
1107 && (sec
->flags
& SEC_LOAD
) == 0
1109 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1110 newdyncommon
= TRUE
;
1112 newdyncommon
= FALSE
;
1116 && h
->root
.type
== bfd_link_hash_defined
1118 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1119 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1121 && h
->type
!= STT_FUNC
)
1122 olddyncommon
= TRUE
;
1124 olddyncommon
= FALSE
;
1126 /* We now know everything about the old and new symbols. We ask the
1127 backend to check if we can merge them. */
1128 bed
= get_elf_backend_data (abfd
);
1129 if (bed
->merge_symbol
1130 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1131 pold_alignment
, skip
, override
,
1132 type_change_ok
, size_change_ok
,
1133 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1135 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1139 /* If both the old and the new symbols look like common symbols in a
1140 dynamic object, set the size of the symbol to the larger of the
1145 && sym
->st_size
!= h
->size
)
1147 /* Since we think we have two common symbols, issue a multiple
1148 common warning if desired. Note that we only warn if the
1149 size is different. If the size is the same, we simply let
1150 the old symbol override the new one as normally happens with
1151 symbols defined in dynamic objects. */
1153 if (! ((*info
->callbacks
->multiple_common
)
1154 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1155 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1158 if (sym
->st_size
> h
->size
)
1159 h
->size
= sym
->st_size
;
1161 *size_change_ok
= TRUE
;
1164 /* If we are looking at a dynamic object, and we have found a
1165 definition, we need to see if the symbol was already defined by
1166 some other object. If so, we want to use the existing
1167 definition, and we do not want to report a multiple symbol
1168 definition error; we do this by clobbering *PSEC to be
1169 bfd_und_section_ptr.
1171 We treat a common symbol as a definition if the symbol in the
1172 shared library is a function, since common symbols always
1173 represent variables; this can cause confusion in principle, but
1174 any such confusion would seem to indicate an erroneous program or
1175 shared library. We also permit a common symbol in a regular
1176 object to override a weak symbol in a shared object. */
1181 || (h
->root
.type
== bfd_link_hash_common
1183 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1187 newdyncommon
= FALSE
;
1189 *psec
= sec
= bfd_und_section_ptr
;
1190 *size_change_ok
= TRUE
;
1192 /* If we get here when the old symbol is a common symbol, then
1193 we are explicitly letting it override a weak symbol or
1194 function in a dynamic object, and we don't want to warn about
1195 a type change. If the old symbol is a defined symbol, a type
1196 change warning may still be appropriate. */
1198 if (h
->root
.type
== bfd_link_hash_common
)
1199 *type_change_ok
= TRUE
;
1202 /* Handle the special case of an old common symbol merging with a
1203 new symbol which looks like a common symbol in a shared object.
1204 We change *PSEC and *PVALUE to make the new symbol look like a
1205 common symbol, and let _bfd_generic_link_add_one_symbol do the
1209 && h
->root
.type
== bfd_link_hash_common
)
1213 newdyncommon
= FALSE
;
1214 *pvalue
= sym
->st_size
;
1215 *psec
= sec
= bed
->common_section (oldsec
);
1216 *size_change_ok
= TRUE
;
1219 /* Skip weak definitions of symbols that are already defined. */
1220 if (newdef
&& olddef
&& newweak
)
1223 /* If the old symbol is from a dynamic object, and the new symbol is
1224 a definition which is not from a dynamic object, then the new
1225 symbol overrides the old symbol. Symbols from regular files
1226 always take precedence over symbols from dynamic objects, even if
1227 they are defined after the dynamic object in the link.
1229 As above, we again permit a common symbol in a regular object to
1230 override a definition in a shared object if the shared object
1231 symbol is a function or is weak. */
1236 || (bfd_is_com_section (sec
)
1238 || h
->type
== STT_FUNC
)))
1243 /* Change the hash table entry to undefined, and let
1244 _bfd_generic_link_add_one_symbol do the right thing with the
1247 h
->root
.type
= bfd_link_hash_undefined
;
1248 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1249 *size_change_ok
= TRUE
;
1252 olddyncommon
= FALSE
;
1254 /* We again permit a type change when a common symbol may be
1255 overriding a function. */
1257 if (bfd_is_com_section (sec
))
1258 *type_change_ok
= TRUE
;
1260 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1263 /* This union may have been set to be non-NULL when this symbol
1264 was seen in a dynamic object. We must force the union to be
1265 NULL, so that it is correct for a regular symbol. */
1266 h
->verinfo
.vertree
= NULL
;
1269 /* Handle the special case of a new common symbol merging with an
1270 old symbol that looks like it might be a common symbol defined in
1271 a shared object. Note that we have already handled the case in
1272 which a new common symbol should simply override the definition
1273 in the shared library. */
1276 && bfd_is_com_section (sec
)
1279 /* It would be best if we could set the hash table entry to a
1280 common symbol, but we don't know what to use for the section
1281 or the alignment. */
1282 if (! ((*info
->callbacks
->multiple_common
)
1283 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1284 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1287 /* If the presumed common symbol in the dynamic object is
1288 larger, pretend that the new symbol has its size. */
1290 if (h
->size
> *pvalue
)
1293 /* We need to remember the alignment required by the symbol
1294 in the dynamic object. */
1295 BFD_ASSERT (pold_alignment
);
1296 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1299 olddyncommon
= FALSE
;
1301 h
->root
.type
= bfd_link_hash_undefined
;
1302 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1304 *size_change_ok
= TRUE
;
1305 *type_change_ok
= TRUE
;
1307 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1310 h
->verinfo
.vertree
= NULL
;
1315 /* Handle the case where we had a versioned symbol in a dynamic
1316 library and now find a definition in a normal object. In this
1317 case, we make the versioned symbol point to the normal one. */
1318 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1319 flip
->root
.type
= h
->root
.type
;
1320 h
->root
.type
= bfd_link_hash_indirect
;
1321 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1322 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1323 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1327 flip
->ref_dynamic
= 1;
1334 /* This function is called to create an indirect symbol from the
1335 default for the symbol with the default version if needed. The
1336 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1337 set DYNSYM if the new indirect symbol is dynamic. */
1340 _bfd_elf_add_default_symbol (bfd
*abfd
,
1341 struct bfd_link_info
*info
,
1342 struct elf_link_hash_entry
*h
,
1344 Elf_Internal_Sym
*sym
,
1347 bfd_boolean
*dynsym
,
1348 bfd_boolean override
)
1350 bfd_boolean type_change_ok
;
1351 bfd_boolean size_change_ok
;
1354 struct elf_link_hash_entry
*hi
;
1355 struct bfd_link_hash_entry
*bh
;
1356 const struct elf_backend_data
*bed
;
1357 bfd_boolean collect
;
1358 bfd_boolean dynamic
;
1360 size_t len
, shortlen
;
1363 /* If this symbol has a version, and it is the default version, we
1364 create an indirect symbol from the default name to the fully
1365 decorated name. This will cause external references which do not
1366 specify a version to be bound to this version of the symbol. */
1367 p
= strchr (name
, ELF_VER_CHR
);
1368 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1373 /* We are overridden by an old definition. We need to check if we
1374 need to create the indirect symbol from the default name. */
1375 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1377 BFD_ASSERT (hi
!= NULL
);
1380 while (hi
->root
.type
== bfd_link_hash_indirect
1381 || hi
->root
.type
== bfd_link_hash_warning
)
1383 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1389 bed
= get_elf_backend_data (abfd
);
1390 collect
= bed
->collect
;
1391 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1393 shortlen
= p
- name
;
1394 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1395 if (shortname
== NULL
)
1397 memcpy (shortname
, name
, shortlen
);
1398 shortname
[shortlen
] = '\0';
1400 /* We are going to create a new symbol. Merge it with any existing
1401 symbol with this name. For the purposes of the merge, act as
1402 though we were defining the symbol we just defined, although we
1403 actually going to define an indirect symbol. */
1404 type_change_ok
= FALSE
;
1405 size_change_ok
= FALSE
;
1407 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1408 NULL
, &hi
, &skip
, &override
,
1409 &type_change_ok
, &size_change_ok
))
1418 if (! (_bfd_generic_link_add_one_symbol
1419 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1420 0, name
, FALSE
, collect
, &bh
)))
1422 hi
= (struct elf_link_hash_entry
*) bh
;
1426 /* In this case the symbol named SHORTNAME is overriding the
1427 indirect symbol we want to add. We were planning on making
1428 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1429 is the name without a version. NAME is the fully versioned
1430 name, and it is the default version.
1432 Overriding means that we already saw a definition for the
1433 symbol SHORTNAME in a regular object, and it is overriding
1434 the symbol defined in the dynamic object.
1436 When this happens, we actually want to change NAME, the
1437 symbol we just added, to refer to SHORTNAME. This will cause
1438 references to NAME in the shared object to become references
1439 to SHORTNAME in the regular object. This is what we expect
1440 when we override a function in a shared object: that the
1441 references in the shared object will be mapped to the
1442 definition in the regular object. */
1444 while (hi
->root
.type
== bfd_link_hash_indirect
1445 || hi
->root
.type
== bfd_link_hash_warning
)
1446 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1448 h
->root
.type
= bfd_link_hash_indirect
;
1449 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1453 hi
->ref_dynamic
= 1;
1457 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1462 /* Now set HI to H, so that the following code will set the
1463 other fields correctly. */
1467 /* If there is a duplicate definition somewhere, then HI may not
1468 point to an indirect symbol. We will have reported an error to
1469 the user in that case. */
1471 if (hi
->root
.type
== bfd_link_hash_indirect
)
1473 struct elf_link_hash_entry
*ht
;
1475 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1476 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1478 /* See if the new flags lead us to realize that the symbol must
1490 if (hi
->ref_regular
)
1496 /* We also need to define an indirection from the nondefault version
1500 len
= strlen (name
);
1501 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1502 if (shortname
== NULL
)
1504 memcpy (shortname
, name
, shortlen
);
1505 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1507 /* Once again, merge with any existing symbol. */
1508 type_change_ok
= FALSE
;
1509 size_change_ok
= FALSE
;
1511 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1512 NULL
, &hi
, &skip
, &override
,
1513 &type_change_ok
, &size_change_ok
))
1521 /* Here SHORTNAME is a versioned name, so we don't expect to see
1522 the type of override we do in the case above unless it is
1523 overridden by a versioned definition. */
1524 if (hi
->root
.type
!= bfd_link_hash_defined
1525 && hi
->root
.type
!= bfd_link_hash_defweak
)
1526 (*_bfd_error_handler
)
1527 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1533 if (! (_bfd_generic_link_add_one_symbol
1534 (info
, abfd
, shortname
, BSF_INDIRECT
,
1535 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1537 hi
= (struct elf_link_hash_entry
*) bh
;
1539 /* If there is a duplicate definition somewhere, then HI may not
1540 point to an indirect symbol. We will have reported an error
1541 to the user in that case. */
1543 if (hi
->root
.type
== bfd_link_hash_indirect
)
1545 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1547 /* See if the new flags lead us to realize that the symbol
1559 if (hi
->ref_regular
)
1569 /* This routine is used to export all defined symbols into the dynamic
1570 symbol table. It is called via elf_link_hash_traverse. */
1573 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1575 struct elf_info_failed
*eif
= data
;
1577 /* Ignore indirect symbols. These are added by the versioning code. */
1578 if (h
->root
.type
== bfd_link_hash_indirect
)
1581 if (h
->root
.type
== bfd_link_hash_warning
)
1582 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1584 if (h
->dynindx
== -1
1588 struct bfd_elf_version_tree
*t
;
1589 struct bfd_elf_version_expr
*d
;
1591 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1593 if (t
->globals
.list
!= NULL
)
1595 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1600 if (t
->locals
.list
!= NULL
)
1602 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1611 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1622 /* Look through the symbols which are defined in other shared
1623 libraries and referenced here. Update the list of version
1624 dependencies. This will be put into the .gnu.version_r section.
1625 This function is called via elf_link_hash_traverse. */
1628 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1631 struct elf_find_verdep_info
*rinfo
= data
;
1632 Elf_Internal_Verneed
*t
;
1633 Elf_Internal_Vernaux
*a
;
1636 if (h
->root
.type
== bfd_link_hash_warning
)
1637 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1639 /* We only care about symbols defined in shared objects with version
1644 || h
->verinfo
.verdef
== NULL
)
1647 /* See if we already know about this version. */
1648 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1650 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1653 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1654 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1660 /* This is a new version. Add it to tree we are building. */
1665 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1668 rinfo
->failed
= TRUE
;
1672 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1673 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1674 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1678 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1680 /* Note that we are copying a string pointer here, and testing it
1681 above. If bfd_elf_string_from_elf_section is ever changed to
1682 discard the string data when low in memory, this will have to be
1684 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1686 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1687 a
->vna_nextptr
= t
->vn_auxptr
;
1689 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1692 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1699 /* Figure out appropriate versions for all the symbols. We may not
1700 have the version number script until we have read all of the input
1701 files, so until that point we don't know which symbols should be
1702 local. This function is called via elf_link_hash_traverse. */
1705 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1707 struct elf_assign_sym_version_info
*sinfo
;
1708 struct bfd_link_info
*info
;
1709 const struct elf_backend_data
*bed
;
1710 struct elf_info_failed eif
;
1717 if (h
->root
.type
== bfd_link_hash_warning
)
1718 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1720 /* Fix the symbol flags. */
1723 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1726 sinfo
->failed
= TRUE
;
1730 /* We only need version numbers for symbols defined in regular
1732 if (!h
->def_regular
)
1735 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1736 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1737 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1739 struct bfd_elf_version_tree
*t
;
1744 /* There are two consecutive ELF_VER_CHR characters if this is
1745 not a hidden symbol. */
1747 if (*p
== ELF_VER_CHR
)
1753 /* If there is no version string, we can just return out. */
1761 /* Look for the version. If we find it, it is no longer weak. */
1762 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1764 if (strcmp (t
->name
, p
) == 0)
1768 struct bfd_elf_version_expr
*d
;
1770 len
= p
- h
->root
.root
.string
;
1771 alc
= bfd_malloc (len
);
1774 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1775 alc
[len
- 1] = '\0';
1776 if (alc
[len
- 2] == ELF_VER_CHR
)
1777 alc
[len
- 2] = '\0';
1779 h
->verinfo
.vertree
= t
;
1783 if (t
->globals
.list
!= NULL
)
1784 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1786 /* See if there is anything to force this symbol to
1788 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1790 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1793 && ! info
->export_dynamic
)
1794 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1802 /* If we are building an application, we need to create a
1803 version node for this version. */
1804 if (t
== NULL
&& info
->executable
)
1806 struct bfd_elf_version_tree
**pp
;
1809 /* If we aren't going to export this symbol, we don't need
1810 to worry about it. */
1811 if (h
->dynindx
== -1)
1815 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1818 sinfo
->failed
= TRUE
;
1823 t
->name_indx
= (unsigned int) -1;
1827 /* Don't count anonymous version tag. */
1828 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1830 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1832 t
->vernum
= version_index
;
1836 h
->verinfo
.vertree
= t
;
1840 /* We could not find the version for a symbol when
1841 generating a shared archive. Return an error. */
1842 (*_bfd_error_handler
)
1843 (_("%B: undefined versioned symbol name %s"),
1844 sinfo
->output_bfd
, h
->root
.root
.string
);
1845 bfd_set_error (bfd_error_bad_value
);
1846 sinfo
->failed
= TRUE
;
1854 /* If we don't have a version for this symbol, see if we can find
1856 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1858 struct bfd_elf_version_tree
*t
;
1859 struct bfd_elf_version_tree
*local_ver
;
1860 struct bfd_elf_version_expr
*d
;
1862 /* See if can find what version this symbol is in. If the
1863 symbol is supposed to be local, then don't actually register
1866 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1868 if (t
->globals
.list
!= NULL
)
1870 bfd_boolean matched
;
1874 while ((d
= (*t
->match
) (&t
->globals
, d
,
1875 h
->root
.root
.string
)) != NULL
)
1880 /* There is a version without definition. Make
1881 the symbol the default definition for this
1883 h
->verinfo
.vertree
= t
;
1891 /* There is no undefined version for this symbol. Hide the
1893 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1896 if (t
->locals
.list
!= NULL
)
1899 while ((d
= (*t
->match
) (&t
->locals
, d
,
1900 h
->root
.root
.string
)) != NULL
)
1903 /* If the match is "*", keep looking for a more
1904 explicit, perhaps even global, match.
1905 XXX: Shouldn't this be !d->wildcard instead? */
1906 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1915 if (local_ver
!= NULL
)
1917 h
->verinfo
.vertree
= local_ver
;
1918 if (h
->dynindx
!= -1
1919 && ! info
->export_dynamic
)
1921 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1929 /* Read and swap the relocs from the section indicated by SHDR. This
1930 may be either a REL or a RELA section. The relocations are
1931 translated into RELA relocations and stored in INTERNAL_RELOCS,
1932 which should have already been allocated to contain enough space.
1933 The EXTERNAL_RELOCS are a buffer where the external form of the
1934 relocations should be stored.
1936 Returns FALSE if something goes wrong. */
1939 elf_link_read_relocs_from_section (bfd
*abfd
,
1941 Elf_Internal_Shdr
*shdr
,
1942 void *external_relocs
,
1943 Elf_Internal_Rela
*internal_relocs
)
1945 const struct elf_backend_data
*bed
;
1946 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1947 const bfd_byte
*erela
;
1948 const bfd_byte
*erelaend
;
1949 Elf_Internal_Rela
*irela
;
1950 Elf_Internal_Shdr
*symtab_hdr
;
1953 /* Position ourselves at the start of the section. */
1954 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1957 /* Read the relocations. */
1958 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1961 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1962 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1964 bed
= get_elf_backend_data (abfd
);
1966 /* Convert the external relocations to the internal format. */
1967 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1968 swap_in
= bed
->s
->swap_reloc_in
;
1969 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1970 swap_in
= bed
->s
->swap_reloca_in
;
1973 bfd_set_error (bfd_error_wrong_format
);
1977 erela
= external_relocs
;
1978 erelaend
= erela
+ shdr
->sh_size
;
1979 irela
= internal_relocs
;
1980 while (erela
< erelaend
)
1984 (*swap_in
) (abfd
, erela
, irela
);
1985 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1986 if (bed
->s
->arch_size
== 64)
1988 if ((size_t) r_symndx
>= nsyms
)
1990 (*_bfd_error_handler
)
1991 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1992 " for offset 0x%lx in section `%A'"),
1994 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1995 bfd_set_error (bfd_error_bad_value
);
1998 irela
+= bed
->s
->int_rels_per_ext_rel
;
1999 erela
+= shdr
->sh_entsize
;
2005 /* Read and swap the relocs for a section O. They may have been
2006 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2007 not NULL, they are used as buffers to read into. They are known to
2008 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2009 the return value is allocated using either malloc or bfd_alloc,
2010 according to the KEEP_MEMORY argument. If O has two relocation
2011 sections (both REL and RELA relocations), then the REL_HDR
2012 relocations will appear first in INTERNAL_RELOCS, followed by the
2013 REL_HDR2 relocations. */
2016 _bfd_elf_link_read_relocs (bfd
*abfd
,
2018 void *external_relocs
,
2019 Elf_Internal_Rela
*internal_relocs
,
2020 bfd_boolean keep_memory
)
2022 Elf_Internal_Shdr
*rel_hdr
;
2023 void *alloc1
= NULL
;
2024 Elf_Internal_Rela
*alloc2
= NULL
;
2025 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2027 if (elf_section_data (o
)->relocs
!= NULL
)
2028 return elf_section_data (o
)->relocs
;
2030 if (o
->reloc_count
== 0)
2033 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2035 if (internal_relocs
== NULL
)
2039 size
= o
->reloc_count
;
2040 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2042 internal_relocs
= bfd_alloc (abfd
, size
);
2044 internal_relocs
= alloc2
= bfd_malloc (size
);
2045 if (internal_relocs
== NULL
)
2049 if (external_relocs
== NULL
)
2051 bfd_size_type size
= rel_hdr
->sh_size
;
2053 if (elf_section_data (o
)->rel_hdr2
)
2054 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2055 alloc1
= bfd_malloc (size
);
2058 external_relocs
= alloc1
;
2061 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2065 if (elf_section_data (o
)->rel_hdr2
2066 && (!elf_link_read_relocs_from_section
2068 elf_section_data (o
)->rel_hdr2
,
2069 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2070 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2071 * bed
->s
->int_rels_per_ext_rel
))))
2074 /* Cache the results for next time, if we can. */
2076 elf_section_data (o
)->relocs
= internal_relocs
;
2081 /* Don't free alloc2, since if it was allocated we are passing it
2082 back (under the name of internal_relocs). */
2084 return internal_relocs
;
2094 /* Compute the size of, and allocate space for, REL_HDR which is the
2095 section header for a section containing relocations for O. */
2098 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2099 Elf_Internal_Shdr
*rel_hdr
,
2102 bfd_size_type reloc_count
;
2103 bfd_size_type num_rel_hashes
;
2105 /* Figure out how many relocations there will be. */
2106 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2107 reloc_count
= elf_section_data (o
)->rel_count
;
2109 reloc_count
= elf_section_data (o
)->rel_count2
;
2111 num_rel_hashes
= o
->reloc_count
;
2112 if (num_rel_hashes
< reloc_count
)
2113 num_rel_hashes
= reloc_count
;
2115 /* That allows us to calculate the size of the section. */
2116 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2118 /* The contents field must last into write_object_contents, so we
2119 allocate it with bfd_alloc rather than malloc. Also since we
2120 cannot be sure that the contents will actually be filled in,
2121 we zero the allocated space. */
2122 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2123 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2126 /* We only allocate one set of hash entries, so we only do it the
2127 first time we are called. */
2128 if (elf_section_data (o
)->rel_hashes
== NULL
2131 struct elf_link_hash_entry
**p
;
2133 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2137 elf_section_data (o
)->rel_hashes
= p
;
2143 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2144 originated from the section given by INPUT_REL_HDR) to the
2148 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2149 asection
*input_section
,
2150 Elf_Internal_Shdr
*input_rel_hdr
,
2151 Elf_Internal_Rela
*internal_relocs
,
2152 struct elf_link_hash_entry
**rel_hash
2155 Elf_Internal_Rela
*irela
;
2156 Elf_Internal_Rela
*irelaend
;
2158 Elf_Internal_Shdr
*output_rel_hdr
;
2159 asection
*output_section
;
2160 unsigned int *rel_countp
= NULL
;
2161 const struct elf_backend_data
*bed
;
2162 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2164 output_section
= input_section
->output_section
;
2165 output_rel_hdr
= NULL
;
2167 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2168 == input_rel_hdr
->sh_entsize
)
2170 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2171 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2173 else if (elf_section_data (output_section
)->rel_hdr2
2174 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2175 == input_rel_hdr
->sh_entsize
))
2177 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2178 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2182 (*_bfd_error_handler
)
2183 (_("%B: relocation size mismatch in %B section %A"),
2184 output_bfd
, input_section
->owner
, input_section
);
2185 bfd_set_error (bfd_error_wrong_object_format
);
2189 bed
= get_elf_backend_data (output_bfd
);
2190 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2191 swap_out
= bed
->s
->swap_reloc_out
;
2192 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2193 swap_out
= bed
->s
->swap_reloca_out
;
2197 erel
= output_rel_hdr
->contents
;
2198 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2199 irela
= internal_relocs
;
2200 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2201 * bed
->s
->int_rels_per_ext_rel
);
2202 while (irela
< irelaend
)
2204 (*swap_out
) (output_bfd
, irela
, erel
);
2205 irela
+= bed
->s
->int_rels_per_ext_rel
;
2206 erel
+= input_rel_hdr
->sh_entsize
;
2209 /* Bump the counter, so that we know where to add the next set of
2211 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2216 /* Make weak undefined symbols in PIE dynamic. */
2219 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2220 struct elf_link_hash_entry
*h
)
2224 && h
->root
.type
== bfd_link_hash_undefweak
)
2225 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2230 /* Fix up the flags for a symbol. This handles various cases which
2231 can only be fixed after all the input files are seen. This is
2232 currently called by both adjust_dynamic_symbol and
2233 assign_sym_version, which is unnecessary but perhaps more robust in
2234 the face of future changes. */
2237 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2238 struct elf_info_failed
*eif
)
2240 const struct elf_backend_data
*bed
= NULL
;
2242 /* If this symbol was mentioned in a non-ELF file, try to set
2243 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2244 permit a non-ELF file to correctly refer to a symbol defined in
2245 an ELF dynamic object. */
2248 while (h
->root
.type
== bfd_link_hash_indirect
)
2249 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2251 if (h
->root
.type
!= bfd_link_hash_defined
2252 && h
->root
.type
!= bfd_link_hash_defweak
)
2255 h
->ref_regular_nonweak
= 1;
2259 if (h
->root
.u
.def
.section
->owner
!= NULL
2260 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2261 == bfd_target_elf_flavour
))
2264 h
->ref_regular_nonweak
= 1;
2270 if (h
->dynindx
== -1
2274 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2283 /* Unfortunately, NON_ELF is only correct if the symbol
2284 was first seen in a non-ELF file. Fortunately, if the symbol
2285 was first seen in an ELF file, we're probably OK unless the
2286 symbol was defined in a non-ELF file. Catch that case here.
2287 FIXME: We're still in trouble if the symbol was first seen in
2288 a dynamic object, and then later in a non-ELF regular object. */
2289 if ((h
->root
.type
== bfd_link_hash_defined
2290 || h
->root
.type
== bfd_link_hash_defweak
)
2292 && (h
->root
.u
.def
.section
->owner
!= NULL
2293 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2294 != bfd_target_elf_flavour
)
2295 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2296 && !h
->def_dynamic
)))
2300 /* Backend specific symbol fixup. */
2301 if (elf_hash_table (eif
->info
)->dynobj
)
2303 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2304 if (bed
->elf_backend_fixup_symbol
2305 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2309 /* If this is a final link, and the symbol was defined as a common
2310 symbol in a regular object file, and there was no definition in
2311 any dynamic object, then the linker will have allocated space for
2312 the symbol in a common section but the DEF_REGULAR
2313 flag will not have been set. */
2314 if (h
->root
.type
== bfd_link_hash_defined
2318 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2321 /* If -Bsymbolic was used (which means to bind references to global
2322 symbols to the definition within the shared object), and this
2323 symbol was defined in a regular object, then it actually doesn't
2324 need a PLT entry. Likewise, if the symbol has non-default
2325 visibility. If the symbol has hidden or internal visibility, we
2326 will force it local. */
2328 && eif
->info
->shared
2329 && is_elf_hash_table (eif
->info
->hash
)
2330 && (eif
->info
->symbolic
2331 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2334 bfd_boolean force_local
;
2336 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2337 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2338 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2341 /* If a weak undefined symbol has non-default visibility, we also
2342 hide it from the dynamic linker. */
2343 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2344 && h
->root
.type
== bfd_link_hash_undefweak
)
2346 const struct elf_backend_data
*bed
;
2347 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2348 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2351 /* If this is a weak defined symbol in a dynamic object, and we know
2352 the real definition in the dynamic object, copy interesting flags
2353 over to the real definition. */
2354 if (h
->u
.weakdef
!= NULL
)
2356 struct elf_link_hash_entry
*weakdef
;
2358 weakdef
= h
->u
.weakdef
;
2359 if (h
->root
.type
== bfd_link_hash_indirect
)
2360 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2362 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2363 || h
->root
.type
== bfd_link_hash_defweak
);
2364 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2365 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2366 BFD_ASSERT (weakdef
->def_dynamic
);
2368 /* If the real definition is defined by a regular object file,
2369 don't do anything special. See the longer description in
2370 _bfd_elf_adjust_dynamic_symbol, below. */
2371 if (weakdef
->def_regular
)
2372 h
->u
.weakdef
= NULL
;
2374 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2381 /* Make the backend pick a good value for a dynamic symbol. This is
2382 called via elf_link_hash_traverse, and also calls itself
2386 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2388 struct elf_info_failed
*eif
= data
;
2390 const struct elf_backend_data
*bed
;
2392 if (! is_elf_hash_table (eif
->info
->hash
))
2395 if (h
->root
.type
== bfd_link_hash_warning
)
2397 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2398 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2400 /* When warning symbols are created, they **replace** the "real"
2401 entry in the hash table, thus we never get to see the real
2402 symbol in a hash traversal. So look at it now. */
2403 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2406 /* Ignore indirect symbols. These are added by the versioning code. */
2407 if (h
->root
.type
== bfd_link_hash_indirect
)
2410 /* Fix the symbol flags. */
2411 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2414 /* If this symbol does not require a PLT entry, and it is not
2415 defined by a dynamic object, or is not referenced by a regular
2416 object, ignore it. We do have to handle a weak defined symbol,
2417 even if no regular object refers to it, if we decided to add it
2418 to the dynamic symbol table. FIXME: Do we normally need to worry
2419 about symbols which are defined by one dynamic object and
2420 referenced by another one? */
2425 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2427 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2431 /* If we've already adjusted this symbol, don't do it again. This
2432 can happen via a recursive call. */
2433 if (h
->dynamic_adjusted
)
2436 /* Don't look at this symbol again. Note that we must set this
2437 after checking the above conditions, because we may look at a
2438 symbol once, decide not to do anything, and then get called
2439 recursively later after REF_REGULAR is set below. */
2440 h
->dynamic_adjusted
= 1;
2442 /* If this is a weak definition, and we know a real definition, and
2443 the real symbol is not itself defined by a regular object file,
2444 then get a good value for the real definition. We handle the
2445 real symbol first, for the convenience of the backend routine.
2447 Note that there is a confusing case here. If the real definition
2448 is defined by a regular object file, we don't get the real symbol
2449 from the dynamic object, but we do get the weak symbol. If the
2450 processor backend uses a COPY reloc, then if some routine in the
2451 dynamic object changes the real symbol, we will not see that
2452 change in the corresponding weak symbol. This is the way other
2453 ELF linkers work as well, and seems to be a result of the shared
2456 I will clarify this issue. Most SVR4 shared libraries define the
2457 variable _timezone and define timezone as a weak synonym. The
2458 tzset call changes _timezone. If you write
2459 extern int timezone;
2461 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2462 you might expect that, since timezone is a synonym for _timezone,
2463 the same number will print both times. However, if the processor
2464 backend uses a COPY reloc, then actually timezone will be copied
2465 into your process image, and, since you define _timezone
2466 yourself, _timezone will not. Thus timezone and _timezone will
2467 wind up at different memory locations. The tzset call will set
2468 _timezone, leaving timezone unchanged. */
2470 if (h
->u
.weakdef
!= NULL
)
2472 /* If we get to this point, we know there is an implicit
2473 reference by a regular object file via the weak symbol H.
2474 FIXME: Is this really true? What if the traversal finds
2475 H->U.WEAKDEF before it finds H? */
2476 h
->u
.weakdef
->ref_regular
= 1;
2478 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2482 /* If a symbol has no type and no size and does not require a PLT
2483 entry, then we are probably about to do the wrong thing here: we
2484 are probably going to create a COPY reloc for an empty object.
2485 This case can arise when a shared object is built with assembly
2486 code, and the assembly code fails to set the symbol type. */
2488 && h
->type
== STT_NOTYPE
2490 (*_bfd_error_handler
)
2491 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2492 h
->root
.root
.string
);
2494 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2495 bed
= get_elf_backend_data (dynobj
);
2496 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2505 /* Adjust all external symbols pointing into SEC_MERGE sections
2506 to reflect the object merging within the sections. */
2509 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2513 if (h
->root
.type
== bfd_link_hash_warning
)
2514 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2516 if ((h
->root
.type
== bfd_link_hash_defined
2517 || h
->root
.type
== bfd_link_hash_defweak
)
2518 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2519 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2521 bfd
*output_bfd
= data
;
2523 h
->root
.u
.def
.value
=
2524 _bfd_merged_section_offset (output_bfd
,
2525 &h
->root
.u
.def
.section
,
2526 elf_section_data (sec
)->sec_info
,
2527 h
->root
.u
.def
.value
);
2533 /* Returns false if the symbol referred to by H should be considered
2534 to resolve local to the current module, and true if it should be
2535 considered to bind dynamically. */
2538 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2539 struct bfd_link_info
*info
,
2540 bfd_boolean ignore_protected
)
2542 bfd_boolean binding_stays_local_p
;
2547 while (h
->root
.type
== bfd_link_hash_indirect
2548 || h
->root
.type
== bfd_link_hash_warning
)
2549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2551 /* If it was forced local, then clearly it's not dynamic. */
2552 if (h
->dynindx
== -1)
2554 if (h
->forced_local
)
2557 /* Identify the cases where name binding rules say that a
2558 visible symbol resolves locally. */
2559 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2561 switch (ELF_ST_VISIBILITY (h
->other
))
2568 /* Proper resolution for function pointer equality may require
2569 that these symbols perhaps be resolved dynamically, even though
2570 we should be resolving them to the current module. */
2571 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2572 binding_stays_local_p
= TRUE
;
2579 /* If it isn't defined locally, then clearly it's dynamic. */
2580 if (!h
->def_regular
)
2583 /* Otherwise, the symbol is dynamic if binding rules don't tell
2584 us that it remains local. */
2585 return !binding_stays_local_p
;
2588 /* Return true if the symbol referred to by H should be considered
2589 to resolve local to the current module, and false otherwise. Differs
2590 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2591 undefined symbols and weak symbols. */
2594 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2595 struct bfd_link_info
*info
,
2596 bfd_boolean local_protected
)
2598 /* If it's a local sym, of course we resolve locally. */
2602 /* Common symbols that become definitions don't get the DEF_REGULAR
2603 flag set, so test it first, and don't bail out. */
2604 if (ELF_COMMON_DEF_P (h
))
2606 /* If we don't have a definition in a regular file, then we can't
2607 resolve locally. The sym is either undefined or dynamic. */
2608 else if (!h
->def_regular
)
2611 /* Forced local symbols resolve locally. */
2612 if (h
->forced_local
)
2615 /* As do non-dynamic symbols. */
2616 if (h
->dynindx
== -1)
2619 /* At this point, we know the symbol is defined and dynamic. In an
2620 executable it must resolve locally, likewise when building symbolic
2621 shared libraries. */
2622 if (info
->executable
|| info
->symbolic
)
2625 /* Now deal with defined dynamic symbols in shared libraries. Ones
2626 with default visibility might not resolve locally. */
2627 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2630 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2631 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2634 /* STV_PROTECTED non-function symbols are local. */
2635 if (h
->type
!= STT_FUNC
)
2638 /* Function pointer equality tests may require that STV_PROTECTED
2639 symbols be treated as dynamic symbols, even when we know that the
2640 dynamic linker will resolve them locally. */
2641 return local_protected
;
2644 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2645 aligned. Returns the first TLS output section. */
2647 struct bfd_section
*
2648 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2650 struct bfd_section
*sec
, *tls
;
2651 unsigned int align
= 0;
2653 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2654 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2658 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2659 if (sec
->alignment_power
> align
)
2660 align
= sec
->alignment_power
;
2662 elf_hash_table (info
)->tls_sec
= tls
;
2664 /* Ensure the alignment of the first section is the largest alignment,
2665 so that the tls segment starts aligned. */
2667 tls
->alignment_power
= align
;
2672 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2674 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2675 Elf_Internal_Sym
*sym
)
2677 const struct elf_backend_data
*bed
;
2679 /* Local symbols do not count, but target specific ones might. */
2680 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2681 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2684 /* Function symbols do not count. */
2685 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2688 /* If the section is undefined, then so is the symbol. */
2689 if (sym
->st_shndx
== SHN_UNDEF
)
2692 /* If the symbol is defined in the common section, then
2693 it is a common definition and so does not count. */
2694 bed
= get_elf_backend_data (abfd
);
2695 if (bed
->common_definition (sym
))
2698 /* If the symbol is in a target specific section then we
2699 must rely upon the backend to tell us what it is. */
2700 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2701 /* FIXME - this function is not coded yet:
2703 return _bfd_is_global_symbol_definition (abfd, sym);
2705 Instead for now assume that the definition is not global,
2706 Even if this is wrong, at least the linker will behave
2707 in the same way that it used to do. */
2713 /* Search the symbol table of the archive element of the archive ABFD
2714 whose archive map contains a mention of SYMDEF, and determine if
2715 the symbol is defined in this element. */
2717 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2719 Elf_Internal_Shdr
* hdr
;
2720 bfd_size_type symcount
;
2721 bfd_size_type extsymcount
;
2722 bfd_size_type extsymoff
;
2723 Elf_Internal_Sym
*isymbuf
;
2724 Elf_Internal_Sym
*isym
;
2725 Elf_Internal_Sym
*isymend
;
2728 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2732 if (! bfd_check_format (abfd
, bfd_object
))
2735 /* If we have already included the element containing this symbol in the
2736 link then we do not need to include it again. Just claim that any symbol
2737 it contains is not a definition, so that our caller will not decide to
2738 (re)include this element. */
2739 if (abfd
->archive_pass
)
2742 /* Select the appropriate symbol table. */
2743 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2744 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2746 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2748 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2750 /* The sh_info field of the symtab header tells us where the
2751 external symbols start. We don't care about the local symbols. */
2752 if (elf_bad_symtab (abfd
))
2754 extsymcount
= symcount
;
2759 extsymcount
= symcount
- hdr
->sh_info
;
2760 extsymoff
= hdr
->sh_info
;
2763 if (extsymcount
== 0)
2766 /* Read in the symbol table. */
2767 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2769 if (isymbuf
== NULL
)
2772 /* Scan the symbol table looking for SYMDEF. */
2774 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2778 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2783 if (strcmp (name
, symdef
->name
) == 0)
2785 result
= is_global_data_symbol_definition (abfd
, isym
);
2795 /* Add an entry to the .dynamic table. */
2798 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2802 struct elf_link_hash_table
*hash_table
;
2803 const struct elf_backend_data
*bed
;
2805 bfd_size_type newsize
;
2806 bfd_byte
*newcontents
;
2807 Elf_Internal_Dyn dyn
;
2809 hash_table
= elf_hash_table (info
);
2810 if (! is_elf_hash_table (hash_table
))
2813 bed
= get_elf_backend_data (hash_table
->dynobj
);
2814 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2815 BFD_ASSERT (s
!= NULL
);
2817 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2818 newcontents
= bfd_realloc (s
->contents
, newsize
);
2819 if (newcontents
== NULL
)
2823 dyn
.d_un
.d_val
= val
;
2824 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2827 s
->contents
= newcontents
;
2832 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2833 otherwise just check whether one already exists. Returns -1 on error,
2834 1 if a DT_NEEDED tag already exists, and 0 on success. */
2837 elf_add_dt_needed_tag (bfd
*abfd
,
2838 struct bfd_link_info
*info
,
2842 struct elf_link_hash_table
*hash_table
;
2843 bfd_size_type oldsize
;
2844 bfd_size_type strindex
;
2846 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2849 hash_table
= elf_hash_table (info
);
2850 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2851 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2852 if (strindex
== (bfd_size_type
) -1)
2855 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2858 const struct elf_backend_data
*bed
;
2861 bed
= get_elf_backend_data (hash_table
->dynobj
);
2862 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2864 for (extdyn
= sdyn
->contents
;
2865 extdyn
< sdyn
->contents
+ sdyn
->size
;
2866 extdyn
+= bed
->s
->sizeof_dyn
)
2868 Elf_Internal_Dyn dyn
;
2870 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2871 if (dyn
.d_tag
== DT_NEEDED
2872 && dyn
.d_un
.d_val
== strindex
)
2874 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2882 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2885 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2889 /* We were just checking for existence of the tag. */
2890 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2895 /* Sort symbol by value and section. */
2897 elf_sort_symbol (const void *arg1
, const void *arg2
)
2899 const struct elf_link_hash_entry
*h1
;
2900 const struct elf_link_hash_entry
*h2
;
2901 bfd_signed_vma vdiff
;
2903 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2904 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2905 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2907 return vdiff
> 0 ? 1 : -1;
2910 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2912 return sdiff
> 0 ? 1 : -1;
2917 /* This function is used to adjust offsets into .dynstr for
2918 dynamic symbols. This is called via elf_link_hash_traverse. */
2921 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2923 struct elf_strtab_hash
*dynstr
= data
;
2925 if (h
->root
.type
== bfd_link_hash_warning
)
2926 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2928 if (h
->dynindx
!= -1)
2929 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2933 /* Assign string offsets in .dynstr, update all structures referencing
2937 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2939 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2940 struct elf_link_local_dynamic_entry
*entry
;
2941 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2942 bfd
*dynobj
= hash_table
->dynobj
;
2945 const struct elf_backend_data
*bed
;
2948 _bfd_elf_strtab_finalize (dynstr
);
2949 size
= _bfd_elf_strtab_size (dynstr
);
2951 bed
= get_elf_backend_data (dynobj
);
2952 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2953 BFD_ASSERT (sdyn
!= NULL
);
2955 /* Update all .dynamic entries referencing .dynstr strings. */
2956 for (extdyn
= sdyn
->contents
;
2957 extdyn
< sdyn
->contents
+ sdyn
->size
;
2958 extdyn
+= bed
->s
->sizeof_dyn
)
2960 Elf_Internal_Dyn dyn
;
2962 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2966 dyn
.d_un
.d_val
= size
;
2974 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2979 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2982 /* Now update local dynamic symbols. */
2983 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2984 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2985 entry
->isym
.st_name
);
2987 /* And the rest of dynamic symbols. */
2988 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2990 /* Adjust version definitions. */
2991 if (elf_tdata (output_bfd
)->cverdefs
)
2996 Elf_Internal_Verdef def
;
2997 Elf_Internal_Verdaux defaux
;
2999 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3003 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3005 p
+= sizeof (Elf_External_Verdef
);
3006 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3008 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3010 _bfd_elf_swap_verdaux_in (output_bfd
,
3011 (Elf_External_Verdaux
*) p
, &defaux
);
3012 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3014 _bfd_elf_swap_verdaux_out (output_bfd
,
3015 &defaux
, (Elf_External_Verdaux
*) p
);
3016 p
+= sizeof (Elf_External_Verdaux
);
3019 while (def
.vd_next
);
3022 /* Adjust version references. */
3023 if (elf_tdata (output_bfd
)->verref
)
3028 Elf_Internal_Verneed need
;
3029 Elf_Internal_Vernaux needaux
;
3031 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3035 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3037 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3038 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3039 (Elf_External_Verneed
*) p
);
3040 p
+= sizeof (Elf_External_Verneed
);
3041 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3043 _bfd_elf_swap_vernaux_in (output_bfd
,
3044 (Elf_External_Vernaux
*) p
, &needaux
);
3045 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3047 _bfd_elf_swap_vernaux_out (output_bfd
,
3049 (Elf_External_Vernaux
*) p
);
3050 p
+= sizeof (Elf_External_Vernaux
);
3053 while (need
.vn_next
);
3059 /* Add symbols from an ELF object file to the linker hash table. */
3062 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3064 Elf_Internal_Shdr
*hdr
;
3065 bfd_size_type symcount
;
3066 bfd_size_type extsymcount
;
3067 bfd_size_type extsymoff
;
3068 struct elf_link_hash_entry
**sym_hash
;
3069 bfd_boolean dynamic
;
3070 Elf_External_Versym
*extversym
= NULL
;
3071 Elf_External_Versym
*ever
;
3072 struct elf_link_hash_entry
*weaks
;
3073 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3074 bfd_size_type nondeflt_vers_cnt
= 0;
3075 Elf_Internal_Sym
*isymbuf
= NULL
;
3076 Elf_Internal_Sym
*isym
;
3077 Elf_Internal_Sym
*isymend
;
3078 const struct elf_backend_data
*bed
;
3079 bfd_boolean add_needed
;
3080 struct elf_link_hash_table
*htab
;
3082 void *alloc_mark
= NULL
;
3083 struct bfd_hash_entry
**old_table
= NULL
;
3084 unsigned int old_size
= 0;
3085 unsigned int old_count
= 0;
3086 void *old_tab
= NULL
;
3089 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3090 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3091 long old_dynsymcount
= 0;
3093 size_t hashsize
= 0;
3095 htab
= elf_hash_table (info
);
3096 bed
= get_elf_backend_data (abfd
);
3098 if ((abfd
->flags
& DYNAMIC
) == 0)
3104 /* You can't use -r against a dynamic object. Also, there's no
3105 hope of using a dynamic object which does not exactly match
3106 the format of the output file. */
3107 if (info
->relocatable
3108 || !is_elf_hash_table (htab
)
3109 || htab
->root
.creator
!= abfd
->xvec
)
3111 if (info
->relocatable
)
3112 bfd_set_error (bfd_error_invalid_operation
);
3114 bfd_set_error (bfd_error_wrong_format
);
3119 /* As a GNU extension, any input sections which are named
3120 .gnu.warning.SYMBOL are treated as warning symbols for the given
3121 symbol. This differs from .gnu.warning sections, which generate
3122 warnings when they are included in an output file. */
3123 if (info
->executable
)
3127 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3131 name
= bfd_get_section_name (abfd
, s
);
3132 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3137 name
+= sizeof ".gnu.warning." - 1;
3139 /* If this is a shared object, then look up the symbol
3140 in the hash table. If it is there, and it is already
3141 been defined, then we will not be using the entry
3142 from this shared object, so we don't need to warn.
3143 FIXME: If we see the definition in a regular object
3144 later on, we will warn, but we shouldn't. The only
3145 fix is to keep track of what warnings we are supposed
3146 to emit, and then handle them all at the end of the
3150 struct elf_link_hash_entry
*h
;
3152 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3154 /* FIXME: What about bfd_link_hash_common? */
3156 && (h
->root
.type
== bfd_link_hash_defined
3157 || h
->root
.type
== bfd_link_hash_defweak
))
3159 /* We don't want to issue this warning. Clobber
3160 the section size so that the warning does not
3161 get copied into the output file. */
3168 msg
= bfd_alloc (abfd
, sz
+ 1);
3172 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3177 if (! (_bfd_generic_link_add_one_symbol
3178 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3179 FALSE
, bed
->collect
, NULL
)))
3182 if (! info
->relocatable
)
3184 /* Clobber the section size so that the warning does
3185 not get copied into the output file. */
3188 /* Also set SEC_EXCLUDE, so that symbols defined in
3189 the warning section don't get copied to the output. */
3190 s
->flags
|= SEC_EXCLUDE
;
3199 /* If we are creating a shared library, create all the dynamic
3200 sections immediately. We need to attach them to something,
3201 so we attach them to this BFD, provided it is the right
3202 format. FIXME: If there are no input BFD's of the same
3203 format as the output, we can't make a shared library. */
3205 && is_elf_hash_table (htab
)
3206 && htab
->root
.creator
== abfd
->xvec
3207 && !htab
->dynamic_sections_created
)
3209 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3213 else if (!is_elf_hash_table (htab
))
3218 const char *soname
= NULL
;
3219 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3222 /* ld --just-symbols and dynamic objects don't mix very well.
3223 ld shouldn't allow it. */
3224 if ((s
= abfd
->sections
) != NULL
3225 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3228 /* If this dynamic lib was specified on the command line with
3229 --as-needed in effect, then we don't want to add a DT_NEEDED
3230 tag unless the lib is actually used. Similary for libs brought
3231 in by another lib's DT_NEEDED. When --no-add-needed is used
3232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3233 any dynamic library in DT_NEEDED tags in the dynamic lib at
3235 add_needed
= (elf_dyn_lib_class (abfd
)
3236 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3237 | DYN_NO_NEEDED
)) == 0;
3239 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3245 unsigned long shlink
;
3247 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3248 goto error_free_dyn
;
3250 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3252 goto error_free_dyn
;
3253 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3255 for (extdyn
= dynbuf
;
3256 extdyn
< dynbuf
+ s
->size
;
3257 extdyn
+= bed
->s
->sizeof_dyn
)
3259 Elf_Internal_Dyn dyn
;
3261 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3262 if (dyn
.d_tag
== DT_SONAME
)
3264 unsigned int tagv
= dyn
.d_un
.d_val
;
3265 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3267 goto error_free_dyn
;
3269 if (dyn
.d_tag
== DT_NEEDED
)
3271 struct bfd_link_needed_list
*n
, **pn
;
3273 unsigned int tagv
= dyn
.d_un
.d_val
;
3275 amt
= sizeof (struct bfd_link_needed_list
);
3276 n
= bfd_alloc (abfd
, amt
);
3277 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3278 if (n
== NULL
|| fnm
== NULL
)
3279 goto error_free_dyn
;
3280 amt
= strlen (fnm
) + 1;
3281 anm
= bfd_alloc (abfd
, amt
);
3283 goto error_free_dyn
;
3284 memcpy (anm
, fnm
, amt
);
3288 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3292 if (dyn
.d_tag
== DT_RUNPATH
)
3294 struct bfd_link_needed_list
*n
, **pn
;
3296 unsigned int tagv
= dyn
.d_un
.d_val
;
3298 amt
= sizeof (struct bfd_link_needed_list
);
3299 n
= bfd_alloc (abfd
, amt
);
3300 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3301 if (n
== NULL
|| fnm
== NULL
)
3302 goto error_free_dyn
;
3303 amt
= strlen (fnm
) + 1;
3304 anm
= bfd_alloc (abfd
, amt
);
3306 goto error_free_dyn
;
3307 memcpy (anm
, fnm
, amt
);
3311 for (pn
= & runpath
;
3317 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3318 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3320 struct bfd_link_needed_list
*n
, **pn
;
3322 unsigned int tagv
= dyn
.d_un
.d_val
;
3324 amt
= sizeof (struct bfd_link_needed_list
);
3325 n
= bfd_alloc (abfd
, amt
);
3326 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3327 if (n
== NULL
|| fnm
== NULL
)
3328 goto error_free_dyn
;
3329 amt
= strlen (fnm
) + 1;
3330 anm
= bfd_alloc (abfd
, amt
);
3337 memcpy (anm
, fnm
, amt
);
3352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3353 frees all more recently bfd_alloc'd blocks as well. */
3359 struct bfd_link_needed_list
**pn
;
3360 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3365 /* We do not want to include any of the sections in a dynamic
3366 object in the output file. We hack by simply clobbering the
3367 list of sections in the BFD. This could be handled more
3368 cleanly by, say, a new section flag; the existing
3369 SEC_NEVER_LOAD flag is not the one we want, because that one
3370 still implies that the section takes up space in the output
3372 bfd_section_list_clear (abfd
);
3374 /* Find the name to use in a DT_NEEDED entry that refers to this
3375 object. If the object has a DT_SONAME entry, we use it.
3376 Otherwise, if the generic linker stuck something in
3377 elf_dt_name, we use that. Otherwise, we just use the file
3379 if (soname
== NULL
|| *soname
== '\0')
3381 soname
= elf_dt_name (abfd
);
3382 if (soname
== NULL
|| *soname
== '\0')
3383 soname
= bfd_get_filename (abfd
);
3386 /* Save the SONAME because sometimes the linker emulation code
3387 will need to know it. */
3388 elf_dt_name (abfd
) = soname
;
3390 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3394 /* If we have already included this dynamic object in the
3395 link, just ignore it. There is no reason to include a
3396 particular dynamic object more than once. */
3401 /* If this is a dynamic object, we always link against the .dynsym
3402 symbol table, not the .symtab symbol table. The dynamic linker
3403 will only see the .dynsym symbol table, so there is no reason to
3404 look at .symtab for a dynamic object. */
3406 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3407 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3409 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3411 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3413 /* The sh_info field of the symtab header tells us where the
3414 external symbols start. We don't care about the local symbols at
3416 if (elf_bad_symtab (abfd
))
3418 extsymcount
= symcount
;
3423 extsymcount
= symcount
- hdr
->sh_info
;
3424 extsymoff
= hdr
->sh_info
;
3428 if (extsymcount
!= 0)
3430 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3432 if (isymbuf
== NULL
)
3435 /* We store a pointer to the hash table entry for each external
3437 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3438 sym_hash
= bfd_alloc (abfd
, amt
);
3439 if (sym_hash
== NULL
)
3440 goto error_free_sym
;
3441 elf_sym_hashes (abfd
) = sym_hash
;
3446 /* Read in any version definitions. */
3447 if (!_bfd_elf_slurp_version_tables (abfd
,
3448 info
->default_imported_symver
))
3449 goto error_free_sym
;
3451 /* Read in the symbol versions, but don't bother to convert them
3452 to internal format. */
3453 if (elf_dynversym (abfd
) != 0)
3455 Elf_Internal_Shdr
*versymhdr
;
3457 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3458 extversym
= bfd_malloc (versymhdr
->sh_size
);
3459 if (extversym
== NULL
)
3460 goto error_free_sym
;
3461 amt
= versymhdr
->sh_size
;
3462 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3463 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3464 goto error_free_vers
;
3468 /* If we are loading an as-needed shared lib, save the symbol table
3469 state before we start adding symbols. If the lib turns out
3470 to be unneeded, restore the state. */
3471 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3476 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3478 struct bfd_hash_entry
*p
;
3479 struct elf_link_hash_entry
*h
;
3481 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3483 h
= (struct elf_link_hash_entry
*) p
;
3484 entsize
+= htab
->root
.table
.entsize
;
3485 if (h
->root
.type
== bfd_link_hash_warning
)
3486 entsize
+= htab
->root
.table
.entsize
;
3490 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3491 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3492 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3493 if (old_tab
== NULL
)
3494 goto error_free_vers
;
3496 /* Remember the current objalloc pointer, so that all mem for
3497 symbols added can later be reclaimed. */
3498 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3499 if (alloc_mark
== NULL
)
3500 goto error_free_vers
;
3502 /* Clone the symbol table and sym hashes. Remember some
3503 pointers into the symbol table, and dynamic symbol count. */
3504 old_hash
= (char *) old_tab
+ tabsize
;
3505 old_ent
= (char *) old_hash
+ hashsize
;
3506 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3507 memcpy (old_hash
, sym_hash
, hashsize
);
3508 old_undefs
= htab
->root
.undefs
;
3509 old_undefs_tail
= htab
->root
.undefs_tail
;
3510 old_table
= htab
->root
.table
.table
;
3511 old_size
= htab
->root
.table
.size
;
3512 old_count
= htab
->root
.table
.count
;
3513 old_dynsymcount
= htab
->dynsymcount
;
3515 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3517 struct bfd_hash_entry
*p
;
3518 struct elf_link_hash_entry
*h
;
3520 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3522 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3523 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3524 h
= (struct elf_link_hash_entry
*) p
;
3525 if (h
->root
.type
== bfd_link_hash_warning
)
3527 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3528 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3535 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3536 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3538 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3542 asection
*sec
, *new_sec
;
3545 struct elf_link_hash_entry
*h
;
3546 bfd_boolean definition
;
3547 bfd_boolean size_change_ok
;
3548 bfd_boolean type_change_ok
;
3549 bfd_boolean new_weakdef
;
3550 bfd_boolean override
;
3552 unsigned int old_alignment
;
3557 flags
= BSF_NO_FLAGS
;
3559 value
= isym
->st_value
;
3561 common
= bed
->common_definition (isym
);
3563 bind
= ELF_ST_BIND (isym
->st_info
);
3564 if (bind
== STB_LOCAL
)
3566 /* This should be impossible, since ELF requires that all
3567 global symbols follow all local symbols, and that sh_info
3568 point to the first global symbol. Unfortunately, Irix 5
3572 else if (bind
== STB_GLOBAL
)
3574 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3577 else if (bind
== STB_WEAK
)
3581 /* Leave it up to the processor backend. */
3584 if (isym
->st_shndx
== SHN_UNDEF
)
3585 sec
= bfd_und_section_ptr
;
3586 else if (isym
->st_shndx
< SHN_LORESERVE
3587 || isym
->st_shndx
> SHN_HIRESERVE
)
3589 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3591 sec
= bfd_abs_section_ptr
;
3592 else if (sec
->kept_section
)
3594 /* Symbols from discarded section are undefined, and have
3595 default visibility. */
3596 sec
= bfd_und_section_ptr
;
3597 isym
->st_shndx
= SHN_UNDEF
;
3598 isym
->st_other
= (STV_DEFAULT
3599 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3601 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3604 else if (isym
->st_shndx
== SHN_ABS
)
3605 sec
= bfd_abs_section_ptr
;
3606 else if (isym
->st_shndx
== SHN_COMMON
)
3608 sec
= bfd_com_section_ptr
;
3609 /* What ELF calls the size we call the value. What ELF
3610 calls the value we call the alignment. */
3611 value
= isym
->st_size
;
3615 /* Leave it up to the processor backend. */
3618 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3621 goto error_free_vers
;
3623 if (isym
->st_shndx
== SHN_COMMON
3624 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3625 && !info
->relocatable
)
3627 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3631 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3634 | SEC_LINKER_CREATED
3635 | SEC_THREAD_LOCAL
));
3637 goto error_free_vers
;
3641 else if (bed
->elf_add_symbol_hook
)
3643 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3645 goto error_free_vers
;
3647 /* The hook function sets the name to NULL if this symbol
3648 should be skipped for some reason. */
3653 /* Sanity check that all possibilities were handled. */
3656 bfd_set_error (bfd_error_bad_value
);
3657 goto error_free_vers
;
3660 if (bfd_is_und_section (sec
)
3661 || bfd_is_com_section (sec
))
3666 size_change_ok
= FALSE
;
3667 type_change_ok
= bed
->type_change_ok
;
3672 if (is_elf_hash_table (htab
))
3674 Elf_Internal_Versym iver
;
3675 unsigned int vernum
= 0;
3680 if (info
->default_imported_symver
)
3681 /* Use the default symbol version created earlier. */
3682 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3687 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3689 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3691 /* If this is a hidden symbol, or if it is not version
3692 1, we append the version name to the symbol name.
3693 However, we do not modify a non-hidden absolute symbol
3694 if it is not a function, because it might be the version
3695 symbol itself. FIXME: What if it isn't? */
3696 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3697 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3698 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3701 size_t namelen
, verlen
, newlen
;
3704 if (isym
->st_shndx
!= SHN_UNDEF
)
3706 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3708 else if (vernum
> 1)
3710 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3716 (*_bfd_error_handler
)
3717 (_("%B: %s: invalid version %u (max %d)"),
3719 elf_tdata (abfd
)->cverdefs
);
3720 bfd_set_error (bfd_error_bad_value
);
3721 goto error_free_vers
;
3726 /* We cannot simply test for the number of
3727 entries in the VERNEED section since the
3728 numbers for the needed versions do not start
3730 Elf_Internal_Verneed
*t
;
3733 for (t
= elf_tdata (abfd
)->verref
;
3737 Elf_Internal_Vernaux
*a
;
3739 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3741 if (a
->vna_other
== vernum
)
3743 verstr
= a
->vna_nodename
;
3752 (*_bfd_error_handler
)
3753 (_("%B: %s: invalid needed version %d"),
3754 abfd
, name
, vernum
);
3755 bfd_set_error (bfd_error_bad_value
);
3756 goto error_free_vers
;
3760 namelen
= strlen (name
);
3761 verlen
= strlen (verstr
);
3762 newlen
= namelen
+ verlen
+ 2;
3763 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3764 && isym
->st_shndx
!= SHN_UNDEF
)
3767 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3768 if (newname
== NULL
)
3769 goto error_free_vers
;
3770 memcpy (newname
, name
, namelen
);
3771 p
= newname
+ namelen
;
3773 /* If this is a defined non-hidden version symbol,
3774 we add another @ to the name. This indicates the
3775 default version of the symbol. */
3776 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3777 && isym
->st_shndx
!= SHN_UNDEF
)
3779 memcpy (p
, verstr
, verlen
+ 1);
3784 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3785 &value
, &old_alignment
,
3786 sym_hash
, &skip
, &override
,
3787 &type_change_ok
, &size_change_ok
))
3788 goto error_free_vers
;
3797 while (h
->root
.type
== bfd_link_hash_indirect
3798 || h
->root
.type
== bfd_link_hash_warning
)
3799 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3801 /* Remember the old alignment if this is a common symbol, so
3802 that we don't reduce the alignment later on. We can't
3803 check later, because _bfd_generic_link_add_one_symbol
3804 will set a default for the alignment which we want to
3805 override. We also remember the old bfd where the existing
3806 definition comes from. */
3807 switch (h
->root
.type
)
3812 case bfd_link_hash_defined
:
3813 case bfd_link_hash_defweak
:
3814 old_bfd
= h
->root
.u
.def
.section
->owner
;
3817 case bfd_link_hash_common
:
3818 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3819 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3823 if (elf_tdata (abfd
)->verdef
!= NULL
3827 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3830 if (! (_bfd_generic_link_add_one_symbol
3831 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3832 (struct bfd_link_hash_entry
**) sym_hash
)))
3833 goto error_free_vers
;
3836 while (h
->root
.type
== bfd_link_hash_indirect
3837 || h
->root
.type
== bfd_link_hash_warning
)
3838 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3841 new_weakdef
= FALSE
;
3844 && (flags
& BSF_WEAK
) != 0
3845 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3846 && is_elf_hash_table (htab
)
3847 && h
->u
.weakdef
== NULL
)
3849 /* Keep a list of all weak defined non function symbols from
3850 a dynamic object, using the weakdef field. Later in this
3851 function we will set the weakdef field to the correct
3852 value. We only put non-function symbols from dynamic
3853 objects on this list, because that happens to be the only
3854 time we need to know the normal symbol corresponding to a
3855 weak symbol, and the information is time consuming to
3856 figure out. If the weakdef field is not already NULL,
3857 then this symbol was already defined by some previous
3858 dynamic object, and we will be using that previous
3859 definition anyhow. */
3861 h
->u
.weakdef
= weaks
;
3866 /* Set the alignment of a common symbol. */
3867 if ((common
|| bfd_is_com_section (sec
))
3868 && h
->root
.type
== bfd_link_hash_common
)
3873 align
= bfd_log2 (isym
->st_value
);
3876 /* The new symbol is a common symbol in a shared object.
3877 We need to get the alignment from the section. */
3878 align
= new_sec
->alignment_power
;
3880 if (align
> old_alignment
3881 /* Permit an alignment power of zero if an alignment of one
3882 is specified and no other alignments have been specified. */
3883 || (isym
->st_value
== 1 && old_alignment
== 0))
3884 h
->root
.u
.c
.p
->alignment_power
= align
;
3886 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3889 if (is_elf_hash_table (htab
))
3893 /* Check the alignment when a common symbol is involved. This
3894 can change when a common symbol is overridden by a normal
3895 definition or a common symbol is ignored due to the old
3896 normal definition. We need to make sure the maximum
3897 alignment is maintained. */
3898 if ((old_alignment
|| common
)
3899 && h
->root
.type
!= bfd_link_hash_common
)
3901 unsigned int common_align
;
3902 unsigned int normal_align
;
3903 unsigned int symbol_align
;
3907 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3908 if (h
->root
.u
.def
.section
->owner
!= NULL
3909 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3911 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3912 if (normal_align
> symbol_align
)
3913 normal_align
= symbol_align
;
3916 normal_align
= symbol_align
;
3920 common_align
= old_alignment
;
3921 common_bfd
= old_bfd
;
3926 common_align
= bfd_log2 (isym
->st_value
);
3928 normal_bfd
= old_bfd
;
3931 if (normal_align
< common_align
)
3933 /* PR binutils/2735 */
3934 if (normal_bfd
== NULL
)
3935 (*_bfd_error_handler
)
3936 (_("Warning: alignment %u of common symbol `%s' in %B"
3937 " is greater than the alignment (%u) of its section %A"),
3938 common_bfd
, h
->root
.u
.def
.section
,
3939 1 << common_align
, name
, 1 << normal_align
);
3941 (*_bfd_error_handler
)
3942 (_("Warning: alignment %u of symbol `%s' in %B"
3943 " is smaller than %u in %B"),
3944 normal_bfd
, common_bfd
,
3945 1 << normal_align
, name
, 1 << common_align
);
3949 /* Remember the symbol size and type. */
3950 if (isym
->st_size
!= 0
3951 && (definition
|| h
->size
== 0))
3953 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3954 (*_bfd_error_handler
)
3955 (_("Warning: size of symbol `%s' changed"
3956 " from %lu in %B to %lu in %B"),
3958 name
, (unsigned long) h
->size
,
3959 (unsigned long) isym
->st_size
);
3961 h
->size
= isym
->st_size
;
3964 /* If this is a common symbol, then we always want H->SIZE
3965 to be the size of the common symbol. The code just above
3966 won't fix the size if a common symbol becomes larger. We
3967 don't warn about a size change here, because that is
3968 covered by --warn-common. */
3969 if (h
->root
.type
== bfd_link_hash_common
)
3970 h
->size
= h
->root
.u
.c
.size
;
3972 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3973 && (definition
|| h
->type
== STT_NOTYPE
))
3975 if (h
->type
!= STT_NOTYPE
3976 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3977 && ! type_change_ok
)
3978 (*_bfd_error_handler
)
3979 (_("Warning: type of symbol `%s' changed"
3980 " from %d to %d in %B"),
3981 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3983 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3986 /* If st_other has a processor-specific meaning, specific
3987 code might be needed here. We never merge the visibility
3988 attribute with the one from a dynamic object. */
3989 if (bed
->elf_backend_merge_symbol_attribute
)
3990 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3993 /* If this symbol has default visibility and the user has requested
3994 we not re-export it, then mark it as hidden. */
3995 if (definition
&& !dynamic
3997 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3998 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3999 isym
->st_other
= (STV_HIDDEN
4000 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4002 if (isym
->st_other
!= 0 && !dynamic
)
4004 unsigned char hvis
, symvis
, other
, nvis
;
4006 /* Take the balance of OTHER from the definition. */
4007 other
= (definition
? isym
->st_other
: h
->other
);
4008 other
&= ~ ELF_ST_VISIBILITY (-1);
4010 /* Combine visibilities, using the most constraining one. */
4011 hvis
= ELF_ST_VISIBILITY (h
->other
);
4012 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4018 nvis
= hvis
< symvis
? hvis
: symvis
;
4020 h
->other
= other
| nvis
;
4023 /* Set a flag in the hash table entry indicating the type of
4024 reference or definition we just found. Keep a count of
4025 the number of dynamic symbols we find. A dynamic symbol
4026 is one which is referenced or defined by both a regular
4027 object and a shared object. */
4034 if (bind
!= STB_WEAK
)
4035 h
->ref_regular_nonweak
= 1;
4039 if (! info
->executable
4052 || (h
->u
.weakdef
!= NULL
4054 && h
->u
.weakdef
->dynindx
!= -1))
4058 /* Check to see if we need to add an indirect symbol for
4059 the default name. */
4060 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4061 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4062 &sec
, &value
, &dynsym
,
4064 goto error_free_vers
;
4066 if (definition
&& !dynamic
)
4068 char *p
= strchr (name
, ELF_VER_CHR
);
4069 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4071 /* Queue non-default versions so that .symver x, x@FOO
4072 aliases can be checked. */
4075 amt
= ((isymend
- isym
+ 1)
4076 * sizeof (struct elf_link_hash_entry
*));
4077 nondeflt_vers
= bfd_malloc (amt
);
4079 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4083 if (dynsym
&& h
->dynindx
== -1)
4085 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4086 goto error_free_vers
;
4087 if (h
->u
.weakdef
!= NULL
4089 && h
->u
.weakdef
->dynindx
== -1)
4091 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4092 goto error_free_vers
;
4095 else if (dynsym
&& h
->dynindx
!= -1)
4096 /* If the symbol already has a dynamic index, but
4097 visibility says it should not be visible, turn it into
4099 switch (ELF_ST_VISIBILITY (h
->other
))
4103 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4114 const char *soname
= elf_dt_name (abfd
);
4116 /* A symbol from a library loaded via DT_NEEDED of some
4117 other library is referenced by a regular object.
4118 Add a DT_NEEDED entry for it. Issue an error if
4119 --no-add-needed is used. */
4120 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4122 (*_bfd_error_handler
)
4123 (_("%s: invalid DSO for symbol `%s' definition"),
4125 bfd_set_error (bfd_error_bad_value
);
4126 goto error_free_vers
;
4129 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4132 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4134 goto error_free_vers
;
4136 BFD_ASSERT (ret
== 0);
4141 if (extversym
!= NULL
)
4147 if (isymbuf
!= NULL
)
4153 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4157 /* Restore the symbol table. */
4158 old_hash
= (char *) old_tab
+ tabsize
;
4159 old_ent
= (char *) old_hash
+ hashsize
;
4160 sym_hash
= elf_sym_hashes (abfd
);
4161 htab
->root
.table
.table
= old_table
;
4162 htab
->root
.table
.size
= old_size
;
4163 htab
->root
.table
.count
= old_count
;
4164 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4165 memcpy (sym_hash
, old_hash
, hashsize
);
4166 htab
->root
.undefs
= old_undefs
;
4167 htab
->root
.undefs_tail
= old_undefs_tail
;
4168 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4170 struct bfd_hash_entry
*p
;
4171 struct elf_link_hash_entry
*h
;
4173 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4175 h
= (struct elf_link_hash_entry
*) p
;
4176 if (h
->root
.type
== bfd_link_hash_warning
)
4177 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4178 if (h
->dynindx
>= old_dynsymcount
)
4179 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4181 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4182 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4183 h
= (struct elf_link_hash_entry
*) p
;
4184 if (h
->root
.type
== bfd_link_hash_warning
)
4186 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4187 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4193 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4195 if (nondeflt_vers
!= NULL
)
4196 free (nondeflt_vers
);
4200 if (old_tab
!= NULL
)
4206 /* Now that all the symbols from this input file are created, handle
4207 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4208 if (nondeflt_vers
!= NULL
)
4210 bfd_size_type cnt
, symidx
;
4212 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4214 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4215 char *shortname
, *p
;
4217 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4219 || (h
->root
.type
!= bfd_link_hash_defined
4220 && h
->root
.type
!= bfd_link_hash_defweak
))
4223 amt
= p
- h
->root
.root
.string
;
4224 shortname
= bfd_malloc (amt
+ 1);
4225 memcpy (shortname
, h
->root
.root
.string
, amt
);
4226 shortname
[amt
] = '\0';
4228 hi
= (struct elf_link_hash_entry
*)
4229 bfd_link_hash_lookup (&htab
->root
, shortname
,
4230 FALSE
, FALSE
, FALSE
);
4232 && hi
->root
.type
== h
->root
.type
4233 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4234 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4236 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4237 hi
->root
.type
= bfd_link_hash_indirect
;
4238 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4239 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4240 sym_hash
= elf_sym_hashes (abfd
);
4242 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4243 if (sym_hash
[symidx
] == hi
)
4245 sym_hash
[symidx
] = h
;
4251 free (nondeflt_vers
);
4252 nondeflt_vers
= NULL
;
4255 /* Now set the weakdefs field correctly for all the weak defined
4256 symbols we found. The only way to do this is to search all the
4257 symbols. Since we only need the information for non functions in
4258 dynamic objects, that's the only time we actually put anything on
4259 the list WEAKS. We need this information so that if a regular
4260 object refers to a symbol defined weakly in a dynamic object, the
4261 real symbol in the dynamic object is also put in the dynamic
4262 symbols; we also must arrange for both symbols to point to the
4263 same memory location. We could handle the general case of symbol
4264 aliasing, but a general symbol alias can only be generated in
4265 assembler code, handling it correctly would be very time
4266 consuming, and other ELF linkers don't handle general aliasing
4270 struct elf_link_hash_entry
**hpp
;
4271 struct elf_link_hash_entry
**hppend
;
4272 struct elf_link_hash_entry
**sorted_sym_hash
;
4273 struct elf_link_hash_entry
*h
;
4276 /* Since we have to search the whole symbol list for each weak
4277 defined symbol, search time for N weak defined symbols will be
4278 O(N^2). Binary search will cut it down to O(NlogN). */
4279 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4280 sorted_sym_hash
= bfd_malloc (amt
);
4281 if (sorted_sym_hash
== NULL
)
4283 sym_hash
= sorted_sym_hash
;
4284 hpp
= elf_sym_hashes (abfd
);
4285 hppend
= hpp
+ extsymcount
;
4287 for (; hpp
< hppend
; hpp
++)
4291 && h
->root
.type
== bfd_link_hash_defined
4292 && h
->type
!= STT_FUNC
)
4300 qsort (sorted_sym_hash
, sym_count
,
4301 sizeof (struct elf_link_hash_entry
*),
4304 while (weaks
!= NULL
)
4306 struct elf_link_hash_entry
*hlook
;
4313 weaks
= hlook
->u
.weakdef
;
4314 hlook
->u
.weakdef
= NULL
;
4316 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4317 || hlook
->root
.type
== bfd_link_hash_defweak
4318 || hlook
->root
.type
== bfd_link_hash_common
4319 || hlook
->root
.type
== bfd_link_hash_indirect
);
4320 slook
= hlook
->root
.u
.def
.section
;
4321 vlook
= hlook
->root
.u
.def
.value
;
4328 bfd_signed_vma vdiff
;
4330 h
= sorted_sym_hash
[idx
];
4331 vdiff
= vlook
- h
->root
.u
.def
.value
;
4338 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4351 /* We didn't find a value/section match. */
4355 for (i
= ilook
; i
< sym_count
; i
++)
4357 h
= sorted_sym_hash
[i
];
4359 /* Stop if value or section doesn't match. */
4360 if (h
->root
.u
.def
.value
!= vlook
4361 || h
->root
.u
.def
.section
!= slook
)
4363 else if (h
!= hlook
)
4365 hlook
->u
.weakdef
= h
;
4367 /* If the weak definition is in the list of dynamic
4368 symbols, make sure the real definition is put
4370 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4372 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4376 /* If the real definition is in the list of dynamic
4377 symbols, make sure the weak definition is put
4378 there as well. If we don't do this, then the
4379 dynamic loader might not merge the entries for the
4380 real definition and the weak definition. */
4381 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4383 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4391 free (sorted_sym_hash
);
4394 if (bed
->check_directives
)
4395 (*bed
->check_directives
) (abfd
, info
);
4397 /* If this object is the same format as the output object, and it is
4398 not a shared library, then let the backend look through the
4401 This is required to build global offset table entries and to
4402 arrange for dynamic relocs. It is not required for the
4403 particular common case of linking non PIC code, even when linking
4404 against shared libraries, but unfortunately there is no way of
4405 knowing whether an object file has been compiled PIC or not.
4406 Looking through the relocs is not particularly time consuming.
4407 The problem is that we must either (1) keep the relocs in memory,
4408 which causes the linker to require additional runtime memory or
4409 (2) read the relocs twice from the input file, which wastes time.
4410 This would be a good case for using mmap.
4412 I have no idea how to handle linking PIC code into a file of a
4413 different format. It probably can't be done. */
4415 && is_elf_hash_table (htab
)
4416 && htab
->root
.creator
== abfd
->xvec
4417 && bed
->check_relocs
!= NULL
)
4421 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4423 Elf_Internal_Rela
*internal_relocs
;
4426 if ((o
->flags
& SEC_RELOC
) == 0
4427 || o
->reloc_count
== 0
4428 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4429 && (o
->flags
& SEC_DEBUGGING
) != 0)
4430 || bfd_is_abs_section (o
->output_section
))
4433 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4435 if (internal_relocs
== NULL
)
4438 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4440 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4441 free (internal_relocs
);
4448 /* If this is a non-traditional link, try to optimize the handling
4449 of the .stab/.stabstr sections. */
4451 && ! info
->traditional_format
4452 && is_elf_hash_table (htab
)
4453 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4457 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4458 if (stabstr
!= NULL
)
4460 bfd_size_type string_offset
= 0;
4463 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4464 if (strncmp (".stab", stab
->name
, 5) == 0
4465 && (!stab
->name
[5] ||
4466 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4467 && (stab
->flags
& SEC_MERGE
) == 0
4468 && !bfd_is_abs_section (stab
->output_section
))
4470 struct bfd_elf_section_data
*secdata
;
4472 secdata
= elf_section_data (stab
);
4473 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4474 stabstr
, &secdata
->sec_info
,
4477 if (secdata
->sec_info
)
4478 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4483 if (is_elf_hash_table (htab
) && add_needed
)
4485 /* Add this bfd to the loaded list. */
4486 struct elf_link_loaded_list
*n
;
4488 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4492 n
->next
= htab
->loaded
;
4499 if (old_tab
!= NULL
)
4501 if (nondeflt_vers
!= NULL
)
4502 free (nondeflt_vers
);
4503 if (extversym
!= NULL
)
4506 if (isymbuf
!= NULL
)
4512 /* Return the linker hash table entry of a symbol that might be
4513 satisfied by an archive symbol. Return -1 on error. */
4515 struct elf_link_hash_entry
*
4516 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4517 struct bfd_link_info
*info
,
4520 struct elf_link_hash_entry
*h
;
4524 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4528 /* If this is a default version (the name contains @@), look up the
4529 symbol again with only one `@' as well as without the version.
4530 The effect is that references to the symbol with and without the
4531 version will be matched by the default symbol in the archive. */
4533 p
= strchr (name
, ELF_VER_CHR
);
4534 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4537 /* First check with only one `@'. */
4538 len
= strlen (name
);
4539 copy
= bfd_alloc (abfd
, len
);
4541 return (struct elf_link_hash_entry
*) 0 - 1;
4543 first
= p
- name
+ 1;
4544 memcpy (copy
, name
, first
);
4545 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4547 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4550 /* We also need to check references to the symbol without the
4552 copy
[first
- 1] = '\0';
4553 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4554 FALSE
, FALSE
, FALSE
);
4557 bfd_release (abfd
, copy
);
4561 /* Add symbols from an ELF archive file to the linker hash table. We
4562 don't use _bfd_generic_link_add_archive_symbols because of a
4563 problem which arises on UnixWare. The UnixWare libc.so is an
4564 archive which includes an entry libc.so.1 which defines a bunch of
4565 symbols. The libc.so archive also includes a number of other
4566 object files, which also define symbols, some of which are the same
4567 as those defined in libc.so.1. Correct linking requires that we
4568 consider each object file in turn, and include it if it defines any
4569 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4570 this; it looks through the list of undefined symbols, and includes
4571 any object file which defines them. When this algorithm is used on
4572 UnixWare, it winds up pulling in libc.so.1 early and defining a
4573 bunch of symbols. This means that some of the other objects in the
4574 archive are not included in the link, which is incorrect since they
4575 precede libc.so.1 in the archive.
4577 Fortunately, ELF archive handling is simpler than that done by
4578 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4579 oddities. In ELF, if we find a symbol in the archive map, and the
4580 symbol is currently undefined, we know that we must pull in that
4583 Unfortunately, we do have to make multiple passes over the symbol
4584 table until nothing further is resolved. */
4587 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4590 bfd_boolean
*defined
= NULL
;
4591 bfd_boolean
*included
= NULL
;
4595 const struct elf_backend_data
*bed
;
4596 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4597 (bfd
*, struct bfd_link_info
*, const char *);
4599 if (! bfd_has_map (abfd
))
4601 /* An empty archive is a special case. */
4602 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4604 bfd_set_error (bfd_error_no_armap
);
4608 /* Keep track of all symbols we know to be already defined, and all
4609 files we know to be already included. This is to speed up the
4610 second and subsequent passes. */
4611 c
= bfd_ardata (abfd
)->symdef_count
;
4615 amt
*= sizeof (bfd_boolean
);
4616 defined
= bfd_zmalloc (amt
);
4617 included
= bfd_zmalloc (amt
);
4618 if (defined
== NULL
|| included
== NULL
)
4621 symdefs
= bfd_ardata (abfd
)->symdefs
;
4622 bed
= get_elf_backend_data (abfd
);
4623 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4636 symdefend
= symdef
+ c
;
4637 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4639 struct elf_link_hash_entry
*h
;
4641 struct bfd_link_hash_entry
*undefs_tail
;
4644 if (defined
[i
] || included
[i
])
4646 if (symdef
->file_offset
== last
)
4652 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4653 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4659 if (h
->root
.type
== bfd_link_hash_common
)
4661 /* We currently have a common symbol. The archive map contains
4662 a reference to this symbol, so we may want to include it. We
4663 only want to include it however, if this archive element
4664 contains a definition of the symbol, not just another common
4667 Unfortunately some archivers (including GNU ar) will put
4668 declarations of common symbols into their archive maps, as
4669 well as real definitions, so we cannot just go by the archive
4670 map alone. Instead we must read in the element's symbol
4671 table and check that to see what kind of symbol definition
4673 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4676 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4678 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4683 /* We need to include this archive member. */
4684 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4685 if (element
== NULL
)
4688 if (! bfd_check_format (element
, bfd_object
))
4691 /* Doublecheck that we have not included this object
4692 already--it should be impossible, but there may be
4693 something wrong with the archive. */
4694 if (element
->archive_pass
!= 0)
4696 bfd_set_error (bfd_error_bad_value
);
4699 element
->archive_pass
= 1;
4701 undefs_tail
= info
->hash
->undefs_tail
;
4703 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4706 if (! bfd_link_add_symbols (element
, info
))
4709 /* If there are any new undefined symbols, we need to make
4710 another pass through the archive in order to see whether
4711 they can be defined. FIXME: This isn't perfect, because
4712 common symbols wind up on undefs_tail and because an
4713 undefined symbol which is defined later on in this pass
4714 does not require another pass. This isn't a bug, but it
4715 does make the code less efficient than it could be. */
4716 if (undefs_tail
!= info
->hash
->undefs_tail
)
4719 /* Look backward to mark all symbols from this object file
4720 which we have already seen in this pass. */
4724 included
[mark
] = TRUE
;
4729 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4731 /* We mark subsequent symbols from this object file as we go
4732 on through the loop. */
4733 last
= symdef
->file_offset
;
4744 if (defined
!= NULL
)
4746 if (included
!= NULL
)
4751 /* Given an ELF BFD, add symbols to the global hash table as
4755 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4757 switch (bfd_get_format (abfd
))
4760 return elf_link_add_object_symbols (abfd
, info
);
4762 return elf_link_add_archive_symbols (abfd
, info
);
4764 bfd_set_error (bfd_error_wrong_format
);
4769 /* This function will be called though elf_link_hash_traverse to store
4770 all hash value of the exported symbols in an array. */
4773 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4775 unsigned long **valuep
= data
;
4781 if (h
->root
.type
== bfd_link_hash_warning
)
4782 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4784 /* Ignore indirect symbols. These are added by the versioning code. */
4785 if (h
->dynindx
== -1)
4788 name
= h
->root
.root
.string
;
4789 p
= strchr (name
, ELF_VER_CHR
);
4792 alc
= bfd_malloc (p
- name
+ 1);
4793 memcpy (alc
, name
, p
- name
);
4794 alc
[p
- name
] = '\0';
4798 /* Compute the hash value. */
4799 ha
= bfd_elf_hash (name
);
4801 /* Store the found hash value in the array given as the argument. */
4804 /* And store it in the struct so that we can put it in the hash table
4806 h
->u
.elf_hash_value
= ha
;
4814 /* Array used to determine the number of hash table buckets to use
4815 based on the number of symbols there are. If there are fewer than
4816 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4817 fewer than 37 we use 17 buckets, and so forth. We never use more
4818 than 32771 buckets. */
4820 static const size_t elf_buckets
[] =
4822 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4826 /* Compute bucket count for hashing table. We do not use a static set
4827 of possible tables sizes anymore. Instead we determine for all
4828 possible reasonable sizes of the table the outcome (i.e., the
4829 number of collisions etc) and choose the best solution. The
4830 weighting functions are not too simple to allow the table to grow
4831 without bounds. Instead one of the weighting factors is the size.
4832 Therefore the result is always a good payoff between few collisions
4833 (= short chain lengths) and table size. */
4835 compute_bucket_count (struct bfd_link_info
*info
)
4837 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4838 size_t best_size
= 0;
4839 unsigned long int *hashcodes
;
4840 unsigned long int *hashcodesp
;
4841 unsigned long int i
;
4844 /* Compute the hash values for all exported symbols. At the same
4845 time store the values in an array so that we could use them for
4848 amt
*= sizeof (unsigned long int);
4849 hashcodes
= bfd_malloc (amt
);
4850 if (hashcodes
== NULL
)
4852 hashcodesp
= hashcodes
;
4854 /* Put all hash values in HASHCODES. */
4855 elf_link_hash_traverse (elf_hash_table (info
),
4856 elf_collect_hash_codes
, &hashcodesp
);
4858 /* We have a problem here. The following code to optimize the table
4859 size requires an integer type with more the 32 bits. If
4860 BFD_HOST_U_64_BIT is set we know about such a type. */
4861 #ifdef BFD_HOST_U_64_BIT
4864 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4867 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4868 unsigned long int *counts
;
4869 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4870 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4872 /* Possible optimization parameters: if we have NSYMS symbols we say
4873 that the hashing table must at least have NSYMS/4 and at most
4875 minsize
= nsyms
/ 4;
4878 best_size
= maxsize
= nsyms
* 2;
4880 /* Create array where we count the collisions in. We must use bfd_malloc
4881 since the size could be large. */
4883 amt
*= sizeof (unsigned long int);
4884 counts
= bfd_malloc (amt
);
4891 /* Compute the "optimal" size for the hash table. The criteria is a
4892 minimal chain length. The minor criteria is (of course) the size
4894 for (i
= minsize
; i
< maxsize
; ++i
)
4896 /* Walk through the array of hashcodes and count the collisions. */
4897 BFD_HOST_U_64_BIT max
;
4898 unsigned long int j
;
4899 unsigned long int fact
;
4901 memset (counts
, '\0', i
* sizeof (unsigned long int));
4903 /* Determine how often each hash bucket is used. */
4904 for (j
= 0; j
< nsyms
; ++j
)
4905 ++counts
[hashcodes
[j
] % i
];
4907 /* For the weight function we need some information about the
4908 pagesize on the target. This is information need not be 100%
4909 accurate. Since this information is not available (so far) we
4910 define it here to a reasonable default value. If it is crucial
4911 to have a better value some day simply define this value. */
4912 # ifndef BFD_TARGET_PAGESIZE
4913 # define BFD_TARGET_PAGESIZE (4096)
4916 /* We in any case need 2 + NSYMS entries for the size values and
4918 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4921 /* Variant 1: optimize for short chains. We add the squares
4922 of all the chain lengths (which favors many small chain
4923 over a few long chains). */
4924 for (j
= 0; j
< i
; ++j
)
4925 max
+= counts
[j
] * counts
[j
];
4927 /* This adds penalties for the overall size of the table. */
4928 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4931 /* Variant 2: Optimize a lot more for small table. Here we
4932 also add squares of the size but we also add penalties for
4933 empty slots (the +1 term). */
4934 for (j
= 0; j
< i
; ++j
)
4935 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4937 /* The overall size of the table is considered, but not as
4938 strong as in variant 1, where it is squared. */
4939 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4943 /* Compare with current best results. */
4944 if (max
< best_chlen
)
4954 #endif /* defined (BFD_HOST_U_64_BIT) */
4956 /* This is the fallback solution if no 64bit type is available or if we
4957 are not supposed to spend much time on optimizations. We select the
4958 bucket count using a fixed set of numbers. */
4959 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4961 best_size
= elf_buckets
[i
];
4962 if (dynsymcount
< elf_buckets
[i
+ 1])
4967 /* Free the arrays we needed. */
4973 /* Set up the sizes and contents of the ELF dynamic sections. This is
4974 called by the ELF linker emulation before_allocation routine. We
4975 must set the sizes of the sections before the linker sets the
4976 addresses of the various sections. */
4979 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4982 const char *filter_shlib
,
4983 const char * const *auxiliary_filters
,
4984 struct bfd_link_info
*info
,
4985 asection
**sinterpptr
,
4986 struct bfd_elf_version_tree
*verdefs
)
4988 bfd_size_type soname_indx
;
4990 const struct elf_backend_data
*bed
;
4991 struct elf_assign_sym_version_info asvinfo
;
4995 soname_indx
= (bfd_size_type
) -1;
4997 if (!is_elf_hash_table (info
->hash
))
5000 elf_tdata (output_bfd
)->relro
= info
->relro
;
5001 if (info
->execstack
)
5002 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5003 else if (info
->noexecstack
)
5004 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5008 asection
*notesec
= NULL
;
5011 for (inputobj
= info
->input_bfds
;
5013 inputobj
= inputobj
->link_next
)
5017 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5019 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5022 if (s
->flags
& SEC_CODE
)
5031 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5032 if (exec
&& info
->relocatable
5033 && notesec
->output_section
!= bfd_abs_section_ptr
)
5034 notesec
->output_section
->flags
|= SEC_CODE
;
5038 /* Any syms created from now on start with -1 in
5039 got.refcount/offset and plt.refcount/offset. */
5040 elf_hash_table (info
)->init_got_refcount
5041 = elf_hash_table (info
)->init_got_offset
;
5042 elf_hash_table (info
)->init_plt_refcount
5043 = elf_hash_table (info
)->init_plt_offset
;
5045 /* The backend may have to create some sections regardless of whether
5046 we're dynamic or not. */
5047 bed
= get_elf_backend_data (output_bfd
);
5048 if (bed
->elf_backend_always_size_sections
5049 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5052 dynobj
= elf_hash_table (info
)->dynobj
;
5054 /* If there were no dynamic objects in the link, there is nothing to
5059 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5062 if (elf_hash_table (info
)->dynamic_sections_created
)
5064 struct elf_info_failed eif
;
5065 struct elf_link_hash_entry
*h
;
5067 struct bfd_elf_version_tree
*t
;
5068 struct bfd_elf_version_expr
*d
;
5070 bfd_boolean all_defined
;
5072 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5073 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5077 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5079 if (soname_indx
== (bfd_size_type
) -1
5080 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5086 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5088 info
->flags
|= DF_SYMBOLIC
;
5095 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5097 if (indx
== (bfd_size_type
) -1
5098 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5101 if (info
->new_dtags
)
5103 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5104 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5109 if (filter_shlib
!= NULL
)
5113 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5114 filter_shlib
, TRUE
);
5115 if (indx
== (bfd_size_type
) -1
5116 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5120 if (auxiliary_filters
!= NULL
)
5122 const char * const *p
;
5124 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5128 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5130 if (indx
== (bfd_size_type
) -1
5131 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5137 eif
.verdefs
= verdefs
;
5140 /* If we are supposed to export all symbols into the dynamic symbol
5141 table (this is not the normal case), then do so. */
5142 if (info
->export_dynamic
)
5144 elf_link_hash_traverse (elf_hash_table (info
),
5145 _bfd_elf_export_symbol
,
5151 /* Make all global versions with definition. */
5152 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5153 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5154 if (!d
->symver
&& d
->symbol
)
5156 const char *verstr
, *name
;
5157 size_t namelen
, verlen
, newlen
;
5159 struct elf_link_hash_entry
*newh
;
5162 namelen
= strlen (name
);
5164 verlen
= strlen (verstr
);
5165 newlen
= namelen
+ verlen
+ 3;
5167 newname
= bfd_malloc (newlen
);
5168 if (newname
== NULL
)
5170 memcpy (newname
, name
, namelen
);
5172 /* Check the hidden versioned definition. */
5173 p
= newname
+ namelen
;
5175 memcpy (p
, verstr
, verlen
+ 1);
5176 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5177 newname
, FALSE
, FALSE
,
5180 || (newh
->root
.type
!= bfd_link_hash_defined
5181 && newh
->root
.type
!= bfd_link_hash_defweak
))
5183 /* Check the default versioned definition. */
5185 memcpy (p
, verstr
, verlen
+ 1);
5186 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5187 newname
, FALSE
, FALSE
,
5192 /* Mark this version if there is a definition and it is
5193 not defined in a shared object. */
5195 && !newh
->def_dynamic
5196 && (newh
->root
.type
== bfd_link_hash_defined
5197 || newh
->root
.type
== bfd_link_hash_defweak
))
5201 /* Attach all the symbols to their version information. */
5202 asvinfo
.output_bfd
= output_bfd
;
5203 asvinfo
.info
= info
;
5204 asvinfo
.verdefs
= verdefs
;
5205 asvinfo
.failed
= FALSE
;
5207 elf_link_hash_traverse (elf_hash_table (info
),
5208 _bfd_elf_link_assign_sym_version
,
5213 if (!info
->allow_undefined_version
)
5215 /* Check if all global versions have a definition. */
5217 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5218 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5219 if (!d
->symver
&& !d
->script
)
5221 (*_bfd_error_handler
)
5222 (_("%s: undefined version: %s"),
5223 d
->pattern
, t
->name
);
5224 all_defined
= FALSE
;
5229 bfd_set_error (bfd_error_bad_value
);
5234 /* Find all symbols which were defined in a dynamic object and make
5235 the backend pick a reasonable value for them. */
5236 elf_link_hash_traverse (elf_hash_table (info
),
5237 _bfd_elf_adjust_dynamic_symbol
,
5242 /* Add some entries to the .dynamic section. We fill in some of the
5243 values later, in bfd_elf_final_link, but we must add the entries
5244 now so that we know the final size of the .dynamic section. */
5246 /* If there are initialization and/or finalization functions to
5247 call then add the corresponding DT_INIT/DT_FINI entries. */
5248 h
= (info
->init_function
5249 ? elf_link_hash_lookup (elf_hash_table (info
),
5250 info
->init_function
, FALSE
,
5257 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5260 h
= (info
->fini_function
5261 ? elf_link_hash_lookup (elf_hash_table (info
),
5262 info
->fini_function
, FALSE
,
5269 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5273 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5274 if (s
!= NULL
&& s
->linker_has_input
)
5276 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5277 if (! info
->executable
)
5282 for (sub
= info
->input_bfds
; sub
!= NULL
;
5283 sub
= sub
->link_next
)
5284 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5285 if (elf_section_data (o
)->this_hdr
.sh_type
5286 == SHT_PREINIT_ARRAY
)
5288 (*_bfd_error_handler
)
5289 (_("%B: .preinit_array section is not allowed in DSO"),
5294 bfd_set_error (bfd_error_nonrepresentable_section
);
5298 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5299 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5302 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5303 if (s
!= NULL
&& s
->linker_has_input
)
5305 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5306 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5309 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5310 if (s
!= NULL
&& s
->linker_has_input
)
5312 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5313 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5317 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5318 /* If .dynstr is excluded from the link, we don't want any of
5319 these tags. Strictly, we should be checking each section
5320 individually; This quick check covers for the case where
5321 someone does a /DISCARD/ : { *(*) }. */
5322 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5324 bfd_size_type strsize
;
5326 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5327 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5328 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5329 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5330 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5331 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5332 bed
->s
->sizeof_sym
))
5337 /* The backend must work out the sizes of all the other dynamic
5339 if (bed
->elf_backend_size_dynamic_sections
5340 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5343 if (elf_hash_table (info
)->dynamic_sections_created
)
5345 unsigned long section_sym_count
;
5348 /* Set up the version definition section. */
5349 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5350 BFD_ASSERT (s
!= NULL
);
5352 /* We may have created additional version definitions if we are
5353 just linking a regular application. */
5354 verdefs
= asvinfo
.verdefs
;
5356 /* Skip anonymous version tag. */
5357 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5358 verdefs
= verdefs
->next
;
5360 if (verdefs
== NULL
&& !info
->create_default_symver
)
5361 s
->flags
|= SEC_EXCLUDE
;
5366 struct bfd_elf_version_tree
*t
;
5368 Elf_Internal_Verdef def
;
5369 Elf_Internal_Verdaux defaux
;
5370 struct bfd_link_hash_entry
*bh
;
5371 struct elf_link_hash_entry
*h
;
5377 /* Make space for the base version. */
5378 size
+= sizeof (Elf_External_Verdef
);
5379 size
+= sizeof (Elf_External_Verdaux
);
5382 /* Make space for the default version. */
5383 if (info
->create_default_symver
)
5385 size
+= sizeof (Elf_External_Verdef
);
5389 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5391 struct bfd_elf_version_deps
*n
;
5393 size
+= sizeof (Elf_External_Verdef
);
5394 size
+= sizeof (Elf_External_Verdaux
);
5397 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5398 size
+= sizeof (Elf_External_Verdaux
);
5402 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5403 if (s
->contents
== NULL
&& s
->size
!= 0)
5406 /* Fill in the version definition section. */
5410 def
.vd_version
= VER_DEF_CURRENT
;
5411 def
.vd_flags
= VER_FLG_BASE
;
5414 if (info
->create_default_symver
)
5416 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5417 def
.vd_next
= sizeof (Elf_External_Verdef
);
5421 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5422 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5423 + sizeof (Elf_External_Verdaux
));
5426 if (soname_indx
!= (bfd_size_type
) -1)
5428 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5430 def
.vd_hash
= bfd_elf_hash (soname
);
5431 defaux
.vda_name
= soname_indx
;
5438 name
= lbasename (output_bfd
->filename
);
5439 def
.vd_hash
= bfd_elf_hash (name
);
5440 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5442 if (indx
== (bfd_size_type
) -1)
5444 defaux
.vda_name
= indx
;
5446 defaux
.vda_next
= 0;
5448 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5449 (Elf_External_Verdef
*) p
);
5450 p
+= sizeof (Elf_External_Verdef
);
5451 if (info
->create_default_symver
)
5453 /* Add a symbol representing this version. */
5455 if (! (_bfd_generic_link_add_one_symbol
5456 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5458 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5460 h
= (struct elf_link_hash_entry
*) bh
;
5463 h
->type
= STT_OBJECT
;
5464 h
->verinfo
.vertree
= NULL
;
5466 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5469 /* Create a duplicate of the base version with the same
5470 aux block, but different flags. */
5473 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5475 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5476 + sizeof (Elf_External_Verdaux
));
5479 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5480 (Elf_External_Verdef
*) p
);
5481 p
+= sizeof (Elf_External_Verdef
);
5483 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5484 (Elf_External_Verdaux
*) p
);
5485 p
+= sizeof (Elf_External_Verdaux
);
5487 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5490 struct bfd_elf_version_deps
*n
;
5493 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5496 /* Add a symbol representing this version. */
5498 if (! (_bfd_generic_link_add_one_symbol
5499 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5501 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5503 h
= (struct elf_link_hash_entry
*) bh
;
5506 h
->type
= STT_OBJECT
;
5507 h
->verinfo
.vertree
= t
;
5509 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5512 def
.vd_version
= VER_DEF_CURRENT
;
5514 if (t
->globals
.list
== NULL
5515 && t
->locals
.list
== NULL
5517 def
.vd_flags
|= VER_FLG_WEAK
;
5518 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5519 def
.vd_cnt
= cdeps
+ 1;
5520 def
.vd_hash
= bfd_elf_hash (t
->name
);
5521 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5523 if (t
->next
!= NULL
)
5524 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5525 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5527 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5528 (Elf_External_Verdef
*) p
);
5529 p
+= sizeof (Elf_External_Verdef
);
5531 defaux
.vda_name
= h
->dynstr_index
;
5532 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5534 defaux
.vda_next
= 0;
5535 if (t
->deps
!= NULL
)
5536 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5537 t
->name_indx
= defaux
.vda_name
;
5539 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5540 (Elf_External_Verdaux
*) p
);
5541 p
+= sizeof (Elf_External_Verdaux
);
5543 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5545 if (n
->version_needed
== NULL
)
5547 /* This can happen if there was an error in the
5549 defaux
.vda_name
= 0;
5553 defaux
.vda_name
= n
->version_needed
->name_indx
;
5554 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5557 if (n
->next
== NULL
)
5558 defaux
.vda_next
= 0;
5560 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5562 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5563 (Elf_External_Verdaux
*) p
);
5564 p
+= sizeof (Elf_External_Verdaux
);
5568 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5569 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5572 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5575 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5577 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5580 else if (info
->flags
& DF_BIND_NOW
)
5582 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5588 if (info
->executable
)
5589 info
->flags_1
&= ~ (DF_1_INITFIRST
5592 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5596 /* Work out the size of the version reference section. */
5598 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5599 BFD_ASSERT (s
!= NULL
);
5601 struct elf_find_verdep_info sinfo
;
5603 sinfo
.output_bfd
= output_bfd
;
5605 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5606 if (sinfo
.vers
== 0)
5608 sinfo
.failed
= FALSE
;
5610 elf_link_hash_traverse (elf_hash_table (info
),
5611 _bfd_elf_link_find_version_dependencies
,
5614 if (elf_tdata (output_bfd
)->verref
== NULL
)
5615 s
->flags
|= SEC_EXCLUDE
;
5618 Elf_Internal_Verneed
*t
;
5623 /* Build the version definition section. */
5626 for (t
= elf_tdata (output_bfd
)->verref
;
5630 Elf_Internal_Vernaux
*a
;
5632 size
+= sizeof (Elf_External_Verneed
);
5634 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5635 size
+= sizeof (Elf_External_Vernaux
);
5639 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5640 if (s
->contents
== NULL
)
5644 for (t
= elf_tdata (output_bfd
)->verref
;
5649 Elf_Internal_Vernaux
*a
;
5653 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5656 t
->vn_version
= VER_NEED_CURRENT
;
5658 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5659 elf_dt_name (t
->vn_bfd
) != NULL
5660 ? elf_dt_name (t
->vn_bfd
)
5661 : lbasename (t
->vn_bfd
->filename
),
5663 if (indx
== (bfd_size_type
) -1)
5666 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5667 if (t
->vn_nextref
== NULL
)
5670 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5671 + caux
* sizeof (Elf_External_Vernaux
));
5673 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5674 (Elf_External_Verneed
*) p
);
5675 p
+= sizeof (Elf_External_Verneed
);
5677 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5679 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5680 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5681 a
->vna_nodename
, FALSE
);
5682 if (indx
== (bfd_size_type
) -1)
5685 if (a
->vna_nextptr
== NULL
)
5688 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5690 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5691 (Elf_External_Vernaux
*) p
);
5692 p
+= sizeof (Elf_External_Vernaux
);
5696 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5697 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5700 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5704 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5705 && elf_tdata (output_bfd
)->cverdefs
== 0)
5706 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5707 §ion_sym_count
) == 0)
5709 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5710 s
->flags
|= SEC_EXCLUDE
;
5717 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5719 if (!is_elf_hash_table (info
->hash
))
5722 if (elf_hash_table (info
)->dynamic_sections_created
)
5725 const struct elf_backend_data
*bed
;
5727 bfd_size_type dynsymcount
;
5728 unsigned long section_sym_count
;
5729 size_t bucketcount
= 0;
5730 size_t hash_entry_size
;
5731 unsigned int dtagcount
;
5733 dynobj
= elf_hash_table (info
)->dynobj
;
5735 /* Assign dynsym indicies. In a shared library we generate a
5736 section symbol for each output section, which come first.
5737 Next come all of the back-end allocated local dynamic syms,
5738 followed by the rest of the global symbols. */
5740 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5741 §ion_sym_count
);
5743 /* Work out the size of the symbol version section. */
5744 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5745 BFD_ASSERT (s
!= NULL
);
5746 if (dynsymcount
!= 0
5747 && (s
->flags
& SEC_EXCLUDE
) == 0)
5749 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5750 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5751 if (s
->contents
== NULL
)
5754 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5758 /* Set the size of the .dynsym and .hash sections. We counted
5759 the number of dynamic symbols in elf_link_add_object_symbols.
5760 We will build the contents of .dynsym and .hash when we build
5761 the final symbol table, because until then we do not know the
5762 correct value to give the symbols. We built the .dynstr
5763 section as we went along in elf_link_add_object_symbols. */
5764 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5765 BFD_ASSERT (s
!= NULL
);
5766 bed
= get_elf_backend_data (output_bfd
);
5767 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5769 if (dynsymcount
!= 0)
5771 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5772 if (s
->contents
== NULL
)
5775 /* The first entry in .dynsym is a dummy symbol.
5776 Clear all the section syms, in case we don't output them all. */
5777 ++section_sym_count
;
5778 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5781 /* Compute the size of the hashing table. As a side effect this
5782 computes the hash values for all the names we export. */
5783 bucketcount
= compute_bucket_count (info
);
5785 s
= bfd_get_section_by_name (dynobj
, ".hash");
5786 BFD_ASSERT (s
!= NULL
);
5787 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5788 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5789 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5790 if (s
->contents
== NULL
)
5793 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5794 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5795 s
->contents
+ hash_entry_size
);
5797 elf_hash_table (info
)->bucketcount
= bucketcount
;
5799 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5800 BFD_ASSERT (s
!= NULL
);
5802 elf_finalize_dynstr (output_bfd
, info
);
5804 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5806 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5807 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5814 /* Final phase of ELF linker. */
5816 /* A structure we use to avoid passing large numbers of arguments. */
5818 struct elf_final_link_info
5820 /* General link information. */
5821 struct bfd_link_info
*info
;
5824 /* Symbol string table. */
5825 struct bfd_strtab_hash
*symstrtab
;
5826 /* .dynsym section. */
5827 asection
*dynsym_sec
;
5828 /* .hash section. */
5830 /* symbol version section (.gnu.version). */
5831 asection
*symver_sec
;
5832 /* Buffer large enough to hold contents of any section. */
5834 /* Buffer large enough to hold external relocs of any section. */
5835 void *external_relocs
;
5836 /* Buffer large enough to hold internal relocs of any section. */
5837 Elf_Internal_Rela
*internal_relocs
;
5838 /* Buffer large enough to hold external local symbols of any input
5840 bfd_byte
*external_syms
;
5841 /* And a buffer for symbol section indices. */
5842 Elf_External_Sym_Shndx
*locsym_shndx
;
5843 /* Buffer large enough to hold internal local symbols of any input
5845 Elf_Internal_Sym
*internal_syms
;
5846 /* Array large enough to hold a symbol index for each local symbol
5847 of any input BFD. */
5849 /* Array large enough to hold a section pointer for each local
5850 symbol of any input BFD. */
5851 asection
**sections
;
5852 /* Buffer to hold swapped out symbols. */
5854 /* And one for symbol section indices. */
5855 Elf_External_Sym_Shndx
*symshndxbuf
;
5856 /* Number of swapped out symbols in buffer. */
5857 size_t symbuf_count
;
5858 /* Number of symbols which fit in symbuf. */
5860 /* And same for symshndxbuf. */
5861 size_t shndxbuf_size
;
5864 /* This struct is used to pass information to elf_link_output_extsym. */
5866 struct elf_outext_info
5869 bfd_boolean localsyms
;
5870 struct elf_final_link_info
*finfo
;
5873 /* When performing a relocatable link, the input relocations are
5874 preserved. But, if they reference global symbols, the indices
5875 referenced must be updated. Update all the relocations in
5876 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5879 elf_link_adjust_relocs (bfd
*abfd
,
5880 Elf_Internal_Shdr
*rel_hdr
,
5882 struct elf_link_hash_entry
**rel_hash
)
5885 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5887 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5888 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5889 bfd_vma r_type_mask
;
5892 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5894 swap_in
= bed
->s
->swap_reloc_in
;
5895 swap_out
= bed
->s
->swap_reloc_out
;
5897 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5899 swap_in
= bed
->s
->swap_reloca_in
;
5900 swap_out
= bed
->s
->swap_reloca_out
;
5905 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5908 if (bed
->s
->arch_size
== 32)
5915 r_type_mask
= 0xffffffff;
5919 erela
= rel_hdr
->contents
;
5920 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5922 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5925 if (*rel_hash
== NULL
)
5928 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5930 (*swap_in
) (abfd
, erela
, irela
);
5931 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5932 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5933 | (irela
[j
].r_info
& r_type_mask
));
5934 (*swap_out
) (abfd
, irela
, erela
);
5938 struct elf_link_sort_rela
5944 enum elf_reloc_type_class type
;
5945 /* We use this as an array of size int_rels_per_ext_rel. */
5946 Elf_Internal_Rela rela
[1];
5950 elf_link_sort_cmp1 (const void *A
, const void *B
)
5952 const struct elf_link_sort_rela
*a
= A
;
5953 const struct elf_link_sort_rela
*b
= B
;
5954 int relativea
, relativeb
;
5956 relativea
= a
->type
== reloc_class_relative
;
5957 relativeb
= b
->type
== reloc_class_relative
;
5959 if (relativea
< relativeb
)
5961 if (relativea
> relativeb
)
5963 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5965 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5967 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5969 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5975 elf_link_sort_cmp2 (const void *A
, const void *B
)
5977 const struct elf_link_sort_rela
*a
= A
;
5978 const struct elf_link_sort_rela
*b
= B
;
5981 if (a
->u
.offset
< b
->u
.offset
)
5983 if (a
->u
.offset
> b
->u
.offset
)
5985 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5986 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5991 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5993 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5999 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
6002 bfd_size_type count
, size
;
6003 size_t i
, ret
, sort_elt
, ext_size
;
6004 bfd_byte
*sort
, *s_non_relative
, *p
;
6005 struct elf_link_sort_rela
*sq
;
6006 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6007 int i2e
= bed
->s
->int_rels_per_ext_rel
;
6008 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6009 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6010 struct bfd_link_order
*lo
;
6013 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
6014 if (reldyn
== NULL
|| reldyn
->size
== 0)
6016 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
6017 if (reldyn
== NULL
|| reldyn
->size
== 0)
6019 ext_size
= bed
->s
->sizeof_rel
;
6020 swap_in
= bed
->s
->swap_reloc_in
;
6021 swap_out
= bed
->s
->swap_reloc_out
;
6025 ext_size
= bed
->s
->sizeof_rela
;
6026 swap_in
= bed
->s
->swap_reloca_in
;
6027 swap_out
= bed
->s
->swap_reloca_out
;
6029 count
= reldyn
->size
/ ext_size
;
6032 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6033 if (lo
->type
== bfd_indirect_link_order
)
6035 asection
*o
= lo
->u
.indirect
.section
;
6039 if (size
!= reldyn
->size
)
6042 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6043 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6044 sort
= bfd_zmalloc (sort_elt
* count
);
6047 (*info
->callbacks
->warning
)
6048 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6052 if (bed
->s
->arch_size
== 32)
6053 r_sym_mask
= ~(bfd_vma
) 0xff;
6055 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6057 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6058 if (lo
->type
== bfd_indirect_link_order
)
6060 bfd_byte
*erel
, *erelend
;
6061 asection
*o
= lo
->u
.indirect
.section
;
6063 if (o
->contents
== NULL
&& o
->size
!= 0)
6065 /* This is a reloc section that is being handled as a normal
6066 section. See bfd_section_from_shdr. We can't combine
6067 relocs in this case. */
6072 erelend
= o
->contents
+ o
->size
;
6073 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6074 while (erel
< erelend
)
6076 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6077 (*swap_in
) (abfd
, erel
, s
->rela
);
6078 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6079 s
->u
.sym_mask
= r_sym_mask
;
6085 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6087 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6089 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6090 if (s
->type
!= reloc_class_relative
)
6096 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6097 for (; i
< count
; i
++, p
+= sort_elt
)
6099 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6100 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6102 sp
->u
.offset
= sq
->rela
->r_offset
;
6105 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6107 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6108 if (lo
->type
== bfd_indirect_link_order
)
6110 bfd_byte
*erel
, *erelend
;
6111 asection
*o
= lo
->u
.indirect
.section
;
6114 erelend
= o
->contents
+ o
->size
;
6115 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6116 while (erel
< erelend
)
6118 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6119 (*swap_out
) (abfd
, s
->rela
, erel
);
6130 /* Flush the output symbols to the file. */
6133 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6134 const struct elf_backend_data
*bed
)
6136 if (finfo
->symbuf_count
> 0)
6138 Elf_Internal_Shdr
*hdr
;
6142 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6143 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6144 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6145 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6146 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6149 hdr
->sh_size
+= amt
;
6150 finfo
->symbuf_count
= 0;
6156 /* Add a symbol to the output symbol table. */
6159 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6161 Elf_Internal_Sym
*elfsym
,
6162 asection
*input_sec
,
6163 struct elf_link_hash_entry
*h
)
6166 Elf_External_Sym_Shndx
*destshndx
;
6167 bfd_boolean (*output_symbol_hook
)
6168 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6169 struct elf_link_hash_entry
*);
6170 const struct elf_backend_data
*bed
;
6172 bed
= get_elf_backend_data (finfo
->output_bfd
);
6173 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6174 if (output_symbol_hook
!= NULL
)
6176 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6180 if (name
== NULL
|| *name
== '\0')
6181 elfsym
->st_name
= 0;
6182 else if (input_sec
->flags
& SEC_EXCLUDE
)
6183 elfsym
->st_name
= 0;
6186 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6188 if (elfsym
->st_name
== (unsigned long) -1)
6192 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6194 if (! elf_link_flush_output_syms (finfo
, bed
))
6198 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6199 destshndx
= finfo
->symshndxbuf
;
6200 if (destshndx
!= NULL
)
6202 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6206 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6207 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6208 if (destshndx
== NULL
)
6210 memset ((char *) destshndx
+ amt
, 0, amt
);
6211 finfo
->shndxbuf_size
*= 2;
6213 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6216 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6217 finfo
->symbuf_count
+= 1;
6218 bfd_get_symcount (finfo
->output_bfd
) += 1;
6223 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6226 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
6228 if (sym
->st_shndx
> SHN_HIRESERVE
)
6230 /* The gABI doesn't support dynamic symbols in output sections
6232 (*_bfd_error_handler
)
6233 (_("%B: Too many sections: %d (>= %d)"),
6234 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
6235 bfd_set_error (bfd_error_nonrepresentable_section
);
6241 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6242 allowing an unsatisfied unversioned symbol in the DSO to match a
6243 versioned symbol that would normally require an explicit version.
6244 We also handle the case that a DSO references a hidden symbol
6245 which may be satisfied by a versioned symbol in another DSO. */
6248 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6249 const struct elf_backend_data
*bed
,
6250 struct elf_link_hash_entry
*h
)
6253 struct elf_link_loaded_list
*loaded
;
6255 if (!is_elf_hash_table (info
->hash
))
6258 switch (h
->root
.type
)
6264 case bfd_link_hash_undefined
:
6265 case bfd_link_hash_undefweak
:
6266 abfd
= h
->root
.u
.undef
.abfd
;
6267 if ((abfd
->flags
& DYNAMIC
) == 0
6268 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6272 case bfd_link_hash_defined
:
6273 case bfd_link_hash_defweak
:
6274 abfd
= h
->root
.u
.def
.section
->owner
;
6277 case bfd_link_hash_common
:
6278 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6281 BFD_ASSERT (abfd
!= NULL
);
6283 for (loaded
= elf_hash_table (info
)->loaded
;
6285 loaded
= loaded
->next
)
6288 Elf_Internal_Shdr
*hdr
;
6289 bfd_size_type symcount
;
6290 bfd_size_type extsymcount
;
6291 bfd_size_type extsymoff
;
6292 Elf_Internal_Shdr
*versymhdr
;
6293 Elf_Internal_Sym
*isym
;
6294 Elf_Internal_Sym
*isymend
;
6295 Elf_Internal_Sym
*isymbuf
;
6296 Elf_External_Versym
*ever
;
6297 Elf_External_Versym
*extversym
;
6299 input
= loaded
->abfd
;
6301 /* We check each DSO for a possible hidden versioned definition. */
6303 || (input
->flags
& DYNAMIC
) == 0
6304 || elf_dynversym (input
) == 0)
6307 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6309 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6310 if (elf_bad_symtab (input
))
6312 extsymcount
= symcount
;
6317 extsymcount
= symcount
- hdr
->sh_info
;
6318 extsymoff
= hdr
->sh_info
;
6321 if (extsymcount
== 0)
6324 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6326 if (isymbuf
== NULL
)
6329 /* Read in any version definitions. */
6330 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6331 extversym
= bfd_malloc (versymhdr
->sh_size
);
6332 if (extversym
== NULL
)
6335 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6336 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6337 != versymhdr
->sh_size
))
6345 ever
= extversym
+ extsymoff
;
6346 isymend
= isymbuf
+ extsymcount
;
6347 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6350 Elf_Internal_Versym iver
;
6351 unsigned short version_index
;
6353 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6354 || isym
->st_shndx
== SHN_UNDEF
)
6357 name
= bfd_elf_string_from_elf_section (input
,
6360 if (strcmp (name
, h
->root
.root
.string
) != 0)
6363 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6365 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6367 /* If we have a non-hidden versioned sym, then it should
6368 have provided a definition for the undefined sym. */
6372 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6373 if (version_index
== 1 || version_index
== 2)
6375 /* This is the base or first version. We can use it. */
6389 /* Add an external symbol to the symbol table. This is called from
6390 the hash table traversal routine. When generating a shared object,
6391 we go through the symbol table twice. The first time we output
6392 anything that might have been forced to local scope in a version
6393 script. The second time we output the symbols that are still
6397 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6399 struct elf_outext_info
*eoinfo
= data
;
6400 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6402 Elf_Internal_Sym sym
;
6403 asection
*input_sec
;
6404 const struct elf_backend_data
*bed
;
6406 if (h
->root
.type
== bfd_link_hash_warning
)
6408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6409 if (h
->root
.type
== bfd_link_hash_new
)
6413 /* Decide whether to output this symbol in this pass. */
6414 if (eoinfo
->localsyms
)
6416 if (!h
->forced_local
)
6421 if (h
->forced_local
)
6425 bed
= get_elf_backend_data (finfo
->output_bfd
);
6427 if (h
->root
.type
== bfd_link_hash_undefined
)
6429 /* If we have an undefined symbol reference here then it must have
6430 come from a shared library that is being linked in. (Undefined
6431 references in regular files have already been handled). */
6432 bfd_boolean ignore_undef
= FALSE
;
6434 /* Some symbols may be special in that the fact that they're
6435 undefined can be safely ignored - let backend determine that. */
6436 if (bed
->elf_backend_ignore_undef_symbol
)
6437 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6439 /* If we are reporting errors for this situation then do so now. */
6440 if (ignore_undef
== FALSE
6443 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6444 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6446 if (! (finfo
->info
->callbacks
->undefined_symbol
6447 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6448 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6450 eoinfo
->failed
= TRUE
;
6456 /* We should also warn if a forced local symbol is referenced from
6457 shared libraries. */
6458 if (! finfo
->info
->relocatable
6459 && (! finfo
->info
->shared
)
6464 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6466 (*_bfd_error_handler
)
6467 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6469 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6470 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6471 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6473 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6474 ? "hidden" : "local",
6475 h
->root
.root
.string
);
6476 eoinfo
->failed
= TRUE
;
6480 /* We don't want to output symbols that have never been mentioned by
6481 a regular file, or that we have been told to strip. However, if
6482 h->indx is set to -2, the symbol is used by a reloc and we must
6486 else if ((h
->def_dynamic
6488 || h
->root
.type
== bfd_link_hash_new
)
6492 else if (finfo
->info
->strip
== strip_all
)
6494 else if (finfo
->info
->strip
== strip_some
6495 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6496 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6498 else if (finfo
->info
->strip_discarded
6499 && (h
->root
.type
== bfd_link_hash_defined
6500 || h
->root
.type
== bfd_link_hash_defweak
)
6501 && elf_discarded_section (h
->root
.u
.def
.section
))
6506 /* If we're stripping it, and it's not a dynamic symbol, there's
6507 nothing else to do unless it is a forced local symbol. */
6510 && !h
->forced_local
)
6514 sym
.st_size
= h
->size
;
6515 sym
.st_other
= h
->other
;
6516 if (h
->forced_local
)
6517 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6518 else if (h
->root
.type
== bfd_link_hash_undefweak
6519 || h
->root
.type
== bfd_link_hash_defweak
)
6520 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6522 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6524 switch (h
->root
.type
)
6527 case bfd_link_hash_new
:
6528 case bfd_link_hash_warning
:
6532 case bfd_link_hash_undefined
:
6533 case bfd_link_hash_undefweak
:
6534 input_sec
= bfd_und_section_ptr
;
6535 sym
.st_shndx
= SHN_UNDEF
;
6538 case bfd_link_hash_defined
:
6539 case bfd_link_hash_defweak
:
6541 input_sec
= h
->root
.u
.def
.section
;
6542 if (input_sec
->output_section
!= NULL
)
6545 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6546 input_sec
->output_section
);
6547 if (sym
.st_shndx
== SHN_BAD
)
6549 (*_bfd_error_handler
)
6550 (_("%B: could not find output section %A for input section %A"),
6551 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6552 eoinfo
->failed
= TRUE
;
6556 /* ELF symbols in relocatable files are section relative,
6557 but in nonrelocatable files they are virtual
6559 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6560 if (! finfo
->info
->relocatable
)
6562 sym
.st_value
+= input_sec
->output_section
->vma
;
6563 if (h
->type
== STT_TLS
)
6565 /* STT_TLS symbols are relative to PT_TLS segment
6567 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6568 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6574 BFD_ASSERT (input_sec
->owner
== NULL
6575 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6576 sym
.st_shndx
= SHN_UNDEF
;
6577 input_sec
= bfd_und_section_ptr
;
6582 case bfd_link_hash_common
:
6583 input_sec
= h
->root
.u
.c
.p
->section
;
6584 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6585 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6588 case bfd_link_hash_indirect
:
6589 /* These symbols are created by symbol versioning. They point
6590 to the decorated version of the name. For example, if the
6591 symbol foo@@GNU_1.2 is the default, which should be used when
6592 foo is used with no version, then we add an indirect symbol
6593 foo which points to foo@@GNU_1.2. We ignore these symbols,
6594 since the indirected symbol is already in the hash table. */
6598 /* Give the processor backend a chance to tweak the symbol value,
6599 and also to finish up anything that needs to be done for this
6600 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6601 forced local syms when non-shared is due to a historical quirk. */
6602 if ((h
->dynindx
!= -1
6604 && ((finfo
->info
->shared
6605 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6606 || h
->root
.type
!= bfd_link_hash_undefweak
))
6607 || !h
->forced_local
)
6608 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6610 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6611 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6613 eoinfo
->failed
= TRUE
;
6618 /* If we are marking the symbol as undefined, and there are no
6619 non-weak references to this symbol from a regular object, then
6620 mark the symbol as weak undefined; if there are non-weak
6621 references, mark the symbol as strong. We can't do this earlier,
6622 because it might not be marked as undefined until the
6623 finish_dynamic_symbol routine gets through with it. */
6624 if (sym
.st_shndx
== SHN_UNDEF
6626 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6627 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6631 if (h
->ref_regular_nonweak
)
6632 bindtype
= STB_GLOBAL
;
6634 bindtype
= STB_WEAK
;
6635 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6638 /* If a non-weak symbol with non-default visibility is not defined
6639 locally, it is a fatal error. */
6640 if (! finfo
->info
->relocatable
6641 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6642 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6643 && h
->root
.type
== bfd_link_hash_undefined
6646 (*_bfd_error_handler
)
6647 (_("%B: %s symbol `%s' isn't defined"),
6649 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6651 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6652 ? "internal" : "hidden",
6653 h
->root
.root
.string
);
6654 eoinfo
->failed
= TRUE
;
6658 /* If this symbol should be put in the .dynsym section, then put it
6659 there now. We already know the symbol index. We also fill in
6660 the entry in the .hash section. */
6661 if (h
->dynindx
!= -1
6662 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6666 size_t hash_entry_size
;
6667 bfd_byte
*bucketpos
;
6671 sym
.st_name
= h
->dynstr_index
;
6672 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6673 if (! check_dynsym (finfo
->output_bfd
, &sym
))
6675 eoinfo
->failed
= TRUE
;
6678 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6680 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6681 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6683 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6684 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6685 + (bucket
+ 2) * hash_entry_size
);
6686 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6687 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6688 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6689 ((bfd_byte
*) finfo
->hash_sec
->contents
6690 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6692 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6694 Elf_Internal_Versym iversym
;
6695 Elf_External_Versym
*eversym
;
6697 if (!h
->def_regular
)
6699 if (h
->verinfo
.verdef
== NULL
)
6700 iversym
.vs_vers
= 0;
6702 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6706 if (h
->verinfo
.vertree
== NULL
)
6707 iversym
.vs_vers
= 1;
6709 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6710 if (finfo
->info
->create_default_symver
)
6715 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6717 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6718 eversym
+= h
->dynindx
;
6719 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6723 /* If we're stripping it, then it was just a dynamic symbol, and
6724 there's nothing else to do. */
6725 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6728 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6730 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6732 eoinfo
->failed
= TRUE
;
6739 /* Return TRUE if special handling is done for relocs in SEC against
6740 symbols defined in discarded sections. */
6743 elf_section_ignore_discarded_relocs (asection
*sec
)
6745 const struct elf_backend_data
*bed
;
6747 switch (sec
->sec_info_type
)
6749 case ELF_INFO_TYPE_STABS
:
6750 case ELF_INFO_TYPE_EH_FRAME
:
6756 bed
= get_elf_backend_data (sec
->owner
);
6757 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6758 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6764 /* Return a mask saying how ld should treat relocations in SEC against
6765 symbols defined in discarded sections. If this function returns
6766 COMPLAIN set, ld will issue a warning message. If this function
6767 returns PRETEND set, and the discarded section was link-once and the
6768 same size as the kept link-once section, ld will pretend that the
6769 symbol was actually defined in the kept section. Otherwise ld will
6770 zero the reloc (at least that is the intent, but some cooperation by
6771 the target dependent code is needed, particularly for REL targets). */
6774 _bfd_elf_default_action_discarded (asection
*sec
)
6776 if (sec
->flags
& SEC_DEBUGGING
)
6779 if (strcmp (".eh_frame", sec
->name
) == 0)
6782 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6785 return COMPLAIN
| PRETEND
;
6788 /* Find a match between a section and a member of a section group. */
6791 match_group_member (asection
*sec
, asection
*group
)
6793 asection
*first
= elf_next_in_group (group
);
6794 asection
*s
= first
;
6798 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6801 s
= elf_next_in_group (s
);
6809 /* Check if the kept section of a discarded section SEC can be used
6810 to replace it. Return the replacement if it is OK. Otherwise return
6814 _bfd_elf_check_kept_section (asection
*sec
)
6818 kept
= sec
->kept_section
;
6821 if (elf_sec_group (sec
) != NULL
)
6822 kept
= match_group_member (sec
, kept
);
6823 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6829 /* Link an input file into the linker output file. This function
6830 handles all the sections and relocations of the input file at once.
6831 This is so that we only have to read the local symbols once, and
6832 don't have to keep them in memory. */
6835 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6837 bfd_boolean (*relocate_section
)
6838 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6839 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6841 Elf_Internal_Shdr
*symtab_hdr
;
6844 Elf_Internal_Sym
*isymbuf
;
6845 Elf_Internal_Sym
*isym
;
6846 Elf_Internal_Sym
*isymend
;
6848 asection
**ppsection
;
6850 const struct elf_backend_data
*bed
;
6851 bfd_boolean emit_relocs
;
6852 struct elf_link_hash_entry
**sym_hashes
;
6854 output_bfd
= finfo
->output_bfd
;
6855 bed
= get_elf_backend_data (output_bfd
);
6856 relocate_section
= bed
->elf_backend_relocate_section
;
6858 /* If this is a dynamic object, we don't want to do anything here:
6859 we don't want the local symbols, and we don't want the section
6861 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6864 emit_relocs
= (finfo
->info
->relocatable
6865 || finfo
->info
->emitrelocations
);
6867 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6868 if (elf_bad_symtab (input_bfd
))
6870 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6875 locsymcount
= symtab_hdr
->sh_info
;
6876 extsymoff
= symtab_hdr
->sh_info
;
6879 /* Read the local symbols. */
6880 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6881 if (isymbuf
== NULL
&& locsymcount
!= 0)
6883 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6884 finfo
->internal_syms
,
6885 finfo
->external_syms
,
6886 finfo
->locsym_shndx
);
6887 if (isymbuf
== NULL
)
6891 /* Find local symbol sections and adjust values of symbols in
6892 SEC_MERGE sections. Write out those local symbols we know are
6893 going into the output file. */
6894 isymend
= isymbuf
+ locsymcount
;
6895 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6897 isym
++, pindex
++, ppsection
++)
6901 Elf_Internal_Sym osym
;
6905 if (elf_bad_symtab (input_bfd
))
6907 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6914 if (isym
->st_shndx
== SHN_UNDEF
)
6915 isec
= bfd_und_section_ptr
;
6916 else if (isym
->st_shndx
< SHN_LORESERVE
6917 || isym
->st_shndx
> SHN_HIRESERVE
)
6919 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6921 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6922 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6924 _bfd_merged_section_offset (output_bfd
, &isec
,
6925 elf_section_data (isec
)->sec_info
,
6928 else if (isym
->st_shndx
== SHN_ABS
)
6929 isec
= bfd_abs_section_ptr
;
6930 else if (isym
->st_shndx
== SHN_COMMON
)
6931 isec
= bfd_com_section_ptr
;
6934 /* Don't attempt to output symbols with st_shnx in the
6935 reserved range other than SHN_ABS and SHN_COMMON. */
6942 /* Don't output the first, undefined, symbol. */
6943 if (ppsection
== finfo
->sections
)
6946 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6948 /* We never output section symbols. Instead, we use the
6949 section symbol of the corresponding section in the output
6954 /* If we are stripping all symbols, we don't want to output this
6956 if (finfo
->info
->strip
== strip_all
)
6959 /* If we are discarding all local symbols, we don't want to
6960 output this one. If we are generating a relocatable output
6961 file, then some of the local symbols may be required by
6962 relocs; we output them below as we discover that they are
6964 if (finfo
->info
->discard
== discard_all
)
6967 /* If this symbol is defined in a section which we are
6968 discarding, we don't need to keep it. */
6969 if (isym
->st_shndx
!= SHN_UNDEF
6970 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6972 || bfd_section_removed_from_list (output_bfd
,
6973 isec
->output_section
)))
6976 /* Get the name of the symbol. */
6977 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6982 /* See if we are discarding symbols with this name. */
6983 if ((finfo
->info
->strip
== strip_some
6984 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6986 || (((finfo
->info
->discard
== discard_sec_merge
6987 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6988 || finfo
->info
->discard
== discard_l
)
6989 && bfd_is_local_label_name (input_bfd
, name
)))
6992 /* If we get here, we are going to output this symbol. */
6996 /* Adjust the section index for the output file. */
6997 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6998 isec
->output_section
);
6999 if (osym
.st_shndx
== SHN_BAD
)
7002 *pindex
= bfd_get_symcount (output_bfd
);
7004 /* ELF symbols in relocatable files are section relative, but
7005 in executable files they are virtual addresses. Note that
7006 this code assumes that all ELF sections have an associated
7007 BFD section with a reasonable value for output_offset; below
7008 we assume that they also have a reasonable value for
7009 output_section. Any special sections must be set up to meet
7010 these requirements. */
7011 osym
.st_value
+= isec
->output_offset
;
7012 if (! finfo
->info
->relocatable
)
7014 osym
.st_value
+= isec
->output_section
->vma
;
7015 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
7017 /* STT_TLS symbols are relative to PT_TLS segment base. */
7018 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7019 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7023 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
7027 /* Relocate the contents of each section. */
7028 sym_hashes
= elf_sym_hashes (input_bfd
);
7029 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7033 if (! o
->linker_mark
)
7035 /* This section was omitted from the link. */
7039 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7040 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
7043 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
7045 /* Section was created by _bfd_elf_link_create_dynamic_sections
7050 /* Get the contents of the section. They have been cached by a
7051 relaxation routine. Note that o is a section in an input
7052 file, so the contents field will not have been set by any of
7053 the routines which work on output files. */
7054 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
7055 contents
= elf_section_data (o
)->this_hdr
.contents
;
7058 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
7060 contents
= finfo
->contents
;
7061 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
7065 if ((o
->flags
& SEC_RELOC
) != 0)
7067 Elf_Internal_Rela
*internal_relocs
;
7068 bfd_vma r_type_mask
;
7071 /* Get the swapped relocs. */
7073 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7074 finfo
->internal_relocs
, FALSE
);
7075 if (internal_relocs
== NULL
7076 && o
->reloc_count
> 0)
7079 if (bed
->s
->arch_size
== 32)
7086 r_type_mask
= 0xffffffff;
7090 /* Run through the relocs looking for any against symbols
7091 from discarded sections and section symbols from
7092 removed link-once sections. Complain about relocs
7093 against discarded sections. Zero relocs against removed
7094 link-once sections. */
7095 if (!elf_section_ignore_discarded_relocs (o
))
7097 Elf_Internal_Rela
*rel
, *relend
;
7098 unsigned int action
= (*bed
->action_discarded
) (o
);
7100 rel
= internal_relocs
;
7101 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7102 for ( ; rel
< relend
; rel
++)
7104 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7105 asection
**ps
, *sec
;
7106 struct elf_link_hash_entry
*h
= NULL
;
7107 const char *sym_name
;
7109 if (r_symndx
== STN_UNDEF
)
7112 if (r_symndx
>= locsymcount
7113 || (elf_bad_symtab (input_bfd
)
7114 && finfo
->sections
[r_symndx
] == NULL
))
7116 h
= sym_hashes
[r_symndx
- extsymoff
];
7118 /* Badly formatted input files can contain relocs that
7119 reference non-existant symbols. Check here so that
7120 we do not seg fault. */
7125 sprintf_vma (buffer
, rel
->r_info
);
7126 (*_bfd_error_handler
)
7127 (_("error: %B contains a reloc (0x%s) for section %A "
7128 "that references a non-existent global symbol"),
7129 input_bfd
, o
, buffer
);
7130 bfd_set_error (bfd_error_bad_value
);
7134 while (h
->root
.type
== bfd_link_hash_indirect
7135 || h
->root
.type
== bfd_link_hash_warning
)
7136 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7138 if (h
->root
.type
!= bfd_link_hash_defined
7139 && h
->root
.type
!= bfd_link_hash_defweak
)
7142 ps
= &h
->root
.u
.def
.section
;
7143 sym_name
= h
->root
.root
.string
;
7147 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7148 ps
= &finfo
->sections
[r_symndx
];
7149 sym_name
= bfd_elf_sym_name (input_bfd
,
7154 /* Complain if the definition comes from a
7155 discarded section. */
7156 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7158 BFD_ASSERT (r_symndx
!= 0);
7159 if (action
& COMPLAIN
)
7160 (*finfo
->info
->callbacks
->einfo
)
7161 (_("%X`%s' referenced in section `%A' of %B: "
7162 "defined in discarded section `%A' of %B\n"),
7163 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7165 /* Try to do the best we can to support buggy old
7166 versions of gcc. Pretend that the symbol is
7167 really defined in the kept linkonce section.
7168 FIXME: This is quite broken. Modifying the
7169 symbol here means we will be changing all later
7170 uses of the symbol, not just in this section. */
7171 if (action
& PRETEND
)
7175 kept
= _bfd_elf_check_kept_section (sec
);
7183 /* Remove the symbol reference from the reloc, but
7184 don't kill the reloc completely. This is so that
7185 a zero value will be written into the section,
7186 which may have non-zero contents put there by the
7187 assembler. Zero in things like an eh_frame fde
7188 pc_begin allows stack unwinders to recognize the
7190 rel
->r_info
&= r_type_mask
;
7196 /* Relocate the section by invoking a back end routine.
7198 The back end routine is responsible for adjusting the
7199 section contents as necessary, and (if using Rela relocs
7200 and generating a relocatable output file) adjusting the
7201 reloc addend as necessary.
7203 The back end routine does not have to worry about setting
7204 the reloc address or the reloc symbol index.
7206 The back end routine is given a pointer to the swapped in
7207 internal symbols, and can access the hash table entries
7208 for the external symbols via elf_sym_hashes (input_bfd).
7210 When generating relocatable output, the back end routine
7211 must handle STB_LOCAL/STT_SECTION symbols specially. The
7212 output symbol is going to be a section symbol
7213 corresponding to the output section, which will require
7214 the addend to be adjusted. */
7216 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7217 input_bfd
, o
, contents
,
7225 Elf_Internal_Rela
*irela
;
7226 Elf_Internal_Rela
*irelaend
;
7227 bfd_vma last_offset
;
7228 struct elf_link_hash_entry
**rel_hash
;
7229 struct elf_link_hash_entry
**rel_hash_list
;
7230 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7231 unsigned int next_erel
;
7232 bfd_boolean rela_normal
;
7234 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7235 rela_normal
= (bed
->rela_normal
7236 && (input_rel_hdr
->sh_entsize
7237 == bed
->s
->sizeof_rela
));
7239 /* Adjust the reloc addresses and symbol indices. */
7241 irela
= internal_relocs
;
7242 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7243 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7244 + elf_section_data (o
->output_section
)->rel_count
7245 + elf_section_data (o
->output_section
)->rel_count2
);
7246 rel_hash_list
= rel_hash
;
7247 last_offset
= o
->output_offset
;
7248 if (!finfo
->info
->relocatable
)
7249 last_offset
+= o
->output_section
->vma
;
7250 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7252 unsigned long r_symndx
;
7254 Elf_Internal_Sym sym
;
7256 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7262 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7265 if (irela
->r_offset
>= (bfd_vma
) -2)
7267 /* This is a reloc for a deleted entry or somesuch.
7268 Turn it into an R_*_NONE reloc, at the same
7269 offset as the last reloc. elf_eh_frame.c and
7270 bfd_elf_discard_info rely on reloc offsets
7272 irela
->r_offset
= last_offset
;
7274 irela
->r_addend
= 0;
7278 irela
->r_offset
+= o
->output_offset
;
7280 /* Relocs in an executable have to be virtual addresses. */
7281 if (!finfo
->info
->relocatable
)
7282 irela
->r_offset
+= o
->output_section
->vma
;
7284 last_offset
= irela
->r_offset
;
7286 r_symndx
= irela
->r_info
>> r_sym_shift
;
7287 if (r_symndx
== STN_UNDEF
)
7290 if (r_symndx
>= locsymcount
7291 || (elf_bad_symtab (input_bfd
)
7292 && finfo
->sections
[r_symndx
] == NULL
))
7294 struct elf_link_hash_entry
*rh
;
7297 /* This is a reloc against a global symbol. We
7298 have not yet output all the local symbols, so
7299 we do not know the symbol index of any global
7300 symbol. We set the rel_hash entry for this
7301 reloc to point to the global hash table entry
7302 for this symbol. The symbol index is then
7303 set at the end of bfd_elf_final_link. */
7304 indx
= r_symndx
- extsymoff
;
7305 rh
= elf_sym_hashes (input_bfd
)[indx
];
7306 while (rh
->root
.type
== bfd_link_hash_indirect
7307 || rh
->root
.type
== bfd_link_hash_warning
)
7308 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7310 /* Setting the index to -2 tells
7311 elf_link_output_extsym that this symbol is
7313 BFD_ASSERT (rh
->indx
< 0);
7321 /* This is a reloc against a local symbol. */
7324 sym
= isymbuf
[r_symndx
];
7325 sec
= finfo
->sections
[r_symndx
];
7326 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7328 /* I suppose the backend ought to fill in the
7329 section of any STT_SECTION symbol against a
7330 processor specific section. */
7332 if (bfd_is_abs_section (sec
))
7334 else if (sec
== NULL
|| sec
->owner
== NULL
)
7336 bfd_set_error (bfd_error_bad_value
);
7341 asection
*osec
= sec
->output_section
;
7343 /* If we have discarded a section, the output
7344 section will be the absolute section. In
7345 case of discarded link-once and discarded
7346 SEC_MERGE sections, use the kept section. */
7347 if (bfd_is_abs_section (osec
)
7348 && sec
->kept_section
!= NULL
7349 && sec
->kept_section
->output_section
!= NULL
)
7351 osec
= sec
->kept_section
->output_section
;
7352 irela
->r_addend
-= osec
->vma
;
7355 if (!bfd_is_abs_section (osec
))
7357 r_symndx
= osec
->target_index
;
7358 BFD_ASSERT (r_symndx
!= 0);
7362 /* Adjust the addend according to where the
7363 section winds up in the output section. */
7365 irela
->r_addend
+= sec
->output_offset
;
7369 if (finfo
->indices
[r_symndx
] == -1)
7371 unsigned long shlink
;
7375 if (finfo
->info
->strip
== strip_all
)
7377 /* You can't do ld -r -s. */
7378 bfd_set_error (bfd_error_invalid_operation
);
7382 /* This symbol was skipped earlier, but
7383 since it is needed by a reloc, we
7384 must output it now. */
7385 shlink
= symtab_hdr
->sh_link
;
7386 name
= (bfd_elf_string_from_elf_section
7387 (input_bfd
, shlink
, sym
.st_name
));
7391 osec
= sec
->output_section
;
7393 _bfd_elf_section_from_bfd_section (output_bfd
,
7395 if (sym
.st_shndx
== SHN_BAD
)
7398 sym
.st_value
+= sec
->output_offset
;
7399 if (! finfo
->info
->relocatable
)
7401 sym
.st_value
+= osec
->vma
;
7402 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7404 /* STT_TLS symbols are relative to PT_TLS
7406 BFD_ASSERT (elf_hash_table (finfo
->info
)
7408 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7413 finfo
->indices
[r_symndx
]
7414 = bfd_get_symcount (output_bfd
);
7416 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7421 r_symndx
= finfo
->indices
[r_symndx
];
7424 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7425 | (irela
->r_info
& r_type_mask
));
7428 /* Swap out the relocs. */
7429 if (input_rel_hdr
->sh_size
!= 0
7430 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7436 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7437 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7439 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7440 * bed
->s
->int_rels_per_ext_rel
);
7441 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7442 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7451 /* Write out the modified section contents. */
7452 if (bed
->elf_backend_write_section
7453 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7455 /* Section written out. */
7457 else switch (o
->sec_info_type
)
7459 case ELF_INFO_TYPE_STABS
:
7460 if (! (_bfd_write_section_stabs
7462 &elf_hash_table (finfo
->info
)->stab_info
,
7463 o
, &elf_section_data (o
)->sec_info
, contents
)))
7466 case ELF_INFO_TYPE_MERGE
:
7467 if (! _bfd_write_merged_section (output_bfd
, o
,
7468 elf_section_data (o
)->sec_info
))
7471 case ELF_INFO_TYPE_EH_FRAME
:
7473 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7480 if (! (o
->flags
& SEC_EXCLUDE
)
7481 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7483 (file_ptr
) o
->output_offset
,
7494 /* Generate a reloc when linking an ELF file. This is a reloc
7495 requested by the linker, and does not come from any input file. This
7496 is used to build constructor and destructor tables when linking
7500 elf_reloc_link_order (bfd
*output_bfd
,
7501 struct bfd_link_info
*info
,
7502 asection
*output_section
,
7503 struct bfd_link_order
*link_order
)
7505 reloc_howto_type
*howto
;
7509 struct elf_link_hash_entry
**rel_hash_ptr
;
7510 Elf_Internal_Shdr
*rel_hdr
;
7511 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7512 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7516 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7519 bfd_set_error (bfd_error_bad_value
);
7523 addend
= link_order
->u
.reloc
.p
->addend
;
7525 /* Figure out the symbol index. */
7526 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7527 + elf_section_data (output_section
)->rel_count
7528 + elf_section_data (output_section
)->rel_count2
);
7529 if (link_order
->type
== bfd_section_reloc_link_order
)
7531 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7532 BFD_ASSERT (indx
!= 0);
7533 *rel_hash_ptr
= NULL
;
7537 struct elf_link_hash_entry
*h
;
7539 /* Treat a reloc against a defined symbol as though it were
7540 actually against the section. */
7541 h
= ((struct elf_link_hash_entry
*)
7542 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7543 link_order
->u
.reloc
.p
->u
.name
,
7544 FALSE
, FALSE
, TRUE
));
7546 && (h
->root
.type
== bfd_link_hash_defined
7547 || h
->root
.type
== bfd_link_hash_defweak
))
7551 section
= h
->root
.u
.def
.section
;
7552 indx
= section
->output_section
->target_index
;
7553 *rel_hash_ptr
= NULL
;
7554 /* It seems that we ought to add the symbol value to the
7555 addend here, but in practice it has already been added
7556 because it was passed to constructor_callback. */
7557 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7561 /* Setting the index to -2 tells elf_link_output_extsym that
7562 this symbol is used by a reloc. */
7569 if (! ((*info
->callbacks
->unattached_reloc
)
7570 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7576 /* If this is an inplace reloc, we must write the addend into the
7578 if (howto
->partial_inplace
&& addend
!= 0)
7581 bfd_reloc_status_type rstat
;
7584 const char *sym_name
;
7586 size
= bfd_get_reloc_size (howto
);
7587 buf
= bfd_zmalloc (size
);
7590 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7597 case bfd_reloc_outofrange
:
7600 case bfd_reloc_overflow
:
7601 if (link_order
->type
== bfd_section_reloc_link_order
)
7602 sym_name
= bfd_section_name (output_bfd
,
7603 link_order
->u
.reloc
.p
->u
.section
);
7605 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7606 if (! ((*info
->callbacks
->reloc_overflow
)
7607 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7608 NULL
, (bfd_vma
) 0)))
7615 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7616 link_order
->offset
, size
);
7622 /* The address of a reloc is relative to the section in a
7623 relocatable file, and is a virtual address in an executable
7625 offset
= link_order
->offset
;
7626 if (! info
->relocatable
)
7627 offset
+= output_section
->vma
;
7629 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7631 irel
[i
].r_offset
= offset
;
7633 irel
[i
].r_addend
= 0;
7635 if (bed
->s
->arch_size
== 32)
7636 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7638 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7640 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7641 erel
= rel_hdr
->contents
;
7642 if (rel_hdr
->sh_type
== SHT_REL
)
7644 erel
+= (elf_section_data (output_section
)->rel_count
7645 * bed
->s
->sizeof_rel
);
7646 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7650 irel
[0].r_addend
= addend
;
7651 erel
+= (elf_section_data (output_section
)->rel_count
7652 * bed
->s
->sizeof_rela
);
7653 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7656 ++elf_section_data (output_section
)->rel_count
;
7662 /* Get the output vma of the section pointed to by the sh_link field. */
7665 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7667 Elf_Internal_Shdr
**elf_shdrp
;
7671 s
= p
->u
.indirect
.section
;
7672 elf_shdrp
= elf_elfsections (s
->owner
);
7673 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7674 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7676 The Intel C compiler generates SHT_IA_64_UNWIND with
7677 SHF_LINK_ORDER. But it doesn't set the sh_link or
7678 sh_info fields. Hence we could get the situation
7679 where elfsec is 0. */
7682 const struct elf_backend_data
*bed
7683 = get_elf_backend_data (s
->owner
);
7684 if (bed
->link_order_error_handler
)
7685 bed
->link_order_error_handler
7686 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7691 s
= elf_shdrp
[elfsec
]->bfd_section
;
7692 return s
->output_section
->vma
+ s
->output_offset
;
7697 /* Compare two sections based on the locations of the sections they are
7698 linked to. Used by elf_fixup_link_order. */
7701 compare_link_order (const void * a
, const void * b
)
7706 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7707 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7714 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7715 order as their linked sections. Returns false if this could not be done
7716 because an output section includes both ordered and unordered
7717 sections. Ideally we'd do this in the linker proper. */
7720 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7725 struct bfd_link_order
*p
;
7727 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7729 struct bfd_link_order
**sections
;
7730 asection
*s
, *other_sec
, *linkorder_sec
;
7734 linkorder_sec
= NULL
;
7737 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7739 if (p
->type
== bfd_indirect_link_order
)
7741 s
= p
->u
.indirect
.section
;
7743 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7744 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
7745 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
7746 && elfsec
< elf_numsections (sub
)
7747 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7761 if (seen_other
&& seen_linkorder
)
7763 if (other_sec
&& linkorder_sec
)
7764 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7766 linkorder_sec
->owner
, other_sec
,
7769 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7771 bfd_set_error (bfd_error_bad_value
);
7776 if (!seen_linkorder
)
7779 sections
= (struct bfd_link_order
**)
7780 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7783 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7785 sections
[seen_linkorder
++] = p
;
7787 /* Sort the input sections in the order of their linked section. */
7788 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7789 compare_link_order
);
7791 /* Change the offsets of the sections. */
7793 for (n
= 0; n
< seen_linkorder
; n
++)
7795 s
= sections
[n
]->u
.indirect
.section
;
7796 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7797 s
->output_offset
= offset
;
7798 sections
[n
]->offset
= offset
;
7799 offset
+= sections
[n
]->size
;
7806 /* Do the final step of an ELF link. */
7809 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7811 bfd_boolean dynamic
;
7812 bfd_boolean emit_relocs
;
7814 struct elf_final_link_info finfo
;
7815 register asection
*o
;
7816 register struct bfd_link_order
*p
;
7818 bfd_size_type max_contents_size
;
7819 bfd_size_type max_external_reloc_size
;
7820 bfd_size_type max_internal_reloc_count
;
7821 bfd_size_type max_sym_count
;
7822 bfd_size_type max_sym_shndx_count
;
7824 Elf_Internal_Sym elfsym
;
7826 Elf_Internal_Shdr
*symtab_hdr
;
7827 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7828 Elf_Internal_Shdr
*symstrtab_hdr
;
7829 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7830 struct elf_outext_info eoinfo
;
7832 size_t relativecount
= 0;
7833 asection
*reldyn
= 0;
7836 if (! is_elf_hash_table (info
->hash
))
7840 abfd
->flags
|= DYNAMIC
;
7842 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7843 dynobj
= elf_hash_table (info
)->dynobj
;
7845 emit_relocs
= (info
->relocatable
7846 || info
->emitrelocations
);
7849 finfo
.output_bfd
= abfd
;
7850 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7851 if (finfo
.symstrtab
== NULL
)
7856 finfo
.dynsym_sec
= NULL
;
7857 finfo
.hash_sec
= NULL
;
7858 finfo
.symver_sec
= NULL
;
7862 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7863 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7864 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7865 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7866 /* Note that it is OK if symver_sec is NULL. */
7869 finfo
.contents
= NULL
;
7870 finfo
.external_relocs
= NULL
;
7871 finfo
.internal_relocs
= NULL
;
7872 finfo
.external_syms
= NULL
;
7873 finfo
.locsym_shndx
= NULL
;
7874 finfo
.internal_syms
= NULL
;
7875 finfo
.indices
= NULL
;
7876 finfo
.sections
= NULL
;
7877 finfo
.symbuf
= NULL
;
7878 finfo
.symshndxbuf
= NULL
;
7879 finfo
.symbuf_count
= 0;
7880 finfo
.shndxbuf_size
= 0;
7882 /* Count up the number of relocations we will output for each output
7883 section, so that we know the sizes of the reloc sections. We
7884 also figure out some maximum sizes. */
7885 max_contents_size
= 0;
7886 max_external_reloc_size
= 0;
7887 max_internal_reloc_count
= 0;
7889 max_sym_shndx_count
= 0;
7891 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7893 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7896 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7898 unsigned int reloc_count
= 0;
7899 struct bfd_elf_section_data
*esdi
= NULL
;
7900 unsigned int *rel_count1
;
7902 if (p
->type
== bfd_section_reloc_link_order
7903 || p
->type
== bfd_symbol_reloc_link_order
)
7905 else if (p
->type
== bfd_indirect_link_order
)
7909 sec
= p
->u
.indirect
.section
;
7910 esdi
= elf_section_data (sec
);
7912 /* Mark all sections which are to be included in the
7913 link. This will normally be every section. We need
7914 to do this so that we can identify any sections which
7915 the linker has decided to not include. */
7916 sec
->linker_mark
= TRUE
;
7918 if (sec
->flags
& SEC_MERGE
)
7921 if (info
->relocatable
|| info
->emitrelocations
)
7922 reloc_count
= sec
->reloc_count
;
7923 else if (bed
->elf_backend_count_relocs
)
7925 Elf_Internal_Rela
* relocs
;
7927 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7930 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7932 if (elf_section_data (o
)->relocs
!= relocs
)
7936 if (sec
->rawsize
> max_contents_size
)
7937 max_contents_size
= sec
->rawsize
;
7938 if (sec
->size
> max_contents_size
)
7939 max_contents_size
= sec
->size
;
7941 /* We are interested in just local symbols, not all
7943 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7944 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7948 if (elf_bad_symtab (sec
->owner
))
7949 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7950 / bed
->s
->sizeof_sym
);
7952 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7954 if (sym_count
> max_sym_count
)
7955 max_sym_count
= sym_count
;
7957 if (sym_count
> max_sym_shndx_count
7958 && elf_symtab_shndx (sec
->owner
) != 0)
7959 max_sym_shndx_count
= sym_count
;
7961 if ((sec
->flags
& SEC_RELOC
) != 0)
7965 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7966 if (ext_size
> max_external_reloc_size
)
7967 max_external_reloc_size
= ext_size
;
7968 if (sec
->reloc_count
> max_internal_reloc_count
)
7969 max_internal_reloc_count
= sec
->reloc_count
;
7974 if (reloc_count
== 0)
7977 o
->reloc_count
+= reloc_count
;
7979 /* MIPS may have a mix of REL and RELA relocs on sections.
7980 To support this curious ABI we keep reloc counts in
7981 elf_section_data too. We must be careful to add the
7982 relocations from the input section to the right output
7983 count. FIXME: Get rid of one count. We have
7984 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7985 rel_count1
= &esdo
->rel_count
;
7988 bfd_boolean same_size
;
7989 bfd_size_type entsize1
;
7991 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7992 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7993 || entsize1
== bed
->s
->sizeof_rela
);
7994 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7997 rel_count1
= &esdo
->rel_count2
;
7999 if (esdi
->rel_hdr2
!= NULL
)
8001 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
8002 unsigned int alt_count
;
8003 unsigned int *rel_count2
;
8005 BFD_ASSERT (entsize2
!= entsize1
8006 && (entsize2
== bed
->s
->sizeof_rel
8007 || entsize2
== bed
->s
->sizeof_rela
));
8009 rel_count2
= &esdo
->rel_count2
;
8011 rel_count2
= &esdo
->rel_count
;
8013 /* The following is probably too simplistic if the
8014 backend counts output relocs unusually. */
8015 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
8016 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
8017 *rel_count2
+= alt_count
;
8018 reloc_count
-= alt_count
;
8021 *rel_count1
+= reloc_count
;
8024 if (o
->reloc_count
> 0)
8025 o
->flags
|= SEC_RELOC
;
8028 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8029 set it (this is probably a bug) and if it is set
8030 assign_section_numbers will create a reloc section. */
8031 o
->flags
&=~ SEC_RELOC
;
8034 /* If the SEC_ALLOC flag is not set, force the section VMA to
8035 zero. This is done in elf_fake_sections as well, but forcing
8036 the VMA to 0 here will ensure that relocs against these
8037 sections are handled correctly. */
8038 if ((o
->flags
& SEC_ALLOC
) == 0
8039 && ! o
->user_set_vma
)
8043 if (! info
->relocatable
&& merged
)
8044 elf_link_hash_traverse (elf_hash_table (info
),
8045 _bfd_elf_link_sec_merge_syms
, abfd
);
8047 /* Figure out the file positions for everything but the symbol table
8048 and the relocs. We set symcount to force assign_section_numbers
8049 to create a symbol table. */
8050 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
8051 BFD_ASSERT (! abfd
->output_has_begun
);
8052 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
8055 /* Set sizes, and assign file positions for reloc sections. */
8056 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8058 if ((o
->flags
& SEC_RELOC
) != 0)
8060 if (!(_bfd_elf_link_size_reloc_section
8061 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
8064 if (elf_section_data (o
)->rel_hdr2
8065 && !(_bfd_elf_link_size_reloc_section
8066 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
8070 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8071 to count upwards while actually outputting the relocations. */
8072 elf_section_data (o
)->rel_count
= 0;
8073 elf_section_data (o
)->rel_count2
= 0;
8076 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8078 /* We have now assigned file positions for all the sections except
8079 .symtab and .strtab. We start the .symtab section at the current
8080 file position, and write directly to it. We build the .strtab
8081 section in memory. */
8082 bfd_get_symcount (abfd
) = 0;
8083 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8084 /* sh_name is set in prep_headers. */
8085 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8086 /* sh_flags, sh_addr and sh_size all start off zero. */
8087 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8088 /* sh_link is set in assign_section_numbers. */
8089 /* sh_info is set below. */
8090 /* sh_offset is set just below. */
8091 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8093 off
= elf_tdata (abfd
)->next_file_pos
;
8094 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8096 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8097 incorrect. We do not yet know the size of the .symtab section.
8098 We correct next_file_pos below, after we do know the size. */
8100 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8101 continuously seeking to the right position in the file. */
8102 if (! info
->keep_memory
|| max_sym_count
< 20)
8103 finfo
.symbuf_size
= 20;
8105 finfo
.symbuf_size
= max_sym_count
;
8106 amt
= finfo
.symbuf_size
;
8107 amt
*= bed
->s
->sizeof_sym
;
8108 finfo
.symbuf
= bfd_malloc (amt
);
8109 if (finfo
.symbuf
== NULL
)
8111 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8113 /* Wild guess at number of output symbols. realloc'd as needed. */
8114 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8115 finfo
.shndxbuf_size
= amt
;
8116 amt
*= sizeof (Elf_External_Sym_Shndx
);
8117 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8118 if (finfo
.symshndxbuf
== NULL
)
8122 /* Start writing out the symbol table. The first symbol is always a
8124 if (info
->strip
!= strip_all
8127 elfsym
.st_value
= 0;
8130 elfsym
.st_other
= 0;
8131 elfsym
.st_shndx
= SHN_UNDEF
;
8132 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8137 /* Output a symbol for each section. We output these even if we are
8138 discarding local symbols, since they are used for relocs. These
8139 symbols have no names. We store the index of each one in the
8140 index field of the section, so that we can find it again when
8141 outputting relocs. */
8142 if (info
->strip
!= strip_all
8146 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8147 elfsym
.st_other
= 0;
8148 elfsym
.st_value
= 0;
8149 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8151 o
= bfd_section_from_elf_index (abfd
, i
);
8154 o
->target_index
= bfd_get_symcount (abfd
);
8155 elfsym
.st_shndx
= i
;
8156 if (!info
->relocatable
)
8157 elfsym
.st_value
= o
->vma
;
8158 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8161 if (i
== SHN_LORESERVE
- 1)
8162 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8166 /* Allocate some memory to hold information read in from the input
8168 if (max_contents_size
!= 0)
8170 finfo
.contents
= bfd_malloc (max_contents_size
);
8171 if (finfo
.contents
== NULL
)
8175 if (max_external_reloc_size
!= 0)
8177 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8178 if (finfo
.external_relocs
== NULL
)
8182 if (max_internal_reloc_count
!= 0)
8184 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8185 amt
*= sizeof (Elf_Internal_Rela
);
8186 finfo
.internal_relocs
= bfd_malloc (amt
);
8187 if (finfo
.internal_relocs
== NULL
)
8191 if (max_sym_count
!= 0)
8193 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8194 finfo
.external_syms
= bfd_malloc (amt
);
8195 if (finfo
.external_syms
== NULL
)
8198 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8199 finfo
.internal_syms
= bfd_malloc (amt
);
8200 if (finfo
.internal_syms
== NULL
)
8203 amt
= max_sym_count
* sizeof (long);
8204 finfo
.indices
= bfd_malloc (amt
);
8205 if (finfo
.indices
== NULL
)
8208 amt
= max_sym_count
* sizeof (asection
*);
8209 finfo
.sections
= bfd_malloc (amt
);
8210 if (finfo
.sections
== NULL
)
8214 if (max_sym_shndx_count
!= 0)
8216 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8217 finfo
.locsym_shndx
= bfd_malloc (amt
);
8218 if (finfo
.locsym_shndx
== NULL
)
8222 if (elf_hash_table (info
)->tls_sec
)
8224 bfd_vma base
, end
= 0;
8227 for (sec
= elf_hash_table (info
)->tls_sec
;
8228 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8231 bfd_size_type size
= sec
->size
;
8234 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8236 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8238 size
= o
->offset
+ o
->size
;
8240 end
= sec
->vma
+ size
;
8242 base
= elf_hash_table (info
)->tls_sec
->vma
;
8243 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8244 elf_hash_table (info
)->tls_size
= end
- base
;
8247 /* Reorder SHF_LINK_ORDER sections. */
8248 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8250 if (!elf_fixup_link_order (abfd
, o
))
8254 /* Since ELF permits relocations to be against local symbols, we
8255 must have the local symbols available when we do the relocations.
8256 Since we would rather only read the local symbols once, and we
8257 would rather not keep them in memory, we handle all the
8258 relocations for a single input file at the same time.
8260 Unfortunately, there is no way to know the total number of local
8261 symbols until we have seen all of them, and the local symbol
8262 indices precede the global symbol indices. This means that when
8263 we are generating relocatable output, and we see a reloc against
8264 a global symbol, we can not know the symbol index until we have
8265 finished examining all the local symbols to see which ones we are
8266 going to output. To deal with this, we keep the relocations in
8267 memory, and don't output them until the end of the link. This is
8268 an unfortunate waste of memory, but I don't see a good way around
8269 it. Fortunately, it only happens when performing a relocatable
8270 link, which is not the common case. FIXME: If keep_memory is set
8271 we could write the relocs out and then read them again; I don't
8272 know how bad the memory loss will be. */
8274 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8275 sub
->output_has_begun
= FALSE
;
8276 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8278 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8280 if (p
->type
== bfd_indirect_link_order
8281 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8282 == bfd_target_elf_flavour
)
8283 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8285 if (! sub
->output_has_begun
)
8287 if (! elf_link_input_bfd (&finfo
, sub
))
8289 sub
->output_has_begun
= TRUE
;
8292 else if (p
->type
== bfd_section_reloc_link_order
8293 || p
->type
== bfd_symbol_reloc_link_order
)
8295 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8300 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8306 /* Output any global symbols that got converted to local in a
8307 version script or due to symbol visibility. We do this in a
8308 separate step since ELF requires all local symbols to appear
8309 prior to any global symbols. FIXME: We should only do this if
8310 some global symbols were, in fact, converted to become local.
8311 FIXME: Will this work correctly with the Irix 5 linker? */
8312 eoinfo
.failed
= FALSE
;
8313 eoinfo
.finfo
= &finfo
;
8314 eoinfo
.localsyms
= TRUE
;
8315 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8320 /* If backend needs to output some local symbols not present in the hash
8321 table, do it now. */
8322 if (bed
->elf_backend_output_arch_local_syms
)
8324 typedef bfd_boolean (*out_sym_func
)
8325 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8326 struct elf_link_hash_entry
*);
8328 if (! ((*bed
->elf_backend_output_arch_local_syms
)
8329 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8333 /* That wrote out all the local symbols. Finish up the symbol table
8334 with the global symbols. Even if we want to strip everything we
8335 can, we still need to deal with those global symbols that got
8336 converted to local in a version script. */
8338 /* The sh_info field records the index of the first non local symbol. */
8339 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8342 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8344 Elf_Internal_Sym sym
;
8345 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8346 long last_local
= 0;
8348 /* Write out the section symbols for the output sections. */
8349 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8355 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8358 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8364 dynindx
= elf_section_data (s
)->dynindx
;
8367 indx
= elf_section_data (s
)->this_idx
;
8368 BFD_ASSERT (indx
> 0);
8369 sym
.st_shndx
= indx
;
8370 if (! check_dynsym (abfd
, &sym
))
8372 sym
.st_value
= s
->vma
;
8373 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8374 if (last_local
< dynindx
)
8375 last_local
= dynindx
;
8376 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8380 /* Write out the local dynsyms. */
8381 if (elf_hash_table (info
)->dynlocal
)
8383 struct elf_link_local_dynamic_entry
*e
;
8384 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8389 sym
.st_size
= e
->isym
.st_size
;
8390 sym
.st_other
= e
->isym
.st_other
;
8392 /* Copy the internal symbol as is.
8393 Note that we saved a word of storage and overwrote
8394 the original st_name with the dynstr_index. */
8397 if (e
->isym
.st_shndx
!= SHN_UNDEF
8398 && (e
->isym
.st_shndx
< SHN_LORESERVE
8399 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8401 s
= bfd_section_from_elf_index (e
->input_bfd
,
8405 elf_section_data (s
->output_section
)->this_idx
;
8406 if (! check_dynsym (abfd
, &sym
))
8408 sym
.st_value
= (s
->output_section
->vma
8410 + e
->isym
.st_value
);
8413 if (last_local
< e
->dynindx
)
8414 last_local
= e
->dynindx
;
8416 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8417 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8421 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8425 /* We get the global symbols from the hash table. */
8426 eoinfo
.failed
= FALSE
;
8427 eoinfo
.localsyms
= FALSE
;
8428 eoinfo
.finfo
= &finfo
;
8429 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8434 /* If backend needs to output some symbols not present in the hash
8435 table, do it now. */
8436 if (bed
->elf_backend_output_arch_syms
)
8438 typedef bfd_boolean (*out_sym_func
)
8439 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8440 struct elf_link_hash_entry
*);
8442 if (! ((*bed
->elf_backend_output_arch_syms
)
8443 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8447 /* Flush all symbols to the file. */
8448 if (! elf_link_flush_output_syms (&finfo
, bed
))
8451 /* Now we know the size of the symtab section. */
8452 off
+= symtab_hdr
->sh_size
;
8454 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8455 if (symtab_shndx_hdr
->sh_name
!= 0)
8457 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8458 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8459 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8460 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8461 symtab_shndx_hdr
->sh_size
= amt
;
8463 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8466 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8467 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8472 /* Finish up and write out the symbol string table (.strtab)
8474 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8475 /* sh_name was set in prep_headers. */
8476 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8477 symstrtab_hdr
->sh_flags
= 0;
8478 symstrtab_hdr
->sh_addr
= 0;
8479 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8480 symstrtab_hdr
->sh_entsize
= 0;
8481 symstrtab_hdr
->sh_link
= 0;
8482 symstrtab_hdr
->sh_info
= 0;
8483 /* sh_offset is set just below. */
8484 symstrtab_hdr
->sh_addralign
= 1;
8486 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8487 elf_tdata (abfd
)->next_file_pos
= off
;
8489 if (bfd_get_symcount (abfd
) > 0)
8491 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8492 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8496 /* Adjust the relocs to have the correct symbol indices. */
8497 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8499 if ((o
->flags
& SEC_RELOC
) == 0)
8502 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8503 elf_section_data (o
)->rel_count
,
8504 elf_section_data (o
)->rel_hashes
);
8505 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8506 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8507 elf_section_data (o
)->rel_count2
,
8508 (elf_section_data (o
)->rel_hashes
8509 + elf_section_data (o
)->rel_count
));
8511 /* Set the reloc_count field to 0 to prevent write_relocs from
8512 trying to swap the relocs out itself. */
8516 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8517 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8519 /* If we are linking against a dynamic object, or generating a
8520 shared library, finish up the dynamic linking information. */
8523 bfd_byte
*dyncon
, *dynconend
;
8525 /* Fix up .dynamic entries. */
8526 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8527 BFD_ASSERT (o
!= NULL
);
8529 dyncon
= o
->contents
;
8530 dynconend
= o
->contents
+ o
->size
;
8531 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8533 Elf_Internal_Dyn dyn
;
8537 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8544 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8546 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8548 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8549 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8552 dyn
.d_un
.d_val
= relativecount
;
8559 name
= info
->init_function
;
8562 name
= info
->fini_function
;
8565 struct elf_link_hash_entry
*h
;
8567 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8568 FALSE
, FALSE
, TRUE
);
8570 && (h
->root
.type
== bfd_link_hash_defined
8571 || h
->root
.type
== bfd_link_hash_defweak
))
8573 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8574 o
= h
->root
.u
.def
.section
;
8575 if (o
->output_section
!= NULL
)
8576 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8577 + o
->output_offset
);
8580 /* The symbol is imported from another shared
8581 library and does not apply to this one. */
8589 case DT_PREINIT_ARRAYSZ
:
8590 name
= ".preinit_array";
8592 case DT_INIT_ARRAYSZ
:
8593 name
= ".init_array";
8595 case DT_FINI_ARRAYSZ
:
8596 name
= ".fini_array";
8598 o
= bfd_get_section_by_name (abfd
, name
);
8601 (*_bfd_error_handler
)
8602 (_("%B: could not find output section %s"), abfd
, name
);
8606 (*_bfd_error_handler
)
8607 (_("warning: %s section has zero size"), name
);
8608 dyn
.d_un
.d_val
= o
->size
;
8611 case DT_PREINIT_ARRAY
:
8612 name
= ".preinit_array";
8615 name
= ".init_array";
8618 name
= ".fini_array";
8631 name
= ".gnu.version_d";
8634 name
= ".gnu.version_r";
8637 name
= ".gnu.version";
8639 o
= bfd_get_section_by_name (abfd
, name
);
8642 (*_bfd_error_handler
)
8643 (_("%B: could not find output section %s"), abfd
, name
);
8646 dyn
.d_un
.d_ptr
= o
->vma
;
8653 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8658 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8660 Elf_Internal_Shdr
*hdr
;
8662 hdr
= elf_elfsections (abfd
)[i
];
8663 if (hdr
->sh_type
== type
8664 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8666 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8667 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8670 if (dyn
.d_un
.d_val
== 0
8671 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8672 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8678 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8682 /* If we have created any dynamic sections, then output them. */
8685 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8688 /* Check for DT_TEXTREL (late, in case the backend removes it). */
8689 if (info
->warn_shared_textrel
&& info
->shared
)
8691 bfd_byte
*dyncon
, *dynconend
;
8693 /* Fix up .dynamic entries. */
8694 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8695 BFD_ASSERT (o
!= NULL
);
8697 dyncon
= o
->contents
;
8698 dynconend
= o
->contents
+ o
->size
;
8699 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8701 Elf_Internal_Dyn dyn
;
8703 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8705 if (dyn
.d_tag
== DT_TEXTREL
)
8708 (_("warning: creating a DT_TEXTREL in a shared object."));
8714 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8716 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8718 || o
->output_section
== bfd_abs_section_ptr
)
8720 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8722 /* At this point, we are only interested in sections
8723 created by _bfd_elf_link_create_dynamic_sections. */
8726 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8728 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8730 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8732 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8734 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8736 (file_ptr
) o
->output_offset
,
8742 /* The contents of the .dynstr section are actually in a
8744 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8745 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8746 || ! _bfd_elf_strtab_emit (abfd
,
8747 elf_hash_table (info
)->dynstr
))
8753 if (info
->relocatable
)
8755 bfd_boolean failed
= FALSE
;
8757 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8762 /* If we have optimized stabs strings, output them. */
8763 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8765 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8769 if (info
->eh_frame_hdr
)
8771 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8775 if (finfo
.symstrtab
!= NULL
)
8776 _bfd_stringtab_free (finfo
.symstrtab
);
8777 if (finfo
.contents
!= NULL
)
8778 free (finfo
.contents
);
8779 if (finfo
.external_relocs
!= NULL
)
8780 free (finfo
.external_relocs
);
8781 if (finfo
.internal_relocs
!= NULL
)
8782 free (finfo
.internal_relocs
);
8783 if (finfo
.external_syms
!= NULL
)
8784 free (finfo
.external_syms
);
8785 if (finfo
.locsym_shndx
!= NULL
)
8786 free (finfo
.locsym_shndx
);
8787 if (finfo
.internal_syms
!= NULL
)
8788 free (finfo
.internal_syms
);
8789 if (finfo
.indices
!= NULL
)
8790 free (finfo
.indices
);
8791 if (finfo
.sections
!= NULL
)
8792 free (finfo
.sections
);
8793 if (finfo
.symbuf
!= NULL
)
8794 free (finfo
.symbuf
);
8795 if (finfo
.symshndxbuf
!= NULL
)
8796 free (finfo
.symshndxbuf
);
8797 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8799 if ((o
->flags
& SEC_RELOC
) != 0
8800 && elf_section_data (o
)->rel_hashes
!= NULL
)
8801 free (elf_section_data (o
)->rel_hashes
);
8804 elf_tdata (abfd
)->linker
= TRUE
;
8809 if (finfo
.symstrtab
!= NULL
)
8810 _bfd_stringtab_free (finfo
.symstrtab
);
8811 if (finfo
.contents
!= NULL
)
8812 free (finfo
.contents
);
8813 if (finfo
.external_relocs
!= NULL
)
8814 free (finfo
.external_relocs
);
8815 if (finfo
.internal_relocs
!= NULL
)
8816 free (finfo
.internal_relocs
);
8817 if (finfo
.external_syms
!= NULL
)
8818 free (finfo
.external_syms
);
8819 if (finfo
.locsym_shndx
!= NULL
)
8820 free (finfo
.locsym_shndx
);
8821 if (finfo
.internal_syms
!= NULL
)
8822 free (finfo
.internal_syms
);
8823 if (finfo
.indices
!= NULL
)
8824 free (finfo
.indices
);
8825 if (finfo
.sections
!= NULL
)
8826 free (finfo
.sections
);
8827 if (finfo
.symbuf
!= NULL
)
8828 free (finfo
.symbuf
);
8829 if (finfo
.symshndxbuf
!= NULL
)
8830 free (finfo
.symshndxbuf
);
8831 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8833 if ((o
->flags
& SEC_RELOC
) != 0
8834 && elf_section_data (o
)->rel_hashes
!= NULL
)
8835 free (elf_section_data (o
)->rel_hashes
);
8841 /* Garbage collect unused sections. */
8843 /* The mark phase of garbage collection. For a given section, mark
8844 it and any sections in this section's group, and all the sections
8845 which define symbols to which it refers. */
8847 typedef asection
* (*gc_mark_hook_fn
)
8848 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8849 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8852 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8854 gc_mark_hook_fn gc_mark_hook
)
8858 asection
*group_sec
;
8862 /* Mark all the sections in the group. */
8863 group_sec
= elf_section_data (sec
)->next_in_group
;
8864 if (group_sec
&& !group_sec
->gc_mark
)
8865 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8868 /* Look through the section relocs. */
8870 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8871 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8873 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8874 Elf_Internal_Shdr
*symtab_hdr
;
8875 struct elf_link_hash_entry
**sym_hashes
;
8878 bfd
*input_bfd
= sec
->owner
;
8879 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8880 Elf_Internal_Sym
*isym
= NULL
;
8883 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8884 sym_hashes
= elf_sym_hashes (input_bfd
);
8886 /* Read the local symbols. */
8887 if (elf_bad_symtab (input_bfd
))
8889 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8893 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8895 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8896 if (isym
== NULL
&& nlocsyms
!= 0)
8898 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8904 /* Read the relocations. */
8905 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8907 if (relstart
== NULL
)
8912 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8914 if (bed
->s
->arch_size
== 32)
8919 for (rel
= relstart
; rel
< relend
; rel
++)
8921 unsigned long r_symndx
;
8923 struct elf_link_hash_entry
*h
;
8925 r_symndx
= rel
->r_info
>> r_sym_shift
;
8929 if (r_symndx
>= nlocsyms
8930 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8932 h
= sym_hashes
[r_symndx
- extsymoff
];
8933 while (h
->root
.type
== bfd_link_hash_indirect
8934 || h
->root
.type
== bfd_link_hash_warning
)
8935 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8936 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8940 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8943 if (rsec
&& !rsec
->gc_mark
)
8945 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8948 rsec
->gc_mark_from_eh
= 1;
8949 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8958 if (elf_section_data (sec
)->relocs
!= relstart
)
8961 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8963 if (! info
->keep_memory
)
8966 symtab_hdr
->contents
= (unsigned char *) isym
;
8973 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8975 struct elf_gc_sweep_symbol_info
{
8976 struct bfd_link_info
*info
;
8977 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8982 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8984 if (h
->root
.type
== bfd_link_hash_warning
)
8985 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8987 if ((h
->root
.type
== bfd_link_hash_defined
8988 || h
->root
.type
== bfd_link_hash_defweak
)
8989 && !h
->root
.u
.def
.section
->gc_mark
8990 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8992 struct elf_gc_sweep_symbol_info
*inf
= data
;
8993 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8999 /* The sweep phase of garbage collection. Remove all garbage sections. */
9001 typedef bfd_boolean (*gc_sweep_hook_fn
)
9002 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
9005 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
9008 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9009 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
9010 unsigned long section_sym_count
;
9011 struct elf_gc_sweep_symbol_info sweep_info
;
9013 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9017 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9020 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9022 /* Keep debug and special sections. */
9023 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
9024 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
9030 /* Skip sweeping sections already excluded. */
9031 if (o
->flags
& SEC_EXCLUDE
)
9034 /* Since this is early in the link process, it is simple
9035 to remove a section from the output. */
9036 o
->flags
|= SEC_EXCLUDE
;
9038 /* But we also have to update some of the relocation
9039 info we collected before. */
9041 && (o
->flags
& SEC_RELOC
) != 0
9042 && o
->reloc_count
> 0
9043 && !bfd_is_abs_section (o
->output_section
))
9045 Elf_Internal_Rela
*internal_relocs
;
9049 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
9051 if (internal_relocs
== NULL
)
9054 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
9056 if (elf_section_data (o
)->relocs
!= internal_relocs
)
9057 free (internal_relocs
);
9065 /* Remove the symbols that were in the swept sections from the dynamic
9066 symbol table. GCFIXME: Anyone know how to get them out of the
9067 static symbol table as well? */
9068 sweep_info
.info
= info
;
9069 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
9070 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
9073 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
9077 /* Propagate collected vtable information. This is called through
9078 elf_link_hash_traverse. */
9081 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
9083 if (h
->root
.type
== bfd_link_hash_warning
)
9084 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9086 /* Those that are not vtables. */
9087 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9090 /* Those vtables that do not have parents, we cannot merge. */
9091 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
9094 /* If we've already been done, exit. */
9095 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
9098 /* Make sure the parent's table is up to date. */
9099 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
9101 if (h
->vtable
->used
== NULL
)
9103 /* None of this table's entries were referenced. Re-use the
9105 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
9106 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
9111 bfd_boolean
*cu
, *pu
;
9113 /* Or the parent's entries into ours. */
9114 cu
= h
->vtable
->used
;
9116 pu
= h
->vtable
->parent
->vtable
->used
;
9119 const struct elf_backend_data
*bed
;
9120 unsigned int log_file_align
;
9122 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9123 log_file_align
= bed
->s
->log_file_align
;
9124 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9139 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9142 bfd_vma hstart
, hend
;
9143 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9144 const struct elf_backend_data
*bed
;
9145 unsigned int log_file_align
;
9147 if (h
->root
.type
== bfd_link_hash_warning
)
9148 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9150 /* Take care of both those symbols that do not describe vtables as
9151 well as those that are not loaded. */
9152 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9155 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9156 || h
->root
.type
== bfd_link_hash_defweak
);
9158 sec
= h
->root
.u
.def
.section
;
9159 hstart
= h
->root
.u
.def
.value
;
9160 hend
= hstart
+ h
->size
;
9162 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9164 return *(bfd_boolean
*) okp
= FALSE
;
9165 bed
= get_elf_backend_data (sec
->owner
);
9166 log_file_align
= bed
->s
->log_file_align
;
9168 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9170 for (rel
= relstart
; rel
< relend
; ++rel
)
9171 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9173 /* If the entry is in use, do nothing. */
9175 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9177 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9178 if (h
->vtable
->used
[entry
])
9181 /* Otherwise, kill it. */
9182 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9188 /* Mark sections containing dynamically referenced symbols. When
9189 building shared libraries, we must assume that any visible symbol is
9193 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9195 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9197 if (h
->root
.type
== bfd_link_hash_warning
)
9198 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9200 if ((h
->root
.type
== bfd_link_hash_defined
9201 || h
->root
.type
== bfd_link_hash_defweak
)
9203 || (!info
->executable
9205 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9206 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9207 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9212 /* Do mark and sweep of unused sections. */
9215 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9217 bfd_boolean ok
= TRUE
;
9219 asection
* (*gc_mark_hook
)
9220 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9221 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9222 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9224 if (!bed
->can_gc_sections
9225 || info
->relocatable
9226 || info
->emitrelocations
9227 || !is_elf_hash_table (info
->hash
))
9229 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9233 /* Apply transitive closure to the vtable entry usage info. */
9234 elf_link_hash_traverse (elf_hash_table (info
),
9235 elf_gc_propagate_vtable_entries_used
,
9240 /* Kill the vtable relocations that were not used. */
9241 elf_link_hash_traverse (elf_hash_table (info
),
9242 elf_gc_smash_unused_vtentry_relocs
,
9247 /* Mark dynamically referenced symbols. */
9248 if (elf_hash_table (info
)->dynamic_sections_created
)
9249 elf_link_hash_traverse (elf_hash_table (info
),
9250 bed
->gc_mark_dynamic_ref
,
9253 /* Grovel through relocs to find out who stays ... */
9254 gc_mark_hook
= bed
->gc_mark_hook
;
9255 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9259 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9262 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9263 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9264 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9268 /* ... again for sections marked from eh_frame. */
9269 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9273 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9276 /* Keep .gcc_except_table.* if the associated .text.* is
9277 marked. This isn't very nice, but the proper solution,
9278 splitting .eh_frame up and using comdat doesn't pan out
9279 easily due to needing special relocs to handle the
9280 difference of two symbols in separate sections.
9281 Don't keep code sections referenced by .eh_frame. */
9282 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9283 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9285 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9291 len
= strlen (o
->name
+ 18) + 1;
9292 fn_name
= bfd_malloc (len
+ 6);
9293 if (fn_name
== NULL
)
9295 memcpy (fn_name
, ".text.", 6);
9296 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9297 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9299 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9303 /* If not using specially named exception table section,
9304 then keep whatever we are using. */
9305 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9310 /* ... and mark SEC_EXCLUDE for those that go. */
9311 return elf_gc_sweep (abfd
, info
);
9314 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9317 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9319 struct elf_link_hash_entry
*h
,
9322 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9323 struct elf_link_hash_entry
**search
, *child
;
9324 bfd_size_type extsymcount
;
9325 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9327 /* The sh_info field of the symtab header tells us where the
9328 external symbols start. We don't care about the local symbols at
9330 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9331 if (!elf_bad_symtab (abfd
))
9332 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9334 sym_hashes
= elf_sym_hashes (abfd
);
9335 sym_hashes_end
= sym_hashes
+ extsymcount
;
9337 /* Hunt down the child symbol, which is in this section at the same
9338 offset as the relocation. */
9339 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9341 if ((child
= *search
) != NULL
9342 && (child
->root
.type
== bfd_link_hash_defined
9343 || child
->root
.type
== bfd_link_hash_defweak
)
9344 && child
->root
.u
.def
.section
== sec
9345 && child
->root
.u
.def
.value
== offset
)
9349 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9350 abfd
, sec
, (unsigned long) offset
);
9351 bfd_set_error (bfd_error_invalid_operation
);
9357 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9363 /* This *should* only be the absolute section. It could potentially
9364 be that someone has defined a non-global vtable though, which
9365 would be bad. It isn't worth paging in the local symbols to be
9366 sure though; that case should simply be handled by the assembler. */
9368 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9371 child
->vtable
->parent
= h
;
9376 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9379 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9380 asection
*sec ATTRIBUTE_UNUSED
,
9381 struct elf_link_hash_entry
*h
,
9384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9385 unsigned int log_file_align
= bed
->s
->log_file_align
;
9389 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9394 if (addend
>= h
->vtable
->size
)
9396 size_t size
, bytes
, file_align
;
9397 bfd_boolean
*ptr
= h
->vtable
->used
;
9399 /* While the symbol is undefined, we have to be prepared to handle
9401 file_align
= 1 << log_file_align
;
9402 if (h
->root
.type
== bfd_link_hash_undefined
)
9403 size
= addend
+ file_align
;
9409 /* Oops! We've got a reference past the defined end of
9410 the table. This is probably a bug -- shall we warn? */
9411 size
= addend
+ file_align
;
9414 size
= (size
+ file_align
- 1) & -file_align
;
9416 /* Allocate one extra entry for use as a "done" flag for the
9417 consolidation pass. */
9418 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9422 ptr
= bfd_realloc (ptr
- 1, bytes
);
9428 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9429 * sizeof (bfd_boolean
));
9430 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9434 ptr
= bfd_zmalloc (bytes
);
9439 /* And arrange for that done flag to be at index -1. */
9440 h
->vtable
->used
= ptr
+ 1;
9441 h
->vtable
->size
= size
;
9444 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9449 struct alloc_got_off_arg
{
9451 unsigned int got_elt_size
;
9454 /* We need a special top-level link routine to convert got reference counts
9455 to real got offsets. */
9458 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9460 struct alloc_got_off_arg
*gofarg
= arg
;
9462 if (h
->root
.type
== bfd_link_hash_warning
)
9463 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9465 if (h
->got
.refcount
> 0)
9467 h
->got
.offset
= gofarg
->gotoff
;
9468 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9471 h
->got
.offset
= (bfd_vma
) -1;
9476 /* And an accompanying bit to work out final got entry offsets once
9477 we're done. Should be called from final_link. */
9480 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9481 struct bfd_link_info
*info
)
9484 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9486 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9487 struct alloc_got_off_arg gofarg
;
9489 if (! is_elf_hash_table (info
->hash
))
9492 /* The GOT offset is relative to the .got section, but the GOT header is
9493 put into the .got.plt section, if the backend uses it. */
9494 if (bed
->want_got_plt
)
9497 gotoff
= bed
->got_header_size
;
9499 /* Do the local .got entries first. */
9500 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9502 bfd_signed_vma
*local_got
;
9503 bfd_size_type j
, locsymcount
;
9504 Elf_Internal_Shdr
*symtab_hdr
;
9506 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9509 local_got
= elf_local_got_refcounts (i
);
9513 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9514 if (elf_bad_symtab (i
))
9515 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9517 locsymcount
= symtab_hdr
->sh_info
;
9519 for (j
= 0; j
< locsymcount
; ++j
)
9521 if (local_got
[j
] > 0)
9523 local_got
[j
] = gotoff
;
9524 gotoff
+= got_elt_size
;
9527 local_got
[j
] = (bfd_vma
) -1;
9531 /* Then the global .got entries. .plt refcounts are handled by
9532 adjust_dynamic_symbol */
9533 gofarg
.gotoff
= gotoff
;
9534 gofarg
.got_elt_size
= got_elt_size
;
9535 elf_link_hash_traverse (elf_hash_table (info
),
9536 elf_gc_allocate_got_offsets
,
9541 /* Many folk need no more in the way of final link than this, once
9542 got entry reference counting is enabled. */
9545 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9547 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9550 /* Invoke the regular ELF backend linker to do all the work. */
9551 return bfd_elf_final_link (abfd
, info
);
9555 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9557 struct elf_reloc_cookie
*rcookie
= cookie
;
9559 if (rcookie
->bad_symtab
)
9560 rcookie
->rel
= rcookie
->rels
;
9562 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9564 unsigned long r_symndx
;
9566 if (! rcookie
->bad_symtab
)
9567 if (rcookie
->rel
->r_offset
> offset
)
9569 if (rcookie
->rel
->r_offset
!= offset
)
9572 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9573 if (r_symndx
== SHN_UNDEF
)
9576 if (r_symndx
>= rcookie
->locsymcount
9577 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9579 struct elf_link_hash_entry
*h
;
9581 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9583 while (h
->root
.type
== bfd_link_hash_indirect
9584 || h
->root
.type
== bfd_link_hash_warning
)
9585 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9587 if ((h
->root
.type
== bfd_link_hash_defined
9588 || h
->root
.type
== bfd_link_hash_defweak
)
9589 && elf_discarded_section (h
->root
.u
.def
.section
))
9596 /* It's not a relocation against a global symbol,
9597 but it could be a relocation against a local
9598 symbol for a discarded section. */
9600 Elf_Internal_Sym
*isym
;
9602 /* Need to: get the symbol; get the section. */
9603 isym
= &rcookie
->locsyms
[r_symndx
];
9604 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9606 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9607 if (isec
!= NULL
&& elf_discarded_section (isec
))
9616 /* Discard unneeded references to discarded sections.
9617 Returns TRUE if any section's size was changed. */
9618 /* This function assumes that the relocations are in sorted order,
9619 which is true for all known assemblers. */
9622 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9624 struct elf_reloc_cookie cookie
;
9625 asection
*stab
, *eh
;
9626 Elf_Internal_Shdr
*symtab_hdr
;
9627 const struct elf_backend_data
*bed
;
9630 bfd_boolean ret
= FALSE
;
9632 if (info
->traditional_format
9633 || !is_elf_hash_table (info
->hash
))
9636 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9638 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9641 bed
= get_elf_backend_data (abfd
);
9643 if ((abfd
->flags
& DYNAMIC
) != 0)
9646 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9647 if (info
->relocatable
9650 || bfd_is_abs_section (eh
->output_section
))))
9653 stab
= bfd_get_section_by_name (abfd
, ".stab");
9656 || bfd_is_abs_section (stab
->output_section
)
9657 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9662 && bed
->elf_backend_discard_info
== NULL
)
9665 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9667 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9668 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9669 if (cookie
.bad_symtab
)
9671 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9672 cookie
.extsymoff
= 0;
9676 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9677 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9680 if (bed
->s
->arch_size
== 32)
9681 cookie
.r_sym_shift
= 8;
9683 cookie
.r_sym_shift
= 32;
9685 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9686 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9688 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9689 cookie
.locsymcount
, 0,
9691 if (cookie
.locsyms
== NULL
)
9698 count
= stab
->reloc_count
;
9700 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9702 if (cookie
.rels
!= NULL
)
9704 cookie
.rel
= cookie
.rels
;
9705 cookie
.relend
= cookie
.rels
;
9706 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9707 if (_bfd_discard_section_stabs (abfd
, stab
,
9708 elf_section_data (stab
)->sec_info
,
9709 bfd_elf_reloc_symbol_deleted_p
,
9712 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9720 count
= eh
->reloc_count
;
9722 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9724 cookie
.rel
= cookie
.rels
;
9725 cookie
.relend
= cookie
.rels
;
9726 if (cookie
.rels
!= NULL
)
9727 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9729 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9730 bfd_elf_reloc_symbol_deleted_p
,
9734 if (cookie
.rels
!= NULL
9735 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9739 if (bed
->elf_backend_discard_info
!= NULL
9740 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9743 if (cookie
.locsyms
!= NULL
9744 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9746 if (! info
->keep_memory
)
9747 free (cookie
.locsyms
);
9749 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9753 if (info
->eh_frame_hdr
9754 && !info
->relocatable
9755 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9762 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9765 const char *name
, *p
;
9766 struct bfd_section_already_linked
*l
;
9767 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9770 /* A single member comdat group section may be discarded by a
9771 linkonce section. See below. */
9772 if (sec
->output_section
== bfd_abs_section_ptr
)
9777 /* Check if it belongs to a section group. */
9778 group
= elf_sec_group (sec
);
9780 /* Return if it isn't a linkonce section nor a member of a group. A
9781 comdat group section also has SEC_LINK_ONCE set. */
9782 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9787 /* If this is the member of a single member comdat group, check if
9788 the group should be discarded. */
9789 if (elf_next_in_group (sec
) == sec
9790 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9796 /* FIXME: When doing a relocatable link, we may have trouble
9797 copying relocations in other sections that refer to local symbols
9798 in the section being discarded. Those relocations will have to
9799 be converted somehow; as of this writing I'm not sure that any of
9800 the backends handle that correctly.
9802 It is tempting to instead not discard link once sections when
9803 doing a relocatable link (technically, they should be discarded
9804 whenever we are building constructors). However, that fails,
9805 because the linker winds up combining all the link once sections
9806 into a single large link once section, which defeats the purpose
9807 of having link once sections in the first place.
9809 Also, not merging link once sections in a relocatable link
9810 causes trouble for MIPS ELF, which relies on link once semantics
9811 to handle the .reginfo section correctly. */
9813 name
= bfd_get_section_name (abfd
, sec
);
9815 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9816 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9821 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9823 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9825 /* We may have 3 different sections on the list: group section,
9826 comdat section and linkonce section. SEC may be a linkonce or
9827 group section. We match a group section with a group section,
9828 a linkonce section with a linkonce section, and ignore comdat
9830 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9831 && strcmp (name
, l
->sec
->name
) == 0
9832 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9834 /* The section has already been linked. See if we should
9836 switch (flags
& SEC_LINK_DUPLICATES
)
9841 case SEC_LINK_DUPLICATES_DISCARD
:
9844 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9845 (*_bfd_error_handler
)
9846 (_("%B: ignoring duplicate section `%A'"),
9850 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9851 if (sec
->size
!= l
->sec
->size
)
9852 (*_bfd_error_handler
)
9853 (_("%B: duplicate section `%A' has different size"),
9857 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9858 if (sec
->size
!= l
->sec
->size
)
9859 (*_bfd_error_handler
)
9860 (_("%B: duplicate section `%A' has different size"),
9862 else if (sec
->size
!= 0)
9864 bfd_byte
*sec_contents
, *l_sec_contents
;
9866 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9867 (*_bfd_error_handler
)
9868 (_("%B: warning: could not read contents of section `%A'"),
9870 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9872 (*_bfd_error_handler
)
9873 (_("%B: warning: could not read contents of section `%A'"),
9874 l
->sec
->owner
, l
->sec
);
9875 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9876 (*_bfd_error_handler
)
9877 (_("%B: warning: duplicate section `%A' has different contents"),
9881 free (sec_contents
);
9883 free (l_sec_contents
);
9888 /* Set the output_section field so that lang_add_section
9889 does not create a lang_input_section structure for this
9890 section. Since there might be a symbol in the section
9891 being discarded, we must retain a pointer to the section
9892 which we are really going to use. */
9893 sec
->output_section
= bfd_abs_section_ptr
;
9894 sec
->kept_section
= l
->sec
;
9896 if (flags
& SEC_GROUP
)
9898 asection
*first
= elf_next_in_group (sec
);
9899 asection
*s
= first
;
9903 s
->output_section
= bfd_abs_section_ptr
;
9904 /* Record which group discards it. */
9905 s
->kept_section
= l
->sec
;
9906 s
= elf_next_in_group (s
);
9907 /* These lists are circular. */
9919 /* If this is the member of a single member comdat group and the
9920 group hasn't be discarded, we check if it matches a linkonce
9921 section. We only record the discarded comdat group. Otherwise
9922 the undiscarded group will be discarded incorrectly later since
9923 itself has been recorded. */
9924 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9925 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9926 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9927 && bfd_elf_match_symbols_in_sections (l
->sec
,
9928 elf_next_in_group (sec
)))
9930 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9931 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9932 group
->output_section
= bfd_abs_section_ptr
;
9939 /* There is no direct match. But for linkonce section, we should
9940 check if there is a match with comdat group member. We always
9941 record the linkonce section, discarded or not. */
9942 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9943 if (l
->sec
->flags
& SEC_GROUP
)
9945 asection
*first
= elf_next_in_group (l
->sec
);
9948 && elf_next_in_group (first
) == first
9949 && bfd_elf_match_symbols_in_sections (first
, sec
))
9951 sec
->output_section
= bfd_abs_section_ptr
;
9952 sec
->kept_section
= l
->sec
;
9957 /* This is the first section with this name. Record it. */
9958 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9962 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9964 return sym
->st_shndx
== SHN_COMMON
;
9968 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9974 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9976 return bfd_com_section_ptr
;