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"))
245 s
= bfd_make_section_with_flags (abfd
, ".hash", flags
| SEC_READONLY
);
247 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
249 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
252 if (info
->emit_gnu_hash
)
254 s
= bfd_make_section_with_flags (abfd
, ".gnu.hash",
255 flags
| SEC_READONLY
);
257 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
259 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
260 4 32-bit words followed by variable count of 64-bit words, then
261 variable count of 32-bit words. */
262 if (bed
->s
->arch_size
== 64)
263 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
265 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
268 /* Let the backend create the rest of the sections. This lets the
269 backend set the right flags. The backend will normally create
270 the .got and .plt sections. */
271 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
274 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
279 /* Create dynamic sections when linking against a dynamic object. */
282 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
284 flagword flags
, pltflags
;
285 struct elf_link_hash_entry
*h
;
287 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
289 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
290 .rel[a].bss sections. */
291 flags
= bed
->dynamic_sec_flags
;
294 if (bed
->plt_not_loaded
)
295 /* We do not clear SEC_ALLOC here because we still want the OS to
296 allocate space for the section; it's just that there's nothing
297 to read in from the object file. */
298 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
300 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
301 if (bed
->plt_readonly
)
302 pltflags
|= SEC_READONLY
;
304 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
306 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
309 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
311 if (bed
->want_plt_sym
)
313 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
314 "_PROCEDURE_LINKAGE_TABLE_");
315 elf_hash_table (info
)->hplt
= h
;
320 s
= bfd_make_section_with_flags (abfd
,
321 (bed
->default_use_rela_p
322 ? ".rela.plt" : ".rel.plt"),
323 flags
| SEC_READONLY
);
325 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
328 if (! _bfd_elf_create_got_section (abfd
, info
))
331 if (bed
->want_dynbss
)
333 /* The .dynbss section is a place to put symbols which are defined
334 by dynamic objects, are referenced by regular objects, and are
335 not functions. We must allocate space for them in the process
336 image and use a R_*_COPY reloc to tell the dynamic linker to
337 initialize them at run time. The linker script puts the .dynbss
338 section into the .bss section of the final image. */
339 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
341 | SEC_LINKER_CREATED
));
345 /* The .rel[a].bss section holds copy relocs. This section is not
346 normally needed. We need to create it here, though, so that the
347 linker will map it to an output section. We can't just create it
348 only if we need it, because we will not know whether we need it
349 until we have seen all the input files, and the first time the
350 main linker code calls BFD after examining all the input files
351 (size_dynamic_sections) the input sections have already been
352 mapped to the output sections. If the section turns out not to
353 be needed, we can discard it later. We will never need this
354 section when generating a shared object, since they do not use
358 s
= bfd_make_section_with_flags (abfd
,
359 (bed
->default_use_rela_p
360 ? ".rela.bss" : ".rel.bss"),
361 flags
| SEC_READONLY
);
363 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
371 /* Record a new dynamic symbol. We record the dynamic symbols as we
372 read the input files, since we need to have a list of all of them
373 before we can determine the final sizes of the output sections.
374 Note that we may actually call this function even though we are not
375 going to output any dynamic symbols; in some cases we know that a
376 symbol should be in the dynamic symbol table, but only if there is
380 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
381 struct elf_link_hash_entry
*h
)
383 if (h
->dynindx
== -1)
385 struct elf_strtab_hash
*dynstr
;
390 /* XXX: The ABI draft says the linker must turn hidden and
391 internal symbols into STB_LOCAL symbols when producing the
392 DSO. However, if ld.so honors st_other in the dynamic table,
393 this would not be necessary. */
394 switch (ELF_ST_VISIBILITY (h
->other
))
398 if (h
->root
.type
!= bfd_link_hash_undefined
399 && h
->root
.type
!= bfd_link_hash_undefweak
)
402 if (!elf_hash_table (info
)->is_relocatable_executable
)
410 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
411 ++elf_hash_table (info
)->dynsymcount
;
413 dynstr
= elf_hash_table (info
)->dynstr
;
416 /* Create a strtab to hold the dynamic symbol names. */
417 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
422 /* We don't put any version information in the dynamic string
424 name
= h
->root
.root
.string
;
425 p
= strchr (name
, ELF_VER_CHR
);
427 /* We know that the p points into writable memory. In fact,
428 there are only a few symbols that have read-only names, being
429 those like _GLOBAL_OFFSET_TABLE_ that are created specially
430 by the backends. Most symbols will have names pointing into
431 an ELF string table read from a file, or to objalloc memory. */
434 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
439 if (indx
== (bfd_size_type
) -1)
441 h
->dynstr_index
= indx
;
447 /* Record an assignment to a symbol made by a linker script. We need
448 this in case some dynamic object refers to this symbol. */
451 bfd_elf_record_link_assignment (bfd
*output_bfd
,
452 struct bfd_link_info
*info
,
457 struct elf_link_hash_entry
*h
;
458 struct elf_link_hash_table
*htab
;
460 if (!is_elf_hash_table (info
->hash
))
463 htab
= elf_hash_table (info
);
464 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
468 /* Since we're defining the symbol, don't let it seem to have not
469 been defined. record_dynamic_symbol and size_dynamic_sections
470 may depend on this. */
471 if (h
->root
.type
== bfd_link_hash_undefweak
472 || h
->root
.type
== bfd_link_hash_undefined
)
474 h
->root
.type
= bfd_link_hash_new
;
475 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
476 bfd_link_repair_undef_list (&htab
->root
);
479 if (h
->root
.type
== bfd_link_hash_new
)
482 /* If this symbol is being provided by the linker script, and it is
483 currently defined by a dynamic object, but not by a regular
484 object, then mark it as undefined so that the generic linker will
485 force the correct value. */
489 h
->root
.type
= bfd_link_hash_undefined
;
491 /* If this symbol is not being provided by the linker script, and it is
492 currently defined by a dynamic object, but not by a regular object,
493 then clear out any version information because the symbol will not be
494 associated with the dynamic object any more. */
498 h
->verinfo
.verdef
= NULL
;
502 if (provide
&& hidden
)
504 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
506 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
507 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
510 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
512 if (!info
->relocatable
514 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
515 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
521 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
524 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
527 /* If this is a weak defined symbol, and we know a corresponding
528 real symbol from the same dynamic object, make sure the real
529 symbol is also made into a dynamic symbol. */
530 if (h
->u
.weakdef
!= NULL
531 && h
->u
.weakdef
->dynindx
== -1)
533 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
541 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
542 success, and 2 on a failure caused by attempting to record a symbol
543 in a discarded section, eg. a discarded link-once section symbol. */
546 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
551 struct elf_link_local_dynamic_entry
*entry
;
552 struct elf_link_hash_table
*eht
;
553 struct elf_strtab_hash
*dynstr
;
554 unsigned long dynstr_index
;
556 Elf_External_Sym_Shndx eshndx
;
557 char esym
[sizeof (Elf64_External_Sym
)];
559 if (! is_elf_hash_table (info
->hash
))
562 /* See if the entry exists already. */
563 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
564 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
567 amt
= sizeof (*entry
);
568 entry
= bfd_alloc (input_bfd
, amt
);
572 /* Go find the symbol, so that we can find it's name. */
573 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
574 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
576 bfd_release (input_bfd
, entry
);
580 if (entry
->isym
.st_shndx
!= SHN_UNDEF
581 && (entry
->isym
.st_shndx
< SHN_LORESERVE
582 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
586 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
587 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
589 /* We can still bfd_release here as nothing has done another
590 bfd_alloc. We can't do this later in this function. */
591 bfd_release (input_bfd
, entry
);
596 name
= (bfd_elf_string_from_elf_section
597 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
598 entry
->isym
.st_name
));
600 dynstr
= elf_hash_table (info
)->dynstr
;
603 /* Create a strtab to hold the dynamic symbol names. */
604 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
609 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
610 if (dynstr_index
== (unsigned long) -1)
612 entry
->isym
.st_name
= dynstr_index
;
614 eht
= elf_hash_table (info
);
616 entry
->next
= eht
->dynlocal
;
617 eht
->dynlocal
= entry
;
618 entry
->input_bfd
= input_bfd
;
619 entry
->input_indx
= input_indx
;
622 /* Whatever binding the symbol had before, it's now local. */
624 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
626 /* The dynindx will be set at the end of size_dynamic_sections. */
631 /* Return the dynindex of a local dynamic symbol. */
634 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
638 struct elf_link_local_dynamic_entry
*e
;
640 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
641 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
646 /* This function is used to renumber the dynamic symbols, if some of
647 them are removed because they are marked as local. This is called
648 via elf_link_hash_traverse. */
651 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
654 size_t *count
= data
;
656 if (h
->root
.type
== bfd_link_hash_warning
)
657 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
662 if (h
->dynindx
!= -1)
663 h
->dynindx
= ++(*count
);
669 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
670 STB_LOCAL binding. */
673 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
676 size_t *count
= data
;
678 if (h
->root
.type
== bfd_link_hash_warning
)
679 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
681 if (!h
->forced_local
)
684 if (h
->dynindx
!= -1)
685 h
->dynindx
= ++(*count
);
690 /* Return true if the dynamic symbol for a given section should be
691 omitted when creating a shared library. */
693 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
694 struct bfd_link_info
*info
,
697 switch (elf_section_data (p
)->this_hdr
.sh_type
)
701 /* If sh_type is yet undecided, assume it could be
702 SHT_PROGBITS/SHT_NOBITS. */
704 if (strcmp (p
->name
, ".got") == 0
705 || strcmp (p
->name
, ".got.plt") == 0
706 || strcmp (p
->name
, ".plt") == 0)
709 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
712 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
713 && (ip
->flags
& SEC_LINKER_CREATED
)
714 && ip
->output_section
== p
)
719 /* There shouldn't be section relative relocations
720 against any other section. */
726 /* Assign dynsym indices. In a shared library we generate a section
727 symbol for each output section, which come first. Next come symbols
728 which have been forced to local binding. Then all of the back-end
729 allocated local dynamic syms, followed by the rest of the global
733 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
734 struct bfd_link_info
*info
,
735 unsigned long *section_sym_count
)
737 unsigned long dynsymcount
= 0;
739 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
741 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
743 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
744 if ((p
->flags
& SEC_EXCLUDE
) == 0
745 && (p
->flags
& SEC_ALLOC
) != 0
746 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
747 elf_section_data (p
)->dynindx
= ++dynsymcount
;
749 *section_sym_count
= dynsymcount
;
751 elf_link_hash_traverse (elf_hash_table (info
),
752 elf_link_renumber_local_hash_table_dynsyms
,
755 if (elf_hash_table (info
)->dynlocal
)
757 struct elf_link_local_dynamic_entry
*p
;
758 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
759 p
->dynindx
= ++dynsymcount
;
762 elf_link_hash_traverse (elf_hash_table (info
),
763 elf_link_renumber_hash_table_dynsyms
,
766 /* There is an unused NULL entry at the head of the table which
767 we must account for in our count. Unless there weren't any
768 symbols, which means we'll have no table at all. */
769 if (dynsymcount
!= 0)
772 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
776 /* This function is called when we want to define a new symbol. It
777 handles the various cases which arise when we find a definition in
778 a dynamic object, or when there is already a definition in a
779 dynamic object. The new symbol is described by NAME, SYM, PSEC,
780 and PVALUE. We set SYM_HASH to the hash table entry. We set
781 OVERRIDE if the old symbol is overriding a new definition. We set
782 TYPE_CHANGE_OK if it is OK for the type to change. We set
783 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
784 change, we mean that we shouldn't warn if the type or size does
785 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
786 object is overridden by a regular object. */
789 _bfd_elf_merge_symbol (bfd
*abfd
,
790 struct bfd_link_info
*info
,
792 Elf_Internal_Sym
*sym
,
795 unsigned int *pold_alignment
,
796 struct elf_link_hash_entry
**sym_hash
,
798 bfd_boolean
*override
,
799 bfd_boolean
*type_change_ok
,
800 bfd_boolean
*size_change_ok
)
802 asection
*sec
, *oldsec
;
803 struct elf_link_hash_entry
*h
;
804 struct elf_link_hash_entry
*flip
;
807 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
808 bfd_boolean newweak
, oldweak
;
809 const struct elf_backend_data
*bed
;
815 bind
= ELF_ST_BIND (sym
->st_info
);
817 if (! bfd_is_und_section (sec
))
818 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
820 h
= ((struct elf_link_hash_entry
*)
821 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
826 /* This code is for coping with dynamic objects, and is only useful
827 if we are doing an ELF link. */
828 if (info
->hash
->creator
!= abfd
->xvec
)
831 /* For merging, we only care about real symbols. */
833 while (h
->root
.type
== bfd_link_hash_indirect
834 || h
->root
.type
== bfd_link_hash_warning
)
835 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
837 /* If we just created the symbol, mark it as being an ELF symbol.
838 Other than that, there is nothing to do--there is no merge issue
839 with a newly defined symbol--so we just return. */
841 if (h
->root
.type
== bfd_link_hash_new
)
847 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
850 switch (h
->root
.type
)
857 case bfd_link_hash_undefined
:
858 case bfd_link_hash_undefweak
:
859 oldbfd
= h
->root
.u
.undef
.abfd
;
863 case bfd_link_hash_defined
:
864 case bfd_link_hash_defweak
:
865 oldbfd
= h
->root
.u
.def
.section
->owner
;
866 oldsec
= h
->root
.u
.def
.section
;
869 case bfd_link_hash_common
:
870 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
871 oldsec
= h
->root
.u
.c
.p
->section
;
875 /* In cases involving weak versioned symbols, we may wind up trying
876 to merge a symbol with itself. Catch that here, to avoid the
877 confusion that results if we try to override a symbol with
878 itself. The additional tests catch cases like
879 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
880 dynamic object, which we do want to handle here. */
882 && ((abfd
->flags
& DYNAMIC
) == 0
886 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
887 respectively, is from a dynamic object. */
889 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
893 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
894 else if (oldsec
!= NULL
)
896 /* This handles the special SHN_MIPS_{TEXT,DATA} section
897 indices used by MIPS ELF. */
898 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
901 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
902 respectively, appear to be a definition rather than reference. */
904 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
906 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
907 && h
->root
.type
!= bfd_link_hash_undefweak
908 && h
->root
.type
!= bfd_link_hash_common
);
910 /* When we try to create a default indirect symbol from the dynamic
911 definition with the default version, we skip it if its type and
912 the type of existing regular definition mismatch. We only do it
913 if the existing regular definition won't be dynamic. */
914 if (pold_alignment
== NULL
916 && !info
->export_dynamic
921 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
922 && ELF_ST_TYPE (sym
->st_info
) != h
->type
923 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
924 && h
->type
!= STT_NOTYPE
)
930 /* Check TLS symbol. We don't check undefined symbol introduced by
932 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
933 && ELF_ST_TYPE (sym
->st_info
) != h
->type
937 bfd_boolean ntdef
, tdef
;
938 asection
*ntsec
, *tsec
;
940 if (h
->type
== STT_TLS
)
960 (*_bfd_error_handler
)
961 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
962 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
963 else if (!tdef
&& !ntdef
)
964 (*_bfd_error_handler
)
965 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
966 tbfd
, ntbfd
, h
->root
.root
.string
);
968 (*_bfd_error_handler
)
969 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
970 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
972 (*_bfd_error_handler
)
973 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
974 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
976 bfd_set_error (bfd_error_bad_value
);
980 /* We need to remember if a symbol has a definition in a dynamic
981 object or is weak in all dynamic objects. Internal and hidden
982 visibility will make it unavailable to dynamic objects. */
983 if (newdyn
&& !h
->dynamic_def
)
985 if (!bfd_is_und_section (sec
))
989 /* Check if this symbol is weak in all dynamic objects. If it
990 is the first time we see it in a dynamic object, we mark
991 if it is weak. Otherwise, we clear it. */
994 if (bind
== STB_WEAK
)
997 else if (bind
!= STB_WEAK
)
1002 /* If the old symbol has non-default visibility, we ignore the new
1003 definition from a dynamic object. */
1005 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1006 && !bfd_is_und_section (sec
))
1009 /* Make sure this symbol is dynamic. */
1011 /* A protected symbol has external availability. Make sure it is
1012 recorded as dynamic.
1014 FIXME: Should we check type and size for protected symbol? */
1015 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1016 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1021 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1024 /* If the new symbol with non-default visibility comes from a
1025 relocatable file and the old definition comes from a dynamic
1026 object, we remove the old definition. */
1027 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1029 /* Handle the case where the old dynamic definition is
1030 default versioned. We need to copy the symbol info from
1031 the symbol with default version to the normal one if it
1032 was referenced before. */
1035 const struct elf_backend_data
*bed
1036 = get_elf_backend_data (abfd
);
1037 struct elf_link_hash_entry
*vh
= *sym_hash
;
1038 vh
->root
.type
= h
->root
.type
;
1039 h
->root
.type
= bfd_link_hash_indirect
;
1040 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1041 /* Protected symbols will override the dynamic definition
1042 with default version. */
1043 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1045 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1046 vh
->dynamic_def
= 1;
1047 vh
->ref_dynamic
= 1;
1051 h
->root
.type
= vh
->root
.type
;
1052 vh
->ref_dynamic
= 0;
1053 /* We have to hide it here since it was made dynamic
1054 global with extra bits when the symbol info was
1055 copied from the old dynamic definition. */
1056 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1064 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1065 && bfd_is_und_section (sec
))
1067 /* If the new symbol is undefined and the old symbol was
1068 also undefined before, we need to make sure
1069 _bfd_generic_link_add_one_symbol doesn't mess
1070 up the linker hash table undefs list. Since the old
1071 definition came from a dynamic object, it is still on the
1073 h
->root
.type
= bfd_link_hash_undefined
;
1074 h
->root
.u
.undef
.abfd
= abfd
;
1078 h
->root
.type
= bfd_link_hash_new
;
1079 h
->root
.u
.undef
.abfd
= NULL
;
1088 /* FIXME: Should we check type and size for protected symbol? */
1094 /* Differentiate strong and weak symbols. */
1095 newweak
= bind
== STB_WEAK
;
1096 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1097 || h
->root
.type
== bfd_link_hash_undefweak
);
1099 /* If a new weak symbol definition comes from a regular file and the
1100 old symbol comes from a dynamic library, we treat the new one as
1101 strong. Similarly, an old weak symbol definition from a regular
1102 file is treated as strong when the new symbol comes from a dynamic
1103 library. Further, an old weak symbol from a dynamic library is
1104 treated as strong if the new symbol is from a dynamic library.
1105 This reflects the way glibc's ld.so works.
1107 Do this before setting *type_change_ok or *size_change_ok so that
1108 we warn properly when dynamic library symbols are overridden. */
1110 if (newdef
&& !newdyn
&& olddyn
)
1112 if (olddef
&& newdyn
)
1115 /* It's OK to change the type if either the existing symbol or the
1116 new symbol is weak. A type change is also OK if the old symbol
1117 is undefined and the new symbol is defined. */
1122 && h
->root
.type
== bfd_link_hash_undefined
))
1123 *type_change_ok
= TRUE
;
1125 /* It's OK to change the size if either the existing symbol or the
1126 new symbol is weak, or if the old symbol is undefined. */
1129 || h
->root
.type
== bfd_link_hash_undefined
)
1130 *size_change_ok
= TRUE
;
1132 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1133 symbol, respectively, appears to be a common symbol in a dynamic
1134 object. If a symbol appears in an uninitialized section, and is
1135 not weak, and is not a function, then it may be a common symbol
1136 which was resolved when the dynamic object was created. We want
1137 to treat such symbols specially, because they raise special
1138 considerations when setting the symbol size: if the symbol
1139 appears as a common symbol in a regular object, and the size in
1140 the regular object is larger, we must make sure that we use the
1141 larger size. This problematic case can always be avoided in C,
1142 but it must be handled correctly when using Fortran shared
1145 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1146 likewise for OLDDYNCOMMON and OLDDEF.
1148 Note that this test is just a heuristic, and that it is quite
1149 possible to have an uninitialized symbol in a shared object which
1150 is really a definition, rather than a common symbol. This could
1151 lead to some minor confusion when the symbol really is a common
1152 symbol in some regular object. However, I think it will be
1158 && (sec
->flags
& SEC_ALLOC
) != 0
1159 && (sec
->flags
& SEC_LOAD
) == 0
1161 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1162 newdyncommon
= TRUE
;
1164 newdyncommon
= FALSE
;
1168 && h
->root
.type
== bfd_link_hash_defined
1170 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1171 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1173 && h
->type
!= STT_FUNC
)
1174 olddyncommon
= TRUE
;
1176 olddyncommon
= FALSE
;
1178 /* We now know everything about the old and new symbols. We ask the
1179 backend to check if we can merge them. */
1180 bed
= get_elf_backend_data (abfd
);
1181 if (bed
->merge_symbol
1182 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1183 pold_alignment
, skip
, override
,
1184 type_change_ok
, size_change_ok
,
1185 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1187 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1191 /* If both the old and the new symbols look like common symbols in a
1192 dynamic object, set the size of the symbol to the larger of the
1197 && sym
->st_size
!= h
->size
)
1199 /* Since we think we have two common symbols, issue a multiple
1200 common warning if desired. Note that we only warn if the
1201 size is different. If the size is the same, we simply let
1202 the old symbol override the new one as normally happens with
1203 symbols defined in dynamic objects. */
1205 if (! ((*info
->callbacks
->multiple_common
)
1206 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1207 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1210 if (sym
->st_size
> h
->size
)
1211 h
->size
= sym
->st_size
;
1213 *size_change_ok
= TRUE
;
1216 /* If we are looking at a dynamic object, and we have found a
1217 definition, we need to see if the symbol was already defined by
1218 some other object. If so, we want to use the existing
1219 definition, and we do not want to report a multiple symbol
1220 definition error; we do this by clobbering *PSEC to be
1221 bfd_und_section_ptr.
1223 We treat a common symbol as a definition if the symbol in the
1224 shared library is a function, since common symbols always
1225 represent variables; this can cause confusion in principle, but
1226 any such confusion would seem to indicate an erroneous program or
1227 shared library. We also permit a common symbol in a regular
1228 object to override a weak symbol in a shared object. */
1233 || (h
->root
.type
== bfd_link_hash_common
1235 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1239 newdyncommon
= FALSE
;
1241 *psec
= sec
= bfd_und_section_ptr
;
1242 *size_change_ok
= TRUE
;
1244 /* If we get here when the old symbol is a common symbol, then
1245 we are explicitly letting it override a weak symbol or
1246 function in a dynamic object, and we don't want to warn about
1247 a type change. If the old symbol is a defined symbol, a type
1248 change warning may still be appropriate. */
1250 if (h
->root
.type
== bfd_link_hash_common
)
1251 *type_change_ok
= TRUE
;
1254 /* Handle the special case of an old common symbol merging with a
1255 new symbol which looks like a common symbol in a shared object.
1256 We change *PSEC and *PVALUE to make the new symbol look like a
1257 common symbol, and let _bfd_generic_link_add_one_symbol do the
1261 && h
->root
.type
== bfd_link_hash_common
)
1265 newdyncommon
= FALSE
;
1266 *pvalue
= sym
->st_size
;
1267 *psec
= sec
= bed
->common_section (oldsec
);
1268 *size_change_ok
= TRUE
;
1271 /* Skip weak definitions of symbols that are already defined. */
1272 if (newdef
&& olddef
&& newweak
)
1275 /* If the old symbol is from a dynamic object, and the new symbol is
1276 a definition which is not from a dynamic object, then the new
1277 symbol overrides the old symbol. Symbols from regular files
1278 always take precedence over symbols from dynamic objects, even if
1279 they are defined after the dynamic object in the link.
1281 As above, we again permit a common symbol in a regular object to
1282 override a definition in a shared object if the shared object
1283 symbol is a function or is weak. */
1288 || (bfd_is_com_section (sec
)
1290 || h
->type
== STT_FUNC
)))
1295 /* Change the hash table entry to undefined, and let
1296 _bfd_generic_link_add_one_symbol do the right thing with the
1299 h
->root
.type
= bfd_link_hash_undefined
;
1300 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1301 *size_change_ok
= TRUE
;
1304 olddyncommon
= FALSE
;
1306 /* We again permit a type change when a common symbol may be
1307 overriding a function. */
1309 if (bfd_is_com_section (sec
))
1310 *type_change_ok
= TRUE
;
1312 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1315 /* This union may have been set to be non-NULL when this symbol
1316 was seen in a dynamic object. We must force the union to be
1317 NULL, so that it is correct for a regular symbol. */
1318 h
->verinfo
.vertree
= NULL
;
1321 /* Handle the special case of a new common symbol merging with an
1322 old symbol that looks like it might be a common symbol defined in
1323 a shared object. Note that we have already handled the case in
1324 which a new common symbol should simply override the definition
1325 in the shared library. */
1328 && bfd_is_com_section (sec
)
1331 /* It would be best if we could set the hash table entry to a
1332 common symbol, but we don't know what to use for the section
1333 or the alignment. */
1334 if (! ((*info
->callbacks
->multiple_common
)
1335 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1336 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1339 /* If the presumed common symbol in the dynamic object is
1340 larger, pretend that the new symbol has its size. */
1342 if (h
->size
> *pvalue
)
1345 /* We need to remember the alignment required by the symbol
1346 in the dynamic object. */
1347 BFD_ASSERT (pold_alignment
);
1348 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1351 olddyncommon
= FALSE
;
1353 h
->root
.type
= bfd_link_hash_undefined
;
1354 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1356 *size_change_ok
= TRUE
;
1357 *type_change_ok
= TRUE
;
1359 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1362 h
->verinfo
.vertree
= NULL
;
1367 /* Handle the case where we had a versioned symbol in a dynamic
1368 library and now find a definition in a normal object. In this
1369 case, we make the versioned symbol point to the normal one. */
1370 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1371 flip
->root
.type
= h
->root
.type
;
1372 h
->root
.type
= bfd_link_hash_indirect
;
1373 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1374 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1375 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1379 flip
->ref_dynamic
= 1;
1386 /* This function is called to create an indirect symbol from the
1387 default for the symbol with the default version if needed. The
1388 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1389 set DYNSYM if the new indirect symbol is dynamic. */
1392 _bfd_elf_add_default_symbol (bfd
*abfd
,
1393 struct bfd_link_info
*info
,
1394 struct elf_link_hash_entry
*h
,
1396 Elf_Internal_Sym
*sym
,
1399 bfd_boolean
*dynsym
,
1400 bfd_boolean override
)
1402 bfd_boolean type_change_ok
;
1403 bfd_boolean size_change_ok
;
1406 struct elf_link_hash_entry
*hi
;
1407 struct bfd_link_hash_entry
*bh
;
1408 const struct elf_backend_data
*bed
;
1409 bfd_boolean collect
;
1410 bfd_boolean dynamic
;
1412 size_t len
, shortlen
;
1415 /* If this symbol has a version, and it is the default version, we
1416 create an indirect symbol from the default name to the fully
1417 decorated name. This will cause external references which do not
1418 specify a version to be bound to this version of the symbol. */
1419 p
= strchr (name
, ELF_VER_CHR
);
1420 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1425 /* We are overridden by an old definition. We need to check if we
1426 need to create the indirect symbol from the default name. */
1427 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1429 BFD_ASSERT (hi
!= NULL
);
1432 while (hi
->root
.type
== bfd_link_hash_indirect
1433 || hi
->root
.type
== bfd_link_hash_warning
)
1435 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1441 bed
= get_elf_backend_data (abfd
);
1442 collect
= bed
->collect
;
1443 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1445 shortlen
= p
- name
;
1446 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1447 if (shortname
== NULL
)
1449 memcpy (shortname
, name
, shortlen
);
1450 shortname
[shortlen
] = '\0';
1452 /* We are going to create a new symbol. Merge it with any existing
1453 symbol with this name. For the purposes of the merge, act as
1454 though we were defining the symbol we just defined, although we
1455 actually going to define an indirect symbol. */
1456 type_change_ok
= FALSE
;
1457 size_change_ok
= FALSE
;
1459 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1460 NULL
, &hi
, &skip
, &override
,
1461 &type_change_ok
, &size_change_ok
))
1470 if (! (_bfd_generic_link_add_one_symbol
1471 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1472 0, name
, FALSE
, collect
, &bh
)))
1474 hi
= (struct elf_link_hash_entry
*) bh
;
1478 /* In this case the symbol named SHORTNAME is overriding the
1479 indirect symbol we want to add. We were planning on making
1480 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1481 is the name without a version. NAME is the fully versioned
1482 name, and it is the default version.
1484 Overriding means that we already saw a definition for the
1485 symbol SHORTNAME in a regular object, and it is overriding
1486 the symbol defined in the dynamic object.
1488 When this happens, we actually want to change NAME, the
1489 symbol we just added, to refer to SHORTNAME. This will cause
1490 references to NAME in the shared object to become references
1491 to SHORTNAME in the regular object. This is what we expect
1492 when we override a function in a shared object: that the
1493 references in the shared object will be mapped to the
1494 definition in the regular object. */
1496 while (hi
->root
.type
== bfd_link_hash_indirect
1497 || hi
->root
.type
== bfd_link_hash_warning
)
1498 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1500 h
->root
.type
= bfd_link_hash_indirect
;
1501 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1505 hi
->ref_dynamic
= 1;
1509 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1514 /* Now set HI to H, so that the following code will set the
1515 other fields correctly. */
1519 /* If there is a duplicate definition somewhere, then HI may not
1520 point to an indirect symbol. We will have reported an error to
1521 the user in that case. */
1523 if (hi
->root
.type
== bfd_link_hash_indirect
)
1525 struct elf_link_hash_entry
*ht
;
1527 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1528 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1530 /* See if the new flags lead us to realize that the symbol must
1542 if (hi
->ref_regular
)
1548 /* We also need to define an indirection from the nondefault version
1552 len
= strlen (name
);
1553 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1554 if (shortname
== NULL
)
1556 memcpy (shortname
, name
, shortlen
);
1557 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1559 /* Once again, merge with any existing symbol. */
1560 type_change_ok
= FALSE
;
1561 size_change_ok
= FALSE
;
1563 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1564 NULL
, &hi
, &skip
, &override
,
1565 &type_change_ok
, &size_change_ok
))
1573 /* Here SHORTNAME is a versioned name, so we don't expect to see
1574 the type of override we do in the case above unless it is
1575 overridden by a versioned definition. */
1576 if (hi
->root
.type
!= bfd_link_hash_defined
1577 && hi
->root
.type
!= bfd_link_hash_defweak
)
1578 (*_bfd_error_handler
)
1579 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1585 if (! (_bfd_generic_link_add_one_symbol
1586 (info
, abfd
, shortname
, BSF_INDIRECT
,
1587 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1589 hi
= (struct elf_link_hash_entry
*) bh
;
1591 /* If there is a duplicate definition somewhere, then HI may not
1592 point to an indirect symbol. We will have reported an error
1593 to the user in that case. */
1595 if (hi
->root
.type
== bfd_link_hash_indirect
)
1597 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1599 /* See if the new flags lead us to realize that the symbol
1611 if (hi
->ref_regular
)
1621 /* This routine is used to export all defined symbols into the dynamic
1622 symbol table. It is called via elf_link_hash_traverse. */
1625 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1627 struct elf_info_failed
*eif
= data
;
1629 /* Ignore indirect symbols. These are added by the versioning code. */
1630 if (h
->root
.type
== bfd_link_hash_indirect
)
1633 if (h
->root
.type
== bfd_link_hash_warning
)
1634 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1636 if (h
->dynindx
== -1
1640 struct bfd_elf_version_tree
*t
;
1641 struct bfd_elf_version_expr
*d
;
1643 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1645 if (t
->globals
.list
!= NULL
)
1647 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1652 if (t
->locals
.list
!= NULL
)
1654 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1663 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1674 /* Look through the symbols which are defined in other shared
1675 libraries and referenced here. Update the list of version
1676 dependencies. This will be put into the .gnu.version_r section.
1677 This function is called via elf_link_hash_traverse. */
1680 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1683 struct elf_find_verdep_info
*rinfo
= data
;
1684 Elf_Internal_Verneed
*t
;
1685 Elf_Internal_Vernaux
*a
;
1688 if (h
->root
.type
== bfd_link_hash_warning
)
1689 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1691 /* We only care about symbols defined in shared objects with version
1696 || h
->verinfo
.verdef
== NULL
)
1699 /* See if we already know about this version. */
1700 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1702 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1705 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1706 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1712 /* This is a new version. Add it to tree we are building. */
1717 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1720 rinfo
->failed
= TRUE
;
1724 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1725 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1726 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1730 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1732 /* Note that we are copying a string pointer here, and testing it
1733 above. If bfd_elf_string_from_elf_section is ever changed to
1734 discard the string data when low in memory, this will have to be
1736 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1738 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1739 a
->vna_nextptr
= t
->vn_auxptr
;
1741 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1744 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1751 /* Figure out appropriate versions for all the symbols. We may not
1752 have the version number script until we have read all of the input
1753 files, so until that point we don't know which symbols should be
1754 local. This function is called via elf_link_hash_traverse. */
1757 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1759 struct elf_assign_sym_version_info
*sinfo
;
1760 struct bfd_link_info
*info
;
1761 const struct elf_backend_data
*bed
;
1762 struct elf_info_failed eif
;
1769 if (h
->root
.type
== bfd_link_hash_warning
)
1770 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1772 /* Fix the symbol flags. */
1775 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1778 sinfo
->failed
= TRUE
;
1782 /* We only need version numbers for symbols defined in regular
1784 if (!h
->def_regular
)
1787 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1788 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1789 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1791 struct bfd_elf_version_tree
*t
;
1796 /* There are two consecutive ELF_VER_CHR characters if this is
1797 not a hidden symbol. */
1799 if (*p
== ELF_VER_CHR
)
1805 /* If there is no version string, we can just return out. */
1813 /* Look for the version. If we find it, it is no longer weak. */
1814 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1816 if (strcmp (t
->name
, p
) == 0)
1820 struct bfd_elf_version_expr
*d
;
1822 len
= p
- h
->root
.root
.string
;
1823 alc
= bfd_malloc (len
);
1826 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1827 alc
[len
- 1] = '\0';
1828 if (alc
[len
- 2] == ELF_VER_CHR
)
1829 alc
[len
- 2] = '\0';
1831 h
->verinfo
.vertree
= t
;
1835 if (t
->globals
.list
!= NULL
)
1836 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1838 /* See if there is anything to force this symbol to
1840 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1842 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1845 && ! info
->export_dynamic
)
1846 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1854 /* If we are building an application, we need to create a
1855 version node for this version. */
1856 if (t
== NULL
&& info
->executable
)
1858 struct bfd_elf_version_tree
**pp
;
1861 /* If we aren't going to export this symbol, we don't need
1862 to worry about it. */
1863 if (h
->dynindx
== -1)
1867 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1870 sinfo
->failed
= TRUE
;
1875 t
->name_indx
= (unsigned int) -1;
1879 /* Don't count anonymous version tag. */
1880 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1882 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1884 t
->vernum
= version_index
;
1888 h
->verinfo
.vertree
= t
;
1892 /* We could not find the version for a symbol when
1893 generating a shared archive. Return an error. */
1894 (*_bfd_error_handler
)
1895 (_("%B: undefined versioned symbol name %s"),
1896 sinfo
->output_bfd
, h
->root
.root
.string
);
1897 bfd_set_error (bfd_error_bad_value
);
1898 sinfo
->failed
= TRUE
;
1906 /* If we don't have a version for this symbol, see if we can find
1908 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1910 struct bfd_elf_version_tree
*t
;
1911 struct bfd_elf_version_tree
*local_ver
;
1912 struct bfd_elf_version_expr
*d
;
1914 /* See if can find what version this symbol is in. If the
1915 symbol is supposed to be local, then don't actually register
1918 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1920 if (t
->globals
.list
!= NULL
)
1922 bfd_boolean matched
;
1926 while ((d
= (*t
->match
) (&t
->globals
, d
,
1927 h
->root
.root
.string
)) != NULL
)
1932 /* There is a version without definition. Make
1933 the symbol the default definition for this
1935 h
->verinfo
.vertree
= t
;
1943 /* There is no undefined version for this symbol. Hide the
1945 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1948 if (t
->locals
.list
!= NULL
)
1951 while ((d
= (*t
->match
) (&t
->locals
, d
,
1952 h
->root
.root
.string
)) != NULL
)
1955 /* If the match is "*", keep looking for a more
1956 explicit, perhaps even global, match.
1957 XXX: Shouldn't this be !d->wildcard instead? */
1958 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1967 if (local_ver
!= NULL
)
1969 h
->verinfo
.vertree
= local_ver
;
1970 if (h
->dynindx
!= -1
1971 && ! info
->export_dynamic
)
1973 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1981 /* Read and swap the relocs from the section indicated by SHDR. This
1982 may be either a REL or a RELA section. The relocations are
1983 translated into RELA relocations and stored in INTERNAL_RELOCS,
1984 which should have already been allocated to contain enough space.
1985 The EXTERNAL_RELOCS are a buffer where the external form of the
1986 relocations should be stored.
1988 Returns FALSE if something goes wrong. */
1991 elf_link_read_relocs_from_section (bfd
*abfd
,
1993 Elf_Internal_Shdr
*shdr
,
1994 void *external_relocs
,
1995 Elf_Internal_Rela
*internal_relocs
)
1997 const struct elf_backend_data
*bed
;
1998 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1999 const bfd_byte
*erela
;
2000 const bfd_byte
*erelaend
;
2001 Elf_Internal_Rela
*irela
;
2002 Elf_Internal_Shdr
*symtab_hdr
;
2005 /* Position ourselves at the start of the section. */
2006 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2009 /* Read the relocations. */
2010 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2013 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2014 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2016 bed
= get_elf_backend_data (abfd
);
2018 /* Convert the external relocations to the internal format. */
2019 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2020 swap_in
= bed
->s
->swap_reloc_in
;
2021 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2022 swap_in
= bed
->s
->swap_reloca_in
;
2025 bfd_set_error (bfd_error_wrong_format
);
2029 erela
= external_relocs
;
2030 erelaend
= erela
+ shdr
->sh_size
;
2031 irela
= internal_relocs
;
2032 while (erela
< erelaend
)
2036 (*swap_in
) (abfd
, erela
, irela
);
2037 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2038 if (bed
->s
->arch_size
== 64)
2040 if ((size_t) r_symndx
>= nsyms
)
2042 (*_bfd_error_handler
)
2043 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2044 " for offset 0x%lx in section `%A'"),
2046 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2047 bfd_set_error (bfd_error_bad_value
);
2050 irela
+= bed
->s
->int_rels_per_ext_rel
;
2051 erela
+= shdr
->sh_entsize
;
2057 /* Read and swap the relocs for a section O. They may have been
2058 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2059 not NULL, they are used as buffers to read into. They are known to
2060 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2061 the return value is allocated using either malloc or bfd_alloc,
2062 according to the KEEP_MEMORY argument. If O has two relocation
2063 sections (both REL and RELA relocations), then the REL_HDR
2064 relocations will appear first in INTERNAL_RELOCS, followed by the
2065 REL_HDR2 relocations. */
2068 _bfd_elf_link_read_relocs (bfd
*abfd
,
2070 void *external_relocs
,
2071 Elf_Internal_Rela
*internal_relocs
,
2072 bfd_boolean keep_memory
)
2074 Elf_Internal_Shdr
*rel_hdr
;
2075 void *alloc1
= NULL
;
2076 Elf_Internal_Rela
*alloc2
= NULL
;
2077 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2079 if (elf_section_data (o
)->relocs
!= NULL
)
2080 return elf_section_data (o
)->relocs
;
2082 if (o
->reloc_count
== 0)
2085 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2087 if (internal_relocs
== NULL
)
2091 size
= o
->reloc_count
;
2092 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2094 internal_relocs
= bfd_alloc (abfd
, size
);
2096 internal_relocs
= alloc2
= bfd_malloc (size
);
2097 if (internal_relocs
== NULL
)
2101 if (external_relocs
== NULL
)
2103 bfd_size_type size
= rel_hdr
->sh_size
;
2105 if (elf_section_data (o
)->rel_hdr2
)
2106 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2107 alloc1
= bfd_malloc (size
);
2110 external_relocs
= alloc1
;
2113 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2117 if (elf_section_data (o
)->rel_hdr2
2118 && (!elf_link_read_relocs_from_section
2120 elf_section_data (o
)->rel_hdr2
,
2121 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2122 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2123 * bed
->s
->int_rels_per_ext_rel
))))
2126 /* Cache the results for next time, if we can. */
2128 elf_section_data (o
)->relocs
= internal_relocs
;
2133 /* Don't free alloc2, since if it was allocated we are passing it
2134 back (under the name of internal_relocs). */
2136 return internal_relocs
;
2146 /* Compute the size of, and allocate space for, REL_HDR which is the
2147 section header for a section containing relocations for O. */
2150 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2151 Elf_Internal_Shdr
*rel_hdr
,
2154 bfd_size_type reloc_count
;
2155 bfd_size_type num_rel_hashes
;
2157 /* Figure out how many relocations there will be. */
2158 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2159 reloc_count
= elf_section_data (o
)->rel_count
;
2161 reloc_count
= elf_section_data (o
)->rel_count2
;
2163 num_rel_hashes
= o
->reloc_count
;
2164 if (num_rel_hashes
< reloc_count
)
2165 num_rel_hashes
= reloc_count
;
2167 /* That allows us to calculate the size of the section. */
2168 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2170 /* The contents field must last into write_object_contents, so we
2171 allocate it with bfd_alloc rather than malloc. Also since we
2172 cannot be sure that the contents will actually be filled in,
2173 we zero the allocated space. */
2174 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2175 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2178 /* We only allocate one set of hash entries, so we only do it the
2179 first time we are called. */
2180 if (elf_section_data (o
)->rel_hashes
== NULL
2183 struct elf_link_hash_entry
**p
;
2185 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2189 elf_section_data (o
)->rel_hashes
= p
;
2195 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2196 originated from the section given by INPUT_REL_HDR) to the
2200 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2201 asection
*input_section
,
2202 Elf_Internal_Shdr
*input_rel_hdr
,
2203 Elf_Internal_Rela
*internal_relocs
,
2204 struct elf_link_hash_entry
**rel_hash
2207 Elf_Internal_Rela
*irela
;
2208 Elf_Internal_Rela
*irelaend
;
2210 Elf_Internal_Shdr
*output_rel_hdr
;
2211 asection
*output_section
;
2212 unsigned int *rel_countp
= NULL
;
2213 const struct elf_backend_data
*bed
;
2214 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2216 output_section
= input_section
->output_section
;
2217 output_rel_hdr
= NULL
;
2219 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2220 == input_rel_hdr
->sh_entsize
)
2222 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2223 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2225 else if (elf_section_data (output_section
)->rel_hdr2
2226 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2227 == input_rel_hdr
->sh_entsize
))
2229 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2230 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2234 (*_bfd_error_handler
)
2235 (_("%B: relocation size mismatch in %B section %A"),
2236 output_bfd
, input_section
->owner
, input_section
);
2237 bfd_set_error (bfd_error_wrong_object_format
);
2241 bed
= get_elf_backend_data (output_bfd
);
2242 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2243 swap_out
= bed
->s
->swap_reloc_out
;
2244 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2245 swap_out
= bed
->s
->swap_reloca_out
;
2249 erel
= output_rel_hdr
->contents
;
2250 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2251 irela
= internal_relocs
;
2252 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2253 * bed
->s
->int_rels_per_ext_rel
);
2254 while (irela
< irelaend
)
2256 (*swap_out
) (output_bfd
, irela
, erel
);
2257 irela
+= bed
->s
->int_rels_per_ext_rel
;
2258 erel
+= input_rel_hdr
->sh_entsize
;
2261 /* Bump the counter, so that we know where to add the next set of
2263 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2268 /* Make weak undefined symbols in PIE dynamic. */
2271 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2272 struct elf_link_hash_entry
*h
)
2276 && h
->root
.type
== bfd_link_hash_undefweak
)
2277 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2282 /* Fix up the flags for a symbol. This handles various cases which
2283 can only be fixed after all the input files are seen. This is
2284 currently called by both adjust_dynamic_symbol and
2285 assign_sym_version, which is unnecessary but perhaps more robust in
2286 the face of future changes. */
2289 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2290 struct elf_info_failed
*eif
)
2292 const struct elf_backend_data
*bed
= NULL
;
2294 /* If this symbol was mentioned in a non-ELF file, try to set
2295 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2296 permit a non-ELF file to correctly refer to a symbol defined in
2297 an ELF dynamic object. */
2300 while (h
->root
.type
== bfd_link_hash_indirect
)
2301 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2303 if (h
->root
.type
!= bfd_link_hash_defined
2304 && h
->root
.type
!= bfd_link_hash_defweak
)
2307 h
->ref_regular_nonweak
= 1;
2311 if (h
->root
.u
.def
.section
->owner
!= NULL
2312 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2313 == bfd_target_elf_flavour
))
2316 h
->ref_regular_nonweak
= 1;
2322 if (h
->dynindx
== -1
2326 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2335 /* Unfortunately, NON_ELF is only correct if the symbol
2336 was first seen in a non-ELF file. Fortunately, if the symbol
2337 was first seen in an ELF file, we're probably OK unless the
2338 symbol was defined in a non-ELF file. Catch that case here.
2339 FIXME: We're still in trouble if the symbol was first seen in
2340 a dynamic object, and then later in a non-ELF regular object. */
2341 if ((h
->root
.type
== bfd_link_hash_defined
2342 || h
->root
.type
== bfd_link_hash_defweak
)
2344 && (h
->root
.u
.def
.section
->owner
!= NULL
2345 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2346 != bfd_target_elf_flavour
)
2347 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2348 && !h
->def_dynamic
)))
2352 /* Backend specific symbol fixup. */
2353 if (elf_hash_table (eif
->info
)->dynobj
)
2355 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2356 if (bed
->elf_backend_fixup_symbol
2357 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2361 /* If this is a final link, and the symbol was defined as a common
2362 symbol in a regular object file, and there was no definition in
2363 any dynamic object, then the linker will have allocated space for
2364 the symbol in a common section but the DEF_REGULAR
2365 flag will not have been set. */
2366 if (h
->root
.type
== bfd_link_hash_defined
2370 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2373 /* If -Bsymbolic was used (which means to bind references to global
2374 symbols to the definition within the shared object), and this
2375 symbol was defined in a regular object, then it actually doesn't
2376 need a PLT entry. Likewise, if the symbol has non-default
2377 visibility. If the symbol has hidden or internal visibility, we
2378 will force it local. */
2380 && eif
->info
->shared
2381 && is_elf_hash_table (eif
->info
->hash
)
2382 && (eif
->info
->symbolic
2383 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2386 bfd_boolean force_local
;
2388 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2389 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2390 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2393 /* If a weak undefined symbol has non-default visibility, we also
2394 hide it from the dynamic linker. */
2395 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2396 && h
->root
.type
== bfd_link_hash_undefweak
)
2398 const struct elf_backend_data
*bed
;
2399 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2400 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2403 /* If this is a weak defined symbol in a dynamic object, and we know
2404 the real definition in the dynamic object, copy interesting flags
2405 over to the real definition. */
2406 if (h
->u
.weakdef
!= NULL
)
2408 struct elf_link_hash_entry
*weakdef
;
2410 weakdef
= h
->u
.weakdef
;
2411 if (h
->root
.type
== bfd_link_hash_indirect
)
2412 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2414 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2415 || h
->root
.type
== bfd_link_hash_defweak
);
2416 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2417 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2418 BFD_ASSERT (weakdef
->def_dynamic
);
2420 /* If the real definition is defined by a regular object file,
2421 don't do anything special. See the longer description in
2422 _bfd_elf_adjust_dynamic_symbol, below. */
2423 if (weakdef
->def_regular
)
2424 h
->u
.weakdef
= NULL
;
2426 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2433 /* Make the backend pick a good value for a dynamic symbol. This is
2434 called via elf_link_hash_traverse, and also calls itself
2438 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2440 struct elf_info_failed
*eif
= data
;
2442 const struct elf_backend_data
*bed
;
2444 if (! is_elf_hash_table (eif
->info
->hash
))
2447 if (h
->root
.type
== bfd_link_hash_warning
)
2449 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2450 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2452 /* When warning symbols are created, they **replace** the "real"
2453 entry in the hash table, thus we never get to see the real
2454 symbol in a hash traversal. So look at it now. */
2455 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2458 /* Ignore indirect symbols. These are added by the versioning code. */
2459 if (h
->root
.type
== bfd_link_hash_indirect
)
2462 /* Fix the symbol flags. */
2463 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2466 /* If this symbol does not require a PLT entry, and it is not
2467 defined by a dynamic object, or is not referenced by a regular
2468 object, ignore it. We do have to handle a weak defined symbol,
2469 even if no regular object refers to it, if we decided to add it
2470 to the dynamic symbol table. FIXME: Do we normally need to worry
2471 about symbols which are defined by one dynamic object and
2472 referenced by another one? */
2477 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2479 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2483 /* If we've already adjusted this symbol, don't do it again. This
2484 can happen via a recursive call. */
2485 if (h
->dynamic_adjusted
)
2488 /* Don't look at this symbol again. Note that we must set this
2489 after checking the above conditions, because we may look at a
2490 symbol once, decide not to do anything, and then get called
2491 recursively later after REF_REGULAR is set below. */
2492 h
->dynamic_adjusted
= 1;
2494 /* If this is a weak definition, and we know a real definition, and
2495 the real symbol is not itself defined by a regular object file,
2496 then get a good value for the real definition. We handle the
2497 real symbol first, for the convenience of the backend routine.
2499 Note that there is a confusing case here. If the real definition
2500 is defined by a regular object file, we don't get the real symbol
2501 from the dynamic object, but we do get the weak symbol. If the
2502 processor backend uses a COPY reloc, then if some routine in the
2503 dynamic object changes the real symbol, we will not see that
2504 change in the corresponding weak symbol. This is the way other
2505 ELF linkers work as well, and seems to be a result of the shared
2508 I will clarify this issue. Most SVR4 shared libraries define the
2509 variable _timezone and define timezone as a weak synonym. The
2510 tzset call changes _timezone. If you write
2511 extern int timezone;
2513 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2514 you might expect that, since timezone is a synonym for _timezone,
2515 the same number will print both times. However, if the processor
2516 backend uses a COPY reloc, then actually timezone will be copied
2517 into your process image, and, since you define _timezone
2518 yourself, _timezone will not. Thus timezone and _timezone will
2519 wind up at different memory locations. The tzset call will set
2520 _timezone, leaving timezone unchanged. */
2522 if (h
->u
.weakdef
!= NULL
)
2524 /* If we get to this point, we know there is an implicit
2525 reference by a regular object file via the weak symbol H.
2526 FIXME: Is this really true? What if the traversal finds
2527 H->U.WEAKDEF before it finds H? */
2528 h
->u
.weakdef
->ref_regular
= 1;
2530 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2534 /* If a symbol has no type and no size and does not require a PLT
2535 entry, then we are probably about to do the wrong thing here: we
2536 are probably going to create a COPY reloc for an empty object.
2537 This case can arise when a shared object is built with assembly
2538 code, and the assembly code fails to set the symbol type. */
2540 && h
->type
== STT_NOTYPE
2542 (*_bfd_error_handler
)
2543 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2544 h
->root
.root
.string
);
2546 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2547 bed
= get_elf_backend_data (dynobj
);
2548 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2557 /* Adjust all external symbols pointing into SEC_MERGE sections
2558 to reflect the object merging within the sections. */
2561 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2565 if (h
->root
.type
== bfd_link_hash_warning
)
2566 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2568 if ((h
->root
.type
== bfd_link_hash_defined
2569 || h
->root
.type
== bfd_link_hash_defweak
)
2570 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2571 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2573 bfd
*output_bfd
= data
;
2575 h
->root
.u
.def
.value
=
2576 _bfd_merged_section_offset (output_bfd
,
2577 &h
->root
.u
.def
.section
,
2578 elf_section_data (sec
)->sec_info
,
2579 h
->root
.u
.def
.value
);
2585 /* Returns false if the symbol referred to by H should be considered
2586 to resolve local to the current module, and true if it should be
2587 considered to bind dynamically. */
2590 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2591 struct bfd_link_info
*info
,
2592 bfd_boolean ignore_protected
)
2594 bfd_boolean binding_stays_local_p
;
2599 while (h
->root
.type
== bfd_link_hash_indirect
2600 || h
->root
.type
== bfd_link_hash_warning
)
2601 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2603 /* If it was forced local, then clearly it's not dynamic. */
2604 if (h
->dynindx
== -1)
2606 if (h
->forced_local
)
2609 /* Identify the cases where name binding rules say that a
2610 visible symbol resolves locally. */
2611 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2613 switch (ELF_ST_VISIBILITY (h
->other
))
2620 /* Proper resolution for function pointer equality may require
2621 that these symbols perhaps be resolved dynamically, even though
2622 we should be resolving them to the current module. */
2623 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2624 binding_stays_local_p
= TRUE
;
2631 /* If it isn't defined locally, then clearly it's dynamic. */
2632 if (!h
->def_regular
)
2635 /* Otherwise, the symbol is dynamic if binding rules don't tell
2636 us that it remains local. */
2637 return !binding_stays_local_p
;
2640 /* Return true if the symbol referred to by H should be considered
2641 to resolve local to the current module, and false otherwise. Differs
2642 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2643 undefined symbols and weak symbols. */
2646 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2647 struct bfd_link_info
*info
,
2648 bfd_boolean local_protected
)
2650 /* If it's a local sym, of course we resolve locally. */
2654 /* Common symbols that become definitions don't get the DEF_REGULAR
2655 flag set, so test it first, and don't bail out. */
2656 if (ELF_COMMON_DEF_P (h
))
2658 /* If we don't have a definition in a regular file, then we can't
2659 resolve locally. The sym is either undefined or dynamic. */
2660 else if (!h
->def_regular
)
2663 /* Forced local symbols resolve locally. */
2664 if (h
->forced_local
)
2667 /* As do non-dynamic symbols. */
2668 if (h
->dynindx
== -1)
2671 /* At this point, we know the symbol is defined and dynamic. In an
2672 executable it must resolve locally, likewise when building symbolic
2673 shared libraries. */
2674 if (info
->executable
|| info
->symbolic
)
2677 /* Now deal with defined dynamic symbols in shared libraries. Ones
2678 with default visibility might not resolve locally. */
2679 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2682 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2683 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2686 /* STV_PROTECTED non-function symbols are local. */
2687 if (h
->type
!= STT_FUNC
)
2690 /* Function pointer equality tests may require that STV_PROTECTED
2691 symbols be treated as dynamic symbols, even when we know that the
2692 dynamic linker will resolve them locally. */
2693 return local_protected
;
2696 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2697 aligned. Returns the first TLS output section. */
2699 struct bfd_section
*
2700 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2702 struct bfd_section
*sec
, *tls
;
2703 unsigned int align
= 0;
2705 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2706 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2710 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2711 if (sec
->alignment_power
> align
)
2712 align
= sec
->alignment_power
;
2714 elf_hash_table (info
)->tls_sec
= tls
;
2716 /* Ensure the alignment of the first section is the largest alignment,
2717 so that the tls segment starts aligned. */
2719 tls
->alignment_power
= align
;
2724 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2726 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2727 Elf_Internal_Sym
*sym
)
2729 const struct elf_backend_data
*bed
;
2731 /* Local symbols do not count, but target specific ones might. */
2732 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2733 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2736 /* Function symbols do not count. */
2737 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2740 /* If the section is undefined, then so is the symbol. */
2741 if (sym
->st_shndx
== SHN_UNDEF
)
2744 /* If the symbol is defined in the common section, then
2745 it is a common definition and so does not count. */
2746 bed
= get_elf_backend_data (abfd
);
2747 if (bed
->common_definition (sym
))
2750 /* If the symbol is in a target specific section then we
2751 must rely upon the backend to tell us what it is. */
2752 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2753 /* FIXME - this function is not coded yet:
2755 return _bfd_is_global_symbol_definition (abfd, sym);
2757 Instead for now assume that the definition is not global,
2758 Even if this is wrong, at least the linker will behave
2759 in the same way that it used to do. */
2765 /* Search the symbol table of the archive element of the archive ABFD
2766 whose archive map contains a mention of SYMDEF, and determine if
2767 the symbol is defined in this element. */
2769 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2771 Elf_Internal_Shdr
* hdr
;
2772 bfd_size_type symcount
;
2773 bfd_size_type extsymcount
;
2774 bfd_size_type extsymoff
;
2775 Elf_Internal_Sym
*isymbuf
;
2776 Elf_Internal_Sym
*isym
;
2777 Elf_Internal_Sym
*isymend
;
2780 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2784 if (! bfd_check_format (abfd
, bfd_object
))
2787 /* If we have already included the element containing this symbol in the
2788 link then we do not need to include it again. Just claim that any symbol
2789 it contains is not a definition, so that our caller will not decide to
2790 (re)include this element. */
2791 if (abfd
->archive_pass
)
2794 /* Select the appropriate symbol table. */
2795 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2796 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2798 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2800 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2802 /* The sh_info field of the symtab header tells us where the
2803 external symbols start. We don't care about the local symbols. */
2804 if (elf_bad_symtab (abfd
))
2806 extsymcount
= symcount
;
2811 extsymcount
= symcount
- hdr
->sh_info
;
2812 extsymoff
= hdr
->sh_info
;
2815 if (extsymcount
== 0)
2818 /* Read in the symbol table. */
2819 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2821 if (isymbuf
== NULL
)
2824 /* Scan the symbol table looking for SYMDEF. */
2826 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2830 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2835 if (strcmp (name
, symdef
->name
) == 0)
2837 result
= is_global_data_symbol_definition (abfd
, isym
);
2847 /* Add an entry to the .dynamic table. */
2850 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2854 struct elf_link_hash_table
*hash_table
;
2855 const struct elf_backend_data
*bed
;
2857 bfd_size_type newsize
;
2858 bfd_byte
*newcontents
;
2859 Elf_Internal_Dyn dyn
;
2861 hash_table
= elf_hash_table (info
);
2862 if (! is_elf_hash_table (hash_table
))
2865 bed
= get_elf_backend_data (hash_table
->dynobj
);
2866 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2867 BFD_ASSERT (s
!= NULL
);
2869 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2870 newcontents
= bfd_realloc (s
->contents
, newsize
);
2871 if (newcontents
== NULL
)
2875 dyn
.d_un
.d_val
= val
;
2876 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2879 s
->contents
= newcontents
;
2884 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2885 otherwise just check whether one already exists. Returns -1 on error,
2886 1 if a DT_NEEDED tag already exists, and 0 on success. */
2889 elf_add_dt_needed_tag (bfd
*abfd
,
2890 struct bfd_link_info
*info
,
2894 struct elf_link_hash_table
*hash_table
;
2895 bfd_size_type oldsize
;
2896 bfd_size_type strindex
;
2898 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2901 hash_table
= elf_hash_table (info
);
2902 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2903 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2904 if (strindex
== (bfd_size_type
) -1)
2907 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2910 const struct elf_backend_data
*bed
;
2913 bed
= get_elf_backend_data (hash_table
->dynobj
);
2914 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2916 for (extdyn
= sdyn
->contents
;
2917 extdyn
< sdyn
->contents
+ sdyn
->size
;
2918 extdyn
+= bed
->s
->sizeof_dyn
)
2920 Elf_Internal_Dyn dyn
;
2922 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2923 if (dyn
.d_tag
== DT_NEEDED
2924 && dyn
.d_un
.d_val
== strindex
)
2926 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2934 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2937 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2941 /* We were just checking for existence of the tag. */
2942 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2947 /* Sort symbol by value and section. */
2949 elf_sort_symbol (const void *arg1
, const void *arg2
)
2951 const struct elf_link_hash_entry
*h1
;
2952 const struct elf_link_hash_entry
*h2
;
2953 bfd_signed_vma vdiff
;
2955 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2956 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2957 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2959 return vdiff
> 0 ? 1 : -1;
2962 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2964 return sdiff
> 0 ? 1 : -1;
2969 /* This function is used to adjust offsets into .dynstr for
2970 dynamic symbols. This is called via elf_link_hash_traverse. */
2973 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2975 struct elf_strtab_hash
*dynstr
= data
;
2977 if (h
->root
.type
== bfd_link_hash_warning
)
2978 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2980 if (h
->dynindx
!= -1)
2981 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2985 /* Assign string offsets in .dynstr, update all structures referencing
2989 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2991 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2992 struct elf_link_local_dynamic_entry
*entry
;
2993 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2994 bfd
*dynobj
= hash_table
->dynobj
;
2997 const struct elf_backend_data
*bed
;
3000 _bfd_elf_strtab_finalize (dynstr
);
3001 size
= _bfd_elf_strtab_size (dynstr
);
3003 bed
= get_elf_backend_data (dynobj
);
3004 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3005 BFD_ASSERT (sdyn
!= NULL
);
3007 /* Update all .dynamic entries referencing .dynstr strings. */
3008 for (extdyn
= sdyn
->contents
;
3009 extdyn
< sdyn
->contents
+ sdyn
->size
;
3010 extdyn
+= bed
->s
->sizeof_dyn
)
3012 Elf_Internal_Dyn dyn
;
3014 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3018 dyn
.d_un
.d_val
= size
;
3026 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3031 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3034 /* Now update local dynamic symbols. */
3035 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3036 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3037 entry
->isym
.st_name
);
3039 /* And the rest of dynamic symbols. */
3040 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3042 /* Adjust version definitions. */
3043 if (elf_tdata (output_bfd
)->cverdefs
)
3048 Elf_Internal_Verdef def
;
3049 Elf_Internal_Verdaux defaux
;
3051 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3055 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3057 p
+= sizeof (Elf_External_Verdef
);
3058 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3060 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3062 _bfd_elf_swap_verdaux_in (output_bfd
,
3063 (Elf_External_Verdaux
*) p
, &defaux
);
3064 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3066 _bfd_elf_swap_verdaux_out (output_bfd
,
3067 &defaux
, (Elf_External_Verdaux
*) p
);
3068 p
+= sizeof (Elf_External_Verdaux
);
3071 while (def
.vd_next
);
3074 /* Adjust version references. */
3075 if (elf_tdata (output_bfd
)->verref
)
3080 Elf_Internal_Verneed need
;
3081 Elf_Internal_Vernaux needaux
;
3083 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3087 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3089 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3090 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3091 (Elf_External_Verneed
*) p
);
3092 p
+= sizeof (Elf_External_Verneed
);
3093 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3095 _bfd_elf_swap_vernaux_in (output_bfd
,
3096 (Elf_External_Vernaux
*) p
, &needaux
);
3097 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3099 _bfd_elf_swap_vernaux_out (output_bfd
,
3101 (Elf_External_Vernaux
*) p
);
3102 p
+= sizeof (Elf_External_Vernaux
);
3105 while (need
.vn_next
);
3111 /* Add symbols from an ELF object file to the linker hash table. */
3114 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3116 Elf_Internal_Shdr
*hdr
;
3117 bfd_size_type symcount
;
3118 bfd_size_type extsymcount
;
3119 bfd_size_type extsymoff
;
3120 struct elf_link_hash_entry
**sym_hash
;
3121 bfd_boolean dynamic
;
3122 Elf_External_Versym
*extversym
= NULL
;
3123 Elf_External_Versym
*ever
;
3124 struct elf_link_hash_entry
*weaks
;
3125 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3126 bfd_size_type nondeflt_vers_cnt
= 0;
3127 Elf_Internal_Sym
*isymbuf
= NULL
;
3128 Elf_Internal_Sym
*isym
;
3129 Elf_Internal_Sym
*isymend
;
3130 const struct elf_backend_data
*bed
;
3131 bfd_boolean add_needed
;
3132 struct elf_link_hash_table
*htab
;
3134 void *alloc_mark
= NULL
;
3135 struct bfd_hash_entry
**old_table
= NULL
;
3136 unsigned int old_size
= 0;
3137 unsigned int old_count
= 0;
3138 void *old_tab
= NULL
;
3141 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3142 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3143 long old_dynsymcount
= 0;
3145 size_t hashsize
= 0;
3147 htab
= elf_hash_table (info
);
3148 bed
= get_elf_backend_data (abfd
);
3150 if ((abfd
->flags
& DYNAMIC
) == 0)
3156 /* You can't use -r against a dynamic object. Also, there's no
3157 hope of using a dynamic object which does not exactly match
3158 the format of the output file. */
3159 if (info
->relocatable
3160 || !is_elf_hash_table (htab
)
3161 || htab
->root
.creator
!= abfd
->xvec
)
3163 if (info
->relocatable
)
3164 bfd_set_error (bfd_error_invalid_operation
);
3166 bfd_set_error (bfd_error_wrong_format
);
3171 /* As a GNU extension, any input sections which are named
3172 .gnu.warning.SYMBOL are treated as warning symbols for the given
3173 symbol. This differs from .gnu.warning sections, which generate
3174 warnings when they are included in an output file. */
3175 if (info
->executable
)
3179 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3183 name
= bfd_get_section_name (abfd
, s
);
3184 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3189 name
+= sizeof ".gnu.warning." - 1;
3191 /* If this is a shared object, then look up the symbol
3192 in the hash table. If it is there, and it is already
3193 been defined, then we will not be using the entry
3194 from this shared object, so we don't need to warn.
3195 FIXME: If we see the definition in a regular object
3196 later on, we will warn, but we shouldn't. The only
3197 fix is to keep track of what warnings we are supposed
3198 to emit, and then handle them all at the end of the
3202 struct elf_link_hash_entry
*h
;
3204 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3206 /* FIXME: What about bfd_link_hash_common? */
3208 && (h
->root
.type
== bfd_link_hash_defined
3209 || h
->root
.type
== bfd_link_hash_defweak
))
3211 /* We don't want to issue this warning. Clobber
3212 the section size so that the warning does not
3213 get copied into the output file. */
3220 msg
= bfd_alloc (abfd
, sz
+ 1);
3224 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3229 if (! (_bfd_generic_link_add_one_symbol
3230 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3231 FALSE
, bed
->collect
, NULL
)))
3234 if (! info
->relocatable
)
3236 /* Clobber the section size so that the warning does
3237 not get copied into the output file. */
3240 /* Also set SEC_EXCLUDE, so that symbols defined in
3241 the warning section don't get copied to the output. */
3242 s
->flags
|= SEC_EXCLUDE
;
3251 /* If we are creating a shared library, create all the dynamic
3252 sections immediately. We need to attach them to something,
3253 so we attach them to this BFD, provided it is the right
3254 format. FIXME: If there are no input BFD's of the same
3255 format as the output, we can't make a shared library. */
3257 && is_elf_hash_table (htab
)
3258 && htab
->root
.creator
== abfd
->xvec
3259 && !htab
->dynamic_sections_created
)
3261 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3265 else if (!is_elf_hash_table (htab
))
3270 const char *soname
= NULL
;
3271 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3274 /* ld --just-symbols and dynamic objects don't mix very well.
3275 ld shouldn't allow it. */
3276 if ((s
= abfd
->sections
) != NULL
3277 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3280 /* If this dynamic lib was specified on the command line with
3281 --as-needed in effect, then we don't want to add a DT_NEEDED
3282 tag unless the lib is actually used. Similary for libs brought
3283 in by another lib's DT_NEEDED. When --no-add-needed is used
3284 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3285 any dynamic library in DT_NEEDED tags in the dynamic lib at
3287 add_needed
= (elf_dyn_lib_class (abfd
)
3288 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3289 | DYN_NO_NEEDED
)) == 0;
3291 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3297 unsigned long shlink
;
3299 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3300 goto error_free_dyn
;
3302 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3304 goto error_free_dyn
;
3305 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3307 for (extdyn
= dynbuf
;
3308 extdyn
< dynbuf
+ s
->size
;
3309 extdyn
+= bed
->s
->sizeof_dyn
)
3311 Elf_Internal_Dyn dyn
;
3313 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3314 if (dyn
.d_tag
== DT_SONAME
)
3316 unsigned int tagv
= dyn
.d_un
.d_val
;
3317 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3319 goto error_free_dyn
;
3321 if (dyn
.d_tag
== DT_NEEDED
)
3323 struct bfd_link_needed_list
*n
, **pn
;
3325 unsigned int tagv
= dyn
.d_un
.d_val
;
3327 amt
= sizeof (struct bfd_link_needed_list
);
3328 n
= bfd_alloc (abfd
, amt
);
3329 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3330 if (n
== NULL
|| fnm
== NULL
)
3331 goto error_free_dyn
;
3332 amt
= strlen (fnm
) + 1;
3333 anm
= bfd_alloc (abfd
, amt
);
3335 goto error_free_dyn
;
3336 memcpy (anm
, fnm
, amt
);
3340 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3344 if (dyn
.d_tag
== DT_RUNPATH
)
3346 struct bfd_link_needed_list
*n
, **pn
;
3348 unsigned int tagv
= dyn
.d_un
.d_val
;
3350 amt
= sizeof (struct bfd_link_needed_list
);
3351 n
= bfd_alloc (abfd
, amt
);
3352 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3353 if (n
== NULL
|| fnm
== NULL
)
3354 goto error_free_dyn
;
3355 amt
= strlen (fnm
) + 1;
3356 anm
= bfd_alloc (abfd
, amt
);
3358 goto error_free_dyn
;
3359 memcpy (anm
, fnm
, amt
);
3363 for (pn
= & runpath
;
3369 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3370 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3372 struct bfd_link_needed_list
*n
, **pn
;
3374 unsigned int tagv
= dyn
.d_un
.d_val
;
3376 amt
= sizeof (struct bfd_link_needed_list
);
3377 n
= bfd_alloc (abfd
, amt
);
3378 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3379 if (n
== NULL
|| fnm
== NULL
)
3380 goto error_free_dyn
;
3381 amt
= strlen (fnm
) + 1;
3382 anm
= bfd_alloc (abfd
, amt
);
3389 memcpy (anm
, fnm
, amt
);
3404 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3405 frees all more recently bfd_alloc'd blocks as well. */
3411 struct bfd_link_needed_list
**pn
;
3412 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3417 /* We do not want to include any of the sections in a dynamic
3418 object in the output file. We hack by simply clobbering the
3419 list of sections in the BFD. This could be handled more
3420 cleanly by, say, a new section flag; the existing
3421 SEC_NEVER_LOAD flag is not the one we want, because that one
3422 still implies that the section takes up space in the output
3424 bfd_section_list_clear (abfd
);
3426 /* Find the name to use in a DT_NEEDED entry that refers to this
3427 object. If the object has a DT_SONAME entry, we use it.
3428 Otherwise, if the generic linker stuck something in
3429 elf_dt_name, we use that. Otherwise, we just use the file
3431 if (soname
== NULL
|| *soname
== '\0')
3433 soname
= elf_dt_name (abfd
);
3434 if (soname
== NULL
|| *soname
== '\0')
3435 soname
= bfd_get_filename (abfd
);
3438 /* Save the SONAME because sometimes the linker emulation code
3439 will need to know it. */
3440 elf_dt_name (abfd
) = soname
;
3442 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3446 /* If we have already included this dynamic object in the
3447 link, just ignore it. There is no reason to include a
3448 particular dynamic object more than once. */
3453 /* If this is a dynamic object, we always link against the .dynsym
3454 symbol table, not the .symtab symbol table. The dynamic linker
3455 will only see the .dynsym symbol table, so there is no reason to
3456 look at .symtab for a dynamic object. */
3458 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3459 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3461 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3463 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols at
3468 if (elf_bad_symtab (abfd
))
3470 extsymcount
= symcount
;
3475 extsymcount
= symcount
- hdr
->sh_info
;
3476 extsymoff
= hdr
->sh_info
;
3480 if (extsymcount
!= 0)
3482 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3484 if (isymbuf
== NULL
)
3487 /* We store a pointer to the hash table entry for each external
3489 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3490 sym_hash
= bfd_alloc (abfd
, amt
);
3491 if (sym_hash
== NULL
)
3492 goto error_free_sym
;
3493 elf_sym_hashes (abfd
) = sym_hash
;
3498 /* Read in any version definitions. */
3499 if (!_bfd_elf_slurp_version_tables (abfd
,
3500 info
->default_imported_symver
))
3501 goto error_free_sym
;
3503 /* Read in the symbol versions, but don't bother to convert them
3504 to internal format. */
3505 if (elf_dynversym (abfd
) != 0)
3507 Elf_Internal_Shdr
*versymhdr
;
3509 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3510 extversym
= bfd_malloc (versymhdr
->sh_size
);
3511 if (extversym
== NULL
)
3512 goto error_free_sym
;
3513 amt
= versymhdr
->sh_size
;
3514 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3515 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3516 goto error_free_vers
;
3520 /* If we are loading an as-needed shared lib, save the symbol table
3521 state before we start adding symbols. If the lib turns out
3522 to be unneeded, restore the state. */
3523 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3528 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3530 struct bfd_hash_entry
*p
;
3531 struct elf_link_hash_entry
*h
;
3533 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3535 h
= (struct elf_link_hash_entry
*) p
;
3536 entsize
+= htab
->root
.table
.entsize
;
3537 if (h
->root
.type
== bfd_link_hash_warning
)
3538 entsize
+= htab
->root
.table
.entsize
;
3542 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3543 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3544 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3545 if (old_tab
== NULL
)
3546 goto error_free_vers
;
3548 /* Remember the current objalloc pointer, so that all mem for
3549 symbols added can later be reclaimed. */
3550 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3551 if (alloc_mark
== NULL
)
3552 goto error_free_vers
;
3554 /* Clone the symbol table and sym hashes. Remember some
3555 pointers into the symbol table, and dynamic symbol count. */
3556 old_hash
= (char *) old_tab
+ tabsize
;
3557 old_ent
= (char *) old_hash
+ hashsize
;
3558 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3559 memcpy (old_hash
, sym_hash
, hashsize
);
3560 old_undefs
= htab
->root
.undefs
;
3561 old_undefs_tail
= htab
->root
.undefs_tail
;
3562 old_table
= htab
->root
.table
.table
;
3563 old_size
= htab
->root
.table
.size
;
3564 old_count
= htab
->root
.table
.count
;
3565 old_dynsymcount
= htab
->dynsymcount
;
3567 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3569 struct bfd_hash_entry
*p
;
3570 struct elf_link_hash_entry
*h
;
3572 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3574 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3575 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3576 h
= (struct elf_link_hash_entry
*) p
;
3577 if (h
->root
.type
== bfd_link_hash_warning
)
3579 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3580 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3587 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3588 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3590 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3594 asection
*sec
, *new_sec
;
3597 struct elf_link_hash_entry
*h
;
3598 bfd_boolean definition
;
3599 bfd_boolean size_change_ok
;
3600 bfd_boolean type_change_ok
;
3601 bfd_boolean new_weakdef
;
3602 bfd_boolean override
;
3604 unsigned int old_alignment
;
3609 flags
= BSF_NO_FLAGS
;
3611 value
= isym
->st_value
;
3613 common
= bed
->common_definition (isym
);
3615 bind
= ELF_ST_BIND (isym
->st_info
);
3616 if (bind
== STB_LOCAL
)
3618 /* This should be impossible, since ELF requires that all
3619 global symbols follow all local symbols, and that sh_info
3620 point to the first global symbol. Unfortunately, Irix 5
3624 else if (bind
== STB_GLOBAL
)
3626 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3629 else if (bind
== STB_WEAK
)
3633 /* Leave it up to the processor backend. */
3636 if (isym
->st_shndx
== SHN_UNDEF
)
3637 sec
= bfd_und_section_ptr
;
3638 else if (isym
->st_shndx
< SHN_LORESERVE
3639 || isym
->st_shndx
> SHN_HIRESERVE
)
3641 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3643 sec
= bfd_abs_section_ptr
;
3644 else if (sec
->kept_section
)
3646 /* Symbols from discarded section are undefined, and have
3647 default visibility. */
3648 sec
= bfd_und_section_ptr
;
3649 isym
->st_shndx
= SHN_UNDEF
;
3650 isym
->st_other
= (STV_DEFAULT
3651 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3653 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3656 else if (isym
->st_shndx
== SHN_ABS
)
3657 sec
= bfd_abs_section_ptr
;
3658 else if (isym
->st_shndx
== SHN_COMMON
)
3660 sec
= bfd_com_section_ptr
;
3661 /* What ELF calls the size we call the value. What ELF
3662 calls the value we call the alignment. */
3663 value
= isym
->st_size
;
3667 /* Leave it up to the processor backend. */
3670 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3673 goto error_free_vers
;
3675 if (isym
->st_shndx
== SHN_COMMON
3676 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3677 && !info
->relocatable
)
3679 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3683 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3686 | SEC_LINKER_CREATED
3687 | SEC_THREAD_LOCAL
));
3689 goto error_free_vers
;
3693 else if (bed
->elf_add_symbol_hook
)
3695 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3697 goto error_free_vers
;
3699 /* The hook function sets the name to NULL if this symbol
3700 should be skipped for some reason. */
3705 /* Sanity check that all possibilities were handled. */
3708 bfd_set_error (bfd_error_bad_value
);
3709 goto error_free_vers
;
3712 if (bfd_is_und_section (sec
)
3713 || bfd_is_com_section (sec
))
3718 size_change_ok
= FALSE
;
3719 type_change_ok
= bed
->type_change_ok
;
3724 if (is_elf_hash_table (htab
))
3726 Elf_Internal_Versym iver
;
3727 unsigned int vernum
= 0;
3732 if (info
->default_imported_symver
)
3733 /* Use the default symbol version created earlier. */
3734 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3739 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3741 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3743 /* If this is a hidden symbol, or if it is not version
3744 1, we append the version name to the symbol name.
3745 However, we do not modify a non-hidden absolute symbol
3746 if it is not a function, because it might be the version
3747 symbol itself. FIXME: What if it isn't? */
3748 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3749 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3750 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3753 size_t namelen
, verlen
, newlen
;
3756 if (isym
->st_shndx
!= SHN_UNDEF
)
3758 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3760 else if (vernum
> 1)
3762 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3768 (*_bfd_error_handler
)
3769 (_("%B: %s: invalid version %u (max %d)"),
3771 elf_tdata (abfd
)->cverdefs
);
3772 bfd_set_error (bfd_error_bad_value
);
3773 goto error_free_vers
;
3778 /* We cannot simply test for the number of
3779 entries in the VERNEED section since the
3780 numbers for the needed versions do not start
3782 Elf_Internal_Verneed
*t
;
3785 for (t
= elf_tdata (abfd
)->verref
;
3789 Elf_Internal_Vernaux
*a
;
3791 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3793 if (a
->vna_other
== vernum
)
3795 verstr
= a
->vna_nodename
;
3804 (*_bfd_error_handler
)
3805 (_("%B: %s: invalid needed version %d"),
3806 abfd
, name
, vernum
);
3807 bfd_set_error (bfd_error_bad_value
);
3808 goto error_free_vers
;
3812 namelen
= strlen (name
);
3813 verlen
= strlen (verstr
);
3814 newlen
= namelen
+ verlen
+ 2;
3815 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3816 && isym
->st_shndx
!= SHN_UNDEF
)
3819 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3820 if (newname
== NULL
)
3821 goto error_free_vers
;
3822 memcpy (newname
, name
, namelen
);
3823 p
= newname
+ namelen
;
3825 /* If this is a defined non-hidden version symbol,
3826 we add another @ to the name. This indicates the
3827 default version of the symbol. */
3828 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3829 && isym
->st_shndx
!= SHN_UNDEF
)
3831 memcpy (p
, verstr
, verlen
+ 1);
3836 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3837 &value
, &old_alignment
,
3838 sym_hash
, &skip
, &override
,
3839 &type_change_ok
, &size_change_ok
))
3840 goto error_free_vers
;
3849 while (h
->root
.type
== bfd_link_hash_indirect
3850 || h
->root
.type
== bfd_link_hash_warning
)
3851 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3853 /* Remember the old alignment if this is a common symbol, so
3854 that we don't reduce the alignment later on. We can't
3855 check later, because _bfd_generic_link_add_one_symbol
3856 will set a default for the alignment which we want to
3857 override. We also remember the old bfd where the existing
3858 definition comes from. */
3859 switch (h
->root
.type
)
3864 case bfd_link_hash_defined
:
3865 case bfd_link_hash_defweak
:
3866 old_bfd
= h
->root
.u
.def
.section
->owner
;
3869 case bfd_link_hash_common
:
3870 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3871 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3875 if (elf_tdata (abfd
)->verdef
!= NULL
3879 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3882 if (! (_bfd_generic_link_add_one_symbol
3883 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3884 (struct bfd_link_hash_entry
**) sym_hash
)))
3885 goto error_free_vers
;
3888 while (h
->root
.type
== bfd_link_hash_indirect
3889 || h
->root
.type
== bfd_link_hash_warning
)
3890 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3893 new_weakdef
= FALSE
;
3896 && (flags
& BSF_WEAK
) != 0
3897 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3898 && is_elf_hash_table (htab
)
3899 && h
->u
.weakdef
== NULL
)
3901 /* Keep a list of all weak defined non function symbols from
3902 a dynamic object, using the weakdef field. Later in this
3903 function we will set the weakdef field to the correct
3904 value. We only put non-function symbols from dynamic
3905 objects on this list, because that happens to be the only
3906 time we need to know the normal symbol corresponding to a
3907 weak symbol, and the information is time consuming to
3908 figure out. If the weakdef field is not already NULL,
3909 then this symbol was already defined by some previous
3910 dynamic object, and we will be using that previous
3911 definition anyhow. */
3913 h
->u
.weakdef
= weaks
;
3918 /* Set the alignment of a common symbol. */
3919 if ((common
|| bfd_is_com_section (sec
))
3920 && h
->root
.type
== bfd_link_hash_common
)
3925 align
= bfd_log2 (isym
->st_value
);
3928 /* The new symbol is a common symbol in a shared object.
3929 We need to get the alignment from the section. */
3930 align
= new_sec
->alignment_power
;
3932 if (align
> old_alignment
3933 /* Permit an alignment power of zero if an alignment of one
3934 is specified and no other alignments have been specified. */
3935 || (isym
->st_value
== 1 && old_alignment
== 0))
3936 h
->root
.u
.c
.p
->alignment_power
= align
;
3938 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3941 if (is_elf_hash_table (htab
))
3945 /* Check the alignment when a common symbol is involved. This
3946 can change when a common symbol is overridden by a normal
3947 definition or a common symbol is ignored due to the old
3948 normal definition. We need to make sure the maximum
3949 alignment is maintained. */
3950 if ((old_alignment
|| common
)
3951 && h
->root
.type
!= bfd_link_hash_common
)
3953 unsigned int common_align
;
3954 unsigned int normal_align
;
3955 unsigned int symbol_align
;
3959 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3960 if (h
->root
.u
.def
.section
->owner
!= NULL
3961 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3963 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3964 if (normal_align
> symbol_align
)
3965 normal_align
= symbol_align
;
3968 normal_align
= symbol_align
;
3972 common_align
= old_alignment
;
3973 common_bfd
= old_bfd
;
3978 common_align
= bfd_log2 (isym
->st_value
);
3980 normal_bfd
= old_bfd
;
3983 if (normal_align
< common_align
)
3985 /* PR binutils/2735 */
3986 if (normal_bfd
== NULL
)
3987 (*_bfd_error_handler
)
3988 (_("Warning: alignment %u of common symbol `%s' in %B"
3989 " is greater than the alignment (%u) of its section %A"),
3990 common_bfd
, h
->root
.u
.def
.section
,
3991 1 << common_align
, name
, 1 << normal_align
);
3993 (*_bfd_error_handler
)
3994 (_("Warning: alignment %u of symbol `%s' in %B"
3995 " is smaller than %u in %B"),
3996 normal_bfd
, common_bfd
,
3997 1 << normal_align
, name
, 1 << common_align
);
4001 /* Remember the symbol size and type. */
4002 if (isym
->st_size
!= 0
4003 && (definition
|| h
->size
== 0))
4005 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
4006 (*_bfd_error_handler
)
4007 (_("Warning: size of symbol `%s' changed"
4008 " from %lu in %B to %lu in %B"),
4010 name
, (unsigned long) h
->size
,
4011 (unsigned long) isym
->st_size
);
4013 h
->size
= isym
->st_size
;
4016 /* If this is a common symbol, then we always want H->SIZE
4017 to be the size of the common symbol. The code just above
4018 won't fix the size if a common symbol becomes larger. We
4019 don't warn about a size change here, because that is
4020 covered by --warn-common. */
4021 if (h
->root
.type
== bfd_link_hash_common
)
4022 h
->size
= h
->root
.u
.c
.size
;
4024 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4025 && (definition
|| h
->type
== STT_NOTYPE
))
4027 if (h
->type
!= STT_NOTYPE
4028 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4029 && ! type_change_ok
)
4030 (*_bfd_error_handler
)
4031 (_("Warning: type of symbol `%s' changed"
4032 " from %d to %d in %B"),
4033 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4035 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4038 /* If st_other has a processor-specific meaning, specific
4039 code might be needed here. We never merge the visibility
4040 attribute with the one from a dynamic object. */
4041 if (bed
->elf_backend_merge_symbol_attribute
)
4042 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4045 /* If this symbol has default visibility and the user has requested
4046 we not re-export it, then mark it as hidden. */
4047 if (definition
&& !dynamic
4049 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4050 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4051 isym
->st_other
= (STV_HIDDEN
4052 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4054 if (isym
->st_other
!= 0 && !dynamic
)
4056 unsigned char hvis
, symvis
, other
, nvis
;
4058 /* Take the balance of OTHER from the definition. */
4059 other
= (definition
? isym
->st_other
: h
->other
);
4060 other
&= ~ ELF_ST_VISIBILITY (-1);
4062 /* Combine visibilities, using the most constraining one. */
4063 hvis
= ELF_ST_VISIBILITY (h
->other
);
4064 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4070 nvis
= hvis
< symvis
? hvis
: symvis
;
4072 h
->other
= other
| nvis
;
4075 /* Set a flag in the hash table entry indicating the type of
4076 reference or definition we just found. Keep a count of
4077 the number of dynamic symbols we find. A dynamic symbol
4078 is one which is referenced or defined by both a regular
4079 object and a shared object. */
4086 if (bind
!= STB_WEAK
)
4087 h
->ref_regular_nonweak
= 1;
4091 if (! info
->executable
4104 || (h
->u
.weakdef
!= NULL
4106 && h
->u
.weakdef
->dynindx
!= -1))
4110 /* Check to see if we need to add an indirect symbol for
4111 the default name. */
4112 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4113 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4114 &sec
, &value
, &dynsym
,
4116 goto error_free_vers
;
4118 if (definition
&& !dynamic
)
4120 char *p
= strchr (name
, ELF_VER_CHR
);
4121 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4123 /* Queue non-default versions so that .symver x, x@FOO
4124 aliases can be checked. */
4127 amt
= ((isymend
- isym
+ 1)
4128 * sizeof (struct elf_link_hash_entry
*));
4129 nondeflt_vers
= bfd_malloc (amt
);
4131 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4135 if (dynsym
&& h
->dynindx
== -1)
4137 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4138 goto error_free_vers
;
4139 if (h
->u
.weakdef
!= NULL
4141 && h
->u
.weakdef
->dynindx
== -1)
4143 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4144 goto error_free_vers
;
4147 else if (dynsym
&& h
->dynindx
!= -1)
4148 /* If the symbol already has a dynamic index, but
4149 visibility says it should not be visible, turn it into
4151 switch (ELF_ST_VISIBILITY (h
->other
))
4155 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4166 const char *soname
= elf_dt_name (abfd
);
4168 /* A symbol from a library loaded via DT_NEEDED of some
4169 other library is referenced by a regular object.
4170 Add a DT_NEEDED entry for it. Issue an error if
4171 --no-add-needed is used. */
4172 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4174 (*_bfd_error_handler
)
4175 (_("%s: invalid DSO for symbol `%s' definition"),
4177 bfd_set_error (bfd_error_bad_value
);
4178 goto error_free_vers
;
4181 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4184 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4186 goto error_free_vers
;
4188 BFD_ASSERT (ret
== 0);
4193 if (extversym
!= NULL
)
4199 if (isymbuf
!= NULL
)
4205 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4209 /* Restore the symbol table. */
4210 old_hash
= (char *) old_tab
+ tabsize
;
4211 old_ent
= (char *) old_hash
+ hashsize
;
4212 sym_hash
= elf_sym_hashes (abfd
);
4213 htab
->root
.table
.table
= old_table
;
4214 htab
->root
.table
.size
= old_size
;
4215 htab
->root
.table
.count
= old_count
;
4216 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4217 memcpy (sym_hash
, old_hash
, hashsize
);
4218 htab
->root
.undefs
= old_undefs
;
4219 htab
->root
.undefs_tail
= old_undefs_tail
;
4220 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4222 struct bfd_hash_entry
*p
;
4223 struct elf_link_hash_entry
*h
;
4225 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4227 h
= (struct elf_link_hash_entry
*) p
;
4228 if (h
->root
.type
== bfd_link_hash_warning
)
4229 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4230 if (h
->dynindx
>= old_dynsymcount
)
4231 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4233 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4234 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4235 h
= (struct elf_link_hash_entry
*) p
;
4236 if (h
->root
.type
== bfd_link_hash_warning
)
4238 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4239 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4245 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4247 if (nondeflt_vers
!= NULL
)
4248 free (nondeflt_vers
);
4252 if (old_tab
!= NULL
)
4258 /* Now that all the symbols from this input file are created, handle
4259 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4260 if (nondeflt_vers
!= NULL
)
4262 bfd_size_type cnt
, symidx
;
4264 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4266 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4267 char *shortname
, *p
;
4269 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4271 || (h
->root
.type
!= bfd_link_hash_defined
4272 && h
->root
.type
!= bfd_link_hash_defweak
))
4275 amt
= p
- h
->root
.root
.string
;
4276 shortname
= bfd_malloc (amt
+ 1);
4277 memcpy (shortname
, h
->root
.root
.string
, amt
);
4278 shortname
[amt
] = '\0';
4280 hi
= (struct elf_link_hash_entry
*)
4281 bfd_link_hash_lookup (&htab
->root
, shortname
,
4282 FALSE
, FALSE
, FALSE
);
4284 && hi
->root
.type
== h
->root
.type
4285 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4286 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4288 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4289 hi
->root
.type
= bfd_link_hash_indirect
;
4290 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4291 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4292 sym_hash
= elf_sym_hashes (abfd
);
4294 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4295 if (sym_hash
[symidx
] == hi
)
4297 sym_hash
[symidx
] = h
;
4303 free (nondeflt_vers
);
4304 nondeflt_vers
= NULL
;
4307 /* Now set the weakdefs field correctly for all the weak defined
4308 symbols we found. The only way to do this is to search all the
4309 symbols. Since we only need the information for non functions in
4310 dynamic objects, that's the only time we actually put anything on
4311 the list WEAKS. We need this information so that if a regular
4312 object refers to a symbol defined weakly in a dynamic object, the
4313 real symbol in the dynamic object is also put in the dynamic
4314 symbols; we also must arrange for both symbols to point to the
4315 same memory location. We could handle the general case of symbol
4316 aliasing, but a general symbol alias can only be generated in
4317 assembler code, handling it correctly would be very time
4318 consuming, and other ELF linkers don't handle general aliasing
4322 struct elf_link_hash_entry
**hpp
;
4323 struct elf_link_hash_entry
**hppend
;
4324 struct elf_link_hash_entry
**sorted_sym_hash
;
4325 struct elf_link_hash_entry
*h
;
4328 /* Since we have to search the whole symbol list for each weak
4329 defined symbol, search time for N weak defined symbols will be
4330 O(N^2). Binary search will cut it down to O(NlogN). */
4331 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4332 sorted_sym_hash
= bfd_malloc (amt
);
4333 if (sorted_sym_hash
== NULL
)
4335 sym_hash
= sorted_sym_hash
;
4336 hpp
= elf_sym_hashes (abfd
);
4337 hppend
= hpp
+ extsymcount
;
4339 for (; hpp
< hppend
; hpp
++)
4343 && h
->root
.type
== bfd_link_hash_defined
4344 && h
->type
!= STT_FUNC
)
4352 qsort (sorted_sym_hash
, sym_count
,
4353 sizeof (struct elf_link_hash_entry
*),
4356 while (weaks
!= NULL
)
4358 struct elf_link_hash_entry
*hlook
;
4365 weaks
= hlook
->u
.weakdef
;
4366 hlook
->u
.weakdef
= NULL
;
4368 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4369 || hlook
->root
.type
== bfd_link_hash_defweak
4370 || hlook
->root
.type
== bfd_link_hash_common
4371 || hlook
->root
.type
== bfd_link_hash_indirect
);
4372 slook
= hlook
->root
.u
.def
.section
;
4373 vlook
= hlook
->root
.u
.def
.value
;
4380 bfd_signed_vma vdiff
;
4382 h
= sorted_sym_hash
[idx
];
4383 vdiff
= vlook
- h
->root
.u
.def
.value
;
4390 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4403 /* We didn't find a value/section match. */
4407 for (i
= ilook
; i
< sym_count
; i
++)
4409 h
= sorted_sym_hash
[i
];
4411 /* Stop if value or section doesn't match. */
4412 if (h
->root
.u
.def
.value
!= vlook
4413 || h
->root
.u
.def
.section
!= slook
)
4415 else if (h
!= hlook
)
4417 hlook
->u
.weakdef
= h
;
4419 /* If the weak definition is in the list of dynamic
4420 symbols, make sure the real definition is put
4422 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4424 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4428 /* If the real definition is in the list of dynamic
4429 symbols, make sure the weak definition is put
4430 there as well. If we don't do this, then the
4431 dynamic loader might not merge the entries for the
4432 real definition and the weak definition. */
4433 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4435 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4443 free (sorted_sym_hash
);
4446 if (bed
->check_directives
)
4447 (*bed
->check_directives
) (abfd
, info
);
4449 /* If this object is the same format as the output object, and it is
4450 not a shared library, then let the backend look through the
4453 This is required to build global offset table entries and to
4454 arrange for dynamic relocs. It is not required for the
4455 particular common case of linking non PIC code, even when linking
4456 against shared libraries, but unfortunately there is no way of
4457 knowing whether an object file has been compiled PIC or not.
4458 Looking through the relocs is not particularly time consuming.
4459 The problem is that we must either (1) keep the relocs in memory,
4460 which causes the linker to require additional runtime memory or
4461 (2) read the relocs twice from the input file, which wastes time.
4462 This would be a good case for using mmap.
4464 I have no idea how to handle linking PIC code into a file of a
4465 different format. It probably can't be done. */
4467 && is_elf_hash_table (htab
)
4468 && htab
->root
.creator
== abfd
->xvec
4469 && bed
->check_relocs
!= NULL
)
4473 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4475 Elf_Internal_Rela
*internal_relocs
;
4478 if ((o
->flags
& SEC_RELOC
) == 0
4479 || o
->reloc_count
== 0
4480 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4481 && (o
->flags
& SEC_DEBUGGING
) != 0)
4482 || bfd_is_abs_section (o
->output_section
))
4485 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4487 if (internal_relocs
== NULL
)
4490 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4492 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4493 free (internal_relocs
);
4500 /* If this is a non-traditional link, try to optimize the handling
4501 of the .stab/.stabstr sections. */
4503 && ! info
->traditional_format
4504 && is_elf_hash_table (htab
)
4505 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4509 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4510 if (stabstr
!= NULL
)
4512 bfd_size_type string_offset
= 0;
4515 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4516 if (strncmp (".stab", stab
->name
, 5) == 0
4517 && (!stab
->name
[5] ||
4518 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4519 && (stab
->flags
& SEC_MERGE
) == 0
4520 && !bfd_is_abs_section (stab
->output_section
))
4522 struct bfd_elf_section_data
*secdata
;
4524 secdata
= elf_section_data (stab
);
4525 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4526 stabstr
, &secdata
->sec_info
,
4529 if (secdata
->sec_info
)
4530 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4535 if (is_elf_hash_table (htab
) && add_needed
)
4537 /* Add this bfd to the loaded list. */
4538 struct elf_link_loaded_list
*n
;
4540 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4544 n
->next
= htab
->loaded
;
4551 if (old_tab
!= NULL
)
4553 if (nondeflt_vers
!= NULL
)
4554 free (nondeflt_vers
);
4555 if (extversym
!= NULL
)
4558 if (isymbuf
!= NULL
)
4564 /* Return the linker hash table entry of a symbol that might be
4565 satisfied by an archive symbol. Return -1 on error. */
4567 struct elf_link_hash_entry
*
4568 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4569 struct bfd_link_info
*info
,
4572 struct elf_link_hash_entry
*h
;
4576 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4580 /* If this is a default version (the name contains @@), look up the
4581 symbol again with only one `@' as well as without the version.
4582 The effect is that references to the symbol with and without the
4583 version will be matched by the default symbol in the archive. */
4585 p
= strchr (name
, ELF_VER_CHR
);
4586 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4589 /* First check with only one `@'. */
4590 len
= strlen (name
);
4591 copy
= bfd_alloc (abfd
, len
);
4593 return (struct elf_link_hash_entry
*) 0 - 1;
4595 first
= p
- name
+ 1;
4596 memcpy (copy
, name
, first
);
4597 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4599 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4602 /* We also need to check references to the symbol without the
4604 copy
[first
- 1] = '\0';
4605 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4606 FALSE
, FALSE
, FALSE
);
4609 bfd_release (abfd
, copy
);
4613 /* Add symbols from an ELF archive file to the linker hash table. We
4614 don't use _bfd_generic_link_add_archive_symbols because of a
4615 problem which arises on UnixWare. The UnixWare libc.so is an
4616 archive which includes an entry libc.so.1 which defines a bunch of
4617 symbols. The libc.so archive also includes a number of other
4618 object files, which also define symbols, some of which are the same
4619 as those defined in libc.so.1. Correct linking requires that we
4620 consider each object file in turn, and include it if it defines any
4621 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4622 this; it looks through the list of undefined symbols, and includes
4623 any object file which defines them. When this algorithm is used on
4624 UnixWare, it winds up pulling in libc.so.1 early and defining a
4625 bunch of symbols. This means that some of the other objects in the
4626 archive are not included in the link, which is incorrect since they
4627 precede libc.so.1 in the archive.
4629 Fortunately, ELF archive handling is simpler than that done by
4630 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4631 oddities. In ELF, if we find a symbol in the archive map, and the
4632 symbol is currently undefined, we know that we must pull in that
4635 Unfortunately, we do have to make multiple passes over the symbol
4636 table until nothing further is resolved. */
4639 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4642 bfd_boolean
*defined
= NULL
;
4643 bfd_boolean
*included
= NULL
;
4647 const struct elf_backend_data
*bed
;
4648 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4649 (bfd
*, struct bfd_link_info
*, const char *);
4651 if (! bfd_has_map (abfd
))
4653 /* An empty archive is a special case. */
4654 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4656 bfd_set_error (bfd_error_no_armap
);
4660 /* Keep track of all symbols we know to be already defined, and all
4661 files we know to be already included. This is to speed up the
4662 second and subsequent passes. */
4663 c
= bfd_ardata (abfd
)->symdef_count
;
4667 amt
*= sizeof (bfd_boolean
);
4668 defined
= bfd_zmalloc (amt
);
4669 included
= bfd_zmalloc (amt
);
4670 if (defined
== NULL
|| included
== NULL
)
4673 symdefs
= bfd_ardata (abfd
)->symdefs
;
4674 bed
= get_elf_backend_data (abfd
);
4675 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4688 symdefend
= symdef
+ c
;
4689 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4691 struct elf_link_hash_entry
*h
;
4693 struct bfd_link_hash_entry
*undefs_tail
;
4696 if (defined
[i
] || included
[i
])
4698 if (symdef
->file_offset
== last
)
4704 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4705 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4711 if (h
->root
.type
== bfd_link_hash_common
)
4713 /* We currently have a common symbol. The archive map contains
4714 a reference to this symbol, so we may want to include it. We
4715 only want to include it however, if this archive element
4716 contains a definition of the symbol, not just another common
4719 Unfortunately some archivers (including GNU ar) will put
4720 declarations of common symbols into their archive maps, as
4721 well as real definitions, so we cannot just go by the archive
4722 map alone. Instead we must read in the element's symbol
4723 table and check that to see what kind of symbol definition
4725 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4728 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4730 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4735 /* We need to include this archive member. */
4736 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4737 if (element
== NULL
)
4740 if (! bfd_check_format (element
, bfd_object
))
4743 /* Doublecheck that we have not included this object
4744 already--it should be impossible, but there may be
4745 something wrong with the archive. */
4746 if (element
->archive_pass
!= 0)
4748 bfd_set_error (bfd_error_bad_value
);
4751 element
->archive_pass
= 1;
4753 undefs_tail
= info
->hash
->undefs_tail
;
4755 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4758 if (! bfd_link_add_symbols (element
, info
))
4761 /* If there are any new undefined symbols, we need to make
4762 another pass through the archive in order to see whether
4763 they can be defined. FIXME: This isn't perfect, because
4764 common symbols wind up on undefs_tail and because an
4765 undefined symbol which is defined later on in this pass
4766 does not require another pass. This isn't a bug, but it
4767 does make the code less efficient than it could be. */
4768 if (undefs_tail
!= info
->hash
->undefs_tail
)
4771 /* Look backward to mark all symbols from this object file
4772 which we have already seen in this pass. */
4776 included
[mark
] = TRUE
;
4781 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4783 /* We mark subsequent symbols from this object file as we go
4784 on through the loop. */
4785 last
= symdef
->file_offset
;
4796 if (defined
!= NULL
)
4798 if (included
!= NULL
)
4803 /* Given an ELF BFD, add symbols to the global hash table as
4807 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4809 switch (bfd_get_format (abfd
))
4812 return elf_link_add_object_symbols (abfd
, info
);
4814 return elf_link_add_archive_symbols (abfd
, info
);
4816 bfd_set_error (bfd_error_wrong_format
);
4821 /* This function will be called though elf_link_hash_traverse to store
4822 all hash value of the exported symbols in an array. */
4825 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4827 unsigned long **valuep
= data
;
4833 if (h
->root
.type
== bfd_link_hash_warning
)
4834 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4836 /* Ignore indirect symbols. These are added by the versioning code. */
4837 if (h
->dynindx
== -1)
4840 name
= h
->root
.root
.string
;
4841 p
= strchr (name
, ELF_VER_CHR
);
4844 alc
= bfd_malloc (p
- name
+ 1);
4845 memcpy (alc
, name
, p
- name
);
4846 alc
[p
- name
] = '\0';
4850 /* Compute the hash value. */
4851 ha
= bfd_elf_hash (name
);
4853 /* Store the found hash value in the array given as the argument. */
4856 /* And store it in the struct so that we can put it in the hash table
4858 h
->u
.elf_hash_value
= ha
;
4866 struct collect_gnu_hash_codes
4869 const struct elf_backend_data
*bed
;
4870 unsigned long int nsyms
;
4871 unsigned long int maskbits
;
4872 unsigned long int *hashcodes
;
4873 unsigned long int *hashval
;
4874 unsigned long int *indx
;
4875 unsigned long int *counts
;
4878 long int min_dynindx
;
4879 unsigned long int bucketcount
;
4880 unsigned long int symindx
;
4881 long int local_indx
;
4882 long int shift1
, shift2
;
4883 unsigned long int mask
;
4886 /* This function will be called though elf_link_hash_traverse to store
4887 all hash value of the exported symbols in an array. */
4890 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4892 struct collect_gnu_hash_codes
*s
= data
;
4898 if (h
->root
.type
== bfd_link_hash_warning
)
4899 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4901 /* Ignore indirect symbols. These are added by the versioning code. */
4902 if (h
->dynindx
== -1)
4905 /* Ignore also local symbols and undefined symbols. */
4906 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4909 name
= h
->root
.root
.string
;
4910 p
= strchr (name
, ELF_VER_CHR
);
4913 alc
= bfd_malloc (p
- name
+ 1);
4914 memcpy (alc
, name
, p
- name
);
4915 alc
[p
- name
] = '\0';
4919 /* Compute the hash value. */
4920 ha
= bfd_elf_gnu_hash (name
);
4922 /* Store the found hash value in the array for compute_bucket_count,
4923 and also for .dynsym reordering purposes. */
4924 s
->hashcodes
[s
->nsyms
] = ha
;
4925 s
->hashval
[h
->dynindx
] = ha
;
4927 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
4928 s
->min_dynindx
= h
->dynindx
;
4936 /* This function will be called though elf_link_hash_traverse to do
4937 final dynaminc symbol renumbering. */
4940 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
4942 struct collect_gnu_hash_codes
*s
= data
;
4943 unsigned long int bucket
;
4944 unsigned long int val
;
4946 if (h
->root
.type
== bfd_link_hash_warning
)
4947 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4949 /* Ignore indirect symbols. */
4950 if (h
->dynindx
== -1)
4953 /* Ignore also local symbols and undefined symbols. */
4954 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4956 if (h
->dynindx
>= s
->min_dynindx
)
4957 h
->dynindx
= s
->local_indx
++;
4961 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
4962 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
4963 & ((s
->maskbits
>> s
->shift1
) - 1);
4964 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
4966 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
4967 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
4968 if (s
->counts
[bucket
] == 1)
4969 /* Last element terminates the chain. */
4971 bfd_put_32 (s
->output_bfd
, val
,
4972 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
4973 --s
->counts
[bucket
];
4974 h
->dynindx
= s
->indx
[bucket
]++;
4978 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4981 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
4983 return !(h
->forced_local
4984 || h
->root
.type
== bfd_link_hash_undefined
4985 || h
->root
.type
== bfd_link_hash_undefweak
4986 || ((h
->root
.type
== bfd_link_hash_defined
4987 || h
->root
.type
== bfd_link_hash_defweak
)
4988 && h
->root
.u
.def
.section
->output_section
== NULL
));
4991 /* Array used to determine the number of hash table buckets to use
4992 based on the number of symbols there are. If there are fewer than
4993 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4994 fewer than 37 we use 17 buckets, and so forth. We never use more
4995 than 32771 buckets. */
4997 static const size_t elf_buckets
[] =
4999 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5003 /* Compute bucket count for hashing table. We do not use a static set
5004 of possible tables sizes anymore. Instead we determine for all
5005 possible reasonable sizes of the table the outcome (i.e., the
5006 number of collisions etc) and choose the best solution. The
5007 weighting functions are not too simple to allow the table to grow
5008 without bounds. Instead one of the weighting factors is the size.
5009 Therefore the result is always a good payoff between few collisions
5010 (= short chain lengths) and table size. */
5012 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5013 unsigned long int nsyms
, int gnu_hash
)
5015 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5016 size_t best_size
= 0;
5017 unsigned long int i
;
5020 /* We have a problem here. The following code to optimize the table
5021 size requires an integer type with more the 32 bits. If
5022 BFD_HOST_U_64_BIT is set we know about such a type. */
5023 #ifdef BFD_HOST_U_64_BIT
5028 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5029 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5030 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5031 unsigned long int *counts
;
5033 /* Possible optimization parameters: if we have NSYMS symbols we say
5034 that the hashing table must at least have NSYMS/4 and at most
5036 minsize
= nsyms
/ 4;
5039 best_size
= maxsize
= nsyms
* 2;
5044 if ((best_size
& 31) == 0)
5048 /* Create array where we count the collisions in. We must use bfd_malloc
5049 since the size could be large. */
5051 amt
*= sizeof (unsigned long int);
5052 counts
= bfd_malloc (amt
);
5056 /* Compute the "optimal" size for the hash table. The criteria is a
5057 minimal chain length. The minor criteria is (of course) the size
5059 for (i
= minsize
; i
< maxsize
; ++i
)
5061 /* Walk through the array of hashcodes and count the collisions. */
5062 BFD_HOST_U_64_BIT max
;
5063 unsigned long int j
;
5064 unsigned long int fact
;
5066 if (gnu_hash
&& (i
& 31) == 0)
5069 memset (counts
, '\0', i
* sizeof (unsigned long int));
5071 /* Determine how often each hash bucket is used. */
5072 for (j
= 0; j
< nsyms
; ++j
)
5073 ++counts
[hashcodes
[j
] % i
];
5075 /* For the weight function we need some information about the
5076 pagesize on the target. This is information need not be 100%
5077 accurate. Since this information is not available (so far) we
5078 define it here to a reasonable default value. If it is crucial
5079 to have a better value some day simply define this value. */
5080 # ifndef BFD_TARGET_PAGESIZE
5081 # define BFD_TARGET_PAGESIZE (4096)
5084 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5086 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5089 /* Variant 1: optimize for short chains. We add the squares
5090 of all the chain lengths (which favors many small chain
5091 over a few long chains). */
5092 for (j
= 0; j
< i
; ++j
)
5093 max
+= counts
[j
] * counts
[j
];
5095 /* This adds penalties for the overall size of the table. */
5096 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5099 /* Variant 2: Optimize a lot more for small table. Here we
5100 also add squares of the size but we also add penalties for
5101 empty slots (the +1 term). */
5102 for (j
= 0; j
< i
; ++j
)
5103 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5105 /* The overall size of the table is considered, but not as
5106 strong as in variant 1, where it is squared. */
5107 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5111 /* Compare with current best results. */
5112 if (max
< best_chlen
)
5122 #endif /* defined (BFD_HOST_U_64_BIT) */
5124 /* This is the fallback solution if no 64bit type is available or if we
5125 are not supposed to spend much time on optimizations. We select the
5126 bucket count using a fixed set of numbers. */
5127 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5129 best_size
= elf_buckets
[i
];
5130 if (nsyms
< elf_buckets
[i
+ 1])
5133 if (gnu_hash
&& best_size
< 2)
5140 /* Set up the sizes and contents of the ELF dynamic sections. This is
5141 called by the ELF linker emulation before_allocation routine. We
5142 must set the sizes of the sections before the linker sets the
5143 addresses of the various sections. */
5146 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5149 const char *filter_shlib
,
5150 const char * const *auxiliary_filters
,
5151 struct bfd_link_info
*info
,
5152 asection
**sinterpptr
,
5153 struct bfd_elf_version_tree
*verdefs
)
5155 bfd_size_type soname_indx
;
5157 const struct elf_backend_data
*bed
;
5158 struct elf_assign_sym_version_info asvinfo
;
5162 soname_indx
= (bfd_size_type
) -1;
5164 if (!is_elf_hash_table (info
->hash
))
5167 elf_tdata (output_bfd
)->relro
= info
->relro
;
5168 if (info
->execstack
)
5169 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5170 else if (info
->noexecstack
)
5171 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5175 asection
*notesec
= NULL
;
5178 for (inputobj
= info
->input_bfds
;
5180 inputobj
= inputobj
->link_next
)
5184 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5186 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5189 if (s
->flags
& SEC_CODE
)
5198 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5199 if (exec
&& info
->relocatable
5200 && notesec
->output_section
!= bfd_abs_section_ptr
)
5201 notesec
->output_section
->flags
|= SEC_CODE
;
5205 /* Any syms created from now on start with -1 in
5206 got.refcount/offset and plt.refcount/offset. */
5207 elf_hash_table (info
)->init_got_refcount
5208 = elf_hash_table (info
)->init_got_offset
;
5209 elf_hash_table (info
)->init_plt_refcount
5210 = elf_hash_table (info
)->init_plt_offset
;
5212 /* The backend may have to create some sections regardless of whether
5213 we're dynamic or not. */
5214 bed
= get_elf_backend_data (output_bfd
);
5215 if (bed
->elf_backend_always_size_sections
5216 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5219 dynobj
= elf_hash_table (info
)->dynobj
;
5221 /* If there were no dynamic objects in the link, there is nothing to
5226 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5229 if (elf_hash_table (info
)->dynamic_sections_created
)
5231 struct elf_info_failed eif
;
5232 struct elf_link_hash_entry
*h
;
5234 struct bfd_elf_version_tree
*t
;
5235 struct bfd_elf_version_expr
*d
;
5237 bfd_boolean all_defined
;
5239 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5240 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5244 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5246 if (soname_indx
== (bfd_size_type
) -1
5247 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5253 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5255 info
->flags
|= DF_SYMBOLIC
;
5262 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5264 if (indx
== (bfd_size_type
) -1
5265 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5268 if (info
->new_dtags
)
5270 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5271 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5276 if (filter_shlib
!= NULL
)
5280 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5281 filter_shlib
, TRUE
);
5282 if (indx
== (bfd_size_type
) -1
5283 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5287 if (auxiliary_filters
!= NULL
)
5289 const char * const *p
;
5291 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5295 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5297 if (indx
== (bfd_size_type
) -1
5298 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5304 eif
.verdefs
= verdefs
;
5307 /* If we are supposed to export all symbols into the dynamic symbol
5308 table (this is not the normal case), then do so. */
5309 if (info
->export_dynamic
)
5311 elf_link_hash_traverse (elf_hash_table (info
),
5312 _bfd_elf_export_symbol
,
5318 /* Make all global versions with definition. */
5319 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5320 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5321 if (!d
->symver
&& d
->symbol
)
5323 const char *verstr
, *name
;
5324 size_t namelen
, verlen
, newlen
;
5326 struct elf_link_hash_entry
*newh
;
5329 namelen
= strlen (name
);
5331 verlen
= strlen (verstr
);
5332 newlen
= namelen
+ verlen
+ 3;
5334 newname
= bfd_malloc (newlen
);
5335 if (newname
== NULL
)
5337 memcpy (newname
, name
, namelen
);
5339 /* Check the hidden versioned definition. */
5340 p
= newname
+ namelen
;
5342 memcpy (p
, verstr
, verlen
+ 1);
5343 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5344 newname
, FALSE
, FALSE
,
5347 || (newh
->root
.type
!= bfd_link_hash_defined
5348 && newh
->root
.type
!= bfd_link_hash_defweak
))
5350 /* Check the default versioned definition. */
5352 memcpy (p
, verstr
, verlen
+ 1);
5353 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5354 newname
, FALSE
, FALSE
,
5359 /* Mark this version if there is a definition and it is
5360 not defined in a shared object. */
5362 && !newh
->def_dynamic
5363 && (newh
->root
.type
== bfd_link_hash_defined
5364 || newh
->root
.type
== bfd_link_hash_defweak
))
5368 /* Attach all the symbols to their version information. */
5369 asvinfo
.output_bfd
= output_bfd
;
5370 asvinfo
.info
= info
;
5371 asvinfo
.verdefs
= verdefs
;
5372 asvinfo
.failed
= FALSE
;
5374 elf_link_hash_traverse (elf_hash_table (info
),
5375 _bfd_elf_link_assign_sym_version
,
5380 if (!info
->allow_undefined_version
)
5382 /* Check if all global versions have a definition. */
5384 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5385 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5386 if (!d
->symver
&& !d
->script
)
5388 (*_bfd_error_handler
)
5389 (_("%s: undefined version: %s"),
5390 d
->pattern
, t
->name
);
5391 all_defined
= FALSE
;
5396 bfd_set_error (bfd_error_bad_value
);
5401 /* Find all symbols which were defined in a dynamic object and make
5402 the backend pick a reasonable value for them. */
5403 elf_link_hash_traverse (elf_hash_table (info
),
5404 _bfd_elf_adjust_dynamic_symbol
,
5409 /* Add some entries to the .dynamic section. We fill in some of the
5410 values later, in bfd_elf_final_link, but we must add the entries
5411 now so that we know the final size of the .dynamic section. */
5413 /* If there are initialization and/or finalization functions to
5414 call then add the corresponding DT_INIT/DT_FINI entries. */
5415 h
= (info
->init_function
5416 ? elf_link_hash_lookup (elf_hash_table (info
),
5417 info
->init_function
, FALSE
,
5424 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5427 h
= (info
->fini_function
5428 ? elf_link_hash_lookup (elf_hash_table (info
),
5429 info
->fini_function
, FALSE
,
5436 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5440 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5441 if (s
!= NULL
&& s
->linker_has_input
)
5443 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5444 if (! info
->executable
)
5449 for (sub
= info
->input_bfds
; sub
!= NULL
;
5450 sub
= sub
->link_next
)
5451 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5452 if (elf_section_data (o
)->this_hdr
.sh_type
5453 == SHT_PREINIT_ARRAY
)
5455 (*_bfd_error_handler
)
5456 (_("%B: .preinit_array section is not allowed in DSO"),
5461 bfd_set_error (bfd_error_nonrepresentable_section
);
5465 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5466 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5469 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5470 if (s
!= NULL
&& s
->linker_has_input
)
5472 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5473 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5476 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5477 if (s
!= NULL
&& s
->linker_has_input
)
5479 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5480 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5484 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5485 /* If .dynstr is excluded from the link, we don't want any of
5486 these tags. Strictly, we should be checking each section
5487 individually; This quick check covers for the case where
5488 someone does a /DISCARD/ : { *(*) }. */
5489 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5491 bfd_size_type strsize
;
5493 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5494 if ((info
->emit_hash
5495 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5496 || (info
->emit_gnu_hash
5497 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5498 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5499 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5500 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5501 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5502 bed
->s
->sizeof_sym
))
5507 /* The backend must work out the sizes of all the other dynamic
5509 if (bed
->elf_backend_size_dynamic_sections
5510 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5513 if (elf_hash_table (info
)->dynamic_sections_created
)
5515 unsigned long section_sym_count
;
5518 /* Set up the version definition section. */
5519 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5520 BFD_ASSERT (s
!= NULL
);
5522 /* We may have created additional version definitions if we are
5523 just linking a regular application. */
5524 verdefs
= asvinfo
.verdefs
;
5526 /* Skip anonymous version tag. */
5527 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5528 verdefs
= verdefs
->next
;
5530 if (verdefs
== NULL
&& !info
->create_default_symver
)
5531 s
->flags
|= SEC_EXCLUDE
;
5536 struct bfd_elf_version_tree
*t
;
5538 Elf_Internal_Verdef def
;
5539 Elf_Internal_Verdaux defaux
;
5540 struct bfd_link_hash_entry
*bh
;
5541 struct elf_link_hash_entry
*h
;
5547 /* Make space for the base version. */
5548 size
+= sizeof (Elf_External_Verdef
);
5549 size
+= sizeof (Elf_External_Verdaux
);
5552 /* Make space for the default version. */
5553 if (info
->create_default_symver
)
5555 size
+= sizeof (Elf_External_Verdef
);
5559 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5561 struct bfd_elf_version_deps
*n
;
5563 size
+= sizeof (Elf_External_Verdef
);
5564 size
+= sizeof (Elf_External_Verdaux
);
5567 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5568 size
+= sizeof (Elf_External_Verdaux
);
5572 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5573 if (s
->contents
== NULL
&& s
->size
!= 0)
5576 /* Fill in the version definition section. */
5580 def
.vd_version
= VER_DEF_CURRENT
;
5581 def
.vd_flags
= VER_FLG_BASE
;
5584 if (info
->create_default_symver
)
5586 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5587 def
.vd_next
= sizeof (Elf_External_Verdef
);
5591 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5592 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5593 + sizeof (Elf_External_Verdaux
));
5596 if (soname_indx
!= (bfd_size_type
) -1)
5598 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5600 def
.vd_hash
= bfd_elf_hash (soname
);
5601 defaux
.vda_name
= soname_indx
;
5608 name
= lbasename (output_bfd
->filename
);
5609 def
.vd_hash
= bfd_elf_hash (name
);
5610 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5612 if (indx
== (bfd_size_type
) -1)
5614 defaux
.vda_name
= indx
;
5616 defaux
.vda_next
= 0;
5618 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5619 (Elf_External_Verdef
*) p
);
5620 p
+= sizeof (Elf_External_Verdef
);
5621 if (info
->create_default_symver
)
5623 /* Add a symbol representing this version. */
5625 if (! (_bfd_generic_link_add_one_symbol
5626 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5628 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5630 h
= (struct elf_link_hash_entry
*) bh
;
5633 h
->type
= STT_OBJECT
;
5634 h
->verinfo
.vertree
= NULL
;
5636 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5639 /* Create a duplicate of the base version with the same
5640 aux block, but different flags. */
5643 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5645 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5646 + sizeof (Elf_External_Verdaux
));
5649 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5650 (Elf_External_Verdef
*) p
);
5651 p
+= sizeof (Elf_External_Verdef
);
5653 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5654 (Elf_External_Verdaux
*) p
);
5655 p
+= sizeof (Elf_External_Verdaux
);
5657 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5660 struct bfd_elf_version_deps
*n
;
5663 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5666 /* Add a symbol representing this version. */
5668 if (! (_bfd_generic_link_add_one_symbol
5669 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5671 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5673 h
= (struct elf_link_hash_entry
*) bh
;
5676 h
->type
= STT_OBJECT
;
5677 h
->verinfo
.vertree
= t
;
5679 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5682 def
.vd_version
= VER_DEF_CURRENT
;
5684 if (t
->globals
.list
== NULL
5685 && t
->locals
.list
== NULL
5687 def
.vd_flags
|= VER_FLG_WEAK
;
5688 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5689 def
.vd_cnt
= cdeps
+ 1;
5690 def
.vd_hash
= bfd_elf_hash (t
->name
);
5691 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5693 if (t
->next
!= NULL
)
5694 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5695 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5697 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5698 (Elf_External_Verdef
*) p
);
5699 p
+= sizeof (Elf_External_Verdef
);
5701 defaux
.vda_name
= h
->dynstr_index
;
5702 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5704 defaux
.vda_next
= 0;
5705 if (t
->deps
!= NULL
)
5706 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5707 t
->name_indx
= defaux
.vda_name
;
5709 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5710 (Elf_External_Verdaux
*) p
);
5711 p
+= sizeof (Elf_External_Verdaux
);
5713 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5715 if (n
->version_needed
== NULL
)
5717 /* This can happen if there was an error in the
5719 defaux
.vda_name
= 0;
5723 defaux
.vda_name
= n
->version_needed
->name_indx
;
5724 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5727 if (n
->next
== NULL
)
5728 defaux
.vda_next
= 0;
5730 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5732 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5733 (Elf_External_Verdaux
*) p
);
5734 p
+= sizeof (Elf_External_Verdaux
);
5738 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5739 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5742 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5745 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5747 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5750 else if (info
->flags
& DF_BIND_NOW
)
5752 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5758 if (info
->executable
)
5759 info
->flags_1
&= ~ (DF_1_INITFIRST
5762 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5766 /* Work out the size of the version reference section. */
5768 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5769 BFD_ASSERT (s
!= NULL
);
5771 struct elf_find_verdep_info sinfo
;
5773 sinfo
.output_bfd
= output_bfd
;
5775 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5776 if (sinfo
.vers
== 0)
5778 sinfo
.failed
= FALSE
;
5780 elf_link_hash_traverse (elf_hash_table (info
),
5781 _bfd_elf_link_find_version_dependencies
,
5784 if (elf_tdata (output_bfd
)->verref
== NULL
)
5785 s
->flags
|= SEC_EXCLUDE
;
5788 Elf_Internal_Verneed
*t
;
5793 /* Build the version definition section. */
5796 for (t
= elf_tdata (output_bfd
)->verref
;
5800 Elf_Internal_Vernaux
*a
;
5802 size
+= sizeof (Elf_External_Verneed
);
5804 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5805 size
+= sizeof (Elf_External_Vernaux
);
5809 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5810 if (s
->contents
== NULL
)
5814 for (t
= elf_tdata (output_bfd
)->verref
;
5819 Elf_Internal_Vernaux
*a
;
5823 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5826 t
->vn_version
= VER_NEED_CURRENT
;
5828 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5829 elf_dt_name (t
->vn_bfd
) != NULL
5830 ? elf_dt_name (t
->vn_bfd
)
5831 : lbasename (t
->vn_bfd
->filename
),
5833 if (indx
== (bfd_size_type
) -1)
5836 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5837 if (t
->vn_nextref
== NULL
)
5840 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5841 + caux
* sizeof (Elf_External_Vernaux
));
5843 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5844 (Elf_External_Verneed
*) p
);
5845 p
+= sizeof (Elf_External_Verneed
);
5847 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5849 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5850 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5851 a
->vna_nodename
, FALSE
);
5852 if (indx
== (bfd_size_type
) -1)
5855 if (a
->vna_nextptr
== NULL
)
5858 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5860 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5861 (Elf_External_Vernaux
*) p
);
5862 p
+= sizeof (Elf_External_Vernaux
);
5866 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5867 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5870 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5874 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5875 && elf_tdata (output_bfd
)->cverdefs
== 0)
5876 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5877 §ion_sym_count
) == 0)
5879 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5880 s
->flags
|= SEC_EXCLUDE
;
5887 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5889 if (!is_elf_hash_table (info
->hash
))
5892 if (elf_hash_table (info
)->dynamic_sections_created
)
5895 const struct elf_backend_data
*bed
;
5897 bfd_size_type dynsymcount
;
5898 unsigned long section_sym_count
;
5899 unsigned int dtagcount
;
5901 dynobj
= elf_hash_table (info
)->dynobj
;
5903 /* Assign dynsym indicies. In a shared library we generate a
5904 section symbol for each output section, which come first.
5905 Next come all of the back-end allocated local dynamic syms,
5906 followed by the rest of the global symbols. */
5908 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5909 §ion_sym_count
);
5911 /* Work out the size of the symbol version section. */
5912 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5913 BFD_ASSERT (s
!= NULL
);
5914 if (dynsymcount
!= 0
5915 && (s
->flags
& SEC_EXCLUDE
) == 0)
5917 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5918 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5919 if (s
->contents
== NULL
)
5922 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5926 /* Set the size of the .dynsym and .hash sections. We counted
5927 the number of dynamic symbols in elf_link_add_object_symbols.
5928 We will build the contents of .dynsym and .hash when we build
5929 the final symbol table, because until then we do not know the
5930 correct value to give the symbols. We built the .dynstr
5931 section as we went along in elf_link_add_object_symbols. */
5932 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5933 BFD_ASSERT (s
!= NULL
);
5934 bed
= get_elf_backend_data (output_bfd
);
5935 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5937 if (dynsymcount
!= 0)
5939 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5940 if (s
->contents
== NULL
)
5943 /* The first entry in .dynsym is a dummy symbol.
5944 Clear all the section syms, in case we don't output them all. */
5945 ++section_sym_count
;
5946 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5949 elf_hash_table (info
)->bucketcount
= 0;
5951 /* Compute the size of the hashing table. As a side effect this
5952 computes the hash values for all the names we export. */
5953 if (info
->emit_hash
)
5955 unsigned long int *hashcodes
;
5956 unsigned long int *hashcodesp
;
5958 unsigned long int nsyms
;
5960 size_t hash_entry_size
;
5962 /* Compute the hash values for all exported symbols. At the same
5963 time store the values in an array so that we could use them for
5965 amt
= dynsymcount
* sizeof (unsigned long int);
5966 hashcodes
= bfd_malloc (amt
);
5967 if (hashcodes
== NULL
)
5969 hashcodesp
= hashcodes
;
5971 /* Put all hash values in HASHCODES. */
5972 elf_link_hash_traverse (elf_hash_table (info
),
5973 elf_collect_hash_codes
, &hashcodesp
);
5975 nsyms
= hashcodesp
- hashcodes
;
5977 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
5980 if (bucketcount
== 0)
5983 elf_hash_table (info
)->bucketcount
= bucketcount
;
5985 s
= bfd_get_section_by_name (dynobj
, ".hash");
5986 BFD_ASSERT (s
!= NULL
);
5987 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5988 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5989 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5990 if (s
->contents
== NULL
)
5993 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5994 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5995 s
->contents
+ hash_entry_size
);
5998 if (info
->emit_gnu_hash
)
6001 unsigned char *contents
;
6002 struct collect_gnu_hash_codes cinfo
;
6006 memset (&cinfo
, 0, sizeof (cinfo
));
6008 /* Compute the hash values for all exported symbols. At the same
6009 time store the values in an array so that we could use them for
6011 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6012 cinfo
.hashcodes
= bfd_malloc (amt
);
6013 if (cinfo
.hashcodes
== NULL
)
6016 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6017 cinfo
.min_dynindx
= -1;
6018 cinfo
.output_bfd
= output_bfd
;
6021 /* Put all hash values in HASHCODES. */
6022 elf_link_hash_traverse (elf_hash_table (info
),
6023 elf_collect_gnu_hash_codes
, &cinfo
);
6026 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6028 if (bucketcount
== 0)
6030 free (cinfo
.hashcodes
);
6034 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6035 BFD_ASSERT (s
!= NULL
);
6037 if (cinfo
.nsyms
== 0)
6039 /* Empty .gnu.hash section is special. */
6040 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6041 free (cinfo
.hashcodes
);
6042 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6043 contents
= bfd_zalloc (output_bfd
, s
->size
);
6044 if (contents
== NULL
)
6046 s
->contents
= contents
;
6047 /* 1 empty bucket. */
6048 bfd_put_32 (output_bfd
, 1, contents
);
6049 /* SYMIDX above the special symbol 0. */
6050 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6051 /* Just one word for bitmask. */
6052 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6053 /* Only hash fn bloom filter. */
6054 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6055 /* No hashes are valid - empty bitmask. */
6056 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6057 /* No hashes in the only bucket. */
6058 bfd_put_32 (output_bfd
, 0,
6059 contents
+ 16 + bed
->s
->arch_size
/ 8);
6063 unsigned long int maskwords
, maskbitslog2
;
6064 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6066 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6067 if (maskbitslog2
< 3)
6069 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6070 maskbitslog2
= maskbitslog2
+ 3;
6072 maskbitslog2
= maskbitslog2
+ 2;
6073 if (bed
->s
->arch_size
== 64)
6075 if (maskbitslog2
== 5)
6081 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6082 cinfo
.shift2
= maskbitslog2
;
6083 cinfo
.maskbits
= 1 << maskbitslog2
;
6084 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6085 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6086 amt
+= maskwords
* sizeof (bfd_vma
);
6087 cinfo
.bitmask
= bfd_malloc (amt
);
6088 if (cinfo
.bitmask
== NULL
)
6090 free (cinfo
.hashcodes
);
6094 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6095 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6096 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6097 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6099 /* Determine how often each hash bucket is used. */
6100 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6101 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6102 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6104 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6105 if (cinfo
.counts
[i
] != 0)
6107 cinfo
.indx
[i
] = cnt
;
6108 cnt
+= cinfo
.counts
[i
];
6110 BFD_ASSERT (cnt
== dynsymcount
);
6111 cinfo
.bucketcount
= bucketcount
;
6112 cinfo
.local_indx
= cinfo
.min_dynindx
;
6114 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6115 s
->size
+= cinfo
.maskbits
/ 8;
6116 contents
= bfd_zalloc (output_bfd
, s
->size
);
6117 if (contents
== NULL
)
6119 free (cinfo
.bitmask
);
6120 free (cinfo
.hashcodes
);
6124 s
->contents
= contents
;
6125 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6126 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6127 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6128 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6129 contents
+= 16 + cinfo
.maskbits
/ 8;
6131 for (i
= 0; i
< bucketcount
; ++i
)
6133 if (cinfo
.counts
[i
] == 0)
6134 bfd_put_32 (output_bfd
, 0, contents
);
6136 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6140 cinfo
.contents
= contents
;
6142 /* Renumber dynamic symbols, populate .gnu.hash section. */
6143 elf_link_hash_traverse (elf_hash_table (info
),
6144 elf_renumber_gnu_hash_syms
, &cinfo
);
6146 contents
= s
->contents
+ 16;
6147 for (i
= 0; i
< maskwords
; ++i
)
6149 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6151 contents
+= bed
->s
->arch_size
/ 8;
6154 free (cinfo
.bitmask
);
6155 free (cinfo
.hashcodes
);
6159 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6160 BFD_ASSERT (s
!= NULL
);
6162 elf_finalize_dynstr (output_bfd
, info
);
6164 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6166 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6167 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6174 /* Final phase of ELF linker. */
6176 /* A structure we use to avoid passing large numbers of arguments. */
6178 struct elf_final_link_info
6180 /* General link information. */
6181 struct bfd_link_info
*info
;
6184 /* Symbol string table. */
6185 struct bfd_strtab_hash
*symstrtab
;
6186 /* .dynsym section. */
6187 asection
*dynsym_sec
;
6188 /* .hash section. */
6190 /* symbol version section (.gnu.version). */
6191 asection
*symver_sec
;
6192 /* Buffer large enough to hold contents of any section. */
6194 /* Buffer large enough to hold external relocs of any section. */
6195 void *external_relocs
;
6196 /* Buffer large enough to hold internal relocs of any section. */
6197 Elf_Internal_Rela
*internal_relocs
;
6198 /* Buffer large enough to hold external local symbols of any input
6200 bfd_byte
*external_syms
;
6201 /* And a buffer for symbol section indices. */
6202 Elf_External_Sym_Shndx
*locsym_shndx
;
6203 /* Buffer large enough to hold internal local symbols of any input
6205 Elf_Internal_Sym
*internal_syms
;
6206 /* Array large enough to hold a symbol index for each local symbol
6207 of any input BFD. */
6209 /* Array large enough to hold a section pointer for each local
6210 symbol of any input BFD. */
6211 asection
**sections
;
6212 /* Buffer to hold swapped out symbols. */
6214 /* And one for symbol section indices. */
6215 Elf_External_Sym_Shndx
*symshndxbuf
;
6216 /* Number of swapped out symbols in buffer. */
6217 size_t symbuf_count
;
6218 /* Number of symbols which fit in symbuf. */
6220 /* And same for symshndxbuf. */
6221 size_t shndxbuf_size
;
6224 /* This struct is used to pass information to elf_link_output_extsym. */
6226 struct elf_outext_info
6229 bfd_boolean localsyms
;
6230 struct elf_final_link_info
*finfo
;
6233 /* When performing a relocatable link, the input relocations are
6234 preserved. But, if they reference global symbols, the indices
6235 referenced must be updated. Update all the relocations in
6236 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
6239 elf_link_adjust_relocs (bfd
*abfd
,
6240 Elf_Internal_Shdr
*rel_hdr
,
6242 struct elf_link_hash_entry
**rel_hash
)
6245 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6247 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6248 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6249 bfd_vma r_type_mask
;
6252 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
6254 swap_in
= bed
->s
->swap_reloc_in
;
6255 swap_out
= bed
->s
->swap_reloc_out
;
6257 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
6259 swap_in
= bed
->s
->swap_reloca_in
;
6260 swap_out
= bed
->s
->swap_reloca_out
;
6265 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
6268 if (bed
->s
->arch_size
== 32)
6275 r_type_mask
= 0xffffffff;
6279 erela
= rel_hdr
->contents
;
6280 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
6282 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
6285 if (*rel_hash
== NULL
)
6288 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
6290 (*swap_in
) (abfd
, erela
, irela
);
6291 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
6292 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
6293 | (irela
[j
].r_info
& r_type_mask
));
6294 (*swap_out
) (abfd
, irela
, erela
);
6298 struct elf_link_sort_rela
6304 enum elf_reloc_type_class type
;
6305 /* We use this as an array of size int_rels_per_ext_rel. */
6306 Elf_Internal_Rela rela
[1];
6310 elf_link_sort_cmp1 (const void *A
, const void *B
)
6312 const struct elf_link_sort_rela
*a
= A
;
6313 const struct elf_link_sort_rela
*b
= B
;
6314 int relativea
, relativeb
;
6316 relativea
= a
->type
== reloc_class_relative
;
6317 relativeb
= b
->type
== reloc_class_relative
;
6319 if (relativea
< relativeb
)
6321 if (relativea
> relativeb
)
6323 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
6325 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
6327 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6329 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6335 elf_link_sort_cmp2 (const void *A
, const void *B
)
6337 const struct elf_link_sort_rela
*a
= A
;
6338 const struct elf_link_sort_rela
*b
= B
;
6341 if (a
->u
.offset
< b
->u
.offset
)
6343 if (a
->u
.offset
> b
->u
.offset
)
6345 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
6346 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
6351 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6353 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6359 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
6362 bfd_size_type count
, size
;
6363 size_t i
, ret
, sort_elt
, ext_size
;
6364 bfd_byte
*sort
, *s_non_relative
, *p
;
6365 struct elf_link_sort_rela
*sq
;
6366 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6367 int i2e
= bed
->s
->int_rels_per_ext_rel
;
6368 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6369 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6370 struct bfd_link_order
*lo
;
6373 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
6374 if (reldyn
== NULL
|| reldyn
->size
== 0)
6376 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
6377 if (reldyn
== NULL
|| reldyn
->size
== 0)
6379 ext_size
= bed
->s
->sizeof_rel
;
6380 swap_in
= bed
->s
->swap_reloc_in
;
6381 swap_out
= bed
->s
->swap_reloc_out
;
6385 ext_size
= bed
->s
->sizeof_rela
;
6386 swap_in
= bed
->s
->swap_reloca_in
;
6387 swap_out
= bed
->s
->swap_reloca_out
;
6389 count
= reldyn
->size
/ ext_size
;
6392 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6393 if (lo
->type
== bfd_indirect_link_order
)
6395 asection
*o
= lo
->u
.indirect
.section
;
6399 if (size
!= reldyn
->size
)
6402 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6403 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6404 sort
= bfd_zmalloc (sort_elt
* count
);
6407 (*info
->callbacks
->warning
)
6408 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6412 if (bed
->s
->arch_size
== 32)
6413 r_sym_mask
= ~(bfd_vma
) 0xff;
6415 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6417 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6418 if (lo
->type
== bfd_indirect_link_order
)
6420 bfd_byte
*erel
, *erelend
;
6421 asection
*o
= lo
->u
.indirect
.section
;
6423 if (o
->contents
== NULL
&& o
->size
!= 0)
6425 /* This is a reloc section that is being handled as a normal
6426 section. See bfd_section_from_shdr. We can't combine
6427 relocs in this case. */
6432 erelend
= o
->contents
+ o
->size
;
6433 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6434 while (erel
< erelend
)
6436 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6437 (*swap_in
) (abfd
, erel
, s
->rela
);
6438 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6439 s
->u
.sym_mask
= r_sym_mask
;
6445 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6447 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6449 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6450 if (s
->type
!= reloc_class_relative
)
6456 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6457 for (; i
< count
; i
++, p
+= sort_elt
)
6459 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6460 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6462 sp
->u
.offset
= sq
->rela
->r_offset
;
6465 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6467 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6468 if (lo
->type
== bfd_indirect_link_order
)
6470 bfd_byte
*erel
, *erelend
;
6471 asection
*o
= lo
->u
.indirect
.section
;
6474 erelend
= o
->contents
+ o
->size
;
6475 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6476 while (erel
< erelend
)
6478 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6479 (*swap_out
) (abfd
, s
->rela
, erel
);
6490 /* Flush the output symbols to the file. */
6493 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6494 const struct elf_backend_data
*bed
)
6496 if (finfo
->symbuf_count
> 0)
6498 Elf_Internal_Shdr
*hdr
;
6502 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6503 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6504 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6505 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6506 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6509 hdr
->sh_size
+= amt
;
6510 finfo
->symbuf_count
= 0;
6516 /* Add a symbol to the output symbol table. */
6519 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6521 Elf_Internal_Sym
*elfsym
,
6522 asection
*input_sec
,
6523 struct elf_link_hash_entry
*h
)
6526 Elf_External_Sym_Shndx
*destshndx
;
6527 bfd_boolean (*output_symbol_hook
)
6528 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6529 struct elf_link_hash_entry
*);
6530 const struct elf_backend_data
*bed
;
6532 bed
= get_elf_backend_data (finfo
->output_bfd
);
6533 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6534 if (output_symbol_hook
!= NULL
)
6536 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6540 if (name
== NULL
|| *name
== '\0')
6541 elfsym
->st_name
= 0;
6542 else if (input_sec
->flags
& SEC_EXCLUDE
)
6543 elfsym
->st_name
= 0;
6546 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6548 if (elfsym
->st_name
== (unsigned long) -1)
6552 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6554 if (! elf_link_flush_output_syms (finfo
, bed
))
6558 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6559 destshndx
= finfo
->symshndxbuf
;
6560 if (destshndx
!= NULL
)
6562 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6566 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6567 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6568 if (destshndx
== NULL
)
6570 memset ((char *) destshndx
+ amt
, 0, amt
);
6571 finfo
->shndxbuf_size
*= 2;
6573 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6576 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6577 finfo
->symbuf_count
+= 1;
6578 bfd_get_symcount (finfo
->output_bfd
) += 1;
6583 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6586 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
6588 if (sym
->st_shndx
> SHN_HIRESERVE
)
6590 /* The gABI doesn't support dynamic symbols in output sections
6592 (*_bfd_error_handler
)
6593 (_("%B: Too many sections: %d (>= %d)"),
6594 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
6595 bfd_set_error (bfd_error_nonrepresentable_section
);
6601 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6602 allowing an unsatisfied unversioned symbol in the DSO to match a
6603 versioned symbol that would normally require an explicit version.
6604 We also handle the case that a DSO references a hidden symbol
6605 which may be satisfied by a versioned symbol in another DSO. */
6608 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6609 const struct elf_backend_data
*bed
,
6610 struct elf_link_hash_entry
*h
)
6613 struct elf_link_loaded_list
*loaded
;
6615 if (!is_elf_hash_table (info
->hash
))
6618 switch (h
->root
.type
)
6624 case bfd_link_hash_undefined
:
6625 case bfd_link_hash_undefweak
:
6626 abfd
= h
->root
.u
.undef
.abfd
;
6627 if ((abfd
->flags
& DYNAMIC
) == 0
6628 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6632 case bfd_link_hash_defined
:
6633 case bfd_link_hash_defweak
:
6634 abfd
= h
->root
.u
.def
.section
->owner
;
6637 case bfd_link_hash_common
:
6638 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6641 BFD_ASSERT (abfd
!= NULL
);
6643 for (loaded
= elf_hash_table (info
)->loaded
;
6645 loaded
= loaded
->next
)
6648 Elf_Internal_Shdr
*hdr
;
6649 bfd_size_type symcount
;
6650 bfd_size_type extsymcount
;
6651 bfd_size_type extsymoff
;
6652 Elf_Internal_Shdr
*versymhdr
;
6653 Elf_Internal_Sym
*isym
;
6654 Elf_Internal_Sym
*isymend
;
6655 Elf_Internal_Sym
*isymbuf
;
6656 Elf_External_Versym
*ever
;
6657 Elf_External_Versym
*extversym
;
6659 input
= loaded
->abfd
;
6661 /* We check each DSO for a possible hidden versioned definition. */
6663 || (input
->flags
& DYNAMIC
) == 0
6664 || elf_dynversym (input
) == 0)
6667 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6669 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6670 if (elf_bad_symtab (input
))
6672 extsymcount
= symcount
;
6677 extsymcount
= symcount
- hdr
->sh_info
;
6678 extsymoff
= hdr
->sh_info
;
6681 if (extsymcount
== 0)
6684 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6686 if (isymbuf
== NULL
)
6689 /* Read in any version definitions. */
6690 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6691 extversym
= bfd_malloc (versymhdr
->sh_size
);
6692 if (extversym
== NULL
)
6695 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6696 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6697 != versymhdr
->sh_size
))
6705 ever
= extversym
+ extsymoff
;
6706 isymend
= isymbuf
+ extsymcount
;
6707 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6710 Elf_Internal_Versym iver
;
6711 unsigned short version_index
;
6713 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6714 || isym
->st_shndx
== SHN_UNDEF
)
6717 name
= bfd_elf_string_from_elf_section (input
,
6720 if (strcmp (name
, h
->root
.root
.string
) != 0)
6723 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6725 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6727 /* If we have a non-hidden versioned sym, then it should
6728 have provided a definition for the undefined sym. */
6732 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6733 if (version_index
== 1 || version_index
== 2)
6735 /* This is the base or first version. We can use it. */
6749 /* Add an external symbol to the symbol table. This is called from
6750 the hash table traversal routine. When generating a shared object,
6751 we go through the symbol table twice. The first time we output
6752 anything that might have been forced to local scope in a version
6753 script. The second time we output the symbols that are still
6757 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6759 struct elf_outext_info
*eoinfo
= data
;
6760 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6762 Elf_Internal_Sym sym
;
6763 asection
*input_sec
;
6764 const struct elf_backend_data
*bed
;
6766 if (h
->root
.type
== bfd_link_hash_warning
)
6768 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6769 if (h
->root
.type
== bfd_link_hash_new
)
6773 /* Decide whether to output this symbol in this pass. */
6774 if (eoinfo
->localsyms
)
6776 if (!h
->forced_local
)
6781 if (h
->forced_local
)
6785 bed
= get_elf_backend_data (finfo
->output_bfd
);
6787 if (h
->root
.type
== bfd_link_hash_undefined
)
6789 /* If we have an undefined symbol reference here then it must have
6790 come from a shared library that is being linked in. (Undefined
6791 references in regular files have already been handled). */
6792 bfd_boolean ignore_undef
= FALSE
;
6794 /* Some symbols may be special in that the fact that they're
6795 undefined can be safely ignored - let backend determine that. */
6796 if (bed
->elf_backend_ignore_undef_symbol
)
6797 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6799 /* If we are reporting errors for this situation then do so now. */
6800 if (ignore_undef
== FALSE
6803 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6804 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6806 if (! (finfo
->info
->callbacks
->undefined_symbol
6807 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6808 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6810 eoinfo
->failed
= TRUE
;
6816 /* We should also warn if a forced local symbol is referenced from
6817 shared libraries. */
6818 if (! finfo
->info
->relocatable
6819 && (! finfo
->info
->shared
)
6824 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6826 (*_bfd_error_handler
)
6827 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6829 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6830 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6831 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6833 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6834 ? "hidden" : "local",
6835 h
->root
.root
.string
);
6836 eoinfo
->failed
= TRUE
;
6840 /* We don't want to output symbols that have never been mentioned by
6841 a regular file, or that we have been told to strip. However, if
6842 h->indx is set to -2, the symbol is used by a reloc and we must
6846 else if ((h
->def_dynamic
6848 || h
->root
.type
== bfd_link_hash_new
)
6852 else if (finfo
->info
->strip
== strip_all
)
6854 else if (finfo
->info
->strip
== strip_some
6855 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6856 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6858 else if (finfo
->info
->strip_discarded
6859 && (h
->root
.type
== bfd_link_hash_defined
6860 || h
->root
.type
== bfd_link_hash_defweak
)
6861 && elf_discarded_section (h
->root
.u
.def
.section
))
6866 /* If we're stripping it, and it's not a dynamic symbol, there's
6867 nothing else to do unless it is a forced local symbol. */
6870 && !h
->forced_local
)
6874 sym
.st_size
= h
->size
;
6875 sym
.st_other
= h
->other
;
6876 if (h
->forced_local
)
6877 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6878 else if (h
->root
.type
== bfd_link_hash_undefweak
6879 || h
->root
.type
== bfd_link_hash_defweak
)
6880 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6882 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6884 switch (h
->root
.type
)
6887 case bfd_link_hash_new
:
6888 case bfd_link_hash_warning
:
6892 case bfd_link_hash_undefined
:
6893 case bfd_link_hash_undefweak
:
6894 input_sec
= bfd_und_section_ptr
;
6895 sym
.st_shndx
= SHN_UNDEF
;
6898 case bfd_link_hash_defined
:
6899 case bfd_link_hash_defweak
:
6901 input_sec
= h
->root
.u
.def
.section
;
6902 if (input_sec
->output_section
!= NULL
)
6905 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6906 input_sec
->output_section
);
6907 if (sym
.st_shndx
== SHN_BAD
)
6909 (*_bfd_error_handler
)
6910 (_("%B: could not find output section %A for input section %A"),
6911 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6912 eoinfo
->failed
= TRUE
;
6916 /* ELF symbols in relocatable files are section relative,
6917 but in nonrelocatable files they are virtual
6919 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6920 if (! finfo
->info
->relocatable
)
6922 sym
.st_value
+= input_sec
->output_section
->vma
;
6923 if (h
->type
== STT_TLS
)
6925 /* STT_TLS symbols are relative to PT_TLS segment
6927 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6928 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6934 BFD_ASSERT (input_sec
->owner
== NULL
6935 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6936 sym
.st_shndx
= SHN_UNDEF
;
6937 input_sec
= bfd_und_section_ptr
;
6942 case bfd_link_hash_common
:
6943 input_sec
= h
->root
.u
.c
.p
->section
;
6944 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6945 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6948 case bfd_link_hash_indirect
:
6949 /* These symbols are created by symbol versioning. They point
6950 to the decorated version of the name. For example, if the
6951 symbol foo@@GNU_1.2 is the default, which should be used when
6952 foo is used with no version, then we add an indirect symbol
6953 foo which points to foo@@GNU_1.2. We ignore these symbols,
6954 since the indirected symbol is already in the hash table. */
6958 /* Give the processor backend a chance to tweak the symbol value,
6959 and also to finish up anything that needs to be done for this
6960 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6961 forced local syms when non-shared is due to a historical quirk. */
6962 if ((h
->dynindx
!= -1
6964 && ((finfo
->info
->shared
6965 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6966 || h
->root
.type
!= bfd_link_hash_undefweak
))
6967 || !h
->forced_local
)
6968 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6970 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6971 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6973 eoinfo
->failed
= TRUE
;
6978 /* If we are marking the symbol as undefined, and there are no
6979 non-weak references to this symbol from a regular object, then
6980 mark the symbol as weak undefined; if there are non-weak
6981 references, mark the symbol as strong. We can't do this earlier,
6982 because it might not be marked as undefined until the
6983 finish_dynamic_symbol routine gets through with it. */
6984 if (sym
.st_shndx
== SHN_UNDEF
6986 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6987 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6991 if (h
->ref_regular_nonweak
)
6992 bindtype
= STB_GLOBAL
;
6994 bindtype
= STB_WEAK
;
6995 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6998 /* If a non-weak symbol with non-default visibility is not defined
6999 locally, it is a fatal error. */
7000 if (! finfo
->info
->relocatable
7001 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7002 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7003 && h
->root
.type
== bfd_link_hash_undefined
7006 (*_bfd_error_handler
)
7007 (_("%B: %s symbol `%s' isn't defined"),
7009 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7011 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7012 ? "internal" : "hidden",
7013 h
->root
.root
.string
);
7014 eoinfo
->failed
= TRUE
;
7018 /* If this symbol should be put in the .dynsym section, then put it
7019 there now. We already know the symbol index. We also fill in
7020 the entry in the .hash section. */
7021 if (h
->dynindx
!= -1
7022 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7028 sym
.st_name
= h
->dynstr_index
;
7029 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7030 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7032 eoinfo
->failed
= TRUE
;
7035 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7037 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7038 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7040 if (finfo
->hash_sec
!= NULL
)
7042 size_t hash_entry_size
;
7043 bfd_byte
*bucketpos
;
7047 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7048 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7049 + (bucket
+ 2) * hash_entry_size
);
7050 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7051 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7052 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7053 ((bfd_byte
*) finfo
->hash_sec
->contents
7054 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7057 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7059 Elf_Internal_Versym iversym
;
7060 Elf_External_Versym
*eversym
;
7062 if (!h
->def_regular
)
7064 if (h
->verinfo
.verdef
== NULL
)
7065 iversym
.vs_vers
= 0;
7067 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7071 if (h
->verinfo
.vertree
== NULL
)
7072 iversym
.vs_vers
= 1;
7074 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7075 if (finfo
->info
->create_default_symver
)
7080 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7082 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7083 eversym
+= h
->dynindx
;
7084 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7088 /* If we're stripping it, then it was just a dynamic symbol, and
7089 there's nothing else to do. */
7090 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7093 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7095 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7097 eoinfo
->failed
= TRUE
;
7104 /* Return TRUE if special handling is done for relocs in SEC against
7105 symbols defined in discarded sections. */
7108 elf_section_ignore_discarded_relocs (asection
*sec
)
7110 const struct elf_backend_data
*bed
;
7112 switch (sec
->sec_info_type
)
7114 case ELF_INFO_TYPE_STABS
:
7115 case ELF_INFO_TYPE_EH_FRAME
:
7121 bed
= get_elf_backend_data (sec
->owner
);
7122 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7123 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7129 /* Return a mask saying how ld should treat relocations in SEC against
7130 symbols defined in discarded sections. If this function returns
7131 COMPLAIN set, ld will issue a warning message. If this function
7132 returns PRETEND set, and the discarded section was link-once and the
7133 same size as the kept link-once section, ld will pretend that the
7134 symbol was actually defined in the kept section. Otherwise ld will
7135 zero the reloc (at least that is the intent, but some cooperation by
7136 the target dependent code is needed, particularly for REL targets). */
7139 _bfd_elf_default_action_discarded (asection
*sec
)
7141 if (sec
->flags
& SEC_DEBUGGING
)
7144 if (strcmp (".eh_frame", sec
->name
) == 0)
7147 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7150 return COMPLAIN
| PRETEND
;
7153 /* Find a match between a section and a member of a section group. */
7156 match_group_member (asection
*sec
, asection
*group
)
7158 asection
*first
= elf_next_in_group (group
);
7159 asection
*s
= first
;
7163 if (bfd_elf_match_symbols_in_sections (s
, sec
))
7166 s
= elf_next_in_group (s
);
7174 /* Check if the kept section of a discarded section SEC can be used
7175 to replace it. Return the replacement if it is OK. Otherwise return
7179 _bfd_elf_check_kept_section (asection
*sec
)
7183 kept
= sec
->kept_section
;
7186 if (elf_sec_group (sec
) != NULL
)
7187 kept
= match_group_member (sec
, kept
);
7188 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
7194 /* Link an input file into the linker output file. This function
7195 handles all the sections and relocations of the input file at once.
7196 This is so that we only have to read the local symbols once, and
7197 don't have to keep them in memory. */
7200 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
7202 bfd_boolean (*relocate_section
)
7203 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
7204 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
7206 Elf_Internal_Shdr
*symtab_hdr
;
7209 Elf_Internal_Sym
*isymbuf
;
7210 Elf_Internal_Sym
*isym
;
7211 Elf_Internal_Sym
*isymend
;
7213 asection
**ppsection
;
7215 const struct elf_backend_data
*bed
;
7216 bfd_boolean emit_relocs
;
7217 struct elf_link_hash_entry
**sym_hashes
;
7219 output_bfd
= finfo
->output_bfd
;
7220 bed
= get_elf_backend_data (output_bfd
);
7221 relocate_section
= bed
->elf_backend_relocate_section
;
7223 /* If this is a dynamic object, we don't want to do anything here:
7224 we don't want the local symbols, and we don't want the section
7226 if ((input_bfd
->flags
& DYNAMIC
) != 0)
7229 emit_relocs
= (finfo
->info
->relocatable
7230 || finfo
->info
->emitrelocations
);
7232 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7233 if (elf_bad_symtab (input_bfd
))
7235 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7240 locsymcount
= symtab_hdr
->sh_info
;
7241 extsymoff
= symtab_hdr
->sh_info
;
7244 /* Read the local symbols. */
7245 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7246 if (isymbuf
== NULL
&& locsymcount
!= 0)
7248 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
7249 finfo
->internal_syms
,
7250 finfo
->external_syms
,
7251 finfo
->locsym_shndx
);
7252 if (isymbuf
== NULL
)
7256 /* Find local symbol sections and adjust values of symbols in
7257 SEC_MERGE sections. Write out those local symbols we know are
7258 going into the output file. */
7259 isymend
= isymbuf
+ locsymcount
;
7260 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
7262 isym
++, pindex
++, ppsection
++)
7266 Elf_Internal_Sym osym
;
7270 if (elf_bad_symtab (input_bfd
))
7272 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
7279 if (isym
->st_shndx
== SHN_UNDEF
)
7280 isec
= bfd_und_section_ptr
;
7281 else if (isym
->st_shndx
< SHN_LORESERVE
7282 || isym
->st_shndx
> SHN_HIRESERVE
)
7284 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
7286 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
7287 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
7289 _bfd_merged_section_offset (output_bfd
, &isec
,
7290 elf_section_data (isec
)->sec_info
,
7293 else if (isym
->st_shndx
== SHN_ABS
)
7294 isec
= bfd_abs_section_ptr
;
7295 else if (isym
->st_shndx
== SHN_COMMON
)
7296 isec
= bfd_com_section_ptr
;
7299 /* Don't attempt to output symbols with st_shnx in the
7300 reserved range other than SHN_ABS and SHN_COMMON. */
7307 /* Don't output the first, undefined, symbol. */
7308 if (ppsection
== finfo
->sections
)
7311 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7313 /* We never output section symbols. Instead, we use the
7314 section symbol of the corresponding section in the output
7319 /* If we are stripping all symbols, we don't want to output this
7321 if (finfo
->info
->strip
== strip_all
)
7324 /* If we are discarding all local symbols, we don't want to
7325 output this one. If we are generating a relocatable output
7326 file, then some of the local symbols may be required by
7327 relocs; we output them below as we discover that they are
7329 if (finfo
->info
->discard
== discard_all
)
7332 /* If this symbol is defined in a section which we are
7333 discarding, we don't need to keep it. */
7334 if (isym
->st_shndx
!= SHN_UNDEF
7335 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
7337 || bfd_section_removed_from_list (output_bfd
,
7338 isec
->output_section
)))
7341 /* Get the name of the symbol. */
7342 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
7347 /* See if we are discarding symbols with this name. */
7348 if ((finfo
->info
->strip
== strip_some
7349 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
7351 || (((finfo
->info
->discard
== discard_sec_merge
7352 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
7353 || finfo
->info
->discard
== discard_l
)
7354 && bfd_is_local_label_name (input_bfd
, name
)))
7357 /* If we get here, we are going to output this symbol. */
7361 /* Adjust the section index for the output file. */
7362 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
7363 isec
->output_section
);
7364 if (osym
.st_shndx
== SHN_BAD
)
7367 *pindex
= bfd_get_symcount (output_bfd
);
7369 /* ELF symbols in relocatable files are section relative, but
7370 in executable files they are virtual addresses. Note that
7371 this code assumes that all ELF sections have an associated
7372 BFD section with a reasonable value for output_offset; below
7373 we assume that they also have a reasonable value for
7374 output_section. Any special sections must be set up to meet
7375 these requirements. */
7376 osym
.st_value
+= isec
->output_offset
;
7377 if (! finfo
->info
->relocatable
)
7379 osym
.st_value
+= isec
->output_section
->vma
;
7380 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
7382 /* STT_TLS symbols are relative to PT_TLS segment base. */
7383 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7384 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7388 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
7392 /* Relocate the contents of each section. */
7393 sym_hashes
= elf_sym_hashes (input_bfd
);
7394 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7398 if (! o
->linker_mark
)
7400 /* This section was omitted from the link. */
7404 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7405 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
7408 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
7410 /* Section was created by _bfd_elf_link_create_dynamic_sections
7415 /* Get the contents of the section. They have been cached by a
7416 relaxation routine. Note that o is a section in an input
7417 file, so the contents field will not have been set by any of
7418 the routines which work on output files. */
7419 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
7420 contents
= elf_section_data (o
)->this_hdr
.contents
;
7423 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
7425 contents
= finfo
->contents
;
7426 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
7430 if ((o
->flags
& SEC_RELOC
) != 0)
7432 Elf_Internal_Rela
*internal_relocs
;
7433 bfd_vma r_type_mask
;
7436 /* Get the swapped relocs. */
7438 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7439 finfo
->internal_relocs
, FALSE
);
7440 if (internal_relocs
== NULL
7441 && o
->reloc_count
> 0)
7444 if (bed
->s
->arch_size
== 32)
7451 r_type_mask
= 0xffffffff;
7455 /* Run through the relocs looking for any against symbols
7456 from discarded sections and section symbols from
7457 removed link-once sections. Complain about relocs
7458 against discarded sections. Zero relocs against removed
7459 link-once sections. */
7460 if (!elf_section_ignore_discarded_relocs (o
))
7462 Elf_Internal_Rela
*rel
, *relend
;
7463 unsigned int action
= (*bed
->action_discarded
) (o
);
7465 rel
= internal_relocs
;
7466 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7467 for ( ; rel
< relend
; rel
++)
7469 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7470 asection
**ps
, *sec
;
7471 struct elf_link_hash_entry
*h
= NULL
;
7472 const char *sym_name
;
7474 if (r_symndx
== STN_UNDEF
)
7477 if (r_symndx
>= locsymcount
7478 || (elf_bad_symtab (input_bfd
)
7479 && finfo
->sections
[r_symndx
] == NULL
))
7481 h
= sym_hashes
[r_symndx
- extsymoff
];
7483 /* Badly formatted input files can contain relocs that
7484 reference non-existant symbols. Check here so that
7485 we do not seg fault. */
7490 sprintf_vma (buffer
, rel
->r_info
);
7491 (*_bfd_error_handler
)
7492 (_("error: %B contains a reloc (0x%s) for section %A "
7493 "that references a non-existent global symbol"),
7494 input_bfd
, o
, buffer
);
7495 bfd_set_error (bfd_error_bad_value
);
7499 while (h
->root
.type
== bfd_link_hash_indirect
7500 || h
->root
.type
== bfd_link_hash_warning
)
7501 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7503 if (h
->root
.type
!= bfd_link_hash_defined
7504 && h
->root
.type
!= bfd_link_hash_defweak
)
7507 ps
= &h
->root
.u
.def
.section
;
7508 sym_name
= h
->root
.root
.string
;
7512 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7513 ps
= &finfo
->sections
[r_symndx
];
7514 sym_name
= bfd_elf_sym_name (input_bfd
,
7519 /* Complain if the definition comes from a
7520 discarded section. */
7521 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7523 BFD_ASSERT (r_symndx
!= 0);
7524 if (action
& COMPLAIN
)
7525 (*finfo
->info
->callbacks
->einfo
)
7526 (_("%X`%s' referenced in section `%A' of %B: "
7527 "defined in discarded section `%A' of %B\n"),
7528 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7530 /* Try to do the best we can to support buggy old
7531 versions of gcc. Pretend that the symbol is
7532 really defined in the kept linkonce section.
7533 FIXME: This is quite broken. Modifying the
7534 symbol here means we will be changing all later
7535 uses of the symbol, not just in this section. */
7536 if (action
& PRETEND
)
7540 kept
= _bfd_elf_check_kept_section (sec
);
7548 /* Remove the symbol reference from the reloc, but
7549 don't kill the reloc completely. This is so that
7550 a zero value will be written into the section,
7551 which may have non-zero contents put there by the
7552 assembler. Zero in things like an eh_frame fde
7553 pc_begin allows stack unwinders to recognize the
7555 rel
->r_info
&= r_type_mask
;
7561 /* Relocate the section by invoking a back end routine.
7563 The back end routine is responsible for adjusting the
7564 section contents as necessary, and (if using Rela relocs
7565 and generating a relocatable output file) adjusting the
7566 reloc addend as necessary.
7568 The back end routine does not have to worry about setting
7569 the reloc address or the reloc symbol index.
7571 The back end routine is given a pointer to the swapped in
7572 internal symbols, and can access the hash table entries
7573 for the external symbols via elf_sym_hashes (input_bfd).
7575 When generating relocatable output, the back end routine
7576 must handle STB_LOCAL/STT_SECTION symbols specially. The
7577 output symbol is going to be a section symbol
7578 corresponding to the output section, which will require
7579 the addend to be adjusted. */
7581 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7582 input_bfd
, o
, contents
,
7590 Elf_Internal_Rela
*irela
;
7591 Elf_Internal_Rela
*irelaend
;
7592 bfd_vma last_offset
;
7593 struct elf_link_hash_entry
**rel_hash
;
7594 struct elf_link_hash_entry
**rel_hash_list
;
7595 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7596 unsigned int next_erel
;
7597 bfd_boolean rela_normal
;
7599 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7600 rela_normal
= (bed
->rela_normal
7601 && (input_rel_hdr
->sh_entsize
7602 == bed
->s
->sizeof_rela
));
7604 /* Adjust the reloc addresses and symbol indices. */
7606 irela
= internal_relocs
;
7607 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7608 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7609 + elf_section_data (o
->output_section
)->rel_count
7610 + elf_section_data (o
->output_section
)->rel_count2
);
7611 rel_hash_list
= rel_hash
;
7612 last_offset
= o
->output_offset
;
7613 if (!finfo
->info
->relocatable
)
7614 last_offset
+= o
->output_section
->vma
;
7615 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7617 unsigned long r_symndx
;
7619 Elf_Internal_Sym sym
;
7621 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7627 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7630 if (irela
->r_offset
>= (bfd_vma
) -2)
7632 /* This is a reloc for a deleted entry or somesuch.
7633 Turn it into an R_*_NONE reloc, at the same
7634 offset as the last reloc. elf_eh_frame.c and
7635 bfd_elf_discard_info rely on reloc offsets
7637 irela
->r_offset
= last_offset
;
7639 irela
->r_addend
= 0;
7643 irela
->r_offset
+= o
->output_offset
;
7645 /* Relocs in an executable have to be virtual addresses. */
7646 if (!finfo
->info
->relocatable
)
7647 irela
->r_offset
+= o
->output_section
->vma
;
7649 last_offset
= irela
->r_offset
;
7651 r_symndx
= irela
->r_info
>> r_sym_shift
;
7652 if (r_symndx
== STN_UNDEF
)
7655 if (r_symndx
>= locsymcount
7656 || (elf_bad_symtab (input_bfd
)
7657 && finfo
->sections
[r_symndx
] == NULL
))
7659 struct elf_link_hash_entry
*rh
;
7662 /* This is a reloc against a global symbol. We
7663 have not yet output all the local symbols, so
7664 we do not know the symbol index of any global
7665 symbol. We set the rel_hash entry for this
7666 reloc to point to the global hash table entry
7667 for this symbol. The symbol index is then
7668 set at the end of bfd_elf_final_link. */
7669 indx
= r_symndx
- extsymoff
;
7670 rh
= elf_sym_hashes (input_bfd
)[indx
];
7671 while (rh
->root
.type
== bfd_link_hash_indirect
7672 || rh
->root
.type
== bfd_link_hash_warning
)
7673 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7675 /* Setting the index to -2 tells
7676 elf_link_output_extsym that this symbol is
7678 BFD_ASSERT (rh
->indx
< 0);
7686 /* This is a reloc against a local symbol. */
7689 sym
= isymbuf
[r_symndx
];
7690 sec
= finfo
->sections
[r_symndx
];
7691 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7693 /* I suppose the backend ought to fill in the
7694 section of any STT_SECTION symbol against a
7695 processor specific section. */
7697 if (bfd_is_abs_section (sec
))
7699 else if (sec
== NULL
|| sec
->owner
== NULL
)
7701 bfd_set_error (bfd_error_bad_value
);
7706 asection
*osec
= sec
->output_section
;
7708 /* If we have discarded a section, the output
7709 section will be the absolute section. In
7710 case of discarded link-once and discarded
7711 SEC_MERGE sections, use the kept section. */
7712 if (bfd_is_abs_section (osec
)
7713 && sec
->kept_section
!= NULL
7714 && sec
->kept_section
->output_section
!= NULL
)
7716 osec
= sec
->kept_section
->output_section
;
7717 irela
->r_addend
-= osec
->vma
;
7720 if (!bfd_is_abs_section (osec
))
7722 r_symndx
= osec
->target_index
;
7723 BFD_ASSERT (r_symndx
!= 0);
7727 /* Adjust the addend according to where the
7728 section winds up in the output section. */
7730 irela
->r_addend
+= sec
->output_offset
;
7734 if (finfo
->indices
[r_symndx
] == -1)
7736 unsigned long shlink
;
7740 if (finfo
->info
->strip
== strip_all
)
7742 /* You can't do ld -r -s. */
7743 bfd_set_error (bfd_error_invalid_operation
);
7747 /* This symbol was skipped earlier, but
7748 since it is needed by a reloc, we
7749 must output it now. */
7750 shlink
= symtab_hdr
->sh_link
;
7751 name
= (bfd_elf_string_from_elf_section
7752 (input_bfd
, shlink
, sym
.st_name
));
7756 osec
= sec
->output_section
;
7758 _bfd_elf_section_from_bfd_section (output_bfd
,
7760 if (sym
.st_shndx
== SHN_BAD
)
7763 sym
.st_value
+= sec
->output_offset
;
7764 if (! finfo
->info
->relocatable
)
7766 sym
.st_value
+= osec
->vma
;
7767 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7769 /* STT_TLS symbols are relative to PT_TLS
7771 BFD_ASSERT (elf_hash_table (finfo
->info
)
7773 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7778 finfo
->indices
[r_symndx
]
7779 = bfd_get_symcount (output_bfd
);
7781 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7786 r_symndx
= finfo
->indices
[r_symndx
];
7789 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7790 | (irela
->r_info
& r_type_mask
));
7793 /* Swap out the relocs. */
7794 if (input_rel_hdr
->sh_size
!= 0
7795 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7801 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7802 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7804 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7805 * bed
->s
->int_rels_per_ext_rel
);
7806 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7807 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7816 /* Write out the modified section contents. */
7817 if (bed
->elf_backend_write_section
7818 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7820 /* Section written out. */
7822 else switch (o
->sec_info_type
)
7824 case ELF_INFO_TYPE_STABS
:
7825 if (! (_bfd_write_section_stabs
7827 &elf_hash_table (finfo
->info
)->stab_info
,
7828 o
, &elf_section_data (o
)->sec_info
, contents
)))
7831 case ELF_INFO_TYPE_MERGE
:
7832 if (! _bfd_write_merged_section (output_bfd
, o
,
7833 elf_section_data (o
)->sec_info
))
7836 case ELF_INFO_TYPE_EH_FRAME
:
7838 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7845 if (! (o
->flags
& SEC_EXCLUDE
)
7846 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7848 (file_ptr
) o
->output_offset
,
7859 /* Generate a reloc when linking an ELF file. This is a reloc
7860 requested by the linker, and does not come from any input file. This
7861 is used to build constructor and destructor tables when linking
7865 elf_reloc_link_order (bfd
*output_bfd
,
7866 struct bfd_link_info
*info
,
7867 asection
*output_section
,
7868 struct bfd_link_order
*link_order
)
7870 reloc_howto_type
*howto
;
7874 struct elf_link_hash_entry
**rel_hash_ptr
;
7875 Elf_Internal_Shdr
*rel_hdr
;
7876 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7877 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7881 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7884 bfd_set_error (bfd_error_bad_value
);
7888 addend
= link_order
->u
.reloc
.p
->addend
;
7890 /* Figure out the symbol index. */
7891 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7892 + elf_section_data (output_section
)->rel_count
7893 + elf_section_data (output_section
)->rel_count2
);
7894 if (link_order
->type
== bfd_section_reloc_link_order
)
7896 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7897 BFD_ASSERT (indx
!= 0);
7898 *rel_hash_ptr
= NULL
;
7902 struct elf_link_hash_entry
*h
;
7904 /* Treat a reloc against a defined symbol as though it were
7905 actually against the section. */
7906 h
= ((struct elf_link_hash_entry
*)
7907 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7908 link_order
->u
.reloc
.p
->u
.name
,
7909 FALSE
, FALSE
, TRUE
));
7911 && (h
->root
.type
== bfd_link_hash_defined
7912 || h
->root
.type
== bfd_link_hash_defweak
))
7916 section
= h
->root
.u
.def
.section
;
7917 indx
= section
->output_section
->target_index
;
7918 *rel_hash_ptr
= NULL
;
7919 /* It seems that we ought to add the symbol value to the
7920 addend here, but in practice it has already been added
7921 because it was passed to constructor_callback. */
7922 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7926 /* Setting the index to -2 tells elf_link_output_extsym that
7927 this symbol is used by a reloc. */
7934 if (! ((*info
->callbacks
->unattached_reloc
)
7935 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7941 /* If this is an inplace reloc, we must write the addend into the
7943 if (howto
->partial_inplace
&& addend
!= 0)
7946 bfd_reloc_status_type rstat
;
7949 const char *sym_name
;
7951 size
= bfd_get_reloc_size (howto
);
7952 buf
= bfd_zmalloc (size
);
7955 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7962 case bfd_reloc_outofrange
:
7965 case bfd_reloc_overflow
:
7966 if (link_order
->type
== bfd_section_reloc_link_order
)
7967 sym_name
= bfd_section_name (output_bfd
,
7968 link_order
->u
.reloc
.p
->u
.section
);
7970 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7971 if (! ((*info
->callbacks
->reloc_overflow
)
7972 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7973 NULL
, (bfd_vma
) 0)))
7980 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7981 link_order
->offset
, size
);
7987 /* The address of a reloc is relative to the section in a
7988 relocatable file, and is a virtual address in an executable
7990 offset
= link_order
->offset
;
7991 if (! info
->relocatable
)
7992 offset
+= output_section
->vma
;
7994 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7996 irel
[i
].r_offset
= offset
;
7998 irel
[i
].r_addend
= 0;
8000 if (bed
->s
->arch_size
== 32)
8001 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8003 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8005 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8006 erel
= rel_hdr
->contents
;
8007 if (rel_hdr
->sh_type
== SHT_REL
)
8009 erel
+= (elf_section_data (output_section
)->rel_count
8010 * bed
->s
->sizeof_rel
);
8011 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8015 irel
[0].r_addend
= addend
;
8016 erel
+= (elf_section_data (output_section
)->rel_count
8017 * bed
->s
->sizeof_rela
);
8018 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8021 ++elf_section_data (output_section
)->rel_count
;
8027 /* Get the output vma of the section pointed to by the sh_link field. */
8030 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8032 Elf_Internal_Shdr
**elf_shdrp
;
8036 s
= p
->u
.indirect
.section
;
8037 elf_shdrp
= elf_elfsections (s
->owner
);
8038 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8039 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8041 The Intel C compiler generates SHT_IA_64_UNWIND with
8042 SHF_LINK_ORDER. But it doesn't set the sh_link or
8043 sh_info fields. Hence we could get the situation
8044 where elfsec is 0. */
8047 const struct elf_backend_data
*bed
8048 = get_elf_backend_data (s
->owner
);
8049 if (bed
->link_order_error_handler
)
8050 bed
->link_order_error_handler
8051 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8056 s
= elf_shdrp
[elfsec
]->bfd_section
;
8057 return s
->output_section
->vma
+ s
->output_offset
;
8062 /* Compare two sections based on the locations of the sections they are
8063 linked to. Used by elf_fixup_link_order. */
8066 compare_link_order (const void * a
, const void * b
)
8071 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8072 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8079 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8080 order as their linked sections. Returns false if this could not be done
8081 because an output section includes both ordered and unordered
8082 sections. Ideally we'd do this in the linker proper. */
8085 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8090 struct bfd_link_order
*p
;
8092 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8094 struct bfd_link_order
**sections
;
8095 asection
*s
, *other_sec
, *linkorder_sec
;
8099 linkorder_sec
= NULL
;
8102 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8104 if (p
->type
== bfd_indirect_link_order
)
8106 s
= p
->u
.indirect
.section
;
8108 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8109 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8110 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8111 && elfsec
< elf_numsections (sub
)
8112 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8126 if (seen_other
&& seen_linkorder
)
8128 if (other_sec
&& linkorder_sec
)
8129 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8131 linkorder_sec
->owner
, other_sec
,
8134 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
8136 bfd_set_error (bfd_error_bad_value
);
8141 if (!seen_linkorder
)
8144 sections
= (struct bfd_link_order
**)
8145 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
8148 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8150 sections
[seen_linkorder
++] = p
;
8152 /* Sort the input sections in the order of their linked section. */
8153 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
8154 compare_link_order
);
8156 /* Change the offsets of the sections. */
8158 for (n
= 0; n
< seen_linkorder
; n
++)
8160 s
= sections
[n
]->u
.indirect
.section
;
8161 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
8162 s
->output_offset
= offset
;
8163 sections
[n
]->offset
= offset
;
8164 offset
+= sections
[n
]->size
;
8171 /* Do the final step of an ELF link. */
8174 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8176 bfd_boolean dynamic
;
8177 bfd_boolean emit_relocs
;
8179 struct elf_final_link_info finfo
;
8180 register asection
*o
;
8181 register struct bfd_link_order
*p
;
8183 bfd_size_type max_contents_size
;
8184 bfd_size_type max_external_reloc_size
;
8185 bfd_size_type max_internal_reloc_count
;
8186 bfd_size_type max_sym_count
;
8187 bfd_size_type max_sym_shndx_count
;
8189 Elf_Internal_Sym elfsym
;
8191 Elf_Internal_Shdr
*symtab_hdr
;
8192 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8193 Elf_Internal_Shdr
*symstrtab_hdr
;
8194 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8195 struct elf_outext_info eoinfo
;
8197 size_t relativecount
= 0;
8198 asection
*reldyn
= 0;
8201 if (! is_elf_hash_table (info
->hash
))
8205 abfd
->flags
|= DYNAMIC
;
8207 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
8208 dynobj
= elf_hash_table (info
)->dynobj
;
8210 emit_relocs
= (info
->relocatable
8211 || info
->emitrelocations
);
8214 finfo
.output_bfd
= abfd
;
8215 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
8216 if (finfo
.symstrtab
== NULL
)
8221 finfo
.dynsym_sec
= NULL
;
8222 finfo
.hash_sec
= NULL
;
8223 finfo
.symver_sec
= NULL
;
8227 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
8228 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
8229 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
8230 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
8231 /* Note that it is OK if symver_sec is NULL. */
8234 finfo
.contents
= NULL
;
8235 finfo
.external_relocs
= NULL
;
8236 finfo
.internal_relocs
= NULL
;
8237 finfo
.external_syms
= NULL
;
8238 finfo
.locsym_shndx
= NULL
;
8239 finfo
.internal_syms
= NULL
;
8240 finfo
.indices
= NULL
;
8241 finfo
.sections
= NULL
;
8242 finfo
.symbuf
= NULL
;
8243 finfo
.symshndxbuf
= NULL
;
8244 finfo
.symbuf_count
= 0;
8245 finfo
.shndxbuf_size
= 0;
8247 /* Count up the number of relocations we will output for each output
8248 section, so that we know the sizes of the reloc sections. We
8249 also figure out some maximum sizes. */
8250 max_contents_size
= 0;
8251 max_external_reloc_size
= 0;
8252 max_internal_reloc_count
= 0;
8254 max_sym_shndx_count
= 0;
8256 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8258 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
8261 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8263 unsigned int reloc_count
= 0;
8264 struct bfd_elf_section_data
*esdi
= NULL
;
8265 unsigned int *rel_count1
;
8267 if (p
->type
== bfd_section_reloc_link_order
8268 || p
->type
== bfd_symbol_reloc_link_order
)
8270 else if (p
->type
== bfd_indirect_link_order
)
8274 sec
= p
->u
.indirect
.section
;
8275 esdi
= elf_section_data (sec
);
8277 /* Mark all sections which are to be included in the
8278 link. This will normally be every section. We need
8279 to do this so that we can identify any sections which
8280 the linker has decided to not include. */
8281 sec
->linker_mark
= TRUE
;
8283 if (sec
->flags
& SEC_MERGE
)
8286 if (info
->relocatable
|| info
->emitrelocations
)
8287 reloc_count
= sec
->reloc_count
;
8288 else if (bed
->elf_backend_count_relocs
)
8290 Elf_Internal_Rela
* relocs
;
8292 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
8295 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
8297 if (elf_section_data (o
)->relocs
!= relocs
)
8301 if (sec
->rawsize
> max_contents_size
)
8302 max_contents_size
= sec
->rawsize
;
8303 if (sec
->size
> max_contents_size
)
8304 max_contents_size
= sec
->size
;
8306 /* We are interested in just local symbols, not all
8308 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
8309 && (sec
->owner
->flags
& DYNAMIC
) == 0)
8313 if (elf_bad_symtab (sec
->owner
))
8314 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
8315 / bed
->s
->sizeof_sym
);
8317 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
8319 if (sym_count
> max_sym_count
)
8320 max_sym_count
= sym_count
;
8322 if (sym_count
> max_sym_shndx_count
8323 && elf_symtab_shndx (sec
->owner
) != 0)
8324 max_sym_shndx_count
= sym_count
;
8326 if ((sec
->flags
& SEC_RELOC
) != 0)
8330 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
8331 if (ext_size
> max_external_reloc_size
)
8332 max_external_reloc_size
= ext_size
;
8333 if (sec
->reloc_count
> max_internal_reloc_count
)
8334 max_internal_reloc_count
= sec
->reloc_count
;
8339 if (reloc_count
== 0)
8342 o
->reloc_count
+= reloc_count
;
8344 /* MIPS may have a mix of REL and RELA relocs on sections.
8345 To support this curious ABI we keep reloc counts in
8346 elf_section_data too. We must be careful to add the
8347 relocations from the input section to the right output
8348 count. FIXME: Get rid of one count. We have
8349 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
8350 rel_count1
= &esdo
->rel_count
;
8353 bfd_boolean same_size
;
8354 bfd_size_type entsize1
;
8356 entsize1
= esdi
->rel_hdr
.sh_entsize
;
8357 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
8358 || entsize1
== bed
->s
->sizeof_rela
);
8359 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
8362 rel_count1
= &esdo
->rel_count2
;
8364 if (esdi
->rel_hdr2
!= NULL
)
8366 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
8367 unsigned int alt_count
;
8368 unsigned int *rel_count2
;
8370 BFD_ASSERT (entsize2
!= entsize1
8371 && (entsize2
== bed
->s
->sizeof_rel
8372 || entsize2
== bed
->s
->sizeof_rela
));
8374 rel_count2
= &esdo
->rel_count2
;
8376 rel_count2
= &esdo
->rel_count
;
8378 /* The following is probably too simplistic if the
8379 backend counts output relocs unusually. */
8380 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
8381 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
8382 *rel_count2
+= alt_count
;
8383 reloc_count
-= alt_count
;
8386 *rel_count1
+= reloc_count
;
8389 if (o
->reloc_count
> 0)
8390 o
->flags
|= SEC_RELOC
;
8393 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8394 set it (this is probably a bug) and if it is set
8395 assign_section_numbers will create a reloc section. */
8396 o
->flags
&=~ SEC_RELOC
;
8399 /* If the SEC_ALLOC flag is not set, force the section VMA to
8400 zero. This is done in elf_fake_sections as well, but forcing
8401 the VMA to 0 here will ensure that relocs against these
8402 sections are handled correctly. */
8403 if ((o
->flags
& SEC_ALLOC
) == 0
8404 && ! o
->user_set_vma
)
8408 if (! info
->relocatable
&& merged
)
8409 elf_link_hash_traverse (elf_hash_table (info
),
8410 _bfd_elf_link_sec_merge_syms
, abfd
);
8412 /* Figure out the file positions for everything but the symbol table
8413 and the relocs. We set symcount to force assign_section_numbers
8414 to create a symbol table. */
8415 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
8416 BFD_ASSERT (! abfd
->output_has_begun
);
8417 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
8420 /* Set sizes, and assign file positions for reloc sections. */
8421 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8423 if ((o
->flags
& SEC_RELOC
) != 0)
8425 if (!(_bfd_elf_link_size_reloc_section
8426 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
8429 if (elf_section_data (o
)->rel_hdr2
8430 && !(_bfd_elf_link_size_reloc_section
8431 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
8435 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8436 to count upwards while actually outputting the relocations. */
8437 elf_section_data (o
)->rel_count
= 0;
8438 elf_section_data (o
)->rel_count2
= 0;
8441 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8443 /* We have now assigned file positions for all the sections except
8444 .symtab and .strtab. We start the .symtab section at the current
8445 file position, and write directly to it. We build the .strtab
8446 section in memory. */
8447 bfd_get_symcount (abfd
) = 0;
8448 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8449 /* sh_name is set in prep_headers. */
8450 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8451 /* sh_flags, sh_addr and sh_size all start off zero. */
8452 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8453 /* sh_link is set in assign_section_numbers. */
8454 /* sh_info is set below. */
8455 /* sh_offset is set just below. */
8456 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8458 off
= elf_tdata (abfd
)->next_file_pos
;
8459 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8461 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8462 incorrect. We do not yet know the size of the .symtab section.
8463 We correct next_file_pos below, after we do know the size. */
8465 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8466 continuously seeking to the right position in the file. */
8467 if (! info
->keep_memory
|| max_sym_count
< 20)
8468 finfo
.symbuf_size
= 20;
8470 finfo
.symbuf_size
= max_sym_count
;
8471 amt
= finfo
.symbuf_size
;
8472 amt
*= bed
->s
->sizeof_sym
;
8473 finfo
.symbuf
= bfd_malloc (amt
);
8474 if (finfo
.symbuf
== NULL
)
8476 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8478 /* Wild guess at number of output symbols. realloc'd as needed. */
8479 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8480 finfo
.shndxbuf_size
= amt
;
8481 amt
*= sizeof (Elf_External_Sym_Shndx
);
8482 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8483 if (finfo
.symshndxbuf
== NULL
)
8487 /* Start writing out the symbol table. The first symbol is always a
8489 if (info
->strip
!= strip_all
8492 elfsym
.st_value
= 0;
8495 elfsym
.st_other
= 0;
8496 elfsym
.st_shndx
= SHN_UNDEF
;
8497 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8502 /* Output a symbol for each section. We output these even if we are
8503 discarding local symbols, since they are used for relocs. These
8504 symbols have no names. We store the index of each one in the
8505 index field of the section, so that we can find it again when
8506 outputting relocs. */
8507 if (info
->strip
!= strip_all
8511 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8512 elfsym
.st_other
= 0;
8513 elfsym
.st_value
= 0;
8514 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8516 o
= bfd_section_from_elf_index (abfd
, i
);
8519 o
->target_index
= bfd_get_symcount (abfd
);
8520 elfsym
.st_shndx
= i
;
8521 if (!info
->relocatable
)
8522 elfsym
.st_value
= o
->vma
;
8523 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8526 if (i
== SHN_LORESERVE
- 1)
8527 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8531 /* Allocate some memory to hold information read in from the input
8533 if (max_contents_size
!= 0)
8535 finfo
.contents
= bfd_malloc (max_contents_size
);
8536 if (finfo
.contents
== NULL
)
8540 if (max_external_reloc_size
!= 0)
8542 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8543 if (finfo
.external_relocs
== NULL
)
8547 if (max_internal_reloc_count
!= 0)
8549 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8550 amt
*= sizeof (Elf_Internal_Rela
);
8551 finfo
.internal_relocs
= bfd_malloc (amt
);
8552 if (finfo
.internal_relocs
== NULL
)
8556 if (max_sym_count
!= 0)
8558 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8559 finfo
.external_syms
= bfd_malloc (amt
);
8560 if (finfo
.external_syms
== NULL
)
8563 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8564 finfo
.internal_syms
= bfd_malloc (amt
);
8565 if (finfo
.internal_syms
== NULL
)
8568 amt
= max_sym_count
* sizeof (long);
8569 finfo
.indices
= bfd_malloc (amt
);
8570 if (finfo
.indices
== NULL
)
8573 amt
= max_sym_count
* sizeof (asection
*);
8574 finfo
.sections
= bfd_malloc (amt
);
8575 if (finfo
.sections
== NULL
)
8579 if (max_sym_shndx_count
!= 0)
8581 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8582 finfo
.locsym_shndx
= bfd_malloc (amt
);
8583 if (finfo
.locsym_shndx
== NULL
)
8587 if (elf_hash_table (info
)->tls_sec
)
8589 bfd_vma base
, end
= 0;
8592 for (sec
= elf_hash_table (info
)->tls_sec
;
8593 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8596 bfd_size_type size
= sec
->size
;
8599 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8601 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8603 size
= o
->offset
+ o
->size
;
8605 end
= sec
->vma
+ size
;
8607 base
= elf_hash_table (info
)->tls_sec
->vma
;
8608 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8609 elf_hash_table (info
)->tls_size
= end
- base
;
8612 /* Reorder SHF_LINK_ORDER sections. */
8613 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8615 if (!elf_fixup_link_order (abfd
, o
))
8619 /* Since ELF permits relocations to be against local symbols, we
8620 must have the local symbols available when we do the relocations.
8621 Since we would rather only read the local symbols once, and we
8622 would rather not keep them in memory, we handle all the
8623 relocations for a single input file at the same time.
8625 Unfortunately, there is no way to know the total number of local
8626 symbols until we have seen all of them, and the local symbol
8627 indices precede the global symbol indices. This means that when
8628 we are generating relocatable output, and we see a reloc against
8629 a global symbol, we can not know the symbol index until we have
8630 finished examining all the local symbols to see which ones we are
8631 going to output. To deal with this, we keep the relocations in
8632 memory, and don't output them until the end of the link. This is
8633 an unfortunate waste of memory, but I don't see a good way around
8634 it. Fortunately, it only happens when performing a relocatable
8635 link, which is not the common case. FIXME: If keep_memory is set
8636 we could write the relocs out and then read them again; I don't
8637 know how bad the memory loss will be. */
8639 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8640 sub
->output_has_begun
= FALSE
;
8641 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8643 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8645 if (p
->type
== bfd_indirect_link_order
8646 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8647 == bfd_target_elf_flavour
)
8648 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8650 if (! sub
->output_has_begun
)
8652 if (! elf_link_input_bfd (&finfo
, sub
))
8654 sub
->output_has_begun
= TRUE
;
8657 else if (p
->type
== bfd_section_reloc_link_order
8658 || p
->type
== bfd_symbol_reloc_link_order
)
8660 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8665 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8671 /* Output any global symbols that got converted to local in a
8672 version script or due to symbol visibility. We do this in a
8673 separate step since ELF requires all local symbols to appear
8674 prior to any global symbols. FIXME: We should only do this if
8675 some global symbols were, in fact, converted to become local.
8676 FIXME: Will this work correctly with the Irix 5 linker? */
8677 eoinfo
.failed
= FALSE
;
8678 eoinfo
.finfo
= &finfo
;
8679 eoinfo
.localsyms
= TRUE
;
8680 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8685 /* If backend needs to output some local symbols not present in the hash
8686 table, do it now. */
8687 if (bed
->elf_backend_output_arch_local_syms
)
8689 typedef bfd_boolean (*out_sym_func
)
8690 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8691 struct elf_link_hash_entry
*);
8693 if (! ((*bed
->elf_backend_output_arch_local_syms
)
8694 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8698 /* That wrote out all the local symbols. Finish up the symbol table
8699 with the global symbols. Even if we want to strip everything we
8700 can, we still need to deal with those global symbols that got
8701 converted to local in a version script. */
8703 /* The sh_info field records the index of the first non local symbol. */
8704 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8707 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8709 Elf_Internal_Sym sym
;
8710 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8711 long last_local
= 0;
8713 /* Write out the section symbols for the output sections. */
8714 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8720 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8723 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8729 dynindx
= elf_section_data (s
)->dynindx
;
8732 indx
= elf_section_data (s
)->this_idx
;
8733 BFD_ASSERT (indx
> 0);
8734 sym
.st_shndx
= indx
;
8735 if (! check_dynsym (abfd
, &sym
))
8737 sym
.st_value
= s
->vma
;
8738 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8739 if (last_local
< dynindx
)
8740 last_local
= dynindx
;
8741 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8745 /* Write out the local dynsyms. */
8746 if (elf_hash_table (info
)->dynlocal
)
8748 struct elf_link_local_dynamic_entry
*e
;
8749 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8754 sym
.st_size
= e
->isym
.st_size
;
8755 sym
.st_other
= e
->isym
.st_other
;
8757 /* Copy the internal symbol as is.
8758 Note that we saved a word of storage and overwrote
8759 the original st_name with the dynstr_index. */
8762 if (e
->isym
.st_shndx
!= SHN_UNDEF
8763 && (e
->isym
.st_shndx
< SHN_LORESERVE
8764 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8766 s
= bfd_section_from_elf_index (e
->input_bfd
,
8770 elf_section_data (s
->output_section
)->this_idx
;
8771 if (! check_dynsym (abfd
, &sym
))
8773 sym
.st_value
= (s
->output_section
->vma
8775 + e
->isym
.st_value
);
8778 if (last_local
< e
->dynindx
)
8779 last_local
= e
->dynindx
;
8781 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8782 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8786 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8790 /* We get the global symbols from the hash table. */
8791 eoinfo
.failed
= FALSE
;
8792 eoinfo
.localsyms
= FALSE
;
8793 eoinfo
.finfo
= &finfo
;
8794 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8799 /* If backend needs to output some symbols not present in the hash
8800 table, do it now. */
8801 if (bed
->elf_backend_output_arch_syms
)
8803 typedef bfd_boolean (*out_sym_func
)
8804 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8805 struct elf_link_hash_entry
*);
8807 if (! ((*bed
->elf_backend_output_arch_syms
)
8808 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8812 /* Flush all symbols to the file. */
8813 if (! elf_link_flush_output_syms (&finfo
, bed
))
8816 /* Now we know the size of the symtab section. */
8817 off
+= symtab_hdr
->sh_size
;
8819 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8820 if (symtab_shndx_hdr
->sh_name
!= 0)
8822 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8823 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8824 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8825 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8826 symtab_shndx_hdr
->sh_size
= amt
;
8828 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8831 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8832 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8837 /* Finish up and write out the symbol string table (.strtab)
8839 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8840 /* sh_name was set in prep_headers. */
8841 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8842 symstrtab_hdr
->sh_flags
= 0;
8843 symstrtab_hdr
->sh_addr
= 0;
8844 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8845 symstrtab_hdr
->sh_entsize
= 0;
8846 symstrtab_hdr
->sh_link
= 0;
8847 symstrtab_hdr
->sh_info
= 0;
8848 /* sh_offset is set just below. */
8849 symstrtab_hdr
->sh_addralign
= 1;
8851 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8852 elf_tdata (abfd
)->next_file_pos
= off
;
8854 if (bfd_get_symcount (abfd
) > 0)
8856 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8857 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8861 /* Adjust the relocs to have the correct symbol indices. */
8862 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8864 if ((o
->flags
& SEC_RELOC
) == 0)
8867 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8868 elf_section_data (o
)->rel_count
,
8869 elf_section_data (o
)->rel_hashes
);
8870 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8871 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8872 elf_section_data (o
)->rel_count2
,
8873 (elf_section_data (o
)->rel_hashes
8874 + elf_section_data (o
)->rel_count
));
8876 /* Set the reloc_count field to 0 to prevent write_relocs from
8877 trying to swap the relocs out itself. */
8881 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8882 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8884 /* If we are linking against a dynamic object, or generating a
8885 shared library, finish up the dynamic linking information. */
8888 bfd_byte
*dyncon
, *dynconend
;
8890 /* Fix up .dynamic entries. */
8891 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8892 BFD_ASSERT (o
!= NULL
);
8894 dyncon
= o
->contents
;
8895 dynconend
= o
->contents
+ o
->size
;
8896 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8898 Elf_Internal_Dyn dyn
;
8902 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8909 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8911 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8913 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8914 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8917 dyn
.d_un
.d_val
= relativecount
;
8924 name
= info
->init_function
;
8927 name
= info
->fini_function
;
8930 struct elf_link_hash_entry
*h
;
8932 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8933 FALSE
, FALSE
, TRUE
);
8935 && (h
->root
.type
== bfd_link_hash_defined
8936 || h
->root
.type
== bfd_link_hash_defweak
))
8938 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8939 o
= h
->root
.u
.def
.section
;
8940 if (o
->output_section
!= NULL
)
8941 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8942 + o
->output_offset
);
8945 /* The symbol is imported from another shared
8946 library and does not apply to this one. */
8954 case DT_PREINIT_ARRAYSZ
:
8955 name
= ".preinit_array";
8957 case DT_INIT_ARRAYSZ
:
8958 name
= ".init_array";
8960 case DT_FINI_ARRAYSZ
:
8961 name
= ".fini_array";
8963 o
= bfd_get_section_by_name (abfd
, name
);
8966 (*_bfd_error_handler
)
8967 (_("%B: could not find output section %s"), abfd
, name
);
8971 (*_bfd_error_handler
)
8972 (_("warning: %s section has zero size"), name
);
8973 dyn
.d_un
.d_val
= o
->size
;
8976 case DT_PREINIT_ARRAY
:
8977 name
= ".preinit_array";
8980 name
= ".init_array";
8983 name
= ".fini_array";
8999 name
= ".gnu.version_d";
9002 name
= ".gnu.version_r";
9005 name
= ".gnu.version";
9007 o
= bfd_get_section_by_name (abfd
, name
);
9010 (*_bfd_error_handler
)
9011 (_("%B: could not find output section %s"), abfd
, name
);
9014 dyn
.d_un
.d_ptr
= o
->vma
;
9021 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9026 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9028 Elf_Internal_Shdr
*hdr
;
9030 hdr
= elf_elfsections (abfd
)[i
];
9031 if (hdr
->sh_type
== type
9032 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9034 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9035 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9038 if (dyn
.d_un
.d_val
== 0
9039 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9040 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9046 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9050 /* If we have created any dynamic sections, then output them. */
9053 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9056 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9057 if (info
->warn_shared_textrel
&& info
->shared
)
9059 bfd_byte
*dyncon
, *dynconend
;
9061 /* Fix up .dynamic entries. */
9062 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9063 BFD_ASSERT (o
!= NULL
);
9065 dyncon
= o
->contents
;
9066 dynconend
= o
->contents
+ o
->size
;
9067 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9069 Elf_Internal_Dyn dyn
;
9071 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9073 if (dyn
.d_tag
== DT_TEXTREL
)
9076 (_("warning: creating a DT_TEXTREL in a shared object."));
9082 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9084 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9086 || o
->output_section
== bfd_abs_section_ptr
)
9088 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9090 /* At this point, we are only interested in sections
9091 created by _bfd_elf_link_create_dynamic_sections. */
9094 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9096 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9098 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9100 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9102 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9104 (file_ptr
) o
->output_offset
,
9110 /* The contents of the .dynstr section are actually in a
9112 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9113 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9114 || ! _bfd_elf_strtab_emit (abfd
,
9115 elf_hash_table (info
)->dynstr
))
9121 if (info
->relocatable
)
9123 bfd_boolean failed
= FALSE
;
9125 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
9130 /* If we have optimized stabs strings, output them. */
9131 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
9133 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
9137 if (info
->eh_frame_hdr
)
9139 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
9143 if (finfo
.symstrtab
!= NULL
)
9144 _bfd_stringtab_free (finfo
.symstrtab
);
9145 if (finfo
.contents
!= NULL
)
9146 free (finfo
.contents
);
9147 if (finfo
.external_relocs
!= NULL
)
9148 free (finfo
.external_relocs
);
9149 if (finfo
.internal_relocs
!= NULL
)
9150 free (finfo
.internal_relocs
);
9151 if (finfo
.external_syms
!= NULL
)
9152 free (finfo
.external_syms
);
9153 if (finfo
.locsym_shndx
!= NULL
)
9154 free (finfo
.locsym_shndx
);
9155 if (finfo
.internal_syms
!= NULL
)
9156 free (finfo
.internal_syms
);
9157 if (finfo
.indices
!= NULL
)
9158 free (finfo
.indices
);
9159 if (finfo
.sections
!= NULL
)
9160 free (finfo
.sections
);
9161 if (finfo
.symbuf
!= NULL
)
9162 free (finfo
.symbuf
);
9163 if (finfo
.symshndxbuf
!= NULL
)
9164 free (finfo
.symshndxbuf
);
9165 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9167 if ((o
->flags
& SEC_RELOC
) != 0
9168 && elf_section_data (o
)->rel_hashes
!= NULL
)
9169 free (elf_section_data (o
)->rel_hashes
);
9172 elf_tdata (abfd
)->linker
= TRUE
;
9177 if (finfo
.symstrtab
!= NULL
)
9178 _bfd_stringtab_free (finfo
.symstrtab
);
9179 if (finfo
.contents
!= NULL
)
9180 free (finfo
.contents
);
9181 if (finfo
.external_relocs
!= NULL
)
9182 free (finfo
.external_relocs
);
9183 if (finfo
.internal_relocs
!= NULL
)
9184 free (finfo
.internal_relocs
);
9185 if (finfo
.external_syms
!= NULL
)
9186 free (finfo
.external_syms
);
9187 if (finfo
.locsym_shndx
!= NULL
)
9188 free (finfo
.locsym_shndx
);
9189 if (finfo
.internal_syms
!= NULL
)
9190 free (finfo
.internal_syms
);
9191 if (finfo
.indices
!= NULL
)
9192 free (finfo
.indices
);
9193 if (finfo
.sections
!= NULL
)
9194 free (finfo
.sections
);
9195 if (finfo
.symbuf
!= NULL
)
9196 free (finfo
.symbuf
);
9197 if (finfo
.symshndxbuf
!= NULL
)
9198 free (finfo
.symshndxbuf
);
9199 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9201 if ((o
->flags
& SEC_RELOC
) != 0
9202 && elf_section_data (o
)->rel_hashes
!= NULL
)
9203 free (elf_section_data (o
)->rel_hashes
);
9209 /* Garbage collect unused sections. */
9211 /* The mark phase of garbage collection. For a given section, mark
9212 it and any sections in this section's group, and all the sections
9213 which define symbols to which it refers. */
9215 typedef asection
* (*gc_mark_hook_fn
)
9216 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9217 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
9220 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
9222 gc_mark_hook_fn gc_mark_hook
)
9226 asection
*group_sec
;
9230 /* Mark all the sections in the group. */
9231 group_sec
= elf_section_data (sec
)->next_in_group
;
9232 if (group_sec
&& !group_sec
->gc_mark
)
9233 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
9236 /* Look through the section relocs. */
9238 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
9239 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
9241 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
9242 Elf_Internal_Shdr
*symtab_hdr
;
9243 struct elf_link_hash_entry
**sym_hashes
;
9246 bfd
*input_bfd
= sec
->owner
;
9247 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
9248 Elf_Internal_Sym
*isym
= NULL
;
9251 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9252 sym_hashes
= elf_sym_hashes (input_bfd
);
9254 /* Read the local symbols. */
9255 if (elf_bad_symtab (input_bfd
))
9257 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9261 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
9263 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9264 if (isym
== NULL
&& nlocsyms
!= 0)
9266 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
9272 /* Read the relocations. */
9273 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
9275 if (relstart
== NULL
)
9280 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9282 if (bed
->s
->arch_size
== 32)
9287 for (rel
= relstart
; rel
< relend
; rel
++)
9289 unsigned long r_symndx
;
9291 struct elf_link_hash_entry
*h
;
9293 r_symndx
= rel
->r_info
>> r_sym_shift
;
9297 if (r_symndx
>= nlocsyms
9298 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
9300 h
= sym_hashes
[r_symndx
- extsymoff
];
9301 while (h
->root
.type
== bfd_link_hash_indirect
9302 || h
->root
.type
== bfd_link_hash_warning
)
9303 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9304 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
9308 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
9311 if (rsec
&& !rsec
->gc_mark
)
9313 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
9316 rsec
->gc_mark_from_eh
= 1;
9317 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
9326 if (elf_section_data (sec
)->relocs
!= relstart
)
9329 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
9331 if (! info
->keep_memory
)
9334 symtab_hdr
->contents
= (unsigned char *) isym
;
9341 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
9343 struct elf_gc_sweep_symbol_info
{
9344 struct bfd_link_info
*info
;
9345 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
9350 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
9352 if (h
->root
.type
== bfd_link_hash_warning
)
9353 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9355 if ((h
->root
.type
== bfd_link_hash_defined
9356 || h
->root
.type
== bfd_link_hash_defweak
)
9357 && !h
->root
.u
.def
.section
->gc_mark
9358 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
9360 struct elf_gc_sweep_symbol_info
*inf
= data
;
9361 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
9367 /* The sweep phase of garbage collection. Remove all garbage sections. */
9369 typedef bfd_boolean (*gc_sweep_hook_fn
)
9370 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
9373 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
9376 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9377 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
9378 unsigned long section_sym_count
;
9379 struct elf_gc_sweep_symbol_info sweep_info
;
9381 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9385 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9388 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9390 /* Keep debug and special sections. */
9391 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
9392 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
9398 /* Skip sweeping sections already excluded. */
9399 if (o
->flags
& SEC_EXCLUDE
)
9402 /* Since this is early in the link process, it is simple
9403 to remove a section from the output. */
9404 o
->flags
|= SEC_EXCLUDE
;
9406 /* But we also have to update some of the relocation
9407 info we collected before. */
9409 && (o
->flags
& SEC_RELOC
) != 0
9410 && o
->reloc_count
> 0
9411 && !bfd_is_abs_section (o
->output_section
))
9413 Elf_Internal_Rela
*internal_relocs
;
9417 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
9419 if (internal_relocs
== NULL
)
9422 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
9424 if (elf_section_data (o
)->relocs
!= internal_relocs
)
9425 free (internal_relocs
);
9433 /* Remove the symbols that were in the swept sections from the dynamic
9434 symbol table. GCFIXME: Anyone know how to get them out of the
9435 static symbol table as well? */
9436 sweep_info
.info
= info
;
9437 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
9438 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
9441 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
9445 /* Propagate collected vtable information. This is called through
9446 elf_link_hash_traverse. */
9449 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
9451 if (h
->root
.type
== bfd_link_hash_warning
)
9452 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9454 /* Those that are not vtables. */
9455 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9458 /* Those vtables that do not have parents, we cannot merge. */
9459 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
9462 /* If we've already been done, exit. */
9463 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
9466 /* Make sure the parent's table is up to date. */
9467 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
9469 if (h
->vtable
->used
== NULL
)
9471 /* None of this table's entries were referenced. Re-use the
9473 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
9474 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
9479 bfd_boolean
*cu
, *pu
;
9481 /* Or the parent's entries into ours. */
9482 cu
= h
->vtable
->used
;
9484 pu
= h
->vtable
->parent
->vtable
->used
;
9487 const struct elf_backend_data
*bed
;
9488 unsigned int log_file_align
;
9490 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9491 log_file_align
= bed
->s
->log_file_align
;
9492 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9507 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9510 bfd_vma hstart
, hend
;
9511 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9512 const struct elf_backend_data
*bed
;
9513 unsigned int log_file_align
;
9515 if (h
->root
.type
== bfd_link_hash_warning
)
9516 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9518 /* Take care of both those symbols that do not describe vtables as
9519 well as those that are not loaded. */
9520 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9523 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9524 || h
->root
.type
== bfd_link_hash_defweak
);
9526 sec
= h
->root
.u
.def
.section
;
9527 hstart
= h
->root
.u
.def
.value
;
9528 hend
= hstart
+ h
->size
;
9530 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9532 return *(bfd_boolean
*) okp
= FALSE
;
9533 bed
= get_elf_backend_data (sec
->owner
);
9534 log_file_align
= bed
->s
->log_file_align
;
9536 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9538 for (rel
= relstart
; rel
< relend
; ++rel
)
9539 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9541 /* If the entry is in use, do nothing. */
9543 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9545 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9546 if (h
->vtable
->used
[entry
])
9549 /* Otherwise, kill it. */
9550 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9556 /* Mark sections containing dynamically referenced symbols. When
9557 building shared libraries, we must assume that any visible symbol is
9561 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9563 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9565 if (h
->root
.type
== bfd_link_hash_warning
)
9566 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9568 if ((h
->root
.type
== bfd_link_hash_defined
9569 || h
->root
.type
== bfd_link_hash_defweak
)
9571 || (!info
->executable
9573 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9574 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9575 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9580 /* Do mark and sweep of unused sections. */
9583 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9585 bfd_boolean ok
= TRUE
;
9587 asection
* (*gc_mark_hook
)
9588 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9589 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9590 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9592 if (!bed
->can_gc_sections
9593 || info
->relocatable
9594 || info
->emitrelocations
9595 || !is_elf_hash_table (info
->hash
))
9597 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9601 /* Apply transitive closure to the vtable entry usage info. */
9602 elf_link_hash_traverse (elf_hash_table (info
),
9603 elf_gc_propagate_vtable_entries_used
,
9608 /* Kill the vtable relocations that were not used. */
9609 elf_link_hash_traverse (elf_hash_table (info
),
9610 elf_gc_smash_unused_vtentry_relocs
,
9615 /* Mark dynamically referenced symbols. */
9616 if (elf_hash_table (info
)->dynamic_sections_created
)
9617 elf_link_hash_traverse (elf_hash_table (info
),
9618 bed
->gc_mark_dynamic_ref
,
9621 /* Grovel through relocs to find out who stays ... */
9622 gc_mark_hook
= bed
->gc_mark_hook
;
9623 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9627 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9630 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9631 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9632 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9636 /* ... again for sections marked from eh_frame. */
9637 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9641 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9644 /* Keep .gcc_except_table.* if the associated .text.* is
9645 marked. This isn't very nice, but the proper solution,
9646 splitting .eh_frame up and using comdat doesn't pan out
9647 easily due to needing special relocs to handle the
9648 difference of two symbols in separate sections.
9649 Don't keep code sections referenced by .eh_frame. */
9650 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9651 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9653 if (strncmp (o
->name
, ".gcc_except_table.", 18) == 0)
9659 len
= strlen (o
->name
+ 18) + 1;
9660 fn_name
= bfd_malloc (len
+ 6);
9661 if (fn_name
== NULL
)
9663 memcpy (fn_name
, ".text.", 6);
9664 memcpy (fn_name
+ 6, o
->name
+ 18, len
);
9665 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9667 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9671 /* If not using specially named exception table section,
9672 then keep whatever we are using. */
9673 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9678 /* ... and mark SEC_EXCLUDE for those that go. */
9679 return elf_gc_sweep (abfd
, info
);
9682 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9685 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9687 struct elf_link_hash_entry
*h
,
9690 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9691 struct elf_link_hash_entry
**search
, *child
;
9692 bfd_size_type extsymcount
;
9693 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9695 /* The sh_info field of the symtab header tells us where the
9696 external symbols start. We don't care about the local symbols at
9698 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9699 if (!elf_bad_symtab (abfd
))
9700 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9702 sym_hashes
= elf_sym_hashes (abfd
);
9703 sym_hashes_end
= sym_hashes
+ extsymcount
;
9705 /* Hunt down the child symbol, which is in this section at the same
9706 offset as the relocation. */
9707 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9709 if ((child
= *search
) != NULL
9710 && (child
->root
.type
== bfd_link_hash_defined
9711 || child
->root
.type
== bfd_link_hash_defweak
)
9712 && child
->root
.u
.def
.section
== sec
9713 && child
->root
.u
.def
.value
== offset
)
9717 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9718 abfd
, sec
, (unsigned long) offset
);
9719 bfd_set_error (bfd_error_invalid_operation
);
9725 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9731 /* This *should* only be the absolute section. It could potentially
9732 be that someone has defined a non-global vtable though, which
9733 would be bad. It isn't worth paging in the local symbols to be
9734 sure though; that case should simply be handled by the assembler. */
9736 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9739 child
->vtable
->parent
= h
;
9744 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9747 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9748 asection
*sec ATTRIBUTE_UNUSED
,
9749 struct elf_link_hash_entry
*h
,
9752 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9753 unsigned int log_file_align
= bed
->s
->log_file_align
;
9757 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9762 if (addend
>= h
->vtable
->size
)
9764 size_t size
, bytes
, file_align
;
9765 bfd_boolean
*ptr
= h
->vtable
->used
;
9767 /* While the symbol is undefined, we have to be prepared to handle
9769 file_align
= 1 << log_file_align
;
9770 if (h
->root
.type
== bfd_link_hash_undefined
)
9771 size
= addend
+ file_align
;
9777 /* Oops! We've got a reference past the defined end of
9778 the table. This is probably a bug -- shall we warn? */
9779 size
= addend
+ file_align
;
9782 size
= (size
+ file_align
- 1) & -file_align
;
9784 /* Allocate one extra entry for use as a "done" flag for the
9785 consolidation pass. */
9786 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9790 ptr
= bfd_realloc (ptr
- 1, bytes
);
9796 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9797 * sizeof (bfd_boolean
));
9798 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9802 ptr
= bfd_zmalloc (bytes
);
9807 /* And arrange for that done flag to be at index -1. */
9808 h
->vtable
->used
= ptr
+ 1;
9809 h
->vtable
->size
= size
;
9812 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9817 struct alloc_got_off_arg
{
9819 unsigned int got_elt_size
;
9822 /* We need a special top-level link routine to convert got reference counts
9823 to real got offsets. */
9826 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9828 struct alloc_got_off_arg
*gofarg
= arg
;
9830 if (h
->root
.type
== bfd_link_hash_warning
)
9831 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9833 if (h
->got
.refcount
> 0)
9835 h
->got
.offset
= gofarg
->gotoff
;
9836 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9839 h
->got
.offset
= (bfd_vma
) -1;
9844 /* And an accompanying bit to work out final got entry offsets once
9845 we're done. Should be called from final_link. */
9848 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9849 struct bfd_link_info
*info
)
9852 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9854 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9855 struct alloc_got_off_arg gofarg
;
9857 if (! is_elf_hash_table (info
->hash
))
9860 /* The GOT offset is relative to the .got section, but the GOT header is
9861 put into the .got.plt section, if the backend uses it. */
9862 if (bed
->want_got_plt
)
9865 gotoff
= bed
->got_header_size
;
9867 /* Do the local .got entries first. */
9868 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9870 bfd_signed_vma
*local_got
;
9871 bfd_size_type j
, locsymcount
;
9872 Elf_Internal_Shdr
*symtab_hdr
;
9874 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9877 local_got
= elf_local_got_refcounts (i
);
9881 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9882 if (elf_bad_symtab (i
))
9883 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9885 locsymcount
= symtab_hdr
->sh_info
;
9887 for (j
= 0; j
< locsymcount
; ++j
)
9889 if (local_got
[j
] > 0)
9891 local_got
[j
] = gotoff
;
9892 gotoff
+= got_elt_size
;
9895 local_got
[j
] = (bfd_vma
) -1;
9899 /* Then the global .got entries. .plt refcounts are handled by
9900 adjust_dynamic_symbol */
9901 gofarg
.gotoff
= gotoff
;
9902 gofarg
.got_elt_size
= got_elt_size
;
9903 elf_link_hash_traverse (elf_hash_table (info
),
9904 elf_gc_allocate_got_offsets
,
9909 /* Many folk need no more in the way of final link than this, once
9910 got entry reference counting is enabled. */
9913 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9915 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9918 /* Invoke the regular ELF backend linker to do all the work. */
9919 return bfd_elf_final_link (abfd
, info
);
9923 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9925 struct elf_reloc_cookie
*rcookie
= cookie
;
9927 if (rcookie
->bad_symtab
)
9928 rcookie
->rel
= rcookie
->rels
;
9930 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9932 unsigned long r_symndx
;
9934 if (! rcookie
->bad_symtab
)
9935 if (rcookie
->rel
->r_offset
> offset
)
9937 if (rcookie
->rel
->r_offset
!= offset
)
9940 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9941 if (r_symndx
== SHN_UNDEF
)
9944 if (r_symndx
>= rcookie
->locsymcount
9945 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9947 struct elf_link_hash_entry
*h
;
9949 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9951 while (h
->root
.type
== bfd_link_hash_indirect
9952 || h
->root
.type
== bfd_link_hash_warning
)
9953 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9955 if ((h
->root
.type
== bfd_link_hash_defined
9956 || h
->root
.type
== bfd_link_hash_defweak
)
9957 && elf_discarded_section (h
->root
.u
.def
.section
))
9964 /* It's not a relocation against a global symbol,
9965 but it could be a relocation against a local
9966 symbol for a discarded section. */
9968 Elf_Internal_Sym
*isym
;
9970 /* Need to: get the symbol; get the section. */
9971 isym
= &rcookie
->locsyms
[r_symndx
];
9972 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9974 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9975 if (isec
!= NULL
&& elf_discarded_section (isec
))
9984 /* Discard unneeded references to discarded sections.
9985 Returns TRUE if any section's size was changed. */
9986 /* This function assumes that the relocations are in sorted order,
9987 which is true for all known assemblers. */
9990 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9992 struct elf_reloc_cookie cookie
;
9993 asection
*stab
, *eh
;
9994 Elf_Internal_Shdr
*symtab_hdr
;
9995 const struct elf_backend_data
*bed
;
9998 bfd_boolean ret
= FALSE
;
10000 if (info
->traditional_format
10001 || !is_elf_hash_table (info
->hash
))
10004 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10006 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10009 bed
= get_elf_backend_data (abfd
);
10011 if ((abfd
->flags
& DYNAMIC
) != 0)
10014 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10015 if (info
->relocatable
10018 || bfd_is_abs_section (eh
->output_section
))))
10021 stab
= bfd_get_section_by_name (abfd
, ".stab");
10023 && (stab
->size
== 0
10024 || bfd_is_abs_section (stab
->output_section
)
10025 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10030 && bed
->elf_backend_discard_info
== NULL
)
10033 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10034 cookie
.abfd
= abfd
;
10035 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10036 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10037 if (cookie
.bad_symtab
)
10039 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10040 cookie
.extsymoff
= 0;
10044 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10045 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10048 if (bed
->s
->arch_size
== 32)
10049 cookie
.r_sym_shift
= 8;
10051 cookie
.r_sym_shift
= 32;
10053 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10054 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10056 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10057 cookie
.locsymcount
, 0,
10059 if (cookie
.locsyms
== NULL
)
10065 cookie
.rels
= NULL
;
10066 count
= stab
->reloc_count
;
10068 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
10069 info
->keep_memory
);
10070 if (cookie
.rels
!= NULL
)
10072 cookie
.rel
= cookie
.rels
;
10073 cookie
.relend
= cookie
.rels
;
10074 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10075 if (_bfd_discard_section_stabs (abfd
, stab
,
10076 elf_section_data (stab
)->sec_info
,
10077 bfd_elf_reloc_symbol_deleted_p
,
10080 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
10081 free (cookie
.rels
);
10087 cookie
.rels
= NULL
;
10088 count
= eh
->reloc_count
;
10090 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
10091 info
->keep_memory
);
10092 cookie
.rel
= cookie
.rels
;
10093 cookie
.relend
= cookie
.rels
;
10094 if (cookie
.rels
!= NULL
)
10095 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10097 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
10098 bfd_elf_reloc_symbol_deleted_p
,
10102 if (cookie
.rels
!= NULL
10103 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
10104 free (cookie
.rels
);
10107 if (bed
->elf_backend_discard_info
!= NULL
10108 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
10111 if (cookie
.locsyms
!= NULL
10112 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
10114 if (! info
->keep_memory
)
10115 free (cookie
.locsyms
);
10117 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
10121 if (info
->eh_frame_hdr
10122 && !info
->relocatable
10123 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
10130 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
10133 const char *name
, *p
;
10134 struct bfd_section_already_linked
*l
;
10135 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
10138 /* A single member comdat group section may be discarded by a
10139 linkonce section. See below. */
10140 if (sec
->output_section
== bfd_abs_section_ptr
)
10143 flags
= sec
->flags
;
10145 /* Check if it belongs to a section group. */
10146 group
= elf_sec_group (sec
);
10148 /* Return if it isn't a linkonce section nor a member of a group. A
10149 comdat group section also has SEC_LINK_ONCE set. */
10150 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
10155 /* If this is the member of a single member comdat group, check if
10156 the group should be discarded. */
10157 if (elf_next_in_group (sec
) == sec
10158 && (group
->flags
& SEC_LINK_ONCE
) != 0)
10164 /* FIXME: When doing a relocatable link, we may have trouble
10165 copying relocations in other sections that refer to local symbols
10166 in the section being discarded. Those relocations will have to
10167 be converted somehow; as of this writing I'm not sure that any of
10168 the backends handle that correctly.
10170 It is tempting to instead not discard link once sections when
10171 doing a relocatable link (technically, they should be discarded
10172 whenever we are building constructors). However, that fails,
10173 because the linker winds up combining all the link once sections
10174 into a single large link once section, which defeats the purpose
10175 of having link once sections in the first place.
10177 Also, not merging link once sections in a relocatable link
10178 causes trouble for MIPS ELF, which relies on link once semantics
10179 to handle the .reginfo section correctly. */
10181 name
= bfd_get_section_name (abfd
, sec
);
10183 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
10184 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
10189 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
10191 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10193 /* We may have 3 different sections on the list: group section,
10194 comdat section and linkonce section. SEC may be a linkonce or
10195 group section. We match a group section with a group section,
10196 a linkonce section with a linkonce section, and ignore comdat
10198 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
10199 && strcmp (name
, l
->sec
->name
) == 0
10200 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
10202 /* The section has already been linked. See if we should
10203 issue a warning. */
10204 switch (flags
& SEC_LINK_DUPLICATES
)
10209 case SEC_LINK_DUPLICATES_DISCARD
:
10212 case SEC_LINK_DUPLICATES_ONE_ONLY
:
10213 (*_bfd_error_handler
)
10214 (_("%B: ignoring duplicate section `%A'"),
10218 case SEC_LINK_DUPLICATES_SAME_SIZE
:
10219 if (sec
->size
!= l
->sec
->size
)
10220 (*_bfd_error_handler
)
10221 (_("%B: duplicate section `%A' has different size"),
10225 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
10226 if (sec
->size
!= l
->sec
->size
)
10227 (*_bfd_error_handler
)
10228 (_("%B: duplicate section `%A' has different size"),
10230 else if (sec
->size
!= 0)
10232 bfd_byte
*sec_contents
, *l_sec_contents
;
10234 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
10235 (*_bfd_error_handler
)
10236 (_("%B: warning: could not read contents of section `%A'"),
10238 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
10240 (*_bfd_error_handler
)
10241 (_("%B: warning: could not read contents of section `%A'"),
10242 l
->sec
->owner
, l
->sec
);
10243 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
10244 (*_bfd_error_handler
)
10245 (_("%B: warning: duplicate section `%A' has different contents"),
10249 free (sec_contents
);
10250 if (l_sec_contents
)
10251 free (l_sec_contents
);
10256 /* Set the output_section field so that lang_add_section
10257 does not create a lang_input_section structure for this
10258 section. Since there might be a symbol in the section
10259 being discarded, we must retain a pointer to the section
10260 which we are really going to use. */
10261 sec
->output_section
= bfd_abs_section_ptr
;
10262 sec
->kept_section
= l
->sec
;
10264 if (flags
& SEC_GROUP
)
10266 asection
*first
= elf_next_in_group (sec
);
10267 asection
*s
= first
;
10271 s
->output_section
= bfd_abs_section_ptr
;
10272 /* Record which group discards it. */
10273 s
->kept_section
= l
->sec
;
10274 s
= elf_next_in_group (s
);
10275 /* These lists are circular. */
10287 /* If this is the member of a single member comdat group and the
10288 group hasn't be discarded, we check if it matches a linkonce
10289 section. We only record the discarded comdat group. Otherwise
10290 the undiscarded group will be discarded incorrectly later since
10291 itself has been recorded. */
10292 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10293 if ((l
->sec
->flags
& SEC_GROUP
) == 0
10294 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
10295 && bfd_elf_match_symbols_in_sections (l
->sec
,
10296 elf_next_in_group (sec
)))
10298 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
10299 elf_next_in_group (sec
)->kept_section
= l
->sec
;
10300 group
->output_section
= bfd_abs_section_ptr
;
10307 /* There is no direct match. But for linkonce section, we should
10308 check if there is a match with comdat group member. We always
10309 record the linkonce section, discarded or not. */
10310 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10311 if (l
->sec
->flags
& SEC_GROUP
)
10313 asection
*first
= elf_next_in_group (l
->sec
);
10316 && elf_next_in_group (first
) == first
10317 && bfd_elf_match_symbols_in_sections (first
, sec
))
10319 sec
->output_section
= bfd_abs_section_ptr
;
10320 sec
->kept_section
= l
->sec
;
10325 /* This is the first section with this name. Record it. */
10326 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
10330 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
10332 return sym
->st_shndx
== SHN_COMMON
;
10336 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
10342 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
10344 return bfd_com_section_ptr
;