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 /* Mark a symbol dynamic. */
450 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
451 struct elf_link_hash_entry
*h
)
453 struct bfd_elf_dynamic_list
*d
= info
->dynamic
;
455 if (d
== NULL
|| info
->relocatable
)
458 if ((*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
))
462 /* Record an assignment to a symbol made by a linker script. We need
463 this in case some dynamic object refers to this symbol. */
466 bfd_elf_record_link_assignment (bfd
*output_bfd
,
467 struct bfd_link_info
*info
,
472 struct elf_link_hash_entry
*h
;
473 struct elf_link_hash_table
*htab
;
475 if (!is_elf_hash_table (info
->hash
))
478 htab
= elf_hash_table (info
);
479 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
483 /* Since we're defining the symbol, don't let it seem to have not
484 been defined. record_dynamic_symbol and size_dynamic_sections
485 may depend on this. */
486 if (h
->root
.type
== bfd_link_hash_undefweak
487 || h
->root
.type
== bfd_link_hash_undefined
)
489 h
->root
.type
= bfd_link_hash_new
;
490 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
491 bfd_link_repair_undef_list (&htab
->root
);
494 if (h
->root
.type
== bfd_link_hash_new
)
496 bfd_elf_link_mark_dynamic_symbol (info
, h
);
500 /* If this symbol is being provided by the linker script, and it is
501 currently defined by a dynamic object, but not by a regular
502 object, then mark it as undefined so that the generic linker will
503 force the correct value. */
507 h
->root
.type
= bfd_link_hash_undefined
;
509 /* If this symbol is not being provided by the linker script, and it is
510 currently defined by a dynamic object, but not by a regular object,
511 then clear out any version information because the symbol will not be
512 associated with the dynamic object any more. */
516 h
->verinfo
.verdef
= NULL
;
520 if (provide
&& hidden
)
522 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
524 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
525 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
528 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
530 if (!info
->relocatable
532 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
533 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
539 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
542 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
545 /* If this is a weak defined symbol, and we know a corresponding
546 real symbol from the same dynamic object, make sure the real
547 symbol is also made into a dynamic symbol. */
548 if (h
->u
.weakdef
!= NULL
549 && h
->u
.weakdef
->dynindx
== -1)
551 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
559 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
560 success, and 2 on a failure caused by attempting to record a symbol
561 in a discarded section, eg. a discarded link-once section symbol. */
564 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
569 struct elf_link_local_dynamic_entry
*entry
;
570 struct elf_link_hash_table
*eht
;
571 struct elf_strtab_hash
*dynstr
;
572 unsigned long dynstr_index
;
574 Elf_External_Sym_Shndx eshndx
;
575 char esym
[sizeof (Elf64_External_Sym
)];
577 if (! is_elf_hash_table (info
->hash
))
580 /* See if the entry exists already. */
581 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
582 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
585 amt
= sizeof (*entry
);
586 entry
= bfd_alloc (input_bfd
, amt
);
590 /* Go find the symbol, so that we can find it's name. */
591 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
592 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
594 bfd_release (input_bfd
, entry
);
598 if (entry
->isym
.st_shndx
!= SHN_UNDEF
599 && (entry
->isym
.st_shndx
< SHN_LORESERVE
600 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
604 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
605 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
607 /* We can still bfd_release here as nothing has done another
608 bfd_alloc. We can't do this later in this function. */
609 bfd_release (input_bfd
, entry
);
614 name
= (bfd_elf_string_from_elf_section
615 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
616 entry
->isym
.st_name
));
618 dynstr
= elf_hash_table (info
)->dynstr
;
621 /* Create a strtab to hold the dynamic symbol names. */
622 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
627 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
628 if (dynstr_index
== (unsigned long) -1)
630 entry
->isym
.st_name
= dynstr_index
;
632 eht
= elf_hash_table (info
);
634 entry
->next
= eht
->dynlocal
;
635 eht
->dynlocal
= entry
;
636 entry
->input_bfd
= input_bfd
;
637 entry
->input_indx
= input_indx
;
640 /* Whatever binding the symbol had before, it's now local. */
642 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
644 /* The dynindx will be set at the end of size_dynamic_sections. */
649 /* Return the dynindex of a local dynamic symbol. */
652 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
656 struct elf_link_local_dynamic_entry
*e
;
658 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
659 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
664 /* This function is used to renumber the dynamic symbols, if some of
665 them are removed because they are marked as local. This is called
666 via elf_link_hash_traverse. */
669 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
672 size_t *count
= data
;
674 if (h
->root
.type
== bfd_link_hash_warning
)
675 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
680 if (h
->dynindx
!= -1)
681 h
->dynindx
= ++(*count
);
687 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
688 STB_LOCAL binding. */
691 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
694 size_t *count
= data
;
696 if (h
->root
.type
== bfd_link_hash_warning
)
697 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
699 if (!h
->forced_local
)
702 if (h
->dynindx
!= -1)
703 h
->dynindx
= ++(*count
);
708 /* Return true if the dynamic symbol for a given section should be
709 omitted when creating a shared library. */
711 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
712 struct bfd_link_info
*info
,
715 switch (elf_section_data (p
)->this_hdr
.sh_type
)
719 /* If sh_type is yet undecided, assume it could be
720 SHT_PROGBITS/SHT_NOBITS. */
722 if (strcmp (p
->name
, ".got") == 0
723 || strcmp (p
->name
, ".got.plt") == 0
724 || strcmp (p
->name
, ".plt") == 0)
727 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
730 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
731 && (ip
->flags
& SEC_LINKER_CREATED
)
732 && ip
->output_section
== p
)
737 /* There shouldn't be section relative relocations
738 against any other section. */
744 /* Assign dynsym indices. In a shared library we generate a section
745 symbol for each output section, which come first. Next come symbols
746 which have been forced to local binding. Then all of the back-end
747 allocated local dynamic syms, followed by the rest of the global
751 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
752 struct bfd_link_info
*info
,
753 unsigned long *section_sym_count
)
755 unsigned long dynsymcount
= 0;
757 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
759 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
761 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
762 if ((p
->flags
& SEC_EXCLUDE
) == 0
763 && (p
->flags
& SEC_ALLOC
) != 0
764 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
765 elf_section_data (p
)->dynindx
= ++dynsymcount
;
767 *section_sym_count
= dynsymcount
;
769 elf_link_hash_traverse (elf_hash_table (info
),
770 elf_link_renumber_local_hash_table_dynsyms
,
773 if (elf_hash_table (info
)->dynlocal
)
775 struct elf_link_local_dynamic_entry
*p
;
776 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
777 p
->dynindx
= ++dynsymcount
;
780 elf_link_hash_traverse (elf_hash_table (info
),
781 elf_link_renumber_hash_table_dynsyms
,
784 /* There is an unused NULL entry at the head of the table which
785 we must account for in our count. Unless there weren't any
786 symbols, which means we'll have no table at all. */
787 if (dynsymcount
!= 0)
790 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
794 /* This function is called when we want to define a new symbol. It
795 handles the various cases which arise when we find a definition in
796 a dynamic object, or when there is already a definition in a
797 dynamic object. The new symbol is described by NAME, SYM, PSEC,
798 and PVALUE. We set SYM_HASH to the hash table entry. We set
799 OVERRIDE if the old symbol is overriding a new definition. We set
800 TYPE_CHANGE_OK if it is OK for the type to change. We set
801 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
802 change, we mean that we shouldn't warn if the type or size does
803 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
804 object is overridden by a regular object. */
807 _bfd_elf_merge_symbol (bfd
*abfd
,
808 struct bfd_link_info
*info
,
810 Elf_Internal_Sym
*sym
,
813 unsigned int *pold_alignment
,
814 struct elf_link_hash_entry
**sym_hash
,
816 bfd_boolean
*override
,
817 bfd_boolean
*type_change_ok
,
818 bfd_boolean
*size_change_ok
)
820 asection
*sec
, *oldsec
;
821 struct elf_link_hash_entry
*h
;
822 struct elf_link_hash_entry
*flip
;
825 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
826 bfd_boolean newweak
, oldweak
;
827 const struct elf_backend_data
*bed
;
833 bind
= ELF_ST_BIND (sym
->st_info
);
835 /* Silently discard TLS symbols from --just-syms. There's no way to
836 combine a static TLS block with a new TLS block for this executable. */
837 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
838 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
844 if (! bfd_is_und_section (sec
))
845 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
847 h
= ((struct elf_link_hash_entry
*)
848 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
853 /* This code is for coping with dynamic objects, and is only useful
854 if we are doing an ELF link. */
855 if (info
->hash
->creator
!= abfd
->xvec
)
858 /* For merging, we only care about real symbols. */
860 while (h
->root
.type
== bfd_link_hash_indirect
861 || h
->root
.type
== bfd_link_hash_warning
)
862 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
864 /* If we just created the symbol, mark it as being an ELF symbol.
865 Other than that, there is nothing to do--there is no merge issue
866 with a newly defined symbol--so we just return. */
868 if (h
->root
.type
== bfd_link_hash_new
)
870 bfd_elf_link_mark_dynamic_symbol (info
, h
);
875 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
878 switch (h
->root
.type
)
885 case bfd_link_hash_undefined
:
886 case bfd_link_hash_undefweak
:
887 oldbfd
= h
->root
.u
.undef
.abfd
;
891 case bfd_link_hash_defined
:
892 case bfd_link_hash_defweak
:
893 oldbfd
= h
->root
.u
.def
.section
->owner
;
894 oldsec
= h
->root
.u
.def
.section
;
897 case bfd_link_hash_common
:
898 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
899 oldsec
= h
->root
.u
.c
.p
->section
;
903 /* In cases involving weak versioned symbols, we may wind up trying
904 to merge a symbol with itself. Catch that here, to avoid the
905 confusion that results if we try to override a symbol with
906 itself. The additional tests catch cases like
907 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
908 dynamic object, which we do want to handle here. */
910 && ((abfd
->flags
& DYNAMIC
) == 0
914 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
915 respectively, is from a dynamic object. */
917 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
921 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
922 else if (oldsec
!= NULL
)
924 /* This handles the special SHN_MIPS_{TEXT,DATA} section
925 indices used by MIPS ELF. */
926 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
929 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
930 respectively, appear to be a definition rather than reference. */
932 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
934 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
935 && h
->root
.type
!= bfd_link_hash_undefweak
936 && h
->root
.type
!= bfd_link_hash_common
);
938 /* When we try to create a default indirect symbol from the dynamic
939 definition with the default version, we skip it if its type and
940 the type of existing regular definition mismatch. We only do it
941 if the existing regular definition won't be dynamic. */
942 if (pold_alignment
== NULL
944 && !info
->export_dynamic
949 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
950 && ELF_ST_TYPE (sym
->st_info
) != h
->type
951 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
952 && h
->type
!= STT_NOTYPE
)
958 /* Check TLS symbol. We don't check undefined symbol introduced by
960 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
961 && ELF_ST_TYPE (sym
->st_info
) != h
->type
965 bfd_boolean ntdef
, tdef
;
966 asection
*ntsec
, *tsec
;
968 if (h
->type
== STT_TLS
)
988 (*_bfd_error_handler
)
989 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
990 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
991 else if (!tdef
&& !ntdef
)
992 (*_bfd_error_handler
)
993 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
994 tbfd
, ntbfd
, h
->root
.root
.string
);
996 (*_bfd_error_handler
)
997 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
998 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1000 (*_bfd_error_handler
)
1001 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1002 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1004 bfd_set_error (bfd_error_bad_value
);
1008 /* We need to remember if a symbol has a definition in a dynamic
1009 object or is weak in all dynamic objects. Internal and hidden
1010 visibility will make it unavailable to dynamic objects. */
1011 if (newdyn
&& !h
->dynamic_def
)
1013 if (!bfd_is_und_section (sec
))
1017 /* Check if this symbol is weak in all dynamic objects. If it
1018 is the first time we see it in a dynamic object, we mark
1019 if it is weak. Otherwise, we clear it. */
1020 if (!h
->ref_dynamic
)
1022 if (bind
== STB_WEAK
)
1023 h
->dynamic_weak
= 1;
1025 else if (bind
!= STB_WEAK
)
1026 h
->dynamic_weak
= 0;
1030 /* If the old symbol has non-default visibility, we ignore the new
1031 definition from a dynamic object. */
1033 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1034 && !bfd_is_und_section (sec
))
1037 /* Make sure this symbol is dynamic. */
1039 /* A protected symbol has external availability. Make sure it is
1040 recorded as dynamic.
1042 FIXME: Should we check type and size for protected symbol? */
1043 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1044 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1049 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1052 /* If the new symbol with non-default visibility comes from a
1053 relocatable file and the old definition comes from a dynamic
1054 object, we remove the old definition. */
1055 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1057 /* Handle the case where the old dynamic definition is
1058 default versioned. We need to copy the symbol info from
1059 the symbol with default version to the normal one if it
1060 was referenced before. */
1063 const struct elf_backend_data
*bed
1064 = get_elf_backend_data (abfd
);
1065 struct elf_link_hash_entry
*vh
= *sym_hash
;
1066 vh
->root
.type
= h
->root
.type
;
1067 h
->root
.type
= bfd_link_hash_indirect
;
1068 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1069 /* Protected symbols will override the dynamic definition
1070 with default version. */
1071 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1073 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1074 vh
->dynamic_def
= 1;
1075 vh
->ref_dynamic
= 1;
1079 h
->root
.type
= vh
->root
.type
;
1080 vh
->ref_dynamic
= 0;
1081 /* We have to hide it here since it was made dynamic
1082 global with extra bits when the symbol info was
1083 copied from the old dynamic definition. */
1084 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1092 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1093 && bfd_is_und_section (sec
))
1095 /* If the new symbol is undefined and the old symbol was
1096 also undefined before, we need to make sure
1097 _bfd_generic_link_add_one_symbol doesn't mess
1098 up the linker hash table undefs list. Since the old
1099 definition came from a dynamic object, it is still on the
1101 h
->root
.type
= bfd_link_hash_undefined
;
1102 h
->root
.u
.undef
.abfd
= abfd
;
1106 h
->root
.type
= bfd_link_hash_new
;
1107 h
->root
.u
.undef
.abfd
= NULL
;
1116 /* FIXME: Should we check type and size for protected symbol? */
1122 /* Differentiate strong and weak symbols. */
1123 newweak
= bind
== STB_WEAK
;
1124 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1125 || h
->root
.type
== bfd_link_hash_undefweak
);
1127 /* If a new weak symbol definition comes from a regular file and the
1128 old symbol comes from a dynamic library, we treat the new one as
1129 strong. Similarly, an old weak symbol definition from a regular
1130 file is treated as strong when the new symbol comes from a dynamic
1131 library. Further, an old weak symbol from a dynamic library is
1132 treated as strong if the new symbol is from a dynamic library.
1133 This reflects the way glibc's ld.so works.
1135 Do this before setting *type_change_ok or *size_change_ok so that
1136 we warn properly when dynamic library symbols are overridden. */
1138 if (newdef
&& !newdyn
&& olddyn
)
1140 if (olddef
&& newdyn
)
1143 /* It's OK to change the type if either the existing symbol or the
1144 new symbol is weak. A type change is also OK if the old symbol
1145 is undefined and the new symbol is defined. */
1150 && h
->root
.type
== bfd_link_hash_undefined
))
1151 *type_change_ok
= TRUE
;
1153 /* It's OK to change the size if either the existing symbol or the
1154 new symbol is weak, or if the old symbol is undefined. */
1157 || h
->root
.type
== bfd_link_hash_undefined
)
1158 *size_change_ok
= TRUE
;
1160 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1161 symbol, respectively, appears to be a common symbol in a dynamic
1162 object. If a symbol appears in an uninitialized section, and is
1163 not weak, and is not a function, then it may be a common symbol
1164 which was resolved when the dynamic object was created. We want
1165 to treat such symbols specially, because they raise special
1166 considerations when setting the symbol size: if the symbol
1167 appears as a common symbol in a regular object, and the size in
1168 the regular object is larger, we must make sure that we use the
1169 larger size. This problematic case can always be avoided in C,
1170 but it must be handled correctly when using Fortran shared
1173 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1174 likewise for OLDDYNCOMMON and OLDDEF.
1176 Note that this test is just a heuristic, and that it is quite
1177 possible to have an uninitialized symbol in a shared object which
1178 is really a definition, rather than a common symbol. This could
1179 lead to some minor confusion when the symbol really is a common
1180 symbol in some regular object. However, I think it will be
1186 && (sec
->flags
& SEC_ALLOC
) != 0
1187 && (sec
->flags
& SEC_LOAD
) == 0
1189 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1190 newdyncommon
= TRUE
;
1192 newdyncommon
= FALSE
;
1196 && h
->root
.type
== bfd_link_hash_defined
1198 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1199 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1201 && h
->type
!= STT_FUNC
)
1202 olddyncommon
= TRUE
;
1204 olddyncommon
= FALSE
;
1206 /* We now know everything about the old and new symbols. We ask the
1207 backend to check if we can merge them. */
1208 bed
= get_elf_backend_data (abfd
);
1209 if (bed
->merge_symbol
1210 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1211 pold_alignment
, skip
, override
,
1212 type_change_ok
, size_change_ok
,
1213 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1215 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1219 /* If both the old and the new symbols look like common symbols in a
1220 dynamic object, set the size of the symbol to the larger of the
1225 && sym
->st_size
!= h
->size
)
1227 /* Since we think we have two common symbols, issue a multiple
1228 common warning if desired. Note that we only warn if the
1229 size is different. If the size is the same, we simply let
1230 the old symbol override the new one as normally happens with
1231 symbols defined in dynamic objects. */
1233 if (! ((*info
->callbacks
->multiple_common
)
1234 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1235 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1238 if (sym
->st_size
> h
->size
)
1239 h
->size
= sym
->st_size
;
1241 *size_change_ok
= TRUE
;
1244 /* If we are looking at a dynamic object, and we have found a
1245 definition, we need to see if the symbol was already defined by
1246 some other object. If so, we want to use the existing
1247 definition, and we do not want to report a multiple symbol
1248 definition error; we do this by clobbering *PSEC to be
1249 bfd_und_section_ptr.
1251 We treat a common symbol as a definition if the symbol in the
1252 shared library is a function, since common symbols always
1253 represent variables; this can cause confusion in principle, but
1254 any such confusion would seem to indicate an erroneous program or
1255 shared library. We also permit a common symbol in a regular
1256 object to override a weak symbol in a shared object. */
1261 || (h
->root
.type
== bfd_link_hash_common
1263 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1267 newdyncommon
= FALSE
;
1269 *psec
= sec
= bfd_und_section_ptr
;
1270 *size_change_ok
= TRUE
;
1272 /* If we get here when the old symbol is a common symbol, then
1273 we are explicitly letting it override a weak symbol or
1274 function in a dynamic object, and we don't want to warn about
1275 a type change. If the old symbol is a defined symbol, a type
1276 change warning may still be appropriate. */
1278 if (h
->root
.type
== bfd_link_hash_common
)
1279 *type_change_ok
= TRUE
;
1282 /* Handle the special case of an old common symbol merging with a
1283 new symbol which looks like a common symbol in a shared object.
1284 We change *PSEC and *PVALUE to make the new symbol look like a
1285 common symbol, and let _bfd_generic_link_add_one_symbol do the
1289 && h
->root
.type
== bfd_link_hash_common
)
1293 newdyncommon
= FALSE
;
1294 *pvalue
= sym
->st_size
;
1295 *psec
= sec
= bed
->common_section (oldsec
);
1296 *size_change_ok
= TRUE
;
1299 /* Skip weak definitions of symbols that are already defined. */
1300 if (newdef
&& olddef
&& newweak
)
1303 /* If the old symbol is from a dynamic object, and the new symbol is
1304 a definition which is not from a dynamic object, then the new
1305 symbol overrides the old symbol. Symbols from regular files
1306 always take precedence over symbols from dynamic objects, even if
1307 they are defined after the dynamic object in the link.
1309 As above, we again permit a common symbol in a regular object to
1310 override a definition in a shared object if the shared object
1311 symbol is a function or is weak. */
1316 || (bfd_is_com_section (sec
)
1318 || h
->type
== STT_FUNC
)))
1323 /* Change the hash table entry to undefined, and let
1324 _bfd_generic_link_add_one_symbol do the right thing with the
1327 h
->root
.type
= bfd_link_hash_undefined
;
1328 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1329 *size_change_ok
= TRUE
;
1332 olddyncommon
= FALSE
;
1334 /* We again permit a type change when a common symbol may be
1335 overriding a function. */
1337 if (bfd_is_com_section (sec
))
1338 *type_change_ok
= TRUE
;
1340 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1343 /* This union may have been set to be non-NULL when this symbol
1344 was seen in a dynamic object. We must force the union to be
1345 NULL, so that it is correct for a regular symbol. */
1346 h
->verinfo
.vertree
= NULL
;
1349 /* Handle the special case of a new common symbol merging with an
1350 old symbol that looks like it might be a common symbol defined in
1351 a shared object. Note that we have already handled the case in
1352 which a new common symbol should simply override the definition
1353 in the shared library. */
1356 && bfd_is_com_section (sec
)
1359 /* It would be best if we could set the hash table entry to a
1360 common symbol, but we don't know what to use for the section
1361 or the alignment. */
1362 if (! ((*info
->callbacks
->multiple_common
)
1363 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1364 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1367 /* If the presumed common symbol in the dynamic object is
1368 larger, pretend that the new symbol has its size. */
1370 if (h
->size
> *pvalue
)
1373 /* We need to remember the alignment required by the symbol
1374 in the dynamic object. */
1375 BFD_ASSERT (pold_alignment
);
1376 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1379 olddyncommon
= FALSE
;
1381 h
->root
.type
= bfd_link_hash_undefined
;
1382 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1384 *size_change_ok
= TRUE
;
1385 *type_change_ok
= TRUE
;
1387 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1390 h
->verinfo
.vertree
= NULL
;
1395 /* Handle the case where we had a versioned symbol in a dynamic
1396 library and now find a definition in a normal object. In this
1397 case, we make the versioned symbol point to the normal one. */
1398 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1399 flip
->root
.type
= h
->root
.type
;
1400 h
->root
.type
= bfd_link_hash_indirect
;
1401 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1402 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1403 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1407 flip
->ref_dynamic
= 1;
1414 /* This function is called to create an indirect symbol from the
1415 default for the symbol with the default version if needed. The
1416 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1417 set DYNSYM if the new indirect symbol is dynamic. */
1420 _bfd_elf_add_default_symbol (bfd
*abfd
,
1421 struct bfd_link_info
*info
,
1422 struct elf_link_hash_entry
*h
,
1424 Elf_Internal_Sym
*sym
,
1427 bfd_boolean
*dynsym
,
1428 bfd_boolean override
)
1430 bfd_boolean type_change_ok
;
1431 bfd_boolean size_change_ok
;
1434 struct elf_link_hash_entry
*hi
;
1435 struct bfd_link_hash_entry
*bh
;
1436 const struct elf_backend_data
*bed
;
1437 bfd_boolean collect
;
1438 bfd_boolean dynamic
;
1440 size_t len
, shortlen
;
1443 /* If this symbol has a version, and it is the default version, we
1444 create an indirect symbol from the default name to the fully
1445 decorated name. This will cause external references which do not
1446 specify a version to be bound to this version of the symbol. */
1447 p
= strchr (name
, ELF_VER_CHR
);
1448 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1453 /* We are overridden by an old definition. We need to check if we
1454 need to create the indirect symbol from the default name. */
1455 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1457 BFD_ASSERT (hi
!= NULL
);
1460 while (hi
->root
.type
== bfd_link_hash_indirect
1461 || hi
->root
.type
== bfd_link_hash_warning
)
1463 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1469 bed
= get_elf_backend_data (abfd
);
1470 collect
= bed
->collect
;
1471 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1473 shortlen
= p
- name
;
1474 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1475 if (shortname
== NULL
)
1477 memcpy (shortname
, name
, shortlen
);
1478 shortname
[shortlen
] = '\0';
1480 /* We are going to create a new symbol. Merge it with any existing
1481 symbol with this name. For the purposes of the merge, act as
1482 though we were defining the symbol we just defined, although we
1483 actually going to define an indirect symbol. */
1484 type_change_ok
= FALSE
;
1485 size_change_ok
= FALSE
;
1487 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1488 NULL
, &hi
, &skip
, &override
,
1489 &type_change_ok
, &size_change_ok
))
1498 if (! (_bfd_generic_link_add_one_symbol
1499 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1500 0, name
, FALSE
, collect
, &bh
)))
1502 hi
= (struct elf_link_hash_entry
*) bh
;
1506 /* In this case the symbol named SHORTNAME is overriding the
1507 indirect symbol we want to add. We were planning on making
1508 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1509 is the name without a version. NAME is the fully versioned
1510 name, and it is the default version.
1512 Overriding means that we already saw a definition for the
1513 symbol SHORTNAME in a regular object, and it is overriding
1514 the symbol defined in the dynamic object.
1516 When this happens, we actually want to change NAME, the
1517 symbol we just added, to refer to SHORTNAME. This will cause
1518 references to NAME in the shared object to become references
1519 to SHORTNAME in the regular object. This is what we expect
1520 when we override a function in a shared object: that the
1521 references in the shared object will be mapped to the
1522 definition in the regular object. */
1524 while (hi
->root
.type
== bfd_link_hash_indirect
1525 || hi
->root
.type
== bfd_link_hash_warning
)
1526 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1528 h
->root
.type
= bfd_link_hash_indirect
;
1529 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1533 hi
->ref_dynamic
= 1;
1537 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1542 /* Now set HI to H, so that the following code will set the
1543 other fields correctly. */
1547 /* If there is a duplicate definition somewhere, then HI may not
1548 point to an indirect symbol. We will have reported an error to
1549 the user in that case. */
1551 if (hi
->root
.type
== bfd_link_hash_indirect
)
1553 struct elf_link_hash_entry
*ht
;
1555 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1556 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1558 /* See if the new flags lead us to realize that the symbol must
1570 if (hi
->ref_regular
)
1576 /* We also need to define an indirection from the nondefault version
1580 len
= strlen (name
);
1581 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1582 if (shortname
== NULL
)
1584 memcpy (shortname
, name
, shortlen
);
1585 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1587 /* Once again, merge with any existing symbol. */
1588 type_change_ok
= FALSE
;
1589 size_change_ok
= FALSE
;
1591 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1592 NULL
, &hi
, &skip
, &override
,
1593 &type_change_ok
, &size_change_ok
))
1601 /* Here SHORTNAME is a versioned name, so we don't expect to see
1602 the type of override we do in the case above unless it is
1603 overridden by a versioned definition. */
1604 if (hi
->root
.type
!= bfd_link_hash_defined
1605 && hi
->root
.type
!= bfd_link_hash_defweak
)
1606 (*_bfd_error_handler
)
1607 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1613 if (! (_bfd_generic_link_add_one_symbol
1614 (info
, abfd
, shortname
, BSF_INDIRECT
,
1615 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1617 hi
= (struct elf_link_hash_entry
*) bh
;
1619 /* If there is a duplicate definition somewhere, then HI may not
1620 point to an indirect symbol. We will have reported an error
1621 to the user in that case. */
1623 if (hi
->root
.type
== bfd_link_hash_indirect
)
1625 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1627 /* See if the new flags lead us to realize that the symbol
1639 if (hi
->ref_regular
)
1649 /* This routine is used to export all defined symbols into the dynamic
1650 symbol table. It is called via elf_link_hash_traverse. */
1653 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1655 struct elf_info_failed
*eif
= data
;
1657 /* Ignore this if we won't export it. */
1658 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1661 /* Ignore indirect symbols. These are added by the versioning code. */
1662 if (h
->root
.type
== bfd_link_hash_indirect
)
1665 if (h
->root
.type
== bfd_link_hash_warning
)
1666 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1668 if (h
->dynindx
== -1
1672 struct bfd_elf_version_tree
*t
;
1673 struct bfd_elf_version_expr
*d
;
1675 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1677 if (t
->globals
.list
!= NULL
)
1679 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1684 if (t
->locals
.list
!= NULL
)
1686 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1695 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1706 /* Look through the symbols which are defined in other shared
1707 libraries and referenced here. Update the list of version
1708 dependencies. This will be put into the .gnu.version_r section.
1709 This function is called via elf_link_hash_traverse. */
1712 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1715 struct elf_find_verdep_info
*rinfo
= data
;
1716 Elf_Internal_Verneed
*t
;
1717 Elf_Internal_Vernaux
*a
;
1720 if (h
->root
.type
== bfd_link_hash_warning
)
1721 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1723 /* We only care about symbols defined in shared objects with version
1728 || h
->verinfo
.verdef
== NULL
)
1731 /* See if we already know about this version. */
1732 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1734 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1737 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1738 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1744 /* This is a new version. Add it to tree we are building. */
1749 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1752 rinfo
->failed
= TRUE
;
1756 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1757 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1758 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1762 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1764 /* Note that we are copying a string pointer here, and testing it
1765 above. If bfd_elf_string_from_elf_section is ever changed to
1766 discard the string data when low in memory, this will have to be
1768 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1770 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1771 a
->vna_nextptr
= t
->vn_auxptr
;
1773 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1776 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1783 /* Figure out appropriate versions for all the symbols. We may not
1784 have the version number script until we have read all of the input
1785 files, so until that point we don't know which symbols should be
1786 local. This function is called via elf_link_hash_traverse. */
1789 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1791 struct elf_assign_sym_version_info
*sinfo
;
1792 struct bfd_link_info
*info
;
1793 const struct elf_backend_data
*bed
;
1794 struct elf_info_failed eif
;
1801 if (h
->root
.type
== bfd_link_hash_warning
)
1802 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1804 /* Fix the symbol flags. */
1807 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1810 sinfo
->failed
= TRUE
;
1814 /* We only need version numbers for symbols defined in regular
1816 if (!h
->def_regular
)
1819 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1820 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1821 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1823 struct bfd_elf_version_tree
*t
;
1828 /* There are two consecutive ELF_VER_CHR characters if this is
1829 not a hidden symbol. */
1831 if (*p
== ELF_VER_CHR
)
1837 /* If there is no version string, we can just return out. */
1845 /* Look for the version. If we find it, it is no longer weak. */
1846 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1848 if (strcmp (t
->name
, p
) == 0)
1852 struct bfd_elf_version_expr
*d
;
1854 len
= p
- h
->root
.root
.string
;
1855 alc
= bfd_malloc (len
);
1858 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1859 alc
[len
- 1] = '\0';
1860 if (alc
[len
- 2] == ELF_VER_CHR
)
1861 alc
[len
- 2] = '\0';
1863 h
->verinfo
.vertree
= t
;
1867 if (t
->globals
.list
!= NULL
)
1868 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1870 /* See if there is anything to force this symbol to
1872 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1874 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1877 && ! info
->export_dynamic
)
1878 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1886 /* If we are building an application, we need to create a
1887 version node for this version. */
1888 if (t
== NULL
&& info
->executable
)
1890 struct bfd_elf_version_tree
**pp
;
1893 /* If we aren't going to export this symbol, we don't need
1894 to worry about it. */
1895 if (h
->dynindx
== -1)
1899 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1902 sinfo
->failed
= TRUE
;
1907 t
->name_indx
= (unsigned int) -1;
1911 /* Don't count anonymous version tag. */
1912 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1914 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1916 t
->vernum
= version_index
;
1920 h
->verinfo
.vertree
= t
;
1924 /* We could not find the version for a symbol when
1925 generating a shared archive. Return an error. */
1926 (*_bfd_error_handler
)
1927 (_("%B: undefined versioned symbol name %s"),
1928 sinfo
->output_bfd
, h
->root
.root
.string
);
1929 bfd_set_error (bfd_error_bad_value
);
1930 sinfo
->failed
= TRUE
;
1938 /* If we don't have a version for this symbol, see if we can find
1940 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1942 struct bfd_elf_version_tree
*t
;
1943 struct bfd_elf_version_tree
*local_ver
;
1944 struct bfd_elf_version_expr
*d
;
1946 /* See if can find what version this symbol is in. If the
1947 symbol is supposed to be local, then don't actually register
1950 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1952 if (t
->globals
.list
!= NULL
)
1954 bfd_boolean matched
;
1958 while ((d
= (*t
->match
) (&t
->globals
, d
,
1959 h
->root
.root
.string
)) != NULL
)
1964 /* There is a version without definition. Make
1965 the symbol the default definition for this
1967 h
->verinfo
.vertree
= t
;
1975 /* There is no undefined version for this symbol. Hide the
1977 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1980 if (t
->locals
.list
!= NULL
)
1983 while ((d
= (*t
->match
) (&t
->locals
, d
,
1984 h
->root
.root
.string
)) != NULL
)
1987 /* If the match is "*", keep looking for a more
1988 explicit, perhaps even global, match.
1989 XXX: Shouldn't this be !d->wildcard instead? */
1990 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1999 if (local_ver
!= NULL
)
2001 h
->verinfo
.vertree
= local_ver
;
2002 if (h
->dynindx
!= -1
2003 && ! info
->export_dynamic
)
2005 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2013 /* Read and swap the relocs from the section indicated by SHDR. This
2014 may be either a REL or a RELA section. The relocations are
2015 translated into RELA relocations and stored in INTERNAL_RELOCS,
2016 which should have already been allocated to contain enough space.
2017 The EXTERNAL_RELOCS are a buffer where the external form of the
2018 relocations should be stored.
2020 Returns FALSE if something goes wrong. */
2023 elf_link_read_relocs_from_section (bfd
*abfd
,
2025 Elf_Internal_Shdr
*shdr
,
2026 void *external_relocs
,
2027 Elf_Internal_Rela
*internal_relocs
)
2029 const struct elf_backend_data
*bed
;
2030 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2031 const bfd_byte
*erela
;
2032 const bfd_byte
*erelaend
;
2033 Elf_Internal_Rela
*irela
;
2034 Elf_Internal_Shdr
*symtab_hdr
;
2037 /* Position ourselves at the start of the section. */
2038 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2041 /* Read the relocations. */
2042 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2045 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2046 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2048 bed
= get_elf_backend_data (abfd
);
2050 /* Convert the external relocations to the internal format. */
2051 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2052 swap_in
= bed
->s
->swap_reloc_in
;
2053 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2054 swap_in
= bed
->s
->swap_reloca_in
;
2057 bfd_set_error (bfd_error_wrong_format
);
2061 erela
= external_relocs
;
2062 erelaend
= erela
+ shdr
->sh_size
;
2063 irela
= internal_relocs
;
2064 while (erela
< erelaend
)
2068 (*swap_in
) (abfd
, erela
, irela
);
2069 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2070 if (bed
->s
->arch_size
== 64)
2072 if ((size_t) r_symndx
>= nsyms
)
2074 (*_bfd_error_handler
)
2075 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2076 " for offset 0x%lx in section `%A'"),
2078 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2079 bfd_set_error (bfd_error_bad_value
);
2082 irela
+= bed
->s
->int_rels_per_ext_rel
;
2083 erela
+= shdr
->sh_entsize
;
2089 /* Read and swap the relocs for a section O. They may have been
2090 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2091 not NULL, they are used as buffers to read into. They are known to
2092 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2093 the return value is allocated using either malloc or bfd_alloc,
2094 according to the KEEP_MEMORY argument. If O has two relocation
2095 sections (both REL and RELA relocations), then the REL_HDR
2096 relocations will appear first in INTERNAL_RELOCS, followed by the
2097 REL_HDR2 relocations. */
2100 _bfd_elf_link_read_relocs (bfd
*abfd
,
2102 void *external_relocs
,
2103 Elf_Internal_Rela
*internal_relocs
,
2104 bfd_boolean keep_memory
)
2106 Elf_Internal_Shdr
*rel_hdr
;
2107 void *alloc1
= NULL
;
2108 Elf_Internal_Rela
*alloc2
= NULL
;
2109 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2111 if (elf_section_data (o
)->relocs
!= NULL
)
2112 return elf_section_data (o
)->relocs
;
2114 if (o
->reloc_count
== 0)
2117 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2119 if (internal_relocs
== NULL
)
2123 size
= o
->reloc_count
;
2124 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2126 internal_relocs
= bfd_alloc (abfd
, size
);
2128 internal_relocs
= alloc2
= bfd_malloc (size
);
2129 if (internal_relocs
== NULL
)
2133 if (external_relocs
== NULL
)
2135 bfd_size_type size
= rel_hdr
->sh_size
;
2137 if (elf_section_data (o
)->rel_hdr2
)
2138 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2139 alloc1
= bfd_malloc (size
);
2142 external_relocs
= alloc1
;
2145 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2149 if (elf_section_data (o
)->rel_hdr2
2150 && (!elf_link_read_relocs_from_section
2152 elf_section_data (o
)->rel_hdr2
,
2153 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2154 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2155 * bed
->s
->int_rels_per_ext_rel
))))
2158 /* Cache the results for next time, if we can. */
2160 elf_section_data (o
)->relocs
= internal_relocs
;
2165 /* Don't free alloc2, since if it was allocated we are passing it
2166 back (under the name of internal_relocs). */
2168 return internal_relocs
;
2178 /* Compute the size of, and allocate space for, REL_HDR which is the
2179 section header for a section containing relocations for O. */
2182 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2183 Elf_Internal_Shdr
*rel_hdr
,
2186 bfd_size_type reloc_count
;
2187 bfd_size_type num_rel_hashes
;
2189 /* Figure out how many relocations there will be. */
2190 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2191 reloc_count
= elf_section_data (o
)->rel_count
;
2193 reloc_count
= elf_section_data (o
)->rel_count2
;
2195 num_rel_hashes
= o
->reloc_count
;
2196 if (num_rel_hashes
< reloc_count
)
2197 num_rel_hashes
= reloc_count
;
2199 /* That allows us to calculate the size of the section. */
2200 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2202 /* The contents field must last into write_object_contents, so we
2203 allocate it with bfd_alloc rather than malloc. Also since we
2204 cannot be sure that the contents will actually be filled in,
2205 we zero the allocated space. */
2206 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2207 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2210 /* We only allocate one set of hash entries, so we only do it the
2211 first time we are called. */
2212 if (elf_section_data (o
)->rel_hashes
== NULL
2215 struct elf_link_hash_entry
**p
;
2217 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2221 elf_section_data (o
)->rel_hashes
= p
;
2227 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2228 originated from the section given by INPUT_REL_HDR) to the
2232 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2233 asection
*input_section
,
2234 Elf_Internal_Shdr
*input_rel_hdr
,
2235 Elf_Internal_Rela
*internal_relocs
,
2236 struct elf_link_hash_entry
**rel_hash
2239 Elf_Internal_Rela
*irela
;
2240 Elf_Internal_Rela
*irelaend
;
2242 Elf_Internal_Shdr
*output_rel_hdr
;
2243 asection
*output_section
;
2244 unsigned int *rel_countp
= NULL
;
2245 const struct elf_backend_data
*bed
;
2246 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2248 output_section
= input_section
->output_section
;
2249 output_rel_hdr
= NULL
;
2251 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2252 == input_rel_hdr
->sh_entsize
)
2254 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2255 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2257 else if (elf_section_data (output_section
)->rel_hdr2
2258 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2259 == input_rel_hdr
->sh_entsize
))
2261 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2262 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2266 (*_bfd_error_handler
)
2267 (_("%B: relocation size mismatch in %B section %A"),
2268 output_bfd
, input_section
->owner
, input_section
);
2269 bfd_set_error (bfd_error_wrong_object_format
);
2273 bed
= get_elf_backend_data (output_bfd
);
2274 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2275 swap_out
= bed
->s
->swap_reloc_out
;
2276 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2277 swap_out
= bed
->s
->swap_reloca_out
;
2281 erel
= output_rel_hdr
->contents
;
2282 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2283 irela
= internal_relocs
;
2284 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2285 * bed
->s
->int_rels_per_ext_rel
);
2286 while (irela
< irelaend
)
2288 (*swap_out
) (output_bfd
, irela
, erel
);
2289 irela
+= bed
->s
->int_rels_per_ext_rel
;
2290 erel
+= input_rel_hdr
->sh_entsize
;
2293 /* Bump the counter, so that we know where to add the next set of
2295 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2300 /* Make weak undefined symbols in PIE dynamic. */
2303 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2304 struct elf_link_hash_entry
*h
)
2308 && h
->root
.type
== bfd_link_hash_undefweak
)
2309 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2314 /* Fix up the flags for a symbol. This handles various cases which
2315 can only be fixed after all the input files are seen. This is
2316 currently called by both adjust_dynamic_symbol and
2317 assign_sym_version, which is unnecessary but perhaps more robust in
2318 the face of future changes. */
2321 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2322 struct elf_info_failed
*eif
)
2324 const struct elf_backend_data
*bed
= NULL
;
2326 /* If this symbol was mentioned in a non-ELF file, try to set
2327 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2328 permit a non-ELF file to correctly refer to a symbol defined in
2329 an ELF dynamic object. */
2332 while (h
->root
.type
== bfd_link_hash_indirect
)
2333 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2335 if (h
->root
.type
!= bfd_link_hash_defined
2336 && h
->root
.type
!= bfd_link_hash_defweak
)
2339 h
->ref_regular_nonweak
= 1;
2343 if (h
->root
.u
.def
.section
->owner
!= NULL
2344 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2345 == bfd_target_elf_flavour
))
2348 h
->ref_regular_nonweak
= 1;
2354 if (h
->dynindx
== -1
2358 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2367 /* Unfortunately, NON_ELF is only correct if the symbol
2368 was first seen in a non-ELF file. Fortunately, if the symbol
2369 was first seen in an ELF file, we're probably OK unless the
2370 symbol was defined in a non-ELF file. Catch that case here.
2371 FIXME: We're still in trouble if the symbol was first seen in
2372 a dynamic object, and then later in a non-ELF regular object. */
2373 if ((h
->root
.type
== bfd_link_hash_defined
2374 || h
->root
.type
== bfd_link_hash_defweak
)
2376 && (h
->root
.u
.def
.section
->owner
!= NULL
2377 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2378 != bfd_target_elf_flavour
)
2379 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2380 && !h
->def_dynamic
)))
2384 /* Backend specific symbol fixup. */
2385 if (elf_hash_table (eif
->info
)->dynobj
)
2387 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2388 if (bed
->elf_backend_fixup_symbol
2389 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2393 /* If this is a final link, and the symbol was defined as a common
2394 symbol in a regular object file, and there was no definition in
2395 any dynamic object, then the linker will have allocated space for
2396 the symbol in a common section but the DEF_REGULAR
2397 flag will not have been set. */
2398 if (h
->root
.type
== bfd_link_hash_defined
2402 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2405 /* If -Bsymbolic was used (which means to bind references to global
2406 symbols to the definition within the shared object), and this
2407 symbol was defined in a regular object, then it actually doesn't
2408 need a PLT entry. Likewise, if the symbol has non-default
2409 visibility. If the symbol has hidden or internal visibility, we
2410 will force it local. */
2412 && eif
->info
->shared
2413 && is_elf_hash_table (eif
->info
->hash
)
2414 && (SYMBOLIC_BIND (eif
->info
, h
)
2415 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2418 bfd_boolean force_local
;
2420 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2421 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2422 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2425 /* If a weak undefined symbol has non-default visibility, we also
2426 hide it from the dynamic linker. */
2427 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2428 && h
->root
.type
== bfd_link_hash_undefweak
)
2430 const struct elf_backend_data
*bed
;
2431 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2432 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2435 /* If this is a weak defined symbol in a dynamic object, and we know
2436 the real definition in the dynamic object, copy interesting flags
2437 over to the real definition. */
2438 if (h
->u
.weakdef
!= NULL
)
2440 struct elf_link_hash_entry
*weakdef
;
2442 weakdef
= h
->u
.weakdef
;
2443 if (h
->root
.type
== bfd_link_hash_indirect
)
2444 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2446 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2447 || h
->root
.type
== bfd_link_hash_defweak
);
2448 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2449 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2450 BFD_ASSERT (weakdef
->def_dynamic
);
2452 /* If the real definition is defined by a regular object file,
2453 don't do anything special. See the longer description in
2454 _bfd_elf_adjust_dynamic_symbol, below. */
2455 if (weakdef
->def_regular
)
2456 h
->u
.weakdef
= NULL
;
2458 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2465 /* Make the backend pick a good value for a dynamic symbol. This is
2466 called via elf_link_hash_traverse, and also calls itself
2470 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2472 struct elf_info_failed
*eif
= data
;
2474 const struct elf_backend_data
*bed
;
2476 if (! is_elf_hash_table (eif
->info
->hash
))
2479 if (h
->root
.type
== bfd_link_hash_warning
)
2481 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2482 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2484 /* When warning symbols are created, they **replace** the "real"
2485 entry in the hash table, thus we never get to see the real
2486 symbol in a hash traversal. So look at it now. */
2487 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2490 /* Ignore indirect symbols. These are added by the versioning code. */
2491 if (h
->root
.type
== bfd_link_hash_indirect
)
2494 /* Fix the symbol flags. */
2495 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2498 /* If this symbol does not require a PLT entry, and it is not
2499 defined by a dynamic object, or is not referenced by a regular
2500 object, ignore it. We do have to handle a weak defined symbol,
2501 even if no regular object refers to it, if we decided to add it
2502 to the dynamic symbol table. FIXME: Do we normally need to worry
2503 about symbols which are defined by one dynamic object and
2504 referenced by another one? */
2509 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2511 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2515 /* If we've already adjusted this symbol, don't do it again. This
2516 can happen via a recursive call. */
2517 if (h
->dynamic_adjusted
)
2520 /* Don't look at this symbol again. Note that we must set this
2521 after checking the above conditions, because we may look at a
2522 symbol once, decide not to do anything, and then get called
2523 recursively later after REF_REGULAR is set below. */
2524 h
->dynamic_adjusted
= 1;
2526 /* If this is a weak definition, and we know a real definition, and
2527 the real symbol is not itself defined by a regular object file,
2528 then get a good value for the real definition. We handle the
2529 real symbol first, for the convenience of the backend routine.
2531 Note that there is a confusing case here. If the real definition
2532 is defined by a regular object file, we don't get the real symbol
2533 from the dynamic object, but we do get the weak symbol. If the
2534 processor backend uses a COPY reloc, then if some routine in the
2535 dynamic object changes the real symbol, we will not see that
2536 change in the corresponding weak symbol. This is the way other
2537 ELF linkers work as well, and seems to be a result of the shared
2540 I will clarify this issue. Most SVR4 shared libraries define the
2541 variable _timezone and define timezone as a weak synonym. The
2542 tzset call changes _timezone. If you write
2543 extern int timezone;
2545 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2546 you might expect that, since timezone is a synonym for _timezone,
2547 the same number will print both times. However, if the processor
2548 backend uses a COPY reloc, then actually timezone will be copied
2549 into your process image, and, since you define _timezone
2550 yourself, _timezone will not. Thus timezone and _timezone will
2551 wind up at different memory locations. The tzset call will set
2552 _timezone, leaving timezone unchanged. */
2554 if (h
->u
.weakdef
!= NULL
)
2556 /* If we get to this point, we know there is an implicit
2557 reference by a regular object file via the weak symbol H.
2558 FIXME: Is this really true? What if the traversal finds
2559 H->U.WEAKDEF before it finds H? */
2560 h
->u
.weakdef
->ref_regular
= 1;
2562 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2566 /* If a symbol has no type and no size and does not require a PLT
2567 entry, then we are probably about to do the wrong thing here: we
2568 are probably going to create a COPY reloc for an empty object.
2569 This case can arise when a shared object is built with assembly
2570 code, and the assembly code fails to set the symbol type. */
2572 && h
->type
== STT_NOTYPE
2574 (*_bfd_error_handler
)
2575 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2576 h
->root
.root
.string
);
2578 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2579 bed
= get_elf_backend_data (dynobj
);
2580 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2589 /* Adjust all external symbols pointing into SEC_MERGE sections
2590 to reflect the object merging within the sections. */
2593 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2597 if (h
->root
.type
== bfd_link_hash_warning
)
2598 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2600 if ((h
->root
.type
== bfd_link_hash_defined
2601 || h
->root
.type
== bfd_link_hash_defweak
)
2602 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2603 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2605 bfd
*output_bfd
= data
;
2607 h
->root
.u
.def
.value
=
2608 _bfd_merged_section_offset (output_bfd
,
2609 &h
->root
.u
.def
.section
,
2610 elf_section_data (sec
)->sec_info
,
2611 h
->root
.u
.def
.value
);
2617 /* Returns false if the symbol referred to by H should be considered
2618 to resolve local to the current module, and true if it should be
2619 considered to bind dynamically. */
2622 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2623 struct bfd_link_info
*info
,
2624 bfd_boolean ignore_protected
)
2626 bfd_boolean binding_stays_local_p
;
2631 while (h
->root
.type
== bfd_link_hash_indirect
2632 || h
->root
.type
== bfd_link_hash_warning
)
2633 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2635 /* If it was forced local, then clearly it's not dynamic. */
2636 if (h
->dynindx
== -1)
2638 if (h
->forced_local
)
2641 /* Identify the cases where name binding rules say that a
2642 visible symbol resolves locally. */
2643 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2645 switch (ELF_ST_VISIBILITY (h
->other
))
2652 /* Proper resolution for function pointer equality may require
2653 that these symbols perhaps be resolved dynamically, even though
2654 we should be resolving them to the current module. */
2655 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2656 binding_stays_local_p
= TRUE
;
2663 /* If it isn't defined locally, then clearly it's dynamic. */
2664 if (!h
->def_regular
)
2667 /* Otherwise, the symbol is dynamic if binding rules don't tell
2668 us that it remains local. */
2669 return !binding_stays_local_p
;
2672 /* Return true if the symbol referred to by H should be considered
2673 to resolve local to the current module, and false otherwise. Differs
2674 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2675 undefined symbols and weak symbols. */
2678 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2679 struct bfd_link_info
*info
,
2680 bfd_boolean local_protected
)
2682 /* If it's a local sym, of course we resolve locally. */
2686 /* Common symbols that become definitions don't get the DEF_REGULAR
2687 flag set, so test it first, and don't bail out. */
2688 if (ELF_COMMON_DEF_P (h
))
2690 /* If we don't have a definition in a regular file, then we can't
2691 resolve locally. The sym is either undefined or dynamic. */
2692 else if (!h
->def_regular
)
2695 /* Forced local symbols resolve locally. */
2696 if (h
->forced_local
)
2699 /* As do non-dynamic symbols. */
2700 if (h
->dynindx
== -1)
2703 /* At this point, we know the symbol is defined and dynamic. In an
2704 executable it must resolve locally, likewise when building symbolic
2705 shared libraries. */
2706 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2709 /* Now deal with defined dynamic symbols in shared libraries. Ones
2710 with default visibility might not resolve locally. */
2711 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2714 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2715 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2718 /* STV_PROTECTED non-function symbols are local. */
2719 if (h
->type
!= STT_FUNC
)
2722 /* Function pointer equality tests may require that STV_PROTECTED
2723 symbols be treated as dynamic symbols, even when we know that the
2724 dynamic linker will resolve them locally. */
2725 return local_protected
;
2728 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2729 aligned. Returns the first TLS output section. */
2731 struct bfd_section
*
2732 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2734 struct bfd_section
*sec
, *tls
;
2735 unsigned int align
= 0;
2737 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2738 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2742 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2743 if (sec
->alignment_power
> align
)
2744 align
= sec
->alignment_power
;
2746 elf_hash_table (info
)->tls_sec
= tls
;
2748 /* Ensure the alignment of the first section is the largest alignment,
2749 so that the tls segment starts aligned. */
2751 tls
->alignment_power
= align
;
2756 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2758 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2759 Elf_Internal_Sym
*sym
)
2761 const struct elf_backend_data
*bed
;
2763 /* Local symbols do not count, but target specific ones might. */
2764 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2765 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2768 /* Function symbols do not count. */
2769 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2772 /* If the section is undefined, then so is the symbol. */
2773 if (sym
->st_shndx
== SHN_UNDEF
)
2776 /* If the symbol is defined in the common section, then
2777 it is a common definition and so does not count. */
2778 bed
= get_elf_backend_data (abfd
);
2779 if (bed
->common_definition (sym
))
2782 /* If the symbol is in a target specific section then we
2783 must rely upon the backend to tell us what it is. */
2784 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2785 /* FIXME - this function is not coded yet:
2787 return _bfd_is_global_symbol_definition (abfd, sym);
2789 Instead for now assume that the definition is not global,
2790 Even if this is wrong, at least the linker will behave
2791 in the same way that it used to do. */
2797 /* Search the symbol table of the archive element of the archive ABFD
2798 whose archive map contains a mention of SYMDEF, and determine if
2799 the symbol is defined in this element. */
2801 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2803 Elf_Internal_Shdr
* hdr
;
2804 bfd_size_type symcount
;
2805 bfd_size_type extsymcount
;
2806 bfd_size_type extsymoff
;
2807 Elf_Internal_Sym
*isymbuf
;
2808 Elf_Internal_Sym
*isym
;
2809 Elf_Internal_Sym
*isymend
;
2812 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2816 if (! bfd_check_format (abfd
, bfd_object
))
2819 /* If we have already included the element containing this symbol in the
2820 link then we do not need to include it again. Just claim that any symbol
2821 it contains is not a definition, so that our caller will not decide to
2822 (re)include this element. */
2823 if (abfd
->archive_pass
)
2826 /* Select the appropriate symbol table. */
2827 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2828 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2830 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2832 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2834 /* The sh_info field of the symtab header tells us where the
2835 external symbols start. We don't care about the local symbols. */
2836 if (elf_bad_symtab (abfd
))
2838 extsymcount
= symcount
;
2843 extsymcount
= symcount
- hdr
->sh_info
;
2844 extsymoff
= hdr
->sh_info
;
2847 if (extsymcount
== 0)
2850 /* Read in the symbol table. */
2851 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2853 if (isymbuf
== NULL
)
2856 /* Scan the symbol table looking for SYMDEF. */
2858 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2862 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2867 if (strcmp (name
, symdef
->name
) == 0)
2869 result
= is_global_data_symbol_definition (abfd
, isym
);
2879 /* Add an entry to the .dynamic table. */
2882 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2886 struct elf_link_hash_table
*hash_table
;
2887 const struct elf_backend_data
*bed
;
2889 bfd_size_type newsize
;
2890 bfd_byte
*newcontents
;
2891 Elf_Internal_Dyn dyn
;
2893 hash_table
= elf_hash_table (info
);
2894 if (! is_elf_hash_table (hash_table
))
2897 bed
= get_elf_backend_data (hash_table
->dynobj
);
2898 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2899 BFD_ASSERT (s
!= NULL
);
2901 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2902 newcontents
= bfd_realloc (s
->contents
, newsize
);
2903 if (newcontents
== NULL
)
2907 dyn
.d_un
.d_val
= val
;
2908 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2911 s
->contents
= newcontents
;
2916 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2917 otherwise just check whether one already exists. Returns -1 on error,
2918 1 if a DT_NEEDED tag already exists, and 0 on success. */
2921 elf_add_dt_needed_tag (bfd
*abfd
,
2922 struct bfd_link_info
*info
,
2926 struct elf_link_hash_table
*hash_table
;
2927 bfd_size_type oldsize
;
2928 bfd_size_type strindex
;
2930 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2933 hash_table
= elf_hash_table (info
);
2934 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2935 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2936 if (strindex
== (bfd_size_type
) -1)
2939 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2942 const struct elf_backend_data
*bed
;
2945 bed
= get_elf_backend_data (hash_table
->dynobj
);
2946 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2948 for (extdyn
= sdyn
->contents
;
2949 extdyn
< sdyn
->contents
+ sdyn
->size
;
2950 extdyn
+= bed
->s
->sizeof_dyn
)
2952 Elf_Internal_Dyn dyn
;
2954 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2955 if (dyn
.d_tag
== DT_NEEDED
2956 && dyn
.d_un
.d_val
== strindex
)
2958 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2966 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2969 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2973 /* We were just checking for existence of the tag. */
2974 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2979 /* Sort symbol by value and section. */
2981 elf_sort_symbol (const void *arg1
, const void *arg2
)
2983 const struct elf_link_hash_entry
*h1
;
2984 const struct elf_link_hash_entry
*h2
;
2985 bfd_signed_vma vdiff
;
2987 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2988 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2989 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2991 return vdiff
> 0 ? 1 : -1;
2994 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2996 return sdiff
> 0 ? 1 : -1;
3001 /* This function is used to adjust offsets into .dynstr for
3002 dynamic symbols. This is called via elf_link_hash_traverse. */
3005 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3007 struct elf_strtab_hash
*dynstr
= data
;
3009 if (h
->root
.type
== bfd_link_hash_warning
)
3010 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3012 if (h
->dynindx
!= -1)
3013 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3017 /* Assign string offsets in .dynstr, update all structures referencing
3021 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3023 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3024 struct elf_link_local_dynamic_entry
*entry
;
3025 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3026 bfd
*dynobj
= hash_table
->dynobj
;
3029 const struct elf_backend_data
*bed
;
3032 _bfd_elf_strtab_finalize (dynstr
);
3033 size
= _bfd_elf_strtab_size (dynstr
);
3035 bed
= get_elf_backend_data (dynobj
);
3036 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3037 BFD_ASSERT (sdyn
!= NULL
);
3039 /* Update all .dynamic entries referencing .dynstr strings. */
3040 for (extdyn
= sdyn
->contents
;
3041 extdyn
< sdyn
->contents
+ sdyn
->size
;
3042 extdyn
+= bed
->s
->sizeof_dyn
)
3044 Elf_Internal_Dyn dyn
;
3046 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3050 dyn
.d_un
.d_val
= size
;
3058 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3063 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3066 /* Now update local dynamic symbols. */
3067 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3068 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3069 entry
->isym
.st_name
);
3071 /* And the rest of dynamic symbols. */
3072 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3074 /* Adjust version definitions. */
3075 if (elf_tdata (output_bfd
)->cverdefs
)
3080 Elf_Internal_Verdef def
;
3081 Elf_Internal_Verdaux defaux
;
3083 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3087 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3089 p
+= sizeof (Elf_External_Verdef
);
3090 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3092 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3094 _bfd_elf_swap_verdaux_in (output_bfd
,
3095 (Elf_External_Verdaux
*) p
, &defaux
);
3096 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3098 _bfd_elf_swap_verdaux_out (output_bfd
,
3099 &defaux
, (Elf_External_Verdaux
*) p
);
3100 p
+= sizeof (Elf_External_Verdaux
);
3103 while (def
.vd_next
);
3106 /* Adjust version references. */
3107 if (elf_tdata (output_bfd
)->verref
)
3112 Elf_Internal_Verneed need
;
3113 Elf_Internal_Vernaux needaux
;
3115 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3119 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3121 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3122 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3123 (Elf_External_Verneed
*) p
);
3124 p
+= sizeof (Elf_External_Verneed
);
3125 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3127 _bfd_elf_swap_vernaux_in (output_bfd
,
3128 (Elf_External_Vernaux
*) p
, &needaux
);
3129 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3131 _bfd_elf_swap_vernaux_out (output_bfd
,
3133 (Elf_External_Vernaux
*) p
);
3134 p
+= sizeof (Elf_External_Vernaux
);
3137 while (need
.vn_next
);
3143 /* Add symbols from an ELF object file to the linker hash table. */
3146 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3148 Elf_Internal_Shdr
*hdr
;
3149 bfd_size_type symcount
;
3150 bfd_size_type extsymcount
;
3151 bfd_size_type extsymoff
;
3152 struct elf_link_hash_entry
**sym_hash
;
3153 bfd_boolean dynamic
;
3154 Elf_External_Versym
*extversym
= NULL
;
3155 Elf_External_Versym
*ever
;
3156 struct elf_link_hash_entry
*weaks
;
3157 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3158 bfd_size_type nondeflt_vers_cnt
= 0;
3159 Elf_Internal_Sym
*isymbuf
= NULL
;
3160 Elf_Internal_Sym
*isym
;
3161 Elf_Internal_Sym
*isymend
;
3162 const struct elf_backend_data
*bed
;
3163 bfd_boolean add_needed
;
3164 struct elf_link_hash_table
*htab
;
3166 void *alloc_mark
= NULL
;
3167 struct bfd_hash_entry
**old_table
= NULL
;
3168 unsigned int old_size
= 0;
3169 unsigned int old_count
= 0;
3170 void *old_tab
= NULL
;
3173 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3174 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3175 long old_dynsymcount
= 0;
3177 size_t hashsize
= 0;
3179 htab
= elf_hash_table (info
);
3180 bed
= get_elf_backend_data (abfd
);
3182 if ((abfd
->flags
& DYNAMIC
) == 0)
3188 /* You can't use -r against a dynamic object. Also, there's no
3189 hope of using a dynamic object which does not exactly match
3190 the format of the output file. */
3191 if (info
->relocatable
3192 || !is_elf_hash_table (htab
)
3193 || htab
->root
.creator
!= abfd
->xvec
)
3195 if (info
->relocatable
)
3196 bfd_set_error (bfd_error_invalid_operation
);
3198 bfd_set_error (bfd_error_wrong_format
);
3203 /* As a GNU extension, any input sections which are named
3204 .gnu.warning.SYMBOL are treated as warning symbols for the given
3205 symbol. This differs from .gnu.warning sections, which generate
3206 warnings when they are included in an output file. */
3207 if (info
->executable
)
3211 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3215 name
= bfd_get_section_name (abfd
, s
);
3216 if (CONST_STRNEQ (name
, ".gnu.warning."))
3221 name
+= sizeof ".gnu.warning." - 1;
3223 /* If this is a shared object, then look up the symbol
3224 in the hash table. If it is there, and it is already
3225 been defined, then we will not be using the entry
3226 from this shared object, so we don't need to warn.
3227 FIXME: If we see the definition in a regular object
3228 later on, we will warn, but we shouldn't. The only
3229 fix is to keep track of what warnings we are supposed
3230 to emit, and then handle them all at the end of the
3234 struct elf_link_hash_entry
*h
;
3236 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3238 /* FIXME: What about bfd_link_hash_common? */
3240 && (h
->root
.type
== bfd_link_hash_defined
3241 || h
->root
.type
== bfd_link_hash_defweak
))
3243 /* We don't want to issue this warning. Clobber
3244 the section size so that the warning does not
3245 get copied into the output file. */
3252 msg
= bfd_alloc (abfd
, sz
+ 1);
3256 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3261 if (! (_bfd_generic_link_add_one_symbol
3262 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3263 FALSE
, bed
->collect
, NULL
)))
3266 if (! info
->relocatable
)
3268 /* Clobber the section size so that the warning does
3269 not get copied into the output file. */
3272 /* Also set SEC_EXCLUDE, so that symbols defined in
3273 the warning section don't get copied to the output. */
3274 s
->flags
|= SEC_EXCLUDE
;
3283 /* If we are creating a shared library, create all the dynamic
3284 sections immediately. We need to attach them to something,
3285 so we attach them to this BFD, provided it is the right
3286 format. FIXME: If there are no input BFD's of the same
3287 format as the output, we can't make a shared library. */
3289 && is_elf_hash_table (htab
)
3290 && htab
->root
.creator
== abfd
->xvec
3291 && !htab
->dynamic_sections_created
)
3293 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3297 else if (!is_elf_hash_table (htab
))
3302 const char *soname
= NULL
;
3303 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3306 /* ld --just-symbols and dynamic objects don't mix very well.
3307 ld shouldn't allow it. */
3308 if ((s
= abfd
->sections
) != NULL
3309 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3312 /* If this dynamic lib was specified on the command line with
3313 --as-needed in effect, then we don't want to add a DT_NEEDED
3314 tag unless the lib is actually used. Similary for libs brought
3315 in by another lib's DT_NEEDED. When --no-add-needed is used
3316 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3317 any dynamic library in DT_NEEDED tags in the dynamic lib at
3319 add_needed
= (elf_dyn_lib_class (abfd
)
3320 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3321 | DYN_NO_NEEDED
)) == 0;
3323 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3329 unsigned long shlink
;
3331 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3332 goto error_free_dyn
;
3334 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3336 goto error_free_dyn
;
3337 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3339 for (extdyn
= dynbuf
;
3340 extdyn
< dynbuf
+ s
->size
;
3341 extdyn
+= bed
->s
->sizeof_dyn
)
3343 Elf_Internal_Dyn dyn
;
3345 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3346 if (dyn
.d_tag
== DT_SONAME
)
3348 unsigned int tagv
= dyn
.d_un
.d_val
;
3349 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3351 goto error_free_dyn
;
3353 if (dyn
.d_tag
== DT_NEEDED
)
3355 struct bfd_link_needed_list
*n
, **pn
;
3357 unsigned int tagv
= dyn
.d_un
.d_val
;
3359 amt
= sizeof (struct bfd_link_needed_list
);
3360 n
= bfd_alloc (abfd
, amt
);
3361 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3362 if (n
== NULL
|| fnm
== NULL
)
3363 goto error_free_dyn
;
3364 amt
= strlen (fnm
) + 1;
3365 anm
= bfd_alloc (abfd
, amt
);
3367 goto error_free_dyn
;
3368 memcpy (anm
, fnm
, amt
);
3372 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3376 if (dyn
.d_tag
== DT_RUNPATH
)
3378 struct bfd_link_needed_list
*n
, **pn
;
3380 unsigned int tagv
= dyn
.d_un
.d_val
;
3382 amt
= sizeof (struct bfd_link_needed_list
);
3383 n
= bfd_alloc (abfd
, amt
);
3384 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3385 if (n
== NULL
|| fnm
== NULL
)
3386 goto error_free_dyn
;
3387 amt
= strlen (fnm
) + 1;
3388 anm
= bfd_alloc (abfd
, amt
);
3390 goto error_free_dyn
;
3391 memcpy (anm
, fnm
, amt
);
3395 for (pn
= & runpath
;
3401 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3402 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3404 struct bfd_link_needed_list
*n
, **pn
;
3406 unsigned int tagv
= dyn
.d_un
.d_val
;
3408 amt
= sizeof (struct bfd_link_needed_list
);
3409 n
= bfd_alloc (abfd
, amt
);
3410 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3411 if (n
== NULL
|| fnm
== NULL
)
3412 goto error_free_dyn
;
3413 amt
= strlen (fnm
) + 1;
3414 anm
= bfd_alloc (abfd
, amt
);
3421 memcpy (anm
, fnm
, amt
);
3436 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3437 frees all more recently bfd_alloc'd blocks as well. */
3443 struct bfd_link_needed_list
**pn
;
3444 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3449 /* We do not want to include any of the sections in a dynamic
3450 object in the output file. We hack by simply clobbering the
3451 list of sections in the BFD. This could be handled more
3452 cleanly by, say, a new section flag; the existing
3453 SEC_NEVER_LOAD flag is not the one we want, because that one
3454 still implies that the section takes up space in the output
3456 bfd_section_list_clear (abfd
);
3458 /* Find the name to use in a DT_NEEDED entry that refers to this
3459 object. If the object has a DT_SONAME entry, we use it.
3460 Otherwise, if the generic linker stuck something in
3461 elf_dt_name, we use that. Otherwise, we just use the file
3463 if (soname
== NULL
|| *soname
== '\0')
3465 soname
= elf_dt_name (abfd
);
3466 if (soname
== NULL
|| *soname
== '\0')
3467 soname
= bfd_get_filename (abfd
);
3470 /* Save the SONAME because sometimes the linker emulation code
3471 will need to know it. */
3472 elf_dt_name (abfd
) = soname
;
3474 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3478 /* If we have already included this dynamic object in the
3479 link, just ignore it. There is no reason to include a
3480 particular dynamic object more than once. */
3485 /* If this is a dynamic object, we always link against the .dynsym
3486 symbol table, not the .symtab symbol table. The dynamic linker
3487 will only see the .dynsym symbol table, so there is no reason to
3488 look at .symtab for a dynamic object. */
3490 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3491 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3493 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3495 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3497 /* The sh_info field of the symtab header tells us where the
3498 external symbols start. We don't care about the local symbols at
3500 if (elf_bad_symtab (abfd
))
3502 extsymcount
= symcount
;
3507 extsymcount
= symcount
- hdr
->sh_info
;
3508 extsymoff
= hdr
->sh_info
;
3512 if (extsymcount
!= 0)
3514 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3516 if (isymbuf
== NULL
)
3519 /* We store a pointer to the hash table entry for each external
3521 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3522 sym_hash
= bfd_alloc (abfd
, amt
);
3523 if (sym_hash
== NULL
)
3524 goto error_free_sym
;
3525 elf_sym_hashes (abfd
) = sym_hash
;
3530 /* Read in any version definitions. */
3531 if (!_bfd_elf_slurp_version_tables (abfd
,
3532 info
->default_imported_symver
))
3533 goto error_free_sym
;
3535 /* Read in the symbol versions, but don't bother to convert them
3536 to internal format. */
3537 if (elf_dynversym (abfd
) != 0)
3539 Elf_Internal_Shdr
*versymhdr
;
3541 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3542 extversym
= bfd_malloc (versymhdr
->sh_size
);
3543 if (extversym
== NULL
)
3544 goto error_free_sym
;
3545 amt
= versymhdr
->sh_size
;
3546 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3547 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3548 goto error_free_vers
;
3552 /* If we are loading an as-needed shared lib, save the symbol table
3553 state before we start adding symbols. If the lib turns out
3554 to be unneeded, restore the state. */
3555 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3560 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3562 struct bfd_hash_entry
*p
;
3563 struct elf_link_hash_entry
*h
;
3565 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3567 h
= (struct elf_link_hash_entry
*) p
;
3568 entsize
+= htab
->root
.table
.entsize
;
3569 if (h
->root
.type
== bfd_link_hash_warning
)
3570 entsize
+= htab
->root
.table
.entsize
;
3574 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3575 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3576 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3577 if (old_tab
== NULL
)
3578 goto error_free_vers
;
3580 /* Remember the current objalloc pointer, so that all mem for
3581 symbols added can later be reclaimed. */
3582 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3583 if (alloc_mark
== NULL
)
3584 goto error_free_vers
;
3586 /* Make a special call to the linker "notice" function to
3587 tell it that we are about to handle an as-needed lib. */
3588 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3593 /* Clone the symbol table and sym hashes. Remember some
3594 pointers into the symbol table, and dynamic symbol count. */
3595 old_hash
= (char *) old_tab
+ tabsize
;
3596 old_ent
= (char *) old_hash
+ hashsize
;
3597 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3598 memcpy (old_hash
, sym_hash
, hashsize
);
3599 old_undefs
= htab
->root
.undefs
;
3600 old_undefs_tail
= htab
->root
.undefs_tail
;
3601 old_table
= htab
->root
.table
.table
;
3602 old_size
= htab
->root
.table
.size
;
3603 old_count
= htab
->root
.table
.count
;
3604 old_dynsymcount
= htab
->dynsymcount
;
3606 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3608 struct bfd_hash_entry
*p
;
3609 struct elf_link_hash_entry
*h
;
3611 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3613 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3614 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3615 h
= (struct elf_link_hash_entry
*) p
;
3616 if (h
->root
.type
== bfd_link_hash_warning
)
3618 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3619 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3626 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3627 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3629 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3633 asection
*sec
, *new_sec
;
3636 struct elf_link_hash_entry
*h
;
3637 bfd_boolean definition
;
3638 bfd_boolean size_change_ok
;
3639 bfd_boolean type_change_ok
;
3640 bfd_boolean new_weakdef
;
3641 bfd_boolean override
;
3643 unsigned int old_alignment
;
3648 flags
= BSF_NO_FLAGS
;
3650 value
= isym
->st_value
;
3652 common
= bed
->common_definition (isym
);
3654 bind
= ELF_ST_BIND (isym
->st_info
);
3655 if (bind
== STB_LOCAL
)
3657 /* This should be impossible, since ELF requires that all
3658 global symbols follow all local symbols, and that sh_info
3659 point to the first global symbol. Unfortunately, Irix 5
3663 else if (bind
== STB_GLOBAL
)
3665 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3668 else if (bind
== STB_WEAK
)
3672 /* Leave it up to the processor backend. */
3675 if (isym
->st_shndx
== SHN_UNDEF
)
3676 sec
= bfd_und_section_ptr
;
3677 else if (isym
->st_shndx
< SHN_LORESERVE
3678 || isym
->st_shndx
> SHN_HIRESERVE
)
3680 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3682 sec
= bfd_abs_section_ptr
;
3683 else if (sec
->kept_section
)
3685 /* Symbols from discarded section are undefined, and have
3686 default visibility. */
3687 sec
= bfd_und_section_ptr
;
3688 isym
->st_shndx
= SHN_UNDEF
;
3689 isym
->st_other
= (STV_DEFAULT
3690 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3692 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3695 else if (isym
->st_shndx
== SHN_ABS
)
3696 sec
= bfd_abs_section_ptr
;
3697 else if (isym
->st_shndx
== SHN_COMMON
)
3699 sec
= bfd_com_section_ptr
;
3700 /* What ELF calls the size we call the value. What ELF
3701 calls the value we call the alignment. */
3702 value
= isym
->st_size
;
3706 /* Leave it up to the processor backend. */
3709 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3712 goto error_free_vers
;
3714 if (isym
->st_shndx
== SHN_COMMON
3715 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3716 && !info
->relocatable
)
3718 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3722 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3725 | SEC_LINKER_CREATED
3726 | SEC_THREAD_LOCAL
));
3728 goto error_free_vers
;
3732 else if (bed
->elf_add_symbol_hook
)
3734 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3736 goto error_free_vers
;
3738 /* The hook function sets the name to NULL if this symbol
3739 should be skipped for some reason. */
3744 /* Sanity check that all possibilities were handled. */
3747 bfd_set_error (bfd_error_bad_value
);
3748 goto error_free_vers
;
3751 if (bfd_is_und_section (sec
)
3752 || bfd_is_com_section (sec
))
3757 size_change_ok
= FALSE
;
3758 type_change_ok
= bed
->type_change_ok
;
3763 if (is_elf_hash_table (htab
))
3765 Elf_Internal_Versym iver
;
3766 unsigned int vernum
= 0;
3771 if (info
->default_imported_symver
)
3772 /* Use the default symbol version created earlier. */
3773 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3778 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3780 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3782 /* If this is a hidden symbol, or if it is not version
3783 1, we append the version name to the symbol name.
3784 However, we do not modify a non-hidden absolute symbol
3785 if it is not a function, because it might be the version
3786 symbol itself. FIXME: What if it isn't? */
3787 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3788 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3789 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3792 size_t namelen
, verlen
, newlen
;
3795 if (isym
->st_shndx
!= SHN_UNDEF
)
3797 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3799 else if (vernum
> 1)
3801 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3807 (*_bfd_error_handler
)
3808 (_("%B: %s: invalid version %u (max %d)"),
3810 elf_tdata (abfd
)->cverdefs
);
3811 bfd_set_error (bfd_error_bad_value
);
3812 goto error_free_vers
;
3817 /* We cannot simply test for the number of
3818 entries in the VERNEED section since the
3819 numbers for the needed versions do not start
3821 Elf_Internal_Verneed
*t
;
3824 for (t
= elf_tdata (abfd
)->verref
;
3828 Elf_Internal_Vernaux
*a
;
3830 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3832 if (a
->vna_other
== vernum
)
3834 verstr
= a
->vna_nodename
;
3843 (*_bfd_error_handler
)
3844 (_("%B: %s: invalid needed version %d"),
3845 abfd
, name
, vernum
);
3846 bfd_set_error (bfd_error_bad_value
);
3847 goto error_free_vers
;
3851 namelen
= strlen (name
);
3852 verlen
= strlen (verstr
);
3853 newlen
= namelen
+ verlen
+ 2;
3854 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3855 && isym
->st_shndx
!= SHN_UNDEF
)
3858 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3859 if (newname
== NULL
)
3860 goto error_free_vers
;
3861 memcpy (newname
, name
, namelen
);
3862 p
= newname
+ namelen
;
3864 /* If this is a defined non-hidden version symbol,
3865 we add another @ to the name. This indicates the
3866 default version of the symbol. */
3867 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3868 && isym
->st_shndx
!= SHN_UNDEF
)
3870 memcpy (p
, verstr
, verlen
+ 1);
3875 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3876 &value
, &old_alignment
,
3877 sym_hash
, &skip
, &override
,
3878 &type_change_ok
, &size_change_ok
))
3879 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
;
3892 /* Remember the old alignment if this is a common symbol, so
3893 that we don't reduce the alignment later on. We can't
3894 check later, because _bfd_generic_link_add_one_symbol
3895 will set a default for the alignment which we want to
3896 override. We also remember the old bfd where the existing
3897 definition comes from. */
3898 switch (h
->root
.type
)
3903 case bfd_link_hash_defined
:
3904 case bfd_link_hash_defweak
:
3905 old_bfd
= h
->root
.u
.def
.section
->owner
;
3908 case bfd_link_hash_common
:
3909 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3910 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3914 if (elf_tdata (abfd
)->verdef
!= NULL
3918 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3921 if (! (_bfd_generic_link_add_one_symbol
3922 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3923 (struct bfd_link_hash_entry
**) sym_hash
)))
3924 goto error_free_vers
;
3927 while (h
->root
.type
== bfd_link_hash_indirect
3928 || h
->root
.type
== bfd_link_hash_warning
)
3929 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3932 new_weakdef
= FALSE
;
3935 && (flags
& BSF_WEAK
) != 0
3936 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3937 && is_elf_hash_table (htab
)
3938 && h
->u
.weakdef
== NULL
)
3940 /* Keep a list of all weak defined non function symbols from
3941 a dynamic object, using the weakdef field. Later in this
3942 function we will set the weakdef field to the correct
3943 value. We only put non-function symbols from dynamic
3944 objects on this list, because that happens to be the only
3945 time we need to know the normal symbol corresponding to a
3946 weak symbol, and the information is time consuming to
3947 figure out. If the weakdef field is not already NULL,
3948 then this symbol was already defined by some previous
3949 dynamic object, and we will be using that previous
3950 definition anyhow. */
3952 h
->u
.weakdef
= weaks
;
3957 /* Set the alignment of a common symbol. */
3958 if ((common
|| bfd_is_com_section (sec
))
3959 && h
->root
.type
== bfd_link_hash_common
)
3964 align
= bfd_log2 (isym
->st_value
);
3967 /* The new symbol is a common symbol in a shared object.
3968 We need to get the alignment from the section. */
3969 align
= new_sec
->alignment_power
;
3971 if (align
> old_alignment
3972 /* Permit an alignment power of zero if an alignment of one
3973 is specified and no other alignments have been specified. */
3974 || (isym
->st_value
== 1 && old_alignment
== 0))
3975 h
->root
.u
.c
.p
->alignment_power
= align
;
3977 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3980 if (is_elf_hash_table (htab
))
3984 /* Check the alignment when a common symbol is involved. This
3985 can change when a common symbol is overridden by a normal
3986 definition or a common symbol is ignored due to the old
3987 normal definition. We need to make sure the maximum
3988 alignment is maintained. */
3989 if ((old_alignment
|| common
)
3990 && h
->root
.type
!= bfd_link_hash_common
)
3992 unsigned int common_align
;
3993 unsigned int normal_align
;
3994 unsigned int symbol_align
;
3998 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3999 if (h
->root
.u
.def
.section
->owner
!= NULL
4000 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4002 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4003 if (normal_align
> symbol_align
)
4004 normal_align
= symbol_align
;
4007 normal_align
= symbol_align
;
4011 common_align
= old_alignment
;
4012 common_bfd
= old_bfd
;
4017 common_align
= bfd_log2 (isym
->st_value
);
4019 normal_bfd
= old_bfd
;
4022 if (normal_align
< common_align
)
4024 /* PR binutils/2735 */
4025 if (normal_bfd
== NULL
)
4026 (*_bfd_error_handler
)
4027 (_("Warning: alignment %u of common symbol `%s' in %B"
4028 " is greater than the alignment (%u) of its section %A"),
4029 common_bfd
, h
->root
.u
.def
.section
,
4030 1 << common_align
, name
, 1 << normal_align
);
4032 (*_bfd_error_handler
)
4033 (_("Warning: alignment %u of symbol `%s' in %B"
4034 " is smaller than %u in %B"),
4035 normal_bfd
, common_bfd
,
4036 1 << normal_align
, name
, 1 << common_align
);
4040 /* Remember the symbol size and type. */
4041 if (isym
->st_size
!= 0
4042 && (definition
|| h
->size
== 0))
4044 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
4045 (*_bfd_error_handler
)
4046 (_("Warning: size of symbol `%s' changed"
4047 " from %lu in %B to %lu in %B"),
4049 name
, (unsigned long) h
->size
,
4050 (unsigned long) isym
->st_size
);
4052 h
->size
= isym
->st_size
;
4055 /* If this is a common symbol, then we always want H->SIZE
4056 to be the size of the common symbol. The code just above
4057 won't fix the size if a common symbol becomes larger. We
4058 don't warn about a size change here, because that is
4059 covered by --warn-common. */
4060 if (h
->root
.type
== bfd_link_hash_common
)
4061 h
->size
= h
->root
.u
.c
.size
;
4063 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4064 && (definition
|| h
->type
== STT_NOTYPE
))
4066 if (h
->type
!= STT_NOTYPE
4067 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4068 && ! type_change_ok
)
4069 (*_bfd_error_handler
)
4070 (_("Warning: type of symbol `%s' changed"
4071 " from %d to %d in %B"),
4072 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4074 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4077 /* If st_other has a processor-specific meaning, specific
4078 code might be needed here. We never merge the visibility
4079 attribute with the one from a dynamic object. */
4080 if (bed
->elf_backend_merge_symbol_attribute
)
4081 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4084 /* If this symbol has default visibility and the user has requested
4085 we not re-export it, then mark it as hidden. */
4086 if (definition
&& !dynamic
4088 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4089 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4090 isym
->st_other
= (STV_HIDDEN
4091 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4093 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4095 unsigned char hvis
, symvis
, other
, nvis
;
4097 /* Only merge the visibility. Leave the remainder of the
4098 st_other field to elf_backend_merge_symbol_attribute. */
4099 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4101 /* Combine visibilities, using the most constraining one. */
4102 hvis
= ELF_ST_VISIBILITY (h
->other
);
4103 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4109 nvis
= hvis
< symvis
? hvis
: symvis
;
4111 h
->other
= other
| nvis
;
4114 /* Set a flag in the hash table entry indicating the type of
4115 reference or definition we just found. Keep a count of
4116 the number of dynamic symbols we find. A dynamic symbol
4117 is one which is referenced or defined by both a regular
4118 object and a shared object. */
4125 if (bind
!= STB_WEAK
)
4126 h
->ref_regular_nonweak
= 1;
4130 if (! info
->executable
4143 || (h
->u
.weakdef
!= NULL
4145 && h
->u
.weakdef
->dynindx
!= -1))
4149 /* Check to see if we need to add an indirect symbol for
4150 the default name. */
4151 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4152 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4153 &sec
, &value
, &dynsym
,
4155 goto error_free_vers
;
4157 if (definition
&& !dynamic
)
4159 char *p
= strchr (name
, ELF_VER_CHR
);
4160 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4162 /* Queue non-default versions so that .symver x, x@FOO
4163 aliases can be checked. */
4166 amt
= ((isymend
- isym
+ 1)
4167 * sizeof (struct elf_link_hash_entry
*));
4168 nondeflt_vers
= bfd_malloc (amt
);
4170 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4174 if (dynsym
&& h
->dynindx
== -1)
4176 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4177 goto error_free_vers
;
4178 if (h
->u
.weakdef
!= NULL
4180 && h
->u
.weakdef
->dynindx
== -1)
4182 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4183 goto error_free_vers
;
4186 else if (dynsym
&& h
->dynindx
!= -1)
4187 /* If the symbol already has a dynamic index, but
4188 visibility says it should not be visible, turn it into
4190 switch (ELF_ST_VISIBILITY (h
->other
))
4194 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4205 const char *soname
= elf_dt_name (abfd
);
4207 /* A symbol from a library loaded via DT_NEEDED of some
4208 other library is referenced by a regular object.
4209 Add a DT_NEEDED entry for it. Issue an error if
4210 --no-add-needed is used. */
4211 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4213 (*_bfd_error_handler
)
4214 (_("%s: invalid DSO for symbol `%s' definition"),
4216 bfd_set_error (bfd_error_bad_value
);
4217 goto error_free_vers
;
4220 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4223 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4225 goto error_free_vers
;
4227 BFD_ASSERT (ret
== 0);
4232 if (extversym
!= NULL
)
4238 if (isymbuf
!= NULL
)
4244 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4248 /* Restore the symbol table. */
4249 old_hash
= (char *) old_tab
+ tabsize
;
4250 old_ent
= (char *) old_hash
+ hashsize
;
4251 sym_hash
= elf_sym_hashes (abfd
);
4252 htab
->root
.table
.table
= old_table
;
4253 htab
->root
.table
.size
= old_size
;
4254 htab
->root
.table
.count
= old_count
;
4255 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4256 memcpy (sym_hash
, old_hash
, hashsize
);
4257 htab
->root
.undefs
= old_undefs
;
4258 htab
->root
.undefs_tail
= old_undefs_tail
;
4259 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4261 struct bfd_hash_entry
*p
;
4262 struct elf_link_hash_entry
*h
;
4264 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4266 h
= (struct elf_link_hash_entry
*) p
;
4267 if (h
->root
.type
== bfd_link_hash_warning
)
4268 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4269 if (h
->dynindx
>= old_dynsymcount
)
4270 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4272 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4273 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4274 h
= (struct elf_link_hash_entry
*) p
;
4275 if (h
->root
.type
== bfd_link_hash_warning
)
4277 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4278 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4283 /* Make a special call to the linker "notice" function to
4284 tell it that symbols added for crefs may need to be removed. */
4285 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4290 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4292 if (nondeflt_vers
!= NULL
)
4293 free (nondeflt_vers
);
4297 if (old_tab
!= NULL
)
4299 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4306 /* Now that all the symbols from this input file are created, handle
4307 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4308 if (nondeflt_vers
!= NULL
)
4310 bfd_size_type cnt
, symidx
;
4312 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4314 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4315 char *shortname
, *p
;
4317 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4319 || (h
->root
.type
!= bfd_link_hash_defined
4320 && h
->root
.type
!= bfd_link_hash_defweak
))
4323 amt
= p
- h
->root
.root
.string
;
4324 shortname
= bfd_malloc (amt
+ 1);
4325 memcpy (shortname
, h
->root
.root
.string
, amt
);
4326 shortname
[amt
] = '\0';
4328 hi
= (struct elf_link_hash_entry
*)
4329 bfd_link_hash_lookup (&htab
->root
, shortname
,
4330 FALSE
, FALSE
, FALSE
);
4332 && hi
->root
.type
== h
->root
.type
4333 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4334 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4336 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4337 hi
->root
.type
= bfd_link_hash_indirect
;
4338 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4339 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4340 sym_hash
= elf_sym_hashes (abfd
);
4342 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4343 if (sym_hash
[symidx
] == hi
)
4345 sym_hash
[symidx
] = h
;
4351 free (nondeflt_vers
);
4352 nondeflt_vers
= NULL
;
4355 /* Now set the weakdefs field correctly for all the weak defined
4356 symbols we found. The only way to do this is to search all the
4357 symbols. Since we only need the information for non functions in
4358 dynamic objects, that's the only time we actually put anything on
4359 the list WEAKS. We need this information so that if a regular
4360 object refers to a symbol defined weakly in a dynamic object, the
4361 real symbol in the dynamic object is also put in the dynamic
4362 symbols; we also must arrange for both symbols to point to the
4363 same memory location. We could handle the general case of symbol
4364 aliasing, but a general symbol alias can only be generated in
4365 assembler code, handling it correctly would be very time
4366 consuming, and other ELF linkers don't handle general aliasing
4370 struct elf_link_hash_entry
**hpp
;
4371 struct elf_link_hash_entry
**hppend
;
4372 struct elf_link_hash_entry
**sorted_sym_hash
;
4373 struct elf_link_hash_entry
*h
;
4376 /* Since we have to search the whole symbol list for each weak
4377 defined symbol, search time for N weak defined symbols will be
4378 O(N^2). Binary search will cut it down to O(NlogN). */
4379 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4380 sorted_sym_hash
= bfd_malloc (amt
);
4381 if (sorted_sym_hash
== NULL
)
4383 sym_hash
= sorted_sym_hash
;
4384 hpp
= elf_sym_hashes (abfd
);
4385 hppend
= hpp
+ extsymcount
;
4387 for (; hpp
< hppend
; hpp
++)
4391 && h
->root
.type
== bfd_link_hash_defined
4392 && h
->type
!= STT_FUNC
)
4400 qsort (sorted_sym_hash
, sym_count
,
4401 sizeof (struct elf_link_hash_entry
*),
4404 while (weaks
!= NULL
)
4406 struct elf_link_hash_entry
*hlook
;
4413 weaks
= hlook
->u
.weakdef
;
4414 hlook
->u
.weakdef
= NULL
;
4416 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4417 || hlook
->root
.type
== bfd_link_hash_defweak
4418 || hlook
->root
.type
== bfd_link_hash_common
4419 || hlook
->root
.type
== bfd_link_hash_indirect
);
4420 slook
= hlook
->root
.u
.def
.section
;
4421 vlook
= hlook
->root
.u
.def
.value
;
4428 bfd_signed_vma vdiff
;
4430 h
= sorted_sym_hash
[idx
];
4431 vdiff
= vlook
- h
->root
.u
.def
.value
;
4438 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4451 /* We didn't find a value/section match. */
4455 for (i
= ilook
; i
< sym_count
; i
++)
4457 h
= sorted_sym_hash
[i
];
4459 /* Stop if value or section doesn't match. */
4460 if (h
->root
.u
.def
.value
!= vlook
4461 || h
->root
.u
.def
.section
!= slook
)
4463 else if (h
!= hlook
)
4465 hlook
->u
.weakdef
= h
;
4467 /* If the weak definition is in the list of dynamic
4468 symbols, make sure the real definition is put
4470 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4472 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4476 /* If the real definition is in the list of dynamic
4477 symbols, make sure the weak definition is put
4478 there as well. If we don't do this, then the
4479 dynamic loader might not merge the entries for the
4480 real definition and the weak definition. */
4481 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4483 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4491 free (sorted_sym_hash
);
4494 if (bed
->check_directives
)
4495 (*bed
->check_directives
) (abfd
, info
);
4497 /* If this object is the same format as the output object, and it is
4498 not a shared library, then let the backend look through the
4501 This is required to build global offset table entries and to
4502 arrange for dynamic relocs. It is not required for the
4503 particular common case of linking non PIC code, even when linking
4504 against shared libraries, but unfortunately there is no way of
4505 knowing whether an object file has been compiled PIC or not.
4506 Looking through the relocs is not particularly time consuming.
4507 The problem is that we must either (1) keep the relocs in memory,
4508 which causes the linker to require additional runtime memory or
4509 (2) read the relocs twice from the input file, which wastes time.
4510 This would be a good case for using mmap.
4512 I have no idea how to handle linking PIC code into a file of a
4513 different format. It probably can't be done. */
4515 && is_elf_hash_table (htab
)
4516 && htab
->root
.creator
== abfd
->xvec
4517 && bed
->check_relocs
!= NULL
)
4521 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4523 Elf_Internal_Rela
*internal_relocs
;
4526 if ((o
->flags
& SEC_RELOC
) == 0
4527 || o
->reloc_count
== 0
4528 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4529 && (o
->flags
& SEC_DEBUGGING
) != 0)
4530 || bfd_is_abs_section (o
->output_section
))
4533 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4535 if (internal_relocs
== NULL
)
4538 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4540 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4541 free (internal_relocs
);
4548 /* If this is a non-traditional link, try to optimize the handling
4549 of the .stab/.stabstr sections. */
4551 && ! info
->traditional_format
4552 && is_elf_hash_table (htab
)
4553 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4557 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4558 if (stabstr
!= NULL
)
4560 bfd_size_type string_offset
= 0;
4563 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4564 if (CONST_STRNEQ (stab
->name
, ".stab")
4565 && (!stab
->name
[5] ||
4566 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4567 && (stab
->flags
& SEC_MERGE
) == 0
4568 && !bfd_is_abs_section (stab
->output_section
))
4570 struct bfd_elf_section_data
*secdata
;
4572 secdata
= elf_section_data (stab
);
4573 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4574 stabstr
, &secdata
->sec_info
,
4577 if (secdata
->sec_info
)
4578 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4583 if (is_elf_hash_table (htab
) && add_needed
)
4585 /* Add this bfd to the loaded list. */
4586 struct elf_link_loaded_list
*n
;
4588 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4592 n
->next
= htab
->loaded
;
4599 if (old_tab
!= NULL
)
4601 if (nondeflt_vers
!= NULL
)
4602 free (nondeflt_vers
);
4603 if (extversym
!= NULL
)
4606 if (isymbuf
!= NULL
)
4612 /* Return the linker hash table entry of a symbol that might be
4613 satisfied by an archive symbol. Return -1 on error. */
4615 struct elf_link_hash_entry
*
4616 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4617 struct bfd_link_info
*info
,
4620 struct elf_link_hash_entry
*h
;
4624 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4628 /* If this is a default version (the name contains @@), look up the
4629 symbol again with only one `@' as well as without the version.
4630 The effect is that references to the symbol with and without the
4631 version will be matched by the default symbol in the archive. */
4633 p
= strchr (name
, ELF_VER_CHR
);
4634 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4637 /* First check with only one `@'. */
4638 len
= strlen (name
);
4639 copy
= bfd_alloc (abfd
, len
);
4641 return (struct elf_link_hash_entry
*) 0 - 1;
4643 first
= p
- name
+ 1;
4644 memcpy (copy
, name
, first
);
4645 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4647 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4650 /* We also need to check references to the symbol without the
4652 copy
[first
- 1] = '\0';
4653 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4654 FALSE
, FALSE
, FALSE
);
4657 bfd_release (abfd
, copy
);
4661 /* Add symbols from an ELF archive file to the linker hash table. We
4662 don't use _bfd_generic_link_add_archive_symbols because of a
4663 problem which arises on UnixWare. The UnixWare libc.so is an
4664 archive which includes an entry libc.so.1 which defines a bunch of
4665 symbols. The libc.so archive also includes a number of other
4666 object files, which also define symbols, some of which are the same
4667 as those defined in libc.so.1. Correct linking requires that we
4668 consider each object file in turn, and include it if it defines any
4669 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4670 this; it looks through the list of undefined symbols, and includes
4671 any object file which defines them. When this algorithm is used on
4672 UnixWare, it winds up pulling in libc.so.1 early and defining a
4673 bunch of symbols. This means that some of the other objects in the
4674 archive are not included in the link, which is incorrect since they
4675 precede libc.so.1 in the archive.
4677 Fortunately, ELF archive handling is simpler than that done by
4678 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4679 oddities. In ELF, if we find a symbol in the archive map, and the
4680 symbol is currently undefined, we know that we must pull in that
4683 Unfortunately, we do have to make multiple passes over the symbol
4684 table until nothing further is resolved. */
4687 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4690 bfd_boolean
*defined
= NULL
;
4691 bfd_boolean
*included
= NULL
;
4695 const struct elf_backend_data
*bed
;
4696 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4697 (bfd
*, struct bfd_link_info
*, const char *);
4699 if (! bfd_has_map (abfd
))
4701 /* An empty archive is a special case. */
4702 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4704 bfd_set_error (bfd_error_no_armap
);
4708 /* Keep track of all symbols we know to be already defined, and all
4709 files we know to be already included. This is to speed up the
4710 second and subsequent passes. */
4711 c
= bfd_ardata (abfd
)->symdef_count
;
4715 amt
*= sizeof (bfd_boolean
);
4716 defined
= bfd_zmalloc (amt
);
4717 included
= bfd_zmalloc (amt
);
4718 if (defined
== NULL
|| included
== NULL
)
4721 symdefs
= bfd_ardata (abfd
)->symdefs
;
4722 bed
= get_elf_backend_data (abfd
);
4723 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4736 symdefend
= symdef
+ c
;
4737 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4739 struct elf_link_hash_entry
*h
;
4741 struct bfd_link_hash_entry
*undefs_tail
;
4744 if (defined
[i
] || included
[i
])
4746 if (symdef
->file_offset
== last
)
4752 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4753 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4759 if (h
->root
.type
== bfd_link_hash_common
)
4761 /* We currently have a common symbol. The archive map contains
4762 a reference to this symbol, so we may want to include it. We
4763 only want to include it however, if this archive element
4764 contains a definition of the symbol, not just another common
4767 Unfortunately some archivers (including GNU ar) will put
4768 declarations of common symbols into their archive maps, as
4769 well as real definitions, so we cannot just go by the archive
4770 map alone. Instead we must read in the element's symbol
4771 table and check that to see what kind of symbol definition
4773 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4776 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4778 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4783 /* We need to include this archive member. */
4784 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4785 if (element
== NULL
)
4788 if (! bfd_check_format (element
, bfd_object
))
4791 /* Doublecheck that we have not included this object
4792 already--it should be impossible, but there may be
4793 something wrong with the archive. */
4794 if (element
->archive_pass
!= 0)
4796 bfd_set_error (bfd_error_bad_value
);
4799 element
->archive_pass
= 1;
4801 undefs_tail
= info
->hash
->undefs_tail
;
4803 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4806 if (! bfd_link_add_symbols (element
, info
))
4809 /* If there are any new undefined symbols, we need to make
4810 another pass through the archive in order to see whether
4811 they can be defined. FIXME: This isn't perfect, because
4812 common symbols wind up on undefs_tail and because an
4813 undefined symbol which is defined later on in this pass
4814 does not require another pass. This isn't a bug, but it
4815 does make the code less efficient than it could be. */
4816 if (undefs_tail
!= info
->hash
->undefs_tail
)
4819 /* Look backward to mark all symbols from this object file
4820 which we have already seen in this pass. */
4824 included
[mark
] = TRUE
;
4829 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4831 /* We mark subsequent symbols from this object file as we go
4832 on through the loop. */
4833 last
= symdef
->file_offset
;
4844 if (defined
!= NULL
)
4846 if (included
!= NULL
)
4851 /* Given an ELF BFD, add symbols to the global hash table as
4855 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4857 switch (bfd_get_format (abfd
))
4860 return elf_link_add_object_symbols (abfd
, info
);
4862 return elf_link_add_archive_symbols (abfd
, info
);
4864 bfd_set_error (bfd_error_wrong_format
);
4869 /* This function will be called though elf_link_hash_traverse to store
4870 all hash value of the exported symbols in an array. */
4873 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4875 unsigned long **valuep
= data
;
4881 if (h
->root
.type
== bfd_link_hash_warning
)
4882 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4884 /* Ignore indirect symbols. These are added by the versioning code. */
4885 if (h
->dynindx
== -1)
4888 name
= h
->root
.root
.string
;
4889 p
= strchr (name
, ELF_VER_CHR
);
4892 alc
= bfd_malloc (p
- name
+ 1);
4893 memcpy (alc
, name
, p
- name
);
4894 alc
[p
- name
] = '\0';
4898 /* Compute the hash value. */
4899 ha
= bfd_elf_hash (name
);
4901 /* Store the found hash value in the array given as the argument. */
4904 /* And store it in the struct so that we can put it in the hash table
4906 h
->u
.elf_hash_value
= ha
;
4914 struct collect_gnu_hash_codes
4917 const struct elf_backend_data
*bed
;
4918 unsigned long int nsyms
;
4919 unsigned long int maskbits
;
4920 unsigned long int *hashcodes
;
4921 unsigned long int *hashval
;
4922 unsigned long int *indx
;
4923 unsigned long int *counts
;
4926 long int min_dynindx
;
4927 unsigned long int bucketcount
;
4928 unsigned long int symindx
;
4929 long int local_indx
;
4930 long int shift1
, shift2
;
4931 unsigned long int mask
;
4934 /* This function will be called though elf_link_hash_traverse to store
4935 all hash value of the exported symbols in an array. */
4938 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4940 struct collect_gnu_hash_codes
*s
= data
;
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. These are added by the versioning code. */
4950 if (h
->dynindx
== -1)
4953 /* Ignore also local symbols and undefined symbols. */
4954 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4957 name
= h
->root
.root
.string
;
4958 p
= strchr (name
, ELF_VER_CHR
);
4961 alc
= bfd_malloc (p
- name
+ 1);
4962 memcpy (alc
, name
, p
- name
);
4963 alc
[p
- name
] = '\0';
4967 /* Compute the hash value. */
4968 ha
= bfd_elf_gnu_hash (name
);
4970 /* Store the found hash value in the array for compute_bucket_count,
4971 and also for .dynsym reordering purposes. */
4972 s
->hashcodes
[s
->nsyms
] = ha
;
4973 s
->hashval
[h
->dynindx
] = ha
;
4975 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
4976 s
->min_dynindx
= h
->dynindx
;
4984 /* This function will be called though elf_link_hash_traverse to do
4985 final dynaminc symbol renumbering. */
4988 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
4990 struct collect_gnu_hash_codes
*s
= data
;
4991 unsigned long int bucket
;
4992 unsigned long int val
;
4994 if (h
->root
.type
== bfd_link_hash_warning
)
4995 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4997 /* Ignore indirect symbols. */
4998 if (h
->dynindx
== -1)
5001 /* Ignore also local symbols and undefined symbols. */
5002 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5004 if (h
->dynindx
>= s
->min_dynindx
)
5005 h
->dynindx
= s
->local_indx
++;
5009 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5010 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5011 & ((s
->maskbits
>> s
->shift1
) - 1);
5012 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5014 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5015 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5016 if (s
->counts
[bucket
] == 1)
5017 /* Last element terminates the chain. */
5019 bfd_put_32 (s
->output_bfd
, val
,
5020 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5021 --s
->counts
[bucket
];
5022 h
->dynindx
= s
->indx
[bucket
]++;
5026 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5029 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5031 return !(h
->forced_local
5032 || h
->root
.type
== bfd_link_hash_undefined
5033 || h
->root
.type
== bfd_link_hash_undefweak
5034 || ((h
->root
.type
== bfd_link_hash_defined
5035 || h
->root
.type
== bfd_link_hash_defweak
)
5036 && h
->root
.u
.def
.section
->output_section
== NULL
));
5039 /* Array used to determine the number of hash table buckets to use
5040 based on the number of symbols there are. If there are fewer than
5041 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5042 fewer than 37 we use 17 buckets, and so forth. We never use more
5043 than 32771 buckets. */
5045 static const size_t elf_buckets
[] =
5047 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5051 /* Compute bucket count for hashing table. We do not use a static set
5052 of possible tables sizes anymore. Instead we determine for all
5053 possible reasonable sizes of the table the outcome (i.e., the
5054 number of collisions etc) and choose the best solution. The
5055 weighting functions are not too simple to allow the table to grow
5056 without bounds. Instead one of the weighting factors is the size.
5057 Therefore the result is always a good payoff between few collisions
5058 (= short chain lengths) and table size. */
5060 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5061 unsigned long int nsyms
, int gnu_hash
)
5063 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5064 size_t best_size
= 0;
5065 unsigned long int i
;
5068 /* We have a problem here. The following code to optimize the table
5069 size requires an integer type with more the 32 bits. If
5070 BFD_HOST_U_64_BIT is set we know about such a type. */
5071 #ifdef BFD_HOST_U_64_BIT
5076 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5077 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5078 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5079 unsigned long int *counts
;
5081 /* Possible optimization parameters: if we have NSYMS symbols we say
5082 that the hashing table must at least have NSYMS/4 and at most
5084 minsize
= nsyms
/ 4;
5087 best_size
= maxsize
= nsyms
* 2;
5092 if ((best_size
& 31) == 0)
5096 /* Create array where we count the collisions in. We must use bfd_malloc
5097 since the size could be large. */
5099 amt
*= sizeof (unsigned long int);
5100 counts
= bfd_malloc (amt
);
5104 /* Compute the "optimal" size for the hash table. The criteria is a
5105 minimal chain length. The minor criteria is (of course) the size
5107 for (i
= minsize
; i
< maxsize
; ++i
)
5109 /* Walk through the array of hashcodes and count the collisions. */
5110 BFD_HOST_U_64_BIT max
;
5111 unsigned long int j
;
5112 unsigned long int fact
;
5114 if (gnu_hash
&& (i
& 31) == 0)
5117 memset (counts
, '\0', i
* sizeof (unsigned long int));
5119 /* Determine how often each hash bucket is used. */
5120 for (j
= 0; j
< nsyms
; ++j
)
5121 ++counts
[hashcodes
[j
] % i
];
5123 /* For the weight function we need some information about the
5124 pagesize on the target. This is information need not be 100%
5125 accurate. Since this information is not available (so far) we
5126 define it here to a reasonable default value. If it is crucial
5127 to have a better value some day simply define this value. */
5128 # ifndef BFD_TARGET_PAGESIZE
5129 # define BFD_TARGET_PAGESIZE (4096)
5132 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5134 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5137 /* Variant 1: optimize for short chains. We add the squares
5138 of all the chain lengths (which favors many small chain
5139 over a few long chains). */
5140 for (j
= 0; j
< i
; ++j
)
5141 max
+= counts
[j
] * counts
[j
];
5143 /* This adds penalties for the overall size of the table. */
5144 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5147 /* Variant 2: Optimize a lot more for small table. Here we
5148 also add squares of the size but we also add penalties for
5149 empty slots (the +1 term). */
5150 for (j
= 0; j
< i
; ++j
)
5151 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5153 /* The overall size of the table is considered, but not as
5154 strong as in variant 1, where it is squared. */
5155 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5159 /* Compare with current best results. */
5160 if (max
< best_chlen
)
5170 #endif /* defined (BFD_HOST_U_64_BIT) */
5172 /* This is the fallback solution if no 64bit type is available or if we
5173 are not supposed to spend much time on optimizations. We select the
5174 bucket count using a fixed set of numbers. */
5175 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5177 best_size
= elf_buckets
[i
];
5178 if (nsyms
< elf_buckets
[i
+ 1])
5181 if (gnu_hash
&& best_size
< 2)
5188 /* Set up the sizes and contents of the ELF dynamic sections. This is
5189 called by the ELF linker emulation before_allocation routine. We
5190 must set the sizes of the sections before the linker sets the
5191 addresses of the various sections. */
5194 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5197 const char *filter_shlib
,
5198 const char * const *auxiliary_filters
,
5199 struct bfd_link_info
*info
,
5200 asection
**sinterpptr
,
5201 struct bfd_elf_version_tree
*verdefs
)
5203 bfd_size_type soname_indx
;
5205 const struct elf_backend_data
*bed
;
5206 struct elf_assign_sym_version_info asvinfo
;
5210 soname_indx
= (bfd_size_type
) -1;
5212 if (!is_elf_hash_table (info
->hash
))
5215 elf_tdata (output_bfd
)->relro
= info
->relro
;
5216 if (info
->execstack
)
5217 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5218 else if (info
->noexecstack
)
5219 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5223 asection
*notesec
= NULL
;
5226 for (inputobj
= info
->input_bfds
;
5228 inputobj
= inputobj
->link_next
)
5232 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5234 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5237 if (s
->flags
& SEC_CODE
)
5246 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5247 if (exec
&& info
->relocatable
5248 && notesec
->output_section
!= bfd_abs_section_ptr
)
5249 notesec
->output_section
->flags
|= SEC_CODE
;
5253 /* Any syms created from now on start with -1 in
5254 got.refcount/offset and plt.refcount/offset. */
5255 elf_hash_table (info
)->init_got_refcount
5256 = elf_hash_table (info
)->init_got_offset
;
5257 elf_hash_table (info
)->init_plt_refcount
5258 = elf_hash_table (info
)->init_plt_offset
;
5260 /* The backend may have to create some sections regardless of whether
5261 we're dynamic or not. */
5262 bed
= get_elf_backend_data (output_bfd
);
5263 if (bed
->elf_backend_always_size_sections
5264 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5267 dynobj
= elf_hash_table (info
)->dynobj
;
5269 /* If there were no dynamic objects in the link, there is nothing to
5274 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5277 if (elf_hash_table (info
)->dynamic_sections_created
)
5279 struct elf_info_failed eif
;
5280 struct elf_link_hash_entry
*h
;
5282 struct bfd_elf_version_tree
*t
;
5283 struct bfd_elf_version_expr
*d
;
5285 bfd_boolean all_defined
;
5287 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5288 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5292 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5294 if (soname_indx
== (bfd_size_type
) -1
5295 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5301 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5303 info
->flags
|= DF_SYMBOLIC
;
5310 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5312 if (indx
== (bfd_size_type
) -1
5313 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5316 if (info
->new_dtags
)
5318 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5319 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5324 if (filter_shlib
!= NULL
)
5328 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5329 filter_shlib
, TRUE
);
5330 if (indx
== (bfd_size_type
) -1
5331 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5335 if (auxiliary_filters
!= NULL
)
5337 const char * const *p
;
5339 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5343 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5345 if (indx
== (bfd_size_type
) -1
5346 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5352 eif
.verdefs
= verdefs
;
5355 /* If we are supposed to export all symbols into the dynamic symbol
5356 table (this is not the normal case), then do so. */
5357 if (info
->export_dynamic
5358 || (info
->executable
&& info
->dynamic
))
5360 elf_link_hash_traverse (elf_hash_table (info
),
5361 _bfd_elf_export_symbol
,
5367 /* Make all global versions with definition. */
5368 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5369 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5370 if (!d
->symver
&& d
->symbol
)
5372 const char *verstr
, *name
;
5373 size_t namelen
, verlen
, newlen
;
5375 struct elf_link_hash_entry
*newh
;
5378 namelen
= strlen (name
);
5380 verlen
= strlen (verstr
);
5381 newlen
= namelen
+ verlen
+ 3;
5383 newname
= bfd_malloc (newlen
);
5384 if (newname
== NULL
)
5386 memcpy (newname
, name
, namelen
);
5388 /* Check the hidden versioned definition. */
5389 p
= newname
+ namelen
;
5391 memcpy (p
, verstr
, verlen
+ 1);
5392 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5393 newname
, FALSE
, FALSE
,
5396 || (newh
->root
.type
!= bfd_link_hash_defined
5397 && newh
->root
.type
!= bfd_link_hash_defweak
))
5399 /* Check the default versioned definition. */
5401 memcpy (p
, verstr
, verlen
+ 1);
5402 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5403 newname
, FALSE
, FALSE
,
5408 /* Mark this version if there is a definition and it is
5409 not defined in a shared object. */
5411 && !newh
->def_dynamic
5412 && (newh
->root
.type
== bfd_link_hash_defined
5413 || newh
->root
.type
== bfd_link_hash_defweak
))
5417 /* Attach all the symbols to their version information. */
5418 asvinfo
.output_bfd
= output_bfd
;
5419 asvinfo
.info
= info
;
5420 asvinfo
.verdefs
= verdefs
;
5421 asvinfo
.failed
= FALSE
;
5423 elf_link_hash_traverse (elf_hash_table (info
),
5424 _bfd_elf_link_assign_sym_version
,
5429 if (!info
->allow_undefined_version
)
5431 /* Check if all global versions have a definition. */
5433 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5434 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5435 if (!d
->symver
&& !d
->script
)
5437 (*_bfd_error_handler
)
5438 (_("%s: undefined version: %s"),
5439 d
->pattern
, t
->name
);
5440 all_defined
= FALSE
;
5445 bfd_set_error (bfd_error_bad_value
);
5450 /* Find all symbols which were defined in a dynamic object and make
5451 the backend pick a reasonable value for them. */
5452 elf_link_hash_traverse (elf_hash_table (info
),
5453 _bfd_elf_adjust_dynamic_symbol
,
5458 /* Add some entries to the .dynamic section. We fill in some of the
5459 values later, in bfd_elf_final_link, but we must add the entries
5460 now so that we know the final size of the .dynamic section. */
5462 /* If there are initialization and/or finalization functions to
5463 call then add the corresponding DT_INIT/DT_FINI entries. */
5464 h
= (info
->init_function
5465 ? elf_link_hash_lookup (elf_hash_table (info
),
5466 info
->init_function
, FALSE
,
5473 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5476 h
= (info
->fini_function
5477 ? elf_link_hash_lookup (elf_hash_table (info
),
5478 info
->fini_function
, FALSE
,
5485 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5489 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5490 if (s
!= NULL
&& s
->linker_has_input
)
5492 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5493 if (! info
->executable
)
5498 for (sub
= info
->input_bfds
; sub
!= NULL
;
5499 sub
= sub
->link_next
)
5500 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5501 if (elf_section_data (o
)->this_hdr
.sh_type
5502 == SHT_PREINIT_ARRAY
)
5504 (*_bfd_error_handler
)
5505 (_("%B: .preinit_array section is not allowed in DSO"),
5510 bfd_set_error (bfd_error_nonrepresentable_section
);
5514 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5515 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5518 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5519 if (s
!= NULL
&& s
->linker_has_input
)
5521 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5522 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5525 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5526 if (s
!= NULL
&& s
->linker_has_input
)
5528 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5529 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5533 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5534 /* If .dynstr is excluded from the link, we don't want any of
5535 these tags. Strictly, we should be checking each section
5536 individually; This quick check covers for the case where
5537 someone does a /DISCARD/ : { *(*) }. */
5538 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5540 bfd_size_type strsize
;
5542 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5543 if ((info
->emit_hash
5544 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5545 || (info
->emit_gnu_hash
5546 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5547 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5548 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5549 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5550 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5551 bed
->s
->sizeof_sym
))
5556 /* The backend must work out the sizes of all the other dynamic
5558 if (bed
->elf_backend_size_dynamic_sections
5559 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5562 if (elf_hash_table (info
)->dynamic_sections_created
)
5564 unsigned long section_sym_count
;
5567 /* Set up the version definition section. */
5568 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5569 BFD_ASSERT (s
!= NULL
);
5571 /* We may have created additional version definitions if we are
5572 just linking a regular application. */
5573 verdefs
= asvinfo
.verdefs
;
5575 /* Skip anonymous version tag. */
5576 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5577 verdefs
= verdefs
->next
;
5579 if (verdefs
== NULL
&& !info
->create_default_symver
)
5580 s
->flags
|= SEC_EXCLUDE
;
5585 struct bfd_elf_version_tree
*t
;
5587 Elf_Internal_Verdef def
;
5588 Elf_Internal_Verdaux defaux
;
5589 struct bfd_link_hash_entry
*bh
;
5590 struct elf_link_hash_entry
*h
;
5596 /* Make space for the base version. */
5597 size
+= sizeof (Elf_External_Verdef
);
5598 size
+= sizeof (Elf_External_Verdaux
);
5601 /* Make space for the default version. */
5602 if (info
->create_default_symver
)
5604 size
+= sizeof (Elf_External_Verdef
);
5608 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5610 struct bfd_elf_version_deps
*n
;
5612 size
+= sizeof (Elf_External_Verdef
);
5613 size
+= sizeof (Elf_External_Verdaux
);
5616 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5617 size
+= sizeof (Elf_External_Verdaux
);
5621 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5622 if (s
->contents
== NULL
&& s
->size
!= 0)
5625 /* Fill in the version definition section. */
5629 def
.vd_version
= VER_DEF_CURRENT
;
5630 def
.vd_flags
= VER_FLG_BASE
;
5633 if (info
->create_default_symver
)
5635 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5636 def
.vd_next
= sizeof (Elf_External_Verdef
);
5640 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5641 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5642 + sizeof (Elf_External_Verdaux
));
5645 if (soname_indx
!= (bfd_size_type
) -1)
5647 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5649 def
.vd_hash
= bfd_elf_hash (soname
);
5650 defaux
.vda_name
= soname_indx
;
5657 name
= lbasename (output_bfd
->filename
);
5658 def
.vd_hash
= bfd_elf_hash (name
);
5659 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5661 if (indx
== (bfd_size_type
) -1)
5663 defaux
.vda_name
= indx
;
5665 defaux
.vda_next
= 0;
5667 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5668 (Elf_External_Verdef
*) p
);
5669 p
+= sizeof (Elf_External_Verdef
);
5670 if (info
->create_default_symver
)
5672 /* Add a symbol representing this version. */
5674 if (! (_bfd_generic_link_add_one_symbol
5675 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5677 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5679 h
= (struct elf_link_hash_entry
*) bh
;
5682 h
->type
= STT_OBJECT
;
5683 h
->verinfo
.vertree
= NULL
;
5685 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5688 /* Create a duplicate of the base version with the same
5689 aux block, but different flags. */
5692 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5694 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5695 + sizeof (Elf_External_Verdaux
));
5698 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5699 (Elf_External_Verdef
*) p
);
5700 p
+= sizeof (Elf_External_Verdef
);
5702 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5703 (Elf_External_Verdaux
*) p
);
5704 p
+= sizeof (Elf_External_Verdaux
);
5706 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5709 struct bfd_elf_version_deps
*n
;
5712 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5715 /* Add a symbol representing this version. */
5717 if (! (_bfd_generic_link_add_one_symbol
5718 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5720 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5722 h
= (struct elf_link_hash_entry
*) bh
;
5725 h
->type
= STT_OBJECT
;
5726 h
->verinfo
.vertree
= t
;
5728 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5731 def
.vd_version
= VER_DEF_CURRENT
;
5733 if (t
->globals
.list
== NULL
5734 && t
->locals
.list
== NULL
5736 def
.vd_flags
|= VER_FLG_WEAK
;
5737 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5738 def
.vd_cnt
= cdeps
+ 1;
5739 def
.vd_hash
= bfd_elf_hash (t
->name
);
5740 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5742 if (t
->next
!= NULL
)
5743 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5744 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5746 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5747 (Elf_External_Verdef
*) p
);
5748 p
+= sizeof (Elf_External_Verdef
);
5750 defaux
.vda_name
= h
->dynstr_index
;
5751 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5753 defaux
.vda_next
= 0;
5754 if (t
->deps
!= NULL
)
5755 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5756 t
->name_indx
= defaux
.vda_name
;
5758 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5759 (Elf_External_Verdaux
*) p
);
5760 p
+= sizeof (Elf_External_Verdaux
);
5762 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5764 if (n
->version_needed
== NULL
)
5766 /* This can happen if there was an error in the
5768 defaux
.vda_name
= 0;
5772 defaux
.vda_name
= n
->version_needed
->name_indx
;
5773 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5776 if (n
->next
== NULL
)
5777 defaux
.vda_next
= 0;
5779 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5781 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5782 (Elf_External_Verdaux
*) p
);
5783 p
+= sizeof (Elf_External_Verdaux
);
5787 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5788 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5791 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5794 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5796 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5799 else if (info
->flags
& DF_BIND_NOW
)
5801 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5807 if (info
->executable
)
5808 info
->flags_1
&= ~ (DF_1_INITFIRST
5811 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5815 /* Work out the size of the version reference section. */
5817 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5818 BFD_ASSERT (s
!= NULL
);
5820 struct elf_find_verdep_info sinfo
;
5822 sinfo
.output_bfd
= output_bfd
;
5824 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5825 if (sinfo
.vers
== 0)
5827 sinfo
.failed
= FALSE
;
5829 elf_link_hash_traverse (elf_hash_table (info
),
5830 _bfd_elf_link_find_version_dependencies
,
5833 if (elf_tdata (output_bfd
)->verref
== NULL
)
5834 s
->flags
|= SEC_EXCLUDE
;
5837 Elf_Internal_Verneed
*t
;
5842 /* Build the version definition section. */
5845 for (t
= elf_tdata (output_bfd
)->verref
;
5849 Elf_Internal_Vernaux
*a
;
5851 size
+= sizeof (Elf_External_Verneed
);
5853 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5854 size
+= sizeof (Elf_External_Vernaux
);
5858 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5859 if (s
->contents
== NULL
)
5863 for (t
= elf_tdata (output_bfd
)->verref
;
5868 Elf_Internal_Vernaux
*a
;
5872 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5875 t
->vn_version
= VER_NEED_CURRENT
;
5877 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5878 elf_dt_name (t
->vn_bfd
) != NULL
5879 ? elf_dt_name (t
->vn_bfd
)
5880 : lbasename (t
->vn_bfd
->filename
),
5882 if (indx
== (bfd_size_type
) -1)
5885 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5886 if (t
->vn_nextref
== NULL
)
5889 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5890 + caux
* sizeof (Elf_External_Vernaux
));
5892 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5893 (Elf_External_Verneed
*) p
);
5894 p
+= sizeof (Elf_External_Verneed
);
5896 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5898 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5899 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5900 a
->vna_nodename
, FALSE
);
5901 if (indx
== (bfd_size_type
) -1)
5904 if (a
->vna_nextptr
== NULL
)
5907 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5909 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5910 (Elf_External_Vernaux
*) p
);
5911 p
+= sizeof (Elf_External_Vernaux
);
5915 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5916 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5919 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5923 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5924 && elf_tdata (output_bfd
)->cverdefs
== 0)
5925 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5926 §ion_sym_count
) == 0)
5928 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5929 s
->flags
|= SEC_EXCLUDE
;
5936 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5938 if (!is_elf_hash_table (info
->hash
))
5941 if (elf_hash_table (info
)->dynamic_sections_created
)
5944 const struct elf_backend_data
*bed
;
5946 bfd_size_type dynsymcount
;
5947 unsigned long section_sym_count
;
5948 unsigned int dtagcount
;
5950 dynobj
= elf_hash_table (info
)->dynobj
;
5952 /* Assign dynsym indicies. In a shared library we generate a
5953 section symbol for each output section, which come first.
5954 Next come all of the back-end allocated local dynamic syms,
5955 followed by the rest of the global symbols. */
5957 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5958 §ion_sym_count
);
5960 /* Work out the size of the symbol version section. */
5961 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5962 BFD_ASSERT (s
!= NULL
);
5963 if (dynsymcount
!= 0
5964 && (s
->flags
& SEC_EXCLUDE
) == 0)
5966 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5967 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5968 if (s
->contents
== NULL
)
5971 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5975 /* Set the size of the .dynsym and .hash sections. We counted
5976 the number of dynamic symbols in elf_link_add_object_symbols.
5977 We will build the contents of .dynsym and .hash when we build
5978 the final symbol table, because until then we do not know the
5979 correct value to give the symbols. We built the .dynstr
5980 section as we went along in elf_link_add_object_symbols. */
5981 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5982 BFD_ASSERT (s
!= NULL
);
5983 bed
= get_elf_backend_data (output_bfd
);
5984 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5986 if (dynsymcount
!= 0)
5988 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5989 if (s
->contents
== NULL
)
5992 /* The first entry in .dynsym is a dummy symbol.
5993 Clear all the section syms, in case we don't output them all. */
5994 ++section_sym_count
;
5995 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5998 elf_hash_table (info
)->bucketcount
= 0;
6000 /* Compute the size of the hashing table. As a side effect this
6001 computes the hash values for all the names we export. */
6002 if (info
->emit_hash
)
6004 unsigned long int *hashcodes
;
6005 unsigned long int *hashcodesp
;
6007 unsigned long int nsyms
;
6009 size_t hash_entry_size
;
6011 /* Compute the hash values for all exported symbols. At the same
6012 time store the values in an array so that we could use them for
6014 amt
= dynsymcount
* sizeof (unsigned long int);
6015 hashcodes
= bfd_malloc (amt
);
6016 if (hashcodes
== NULL
)
6018 hashcodesp
= hashcodes
;
6020 /* Put all hash values in HASHCODES. */
6021 elf_link_hash_traverse (elf_hash_table (info
),
6022 elf_collect_hash_codes
, &hashcodesp
);
6024 nsyms
= hashcodesp
- hashcodes
;
6026 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6029 if (bucketcount
== 0)
6032 elf_hash_table (info
)->bucketcount
= bucketcount
;
6034 s
= bfd_get_section_by_name (dynobj
, ".hash");
6035 BFD_ASSERT (s
!= NULL
);
6036 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6037 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6038 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6039 if (s
->contents
== NULL
)
6042 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6043 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6044 s
->contents
+ hash_entry_size
);
6047 if (info
->emit_gnu_hash
)
6050 unsigned char *contents
;
6051 struct collect_gnu_hash_codes cinfo
;
6055 memset (&cinfo
, 0, sizeof (cinfo
));
6057 /* Compute the hash values for all exported symbols. At the same
6058 time store the values in an array so that we could use them for
6060 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6061 cinfo
.hashcodes
= bfd_malloc (amt
);
6062 if (cinfo
.hashcodes
== NULL
)
6065 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6066 cinfo
.min_dynindx
= -1;
6067 cinfo
.output_bfd
= output_bfd
;
6070 /* Put all hash values in HASHCODES. */
6071 elf_link_hash_traverse (elf_hash_table (info
),
6072 elf_collect_gnu_hash_codes
, &cinfo
);
6075 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6077 if (bucketcount
== 0)
6079 free (cinfo
.hashcodes
);
6083 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6084 BFD_ASSERT (s
!= NULL
);
6086 if (cinfo
.nsyms
== 0)
6088 /* Empty .gnu.hash section is special. */
6089 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6090 free (cinfo
.hashcodes
);
6091 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6092 contents
= bfd_zalloc (output_bfd
, s
->size
);
6093 if (contents
== NULL
)
6095 s
->contents
= contents
;
6096 /* 1 empty bucket. */
6097 bfd_put_32 (output_bfd
, 1, contents
);
6098 /* SYMIDX above the special symbol 0. */
6099 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6100 /* Just one word for bitmask. */
6101 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6102 /* Only hash fn bloom filter. */
6103 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6104 /* No hashes are valid - empty bitmask. */
6105 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6106 /* No hashes in the only bucket. */
6107 bfd_put_32 (output_bfd
, 0,
6108 contents
+ 16 + bed
->s
->arch_size
/ 8);
6112 unsigned long int maskwords
, maskbitslog2
;
6113 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6115 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6116 if (maskbitslog2
< 3)
6118 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6119 maskbitslog2
= maskbitslog2
+ 3;
6121 maskbitslog2
= maskbitslog2
+ 2;
6122 if (bed
->s
->arch_size
== 64)
6124 if (maskbitslog2
== 5)
6130 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6131 cinfo
.shift2
= maskbitslog2
;
6132 cinfo
.maskbits
= 1 << maskbitslog2
;
6133 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6134 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6135 amt
+= maskwords
* sizeof (bfd_vma
);
6136 cinfo
.bitmask
= bfd_malloc (amt
);
6137 if (cinfo
.bitmask
== NULL
)
6139 free (cinfo
.hashcodes
);
6143 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6144 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6145 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6146 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6148 /* Determine how often each hash bucket is used. */
6149 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6150 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6151 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6153 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6154 if (cinfo
.counts
[i
] != 0)
6156 cinfo
.indx
[i
] = cnt
;
6157 cnt
+= cinfo
.counts
[i
];
6159 BFD_ASSERT (cnt
== dynsymcount
);
6160 cinfo
.bucketcount
= bucketcount
;
6161 cinfo
.local_indx
= cinfo
.min_dynindx
;
6163 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6164 s
->size
+= cinfo
.maskbits
/ 8;
6165 contents
= bfd_zalloc (output_bfd
, s
->size
);
6166 if (contents
== NULL
)
6168 free (cinfo
.bitmask
);
6169 free (cinfo
.hashcodes
);
6173 s
->contents
= contents
;
6174 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6175 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6176 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6177 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6178 contents
+= 16 + cinfo
.maskbits
/ 8;
6180 for (i
= 0; i
< bucketcount
; ++i
)
6182 if (cinfo
.counts
[i
] == 0)
6183 bfd_put_32 (output_bfd
, 0, contents
);
6185 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6189 cinfo
.contents
= contents
;
6191 /* Renumber dynamic symbols, populate .gnu.hash section. */
6192 elf_link_hash_traverse (elf_hash_table (info
),
6193 elf_renumber_gnu_hash_syms
, &cinfo
);
6195 contents
= s
->contents
+ 16;
6196 for (i
= 0; i
< maskwords
; ++i
)
6198 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6200 contents
+= bed
->s
->arch_size
/ 8;
6203 free (cinfo
.bitmask
);
6204 free (cinfo
.hashcodes
);
6208 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6209 BFD_ASSERT (s
!= NULL
);
6211 elf_finalize_dynstr (output_bfd
, info
);
6213 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6215 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6216 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6223 /* Final phase of ELF linker. */
6225 /* A structure we use to avoid passing large numbers of arguments. */
6227 struct elf_final_link_info
6229 /* General link information. */
6230 struct bfd_link_info
*info
;
6233 /* Symbol string table. */
6234 struct bfd_strtab_hash
*symstrtab
;
6235 /* .dynsym section. */
6236 asection
*dynsym_sec
;
6237 /* .hash section. */
6239 /* symbol version section (.gnu.version). */
6240 asection
*symver_sec
;
6241 /* Buffer large enough to hold contents of any section. */
6243 /* Buffer large enough to hold external relocs of any section. */
6244 void *external_relocs
;
6245 /* Buffer large enough to hold internal relocs of any section. */
6246 Elf_Internal_Rela
*internal_relocs
;
6247 /* Buffer large enough to hold external local symbols of any input
6249 bfd_byte
*external_syms
;
6250 /* And a buffer for symbol section indices. */
6251 Elf_External_Sym_Shndx
*locsym_shndx
;
6252 /* Buffer large enough to hold internal local symbols of any input
6254 Elf_Internal_Sym
*internal_syms
;
6255 /* Array large enough to hold a symbol index for each local symbol
6256 of any input BFD. */
6258 /* Array large enough to hold a section pointer for each local
6259 symbol of any input BFD. */
6260 asection
**sections
;
6261 /* Buffer to hold swapped out symbols. */
6263 /* And one for symbol section indices. */
6264 Elf_External_Sym_Shndx
*symshndxbuf
;
6265 /* Number of swapped out symbols in buffer. */
6266 size_t symbuf_count
;
6267 /* Number of symbols which fit in symbuf. */
6269 /* And same for symshndxbuf. */
6270 size_t shndxbuf_size
;
6273 /* This struct is used to pass information to elf_link_output_extsym. */
6275 struct elf_outext_info
6278 bfd_boolean localsyms
;
6279 struct elf_final_link_info
*finfo
;
6282 /* When performing a relocatable link, the input relocations are
6283 preserved. But, if they reference global symbols, the indices
6284 referenced must be updated. Update all the relocations in
6285 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
6288 elf_link_adjust_relocs (bfd
*abfd
,
6289 Elf_Internal_Shdr
*rel_hdr
,
6291 struct elf_link_hash_entry
**rel_hash
)
6294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6296 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6297 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6298 bfd_vma r_type_mask
;
6301 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
6303 swap_in
= bed
->s
->swap_reloc_in
;
6304 swap_out
= bed
->s
->swap_reloc_out
;
6306 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
6308 swap_in
= bed
->s
->swap_reloca_in
;
6309 swap_out
= bed
->s
->swap_reloca_out
;
6314 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
6317 if (bed
->s
->arch_size
== 32)
6324 r_type_mask
= 0xffffffff;
6328 erela
= rel_hdr
->contents
;
6329 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
6331 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
6334 if (*rel_hash
== NULL
)
6337 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
6339 (*swap_in
) (abfd
, erela
, irela
);
6340 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
6341 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
6342 | (irela
[j
].r_info
& r_type_mask
));
6343 (*swap_out
) (abfd
, irela
, erela
);
6347 struct elf_link_sort_rela
6353 enum elf_reloc_type_class type
;
6354 /* We use this as an array of size int_rels_per_ext_rel. */
6355 Elf_Internal_Rela rela
[1];
6359 elf_link_sort_cmp1 (const void *A
, const void *B
)
6361 const struct elf_link_sort_rela
*a
= A
;
6362 const struct elf_link_sort_rela
*b
= B
;
6363 int relativea
, relativeb
;
6365 relativea
= a
->type
== reloc_class_relative
;
6366 relativeb
= b
->type
== reloc_class_relative
;
6368 if (relativea
< relativeb
)
6370 if (relativea
> relativeb
)
6372 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
6374 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
6376 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6378 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6384 elf_link_sort_cmp2 (const void *A
, const void *B
)
6386 const struct elf_link_sort_rela
*a
= A
;
6387 const struct elf_link_sort_rela
*b
= B
;
6390 if (a
->u
.offset
< b
->u
.offset
)
6392 if (a
->u
.offset
> b
->u
.offset
)
6394 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
6395 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
6400 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6402 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6408 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
6411 bfd_size_type count
, size
;
6412 size_t i
, ret
, sort_elt
, ext_size
;
6413 bfd_byte
*sort
, *s_non_relative
, *p
;
6414 struct elf_link_sort_rela
*sq
;
6415 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6416 int i2e
= bed
->s
->int_rels_per_ext_rel
;
6417 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6418 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6419 struct bfd_link_order
*lo
;
6422 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
6423 if (reldyn
== NULL
|| reldyn
->size
== 0)
6425 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
6426 if (reldyn
== NULL
|| reldyn
->size
== 0)
6428 ext_size
= bed
->s
->sizeof_rel
;
6429 swap_in
= bed
->s
->swap_reloc_in
;
6430 swap_out
= bed
->s
->swap_reloc_out
;
6434 ext_size
= bed
->s
->sizeof_rela
;
6435 swap_in
= bed
->s
->swap_reloca_in
;
6436 swap_out
= bed
->s
->swap_reloca_out
;
6438 count
= reldyn
->size
/ ext_size
;
6441 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6442 if (lo
->type
== bfd_indirect_link_order
)
6444 asection
*o
= lo
->u
.indirect
.section
;
6448 if (size
!= reldyn
->size
)
6451 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6452 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6453 sort
= bfd_zmalloc (sort_elt
* count
);
6456 (*info
->callbacks
->warning
)
6457 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6461 if (bed
->s
->arch_size
== 32)
6462 r_sym_mask
= ~(bfd_vma
) 0xff;
6464 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6466 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6467 if (lo
->type
== bfd_indirect_link_order
)
6469 bfd_byte
*erel
, *erelend
;
6470 asection
*o
= lo
->u
.indirect
.section
;
6472 if (o
->contents
== NULL
&& o
->size
!= 0)
6474 /* This is a reloc section that is being handled as a normal
6475 section. See bfd_section_from_shdr. We can't combine
6476 relocs in this case. */
6481 erelend
= o
->contents
+ o
->size
;
6482 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6483 while (erel
< erelend
)
6485 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6486 (*swap_in
) (abfd
, erel
, s
->rela
);
6487 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6488 s
->u
.sym_mask
= r_sym_mask
;
6494 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6496 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6498 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6499 if (s
->type
!= reloc_class_relative
)
6505 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6506 for (; i
< count
; i
++, p
+= sort_elt
)
6508 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6509 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6511 sp
->u
.offset
= sq
->rela
->r_offset
;
6514 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6516 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6517 if (lo
->type
== bfd_indirect_link_order
)
6519 bfd_byte
*erel
, *erelend
;
6520 asection
*o
= lo
->u
.indirect
.section
;
6523 erelend
= o
->contents
+ o
->size
;
6524 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6525 while (erel
< erelend
)
6527 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6528 (*swap_out
) (abfd
, s
->rela
, erel
);
6539 /* Flush the output symbols to the file. */
6542 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6543 const struct elf_backend_data
*bed
)
6545 if (finfo
->symbuf_count
> 0)
6547 Elf_Internal_Shdr
*hdr
;
6551 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6552 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6553 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6554 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6555 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6558 hdr
->sh_size
+= amt
;
6559 finfo
->symbuf_count
= 0;
6565 /* Add a symbol to the output symbol table. */
6568 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6570 Elf_Internal_Sym
*elfsym
,
6571 asection
*input_sec
,
6572 struct elf_link_hash_entry
*h
)
6575 Elf_External_Sym_Shndx
*destshndx
;
6576 bfd_boolean (*output_symbol_hook
)
6577 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6578 struct elf_link_hash_entry
*);
6579 const struct elf_backend_data
*bed
;
6581 bed
= get_elf_backend_data (finfo
->output_bfd
);
6582 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6583 if (output_symbol_hook
!= NULL
)
6585 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6589 if (name
== NULL
|| *name
== '\0')
6590 elfsym
->st_name
= 0;
6591 else if (input_sec
->flags
& SEC_EXCLUDE
)
6592 elfsym
->st_name
= 0;
6595 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6597 if (elfsym
->st_name
== (unsigned long) -1)
6601 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6603 if (! elf_link_flush_output_syms (finfo
, bed
))
6607 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6608 destshndx
= finfo
->symshndxbuf
;
6609 if (destshndx
!= NULL
)
6611 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6615 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6616 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6617 if (destshndx
== NULL
)
6619 memset ((char *) destshndx
+ amt
, 0, amt
);
6620 finfo
->shndxbuf_size
*= 2;
6622 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6625 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6626 finfo
->symbuf_count
+= 1;
6627 bfd_get_symcount (finfo
->output_bfd
) += 1;
6632 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6635 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
6637 if (sym
->st_shndx
> SHN_HIRESERVE
)
6639 /* The gABI doesn't support dynamic symbols in output sections
6641 (*_bfd_error_handler
)
6642 (_("%B: Too many sections: %d (>= %d)"),
6643 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
6644 bfd_set_error (bfd_error_nonrepresentable_section
);
6650 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6651 allowing an unsatisfied unversioned symbol in the DSO to match a
6652 versioned symbol that would normally require an explicit version.
6653 We also handle the case that a DSO references a hidden symbol
6654 which may be satisfied by a versioned symbol in another DSO. */
6657 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6658 const struct elf_backend_data
*bed
,
6659 struct elf_link_hash_entry
*h
)
6662 struct elf_link_loaded_list
*loaded
;
6664 if (!is_elf_hash_table (info
->hash
))
6667 switch (h
->root
.type
)
6673 case bfd_link_hash_undefined
:
6674 case bfd_link_hash_undefweak
:
6675 abfd
= h
->root
.u
.undef
.abfd
;
6676 if ((abfd
->flags
& DYNAMIC
) == 0
6677 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6681 case bfd_link_hash_defined
:
6682 case bfd_link_hash_defweak
:
6683 abfd
= h
->root
.u
.def
.section
->owner
;
6686 case bfd_link_hash_common
:
6687 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6690 BFD_ASSERT (abfd
!= NULL
);
6692 for (loaded
= elf_hash_table (info
)->loaded
;
6694 loaded
= loaded
->next
)
6697 Elf_Internal_Shdr
*hdr
;
6698 bfd_size_type symcount
;
6699 bfd_size_type extsymcount
;
6700 bfd_size_type extsymoff
;
6701 Elf_Internal_Shdr
*versymhdr
;
6702 Elf_Internal_Sym
*isym
;
6703 Elf_Internal_Sym
*isymend
;
6704 Elf_Internal_Sym
*isymbuf
;
6705 Elf_External_Versym
*ever
;
6706 Elf_External_Versym
*extversym
;
6708 input
= loaded
->abfd
;
6710 /* We check each DSO for a possible hidden versioned definition. */
6712 || (input
->flags
& DYNAMIC
) == 0
6713 || elf_dynversym (input
) == 0)
6716 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6718 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6719 if (elf_bad_symtab (input
))
6721 extsymcount
= symcount
;
6726 extsymcount
= symcount
- hdr
->sh_info
;
6727 extsymoff
= hdr
->sh_info
;
6730 if (extsymcount
== 0)
6733 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6735 if (isymbuf
== NULL
)
6738 /* Read in any version definitions. */
6739 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6740 extversym
= bfd_malloc (versymhdr
->sh_size
);
6741 if (extversym
== NULL
)
6744 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6745 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6746 != versymhdr
->sh_size
))
6754 ever
= extversym
+ extsymoff
;
6755 isymend
= isymbuf
+ extsymcount
;
6756 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6759 Elf_Internal_Versym iver
;
6760 unsigned short version_index
;
6762 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6763 || isym
->st_shndx
== SHN_UNDEF
)
6766 name
= bfd_elf_string_from_elf_section (input
,
6769 if (strcmp (name
, h
->root
.root
.string
) != 0)
6772 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6774 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6776 /* If we have a non-hidden versioned sym, then it should
6777 have provided a definition for the undefined sym. */
6781 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6782 if (version_index
== 1 || version_index
== 2)
6784 /* This is the base or first version. We can use it. */
6798 /* Add an external symbol to the symbol table. This is called from
6799 the hash table traversal routine. When generating a shared object,
6800 we go through the symbol table twice. The first time we output
6801 anything that might have been forced to local scope in a version
6802 script. The second time we output the symbols that are still
6806 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6808 struct elf_outext_info
*eoinfo
= data
;
6809 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6811 Elf_Internal_Sym sym
;
6812 asection
*input_sec
;
6813 const struct elf_backend_data
*bed
;
6815 if (h
->root
.type
== bfd_link_hash_warning
)
6817 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6818 if (h
->root
.type
== bfd_link_hash_new
)
6822 /* Decide whether to output this symbol in this pass. */
6823 if (eoinfo
->localsyms
)
6825 if (!h
->forced_local
)
6830 if (h
->forced_local
)
6834 bed
= get_elf_backend_data (finfo
->output_bfd
);
6836 if (h
->root
.type
== bfd_link_hash_undefined
)
6838 /* If we have an undefined symbol reference here then it must have
6839 come from a shared library that is being linked in. (Undefined
6840 references in regular files have already been handled). */
6841 bfd_boolean ignore_undef
= FALSE
;
6843 /* Some symbols may be special in that the fact that they're
6844 undefined can be safely ignored - let backend determine that. */
6845 if (bed
->elf_backend_ignore_undef_symbol
)
6846 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6848 /* If we are reporting errors for this situation then do so now. */
6849 if (ignore_undef
== FALSE
6852 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6853 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6855 if (! (finfo
->info
->callbacks
->undefined_symbol
6856 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6857 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6859 eoinfo
->failed
= TRUE
;
6865 /* We should also warn if a forced local symbol is referenced from
6866 shared libraries. */
6867 if (! finfo
->info
->relocatable
6868 && (! finfo
->info
->shared
)
6873 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6875 (*_bfd_error_handler
)
6876 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6878 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6879 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6880 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6882 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6883 ? "hidden" : "local",
6884 h
->root
.root
.string
);
6885 eoinfo
->failed
= TRUE
;
6889 /* We don't want to output symbols that have never been mentioned by
6890 a regular file, or that we have been told to strip. However, if
6891 h->indx is set to -2, the symbol is used by a reloc and we must
6895 else if ((h
->def_dynamic
6897 || h
->root
.type
== bfd_link_hash_new
)
6901 else if (finfo
->info
->strip
== strip_all
)
6903 else if (finfo
->info
->strip
== strip_some
6904 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6905 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6907 else if (finfo
->info
->strip_discarded
6908 && (h
->root
.type
== bfd_link_hash_defined
6909 || h
->root
.type
== bfd_link_hash_defweak
)
6910 && elf_discarded_section (h
->root
.u
.def
.section
))
6915 /* If we're stripping it, and it's not a dynamic symbol, there's
6916 nothing else to do unless it is a forced local symbol. */
6919 && !h
->forced_local
)
6923 sym
.st_size
= h
->size
;
6924 sym
.st_other
= h
->other
;
6925 if (h
->forced_local
)
6926 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6927 else if (h
->root
.type
== bfd_link_hash_undefweak
6928 || h
->root
.type
== bfd_link_hash_defweak
)
6929 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6931 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6933 switch (h
->root
.type
)
6936 case bfd_link_hash_new
:
6937 case bfd_link_hash_warning
:
6941 case bfd_link_hash_undefined
:
6942 case bfd_link_hash_undefweak
:
6943 input_sec
= bfd_und_section_ptr
;
6944 sym
.st_shndx
= SHN_UNDEF
;
6947 case bfd_link_hash_defined
:
6948 case bfd_link_hash_defweak
:
6950 input_sec
= h
->root
.u
.def
.section
;
6951 if (input_sec
->output_section
!= NULL
)
6954 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6955 input_sec
->output_section
);
6956 if (sym
.st_shndx
== SHN_BAD
)
6958 (*_bfd_error_handler
)
6959 (_("%B: could not find output section %A for input section %A"),
6960 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6961 eoinfo
->failed
= TRUE
;
6965 /* ELF symbols in relocatable files are section relative,
6966 but in nonrelocatable files they are virtual
6968 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6969 if (! finfo
->info
->relocatable
)
6971 sym
.st_value
+= input_sec
->output_section
->vma
;
6972 if (h
->type
== STT_TLS
)
6974 /* STT_TLS symbols are relative to PT_TLS segment
6976 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6977 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6983 BFD_ASSERT (input_sec
->owner
== NULL
6984 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6985 sym
.st_shndx
= SHN_UNDEF
;
6986 input_sec
= bfd_und_section_ptr
;
6991 case bfd_link_hash_common
:
6992 input_sec
= h
->root
.u
.c
.p
->section
;
6993 sym
.st_shndx
= bed
->common_section_index (input_sec
);
6994 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6997 case bfd_link_hash_indirect
:
6998 /* These symbols are created by symbol versioning. They point
6999 to the decorated version of the name. For example, if the
7000 symbol foo@@GNU_1.2 is the default, which should be used when
7001 foo is used with no version, then we add an indirect symbol
7002 foo which points to foo@@GNU_1.2. We ignore these symbols,
7003 since the indirected symbol is already in the hash table. */
7007 /* Give the processor backend a chance to tweak the symbol value,
7008 and also to finish up anything that needs to be done for this
7009 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7010 forced local syms when non-shared is due to a historical quirk. */
7011 if ((h
->dynindx
!= -1
7013 && ((finfo
->info
->shared
7014 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7015 || h
->root
.type
!= bfd_link_hash_undefweak
))
7016 || !h
->forced_local
)
7017 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7019 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7020 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7022 eoinfo
->failed
= TRUE
;
7027 /* If we are marking the symbol as undefined, and there are no
7028 non-weak references to this symbol from a regular object, then
7029 mark the symbol as weak undefined; if there are non-weak
7030 references, mark the symbol as strong. We can't do this earlier,
7031 because it might not be marked as undefined until the
7032 finish_dynamic_symbol routine gets through with it. */
7033 if (sym
.st_shndx
== SHN_UNDEF
7035 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7036 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7040 if (h
->ref_regular_nonweak
)
7041 bindtype
= STB_GLOBAL
;
7043 bindtype
= STB_WEAK
;
7044 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7047 /* If a non-weak symbol with non-default visibility is not defined
7048 locally, it is a fatal error. */
7049 if (! finfo
->info
->relocatable
7050 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7051 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7052 && h
->root
.type
== bfd_link_hash_undefined
7055 (*_bfd_error_handler
)
7056 (_("%B: %s symbol `%s' isn't defined"),
7058 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7060 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7061 ? "internal" : "hidden",
7062 h
->root
.root
.string
);
7063 eoinfo
->failed
= TRUE
;
7067 /* If this symbol should be put in the .dynsym section, then put it
7068 there now. We already know the symbol index. We also fill in
7069 the entry in the .hash section. */
7070 if (h
->dynindx
!= -1
7071 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7075 sym
.st_name
= h
->dynstr_index
;
7076 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7077 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7079 eoinfo
->failed
= TRUE
;
7082 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7084 if (finfo
->hash_sec
!= NULL
)
7086 size_t hash_entry_size
;
7087 bfd_byte
*bucketpos
;
7092 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7093 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7096 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7097 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7098 + (bucket
+ 2) * hash_entry_size
);
7099 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7100 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7101 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7102 ((bfd_byte
*) finfo
->hash_sec
->contents
7103 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7106 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7108 Elf_Internal_Versym iversym
;
7109 Elf_External_Versym
*eversym
;
7111 if (!h
->def_regular
)
7113 if (h
->verinfo
.verdef
== NULL
)
7114 iversym
.vs_vers
= 0;
7116 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7120 if (h
->verinfo
.vertree
== NULL
)
7121 iversym
.vs_vers
= 1;
7123 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7124 if (finfo
->info
->create_default_symver
)
7129 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7131 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7132 eversym
+= h
->dynindx
;
7133 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7137 /* If we're stripping it, then it was just a dynamic symbol, and
7138 there's nothing else to do. */
7139 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7142 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7144 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7146 eoinfo
->failed
= TRUE
;
7153 /* Return TRUE if special handling is done for relocs in SEC against
7154 symbols defined in discarded sections. */
7157 elf_section_ignore_discarded_relocs (asection
*sec
)
7159 const struct elf_backend_data
*bed
;
7161 switch (sec
->sec_info_type
)
7163 case ELF_INFO_TYPE_STABS
:
7164 case ELF_INFO_TYPE_EH_FRAME
:
7170 bed
= get_elf_backend_data (sec
->owner
);
7171 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7172 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7178 /* Return a mask saying how ld should treat relocations in SEC against
7179 symbols defined in discarded sections. If this function returns
7180 COMPLAIN set, ld will issue a warning message. If this function
7181 returns PRETEND set, and the discarded section was link-once and the
7182 same size as the kept link-once section, ld will pretend that the
7183 symbol was actually defined in the kept section. Otherwise ld will
7184 zero the reloc (at least that is the intent, but some cooperation by
7185 the target dependent code is needed, particularly for REL targets). */
7188 _bfd_elf_default_action_discarded (asection
*sec
)
7190 if (sec
->flags
& SEC_DEBUGGING
)
7193 if (strcmp (".eh_frame", sec
->name
) == 0)
7196 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7199 return COMPLAIN
| PRETEND
;
7202 /* Find a match between a section and a member of a section group. */
7205 match_group_member (asection
*sec
, asection
*group
)
7207 asection
*first
= elf_next_in_group (group
);
7208 asection
*s
= first
;
7212 if (bfd_elf_match_symbols_in_sections (s
, sec
))
7215 s
= elf_next_in_group (s
);
7223 /* Check if the kept section of a discarded section SEC can be used
7224 to replace it. Return the replacement if it is OK. Otherwise return
7228 _bfd_elf_check_kept_section (asection
*sec
)
7232 kept
= sec
->kept_section
;
7235 if (elf_sec_group (sec
) != NULL
)
7236 kept
= match_group_member (sec
, kept
);
7237 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
7243 /* Link an input file into the linker output file. This function
7244 handles all the sections and relocations of the input file at once.
7245 This is so that we only have to read the local symbols once, and
7246 don't have to keep them in memory. */
7249 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
7251 bfd_boolean (*relocate_section
)
7252 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
7253 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
7255 Elf_Internal_Shdr
*symtab_hdr
;
7258 Elf_Internal_Sym
*isymbuf
;
7259 Elf_Internal_Sym
*isym
;
7260 Elf_Internal_Sym
*isymend
;
7262 asection
**ppsection
;
7264 const struct elf_backend_data
*bed
;
7265 bfd_boolean emit_relocs
;
7266 struct elf_link_hash_entry
**sym_hashes
;
7268 output_bfd
= finfo
->output_bfd
;
7269 bed
= get_elf_backend_data (output_bfd
);
7270 relocate_section
= bed
->elf_backend_relocate_section
;
7272 /* If this is a dynamic object, we don't want to do anything here:
7273 we don't want the local symbols, and we don't want the section
7275 if ((input_bfd
->flags
& DYNAMIC
) != 0)
7278 emit_relocs
= (finfo
->info
->relocatable
7279 || finfo
->info
->emitrelocations
);
7281 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7282 if (elf_bad_symtab (input_bfd
))
7284 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7289 locsymcount
= symtab_hdr
->sh_info
;
7290 extsymoff
= symtab_hdr
->sh_info
;
7293 /* Read the local symbols. */
7294 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7295 if (isymbuf
== NULL
&& locsymcount
!= 0)
7297 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
7298 finfo
->internal_syms
,
7299 finfo
->external_syms
,
7300 finfo
->locsym_shndx
);
7301 if (isymbuf
== NULL
)
7305 /* Find local symbol sections and adjust values of symbols in
7306 SEC_MERGE sections. Write out those local symbols we know are
7307 going into the output file. */
7308 isymend
= isymbuf
+ locsymcount
;
7309 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
7311 isym
++, pindex
++, ppsection
++)
7315 Elf_Internal_Sym osym
;
7319 if (elf_bad_symtab (input_bfd
))
7321 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
7328 if (isym
->st_shndx
== SHN_UNDEF
)
7329 isec
= bfd_und_section_ptr
;
7330 else if (isym
->st_shndx
< SHN_LORESERVE
7331 || isym
->st_shndx
> SHN_HIRESERVE
)
7333 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
7335 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
7336 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
7338 _bfd_merged_section_offset (output_bfd
, &isec
,
7339 elf_section_data (isec
)->sec_info
,
7342 else if (isym
->st_shndx
== SHN_ABS
)
7343 isec
= bfd_abs_section_ptr
;
7344 else if (isym
->st_shndx
== SHN_COMMON
)
7345 isec
= bfd_com_section_ptr
;
7348 /* Don't attempt to output symbols with st_shnx in the
7349 reserved range other than SHN_ABS and SHN_COMMON. */
7356 /* Don't output the first, undefined, symbol. */
7357 if (ppsection
== finfo
->sections
)
7360 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7362 /* We never output section symbols. Instead, we use the
7363 section symbol of the corresponding section in the output
7368 /* If we are stripping all symbols, we don't want to output this
7370 if (finfo
->info
->strip
== strip_all
)
7373 /* If we are discarding all local symbols, we don't want to
7374 output this one. If we are generating a relocatable output
7375 file, then some of the local symbols may be required by
7376 relocs; we output them below as we discover that they are
7378 if (finfo
->info
->discard
== discard_all
)
7381 /* If this symbol is defined in a section which we are
7382 discarding, we don't need to keep it. */
7383 if (isym
->st_shndx
!= SHN_UNDEF
7384 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
7386 || bfd_section_removed_from_list (output_bfd
,
7387 isec
->output_section
)))
7390 /* Get the name of the symbol. */
7391 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
7396 /* See if we are discarding symbols with this name. */
7397 if ((finfo
->info
->strip
== strip_some
7398 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
7400 || (((finfo
->info
->discard
== discard_sec_merge
7401 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
7402 || finfo
->info
->discard
== discard_l
)
7403 && bfd_is_local_label_name (input_bfd
, name
)))
7406 /* If we get here, we are going to output this symbol. */
7410 /* Adjust the section index for the output file. */
7411 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
7412 isec
->output_section
);
7413 if (osym
.st_shndx
== SHN_BAD
)
7416 *pindex
= bfd_get_symcount (output_bfd
);
7418 /* ELF symbols in relocatable files are section relative, but
7419 in executable files they are virtual addresses. Note that
7420 this code assumes that all ELF sections have an associated
7421 BFD section with a reasonable value for output_offset; below
7422 we assume that they also have a reasonable value for
7423 output_section. Any special sections must be set up to meet
7424 these requirements. */
7425 osym
.st_value
+= isec
->output_offset
;
7426 if (! finfo
->info
->relocatable
)
7428 osym
.st_value
+= isec
->output_section
->vma
;
7429 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
7431 /* STT_TLS symbols are relative to PT_TLS segment base. */
7432 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7433 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7437 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
7441 /* Relocate the contents of each section. */
7442 sym_hashes
= elf_sym_hashes (input_bfd
);
7443 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7447 if (! o
->linker_mark
)
7449 /* This section was omitted from the link. */
7453 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7454 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
7457 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
7459 /* Section was created by _bfd_elf_link_create_dynamic_sections
7464 /* Get the contents of the section. They have been cached by a
7465 relaxation routine. Note that o is a section in an input
7466 file, so the contents field will not have been set by any of
7467 the routines which work on output files. */
7468 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
7469 contents
= elf_section_data (o
)->this_hdr
.contents
;
7472 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
7474 contents
= finfo
->contents
;
7475 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
7479 if ((o
->flags
& SEC_RELOC
) != 0)
7481 Elf_Internal_Rela
*internal_relocs
;
7482 bfd_vma r_type_mask
;
7485 /* Get the swapped relocs. */
7487 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7488 finfo
->internal_relocs
, FALSE
);
7489 if (internal_relocs
== NULL
7490 && o
->reloc_count
> 0)
7493 if (bed
->s
->arch_size
== 32)
7500 r_type_mask
= 0xffffffff;
7504 /* Run through the relocs looking for any against symbols
7505 from discarded sections and section symbols from
7506 removed link-once sections. Complain about relocs
7507 against discarded sections. Zero relocs against removed
7508 link-once sections. */
7509 if (!elf_section_ignore_discarded_relocs (o
))
7511 Elf_Internal_Rela
*rel
, *relend
;
7512 unsigned int action
= (*bed
->action_discarded
) (o
);
7514 rel
= internal_relocs
;
7515 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7516 for ( ; rel
< relend
; rel
++)
7518 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7519 asection
**ps
, *sec
;
7520 struct elf_link_hash_entry
*h
= NULL
;
7521 const char *sym_name
;
7523 if (r_symndx
== STN_UNDEF
)
7526 if (r_symndx
>= locsymcount
7527 || (elf_bad_symtab (input_bfd
)
7528 && finfo
->sections
[r_symndx
] == NULL
))
7530 h
= sym_hashes
[r_symndx
- extsymoff
];
7532 /* Badly formatted input files can contain relocs that
7533 reference non-existant symbols. Check here so that
7534 we do not seg fault. */
7539 sprintf_vma (buffer
, rel
->r_info
);
7540 (*_bfd_error_handler
)
7541 (_("error: %B contains a reloc (0x%s) for section %A "
7542 "that references a non-existent global symbol"),
7543 input_bfd
, o
, buffer
);
7544 bfd_set_error (bfd_error_bad_value
);
7548 while (h
->root
.type
== bfd_link_hash_indirect
7549 || h
->root
.type
== bfd_link_hash_warning
)
7550 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7552 if (h
->root
.type
!= bfd_link_hash_defined
7553 && h
->root
.type
!= bfd_link_hash_defweak
)
7556 ps
= &h
->root
.u
.def
.section
;
7557 sym_name
= h
->root
.root
.string
;
7561 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7562 ps
= &finfo
->sections
[r_symndx
];
7563 sym_name
= bfd_elf_sym_name (input_bfd
,
7568 /* Complain if the definition comes from a
7569 discarded section. */
7570 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7572 BFD_ASSERT (r_symndx
!= 0);
7573 if (action
& COMPLAIN
)
7574 (*finfo
->info
->callbacks
->einfo
)
7575 (_("%X`%s' referenced in section `%A' of %B: "
7576 "defined in discarded section `%A' of %B\n"),
7577 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7579 /* Try to do the best we can to support buggy old
7580 versions of gcc. Pretend that the symbol is
7581 really defined in the kept linkonce section.
7582 FIXME: This is quite broken. Modifying the
7583 symbol here means we will be changing all later
7584 uses of the symbol, not just in this section. */
7585 if (action
& PRETEND
)
7589 kept
= _bfd_elf_check_kept_section (sec
);
7597 /* Remove the symbol reference from the reloc, but
7598 don't kill the reloc completely. This is so that
7599 a zero value will be written into the section,
7600 which may have non-zero contents put there by the
7601 assembler. Zero in things like an eh_frame fde
7602 pc_begin allows stack unwinders to recognize the
7604 rel
->r_info
&= r_type_mask
;
7610 /* Relocate the section by invoking a back end routine.
7612 The back end routine is responsible for adjusting the
7613 section contents as necessary, and (if using Rela relocs
7614 and generating a relocatable output file) adjusting the
7615 reloc addend as necessary.
7617 The back end routine does not have to worry about setting
7618 the reloc address or the reloc symbol index.
7620 The back end routine is given a pointer to the swapped in
7621 internal symbols, and can access the hash table entries
7622 for the external symbols via elf_sym_hashes (input_bfd).
7624 When generating relocatable output, the back end routine
7625 must handle STB_LOCAL/STT_SECTION symbols specially. The
7626 output symbol is going to be a section symbol
7627 corresponding to the output section, which will require
7628 the addend to be adjusted. */
7630 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7631 input_bfd
, o
, contents
,
7639 Elf_Internal_Rela
*irela
;
7640 Elf_Internal_Rela
*irelaend
;
7641 bfd_vma last_offset
;
7642 struct elf_link_hash_entry
**rel_hash
;
7643 struct elf_link_hash_entry
**rel_hash_list
;
7644 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7645 unsigned int next_erel
;
7646 bfd_boolean rela_normal
;
7648 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7649 rela_normal
= (bed
->rela_normal
7650 && (input_rel_hdr
->sh_entsize
7651 == bed
->s
->sizeof_rela
));
7653 /* Adjust the reloc addresses and symbol indices. */
7655 irela
= internal_relocs
;
7656 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7657 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7658 + elf_section_data (o
->output_section
)->rel_count
7659 + elf_section_data (o
->output_section
)->rel_count2
);
7660 rel_hash_list
= rel_hash
;
7661 last_offset
= o
->output_offset
;
7662 if (!finfo
->info
->relocatable
)
7663 last_offset
+= o
->output_section
->vma
;
7664 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7666 unsigned long r_symndx
;
7668 Elf_Internal_Sym sym
;
7670 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7676 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7679 if (irela
->r_offset
>= (bfd_vma
) -2)
7681 /* This is a reloc for a deleted entry or somesuch.
7682 Turn it into an R_*_NONE reloc, at the same
7683 offset as the last reloc. elf_eh_frame.c and
7684 bfd_elf_discard_info rely on reloc offsets
7686 irela
->r_offset
= last_offset
;
7688 irela
->r_addend
= 0;
7692 irela
->r_offset
+= o
->output_offset
;
7694 /* Relocs in an executable have to be virtual addresses. */
7695 if (!finfo
->info
->relocatable
)
7696 irela
->r_offset
+= o
->output_section
->vma
;
7698 last_offset
= irela
->r_offset
;
7700 r_symndx
= irela
->r_info
>> r_sym_shift
;
7701 if (r_symndx
== STN_UNDEF
)
7704 if (r_symndx
>= locsymcount
7705 || (elf_bad_symtab (input_bfd
)
7706 && finfo
->sections
[r_symndx
] == NULL
))
7708 struct elf_link_hash_entry
*rh
;
7711 /* This is a reloc against a global symbol. We
7712 have not yet output all the local symbols, so
7713 we do not know the symbol index of any global
7714 symbol. We set the rel_hash entry for this
7715 reloc to point to the global hash table entry
7716 for this symbol. The symbol index is then
7717 set at the end of bfd_elf_final_link. */
7718 indx
= r_symndx
- extsymoff
;
7719 rh
= elf_sym_hashes (input_bfd
)[indx
];
7720 while (rh
->root
.type
== bfd_link_hash_indirect
7721 || rh
->root
.type
== bfd_link_hash_warning
)
7722 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7724 /* Setting the index to -2 tells
7725 elf_link_output_extsym that this symbol is
7727 BFD_ASSERT (rh
->indx
< 0);
7735 /* This is a reloc against a local symbol. */
7738 sym
= isymbuf
[r_symndx
];
7739 sec
= finfo
->sections
[r_symndx
];
7740 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7742 /* I suppose the backend ought to fill in the
7743 section of any STT_SECTION symbol against a
7744 processor specific section. */
7746 if (bfd_is_abs_section (sec
))
7748 else if (sec
== NULL
|| sec
->owner
== NULL
)
7750 bfd_set_error (bfd_error_bad_value
);
7755 asection
*osec
= sec
->output_section
;
7757 /* If we have discarded a section, the output
7758 section will be the absolute section. In
7759 case of discarded link-once and discarded
7760 SEC_MERGE sections, use the kept section. */
7761 if (bfd_is_abs_section (osec
)
7762 && sec
->kept_section
!= NULL
7763 && sec
->kept_section
->output_section
!= NULL
)
7765 osec
= sec
->kept_section
->output_section
;
7766 irela
->r_addend
-= osec
->vma
;
7769 if (!bfd_is_abs_section (osec
))
7771 r_symndx
= osec
->target_index
;
7772 BFD_ASSERT (r_symndx
!= 0);
7776 /* Adjust the addend according to where the
7777 section winds up in the output section. */
7779 irela
->r_addend
+= sec
->output_offset
;
7783 if (finfo
->indices
[r_symndx
] == -1)
7785 unsigned long shlink
;
7789 if (finfo
->info
->strip
== strip_all
)
7791 /* You can't do ld -r -s. */
7792 bfd_set_error (bfd_error_invalid_operation
);
7796 /* This symbol was skipped earlier, but
7797 since it is needed by a reloc, we
7798 must output it now. */
7799 shlink
= symtab_hdr
->sh_link
;
7800 name
= (bfd_elf_string_from_elf_section
7801 (input_bfd
, shlink
, sym
.st_name
));
7805 osec
= sec
->output_section
;
7807 _bfd_elf_section_from_bfd_section (output_bfd
,
7809 if (sym
.st_shndx
== SHN_BAD
)
7812 sym
.st_value
+= sec
->output_offset
;
7813 if (! finfo
->info
->relocatable
)
7815 sym
.st_value
+= osec
->vma
;
7816 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7818 /* STT_TLS symbols are relative to PT_TLS
7820 BFD_ASSERT (elf_hash_table (finfo
->info
)
7822 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7827 finfo
->indices
[r_symndx
]
7828 = bfd_get_symcount (output_bfd
);
7830 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7835 r_symndx
= finfo
->indices
[r_symndx
];
7838 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7839 | (irela
->r_info
& r_type_mask
));
7842 /* Swap out the relocs. */
7843 if (input_rel_hdr
->sh_size
!= 0
7844 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7850 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7851 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7853 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7854 * bed
->s
->int_rels_per_ext_rel
);
7855 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7856 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7865 /* Write out the modified section contents. */
7866 if (bed
->elf_backend_write_section
7867 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7869 /* Section written out. */
7871 else switch (o
->sec_info_type
)
7873 case ELF_INFO_TYPE_STABS
:
7874 if (! (_bfd_write_section_stabs
7876 &elf_hash_table (finfo
->info
)->stab_info
,
7877 o
, &elf_section_data (o
)->sec_info
, contents
)))
7880 case ELF_INFO_TYPE_MERGE
:
7881 if (! _bfd_write_merged_section (output_bfd
, o
,
7882 elf_section_data (o
)->sec_info
))
7885 case ELF_INFO_TYPE_EH_FRAME
:
7887 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7894 if (! (o
->flags
& SEC_EXCLUDE
)
7895 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7897 (file_ptr
) o
->output_offset
,
7908 /* Generate a reloc when linking an ELF file. This is a reloc
7909 requested by the linker, and does not come from any input file. This
7910 is used to build constructor and destructor tables when linking
7914 elf_reloc_link_order (bfd
*output_bfd
,
7915 struct bfd_link_info
*info
,
7916 asection
*output_section
,
7917 struct bfd_link_order
*link_order
)
7919 reloc_howto_type
*howto
;
7923 struct elf_link_hash_entry
**rel_hash_ptr
;
7924 Elf_Internal_Shdr
*rel_hdr
;
7925 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7926 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7930 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7933 bfd_set_error (bfd_error_bad_value
);
7937 addend
= link_order
->u
.reloc
.p
->addend
;
7939 /* Figure out the symbol index. */
7940 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7941 + elf_section_data (output_section
)->rel_count
7942 + elf_section_data (output_section
)->rel_count2
);
7943 if (link_order
->type
== bfd_section_reloc_link_order
)
7945 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7946 BFD_ASSERT (indx
!= 0);
7947 *rel_hash_ptr
= NULL
;
7951 struct elf_link_hash_entry
*h
;
7953 /* Treat a reloc against a defined symbol as though it were
7954 actually against the section. */
7955 h
= ((struct elf_link_hash_entry
*)
7956 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7957 link_order
->u
.reloc
.p
->u
.name
,
7958 FALSE
, FALSE
, TRUE
));
7960 && (h
->root
.type
== bfd_link_hash_defined
7961 || h
->root
.type
== bfd_link_hash_defweak
))
7965 section
= h
->root
.u
.def
.section
;
7966 indx
= section
->output_section
->target_index
;
7967 *rel_hash_ptr
= NULL
;
7968 /* It seems that we ought to add the symbol value to the
7969 addend here, but in practice it has already been added
7970 because it was passed to constructor_callback. */
7971 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7975 /* Setting the index to -2 tells elf_link_output_extsym that
7976 this symbol is used by a reloc. */
7983 if (! ((*info
->callbacks
->unattached_reloc
)
7984 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7990 /* If this is an inplace reloc, we must write the addend into the
7992 if (howto
->partial_inplace
&& addend
!= 0)
7995 bfd_reloc_status_type rstat
;
7998 const char *sym_name
;
8000 size
= bfd_get_reloc_size (howto
);
8001 buf
= bfd_zmalloc (size
);
8004 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8011 case bfd_reloc_outofrange
:
8014 case bfd_reloc_overflow
:
8015 if (link_order
->type
== bfd_section_reloc_link_order
)
8016 sym_name
= bfd_section_name (output_bfd
,
8017 link_order
->u
.reloc
.p
->u
.section
);
8019 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8020 if (! ((*info
->callbacks
->reloc_overflow
)
8021 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8022 NULL
, (bfd_vma
) 0)))
8029 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
8030 link_order
->offset
, size
);
8036 /* The address of a reloc is relative to the section in a
8037 relocatable file, and is a virtual address in an executable
8039 offset
= link_order
->offset
;
8040 if (! info
->relocatable
)
8041 offset
+= output_section
->vma
;
8043 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
8045 irel
[i
].r_offset
= offset
;
8047 irel
[i
].r_addend
= 0;
8049 if (bed
->s
->arch_size
== 32)
8050 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8052 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8054 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8055 erel
= rel_hdr
->contents
;
8056 if (rel_hdr
->sh_type
== SHT_REL
)
8058 erel
+= (elf_section_data (output_section
)->rel_count
8059 * bed
->s
->sizeof_rel
);
8060 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8064 irel
[0].r_addend
= addend
;
8065 erel
+= (elf_section_data (output_section
)->rel_count
8066 * bed
->s
->sizeof_rela
);
8067 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8070 ++elf_section_data (output_section
)->rel_count
;
8076 /* Get the output vma of the section pointed to by the sh_link field. */
8079 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8081 Elf_Internal_Shdr
**elf_shdrp
;
8085 s
= p
->u
.indirect
.section
;
8086 elf_shdrp
= elf_elfsections (s
->owner
);
8087 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8088 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8090 The Intel C compiler generates SHT_IA_64_UNWIND with
8091 SHF_LINK_ORDER. But it doesn't set the sh_link or
8092 sh_info fields. Hence we could get the situation
8093 where elfsec is 0. */
8096 const struct elf_backend_data
*bed
8097 = get_elf_backend_data (s
->owner
);
8098 if (bed
->link_order_error_handler
)
8099 bed
->link_order_error_handler
8100 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8105 s
= elf_shdrp
[elfsec
]->bfd_section
;
8106 return s
->output_section
->vma
+ s
->output_offset
;
8111 /* Compare two sections based on the locations of the sections they are
8112 linked to. Used by elf_fixup_link_order. */
8115 compare_link_order (const void * a
, const void * b
)
8120 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8121 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8128 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8129 order as their linked sections. Returns false if this could not be done
8130 because an output section includes both ordered and unordered
8131 sections. Ideally we'd do this in the linker proper. */
8134 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8139 struct bfd_link_order
*p
;
8141 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8143 struct bfd_link_order
**sections
;
8144 asection
*s
, *other_sec
, *linkorder_sec
;
8148 linkorder_sec
= NULL
;
8151 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8153 if (p
->type
== bfd_indirect_link_order
)
8155 s
= p
->u
.indirect
.section
;
8157 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8158 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8159 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8160 && elfsec
< elf_numsections (sub
)
8161 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8175 if (seen_other
&& seen_linkorder
)
8177 if (other_sec
&& linkorder_sec
)
8178 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8180 linkorder_sec
->owner
, other_sec
,
8183 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
8185 bfd_set_error (bfd_error_bad_value
);
8190 if (!seen_linkorder
)
8193 sections
= (struct bfd_link_order
**)
8194 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
8197 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8199 sections
[seen_linkorder
++] = p
;
8201 /* Sort the input sections in the order of their linked section. */
8202 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
8203 compare_link_order
);
8205 /* Change the offsets of the sections. */
8207 for (n
= 0; n
< seen_linkorder
; n
++)
8209 s
= sections
[n
]->u
.indirect
.section
;
8210 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
8211 s
->output_offset
= offset
;
8212 sections
[n
]->offset
= offset
;
8213 offset
+= sections
[n
]->size
;
8220 /* Do the final step of an ELF link. */
8223 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8225 bfd_boolean dynamic
;
8226 bfd_boolean emit_relocs
;
8228 struct elf_final_link_info finfo
;
8229 register asection
*o
;
8230 register struct bfd_link_order
*p
;
8232 bfd_size_type max_contents_size
;
8233 bfd_size_type max_external_reloc_size
;
8234 bfd_size_type max_internal_reloc_count
;
8235 bfd_size_type max_sym_count
;
8236 bfd_size_type max_sym_shndx_count
;
8238 Elf_Internal_Sym elfsym
;
8240 Elf_Internal_Shdr
*symtab_hdr
;
8241 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8242 Elf_Internal_Shdr
*symstrtab_hdr
;
8243 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8244 struct elf_outext_info eoinfo
;
8246 size_t relativecount
= 0;
8247 asection
*reldyn
= 0;
8250 if (! is_elf_hash_table (info
->hash
))
8254 abfd
->flags
|= DYNAMIC
;
8256 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
8257 dynobj
= elf_hash_table (info
)->dynobj
;
8259 emit_relocs
= (info
->relocatable
8260 || info
->emitrelocations
);
8263 finfo
.output_bfd
= abfd
;
8264 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
8265 if (finfo
.symstrtab
== NULL
)
8270 finfo
.dynsym_sec
= NULL
;
8271 finfo
.hash_sec
= NULL
;
8272 finfo
.symver_sec
= NULL
;
8276 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
8277 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
8278 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
8279 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
8280 /* Note that it is OK if symver_sec is NULL. */
8283 finfo
.contents
= NULL
;
8284 finfo
.external_relocs
= NULL
;
8285 finfo
.internal_relocs
= NULL
;
8286 finfo
.external_syms
= NULL
;
8287 finfo
.locsym_shndx
= NULL
;
8288 finfo
.internal_syms
= NULL
;
8289 finfo
.indices
= NULL
;
8290 finfo
.sections
= NULL
;
8291 finfo
.symbuf
= NULL
;
8292 finfo
.symshndxbuf
= NULL
;
8293 finfo
.symbuf_count
= 0;
8294 finfo
.shndxbuf_size
= 0;
8296 /* Count up the number of relocations we will output for each output
8297 section, so that we know the sizes of the reloc sections. We
8298 also figure out some maximum sizes. */
8299 max_contents_size
= 0;
8300 max_external_reloc_size
= 0;
8301 max_internal_reloc_count
= 0;
8303 max_sym_shndx_count
= 0;
8305 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8307 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
8310 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8312 unsigned int reloc_count
= 0;
8313 struct bfd_elf_section_data
*esdi
= NULL
;
8314 unsigned int *rel_count1
;
8316 if (p
->type
== bfd_section_reloc_link_order
8317 || p
->type
== bfd_symbol_reloc_link_order
)
8319 else if (p
->type
== bfd_indirect_link_order
)
8323 sec
= p
->u
.indirect
.section
;
8324 esdi
= elf_section_data (sec
);
8326 /* Mark all sections which are to be included in the
8327 link. This will normally be every section. We need
8328 to do this so that we can identify any sections which
8329 the linker has decided to not include. */
8330 sec
->linker_mark
= TRUE
;
8332 if (sec
->flags
& SEC_MERGE
)
8335 if (info
->relocatable
|| info
->emitrelocations
)
8336 reloc_count
= sec
->reloc_count
;
8337 else if (bed
->elf_backend_count_relocs
)
8339 Elf_Internal_Rela
* relocs
;
8341 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
8344 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
8346 if (elf_section_data (o
)->relocs
!= relocs
)
8350 if (sec
->rawsize
> max_contents_size
)
8351 max_contents_size
= sec
->rawsize
;
8352 if (sec
->size
> max_contents_size
)
8353 max_contents_size
= sec
->size
;
8355 /* We are interested in just local symbols, not all
8357 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
8358 && (sec
->owner
->flags
& DYNAMIC
) == 0)
8362 if (elf_bad_symtab (sec
->owner
))
8363 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
8364 / bed
->s
->sizeof_sym
);
8366 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
8368 if (sym_count
> max_sym_count
)
8369 max_sym_count
= sym_count
;
8371 if (sym_count
> max_sym_shndx_count
8372 && elf_symtab_shndx (sec
->owner
) != 0)
8373 max_sym_shndx_count
= sym_count
;
8375 if ((sec
->flags
& SEC_RELOC
) != 0)
8379 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
8380 if (ext_size
> max_external_reloc_size
)
8381 max_external_reloc_size
= ext_size
;
8382 if (sec
->reloc_count
> max_internal_reloc_count
)
8383 max_internal_reloc_count
= sec
->reloc_count
;
8388 if (reloc_count
== 0)
8391 o
->reloc_count
+= reloc_count
;
8393 /* MIPS may have a mix of REL and RELA relocs on sections.
8394 To support this curious ABI we keep reloc counts in
8395 elf_section_data too. We must be careful to add the
8396 relocations from the input section to the right output
8397 count. FIXME: Get rid of one count. We have
8398 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
8399 rel_count1
= &esdo
->rel_count
;
8402 bfd_boolean same_size
;
8403 bfd_size_type entsize1
;
8405 entsize1
= esdi
->rel_hdr
.sh_entsize
;
8406 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
8407 || entsize1
== bed
->s
->sizeof_rela
);
8408 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
8411 rel_count1
= &esdo
->rel_count2
;
8413 if (esdi
->rel_hdr2
!= NULL
)
8415 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
8416 unsigned int alt_count
;
8417 unsigned int *rel_count2
;
8419 BFD_ASSERT (entsize2
!= entsize1
8420 && (entsize2
== bed
->s
->sizeof_rel
8421 || entsize2
== bed
->s
->sizeof_rela
));
8423 rel_count2
= &esdo
->rel_count2
;
8425 rel_count2
= &esdo
->rel_count
;
8427 /* The following is probably too simplistic if the
8428 backend counts output relocs unusually. */
8429 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
8430 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
8431 *rel_count2
+= alt_count
;
8432 reloc_count
-= alt_count
;
8435 *rel_count1
+= reloc_count
;
8438 if (o
->reloc_count
> 0)
8439 o
->flags
|= SEC_RELOC
;
8442 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8443 set it (this is probably a bug) and if it is set
8444 assign_section_numbers will create a reloc section. */
8445 o
->flags
&=~ SEC_RELOC
;
8448 /* If the SEC_ALLOC flag is not set, force the section VMA to
8449 zero. This is done in elf_fake_sections as well, but forcing
8450 the VMA to 0 here will ensure that relocs against these
8451 sections are handled correctly. */
8452 if ((o
->flags
& SEC_ALLOC
) == 0
8453 && ! o
->user_set_vma
)
8457 if (! info
->relocatable
&& merged
)
8458 elf_link_hash_traverse (elf_hash_table (info
),
8459 _bfd_elf_link_sec_merge_syms
, abfd
);
8461 /* Figure out the file positions for everything but the symbol table
8462 and the relocs. We set symcount to force assign_section_numbers
8463 to create a symbol table. */
8464 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
8465 BFD_ASSERT (! abfd
->output_has_begun
);
8466 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
8469 /* Set sizes, and assign file positions for reloc sections. */
8470 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8472 if ((o
->flags
& SEC_RELOC
) != 0)
8474 if (!(_bfd_elf_link_size_reloc_section
8475 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
8478 if (elf_section_data (o
)->rel_hdr2
8479 && !(_bfd_elf_link_size_reloc_section
8480 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
8484 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8485 to count upwards while actually outputting the relocations. */
8486 elf_section_data (o
)->rel_count
= 0;
8487 elf_section_data (o
)->rel_count2
= 0;
8490 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8492 /* We have now assigned file positions for all the sections except
8493 .symtab and .strtab. We start the .symtab section at the current
8494 file position, and write directly to it. We build the .strtab
8495 section in memory. */
8496 bfd_get_symcount (abfd
) = 0;
8497 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8498 /* sh_name is set in prep_headers. */
8499 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8500 /* sh_flags, sh_addr and sh_size all start off zero. */
8501 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8502 /* sh_link is set in assign_section_numbers. */
8503 /* sh_info is set below. */
8504 /* sh_offset is set just below. */
8505 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8507 off
= elf_tdata (abfd
)->next_file_pos
;
8508 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8510 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8511 incorrect. We do not yet know the size of the .symtab section.
8512 We correct next_file_pos below, after we do know the size. */
8514 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8515 continuously seeking to the right position in the file. */
8516 if (! info
->keep_memory
|| max_sym_count
< 20)
8517 finfo
.symbuf_size
= 20;
8519 finfo
.symbuf_size
= max_sym_count
;
8520 amt
= finfo
.symbuf_size
;
8521 amt
*= bed
->s
->sizeof_sym
;
8522 finfo
.symbuf
= bfd_malloc (amt
);
8523 if (finfo
.symbuf
== NULL
)
8525 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8527 /* Wild guess at number of output symbols. realloc'd as needed. */
8528 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8529 finfo
.shndxbuf_size
= amt
;
8530 amt
*= sizeof (Elf_External_Sym_Shndx
);
8531 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8532 if (finfo
.symshndxbuf
== NULL
)
8536 /* Start writing out the symbol table. The first symbol is always a
8538 if (info
->strip
!= strip_all
8541 elfsym
.st_value
= 0;
8544 elfsym
.st_other
= 0;
8545 elfsym
.st_shndx
= SHN_UNDEF
;
8546 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8551 /* Output a symbol for each section. We output these even if we are
8552 discarding local symbols, since they are used for relocs. These
8553 symbols have no names. We store the index of each one in the
8554 index field of the section, so that we can find it again when
8555 outputting relocs. */
8556 if (info
->strip
!= strip_all
8560 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8561 elfsym
.st_other
= 0;
8562 elfsym
.st_value
= 0;
8563 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8565 o
= bfd_section_from_elf_index (abfd
, i
);
8568 o
->target_index
= bfd_get_symcount (abfd
);
8569 elfsym
.st_shndx
= i
;
8570 if (!info
->relocatable
)
8571 elfsym
.st_value
= o
->vma
;
8572 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8575 if (i
== SHN_LORESERVE
- 1)
8576 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8580 /* Allocate some memory to hold information read in from the input
8582 if (max_contents_size
!= 0)
8584 finfo
.contents
= bfd_malloc (max_contents_size
);
8585 if (finfo
.contents
== NULL
)
8589 if (max_external_reloc_size
!= 0)
8591 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8592 if (finfo
.external_relocs
== NULL
)
8596 if (max_internal_reloc_count
!= 0)
8598 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8599 amt
*= sizeof (Elf_Internal_Rela
);
8600 finfo
.internal_relocs
= bfd_malloc (amt
);
8601 if (finfo
.internal_relocs
== NULL
)
8605 if (max_sym_count
!= 0)
8607 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8608 finfo
.external_syms
= bfd_malloc (amt
);
8609 if (finfo
.external_syms
== NULL
)
8612 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8613 finfo
.internal_syms
= bfd_malloc (amt
);
8614 if (finfo
.internal_syms
== NULL
)
8617 amt
= max_sym_count
* sizeof (long);
8618 finfo
.indices
= bfd_malloc (amt
);
8619 if (finfo
.indices
== NULL
)
8622 amt
= max_sym_count
* sizeof (asection
*);
8623 finfo
.sections
= bfd_malloc (amt
);
8624 if (finfo
.sections
== NULL
)
8628 if (max_sym_shndx_count
!= 0)
8630 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8631 finfo
.locsym_shndx
= bfd_malloc (amt
);
8632 if (finfo
.locsym_shndx
== NULL
)
8636 if (elf_hash_table (info
)->tls_sec
)
8638 bfd_vma base
, end
= 0;
8641 for (sec
= elf_hash_table (info
)->tls_sec
;
8642 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8645 bfd_size_type size
= sec
->size
;
8648 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8650 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8652 size
= o
->offset
+ o
->size
;
8654 end
= sec
->vma
+ size
;
8656 base
= elf_hash_table (info
)->tls_sec
->vma
;
8657 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8658 elf_hash_table (info
)->tls_size
= end
- base
;
8661 /* Reorder SHF_LINK_ORDER sections. */
8662 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8664 if (!elf_fixup_link_order (abfd
, o
))
8668 /* Since ELF permits relocations to be against local symbols, we
8669 must have the local symbols available when we do the relocations.
8670 Since we would rather only read the local symbols once, and we
8671 would rather not keep them in memory, we handle all the
8672 relocations for a single input file at the same time.
8674 Unfortunately, there is no way to know the total number of local
8675 symbols until we have seen all of them, and the local symbol
8676 indices precede the global symbol indices. This means that when
8677 we are generating relocatable output, and we see a reloc against
8678 a global symbol, we can not know the symbol index until we have
8679 finished examining all the local symbols to see which ones we are
8680 going to output. To deal with this, we keep the relocations in
8681 memory, and don't output them until the end of the link. This is
8682 an unfortunate waste of memory, but I don't see a good way around
8683 it. Fortunately, it only happens when performing a relocatable
8684 link, which is not the common case. FIXME: If keep_memory is set
8685 we could write the relocs out and then read them again; I don't
8686 know how bad the memory loss will be. */
8688 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8689 sub
->output_has_begun
= FALSE
;
8690 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8692 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8694 if (p
->type
== bfd_indirect_link_order
8695 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8696 == bfd_target_elf_flavour
)
8697 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8699 if (! sub
->output_has_begun
)
8701 if (! elf_link_input_bfd (&finfo
, sub
))
8703 sub
->output_has_begun
= TRUE
;
8706 else if (p
->type
== bfd_section_reloc_link_order
8707 || p
->type
== bfd_symbol_reloc_link_order
)
8709 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8714 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8720 /* Output any global symbols that got converted to local in a
8721 version script or due to symbol visibility. We do this in a
8722 separate step since ELF requires all local symbols to appear
8723 prior to any global symbols. FIXME: We should only do this if
8724 some global symbols were, in fact, converted to become local.
8725 FIXME: Will this work correctly with the Irix 5 linker? */
8726 eoinfo
.failed
= FALSE
;
8727 eoinfo
.finfo
= &finfo
;
8728 eoinfo
.localsyms
= TRUE
;
8729 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8734 /* If backend needs to output some local symbols not present in the hash
8735 table, do it now. */
8736 if (bed
->elf_backend_output_arch_local_syms
)
8738 typedef bfd_boolean (*out_sym_func
)
8739 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8740 struct elf_link_hash_entry
*);
8742 if (! ((*bed
->elf_backend_output_arch_local_syms
)
8743 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8747 /* That wrote out all the local symbols. Finish up the symbol table
8748 with the global symbols. Even if we want to strip everything we
8749 can, we still need to deal with those global symbols that got
8750 converted to local in a version script. */
8752 /* The sh_info field records the index of the first non local symbol. */
8753 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8756 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8758 Elf_Internal_Sym sym
;
8759 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8760 long last_local
= 0;
8762 /* Write out the section symbols for the output sections. */
8763 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8769 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8772 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8778 dynindx
= elf_section_data (s
)->dynindx
;
8781 indx
= elf_section_data (s
)->this_idx
;
8782 BFD_ASSERT (indx
> 0);
8783 sym
.st_shndx
= indx
;
8784 if (! check_dynsym (abfd
, &sym
))
8786 sym
.st_value
= s
->vma
;
8787 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8788 if (last_local
< dynindx
)
8789 last_local
= dynindx
;
8790 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8794 /* Write out the local dynsyms. */
8795 if (elf_hash_table (info
)->dynlocal
)
8797 struct elf_link_local_dynamic_entry
*e
;
8798 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8803 sym
.st_size
= e
->isym
.st_size
;
8804 sym
.st_other
= e
->isym
.st_other
;
8806 /* Copy the internal symbol as is.
8807 Note that we saved a word of storage and overwrote
8808 the original st_name with the dynstr_index. */
8811 if (e
->isym
.st_shndx
!= SHN_UNDEF
8812 && (e
->isym
.st_shndx
< SHN_LORESERVE
8813 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8815 s
= bfd_section_from_elf_index (e
->input_bfd
,
8819 elf_section_data (s
->output_section
)->this_idx
;
8820 if (! check_dynsym (abfd
, &sym
))
8822 sym
.st_value
= (s
->output_section
->vma
8824 + e
->isym
.st_value
);
8827 if (last_local
< e
->dynindx
)
8828 last_local
= e
->dynindx
;
8830 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8831 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8835 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8839 /* We get the global symbols from the hash table. */
8840 eoinfo
.failed
= FALSE
;
8841 eoinfo
.localsyms
= FALSE
;
8842 eoinfo
.finfo
= &finfo
;
8843 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8848 /* If backend needs to output some symbols not present in the hash
8849 table, do it now. */
8850 if (bed
->elf_backend_output_arch_syms
)
8852 typedef bfd_boolean (*out_sym_func
)
8853 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8854 struct elf_link_hash_entry
*);
8856 if (! ((*bed
->elf_backend_output_arch_syms
)
8857 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8861 /* Flush all symbols to the file. */
8862 if (! elf_link_flush_output_syms (&finfo
, bed
))
8865 /* Now we know the size of the symtab section. */
8866 off
+= symtab_hdr
->sh_size
;
8868 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8869 if (symtab_shndx_hdr
->sh_name
!= 0)
8871 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8872 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8873 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8874 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8875 symtab_shndx_hdr
->sh_size
= amt
;
8877 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8880 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8881 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8886 /* Finish up and write out the symbol string table (.strtab)
8888 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8889 /* sh_name was set in prep_headers. */
8890 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8891 symstrtab_hdr
->sh_flags
= 0;
8892 symstrtab_hdr
->sh_addr
= 0;
8893 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8894 symstrtab_hdr
->sh_entsize
= 0;
8895 symstrtab_hdr
->sh_link
= 0;
8896 symstrtab_hdr
->sh_info
= 0;
8897 /* sh_offset is set just below. */
8898 symstrtab_hdr
->sh_addralign
= 1;
8900 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8901 elf_tdata (abfd
)->next_file_pos
= off
;
8903 if (bfd_get_symcount (abfd
) > 0)
8905 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8906 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8910 /* Adjust the relocs to have the correct symbol indices. */
8911 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8913 if ((o
->flags
& SEC_RELOC
) == 0)
8916 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8917 elf_section_data (o
)->rel_count
,
8918 elf_section_data (o
)->rel_hashes
);
8919 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8920 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8921 elf_section_data (o
)->rel_count2
,
8922 (elf_section_data (o
)->rel_hashes
8923 + elf_section_data (o
)->rel_count
));
8925 /* Set the reloc_count field to 0 to prevent write_relocs from
8926 trying to swap the relocs out itself. */
8930 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8931 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8933 /* If we are linking against a dynamic object, or generating a
8934 shared library, finish up the dynamic linking information. */
8937 bfd_byte
*dyncon
, *dynconend
;
8939 /* Fix up .dynamic entries. */
8940 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8941 BFD_ASSERT (o
!= NULL
);
8943 dyncon
= o
->contents
;
8944 dynconend
= o
->contents
+ o
->size
;
8945 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8947 Elf_Internal_Dyn dyn
;
8951 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8958 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8960 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8962 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8963 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8966 dyn
.d_un
.d_val
= relativecount
;
8973 name
= info
->init_function
;
8976 name
= info
->fini_function
;
8979 struct elf_link_hash_entry
*h
;
8981 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8982 FALSE
, FALSE
, TRUE
);
8984 && (h
->root
.type
== bfd_link_hash_defined
8985 || h
->root
.type
== bfd_link_hash_defweak
))
8987 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8988 o
= h
->root
.u
.def
.section
;
8989 if (o
->output_section
!= NULL
)
8990 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8991 + o
->output_offset
);
8994 /* The symbol is imported from another shared
8995 library and does not apply to this one. */
9003 case DT_PREINIT_ARRAYSZ
:
9004 name
= ".preinit_array";
9006 case DT_INIT_ARRAYSZ
:
9007 name
= ".init_array";
9009 case DT_FINI_ARRAYSZ
:
9010 name
= ".fini_array";
9012 o
= bfd_get_section_by_name (abfd
, name
);
9015 (*_bfd_error_handler
)
9016 (_("%B: could not find output section %s"), abfd
, name
);
9020 (*_bfd_error_handler
)
9021 (_("warning: %s section has zero size"), name
);
9022 dyn
.d_un
.d_val
= o
->size
;
9025 case DT_PREINIT_ARRAY
:
9026 name
= ".preinit_array";
9029 name
= ".init_array";
9032 name
= ".fini_array";
9048 name
= ".gnu.version_d";
9051 name
= ".gnu.version_r";
9054 name
= ".gnu.version";
9056 o
= bfd_get_section_by_name (abfd
, name
);
9059 (*_bfd_error_handler
)
9060 (_("%B: could not find output section %s"), abfd
, name
);
9063 dyn
.d_un
.d_ptr
= o
->vma
;
9070 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9075 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9077 Elf_Internal_Shdr
*hdr
;
9079 hdr
= elf_elfsections (abfd
)[i
];
9080 if (hdr
->sh_type
== type
9081 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9083 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9084 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9087 if (dyn
.d_un
.d_val
== 0
9088 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9089 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9095 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9099 /* If we have created any dynamic sections, then output them. */
9102 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9105 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9106 if (info
->warn_shared_textrel
&& info
->shared
)
9108 bfd_byte
*dyncon
, *dynconend
;
9110 /* Fix up .dynamic entries. */
9111 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9112 BFD_ASSERT (o
!= NULL
);
9114 dyncon
= o
->contents
;
9115 dynconend
= o
->contents
+ o
->size
;
9116 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9118 Elf_Internal_Dyn dyn
;
9120 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9122 if (dyn
.d_tag
== DT_TEXTREL
)
9125 (_("warning: creating a DT_TEXTREL in a shared object."));
9131 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9133 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9135 || o
->output_section
== bfd_abs_section_ptr
)
9137 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9139 /* At this point, we are only interested in sections
9140 created by _bfd_elf_link_create_dynamic_sections. */
9143 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9145 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9147 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9149 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9151 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9153 (file_ptr
) o
->output_offset
,
9159 /* The contents of the .dynstr section are actually in a
9161 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9162 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9163 || ! _bfd_elf_strtab_emit (abfd
,
9164 elf_hash_table (info
)->dynstr
))
9170 if (info
->relocatable
)
9172 bfd_boolean failed
= FALSE
;
9174 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
9179 /* If we have optimized stabs strings, output them. */
9180 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
9182 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
9186 if (info
->eh_frame_hdr
)
9188 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
9192 if (finfo
.symstrtab
!= NULL
)
9193 _bfd_stringtab_free (finfo
.symstrtab
);
9194 if (finfo
.contents
!= NULL
)
9195 free (finfo
.contents
);
9196 if (finfo
.external_relocs
!= NULL
)
9197 free (finfo
.external_relocs
);
9198 if (finfo
.internal_relocs
!= NULL
)
9199 free (finfo
.internal_relocs
);
9200 if (finfo
.external_syms
!= NULL
)
9201 free (finfo
.external_syms
);
9202 if (finfo
.locsym_shndx
!= NULL
)
9203 free (finfo
.locsym_shndx
);
9204 if (finfo
.internal_syms
!= NULL
)
9205 free (finfo
.internal_syms
);
9206 if (finfo
.indices
!= NULL
)
9207 free (finfo
.indices
);
9208 if (finfo
.sections
!= NULL
)
9209 free (finfo
.sections
);
9210 if (finfo
.symbuf
!= NULL
)
9211 free (finfo
.symbuf
);
9212 if (finfo
.symshndxbuf
!= NULL
)
9213 free (finfo
.symshndxbuf
);
9214 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9216 if ((o
->flags
& SEC_RELOC
) != 0
9217 && elf_section_data (o
)->rel_hashes
!= NULL
)
9218 free (elf_section_data (o
)->rel_hashes
);
9221 elf_tdata (abfd
)->linker
= TRUE
;
9226 if (finfo
.symstrtab
!= NULL
)
9227 _bfd_stringtab_free (finfo
.symstrtab
);
9228 if (finfo
.contents
!= NULL
)
9229 free (finfo
.contents
);
9230 if (finfo
.external_relocs
!= NULL
)
9231 free (finfo
.external_relocs
);
9232 if (finfo
.internal_relocs
!= NULL
)
9233 free (finfo
.internal_relocs
);
9234 if (finfo
.external_syms
!= NULL
)
9235 free (finfo
.external_syms
);
9236 if (finfo
.locsym_shndx
!= NULL
)
9237 free (finfo
.locsym_shndx
);
9238 if (finfo
.internal_syms
!= NULL
)
9239 free (finfo
.internal_syms
);
9240 if (finfo
.indices
!= NULL
)
9241 free (finfo
.indices
);
9242 if (finfo
.sections
!= NULL
)
9243 free (finfo
.sections
);
9244 if (finfo
.symbuf
!= NULL
)
9245 free (finfo
.symbuf
);
9246 if (finfo
.symshndxbuf
!= NULL
)
9247 free (finfo
.symshndxbuf
);
9248 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9250 if ((o
->flags
& SEC_RELOC
) != 0
9251 && elf_section_data (o
)->rel_hashes
!= NULL
)
9252 free (elf_section_data (o
)->rel_hashes
);
9258 /* Garbage collect unused sections. */
9260 typedef asection
* (*gc_mark_hook_fn
)
9261 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9262 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
9264 /* Default gc_mark_hook. */
9267 _bfd_elf_gc_mark_hook (asection
*sec
,
9268 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9269 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
9270 struct elf_link_hash_entry
*h
,
9271 Elf_Internal_Sym
*sym
)
9275 switch (h
->root
.type
)
9277 case bfd_link_hash_defined
:
9278 case bfd_link_hash_defweak
:
9279 return h
->root
.u
.def
.section
;
9281 case bfd_link_hash_common
:
9282 return h
->root
.u
.c
.p
->section
;
9289 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
9294 /* The mark phase of garbage collection. For a given section, mark
9295 it and any sections in this section's group, and all the sections
9296 which define symbols to which it refers. */
9299 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
9301 gc_mark_hook_fn gc_mark_hook
)
9305 asection
*group_sec
;
9309 /* Mark all the sections in the group. */
9310 group_sec
= elf_section_data (sec
)->next_in_group
;
9311 if (group_sec
&& !group_sec
->gc_mark
)
9312 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
9315 /* Look through the section relocs. */
9317 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
9318 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
9320 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
9321 Elf_Internal_Shdr
*symtab_hdr
;
9322 struct elf_link_hash_entry
**sym_hashes
;
9325 bfd
*input_bfd
= sec
->owner
;
9326 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
9327 Elf_Internal_Sym
*isym
= NULL
;
9330 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9331 sym_hashes
= elf_sym_hashes (input_bfd
);
9333 /* Read the local symbols. */
9334 if (elf_bad_symtab (input_bfd
))
9336 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9340 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
9342 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9343 if (isym
== NULL
&& nlocsyms
!= 0)
9345 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
9351 /* Read the relocations. */
9352 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
9354 if (relstart
== NULL
)
9359 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9361 if (bed
->s
->arch_size
== 32)
9366 for (rel
= relstart
; rel
< relend
; rel
++)
9368 unsigned long r_symndx
;
9370 struct elf_link_hash_entry
*h
;
9372 r_symndx
= rel
->r_info
>> r_sym_shift
;
9376 if (r_symndx
>= nlocsyms
9377 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
9379 h
= sym_hashes
[r_symndx
- extsymoff
];
9380 while (h
->root
.type
== bfd_link_hash_indirect
9381 || h
->root
.type
== bfd_link_hash_warning
)
9382 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9383 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
9387 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
9390 if (rsec
&& !rsec
->gc_mark
)
9392 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
9395 rsec
->gc_mark_from_eh
= 1;
9396 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
9405 if (elf_section_data (sec
)->relocs
!= relstart
)
9408 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
9410 if (! info
->keep_memory
)
9413 symtab_hdr
->contents
= (unsigned char *) isym
;
9420 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
9422 struct elf_gc_sweep_symbol_info
9424 struct bfd_link_info
*info
;
9425 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
9430 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
9432 if (h
->root
.type
== bfd_link_hash_warning
)
9433 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9435 if ((h
->root
.type
== bfd_link_hash_defined
9436 || h
->root
.type
== bfd_link_hash_defweak
)
9437 && !h
->root
.u
.def
.section
->gc_mark
9438 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
9440 struct elf_gc_sweep_symbol_info
*inf
= data
;
9441 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
9447 /* The sweep phase of garbage collection. Remove all garbage sections. */
9449 typedef bfd_boolean (*gc_sweep_hook_fn
)
9450 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
9453 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
9456 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9457 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
9458 unsigned long section_sym_count
;
9459 struct elf_gc_sweep_symbol_info sweep_info
;
9461 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9465 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9468 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9470 /* Keep debug and special sections. */
9471 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
9472 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
9478 /* Skip sweeping sections already excluded. */
9479 if (o
->flags
& SEC_EXCLUDE
)
9482 /* Since this is early in the link process, it is simple
9483 to remove a section from the output. */
9484 o
->flags
|= SEC_EXCLUDE
;
9486 if (info
->print_gc_sections
== TRUE
)
9487 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
9489 /* But we also have to update some of the relocation
9490 info we collected before. */
9492 && (o
->flags
& SEC_RELOC
) != 0
9493 && o
->reloc_count
> 0
9494 && !bfd_is_abs_section (o
->output_section
))
9496 Elf_Internal_Rela
*internal_relocs
;
9500 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
9502 if (internal_relocs
== NULL
)
9505 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
9507 if (elf_section_data (o
)->relocs
!= internal_relocs
)
9508 free (internal_relocs
);
9516 /* Remove the symbols that were in the swept sections from the dynamic
9517 symbol table. GCFIXME: Anyone know how to get them out of the
9518 static symbol table as well? */
9519 sweep_info
.info
= info
;
9520 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
9521 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
9524 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
9528 /* Propagate collected vtable information. This is called through
9529 elf_link_hash_traverse. */
9532 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
9534 if (h
->root
.type
== bfd_link_hash_warning
)
9535 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9537 /* Those that are not vtables. */
9538 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9541 /* Those vtables that do not have parents, we cannot merge. */
9542 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
9545 /* If we've already been done, exit. */
9546 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
9549 /* Make sure the parent's table is up to date. */
9550 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
9552 if (h
->vtable
->used
== NULL
)
9554 /* None of this table's entries were referenced. Re-use the
9556 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
9557 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
9562 bfd_boolean
*cu
, *pu
;
9564 /* Or the parent's entries into ours. */
9565 cu
= h
->vtable
->used
;
9567 pu
= h
->vtable
->parent
->vtable
->used
;
9570 const struct elf_backend_data
*bed
;
9571 unsigned int log_file_align
;
9573 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9574 log_file_align
= bed
->s
->log_file_align
;
9575 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9590 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9593 bfd_vma hstart
, hend
;
9594 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9595 const struct elf_backend_data
*bed
;
9596 unsigned int log_file_align
;
9598 if (h
->root
.type
== bfd_link_hash_warning
)
9599 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9601 /* Take care of both those symbols that do not describe vtables as
9602 well as those that are not loaded. */
9603 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9606 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9607 || h
->root
.type
== bfd_link_hash_defweak
);
9609 sec
= h
->root
.u
.def
.section
;
9610 hstart
= h
->root
.u
.def
.value
;
9611 hend
= hstart
+ h
->size
;
9613 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9615 return *(bfd_boolean
*) okp
= FALSE
;
9616 bed
= get_elf_backend_data (sec
->owner
);
9617 log_file_align
= bed
->s
->log_file_align
;
9619 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9621 for (rel
= relstart
; rel
< relend
; ++rel
)
9622 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9624 /* If the entry is in use, do nothing. */
9626 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9628 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9629 if (h
->vtable
->used
[entry
])
9632 /* Otherwise, kill it. */
9633 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9639 /* Mark sections containing dynamically referenced symbols. When
9640 building shared libraries, we must assume that any visible symbol is
9644 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9646 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9648 if (h
->root
.type
== bfd_link_hash_warning
)
9649 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9651 if ((h
->root
.type
== bfd_link_hash_defined
9652 || h
->root
.type
== bfd_link_hash_defweak
)
9654 || (!info
->executable
9656 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9657 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9658 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9663 /* Do mark and sweep of unused sections. */
9666 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9668 bfd_boolean ok
= TRUE
;
9670 asection
* (*gc_mark_hook
)
9671 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9672 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9673 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9675 if (!bed
->can_gc_sections
9676 || info
->relocatable
9677 || info
->emitrelocations
9678 || !is_elf_hash_table (info
->hash
))
9680 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9684 /* Apply transitive closure to the vtable entry usage info. */
9685 elf_link_hash_traverse (elf_hash_table (info
),
9686 elf_gc_propagate_vtable_entries_used
,
9691 /* Kill the vtable relocations that were not used. */
9692 elf_link_hash_traverse (elf_hash_table (info
),
9693 elf_gc_smash_unused_vtentry_relocs
,
9698 /* Mark dynamically referenced symbols. */
9699 if (elf_hash_table (info
)->dynamic_sections_created
)
9700 elf_link_hash_traverse (elf_hash_table (info
),
9701 bed
->gc_mark_dynamic_ref
,
9704 /* Grovel through relocs to find out who stays ... */
9705 gc_mark_hook
= bed
->gc_mark_hook
;
9706 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9710 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9713 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9714 if ((o
->flags
& SEC_KEEP
) != 0 && !o
->gc_mark
)
9715 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9719 /* ... again for sections marked from eh_frame. */
9720 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9724 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9727 /* Keep .gcc_except_table.* if the associated .text.* is
9728 marked. This isn't very nice, but the proper solution,
9729 splitting .eh_frame up and using comdat doesn't pan out
9730 easily due to needing special relocs to handle the
9731 difference of two symbols in separate sections.
9732 Don't keep code sections referenced by .eh_frame. */
9733 #define TEXT_PREFIX ".text."
9734 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
9735 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9736 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9738 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
9741 const char *sec_name
;
9743 unsigned o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
9744 unsigned fn_name_prefix_len
= strlen (TEXT_PREFIX
);
9746 sec_name
= o
->name
+ o_name_prefix_len
;
9747 fn_name
= bfd_malloc (strlen (sec_name
) + fn_name_prefix_len
+ 1);
9748 if (fn_name
== NULL
)
9750 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
9751 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9753 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9757 /* If not using specially named exception table section,
9758 then keep whatever we are using. */
9759 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9764 /* ... and mark SEC_EXCLUDE for those that go. */
9765 return elf_gc_sweep (abfd
, info
);
9768 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9771 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9773 struct elf_link_hash_entry
*h
,
9776 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9777 struct elf_link_hash_entry
**search
, *child
;
9778 bfd_size_type extsymcount
;
9779 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9781 /* The sh_info field of the symtab header tells us where the
9782 external symbols start. We don't care about the local symbols at
9784 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9785 if (!elf_bad_symtab (abfd
))
9786 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9788 sym_hashes
= elf_sym_hashes (abfd
);
9789 sym_hashes_end
= sym_hashes
+ extsymcount
;
9791 /* Hunt down the child symbol, which is in this section at the same
9792 offset as the relocation. */
9793 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9795 if ((child
= *search
) != NULL
9796 && (child
->root
.type
== bfd_link_hash_defined
9797 || child
->root
.type
== bfd_link_hash_defweak
)
9798 && child
->root
.u
.def
.section
== sec
9799 && child
->root
.u
.def
.value
== offset
)
9803 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9804 abfd
, sec
, (unsigned long) offset
);
9805 bfd_set_error (bfd_error_invalid_operation
);
9811 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9817 /* This *should* only be the absolute section. It could potentially
9818 be that someone has defined a non-global vtable though, which
9819 would be bad. It isn't worth paging in the local symbols to be
9820 sure though; that case should simply be handled by the assembler. */
9822 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9825 child
->vtable
->parent
= h
;
9830 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9833 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9834 asection
*sec ATTRIBUTE_UNUSED
,
9835 struct elf_link_hash_entry
*h
,
9838 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9839 unsigned int log_file_align
= bed
->s
->log_file_align
;
9843 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9848 if (addend
>= h
->vtable
->size
)
9850 size_t size
, bytes
, file_align
;
9851 bfd_boolean
*ptr
= h
->vtable
->used
;
9853 /* While the symbol is undefined, we have to be prepared to handle
9855 file_align
= 1 << log_file_align
;
9856 if (h
->root
.type
== bfd_link_hash_undefined
)
9857 size
= addend
+ file_align
;
9863 /* Oops! We've got a reference past the defined end of
9864 the table. This is probably a bug -- shall we warn? */
9865 size
= addend
+ file_align
;
9868 size
= (size
+ file_align
- 1) & -file_align
;
9870 /* Allocate one extra entry for use as a "done" flag for the
9871 consolidation pass. */
9872 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9876 ptr
= bfd_realloc (ptr
- 1, bytes
);
9882 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9883 * sizeof (bfd_boolean
));
9884 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9888 ptr
= bfd_zmalloc (bytes
);
9893 /* And arrange for that done flag to be at index -1. */
9894 h
->vtable
->used
= ptr
+ 1;
9895 h
->vtable
->size
= size
;
9898 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9903 struct alloc_got_off_arg
{
9905 unsigned int got_elt_size
;
9908 /* We need a special top-level link routine to convert got reference counts
9909 to real got offsets. */
9912 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9914 struct alloc_got_off_arg
*gofarg
= arg
;
9916 if (h
->root
.type
== bfd_link_hash_warning
)
9917 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9919 if (h
->got
.refcount
> 0)
9921 h
->got
.offset
= gofarg
->gotoff
;
9922 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9925 h
->got
.offset
= (bfd_vma
) -1;
9930 /* And an accompanying bit to work out final got entry offsets once
9931 we're done. Should be called from final_link. */
9934 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9935 struct bfd_link_info
*info
)
9938 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9940 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9941 struct alloc_got_off_arg gofarg
;
9943 if (! is_elf_hash_table (info
->hash
))
9946 /* The GOT offset is relative to the .got section, but the GOT header is
9947 put into the .got.plt section, if the backend uses it. */
9948 if (bed
->want_got_plt
)
9951 gotoff
= bed
->got_header_size
;
9953 /* Do the local .got entries first. */
9954 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9956 bfd_signed_vma
*local_got
;
9957 bfd_size_type j
, locsymcount
;
9958 Elf_Internal_Shdr
*symtab_hdr
;
9960 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9963 local_got
= elf_local_got_refcounts (i
);
9967 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9968 if (elf_bad_symtab (i
))
9969 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9971 locsymcount
= symtab_hdr
->sh_info
;
9973 for (j
= 0; j
< locsymcount
; ++j
)
9975 if (local_got
[j
] > 0)
9977 local_got
[j
] = gotoff
;
9978 gotoff
+= got_elt_size
;
9981 local_got
[j
] = (bfd_vma
) -1;
9985 /* Then the global .got entries. .plt refcounts are handled by
9986 adjust_dynamic_symbol */
9987 gofarg
.gotoff
= gotoff
;
9988 gofarg
.got_elt_size
= got_elt_size
;
9989 elf_link_hash_traverse (elf_hash_table (info
),
9990 elf_gc_allocate_got_offsets
,
9995 /* Many folk need no more in the way of final link than this, once
9996 got entry reference counting is enabled. */
9999 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10001 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
10004 /* Invoke the regular ELF backend linker to do all the work. */
10005 return bfd_elf_final_link (abfd
, info
);
10009 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
10011 struct elf_reloc_cookie
*rcookie
= cookie
;
10013 if (rcookie
->bad_symtab
)
10014 rcookie
->rel
= rcookie
->rels
;
10016 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
10018 unsigned long r_symndx
;
10020 if (! rcookie
->bad_symtab
)
10021 if (rcookie
->rel
->r_offset
> offset
)
10023 if (rcookie
->rel
->r_offset
!= offset
)
10026 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
10027 if (r_symndx
== SHN_UNDEF
)
10030 if (r_symndx
>= rcookie
->locsymcount
10031 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
10033 struct elf_link_hash_entry
*h
;
10035 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
10037 while (h
->root
.type
== bfd_link_hash_indirect
10038 || h
->root
.type
== bfd_link_hash_warning
)
10039 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10041 if ((h
->root
.type
== bfd_link_hash_defined
10042 || h
->root
.type
== bfd_link_hash_defweak
)
10043 && elf_discarded_section (h
->root
.u
.def
.section
))
10050 /* It's not a relocation against a global symbol,
10051 but it could be a relocation against a local
10052 symbol for a discarded section. */
10054 Elf_Internal_Sym
*isym
;
10056 /* Need to: get the symbol; get the section. */
10057 isym
= &rcookie
->locsyms
[r_symndx
];
10058 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
10060 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
10061 if (isec
!= NULL
&& elf_discarded_section (isec
))
10070 /* Discard unneeded references to discarded sections.
10071 Returns TRUE if any section's size was changed. */
10072 /* This function assumes that the relocations are in sorted order,
10073 which is true for all known assemblers. */
10076 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
10078 struct elf_reloc_cookie cookie
;
10079 asection
*stab
, *eh
;
10080 Elf_Internal_Shdr
*symtab_hdr
;
10081 const struct elf_backend_data
*bed
;
10083 unsigned int count
;
10084 bfd_boolean ret
= FALSE
;
10086 if (info
->traditional_format
10087 || !is_elf_hash_table (info
->hash
))
10090 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10092 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10095 bed
= get_elf_backend_data (abfd
);
10097 if ((abfd
->flags
& DYNAMIC
) != 0)
10100 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10101 if (info
->relocatable
10104 || bfd_is_abs_section (eh
->output_section
))))
10107 stab
= bfd_get_section_by_name (abfd
, ".stab");
10109 && (stab
->size
== 0
10110 || bfd_is_abs_section (stab
->output_section
)
10111 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10116 && bed
->elf_backend_discard_info
== NULL
)
10119 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10120 cookie
.abfd
= abfd
;
10121 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10122 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10123 if (cookie
.bad_symtab
)
10125 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10126 cookie
.extsymoff
= 0;
10130 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10131 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10134 if (bed
->s
->arch_size
== 32)
10135 cookie
.r_sym_shift
= 8;
10137 cookie
.r_sym_shift
= 32;
10139 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10140 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10142 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10143 cookie
.locsymcount
, 0,
10145 if (cookie
.locsyms
== NULL
)
10151 cookie
.rels
= NULL
;
10152 count
= stab
->reloc_count
;
10154 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
10155 info
->keep_memory
);
10156 if (cookie
.rels
!= NULL
)
10158 cookie
.rel
= cookie
.rels
;
10159 cookie
.relend
= cookie
.rels
;
10160 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10161 if (_bfd_discard_section_stabs (abfd
, stab
,
10162 elf_section_data (stab
)->sec_info
,
10163 bfd_elf_reloc_symbol_deleted_p
,
10166 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
10167 free (cookie
.rels
);
10173 cookie
.rels
= NULL
;
10174 count
= eh
->reloc_count
;
10176 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
10177 info
->keep_memory
);
10178 cookie
.rel
= cookie
.rels
;
10179 cookie
.relend
= cookie
.rels
;
10180 if (cookie
.rels
!= NULL
)
10181 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10183 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
10184 bfd_elf_reloc_symbol_deleted_p
,
10188 if (cookie
.rels
!= NULL
10189 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
10190 free (cookie
.rels
);
10193 if (bed
->elf_backend_discard_info
!= NULL
10194 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
10197 if (cookie
.locsyms
!= NULL
10198 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
10200 if (! info
->keep_memory
)
10201 free (cookie
.locsyms
);
10203 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
10207 if (info
->eh_frame_hdr
10208 && !info
->relocatable
10209 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
10216 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
10219 const char *name
, *p
;
10220 struct bfd_section_already_linked
*l
;
10221 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
10224 /* A single member comdat group section may be discarded by a
10225 linkonce section. See below. */
10226 if (sec
->output_section
== bfd_abs_section_ptr
)
10229 flags
= sec
->flags
;
10231 /* Check if it belongs to a section group. */
10232 group
= elf_sec_group (sec
);
10234 /* Return if it isn't a linkonce section nor a member of a group. A
10235 comdat group section also has SEC_LINK_ONCE set. */
10236 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
10241 /* If this is the member of a single member comdat group, check if
10242 the group should be discarded. */
10243 if (elf_next_in_group (sec
) == sec
10244 && (group
->flags
& SEC_LINK_ONCE
) != 0)
10250 /* FIXME: When doing a relocatable link, we may have trouble
10251 copying relocations in other sections that refer to local symbols
10252 in the section being discarded. Those relocations will have to
10253 be converted somehow; as of this writing I'm not sure that any of
10254 the backends handle that correctly.
10256 It is tempting to instead not discard link once sections when
10257 doing a relocatable link (technically, they should be discarded
10258 whenever we are building constructors). However, that fails,
10259 because the linker winds up combining all the link once sections
10260 into a single large link once section, which defeats the purpose
10261 of having link once sections in the first place.
10263 Also, not merging link once sections in a relocatable link
10264 causes trouble for MIPS ELF, which relies on link once semantics
10265 to handle the .reginfo section correctly. */
10267 name
= bfd_get_section_name (abfd
, sec
);
10269 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
10270 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
10275 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
10277 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10279 /* We may have 3 different sections on the list: group section,
10280 comdat section and linkonce section. SEC may be a linkonce or
10281 group section. We match a group section with a group section,
10282 a linkonce section with a linkonce section, and ignore comdat
10284 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
10285 && strcmp (name
, l
->sec
->name
) == 0
10286 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
10288 /* The section has already been linked. See if we should
10289 issue a warning. */
10290 switch (flags
& SEC_LINK_DUPLICATES
)
10295 case SEC_LINK_DUPLICATES_DISCARD
:
10298 case SEC_LINK_DUPLICATES_ONE_ONLY
:
10299 (*_bfd_error_handler
)
10300 (_("%B: ignoring duplicate section `%A'"),
10304 case SEC_LINK_DUPLICATES_SAME_SIZE
:
10305 if (sec
->size
!= l
->sec
->size
)
10306 (*_bfd_error_handler
)
10307 (_("%B: duplicate section `%A' has different size"),
10311 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
10312 if (sec
->size
!= l
->sec
->size
)
10313 (*_bfd_error_handler
)
10314 (_("%B: duplicate section `%A' has different size"),
10316 else if (sec
->size
!= 0)
10318 bfd_byte
*sec_contents
, *l_sec_contents
;
10320 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
10321 (*_bfd_error_handler
)
10322 (_("%B: warning: could not read contents of section `%A'"),
10324 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
10326 (*_bfd_error_handler
)
10327 (_("%B: warning: could not read contents of section `%A'"),
10328 l
->sec
->owner
, l
->sec
);
10329 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
10330 (*_bfd_error_handler
)
10331 (_("%B: warning: duplicate section `%A' has different contents"),
10335 free (sec_contents
);
10336 if (l_sec_contents
)
10337 free (l_sec_contents
);
10342 /* Set the output_section field so that lang_add_section
10343 does not create a lang_input_section structure for this
10344 section. Since there might be a symbol in the section
10345 being discarded, we must retain a pointer to the section
10346 which we are really going to use. */
10347 sec
->output_section
= bfd_abs_section_ptr
;
10348 sec
->kept_section
= l
->sec
;
10350 if (flags
& SEC_GROUP
)
10352 asection
*first
= elf_next_in_group (sec
);
10353 asection
*s
= first
;
10357 s
->output_section
= bfd_abs_section_ptr
;
10358 /* Record which group discards it. */
10359 s
->kept_section
= l
->sec
;
10360 s
= elf_next_in_group (s
);
10361 /* These lists are circular. */
10373 /* If this is the member of a single member comdat group and the
10374 group hasn't be discarded, we check if it matches a linkonce
10375 section. We only record the discarded comdat group. Otherwise
10376 the undiscarded group will be discarded incorrectly later since
10377 itself has been recorded. */
10378 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10379 if ((l
->sec
->flags
& SEC_GROUP
) == 0
10380 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
10381 && bfd_elf_match_symbols_in_sections (l
->sec
,
10382 elf_next_in_group (sec
)))
10384 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
10385 elf_next_in_group (sec
)->kept_section
= l
->sec
;
10386 group
->output_section
= bfd_abs_section_ptr
;
10393 /* There is no direct match. But for linkonce section, we should
10394 check if there is a match with comdat group member. We always
10395 record the linkonce section, discarded or not. */
10396 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10397 if (l
->sec
->flags
& SEC_GROUP
)
10399 asection
*first
= elf_next_in_group (l
->sec
);
10402 && elf_next_in_group (first
) == first
10403 && bfd_elf_match_symbols_in_sections (first
, sec
))
10405 sec
->output_section
= bfd_abs_section_ptr
;
10406 sec
->kept_section
= l
->sec
;
10411 /* This is the first section with this name. Record it. */
10412 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
10416 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
10418 return sym
->st_shndx
== SHN_COMMON
;
10422 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
10428 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
10430 return bfd_com_section_ptr
;