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
;
3460 struct elf_link_hash_entry
*h
;
3462 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3464 h
= (struct elf_link_hash_entry
*) p
;
3465 entsize
+= htab
->root
.table
.entsize
;
3466 if (h
->root
.type
== bfd_link_hash_warning
)
3467 entsize
+= htab
->root
.table
.entsize
;
3471 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3472 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3473 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3474 if (old_tab
== NULL
)
3475 goto error_free_vers
;
3477 /* Remember the current objalloc pointer, so that all mem for
3478 symbols added can later be reclaimed. */
3479 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3480 if (alloc_mark
== NULL
)
3481 goto error_free_vers
;
3483 /* Clone the symbol table and sym hashes. Remember some
3484 pointers into the symbol table, and dynamic symbol count. */
3485 old_hash
= (char *) old_tab
+ tabsize
;
3486 old_ent
= (char *) old_hash
+ hashsize
;
3487 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3488 memcpy (old_hash
, sym_hash
, hashsize
);
3489 old_undefs
= htab
->root
.undefs
;
3490 old_undefs_tail
= htab
->root
.undefs_tail
;
3491 old_dynsymcount
= htab
->dynsymcount
;
3493 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3495 struct bfd_hash_entry
*p
;
3496 struct elf_link_hash_entry
*h
;
3498 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3500 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3501 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3502 h
= (struct elf_link_hash_entry
*) p
;
3503 if (h
->root
.type
== bfd_link_hash_warning
)
3505 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3506 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3513 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3514 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3516 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3520 asection
*sec
, *new_sec
;
3523 struct elf_link_hash_entry
*h
;
3524 bfd_boolean definition
;
3525 bfd_boolean size_change_ok
;
3526 bfd_boolean type_change_ok
;
3527 bfd_boolean new_weakdef
;
3528 bfd_boolean override
;
3530 unsigned int old_alignment
;
3535 flags
= BSF_NO_FLAGS
;
3537 value
= isym
->st_value
;
3539 common
= bed
->common_definition (isym
);
3541 bind
= ELF_ST_BIND (isym
->st_info
);
3542 if (bind
== STB_LOCAL
)
3544 /* This should be impossible, since ELF requires that all
3545 global symbols follow all local symbols, and that sh_info
3546 point to the first global symbol. Unfortunately, Irix 5
3550 else if (bind
== STB_GLOBAL
)
3552 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3555 else if (bind
== STB_WEAK
)
3559 /* Leave it up to the processor backend. */
3562 if (isym
->st_shndx
== SHN_UNDEF
)
3563 sec
= bfd_und_section_ptr
;
3564 else if (isym
->st_shndx
< SHN_LORESERVE
3565 || isym
->st_shndx
> SHN_HIRESERVE
)
3567 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3569 sec
= bfd_abs_section_ptr
;
3570 else if (sec
->kept_section
)
3572 /* Symbols from discarded section are undefined, and have
3573 default visibility. */
3574 sec
= bfd_und_section_ptr
;
3575 isym
->st_shndx
= SHN_UNDEF
;
3576 isym
->st_other
= (STV_DEFAULT
3577 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3579 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3582 else if (isym
->st_shndx
== SHN_ABS
)
3583 sec
= bfd_abs_section_ptr
;
3584 else if (isym
->st_shndx
== SHN_COMMON
)
3586 sec
= bfd_com_section_ptr
;
3587 /* What ELF calls the size we call the value. What ELF
3588 calls the value we call the alignment. */
3589 value
= isym
->st_size
;
3593 /* Leave it up to the processor backend. */
3596 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3599 goto error_free_vers
;
3601 if (isym
->st_shndx
== SHN_COMMON
3602 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3604 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3608 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3611 | SEC_LINKER_CREATED
3612 | SEC_THREAD_LOCAL
));
3614 goto error_free_vers
;
3618 else if (bed
->elf_add_symbol_hook
)
3620 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3622 goto error_free_vers
;
3624 /* The hook function sets the name to NULL if this symbol
3625 should be skipped for some reason. */
3630 /* Sanity check that all possibilities were handled. */
3633 bfd_set_error (bfd_error_bad_value
);
3634 goto error_free_vers
;
3637 if (bfd_is_und_section (sec
)
3638 || bfd_is_com_section (sec
))
3643 size_change_ok
= FALSE
;
3644 type_change_ok
= bed
->type_change_ok
;
3649 if (is_elf_hash_table (htab
))
3651 Elf_Internal_Versym iver
;
3652 unsigned int vernum
= 0;
3657 if (info
->default_imported_symver
)
3658 /* Use the default symbol version created earlier. */
3659 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3664 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3666 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3668 /* If this is a hidden symbol, or if it is not version
3669 1, we append the version name to the symbol name.
3670 However, we do not modify a non-hidden absolute symbol
3671 if it is not a function, because it might be the version
3672 symbol itself. FIXME: What if it isn't? */
3673 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3674 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3675 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3678 size_t namelen
, verlen
, newlen
;
3681 if (isym
->st_shndx
!= SHN_UNDEF
)
3683 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3685 else if (vernum
> 1)
3687 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3693 (*_bfd_error_handler
)
3694 (_("%B: %s: invalid version %u (max %d)"),
3696 elf_tdata (abfd
)->cverdefs
);
3697 bfd_set_error (bfd_error_bad_value
);
3698 goto error_free_vers
;
3703 /* We cannot simply test for the number of
3704 entries in the VERNEED section since the
3705 numbers for the needed versions do not start
3707 Elf_Internal_Verneed
*t
;
3710 for (t
= elf_tdata (abfd
)->verref
;
3714 Elf_Internal_Vernaux
*a
;
3716 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3718 if (a
->vna_other
== vernum
)
3720 verstr
= a
->vna_nodename
;
3729 (*_bfd_error_handler
)
3730 (_("%B: %s: invalid needed version %d"),
3731 abfd
, name
, vernum
);
3732 bfd_set_error (bfd_error_bad_value
);
3733 goto error_free_vers
;
3737 namelen
= strlen (name
);
3738 verlen
= strlen (verstr
);
3739 newlen
= namelen
+ verlen
+ 2;
3740 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3741 && isym
->st_shndx
!= SHN_UNDEF
)
3744 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3745 if (newname
== NULL
)
3746 goto error_free_vers
;
3747 memcpy (newname
, name
, namelen
);
3748 p
= newname
+ namelen
;
3750 /* If this is a defined non-hidden version symbol,
3751 we add another @ to the name. This indicates the
3752 default version of the symbol. */
3753 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3754 && isym
->st_shndx
!= SHN_UNDEF
)
3756 memcpy (p
, verstr
, verlen
+ 1);
3761 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3762 &value
, &old_alignment
,
3763 sym_hash
, &skip
, &override
,
3764 &type_change_ok
, &size_change_ok
))
3765 goto error_free_vers
;
3774 while (h
->root
.type
== bfd_link_hash_indirect
3775 || h
->root
.type
== bfd_link_hash_warning
)
3776 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3778 /* Remember the old alignment if this is a common symbol, so
3779 that we don't reduce the alignment later on. We can't
3780 check later, because _bfd_generic_link_add_one_symbol
3781 will set a default for the alignment which we want to
3782 override. We also remember the old bfd where the existing
3783 definition comes from. */
3784 switch (h
->root
.type
)
3789 case bfd_link_hash_defined
:
3790 case bfd_link_hash_defweak
:
3791 old_bfd
= h
->root
.u
.def
.section
->owner
;
3794 case bfd_link_hash_common
:
3795 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3796 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3800 if (elf_tdata (abfd
)->verdef
!= NULL
3804 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3807 if (! (_bfd_generic_link_add_one_symbol
3808 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3809 (struct bfd_link_hash_entry
**) sym_hash
)))
3810 goto error_free_vers
;
3813 while (h
->root
.type
== bfd_link_hash_indirect
3814 || h
->root
.type
== bfd_link_hash_warning
)
3815 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3818 new_weakdef
= FALSE
;
3821 && (flags
& BSF_WEAK
) != 0
3822 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3823 && is_elf_hash_table (htab
)
3824 && h
->u
.weakdef
== NULL
)
3826 /* Keep a list of all weak defined non function symbols from
3827 a dynamic object, using the weakdef field. Later in this
3828 function we will set the weakdef field to the correct
3829 value. We only put non-function symbols from dynamic
3830 objects on this list, because that happens to be the only
3831 time we need to know the normal symbol corresponding to a
3832 weak symbol, and the information is time consuming to
3833 figure out. If the weakdef field is not already NULL,
3834 then this symbol was already defined by some previous
3835 dynamic object, and we will be using that previous
3836 definition anyhow. */
3838 h
->u
.weakdef
= weaks
;
3843 /* Set the alignment of a common symbol. */
3844 if ((common
|| bfd_is_com_section (sec
))
3845 && h
->root
.type
== bfd_link_hash_common
)
3850 align
= bfd_log2 (isym
->st_value
);
3853 /* The new symbol is a common symbol in a shared object.
3854 We need to get the alignment from the section. */
3855 align
= new_sec
->alignment_power
;
3857 if (align
> old_alignment
3858 /* Permit an alignment power of zero if an alignment of one
3859 is specified and no other alignments have been specified. */
3860 || (isym
->st_value
== 1 && old_alignment
== 0))
3861 h
->root
.u
.c
.p
->alignment_power
= align
;
3863 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3866 if (is_elf_hash_table (htab
))
3870 /* Check the alignment when a common symbol is involved. This
3871 can change when a common symbol is overridden by a normal
3872 definition or a common symbol is ignored due to the old
3873 normal definition. We need to make sure the maximum
3874 alignment is maintained. */
3875 if ((old_alignment
|| common
)
3876 && h
->root
.type
!= bfd_link_hash_common
)
3878 unsigned int common_align
;
3879 unsigned int normal_align
;
3880 unsigned int symbol_align
;
3884 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3885 if (h
->root
.u
.def
.section
->owner
!= NULL
3886 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3888 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3889 if (normal_align
> symbol_align
)
3890 normal_align
= symbol_align
;
3893 normal_align
= symbol_align
;
3897 common_align
= old_alignment
;
3898 common_bfd
= old_bfd
;
3903 common_align
= bfd_log2 (isym
->st_value
);
3905 normal_bfd
= old_bfd
;
3908 if (normal_align
< common_align
)
3909 (*_bfd_error_handler
)
3910 (_("Warning: alignment %u of symbol `%s' in %B"
3911 " is smaller than %u in %B"),
3912 normal_bfd
, common_bfd
,
3913 1 << normal_align
, name
, 1 << common_align
);
3916 /* Remember the symbol size and type. */
3917 if (isym
->st_size
!= 0
3918 && (definition
|| h
->size
== 0))
3920 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3921 (*_bfd_error_handler
)
3922 (_("Warning: size of symbol `%s' changed"
3923 " from %lu in %B to %lu in %B"),
3925 name
, (unsigned long) h
->size
,
3926 (unsigned long) isym
->st_size
);
3928 h
->size
= isym
->st_size
;
3931 /* If this is a common symbol, then we always want H->SIZE
3932 to be the size of the common symbol. The code just above
3933 won't fix the size if a common symbol becomes larger. We
3934 don't warn about a size change here, because that is
3935 covered by --warn-common. */
3936 if (h
->root
.type
== bfd_link_hash_common
)
3937 h
->size
= h
->root
.u
.c
.size
;
3939 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3940 && (definition
|| h
->type
== STT_NOTYPE
))
3942 if (h
->type
!= STT_NOTYPE
3943 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3944 && ! type_change_ok
)
3945 (*_bfd_error_handler
)
3946 (_("Warning: type of symbol `%s' changed"
3947 " from %d to %d in %B"),
3948 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3950 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3953 /* If st_other has a processor-specific meaning, specific
3954 code might be needed here. We never merge the visibility
3955 attribute with the one from a dynamic object. */
3956 if (bed
->elf_backend_merge_symbol_attribute
)
3957 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3960 /* If this symbol has default visibility and the user has requested
3961 we not re-export it, then mark it as hidden. */
3962 if (definition
&& !dynamic
3964 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3965 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3966 isym
->st_other
= (STV_HIDDEN
3967 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
3969 if (isym
->st_other
!= 0 && !dynamic
)
3971 unsigned char hvis
, symvis
, other
, nvis
;
3973 /* Take the balance of OTHER from the definition. */
3974 other
= (definition
? isym
->st_other
: h
->other
);
3975 other
&= ~ ELF_ST_VISIBILITY (-1);
3977 /* Combine visibilities, using the most constraining one. */
3978 hvis
= ELF_ST_VISIBILITY (h
->other
);
3979 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3985 nvis
= hvis
< symvis
? hvis
: symvis
;
3987 h
->other
= other
| nvis
;
3990 /* Set a flag in the hash table entry indicating the type of
3991 reference or definition we just found. Keep a count of
3992 the number of dynamic symbols we find. A dynamic symbol
3993 is one which is referenced or defined by both a regular
3994 object and a shared object. */
4001 if (bind
!= STB_WEAK
)
4002 h
->ref_regular_nonweak
= 1;
4006 if (! info
->executable
4019 || (h
->u
.weakdef
!= NULL
4021 && h
->u
.weakdef
->dynindx
!= -1))
4025 /* Check to see if we need to add an indirect symbol for
4026 the default name. */
4027 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4028 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4029 &sec
, &value
, &dynsym
,
4031 goto error_free_vers
;
4033 if (definition
&& !dynamic
)
4035 char *p
= strchr (name
, ELF_VER_CHR
);
4036 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4038 /* Queue non-default versions so that .symver x, x@FOO
4039 aliases can be checked. */
4042 amt
= ((isymend
- isym
+ 1)
4043 * sizeof (struct elf_link_hash_entry
*));
4044 nondeflt_vers
= bfd_malloc (amt
);
4046 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4050 if (dynsym
&& h
->dynindx
== -1)
4052 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4053 goto error_free_vers
;
4054 if (h
->u
.weakdef
!= NULL
4056 && h
->u
.weakdef
->dynindx
== -1)
4058 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4059 goto error_free_vers
;
4062 else if (dynsym
&& h
->dynindx
!= -1)
4063 /* If the symbol already has a dynamic index, but
4064 visibility says it should not be visible, turn it into
4066 switch (ELF_ST_VISIBILITY (h
->other
))
4070 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4081 const char *soname
= elf_dt_name (abfd
);
4083 /* A symbol from a library loaded via DT_NEEDED of some
4084 other library is referenced by a regular object.
4085 Add a DT_NEEDED entry for it. Issue an error if
4086 --no-add-needed is used. */
4087 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4089 (*_bfd_error_handler
)
4090 (_("%s: invalid DSO for symbol `%s' definition"),
4092 bfd_set_error (bfd_error_bad_value
);
4093 goto error_free_vers
;
4096 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4099 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4101 goto error_free_vers
;
4103 BFD_ASSERT (ret
== 0);
4108 if (extversym
!= NULL
)
4114 if (isymbuf
!= NULL
)
4120 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4124 /* Restore the symbol table. */
4125 old_hash
= (char *) old_tab
+ tabsize
;
4126 old_ent
= (char *) old_hash
+ hashsize
;
4127 sym_hash
= elf_sym_hashes (abfd
);
4128 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4129 memcpy (sym_hash
, old_hash
, hashsize
);
4130 htab
->root
.undefs
= old_undefs
;
4131 htab
->root
.undefs_tail
= old_undefs_tail
;
4132 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4134 struct bfd_hash_entry
*p
;
4135 struct elf_link_hash_entry
*h
;
4137 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4139 h
= (struct elf_link_hash_entry
*) p
;
4140 if (h
->root
.type
== bfd_link_hash_warning
)
4141 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4142 if (h
->dynindx
>= old_dynsymcount
)
4143 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4145 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4146 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4147 h
= (struct elf_link_hash_entry
*) p
;
4148 if (h
->root
.type
== bfd_link_hash_warning
)
4150 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4151 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4157 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4159 if (nondeflt_vers
!= NULL
)
4160 free (nondeflt_vers
);
4164 if (old_tab
!= NULL
)
4170 /* Now that all the symbols from this input file are created, handle
4171 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4172 if (nondeflt_vers
!= NULL
)
4174 bfd_size_type cnt
, symidx
;
4176 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4178 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4179 char *shortname
, *p
;
4181 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4183 || (h
->root
.type
!= bfd_link_hash_defined
4184 && h
->root
.type
!= bfd_link_hash_defweak
))
4187 amt
= p
- h
->root
.root
.string
;
4188 shortname
= bfd_malloc (amt
+ 1);
4189 memcpy (shortname
, h
->root
.root
.string
, amt
);
4190 shortname
[amt
] = '\0';
4192 hi
= (struct elf_link_hash_entry
*)
4193 bfd_link_hash_lookup (&htab
->root
, shortname
,
4194 FALSE
, FALSE
, FALSE
);
4196 && hi
->root
.type
== h
->root
.type
4197 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4198 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4200 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4201 hi
->root
.type
= bfd_link_hash_indirect
;
4202 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4203 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4204 sym_hash
= elf_sym_hashes (abfd
);
4206 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4207 if (sym_hash
[symidx
] == hi
)
4209 sym_hash
[symidx
] = h
;
4215 free (nondeflt_vers
);
4216 nondeflt_vers
= NULL
;
4219 /* Now set the weakdefs field correctly for all the weak defined
4220 symbols we found. The only way to do this is to search all the
4221 symbols. Since we only need the information for non functions in
4222 dynamic objects, that's the only time we actually put anything on
4223 the list WEAKS. We need this information so that if a regular
4224 object refers to a symbol defined weakly in a dynamic object, the
4225 real symbol in the dynamic object is also put in the dynamic
4226 symbols; we also must arrange for both symbols to point to the
4227 same memory location. We could handle the general case of symbol
4228 aliasing, but a general symbol alias can only be generated in
4229 assembler code, handling it correctly would be very time
4230 consuming, and other ELF linkers don't handle general aliasing
4234 struct elf_link_hash_entry
**hpp
;
4235 struct elf_link_hash_entry
**hppend
;
4236 struct elf_link_hash_entry
**sorted_sym_hash
;
4237 struct elf_link_hash_entry
*h
;
4240 /* Since we have to search the whole symbol list for each weak
4241 defined symbol, search time for N weak defined symbols will be
4242 O(N^2). Binary search will cut it down to O(NlogN). */
4243 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4244 sorted_sym_hash
= bfd_malloc (amt
);
4245 if (sorted_sym_hash
== NULL
)
4247 sym_hash
= sorted_sym_hash
;
4248 hpp
= elf_sym_hashes (abfd
);
4249 hppend
= hpp
+ extsymcount
;
4251 for (; hpp
< hppend
; hpp
++)
4255 && h
->root
.type
== bfd_link_hash_defined
4256 && h
->type
!= STT_FUNC
)
4264 qsort (sorted_sym_hash
, sym_count
,
4265 sizeof (struct elf_link_hash_entry
*),
4268 while (weaks
!= NULL
)
4270 struct elf_link_hash_entry
*hlook
;
4277 weaks
= hlook
->u
.weakdef
;
4278 hlook
->u
.weakdef
= NULL
;
4280 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4281 || hlook
->root
.type
== bfd_link_hash_defweak
4282 || hlook
->root
.type
== bfd_link_hash_common
4283 || hlook
->root
.type
== bfd_link_hash_indirect
);
4284 slook
= hlook
->root
.u
.def
.section
;
4285 vlook
= hlook
->root
.u
.def
.value
;
4292 bfd_signed_vma vdiff
;
4294 h
= sorted_sym_hash
[idx
];
4295 vdiff
= vlook
- h
->root
.u
.def
.value
;
4302 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4315 /* We didn't find a value/section match. */
4319 for (i
= ilook
; i
< sym_count
; i
++)
4321 h
= sorted_sym_hash
[i
];
4323 /* Stop if value or section doesn't match. */
4324 if (h
->root
.u
.def
.value
!= vlook
4325 || h
->root
.u
.def
.section
!= slook
)
4327 else if (h
!= hlook
)
4329 hlook
->u
.weakdef
= h
;
4331 /* If the weak definition is in the list of dynamic
4332 symbols, make sure the real definition is put
4334 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4336 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4340 /* If the real definition is in the list of dynamic
4341 symbols, make sure the weak definition is put
4342 there as well. If we don't do this, then the
4343 dynamic loader might not merge the entries for the
4344 real definition and the weak definition. */
4345 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4347 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4355 free (sorted_sym_hash
);
4358 if (bed
->check_directives
)
4359 (*bed
->check_directives
) (abfd
, info
);
4361 /* If this object is the same format as the output object, and it is
4362 not a shared library, then let the backend look through the
4365 This is required to build global offset table entries and to
4366 arrange for dynamic relocs. It is not required for the
4367 particular common case of linking non PIC code, even when linking
4368 against shared libraries, but unfortunately there is no way of
4369 knowing whether an object file has been compiled PIC or not.
4370 Looking through the relocs is not particularly time consuming.
4371 The problem is that we must either (1) keep the relocs in memory,
4372 which causes the linker to require additional runtime memory or
4373 (2) read the relocs twice from the input file, which wastes time.
4374 This would be a good case for using mmap.
4376 I have no idea how to handle linking PIC code into a file of a
4377 different format. It probably can't be done. */
4379 && is_elf_hash_table (htab
)
4380 && htab
->root
.creator
== abfd
->xvec
4381 && bed
->check_relocs
!= NULL
)
4385 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4387 Elf_Internal_Rela
*internal_relocs
;
4390 if ((o
->flags
& SEC_RELOC
) == 0
4391 || o
->reloc_count
== 0
4392 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4393 && (o
->flags
& SEC_DEBUGGING
) != 0)
4394 || bfd_is_abs_section (o
->output_section
))
4397 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4399 if (internal_relocs
== NULL
)
4402 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4404 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4405 free (internal_relocs
);
4412 /* If this is a non-traditional link, try to optimize the handling
4413 of the .stab/.stabstr sections. */
4415 && ! info
->traditional_format
4416 && is_elf_hash_table (htab
)
4417 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4421 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4422 if (stabstr
!= NULL
)
4424 bfd_size_type string_offset
= 0;
4427 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4428 if (strncmp (".stab", stab
->name
, 5) == 0
4429 && (!stab
->name
[5] ||
4430 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4431 && (stab
->flags
& SEC_MERGE
) == 0
4432 && !bfd_is_abs_section (stab
->output_section
))
4434 struct bfd_elf_section_data
*secdata
;
4436 secdata
= elf_section_data (stab
);
4437 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4438 stabstr
, &secdata
->sec_info
,
4441 if (secdata
->sec_info
)
4442 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4447 if (is_elf_hash_table (htab
) && add_needed
)
4449 /* Add this bfd to the loaded list. */
4450 struct elf_link_loaded_list
*n
;
4452 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4456 n
->next
= htab
->loaded
;
4463 if (old_tab
!= NULL
)
4465 if (nondeflt_vers
!= NULL
)
4466 free (nondeflt_vers
);
4467 if (extversym
!= NULL
)
4470 if (isymbuf
!= NULL
)
4476 /* Return the linker hash table entry of a symbol that might be
4477 satisfied by an archive symbol. Return -1 on error. */
4479 struct elf_link_hash_entry
*
4480 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4481 struct bfd_link_info
*info
,
4484 struct elf_link_hash_entry
*h
;
4488 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4492 /* If this is a default version (the name contains @@), look up the
4493 symbol again with only one `@' as well as without the version.
4494 The effect is that references to the symbol with and without the
4495 version will be matched by the default symbol in the archive. */
4497 p
= strchr (name
, ELF_VER_CHR
);
4498 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4501 /* First check with only one `@'. */
4502 len
= strlen (name
);
4503 copy
= bfd_alloc (abfd
, len
);
4505 return (struct elf_link_hash_entry
*) 0 - 1;
4507 first
= p
- name
+ 1;
4508 memcpy (copy
, name
, first
);
4509 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4511 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4514 /* We also need to check references to the symbol without the
4516 copy
[first
- 1] = '\0';
4517 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4518 FALSE
, FALSE
, FALSE
);
4521 bfd_release (abfd
, copy
);
4525 /* Add symbols from an ELF archive file to the linker hash table. We
4526 don't use _bfd_generic_link_add_archive_symbols because of a
4527 problem which arises on UnixWare. The UnixWare libc.so is an
4528 archive which includes an entry libc.so.1 which defines a bunch of
4529 symbols. The libc.so archive also includes a number of other
4530 object files, which also define symbols, some of which are the same
4531 as those defined in libc.so.1. Correct linking requires that we
4532 consider each object file in turn, and include it if it defines any
4533 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4534 this; it looks through the list of undefined symbols, and includes
4535 any object file which defines them. When this algorithm is used on
4536 UnixWare, it winds up pulling in libc.so.1 early and defining a
4537 bunch of symbols. This means that some of the other objects in the
4538 archive are not included in the link, which is incorrect since they
4539 precede libc.so.1 in the archive.
4541 Fortunately, ELF archive handling is simpler than that done by
4542 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4543 oddities. In ELF, if we find a symbol in the archive map, and the
4544 symbol is currently undefined, we know that we must pull in that
4547 Unfortunately, we do have to make multiple passes over the symbol
4548 table until nothing further is resolved. */
4551 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4554 bfd_boolean
*defined
= NULL
;
4555 bfd_boolean
*included
= NULL
;
4559 const struct elf_backend_data
*bed
;
4560 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4561 (bfd
*, struct bfd_link_info
*, const char *);
4563 if (! bfd_has_map (abfd
))
4565 /* An empty archive is a special case. */
4566 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4568 bfd_set_error (bfd_error_no_armap
);
4572 /* Keep track of all symbols we know to be already defined, and all
4573 files we know to be already included. This is to speed up the
4574 second and subsequent passes. */
4575 c
= bfd_ardata (abfd
)->symdef_count
;
4579 amt
*= sizeof (bfd_boolean
);
4580 defined
= bfd_zmalloc (amt
);
4581 included
= bfd_zmalloc (amt
);
4582 if (defined
== NULL
|| included
== NULL
)
4585 symdefs
= bfd_ardata (abfd
)->symdefs
;
4586 bed
= get_elf_backend_data (abfd
);
4587 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4600 symdefend
= symdef
+ c
;
4601 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4603 struct elf_link_hash_entry
*h
;
4605 struct bfd_link_hash_entry
*undefs_tail
;
4608 if (defined
[i
] || included
[i
])
4610 if (symdef
->file_offset
== last
)
4616 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4617 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4623 if (h
->root
.type
== bfd_link_hash_common
)
4625 /* We currently have a common symbol. The archive map contains
4626 a reference to this symbol, so we may want to include it. We
4627 only want to include it however, if this archive element
4628 contains a definition of the symbol, not just another common
4631 Unfortunately some archivers (including GNU ar) will put
4632 declarations of common symbols into their archive maps, as
4633 well as real definitions, so we cannot just go by the archive
4634 map alone. Instead we must read in the element's symbol
4635 table and check that to see what kind of symbol definition
4637 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4640 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4642 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4647 /* We need to include this archive member. */
4648 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4649 if (element
== NULL
)
4652 if (! bfd_check_format (element
, bfd_object
))
4655 /* Doublecheck that we have not included this object
4656 already--it should be impossible, but there may be
4657 something wrong with the archive. */
4658 if (element
->archive_pass
!= 0)
4660 bfd_set_error (bfd_error_bad_value
);
4663 element
->archive_pass
= 1;
4665 undefs_tail
= info
->hash
->undefs_tail
;
4667 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4670 if (! bfd_link_add_symbols (element
, info
))
4673 /* If there are any new undefined symbols, we need to make
4674 another pass through the archive in order to see whether
4675 they can be defined. FIXME: This isn't perfect, because
4676 common symbols wind up on undefs_tail and because an
4677 undefined symbol which is defined later on in this pass
4678 does not require another pass. This isn't a bug, but it
4679 does make the code less efficient than it could be. */
4680 if (undefs_tail
!= info
->hash
->undefs_tail
)
4683 /* Look backward to mark all symbols from this object file
4684 which we have already seen in this pass. */
4688 included
[mark
] = TRUE
;
4693 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4695 /* We mark subsequent symbols from this object file as we go
4696 on through the loop. */
4697 last
= symdef
->file_offset
;
4708 if (defined
!= NULL
)
4710 if (included
!= NULL
)
4715 /* Given an ELF BFD, add symbols to the global hash table as
4719 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4721 switch (bfd_get_format (abfd
))
4724 return elf_link_add_object_symbols (abfd
, info
);
4726 return elf_link_add_archive_symbols (abfd
, info
);
4728 bfd_set_error (bfd_error_wrong_format
);
4733 /* This function will be called though elf_link_hash_traverse to store
4734 all hash value of the exported symbols in an array. */
4737 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4739 unsigned long **valuep
= data
;
4745 if (h
->root
.type
== bfd_link_hash_warning
)
4746 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4748 /* Ignore indirect symbols. These are added by the versioning code. */
4749 if (h
->dynindx
== -1)
4752 name
= h
->root
.root
.string
;
4753 p
= strchr (name
, ELF_VER_CHR
);
4756 alc
= bfd_malloc (p
- name
+ 1);
4757 memcpy (alc
, name
, p
- name
);
4758 alc
[p
- name
] = '\0';
4762 /* Compute the hash value. */
4763 ha
= bfd_elf_hash (name
);
4765 /* Store the found hash value in the array given as the argument. */
4768 /* And store it in the struct so that we can put it in the hash table
4770 h
->u
.elf_hash_value
= ha
;
4778 /* Array used to determine the number of hash table buckets to use
4779 based on the number of symbols there are. If there are fewer than
4780 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4781 fewer than 37 we use 17 buckets, and so forth. We never use more
4782 than 32771 buckets. */
4784 static const size_t elf_buckets
[] =
4786 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4790 /* Compute bucket count for hashing table. We do not use a static set
4791 of possible tables sizes anymore. Instead we determine for all
4792 possible reasonable sizes of the table the outcome (i.e., the
4793 number of collisions etc) and choose the best solution. The
4794 weighting functions are not too simple to allow the table to grow
4795 without bounds. Instead one of the weighting factors is the size.
4796 Therefore the result is always a good payoff between few collisions
4797 (= short chain lengths) and table size. */
4799 compute_bucket_count (struct bfd_link_info
*info
)
4801 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4802 size_t best_size
= 0;
4803 unsigned long int *hashcodes
;
4804 unsigned long int *hashcodesp
;
4805 unsigned long int i
;
4808 /* Compute the hash values for all exported symbols. At the same
4809 time store the values in an array so that we could use them for
4812 amt
*= sizeof (unsigned long int);
4813 hashcodes
= bfd_malloc (amt
);
4814 if (hashcodes
== NULL
)
4816 hashcodesp
= hashcodes
;
4818 /* Put all hash values in HASHCODES. */
4819 elf_link_hash_traverse (elf_hash_table (info
),
4820 elf_collect_hash_codes
, &hashcodesp
);
4822 /* We have a problem here. The following code to optimize the table
4823 size requires an integer type with more the 32 bits. If
4824 BFD_HOST_U_64_BIT is set we know about such a type. */
4825 #ifdef BFD_HOST_U_64_BIT
4828 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4831 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4832 unsigned long int *counts
;
4833 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4834 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4836 /* Possible optimization parameters: if we have NSYMS symbols we say
4837 that the hashing table must at least have NSYMS/4 and at most
4839 minsize
= nsyms
/ 4;
4842 best_size
= maxsize
= nsyms
* 2;
4844 /* Create array where we count the collisions in. We must use bfd_malloc
4845 since the size could be large. */
4847 amt
*= sizeof (unsigned long int);
4848 counts
= bfd_malloc (amt
);
4855 /* Compute the "optimal" size for the hash table. The criteria is a
4856 minimal chain length. The minor criteria is (of course) the size
4858 for (i
= minsize
; i
< maxsize
; ++i
)
4860 /* Walk through the array of hashcodes and count the collisions. */
4861 BFD_HOST_U_64_BIT max
;
4862 unsigned long int j
;
4863 unsigned long int fact
;
4865 memset (counts
, '\0', i
* sizeof (unsigned long int));
4867 /* Determine how often each hash bucket is used. */
4868 for (j
= 0; j
< nsyms
; ++j
)
4869 ++counts
[hashcodes
[j
] % i
];
4871 /* For the weight function we need some information about the
4872 pagesize on the target. This is information need not be 100%
4873 accurate. Since this information is not available (so far) we
4874 define it here to a reasonable default value. If it is crucial
4875 to have a better value some day simply define this value. */
4876 # ifndef BFD_TARGET_PAGESIZE
4877 # define BFD_TARGET_PAGESIZE (4096)
4880 /* We in any case need 2 + NSYMS entries for the size values and
4882 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4885 /* Variant 1: optimize for short chains. We add the squares
4886 of all the chain lengths (which favors many small chain
4887 over a few long chains). */
4888 for (j
= 0; j
< i
; ++j
)
4889 max
+= counts
[j
] * counts
[j
];
4891 /* This adds penalties for the overall size of the table. */
4892 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4895 /* Variant 2: Optimize a lot more for small table. Here we
4896 also add squares of the size but we also add penalties for
4897 empty slots (the +1 term). */
4898 for (j
= 0; j
< i
; ++j
)
4899 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4901 /* The overall size of the table is considered, but not as
4902 strong as in variant 1, where it is squared. */
4903 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4907 /* Compare with current best results. */
4908 if (max
< best_chlen
)
4918 #endif /* defined (BFD_HOST_U_64_BIT) */
4920 /* This is the fallback solution if no 64bit type is available or if we
4921 are not supposed to spend much time on optimizations. We select the
4922 bucket count using a fixed set of numbers. */
4923 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4925 best_size
= elf_buckets
[i
];
4926 if (dynsymcount
< elf_buckets
[i
+ 1])
4931 /* Free the arrays we needed. */
4937 /* Set up the sizes and contents of the ELF dynamic sections. This is
4938 called by the ELF linker emulation before_allocation routine. We
4939 must set the sizes of the sections before the linker sets the
4940 addresses of the various sections. */
4943 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4946 const char *filter_shlib
,
4947 const char * const *auxiliary_filters
,
4948 struct bfd_link_info
*info
,
4949 asection
**sinterpptr
,
4950 struct bfd_elf_version_tree
*verdefs
)
4952 bfd_size_type soname_indx
;
4954 const struct elf_backend_data
*bed
;
4955 struct elf_assign_sym_version_info asvinfo
;
4959 soname_indx
= (bfd_size_type
) -1;
4961 if (!is_elf_hash_table (info
->hash
))
4964 elf_tdata (output_bfd
)->relro
= info
->relro
;
4965 if (info
->execstack
)
4966 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4967 else if (info
->noexecstack
)
4968 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4972 asection
*notesec
= NULL
;
4975 for (inputobj
= info
->input_bfds
;
4977 inputobj
= inputobj
->link_next
)
4981 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4983 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4986 if (s
->flags
& SEC_CODE
)
4995 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4996 if (exec
&& info
->relocatable
4997 && notesec
->output_section
!= bfd_abs_section_ptr
)
4998 notesec
->output_section
->flags
|= SEC_CODE
;
5002 /* Any syms created from now on start with -1 in
5003 got.refcount/offset and plt.refcount/offset. */
5004 elf_hash_table (info
)->init_got_refcount
5005 = elf_hash_table (info
)->init_got_offset
;
5006 elf_hash_table (info
)->init_plt_refcount
5007 = elf_hash_table (info
)->init_plt_offset
;
5009 /* The backend may have to create some sections regardless of whether
5010 we're dynamic or not. */
5011 bed
= get_elf_backend_data (output_bfd
);
5012 if (bed
->elf_backend_always_size_sections
5013 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5016 dynobj
= elf_hash_table (info
)->dynobj
;
5018 /* If there were no dynamic objects in the link, there is nothing to
5023 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5026 if (elf_hash_table (info
)->dynamic_sections_created
)
5028 struct elf_info_failed eif
;
5029 struct elf_link_hash_entry
*h
;
5031 struct bfd_elf_version_tree
*t
;
5032 struct bfd_elf_version_expr
*d
;
5034 bfd_boolean all_defined
;
5036 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5037 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5041 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5043 if (soname_indx
== (bfd_size_type
) -1
5044 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5050 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5052 info
->flags
|= DF_SYMBOLIC
;
5059 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5061 if (indx
== (bfd_size_type
) -1
5062 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5065 if (info
->new_dtags
)
5067 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5068 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5073 if (filter_shlib
!= NULL
)
5077 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5078 filter_shlib
, TRUE
);
5079 if (indx
== (bfd_size_type
) -1
5080 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5084 if (auxiliary_filters
!= NULL
)
5086 const char * const *p
;
5088 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5092 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5094 if (indx
== (bfd_size_type
) -1
5095 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5101 eif
.verdefs
= verdefs
;
5104 /* If we are supposed to export all symbols into the dynamic symbol
5105 table (this is not the normal case), then do so. */
5106 if (info
->export_dynamic
)
5108 elf_link_hash_traverse (elf_hash_table (info
),
5109 _bfd_elf_export_symbol
,
5115 /* Make all global versions with definition. */
5116 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5117 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5118 if (!d
->symver
&& d
->symbol
)
5120 const char *verstr
, *name
;
5121 size_t namelen
, verlen
, newlen
;
5123 struct elf_link_hash_entry
*newh
;
5126 namelen
= strlen (name
);
5128 verlen
= strlen (verstr
);
5129 newlen
= namelen
+ verlen
+ 3;
5131 newname
= bfd_malloc (newlen
);
5132 if (newname
== NULL
)
5134 memcpy (newname
, name
, namelen
);
5136 /* Check the hidden versioned definition. */
5137 p
= newname
+ namelen
;
5139 memcpy (p
, verstr
, verlen
+ 1);
5140 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5141 newname
, FALSE
, FALSE
,
5144 || (newh
->root
.type
!= bfd_link_hash_defined
5145 && newh
->root
.type
!= bfd_link_hash_defweak
))
5147 /* Check the default versioned definition. */
5149 memcpy (p
, verstr
, verlen
+ 1);
5150 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5151 newname
, FALSE
, FALSE
,
5156 /* Mark this version if there is a definition and it is
5157 not defined in a shared object. */
5159 && !newh
->def_dynamic
5160 && (newh
->root
.type
== bfd_link_hash_defined
5161 || newh
->root
.type
== bfd_link_hash_defweak
))
5165 /* Attach all the symbols to their version information. */
5166 asvinfo
.output_bfd
= output_bfd
;
5167 asvinfo
.info
= info
;
5168 asvinfo
.verdefs
= verdefs
;
5169 asvinfo
.failed
= FALSE
;
5171 elf_link_hash_traverse (elf_hash_table (info
),
5172 _bfd_elf_link_assign_sym_version
,
5177 if (!info
->allow_undefined_version
)
5179 /* Check if all global versions have a definition. */
5181 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5182 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5183 if (!d
->symver
&& !d
->script
)
5185 (*_bfd_error_handler
)
5186 (_("%s: undefined version: %s"),
5187 d
->pattern
, t
->name
);
5188 all_defined
= FALSE
;
5193 bfd_set_error (bfd_error_bad_value
);
5198 /* Find all symbols which were defined in a dynamic object and make
5199 the backend pick a reasonable value for them. */
5200 elf_link_hash_traverse (elf_hash_table (info
),
5201 _bfd_elf_adjust_dynamic_symbol
,
5206 /* Add some entries to the .dynamic section. We fill in some of the
5207 values later, in bfd_elf_final_link, but we must add the entries
5208 now so that we know the final size of the .dynamic section. */
5210 /* If there are initialization and/or finalization functions to
5211 call then add the corresponding DT_INIT/DT_FINI entries. */
5212 h
= (info
->init_function
5213 ? elf_link_hash_lookup (elf_hash_table (info
),
5214 info
->init_function
, FALSE
,
5221 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5224 h
= (info
->fini_function
5225 ? elf_link_hash_lookup (elf_hash_table (info
),
5226 info
->fini_function
, FALSE
,
5233 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5237 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5238 if (s
!= NULL
&& s
->linker_has_input
)
5240 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5241 if (! info
->executable
)
5246 for (sub
= info
->input_bfds
; sub
!= NULL
;
5247 sub
= sub
->link_next
)
5248 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5249 if (elf_section_data (o
)->this_hdr
.sh_type
5250 == SHT_PREINIT_ARRAY
)
5252 (*_bfd_error_handler
)
5253 (_("%B: .preinit_array section is not allowed in DSO"),
5258 bfd_set_error (bfd_error_nonrepresentable_section
);
5262 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5263 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5266 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5267 if (s
!= NULL
&& s
->linker_has_input
)
5269 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5270 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5273 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5274 if (s
!= NULL
&& s
->linker_has_input
)
5276 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5277 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5281 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5282 /* If .dynstr is excluded from the link, we don't want any of
5283 these tags. Strictly, we should be checking each section
5284 individually; This quick check covers for the case where
5285 someone does a /DISCARD/ : { *(*) }. */
5286 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5288 bfd_size_type strsize
;
5290 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5291 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5292 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5293 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5294 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5295 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5296 bed
->s
->sizeof_sym
))
5301 /* The backend must work out the sizes of all the other dynamic
5303 if (bed
->elf_backend_size_dynamic_sections
5304 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5307 if (elf_hash_table (info
)->dynamic_sections_created
)
5309 unsigned long section_sym_count
;
5312 /* Set up the version definition section. */
5313 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5314 BFD_ASSERT (s
!= NULL
);
5316 /* We may have created additional version definitions if we are
5317 just linking a regular application. */
5318 verdefs
= asvinfo
.verdefs
;
5320 /* Skip anonymous version tag. */
5321 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5322 verdefs
= verdefs
->next
;
5324 if (verdefs
== NULL
&& !info
->create_default_symver
)
5325 s
->flags
|= SEC_EXCLUDE
;
5330 struct bfd_elf_version_tree
*t
;
5332 Elf_Internal_Verdef def
;
5333 Elf_Internal_Verdaux defaux
;
5334 struct bfd_link_hash_entry
*bh
;
5335 struct elf_link_hash_entry
*h
;
5341 /* Make space for the base version. */
5342 size
+= sizeof (Elf_External_Verdef
);
5343 size
+= sizeof (Elf_External_Verdaux
);
5346 /* Make space for the default version. */
5347 if (info
->create_default_symver
)
5349 size
+= sizeof (Elf_External_Verdef
);
5353 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5355 struct bfd_elf_version_deps
*n
;
5357 size
+= sizeof (Elf_External_Verdef
);
5358 size
+= sizeof (Elf_External_Verdaux
);
5361 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5362 size
+= sizeof (Elf_External_Verdaux
);
5366 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5367 if (s
->contents
== NULL
&& s
->size
!= 0)
5370 /* Fill in the version definition section. */
5374 def
.vd_version
= VER_DEF_CURRENT
;
5375 def
.vd_flags
= VER_FLG_BASE
;
5378 if (info
->create_default_symver
)
5380 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5381 def
.vd_next
= sizeof (Elf_External_Verdef
);
5385 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5386 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5387 + sizeof (Elf_External_Verdaux
));
5390 if (soname_indx
!= (bfd_size_type
) -1)
5392 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5394 def
.vd_hash
= bfd_elf_hash (soname
);
5395 defaux
.vda_name
= soname_indx
;
5402 name
= lbasename (output_bfd
->filename
);
5403 def
.vd_hash
= bfd_elf_hash (name
);
5404 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5406 if (indx
== (bfd_size_type
) -1)
5408 defaux
.vda_name
= indx
;
5410 defaux
.vda_next
= 0;
5412 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5413 (Elf_External_Verdef
*) p
);
5414 p
+= sizeof (Elf_External_Verdef
);
5415 if (info
->create_default_symver
)
5417 /* Add a symbol representing this version. */
5419 if (! (_bfd_generic_link_add_one_symbol
5420 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5422 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5424 h
= (struct elf_link_hash_entry
*) bh
;
5427 h
->type
= STT_OBJECT
;
5428 h
->verinfo
.vertree
= NULL
;
5430 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5433 /* Create a duplicate of the base version with the same
5434 aux block, but different flags. */
5437 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5439 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5440 + sizeof (Elf_External_Verdaux
));
5443 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5444 (Elf_External_Verdef
*) p
);
5445 p
+= sizeof (Elf_External_Verdef
);
5447 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5448 (Elf_External_Verdaux
*) p
);
5449 p
+= sizeof (Elf_External_Verdaux
);
5451 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5454 struct bfd_elf_version_deps
*n
;
5457 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5460 /* Add a symbol representing this version. */
5462 if (! (_bfd_generic_link_add_one_symbol
5463 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5465 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5467 h
= (struct elf_link_hash_entry
*) bh
;
5470 h
->type
= STT_OBJECT
;
5471 h
->verinfo
.vertree
= t
;
5473 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5476 def
.vd_version
= VER_DEF_CURRENT
;
5478 if (t
->globals
.list
== NULL
5479 && t
->locals
.list
== NULL
5481 def
.vd_flags
|= VER_FLG_WEAK
;
5482 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5483 def
.vd_cnt
= cdeps
+ 1;
5484 def
.vd_hash
= bfd_elf_hash (t
->name
);
5485 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5487 if (t
->next
!= NULL
)
5488 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5489 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5491 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5492 (Elf_External_Verdef
*) p
);
5493 p
+= sizeof (Elf_External_Verdef
);
5495 defaux
.vda_name
= h
->dynstr_index
;
5496 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5498 defaux
.vda_next
= 0;
5499 if (t
->deps
!= NULL
)
5500 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5501 t
->name_indx
= defaux
.vda_name
;
5503 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5504 (Elf_External_Verdaux
*) p
);
5505 p
+= sizeof (Elf_External_Verdaux
);
5507 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5509 if (n
->version_needed
== NULL
)
5511 /* This can happen if there was an error in the
5513 defaux
.vda_name
= 0;
5517 defaux
.vda_name
= n
->version_needed
->name_indx
;
5518 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5521 if (n
->next
== NULL
)
5522 defaux
.vda_next
= 0;
5524 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5526 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5527 (Elf_External_Verdaux
*) p
);
5528 p
+= sizeof (Elf_External_Verdaux
);
5532 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5533 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5536 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5539 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5541 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5544 else if (info
->flags
& DF_BIND_NOW
)
5546 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5552 if (info
->executable
)
5553 info
->flags_1
&= ~ (DF_1_INITFIRST
5556 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5560 /* Work out the size of the version reference section. */
5562 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5563 BFD_ASSERT (s
!= NULL
);
5565 struct elf_find_verdep_info sinfo
;
5567 sinfo
.output_bfd
= output_bfd
;
5569 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5570 if (sinfo
.vers
== 0)
5572 sinfo
.failed
= FALSE
;
5574 elf_link_hash_traverse (elf_hash_table (info
),
5575 _bfd_elf_link_find_version_dependencies
,
5578 if (elf_tdata (output_bfd
)->verref
== NULL
)
5579 s
->flags
|= SEC_EXCLUDE
;
5582 Elf_Internal_Verneed
*t
;
5587 /* Build the version definition section. */
5590 for (t
= elf_tdata (output_bfd
)->verref
;
5594 Elf_Internal_Vernaux
*a
;
5596 size
+= sizeof (Elf_External_Verneed
);
5598 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5599 size
+= sizeof (Elf_External_Vernaux
);
5603 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5604 if (s
->contents
== NULL
)
5608 for (t
= elf_tdata (output_bfd
)->verref
;
5613 Elf_Internal_Vernaux
*a
;
5617 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5620 t
->vn_version
= VER_NEED_CURRENT
;
5622 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5623 elf_dt_name (t
->vn_bfd
) != NULL
5624 ? elf_dt_name (t
->vn_bfd
)
5625 : lbasename (t
->vn_bfd
->filename
),
5627 if (indx
== (bfd_size_type
) -1)
5630 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5631 if (t
->vn_nextref
== NULL
)
5634 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5635 + caux
* sizeof (Elf_External_Vernaux
));
5637 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5638 (Elf_External_Verneed
*) p
);
5639 p
+= sizeof (Elf_External_Verneed
);
5641 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5643 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5644 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5645 a
->vna_nodename
, FALSE
);
5646 if (indx
== (bfd_size_type
) -1)
5649 if (a
->vna_nextptr
== NULL
)
5652 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5654 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5655 (Elf_External_Vernaux
*) p
);
5656 p
+= sizeof (Elf_External_Vernaux
);
5660 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5661 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5664 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5668 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5669 && elf_tdata (output_bfd
)->cverdefs
== 0)
5670 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5671 §ion_sym_count
) == 0)
5673 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5674 s
->flags
|= SEC_EXCLUDE
;
5681 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5683 if (!is_elf_hash_table (info
->hash
))
5686 if (elf_hash_table (info
)->dynamic_sections_created
)
5689 const struct elf_backend_data
*bed
;
5691 bfd_size_type dynsymcount
;
5692 unsigned long section_sym_count
;
5693 size_t bucketcount
= 0;
5694 size_t hash_entry_size
;
5695 unsigned int dtagcount
;
5697 dynobj
= elf_hash_table (info
)->dynobj
;
5699 /* Assign dynsym indicies. In a shared library we generate a
5700 section symbol for each output section, which come first.
5701 Next come all of the back-end allocated local dynamic syms,
5702 followed by the rest of the global symbols. */
5704 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5705 §ion_sym_count
);
5707 /* Work out the size of the symbol version section. */
5708 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5709 BFD_ASSERT (s
!= NULL
);
5710 if (dynsymcount
!= 0
5711 && (s
->flags
& SEC_EXCLUDE
) == 0)
5713 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5714 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5715 if (s
->contents
== NULL
)
5718 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5722 /* Set the size of the .dynsym and .hash sections. We counted
5723 the number of dynamic symbols in elf_link_add_object_symbols.
5724 We will build the contents of .dynsym and .hash when we build
5725 the final symbol table, because until then we do not know the
5726 correct value to give the symbols. We built the .dynstr
5727 section as we went along in elf_link_add_object_symbols. */
5728 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5729 BFD_ASSERT (s
!= NULL
);
5730 bed
= get_elf_backend_data (output_bfd
);
5731 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5733 if (dynsymcount
!= 0)
5735 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5736 if (s
->contents
== NULL
)
5739 /* The first entry in .dynsym is a dummy symbol.
5740 Clear all the section syms, in case we don't output them all. */
5741 ++section_sym_count
;
5742 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5745 /* Compute the size of the hashing table. As a side effect this
5746 computes the hash values for all the names we export. */
5747 bucketcount
= compute_bucket_count (info
);
5749 s
= bfd_get_section_by_name (dynobj
, ".hash");
5750 BFD_ASSERT (s
!= NULL
);
5751 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5752 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5753 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5754 if (s
->contents
== NULL
)
5757 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5758 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5759 s
->contents
+ hash_entry_size
);
5761 elf_hash_table (info
)->bucketcount
= bucketcount
;
5763 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5764 BFD_ASSERT (s
!= NULL
);
5766 elf_finalize_dynstr (output_bfd
, info
);
5768 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5770 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5771 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5778 /* Final phase of ELF linker. */
5780 /* A structure we use to avoid passing large numbers of arguments. */
5782 struct elf_final_link_info
5784 /* General link information. */
5785 struct bfd_link_info
*info
;
5788 /* Symbol string table. */
5789 struct bfd_strtab_hash
*symstrtab
;
5790 /* .dynsym section. */
5791 asection
*dynsym_sec
;
5792 /* .hash section. */
5794 /* symbol version section (.gnu.version). */
5795 asection
*symver_sec
;
5796 /* Buffer large enough to hold contents of any section. */
5798 /* Buffer large enough to hold external relocs of any section. */
5799 void *external_relocs
;
5800 /* Buffer large enough to hold internal relocs of any section. */
5801 Elf_Internal_Rela
*internal_relocs
;
5802 /* Buffer large enough to hold external local symbols of any input
5804 bfd_byte
*external_syms
;
5805 /* And a buffer for symbol section indices. */
5806 Elf_External_Sym_Shndx
*locsym_shndx
;
5807 /* Buffer large enough to hold internal local symbols of any input
5809 Elf_Internal_Sym
*internal_syms
;
5810 /* Array large enough to hold a symbol index for each local symbol
5811 of any input BFD. */
5813 /* Array large enough to hold a section pointer for each local
5814 symbol of any input BFD. */
5815 asection
**sections
;
5816 /* Buffer to hold swapped out symbols. */
5818 /* And one for symbol section indices. */
5819 Elf_External_Sym_Shndx
*symshndxbuf
;
5820 /* Number of swapped out symbols in buffer. */
5821 size_t symbuf_count
;
5822 /* Number of symbols which fit in symbuf. */
5824 /* And same for symshndxbuf. */
5825 size_t shndxbuf_size
;
5828 /* This struct is used to pass information to elf_link_output_extsym. */
5830 struct elf_outext_info
5833 bfd_boolean localsyms
;
5834 struct elf_final_link_info
*finfo
;
5837 /* When performing a relocatable link, the input relocations are
5838 preserved. But, if they reference global symbols, the indices
5839 referenced must be updated. Update all the relocations in
5840 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5843 elf_link_adjust_relocs (bfd
*abfd
,
5844 Elf_Internal_Shdr
*rel_hdr
,
5846 struct elf_link_hash_entry
**rel_hash
)
5849 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5851 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5852 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5853 bfd_vma r_type_mask
;
5856 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5858 swap_in
= bed
->s
->swap_reloc_in
;
5859 swap_out
= bed
->s
->swap_reloc_out
;
5861 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5863 swap_in
= bed
->s
->swap_reloca_in
;
5864 swap_out
= bed
->s
->swap_reloca_out
;
5869 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5872 if (bed
->s
->arch_size
== 32)
5879 r_type_mask
= 0xffffffff;
5883 erela
= rel_hdr
->contents
;
5884 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5886 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5889 if (*rel_hash
== NULL
)
5892 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5894 (*swap_in
) (abfd
, erela
, irela
);
5895 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5896 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5897 | (irela
[j
].r_info
& r_type_mask
));
5898 (*swap_out
) (abfd
, irela
, erela
);
5902 struct elf_link_sort_rela
5908 enum elf_reloc_type_class type
;
5909 /* We use this as an array of size int_rels_per_ext_rel. */
5910 Elf_Internal_Rela rela
[1];
5914 elf_link_sort_cmp1 (const void *A
, const void *B
)
5916 const struct elf_link_sort_rela
*a
= A
;
5917 const struct elf_link_sort_rela
*b
= B
;
5918 int relativea
, relativeb
;
5920 relativea
= a
->type
== reloc_class_relative
;
5921 relativeb
= b
->type
== reloc_class_relative
;
5923 if (relativea
< relativeb
)
5925 if (relativea
> relativeb
)
5927 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5929 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5931 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5933 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5939 elf_link_sort_cmp2 (const void *A
, const void *B
)
5941 const struct elf_link_sort_rela
*a
= A
;
5942 const struct elf_link_sort_rela
*b
= B
;
5945 if (a
->u
.offset
< b
->u
.offset
)
5947 if (a
->u
.offset
> b
->u
.offset
)
5949 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5950 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5955 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5957 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5963 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5966 bfd_size_type count
, size
;
5967 size_t i
, ret
, sort_elt
, ext_size
;
5968 bfd_byte
*sort
, *s_non_relative
, *p
;
5969 struct elf_link_sort_rela
*sq
;
5970 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5971 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5972 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5973 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5974 struct bfd_link_order
*lo
;
5977 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5978 if (reldyn
== NULL
|| reldyn
->size
== 0)
5980 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5981 if (reldyn
== NULL
|| reldyn
->size
== 0)
5983 ext_size
= bed
->s
->sizeof_rel
;
5984 swap_in
= bed
->s
->swap_reloc_in
;
5985 swap_out
= bed
->s
->swap_reloc_out
;
5989 ext_size
= bed
->s
->sizeof_rela
;
5990 swap_in
= bed
->s
->swap_reloca_in
;
5991 swap_out
= bed
->s
->swap_reloca_out
;
5993 count
= reldyn
->size
/ ext_size
;
5996 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5997 if (lo
->type
== bfd_indirect_link_order
)
5999 asection
*o
= lo
->u
.indirect
.section
;
6003 if (size
!= reldyn
->size
)
6006 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6007 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6008 sort
= bfd_zmalloc (sort_elt
* count
);
6011 (*info
->callbacks
->warning
)
6012 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6016 if (bed
->s
->arch_size
== 32)
6017 r_sym_mask
= ~(bfd_vma
) 0xff;
6019 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6021 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6022 if (lo
->type
== bfd_indirect_link_order
)
6024 bfd_byte
*erel
, *erelend
;
6025 asection
*o
= lo
->u
.indirect
.section
;
6027 if (o
->contents
== NULL
&& o
->size
!= 0)
6029 /* This is a reloc section that is being handled as a normal
6030 section. See bfd_section_from_shdr. We can't combine
6031 relocs in this case. */
6036 erelend
= o
->contents
+ o
->size
;
6037 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6038 while (erel
< erelend
)
6040 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6041 (*swap_in
) (abfd
, erel
, s
->rela
);
6042 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6043 s
->u
.sym_mask
= r_sym_mask
;
6049 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6051 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6053 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6054 if (s
->type
!= reloc_class_relative
)
6060 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6061 for (; i
< count
; i
++, p
+= sort_elt
)
6063 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6064 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6066 sp
->u
.offset
= sq
->rela
->r_offset
;
6069 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6071 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6072 if (lo
->type
== bfd_indirect_link_order
)
6074 bfd_byte
*erel
, *erelend
;
6075 asection
*o
= lo
->u
.indirect
.section
;
6078 erelend
= o
->contents
+ o
->size
;
6079 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6080 while (erel
< erelend
)
6082 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6083 (*swap_out
) (abfd
, s
->rela
, erel
);
6094 /* Flush the output symbols to the file. */
6097 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6098 const struct elf_backend_data
*bed
)
6100 if (finfo
->symbuf_count
> 0)
6102 Elf_Internal_Shdr
*hdr
;
6106 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6107 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6108 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6109 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6110 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6113 hdr
->sh_size
+= amt
;
6114 finfo
->symbuf_count
= 0;
6120 /* Add a symbol to the output symbol table. */
6123 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6125 Elf_Internal_Sym
*elfsym
,
6126 asection
*input_sec
,
6127 struct elf_link_hash_entry
*h
)
6130 Elf_External_Sym_Shndx
*destshndx
;
6131 bfd_boolean (*output_symbol_hook
)
6132 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6133 struct elf_link_hash_entry
*);
6134 const struct elf_backend_data
*bed
;
6136 bed
= get_elf_backend_data (finfo
->output_bfd
);
6137 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6138 if (output_symbol_hook
!= NULL
)
6140 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6144 if (name
== NULL
|| *name
== '\0')
6145 elfsym
->st_name
= 0;
6146 else if (input_sec
->flags
& SEC_EXCLUDE
)
6147 elfsym
->st_name
= 0;
6150 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6152 if (elfsym
->st_name
== (unsigned long) -1)
6156 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6158 if (! elf_link_flush_output_syms (finfo
, bed
))
6162 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6163 destshndx
= finfo
->symshndxbuf
;
6164 if (destshndx
!= NULL
)
6166 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6170 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6171 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6172 if (destshndx
== NULL
)
6174 memset ((char *) destshndx
+ amt
, 0, amt
);
6175 finfo
->shndxbuf_size
*= 2;
6177 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6180 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6181 finfo
->symbuf_count
+= 1;
6182 bfd_get_symcount (finfo
->output_bfd
) += 1;
6187 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6188 allowing an unsatisfied unversioned symbol in the DSO to match a
6189 versioned symbol that would normally require an explicit version.
6190 We also handle the case that a DSO references a hidden symbol
6191 which may be satisfied by a versioned symbol in another DSO. */
6194 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6195 const struct elf_backend_data
*bed
,
6196 struct elf_link_hash_entry
*h
)
6199 struct elf_link_loaded_list
*loaded
;
6201 if (!is_elf_hash_table (info
->hash
))
6204 switch (h
->root
.type
)
6210 case bfd_link_hash_undefined
:
6211 case bfd_link_hash_undefweak
:
6212 abfd
= h
->root
.u
.undef
.abfd
;
6213 if ((abfd
->flags
& DYNAMIC
) == 0
6214 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6218 case bfd_link_hash_defined
:
6219 case bfd_link_hash_defweak
:
6220 abfd
= h
->root
.u
.def
.section
->owner
;
6223 case bfd_link_hash_common
:
6224 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6227 BFD_ASSERT (abfd
!= NULL
);
6229 for (loaded
= elf_hash_table (info
)->loaded
;
6231 loaded
= loaded
->next
)
6234 Elf_Internal_Shdr
*hdr
;
6235 bfd_size_type symcount
;
6236 bfd_size_type extsymcount
;
6237 bfd_size_type extsymoff
;
6238 Elf_Internal_Shdr
*versymhdr
;
6239 Elf_Internal_Sym
*isym
;
6240 Elf_Internal_Sym
*isymend
;
6241 Elf_Internal_Sym
*isymbuf
;
6242 Elf_External_Versym
*ever
;
6243 Elf_External_Versym
*extversym
;
6245 input
= loaded
->abfd
;
6247 /* We check each DSO for a possible hidden versioned definition. */
6249 || (input
->flags
& DYNAMIC
) == 0
6250 || elf_dynversym (input
) == 0)
6253 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6255 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6256 if (elf_bad_symtab (input
))
6258 extsymcount
= symcount
;
6263 extsymcount
= symcount
- hdr
->sh_info
;
6264 extsymoff
= hdr
->sh_info
;
6267 if (extsymcount
== 0)
6270 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6272 if (isymbuf
== NULL
)
6275 /* Read in any version definitions. */
6276 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6277 extversym
= bfd_malloc (versymhdr
->sh_size
);
6278 if (extversym
== NULL
)
6281 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6282 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6283 != versymhdr
->sh_size
))
6291 ever
= extversym
+ extsymoff
;
6292 isymend
= isymbuf
+ extsymcount
;
6293 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6296 Elf_Internal_Versym iver
;
6297 unsigned short version_index
;
6299 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6300 || isym
->st_shndx
== SHN_UNDEF
)
6303 name
= bfd_elf_string_from_elf_section (input
,
6306 if (strcmp (name
, h
->root
.root
.string
) != 0)
6309 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6311 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6313 /* If we have a non-hidden versioned sym, then it should
6314 have provided a definition for the undefined sym. */
6318 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6319 if (version_index
== 1 || version_index
== 2)
6321 /* This is the base or first version. We can use it. */
6335 /* Add an external symbol to the symbol table. This is called from
6336 the hash table traversal routine. When generating a shared object,
6337 we go through the symbol table twice. The first time we output
6338 anything that might have been forced to local scope in a version
6339 script. The second time we output the symbols that are still
6343 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6345 struct elf_outext_info
*eoinfo
= data
;
6346 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6348 Elf_Internal_Sym sym
;
6349 asection
*input_sec
;
6350 const struct elf_backend_data
*bed
;
6352 if (h
->root
.type
== bfd_link_hash_warning
)
6354 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6355 if (h
->root
.type
== bfd_link_hash_new
)
6359 /* Decide whether to output this symbol in this pass. */
6360 if (eoinfo
->localsyms
)
6362 if (!h
->forced_local
)
6367 if (h
->forced_local
)
6371 bed
= get_elf_backend_data (finfo
->output_bfd
);
6373 if (h
->root
.type
== bfd_link_hash_undefined
)
6375 /* If we have an undefined symbol reference here then it must have
6376 come from a shared library that is being linked in. (Undefined
6377 references in regular files have already been handled). */
6378 bfd_boolean ignore_undef
= FALSE
;
6380 /* Some symbols may be special in that the fact that they're
6381 undefined can be safely ignored - let backend determine that. */
6382 if (bed
->elf_backend_ignore_undef_symbol
)
6383 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6385 /* If we are reporting errors for this situation then do so now. */
6386 if (ignore_undef
== FALSE
6389 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6390 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6392 if (! (finfo
->info
->callbacks
->undefined_symbol
6393 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6394 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6396 eoinfo
->failed
= TRUE
;
6402 /* We should also warn if a forced local symbol is referenced from
6403 shared libraries. */
6404 if (! finfo
->info
->relocatable
6405 && (! finfo
->info
->shared
)
6410 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6412 (*_bfd_error_handler
)
6413 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6415 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6416 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6417 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6419 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6420 ? "hidden" : "local",
6421 h
->root
.root
.string
);
6422 eoinfo
->failed
= TRUE
;
6426 /* We don't want to output symbols that have never been mentioned by
6427 a regular file, or that we have been told to strip. However, if
6428 h->indx is set to -2, the symbol is used by a reloc and we must
6432 else if ((h
->def_dynamic
6434 || h
->root
.type
== bfd_link_hash_new
)
6438 else if (finfo
->info
->strip
== strip_all
)
6440 else if (finfo
->info
->strip
== strip_some
6441 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6442 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6444 else if (finfo
->info
->strip_discarded
6445 && (h
->root
.type
== bfd_link_hash_defined
6446 || h
->root
.type
== bfd_link_hash_defweak
)
6447 && elf_discarded_section (h
->root
.u
.def
.section
))
6452 /* If we're stripping it, and it's not a dynamic symbol, there's
6453 nothing else to do unless it is a forced local symbol. */
6456 && !h
->forced_local
)
6460 sym
.st_size
= h
->size
;
6461 sym
.st_other
= h
->other
;
6462 if (h
->forced_local
)
6463 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6464 else if (h
->root
.type
== bfd_link_hash_undefweak
6465 || h
->root
.type
== bfd_link_hash_defweak
)
6466 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6468 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6470 switch (h
->root
.type
)
6473 case bfd_link_hash_new
:
6474 case bfd_link_hash_warning
:
6478 case bfd_link_hash_undefined
:
6479 case bfd_link_hash_undefweak
:
6480 input_sec
= bfd_und_section_ptr
;
6481 sym
.st_shndx
= SHN_UNDEF
;
6484 case bfd_link_hash_defined
:
6485 case bfd_link_hash_defweak
:
6487 input_sec
= h
->root
.u
.def
.section
;
6488 if (input_sec
->output_section
!= NULL
)
6491 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6492 input_sec
->output_section
);
6493 if (sym
.st_shndx
== SHN_BAD
)
6495 (*_bfd_error_handler
)
6496 (_("%B: could not find output section %A for input section %A"),
6497 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6498 eoinfo
->failed
= TRUE
;
6502 /* ELF symbols in relocatable files are section relative,
6503 but in nonrelocatable files they are virtual
6505 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6506 if (! finfo
->info
->relocatable
)
6508 sym
.st_value
+= input_sec
->output_section
->vma
;
6509 if (h
->type
== STT_TLS
)
6511 /* STT_TLS symbols are relative to PT_TLS segment
6513 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6514 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6520 BFD_ASSERT (input_sec
->owner
== NULL
6521 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6522 sym
.st_shndx
= SHN_UNDEF
;
6523 input_sec
= bfd_und_section_ptr
;
6528 case bfd_link_hash_common
:
6529 input_sec
= h
->root
.u
.c
.p
->section
;
6530 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6531 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6534 case bfd_link_hash_indirect
:
6535 /* These symbols are created by symbol versioning. They point
6536 to the decorated version of the name. For example, if the
6537 symbol foo@@GNU_1.2 is the default, which should be used when
6538 foo is used with no version, then we add an indirect symbol
6539 foo which points to foo@@GNU_1.2. We ignore these symbols,
6540 since the indirected symbol is already in the hash table. */
6544 /* Give the processor backend a chance to tweak the symbol value,
6545 and also to finish up anything that needs to be done for this
6546 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6547 forced local syms when non-shared is due to a historical quirk. */
6548 if ((h
->dynindx
!= -1
6550 && ((finfo
->info
->shared
6551 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6552 || h
->root
.type
!= bfd_link_hash_undefweak
))
6553 || !h
->forced_local
)
6554 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6556 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6557 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6559 eoinfo
->failed
= TRUE
;
6564 /* If we are marking the symbol as undefined, and there are no
6565 non-weak references to this symbol from a regular object, then
6566 mark the symbol as weak undefined; if there are non-weak
6567 references, mark the symbol as strong. We can't do this earlier,
6568 because it might not be marked as undefined until the
6569 finish_dynamic_symbol routine gets through with it. */
6570 if (sym
.st_shndx
== SHN_UNDEF
6572 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6573 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6577 if (h
->ref_regular_nonweak
)
6578 bindtype
= STB_GLOBAL
;
6580 bindtype
= STB_WEAK
;
6581 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6584 /* If a non-weak symbol with non-default visibility is not defined
6585 locally, it is a fatal error. */
6586 if (! finfo
->info
->relocatable
6587 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6588 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6589 && h
->root
.type
== bfd_link_hash_undefined
6592 (*_bfd_error_handler
)
6593 (_("%B: %s symbol `%s' isn't defined"),
6595 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6597 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6598 ? "internal" : "hidden",
6599 h
->root
.root
.string
);
6600 eoinfo
->failed
= TRUE
;
6604 /* If this symbol should be put in the .dynsym section, then put it
6605 there now. We already know the symbol index. We also fill in
6606 the entry in the .hash section. */
6607 if (h
->dynindx
!= -1
6608 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6612 size_t hash_entry_size
;
6613 bfd_byte
*bucketpos
;
6617 sym
.st_name
= h
->dynstr_index
;
6618 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6619 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6621 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6622 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6624 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6625 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6626 + (bucket
+ 2) * hash_entry_size
);
6627 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6628 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6629 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6630 ((bfd_byte
*) finfo
->hash_sec
->contents
6631 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6633 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6635 Elf_Internal_Versym iversym
;
6636 Elf_External_Versym
*eversym
;
6638 if (!h
->def_regular
)
6640 if (h
->verinfo
.verdef
== NULL
)
6641 iversym
.vs_vers
= 0;
6643 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6647 if (h
->verinfo
.vertree
== NULL
)
6648 iversym
.vs_vers
= 1;
6650 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6651 if (finfo
->info
->create_default_symver
)
6656 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6658 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6659 eversym
+= h
->dynindx
;
6660 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6664 /* If we're stripping it, then it was just a dynamic symbol, and
6665 there's nothing else to do. */
6666 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6669 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6671 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6673 eoinfo
->failed
= TRUE
;
6680 /* Return TRUE if special handling is done for relocs in SEC against
6681 symbols defined in discarded sections. */
6684 elf_section_ignore_discarded_relocs (asection
*sec
)
6686 const struct elf_backend_data
*bed
;
6688 switch (sec
->sec_info_type
)
6690 case ELF_INFO_TYPE_STABS
:
6691 case ELF_INFO_TYPE_EH_FRAME
:
6697 bed
= get_elf_backend_data (sec
->owner
);
6698 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6699 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6705 /* Return a mask saying how ld should treat relocations in SEC against
6706 symbols defined in discarded sections. If this function returns
6707 COMPLAIN set, ld will issue a warning message. If this function
6708 returns PRETEND set, and the discarded section was link-once and the
6709 same size as the kept link-once section, ld will pretend that the
6710 symbol was actually defined in the kept section. Otherwise ld will
6711 zero the reloc (at least that is the intent, but some cooperation by
6712 the target dependent code is needed, particularly for REL targets). */
6715 _bfd_elf_default_action_discarded (asection
*sec
)
6717 if (sec
->flags
& SEC_DEBUGGING
)
6720 if (strcmp (".eh_frame", sec
->name
) == 0)
6723 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6726 return COMPLAIN
| PRETEND
;
6729 /* Find a match between a section and a member of a section group. */
6732 match_group_member (asection
*sec
, asection
*group
)
6734 asection
*first
= elf_next_in_group (group
);
6735 asection
*s
= first
;
6739 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6749 /* Check if the kept section of a discarded section SEC can be used
6750 to replace it. Return the replacement if it is OK. Otherwise return
6754 _bfd_elf_check_kept_section (asection
*sec
)
6758 kept
= sec
->kept_section
;
6761 if (elf_sec_group (sec
) != NULL
)
6762 kept
= match_group_member (sec
, kept
);
6763 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6769 /* Link an input file into the linker output file. This function
6770 handles all the sections and relocations of the input file at once.
6771 This is so that we only have to read the local symbols once, and
6772 don't have to keep them in memory. */
6775 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6777 bfd_boolean (*relocate_section
)
6778 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6779 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6781 Elf_Internal_Shdr
*symtab_hdr
;
6784 Elf_Internal_Sym
*isymbuf
;
6785 Elf_Internal_Sym
*isym
;
6786 Elf_Internal_Sym
*isymend
;
6788 asection
**ppsection
;
6790 const struct elf_backend_data
*bed
;
6791 bfd_boolean emit_relocs
;
6792 struct elf_link_hash_entry
**sym_hashes
;
6794 output_bfd
= finfo
->output_bfd
;
6795 bed
= get_elf_backend_data (output_bfd
);
6796 relocate_section
= bed
->elf_backend_relocate_section
;
6798 /* If this is a dynamic object, we don't want to do anything here:
6799 we don't want the local symbols, and we don't want the section
6801 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6804 emit_relocs
= (finfo
->info
->relocatable
6805 || finfo
->info
->emitrelocations
);
6807 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6808 if (elf_bad_symtab (input_bfd
))
6810 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6815 locsymcount
= symtab_hdr
->sh_info
;
6816 extsymoff
= symtab_hdr
->sh_info
;
6819 /* Read the local symbols. */
6820 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6821 if (isymbuf
== NULL
&& locsymcount
!= 0)
6823 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6824 finfo
->internal_syms
,
6825 finfo
->external_syms
,
6826 finfo
->locsym_shndx
);
6827 if (isymbuf
== NULL
)
6831 /* Find local symbol sections and adjust values of symbols in
6832 SEC_MERGE sections. Write out those local symbols we know are
6833 going into the output file. */
6834 isymend
= isymbuf
+ locsymcount
;
6835 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6837 isym
++, pindex
++, ppsection
++)
6841 Elf_Internal_Sym osym
;
6845 if (elf_bad_symtab (input_bfd
))
6847 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6854 if (isym
->st_shndx
== SHN_UNDEF
)
6855 isec
= bfd_und_section_ptr
;
6856 else if (isym
->st_shndx
< SHN_LORESERVE
6857 || isym
->st_shndx
> SHN_HIRESERVE
)
6859 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6861 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6862 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6864 _bfd_merged_section_offset (output_bfd
, &isec
,
6865 elf_section_data (isec
)->sec_info
,
6868 else if (isym
->st_shndx
== SHN_ABS
)
6869 isec
= bfd_abs_section_ptr
;
6870 else if (isym
->st_shndx
== SHN_COMMON
)
6871 isec
= bfd_com_section_ptr
;
6874 /* Don't attempt to output symbols with st_shnx in the
6875 reserved range other than SHN_ABS and SHN_COMMON. */
6882 /* Don't output the first, undefined, symbol. */
6883 if (ppsection
== finfo
->sections
)
6886 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6888 /* We never output section symbols. Instead, we use the
6889 section symbol of the corresponding section in the output
6894 /* If we are stripping all symbols, we don't want to output this
6896 if (finfo
->info
->strip
== strip_all
)
6899 /* If we are discarding all local symbols, we don't want to
6900 output this one. If we are generating a relocatable output
6901 file, then some of the local symbols may be required by
6902 relocs; we output them below as we discover that they are
6904 if (finfo
->info
->discard
== discard_all
)
6907 /* If this symbol is defined in a section which we are
6908 discarding, we don't need to keep it. */
6909 if (isym
->st_shndx
!= SHN_UNDEF
6910 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6912 || bfd_section_removed_from_list (output_bfd
,
6913 isec
->output_section
)))
6916 /* Get the name of the symbol. */
6917 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6922 /* See if we are discarding symbols with this name. */
6923 if ((finfo
->info
->strip
== strip_some
6924 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6926 || (((finfo
->info
->discard
== discard_sec_merge
6927 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6928 || finfo
->info
->discard
== discard_l
)
6929 && bfd_is_local_label_name (input_bfd
, name
)))
6932 /* If we get here, we are going to output this symbol. */
6936 /* Adjust the section index for the output file. */
6937 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6938 isec
->output_section
);
6939 if (osym
.st_shndx
== SHN_BAD
)
6942 *pindex
= bfd_get_symcount (output_bfd
);
6944 /* ELF symbols in relocatable files are section relative, but
6945 in executable files they are virtual addresses. Note that
6946 this code assumes that all ELF sections have an associated
6947 BFD section with a reasonable value for output_offset; below
6948 we assume that they also have a reasonable value for
6949 output_section. Any special sections must be set up to meet
6950 these requirements. */
6951 osym
.st_value
+= isec
->output_offset
;
6952 if (! finfo
->info
->relocatable
)
6954 osym
.st_value
+= isec
->output_section
->vma
;
6955 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6957 /* STT_TLS symbols are relative to PT_TLS segment base. */
6958 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6959 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6963 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6967 /* Relocate the contents of each section. */
6968 sym_hashes
= elf_sym_hashes (input_bfd
);
6969 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6973 if (! o
->linker_mark
)
6975 /* This section was omitted from the link. */
6979 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6980 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6983 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6985 /* Section was created by _bfd_elf_link_create_dynamic_sections
6990 /* Get the contents of the section. They have been cached by a
6991 relaxation routine. Note that o is a section in an input
6992 file, so the contents field will not have been set by any of
6993 the routines which work on output files. */
6994 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6995 contents
= elf_section_data (o
)->this_hdr
.contents
;
6998 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
7000 contents
= finfo
->contents
;
7001 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
7005 if ((o
->flags
& SEC_RELOC
) != 0)
7007 Elf_Internal_Rela
*internal_relocs
;
7008 bfd_vma r_type_mask
;
7011 /* Get the swapped relocs. */
7013 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7014 finfo
->internal_relocs
, FALSE
);
7015 if (internal_relocs
== NULL
7016 && o
->reloc_count
> 0)
7019 if (bed
->s
->arch_size
== 32)
7026 r_type_mask
= 0xffffffff;
7030 /* Run through the relocs looking for any against symbols
7031 from discarded sections and section symbols from
7032 removed link-once sections. Complain about relocs
7033 against discarded sections. Zero relocs against removed
7034 link-once sections. */
7035 if (!elf_section_ignore_discarded_relocs (o
))
7037 Elf_Internal_Rela
*rel
, *relend
;
7038 unsigned int action
= (*bed
->action_discarded
) (o
);
7040 rel
= internal_relocs
;
7041 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7042 for ( ; rel
< relend
; rel
++)
7044 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7045 asection
**ps
, *sec
;
7046 struct elf_link_hash_entry
*h
= NULL
;
7047 const char *sym_name
;
7049 if (r_symndx
== STN_UNDEF
)
7052 if (r_symndx
>= locsymcount
7053 || (elf_bad_symtab (input_bfd
)
7054 && finfo
->sections
[r_symndx
] == NULL
))
7056 h
= sym_hashes
[r_symndx
- extsymoff
];
7058 /* Badly formatted input files can contain relocs that
7059 reference non-existant symbols. Check here so that
7060 we do not seg fault. */
7065 sprintf_vma (buffer
, rel
->r_info
);
7066 (*_bfd_error_handler
)
7067 (_("error: %B contains a reloc (0x%s) for section %A "
7068 "that references a non-existent global symbol"),
7069 input_bfd
, o
, buffer
);
7070 bfd_set_error (bfd_error_bad_value
);
7074 while (h
->root
.type
== bfd_link_hash_indirect
7075 || h
->root
.type
== bfd_link_hash_warning
)
7076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7078 if (h
->root
.type
!= bfd_link_hash_defined
7079 && h
->root
.type
!= bfd_link_hash_defweak
)
7082 ps
= &h
->root
.u
.def
.section
;
7083 sym_name
= h
->root
.root
.string
;
7087 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7088 ps
= &finfo
->sections
[r_symndx
];
7089 sym_name
= bfd_elf_sym_name (input_bfd
,
7094 /* Complain if the definition comes from a
7095 discarded section. */
7096 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7098 BFD_ASSERT (r_symndx
!= 0);
7099 if (action
& COMPLAIN
)
7100 (*finfo
->info
->callbacks
->einfo
)
7101 (_("%X`%s' referenced in section `%A' of %B: "
7102 "defined in discarded section `%A' of %B\n"),
7103 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7105 /* Try to do the best we can to support buggy old
7106 versions of gcc. Pretend that the symbol is
7107 really defined in the kept linkonce section.
7108 FIXME: This is quite broken. Modifying the
7109 symbol here means we will be changing all later
7110 uses of the symbol, not just in this section. */
7111 if (action
& PRETEND
)
7115 kept
= _bfd_elf_check_kept_section (sec
);
7123 /* Remove the symbol reference from the reloc, but
7124 don't kill the reloc completely. This is so that
7125 a zero value will be written into the section,
7126 which may have non-zero contents put there by the
7127 assembler. Zero in things like an eh_frame fde
7128 pc_begin allows stack unwinders to recognize the
7130 rel
->r_info
&= r_type_mask
;
7136 /* Relocate the section by invoking a back end routine.
7138 The back end routine is responsible for adjusting the
7139 section contents as necessary, and (if using Rela relocs
7140 and generating a relocatable output file) adjusting the
7141 reloc addend as necessary.
7143 The back end routine does not have to worry about setting
7144 the reloc address or the reloc symbol index.
7146 The back end routine is given a pointer to the swapped in
7147 internal symbols, and can access the hash table entries
7148 for the external symbols via elf_sym_hashes (input_bfd).
7150 When generating relocatable output, the back end routine
7151 must handle STB_LOCAL/STT_SECTION symbols specially. The
7152 output symbol is going to be a section symbol
7153 corresponding to the output section, which will require
7154 the addend to be adjusted. */
7156 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7157 input_bfd
, o
, contents
,
7165 Elf_Internal_Rela
*irela
;
7166 Elf_Internal_Rela
*irelaend
;
7167 bfd_vma last_offset
;
7168 struct elf_link_hash_entry
**rel_hash
;
7169 struct elf_link_hash_entry
**rel_hash_list
;
7170 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7171 unsigned int next_erel
;
7172 bfd_boolean rela_normal
;
7174 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7175 rela_normal
= (bed
->rela_normal
7176 && (input_rel_hdr
->sh_entsize
7177 == bed
->s
->sizeof_rela
));
7179 /* Adjust the reloc addresses and symbol indices. */
7181 irela
= internal_relocs
;
7182 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7183 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7184 + elf_section_data (o
->output_section
)->rel_count
7185 + elf_section_data (o
->output_section
)->rel_count2
);
7186 rel_hash_list
= rel_hash
;
7187 last_offset
= o
->output_offset
;
7188 if (!finfo
->info
->relocatable
)
7189 last_offset
+= o
->output_section
->vma
;
7190 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7192 unsigned long r_symndx
;
7194 Elf_Internal_Sym sym
;
7196 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7202 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7205 if (irela
->r_offset
>= (bfd_vma
) -2)
7207 /* This is a reloc for a deleted entry or somesuch.
7208 Turn it into an R_*_NONE reloc, at the same
7209 offset as the last reloc. elf_eh_frame.c and
7210 elf_bfd_discard_info rely on reloc offsets
7212 irela
->r_offset
= last_offset
;
7214 irela
->r_addend
= 0;
7218 irela
->r_offset
+= o
->output_offset
;
7220 /* Relocs in an executable have to be virtual addresses. */
7221 if (!finfo
->info
->relocatable
)
7222 irela
->r_offset
+= o
->output_section
->vma
;
7224 last_offset
= irela
->r_offset
;
7226 r_symndx
= irela
->r_info
>> r_sym_shift
;
7227 if (r_symndx
== STN_UNDEF
)
7230 if (r_symndx
>= locsymcount
7231 || (elf_bad_symtab (input_bfd
)
7232 && finfo
->sections
[r_symndx
] == NULL
))
7234 struct elf_link_hash_entry
*rh
;
7237 /* This is a reloc against a global symbol. We
7238 have not yet output all the local symbols, so
7239 we do not know the symbol index of any global
7240 symbol. We set the rel_hash entry for this
7241 reloc to point to the global hash table entry
7242 for this symbol. The symbol index is then
7243 set at the end of bfd_elf_final_link. */
7244 indx
= r_symndx
- extsymoff
;
7245 rh
= elf_sym_hashes (input_bfd
)[indx
];
7246 while (rh
->root
.type
== bfd_link_hash_indirect
7247 || rh
->root
.type
== bfd_link_hash_warning
)
7248 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7250 /* Setting the index to -2 tells
7251 elf_link_output_extsym that this symbol is
7253 BFD_ASSERT (rh
->indx
< 0);
7261 /* This is a reloc against a local symbol. */
7264 sym
= isymbuf
[r_symndx
];
7265 sec
= finfo
->sections
[r_symndx
];
7266 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7268 /* I suppose the backend ought to fill in the
7269 section of any STT_SECTION symbol against a
7270 processor specific section. */
7272 if (bfd_is_abs_section (sec
))
7274 else if (sec
== NULL
|| sec
->owner
== NULL
)
7276 bfd_set_error (bfd_error_bad_value
);
7281 asection
*osec
= sec
->output_section
;
7283 /* If we have discarded a section, the output
7284 section will be the absolute section. In
7285 case of discarded link-once and discarded
7286 SEC_MERGE sections, use the kept section. */
7287 if (bfd_is_abs_section (osec
)
7288 && sec
->kept_section
!= NULL
7289 && sec
->kept_section
->output_section
!= NULL
)
7291 osec
= sec
->kept_section
->output_section
;
7292 irela
->r_addend
-= osec
->vma
;
7295 if (!bfd_is_abs_section (osec
))
7297 r_symndx
= osec
->target_index
;
7298 BFD_ASSERT (r_symndx
!= 0);
7302 /* Adjust the addend according to where the
7303 section winds up in the output section. */
7305 irela
->r_addend
+= sec
->output_offset
;
7309 if (finfo
->indices
[r_symndx
] == -1)
7311 unsigned long shlink
;
7315 if (finfo
->info
->strip
== strip_all
)
7317 /* You can't do ld -r -s. */
7318 bfd_set_error (bfd_error_invalid_operation
);
7322 /* This symbol was skipped earlier, but
7323 since it is needed by a reloc, we
7324 must output it now. */
7325 shlink
= symtab_hdr
->sh_link
;
7326 name
= (bfd_elf_string_from_elf_section
7327 (input_bfd
, shlink
, sym
.st_name
));
7331 osec
= sec
->output_section
;
7333 _bfd_elf_section_from_bfd_section (output_bfd
,
7335 if (sym
.st_shndx
== SHN_BAD
)
7338 sym
.st_value
+= sec
->output_offset
;
7339 if (! finfo
->info
->relocatable
)
7341 sym
.st_value
+= osec
->vma
;
7342 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7344 /* STT_TLS symbols are relative to PT_TLS
7346 BFD_ASSERT (elf_hash_table (finfo
->info
)
7348 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7353 finfo
->indices
[r_symndx
]
7354 = bfd_get_symcount (output_bfd
);
7356 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7361 r_symndx
= finfo
->indices
[r_symndx
];
7364 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7365 | (irela
->r_info
& r_type_mask
));
7368 /* Swap out the relocs. */
7369 if (input_rel_hdr
->sh_size
!= 0
7370 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7376 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7377 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7379 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7380 * bed
->s
->int_rels_per_ext_rel
);
7381 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7382 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7391 /* Write out the modified section contents. */
7392 if (bed
->elf_backend_write_section
7393 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7395 /* Section written out. */
7397 else switch (o
->sec_info_type
)
7399 case ELF_INFO_TYPE_STABS
:
7400 if (! (_bfd_write_section_stabs
7402 &elf_hash_table (finfo
->info
)->stab_info
,
7403 o
, &elf_section_data (o
)->sec_info
, contents
)))
7406 case ELF_INFO_TYPE_MERGE
:
7407 if (! _bfd_write_merged_section (output_bfd
, o
,
7408 elf_section_data (o
)->sec_info
))
7411 case ELF_INFO_TYPE_EH_FRAME
:
7413 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7420 if (! (o
->flags
& SEC_EXCLUDE
)
7421 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7423 (file_ptr
) o
->output_offset
,
7434 /* Generate a reloc when linking an ELF file. This is a reloc
7435 requested by the linker, and does not come from any input file. This
7436 is used to build constructor and destructor tables when linking
7440 elf_reloc_link_order (bfd
*output_bfd
,
7441 struct bfd_link_info
*info
,
7442 asection
*output_section
,
7443 struct bfd_link_order
*link_order
)
7445 reloc_howto_type
*howto
;
7449 struct elf_link_hash_entry
**rel_hash_ptr
;
7450 Elf_Internal_Shdr
*rel_hdr
;
7451 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7452 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7456 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7459 bfd_set_error (bfd_error_bad_value
);
7463 addend
= link_order
->u
.reloc
.p
->addend
;
7465 /* Figure out the symbol index. */
7466 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7467 + elf_section_data (output_section
)->rel_count
7468 + elf_section_data (output_section
)->rel_count2
);
7469 if (link_order
->type
== bfd_section_reloc_link_order
)
7471 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7472 BFD_ASSERT (indx
!= 0);
7473 *rel_hash_ptr
= NULL
;
7477 struct elf_link_hash_entry
*h
;
7479 /* Treat a reloc against a defined symbol as though it were
7480 actually against the section. */
7481 h
= ((struct elf_link_hash_entry
*)
7482 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7483 link_order
->u
.reloc
.p
->u
.name
,
7484 FALSE
, FALSE
, TRUE
));
7486 && (h
->root
.type
== bfd_link_hash_defined
7487 || h
->root
.type
== bfd_link_hash_defweak
))
7491 section
= h
->root
.u
.def
.section
;
7492 indx
= section
->output_section
->target_index
;
7493 *rel_hash_ptr
= NULL
;
7494 /* It seems that we ought to add the symbol value to the
7495 addend here, but in practice it has already been added
7496 because it was passed to constructor_callback. */
7497 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7501 /* Setting the index to -2 tells elf_link_output_extsym that
7502 this symbol is used by a reloc. */
7509 if (! ((*info
->callbacks
->unattached_reloc
)
7510 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7516 /* If this is an inplace reloc, we must write the addend into the
7518 if (howto
->partial_inplace
&& addend
!= 0)
7521 bfd_reloc_status_type rstat
;
7524 const char *sym_name
;
7526 size
= bfd_get_reloc_size (howto
);
7527 buf
= bfd_zmalloc (size
);
7530 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7537 case bfd_reloc_outofrange
:
7540 case bfd_reloc_overflow
:
7541 if (link_order
->type
== bfd_section_reloc_link_order
)
7542 sym_name
= bfd_section_name (output_bfd
,
7543 link_order
->u
.reloc
.p
->u
.section
);
7545 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7546 if (! ((*info
->callbacks
->reloc_overflow
)
7547 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7548 NULL
, (bfd_vma
) 0)))
7555 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7556 link_order
->offset
, size
);
7562 /* The address of a reloc is relative to the section in a
7563 relocatable file, and is a virtual address in an executable
7565 offset
= link_order
->offset
;
7566 if (! info
->relocatable
)
7567 offset
+= output_section
->vma
;
7569 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7571 irel
[i
].r_offset
= offset
;
7573 irel
[i
].r_addend
= 0;
7575 if (bed
->s
->arch_size
== 32)
7576 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7578 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7580 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7581 erel
= rel_hdr
->contents
;
7582 if (rel_hdr
->sh_type
== SHT_REL
)
7584 erel
+= (elf_section_data (output_section
)->rel_count
7585 * bed
->s
->sizeof_rel
);
7586 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7590 irel
[0].r_addend
= addend
;
7591 erel
+= (elf_section_data (output_section
)->rel_count
7592 * bed
->s
->sizeof_rela
);
7593 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7596 ++elf_section_data (output_section
)->rel_count
;
7602 /* Get the output vma of the section pointed to by the sh_link field. */
7605 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7607 Elf_Internal_Shdr
**elf_shdrp
;
7611 s
= p
->u
.indirect
.section
;
7612 elf_shdrp
= elf_elfsections (s
->owner
);
7613 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7614 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7616 The Intel C compiler generates SHT_IA_64_UNWIND with
7617 SHF_LINK_ORDER. But it doesn't set the sh_link or
7618 sh_info fields. Hence we could get the situation
7619 where elfsec is 0. */
7622 const struct elf_backend_data
*bed
7623 = get_elf_backend_data (s
->owner
);
7624 if (bed
->link_order_error_handler
)
7625 bed
->link_order_error_handler
7626 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7631 s
= elf_shdrp
[elfsec
]->bfd_section
;
7632 return s
->output_section
->vma
+ s
->output_offset
;
7637 /* Compare two sections based on the locations of the sections they are
7638 linked to. Used by elf_fixup_link_order. */
7641 compare_link_order (const void * a
, const void * b
)
7646 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7647 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7654 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7655 order as their linked sections. Returns false if this could not be done
7656 because an output section includes both ordered and unordered
7657 sections. Ideally we'd do this in the linker proper. */
7660 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7665 struct bfd_link_order
*p
;
7667 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7669 struct bfd_link_order
**sections
;
7670 asection
*s
, *other_sec
, *linkorder_sec
;
7674 linkorder_sec
= NULL
;
7677 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7679 if (p
->type
== bfd_indirect_link_order
)
7681 s
= p
->u
.indirect
.section
;
7683 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7684 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
7685 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
)) != -1
7686 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7700 if (seen_other
&& seen_linkorder
)
7702 if (other_sec
&& linkorder_sec
)
7703 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7705 linkorder_sec
->owner
, other_sec
,
7708 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7710 bfd_set_error (bfd_error_bad_value
);
7715 if (!seen_linkorder
)
7718 sections
= (struct bfd_link_order
**)
7719 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7722 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7724 sections
[seen_linkorder
++] = p
;
7726 /* Sort the input sections in the order of their linked section. */
7727 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7728 compare_link_order
);
7730 /* Change the offsets of the sections. */
7732 for (n
= 0; n
< seen_linkorder
; n
++)
7734 s
= sections
[n
]->u
.indirect
.section
;
7735 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7736 s
->output_offset
= offset
;
7737 sections
[n
]->offset
= offset
;
7738 offset
+= sections
[n
]->size
;
7745 /* Do the final step of an ELF link. */
7748 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7750 bfd_boolean dynamic
;
7751 bfd_boolean emit_relocs
;
7753 struct elf_final_link_info finfo
;
7754 register asection
*o
;
7755 register struct bfd_link_order
*p
;
7757 bfd_size_type max_contents_size
;
7758 bfd_size_type max_external_reloc_size
;
7759 bfd_size_type max_internal_reloc_count
;
7760 bfd_size_type max_sym_count
;
7761 bfd_size_type max_sym_shndx_count
;
7763 Elf_Internal_Sym elfsym
;
7765 Elf_Internal_Shdr
*symtab_hdr
;
7766 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7767 Elf_Internal_Shdr
*symstrtab_hdr
;
7768 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7769 struct elf_outext_info eoinfo
;
7771 size_t relativecount
= 0;
7772 asection
*reldyn
= 0;
7775 if (! is_elf_hash_table (info
->hash
))
7779 abfd
->flags
|= DYNAMIC
;
7781 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7782 dynobj
= elf_hash_table (info
)->dynobj
;
7784 emit_relocs
= (info
->relocatable
7785 || info
->emitrelocations
);
7788 finfo
.output_bfd
= abfd
;
7789 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7790 if (finfo
.symstrtab
== NULL
)
7795 finfo
.dynsym_sec
= NULL
;
7796 finfo
.hash_sec
= NULL
;
7797 finfo
.symver_sec
= NULL
;
7801 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7802 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7803 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7804 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7805 /* Note that it is OK if symver_sec is NULL. */
7808 finfo
.contents
= NULL
;
7809 finfo
.external_relocs
= NULL
;
7810 finfo
.internal_relocs
= NULL
;
7811 finfo
.external_syms
= NULL
;
7812 finfo
.locsym_shndx
= NULL
;
7813 finfo
.internal_syms
= NULL
;
7814 finfo
.indices
= NULL
;
7815 finfo
.sections
= NULL
;
7816 finfo
.symbuf
= NULL
;
7817 finfo
.symshndxbuf
= NULL
;
7818 finfo
.symbuf_count
= 0;
7819 finfo
.shndxbuf_size
= 0;
7821 /* Count up the number of relocations we will output for each output
7822 section, so that we know the sizes of the reloc sections. We
7823 also figure out some maximum sizes. */
7824 max_contents_size
= 0;
7825 max_external_reloc_size
= 0;
7826 max_internal_reloc_count
= 0;
7828 max_sym_shndx_count
= 0;
7830 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7832 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7835 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7837 unsigned int reloc_count
= 0;
7838 struct bfd_elf_section_data
*esdi
= NULL
;
7839 unsigned int *rel_count1
;
7841 if (p
->type
== bfd_section_reloc_link_order
7842 || p
->type
== bfd_symbol_reloc_link_order
)
7844 else if (p
->type
== bfd_indirect_link_order
)
7848 sec
= p
->u
.indirect
.section
;
7849 esdi
= elf_section_data (sec
);
7851 /* Mark all sections which are to be included in the
7852 link. This will normally be every section. We need
7853 to do this so that we can identify any sections which
7854 the linker has decided to not include. */
7855 sec
->linker_mark
= TRUE
;
7857 if (sec
->flags
& SEC_MERGE
)
7860 if (info
->relocatable
|| info
->emitrelocations
)
7861 reloc_count
= sec
->reloc_count
;
7862 else if (bed
->elf_backend_count_relocs
)
7864 Elf_Internal_Rela
* relocs
;
7866 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7869 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7871 if (elf_section_data (o
)->relocs
!= relocs
)
7875 if (sec
->rawsize
> max_contents_size
)
7876 max_contents_size
= sec
->rawsize
;
7877 if (sec
->size
> max_contents_size
)
7878 max_contents_size
= sec
->size
;
7880 /* We are interested in just local symbols, not all
7882 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7883 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7887 if (elf_bad_symtab (sec
->owner
))
7888 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7889 / bed
->s
->sizeof_sym
);
7891 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7893 if (sym_count
> max_sym_count
)
7894 max_sym_count
= sym_count
;
7896 if (sym_count
> max_sym_shndx_count
7897 && elf_symtab_shndx (sec
->owner
) != 0)
7898 max_sym_shndx_count
= sym_count
;
7900 if ((sec
->flags
& SEC_RELOC
) != 0)
7904 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7905 if (ext_size
> max_external_reloc_size
)
7906 max_external_reloc_size
= ext_size
;
7907 if (sec
->reloc_count
> max_internal_reloc_count
)
7908 max_internal_reloc_count
= sec
->reloc_count
;
7913 if (reloc_count
== 0)
7916 o
->reloc_count
+= reloc_count
;
7918 /* MIPS may have a mix of REL and RELA relocs on sections.
7919 To support this curious ABI we keep reloc counts in
7920 elf_section_data too. We must be careful to add the
7921 relocations from the input section to the right output
7922 count. FIXME: Get rid of one count. We have
7923 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7924 rel_count1
= &esdo
->rel_count
;
7927 bfd_boolean same_size
;
7928 bfd_size_type entsize1
;
7930 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7931 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7932 || entsize1
== bed
->s
->sizeof_rela
);
7933 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7936 rel_count1
= &esdo
->rel_count2
;
7938 if (esdi
->rel_hdr2
!= NULL
)
7940 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7941 unsigned int alt_count
;
7942 unsigned int *rel_count2
;
7944 BFD_ASSERT (entsize2
!= entsize1
7945 && (entsize2
== bed
->s
->sizeof_rel
7946 || entsize2
== bed
->s
->sizeof_rela
));
7948 rel_count2
= &esdo
->rel_count2
;
7950 rel_count2
= &esdo
->rel_count
;
7952 /* The following is probably too simplistic if the
7953 backend counts output relocs unusually. */
7954 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7955 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7956 *rel_count2
+= alt_count
;
7957 reloc_count
-= alt_count
;
7960 *rel_count1
+= reloc_count
;
7963 if (o
->reloc_count
> 0)
7964 o
->flags
|= SEC_RELOC
;
7967 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7968 set it (this is probably a bug) and if it is set
7969 assign_section_numbers will create a reloc section. */
7970 o
->flags
&=~ SEC_RELOC
;
7973 /* If the SEC_ALLOC flag is not set, force the section VMA to
7974 zero. This is done in elf_fake_sections as well, but forcing
7975 the VMA to 0 here will ensure that relocs against these
7976 sections are handled correctly. */
7977 if ((o
->flags
& SEC_ALLOC
) == 0
7978 && ! o
->user_set_vma
)
7982 if (! info
->relocatable
&& merged
)
7983 elf_link_hash_traverse (elf_hash_table (info
),
7984 _bfd_elf_link_sec_merge_syms
, abfd
);
7986 /* Figure out the file positions for everything but the symbol table
7987 and the relocs. We set symcount to force assign_section_numbers
7988 to create a symbol table. */
7989 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7990 BFD_ASSERT (! abfd
->output_has_begun
);
7991 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7994 /* Set sizes, and assign file positions for reloc sections. */
7995 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7997 if ((o
->flags
& SEC_RELOC
) != 0)
7999 if (!(_bfd_elf_link_size_reloc_section
8000 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
8003 if (elf_section_data (o
)->rel_hdr2
8004 && !(_bfd_elf_link_size_reloc_section
8005 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
8009 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8010 to count upwards while actually outputting the relocations. */
8011 elf_section_data (o
)->rel_count
= 0;
8012 elf_section_data (o
)->rel_count2
= 0;
8015 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8017 /* We have now assigned file positions for all the sections except
8018 .symtab and .strtab. We start the .symtab section at the current
8019 file position, and write directly to it. We build the .strtab
8020 section in memory. */
8021 bfd_get_symcount (abfd
) = 0;
8022 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8023 /* sh_name is set in prep_headers. */
8024 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8025 /* sh_flags, sh_addr and sh_size all start off zero. */
8026 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8027 /* sh_link is set in assign_section_numbers. */
8028 /* sh_info is set below. */
8029 /* sh_offset is set just below. */
8030 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8032 off
= elf_tdata (abfd
)->next_file_pos
;
8033 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8035 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8036 incorrect. We do not yet know the size of the .symtab section.
8037 We correct next_file_pos below, after we do know the size. */
8039 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8040 continuously seeking to the right position in the file. */
8041 if (! info
->keep_memory
|| max_sym_count
< 20)
8042 finfo
.symbuf_size
= 20;
8044 finfo
.symbuf_size
= max_sym_count
;
8045 amt
= finfo
.symbuf_size
;
8046 amt
*= bed
->s
->sizeof_sym
;
8047 finfo
.symbuf
= bfd_malloc (amt
);
8048 if (finfo
.symbuf
== NULL
)
8050 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8052 /* Wild guess at number of output symbols. realloc'd as needed. */
8053 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8054 finfo
.shndxbuf_size
= amt
;
8055 amt
*= sizeof (Elf_External_Sym_Shndx
);
8056 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8057 if (finfo
.symshndxbuf
== NULL
)
8061 /* Start writing out the symbol table. The first symbol is always a
8063 if (info
->strip
!= strip_all
8066 elfsym
.st_value
= 0;
8069 elfsym
.st_other
= 0;
8070 elfsym
.st_shndx
= SHN_UNDEF
;
8071 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8076 /* Output a symbol for each section. We output these even if we are
8077 discarding local symbols, since they are used for relocs. These
8078 symbols have no names. We store the index of each one in the
8079 index field of the section, so that we can find it again when
8080 outputting relocs. */
8081 if (info
->strip
!= strip_all
8085 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8086 elfsym
.st_other
= 0;
8087 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8089 o
= bfd_section_from_elf_index (abfd
, i
);
8091 o
->target_index
= bfd_get_symcount (abfd
);
8092 elfsym
.st_shndx
= i
;
8093 if (info
->relocatable
|| o
== NULL
)
8094 elfsym
.st_value
= 0;
8096 elfsym
.st_value
= o
->vma
;
8097 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8099 if (i
== SHN_LORESERVE
- 1)
8100 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8104 /* Allocate some memory to hold information read in from the input
8106 if (max_contents_size
!= 0)
8108 finfo
.contents
= bfd_malloc (max_contents_size
);
8109 if (finfo
.contents
== NULL
)
8113 if (max_external_reloc_size
!= 0)
8115 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8116 if (finfo
.external_relocs
== NULL
)
8120 if (max_internal_reloc_count
!= 0)
8122 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8123 amt
*= sizeof (Elf_Internal_Rela
);
8124 finfo
.internal_relocs
= bfd_malloc (amt
);
8125 if (finfo
.internal_relocs
== NULL
)
8129 if (max_sym_count
!= 0)
8131 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8132 finfo
.external_syms
= bfd_malloc (amt
);
8133 if (finfo
.external_syms
== NULL
)
8136 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8137 finfo
.internal_syms
= bfd_malloc (amt
);
8138 if (finfo
.internal_syms
== NULL
)
8141 amt
= max_sym_count
* sizeof (long);
8142 finfo
.indices
= bfd_malloc (amt
);
8143 if (finfo
.indices
== NULL
)
8146 amt
= max_sym_count
* sizeof (asection
*);
8147 finfo
.sections
= bfd_malloc (amt
);
8148 if (finfo
.sections
== NULL
)
8152 if (max_sym_shndx_count
!= 0)
8154 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8155 finfo
.locsym_shndx
= bfd_malloc (amt
);
8156 if (finfo
.locsym_shndx
== NULL
)
8160 if (elf_hash_table (info
)->tls_sec
)
8162 bfd_vma base
, end
= 0;
8165 for (sec
= elf_hash_table (info
)->tls_sec
;
8166 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8169 bfd_size_type size
= sec
->size
;
8172 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8174 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8176 size
= o
->offset
+ o
->size
;
8178 end
= sec
->vma
+ size
;
8180 base
= elf_hash_table (info
)->tls_sec
->vma
;
8181 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8182 elf_hash_table (info
)->tls_size
= end
- base
;
8185 /* Reorder SHF_LINK_ORDER sections. */
8186 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8188 if (!elf_fixup_link_order (abfd
, o
))
8192 /* Since ELF permits relocations to be against local symbols, we
8193 must have the local symbols available when we do the relocations.
8194 Since we would rather only read the local symbols once, and we
8195 would rather not keep them in memory, we handle all the
8196 relocations for a single input file at the same time.
8198 Unfortunately, there is no way to know the total number of local
8199 symbols until we have seen all of them, and the local symbol
8200 indices precede the global symbol indices. This means that when
8201 we are generating relocatable output, and we see a reloc against
8202 a global symbol, we can not know the symbol index until we have
8203 finished examining all the local symbols to see which ones we are
8204 going to output. To deal with this, we keep the relocations in
8205 memory, and don't output them until the end of the link. This is
8206 an unfortunate waste of memory, but I don't see a good way around
8207 it. Fortunately, it only happens when performing a relocatable
8208 link, which is not the common case. FIXME: If keep_memory is set
8209 we could write the relocs out and then read them again; I don't
8210 know how bad the memory loss will be. */
8212 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8213 sub
->output_has_begun
= FALSE
;
8214 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8216 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8218 if (p
->type
== bfd_indirect_link_order
8219 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8220 == bfd_target_elf_flavour
)
8221 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8223 if (! sub
->output_has_begun
)
8225 if (! elf_link_input_bfd (&finfo
, sub
))
8227 sub
->output_has_begun
= TRUE
;
8230 else if (p
->type
== bfd_section_reloc_link_order
8231 || p
->type
== bfd_symbol_reloc_link_order
)
8233 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8238 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8244 /* Output any global symbols that got converted to local in a
8245 version script or due to symbol visibility. We do this in a
8246 separate step since ELF requires all local symbols to appear
8247 prior to any global symbols. FIXME: We should only do this if
8248 some global symbols were, in fact, converted to become local.
8249 FIXME: Will this work correctly with the Irix 5 linker? */
8250 eoinfo
.failed
= FALSE
;
8251 eoinfo
.finfo
= &finfo
;
8252 eoinfo
.localsyms
= TRUE
;
8253 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8258 /* If backend needs to output some local symbols not present in the hash
8259 table, do it now. */
8260 if (bed
->elf_backend_output_arch_local_syms
)
8262 typedef bfd_boolean (*out_sym_func
)
8263 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8264 struct elf_link_hash_entry
*);
8266 if (! ((*bed
->elf_backend_output_arch_local_syms
)
8267 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8271 /* That wrote out all the local symbols. Finish up the symbol table
8272 with the global symbols. Even if we want to strip everything we
8273 can, we still need to deal with those global symbols that got
8274 converted to local in a version script. */
8276 /* The sh_info field records the index of the first non local symbol. */
8277 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8280 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8282 Elf_Internal_Sym sym
;
8283 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8284 long last_local
= 0;
8286 /* Write out the section symbols for the output sections. */
8287 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8293 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8296 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8302 dynindx
= elf_section_data (s
)->dynindx
;
8305 indx
= elf_section_data (s
)->this_idx
;
8306 BFD_ASSERT (indx
> 0);
8307 sym
.st_shndx
= indx
;
8308 sym
.st_value
= s
->vma
;
8309 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8310 if (last_local
< dynindx
)
8311 last_local
= dynindx
;
8312 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8316 /* Write out the local dynsyms. */
8317 if (elf_hash_table (info
)->dynlocal
)
8319 struct elf_link_local_dynamic_entry
*e
;
8320 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8325 sym
.st_size
= e
->isym
.st_size
;
8326 sym
.st_other
= e
->isym
.st_other
;
8328 /* Copy the internal symbol as is.
8329 Note that we saved a word of storage and overwrote
8330 the original st_name with the dynstr_index. */
8333 if (e
->isym
.st_shndx
!= SHN_UNDEF
8334 && (e
->isym
.st_shndx
< SHN_LORESERVE
8335 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8337 s
= bfd_section_from_elf_index (e
->input_bfd
,
8341 elf_section_data (s
->output_section
)->this_idx
;
8342 sym
.st_value
= (s
->output_section
->vma
8344 + e
->isym
.st_value
);
8347 if (last_local
< e
->dynindx
)
8348 last_local
= e
->dynindx
;
8350 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8351 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8355 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8359 /* We get the global symbols from the hash table. */
8360 eoinfo
.failed
= FALSE
;
8361 eoinfo
.localsyms
= FALSE
;
8362 eoinfo
.finfo
= &finfo
;
8363 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8368 /* If backend needs to output some symbols not present in the hash
8369 table, do it now. */
8370 if (bed
->elf_backend_output_arch_syms
)
8372 typedef bfd_boolean (*out_sym_func
)
8373 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8374 struct elf_link_hash_entry
*);
8376 if (! ((*bed
->elf_backend_output_arch_syms
)
8377 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8381 /* Flush all symbols to the file. */
8382 if (! elf_link_flush_output_syms (&finfo
, bed
))
8385 /* Now we know the size of the symtab section. */
8386 off
+= symtab_hdr
->sh_size
;
8388 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8389 if (symtab_shndx_hdr
->sh_name
!= 0)
8391 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8392 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8393 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8394 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8395 symtab_shndx_hdr
->sh_size
= amt
;
8397 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8400 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8401 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8406 /* Finish up and write out the symbol string table (.strtab)
8408 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8409 /* sh_name was set in prep_headers. */
8410 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8411 symstrtab_hdr
->sh_flags
= 0;
8412 symstrtab_hdr
->sh_addr
= 0;
8413 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8414 symstrtab_hdr
->sh_entsize
= 0;
8415 symstrtab_hdr
->sh_link
= 0;
8416 symstrtab_hdr
->sh_info
= 0;
8417 /* sh_offset is set just below. */
8418 symstrtab_hdr
->sh_addralign
= 1;
8420 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8421 elf_tdata (abfd
)->next_file_pos
= off
;
8423 if (bfd_get_symcount (abfd
) > 0)
8425 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8426 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8430 /* Adjust the relocs to have the correct symbol indices. */
8431 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8433 if ((o
->flags
& SEC_RELOC
) == 0)
8436 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8437 elf_section_data (o
)->rel_count
,
8438 elf_section_data (o
)->rel_hashes
);
8439 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8440 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8441 elf_section_data (o
)->rel_count2
,
8442 (elf_section_data (o
)->rel_hashes
8443 + elf_section_data (o
)->rel_count
));
8445 /* Set the reloc_count field to 0 to prevent write_relocs from
8446 trying to swap the relocs out itself. */
8450 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8451 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8453 /* If we are linking against a dynamic object, or generating a
8454 shared library, finish up the dynamic linking information. */
8457 bfd_byte
*dyncon
, *dynconend
;
8459 /* Fix up .dynamic entries. */
8460 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8461 BFD_ASSERT (o
!= NULL
);
8463 dyncon
= o
->contents
;
8464 dynconend
= o
->contents
+ o
->size
;
8465 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8467 Elf_Internal_Dyn dyn
;
8471 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8478 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8480 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8482 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8483 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8486 dyn
.d_un
.d_val
= relativecount
;
8493 name
= info
->init_function
;
8496 name
= info
->fini_function
;
8499 struct elf_link_hash_entry
*h
;
8501 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8502 FALSE
, FALSE
, TRUE
);
8504 && (h
->root
.type
== bfd_link_hash_defined
8505 || h
->root
.type
== bfd_link_hash_defweak
))
8507 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8508 o
= h
->root
.u
.def
.section
;
8509 if (o
->output_section
!= NULL
)
8510 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8511 + o
->output_offset
);
8514 /* The symbol is imported from another shared
8515 library and does not apply to this one. */
8523 case DT_PREINIT_ARRAYSZ
:
8524 name
= ".preinit_array";
8526 case DT_INIT_ARRAYSZ
:
8527 name
= ".init_array";
8529 case DT_FINI_ARRAYSZ
:
8530 name
= ".fini_array";
8532 o
= bfd_get_section_by_name (abfd
, name
);
8535 (*_bfd_error_handler
)
8536 (_("%B: could not find output section %s"), abfd
, name
);
8540 (*_bfd_error_handler
)
8541 (_("warning: %s section has zero size"), name
);
8542 dyn
.d_un
.d_val
= o
->size
;
8545 case DT_PREINIT_ARRAY
:
8546 name
= ".preinit_array";
8549 name
= ".init_array";
8552 name
= ".fini_array";
8565 name
= ".gnu.version_d";
8568 name
= ".gnu.version_r";
8571 name
= ".gnu.version";
8573 o
= bfd_get_section_by_name (abfd
, name
);
8576 (*_bfd_error_handler
)
8577 (_("%B: could not find output section %s"), abfd
, name
);
8580 dyn
.d_un
.d_ptr
= o
->vma
;
8587 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8592 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8594 Elf_Internal_Shdr
*hdr
;
8596 hdr
= elf_elfsections (abfd
)[i
];
8597 if (hdr
->sh_type
== type
8598 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8600 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8601 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8604 if (dyn
.d_un
.d_val
== 0
8605 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8606 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8612 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8616 /* If we have created any dynamic sections, then output them. */
8619 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8622 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8624 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8626 || o
->output_section
== bfd_abs_section_ptr
)
8628 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8630 /* At this point, we are only interested in sections
8631 created by _bfd_elf_link_create_dynamic_sections. */
8634 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8636 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8638 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8640 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8642 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8644 (file_ptr
) o
->output_offset
,
8650 /* The contents of the .dynstr section are actually in a
8652 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8653 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8654 || ! _bfd_elf_strtab_emit (abfd
,
8655 elf_hash_table (info
)->dynstr
))
8661 if (info
->relocatable
)
8663 bfd_boolean failed
= FALSE
;
8665 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8670 /* If we have optimized stabs strings, output them. */
8671 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8673 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8677 if (info
->eh_frame_hdr
)
8679 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
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
);
8712 elf_tdata (abfd
)->linker
= TRUE
;
8717 if (finfo
.symstrtab
!= NULL
)
8718 _bfd_stringtab_free (finfo
.symstrtab
);
8719 if (finfo
.contents
!= NULL
)
8720 free (finfo
.contents
);
8721 if (finfo
.external_relocs
!= NULL
)
8722 free (finfo
.external_relocs
);
8723 if (finfo
.internal_relocs
!= NULL
)
8724 free (finfo
.internal_relocs
);
8725 if (finfo
.external_syms
!= NULL
)
8726 free (finfo
.external_syms
);
8727 if (finfo
.locsym_shndx
!= NULL
)
8728 free (finfo
.locsym_shndx
);
8729 if (finfo
.internal_syms
!= NULL
)
8730 free (finfo
.internal_syms
);
8731 if (finfo
.indices
!= NULL
)
8732 free (finfo
.indices
);
8733 if (finfo
.sections
!= NULL
)
8734 free (finfo
.sections
);
8735 if (finfo
.symbuf
!= NULL
)
8736 free (finfo
.symbuf
);
8737 if (finfo
.symshndxbuf
!= NULL
)
8738 free (finfo
.symshndxbuf
);
8739 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8741 if ((o
->flags
& SEC_RELOC
) != 0
8742 && elf_section_data (o
)->rel_hashes
!= NULL
)
8743 free (elf_section_data (o
)->rel_hashes
);
8749 /* Garbage collect unused sections. */
8751 /* The mark phase of garbage collection. For a given section, mark
8752 it and any sections in this section's group, and all the sections
8753 which define symbols to which it refers. */
8755 typedef asection
* (*gc_mark_hook_fn
)
8756 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8757 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8760 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8762 gc_mark_hook_fn gc_mark_hook
)
8766 asection
*group_sec
;
8770 /* Mark all the sections in the group. */
8771 group_sec
= elf_section_data (sec
)->next_in_group
;
8772 if (group_sec
&& !group_sec
->gc_mark
)
8773 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8776 /* Look through the section relocs. */
8778 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
8779 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8781 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8782 Elf_Internal_Shdr
*symtab_hdr
;
8783 struct elf_link_hash_entry
**sym_hashes
;
8786 bfd
*input_bfd
= sec
->owner
;
8787 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8788 Elf_Internal_Sym
*isym
= NULL
;
8791 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8792 sym_hashes
= elf_sym_hashes (input_bfd
);
8794 /* Read the local symbols. */
8795 if (elf_bad_symtab (input_bfd
))
8797 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8801 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8803 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8804 if (isym
== NULL
&& nlocsyms
!= 0)
8806 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8812 /* Read the relocations. */
8813 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8815 if (relstart
== NULL
)
8820 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8822 if (bed
->s
->arch_size
== 32)
8827 for (rel
= relstart
; rel
< relend
; rel
++)
8829 unsigned long r_symndx
;
8831 struct elf_link_hash_entry
*h
;
8833 r_symndx
= rel
->r_info
>> r_sym_shift
;
8837 if (r_symndx
>= nlocsyms
8838 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8840 h
= sym_hashes
[r_symndx
- extsymoff
];
8841 while (h
->root
.type
== bfd_link_hash_indirect
8842 || h
->root
.type
== bfd_link_hash_warning
)
8843 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8844 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8848 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8851 if (rsec
&& !rsec
->gc_mark
)
8853 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8856 rsec
->gc_mark_from_eh
= 1;
8857 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8866 if (elf_section_data (sec
)->relocs
!= relstart
)
8869 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8871 if (! info
->keep_memory
)
8874 symtab_hdr
->contents
= (unsigned char *) isym
;
8881 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8883 struct elf_gc_sweep_symbol_info
{
8884 struct bfd_link_info
*info
;
8885 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
8890 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
8892 if (h
->root
.type
== bfd_link_hash_warning
)
8893 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8895 if ((h
->root
.type
== bfd_link_hash_defined
8896 || h
->root
.type
== bfd_link_hash_defweak
)
8897 && !h
->root
.u
.def
.section
->gc_mark
8898 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
8900 struct elf_gc_sweep_symbol_info
*inf
= data
;
8901 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
8907 /* The sweep phase of garbage collection. Remove all garbage sections. */
8909 typedef bfd_boolean (*gc_sweep_hook_fn
)
8910 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8913 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
8916 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8917 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
8918 unsigned long section_sym_count
;
8919 struct elf_gc_sweep_symbol_info sweep_info
;
8921 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8925 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8928 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8930 /* Keep debug and special sections. */
8931 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8932 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8938 /* Skip sweeping sections already excluded. */
8939 if (o
->flags
& SEC_EXCLUDE
)
8942 /* Since this is early in the link process, it is simple
8943 to remove a section from the output. */
8944 o
->flags
|= SEC_EXCLUDE
;
8946 /* But we also have to update some of the relocation
8947 info we collected before. */
8949 && (o
->flags
& SEC_RELOC
) != 0
8950 && o
->reloc_count
> 0
8951 && !bfd_is_abs_section (o
->output_section
))
8953 Elf_Internal_Rela
*internal_relocs
;
8957 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8959 if (internal_relocs
== NULL
)
8962 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8964 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8965 free (internal_relocs
);
8973 /* Remove the symbols that were in the swept sections from the dynamic
8974 symbol table. GCFIXME: Anyone know how to get them out of the
8975 static symbol table as well? */
8976 sweep_info
.info
= info
;
8977 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
8978 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
8981 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
8985 /* Propagate collected vtable information. This is called through
8986 elf_link_hash_traverse. */
8989 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8991 if (h
->root
.type
== bfd_link_hash_warning
)
8992 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8994 /* Those that are not vtables. */
8995 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8998 /* Those vtables that do not have parents, we cannot merge. */
8999 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
9002 /* If we've already been done, exit. */
9003 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
9006 /* Make sure the parent's table is up to date. */
9007 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
9009 if (h
->vtable
->used
== NULL
)
9011 /* None of this table's entries were referenced. Re-use the
9013 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
9014 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
9019 bfd_boolean
*cu
, *pu
;
9021 /* Or the parent's entries into ours. */
9022 cu
= h
->vtable
->used
;
9024 pu
= h
->vtable
->parent
->vtable
->used
;
9027 const struct elf_backend_data
*bed
;
9028 unsigned int log_file_align
;
9030 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9031 log_file_align
= bed
->s
->log_file_align
;
9032 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9047 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9050 bfd_vma hstart
, hend
;
9051 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9052 const struct elf_backend_data
*bed
;
9053 unsigned int log_file_align
;
9055 if (h
->root
.type
== bfd_link_hash_warning
)
9056 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9058 /* Take care of both those symbols that do not describe vtables as
9059 well as those that are not loaded. */
9060 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9063 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9064 || h
->root
.type
== bfd_link_hash_defweak
);
9066 sec
= h
->root
.u
.def
.section
;
9067 hstart
= h
->root
.u
.def
.value
;
9068 hend
= hstart
+ h
->size
;
9070 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9072 return *(bfd_boolean
*) okp
= FALSE
;
9073 bed
= get_elf_backend_data (sec
->owner
);
9074 log_file_align
= bed
->s
->log_file_align
;
9076 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9078 for (rel
= relstart
; rel
< relend
; ++rel
)
9079 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9081 /* If the entry is in use, do nothing. */
9083 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9085 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9086 if (h
->vtable
->used
[entry
])
9089 /* Otherwise, kill it. */
9090 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9096 /* Mark sections containing dynamically referenced symbols. When
9097 building shared libraries, we must assume that any visible symbol is
9101 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9103 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9105 if (h
->root
.type
== bfd_link_hash_warning
)
9106 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9108 if ((h
->root
.type
== bfd_link_hash_defined
9109 || h
->root
.type
== bfd_link_hash_defweak
)
9111 || (!info
->executable
9113 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9114 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9115 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9120 /* Do mark and sweep of unused sections. */
9123 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9125 bfd_boolean ok
= TRUE
;
9127 asection
* (*gc_mark_hook
)
9128 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9129 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9132 if (!bed
->can_gc_sections
9133 || info
->relocatable
9134 || info
->emitrelocations
9135 || !is_elf_hash_table (info
->hash
))
9137 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9141 /* Apply transitive closure to the vtable entry usage info. */
9142 elf_link_hash_traverse (elf_hash_table (info
),
9143 elf_gc_propagate_vtable_entries_used
,
9148 /* Kill the vtable relocations that were not used. */
9149 elf_link_hash_traverse (elf_hash_table (info
),
9150 elf_gc_smash_unused_vtentry_relocs
,
9155 /* Mark dynamically referenced symbols. */
9156 if (elf_hash_table (info
)->dynamic_sections_created
)
9157 elf_link_hash_traverse (elf_hash_table (info
),
9158 bed
->gc_mark_dynamic_ref
,
9161 /* Grovel through relocs to find out who stays ... */
9162 gc_mark_hook
= bed
->gc_mark_hook
;
9163 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9167 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9170 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9171 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9172 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9176 /* ... again for sections marked from eh_frame. */
9177 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9181 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9184 /* Keep .gcc_except_table.* if the associated .text.* is
9185 marked. This isn't very nice, but the proper solution,
9186 splitting .eh_frame up and using comdat doesn't pan out
9187 easily due to needing special relocs to handle the
9188 difference of two symbols in separate sections.
9189 Don't keep code sections referenced by .eh_frame. */
9190 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9191 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9193 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9199 len
= strlen (o
->name
+ 18) + 1;
9200 fn_name
= bfd_malloc (len
+ 6);
9201 if (fn_name
== NULL
)
9203 memcpy (fn_name
, ".text.", 6);
9204 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9205 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9207 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9211 /* If not using specially named exception table section,
9212 then keep whatever we are using. */
9213 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9218 /* ... and mark SEC_EXCLUDE for those that go. */
9219 return elf_gc_sweep (abfd
, info
);
9222 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9225 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9227 struct elf_link_hash_entry
*h
,
9230 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9231 struct elf_link_hash_entry
**search
, *child
;
9232 bfd_size_type extsymcount
;
9233 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9235 /* The sh_info field of the symtab header tells us where the
9236 external symbols start. We don't care about the local symbols at
9238 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9239 if (!elf_bad_symtab (abfd
))
9240 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9242 sym_hashes
= elf_sym_hashes (abfd
);
9243 sym_hashes_end
= sym_hashes
+ extsymcount
;
9245 /* Hunt down the child symbol, which is in this section at the same
9246 offset as the relocation. */
9247 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9249 if ((child
= *search
) != NULL
9250 && (child
->root
.type
== bfd_link_hash_defined
9251 || child
->root
.type
== bfd_link_hash_defweak
)
9252 && child
->root
.u
.def
.section
== sec
9253 && child
->root
.u
.def
.value
== offset
)
9257 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9258 abfd
, sec
, (unsigned long) offset
);
9259 bfd_set_error (bfd_error_invalid_operation
);
9265 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9271 /* This *should* only be the absolute section. It could potentially
9272 be that someone has defined a non-global vtable though, which
9273 would be bad. It isn't worth paging in the local symbols to be
9274 sure though; that case should simply be handled by the assembler. */
9276 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9279 child
->vtable
->parent
= h
;
9284 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9287 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9288 asection
*sec ATTRIBUTE_UNUSED
,
9289 struct elf_link_hash_entry
*h
,
9292 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9293 unsigned int log_file_align
= bed
->s
->log_file_align
;
9297 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9302 if (addend
>= h
->vtable
->size
)
9304 size_t size
, bytes
, file_align
;
9305 bfd_boolean
*ptr
= h
->vtable
->used
;
9307 /* While the symbol is undefined, we have to be prepared to handle
9309 file_align
= 1 << log_file_align
;
9310 if (h
->root
.type
== bfd_link_hash_undefined
)
9311 size
= addend
+ file_align
;
9317 /* Oops! We've got a reference past the defined end of
9318 the table. This is probably a bug -- shall we warn? */
9319 size
= addend
+ file_align
;
9322 size
= (size
+ file_align
- 1) & -file_align
;
9324 /* Allocate one extra entry for use as a "done" flag for the
9325 consolidation pass. */
9326 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9330 ptr
= bfd_realloc (ptr
- 1, bytes
);
9336 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9337 * sizeof (bfd_boolean
));
9338 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9342 ptr
= bfd_zmalloc (bytes
);
9347 /* And arrange for that done flag to be at index -1. */
9348 h
->vtable
->used
= ptr
+ 1;
9349 h
->vtable
->size
= size
;
9352 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9357 struct alloc_got_off_arg
{
9359 unsigned int got_elt_size
;
9362 /* We need a special top-level link routine to convert got reference counts
9363 to real got offsets. */
9366 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9368 struct alloc_got_off_arg
*gofarg
= arg
;
9370 if (h
->root
.type
== bfd_link_hash_warning
)
9371 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9373 if (h
->got
.refcount
> 0)
9375 h
->got
.offset
= gofarg
->gotoff
;
9376 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9379 h
->got
.offset
= (bfd_vma
) -1;
9384 /* And an accompanying bit to work out final got entry offsets once
9385 we're done. Should be called from final_link. */
9388 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9389 struct bfd_link_info
*info
)
9392 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9394 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9395 struct alloc_got_off_arg gofarg
;
9397 if (! is_elf_hash_table (info
->hash
))
9400 /* The GOT offset is relative to the .got section, but the GOT header is
9401 put into the .got.plt section, if the backend uses it. */
9402 if (bed
->want_got_plt
)
9405 gotoff
= bed
->got_header_size
;
9407 /* Do the local .got entries first. */
9408 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9410 bfd_signed_vma
*local_got
;
9411 bfd_size_type j
, locsymcount
;
9412 Elf_Internal_Shdr
*symtab_hdr
;
9414 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9417 local_got
= elf_local_got_refcounts (i
);
9421 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9422 if (elf_bad_symtab (i
))
9423 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9425 locsymcount
= symtab_hdr
->sh_info
;
9427 for (j
= 0; j
< locsymcount
; ++j
)
9429 if (local_got
[j
] > 0)
9431 local_got
[j
] = gotoff
;
9432 gotoff
+= got_elt_size
;
9435 local_got
[j
] = (bfd_vma
) -1;
9439 /* Then the global .got entries. .plt refcounts are handled by
9440 adjust_dynamic_symbol */
9441 gofarg
.gotoff
= gotoff
;
9442 gofarg
.got_elt_size
= got_elt_size
;
9443 elf_link_hash_traverse (elf_hash_table (info
),
9444 elf_gc_allocate_got_offsets
,
9449 /* Many folk need no more in the way of final link than this, once
9450 got entry reference counting is enabled. */
9453 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9455 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9458 /* Invoke the regular ELF backend linker to do all the work. */
9459 return bfd_elf_final_link (abfd
, info
);
9463 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9465 struct elf_reloc_cookie
*rcookie
= cookie
;
9467 if (rcookie
->bad_symtab
)
9468 rcookie
->rel
= rcookie
->rels
;
9470 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9472 unsigned long r_symndx
;
9474 if (! rcookie
->bad_symtab
)
9475 if (rcookie
->rel
->r_offset
> offset
)
9477 if (rcookie
->rel
->r_offset
!= offset
)
9480 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9481 if (r_symndx
== SHN_UNDEF
)
9484 if (r_symndx
>= rcookie
->locsymcount
9485 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9487 struct elf_link_hash_entry
*h
;
9489 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9491 while (h
->root
.type
== bfd_link_hash_indirect
9492 || h
->root
.type
== bfd_link_hash_warning
)
9493 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9495 if ((h
->root
.type
== bfd_link_hash_defined
9496 || h
->root
.type
== bfd_link_hash_defweak
)
9497 && elf_discarded_section (h
->root
.u
.def
.section
))
9504 /* It's not a relocation against a global symbol,
9505 but it could be a relocation against a local
9506 symbol for a discarded section. */
9508 Elf_Internal_Sym
*isym
;
9510 /* Need to: get the symbol; get the section. */
9511 isym
= &rcookie
->locsyms
[r_symndx
];
9512 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9514 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9515 if (isec
!= NULL
&& elf_discarded_section (isec
))
9524 /* Discard unneeded references to discarded sections.
9525 Returns TRUE if any section's size was changed. */
9526 /* This function assumes that the relocations are in sorted order,
9527 which is true for all known assemblers. */
9530 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9532 struct elf_reloc_cookie cookie
;
9533 asection
*stab
, *eh
;
9534 Elf_Internal_Shdr
*symtab_hdr
;
9535 const struct elf_backend_data
*bed
;
9538 bfd_boolean ret
= FALSE
;
9540 if (info
->traditional_format
9541 || !is_elf_hash_table (info
->hash
))
9544 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9546 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9549 bed
= get_elf_backend_data (abfd
);
9551 if ((abfd
->flags
& DYNAMIC
) != 0)
9554 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9555 if (info
->relocatable
9558 || bfd_is_abs_section (eh
->output_section
))))
9561 stab
= bfd_get_section_by_name (abfd
, ".stab");
9564 || bfd_is_abs_section (stab
->output_section
)
9565 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9570 && bed
->elf_backend_discard_info
== NULL
)
9573 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9575 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9576 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9577 if (cookie
.bad_symtab
)
9579 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9580 cookie
.extsymoff
= 0;
9584 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9585 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9588 if (bed
->s
->arch_size
== 32)
9589 cookie
.r_sym_shift
= 8;
9591 cookie
.r_sym_shift
= 32;
9593 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9594 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9596 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9597 cookie
.locsymcount
, 0,
9599 if (cookie
.locsyms
== NULL
)
9606 count
= stab
->reloc_count
;
9608 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9610 if (cookie
.rels
!= NULL
)
9612 cookie
.rel
= cookie
.rels
;
9613 cookie
.relend
= cookie
.rels
;
9614 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9615 if (_bfd_discard_section_stabs (abfd
, stab
,
9616 elf_section_data (stab
)->sec_info
,
9617 bfd_elf_reloc_symbol_deleted_p
,
9620 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9628 count
= eh
->reloc_count
;
9630 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9632 cookie
.rel
= cookie
.rels
;
9633 cookie
.relend
= cookie
.rels
;
9634 if (cookie
.rels
!= NULL
)
9635 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9637 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9638 bfd_elf_reloc_symbol_deleted_p
,
9642 if (cookie
.rels
!= NULL
9643 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9647 if (bed
->elf_backend_discard_info
!= NULL
9648 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9651 if (cookie
.locsyms
!= NULL
9652 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9654 if (! info
->keep_memory
)
9655 free (cookie
.locsyms
);
9657 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9661 if (info
->eh_frame_hdr
9662 && !info
->relocatable
9663 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9670 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9673 const char *name
, *p
;
9674 struct bfd_section_already_linked
*l
;
9675 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9678 /* A single member comdat group section may be discarded by a
9679 linkonce section. See below. */
9680 if (sec
->output_section
== bfd_abs_section_ptr
)
9685 /* Check if it belongs to a section group. */
9686 group
= elf_sec_group (sec
);
9688 /* Return if it isn't a linkonce section nor a member of a group. A
9689 comdat group section also has SEC_LINK_ONCE set. */
9690 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9695 /* If this is the member of a single member comdat group, check if
9696 the group should be discarded. */
9697 if (elf_next_in_group (sec
) == sec
9698 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9704 /* FIXME: When doing a relocatable link, we may have trouble
9705 copying relocations in other sections that refer to local symbols
9706 in the section being discarded. Those relocations will have to
9707 be converted somehow; as of this writing I'm not sure that any of
9708 the backends handle that correctly.
9710 It is tempting to instead not discard link once sections when
9711 doing a relocatable link (technically, they should be discarded
9712 whenever we are building constructors). However, that fails,
9713 because the linker winds up combining all the link once sections
9714 into a single large link once section, which defeats the purpose
9715 of having link once sections in the first place.
9717 Also, not merging link once sections in a relocatable link
9718 causes trouble for MIPS ELF, which relies on link once semantics
9719 to handle the .reginfo section correctly. */
9721 name
= bfd_get_section_name (abfd
, sec
);
9723 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9724 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9729 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9731 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9733 /* We may have 3 different sections on the list: group section,
9734 comdat section and linkonce section. SEC may be a linkonce or
9735 group section. We match a group section with a group section,
9736 a linkonce section with a linkonce section, and ignore comdat
9738 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9739 && strcmp (name
, l
->sec
->name
) == 0
9740 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9742 /* The section has already been linked. See if we should
9744 switch (flags
& SEC_LINK_DUPLICATES
)
9749 case SEC_LINK_DUPLICATES_DISCARD
:
9752 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9753 (*_bfd_error_handler
)
9754 (_("%B: ignoring duplicate section `%A'"),
9758 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9759 if (sec
->size
!= l
->sec
->size
)
9760 (*_bfd_error_handler
)
9761 (_("%B: duplicate section `%A' has different size"),
9765 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9766 if (sec
->size
!= l
->sec
->size
)
9767 (*_bfd_error_handler
)
9768 (_("%B: duplicate section `%A' has different size"),
9770 else if (sec
->size
!= 0)
9772 bfd_byte
*sec_contents
, *l_sec_contents
;
9774 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9775 (*_bfd_error_handler
)
9776 (_("%B: warning: could not read contents of section `%A'"),
9778 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9780 (*_bfd_error_handler
)
9781 (_("%B: warning: could not read contents of section `%A'"),
9782 l
->sec
->owner
, l
->sec
);
9783 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9784 (*_bfd_error_handler
)
9785 (_("%B: warning: duplicate section `%A' has different contents"),
9789 free (sec_contents
);
9791 free (l_sec_contents
);
9796 /* Set the output_section field so that lang_add_section
9797 does not create a lang_input_section structure for this
9798 section. Since there might be a symbol in the section
9799 being discarded, we must retain a pointer to the section
9800 which we are really going to use. */
9801 sec
->output_section
= bfd_abs_section_ptr
;
9802 sec
->kept_section
= l
->sec
;
9804 if (flags
& SEC_GROUP
)
9806 asection
*first
= elf_next_in_group (sec
);
9807 asection
*s
= first
;
9811 s
->output_section
= bfd_abs_section_ptr
;
9812 /* Record which group discards it. */
9813 s
->kept_section
= l
->sec
;
9814 s
= elf_next_in_group (s
);
9815 /* These lists are circular. */
9827 /* If this is the member of a single member comdat group and the
9828 group hasn't be discarded, we check if it matches a linkonce
9829 section. We only record the discarded comdat group. Otherwise
9830 the undiscarded group will be discarded incorrectly later since
9831 itself has been recorded. */
9832 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9833 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9834 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9835 && bfd_elf_match_symbols_in_sections (l
->sec
,
9836 elf_next_in_group (sec
)))
9838 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9839 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9840 group
->output_section
= bfd_abs_section_ptr
;
9847 /* There is no direct match. But for linkonce section, we should
9848 check if there is a match with comdat group member. We always
9849 record the linkonce section, discarded or not. */
9850 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9851 if (l
->sec
->flags
& SEC_GROUP
)
9853 asection
*first
= elf_next_in_group (l
->sec
);
9856 && elf_next_in_group (first
) == first
9857 && bfd_elf_match_symbols_in_sections (first
, sec
))
9859 sec
->output_section
= bfd_abs_section_ptr
;
9860 sec
->kept_section
= l
->sec
;
9865 /* This is the first section with this name. Record it. */
9866 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9870 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
9872 return sym
->st_shndx
== SHN_COMMON
;
9876 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
9882 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
9884 return bfd_com_section_ptr
;