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 /* Check TLS symbol. We don't check undefined symbol introduced by
894 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
895 && ELF_ST_TYPE (sym
->st_info
) != h
->type
899 bfd_boolean ntdef
, tdef
;
900 asection
*ntsec
, *tsec
;
902 if (h
->type
== STT_TLS
)
922 (*_bfd_error_handler
)
923 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
924 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
925 else if (!tdef
&& !ntdef
)
926 (*_bfd_error_handler
)
927 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
928 tbfd
, ntbfd
, h
->root
.root
.string
);
930 (*_bfd_error_handler
)
931 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
932 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
934 (*_bfd_error_handler
)
935 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
936 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
938 bfd_set_error (bfd_error_bad_value
);
942 /* We need to remember if a symbol has a definition in a dynamic
943 object or is weak in all dynamic objects. Internal and hidden
944 visibility will make it unavailable to dynamic objects. */
945 if (newdyn
&& !h
->dynamic_def
)
947 if (!bfd_is_und_section (sec
))
951 /* Check if this symbol is weak in all dynamic objects. If it
952 is the first time we see it in a dynamic object, we mark
953 if it is weak. Otherwise, we clear it. */
956 if (bind
== STB_WEAK
)
959 else if (bind
!= STB_WEAK
)
964 /* If the old symbol has non-default visibility, we ignore the new
965 definition from a dynamic object. */
967 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
968 && !bfd_is_und_section (sec
))
971 /* Make sure this symbol is dynamic. */
973 /* A protected symbol has external availability. Make sure it is
976 FIXME: Should we check type and size for protected symbol? */
977 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
978 return bfd_elf_link_record_dynamic_symbol (info
, h
);
983 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
986 /* If the new symbol with non-default visibility comes from a
987 relocatable file and the old definition comes from a dynamic
988 object, we remove the old definition. */
989 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
992 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
993 && bfd_is_und_section (sec
))
995 /* If the new symbol is undefined and the old symbol was
996 also undefined before, we need to make sure
997 _bfd_generic_link_add_one_symbol doesn't mess
998 up the linker hash table undefs list. Since the old
999 definition came from a dynamic object, it is still on the
1001 h
->root
.type
= bfd_link_hash_undefined
;
1002 h
->root
.u
.undef
.abfd
= abfd
;
1006 h
->root
.type
= bfd_link_hash_new
;
1007 h
->root
.u
.undef
.abfd
= NULL
;
1016 /* FIXME: Should we check type and size for protected symbol? */
1022 /* Differentiate strong and weak symbols. */
1023 newweak
= bind
== STB_WEAK
;
1024 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1025 || h
->root
.type
== bfd_link_hash_undefweak
);
1027 /* If a new weak symbol definition comes from a regular file and the
1028 old symbol comes from a dynamic library, we treat the new one as
1029 strong. Similarly, an old weak symbol definition from a regular
1030 file is treated as strong when the new symbol comes from a dynamic
1031 library. Further, an old weak symbol from a dynamic library is
1032 treated as strong if the new symbol is from a dynamic library.
1033 This reflects the way glibc's ld.so works.
1035 Do this before setting *type_change_ok or *size_change_ok so that
1036 we warn properly when dynamic library symbols are overridden. */
1038 if (newdef
&& !newdyn
&& olddyn
)
1040 if (olddef
&& newdyn
)
1043 /* It's OK to change the type if either the existing symbol or the
1044 new symbol is weak. A type change is also OK if the old symbol
1045 is undefined and the new symbol is defined. */
1050 && h
->root
.type
== bfd_link_hash_undefined
))
1051 *type_change_ok
= TRUE
;
1053 /* It's OK to change the size if either the existing symbol or the
1054 new symbol is weak, or if the old symbol is undefined. */
1057 || h
->root
.type
== bfd_link_hash_undefined
)
1058 *size_change_ok
= TRUE
;
1060 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1061 symbol, respectively, appears to be a common symbol in a dynamic
1062 object. If a symbol appears in an uninitialized section, and is
1063 not weak, and is not a function, then it may be a common symbol
1064 which was resolved when the dynamic object was created. We want
1065 to treat such symbols specially, because they raise special
1066 considerations when setting the symbol size: if the symbol
1067 appears as a common symbol in a regular object, and the size in
1068 the regular object is larger, we must make sure that we use the
1069 larger size. This problematic case can always be avoided in C,
1070 but it must be handled correctly when using Fortran shared
1073 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1074 likewise for OLDDYNCOMMON and OLDDEF.
1076 Note that this test is just a heuristic, and that it is quite
1077 possible to have an uninitialized symbol in a shared object which
1078 is really a definition, rather than a common symbol. This could
1079 lead to some minor confusion when the symbol really is a common
1080 symbol in some regular object. However, I think it will be
1086 && (sec
->flags
& SEC_ALLOC
) != 0
1087 && (sec
->flags
& SEC_LOAD
) == 0
1089 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1090 newdyncommon
= TRUE
;
1092 newdyncommon
= FALSE
;
1096 && h
->root
.type
== bfd_link_hash_defined
1098 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1099 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1101 && h
->type
!= STT_FUNC
)
1102 olddyncommon
= TRUE
;
1104 olddyncommon
= FALSE
;
1106 /* We now know everything about the old and new symbols. We ask the
1107 backend to check if we can merge them. */
1108 bed
= get_elf_backend_data (abfd
);
1109 if (bed
->merge_symbol
1110 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1111 pold_alignment
, skip
, override
,
1112 type_change_ok
, size_change_ok
,
1113 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1115 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1119 /* If both the old and the new symbols look like common symbols in a
1120 dynamic object, set the size of the symbol to the larger of the
1125 && sym
->st_size
!= h
->size
)
1127 /* Since we think we have two common symbols, issue a multiple
1128 common warning if desired. Note that we only warn if the
1129 size is different. If the size is the same, we simply let
1130 the old symbol override the new one as normally happens with
1131 symbols defined in dynamic objects. */
1133 if (! ((*info
->callbacks
->multiple_common
)
1134 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1135 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1138 if (sym
->st_size
> h
->size
)
1139 h
->size
= sym
->st_size
;
1141 *size_change_ok
= TRUE
;
1144 /* If we are looking at a dynamic object, and we have found a
1145 definition, we need to see if the symbol was already defined by
1146 some other object. If so, we want to use the existing
1147 definition, and we do not want to report a multiple symbol
1148 definition error; we do this by clobbering *PSEC to be
1149 bfd_und_section_ptr.
1151 We treat a common symbol as a definition if the symbol in the
1152 shared library is a function, since common symbols always
1153 represent variables; this can cause confusion in principle, but
1154 any such confusion would seem to indicate an erroneous program or
1155 shared library. We also permit a common symbol in a regular
1156 object to override a weak symbol in a shared object. */
1161 || (h
->root
.type
== bfd_link_hash_common
1163 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1167 newdyncommon
= FALSE
;
1169 *psec
= sec
= bfd_und_section_ptr
;
1170 *size_change_ok
= TRUE
;
1172 /* If we get here when the old symbol is a common symbol, then
1173 we are explicitly letting it override a weak symbol or
1174 function in a dynamic object, and we don't want to warn about
1175 a type change. If the old symbol is a defined symbol, a type
1176 change warning may still be appropriate. */
1178 if (h
->root
.type
== bfd_link_hash_common
)
1179 *type_change_ok
= TRUE
;
1182 /* Handle the special case of an old common symbol merging with a
1183 new symbol which looks like a common symbol in a shared object.
1184 We change *PSEC and *PVALUE to make the new symbol look like a
1185 common symbol, and let _bfd_generic_link_add_one_symbol do the
1189 && h
->root
.type
== bfd_link_hash_common
)
1193 newdyncommon
= FALSE
;
1194 *pvalue
= sym
->st_size
;
1195 *psec
= sec
= bed
->common_section (oldsec
);
1196 *size_change_ok
= TRUE
;
1199 /* Skip weak definitions of symbols that are already defined. */
1200 if (newdef
&& olddef
&& newweak
)
1203 /* If the old symbol is from a dynamic object, and the new symbol is
1204 a definition which is not from a dynamic object, then the new
1205 symbol overrides the old symbol. Symbols from regular files
1206 always take precedence over symbols from dynamic objects, even if
1207 they are defined after the dynamic object in the link.
1209 As above, we again permit a common symbol in a regular object to
1210 override a definition in a shared object if the shared object
1211 symbol is a function or is weak. */
1216 || (bfd_is_com_section (sec
)
1218 || h
->type
== STT_FUNC
)))
1223 /* Change the hash table entry to undefined, and let
1224 _bfd_generic_link_add_one_symbol do the right thing with the
1227 h
->root
.type
= bfd_link_hash_undefined
;
1228 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1229 *size_change_ok
= TRUE
;
1232 olddyncommon
= FALSE
;
1234 /* We again permit a type change when a common symbol may be
1235 overriding a function. */
1237 if (bfd_is_com_section (sec
))
1238 *type_change_ok
= TRUE
;
1240 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1243 /* This union may have been set to be non-NULL when this symbol
1244 was seen in a dynamic object. We must force the union to be
1245 NULL, so that it is correct for a regular symbol. */
1246 h
->verinfo
.vertree
= NULL
;
1249 /* Handle the special case of a new common symbol merging with an
1250 old symbol that looks like it might be a common symbol defined in
1251 a shared object. Note that we have already handled the case in
1252 which a new common symbol should simply override the definition
1253 in the shared library. */
1256 && bfd_is_com_section (sec
)
1259 /* It would be best if we could set the hash table entry to a
1260 common symbol, but we don't know what to use for the section
1261 or the alignment. */
1262 if (! ((*info
->callbacks
->multiple_common
)
1263 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1264 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1267 /* If the presumed common symbol in the dynamic object is
1268 larger, pretend that the new symbol has its size. */
1270 if (h
->size
> *pvalue
)
1273 /* We need to remember the alignment required by the symbol
1274 in the dynamic object. */
1275 BFD_ASSERT (pold_alignment
);
1276 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1279 olddyncommon
= FALSE
;
1281 h
->root
.type
= bfd_link_hash_undefined
;
1282 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1284 *size_change_ok
= TRUE
;
1285 *type_change_ok
= TRUE
;
1287 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1290 h
->verinfo
.vertree
= NULL
;
1295 /* Handle the case where we had a versioned symbol in a dynamic
1296 library and now find a definition in a normal object. In this
1297 case, we make the versioned symbol point to the normal one. */
1298 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1299 flip
->root
.type
= h
->root
.type
;
1300 h
->root
.type
= bfd_link_hash_indirect
;
1301 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1302 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1303 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1307 flip
->ref_dynamic
= 1;
1314 /* This function is called to create an indirect symbol from the
1315 default for the symbol with the default version if needed. The
1316 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1317 set DYNSYM if the new indirect symbol is dynamic. */
1320 _bfd_elf_add_default_symbol (bfd
*abfd
,
1321 struct bfd_link_info
*info
,
1322 struct elf_link_hash_entry
*h
,
1324 Elf_Internal_Sym
*sym
,
1327 bfd_boolean
*dynsym
,
1328 bfd_boolean override
)
1330 bfd_boolean type_change_ok
;
1331 bfd_boolean size_change_ok
;
1334 struct elf_link_hash_entry
*hi
;
1335 struct bfd_link_hash_entry
*bh
;
1336 const struct elf_backend_data
*bed
;
1337 bfd_boolean collect
;
1338 bfd_boolean dynamic
;
1340 size_t len
, shortlen
;
1343 /* If this symbol has a version, and it is the default version, we
1344 create an indirect symbol from the default name to the fully
1345 decorated name. This will cause external references which do not
1346 specify a version to be bound to this version of the symbol. */
1347 p
= strchr (name
, ELF_VER_CHR
);
1348 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1353 /* We are overridden by an old definition. We need to check if we
1354 need to create the indirect symbol from the default name. */
1355 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1357 BFD_ASSERT (hi
!= NULL
);
1360 while (hi
->root
.type
== bfd_link_hash_indirect
1361 || hi
->root
.type
== bfd_link_hash_warning
)
1363 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1369 bed
= get_elf_backend_data (abfd
);
1370 collect
= bed
->collect
;
1371 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1373 shortlen
= p
- name
;
1374 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1375 if (shortname
== NULL
)
1377 memcpy (shortname
, name
, shortlen
);
1378 shortname
[shortlen
] = '\0';
1380 /* We are going to create a new symbol. Merge it with any existing
1381 symbol with this name. For the purposes of the merge, act as
1382 though we were defining the symbol we just defined, although we
1383 actually going to define an indirect symbol. */
1384 type_change_ok
= FALSE
;
1385 size_change_ok
= FALSE
;
1387 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1388 NULL
, &hi
, &skip
, &override
,
1389 &type_change_ok
, &size_change_ok
))
1398 if (! (_bfd_generic_link_add_one_symbol
1399 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1400 0, name
, FALSE
, collect
, &bh
)))
1402 hi
= (struct elf_link_hash_entry
*) bh
;
1406 /* In this case the symbol named SHORTNAME is overriding the
1407 indirect symbol we want to add. We were planning on making
1408 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1409 is the name without a version. NAME is the fully versioned
1410 name, and it is the default version.
1412 Overriding means that we already saw a definition for the
1413 symbol SHORTNAME in a regular object, and it is overriding
1414 the symbol defined in the dynamic object.
1416 When this happens, we actually want to change NAME, the
1417 symbol we just added, to refer to SHORTNAME. This will cause
1418 references to NAME in the shared object to become references
1419 to SHORTNAME in the regular object. This is what we expect
1420 when we override a function in a shared object: that the
1421 references in the shared object will be mapped to the
1422 definition in the regular object. */
1424 while (hi
->root
.type
== bfd_link_hash_indirect
1425 || hi
->root
.type
== bfd_link_hash_warning
)
1426 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1428 h
->root
.type
= bfd_link_hash_indirect
;
1429 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1433 hi
->ref_dynamic
= 1;
1437 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1442 /* Now set HI to H, so that the following code will set the
1443 other fields correctly. */
1447 /* If there is a duplicate definition somewhere, then HI may not
1448 point to an indirect symbol. We will have reported an error to
1449 the user in that case. */
1451 if (hi
->root
.type
== bfd_link_hash_indirect
)
1453 struct elf_link_hash_entry
*ht
;
1455 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1456 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1458 /* See if the new flags lead us to realize that the symbol must
1470 if (hi
->ref_regular
)
1476 /* We also need to define an indirection from the nondefault version
1480 len
= strlen (name
);
1481 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1482 if (shortname
== NULL
)
1484 memcpy (shortname
, name
, shortlen
);
1485 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1487 /* Once again, merge with any existing symbol. */
1488 type_change_ok
= FALSE
;
1489 size_change_ok
= FALSE
;
1491 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1492 NULL
, &hi
, &skip
, &override
,
1493 &type_change_ok
, &size_change_ok
))
1501 /* Here SHORTNAME is a versioned name, so we don't expect to see
1502 the type of override we do in the case above unless it is
1503 overridden by a versioned definition. */
1504 if (hi
->root
.type
!= bfd_link_hash_defined
1505 && hi
->root
.type
!= bfd_link_hash_defweak
)
1506 (*_bfd_error_handler
)
1507 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1513 if (! (_bfd_generic_link_add_one_symbol
1514 (info
, abfd
, shortname
, BSF_INDIRECT
,
1515 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1517 hi
= (struct elf_link_hash_entry
*) bh
;
1519 /* If there is a duplicate definition somewhere, then HI may not
1520 point to an indirect symbol. We will have reported an error
1521 to the user in that case. */
1523 if (hi
->root
.type
== bfd_link_hash_indirect
)
1525 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1527 /* See if the new flags lead us to realize that the symbol
1539 if (hi
->ref_regular
)
1549 /* This routine is used to export all defined symbols into the dynamic
1550 symbol table. It is called via elf_link_hash_traverse. */
1553 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1555 struct elf_info_failed
*eif
= data
;
1557 /* Ignore indirect symbols. These are added by the versioning code. */
1558 if (h
->root
.type
== bfd_link_hash_indirect
)
1561 if (h
->root
.type
== bfd_link_hash_warning
)
1562 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1564 if (h
->dynindx
== -1
1568 struct bfd_elf_version_tree
*t
;
1569 struct bfd_elf_version_expr
*d
;
1571 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1573 if (t
->globals
.list
!= NULL
)
1575 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1580 if (t
->locals
.list
!= NULL
)
1582 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1591 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1602 /* Look through the symbols which are defined in other shared
1603 libraries and referenced here. Update the list of version
1604 dependencies. This will be put into the .gnu.version_r section.
1605 This function is called via elf_link_hash_traverse. */
1608 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1611 struct elf_find_verdep_info
*rinfo
= data
;
1612 Elf_Internal_Verneed
*t
;
1613 Elf_Internal_Vernaux
*a
;
1616 if (h
->root
.type
== bfd_link_hash_warning
)
1617 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1619 /* We only care about symbols defined in shared objects with version
1624 || h
->verinfo
.verdef
== NULL
)
1627 /* See if we already know about this version. */
1628 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1630 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1633 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1634 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1640 /* This is a new version. Add it to tree we are building. */
1645 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1648 rinfo
->failed
= TRUE
;
1652 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1653 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1654 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1658 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1660 /* Note that we are copying a string pointer here, and testing it
1661 above. If bfd_elf_string_from_elf_section is ever changed to
1662 discard the string data when low in memory, this will have to be
1664 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1666 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1667 a
->vna_nextptr
= t
->vn_auxptr
;
1669 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1672 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1679 /* Figure out appropriate versions for all the symbols. We may not
1680 have the version number script until we have read all of the input
1681 files, so until that point we don't know which symbols should be
1682 local. This function is called via elf_link_hash_traverse. */
1685 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1687 struct elf_assign_sym_version_info
*sinfo
;
1688 struct bfd_link_info
*info
;
1689 const struct elf_backend_data
*bed
;
1690 struct elf_info_failed eif
;
1697 if (h
->root
.type
== bfd_link_hash_warning
)
1698 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1700 /* Fix the symbol flags. */
1703 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1706 sinfo
->failed
= TRUE
;
1710 /* We only need version numbers for symbols defined in regular
1712 if (!h
->def_regular
)
1715 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1716 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1717 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1719 struct bfd_elf_version_tree
*t
;
1724 /* There are two consecutive ELF_VER_CHR characters if this is
1725 not a hidden symbol. */
1727 if (*p
== ELF_VER_CHR
)
1733 /* If there is no version string, we can just return out. */
1741 /* Look for the version. If we find it, it is no longer weak. */
1742 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1744 if (strcmp (t
->name
, p
) == 0)
1748 struct bfd_elf_version_expr
*d
;
1750 len
= p
- h
->root
.root
.string
;
1751 alc
= bfd_malloc (len
);
1754 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1755 alc
[len
- 1] = '\0';
1756 if (alc
[len
- 2] == ELF_VER_CHR
)
1757 alc
[len
- 2] = '\0';
1759 h
->verinfo
.vertree
= t
;
1763 if (t
->globals
.list
!= NULL
)
1764 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1766 /* See if there is anything to force this symbol to
1768 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1770 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1773 && ! info
->export_dynamic
)
1774 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1782 /* If we are building an application, we need to create a
1783 version node for this version. */
1784 if (t
== NULL
&& info
->executable
)
1786 struct bfd_elf_version_tree
**pp
;
1789 /* If we aren't going to export this symbol, we don't need
1790 to worry about it. */
1791 if (h
->dynindx
== -1)
1795 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1798 sinfo
->failed
= TRUE
;
1803 t
->name_indx
= (unsigned int) -1;
1807 /* Don't count anonymous version tag. */
1808 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1810 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1812 t
->vernum
= version_index
;
1816 h
->verinfo
.vertree
= t
;
1820 /* We could not find the version for a symbol when
1821 generating a shared archive. Return an error. */
1822 (*_bfd_error_handler
)
1823 (_("%B: undefined versioned symbol name %s"),
1824 sinfo
->output_bfd
, h
->root
.root
.string
);
1825 bfd_set_error (bfd_error_bad_value
);
1826 sinfo
->failed
= TRUE
;
1834 /* If we don't have a version for this symbol, see if we can find
1836 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1838 struct bfd_elf_version_tree
*t
;
1839 struct bfd_elf_version_tree
*local_ver
;
1840 struct bfd_elf_version_expr
*d
;
1842 /* See if can find what version this symbol is in. If the
1843 symbol is supposed to be local, then don't actually register
1846 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1848 if (t
->globals
.list
!= NULL
)
1850 bfd_boolean matched
;
1854 while ((d
= (*t
->match
) (&t
->globals
, d
,
1855 h
->root
.root
.string
)) != NULL
)
1860 /* There is a version without definition. Make
1861 the symbol the default definition for this
1863 h
->verinfo
.vertree
= t
;
1871 /* There is no undefined version for this symbol. Hide the
1873 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1876 if (t
->locals
.list
!= NULL
)
1879 while ((d
= (*t
->match
) (&t
->locals
, d
,
1880 h
->root
.root
.string
)) != NULL
)
1883 /* If the match is "*", keep looking for a more
1884 explicit, perhaps even global, match.
1885 XXX: Shouldn't this be !d->wildcard instead? */
1886 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1895 if (local_ver
!= NULL
)
1897 h
->verinfo
.vertree
= local_ver
;
1898 if (h
->dynindx
!= -1
1899 && ! info
->export_dynamic
)
1901 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1909 /* Read and swap the relocs from the section indicated by SHDR. This
1910 may be either a REL or a RELA section. The relocations are
1911 translated into RELA relocations and stored in INTERNAL_RELOCS,
1912 which should have already been allocated to contain enough space.
1913 The EXTERNAL_RELOCS are a buffer where the external form of the
1914 relocations should be stored.
1916 Returns FALSE if something goes wrong. */
1919 elf_link_read_relocs_from_section (bfd
*abfd
,
1921 Elf_Internal_Shdr
*shdr
,
1922 void *external_relocs
,
1923 Elf_Internal_Rela
*internal_relocs
)
1925 const struct elf_backend_data
*bed
;
1926 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1927 const bfd_byte
*erela
;
1928 const bfd_byte
*erelaend
;
1929 Elf_Internal_Rela
*irela
;
1930 Elf_Internal_Shdr
*symtab_hdr
;
1933 /* Position ourselves at the start of the section. */
1934 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1937 /* Read the relocations. */
1938 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1941 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1942 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1944 bed
= get_elf_backend_data (abfd
);
1946 /* Convert the external relocations to the internal format. */
1947 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1948 swap_in
= bed
->s
->swap_reloc_in
;
1949 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1950 swap_in
= bed
->s
->swap_reloca_in
;
1953 bfd_set_error (bfd_error_wrong_format
);
1957 erela
= external_relocs
;
1958 erelaend
= erela
+ shdr
->sh_size
;
1959 irela
= internal_relocs
;
1960 while (erela
< erelaend
)
1964 (*swap_in
) (abfd
, erela
, irela
);
1965 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1966 if (bed
->s
->arch_size
== 64)
1968 if ((size_t) r_symndx
>= nsyms
)
1970 (*_bfd_error_handler
)
1971 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1972 " for offset 0x%lx in section `%A'"),
1974 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1975 bfd_set_error (bfd_error_bad_value
);
1978 irela
+= bed
->s
->int_rels_per_ext_rel
;
1979 erela
+= shdr
->sh_entsize
;
1985 /* Read and swap the relocs for a section O. They may have been
1986 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1987 not NULL, they are used as buffers to read into. They are known to
1988 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1989 the return value is allocated using either malloc or bfd_alloc,
1990 according to the KEEP_MEMORY argument. If O has two relocation
1991 sections (both REL and RELA relocations), then the REL_HDR
1992 relocations will appear first in INTERNAL_RELOCS, followed by the
1993 REL_HDR2 relocations. */
1996 _bfd_elf_link_read_relocs (bfd
*abfd
,
1998 void *external_relocs
,
1999 Elf_Internal_Rela
*internal_relocs
,
2000 bfd_boolean keep_memory
)
2002 Elf_Internal_Shdr
*rel_hdr
;
2003 void *alloc1
= NULL
;
2004 Elf_Internal_Rela
*alloc2
= NULL
;
2005 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2007 if (elf_section_data (o
)->relocs
!= NULL
)
2008 return elf_section_data (o
)->relocs
;
2010 if (o
->reloc_count
== 0)
2013 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2015 if (internal_relocs
== NULL
)
2019 size
= o
->reloc_count
;
2020 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2022 internal_relocs
= bfd_alloc (abfd
, size
);
2024 internal_relocs
= alloc2
= bfd_malloc (size
);
2025 if (internal_relocs
== NULL
)
2029 if (external_relocs
== NULL
)
2031 bfd_size_type size
= rel_hdr
->sh_size
;
2033 if (elf_section_data (o
)->rel_hdr2
)
2034 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2035 alloc1
= bfd_malloc (size
);
2038 external_relocs
= alloc1
;
2041 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2045 if (elf_section_data (o
)->rel_hdr2
2046 && (!elf_link_read_relocs_from_section
2048 elf_section_data (o
)->rel_hdr2
,
2049 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2050 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2051 * bed
->s
->int_rels_per_ext_rel
))))
2054 /* Cache the results for next time, if we can. */
2056 elf_section_data (o
)->relocs
= internal_relocs
;
2061 /* Don't free alloc2, since if it was allocated we are passing it
2062 back (under the name of internal_relocs). */
2064 return internal_relocs
;
2074 /* Compute the size of, and allocate space for, REL_HDR which is the
2075 section header for a section containing relocations for O. */
2078 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2079 Elf_Internal_Shdr
*rel_hdr
,
2082 bfd_size_type reloc_count
;
2083 bfd_size_type num_rel_hashes
;
2085 /* Figure out how many relocations there will be. */
2086 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2087 reloc_count
= elf_section_data (o
)->rel_count
;
2089 reloc_count
= elf_section_data (o
)->rel_count2
;
2091 num_rel_hashes
= o
->reloc_count
;
2092 if (num_rel_hashes
< reloc_count
)
2093 num_rel_hashes
= reloc_count
;
2095 /* That allows us to calculate the size of the section. */
2096 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2098 /* The contents field must last into write_object_contents, so we
2099 allocate it with bfd_alloc rather than malloc. Also since we
2100 cannot be sure that the contents will actually be filled in,
2101 we zero the allocated space. */
2102 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2103 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2106 /* We only allocate one set of hash entries, so we only do it the
2107 first time we are called. */
2108 if (elf_section_data (o
)->rel_hashes
== NULL
2111 struct elf_link_hash_entry
**p
;
2113 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2117 elf_section_data (o
)->rel_hashes
= p
;
2123 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2124 originated from the section given by INPUT_REL_HDR) to the
2128 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2129 asection
*input_section
,
2130 Elf_Internal_Shdr
*input_rel_hdr
,
2131 Elf_Internal_Rela
*internal_relocs
,
2132 struct elf_link_hash_entry
**rel_hash
2135 Elf_Internal_Rela
*irela
;
2136 Elf_Internal_Rela
*irelaend
;
2138 Elf_Internal_Shdr
*output_rel_hdr
;
2139 asection
*output_section
;
2140 unsigned int *rel_countp
= NULL
;
2141 const struct elf_backend_data
*bed
;
2142 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2144 output_section
= input_section
->output_section
;
2145 output_rel_hdr
= NULL
;
2147 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2148 == input_rel_hdr
->sh_entsize
)
2150 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2151 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2153 else if (elf_section_data (output_section
)->rel_hdr2
2154 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2155 == input_rel_hdr
->sh_entsize
))
2157 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2158 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2162 (*_bfd_error_handler
)
2163 (_("%B: relocation size mismatch in %B section %A"),
2164 output_bfd
, input_section
->owner
, input_section
);
2165 bfd_set_error (bfd_error_wrong_object_format
);
2169 bed
= get_elf_backend_data (output_bfd
);
2170 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2171 swap_out
= bed
->s
->swap_reloc_out
;
2172 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2173 swap_out
= bed
->s
->swap_reloca_out
;
2177 erel
= output_rel_hdr
->contents
;
2178 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2179 irela
= internal_relocs
;
2180 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2181 * bed
->s
->int_rels_per_ext_rel
);
2182 while (irela
< irelaend
)
2184 (*swap_out
) (output_bfd
, irela
, erel
);
2185 irela
+= bed
->s
->int_rels_per_ext_rel
;
2186 erel
+= input_rel_hdr
->sh_entsize
;
2189 /* Bump the counter, so that we know where to add the next set of
2191 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2196 /* Make weak undefined symbols in PIE dynamic. */
2199 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2200 struct elf_link_hash_entry
*h
)
2204 && h
->root
.type
== bfd_link_hash_undefweak
)
2205 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2210 /* Fix up the flags for a symbol. This handles various cases which
2211 can only be fixed after all the input files are seen. This is
2212 currently called by both adjust_dynamic_symbol and
2213 assign_sym_version, which is unnecessary but perhaps more robust in
2214 the face of future changes. */
2217 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2218 struct elf_info_failed
*eif
)
2220 const struct elf_backend_data
*bed
= NULL
;
2222 /* If this symbol was mentioned in a non-ELF file, try to set
2223 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2224 permit a non-ELF file to correctly refer to a symbol defined in
2225 an ELF dynamic object. */
2228 while (h
->root
.type
== bfd_link_hash_indirect
)
2229 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2231 if (h
->root
.type
!= bfd_link_hash_defined
2232 && h
->root
.type
!= bfd_link_hash_defweak
)
2235 h
->ref_regular_nonweak
= 1;
2239 if (h
->root
.u
.def
.section
->owner
!= NULL
2240 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2241 == bfd_target_elf_flavour
))
2244 h
->ref_regular_nonweak
= 1;
2250 if (h
->dynindx
== -1
2254 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2263 /* Unfortunately, NON_ELF is only correct if the symbol
2264 was first seen in a non-ELF file. Fortunately, if the symbol
2265 was first seen in an ELF file, we're probably OK unless the
2266 symbol was defined in a non-ELF file. Catch that case here.
2267 FIXME: We're still in trouble if the symbol was first seen in
2268 a dynamic object, and then later in a non-ELF regular object. */
2269 if ((h
->root
.type
== bfd_link_hash_defined
2270 || h
->root
.type
== bfd_link_hash_defweak
)
2272 && (h
->root
.u
.def
.section
->owner
!= NULL
2273 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2274 != bfd_target_elf_flavour
)
2275 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2276 && !h
->def_dynamic
)))
2280 /* Backend specific symbol fixup. */
2281 if (elf_hash_table (eif
->info
)->dynobj
)
2283 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2284 if (bed
->elf_backend_fixup_symbol
2285 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2289 /* If this is a final link, and the symbol was defined as a common
2290 symbol in a regular object file, and there was no definition in
2291 any dynamic object, then the linker will have allocated space for
2292 the symbol in a common section but the DEF_REGULAR
2293 flag will not have been set. */
2294 if (h
->root
.type
== bfd_link_hash_defined
2298 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2301 /* If -Bsymbolic was used (which means to bind references to global
2302 symbols to the definition within the shared object), and this
2303 symbol was defined in a regular object, then it actually doesn't
2304 need a PLT entry. Likewise, if the symbol has non-default
2305 visibility. If the symbol has hidden or internal visibility, we
2306 will force it local. */
2308 && eif
->info
->shared
2309 && is_elf_hash_table (eif
->info
->hash
)
2310 && (eif
->info
->symbolic
2311 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2314 bfd_boolean force_local
;
2316 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2317 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2318 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2321 /* If a weak undefined symbol has non-default visibility, we also
2322 hide it from the dynamic linker. */
2323 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2324 && h
->root
.type
== bfd_link_hash_undefweak
)
2326 const struct elf_backend_data
*bed
;
2327 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2328 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2331 /* If this is a weak defined symbol in a dynamic object, and we know
2332 the real definition in the dynamic object, copy interesting flags
2333 over to the real definition. */
2334 if (h
->u
.weakdef
!= NULL
)
2336 struct elf_link_hash_entry
*weakdef
;
2338 weakdef
= h
->u
.weakdef
;
2339 if (h
->root
.type
== bfd_link_hash_indirect
)
2340 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2342 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2343 || h
->root
.type
== bfd_link_hash_defweak
);
2344 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2345 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2346 BFD_ASSERT (weakdef
->def_dynamic
);
2348 /* If the real definition is defined by a regular object file,
2349 don't do anything special. See the longer description in
2350 _bfd_elf_adjust_dynamic_symbol, below. */
2351 if (weakdef
->def_regular
)
2352 h
->u
.weakdef
= NULL
;
2354 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2361 /* Make the backend pick a good value for a dynamic symbol. This is
2362 called via elf_link_hash_traverse, and also calls itself
2366 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2368 struct elf_info_failed
*eif
= data
;
2370 const struct elf_backend_data
*bed
;
2372 if (! is_elf_hash_table (eif
->info
->hash
))
2375 if (h
->root
.type
== bfd_link_hash_warning
)
2377 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2378 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2380 /* When warning symbols are created, they **replace** the "real"
2381 entry in the hash table, thus we never get to see the real
2382 symbol in a hash traversal. So look at it now. */
2383 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2386 /* Ignore indirect symbols. These are added by the versioning code. */
2387 if (h
->root
.type
== bfd_link_hash_indirect
)
2390 /* Fix the symbol flags. */
2391 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2394 /* If this symbol does not require a PLT entry, and it is not
2395 defined by a dynamic object, or is not referenced by a regular
2396 object, ignore it. We do have to handle a weak defined symbol,
2397 even if no regular object refers to it, if we decided to add it
2398 to the dynamic symbol table. FIXME: Do we normally need to worry
2399 about symbols which are defined by one dynamic object and
2400 referenced by another one? */
2405 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2407 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2411 /* If we've already adjusted this symbol, don't do it again. This
2412 can happen via a recursive call. */
2413 if (h
->dynamic_adjusted
)
2416 /* Don't look at this symbol again. Note that we must set this
2417 after checking the above conditions, because we may look at a
2418 symbol once, decide not to do anything, and then get called
2419 recursively later after REF_REGULAR is set below. */
2420 h
->dynamic_adjusted
= 1;
2422 /* If this is a weak definition, and we know a real definition, and
2423 the real symbol is not itself defined by a regular object file,
2424 then get a good value for the real definition. We handle the
2425 real symbol first, for the convenience of the backend routine.
2427 Note that there is a confusing case here. If the real definition
2428 is defined by a regular object file, we don't get the real symbol
2429 from the dynamic object, but we do get the weak symbol. If the
2430 processor backend uses a COPY reloc, then if some routine in the
2431 dynamic object changes the real symbol, we will not see that
2432 change in the corresponding weak symbol. This is the way other
2433 ELF linkers work as well, and seems to be a result of the shared
2436 I will clarify this issue. Most SVR4 shared libraries define the
2437 variable _timezone and define timezone as a weak synonym. The
2438 tzset call changes _timezone. If you write
2439 extern int timezone;
2441 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2442 you might expect that, since timezone is a synonym for _timezone,
2443 the same number will print both times. However, if the processor
2444 backend uses a COPY reloc, then actually timezone will be copied
2445 into your process image, and, since you define _timezone
2446 yourself, _timezone will not. Thus timezone and _timezone will
2447 wind up at different memory locations. The tzset call will set
2448 _timezone, leaving timezone unchanged. */
2450 if (h
->u
.weakdef
!= NULL
)
2452 /* If we get to this point, we know there is an implicit
2453 reference by a regular object file via the weak symbol H.
2454 FIXME: Is this really true? What if the traversal finds
2455 H->U.WEAKDEF before it finds H? */
2456 h
->u
.weakdef
->ref_regular
= 1;
2458 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2462 /* If a symbol has no type and no size and does not require a PLT
2463 entry, then we are probably about to do the wrong thing here: we
2464 are probably going to create a COPY reloc for an empty object.
2465 This case can arise when a shared object is built with assembly
2466 code, and the assembly code fails to set the symbol type. */
2468 && h
->type
== STT_NOTYPE
2470 (*_bfd_error_handler
)
2471 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2472 h
->root
.root
.string
);
2474 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2475 bed
= get_elf_backend_data (dynobj
);
2476 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2485 /* Adjust all external symbols pointing into SEC_MERGE sections
2486 to reflect the object merging within the sections. */
2489 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2493 if (h
->root
.type
== bfd_link_hash_warning
)
2494 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2496 if ((h
->root
.type
== bfd_link_hash_defined
2497 || h
->root
.type
== bfd_link_hash_defweak
)
2498 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2499 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2501 bfd
*output_bfd
= data
;
2503 h
->root
.u
.def
.value
=
2504 _bfd_merged_section_offset (output_bfd
,
2505 &h
->root
.u
.def
.section
,
2506 elf_section_data (sec
)->sec_info
,
2507 h
->root
.u
.def
.value
);
2513 /* Returns false if the symbol referred to by H should be considered
2514 to resolve local to the current module, and true if it should be
2515 considered to bind dynamically. */
2518 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2519 struct bfd_link_info
*info
,
2520 bfd_boolean ignore_protected
)
2522 bfd_boolean binding_stays_local_p
;
2527 while (h
->root
.type
== bfd_link_hash_indirect
2528 || h
->root
.type
== bfd_link_hash_warning
)
2529 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2531 /* If it was forced local, then clearly it's not dynamic. */
2532 if (h
->dynindx
== -1)
2534 if (h
->forced_local
)
2537 /* Identify the cases where name binding rules say that a
2538 visible symbol resolves locally. */
2539 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2541 switch (ELF_ST_VISIBILITY (h
->other
))
2548 /* Proper resolution for function pointer equality may require
2549 that these symbols perhaps be resolved dynamically, even though
2550 we should be resolving them to the current module. */
2551 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2552 binding_stays_local_p
= TRUE
;
2559 /* If it isn't defined locally, then clearly it's dynamic. */
2560 if (!h
->def_regular
)
2563 /* Otherwise, the symbol is dynamic if binding rules don't tell
2564 us that it remains local. */
2565 return !binding_stays_local_p
;
2568 /* Return true if the symbol referred to by H should be considered
2569 to resolve local to the current module, and false otherwise. Differs
2570 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2571 undefined symbols and weak symbols. */
2574 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2575 struct bfd_link_info
*info
,
2576 bfd_boolean local_protected
)
2578 /* If it's a local sym, of course we resolve locally. */
2582 /* Common symbols that become definitions don't get the DEF_REGULAR
2583 flag set, so test it first, and don't bail out. */
2584 if (ELF_COMMON_DEF_P (h
))
2586 /* If we don't have a definition in a regular file, then we can't
2587 resolve locally. The sym is either undefined or dynamic. */
2588 else if (!h
->def_regular
)
2591 /* Forced local symbols resolve locally. */
2592 if (h
->forced_local
)
2595 /* As do non-dynamic symbols. */
2596 if (h
->dynindx
== -1)
2599 /* At this point, we know the symbol is defined and dynamic. In an
2600 executable it must resolve locally, likewise when building symbolic
2601 shared libraries. */
2602 if (info
->executable
|| info
->symbolic
)
2605 /* Now deal with defined dynamic symbols in shared libraries. Ones
2606 with default visibility might not resolve locally. */
2607 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2610 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2611 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2614 /* STV_PROTECTED non-function symbols are local. */
2615 if (h
->type
!= STT_FUNC
)
2618 /* Function pointer equality tests may require that STV_PROTECTED
2619 symbols be treated as dynamic symbols, even when we know that the
2620 dynamic linker will resolve them locally. */
2621 return local_protected
;
2624 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2625 aligned. Returns the first TLS output section. */
2627 struct bfd_section
*
2628 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2630 struct bfd_section
*sec
, *tls
;
2631 unsigned int align
= 0;
2633 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2634 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2638 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2639 if (sec
->alignment_power
> align
)
2640 align
= sec
->alignment_power
;
2642 elf_hash_table (info
)->tls_sec
= tls
;
2644 /* Ensure the alignment of the first section is the largest alignment,
2645 so that the tls segment starts aligned. */
2647 tls
->alignment_power
= align
;
2652 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2654 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2655 Elf_Internal_Sym
*sym
)
2657 const struct elf_backend_data
*bed
;
2659 /* Local symbols do not count, but target specific ones might. */
2660 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2661 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2664 /* Function symbols do not count. */
2665 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2668 /* If the section is undefined, then so is the symbol. */
2669 if (sym
->st_shndx
== SHN_UNDEF
)
2672 /* If the symbol is defined in the common section, then
2673 it is a common definition and so does not count. */
2674 bed
= get_elf_backend_data (abfd
);
2675 if (bed
->common_definition (sym
))
2678 /* If the symbol is in a target specific section then we
2679 must rely upon the backend to tell us what it is. */
2680 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2681 /* FIXME - this function is not coded yet:
2683 return _bfd_is_global_symbol_definition (abfd, sym);
2685 Instead for now assume that the definition is not global,
2686 Even if this is wrong, at least the linker will behave
2687 in the same way that it used to do. */
2693 /* Search the symbol table of the archive element of the archive ABFD
2694 whose archive map contains a mention of SYMDEF, and determine if
2695 the symbol is defined in this element. */
2697 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2699 Elf_Internal_Shdr
* hdr
;
2700 bfd_size_type symcount
;
2701 bfd_size_type extsymcount
;
2702 bfd_size_type extsymoff
;
2703 Elf_Internal_Sym
*isymbuf
;
2704 Elf_Internal_Sym
*isym
;
2705 Elf_Internal_Sym
*isymend
;
2708 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2712 if (! bfd_check_format (abfd
, bfd_object
))
2715 /* If we have already included the element containing this symbol in the
2716 link then we do not need to include it again. Just claim that any symbol
2717 it contains is not a definition, so that our caller will not decide to
2718 (re)include this element. */
2719 if (abfd
->archive_pass
)
2722 /* Select the appropriate symbol table. */
2723 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2724 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2726 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2728 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2730 /* The sh_info field of the symtab header tells us where the
2731 external symbols start. We don't care about the local symbols. */
2732 if (elf_bad_symtab (abfd
))
2734 extsymcount
= symcount
;
2739 extsymcount
= symcount
- hdr
->sh_info
;
2740 extsymoff
= hdr
->sh_info
;
2743 if (extsymcount
== 0)
2746 /* Read in the symbol table. */
2747 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2749 if (isymbuf
== NULL
)
2752 /* Scan the symbol table looking for SYMDEF. */
2754 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2758 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2763 if (strcmp (name
, symdef
->name
) == 0)
2765 result
= is_global_data_symbol_definition (abfd
, isym
);
2775 /* Add an entry to the .dynamic table. */
2778 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2782 struct elf_link_hash_table
*hash_table
;
2783 const struct elf_backend_data
*bed
;
2785 bfd_size_type newsize
;
2786 bfd_byte
*newcontents
;
2787 Elf_Internal_Dyn dyn
;
2789 hash_table
= elf_hash_table (info
);
2790 if (! is_elf_hash_table (hash_table
))
2793 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2795 (_("warning: creating a DT_TEXTREL in a shared object."));
2797 bed
= get_elf_backend_data (hash_table
->dynobj
);
2798 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2799 BFD_ASSERT (s
!= NULL
);
2801 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2802 newcontents
= bfd_realloc (s
->contents
, newsize
);
2803 if (newcontents
== NULL
)
2807 dyn
.d_un
.d_val
= val
;
2808 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2811 s
->contents
= newcontents
;
2816 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2817 otherwise just check whether one already exists. Returns -1 on error,
2818 1 if a DT_NEEDED tag already exists, and 0 on success. */
2821 elf_add_dt_needed_tag (bfd
*abfd
,
2822 struct bfd_link_info
*info
,
2826 struct elf_link_hash_table
*hash_table
;
2827 bfd_size_type oldsize
;
2828 bfd_size_type strindex
;
2830 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2833 hash_table
= elf_hash_table (info
);
2834 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2835 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2836 if (strindex
== (bfd_size_type
) -1)
2839 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2842 const struct elf_backend_data
*bed
;
2845 bed
= get_elf_backend_data (hash_table
->dynobj
);
2846 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2848 for (extdyn
= sdyn
->contents
;
2849 extdyn
< sdyn
->contents
+ sdyn
->size
;
2850 extdyn
+= bed
->s
->sizeof_dyn
)
2852 Elf_Internal_Dyn dyn
;
2854 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2855 if (dyn
.d_tag
== DT_NEEDED
2856 && dyn
.d_un
.d_val
== strindex
)
2858 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2866 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2869 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2873 /* We were just checking for existence of the tag. */
2874 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2879 /* Sort symbol by value and section. */
2881 elf_sort_symbol (const void *arg1
, const void *arg2
)
2883 const struct elf_link_hash_entry
*h1
;
2884 const struct elf_link_hash_entry
*h2
;
2885 bfd_signed_vma vdiff
;
2887 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2888 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2889 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2891 return vdiff
> 0 ? 1 : -1;
2894 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2896 return sdiff
> 0 ? 1 : -1;
2901 /* This function is used to adjust offsets into .dynstr for
2902 dynamic symbols. This is called via elf_link_hash_traverse. */
2905 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2907 struct elf_strtab_hash
*dynstr
= data
;
2909 if (h
->root
.type
== bfd_link_hash_warning
)
2910 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2912 if (h
->dynindx
!= -1)
2913 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2917 /* Assign string offsets in .dynstr, update all structures referencing
2921 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2923 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2924 struct elf_link_local_dynamic_entry
*entry
;
2925 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2926 bfd
*dynobj
= hash_table
->dynobj
;
2929 const struct elf_backend_data
*bed
;
2932 _bfd_elf_strtab_finalize (dynstr
);
2933 size
= _bfd_elf_strtab_size (dynstr
);
2935 bed
= get_elf_backend_data (dynobj
);
2936 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2937 BFD_ASSERT (sdyn
!= NULL
);
2939 /* Update all .dynamic entries referencing .dynstr strings. */
2940 for (extdyn
= sdyn
->contents
;
2941 extdyn
< sdyn
->contents
+ sdyn
->size
;
2942 extdyn
+= bed
->s
->sizeof_dyn
)
2944 Elf_Internal_Dyn dyn
;
2946 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2950 dyn
.d_un
.d_val
= size
;
2958 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2963 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2966 /* Now update local dynamic symbols. */
2967 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2968 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2969 entry
->isym
.st_name
);
2971 /* And the rest of dynamic symbols. */
2972 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2974 /* Adjust version definitions. */
2975 if (elf_tdata (output_bfd
)->cverdefs
)
2980 Elf_Internal_Verdef def
;
2981 Elf_Internal_Verdaux defaux
;
2983 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2987 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2989 p
+= sizeof (Elf_External_Verdef
);
2990 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
2992 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2994 _bfd_elf_swap_verdaux_in (output_bfd
,
2995 (Elf_External_Verdaux
*) p
, &defaux
);
2996 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2998 _bfd_elf_swap_verdaux_out (output_bfd
,
2999 &defaux
, (Elf_External_Verdaux
*) p
);
3000 p
+= sizeof (Elf_External_Verdaux
);
3003 while (def
.vd_next
);
3006 /* Adjust version references. */
3007 if (elf_tdata (output_bfd
)->verref
)
3012 Elf_Internal_Verneed need
;
3013 Elf_Internal_Vernaux needaux
;
3015 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3019 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3021 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3022 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3023 (Elf_External_Verneed
*) p
);
3024 p
+= sizeof (Elf_External_Verneed
);
3025 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3027 _bfd_elf_swap_vernaux_in (output_bfd
,
3028 (Elf_External_Vernaux
*) p
, &needaux
);
3029 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3031 _bfd_elf_swap_vernaux_out (output_bfd
,
3033 (Elf_External_Vernaux
*) p
);
3034 p
+= sizeof (Elf_External_Vernaux
);
3037 while (need
.vn_next
);
3043 /* Add symbols from an ELF object file to the linker hash table. */
3046 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3048 Elf_Internal_Shdr
*hdr
;
3049 bfd_size_type symcount
;
3050 bfd_size_type extsymcount
;
3051 bfd_size_type extsymoff
;
3052 struct elf_link_hash_entry
**sym_hash
;
3053 bfd_boolean dynamic
;
3054 Elf_External_Versym
*extversym
= NULL
;
3055 Elf_External_Versym
*ever
;
3056 struct elf_link_hash_entry
*weaks
;
3057 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3058 bfd_size_type nondeflt_vers_cnt
= 0;
3059 Elf_Internal_Sym
*isymbuf
= NULL
;
3060 Elf_Internal_Sym
*isym
;
3061 Elf_Internal_Sym
*isymend
;
3062 const struct elf_backend_data
*bed
;
3063 bfd_boolean add_needed
;
3064 struct elf_link_hash_table
*htab
;
3066 void *alloc_mark
= NULL
;
3067 void *old_tab
= NULL
;
3070 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3071 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3072 long old_dynsymcount
= 0;
3074 size_t hashsize
= 0;
3076 htab
= elf_hash_table (info
);
3077 bed
= get_elf_backend_data (abfd
);
3079 if ((abfd
->flags
& DYNAMIC
) == 0)
3085 /* You can't use -r against a dynamic object. Also, there's no
3086 hope of using a dynamic object which does not exactly match
3087 the format of the output file. */
3088 if (info
->relocatable
3089 || !is_elf_hash_table (htab
)
3090 || htab
->root
.creator
!= abfd
->xvec
)
3092 if (info
->relocatable
)
3093 bfd_set_error (bfd_error_invalid_operation
);
3095 bfd_set_error (bfd_error_wrong_format
);
3100 /* As a GNU extension, any input sections which are named
3101 .gnu.warning.SYMBOL are treated as warning symbols for the given
3102 symbol. This differs from .gnu.warning sections, which generate
3103 warnings when they are included in an output file. */
3104 if (info
->executable
)
3108 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3112 name
= bfd_get_section_name (abfd
, s
);
3113 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3118 name
+= sizeof ".gnu.warning." - 1;
3120 /* If this is a shared object, then look up the symbol
3121 in the hash table. If it is there, and it is already
3122 been defined, then we will not be using the entry
3123 from this shared object, so we don't need to warn.
3124 FIXME: If we see the definition in a regular object
3125 later on, we will warn, but we shouldn't. The only
3126 fix is to keep track of what warnings we are supposed
3127 to emit, and then handle them all at the end of the
3131 struct elf_link_hash_entry
*h
;
3133 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3135 /* FIXME: What about bfd_link_hash_common? */
3137 && (h
->root
.type
== bfd_link_hash_defined
3138 || h
->root
.type
== bfd_link_hash_defweak
))
3140 /* We don't want to issue this warning. Clobber
3141 the section size so that the warning does not
3142 get copied into the output file. */
3149 msg
= bfd_alloc (abfd
, sz
+ 1);
3153 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3158 if (! (_bfd_generic_link_add_one_symbol
3159 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3160 FALSE
, bed
->collect
, NULL
)))
3163 if (! info
->relocatable
)
3165 /* Clobber the section size so that the warning does
3166 not get copied into the output file. */
3169 /* Also set SEC_EXCLUDE, so that symbols defined in
3170 the warning section don't get copied to the output. */
3171 s
->flags
|= SEC_EXCLUDE
;
3180 /* If we are creating a shared library, create all the dynamic
3181 sections immediately. We need to attach them to something,
3182 so we attach them to this BFD, provided it is the right
3183 format. FIXME: If there are no input BFD's of the same
3184 format as the output, we can't make a shared library. */
3186 && is_elf_hash_table (htab
)
3187 && htab
->root
.creator
== abfd
->xvec
3188 && !htab
->dynamic_sections_created
)
3190 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3194 else if (!is_elf_hash_table (htab
))
3199 const char *soname
= NULL
;
3200 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3203 /* ld --just-symbols and dynamic objects don't mix very well.
3204 ld shouldn't allow it. */
3205 if ((s
= abfd
->sections
) != NULL
3206 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3209 /* If this dynamic lib was specified on the command line with
3210 --as-needed in effect, then we don't want to add a DT_NEEDED
3211 tag unless the lib is actually used. Similary for libs brought
3212 in by another lib's DT_NEEDED. When --no-add-needed is used
3213 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3214 any dynamic library in DT_NEEDED tags in the dynamic lib at
3216 add_needed
= (elf_dyn_lib_class (abfd
)
3217 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3218 | DYN_NO_NEEDED
)) == 0;
3220 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3226 unsigned long shlink
;
3228 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3229 goto error_free_dyn
;
3231 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3233 goto error_free_dyn
;
3234 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3236 for (extdyn
= dynbuf
;
3237 extdyn
< dynbuf
+ s
->size
;
3238 extdyn
+= bed
->s
->sizeof_dyn
)
3240 Elf_Internal_Dyn dyn
;
3242 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3243 if (dyn
.d_tag
== DT_SONAME
)
3245 unsigned int tagv
= dyn
.d_un
.d_val
;
3246 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3248 goto error_free_dyn
;
3250 if (dyn
.d_tag
== DT_NEEDED
)
3252 struct bfd_link_needed_list
*n
, **pn
;
3254 unsigned int tagv
= dyn
.d_un
.d_val
;
3256 amt
= sizeof (struct bfd_link_needed_list
);
3257 n
= bfd_alloc (abfd
, amt
);
3258 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3259 if (n
== NULL
|| fnm
== NULL
)
3260 goto error_free_dyn
;
3261 amt
= strlen (fnm
) + 1;
3262 anm
= bfd_alloc (abfd
, amt
);
3264 goto error_free_dyn
;
3265 memcpy (anm
, fnm
, amt
);
3269 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3273 if (dyn
.d_tag
== DT_RUNPATH
)
3275 struct bfd_link_needed_list
*n
, **pn
;
3277 unsigned int tagv
= dyn
.d_un
.d_val
;
3279 amt
= sizeof (struct bfd_link_needed_list
);
3280 n
= bfd_alloc (abfd
, amt
);
3281 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3282 if (n
== NULL
|| fnm
== NULL
)
3283 goto error_free_dyn
;
3284 amt
= strlen (fnm
) + 1;
3285 anm
= bfd_alloc (abfd
, amt
);
3287 goto error_free_dyn
;
3288 memcpy (anm
, fnm
, amt
);
3292 for (pn
= & runpath
;
3298 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3299 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3301 struct bfd_link_needed_list
*n
, **pn
;
3303 unsigned int tagv
= dyn
.d_un
.d_val
;
3305 amt
= sizeof (struct bfd_link_needed_list
);
3306 n
= bfd_alloc (abfd
, amt
);
3307 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3308 if (n
== NULL
|| fnm
== NULL
)
3309 goto error_free_dyn
;
3310 amt
= strlen (fnm
) + 1;
3311 anm
= bfd_alloc (abfd
, amt
);
3318 memcpy (anm
, fnm
, amt
);
3333 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3334 frees all more recently bfd_alloc'd blocks as well. */
3340 struct bfd_link_needed_list
**pn
;
3341 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3346 /* We do not want to include any of the sections in a dynamic
3347 object in the output file. We hack by simply clobbering the
3348 list of sections in the BFD. This could be handled more
3349 cleanly by, say, a new section flag; the existing
3350 SEC_NEVER_LOAD flag is not the one we want, because that one
3351 still implies that the section takes up space in the output
3353 bfd_section_list_clear (abfd
);
3355 /* Find the name to use in a DT_NEEDED entry that refers to this
3356 object. If the object has a DT_SONAME entry, we use it.
3357 Otherwise, if the generic linker stuck something in
3358 elf_dt_name, we use that. Otherwise, we just use the file
3360 if (soname
== NULL
|| *soname
== '\0')
3362 soname
= elf_dt_name (abfd
);
3363 if (soname
== NULL
|| *soname
== '\0')
3364 soname
= bfd_get_filename (abfd
);
3367 /* Save the SONAME because sometimes the linker emulation code
3368 will need to know it. */
3369 elf_dt_name (abfd
) = soname
;
3371 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3375 /* If we have already included this dynamic object in the
3376 link, just ignore it. There is no reason to include a
3377 particular dynamic object more than once. */
3382 /* If this is a dynamic object, we always link against the .dynsym
3383 symbol table, not the .symtab symbol table. The dynamic linker
3384 will only see the .dynsym symbol table, so there is no reason to
3385 look at .symtab for a dynamic object. */
3387 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3388 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3390 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3392 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3394 /* The sh_info field of the symtab header tells us where the
3395 external symbols start. We don't care about the local symbols at
3397 if (elf_bad_symtab (abfd
))
3399 extsymcount
= symcount
;
3404 extsymcount
= symcount
- hdr
->sh_info
;
3405 extsymoff
= hdr
->sh_info
;
3409 if (extsymcount
!= 0)
3411 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3413 if (isymbuf
== NULL
)
3416 /* We store a pointer to the hash table entry for each external
3418 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3419 sym_hash
= bfd_alloc (abfd
, amt
);
3420 if (sym_hash
== NULL
)
3421 goto error_free_sym
;
3422 elf_sym_hashes (abfd
) = sym_hash
;
3427 /* Read in any version definitions. */
3428 if (!_bfd_elf_slurp_version_tables (abfd
,
3429 info
->default_imported_symver
))
3430 goto error_free_sym
;
3432 /* Read in the symbol versions, but don't bother to convert them
3433 to internal format. */
3434 if (elf_dynversym (abfd
) != 0)
3436 Elf_Internal_Shdr
*versymhdr
;
3438 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3439 extversym
= bfd_malloc (versymhdr
->sh_size
);
3440 if (extversym
== NULL
)
3441 goto error_free_sym
;
3442 amt
= versymhdr
->sh_size
;
3443 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3444 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3445 goto error_free_vers
;
3449 /* If we are loading an as-needed shared lib, save the symbol table
3450 state before we start adding symbols. If the lib turns out
3451 to be unneeded, restore the state. */
3452 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3457 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3459 struct bfd_hash_entry
*p
;
3461 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3462 entsize
+= htab
->root
.table
.entsize
;
3465 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3466 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3467 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3468 if (old_tab
== NULL
)
3469 goto error_free_vers
;
3471 /* Remember the current objalloc pointer, so that all mem for
3472 symbols added can later be reclaimed. */
3473 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3474 if (alloc_mark
== NULL
)
3475 goto error_free_vers
;
3477 /* Clone the symbol table and sym hashes. Remember some
3478 pointers into the symbol table, and dynamic symbol count. */
3479 old_hash
= (char *) old_tab
+ tabsize
;
3480 old_ent
= (char *) old_hash
+ hashsize
;
3481 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3482 memcpy (old_hash
, sym_hash
, hashsize
);
3483 old_undefs
= htab
->root
.undefs
;
3484 old_undefs_tail
= htab
->root
.undefs_tail
;
3485 old_dynsymcount
= htab
->dynsymcount
;
3487 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3489 struct bfd_hash_entry
*p
;
3491 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3493 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3494 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3500 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3501 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3503 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3507 asection
*sec
, *new_sec
;
3510 struct elf_link_hash_entry
*h
;
3511 bfd_boolean definition
;
3512 bfd_boolean size_change_ok
;
3513 bfd_boolean type_change_ok
;
3514 bfd_boolean new_weakdef
;
3515 bfd_boolean override
;
3517 unsigned int old_alignment
;
3522 flags
= BSF_NO_FLAGS
;
3524 value
= isym
->st_value
;
3526 common
= bed
->common_definition (isym
);
3528 bind
= ELF_ST_BIND (isym
->st_info
);
3529 if (bind
== STB_LOCAL
)
3531 /* This should be impossible, since ELF requires that all
3532 global symbols follow all local symbols, and that sh_info
3533 point to the first global symbol. Unfortunately, Irix 5
3537 else if (bind
== STB_GLOBAL
)
3539 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3542 else if (bind
== STB_WEAK
)
3546 /* Leave it up to the processor backend. */
3549 if (isym
->st_shndx
== SHN_UNDEF
)
3550 sec
= bfd_und_section_ptr
;
3551 else if (isym
->st_shndx
< SHN_LORESERVE
3552 || isym
->st_shndx
> SHN_HIRESERVE
)
3554 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3556 sec
= bfd_abs_section_ptr
;
3557 else if (sec
->kept_section
)
3559 /* Symbols from discarded section are undefined, and have
3560 default visibility. */
3561 sec
= bfd_und_section_ptr
;
3562 isym
->st_shndx
= SHN_UNDEF
;
3563 isym
->st_other
= (STV_DEFAULT
3564 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3566 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3569 else if (isym
->st_shndx
== SHN_ABS
)
3570 sec
= bfd_abs_section_ptr
;
3571 else if (isym
->st_shndx
== SHN_COMMON
)
3573 sec
= bfd_com_section_ptr
;
3574 /* What ELF calls the size we call the value. What ELF
3575 calls the value we call the alignment. */
3576 value
= isym
->st_size
;
3580 /* Leave it up to the processor backend. */
3583 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3586 goto error_free_vers
;
3588 if (isym
->st_shndx
== SHN_COMMON
3589 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3591 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3595 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3598 | SEC_LINKER_CREATED
3599 | SEC_THREAD_LOCAL
));
3601 goto error_free_vers
;
3605 else if (bed
->elf_add_symbol_hook
)
3607 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3609 goto error_free_vers
;
3611 /* The hook function sets the name to NULL if this symbol
3612 should be skipped for some reason. */
3617 /* Sanity check that all possibilities were handled. */
3620 bfd_set_error (bfd_error_bad_value
);
3621 goto error_free_vers
;
3624 if (bfd_is_und_section (sec
)
3625 || bfd_is_com_section (sec
))
3630 size_change_ok
= FALSE
;
3631 type_change_ok
= bed
->type_change_ok
;
3636 if (is_elf_hash_table (htab
))
3638 Elf_Internal_Versym iver
;
3639 unsigned int vernum
= 0;
3644 if (info
->default_imported_symver
)
3645 /* Use the default symbol version created earlier. */
3646 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3651 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3653 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3655 /* If this is a hidden symbol, or if it is not version
3656 1, we append the version name to the symbol name.
3657 However, we do not modify a non-hidden absolute symbol
3658 if it is not a function, because it might be the version
3659 symbol itself. FIXME: What if it isn't? */
3660 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3661 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3662 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3665 size_t namelen
, verlen
, newlen
;
3668 if (isym
->st_shndx
!= SHN_UNDEF
)
3670 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3672 else if (vernum
> 1)
3674 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3680 (*_bfd_error_handler
)
3681 (_("%B: %s: invalid version %u (max %d)"),
3683 elf_tdata (abfd
)->cverdefs
);
3684 bfd_set_error (bfd_error_bad_value
);
3685 goto error_free_vers
;
3690 /* We cannot simply test for the number of
3691 entries in the VERNEED section since the
3692 numbers for the needed versions do not start
3694 Elf_Internal_Verneed
*t
;
3697 for (t
= elf_tdata (abfd
)->verref
;
3701 Elf_Internal_Vernaux
*a
;
3703 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3705 if (a
->vna_other
== vernum
)
3707 verstr
= a
->vna_nodename
;
3716 (*_bfd_error_handler
)
3717 (_("%B: %s: invalid needed version %d"),
3718 abfd
, name
, vernum
);
3719 bfd_set_error (bfd_error_bad_value
);
3720 goto error_free_vers
;
3724 namelen
= strlen (name
);
3725 verlen
= strlen (verstr
);
3726 newlen
= namelen
+ verlen
+ 2;
3727 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3728 && isym
->st_shndx
!= SHN_UNDEF
)
3731 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3732 if (newname
== NULL
)
3733 goto error_free_vers
;
3734 memcpy (newname
, name
, namelen
);
3735 p
= newname
+ namelen
;
3737 /* If this is a defined non-hidden version symbol,
3738 we add another @ to the name. This indicates the
3739 default version of the symbol. */
3740 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3741 && isym
->st_shndx
!= SHN_UNDEF
)
3743 memcpy (p
, verstr
, verlen
+ 1);
3748 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3749 &value
, &old_alignment
,
3750 sym_hash
, &skip
, &override
,
3751 &type_change_ok
, &size_change_ok
))
3752 goto error_free_vers
;
3761 while (h
->root
.type
== bfd_link_hash_indirect
3762 || h
->root
.type
== bfd_link_hash_warning
)
3763 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3765 /* Remember the old alignment if this is a common symbol, so
3766 that we don't reduce the alignment later on. We can't
3767 check later, because _bfd_generic_link_add_one_symbol
3768 will set a default for the alignment which we want to
3769 override. We also remember the old bfd where the existing
3770 definition comes from. */
3771 switch (h
->root
.type
)
3776 case bfd_link_hash_defined
:
3777 case bfd_link_hash_defweak
:
3778 old_bfd
= h
->root
.u
.def
.section
->owner
;
3781 case bfd_link_hash_common
:
3782 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3783 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3787 if (elf_tdata (abfd
)->verdef
!= NULL
3791 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3794 if (! (_bfd_generic_link_add_one_symbol
3795 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3796 (struct bfd_link_hash_entry
**) sym_hash
)))
3797 goto error_free_vers
;
3800 while (h
->root
.type
== bfd_link_hash_indirect
3801 || h
->root
.type
== bfd_link_hash_warning
)
3802 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3805 new_weakdef
= FALSE
;
3808 && (flags
& BSF_WEAK
) != 0
3809 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3810 && is_elf_hash_table (htab
)
3811 && h
->u
.weakdef
== NULL
)
3813 /* Keep a list of all weak defined non function symbols from
3814 a dynamic object, using the weakdef field. Later in this
3815 function we will set the weakdef field to the correct
3816 value. We only put non-function symbols from dynamic
3817 objects on this list, because that happens to be the only
3818 time we need to know the normal symbol corresponding to a
3819 weak symbol, and the information is time consuming to
3820 figure out. If the weakdef field is not already NULL,
3821 then this symbol was already defined by some previous
3822 dynamic object, and we will be using that previous
3823 definition anyhow. */
3825 h
->u
.weakdef
= weaks
;
3830 /* Set the alignment of a common symbol. */
3831 if ((common
|| bfd_is_com_section (sec
))
3832 && h
->root
.type
== bfd_link_hash_common
)
3837 align
= bfd_log2 (isym
->st_value
);
3840 /* The new symbol is a common symbol in a shared object.
3841 We need to get the alignment from the section. */
3842 align
= new_sec
->alignment_power
;
3844 if (align
> old_alignment
3845 /* Permit an alignment power of zero if an alignment of one
3846 is specified and no other alignments have been specified. */
3847 || (isym
->st_value
== 1 && old_alignment
== 0))
3848 h
->root
.u
.c
.p
->alignment_power
= align
;
3850 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3853 if (is_elf_hash_table (htab
))
3857 /* Check the alignment when a common symbol is involved. This
3858 can change when a common symbol is overridden by a normal
3859 definition or a common symbol is ignored due to the old
3860 normal definition. We need to make sure the maximum
3861 alignment is maintained. */
3862 if ((old_alignment
|| common
)
3863 && h
->root
.type
!= bfd_link_hash_common
)
3865 unsigned int common_align
;
3866 unsigned int normal_align
;
3867 unsigned int symbol_align
;
3871 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3872 if (h
->root
.u
.def
.section
->owner
!= NULL
3873 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3875 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3876 if (normal_align
> symbol_align
)
3877 normal_align
= symbol_align
;
3880 normal_align
= symbol_align
;
3884 common_align
= old_alignment
;
3885 common_bfd
= old_bfd
;
3890 common_align
= bfd_log2 (isym
->st_value
);
3892 normal_bfd
= old_bfd
;
3895 if (normal_align
< common_align
)
3896 (*_bfd_error_handler
)
3897 (_("Warning: alignment %u of symbol `%s' in %B"
3898 " is smaller than %u in %B"),
3899 normal_bfd
, common_bfd
,
3900 1 << normal_align
, name
, 1 << common_align
);
3903 /* Remember the symbol size and type. */
3904 if (isym
->st_size
!= 0
3905 && (definition
|| h
->size
== 0))
3907 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3908 (*_bfd_error_handler
)
3909 (_("Warning: size of symbol `%s' changed"
3910 " from %lu in %B to %lu in %B"),
3912 name
, (unsigned long) h
->size
,
3913 (unsigned long) isym
->st_size
);
3915 h
->size
= isym
->st_size
;
3918 /* If this is a common symbol, then we always want H->SIZE
3919 to be the size of the common symbol. The code just above
3920 won't fix the size if a common symbol becomes larger. We
3921 don't warn about a size change here, because that is
3922 covered by --warn-common. */
3923 if (h
->root
.type
== bfd_link_hash_common
)
3924 h
->size
= h
->root
.u
.c
.size
;
3926 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3927 && (definition
|| h
->type
== STT_NOTYPE
))
3929 if (h
->type
!= STT_NOTYPE
3930 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3931 && ! type_change_ok
)
3932 (*_bfd_error_handler
)
3933 (_("Warning: type of symbol `%s' changed"
3934 " from %d to %d in %B"),
3935 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3937 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3940 /* If st_other has a processor-specific meaning, specific
3941 code might be needed here. We never merge the visibility
3942 attribute with the one from a dynamic object. */
3943 if (bed
->elf_backend_merge_symbol_attribute
)
3944 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3947 /* If this symbol has default visibility and the user has requested
3948 we not re-export it, then mark it as hidden. */
3949 if (definition
&& !dynamic
3951 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3952 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3953 isym
->st_other
= (STV_HIDDEN
3954 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
3956 if (isym
->st_other
!= 0 && !dynamic
)
3958 unsigned char hvis
, symvis
, other
, nvis
;
3960 /* Take the balance of OTHER from the definition. */
3961 other
= (definition
? isym
->st_other
: h
->other
);
3962 other
&= ~ ELF_ST_VISIBILITY (-1);
3964 /* Combine visibilities, using the most constraining one. */
3965 hvis
= ELF_ST_VISIBILITY (h
->other
);
3966 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3972 nvis
= hvis
< symvis
? hvis
: symvis
;
3974 h
->other
= other
| nvis
;
3977 /* Set a flag in the hash table entry indicating the type of
3978 reference or definition we just found. Keep a count of
3979 the number of dynamic symbols we find. A dynamic symbol
3980 is one which is referenced or defined by both a regular
3981 object and a shared object. */
3988 if (bind
!= STB_WEAK
)
3989 h
->ref_regular_nonweak
= 1;
3993 if (! info
->executable
4006 || (h
->u
.weakdef
!= NULL
4008 && h
->u
.weakdef
->dynindx
!= -1))
4012 /* Check to see if we need to add an indirect symbol for
4013 the default name. */
4014 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4015 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4016 &sec
, &value
, &dynsym
,
4018 goto error_free_vers
;
4020 if (definition
&& !dynamic
)
4022 char *p
= strchr (name
, ELF_VER_CHR
);
4023 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4025 /* Queue non-default versions so that .symver x, x@FOO
4026 aliases can be checked. */
4029 amt
= ((isymend
- isym
+ 1)
4030 * sizeof (struct elf_link_hash_entry
*));
4031 nondeflt_vers
= bfd_malloc (amt
);
4033 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4037 if (dynsym
&& h
->dynindx
== -1)
4039 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4040 goto error_free_vers
;
4041 if (h
->u
.weakdef
!= NULL
4043 && h
->u
.weakdef
->dynindx
== -1)
4045 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4046 goto error_free_vers
;
4049 else if (dynsym
&& h
->dynindx
!= -1)
4050 /* If the symbol already has a dynamic index, but
4051 visibility says it should not be visible, turn it into
4053 switch (ELF_ST_VISIBILITY (h
->other
))
4057 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4068 const char *soname
= elf_dt_name (abfd
);
4070 /* A symbol from a library loaded via DT_NEEDED of some
4071 other library is referenced by a regular object.
4072 Add a DT_NEEDED entry for it. Issue an error if
4073 --no-add-needed is used. */
4074 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4076 (*_bfd_error_handler
)
4077 (_("%s: invalid DSO for symbol `%s' definition"),
4079 bfd_set_error (bfd_error_bad_value
);
4080 goto error_free_vers
;
4083 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4086 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4088 goto error_free_vers
;
4090 BFD_ASSERT (ret
== 0);
4095 if (extversym
!= NULL
)
4101 if (isymbuf
!= NULL
)
4107 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4111 /* Restore the symbol table. */
4112 old_hash
= (char *) old_tab
+ tabsize
;
4113 old_ent
= (char *) old_hash
+ hashsize
;
4114 sym_hash
= elf_sym_hashes (abfd
);
4115 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4116 memcpy (sym_hash
, old_hash
, hashsize
);
4117 htab
->root
.undefs
= old_undefs
;
4118 htab
->root
.undefs_tail
= old_undefs_tail
;
4119 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4121 struct bfd_hash_entry
*p
;
4122 struct elf_link_hash_entry
*h
;
4124 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4126 h
= (struct elf_link_hash_entry
*) p
;
4127 if (h
->dynindx
>= old_dynsymcount
)
4128 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4129 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4130 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4135 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4137 if (nondeflt_vers
!= NULL
)
4138 free (nondeflt_vers
);
4142 if (old_tab
!= NULL
)
4148 /* Now that all the symbols from this input file are created, handle
4149 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4150 if (nondeflt_vers
!= NULL
)
4152 bfd_size_type cnt
, symidx
;
4154 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4156 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4157 char *shortname
, *p
;
4159 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4161 || (h
->root
.type
!= bfd_link_hash_defined
4162 && h
->root
.type
!= bfd_link_hash_defweak
))
4165 amt
= p
- h
->root
.root
.string
;
4166 shortname
= bfd_malloc (amt
+ 1);
4167 memcpy (shortname
, h
->root
.root
.string
, amt
);
4168 shortname
[amt
] = '\0';
4170 hi
= (struct elf_link_hash_entry
*)
4171 bfd_link_hash_lookup (&htab
->root
, shortname
,
4172 FALSE
, FALSE
, FALSE
);
4174 && hi
->root
.type
== h
->root
.type
4175 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4176 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4178 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4179 hi
->root
.type
= bfd_link_hash_indirect
;
4180 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4181 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4182 sym_hash
= elf_sym_hashes (abfd
);
4184 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4185 if (sym_hash
[symidx
] == hi
)
4187 sym_hash
[symidx
] = h
;
4193 free (nondeflt_vers
);
4194 nondeflt_vers
= NULL
;
4197 /* Now set the weakdefs field correctly for all the weak defined
4198 symbols we found. The only way to do this is to search all the
4199 symbols. Since we only need the information for non functions in
4200 dynamic objects, that's the only time we actually put anything on
4201 the list WEAKS. We need this information so that if a regular
4202 object refers to a symbol defined weakly in a dynamic object, the
4203 real symbol in the dynamic object is also put in the dynamic
4204 symbols; we also must arrange for both symbols to point to the
4205 same memory location. We could handle the general case of symbol
4206 aliasing, but a general symbol alias can only be generated in
4207 assembler code, handling it correctly would be very time
4208 consuming, and other ELF linkers don't handle general aliasing
4212 struct elf_link_hash_entry
**hpp
;
4213 struct elf_link_hash_entry
**hppend
;
4214 struct elf_link_hash_entry
**sorted_sym_hash
;
4215 struct elf_link_hash_entry
*h
;
4218 /* Since we have to search the whole symbol list for each weak
4219 defined symbol, search time for N weak defined symbols will be
4220 O(N^2). Binary search will cut it down to O(NlogN). */
4221 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4222 sorted_sym_hash
= bfd_malloc (amt
);
4223 if (sorted_sym_hash
== NULL
)
4225 sym_hash
= sorted_sym_hash
;
4226 hpp
= elf_sym_hashes (abfd
);
4227 hppend
= hpp
+ extsymcount
;
4229 for (; hpp
< hppend
; hpp
++)
4233 && h
->root
.type
== bfd_link_hash_defined
4234 && h
->type
!= STT_FUNC
)
4242 qsort (sorted_sym_hash
, sym_count
,
4243 sizeof (struct elf_link_hash_entry
*),
4246 while (weaks
!= NULL
)
4248 struct elf_link_hash_entry
*hlook
;
4255 weaks
= hlook
->u
.weakdef
;
4256 hlook
->u
.weakdef
= NULL
;
4258 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4259 || hlook
->root
.type
== bfd_link_hash_defweak
4260 || hlook
->root
.type
== bfd_link_hash_common
4261 || hlook
->root
.type
== bfd_link_hash_indirect
);
4262 slook
= hlook
->root
.u
.def
.section
;
4263 vlook
= hlook
->root
.u
.def
.value
;
4270 bfd_signed_vma vdiff
;
4272 h
= sorted_sym_hash
[idx
];
4273 vdiff
= vlook
- h
->root
.u
.def
.value
;
4280 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4293 /* We didn't find a value/section match. */
4297 for (i
= ilook
; i
< sym_count
; i
++)
4299 h
= sorted_sym_hash
[i
];
4301 /* Stop if value or section doesn't match. */
4302 if (h
->root
.u
.def
.value
!= vlook
4303 || h
->root
.u
.def
.section
!= slook
)
4305 else if (h
!= hlook
)
4307 hlook
->u
.weakdef
= h
;
4309 /* If the weak definition is in the list of dynamic
4310 symbols, make sure the real definition is put
4312 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4314 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4318 /* If the real definition is in the list of dynamic
4319 symbols, make sure the weak definition is put
4320 there as well. If we don't do this, then the
4321 dynamic loader might not merge the entries for the
4322 real definition and the weak definition. */
4323 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4325 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4333 free (sorted_sym_hash
);
4336 if (bed
->check_directives
)
4337 (*bed
->check_directives
) (abfd
, info
);
4339 /* If this object is the same format as the output object, and it is
4340 not a shared library, then let the backend look through the
4343 This is required to build global offset table entries and to
4344 arrange for dynamic relocs. It is not required for the
4345 particular common case of linking non PIC code, even when linking
4346 against shared libraries, but unfortunately there is no way of
4347 knowing whether an object file has been compiled PIC or not.
4348 Looking through the relocs is not particularly time consuming.
4349 The problem is that we must either (1) keep the relocs in memory,
4350 which causes the linker to require additional runtime memory or
4351 (2) read the relocs twice from the input file, which wastes time.
4352 This would be a good case for using mmap.
4354 I have no idea how to handle linking PIC code into a file of a
4355 different format. It probably can't be done. */
4357 && is_elf_hash_table (htab
)
4358 && htab
->root
.creator
== abfd
->xvec
4359 && bed
->check_relocs
!= NULL
)
4363 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4365 Elf_Internal_Rela
*internal_relocs
;
4368 if ((o
->flags
& SEC_RELOC
) == 0
4369 || o
->reloc_count
== 0
4370 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4371 && (o
->flags
& SEC_DEBUGGING
) != 0)
4372 || bfd_is_abs_section (o
->output_section
))
4375 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4377 if (internal_relocs
== NULL
)
4380 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4382 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4383 free (internal_relocs
);
4390 /* If this is a non-traditional link, try to optimize the handling
4391 of the .stab/.stabstr sections. */
4393 && ! info
->traditional_format
4394 && is_elf_hash_table (htab
)
4395 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4399 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4400 if (stabstr
!= NULL
)
4402 bfd_size_type string_offset
= 0;
4405 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4406 if (strncmp (".stab", stab
->name
, 5) == 0
4407 && (!stab
->name
[5] ||
4408 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4409 && (stab
->flags
& SEC_MERGE
) == 0
4410 && !bfd_is_abs_section (stab
->output_section
))
4412 struct bfd_elf_section_data
*secdata
;
4414 secdata
= elf_section_data (stab
);
4415 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4416 stabstr
, &secdata
->sec_info
,
4419 if (secdata
->sec_info
)
4420 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4425 if (is_elf_hash_table (htab
) && add_needed
)
4427 /* Add this bfd to the loaded list. */
4428 struct elf_link_loaded_list
*n
;
4430 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4434 n
->next
= htab
->loaded
;
4441 if (old_tab
!= NULL
)
4443 if (nondeflt_vers
!= NULL
)
4444 free (nondeflt_vers
);
4445 if (extversym
!= NULL
)
4448 if (isymbuf
!= NULL
)
4454 /* Return the linker hash table entry of a symbol that might be
4455 satisfied by an archive symbol. Return -1 on error. */
4457 struct elf_link_hash_entry
*
4458 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4459 struct bfd_link_info
*info
,
4462 struct elf_link_hash_entry
*h
;
4466 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4470 /* If this is a default version (the name contains @@), look up the
4471 symbol again with only one `@' as well as without the version.
4472 The effect is that references to the symbol with and without the
4473 version will be matched by the default symbol in the archive. */
4475 p
= strchr (name
, ELF_VER_CHR
);
4476 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4479 /* First check with only one `@'. */
4480 len
= strlen (name
);
4481 copy
= bfd_alloc (abfd
, len
);
4483 return (struct elf_link_hash_entry
*) 0 - 1;
4485 first
= p
- name
+ 1;
4486 memcpy (copy
, name
, first
);
4487 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4489 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4492 /* We also need to check references to the symbol without the
4494 copy
[first
- 1] = '\0';
4495 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4496 FALSE
, FALSE
, FALSE
);
4499 bfd_release (abfd
, copy
);
4503 /* Add symbols from an ELF archive file to the linker hash table. We
4504 don't use _bfd_generic_link_add_archive_symbols because of a
4505 problem which arises on UnixWare. The UnixWare libc.so is an
4506 archive which includes an entry libc.so.1 which defines a bunch of
4507 symbols. The libc.so archive also includes a number of other
4508 object files, which also define symbols, some of which are the same
4509 as those defined in libc.so.1. Correct linking requires that we
4510 consider each object file in turn, and include it if it defines any
4511 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4512 this; it looks through the list of undefined symbols, and includes
4513 any object file which defines them. When this algorithm is used on
4514 UnixWare, it winds up pulling in libc.so.1 early and defining a
4515 bunch of symbols. This means that some of the other objects in the
4516 archive are not included in the link, which is incorrect since they
4517 precede libc.so.1 in the archive.
4519 Fortunately, ELF archive handling is simpler than that done by
4520 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4521 oddities. In ELF, if we find a symbol in the archive map, and the
4522 symbol is currently undefined, we know that we must pull in that
4525 Unfortunately, we do have to make multiple passes over the symbol
4526 table until nothing further is resolved. */
4529 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4532 bfd_boolean
*defined
= NULL
;
4533 bfd_boolean
*included
= NULL
;
4537 const struct elf_backend_data
*bed
;
4538 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4539 (bfd
*, struct bfd_link_info
*, const char *);
4541 if (! bfd_has_map (abfd
))
4543 /* An empty archive is a special case. */
4544 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4546 bfd_set_error (bfd_error_no_armap
);
4550 /* Keep track of all symbols we know to be already defined, and all
4551 files we know to be already included. This is to speed up the
4552 second and subsequent passes. */
4553 c
= bfd_ardata (abfd
)->symdef_count
;
4557 amt
*= sizeof (bfd_boolean
);
4558 defined
= bfd_zmalloc (amt
);
4559 included
= bfd_zmalloc (amt
);
4560 if (defined
== NULL
|| included
== NULL
)
4563 symdefs
= bfd_ardata (abfd
)->symdefs
;
4564 bed
= get_elf_backend_data (abfd
);
4565 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4578 symdefend
= symdef
+ c
;
4579 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4581 struct elf_link_hash_entry
*h
;
4583 struct bfd_link_hash_entry
*undefs_tail
;
4586 if (defined
[i
] || included
[i
])
4588 if (symdef
->file_offset
== last
)
4594 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4595 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4601 if (h
->root
.type
== bfd_link_hash_common
)
4603 /* We currently have a common symbol. The archive map contains
4604 a reference to this symbol, so we may want to include it. We
4605 only want to include it however, if this archive element
4606 contains a definition of the symbol, not just another common
4609 Unfortunately some archivers (including GNU ar) will put
4610 declarations of common symbols into their archive maps, as
4611 well as real definitions, so we cannot just go by the archive
4612 map alone. Instead we must read in the element's symbol
4613 table and check that to see what kind of symbol definition
4615 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4618 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4620 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4625 /* We need to include this archive member. */
4626 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4627 if (element
== NULL
)
4630 if (! bfd_check_format (element
, bfd_object
))
4633 /* Doublecheck that we have not included this object
4634 already--it should be impossible, but there may be
4635 something wrong with the archive. */
4636 if (element
->archive_pass
!= 0)
4638 bfd_set_error (bfd_error_bad_value
);
4641 element
->archive_pass
= 1;
4643 undefs_tail
= info
->hash
->undefs_tail
;
4645 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4648 if (! bfd_link_add_symbols (element
, info
))
4651 /* If there are any new undefined symbols, we need to make
4652 another pass through the archive in order to see whether
4653 they can be defined. FIXME: This isn't perfect, because
4654 common symbols wind up on undefs_tail and because an
4655 undefined symbol which is defined later on in this pass
4656 does not require another pass. This isn't a bug, but it
4657 does make the code less efficient than it could be. */
4658 if (undefs_tail
!= info
->hash
->undefs_tail
)
4661 /* Look backward to mark all symbols from this object file
4662 which we have already seen in this pass. */
4666 included
[mark
] = TRUE
;
4671 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4673 /* We mark subsequent symbols from this object file as we go
4674 on through the loop. */
4675 last
= symdef
->file_offset
;
4686 if (defined
!= NULL
)
4688 if (included
!= NULL
)
4693 /* Given an ELF BFD, add symbols to the global hash table as
4697 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4699 switch (bfd_get_format (abfd
))
4702 return elf_link_add_object_symbols (abfd
, info
);
4704 return elf_link_add_archive_symbols (abfd
, info
);
4706 bfd_set_error (bfd_error_wrong_format
);
4711 /* This function will be called though elf_link_hash_traverse to store
4712 all hash value of the exported symbols in an array. */
4715 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4717 unsigned long **valuep
= data
;
4723 if (h
->root
.type
== bfd_link_hash_warning
)
4724 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4726 /* Ignore indirect symbols. These are added by the versioning code. */
4727 if (h
->dynindx
== -1)
4730 name
= h
->root
.root
.string
;
4731 p
= strchr (name
, ELF_VER_CHR
);
4734 alc
= bfd_malloc (p
- name
+ 1);
4735 memcpy (alc
, name
, p
- name
);
4736 alc
[p
- name
] = '\0';
4740 /* Compute the hash value. */
4741 ha
= bfd_elf_hash (name
);
4743 /* Store the found hash value in the array given as the argument. */
4746 /* And store it in the struct so that we can put it in the hash table
4748 h
->u
.elf_hash_value
= ha
;
4756 /* Array used to determine the number of hash table buckets to use
4757 based on the number of symbols there are. If there are fewer than
4758 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4759 fewer than 37 we use 17 buckets, and so forth. We never use more
4760 than 32771 buckets. */
4762 static const size_t elf_buckets
[] =
4764 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4768 /* Compute bucket count for hashing table. We do not use a static set
4769 of possible tables sizes anymore. Instead we determine for all
4770 possible reasonable sizes of the table the outcome (i.e., the
4771 number of collisions etc) and choose the best solution. The
4772 weighting functions are not too simple to allow the table to grow
4773 without bounds. Instead one of the weighting factors is the size.
4774 Therefore the result is always a good payoff between few collisions
4775 (= short chain lengths) and table size. */
4777 compute_bucket_count (struct bfd_link_info
*info
)
4779 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4780 size_t best_size
= 0;
4781 unsigned long int *hashcodes
;
4782 unsigned long int *hashcodesp
;
4783 unsigned long int i
;
4786 /* Compute the hash values for all exported symbols. At the same
4787 time store the values in an array so that we could use them for
4790 amt
*= sizeof (unsigned long int);
4791 hashcodes
= bfd_malloc (amt
);
4792 if (hashcodes
== NULL
)
4794 hashcodesp
= hashcodes
;
4796 /* Put all hash values in HASHCODES. */
4797 elf_link_hash_traverse (elf_hash_table (info
),
4798 elf_collect_hash_codes
, &hashcodesp
);
4800 /* We have a problem here. The following code to optimize the table
4801 size requires an integer type with more the 32 bits. If
4802 BFD_HOST_U_64_BIT is set we know about such a type. */
4803 #ifdef BFD_HOST_U_64_BIT
4806 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4809 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4810 unsigned long int *counts
;
4811 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4812 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4814 /* Possible optimization parameters: if we have NSYMS symbols we say
4815 that the hashing table must at least have NSYMS/4 and at most
4817 minsize
= nsyms
/ 4;
4820 best_size
= maxsize
= nsyms
* 2;
4822 /* Create array where we count the collisions in. We must use bfd_malloc
4823 since the size could be large. */
4825 amt
*= sizeof (unsigned long int);
4826 counts
= bfd_malloc (amt
);
4833 /* Compute the "optimal" size for the hash table. The criteria is a
4834 minimal chain length. The minor criteria is (of course) the size
4836 for (i
= minsize
; i
< maxsize
; ++i
)
4838 /* Walk through the array of hashcodes and count the collisions. */
4839 BFD_HOST_U_64_BIT max
;
4840 unsigned long int j
;
4841 unsigned long int fact
;
4843 memset (counts
, '\0', i
* sizeof (unsigned long int));
4845 /* Determine how often each hash bucket is used. */
4846 for (j
= 0; j
< nsyms
; ++j
)
4847 ++counts
[hashcodes
[j
] % i
];
4849 /* For the weight function we need some information about the
4850 pagesize on the target. This is information need not be 100%
4851 accurate. Since this information is not available (so far) we
4852 define it here to a reasonable default value. If it is crucial
4853 to have a better value some day simply define this value. */
4854 # ifndef BFD_TARGET_PAGESIZE
4855 # define BFD_TARGET_PAGESIZE (4096)
4858 /* We in any case need 2 + NSYMS entries for the size values and
4860 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4863 /* Variant 1: optimize for short chains. We add the squares
4864 of all the chain lengths (which favors many small chain
4865 over a few long chains). */
4866 for (j
= 0; j
< i
; ++j
)
4867 max
+= counts
[j
] * counts
[j
];
4869 /* This adds penalties for the overall size of the table. */
4870 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4873 /* Variant 2: Optimize a lot more for small table. Here we
4874 also add squares of the size but we also add penalties for
4875 empty slots (the +1 term). */
4876 for (j
= 0; j
< i
; ++j
)
4877 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4879 /* The overall size of the table is considered, but not as
4880 strong as in variant 1, where it is squared. */
4881 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4885 /* Compare with current best results. */
4886 if (max
< best_chlen
)
4896 #endif /* defined (BFD_HOST_U_64_BIT) */
4898 /* This is the fallback solution if no 64bit type is available or if we
4899 are not supposed to spend much time on optimizations. We select the
4900 bucket count using a fixed set of numbers. */
4901 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4903 best_size
= elf_buckets
[i
];
4904 if (dynsymcount
< elf_buckets
[i
+ 1])
4909 /* Free the arrays we needed. */
4915 /* Set up the sizes and contents of the ELF dynamic sections. This is
4916 called by the ELF linker emulation before_allocation routine. We
4917 must set the sizes of the sections before the linker sets the
4918 addresses of the various sections. */
4921 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4924 const char *filter_shlib
,
4925 const char * const *auxiliary_filters
,
4926 struct bfd_link_info
*info
,
4927 asection
**sinterpptr
,
4928 struct bfd_elf_version_tree
*verdefs
)
4930 bfd_size_type soname_indx
;
4932 const struct elf_backend_data
*bed
;
4933 struct elf_assign_sym_version_info asvinfo
;
4937 soname_indx
= (bfd_size_type
) -1;
4939 if (!is_elf_hash_table (info
->hash
))
4942 elf_tdata (output_bfd
)->relro
= info
->relro
;
4943 if (info
->execstack
)
4944 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4945 else if (info
->noexecstack
)
4946 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4950 asection
*notesec
= NULL
;
4953 for (inputobj
= info
->input_bfds
;
4955 inputobj
= inputobj
->link_next
)
4959 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4961 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4964 if (s
->flags
& SEC_CODE
)
4973 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4974 if (exec
&& info
->relocatable
4975 && notesec
->output_section
!= bfd_abs_section_ptr
)
4976 notesec
->output_section
->flags
|= SEC_CODE
;
4980 /* Any syms created from now on start with -1 in
4981 got.refcount/offset and plt.refcount/offset. */
4982 elf_hash_table (info
)->init_got_refcount
4983 = elf_hash_table (info
)->init_got_offset
;
4984 elf_hash_table (info
)->init_plt_refcount
4985 = elf_hash_table (info
)->init_plt_offset
;
4987 /* The backend may have to create some sections regardless of whether
4988 we're dynamic or not. */
4989 bed
= get_elf_backend_data (output_bfd
);
4990 if (bed
->elf_backend_always_size_sections
4991 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4994 dynobj
= elf_hash_table (info
)->dynobj
;
4996 /* If there were no dynamic objects in the link, there is nothing to
5001 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5004 if (elf_hash_table (info
)->dynamic_sections_created
)
5006 struct elf_info_failed eif
;
5007 struct elf_link_hash_entry
*h
;
5009 struct bfd_elf_version_tree
*t
;
5010 struct bfd_elf_version_expr
*d
;
5012 bfd_boolean all_defined
;
5014 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5015 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5019 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5021 if (soname_indx
== (bfd_size_type
) -1
5022 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5028 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5030 info
->flags
|= DF_SYMBOLIC
;
5037 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5039 if (indx
== (bfd_size_type
) -1
5040 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5043 if (info
->new_dtags
)
5045 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5046 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5051 if (filter_shlib
!= NULL
)
5055 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5056 filter_shlib
, TRUE
);
5057 if (indx
== (bfd_size_type
) -1
5058 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5062 if (auxiliary_filters
!= NULL
)
5064 const char * const *p
;
5066 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5070 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5072 if (indx
== (bfd_size_type
) -1
5073 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5079 eif
.verdefs
= verdefs
;
5082 /* If we are supposed to export all symbols into the dynamic symbol
5083 table (this is not the normal case), then do so. */
5084 if (info
->export_dynamic
)
5086 elf_link_hash_traverse (elf_hash_table (info
),
5087 _bfd_elf_export_symbol
,
5093 /* Make all global versions with definition. */
5094 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5095 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5096 if (!d
->symver
&& d
->symbol
)
5098 const char *verstr
, *name
;
5099 size_t namelen
, verlen
, newlen
;
5101 struct elf_link_hash_entry
*newh
;
5104 namelen
= strlen (name
);
5106 verlen
= strlen (verstr
);
5107 newlen
= namelen
+ verlen
+ 3;
5109 newname
= bfd_malloc (newlen
);
5110 if (newname
== NULL
)
5112 memcpy (newname
, name
, namelen
);
5114 /* Check the hidden versioned definition. */
5115 p
= newname
+ namelen
;
5117 memcpy (p
, verstr
, verlen
+ 1);
5118 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5119 newname
, FALSE
, FALSE
,
5122 || (newh
->root
.type
!= bfd_link_hash_defined
5123 && newh
->root
.type
!= bfd_link_hash_defweak
))
5125 /* Check the default versioned definition. */
5127 memcpy (p
, verstr
, verlen
+ 1);
5128 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5129 newname
, FALSE
, FALSE
,
5134 /* Mark this version if there is a definition and it is
5135 not defined in a shared object. */
5137 && !newh
->def_dynamic
5138 && (newh
->root
.type
== bfd_link_hash_defined
5139 || newh
->root
.type
== bfd_link_hash_defweak
))
5143 /* Attach all the symbols to their version information. */
5144 asvinfo
.output_bfd
= output_bfd
;
5145 asvinfo
.info
= info
;
5146 asvinfo
.verdefs
= verdefs
;
5147 asvinfo
.failed
= FALSE
;
5149 elf_link_hash_traverse (elf_hash_table (info
),
5150 _bfd_elf_link_assign_sym_version
,
5155 if (!info
->allow_undefined_version
)
5157 /* Check if all global versions have a definition. */
5159 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5160 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5161 if (!d
->symver
&& !d
->script
)
5163 (*_bfd_error_handler
)
5164 (_("%s: undefined version: %s"),
5165 d
->pattern
, t
->name
);
5166 all_defined
= FALSE
;
5171 bfd_set_error (bfd_error_bad_value
);
5176 /* Find all symbols which were defined in a dynamic object and make
5177 the backend pick a reasonable value for them. */
5178 elf_link_hash_traverse (elf_hash_table (info
),
5179 _bfd_elf_adjust_dynamic_symbol
,
5184 /* Add some entries to the .dynamic section. We fill in some of the
5185 values later, in bfd_elf_final_link, but we must add the entries
5186 now so that we know the final size of the .dynamic section. */
5188 /* If there are initialization and/or finalization functions to
5189 call then add the corresponding DT_INIT/DT_FINI entries. */
5190 h
= (info
->init_function
5191 ? elf_link_hash_lookup (elf_hash_table (info
),
5192 info
->init_function
, FALSE
,
5199 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5202 h
= (info
->fini_function
5203 ? elf_link_hash_lookup (elf_hash_table (info
),
5204 info
->fini_function
, FALSE
,
5211 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5215 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5216 if (s
!= NULL
&& s
->linker_has_input
)
5218 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5219 if (! info
->executable
)
5224 for (sub
= info
->input_bfds
; sub
!= NULL
;
5225 sub
= sub
->link_next
)
5226 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5227 if (elf_section_data (o
)->this_hdr
.sh_type
5228 == SHT_PREINIT_ARRAY
)
5230 (*_bfd_error_handler
)
5231 (_("%B: .preinit_array section is not allowed in DSO"),
5236 bfd_set_error (bfd_error_nonrepresentable_section
);
5240 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5241 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5244 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5245 if (s
!= NULL
&& s
->linker_has_input
)
5247 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5248 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5251 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5252 if (s
!= NULL
&& s
->linker_has_input
)
5254 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5255 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5259 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5260 /* If .dynstr is excluded from the link, we don't want any of
5261 these tags. Strictly, we should be checking each section
5262 individually; This quick check covers for the case where
5263 someone does a /DISCARD/ : { *(*) }. */
5264 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5266 bfd_size_type strsize
;
5268 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5269 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5270 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5271 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5272 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5273 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5274 bed
->s
->sizeof_sym
))
5279 /* The backend must work out the sizes of all the other dynamic
5281 if (bed
->elf_backend_size_dynamic_sections
5282 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5285 if (elf_hash_table (info
)->dynamic_sections_created
)
5287 unsigned long section_sym_count
;
5290 /* Set up the version definition section. */
5291 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5292 BFD_ASSERT (s
!= NULL
);
5294 /* We may have created additional version definitions if we are
5295 just linking a regular application. */
5296 verdefs
= asvinfo
.verdefs
;
5298 /* Skip anonymous version tag. */
5299 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5300 verdefs
= verdefs
->next
;
5302 if (verdefs
== NULL
&& !info
->create_default_symver
)
5303 s
->flags
|= SEC_EXCLUDE
;
5308 struct bfd_elf_version_tree
*t
;
5310 Elf_Internal_Verdef def
;
5311 Elf_Internal_Verdaux defaux
;
5312 struct bfd_link_hash_entry
*bh
;
5313 struct elf_link_hash_entry
*h
;
5319 /* Make space for the base version. */
5320 size
+= sizeof (Elf_External_Verdef
);
5321 size
+= sizeof (Elf_External_Verdaux
);
5324 /* Make space for the default version. */
5325 if (info
->create_default_symver
)
5327 size
+= sizeof (Elf_External_Verdef
);
5331 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5333 struct bfd_elf_version_deps
*n
;
5335 size
+= sizeof (Elf_External_Verdef
);
5336 size
+= sizeof (Elf_External_Verdaux
);
5339 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5340 size
+= sizeof (Elf_External_Verdaux
);
5344 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5345 if (s
->contents
== NULL
&& s
->size
!= 0)
5348 /* Fill in the version definition section. */
5352 def
.vd_version
= VER_DEF_CURRENT
;
5353 def
.vd_flags
= VER_FLG_BASE
;
5356 if (info
->create_default_symver
)
5358 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5359 def
.vd_next
= sizeof (Elf_External_Verdef
);
5363 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5364 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5365 + sizeof (Elf_External_Verdaux
));
5368 if (soname_indx
!= (bfd_size_type
) -1)
5370 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5372 def
.vd_hash
= bfd_elf_hash (soname
);
5373 defaux
.vda_name
= soname_indx
;
5380 name
= lbasename (output_bfd
->filename
);
5381 def
.vd_hash
= bfd_elf_hash (name
);
5382 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5384 if (indx
== (bfd_size_type
) -1)
5386 defaux
.vda_name
= indx
;
5388 defaux
.vda_next
= 0;
5390 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5391 (Elf_External_Verdef
*) p
);
5392 p
+= sizeof (Elf_External_Verdef
);
5393 if (info
->create_default_symver
)
5395 /* Add a symbol representing this version. */
5397 if (! (_bfd_generic_link_add_one_symbol
5398 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5400 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5402 h
= (struct elf_link_hash_entry
*) bh
;
5405 h
->type
= STT_OBJECT
;
5406 h
->verinfo
.vertree
= NULL
;
5408 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5411 /* Create a duplicate of the base version with the same
5412 aux block, but different flags. */
5415 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5417 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5418 + sizeof (Elf_External_Verdaux
));
5421 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5422 (Elf_External_Verdef
*) p
);
5423 p
+= sizeof (Elf_External_Verdef
);
5425 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5426 (Elf_External_Verdaux
*) p
);
5427 p
+= sizeof (Elf_External_Verdaux
);
5429 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5432 struct bfd_elf_version_deps
*n
;
5435 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5438 /* Add a symbol representing this version. */
5440 if (! (_bfd_generic_link_add_one_symbol
5441 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5443 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5445 h
= (struct elf_link_hash_entry
*) bh
;
5448 h
->type
= STT_OBJECT
;
5449 h
->verinfo
.vertree
= t
;
5451 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5454 def
.vd_version
= VER_DEF_CURRENT
;
5456 if (t
->globals
.list
== NULL
5457 && t
->locals
.list
== NULL
5459 def
.vd_flags
|= VER_FLG_WEAK
;
5460 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5461 def
.vd_cnt
= cdeps
+ 1;
5462 def
.vd_hash
= bfd_elf_hash (t
->name
);
5463 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5465 if (t
->next
!= NULL
)
5466 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5467 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5469 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5470 (Elf_External_Verdef
*) p
);
5471 p
+= sizeof (Elf_External_Verdef
);
5473 defaux
.vda_name
= h
->dynstr_index
;
5474 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5476 defaux
.vda_next
= 0;
5477 if (t
->deps
!= NULL
)
5478 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5479 t
->name_indx
= defaux
.vda_name
;
5481 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5482 (Elf_External_Verdaux
*) p
);
5483 p
+= sizeof (Elf_External_Verdaux
);
5485 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5487 if (n
->version_needed
== NULL
)
5489 /* This can happen if there was an error in the
5491 defaux
.vda_name
= 0;
5495 defaux
.vda_name
= n
->version_needed
->name_indx
;
5496 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5499 if (n
->next
== NULL
)
5500 defaux
.vda_next
= 0;
5502 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5504 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5505 (Elf_External_Verdaux
*) p
);
5506 p
+= sizeof (Elf_External_Verdaux
);
5510 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5511 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5514 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5517 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5519 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5522 else if (info
->flags
& DF_BIND_NOW
)
5524 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5530 if (info
->executable
)
5531 info
->flags_1
&= ~ (DF_1_INITFIRST
5534 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5538 /* Work out the size of the version reference section. */
5540 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5541 BFD_ASSERT (s
!= NULL
);
5543 struct elf_find_verdep_info sinfo
;
5545 sinfo
.output_bfd
= output_bfd
;
5547 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5548 if (sinfo
.vers
== 0)
5550 sinfo
.failed
= FALSE
;
5552 elf_link_hash_traverse (elf_hash_table (info
),
5553 _bfd_elf_link_find_version_dependencies
,
5556 if (elf_tdata (output_bfd
)->verref
== NULL
)
5557 s
->flags
|= SEC_EXCLUDE
;
5560 Elf_Internal_Verneed
*t
;
5565 /* Build the version definition section. */
5568 for (t
= elf_tdata (output_bfd
)->verref
;
5572 Elf_Internal_Vernaux
*a
;
5574 size
+= sizeof (Elf_External_Verneed
);
5576 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5577 size
+= sizeof (Elf_External_Vernaux
);
5581 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5582 if (s
->contents
== NULL
)
5586 for (t
= elf_tdata (output_bfd
)->verref
;
5591 Elf_Internal_Vernaux
*a
;
5595 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5598 t
->vn_version
= VER_NEED_CURRENT
;
5600 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5601 elf_dt_name (t
->vn_bfd
) != NULL
5602 ? elf_dt_name (t
->vn_bfd
)
5603 : lbasename (t
->vn_bfd
->filename
),
5605 if (indx
== (bfd_size_type
) -1)
5608 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5609 if (t
->vn_nextref
== NULL
)
5612 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5613 + caux
* sizeof (Elf_External_Vernaux
));
5615 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5616 (Elf_External_Verneed
*) p
);
5617 p
+= sizeof (Elf_External_Verneed
);
5619 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5621 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5622 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5623 a
->vna_nodename
, FALSE
);
5624 if (indx
== (bfd_size_type
) -1)
5627 if (a
->vna_nextptr
== NULL
)
5630 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5632 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5633 (Elf_External_Vernaux
*) p
);
5634 p
+= sizeof (Elf_External_Vernaux
);
5638 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5639 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5642 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5646 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5647 && elf_tdata (output_bfd
)->cverdefs
== 0)
5648 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5649 §ion_sym_count
) == 0)
5651 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5652 s
->flags
|= SEC_EXCLUDE
;
5659 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5661 if (!is_elf_hash_table (info
->hash
))
5664 if (elf_hash_table (info
)->dynamic_sections_created
)
5667 const struct elf_backend_data
*bed
;
5669 bfd_size_type dynsymcount
;
5670 unsigned long section_sym_count
;
5671 size_t bucketcount
= 0;
5672 size_t hash_entry_size
;
5673 unsigned int dtagcount
;
5675 dynobj
= elf_hash_table (info
)->dynobj
;
5677 /* Assign dynsym indicies. In a shared library we generate a
5678 section symbol for each output section, which come first.
5679 Next come all of the back-end allocated local dynamic syms,
5680 followed by the rest of the global symbols. */
5682 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5683 §ion_sym_count
);
5685 /* Work out the size of the symbol version section. */
5686 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5687 BFD_ASSERT (s
!= NULL
);
5688 if (dynsymcount
!= 0
5689 && (s
->flags
& SEC_EXCLUDE
) == 0)
5691 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5692 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5693 if (s
->contents
== NULL
)
5696 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5700 /* Set the size of the .dynsym and .hash sections. We counted
5701 the number of dynamic symbols in elf_link_add_object_symbols.
5702 We will build the contents of .dynsym and .hash when we build
5703 the final symbol table, because until then we do not know the
5704 correct value to give the symbols. We built the .dynstr
5705 section as we went along in elf_link_add_object_symbols. */
5706 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5707 BFD_ASSERT (s
!= NULL
);
5708 bed
= get_elf_backend_data (output_bfd
);
5709 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5711 if (dynsymcount
!= 0)
5713 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5714 if (s
->contents
== NULL
)
5717 /* The first entry in .dynsym is a dummy symbol.
5718 Clear all the section syms, in case we don't output them all. */
5719 ++section_sym_count
;
5720 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5723 /* Compute the size of the hashing table. As a side effect this
5724 computes the hash values for all the names we export. */
5725 bucketcount
= compute_bucket_count (info
);
5727 s
= bfd_get_section_by_name (dynobj
, ".hash");
5728 BFD_ASSERT (s
!= NULL
);
5729 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5730 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5731 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5732 if (s
->contents
== NULL
)
5735 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5736 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5737 s
->contents
+ hash_entry_size
);
5739 elf_hash_table (info
)->bucketcount
= bucketcount
;
5741 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5742 BFD_ASSERT (s
!= NULL
);
5744 elf_finalize_dynstr (output_bfd
, info
);
5746 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5748 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5749 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5756 /* Final phase of ELF linker. */
5758 /* A structure we use to avoid passing large numbers of arguments. */
5760 struct elf_final_link_info
5762 /* General link information. */
5763 struct bfd_link_info
*info
;
5766 /* Symbol string table. */
5767 struct bfd_strtab_hash
*symstrtab
;
5768 /* .dynsym section. */
5769 asection
*dynsym_sec
;
5770 /* .hash section. */
5772 /* symbol version section (.gnu.version). */
5773 asection
*symver_sec
;
5774 /* Buffer large enough to hold contents of any section. */
5776 /* Buffer large enough to hold external relocs of any section. */
5777 void *external_relocs
;
5778 /* Buffer large enough to hold internal relocs of any section. */
5779 Elf_Internal_Rela
*internal_relocs
;
5780 /* Buffer large enough to hold external local symbols of any input
5782 bfd_byte
*external_syms
;
5783 /* And a buffer for symbol section indices. */
5784 Elf_External_Sym_Shndx
*locsym_shndx
;
5785 /* Buffer large enough to hold internal local symbols of any input
5787 Elf_Internal_Sym
*internal_syms
;
5788 /* Array large enough to hold a symbol index for each local symbol
5789 of any input BFD. */
5791 /* Array large enough to hold a section pointer for each local
5792 symbol of any input BFD. */
5793 asection
**sections
;
5794 /* Buffer to hold swapped out symbols. */
5796 /* And one for symbol section indices. */
5797 Elf_External_Sym_Shndx
*symshndxbuf
;
5798 /* Number of swapped out symbols in buffer. */
5799 size_t symbuf_count
;
5800 /* Number of symbols which fit in symbuf. */
5802 /* And same for symshndxbuf. */
5803 size_t shndxbuf_size
;
5806 /* This struct is used to pass information to elf_link_output_extsym. */
5808 struct elf_outext_info
5811 bfd_boolean localsyms
;
5812 struct elf_final_link_info
*finfo
;
5815 /* When performing a relocatable link, the input relocations are
5816 preserved. But, if they reference global symbols, the indices
5817 referenced must be updated. Update all the relocations in
5818 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5821 elf_link_adjust_relocs (bfd
*abfd
,
5822 Elf_Internal_Shdr
*rel_hdr
,
5824 struct elf_link_hash_entry
**rel_hash
)
5827 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5829 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5830 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5831 bfd_vma r_type_mask
;
5834 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5836 swap_in
= bed
->s
->swap_reloc_in
;
5837 swap_out
= bed
->s
->swap_reloc_out
;
5839 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5841 swap_in
= bed
->s
->swap_reloca_in
;
5842 swap_out
= bed
->s
->swap_reloca_out
;
5847 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5850 if (bed
->s
->arch_size
== 32)
5857 r_type_mask
= 0xffffffff;
5861 erela
= rel_hdr
->contents
;
5862 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5864 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5867 if (*rel_hash
== NULL
)
5870 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5872 (*swap_in
) (abfd
, erela
, irela
);
5873 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5874 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5875 | (irela
[j
].r_info
& r_type_mask
));
5876 (*swap_out
) (abfd
, irela
, erela
);
5880 struct elf_link_sort_rela
5886 enum elf_reloc_type_class type
;
5887 /* We use this as an array of size int_rels_per_ext_rel. */
5888 Elf_Internal_Rela rela
[1];
5892 elf_link_sort_cmp1 (const void *A
, const void *B
)
5894 const struct elf_link_sort_rela
*a
= A
;
5895 const struct elf_link_sort_rela
*b
= B
;
5896 int relativea
, relativeb
;
5898 relativea
= a
->type
== reloc_class_relative
;
5899 relativeb
= b
->type
== reloc_class_relative
;
5901 if (relativea
< relativeb
)
5903 if (relativea
> relativeb
)
5905 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5907 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5909 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5911 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5917 elf_link_sort_cmp2 (const void *A
, const void *B
)
5919 const struct elf_link_sort_rela
*a
= A
;
5920 const struct elf_link_sort_rela
*b
= B
;
5923 if (a
->u
.offset
< b
->u
.offset
)
5925 if (a
->u
.offset
> b
->u
.offset
)
5927 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5928 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5933 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5935 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5941 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5944 bfd_size_type count
, size
;
5945 size_t i
, ret
, sort_elt
, ext_size
;
5946 bfd_byte
*sort
, *s_non_relative
, *p
;
5947 struct elf_link_sort_rela
*sq
;
5948 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5949 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5950 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5951 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5952 struct bfd_link_order
*lo
;
5955 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5956 if (reldyn
== NULL
|| reldyn
->size
== 0)
5958 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5959 if (reldyn
== NULL
|| reldyn
->size
== 0)
5961 ext_size
= bed
->s
->sizeof_rel
;
5962 swap_in
= bed
->s
->swap_reloc_in
;
5963 swap_out
= bed
->s
->swap_reloc_out
;
5967 ext_size
= bed
->s
->sizeof_rela
;
5968 swap_in
= bed
->s
->swap_reloca_in
;
5969 swap_out
= bed
->s
->swap_reloca_out
;
5971 count
= reldyn
->size
/ ext_size
;
5974 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5975 if (lo
->type
== bfd_indirect_link_order
)
5977 asection
*o
= lo
->u
.indirect
.section
;
5981 if (size
!= reldyn
->size
)
5984 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5985 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5986 sort
= bfd_zmalloc (sort_elt
* count
);
5989 (*info
->callbacks
->warning
)
5990 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5994 if (bed
->s
->arch_size
== 32)
5995 r_sym_mask
= ~(bfd_vma
) 0xff;
5997 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5999 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6000 if (lo
->type
== bfd_indirect_link_order
)
6002 bfd_byte
*erel
, *erelend
;
6003 asection
*o
= lo
->u
.indirect
.section
;
6005 if (o
->contents
== NULL
&& o
->size
!= 0)
6007 /* This is a reloc section that is being handled as a normal
6008 section. See bfd_section_from_shdr. We can't combine
6009 relocs in this case. */
6014 erelend
= o
->contents
+ o
->size
;
6015 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6016 while (erel
< erelend
)
6018 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6019 (*swap_in
) (abfd
, erel
, s
->rela
);
6020 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6021 s
->u
.sym_mask
= r_sym_mask
;
6027 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6029 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6031 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6032 if (s
->type
!= reloc_class_relative
)
6038 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6039 for (; i
< count
; i
++, p
+= sort_elt
)
6041 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6042 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6044 sp
->u
.offset
= sq
->rela
->r_offset
;
6047 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6049 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6050 if (lo
->type
== bfd_indirect_link_order
)
6052 bfd_byte
*erel
, *erelend
;
6053 asection
*o
= lo
->u
.indirect
.section
;
6056 erelend
= o
->contents
+ o
->size
;
6057 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6058 while (erel
< erelend
)
6060 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6061 (*swap_out
) (abfd
, s
->rela
, erel
);
6072 /* Flush the output symbols to the file. */
6075 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6076 const struct elf_backend_data
*bed
)
6078 if (finfo
->symbuf_count
> 0)
6080 Elf_Internal_Shdr
*hdr
;
6084 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6085 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6086 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6087 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6088 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6091 hdr
->sh_size
+= amt
;
6092 finfo
->symbuf_count
= 0;
6098 /* Add a symbol to the output symbol table. */
6101 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6103 Elf_Internal_Sym
*elfsym
,
6104 asection
*input_sec
,
6105 struct elf_link_hash_entry
*h
)
6108 Elf_External_Sym_Shndx
*destshndx
;
6109 bfd_boolean (*output_symbol_hook
)
6110 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6111 struct elf_link_hash_entry
*);
6112 const struct elf_backend_data
*bed
;
6114 bed
= get_elf_backend_data (finfo
->output_bfd
);
6115 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6116 if (output_symbol_hook
!= NULL
)
6118 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6122 if (name
== NULL
|| *name
== '\0')
6123 elfsym
->st_name
= 0;
6124 else if (input_sec
->flags
& SEC_EXCLUDE
)
6125 elfsym
->st_name
= 0;
6128 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6130 if (elfsym
->st_name
== (unsigned long) -1)
6134 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6136 if (! elf_link_flush_output_syms (finfo
, bed
))
6140 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6141 destshndx
= finfo
->symshndxbuf
;
6142 if (destshndx
!= NULL
)
6144 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6148 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6149 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6150 if (destshndx
== NULL
)
6152 memset ((char *) destshndx
+ amt
, 0, amt
);
6153 finfo
->shndxbuf_size
*= 2;
6155 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6158 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6159 finfo
->symbuf_count
+= 1;
6160 bfd_get_symcount (finfo
->output_bfd
) += 1;
6165 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6166 allowing an unsatisfied unversioned symbol in the DSO to match a
6167 versioned symbol that would normally require an explicit version.
6168 We also handle the case that a DSO references a hidden symbol
6169 which may be satisfied by a versioned symbol in another DSO. */
6172 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6173 const struct elf_backend_data
*bed
,
6174 struct elf_link_hash_entry
*h
)
6177 struct elf_link_loaded_list
*loaded
;
6179 if (!is_elf_hash_table (info
->hash
))
6182 switch (h
->root
.type
)
6188 case bfd_link_hash_undefined
:
6189 case bfd_link_hash_undefweak
:
6190 abfd
= h
->root
.u
.undef
.abfd
;
6191 if ((abfd
->flags
& DYNAMIC
) == 0
6192 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6196 case bfd_link_hash_defined
:
6197 case bfd_link_hash_defweak
:
6198 abfd
= h
->root
.u
.def
.section
->owner
;
6201 case bfd_link_hash_common
:
6202 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6205 BFD_ASSERT (abfd
!= NULL
);
6207 for (loaded
= elf_hash_table (info
)->loaded
;
6209 loaded
= loaded
->next
)
6212 Elf_Internal_Shdr
*hdr
;
6213 bfd_size_type symcount
;
6214 bfd_size_type extsymcount
;
6215 bfd_size_type extsymoff
;
6216 Elf_Internal_Shdr
*versymhdr
;
6217 Elf_Internal_Sym
*isym
;
6218 Elf_Internal_Sym
*isymend
;
6219 Elf_Internal_Sym
*isymbuf
;
6220 Elf_External_Versym
*ever
;
6221 Elf_External_Versym
*extversym
;
6223 input
= loaded
->abfd
;
6225 /* We check each DSO for a possible hidden versioned definition. */
6227 || (input
->flags
& DYNAMIC
) == 0
6228 || elf_dynversym (input
) == 0)
6231 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6233 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6234 if (elf_bad_symtab (input
))
6236 extsymcount
= symcount
;
6241 extsymcount
= symcount
- hdr
->sh_info
;
6242 extsymoff
= hdr
->sh_info
;
6245 if (extsymcount
== 0)
6248 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6250 if (isymbuf
== NULL
)
6253 /* Read in any version definitions. */
6254 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6255 extversym
= bfd_malloc (versymhdr
->sh_size
);
6256 if (extversym
== NULL
)
6259 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6260 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6261 != versymhdr
->sh_size
))
6269 ever
= extversym
+ extsymoff
;
6270 isymend
= isymbuf
+ extsymcount
;
6271 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6274 Elf_Internal_Versym iver
;
6275 unsigned short version_index
;
6277 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6278 || isym
->st_shndx
== SHN_UNDEF
)
6281 name
= bfd_elf_string_from_elf_section (input
,
6284 if (strcmp (name
, h
->root
.root
.string
) != 0)
6287 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6289 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6291 /* If we have a non-hidden versioned sym, then it should
6292 have provided a definition for the undefined sym. */
6296 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6297 if (version_index
== 1 || version_index
== 2)
6299 /* This is the base or first version. We can use it. */
6313 /* Add an external symbol to the symbol table. This is called from
6314 the hash table traversal routine. When generating a shared object,
6315 we go through the symbol table twice. The first time we output
6316 anything that might have been forced to local scope in a version
6317 script. The second time we output the symbols that are still
6321 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6323 struct elf_outext_info
*eoinfo
= data
;
6324 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6326 Elf_Internal_Sym sym
;
6327 asection
*input_sec
;
6328 const struct elf_backend_data
*bed
;
6330 if (h
->root
.type
== bfd_link_hash_warning
)
6332 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6333 if (h
->root
.type
== bfd_link_hash_new
)
6337 /* Decide whether to output this symbol in this pass. */
6338 if (eoinfo
->localsyms
)
6340 if (!h
->forced_local
)
6345 if (h
->forced_local
)
6349 bed
= get_elf_backend_data (finfo
->output_bfd
);
6351 if (h
->root
.type
== bfd_link_hash_undefined
)
6353 /* If we have an undefined symbol reference here then it must have
6354 come from a shared library that is being linked in. (Undefined
6355 references in regular files have already been handled). */
6356 bfd_boolean ignore_undef
= FALSE
;
6358 /* Some symbols may be special in that the fact that they're
6359 undefined can be safely ignored - let backend determine that. */
6360 if (bed
->elf_backend_ignore_undef_symbol
)
6361 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6363 /* If we are reporting errors for this situation then do so now. */
6364 if (ignore_undef
== FALSE
6367 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6368 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6370 if (! (finfo
->info
->callbacks
->undefined_symbol
6371 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6372 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6374 eoinfo
->failed
= TRUE
;
6380 /* We should also warn if a forced local symbol is referenced from
6381 shared libraries. */
6382 if (! finfo
->info
->relocatable
6383 && (! finfo
->info
->shared
)
6388 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6390 (*_bfd_error_handler
)
6391 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6393 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6394 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6395 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6397 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6398 ? "hidden" : "local",
6399 h
->root
.root
.string
);
6400 eoinfo
->failed
= TRUE
;
6404 /* We don't want to output symbols that have never been mentioned by
6405 a regular file, or that we have been told to strip. However, if
6406 h->indx is set to -2, the symbol is used by a reloc and we must
6410 else if ((h
->def_dynamic
6412 || h
->root
.type
== bfd_link_hash_new
)
6416 else if (finfo
->info
->strip
== strip_all
)
6418 else if (finfo
->info
->strip
== strip_some
6419 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6420 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6422 else if (finfo
->info
->strip_discarded
6423 && (h
->root
.type
== bfd_link_hash_defined
6424 || h
->root
.type
== bfd_link_hash_defweak
)
6425 && elf_discarded_section (h
->root
.u
.def
.section
))
6430 /* If we're stripping it, and it's not a dynamic symbol, there's
6431 nothing else to do unless it is a forced local symbol. */
6434 && !h
->forced_local
)
6438 sym
.st_size
= h
->size
;
6439 sym
.st_other
= h
->other
;
6440 if (h
->forced_local
)
6441 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6442 else if (h
->root
.type
== bfd_link_hash_undefweak
6443 || h
->root
.type
== bfd_link_hash_defweak
)
6444 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6446 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6448 switch (h
->root
.type
)
6451 case bfd_link_hash_new
:
6452 case bfd_link_hash_warning
:
6456 case bfd_link_hash_undefined
:
6457 case bfd_link_hash_undefweak
:
6458 input_sec
= bfd_und_section_ptr
;
6459 sym
.st_shndx
= SHN_UNDEF
;
6462 case bfd_link_hash_defined
:
6463 case bfd_link_hash_defweak
:
6465 input_sec
= h
->root
.u
.def
.section
;
6466 if (input_sec
->output_section
!= NULL
)
6469 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6470 input_sec
->output_section
);
6471 if (sym
.st_shndx
== SHN_BAD
)
6473 (*_bfd_error_handler
)
6474 (_("%B: could not find output section %A for input section %A"),
6475 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6476 eoinfo
->failed
= TRUE
;
6480 /* ELF symbols in relocatable files are section relative,
6481 but in nonrelocatable files they are virtual
6483 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6484 if (! finfo
->info
->relocatable
)
6486 sym
.st_value
+= input_sec
->output_section
->vma
;
6487 if (h
->type
== STT_TLS
)
6489 /* STT_TLS symbols are relative to PT_TLS segment
6491 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6492 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6498 BFD_ASSERT (input_sec
->owner
== NULL
6499 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6500 sym
.st_shndx
= SHN_UNDEF
;
6501 input_sec
= bfd_und_section_ptr
;
6506 case bfd_link_hash_common
:
6507 input_sec
= h
->root
.u
.c
.p
->section
;
6508 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6509 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6512 case bfd_link_hash_indirect
:
6513 /* These symbols are created by symbol versioning. They point
6514 to the decorated version of the name. For example, if the
6515 symbol foo@@GNU_1.2 is the default, which should be used when
6516 foo is used with no version, then we add an indirect symbol
6517 foo which points to foo@@GNU_1.2. We ignore these symbols,
6518 since the indirected symbol is already in the hash table. */
6522 /* Give the processor backend a chance to tweak the symbol value,
6523 and also to finish up anything that needs to be done for this
6524 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6525 forced local syms when non-shared is due to a historical quirk. */
6526 if ((h
->dynindx
!= -1
6528 && ((finfo
->info
->shared
6529 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6530 || h
->root
.type
!= bfd_link_hash_undefweak
))
6531 || !h
->forced_local
)
6532 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6534 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6535 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6537 eoinfo
->failed
= TRUE
;
6542 /* If we are marking the symbol as undefined, and there are no
6543 non-weak references to this symbol from a regular object, then
6544 mark the symbol as weak undefined; if there are non-weak
6545 references, mark the symbol as strong. We can't do this earlier,
6546 because it might not be marked as undefined until the
6547 finish_dynamic_symbol routine gets through with it. */
6548 if (sym
.st_shndx
== SHN_UNDEF
6550 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6551 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6555 if (h
->ref_regular_nonweak
)
6556 bindtype
= STB_GLOBAL
;
6558 bindtype
= STB_WEAK
;
6559 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6562 /* If a non-weak symbol with non-default visibility is not defined
6563 locally, it is a fatal error. */
6564 if (! finfo
->info
->relocatable
6565 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6566 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6567 && h
->root
.type
== bfd_link_hash_undefined
6570 (*_bfd_error_handler
)
6571 (_("%B: %s symbol `%s' isn't defined"),
6573 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6575 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6576 ? "internal" : "hidden",
6577 h
->root
.root
.string
);
6578 eoinfo
->failed
= TRUE
;
6582 /* If this symbol should be put in the .dynsym section, then put it
6583 there now. We already know the symbol index. We also fill in
6584 the entry in the .hash section. */
6585 if (h
->dynindx
!= -1
6586 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6590 size_t hash_entry_size
;
6591 bfd_byte
*bucketpos
;
6595 sym
.st_name
= h
->dynstr_index
;
6596 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6597 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6599 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6600 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6602 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6603 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6604 + (bucket
+ 2) * hash_entry_size
);
6605 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6606 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6607 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6608 ((bfd_byte
*) finfo
->hash_sec
->contents
6609 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6611 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6613 Elf_Internal_Versym iversym
;
6614 Elf_External_Versym
*eversym
;
6616 if (!h
->def_regular
)
6618 if (h
->verinfo
.verdef
== NULL
)
6619 iversym
.vs_vers
= 0;
6621 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6625 if (h
->verinfo
.vertree
== NULL
)
6626 iversym
.vs_vers
= 1;
6628 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6629 if (finfo
->info
->create_default_symver
)
6634 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6636 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6637 eversym
+= h
->dynindx
;
6638 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6642 /* If we're stripping it, then it was just a dynamic symbol, and
6643 there's nothing else to do. */
6644 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6647 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6649 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6651 eoinfo
->failed
= TRUE
;
6658 /* Return TRUE if special handling is done for relocs in SEC against
6659 symbols defined in discarded sections. */
6662 elf_section_ignore_discarded_relocs (asection
*sec
)
6664 const struct elf_backend_data
*bed
;
6666 switch (sec
->sec_info_type
)
6668 case ELF_INFO_TYPE_STABS
:
6669 case ELF_INFO_TYPE_EH_FRAME
:
6675 bed
= get_elf_backend_data (sec
->owner
);
6676 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6677 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6683 /* Return a mask saying how ld should treat relocations in SEC against
6684 symbols defined in discarded sections. If this function returns
6685 COMPLAIN set, ld will issue a warning message. If this function
6686 returns PRETEND set, and the discarded section was link-once and the
6687 same size as the kept link-once section, ld will pretend that the
6688 symbol was actually defined in the kept section. Otherwise ld will
6689 zero the reloc (at least that is the intent, but some cooperation by
6690 the target dependent code is needed, particularly for REL targets). */
6693 _bfd_elf_default_action_discarded (asection
*sec
)
6695 if (sec
->flags
& SEC_DEBUGGING
)
6698 if (strcmp (".eh_frame", sec
->name
) == 0)
6701 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6704 return COMPLAIN
| PRETEND
;
6707 /* Find a match between a section and a member of a section group. */
6710 match_group_member (asection
*sec
, asection
*group
)
6712 asection
*first
= elf_next_in_group (group
);
6713 asection
*s
= first
;
6717 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6727 /* Check if the kept section of a discarded section SEC can be used
6728 to replace it. Return the replacement if it is OK. Otherwise return
6732 _bfd_elf_check_kept_section (asection
*sec
)
6736 kept
= sec
->kept_section
;
6739 if (elf_sec_group (sec
) != NULL
)
6740 kept
= match_group_member (sec
, kept
);
6741 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6747 /* Link an input file into the linker output file. This function
6748 handles all the sections and relocations of the input file at once.
6749 This is so that we only have to read the local symbols once, and
6750 don't have to keep them in memory. */
6753 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6755 bfd_boolean (*relocate_section
)
6756 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6757 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6759 Elf_Internal_Shdr
*symtab_hdr
;
6762 Elf_Internal_Sym
*isymbuf
;
6763 Elf_Internal_Sym
*isym
;
6764 Elf_Internal_Sym
*isymend
;
6766 asection
**ppsection
;
6768 const struct elf_backend_data
*bed
;
6769 bfd_boolean emit_relocs
;
6770 struct elf_link_hash_entry
**sym_hashes
;
6772 output_bfd
= finfo
->output_bfd
;
6773 bed
= get_elf_backend_data (output_bfd
);
6774 relocate_section
= bed
->elf_backend_relocate_section
;
6776 /* If this is a dynamic object, we don't want to do anything here:
6777 we don't want the local symbols, and we don't want the section
6779 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6782 emit_relocs
= (finfo
->info
->relocatable
6783 || finfo
->info
->emitrelocations
);
6785 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6786 if (elf_bad_symtab (input_bfd
))
6788 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6793 locsymcount
= symtab_hdr
->sh_info
;
6794 extsymoff
= symtab_hdr
->sh_info
;
6797 /* Read the local symbols. */
6798 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6799 if (isymbuf
== NULL
&& locsymcount
!= 0)
6801 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6802 finfo
->internal_syms
,
6803 finfo
->external_syms
,
6804 finfo
->locsym_shndx
);
6805 if (isymbuf
== NULL
)
6809 /* Find local symbol sections and adjust values of symbols in
6810 SEC_MERGE sections. Write out those local symbols we know are
6811 going into the output file. */
6812 isymend
= isymbuf
+ locsymcount
;
6813 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6815 isym
++, pindex
++, ppsection
++)
6819 Elf_Internal_Sym osym
;
6823 if (elf_bad_symtab (input_bfd
))
6825 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6832 if (isym
->st_shndx
== SHN_UNDEF
)
6833 isec
= bfd_und_section_ptr
;
6834 else if (isym
->st_shndx
< SHN_LORESERVE
6835 || isym
->st_shndx
> SHN_HIRESERVE
)
6837 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6839 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6840 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6842 _bfd_merged_section_offset (output_bfd
, &isec
,
6843 elf_section_data (isec
)->sec_info
,
6846 else if (isym
->st_shndx
== SHN_ABS
)
6847 isec
= bfd_abs_section_ptr
;
6848 else if (isym
->st_shndx
== SHN_COMMON
)
6849 isec
= bfd_com_section_ptr
;
6852 /* Don't attempt to output symbols with st_shnx in the
6853 reserved range other than SHN_ABS and SHN_COMMON. */
6860 /* Don't output the first, undefined, symbol. */
6861 if (ppsection
== finfo
->sections
)
6864 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6866 /* We never output section symbols. Instead, we use the
6867 section symbol of the corresponding section in the output
6872 /* If we are stripping all symbols, we don't want to output this
6874 if (finfo
->info
->strip
== strip_all
)
6877 /* If we are discarding all local symbols, we don't want to
6878 output this one. If we are generating a relocatable output
6879 file, then some of the local symbols may be required by
6880 relocs; we output them below as we discover that they are
6882 if (finfo
->info
->discard
== discard_all
)
6885 /* If this symbol is defined in a section which we are
6886 discarding, we don't need to keep it. */
6887 if (isym
->st_shndx
!= SHN_UNDEF
6888 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6890 || bfd_section_removed_from_list (output_bfd
,
6891 isec
->output_section
)))
6894 /* Get the name of the symbol. */
6895 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6900 /* See if we are discarding symbols with this name. */
6901 if ((finfo
->info
->strip
== strip_some
6902 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6904 || (((finfo
->info
->discard
== discard_sec_merge
6905 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6906 || finfo
->info
->discard
== discard_l
)
6907 && bfd_is_local_label_name (input_bfd
, name
)))
6910 /* If we get here, we are going to output this symbol. */
6914 /* Adjust the section index for the output file. */
6915 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6916 isec
->output_section
);
6917 if (osym
.st_shndx
== SHN_BAD
)
6920 *pindex
= bfd_get_symcount (output_bfd
);
6922 /* ELF symbols in relocatable files are section relative, but
6923 in executable files they are virtual addresses. Note that
6924 this code assumes that all ELF sections have an associated
6925 BFD section with a reasonable value for output_offset; below
6926 we assume that they also have a reasonable value for
6927 output_section. Any special sections must be set up to meet
6928 these requirements. */
6929 osym
.st_value
+= isec
->output_offset
;
6930 if (! finfo
->info
->relocatable
)
6932 osym
.st_value
+= isec
->output_section
->vma
;
6933 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6935 /* STT_TLS symbols are relative to PT_TLS segment base. */
6936 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6937 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6941 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6945 /* Relocate the contents of each section. */
6946 sym_hashes
= elf_sym_hashes (input_bfd
);
6947 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6951 if (! o
->linker_mark
)
6953 /* This section was omitted from the link. */
6957 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6958 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6961 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6963 /* Section was created by _bfd_elf_link_create_dynamic_sections
6968 /* Get the contents of the section. They have been cached by a
6969 relaxation routine. Note that o is a section in an input
6970 file, so the contents field will not have been set by any of
6971 the routines which work on output files. */
6972 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6973 contents
= elf_section_data (o
)->this_hdr
.contents
;
6976 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6978 contents
= finfo
->contents
;
6979 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6983 if ((o
->flags
& SEC_RELOC
) != 0)
6985 Elf_Internal_Rela
*internal_relocs
;
6986 bfd_vma r_type_mask
;
6989 /* Get the swapped relocs. */
6991 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6992 finfo
->internal_relocs
, FALSE
);
6993 if (internal_relocs
== NULL
6994 && o
->reloc_count
> 0)
6997 if (bed
->s
->arch_size
== 32)
7004 r_type_mask
= 0xffffffff;
7008 /* Run through the relocs looking for any against symbols
7009 from discarded sections and section symbols from
7010 removed link-once sections. Complain about relocs
7011 against discarded sections. Zero relocs against removed
7012 link-once sections. */
7013 if (!elf_section_ignore_discarded_relocs (o
))
7015 Elf_Internal_Rela
*rel
, *relend
;
7016 unsigned int action
= (*bed
->action_discarded
) (o
);
7018 rel
= internal_relocs
;
7019 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7020 for ( ; rel
< relend
; rel
++)
7022 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7023 asection
**ps
, *sec
;
7024 struct elf_link_hash_entry
*h
= NULL
;
7025 const char *sym_name
;
7027 if (r_symndx
== STN_UNDEF
)
7030 if (r_symndx
>= locsymcount
7031 || (elf_bad_symtab (input_bfd
)
7032 && finfo
->sections
[r_symndx
] == NULL
))
7034 h
= sym_hashes
[r_symndx
- extsymoff
];
7036 /* Badly formatted input files can contain relocs that
7037 reference non-existant symbols. Check here so that
7038 we do not seg fault. */
7043 sprintf_vma (buffer
, rel
->r_info
);
7044 (*_bfd_error_handler
)
7045 (_("error: %B contains a reloc (0x%s) for section %A "
7046 "that references a non-existent global symbol"),
7047 input_bfd
, o
, buffer
);
7048 bfd_set_error (bfd_error_bad_value
);
7052 while (h
->root
.type
== bfd_link_hash_indirect
7053 || h
->root
.type
== bfd_link_hash_warning
)
7054 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7056 if (h
->root
.type
!= bfd_link_hash_defined
7057 && h
->root
.type
!= bfd_link_hash_defweak
)
7060 ps
= &h
->root
.u
.def
.section
;
7061 sym_name
= h
->root
.root
.string
;
7065 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7066 ps
= &finfo
->sections
[r_symndx
];
7067 sym_name
= bfd_elf_sym_name (input_bfd
,
7072 /* Complain if the definition comes from a
7073 discarded section. */
7074 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7076 BFD_ASSERT (r_symndx
!= 0);
7077 if (action
& COMPLAIN
)
7078 (*finfo
->info
->callbacks
->einfo
)
7079 (_("%X`%s' referenced in section `%A' of %B: "
7080 "defined in discarded section `%A' of %B\n"),
7081 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7083 /* Try to do the best we can to support buggy old
7084 versions of gcc. Pretend that the symbol is
7085 really defined in the kept linkonce section.
7086 FIXME: This is quite broken. Modifying the
7087 symbol here means we will be changing all later
7088 uses of the symbol, not just in this section. */
7089 if (action
& PRETEND
)
7093 kept
= _bfd_elf_check_kept_section (sec
);
7101 /* Remove the symbol reference from the reloc, but
7102 don't kill the reloc completely. This is so that
7103 a zero value will be written into the section,
7104 which may have non-zero contents put there by the
7105 assembler. Zero in things like an eh_frame fde
7106 pc_begin allows stack unwinders to recognize the
7108 rel
->r_info
&= r_type_mask
;
7114 /* Relocate the section by invoking a back end routine.
7116 The back end routine is responsible for adjusting the
7117 section contents as necessary, and (if using Rela relocs
7118 and generating a relocatable output file) adjusting the
7119 reloc addend as necessary.
7121 The back end routine does not have to worry about setting
7122 the reloc address or the reloc symbol index.
7124 The back end routine is given a pointer to the swapped in
7125 internal symbols, and can access the hash table entries
7126 for the external symbols via elf_sym_hashes (input_bfd).
7128 When generating relocatable output, the back end routine
7129 must handle STB_LOCAL/STT_SECTION symbols specially. The
7130 output symbol is going to be a section symbol
7131 corresponding to the output section, which will require
7132 the addend to be adjusted. */
7134 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7135 input_bfd
, o
, contents
,
7143 Elf_Internal_Rela
*irela
;
7144 Elf_Internal_Rela
*irelaend
;
7145 bfd_vma last_offset
;
7146 struct elf_link_hash_entry
**rel_hash
;
7147 struct elf_link_hash_entry
**rel_hash_list
;
7148 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7149 unsigned int next_erel
;
7150 bfd_boolean rela_normal
;
7152 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7153 rela_normal
= (bed
->rela_normal
7154 && (input_rel_hdr
->sh_entsize
7155 == bed
->s
->sizeof_rela
));
7157 /* Adjust the reloc addresses and symbol indices. */
7159 irela
= internal_relocs
;
7160 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7161 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7162 + elf_section_data (o
->output_section
)->rel_count
7163 + elf_section_data (o
->output_section
)->rel_count2
);
7164 rel_hash_list
= rel_hash
;
7165 last_offset
= o
->output_offset
;
7166 if (!finfo
->info
->relocatable
)
7167 last_offset
+= o
->output_section
->vma
;
7168 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7170 unsigned long r_symndx
;
7172 Elf_Internal_Sym sym
;
7174 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7180 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7183 if (irela
->r_offset
>= (bfd_vma
) -2)
7185 /* This is a reloc for a deleted entry or somesuch.
7186 Turn it into an R_*_NONE reloc, at the same
7187 offset as the last reloc. elf_eh_frame.c and
7188 elf_bfd_discard_info rely on reloc offsets
7190 irela
->r_offset
= last_offset
;
7192 irela
->r_addend
= 0;
7196 irela
->r_offset
+= o
->output_offset
;
7198 /* Relocs in an executable have to be virtual addresses. */
7199 if (!finfo
->info
->relocatable
)
7200 irela
->r_offset
+= o
->output_section
->vma
;
7202 last_offset
= irela
->r_offset
;
7204 r_symndx
= irela
->r_info
>> r_sym_shift
;
7205 if (r_symndx
== STN_UNDEF
)
7208 if (r_symndx
>= locsymcount
7209 || (elf_bad_symtab (input_bfd
)
7210 && finfo
->sections
[r_symndx
] == NULL
))
7212 struct elf_link_hash_entry
*rh
;
7215 /* This is a reloc against a global symbol. We
7216 have not yet output all the local symbols, so
7217 we do not know the symbol index of any global
7218 symbol. We set the rel_hash entry for this
7219 reloc to point to the global hash table entry
7220 for this symbol. The symbol index is then
7221 set at the end of bfd_elf_final_link. */
7222 indx
= r_symndx
- extsymoff
;
7223 rh
= elf_sym_hashes (input_bfd
)[indx
];
7224 while (rh
->root
.type
== bfd_link_hash_indirect
7225 || rh
->root
.type
== bfd_link_hash_warning
)
7226 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7228 /* Setting the index to -2 tells
7229 elf_link_output_extsym that this symbol is
7231 BFD_ASSERT (rh
->indx
< 0);
7239 /* This is a reloc against a local symbol. */
7242 sym
= isymbuf
[r_symndx
];
7243 sec
= finfo
->sections
[r_symndx
];
7244 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7246 /* I suppose the backend ought to fill in the
7247 section of any STT_SECTION symbol against a
7248 processor specific section. */
7250 if (bfd_is_abs_section (sec
))
7252 else if (sec
== NULL
|| sec
->owner
== NULL
)
7254 bfd_set_error (bfd_error_bad_value
);
7259 asection
*osec
= sec
->output_section
;
7261 /* If we have discarded a section, the output
7262 section will be the absolute section. In
7263 case of discarded link-once and discarded
7264 SEC_MERGE sections, use the kept section. */
7265 if (bfd_is_abs_section (osec
)
7266 && sec
->kept_section
!= NULL
7267 && sec
->kept_section
->output_section
!= NULL
)
7269 osec
= sec
->kept_section
->output_section
;
7270 irela
->r_addend
-= osec
->vma
;
7273 if (!bfd_is_abs_section (osec
))
7275 r_symndx
= osec
->target_index
;
7276 BFD_ASSERT (r_symndx
!= 0);
7280 /* Adjust the addend according to where the
7281 section winds up in the output section. */
7283 irela
->r_addend
+= sec
->output_offset
;
7287 if (finfo
->indices
[r_symndx
] == -1)
7289 unsigned long shlink
;
7293 if (finfo
->info
->strip
== strip_all
)
7295 /* You can't do ld -r -s. */
7296 bfd_set_error (bfd_error_invalid_operation
);
7300 /* This symbol was skipped earlier, but
7301 since it is needed by a reloc, we
7302 must output it now. */
7303 shlink
= symtab_hdr
->sh_link
;
7304 name
= (bfd_elf_string_from_elf_section
7305 (input_bfd
, shlink
, sym
.st_name
));
7309 osec
= sec
->output_section
;
7311 _bfd_elf_section_from_bfd_section (output_bfd
,
7313 if (sym
.st_shndx
== SHN_BAD
)
7316 sym
.st_value
+= sec
->output_offset
;
7317 if (! finfo
->info
->relocatable
)
7319 sym
.st_value
+= osec
->vma
;
7320 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7322 /* STT_TLS symbols are relative to PT_TLS
7324 BFD_ASSERT (elf_hash_table (finfo
->info
)
7326 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7331 finfo
->indices
[r_symndx
]
7332 = bfd_get_symcount (output_bfd
);
7334 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7339 r_symndx
= finfo
->indices
[r_symndx
];
7342 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7343 | (irela
->r_info
& r_type_mask
));
7346 /* Swap out the relocs. */
7347 if (input_rel_hdr
->sh_size
!= 0
7348 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7354 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7355 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7357 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7358 * bed
->s
->int_rels_per_ext_rel
);
7359 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7360 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7369 /* Write out the modified section contents. */
7370 if (bed
->elf_backend_write_section
7371 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7373 /* Section written out. */
7375 else switch (o
->sec_info_type
)
7377 case ELF_INFO_TYPE_STABS
:
7378 if (! (_bfd_write_section_stabs
7380 &elf_hash_table (finfo
->info
)->stab_info
,
7381 o
, &elf_section_data (o
)->sec_info
, contents
)))
7384 case ELF_INFO_TYPE_MERGE
:
7385 if (! _bfd_write_merged_section (output_bfd
, o
,
7386 elf_section_data (o
)->sec_info
))
7389 case ELF_INFO_TYPE_EH_FRAME
:
7391 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7398 if (! (o
->flags
& SEC_EXCLUDE
)
7399 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7401 (file_ptr
) o
->output_offset
,
7412 /* Generate a reloc when linking an ELF file. This is a reloc
7413 requested by the linker, and does not come from any input file. This
7414 is used to build constructor and destructor tables when linking
7418 elf_reloc_link_order (bfd
*output_bfd
,
7419 struct bfd_link_info
*info
,
7420 asection
*output_section
,
7421 struct bfd_link_order
*link_order
)
7423 reloc_howto_type
*howto
;
7427 struct elf_link_hash_entry
**rel_hash_ptr
;
7428 Elf_Internal_Shdr
*rel_hdr
;
7429 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7430 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7434 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7437 bfd_set_error (bfd_error_bad_value
);
7441 addend
= link_order
->u
.reloc
.p
->addend
;
7443 /* Figure out the symbol index. */
7444 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7445 + elf_section_data (output_section
)->rel_count
7446 + elf_section_data (output_section
)->rel_count2
);
7447 if (link_order
->type
== bfd_section_reloc_link_order
)
7449 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7450 BFD_ASSERT (indx
!= 0);
7451 *rel_hash_ptr
= NULL
;
7455 struct elf_link_hash_entry
*h
;
7457 /* Treat a reloc against a defined symbol as though it were
7458 actually against the section. */
7459 h
= ((struct elf_link_hash_entry
*)
7460 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7461 link_order
->u
.reloc
.p
->u
.name
,
7462 FALSE
, FALSE
, TRUE
));
7464 && (h
->root
.type
== bfd_link_hash_defined
7465 || h
->root
.type
== bfd_link_hash_defweak
))
7469 section
= h
->root
.u
.def
.section
;
7470 indx
= section
->output_section
->target_index
;
7471 *rel_hash_ptr
= NULL
;
7472 /* It seems that we ought to add the symbol value to the
7473 addend here, but in practice it has already been added
7474 because it was passed to constructor_callback. */
7475 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7479 /* Setting the index to -2 tells elf_link_output_extsym that
7480 this symbol is used by a reloc. */
7487 if (! ((*info
->callbacks
->unattached_reloc
)
7488 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7494 /* If this is an inplace reloc, we must write the addend into the
7496 if (howto
->partial_inplace
&& addend
!= 0)
7499 bfd_reloc_status_type rstat
;
7502 const char *sym_name
;
7504 size
= bfd_get_reloc_size (howto
);
7505 buf
= bfd_zmalloc (size
);
7508 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7515 case bfd_reloc_outofrange
:
7518 case bfd_reloc_overflow
:
7519 if (link_order
->type
== bfd_section_reloc_link_order
)
7520 sym_name
= bfd_section_name (output_bfd
,
7521 link_order
->u
.reloc
.p
->u
.section
);
7523 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7524 if (! ((*info
->callbacks
->reloc_overflow
)
7525 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7526 NULL
, (bfd_vma
) 0)))
7533 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7534 link_order
->offset
, size
);
7540 /* The address of a reloc is relative to the section in a
7541 relocatable file, and is a virtual address in an executable
7543 offset
= link_order
->offset
;
7544 if (! info
->relocatable
)
7545 offset
+= output_section
->vma
;
7547 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7549 irel
[i
].r_offset
= offset
;
7551 irel
[i
].r_addend
= 0;
7553 if (bed
->s
->arch_size
== 32)
7554 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7556 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7558 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7559 erel
= rel_hdr
->contents
;
7560 if (rel_hdr
->sh_type
== SHT_REL
)
7562 erel
+= (elf_section_data (output_section
)->rel_count
7563 * bed
->s
->sizeof_rel
);
7564 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7568 irel
[0].r_addend
= addend
;
7569 erel
+= (elf_section_data (output_section
)->rel_count
7570 * bed
->s
->sizeof_rela
);
7571 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7574 ++elf_section_data (output_section
)->rel_count
;
7580 /* Get the output vma of the section pointed to by the sh_link field. */
7583 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7585 Elf_Internal_Shdr
**elf_shdrp
;
7589 s
= p
->u
.indirect
.section
;
7590 elf_shdrp
= elf_elfsections (s
->owner
);
7591 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7592 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7594 The Intel C compiler generates SHT_IA_64_UNWIND with
7595 SHF_LINK_ORDER. But it doesn't set the sh_link or
7596 sh_info fields. Hence we could get the situation
7597 where elfsec is 0. */
7600 const struct elf_backend_data
*bed
7601 = get_elf_backend_data (s
->owner
);
7602 if (bed
->link_order_error_handler
)
7603 bed
->link_order_error_handler
7604 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7609 s
= elf_shdrp
[elfsec
]->bfd_section
;
7610 return s
->output_section
->vma
+ s
->output_offset
;
7615 /* Compare two sections based on the locations of the sections they are
7616 linked to. Used by elf_fixup_link_order. */
7619 compare_link_order (const void * a
, const void * b
)
7624 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7625 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7632 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7633 order as their linked sections. Returns false if this could not be done
7634 because an output section includes both ordered and unordered
7635 sections. Ideally we'd do this in the linker proper. */
7638 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7643 struct bfd_link_order
*p
;
7645 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7647 struct bfd_link_order
**sections
;
7648 asection
*s
, *other_sec
, *linkorder_sec
;
7652 linkorder_sec
= NULL
;
7655 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7657 if (p
->type
== bfd_indirect_link_order
)
7659 s
= p
->u
.indirect
.section
;
7661 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7662 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
7663 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
)) != -1
7664 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7678 if (seen_other
&& seen_linkorder
)
7680 if (other_sec
&& linkorder_sec
)
7681 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7683 linkorder_sec
->owner
, other_sec
,
7686 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7688 bfd_set_error (bfd_error_bad_value
);
7693 if (!seen_linkorder
)
7696 sections
= (struct bfd_link_order
**)
7697 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7700 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7702 sections
[seen_linkorder
++] = p
;
7704 /* Sort the input sections in the order of their linked section. */
7705 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7706 compare_link_order
);
7708 /* Change the offsets of the sections. */
7710 for (n
= 0; n
< seen_linkorder
; n
++)
7712 s
= sections
[n
]->u
.indirect
.section
;
7713 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7714 s
->output_offset
= offset
;
7715 sections
[n
]->offset
= offset
;
7716 offset
+= sections
[n
]->size
;
7723 /* Do the final step of an ELF link. */
7726 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7728 bfd_boolean dynamic
;
7729 bfd_boolean emit_relocs
;
7731 struct elf_final_link_info finfo
;
7732 register asection
*o
;
7733 register struct bfd_link_order
*p
;
7735 bfd_size_type max_contents_size
;
7736 bfd_size_type max_external_reloc_size
;
7737 bfd_size_type max_internal_reloc_count
;
7738 bfd_size_type max_sym_count
;
7739 bfd_size_type max_sym_shndx_count
;
7741 Elf_Internal_Sym elfsym
;
7743 Elf_Internal_Shdr
*symtab_hdr
;
7744 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7745 Elf_Internal_Shdr
*symstrtab_hdr
;
7746 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7747 struct elf_outext_info eoinfo
;
7749 size_t relativecount
= 0;
7750 asection
*reldyn
= 0;
7753 if (! is_elf_hash_table (info
->hash
))
7757 abfd
->flags
|= DYNAMIC
;
7759 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7760 dynobj
= elf_hash_table (info
)->dynobj
;
7762 emit_relocs
= (info
->relocatable
7763 || info
->emitrelocations
7764 || bed
->elf_backend_emit_relocs
);
7767 finfo
.output_bfd
= abfd
;
7768 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7769 if (finfo
.symstrtab
== NULL
)
7774 finfo
.dynsym_sec
= NULL
;
7775 finfo
.hash_sec
= NULL
;
7776 finfo
.symver_sec
= NULL
;
7780 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7781 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7782 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7783 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7784 /* Note that it is OK if symver_sec is NULL. */
7787 finfo
.contents
= NULL
;
7788 finfo
.external_relocs
= NULL
;
7789 finfo
.internal_relocs
= NULL
;
7790 finfo
.external_syms
= NULL
;
7791 finfo
.locsym_shndx
= NULL
;
7792 finfo
.internal_syms
= NULL
;
7793 finfo
.indices
= NULL
;
7794 finfo
.sections
= NULL
;
7795 finfo
.symbuf
= NULL
;
7796 finfo
.symshndxbuf
= NULL
;
7797 finfo
.symbuf_count
= 0;
7798 finfo
.shndxbuf_size
= 0;
7800 /* Count up the number of relocations we will output for each output
7801 section, so that we know the sizes of the reloc sections. We
7802 also figure out some maximum sizes. */
7803 max_contents_size
= 0;
7804 max_external_reloc_size
= 0;
7805 max_internal_reloc_count
= 0;
7807 max_sym_shndx_count
= 0;
7809 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7811 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7814 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7816 unsigned int reloc_count
= 0;
7817 struct bfd_elf_section_data
*esdi
= NULL
;
7818 unsigned int *rel_count1
;
7820 if (p
->type
== bfd_section_reloc_link_order
7821 || p
->type
== bfd_symbol_reloc_link_order
)
7823 else if (p
->type
== bfd_indirect_link_order
)
7827 sec
= p
->u
.indirect
.section
;
7828 esdi
= elf_section_data (sec
);
7830 /* Mark all sections which are to be included in the
7831 link. This will normally be every section. We need
7832 to do this so that we can identify any sections which
7833 the linker has decided to not include. */
7834 sec
->linker_mark
= TRUE
;
7836 if (sec
->flags
& SEC_MERGE
)
7839 if (info
->relocatable
|| info
->emitrelocations
)
7840 reloc_count
= sec
->reloc_count
;
7841 else if (bed
->elf_backend_count_relocs
)
7843 Elf_Internal_Rela
* relocs
;
7845 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7848 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7850 if (elf_section_data (o
)->relocs
!= relocs
)
7854 if (sec
->rawsize
> max_contents_size
)
7855 max_contents_size
= sec
->rawsize
;
7856 if (sec
->size
> max_contents_size
)
7857 max_contents_size
= sec
->size
;
7859 /* We are interested in just local symbols, not all
7861 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7862 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7866 if (elf_bad_symtab (sec
->owner
))
7867 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7868 / bed
->s
->sizeof_sym
);
7870 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7872 if (sym_count
> max_sym_count
)
7873 max_sym_count
= sym_count
;
7875 if (sym_count
> max_sym_shndx_count
7876 && elf_symtab_shndx (sec
->owner
) != 0)
7877 max_sym_shndx_count
= sym_count
;
7879 if ((sec
->flags
& SEC_RELOC
) != 0)
7883 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7884 if (ext_size
> max_external_reloc_size
)
7885 max_external_reloc_size
= ext_size
;
7886 if (sec
->reloc_count
> max_internal_reloc_count
)
7887 max_internal_reloc_count
= sec
->reloc_count
;
7892 if (reloc_count
== 0)
7895 o
->reloc_count
+= reloc_count
;
7897 /* MIPS may have a mix of REL and RELA relocs on sections.
7898 To support this curious ABI we keep reloc counts in
7899 elf_section_data too. We must be careful to add the
7900 relocations from the input section to the right output
7901 count. FIXME: Get rid of one count. We have
7902 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7903 rel_count1
= &esdo
->rel_count
;
7906 bfd_boolean same_size
;
7907 bfd_size_type entsize1
;
7909 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7910 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7911 || entsize1
== bed
->s
->sizeof_rela
);
7912 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7915 rel_count1
= &esdo
->rel_count2
;
7917 if (esdi
->rel_hdr2
!= NULL
)
7919 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7920 unsigned int alt_count
;
7921 unsigned int *rel_count2
;
7923 BFD_ASSERT (entsize2
!= entsize1
7924 && (entsize2
== bed
->s
->sizeof_rel
7925 || entsize2
== bed
->s
->sizeof_rela
));
7927 rel_count2
= &esdo
->rel_count2
;
7929 rel_count2
= &esdo
->rel_count
;
7931 /* The following is probably too simplistic if the
7932 backend counts output relocs unusually. */
7933 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7934 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7935 *rel_count2
+= alt_count
;
7936 reloc_count
-= alt_count
;
7939 *rel_count1
+= reloc_count
;
7942 if (o
->reloc_count
> 0)
7943 o
->flags
|= SEC_RELOC
;
7946 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7947 set it (this is probably a bug) and if it is set
7948 assign_section_numbers will create a reloc section. */
7949 o
->flags
&=~ SEC_RELOC
;
7952 /* If the SEC_ALLOC flag is not set, force the section VMA to
7953 zero. This is done in elf_fake_sections as well, but forcing
7954 the VMA to 0 here will ensure that relocs against these
7955 sections are handled correctly. */
7956 if ((o
->flags
& SEC_ALLOC
) == 0
7957 && ! o
->user_set_vma
)
7961 if (! info
->relocatable
&& merged
)
7962 elf_link_hash_traverse (elf_hash_table (info
),
7963 _bfd_elf_link_sec_merge_syms
, abfd
);
7965 /* Figure out the file positions for everything but the symbol table
7966 and the relocs. We set symcount to force assign_section_numbers
7967 to create a symbol table. */
7968 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7969 BFD_ASSERT (! abfd
->output_has_begun
);
7970 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7973 /* Set sizes, and assign file positions for reloc sections. */
7974 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7976 if ((o
->flags
& SEC_RELOC
) != 0)
7978 if (!(_bfd_elf_link_size_reloc_section
7979 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7982 if (elf_section_data (o
)->rel_hdr2
7983 && !(_bfd_elf_link_size_reloc_section
7984 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7988 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7989 to count upwards while actually outputting the relocations. */
7990 elf_section_data (o
)->rel_count
= 0;
7991 elf_section_data (o
)->rel_count2
= 0;
7994 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7996 /* We have now assigned file positions for all the sections except
7997 .symtab and .strtab. We start the .symtab section at the current
7998 file position, and write directly to it. We build the .strtab
7999 section in memory. */
8000 bfd_get_symcount (abfd
) = 0;
8001 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8002 /* sh_name is set in prep_headers. */
8003 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8004 /* sh_flags, sh_addr and sh_size all start off zero. */
8005 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8006 /* sh_link is set in assign_section_numbers. */
8007 /* sh_info is set below. */
8008 /* sh_offset is set just below. */
8009 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8011 off
= elf_tdata (abfd
)->next_file_pos
;
8012 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8014 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8015 incorrect. We do not yet know the size of the .symtab section.
8016 We correct next_file_pos below, after we do know the size. */
8018 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8019 continuously seeking to the right position in the file. */
8020 if (! info
->keep_memory
|| max_sym_count
< 20)
8021 finfo
.symbuf_size
= 20;
8023 finfo
.symbuf_size
= max_sym_count
;
8024 amt
= finfo
.symbuf_size
;
8025 amt
*= bed
->s
->sizeof_sym
;
8026 finfo
.symbuf
= bfd_malloc (amt
);
8027 if (finfo
.symbuf
== NULL
)
8029 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8031 /* Wild guess at number of output symbols. realloc'd as needed. */
8032 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8033 finfo
.shndxbuf_size
= amt
;
8034 amt
*= sizeof (Elf_External_Sym_Shndx
);
8035 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8036 if (finfo
.symshndxbuf
== NULL
)
8040 /* Start writing out the symbol table. The first symbol is always a
8042 if (info
->strip
!= strip_all
8045 elfsym
.st_value
= 0;
8048 elfsym
.st_other
= 0;
8049 elfsym
.st_shndx
= SHN_UNDEF
;
8050 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8055 /* Output a symbol for each section. We output these even if we are
8056 discarding local symbols, since they are used for relocs. These
8057 symbols have no names. We store the index of each one in the
8058 index field of the section, so that we can find it again when
8059 outputting relocs. */
8060 if (info
->strip
!= strip_all
8064 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8065 elfsym
.st_other
= 0;
8066 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8068 o
= bfd_section_from_elf_index (abfd
, i
);
8070 o
->target_index
= bfd_get_symcount (abfd
);
8071 elfsym
.st_shndx
= i
;
8072 if (info
->relocatable
|| o
== NULL
)
8073 elfsym
.st_value
= 0;
8075 elfsym
.st_value
= o
->vma
;
8076 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8078 if (i
== SHN_LORESERVE
- 1)
8079 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8083 /* Allocate some memory to hold information read in from the input
8085 if (max_contents_size
!= 0)
8087 finfo
.contents
= bfd_malloc (max_contents_size
);
8088 if (finfo
.contents
== NULL
)
8092 if (max_external_reloc_size
!= 0)
8094 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8095 if (finfo
.external_relocs
== NULL
)
8099 if (max_internal_reloc_count
!= 0)
8101 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8102 amt
*= sizeof (Elf_Internal_Rela
);
8103 finfo
.internal_relocs
= bfd_malloc (amt
);
8104 if (finfo
.internal_relocs
== NULL
)
8108 if (max_sym_count
!= 0)
8110 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8111 finfo
.external_syms
= bfd_malloc (amt
);
8112 if (finfo
.external_syms
== NULL
)
8115 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8116 finfo
.internal_syms
= bfd_malloc (amt
);
8117 if (finfo
.internal_syms
== NULL
)
8120 amt
= max_sym_count
* sizeof (long);
8121 finfo
.indices
= bfd_malloc (amt
);
8122 if (finfo
.indices
== NULL
)
8125 amt
= max_sym_count
* sizeof (asection
*);
8126 finfo
.sections
= bfd_malloc (amt
);
8127 if (finfo
.sections
== NULL
)
8131 if (max_sym_shndx_count
!= 0)
8133 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8134 finfo
.locsym_shndx
= bfd_malloc (amt
);
8135 if (finfo
.locsym_shndx
== NULL
)
8139 if (elf_hash_table (info
)->tls_sec
)
8141 bfd_vma base
, end
= 0;
8144 for (sec
= elf_hash_table (info
)->tls_sec
;
8145 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8148 bfd_size_type size
= sec
->size
;
8151 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8153 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8155 size
= o
->offset
+ o
->size
;
8157 end
= sec
->vma
+ size
;
8159 base
= elf_hash_table (info
)->tls_sec
->vma
;
8160 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8161 elf_hash_table (info
)->tls_size
= end
- base
;
8164 /* Reorder SHF_LINK_ORDER sections. */
8165 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8167 if (!elf_fixup_link_order (abfd
, o
))
8171 /* Since ELF permits relocations to be against local symbols, we
8172 must have the local symbols available when we do the relocations.
8173 Since we would rather only read the local symbols once, and we
8174 would rather not keep them in memory, we handle all the
8175 relocations for a single input file at the same time.
8177 Unfortunately, there is no way to know the total number of local
8178 symbols until we have seen all of them, and the local symbol
8179 indices precede the global symbol indices. This means that when
8180 we are generating relocatable output, and we see a reloc against
8181 a global symbol, we can not know the symbol index until we have
8182 finished examining all the local symbols to see which ones we are
8183 going to output. To deal with this, we keep the relocations in
8184 memory, and don't output them until the end of the link. This is
8185 an unfortunate waste of memory, but I don't see a good way around
8186 it. Fortunately, it only happens when performing a relocatable
8187 link, which is not the common case. FIXME: If keep_memory is set
8188 we could write the relocs out and then read them again; I don't
8189 know how bad the memory loss will be. */
8191 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8192 sub
->output_has_begun
= FALSE
;
8193 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8195 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8197 if (p
->type
== bfd_indirect_link_order
8198 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8199 == bfd_target_elf_flavour
)
8200 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8202 if (! sub
->output_has_begun
)
8204 if (! elf_link_input_bfd (&finfo
, sub
))
8206 sub
->output_has_begun
= TRUE
;
8209 else if (p
->type
== bfd_section_reloc_link_order
8210 || p
->type
== bfd_symbol_reloc_link_order
)
8212 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8217 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8223 /* Output any global symbols that got converted to local in a
8224 version script or due to symbol visibility. We do this in a
8225 separate step since ELF requires all local symbols to appear
8226 prior to any global symbols. FIXME: We should only do this if
8227 some global symbols were, in fact, converted to become local.
8228 FIXME: Will this work correctly with the Irix 5 linker? */
8229 eoinfo
.failed
= FALSE
;
8230 eoinfo
.finfo
= &finfo
;
8231 eoinfo
.localsyms
= TRUE
;
8232 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8237 /* That wrote out all the local symbols. Finish up the symbol table
8238 with the global symbols. Even if we want to strip everything we
8239 can, we still need to deal with those global symbols that got
8240 converted to local in a version script. */
8242 /* The sh_info field records the index of the first non local symbol. */
8243 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8246 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8248 Elf_Internal_Sym sym
;
8249 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8250 long last_local
= 0;
8252 /* Write out the section symbols for the output sections. */
8253 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8259 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8262 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8268 dynindx
= elf_section_data (s
)->dynindx
;
8271 indx
= elf_section_data (s
)->this_idx
;
8272 BFD_ASSERT (indx
> 0);
8273 sym
.st_shndx
= indx
;
8274 sym
.st_value
= s
->vma
;
8275 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8276 if (last_local
< dynindx
)
8277 last_local
= dynindx
;
8278 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8282 /* Write out the local dynsyms. */
8283 if (elf_hash_table (info
)->dynlocal
)
8285 struct elf_link_local_dynamic_entry
*e
;
8286 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8291 sym
.st_size
= e
->isym
.st_size
;
8292 sym
.st_other
= e
->isym
.st_other
;
8294 /* Copy the internal symbol as is.
8295 Note that we saved a word of storage and overwrote
8296 the original st_name with the dynstr_index. */
8299 if (e
->isym
.st_shndx
!= SHN_UNDEF
8300 && (e
->isym
.st_shndx
< SHN_LORESERVE
8301 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8303 s
= bfd_section_from_elf_index (e
->input_bfd
,
8307 elf_section_data (s
->output_section
)->this_idx
;
8308 sym
.st_value
= (s
->output_section
->vma
8310 + e
->isym
.st_value
);
8313 if (last_local
< e
->dynindx
)
8314 last_local
= e
->dynindx
;
8316 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8317 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8321 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8325 /* We get the global symbols from the hash table. */
8326 eoinfo
.failed
= FALSE
;
8327 eoinfo
.localsyms
= FALSE
;
8328 eoinfo
.finfo
= &finfo
;
8329 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8334 /* If backend needs to output some symbols not present in the hash
8335 table, do it now. */
8336 if (bed
->elf_backend_output_arch_syms
)
8338 typedef bfd_boolean (*out_sym_func
)
8339 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8340 struct elf_link_hash_entry
*);
8342 if (! ((*bed
->elf_backend_output_arch_syms
)
8343 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8347 /* Flush all symbols to the file. */
8348 if (! elf_link_flush_output_syms (&finfo
, bed
))
8351 /* Now we know the size of the symtab section. */
8352 off
+= symtab_hdr
->sh_size
;
8354 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8355 if (symtab_shndx_hdr
->sh_name
!= 0)
8357 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8358 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8359 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8360 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8361 symtab_shndx_hdr
->sh_size
= amt
;
8363 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8366 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8367 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8372 /* Finish up and write out the symbol string table (.strtab)
8374 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8375 /* sh_name was set in prep_headers. */
8376 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8377 symstrtab_hdr
->sh_flags
= 0;
8378 symstrtab_hdr
->sh_addr
= 0;
8379 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8380 symstrtab_hdr
->sh_entsize
= 0;
8381 symstrtab_hdr
->sh_link
= 0;
8382 symstrtab_hdr
->sh_info
= 0;
8383 /* sh_offset is set just below. */
8384 symstrtab_hdr
->sh_addralign
= 1;
8386 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8387 elf_tdata (abfd
)->next_file_pos
= off
;
8389 if (bfd_get_symcount (abfd
) > 0)
8391 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8392 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8396 /* Adjust the relocs to have the correct symbol indices. */
8397 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8399 if ((o
->flags
& SEC_RELOC
) == 0)
8402 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8403 elf_section_data (o
)->rel_count
,
8404 elf_section_data (o
)->rel_hashes
);
8405 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8406 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8407 elf_section_data (o
)->rel_count2
,
8408 (elf_section_data (o
)->rel_hashes
8409 + elf_section_data (o
)->rel_count
));
8411 /* Set the reloc_count field to 0 to prevent write_relocs from
8412 trying to swap the relocs out itself. */
8416 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8417 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8419 /* If we are linking against a dynamic object, or generating a
8420 shared library, finish up the dynamic linking information. */
8423 bfd_byte
*dyncon
, *dynconend
;
8425 /* Fix up .dynamic entries. */
8426 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8427 BFD_ASSERT (o
!= NULL
);
8429 dyncon
= o
->contents
;
8430 dynconend
= o
->contents
+ o
->size
;
8431 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8433 Elf_Internal_Dyn dyn
;
8437 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8444 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8446 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8448 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8449 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8452 dyn
.d_un
.d_val
= relativecount
;
8459 name
= info
->init_function
;
8462 name
= info
->fini_function
;
8465 struct elf_link_hash_entry
*h
;
8467 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8468 FALSE
, FALSE
, TRUE
);
8470 && (h
->root
.type
== bfd_link_hash_defined
8471 || h
->root
.type
== bfd_link_hash_defweak
))
8473 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8474 o
= h
->root
.u
.def
.section
;
8475 if (o
->output_section
!= NULL
)
8476 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8477 + o
->output_offset
);
8480 /* The symbol is imported from another shared
8481 library and does not apply to this one. */
8489 case DT_PREINIT_ARRAYSZ
:
8490 name
= ".preinit_array";
8492 case DT_INIT_ARRAYSZ
:
8493 name
= ".init_array";
8495 case DT_FINI_ARRAYSZ
:
8496 name
= ".fini_array";
8498 o
= bfd_get_section_by_name (abfd
, name
);
8501 (*_bfd_error_handler
)
8502 (_("%B: could not find output section %s"), abfd
, name
);
8506 (*_bfd_error_handler
)
8507 (_("warning: %s section has zero size"), name
);
8508 dyn
.d_un
.d_val
= o
->size
;
8511 case DT_PREINIT_ARRAY
:
8512 name
= ".preinit_array";
8515 name
= ".init_array";
8518 name
= ".fini_array";
8531 name
= ".gnu.version_d";
8534 name
= ".gnu.version_r";
8537 name
= ".gnu.version";
8539 o
= bfd_get_section_by_name (abfd
, name
);
8542 (*_bfd_error_handler
)
8543 (_("%B: could not find output section %s"), abfd
, name
);
8546 dyn
.d_un
.d_ptr
= o
->vma
;
8553 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8558 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8560 Elf_Internal_Shdr
*hdr
;
8562 hdr
= elf_elfsections (abfd
)[i
];
8563 if (hdr
->sh_type
== type
8564 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8566 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8567 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8570 if (dyn
.d_un
.d_val
== 0
8571 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8572 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8578 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8582 /* If we have created any dynamic sections, then output them. */
8585 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8588 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8590 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8592 || o
->output_section
== bfd_abs_section_ptr
)
8594 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8596 /* At this point, we are only interested in sections
8597 created by _bfd_elf_link_create_dynamic_sections. */
8600 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8602 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8604 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8606 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8608 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8610 (file_ptr
) o
->output_offset
,
8616 /* The contents of the .dynstr section are actually in a
8618 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8619 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8620 || ! _bfd_elf_strtab_emit (abfd
,
8621 elf_hash_table (info
)->dynstr
))
8627 if (info
->relocatable
)
8629 bfd_boolean failed
= FALSE
;
8631 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8636 /* If we have optimized stabs strings, output them. */
8637 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8639 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8643 if (info
->eh_frame_hdr
)
8645 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8649 if (finfo
.symstrtab
!= NULL
)
8650 _bfd_stringtab_free (finfo
.symstrtab
);
8651 if (finfo
.contents
!= NULL
)
8652 free (finfo
.contents
);
8653 if (finfo
.external_relocs
!= NULL
)
8654 free (finfo
.external_relocs
);
8655 if (finfo
.internal_relocs
!= NULL
)
8656 free (finfo
.internal_relocs
);
8657 if (finfo
.external_syms
!= NULL
)
8658 free (finfo
.external_syms
);
8659 if (finfo
.locsym_shndx
!= NULL
)
8660 free (finfo
.locsym_shndx
);
8661 if (finfo
.internal_syms
!= NULL
)
8662 free (finfo
.internal_syms
);
8663 if (finfo
.indices
!= NULL
)
8664 free (finfo
.indices
);
8665 if (finfo
.sections
!= NULL
)
8666 free (finfo
.sections
);
8667 if (finfo
.symbuf
!= NULL
)
8668 free (finfo
.symbuf
);
8669 if (finfo
.symshndxbuf
!= NULL
)
8670 free (finfo
.symshndxbuf
);
8671 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8673 if ((o
->flags
& SEC_RELOC
) != 0
8674 && elf_section_data (o
)->rel_hashes
!= NULL
)
8675 free (elf_section_data (o
)->rel_hashes
);
8678 elf_tdata (abfd
)->linker
= TRUE
;
8683 if (finfo
.symstrtab
!= NULL
)
8684 _bfd_stringtab_free (finfo
.symstrtab
);
8685 if (finfo
.contents
!= NULL
)
8686 free (finfo
.contents
);
8687 if (finfo
.external_relocs
!= NULL
)
8688 free (finfo
.external_relocs
);
8689 if (finfo
.internal_relocs
!= NULL
)
8690 free (finfo
.internal_relocs
);
8691 if (finfo
.external_syms
!= NULL
)
8692 free (finfo
.external_syms
);
8693 if (finfo
.locsym_shndx
!= NULL
)
8694 free (finfo
.locsym_shndx
);
8695 if (finfo
.internal_syms
!= NULL
)
8696 free (finfo
.internal_syms
);
8697 if (finfo
.indices
!= NULL
)
8698 free (finfo
.indices
);
8699 if (finfo
.sections
!= NULL
)
8700 free (finfo
.sections
);
8701 if (finfo
.symbuf
!= NULL
)
8702 free (finfo
.symbuf
);
8703 if (finfo
.symshndxbuf
!= NULL
)
8704 free (finfo
.symshndxbuf
);
8705 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8707 if ((o
->flags
& SEC_RELOC
) != 0
8708 && elf_section_data (o
)->rel_hashes
!= NULL
)
8709 free (elf_section_data (o
)->rel_hashes
);
8715 /* Garbage collect unused sections. */
8717 /* The mark phase of garbage collection. For a given section, mark
8718 it and any sections in this section's group, and all the sections
8719 which define symbols to which it refers. */
8721 typedef asection
* (*gc_mark_hook_fn
)
8722 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8723 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8726 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8728 gc_mark_hook_fn gc_mark_hook
)
8732 asection
*group_sec
;
8736 /* Mark all the sections in the group. */
8737 group_sec
= elf_section_data (sec
)->next_in_group
;
8738 if (group_sec
&& !group_sec
->gc_mark
)
8739 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8742 /* Look through the section relocs. */
8744 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8745 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8747 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8748 Elf_Internal_Shdr
*symtab_hdr
;
8749 struct elf_link_hash_entry
**sym_hashes
;
8752 bfd
*input_bfd
= sec
->owner
;
8753 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8754 Elf_Internal_Sym
*isym
= NULL
;
8757 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8758 sym_hashes
= elf_sym_hashes (input_bfd
);
8760 /* Read the local symbols. */
8761 if (elf_bad_symtab (input_bfd
))
8763 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8767 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8769 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8770 if (isym
== NULL
&& nlocsyms
!= 0)
8772 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8778 /* Read the relocations. */
8779 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8781 if (relstart
== NULL
)
8786 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8788 if (bed
->s
->arch_size
== 32)
8793 for (rel
= relstart
; rel
< relend
; rel
++)
8795 unsigned long r_symndx
;
8797 struct elf_link_hash_entry
*h
;
8799 r_symndx
= rel
->r_info
>> r_sym_shift
;
8803 if (r_symndx
>= nlocsyms
8804 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8806 h
= sym_hashes
[r_symndx
- extsymoff
];
8807 while (h
->root
.type
== bfd_link_hash_indirect
8808 || h
->root
.type
== bfd_link_hash_warning
)
8809 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8810 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8814 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8817 if (rsec
&& !rsec
->gc_mark
)
8819 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8822 rsec
->gc_mark_from_eh
= 1;
8823 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8832 if (elf_section_data (sec
)->relocs
!= relstart
)
8835 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8837 if (! info
->keep_memory
)
8840 symtab_hdr
->contents
= (unsigned char *) isym
;
8847 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8849 struct elf_gc_sweep_symbol_info
{
8850 struct bfd_link_info
*info
;
8851 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8856 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8858 if (h
->root
.type
== bfd_link_hash_warning
)
8859 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8861 if ((h
->root
.type
== bfd_link_hash_defined
8862 || h
->root
.type
== bfd_link_hash_defweak
)
8863 && !h
->root
.u
.def
.section
->gc_mark
8864 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8866 struct elf_gc_sweep_symbol_info
*inf
= data
;
8867 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8873 /* The sweep phase of garbage collection. Remove all garbage sections. */
8875 typedef bfd_boolean (*gc_sweep_hook_fn
)
8876 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8879 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
8882 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8883 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
8884 unsigned long section_sym_count
;
8885 struct elf_gc_sweep_symbol_info sweep_info
;
8887 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8891 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8894 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8896 /* Keep debug and special sections. */
8897 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8898 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8904 /* Skip sweeping sections already excluded. */
8905 if (o
->flags
& SEC_EXCLUDE
)
8908 /* Since this is early in the link process, it is simple
8909 to remove a section from the output. */
8910 o
->flags
|= SEC_EXCLUDE
;
8912 /* But we also have to update some of the relocation
8913 info we collected before. */
8915 && (o
->flags
& SEC_RELOC
) != 0
8916 && o
->reloc_count
> 0
8917 && !bfd_is_abs_section (o
->output_section
))
8919 Elf_Internal_Rela
*internal_relocs
;
8923 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8925 if (internal_relocs
== NULL
)
8928 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8930 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8931 free (internal_relocs
);
8939 /* Remove the symbols that were in the swept sections from the dynamic
8940 symbol table. GCFIXME: Anyone know how to get them out of the
8941 static symbol table as well? */
8942 sweep_info
.info
= info
;
8943 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
8944 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
8947 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
8951 /* Propagate collected vtable information. This is called through
8952 elf_link_hash_traverse. */
8955 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8957 if (h
->root
.type
== bfd_link_hash_warning
)
8958 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8960 /* Those that are not vtables. */
8961 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8964 /* Those vtables that do not have parents, we cannot merge. */
8965 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8968 /* If we've already been done, exit. */
8969 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8972 /* Make sure the parent's table is up to date. */
8973 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8975 if (h
->vtable
->used
== NULL
)
8977 /* None of this table's entries were referenced. Re-use the
8979 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8980 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8985 bfd_boolean
*cu
, *pu
;
8987 /* Or the parent's entries into ours. */
8988 cu
= h
->vtable
->used
;
8990 pu
= h
->vtable
->parent
->vtable
->used
;
8993 const struct elf_backend_data
*bed
;
8994 unsigned int log_file_align
;
8996 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8997 log_file_align
= bed
->s
->log_file_align
;
8998 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9013 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9016 bfd_vma hstart
, hend
;
9017 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9018 const struct elf_backend_data
*bed
;
9019 unsigned int log_file_align
;
9021 if (h
->root
.type
== bfd_link_hash_warning
)
9022 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9024 /* Take care of both those symbols that do not describe vtables as
9025 well as those that are not loaded. */
9026 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9029 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9030 || h
->root
.type
== bfd_link_hash_defweak
);
9032 sec
= h
->root
.u
.def
.section
;
9033 hstart
= h
->root
.u
.def
.value
;
9034 hend
= hstart
+ h
->size
;
9036 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9038 return *(bfd_boolean
*) okp
= FALSE
;
9039 bed
= get_elf_backend_data (sec
->owner
);
9040 log_file_align
= bed
->s
->log_file_align
;
9042 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9044 for (rel
= relstart
; rel
< relend
; ++rel
)
9045 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9047 /* If the entry is in use, do nothing. */
9049 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9051 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9052 if (h
->vtable
->used
[entry
])
9055 /* Otherwise, kill it. */
9056 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9062 /* Mark sections containing dynamically referenced symbols. When
9063 building shared libraries, we must assume that any visible symbol is
9067 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9069 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9071 if (h
->root
.type
== bfd_link_hash_warning
)
9072 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9074 if ((h
->root
.type
== bfd_link_hash_defined
9075 || h
->root
.type
== bfd_link_hash_defweak
)
9077 || (!info
->executable
9079 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9080 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9081 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9086 /* Do mark and sweep of unused sections. */
9089 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9091 bfd_boolean ok
= TRUE
;
9093 asection
* (*gc_mark_hook
)
9094 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9095 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9096 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9098 if (!bed
->can_gc_sections
9099 || info
->relocatable
9100 || info
->emitrelocations
9101 || !is_elf_hash_table (info
->hash
))
9103 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9107 /* Apply transitive closure to the vtable entry usage info. */
9108 elf_link_hash_traverse (elf_hash_table (info
),
9109 elf_gc_propagate_vtable_entries_used
,
9114 /* Kill the vtable relocations that were not used. */
9115 elf_link_hash_traverse (elf_hash_table (info
),
9116 elf_gc_smash_unused_vtentry_relocs
,
9121 /* Mark dynamically referenced symbols. */
9122 if (elf_hash_table (info
)->dynamic_sections_created
)
9123 elf_link_hash_traverse (elf_hash_table (info
),
9124 bed
->gc_mark_dynamic_ref
,
9127 /* Grovel through relocs to find out who stays ... */
9128 gc_mark_hook
= bed
->gc_mark_hook
;
9129 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9133 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9136 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9137 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9138 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9142 /* ... again for sections marked from eh_frame. */
9143 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9147 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9150 /* Keep .gcc_except_table.* if the associated .text.* is
9151 marked. This isn't very nice, but the proper solution,
9152 splitting .eh_frame up and using comdat doesn't pan out
9153 easily due to needing special relocs to handle the
9154 difference of two symbols in separate sections.
9155 Don't keep code sections referenced by .eh_frame. */
9156 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9157 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9159 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9165 len
= strlen (o
->name
+ 18) + 1;
9166 fn_name
= bfd_malloc (len
+ 6);
9167 if (fn_name
== NULL
)
9169 memcpy (fn_name
, ".text.", 6);
9170 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9171 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9173 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9177 /* If not using specially named exception table section,
9178 then keep whatever we are using. */
9179 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9184 /* ... and mark SEC_EXCLUDE for those that go. */
9185 return elf_gc_sweep (abfd
, info
);
9188 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9191 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9193 struct elf_link_hash_entry
*h
,
9196 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9197 struct elf_link_hash_entry
**search
, *child
;
9198 bfd_size_type extsymcount
;
9199 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9201 /* The sh_info field of the symtab header tells us where the
9202 external symbols start. We don't care about the local symbols at
9204 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9205 if (!elf_bad_symtab (abfd
))
9206 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9208 sym_hashes
= elf_sym_hashes (abfd
);
9209 sym_hashes_end
= sym_hashes
+ extsymcount
;
9211 /* Hunt down the child symbol, which is in this section at the same
9212 offset as the relocation. */
9213 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9215 if ((child
= *search
) != NULL
9216 && (child
->root
.type
== bfd_link_hash_defined
9217 || child
->root
.type
== bfd_link_hash_defweak
)
9218 && child
->root
.u
.def
.section
== sec
9219 && child
->root
.u
.def
.value
== offset
)
9223 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9224 abfd
, sec
, (unsigned long) offset
);
9225 bfd_set_error (bfd_error_invalid_operation
);
9231 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9237 /* This *should* only be the absolute section. It could potentially
9238 be that someone has defined a non-global vtable though, which
9239 would be bad. It isn't worth paging in the local symbols to be
9240 sure though; that case should simply be handled by the assembler. */
9242 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9245 child
->vtable
->parent
= h
;
9250 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9253 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9254 asection
*sec ATTRIBUTE_UNUSED
,
9255 struct elf_link_hash_entry
*h
,
9258 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9259 unsigned int log_file_align
= bed
->s
->log_file_align
;
9263 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9268 if (addend
>= h
->vtable
->size
)
9270 size_t size
, bytes
, file_align
;
9271 bfd_boolean
*ptr
= h
->vtable
->used
;
9273 /* While the symbol is undefined, we have to be prepared to handle
9275 file_align
= 1 << log_file_align
;
9276 if (h
->root
.type
== bfd_link_hash_undefined
)
9277 size
= addend
+ file_align
;
9283 /* Oops! We've got a reference past the defined end of
9284 the table. This is probably a bug -- shall we warn? */
9285 size
= addend
+ file_align
;
9288 size
= (size
+ file_align
- 1) & -file_align
;
9290 /* Allocate one extra entry for use as a "done" flag for the
9291 consolidation pass. */
9292 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9296 ptr
= bfd_realloc (ptr
- 1, bytes
);
9302 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9303 * sizeof (bfd_boolean
));
9304 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9308 ptr
= bfd_zmalloc (bytes
);
9313 /* And arrange for that done flag to be at index -1. */
9314 h
->vtable
->used
= ptr
+ 1;
9315 h
->vtable
->size
= size
;
9318 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9323 struct alloc_got_off_arg
{
9325 unsigned int got_elt_size
;
9328 /* We need a special top-level link routine to convert got reference counts
9329 to real got offsets. */
9332 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9334 struct alloc_got_off_arg
*gofarg
= arg
;
9336 if (h
->root
.type
== bfd_link_hash_warning
)
9337 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9339 if (h
->got
.refcount
> 0)
9341 h
->got
.offset
= gofarg
->gotoff
;
9342 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9345 h
->got
.offset
= (bfd_vma
) -1;
9350 /* And an accompanying bit to work out final got entry offsets once
9351 we're done. Should be called from final_link. */
9354 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9355 struct bfd_link_info
*info
)
9358 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9360 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9361 struct alloc_got_off_arg gofarg
;
9363 if (! is_elf_hash_table (info
->hash
))
9366 /* The GOT offset is relative to the .got section, but the GOT header is
9367 put into the .got.plt section, if the backend uses it. */
9368 if (bed
->want_got_plt
)
9371 gotoff
= bed
->got_header_size
;
9373 /* Do the local .got entries first. */
9374 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9376 bfd_signed_vma
*local_got
;
9377 bfd_size_type j
, locsymcount
;
9378 Elf_Internal_Shdr
*symtab_hdr
;
9380 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9383 local_got
= elf_local_got_refcounts (i
);
9387 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9388 if (elf_bad_symtab (i
))
9389 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9391 locsymcount
= symtab_hdr
->sh_info
;
9393 for (j
= 0; j
< locsymcount
; ++j
)
9395 if (local_got
[j
] > 0)
9397 local_got
[j
] = gotoff
;
9398 gotoff
+= got_elt_size
;
9401 local_got
[j
] = (bfd_vma
) -1;
9405 /* Then the global .got entries. .plt refcounts are handled by
9406 adjust_dynamic_symbol */
9407 gofarg
.gotoff
= gotoff
;
9408 gofarg
.got_elt_size
= got_elt_size
;
9409 elf_link_hash_traverse (elf_hash_table (info
),
9410 elf_gc_allocate_got_offsets
,
9415 /* Many folk need no more in the way of final link than this, once
9416 got entry reference counting is enabled. */
9419 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9421 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9424 /* Invoke the regular ELF backend linker to do all the work. */
9425 return bfd_elf_final_link (abfd
, info
);
9429 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9431 struct elf_reloc_cookie
*rcookie
= cookie
;
9433 if (rcookie
->bad_symtab
)
9434 rcookie
->rel
= rcookie
->rels
;
9436 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9438 unsigned long r_symndx
;
9440 if (! rcookie
->bad_symtab
)
9441 if (rcookie
->rel
->r_offset
> offset
)
9443 if (rcookie
->rel
->r_offset
!= offset
)
9446 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9447 if (r_symndx
== SHN_UNDEF
)
9450 if (r_symndx
>= rcookie
->locsymcount
9451 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9453 struct elf_link_hash_entry
*h
;
9455 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9457 while (h
->root
.type
== bfd_link_hash_indirect
9458 || h
->root
.type
== bfd_link_hash_warning
)
9459 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9461 if ((h
->root
.type
== bfd_link_hash_defined
9462 || h
->root
.type
== bfd_link_hash_defweak
)
9463 && elf_discarded_section (h
->root
.u
.def
.section
))
9470 /* It's not a relocation against a global symbol,
9471 but it could be a relocation against a local
9472 symbol for a discarded section. */
9474 Elf_Internal_Sym
*isym
;
9476 /* Need to: get the symbol; get the section. */
9477 isym
= &rcookie
->locsyms
[r_symndx
];
9478 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9480 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9481 if (isec
!= NULL
&& elf_discarded_section (isec
))
9490 /* Discard unneeded references to discarded sections.
9491 Returns TRUE if any section's size was changed. */
9492 /* This function assumes that the relocations are in sorted order,
9493 which is true for all known assemblers. */
9496 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9498 struct elf_reloc_cookie cookie
;
9499 asection
*stab
, *eh
;
9500 Elf_Internal_Shdr
*symtab_hdr
;
9501 const struct elf_backend_data
*bed
;
9504 bfd_boolean ret
= FALSE
;
9506 if (info
->traditional_format
9507 || !is_elf_hash_table (info
->hash
))
9510 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9512 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9515 bed
= get_elf_backend_data (abfd
);
9517 if ((abfd
->flags
& DYNAMIC
) != 0)
9520 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9521 if (info
->relocatable
9524 || bfd_is_abs_section (eh
->output_section
))))
9527 stab
= bfd_get_section_by_name (abfd
, ".stab");
9530 || bfd_is_abs_section (stab
->output_section
)
9531 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9536 && bed
->elf_backend_discard_info
== NULL
)
9539 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9541 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9542 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9543 if (cookie
.bad_symtab
)
9545 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9546 cookie
.extsymoff
= 0;
9550 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9551 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9554 if (bed
->s
->arch_size
== 32)
9555 cookie
.r_sym_shift
= 8;
9557 cookie
.r_sym_shift
= 32;
9559 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9560 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9562 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9563 cookie
.locsymcount
, 0,
9565 if (cookie
.locsyms
== NULL
)
9572 count
= stab
->reloc_count
;
9574 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9576 if (cookie
.rels
!= NULL
)
9578 cookie
.rel
= cookie
.rels
;
9579 cookie
.relend
= cookie
.rels
;
9580 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9581 if (_bfd_discard_section_stabs (abfd
, stab
,
9582 elf_section_data (stab
)->sec_info
,
9583 bfd_elf_reloc_symbol_deleted_p
,
9586 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9594 count
= eh
->reloc_count
;
9596 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9598 cookie
.rel
= cookie
.rels
;
9599 cookie
.relend
= cookie
.rels
;
9600 if (cookie
.rels
!= NULL
)
9601 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9603 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9604 bfd_elf_reloc_symbol_deleted_p
,
9608 if (cookie
.rels
!= NULL
9609 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9613 if (bed
->elf_backend_discard_info
!= NULL
9614 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9617 if (cookie
.locsyms
!= NULL
9618 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9620 if (! info
->keep_memory
)
9621 free (cookie
.locsyms
);
9623 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9627 if (info
->eh_frame_hdr
9628 && !info
->relocatable
9629 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9636 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9639 const char *name
, *p
;
9640 struct bfd_section_already_linked
*l
;
9641 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9644 /* A single member comdat group section may be discarded by a
9645 linkonce section. See below. */
9646 if (sec
->output_section
== bfd_abs_section_ptr
)
9651 /* Check if it belongs to a section group. */
9652 group
= elf_sec_group (sec
);
9654 /* Return if it isn't a linkonce section nor a member of a group. A
9655 comdat group section also has SEC_LINK_ONCE set. */
9656 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9661 /* If this is the member of a single member comdat group, check if
9662 the group should be discarded. */
9663 if (elf_next_in_group (sec
) == sec
9664 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9670 /* FIXME: When doing a relocatable link, we may have trouble
9671 copying relocations in other sections that refer to local symbols
9672 in the section being discarded. Those relocations will have to
9673 be converted somehow; as of this writing I'm not sure that any of
9674 the backends handle that correctly.
9676 It is tempting to instead not discard link once sections when
9677 doing a relocatable link (technically, they should be discarded
9678 whenever we are building constructors). However, that fails,
9679 because the linker winds up combining all the link once sections
9680 into a single large link once section, which defeats the purpose
9681 of having link once sections in the first place.
9683 Also, not merging link once sections in a relocatable link
9684 causes trouble for MIPS ELF, which relies on link once semantics
9685 to handle the .reginfo section correctly. */
9687 name
= bfd_get_section_name (abfd
, sec
);
9689 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9690 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9695 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9697 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9699 /* We may have 3 different sections on the list: group section,
9700 comdat section and linkonce section. SEC may be a linkonce or
9701 group section. We match a group section with a group section,
9702 a linkonce section with a linkonce section, and ignore comdat
9704 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9705 && strcmp (name
, l
->sec
->name
) == 0
9706 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9708 /* The section has already been linked. See if we should
9710 switch (flags
& SEC_LINK_DUPLICATES
)
9715 case SEC_LINK_DUPLICATES_DISCARD
:
9718 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9719 (*_bfd_error_handler
)
9720 (_("%B: ignoring duplicate section `%A'"),
9724 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9725 if (sec
->size
!= l
->sec
->size
)
9726 (*_bfd_error_handler
)
9727 (_("%B: duplicate section `%A' has different size"),
9731 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9732 if (sec
->size
!= l
->sec
->size
)
9733 (*_bfd_error_handler
)
9734 (_("%B: duplicate section `%A' has different size"),
9736 else if (sec
->size
!= 0)
9738 bfd_byte
*sec_contents
, *l_sec_contents
;
9740 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9741 (*_bfd_error_handler
)
9742 (_("%B: warning: could not read contents of section `%A'"),
9744 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9746 (*_bfd_error_handler
)
9747 (_("%B: warning: could not read contents of section `%A'"),
9748 l
->sec
->owner
, l
->sec
);
9749 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9750 (*_bfd_error_handler
)
9751 (_("%B: warning: duplicate section `%A' has different contents"),
9755 free (sec_contents
);
9757 free (l_sec_contents
);
9762 /* Set the output_section field so that lang_add_section
9763 does not create a lang_input_section structure for this
9764 section. Since there might be a symbol in the section
9765 being discarded, we must retain a pointer to the section
9766 which we are really going to use. */
9767 sec
->output_section
= bfd_abs_section_ptr
;
9768 sec
->kept_section
= l
->sec
;
9770 if (flags
& SEC_GROUP
)
9772 asection
*first
= elf_next_in_group (sec
);
9773 asection
*s
= first
;
9777 s
->output_section
= bfd_abs_section_ptr
;
9778 /* Record which group discards it. */
9779 s
->kept_section
= l
->sec
;
9780 s
= elf_next_in_group (s
);
9781 /* These lists are circular. */
9793 /* If this is the member of a single member comdat group and the
9794 group hasn't be discarded, we check if it matches a linkonce
9795 section. We only record the discarded comdat group. Otherwise
9796 the undiscarded group will be discarded incorrectly later since
9797 itself has been recorded. */
9798 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9799 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9800 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9801 && bfd_elf_match_symbols_in_sections (l
->sec
,
9802 elf_next_in_group (sec
)))
9804 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9805 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9806 group
->output_section
= bfd_abs_section_ptr
;
9813 /* There is no direct match. But for linkonce section, we should
9814 check if there is a match with comdat group member. We always
9815 record the linkonce section, discarded or not. */
9816 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9817 if (l
->sec
->flags
& SEC_GROUP
)
9819 asection
*first
= elf_next_in_group (l
->sec
);
9822 && elf_next_in_group (first
) == first
9823 && bfd_elf_match_symbols_in_sections (first
, sec
))
9825 sec
->output_section
= bfd_abs_section_ptr
;
9826 sec
->kept_section
= l
->sec
;
9831 /* This is the first section with this name. Record it. */
9832 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9836 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9838 return sym
->st_shndx
== SHN_COMMON
;
9842 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9848 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9850 return bfd_com_section_ptr
;