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 struct elf_link_hash_table
*htab
;
717 switch (elf_section_data (p
)->this_hdr
.sh_type
)
721 /* If sh_type is yet undecided, assume it could be
722 SHT_PROGBITS/SHT_NOBITS. */
724 htab
= elf_hash_table (info
);
725 if (p
== htab
->tls_sec
)
728 if (htab
->text_index_section
!= NULL
)
729 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
731 if (strcmp (p
->name
, ".got") == 0
732 || strcmp (p
->name
, ".got.plt") == 0
733 || strcmp (p
->name
, ".plt") == 0)
737 if (htab
->dynobj
!= NULL
738 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
739 && (ip
->flags
& SEC_LINKER_CREATED
)
740 && ip
->output_section
== p
)
745 /* There shouldn't be section relative relocations
746 against any other section. */
752 /* Assign dynsym indices. In a shared library we generate a section
753 symbol for each output section, which come first. Next come symbols
754 which have been forced to local binding. Then all of the back-end
755 allocated local dynamic syms, followed by the rest of the global
759 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
760 struct bfd_link_info
*info
,
761 unsigned long *section_sym_count
)
763 unsigned long dynsymcount
= 0;
765 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
767 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
769 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
770 if ((p
->flags
& SEC_EXCLUDE
) == 0
771 && (p
->flags
& SEC_ALLOC
) != 0
772 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
773 elf_section_data (p
)->dynindx
= ++dynsymcount
;
775 elf_section_data (p
)->dynindx
= 0;
777 *section_sym_count
= dynsymcount
;
779 elf_link_hash_traverse (elf_hash_table (info
),
780 elf_link_renumber_local_hash_table_dynsyms
,
783 if (elf_hash_table (info
)->dynlocal
)
785 struct elf_link_local_dynamic_entry
*p
;
786 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
787 p
->dynindx
= ++dynsymcount
;
790 elf_link_hash_traverse (elf_hash_table (info
),
791 elf_link_renumber_hash_table_dynsyms
,
794 /* There is an unused NULL entry at the head of the table which
795 we must account for in our count. Unless there weren't any
796 symbols, which means we'll have no table at all. */
797 if (dynsymcount
!= 0)
800 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
804 /* This function is called when we want to define a new symbol. It
805 handles the various cases which arise when we find a definition in
806 a dynamic object, or when there is already a definition in a
807 dynamic object. The new symbol is described by NAME, SYM, PSEC,
808 and PVALUE. We set SYM_HASH to the hash table entry. We set
809 OVERRIDE if the old symbol is overriding a new definition. We set
810 TYPE_CHANGE_OK if it is OK for the type to change. We set
811 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
812 change, we mean that we shouldn't warn if the type or size does
813 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
814 object is overridden by a regular object. */
817 _bfd_elf_merge_symbol (bfd
*abfd
,
818 struct bfd_link_info
*info
,
820 Elf_Internal_Sym
*sym
,
823 unsigned int *pold_alignment
,
824 struct elf_link_hash_entry
**sym_hash
,
826 bfd_boolean
*override
,
827 bfd_boolean
*type_change_ok
,
828 bfd_boolean
*size_change_ok
)
830 asection
*sec
, *oldsec
;
831 struct elf_link_hash_entry
*h
;
832 struct elf_link_hash_entry
*flip
;
835 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
836 bfd_boolean newweak
, oldweak
;
837 const struct elf_backend_data
*bed
;
843 bind
= ELF_ST_BIND (sym
->st_info
);
845 /* Silently discard TLS symbols from --just-syms. There's no way to
846 combine a static TLS block with a new TLS block for this executable. */
847 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
848 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
854 if (! bfd_is_und_section (sec
))
855 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
857 h
= ((struct elf_link_hash_entry
*)
858 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
863 /* This code is for coping with dynamic objects, and is only useful
864 if we are doing an ELF link. */
865 if (info
->hash
->creator
!= abfd
->xvec
)
868 /* For merging, we only care about real symbols. */
870 while (h
->root
.type
== bfd_link_hash_indirect
871 || h
->root
.type
== bfd_link_hash_warning
)
872 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
874 /* If we just created the symbol, mark it as being an ELF symbol.
875 Other than that, there is nothing to do--there is no merge issue
876 with a newly defined symbol--so we just return. */
878 if (h
->root
.type
== bfd_link_hash_new
)
880 bfd_elf_link_mark_dynamic_symbol (info
, h
);
885 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
888 switch (h
->root
.type
)
895 case bfd_link_hash_undefined
:
896 case bfd_link_hash_undefweak
:
897 oldbfd
= h
->root
.u
.undef
.abfd
;
901 case bfd_link_hash_defined
:
902 case bfd_link_hash_defweak
:
903 oldbfd
= h
->root
.u
.def
.section
->owner
;
904 oldsec
= h
->root
.u
.def
.section
;
907 case bfd_link_hash_common
:
908 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
909 oldsec
= h
->root
.u
.c
.p
->section
;
913 /* In cases involving weak versioned symbols, we may wind up trying
914 to merge a symbol with itself. Catch that here, to avoid the
915 confusion that results if we try to override a symbol with
916 itself. The additional tests catch cases like
917 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
918 dynamic object, which we do want to handle here. */
920 && ((abfd
->flags
& DYNAMIC
) == 0
924 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
925 respectively, is from a dynamic object. */
927 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
931 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
932 else if (oldsec
!= NULL
)
934 /* This handles the special SHN_MIPS_{TEXT,DATA} section
935 indices used by MIPS ELF. */
936 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
939 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
940 respectively, appear to be a definition rather than reference. */
942 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
944 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
945 && h
->root
.type
!= bfd_link_hash_undefweak
946 && h
->root
.type
!= bfd_link_hash_common
);
948 /* When we try to create a default indirect symbol from the dynamic
949 definition with the default version, we skip it if its type and
950 the type of existing regular definition mismatch. We only do it
951 if the existing regular definition won't be dynamic. */
952 if (pold_alignment
== NULL
954 && !info
->export_dynamic
959 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
960 && ELF_ST_TYPE (sym
->st_info
) != h
->type
961 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
962 && h
->type
!= STT_NOTYPE
)
968 /* Check TLS symbol. We don't check undefined symbol introduced by
970 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
971 && ELF_ST_TYPE (sym
->st_info
) != h
->type
975 bfd_boolean ntdef
, tdef
;
976 asection
*ntsec
, *tsec
;
978 if (h
->type
== STT_TLS
)
998 (*_bfd_error_handler
)
999 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1000 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1001 else if (!tdef
&& !ntdef
)
1002 (*_bfd_error_handler
)
1003 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1004 tbfd
, ntbfd
, h
->root
.root
.string
);
1006 (*_bfd_error_handler
)
1007 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1008 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1010 (*_bfd_error_handler
)
1011 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1012 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1014 bfd_set_error (bfd_error_bad_value
);
1018 /* We need to remember if a symbol has a definition in a dynamic
1019 object or is weak in all dynamic objects. Internal and hidden
1020 visibility will make it unavailable to dynamic objects. */
1021 if (newdyn
&& !h
->dynamic_def
)
1023 if (!bfd_is_und_section (sec
))
1027 /* Check if this symbol is weak in all dynamic objects. If it
1028 is the first time we see it in a dynamic object, we mark
1029 if it is weak. Otherwise, we clear it. */
1030 if (!h
->ref_dynamic
)
1032 if (bind
== STB_WEAK
)
1033 h
->dynamic_weak
= 1;
1035 else if (bind
!= STB_WEAK
)
1036 h
->dynamic_weak
= 0;
1040 /* If the old symbol has non-default visibility, we ignore the new
1041 definition from a dynamic object. */
1043 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1044 && !bfd_is_und_section (sec
))
1047 /* Make sure this symbol is dynamic. */
1049 /* A protected symbol has external availability. Make sure it is
1050 recorded as dynamic.
1052 FIXME: Should we check type and size for protected symbol? */
1053 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1054 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1059 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1062 /* If the new symbol with non-default visibility comes from a
1063 relocatable file and the old definition comes from a dynamic
1064 object, we remove the old definition. */
1065 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1067 /* Handle the case where the old dynamic definition is
1068 default versioned. We need to copy the symbol info from
1069 the symbol with default version to the normal one if it
1070 was referenced before. */
1073 const struct elf_backend_data
*bed
1074 = get_elf_backend_data (abfd
);
1075 struct elf_link_hash_entry
*vh
= *sym_hash
;
1076 vh
->root
.type
= h
->root
.type
;
1077 h
->root
.type
= bfd_link_hash_indirect
;
1078 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1079 /* Protected symbols will override the dynamic definition
1080 with default version. */
1081 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1083 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1084 vh
->dynamic_def
= 1;
1085 vh
->ref_dynamic
= 1;
1089 h
->root
.type
= vh
->root
.type
;
1090 vh
->ref_dynamic
= 0;
1091 /* We have to hide it here since it was made dynamic
1092 global with extra bits when the symbol info was
1093 copied from the old dynamic definition. */
1094 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1102 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1103 && bfd_is_und_section (sec
))
1105 /* If the new symbol is undefined and the old symbol was
1106 also undefined before, we need to make sure
1107 _bfd_generic_link_add_one_symbol doesn't mess
1108 up the linker hash table undefs list. Since the old
1109 definition came from a dynamic object, it is still on the
1111 h
->root
.type
= bfd_link_hash_undefined
;
1112 h
->root
.u
.undef
.abfd
= abfd
;
1116 h
->root
.type
= bfd_link_hash_new
;
1117 h
->root
.u
.undef
.abfd
= NULL
;
1126 /* FIXME: Should we check type and size for protected symbol? */
1132 /* Differentiate strong and weak symbols. */
1133 newweak
= bind
== STB_WEAK
;
1134 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1135 || h
->root
.type
== bfd_link_hash_undefweak
);
1137 /* If a new weak symbol definition comes from a regular file and the
1138 old symbol comes from a dynamic library, we treat the new one as
1139 strong. Similarly, an old weak symbol definition from a regular
1140 file is treated as strong when the new symbol comes from a dynamic
1141 library. Further, an old weak symbol from a dynamic library is
1142 treated as strong if the new symbol is from a dynamic library.
1143 This reflects the way glibc's ld.so works.
1145 Do this before setting *type_change_ok or *size_change_ok so that
1146 we warn properly when dynamic library symbols are overridden. */
1148 if (newdef
&& !newdyn
&& olddyn
)
1150 if (olddef
&& newdyn
)
1153 /* It's OK to change the type if either the existing symbol or the
1154 new symbol is weak. A type change is also OK if the old symbol
1155 is undefined and the new symbol is defined. */
1160 && h
->root
.type
== bfd_link_hash_undefined
))
1161 *type_change_ok
= TRUE
;
1163 /* It's OK to change the size if either the existing symbol or the
1164 new symbol is weak, or if the old symbol is undefined. */
1167 || h
->root
.type
== bfd_link_hash_undefined
)
1168 *size_change_ok
= TRUE
;
1170 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1171 symbol, respectively, appears to be a common symbol in a dynamic
1172 object. If a symbol appears in an uninitialized section, and is
1173 not weak, and is not a function, then it may be a common symbol
1174 which was resolved when the dynamic object was created. We want
1175 to treat such symbols specially, because they raise special
1176 considerations when setting the symbol size: if the symbol
1177 appears as a common symbol in a regular object, and the size in
1178 the regular object is larger, we must make sure that we use the
1179 larger size. This problematic case can always be avoided in C,
1180 but it must be handled correctly when using Fortran shared
1183 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1184 likewise for OLDDYNCOMMON and OLDDEF.
1186 Note that this test is just a heuristic, and that it is quite
1187 possible to have an uninitialized symbol in a shared object which
1188 is really a definition, rather than a common symbol. This could
1189 lead to some minor confusion when the symbol really is a common
1190 symbol in some regular object. However, I think it will be
1196 && (sec
->flags
& SEC_ALLOC
) != 0
1197 && (sec
->flags
& SEC_LOAD
) == 0
1199 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1200 newdyncommon
= TRUE
;
1202 newdyncommon
= FALSE
;
1206 && h
->root
.type
== bfd_link_hash_defined
1208 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1209 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1211 && h
->type
!= STT_FUNC
)
1212 olddyncommon
= TRUE
;
1214 olddyncommon
= FALSE
;
1216 /* We now know everything about the old and new symbols. We ask the
1217 backend to check if we can merge them. */
1218 bed
= get_elf_backend_data (abfd
);
1219 if (bed
->merge_symbol
1220 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1221 pold_alignment
, skip
, override
,
1222 type_change_ok
, size_change_ok
,
1223 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1225 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1229 /* If both the old and the new symbols look like common symbols in a
1230 dynamic object, set the size of the symbol to the larger of the
1235 && sym
->st_size
!= h
->size
)
1237 /* Since we think we have two common symbols, issue a multiple
1238 common warning if desired. Note that we only warn if the
1239 size is different. If the size is the same, we simply let
1240 the old symbol override the new one as normally happens with
1241 symbols defined in dynamic objects. */
1243 if (! ((*info
->callbacks
->multiple_common
)
1244 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1245 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1248 if (sym
->st_size
> h
->size
)
1249 h
->size
= sym
->st_size
;
1251 *size_change_ok
= TRUE
;
1254 /* If we are looking at a dynamic object, and we have found a
1255 definition, we need to see if the symbol was already defined by
1256 some other object. If so, we want to use the existing
1257 definition, and we do not want to report a multiple symbol
1258 definition error; we do this by clobbering *PSEC to be
1259 bfd_und_section_ptr.
1261 We treat a common symbol as a definition if the symbol in the
1262 shared library is a function, since common symbols always
1263 represent variables; this can cause confusion in principle, but
1264 any such confusion would seem to indicate an erroneous program or
1265 shared library. We also permit a common symbol in a regular
1266 object to override a weak symbol in a shared object. */
1271 || (h
->root
.type
== bfd_link_hash_common
1273 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1277 newdyncommon
= FALSE
;
1279 *psec
= sec
= bfd_und_section_ptr
;
1280 *size_change_ok
= TRUE
;
1282 /* If we get here when the old symbol is a common symbol, then
1283 we are explicitly letting it override a weak symbol or
1284 function in a dynamic object, and we don't want to warn about
1285 a type change. If the old symbol is a defined symbol, a type
1286 change warning may still be appropriate. */
1288 if (h
->root
.type
== bfd_link_hash_common
)
1289 *type_change_ok
= TRUE
;
1292 /* Handle the special case of an old common symbol merging with a
1293 new symbol which looks like a common symbol in a shared object.
1294 We change *PSEC and *PVALUE to make the new symbol look like a
1295 common symbol, and let _bfd_generic_link_add_one_symbol do the
1299 && h
->root
.type
== bfd_link_hash_common
)
1303 newdyncommon
= FALSE
;
1304 *pvalue
= sym
->st_size
;
1305 *psec
= sec
= bed
->common_section (oldsec
);
1306 *size_change_ok
= TRUE
;
1309 /* Skip weak definitions of symbols that are already defined. */
1310 if (newdef
&& olddef
&& newweak
)
1313 /* If the old symbol is from a dynamic object, and the new symbol is
1314 a definition which is not from a dynamic object, then the new
1315 symbol overrides the old symbol. Symbols from regular files
1316 always take precedence over symbols from dynamic objects, even if
1317 they are defined after the dynamic object in the link.
1319 As above, we again permit a common symbol in a regular object to
1320 override a definition in a shared object if the shared object
1321 symbol is a function or is weak. */
1326 || (bfd_is_com_section (sec
)
1328 || h
->type
== STT_FUNC
)))
1333 /* Change the hash table entry to undefined, and let
1334 _bfd_generic_link_add_one_symbol do the right thing with the
1337 h
->root
.type
= bfd_link_hash_undefined
;
1338 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1339 *size_change_ok
= TRUE
;
1342 olddyncommon
= FALSE
;
1344 /* We again permit a type change when a common symbol may be
1345 overriding a function. */
1347 if (bfd_is_com_section (sec
))
1348 *type_change_ok
= TRUE
;
1350 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1353 /* This union may have been set to be non-NULL when this symbol
1354 was seen in a dynamic object. We must force the union to be
1355 NULL, so that it is correct for a regular symbol. */
1356 h
->verinfo
.vertree
= NULL
;
1359 /* Handle the special case of a new common symbol merging with an
1360 old symbol that looks like it might be a common symbol defined in
1361 a shared object. Note that we have already handled the case in
1362 which a new common symbol should simply override the definition
1363 in the shared library. */
1366 && bfd_is_com_section (sec
)
1369 /* It would be best if we could set the hash table entry to a
1370 common symbol, but we don't know what to use for the section
1371 or the alignment. */
1372 if (! ((*info
->callbacks
->multiple_common
)
1373 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1374 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1377 /* If the presumed common symbol in the dynamic object is
1378 larger, pretend that the new symbol has its size. */
1380 if (h
->size
> *pvalue
)
1383 /* We need to remember the alignment required by the symbol
1384 in the dynamic object. */
1385 BFD_ASSERT (pold_alignment
);
1386 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1389 olddyncommon
= FALSE
;
1391 h
->root
.type
= bfd_link_hash_undefined
;
1392 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1394 *size_change_ok
= TRUE
;
1395 *type_change_ok
= TRUE
;
1397 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1400 h
->verinfo
.vertree
= NULL
;
1405 /* Handle the case where we had a versioned symbol in a dynamic
1406 library and now find a definition in a normal object. In this
1407 case, we make the versioned symbol point to the normal one. */
1408 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1409 flip
->root
.type
= h
->root
.type
;
1410 h
->root
.type
= bfd_link_hash_indirect
;
1411 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1412 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1413 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1417 flip
->ref_dynamic
= 1;
1424 /* This function is called to create an indirect symbol from the
1425 default for the symbol with the default version if needed. The
1426 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1427 set DYNSYM if the new indirect symbol is dynamic. */
1430 _bfd_elf_add_default_symbol (bfd
*abfd
,
1431 struct bfd_link_info
*info
,
1432 struct elf_link_hash_entry
*h
,
1434 Elf_Internal_Sym
*sym
,
1437 bfd_boolean
*dynsym
,
1438 bfd_boolean override
)
1440 bfd_boolean type_change_ok
;
1441 bfd_boolean size_change_ok
;
1444 struct elf_link_hash_entry
*hi
;
1445 struct bfd_link_hash_entry
*bh
;
1446 const struct elf_backend_data
*bed
;
1447 bfd_boolean collect
;
1448 bfd_boolean dynamic
;
1450 size_t len
, shortlen
;
1453 /* If this symbol has a version, and it is the default version, we
1454 create an indirect symbol from the default name to the fully
1455 decorated name. This will cause external references which do not
1456 specify a version to be bound to this version of the symbol. */
1457 p
= strchr (name
, ELF_VER_CHR
);
1458 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1463 /* We are overridden by an old definition. We need to check if we
1464 need to create the indirect symbol from the default name. */
1465 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1467 BFD_ASSERT (hi
!= NULL
);
1470 while (hi
->root
.type
== bfd_link_hash_indirect
1471 || hi
->root
.type
== bfd_link_hash_warning
)
1473 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1479 bed
= get_elf_backend_data (abfd
);
1480 collect
= bed
->collect
;
1481 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1483 shortlen
= p
- name
;
1484 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1485 if (shortname
== NULL
)
1487 memcpy (shortname
, name
, shortlen
);
1488 shortname
[shortlen
] = '\0';
1490 /* We are going to create a new symbol. Merge it with any existing
1491 symbol with this name. For the purposes of the merge, act as
1492 though we were defining the symbol we just defined, although we
1493 actually going to define an indirect symbol. */
1494 type_change_ok
= FALSE
;
1495 size_change_ok
= FALSE
;
1497 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1498 NULL
, &hi
, &skip
, &override
,
1499 &type_change_ok
, &size_change_ok
))
1508 if (! (_bfd_generic_link_add_one_symbol
1509 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1510 0, name
, FALSE
, collect
, &bh
)))
1512 hi
= (struct elf_link_hash_entry
*) bh
;
1516 /* In this case the symbol named SHORTNAME is overriding the
1517 indirect symbol we want to add. We were planning on making
1518 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1519 is the name without a version. NAME is the fully versioned
1520 name, and it is the default version.
1522 Overriding means that we already saw a definition for the
1523 symbol SHORTNAME in a regular object, and it is overriding
1524 the symbol defined in the dynamic object.
1526 When this happens, we actually want to change NAME, the
1527 symbol we just added, to refer to SHORTNAME. This will cause
1528 references to NAME in the shared object to become references
1529 to SHORTNAME in the regular object. This is what we expect
1530 when we override a function in a shared object: that the
1531 references in the shared object will be mapped to the
1532 definition in the regular object. */
1534 while (hi
->root
.type
== bfd_link_hash_indirect
1535 || hi
->root
.type
== bfd_link_hash_warning
)
1536 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1538 h
->root
.type
= bfd_link_hash_indirect
;
1539 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1543 hi
->ref_dynamic
= 1;
1547 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1552 /* Now set HI to H, so that the following code will set the
1553 other fields correctly. */
1557 /* If there is a duplicate definition somewhere, then HI may not
1558 point to an indirect symbol. We will have reported an error to
1559 the user in that case. */
1561 if (hi
->root
.type
== bfd_link_hash_indirect
)
1563 struct elf_link_hash_entry
*ht
;
1565 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1566 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1568 /* See if the new flags lead us to realize that the symbol must
1580 if (hi
->ref_regular
)
1586 /* We also need to define an indirection from the nondefault version
1590 len
= strlen (name
);
1591 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1592 if (shortname
== NULL
)
1594 memcpy (shortname
, name
, shortlen
);
1595 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1597 /* Once again, merge with any existing symbol. */
1598 type_change_ok
= FALSE
;
1599 size_change_ok
= FALSE
;
1601 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1602 NULL
, &hi
, &skip
, &override
,
1603 &type_change_ok
, &size_change_ok
))
1611 /* Here SHORTNAME is a versioned name, so we don't expect to see
1612 the type of override we do in the case above unless it is
1613 overridden by a versioned definition. */
1614 if (hi
->root
.type
!= bfd_link_hash_defined
1615 && hi
->root
.type
!= bfd_link_hash_defweak
)
1616 (*_bfd_error_handler
)
1617 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1623 if (! (_bfd_generic_link_add_one_symbol
1624 (info
, abfd
, shortname
, BSF_INDIRECT
,
1625 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1627 hi
= (struct elf_link_hash_entry
*) bh
;
1629 /* If there is a duplicate definition somewhere, then HI may not
1630 point to an indirect symbol. We will have reported an error
1631 to the user in that case. */
1633 if (hi
->root
.type
== bfd_link_hash_indirect
)
1635 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1637 /* See if the new flags lead us to realize that the symbol
1649 if (hi
->ref_regular
)
1659 /* This routine is used to export all defined symbols into the dynamic
1660 symbol table. It is called via elf_link_hash_traverse. */
1663 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1665 struct elf_info_failed
*eif
= data
;
1667 /* Ignore this if we won't export it. */
1668 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1671 /* Ignore indirect symbols. These are added by the versioning code. */
1672 if (h
->root
.type
== bfd_link_hash_indirect
)
1675 if (h
->root
.type
== bfd_link_hash_warning
)
1676 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1678 if (h
->dynindx
== -1
1682 struct bfd_elf_version_tree
*t
;
1683 struct bfd_elf_version_expr
*d
;
1685 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1687 if (t
->globals
.list
!= NULL
)
1689 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1694 if (t
->locals
.list
!= NULL
)
1696 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1705 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1716 /* Look through the symbols which are defined in other shared
1717 libraries and referenced here. Update the list of version
1718 dependencies. This will be put into the .gnu.version_r section.
1719 This function is called via elf_link_hash_traverse. */
1722 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1725 struct elf_find_verdep_info
*rinfo
= data
;
1726 Elf_Internal_Verneed
*t
;
1727 Elf_Internal_Vernaux
*a
;
1730 if (h
->root
.type
== bfd_link_hash_warning
)
1731 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1733 /* We only care about symbols defined in shared objects with version
1738 || h
->verinfo
.verdef
== NULL
)
1741 /* See if we already know about this version. */
1742 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1744 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1747 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1748 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1754 /* This is a new version. Add it to tree we are building. */
1759 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1762 rinfo
->failed
= TRUE
;
1766 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1767 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1768 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1772 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1774 /* Note that we are copying a string pointer here, and testing it
1775 above. If bfd_elf_string_from_elf_section is ever changed to
1776 discard the string data when low in memory, this will have to be
1778 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1780 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1781 a
->vna_nextptr
= t
->vn_auxptr
;
1783 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1786 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1793 /* Figure out appropriate versions for all the symbols. We may not
1794 have the version number script until we have read all of the input
1795 files, so until that point we don't know which symbols should be
1796 local. This function is called via elf_link_hash_traverse. */
1799 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1801 struct elf_assign_sym_version_info
*sinfo
;
1802 struct bfd_link_info
*info
;
1803 const struct elf_backend_data
*bed
;
1804 struct elf_info_failed eif
;
1811 if (h
->root
.type
== bfd_link_hash_warning
)
1812 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1814 /* Fix the symbol flags. */
1817 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1820 sinfo
->failed
= TRUE
;
1824 /* We only need version numbers for symbols defined in regular
1826 if (!h
->def_regular
)
1829 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1830 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1831 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1833 struct bfd_elf_version_tree
*t
;
1838 /* There are two consecutive ELF_VER_CHR characters if this is
1839 not a hidden symbol. */
1841 if (*p
== ELF_VER_CHR
)
1847 /* If there is no version string, we can just return out. */
1855 /* Look for the version. If we find it, it is no longer weak. */
1856 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1858 if (strcmp (t
->name
, p
) == 0)
1862 struct bfd_elf_version_expr
*d
;
1864 len
= p
- h
->root
.root
.string
;
1865 alc
= bfd_malloc (len
);
1868 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1869 alc
[len
- 1] = '\0';
1870 if (alc
[len
- 2] == ELF_VER_CHR
)
1871 alc
[len
- 2] = '\0';
1873 h
->verinfo
.vertree
= t
;
1877 if (t
->globals
.list
!= NULL
)
1878 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1880 /* See if there is anything to force this symbol to
1882 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1884 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1887 && ! info
->export_dynamic
)
1888 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1896 /* If we are building an application, we need to create a
1897 version node for this version. */
1898 if (t
== NULL
&& info
->executable
)
1900 struct bfd_elf_version_tree
**pp
;
1903 /* If we aren't going to export this symbol, we don't need
1904 to worry about it. */
1905 if (h
->dynindx
== -1)
1909 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1912 sinfo
->failed
= TRUE
;
1917 t
->name_indx
= (unsigned int) -1;
1921 /* Don't count anonymous version tag. */
1922 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1924 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1926 t
->vernum
= version_index
;
1930 h
->verinfo
.vertree
= t
;
1934 /* We could not find the version for a symbol when
1935 generating a shared archive. Return an error. */
1936 (*_bfd_error_handler
)
1937 (_("%B: undefined versioned symbol name %s"),
1938 sinfo
->output_bfd
, h
->root
.root
.string
);
1939 bfd_set_error (bfd_error_bad_value
);
1940 sinfo
->failed
= TRUE
;
1948 /* If we don't have a version for this symbol, see if we can find
1950 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1952 struct bfd_elf_version_tree
*t
;
1953 struct bfd_elf_version_tree
*local_ver
;
1954 struct bfd_elf_version_expr
*d
;
1956 /* See if can find what version this symbol is in. If the
1957 symbol is supposed to be local, then don't actually register
1960 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1962 if (t
->globals
.list
!= NULL
)
1964 bfd_boolean matched
;
1968 while ((d
= (*t
->match
) (&t
->globals
, d
,
1969 h
->root
.root
.string
)) != NULL
)
1974 /* There is a version without definition. Make
1975 the symbol the default definition for this
1977 h
->verinfo
.vertree
= t
;
1985 /* There is no undefined version for this symbol. Hide the
1987 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1990 if (t
->locals
.list
!= NULL
)
1993 while ((d
= (*t
->match
) (&t
->locals
, d
,
1994 h
->root
.root
.string
)) != NULL
)
1997 /* If the match is "*", keep looking for a more
1998 explicit, perhaps even global, match.
1999 XXX: Shouldn't this be !d->wildcard instead? */
2000 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2009 if (local_ver
!= NULL
)
2011 h
->verinfo
.vertree
= local_ver
;
2012 if (h
->dynindx
!= -1
2013 && ! info
->export_dynamic
)
2015 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2023 /* Read and swap the relocs from the section indicated by SHDR. This
2024 may be either a REL or a RELA section. The relocations are
2025 translated into RELA relocations and stored in INTERNAL_RELOCS,
2026 which should have already been allocated to contain enough space.
2027 The EXTERNAL_RELOCS are a buffer where the external form of the
2028 relocations should be stored.
2030 Returns FALSE if something goes wrong. */
2033 elf_link_read_relocs_from_section (bfd
*abfd
,
2035 Elf_Internal_Shdr
*shdr
,
2036 void *external_relocs
,
2037 Elf_Internal_Rela
*internal_relocs
)
2039 const struct elf_backend_data
*bed
;
2040 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2041 const bfd_byte
*erela
;
2042 const bfd_byte
*erelaend
;
2043 Elf_Internal_Rela
*irela
;
2044 Elf_Internal_Shdr
*symtab_hdr
;
2047 /* Position ourselves at the start of the section. */
2048 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2051 /* Read the relocations. */
2052 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2055 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2056 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2058 bed
= get_elf_backend_data (abfd
);
2060 /* Convert the external relocations to the internal format. */
2061 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2062 swap_in
= bed
->s
->swap_reloc_in
;
2063 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2064 swap_in
= bed
->s
->swap_reloca_in
;
2067 bfd_set_error (bfd_error_wrong_format
);
2071 erela
= external_relocs
;
2072 erelaend
= erela
+ shdr
->sh_size
;
2073 irela
= internal_relocs
;
2074 while (erela
< erelaend
)
2078 (*swap_in
) (abfd
, erela
, irela
);
2079 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2080 if (bed
->s
->arch_size
== 64)
2082 if ((size_t) r_symndx
>= nsyms
)
2084 (*_bfd_error_handler
)
2085 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2086 " for offset 0x%lx in section `%A'"),
2088 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2089 bfd_set_error (bfd_error_bad_value
);
2092 irela
+= bed
->s
->int_rels_per_ext_rel
;
2093 erela
+= shdr
->sh_entsize
;
2099 /* Read and swap the relocs for a section O. They may have been
2100 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2101 not NULL, they are used as buffers to read into. They are known to
2102 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2103 the return value is allocated using either malloc or bfd_alloc,
2104 according to the KEEP_MEMORY argument. If O has two relocation
2105 sections (both REL and RELA relocations), then the REL_HDR
2106 relocations will appear first in INTERNAL_RELOCS, followed by the
2107 REL_HDR2 relocations. */
2110 _bfd_elf_link_read_relocs (bfd
*abfd
,
2112 void *external_relocs
,
2113 Elf_Internal_Rela
*internal_relocs
,
2114 bfd_boolean keep_memory
)
2116 Elf_Internal_Shdr
*rel_hdr
;
2117 void *alloc1
= NULL
;
2118 Elf_Internal_Rela
*alloc2
= NULL
;
2119 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2121 if (elf_section_data (o
)->relocs
!= NULL
)
2122 return elf_section_data (o
)->relocs
;
2124 if (o
->reloc_count
== 0)
2127 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2129 if (internal_relocs
== NULL
)
2133 size
= o
->reloc_count
;
2134 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2136 internal_relocs
= bfd_alloc (abfd
, size
);
2138 internal_relocs
= alloc2
= bfd_malloc (size
);
2139 if (internal_relocs
== NULL
)
2143 if (external_relocs
== NULL
)
2145 bfd_size_type size
= rel_hdr
->sh_size
;
2147 if (elf_section_data (o
)->rel_hdr2
)
2148 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2149 alloc1
= bfd_malloc (size
);
2152 external_relocs
= alloc1
;
2155 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2159 if (elf_section_data (o
)->rel_hdr2
2160 && (!elf_link_read_relocs_from_section
2162 elf_section_data (o
)->rel_hdr2
,
2163 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2164 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2165 * bed
->s
->int_rels_per_ext_rel
))))
2168 /* Cache the results for next time, if we can. */
2170 elf_section_data (o
)->relocs
= internal_relocs
;
2175 /* Don't free alloc2, since if it was allocated we are passing it
2176 back (under the name of internal_relocs). */
2178 return internal_relocs
;
2188 /* Compute the size of, and allocate space for, REL_HDR which is the
2189 section header for a section containing relocations for O. */
2192 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2193 Elf_Internal_Shdr
*rel_hdr
,
2196 bfd_size_type reloc_count
;
2197 bfd_size_type num_rel_hashes
;
2199 /* Figure out how many relocations there will be. */
2200 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2201 reloc_count
= elf_section_data (o
)->rel_count
;
2203 reloc_count
= elf_section_data (o
)->rel_count2
;
2205 num_rel_hashes
= o
->reloc_count
;
2206 if (num_rel_hashes
< reloc_count
)
2207 num_rel_hashes
= reloc_count
;
2209 /* That allows us to calculate the size of the section. */
2210 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2212 /* The contents field must last into write_object_contents, so we
2213 allocate it with bfd_alloc rather than malloc. Also since we
2214 cannot be sure that the contents will actually be filled in,
2215 we zero the allocated space. */
2216 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2217 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2220 /* We only allocate one set of hash entries, so we only do it the
2221 first time we are called. */
2222 if (elf_section_data (o
)->rel_hashes
== NULL
2225 struct elf_link_hash_entry
**p
;
2227 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2231 elf_section_data (o
)->rel_hashes
= p
;
2237 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2238 originated from the section given by INPUT_REL_HDR) to the
2242 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2243 asection
*input_section
,
2244 Elf_Internal_Shdr
*input_rel_hdr
,
2245 Elf_Internal_Rela
*internal_relocs
,
2246 struct elf_link_hash_entry
**rel_hash
2249 Elf_Internal_Rela
*irela
;
2250 Elf_Internal_Rela
*irelaend
;
2252 Elf_Internal_Shdr
*output_rel_hdr
;
2253 asection
*output_section
;
2254 unsigned int *rel_countp
= NULL
;
2255 const struct elf_backend_data
*bed
;
2256 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2258 output_section
= input_section
->output_section
;
2259 output_rel_hdr
= NULL
;
2261 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2262 == input_rel_hdr
->sh_entsize
)
2264 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2265 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2267 else if (elf_section_data (output_section
)->rel_hdr2
2268 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2269 == input_rel_hdr
->sh_entsize
))
2271 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2272 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2276 (*_bfd_error_handler
)
2277 (_("%B: relocation size mismatch in %B section %A"),
2278 output_bfd
, input_section
->owner
, input_section
);
2279 bfd_set_error (bfd_error_wrong_object_format
);
2283 bed
= get_elf_backend_data (output_bfd
);
2284 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2285 swap_out
= bed
->s
->swap_reloc_out
;
2286 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2287 swap_out
= bed
->s
->swap_reloca_out
;
2291 erel
= output_rel_hdr
->contents
;
2292 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2293 irela
= internal_relocs
;
2294 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2295 * bed
->s
->int_rels_per_ext_rel
);
2296 while (irela
< irelaend
)
2298 (*swap_out
) (output_bfd
, irela
, erel
);
2299 irela
+= bed
->s
->int_rels_per_ext_rel
;
2300 erel
+= input_rel_hdr
->sh_entsize
;
2303 /* Bump the counter, so that we know where to add the next set of
2305 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2310 /* Make weak undefined symbols in PIE dynamic. */
2313 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2314 struct elf_link_hash_entry
*h
)
2318 && h
->root
.type
== bfd_link_hash_undefweak
)
2319 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2324 /* Fix up the flags for a symbol. This handles various cases which
2325 can only be fixed after all the input files are seen. This is
2326 currently called by both adjust_dynamic_symbol and
2327 assign_sym_version, which is unnecessary but perhaps more robust in
2328 the face of future changes. */
2331 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2332 struct elf_info_failed
*eif
)
2334 const struct elf_backend_data
*bed
= NULL
;
2336 /* If this symbol was mentioned in a non-ELF file, try to set
2337 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2338 permit a non-ELF file to correctly refer to a symbol defined in
2339 an ELF dynamic object. */
2342 while (h
->root
.type
== bfd_link_hash_indirect
)
2343 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2345 if (h
->root
.type
!= bfd_link_hash_defined
2346 && h
->root
.type
!= bfd_link_hash_defweak
)
2349 h
->ref_regular_nonweak
= 1;
2353 if (h
->root
.u
.def
.section
->owner
!= NULL
2354 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2355 == bfd_target_elf_flavour
))
2358 h
->ref_regular_nonweak
= 1;
2364 if (h
->dynindx
== -1
2368 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2377 /* Unfortunately, NON_ELF is only correct if the symbol
2378 was first seen in a non-ELF file. Fortunately, if the symbol
2379 was first seen in an ELF file, we're probably OK unless the
2380 symbol was defined in a non-ELF file. Catch that case here.
2381 FIXME: We're still in trouble if the symbol was first seen in
2382 a dynamic object, and then later in a non-ELF regular object. */
2383 if ((h
->root
.type
== bfd_link_hash_defined
2384 || h
->root
.type
== bfd_link_hash_defweak
)
2386 && (h
->root
.u
.def
.section
->owner
!= NULL
2387 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2388 != bfd_target_elf_flavour
)
2389 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2390 && !h
->def_dynamic
)))
2394 /* Backend specific symbol fixup. */
2395 if (elf_hash_table (eif
->info
)->dynobj
)
2397 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2398 if (bed
->elf_backend_fixup_symbol
2399 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2403 /* If this is a final link, and the symbol was defined as a common
2404 symbol in a regular object file, and there was no definition in
2405 any dynamic object, then the linker will have allocated space for
2406 the symbol in a common section but the DEF_REGULAR
2407 flag will not have been set. */
2408 if (h
->root
.type
== bfd_link_hash_defined
2412 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2415 /* If -Bsymbolic was used (which means to bind references to global
2416 symbols to the definition within the shared object), and this
2417 symbol was defined in a regular object, then it actually doesn't
2418 need a PLT entry. Likewise, if the symbol has non-default
2419 visibility. If the symbol has hidden or internal visibility, we
2420 will force it local. */
2422 && eif
->info
->shared
2423 && is_elf_hash_table (eif
->info
->hash
)
2424 && (SYMBOLIC_BIND (eif
->info
, h
)
2425 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2428 bfd_boolean force_local
;
2430 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2431 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2432 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2435 /* If a weak undefined symbol has non-default visibility, we also
2436 hide it from the dynamic linker. */
2437 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2438 && h
->root
.type
== bfd_link_hash_undefweak
)
2440 const struct elf_backend_data
*bed
;
2441 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2442 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2445 /* If this is a weak defined symbol in a dynamic object, and we know
2446 the real definition in the dynamic object, copy interesting flags
2447 over to the real definition. */
2448 if (h
->u
.weakdef
!= NULL
)
2450 struct elf_link_hash_entry
*weakdef
;
2452 weakdef
= h
->u
.weakdef
;
2453 if (h
->root
.type
== bfd_link_hash_indirect
)
2454 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2456 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2457 || h
->root
.type
== bfd_link_hash_defweak
);
2458 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2459 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2460 BFD_ASSERT (weakdef
->def_dynamic
);
2462 /* If the real definition is defined by a regular object file,
2463 don't do anything special. See the longer description in
2464 _bfd_elf_adjust_dynamic_symbol, below. */
2465 if (weakdef
->def_regular
)
2466 h
->u
.weakdef
= NULL
;
2468 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2475 /* Make the backend pick a good value for a dynamic symbol. This is
2476 called via elf_link_hash_traverse, and also calls itself
2480 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2482 struct elf_info_failed
*eif
= data
;
2484 const struct elf_backend_data
*bed
;
2486 if (! is_elf_hash_table (eif
->info
->hash
))
2489 if (h
->root
.type
== bfd_link_hash_warning
)
2491 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2492 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2494 /* When warning symbols are created, they **replace** the "real"
2495 entry in the hash table, thus we never get to see the real
2496 symbol in a hash traversal. So look at it now. */
2497 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2500 /* Ignore indirect symbols. These are added by the versioning code. */
2501 if (h
->root
.type
== bfd_link_hash_indirect
)
2504 /* Fix the symbol flags. */
2505 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2508 /* If this symbol does not require a PLT entry, and it is not
2509 defined by a dynamic object, or is not referenced by a regular
2510 object, ignore it. We do have to handle a weak defined symbol,
2511 even if no regular object refers to it, if we decided to add it
2512 to the dynamic symbol table. FIXME: Do we normally need to worry
2513 about symbols which are defined by one dynamic object and
2514 referenced by another one? */
2519 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2521 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2525 /* If we've already adjusted this symbol, don't do it again. This
2526 can happen via a recursive call. */
2527 if (h
->dynamic_adjusted
)
2530 /* Don't look at this symbol again. Note that we must set this
2531 after checking the above conditions, because we may look at a
2532 symbol once, decide not to do anything, and then get called
2533 recursively later after REF_REGULAR is set below. */
2534 h
->dynamic_adjusted
= 1;
2536 /* If this is a weak definition, and we know a real definition, and
2537 the real symbol is not itself defined by a regular object file,
2538 then get a good value for the real definition. We handle the
2539 real symbol first, for the convenience of the backend routine.
2541 Note that there is a confusing case here. If the real definition
2542 is defined by a regular object file, we don't get the real symbol
2543 from the dynamic object, but we do get the weak symbol. If the
2544 processor backend uses a COPY reloc, then if some routine in the
2545 dynamic object changes the real symbol, we will not see that
2546 change in the corresponding weak symbol. This is the way other
2547 ELF linkers work as well, and seems to be a result of the shared
2550 I will clarify this issue. Most SVR4 shared libraries define the
2551 variable _timezone and define timezone as a weak synonym. The
2552 tzset call changes _timezone. If you write
2553 extern int timezone;
2555 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2556 you might expect that, since timezone is a synonym for _timezone,
2557 the same number will print both times. However, if the processor
2558 backend uses a COPY reloc, then actually timezone will be copied
2559 into your process image, and, since you define _timezone
2560 yourself, _timezone will not. Thus timezone and _timezone will
2561 wind up at different memory locations. The tzset call will set
2562 _timezone, leaving timezone unchanged. */
2564 if (h
->u
.weakdef
!= NULL
)
2566 /* If we get to this point, we know there is an implicit
2567 reference by a regular object file via the weak symbol H.
2568 FIXME: Is this really true? What if the traversal finds
2569 H->U.WEAKDEF before it finds H? */
2570 h
->u
.weakdef
->ref_regular
= 1;
2572 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2576 /* If a symbol has no type and no size and does not require a PLT
2577 entry, then we are probably about to do the wrong thing here: we
2578 are probably going to create a COPY reloc for an empty object.
2579 This case can arise when a shared object is built with assembly
2580 code, and the assembly code fails to set the symbol type. */
2582 && h
->type
== STT_NOTYPE
2584 (*_bfd_error_handler
)
2585 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2586 h
->root
.root
.string
);
2588 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2589 bed
= get_elf_backend_data (dynobj
);
2590 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2599 /* Adjust all external symbols pointing into SEC_MERGE sections
2600 to reflect the object merging within the sections. */
2603 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2607 if (h
->root
.type
== bfd_link_hash_warning
)
2608 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2610 if ((h
->root
.type
== bfd_link_hash_defined
2611 || h
->root
.type
== bfd_link_hash_defweak
)
2612 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2613 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2615 bfd
*output_bfd
= data
;
2617 h
->root
.u
.def
.value
=
2618 _bfd_merged_section_offset (output_bfd
,
2619 &h
->root
.u
.def
.section
,
2620 elf_section_data (sec
)->sec_info
,
2621 h
->root
.u
.def
.value
);
2627 /* Returns false if the symbol referred to by H should be considered
2628 to resolve local to the current module, and true if it should be
2629 considered to bind dynamically. */
2632 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2633 struct bfd_link_info
*info
,
2634 bfd_boolean ignore_protected
)
2636 bfd_boolean binding_stays_local_p
;
2641 while (h
->root
.type
== bfd_link_hash_indirect
2642 || h
->root
.type
== bfd_link_hash_warning
)
2643 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2645 /* If it was forced local, then clearly it's not dynamic. */
2646 if (h
->dynindx
== -1)
2648 if (h
->forced_local
)
2651 /* Identify the cases where name binding rules say that a
2652 visible symbol resolves locally. */
2653 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2655 switch (ELF_ST_VISIBILITY (h
->other
))
2662 /* Proper resolution for function pointer equality may require
2663 that these symbols perhaps be resolved dynamically, even though
2664 we should be resolving them to the current module. */
2665 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2666 binding_stays_local_p
= TRUE
;
2673 /* If it isn't defined locally, then clearly it's dynamic. */
2674 if (!h
->def_regular
)
2677 /* Otherwise, the symbol is dynamic if binding rules don't tell
2678 us that it remains local. */
2679 return !binding_stays_local_p
;
2682 /* Return true if the symbol referred to by H should be considered
2683 to resolve local to the current module, and false otherwise. Differs
2684 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2685 undefined symbols and weak symbols. */
2688 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2689 struct bfd_link_info
*info
,
2690 bfd_boolean local_protected
)
2692 /* If it's a local sym, of course we resolve locally. */
2696 /* Common symbols that become definitions don't get the DEF_REGULAR
2697 flag set, so test it first, and don't bail out. */
2698 if (ELF_COMMON_DEF_P (h
))
2700 /* If we don't have a definition in a regular file, then we can't
2701 resolve locally. The sym is either undefined or dynamic. */
2702 else if (!h
->def_regular
)
2705 /* Forced local symbols resolve locally. */
2706 if (h
->forced_local
)
2709 /* As do non-dynamic symbols. */
2710 if (h
->dynindx
== -1)
2713 /* At this point, we know the symbol is defined and dynamic. In an
2714 executable it must resolve locally, likewise when building symbolic
2715 shared libraries. */
2716 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2719 /* Now deal with defined dynamic symbols in shared libraries. Ones
2720 with default visibility might not resolve locally. */
2721 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2724 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2725 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2728 /* STV_PROTECTED non-function symbols are local. */
2729 if (h
->type
!= STT_FUNC
)
2732 /* Function pointer equality tests may require that STV_PROTECTED
2733 symbols be treated as dynamic symbols, even when we know that the
2734 dynamic linker will resolve them locally. */
2735 return local_protected
;
2738 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2739 aligned. Returns the first TLS output section. */
2741 struct bfd_section
*
2742 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2744 struct bfd_section
*sec
, *tls
;
2745 unsigned int align
= 0;
2747 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2748 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2752 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2753 if (sec
->alignment_power
> align
)
2754 align
= sec
->alignment_power
;
2756 elf_hash_table (info
)->tls_sec
= tls
;
2758 /* Ensure the alignment of the first section is the largest alignment,
2759 so that the tls segment starts aligned. */
2761 tls
->alignment_power
= align
;
2766 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2768 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2769 Elf_Internal_Sym
*sym
)
2771 const struct elf_backend_data
*bed
;
2773 /* Local symbols do not count, but target specific ones might. */
2774 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2775 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2778 /* Function symbols do not count. */
2779 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2782 /* If the section is undefined, then so is the symbol. */
2783 if (sym
->st_shndx
== SHN_UNDEF
)
2786 /* If the symbol is defined in the common section, then
2787 it is a common definition and so does not count. */
2788 bed
= get_elf_backend_data (abfd
);
2789 if (bed
->common_definition (sym
))
2792 /* If the symbol is in a target specific section then we
2793 must rely upon the backend to tell us what it is. */
2794 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2795 /* FIXME - this function is not coded yet:
2797 return _bfd_is_global_symbol_definition (abfd, sym);
2799 Instead for now assume that the definition is not global,
2800 Even if this is wrong, at least the linker will behave
2801 in the same way that it used to do. */
2807 /* Search the symbol table of the archive element of the archive ABFD
2808 whose archive map contains a mention of SYMDEF, and determine if
2809 the symbol is defined in this element. */
2811 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2813 Elf_Internal_Shdr
* hdr
;
2814 bfd_size_type symcount
;
2815 bfd_size_type extsymcount
;
2816 bfd_size_type extsymoff
;
2817 Elf_Internal_Sym
*isymbuf
;
2818 Elf_Internal_Sym
*isym
;
2819 Elf_Internal_Sym
*isymend
;
2822 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2826 if (! bfd_check_format (abfd
, bfd_object
))
2829 /* If we have already included the element containing this symbol in the
2830 link then we do not need to include it again. Just claim that any symbol
2831 it contains is not a definition, so that our caller will not decide to
2832 (re)include this element. */
2833 if (abfd
->archive_pass
)
2836 /* Select the appropriate symbol table. */
2837 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2838 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2840 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2842 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2844 /* The sh_info field of the symtab header tells us where the
2845 external symbols start. We don't care about the local symbols. */
2846 if (elf_bad_symtab (abfd
))
2848 extsymcount
= symcount
;
2853 extsymcount
= symcount
- hdr
->sh_info
;
2854 extsymoff
= hdr
->sh_info
;
2857 if (extsymcount
== 0)
2860 /* Read in the symbol table. */
2861 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2863 if (isymbuf
== NULL
)
2866 /* Scan the symbol table looking for SYMDEF. */
2868 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2872 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2877 if (strcmp (name
, symdef
->name
) == 0)
2879 result
= is_global_data_symbol_definition (abfd
, isym
);
2889 /* Add an entry to the .dynamic table. */
2892 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2896 struct elf_link_hash_table
*hash_table
;
2897 const struct elf_backend_data
*bed
;
2899 bfd_size_type newsize
;
2900 bfd_byte
*newcontents
;
2901 Elf_Internal_Dyn dyn
;
2903 hash_table
= elf_hash_table (info
);
2904 if (! is_elf_hash_table (hash_table
))
2907 bed
= get_elf_backend_data (hash_table
->dynobj
);
2908 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2909 BFD_ASSERT (s
!= NULL
);
2911 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2912 newcontents
= bfd_realloc (s
->contents
, newsize
);
2913 if (newcontents
== NULL
)
2917 dyn
.d_un
.d_val
= val
;
2918 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2921 s
->contents
= newcontents
;
2926 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2927 otherwise just check whether one already exists. Returns -1 on error,
2928 1 if a DT_NEEDED tag already exists, and 0 on success. */
2931 elf_add_dt_needed_tag (bfd
*abfd
,
2932 struct bfd_link_info
*info
,
2936 struct elf_link_hash_table
*hash_table
;
2937 bfd_size_type oldsize
;
2938 bfd_size_type strindex
;
2940 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2943 hash_table
= elf_hash_table (info
);
2944 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2945 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2946 if (strindex
== (bfd_size_type
) -1)
2949 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2952 const struct elf_backend_data
*bed
;
2955 bed
= get_elf_backend_data (hash_table
->dynobj
);
2956 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2958 for (extdyn
= sdyn
->contents
;
2959 extdyn
< sdyn
->contents
+ sdyn
->size
;
2960 extdyn
+= bed
->s
->sizeof_dyn
)
2962 Elf_Internal_Dyn dyn
;
2964 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2965 if (dyn
.d_tag
== DT_NEEDED
2966 && dyn
.d_un
.d_val
== strindex
)
2968 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2976 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2979 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2983 /* We were just checking for existence of the tag. */
2984 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2989 /* Sort symbol by value and section. */
2991 elf_sort_symbol (const void *arg1
, const void *arg2
)
2993 const struct elf_link_hash_entry
*h1
;
2994 const struct elf_link_hash_entry
*h2
;
2995 bfd_signed_vma vdiff
;
2997 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2998 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2999 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3001 return vdiff
> 0 ? 1 : -1;
3004 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3006 return sdiff
> 0 ? 1 : -1;
3011 /* This function is used to adjust offsets into .dynstr for
3012 dynamic symbols. This is called via elf_link_hash_traverse. */
3015 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3017 struct elf_strtab_hash
*dynstr
= data
;
3019 if (h
->root
.type
== bfd_link_hash_warning
)
3020 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3022 if (h
->dynindx
!= -1)
3023 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3027 /* Assign string offsets in .dynstr, update all structures referencing
3031 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3033 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3034 struct elf_link_local_dynamic_entry
*entry
;
3035 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3036 bfd
*dynobj
= hash_table
->dynobj
;
3039 const struct elf_backend_data
*bed
;
3042 _bfd_elf_strtab_finalize (dynstr
);
3043 size
= _bfd_elf_strtab_size (dynstr
);
3045 bed
= get_elf_backend_data (dynobj
);
3046 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3047 BFD_ASSERT (sdyn
!= NULL
);
3049 /* Update all .dynamic entries referencing .dynstr strings. */
3050 for (extdyn
= sdyn
->contents
;
3051 extdyn
< sdyn
->contents
+ sdyn
->size
;
3052 extdyn
+= bed
->s
->sizeof_dyn
)
3054 Elf_Internal_Dyn dyn
;
3056 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3060 dyn
.d_un
.d_val
= size
;
3068 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3073 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3076 /* Now update local dynamic symbols. */
3077 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3078 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3079 entry
->isym
.st_name
);
3081 /* And the rest of dynamic symbols. */
3082 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3084 /* Adjust version definitions. */
3085 if (elf_tdata (output_bfd
)->cverdefs
)
3090 Elf_Internal_Verdef def
;
3091 Elf_Internal_Verdaux defaux
;
3093 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3097 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3099 p
+= sizeof (Elf_External_Verdef
);
3100 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3102 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3104 _bfd_elf_swap_verdaux_in (output_bfd
,
3105 (Elf_External_Verdaux
*) p
, &defaux
);
3106 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3108 _bfd_elf_swap_verdaux_out (output_bfd
,
3109 &defaux
, (Elf_External_Verdaux
*) p
);
3110 p
+= sizeof (Elf_External_Verdaux
);
3113 while (def
.vd_next
);
3116 /* Adjust version references. */
3117 if (elf_tdata (output_bfd
)->verref
)
3122 Elf_Internal_Verneed need
;
3123 Elf_Internal_Vernaux needaux
;
3125 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3129 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3131 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3132 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3133 (Elf_External_Verneed
*) p
);
3134 p
+= sizeof (Elf_External_Verneed
);
3135 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3137 _bfd_elf_swap_vernaux_in (output_bfd
,
3138 (Elf_External_Vernaux
*) p
, &needaux
);
3139 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3141 _bfd_elf_swap_vernaux_out (output_bfd
,
3143 (Elf_External_Vernaux
*) p
);
3144 p
+= sizeof (Elf_External_Vernaux
);
3147 while (need
.vn_next
);
3153 /* Add symbols from an ELF object file to the linker hash table. */
3156 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3158 Elf_Internal_Shdr
*hdr
;
3159 bfd_size_type symcount
;
3160 bfd_size_type extsymcount
;
3161 bfd_size_type extsymoff
;
3162 struct elf_link_hash_entry
**sym_hash
;
3163 bfd_boolean dynamic
;
3164 Elf_External_Versym
*extversym
= NULL
;
3165 Elf_External_Versym
*ever
;
3166 struct elf_link_hash_entry
*weaks
;
3167 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3168 bfd_size_type nondeflt_vers_cnt
= 0;
3169 Elf_Internal_Sym
*isymbuf
= NULL
;
3170 Elf_Internal_Sym
*isym
;
3171 Elf_Internal_Sym
*isymend
;
3172 const struct elf_backend_data
*bed
;
3173 bfd_boolean add_needed
;
3174 struct elf_link_hash_table
*htab
;
3176 void *alloc_mark
= NULL
;
3177 struct bfd_hash_entry
**old_table
= NULL
;
3178 unsigned int old_size
= 0;
3179 unsigned int old_count
= 0;
3180 void *old_tab
= NULL
;
3183 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3184 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3185 long old_dynsymcount
= 0;
3187 size_t hashsize
= 0;
3189 htab
= elf_hash_table (info
);
3190 bed
= get_elf_backend_data (abfd
);
3192 if ((abfd
->flags
& DYNAMIC
) == 0)
3198 /* You can't use -r against a dynamic object. Also, there's no
3199 hope of using a dynamic object which does not exactly match
3200 the format of the output file. */
3201 if (info
->relocatable
3202 || !is_elf_hash_table (htab
)
3203 || htab
->root
.creator
!= abfd
->xvec
)
3205 if (info
->relocatable
)
3206 bfd_set_error (bfd_error_invalid_operation
);
3208 bfd_set_error (bfd_error_wrong_format
);
3213 /* As a GNU extension, any input sections which are named
3214 .gnu.warning.SYMBOL are treated as warning symbols for the given
3215 symbol. This differs from .gnu.warning sections, which generate
3216 warnings when they are included in an output file. */
3217 if (info
->executable
)
3221 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3225 name
= bfd_get_section_name (abfd
, s
);
3226 if (CONST_STRNEQ (name
, ".gnu.warning."))
3231 name
+= sizeof ".gnu.warning." - 1;
3233 /* If this is a shared object, then look up the symbol
3234 in the hash table. If it is there, and it is already
3235 been defined, then we will not be using the entry
3236 from this shared object, so we don't need to warn.
3237 FIXME: If we see the definition in a regular object
3238 later on, we will warn, but we shouldn't. The only
3239 fix is to keep track of what warnings we are supposed
3240 to emit, and then handle them all at the end of the
3244 struct elf_link_hash_entry
*h
;
3246 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3248 /* FIXME: What about bfd_link_hash_common? */
3250 && (h
->root
.type
== bfd_link_hash_defined
3251 || h
->root
.type
== bfd_link_hash_defweak
))
3253 /* We don't want to issue this warning. Clobber
3254 the section size so that the warning does not
3255 get copied into the output file. */
3262 msg
= bfd_alloc (abfd
, sz
+ 1);
3266 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3271 if (! (_bfd_generic_link_add_one_symbol
3272 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3273 FALSE
, bed
->collect
, NULL
)))
3276 if (! info
->relocatable
)
3278 /* Clobber the section size so that the warning does
3279 not get copied into the output file. */
3282 /* Also set SEC_EXCLUDE, so that symbols defined in
3283 the warning section don't get copied to the output. */
3284 s
->flags
|= SEC_EXCLUDE
;
3293 /* If we are creating a shared library, create all the dynamic
3294 sections immediately. We need to attach them to something,
3295 so we attach them to this BFD, provided it is the right
3296 format. FIXME: If there are no input BFD's of the same
3297 format as the output, we can't make a shared library. */
3299 && is_elf_hash_table (htab
)
3300 && htab
->root
.creator
== abfd
->xvec
3301 && !htab
->dynamic_sections_created
)
3303 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3307 else if (!is_elf_hash_table (htab
))
3312 const char *soname
= NULL
;
3313 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3316 /* ld --just-symbols and dynamic objects don't mix very well.
3317 ld shouldn't allow it. */
3318 if ((s
= abfd
->sections
) != NULL
3319 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3322 /* If this dynamic lib was specified on the command line with
3323 --as-needed in effect, then we don't want to add a DT_NEEDED
3324 tag unless the lib is actually used. Similary for libs brought
3325 in by another lib's DT_NEEDED. When --no-add-needed is used
3326 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3327 any dynamic library in DT_NEEDED tags in the dynamic lib at
3329 add_needed
= (elf_dyn_lib_class (abfd
)
3330 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3331 | DYN_NO_NEEDED
)) == 0;
3333 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3339 unsigned long shlink
;
3341 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3342 goto error_free_dyn
;
3344 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3346 goto error_free_dyn
;
3347 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3349 for (extdyn
= dynbuf
;
3350 extdyn
< dynbuf
+ s
->size
;
3351 extdyn
+= bed
->s
->sizeof_dyn
)
3353 Elf_Internal_Dyn dyn
;
3355 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3356 if (dyn
.d_tag
== DT_SONAME
)
3358 unsigned int tagv
= dyn
.d_un
.d_val
;
3359 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3361 goto error_free_dyn
;
3363 if (dyn
.d_tag
== DT_NEEDED
)
3365 struct bfd_link_needed_list
*n
, **pn
;
3367 unsigned int tagv
= dyn
.d_un
.d_val
;
3369 amt
= sizeof (struct bfd_link_needed_list
);
3370 n
= bfd_alloc (abfd
, amt
);
3371 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3372 if (n
== NULL
|| fnm
== NULL
)
3373 goto error_free_dyn
;
3374 amt
= strlen (fnm
) + 1;
3375 anm
= bfd_alloc (abfd
, amt
);
3377 goto error_free_dyn
;
3378 memcpy (anm
, fnm
, amt
);
3382 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3386 if (dyn
.d_tag
== DT_RUNPATH
)
3388 struct bfd_link_needed_list
*n
, **pn
;
3390 unsigned int tagv
= dyn
.d_un
.d_val
;
3392 amt
= sizeof (struct bfd_link_needed_list
);
3393 n
= bfd_alloc (abfd
, amt
);
3394 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3395 if (n
== NULL
|| fnm
== NULL
)
3396 goto error_free_dyn
;
3397 amt
= strlen (fnm
) + 1;
3398 anm
= bfd_alloc (abfd
, amt
);
3400 goto error_free_dyn
;
3401 memcpy (anm
, fnm
, amt
);
3405 for (pn
= & runpath
;
3411 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3412 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3414 struct bfd_link_needed_list
*n
, **pn
;
3416 unsigned int tagv
= dyn
.d_un
.d_val
;
3418 amt
= sizeof (struct bfd_link_needed_list
);
3419 n
= bfd_alloc (abfd
, amt
);
3420 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3421 if (n
== NULL
|| fnm
== NULL
)
3422 goto error_free_dyn
;
3423 amt
= strlen (fnm
) + 1;
3424 anm
= bfd_alloc (abfd
, amt
);
3431 memcpy (anm
, fnm
, amt
);
3446 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3447 frees all more recently bfd_alloc'd blocks as well. */
3453 struct bfd_link_needed_list
**pn
;
3454 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3459 /* We do not want to include any of the sections in a dynamic
3460 object in the output file. We hack by simply clobbering the
3461 list of sections in the BFD. This could be handled more
3462 cleanly by, say, a new section flag; the existing
3463 SEC_NEVER_LOAD flag is not the one we want, because that one
3464 still implies that the section takes up space in the output
3466 bfd_section_list_clear (abfd
);
3468 /* Find the name to use in a DT_NEEDED entry that refers to this
3469 object. If the object has a DT_SONAME entry, we use it.
3470 Otherwise, if the generic linker stuck something in
3471 elf_dt_name, we use that. Otherwise, we just use the file
3473 if (soname
== NULL
|| *soname
== '\0')
3475 soname
= elf_dt_name (abfd
);
3476 if (soname
== NULL
|| *soname
== '\0')
3477 soname
= bfd_get_filename (abfd
);
3480 /* Save the SONAME because sometimes the linker emulation code
3481 will need to know it. */
3482 elf_dt_name (abfd
) = soname
;
3484 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3488 /* If we have already included this dynamic object in the
3489 link, just ignore it. There is no reason to include a
3490 particular dynamic object more than once. */
3495 /* If this is a dynamic object, we always link against the .dynsym
3496 symbol table, not the .symtab symbol table. The dynamic linker
3497 will only see the .dynsym symbol table, so there is no reason to
3498 look at .symtab for a dynamic object. */
3500 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3501 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3503 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3505 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3507 /* The sh_info field of the symtab header tells us where the
3508 external symbols start. We don't care about the local symbols at
3510 if (elf_bad_symtab (abfd
))
3512 extsymcount
= symcount
;
3517 extsymcount
= symcount
- hdr
->sh_info
;
3518 extsymoff
= hdr
->sh_info
;
3522 if (extsymcount
!= 0)
3524 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3526 if (isymbuf
== NULL
)
3529 /* We store a pointer to the hash table entry for each external
3531 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3532 sym_hash
= bfd_alloc (abfd
, amt
);
3533 if (sym_hash
== NULL
)
3534 goto error_free_sym
;
3535 elf_sym_hashes (abfd
) = sym_hash
;
3540 /* Read in any version definitions. */
3541 if (!_bfd_elf_slurp_version_tables (abfd
,
3542 info
->default_imported_symver
))
3543 goto error_free_sym
;
3545 /* Read in the symbol versions, but don't bother to convert them
3546 to internal format. */
3547 if (elf_dynversym (abfd
) != 0)
3549 Elf_Internal_Shdr
*versymhdr
;
3551 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3552 extversym
= bfd_malloc (versymhdr
->sh_size
);
3553 if (extversym
== NULL
)
3554 goto error_free_sym
;
3555 amt
= versymhdr
->sh_size
;
3556 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3557 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3558 goto error_free_vers
;
3562 /* If we are loading an as-needed shared lib, save the symbol table
3563 state before we start adding symbols. If the lib turns out
3564 to be unneeded, restore the state. */
3565 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3570 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3572 struct bfd_hash_entry
*p
;
3573 struct elf_link_hash_entry
*h
;
3575 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3577 h
= (struct elf_link_hash_entry
*) p
;
3578 entsize
+= htab
->root
.table
.entsize
;
3579 if (h
->root
.type
== bfd_link_hash_warning
)
3580 entsize
+= htab
->root
.table
.entsize
;
3584 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3585 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3586 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3587 if (old_tab
== NULL
)
3588 goto error_free_vers
;
3590 /* Remember the current objalloc pointer, so that all mem for
3591 symbols added can later be reclaimed. */
3592 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3593 if (alloc_mark
== NULL
)
3594 goto error_free_vers
;
3596 /* Make a special call to the linker "notice" function to
3597 tell it that we are about to handle an as-needed lib. */
3598 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3603 /* Clone the symbol table and sym hashes. Remember some
3604 pointers into the symbol table, and dynamic symbol count. */
3605 old_hash
= (char *) old_tab
+ tabsize
;
3606 old_ent
= (char *) old_hash
+ hashsize
;
3607 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3608 memcpy (old_hash
, sym_hash
, hashsize
);
3609 old_undefs
= htab
->root
.undefs
;
3610 old_undefs_tail
= htab
->root
.undefs_tail
;
3611 old_table
= htab
->root
.table
.table
;
3612 old_size
= htab
->root
.table
.size
;
3613 old_count
= htab
->root
.table
.count
;
3614 old_dynsymcount
= htab
->dynsymcount
;
3616 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3618 struct bfd_hash_entry
*p
;
3619 struct elf_link_hash_entry
*h
;
3621 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3623 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3624 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3625 h
= (struct elf_link_hash_entry
*) p
;
3626 if (h
->root
.type
== bfd_link_hash_warning
)
3628 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3629 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3636 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3637 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3639 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3643 asection
*sec
, *new_sec
;
3646 struct elf_link_hash_entry
*h
;
3647 bfd_boolean definition
;
3648 bfd_boolean size_change_ok
;
3649 bfd_boolean type_change_ok
;
3650 bfd_boolean new_weakdef
;
3651 bfd_boolean override
;
3653 unsigned int old_alignment
;
3658 flags
= BSF_NO_FLAGS
;
3660 value
= isym
->st_value
;
3662 common
= bed
->common_definition (isym
);
3664 bind
= ELF_ST_BIND (isym
->st_info
);
3665 if (bind
== STB_LOCAL
)
3667 /* This should be impossible, since ELF requires that all
3668 global symbols follow all local symbols, and that sh_info
3669 point to the first global symbol. Unfortunately, Irix 5
3673 else if (bind
== STB_GLOBAL
)
3675 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3678 else if (bind
== STB_WEAK
)
3682 /* Leave it up to the processor backend. */
3685 if (isym
->st_shndx
== SHN_UNDEF
)
3686 sec
= bfd_und_section_ptr
;
3687 else if (isym
->st_shndx
< SHN_LORESERVE
3688 || isym
->st_shndx
> SHN_HIRESERVE
)
3690 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3692 sec
= bfd_abs_section_ptr
;
3693 else if (sec
->kept_section
)
3695 /* Symbols from discarded section are undefined, and have
3696 default visibility. */
3697 sec
= bfd_und_section_ptr
;
3698 isym
->st_shndx
= SHN_UNDEF
;
3699 isym
->st_other
= (STV_DEFAULT
3700 | (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1)));
3702 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3705 else if (isym
->st_shndx
== SHN_ABS
)
3706 sec
= bfd_abs_section_ptr
;
3707 else if (isym
->st_shndx
== SHN_COMMON
)
3709 sec
= bfd_com_section_ptr
;
3710 /* What ELF calls the size we call the value. What ELF
3711 calls the value we call the alignment. */
3712 value
= isym
->st_size
;
3716 /* Leave it up to the processor backend. */
3719 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3722 goto error_free_vers
;
3724 if (isym
->st_shndx
== SHN_COMMON
3725 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3726 && !info
->relocatable
)
3728 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3732 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3735 | SEC_LINKER_CREATED
3736 | SEC_THREAD_LOCAL
));
3738 goto error_free_vers
;
3742 else if (bed
->elf_add_symbol_hook
)
3744 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3746 goto error_free_vers
;
3748 /* The hook function sets the name to NULL if this symbol
3749 should be skipped for some reason. */
3754 /* Sanity check that all possibilities were handled. */
3757 bfd_set_error (bfd_error_bad_value
);
3758 goto error_free_vers
;
3761 if (bfd_is_und_section (sec
)
3762 || bfd_is_com_section (sec
))
3767 size_change_ok
= FALSE
;
3768 type_change_ok
= bed
->type_change_ok
;
3773 if (is_elf_hash_table (htab
))
3775 Elf_Internal_Versym iver
;
3776 unsigned int vernum
= 0;
3781 if (info
->default_imported_symver
)
3782 /* Use the default symbol version created earlier. */
3783 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3788 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3790 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3792 /* If this is a hidden symbol, or if it is not version
3793 1, we append the version name to the symbol name.
3794 However, we do not modify a non-hidden absolute symbol
3795 if it is not a function, because it might be the version
3796 symbol itself. FIXME: What if it isn't? */
3797 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3798 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3799 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3802 size_t namelen
, verlen
, newlen
;
3805 if (isym
->st_shndx
!= SHN_UNDEF
)
3807 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3809 else if (vernum
> 1)
3811 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3817 (*_bfd_error_handler
)
3818 (_("%B: %s: invalid version %u (max %d)"),
3820 elf_tdata (abfd
)->cverdefs
);
3821 bfd_set_error (bfd_error_bad_value
);
3822 goto error_free_vers
;
3827 /* We cannot simply test for the number of
3828 entries in the VERNEED section since the
3829 numbers for the needed versions do not start
3831 Elf_Internal_Verneed
*t
;
3834 for (t
= elf_tdata (abfd
)->verref
;
3838 Elf_Internal_Vernaux
*a
;
3840 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3842 if (a
->vna_other
== vernum
)
3844 verstr
= a
->vna_nodename
;
3853 (*_bfd_error_handler
)
3854 (_("%B: %s: invalid needed version %d"),
3855 abfd
, name
, vernum
);
3856 bfd_set_error (bfd_error_bad_value
);
3857 goto error_free_vers
;
3861 namelen
= strlen (name
);
3862 verlen
= strlen (verstr
);
3863 newlen
= namelen
+ verlen
+ 2;
3864 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3865 && isym
->st_shndx
!= SHN_UNDEF
)
3868 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3869 if (newname
== NULL
)
3870 goto error_free_vers
;
3871 memcpy (newname
, name
, namelen
);
3872 p
= newname
+ namelen
;
3874 /* If this is a defined non-hidden version symbol,
3875 we add another @ to the name. This indicates the
3876 default version of the symbol. */
3877 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3878 && isym
->st_shndx
!= SHN_UNDEF
)
3880 memcpy (p
, verstr
, verlen
+ 1);
3885 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3886 &value
, &old_alignment
,
3887 sym_hash
, &skip
, &override
,
3888 &type_change_ok
, &size_change_ok
))
3889 goto error_free_vers
;
3898 while (h
->root
.type
== bfd_link_hash_indirect
3899 || h
->root
.type
== bfd_link_hash_warning
)
3900 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3902 /* Remember the old alignment if this is a common symbol, so
3903 that we don't reduce the alignment later on. We can't
3904 check later, because _bfd_generic_link_add_one_symbol
3905 will set a default for the alignment which we want to
3906 override. We also remember the old bfd where the existing
3907 definition comes from. */
3908 switch (h
->root
.type
)
3913 case bfd_link_hash_defined
:
3914 case bfd_link_hash_defweak
:
3915 old_bfd
= h
->root
.u
.def
.section
->owner
;
3918 case bfd_link_hash_common
:
3919 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3920 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3924 if (elf_tdata (abfd
)->verdef
!= NULL
3928 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3931 if (! (_bfd_generic_link_add_one_symbol
3932 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3933 (struct bfd_link_hash_entry
**) sym_hash
)))
3934 goto error_free_vers
;
3937 while (h
->root
.type
== bfd_link_hash_indirect
3938 || h
->root
.type
== bfd_link_hash_warning
)
3939 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3942 new_weakdef
= FALSE
;
3945 && (flags
& BSF_WEAK
) != 0
3946 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3947 && is_elf_hash_table (htab
)
3948 && h
->u
.weakdef
== NULL
)
3950 /* Keep a list of all weak defined non function symbols from
3951 a dynamic object, using the weakdef field. Later in this
3952 function we will set the weakdef field to the correct
3953 value. We only put non-function symbols from dynamic
3954 objects on this list, because that happens to be the only
3955 time we need to know the normal symbol corresponding to a
3956 weak symbol, and the information is time consuming to
3957 figure out. If the weakdef field is not already NULL,
3958 then this symbol was already defined by some previous
3959 dynamic object, and we will be using that previous
3960 definition anyhow. */
3962 h
->u
.weakdef
= weaks
;
3967 /* Set the alignment of a common symbol. */
3968 if ((common
|| bfd_is_com_section (sec
))
3969 && h
->root
.type
== bfd_link_hash_common
)
3974 align
= bfd_log2 (isym
->st_value
);
3977 /* The new symbol is a common symbol in a shared object.
3978 We need to get the alignment from the section. */
3979 align
= new_sec
->alignment_power
;
3981 if (align
> old_alignment
3982 /* Permit an alignment power of zero if an alignment of one
3983 is specified and no other alignments have been specified. */
3984 || (isym
->st_value
== 1 && old_alignment
== 0))
3985 h
->root
.u
.c
.p
->alignment_power
= align
;
3987 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3990 if (is_elf_hash_table (htab
))
3994 /* Check the alignment when a common symbol is involved. This
3995 can change when a common symbol is overridden by a normal
3996 definition or a common symbol is ignored due to the old
3997 normal definition. We need to make sure the maximum
3998 alignment is maintained. */
3999 if ((old_alignment
|| common
)
4000 && h
->root
.type
!= bfd_link_hash_common
)
4002 unsigned int common_align
;
4003 unsigned int normal_align
;
4004 unsigned int symbol_align
;
4008 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4009 if (h
->root
.u
.def
.section
->owner
!= NULL
4010 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4012 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4013 if (normal_align
> symbol_align
)
4014 normal_align
= symbol_align
;
4017 normal_align
= symbol_align
;
4021 common_align
= old_alignment
;
4022 common_bfd
= old_bfd
;
4027 common_align
= bfd_log2 (isym
->st_value
);
4029 normal_bfd
= old_bfd
;
4032 if (normal_align
< common_align
)
4034 /* PR binutils/2735 */
4035 if (normal_bfd
== NULL
)
4036 (*_bfd_error_handler
)
4037 (_("Warning: alignment %u of common symbol `%s' in %B"
4038 " is greater than the alignment (%u) of its section %A"),
4039 common_bfd
, h
->root
.u
.def
.section
,
4040 1 << common_align
, name
, 1 << normal_align
);
4042 (*_bfd_error_handler
)
4043 (_("Warning: alignment %u of symbol `%s' in %B"
4044 " is smaller than %u in %B"),
4045 normal_bfd
, common_bfd
,
4046 1 << normal_align
, name
, 1 << common_align
);
4050 /* Remember the symbol size and type. */
4051 if (isym
->st_size
!= 0
4052 && (definition
|| h
->size
== 0))
4054 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
4055 (*_bfd_error_handler
)
4056 (_("Warning: size of symbol `%s' changed"
4057 " from %lu in %B to %lu in %B"),
4059 name
, (unsigned long) h
->size
,
4060 (unsigned long) isym
->st_size
);
4062 h
->size
= isym
->st_size
;
4065 /* If this is a common symbol, then we always want H->SIZE
4066 to be the size of the common symbol. The code just above
4067 won't fix the size if a common symbol becomes larger. We
4068 don't warn about a size change here, because that is
4069 covered by --warn-common. */
4070 if (h
->root
.type
== bfd_link_hash_common
)
4071 h
->size
= h
->root
.u
.c
.size
;
4073 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4074 && (definition
|| h
->type
== STT_NOTYPE
))
4076 if (h
->type
!= STT_NOTYPE
4077 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4078 && ! type_change_ok
)
4079 (*_bfd_error_handler
)
4080 (_("Warning: type of symbol `%s' changed"
4081 " from %d to %d in %B"),
4082 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4084 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4087 /* If st_other has a processor-specific meaning, specific
4088 code might be needed here. We never merge the visibility
4089 attribute with the one from a dynamic object. */
4090 if (bed
->elf_backend_merge_symbol_attribute
)
4091 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4094 /* If this symbol has default visibility and the user has requested
4095 we not re-export it, then mark it as hidden. */
4096 if (definition
&& !dynamic
4098 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4099 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4100 isym
->st_other
= (STV_HIDDEN
4101 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4103 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4105 unsigned char hvis
, symvis
, other
, nvis
;
4107 /* Only merge the visibility. Leave the remainder of the
4108 st_other field to elf_backend_merge_symbol_attribute. */
4109 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4111 /* Combine visibilities, using the most constraining one. */
4112 hvis
= ELF_ST_VISIBILITY (h
->other
);
4113 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4119 nvis
= hvis
< symvis
? hvis
: symvis
;
4121 h
->other
= other
| nvis
;
4124 /* Set a flag in the hash table entry indicating the type of
4125 reference or definition we just found. Keep a count of
4126 the number of dynamic symbols we find. A dynamic symbol
4127 is one which is referenced or defined by both a regular
4128 object and a shared object. */
4135 if (bind
!= STB_WEAK
)
4136 h
->ref_regular_nonweak
= 1;
4140 if (! info
->executable
4153 || (h
->u
.weakdef
!= NULL
4155 && h
->u
.weakdef
->dynindx
!= -1))
4159 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4161 /* We don't want to make debug symbol dynamic. */
4162 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4166 /* Check to see if we need to add an indirect symbol for
4167 the default name. */
4168 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4169 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4170 &sec
, &value
, &dynsym
,
4172 goto error_free_vers
;
4174 if (definition
&& !dynamic
)
4176 char *p
= strchr (name
, ELF_VER_CHR
);
4177 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4179 /* Queue non-default versions so that .symver x, x@FOO
4180 aliases can be checked. */
4183 amt
= ((isymend
- isym
+ 1)
4184 * sizeof (struct elf_link_hash_entry
*));
4185 nondeflt_vers
= bfd_malloc (amt
);
4187 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4191 if (dynsym
&& h
->dynindx
== -1)
4193 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4194 goto error_free_vers
;
4195 if (h
->u
.weakdef
!= NULL
4197 && h
->u
.weakdef
->dynindx
== -1)
4199 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4200 goto error_free_vers
;
4203 else if (dynsym
&& h
->dynindx
!= -1)
4204 /* If the symbol already has a dynamic index, but
4205 visibility says it should not be visible, turn it into
4207 switch (ELF_ST_VISIBILITY (h
->other
))
4211 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4222 const char *soname
= elf_dt_name (abfd
);
4224 /* A symbol from a library loaded via DT_NEEDED of some
4225 other library is referenced by a regular object.
4226 Add a DT_NEEDED entry for it. Issue an error if
4227 --no-add-needed is used. */
4228 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4230 (*_bfd_error_handler
)
4231 (_("%s: invalid DSO for symbol `%s' definition"),
4233 bfd_set_error (bfd_error_bad_value
);
4234 goto error_free_vers
;
4237 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4240 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4242 goto error_free_vers
;
4244 BFD_ASSERT (ret
== 0);
4249 if (extversym
!= NULL
)
4255 if (isymbuf
!= NULL
)
4261 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4265 /* Restore the symbol table. */
4266 old_hash
= (char *) old_tab
+ tabsize
;
4267 old_ent
= (char *) old_hash
+ hashsize
;
4268 sym_hash
= elf_sym_hashes (abfd
);
4269 htab
->root
.table
.table
= old_table
;
4270 htab
->root
.table
.size
= old_size
;
4271 htab
->root
.table
.count
= old_count
;
4272 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4273 memcpy (sym_hash
, old_hash
, hashsize
);
4274 htab
->root
.undefs
= old_undefs
;
4275 htab
->root
.undefs_tail
= old_undefs_tail
;
4276 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4278 struct bfd_hash_entry
*p
;
4279 struct elf_link_hash_entry
*h
;
4281 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4283 h
= (struct elf_link_hash_entry
*) p
;
4284 if (h
->root
.type
== bfd_link_hash_warning
)
4285 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4286 if (h
->dynindx
>= old_dynsymcount
)
4287 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4289 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4290 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4291 h
= (struct elf_link_hash_entry
*) p
;
4292 if (h
->root
.type
== bfd_link_hash_warning
)
4294 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4295 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4300 /* Make a special call to the linker "notice" function to
4301 tell it that symbols added for crefs may need to be removed. */
4302 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4307 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4309 if (nondeflt_vers
!= NULL
)
4310 free (nondeflt_vers
);
4314 if (old_tab
!= NULL
)
4316 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4323 /* Now that all the symbols from this input file are created, handle
4324 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4325 if (nondeflt_vers
!= NULL
)
4327 bfd_size_type cnt
, symidx
;
4329 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4331 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4332 char *shortname
, *p
;
4334 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4336 || (h
->root
.type
!= bfd_link_hash_defined
4337 && h
->root
.type
!= bfd_link_hash_defweak
))
4340 amt
= p
- h
->root
.root
.string
;
4341 shortname
= bfd_malloc (amt
+ 1);
4342 memcpy (shortname
, h
->root
.root
.string
, amt
);
4343 shortname
[amt
] = '\0';
4345 hi
= (struct elf_link_hash_entry
*)
4346 bfd_link_hash_lookup (&htab
->root
, shortname
,
4347 FALSE
, FALSE
, FALSE
);
4349 && hi
->root
.type
== h
->root
.type
4350 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4351 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4353 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4354 hi
->root
.type
= bfd_link_hash_indirect
;
4355 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4356 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4357 sym_hash
= elf_sym_hashes (abfd
);
4359 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4360 if (sym_hash
[symidx
] == hi
)
4362 sym_hash
[symidx
] = h
;
4368 free (nondeflt_vers
);
4369 nondeflt_vers
= NULL
;
4372 /* Now set the weakdefs field correctly for all the weak defined
4373 symbols we found. The only way to do this is to search all the
4374 symbols. Since we only need the information for non functions in
4375 dynamic objects, that's the only time we actually put anything on
4376 the list WEAKS. We need this information so that if a regular
4377 object refers to a symbol defined weakly in a dynamic object, the
4378 real symbol in the dynamic object is also put in the dynamic
4379 symbols; we also must arrange for both symbols to point to the
4380 same memory location. We could handle the general case of symbol
4381 aliasing, but a general symbol alias can only be generated in
4382 assembler code, handling it correctly would be very time
4383 consuming, and other ELF linkers don't handle general aliasing
4387 struct elf_link_hash_entry
**hpp
;
4388 struct elf_link_hash_entry
**hppend
;
4389 struct elf_link_hash_entry
**sorted_sym_hash
;
4390 struct elf_link_hash_entry
*h
;
4393 /* Since we have to search the whole symbol list for each weak
4394 defined symbol, search time for N weak defined symbols will be
4395 O(N^2). Binary search will cut it down to O(NlogN). */
4396 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4397 sorted_sym_hash
= bfd_malloc (amt
);
4398 if (sorted_sym_hash
== NULL
)
4400 sym_hash
= sorted_sym_hash
;
4401 hpp
= elf_sym_hashes (abfd
);
4402 hppend
= hpp
+ extsymcount
;
4404 for (; hpp
< hppend
; hpp
++)
4408 && h
->root
.type
== bfd_link_hash_defined
4409 && h
->type
!= STT_FUNC
)
4417 qsort (sorted_sym_hash
, sym_count
,
4418 sizeof (struct elf_link_hash_entry
*),
4421 while (weaks
!= NULL
)
4423 struct elf_link_hash_entry
*hlook
;
4430 weaks
= hlook
->u
.weakdef
;
4431 hlook
->u
.weakdef
= NULL
;
4433 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4434 || hlook
->root
.type
== bfd_link_hash_defweak
4435 || hlook
->root
.type
== bfd_link_hash_common
4436 || hlook
->root
.type
== bfd_link_hash_indirect
);
4437 slook
= hlook
->root
.u
.def
.section
;
4438 vlook
= hlook
->root
.u
.def
.value
;
4445 bfd_signed_vma vdiff
;
4447 h
= sorted_sym_hash
[idx
];
4448 vdiff
= vlook
- h
->root
.u
.def
.value
;
4455 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4468 /* We didn't find a value/section match. */
4472 for (i
= ilook
; i
< sym_count
; i
++)
4474 h
= sorted_sym_hash
[i
];
4476 /* Stop if value or section doesn't match. */
4477 if (h
->root
.u
.def
.value
!= vlook
4478 || h
->root
.u
.def
.section
!= slook
)
4480 else if (h
!= hlook
)
4482 hlook
->u
.weakdef
= h
;
4484 /* If the weak definition is in the list of dynamic
4485 symbols, make sure the real definition is put
4487 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4489 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4493 /* If the real definition is in the list of dynamic
4494 symbols, make sure the weak definition is put
4495 there as well. If we don't do this, then the
4496 dynamic loader might not merge the entries for the
4497 real definition and the weak definition. */
4498 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4500 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4508 free (sorted_sym_hash
);
4511 if (bed
->check_directives
)
4512 (*bed
->check_directives
) (abfd
, info
);
4514 /* If this object is the same format as the output object, and it is
4515 not a shared library, then let the backend look through the
4518 This is required to build global offset table entries and to
4519 arrange for dynamic relocs. It is not required for the
4520 particular common case of linking non PIC code, even when linking
4521 against shared libraries, but unfortunately there is no way of
4522 knowing whether an object file has been compiled PIC or not.
4523 Looking through the relocs is not particularly time consuming.
4524 The problem is that we must either (1) keep the relocs in memory,
4525 which causes the linker to require additional runtime memory or
4526 (2) read the relocs twice from the input file, which wastes time.
4527 This would be a good case for using mmap.
4529 I have no idea how to handle linking PIC code into a file of a
4530 different format. It probably can't be done. */
4532 && is_elf_hash_table (htab
)
4533 && htab
->root
.creator
== abfd
->xvec
4534 && bed
->check_relocs
!= NULL
)
4538 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4540 Elf_Internal_Rela
*internal_relocs
;
4543 if ((o
->flags
& SEC_RELOC
) == 0
4544 || o
->reloc_count
== 0
4545 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4546 && (o
->flags
& SEC_DEBUGGING
) != 0)
4547 || bfd_is_abs_section (o
->output_section
))
4550 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4552 if (internal_relocs
== NULL
)
4555 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4557 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4558 free (internal_relocs
);
4565 /* If this is a non-traditional link, try to optimize the handling
4566 of the .stab/.stabstr sections. */
4568 && ! info
->traditional_format
4569 && is_elf_hash_table (htab
)
4570 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4574 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4575 if (stabstr
!= NULL
)
4577 bfd_size_type string_offset
= 0;
4580 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4581 if (CONST_STRNEQ (stab
->name
, ".stab")
4582 && (!stab
->name
[5] ||
4583 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4584 && (stab
->flags
& SEC_MERGE
) == 0
4585 && !bfd_is_abs_section (stab
->output_section
))
4587 struct bfd_elf_section_data
*secdata
;
4589 secdata
= elf_section_data (stab
);
4590 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4591 stabstr
, &secdata
->sec_info
,
4594 if (secdata
->sec_info
)
4595 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4600 if (is_elf_hash_table (htab
) && add_needed
)
4602 /* Add this bfd to the loaded list. */
4603 struct elf_link_loaded_list
*n
;
4605 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4609 n
->next
= htab
->loaded
;
4616 if (old_tab
!= NULL
)
4618 if (nondeflt_vers
!= NULL
)
4619 free (nondeflt_vers
);
4620 if (extversym
!= NULL
)
4623 if (isymbuf
!= NULL
)
4629 /* Return the linker hash table entry of a symbol that might be
4630 satisfied by an archive symbol. Return -1 on error. */
4632 struct elf_link_hash_entry
*
4633 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4634 struct bfd_link_info
*info
,
4637 struct elf_link_hash_entry
*h
;
4641 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4645 /* If this is a default version (the name contains @@), look up the
4646 symbol again with only one `@' as well as without the version.
4647 The effect is that references to the symbol with and without the
4648 version will be matched by the default symbol in the archive. */
4650 p
= strchr (name
, ELF_VER_CHR
);
4651 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4654 /* First check with only one `@'. */
4655 len
= strlen (name
);
4656 copy
= bfd_alloc (abfd
, len
);
4658 return (struct elf_link_hash_entry
*) 0 - 1;
4660 first
= p
- name
+ 1;
4661 memcpy (copy
, name
, first
);
4662 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4664 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4667 /* We also need to check references to the symbol without the
4669 copy
[first
- 1] = '\0';
4670 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4671 FALSE
, FALSE
, FALSE
);
4674 bfd_release (abfd
, copy
);
4678 /* Add symbols from an ELF archive file to the linker hash table. We
4679 don't use _bfd_generic_link_add_archive_symbols because of a
4680 problem which arises on UnixWare. The UnixWare libc.so is an
4681 archive which includes an entry libc.so.1 which defines a bunch of
4682 symbols. The libc.so archive also includes a number of other
4683 object files, which also define symbols, some of which are the same
4684 as those defined in libc.so.1. Correct linking requires that we
4685 consider each object file in turn, and include it if it defines any
4686 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4687 this; it looks through the list of undefined symbols, and includes
4688 any object file which defines them. When this algorithm is used on
4689 UnixWare, it winds up pulling in libc.so.1 early and defining a
4690 bunch of symbols. This means that some of the other objects in the
4691 archive are not included in the link, which is incorrect since they
4692 precede libc.so.1 in the archive.
4694 Fortunately, ELF archive handling is simpler than that done by
4695 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4696 oddities. In ELF, if we find a symbol in the archive map, and the
4697 symbol is currently undefined, we know that we must pull in that
4700 Unfortunately, we do have to make multiple passes over the symbol
4701 table until nothing further is resolved. */
4704 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4707 bfd_boolean
*defined
= NULL
;
4708 bfd_boolean
*included
= NULL
;
4712 const struct elf_backend_data
*bed
;
4713 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4714 (bfd
*, struct bfd_link_info
*, const char *);
4716 if (! bfd_has_map (abfd
))
4718 /* An empty archive is a special case. */
4719 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4721 bfd_set_error (bfd_error_no_armap
);
4725 /* Keep track of all symbols we know to be already defined, and all
4726 files we know to be already included. This is to speed up the
4727 second and subsequent passes. */
4728 c
= bfd_ardata (abfd
)->symdef_count
;
4732 amt
*= sizeof (bfd_boolean
);
4733 defined
= bfd_zmalloc (amt
);
4734 included
= bfd_zmalloc (amt
);
4735 if (defined
== NULL
|| included
== NULL
)
4738 symdefs
= bfd_ardata (abfd
)->symdefs
;
4739 bed
= get_elf_backend_data (abfd
);
4740 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4753 symdefend
= symdef
+ c
;
4754 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4756 struct elf_link_hash_entry
*h
;
4758 struct bfd_link_hash_entry
*undefs_tail
;
4761 if (defined
[i
] || included
[i
])
4763 if (symdef
->file_offset
== last
)
4769 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4770 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4776 if (h
->root
.type
== bfd_link_hash_common
)
4778 /* We currently have a common symbol. The archive map contains
4779 a reference to this symbol, so we may want to include it. We
4780 only want to include it however, if this archive element
4781 contains a definition of the symbol, not just another common
4784 Unfortunately some archivers (including GNU ar) will put
4785 declarations of common symbols into their archive maps, as
4786 well as real definitions, so we cannot just go by the archive
4787 map alone. Instead we must read in the element's symbol
4788 table and check that to see what kind of symbol definition
4790 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4793 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4795 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4800 /* We need to include this archive member. */
4801 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4802 if (element
== NULL
)
4805 if (! bfd_check_format (element
, bfd_object
))
4808 /* Doublecheck that we have not included this object
4809 already--it should be impossible, but there may be
4810 something wrong with the archive. */
4811 if (element
->archive_pass
!= 0)
4813 bfd_set_error (bfd_error_bad_value
);
4816 element
->archive_pass
= 1;
4818 undefs_tail
= info
->hash
->undefs_tail
;
4820 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4823 if (! bfd_link_add_symbols (element
, info
))
4826 /* If there are any new undefined symbols, we need to make
4827 another pass through the archive in order to see whether
4828 they can be defined. FIXME: This isn't perfect, because
4829 common symbols wind up on undefs_tail and because an
4830 undefined symbol which is defined later on in this pass
4831 does not require another pass. This isn't a bug, but it
4832 does make the code less efficient than it could be. */
4833 if (undefs_tail
!= info
->hash
->undefs_tail
)
4836 /* Look backward to mark all symbols from this object file
4837 which we have already seen in this pass. */
4841 included
[mark
] = TRUE
;
4846 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4848 /* We mark subsequent symbols from this object file as we go
4849 on through the loop. */
4850 last
= symdef
->file_offset
;
4861 if (defined
!= NULL
)
4863 if (included
!= NULL
)
4868 /* Given an ELF BFD, add symbols to the global hash table as
4872 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4874 switch (bfd_get_format (abfd
))
4877 return elf_link_add_object_symbols (abfd
, info
);
4879 return elf_link_add_archive_symbols (abfd
, info
);
4881 bfd_set_error (bfd_error_wrong_format
);
4886 /* This function will be called though elf_link_hash_traverse to store
4887 all hash value of the exported symbols in an array. */
4890 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4892 unsigned long **valuep
= data
;
4898 if (h
->root
.type
== bfd_link_hash_warning
)
4899 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4901 /* Ignore indirect symbols. These are added by the versioning code. */
4902 if (h
->dynindx
== -1)
4905 name
= h
->root
.root
.string
;
4906 p
= strchr (name
, ELF_VER_CHR
);
4909 alc
= bfd_malloc (p
- name
+ 1);
4910 memcpy (alc
, name
, p
- name
);
4911 alc
[p
- name
] = '\0';
4915 /* Compute the hash value. */
4916 ha
= bfd_elf_hash (name
);
4918 /* Store the found hash value in the array given as the argument. */
4921 /* And store it in the struct so that we can put it in the hash table
4923 h
->u
.elf_hash_value
= ha
;
4931 struct collect_gnu_hash_codes
4934 const struct elf_backend_data
*bed
;
4935 unsigned long int nsyms
;
4936 unsigned long int maskbits
;
4937 unsigned long int *hashcodes
;
4938 unsigned long int *hashval
;
4939 unsigned long int *indx
;
4940 unsigned long int *counts
;
4943 long int min_dynindx
;
4944 unsigned long int bucketcount
;
4945 unsigned long int symindx
;
4946 long int local_indx
;
4947 long int shift1
, shift2
;
4948 unsigned long int mask
;
4951 /* This function will be called though elf_link_hash_traverse to store
4952 all hash value of the exported symbols in an array. */
4955 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4957 struct collect_gnu_hash_codes
*s
= data
;
4963 if (h
->root
.type
== bfd_link_hash_warning
)
4964 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4966 /* Ignore indirect symbols. These are added by the versioning code. */
4967 if (h
->dynindx
== -1)
4970 /* Ignore also local symbols and undefined symbols. */
4971 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4974 name
= h
->root
.root
.string
;
4975 p
= strchr (name
, ELF_VER_CHR
);
4978 alc
= bfd_malloc (p
- name
+ 1);
4979 memcpy (alc
, name
, p
- name
);
4980 alc
[p
- name
] = '\0';
4984 /* Compute the hash value. */
4985 ha
= bfd_elf_gnu_hash (name
);
4987 /* Store the found hash value in the array for compute_bucket_count,
4988 and also for .dynsym reordering purposes. */
4989 s
->hashcodes
[s
->nsyms
] = ha
;
4990 s
->hashval
[h
->dynindx
] = ha
;
4992 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
4993 s
->min_dynindx
= h
->dynindx
;
5001 /* This function will be called though elf_link_hash_traverse to do
5002 final dynaminc symbol renumbering. */
5005 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5007 struct collect_gnu_hash_codes
*s
= data
;
5008 unsigned long int bucket
;
5009 unsigned long int val
;
5011 if (h
->root
.type
== bfd_link_hash_warning
)
5012 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5014 /* Ignore indirect symbols. */
5015 if (h
->dynindx
== -1)
5018 /* Ignore also local symbols and undefined symbols. */
5019 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5021 if (h
->dynindx
>= s
->min_dynindx
)
5022 h
->dynindx
= s
->local_indx
++;
5026 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5027 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5028 & ((s
->maskbits
>> s
->shift1
) - 1);
5029 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5031 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5032 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5033 if (s
->counts
[bucket
] == 1)
5034 /* Last element terminates the chain. */
5036 bfd_put_32 (s
->output_bfd
, val
,
5037 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5038 --s
->counts
[bucket
];
5039 h
->dynindx
= s
->indx
[bucket
]++;
5043 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5046 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5048 return !(h
->forced_local
5049 || h
->root
.type
== bfd_link_hash_undefined
5050 || h
->root
.type
== bfd_link_hash_undefweak
5051 || ((h
->root
.type
== bfd_link_hash_defined
5052 || h
->root
.type
== bfd_link_hash_defweak
)
5053 && h
->root
.u
.def
.section
->output_section
== NULL
));
5056 /* Array used to determine the number of hash table buckets to use
5057 based on the number of symbols there are. If there are fewer than
5058 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5059 fewer than 37 we use 17 buckets, and so forth. We never use more
5060 than 32771 buckets. */
5062 static const size_t elf_buckets
[] =
5064 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5068 /* Compute bucket count for hashing table. We do not use a static set
5069 of possible tables sizes anymore. Instead we determine for all
5070 possible reasonable sizes of the table the outcome (i.e., the
5071 number of collisions etc) and choose the best solution. The
5072 weighting functions are not too simple to allow the table to grow
5073 without bounds. Instead one of the weighting factors is the size.
5074 Therefore the result is always a good payoff between few collisions
5075 (= short chain lengths) and table size. */
5077 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5078 unsigned long int nsyms
, int gnu_hash
)
5080 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5081 size_t best_size
= 0;
5082 unsigned long int i
;
5085 /* We have a problem here. The following code to optimize the table
5086 size requires an integer type with more the 32 bits. If
5087 BFD_HOST_U_64_BIT is set we know about such a type. */
5088 #ifdef BFD_HOST_U_64_BIT
5093 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5094 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5095 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5096 unsigned long int *counts
;
5098 /* Possible optimization parameters: if we have NSYMS symbols we say
5099 that the hashing table must at least have NSYMS/4 and at most
5101 minsize
= nsyms
/ 4;
5104 best_size
= maxsize
= nsyms
* 2;
5109 if ((best_size
& 31) == 0)
5113 /* Create array where we count the collisions in. We must use bfd_malloc
5114 since the size could be large. */
5116 amt
*= sizeof (unsigned long int);
5117 counts
= bfd_malloc (amt
);
5121 /* Compute the "optimal" size for the hash table. The criteria is a
5122 minimal chain length. The minor criteria is (of course) the size
5124 for (i
= minsize
; i
< maxsize
; ++i
)
5126 /* Walk through the array of hashcodes and count the collisions. */
5127 BFD_HOST_U_64_BIT max
;
5128 unsigned long int j
;
5129 unsigned long int fact
;
5131 if (gnu_hash
&& (i
& 31) == 0)
5134 memset (counts
, '\0', i
* sizeof (unsigned long int));
5136 /* Determine how often each hash bucket is used. */
5137 for (j
= 0; j
< nsyms
; ++j
)
5138 ++counts
[hashcodes
[j
] % i
];
5140 /* For the weight function we need some information about the
5141 pagesize on the target. This is information need not be 100%
5142 accurate. Since this information is not available (so far) we
5143 define it here to a reasonable default value. If it is crucial
5144 to have a better value some day simply define this value. */
5145 # ifndef BFD_TARGET_PAGESIZE
5146 # define BFD_TARGET_PAGESIZE (4096)
5149 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5151 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5154 /* Variant 1: optimize for short chains. We add the squares
5155 of all the chain lengths (which favors many small chain
5156 over a few long chains). */
5157 for (j
= 0; j
< i
; ++j
)
5158 max
+= counts
[j
] * counts
[j
];
5160 /* This adds penalties for the overall size of the table. */
5161 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5164 /* Variant 2: Optimize a lot more for small table. Here we
5165 also add squares of the size but we also add penalties for
5166 empty slots (the +1 term). */
5167 for (j
= 0; j
< i
; ++j
)
5168 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5170 /* The overall size of the table is considered, but not as
5171 strong as in variant 1, where it is squared. */
5172 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5176 /* Compare with current best results. */
5177 if (max
< best_chlen
)
5187 #endif /* defined (BFD_HOST_U_64_BIT) */
5189 /* This is the fallback solution if no 64bit type is available or if we
5190 are not supposed to spend much time on optimizations. We select the
5191 bucket count using a fixed set of numbers. */
5192 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5194 best_size
= elf_buckets
[i
];
5195 if (nsyms
< elf_buckets
[i
+ 1])
5198 if (gnu_hash
&& best_size
< 2)
5205 /* Set up the sizes and contents of the ELF dynamic sections. This is
5206 called by the ELF linker emulation before_allocation routine. We
5207 must set the sizes of the sections before the linker sets the
5208 addresses of the various sections. */
5211 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5214 const char *filter_shlib
,
5215 const char * const *auxiliary_filters
,
5216 struct bfd_link_info
*info
,
5217 asection
**sinterpptr
,
5218 struct bfd_elf_version_tree
*verdefs
)
5220 bfd_size_type soname_indx
;
5222 const struct elf_backend_data
*bed
;
5223 struct elf_assign_sym_version_info asvinfo
;
5227 soname_indx
= (bfd_size_type
) -1;
5229 if (!is_elf_hash_table (info
->hash
))
5232 elf_tdata (output_bfd
)->relro
= info
->relro
;
5233 if (info
->execstack
)
5234 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5235 else if (info
->noexecstack
)
5236 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5240 asection
*notesec
= NULL
;
5243 for (inputobj
= info
->input_bfds
;
5245 inputobj
= inputobj
->link_next
)
5249 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5251 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5254 if (s
->flags
& SEC_CODE
)
5263 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5264 if (exec
&& info
->relocatable
5265 && notesec
->output_section
!= bfd_abs_section_ptr
)
5266 notesec
->output_section
->flags
|= SEC_CODE
;
5270 /* Any syms created from now on start with -1 in
5271 got.refcount/offset and plt.refcount/offset. */
5272 elf_hash_table (info
)->init_got_refcount
5273 = elf_hash_table (info
)->init_got_offset
;
5274 elf_hash_table (info
)->init_plt_refcount
5275 = elf_hash_table (info
)->init_plt_offset
;
5277 /* The backend may have to create some sections regardless of whether
5278 we're dynamic or not. */
5279 bed
= get_elf_backend_data (output_bfd
);
5280 if (bed
->elf_backend_always_size_sections
5281 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5284 dynobj
= elf_hash_table (info
)->dynobj
;
5286 /* If there were no dynamic objects in the link, there is nothing to
5291 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5294 if (elf_hash_table (info
)->dynamic_sections_created
)
5296 struct elf_info_failed eif
;
5297 struct elf_link_hash_entry
*h
;
5299 struct bfd_elf_version_tree
*t
;
5300 struct bfd_elf_version_expr
*d
;
5302 bfd_boolean all_defined
;
5304 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5305 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5309 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5311 if (soname_indx
== (bfd_size_type
) -1
5312 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5318 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5320 info
->flags
|= DF_SYMBOLIC
;
5327 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5329 if (indx
== (bfd_size_type
) -1
5330 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5333 if (info
->new_dtags
)
5335 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5336 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5341 if (filter_shlib
!= NULL
)
5345 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5346 filter_shlib
, TRUE
);
5347 if (indx
== (bfd_size_type
) -1
5348 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5352 if (auxiliary_filters
!= NULL
)
5354 const char * const *p
;
5356 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5360 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5362 if (indx
== (bfd_size_type
) -1
5363 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5369 eif
.verdefs
= verdefs
;
5372 /* If we are supposed to export all symbols into the dynamic symbol
5373 table (this is not the normal case), then do so. */
5374 if (info
->export_dynamic
5375 || (info
->executable
&& info
->dynamic
))
5377 elf_link_hash_traverse (elf_hash_table (info
),
5378 _bfd_elf_export_symbol
,
5384 /* Make all global versions with definition. */
5385 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5386 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5387 if (!d
->symver
&& d
->symbol
)
5389 const char *verstr
, *name
;
5390 size_t namelen
, verlen
, newlen
;
5392 struct elf_link_hash_entry
*newh
;
5395 namelen
= strlen (name
);
5397 verlen
= strlen (verstr
);
5398 newlen
= namelen
+ verlen
+ 3;
5400 newname
= bfd_malloc (newlen
);
5401 if (newname
== NULL
)
5403 memcpy (newname
, name
, namelen
);
5405 /* Check the hidden versioned definition. */
5406 p
= newname
+ namelen
;
5408 memcpy (p
, verstr
, verlen
+ 1);
5409 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5410 newname
, FALSE
, FALSE
,
5413 || (newh
->root
.type
!= bfd_link_hash_defined
5414 && newh
->root
.type
!= bfd_link_hash_defweak
))
5416 /* Check the default versioned definition. */
5418 memcpy (p
, verstr
, verlen
+ 1);
5419 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5420 newname
, FALSE
, FALSE
,
5425 /* Mark this version if there is a definition and it is
5426 not defined in a shared object. */
5428 && !newh
->def_dynamic
5429 && (newh
->root
.type
== bfd_link_hash_defined
5430 || newh
->root
.type
== bfd_link_hash_defweak
))
5434 /* Attach all the symbols to their version information. */
5435 asvinfo
.output_bfd
= output_bfd
;
5436 asvinfo
.info
= info
;
5437 asvinfo
.verdefs
= verdefs
;
5438 asvinfo
.failed
= FALSE
;
5440 elf_link_hash_traverse (elf_hash_table (info
),
5441 _bfd_elf_link_assign_sym_version
,
5446 if (!info
->allow_undefined_version
)
5448 /* Check if all global versions have a definition. */
5450 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5451 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5452 if (!d
->symver
&& !d
->script
)
5454 (*_bfd_error_handler
)
5455 (_("%s: undefined version: %s"),
5456 d
->pattern
, t
->name
);
5457 all_defined
= FALSE
;
5462 bfd_set_error (bfd_error_bad_value
);
5467 /* Find all symbols which were defined in a dynamic object and make
5468 the backend pick a reasonable value for them. */
5469 elf_link_hash_traverse (elf_hash_table (info
),
5470 _bfd_elf_adjust_dynamic_symbol
,
5475 /* Add some entries to the .dynamic section. We fill in some of the
5476 values later, in bfd_elf_final_link, but we must add the entries
5477 now so that we know the final size of the .dynamic section. */
5479 /* If there are initialization and/or finalization functions to
5480 call then add the corresponding DT_INIT/DT_FINI entries. */
5481 h
= (info
->init_function
5482 ? elf_link_hash_lookup (elf_hash_table (info
),
5483 info
->init_function
, FALSE
,
5490 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5493 h
= (info
->fini_function
5494 ? elf_link_hash_lookup (elf_hash_table (info
),
5495 info
->fini_function
, FALSE
,
5502 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5506 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5507 if (s
!= NULL
&& s
->linker_has_input
)
5509 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5510 if (! info
->executable
)
5515 for (sub
= info
->input_bfds
; sub
!= NULL
;
5516 sub
= sub
->link_next
)
5517 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5518 if (elf_section_data (o
)->this_hdr
.sh_type
5519 == SHT_PREINIT_ARRAY
)
5521 (*_bfd_error_handler
)
5522 (_("%B: .preinit_array section is not allowed in DSO"),
5527 bfd_set_error (bfd_error_nonrepresentable_section
);
5531 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5532 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5535 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5536 if (s
!= NULL
&& s
->linker_has_input
)
5538 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5539 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5542 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5543 if (s
!= NULL
&& s
->linker_has_input
)
5545 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5546 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5550 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5551 /* If .dynstr is excluded from the link, we don't want any of
5552 these tags. Strictly, we should be checking each section
5553 individually; This quick check covers for the case where
5554 someone does a /DISCARD/ : { *(*) }. */
5555 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5557 bfd_size_type strsize
;
5559 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5560 if ((info
->emit_hash
5561 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5562 || (info
->emit_gnu_hash
5563 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5564 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5565 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5566 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5567 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5568 bed
->s
->sizeof_sym
))
5573 /* The backend must work out the sizes of all the other dynamic
5575 if (bed
->elf_backend_size_dynamic_sections
5576 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5579 if (elf_hash_table (info
)->dynamic_sections_created
)
5581 unsigned long section_sym_count
;
5584 /* Set up the version definition section. */
5585 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5586 BFD_ASSERT (s
!= NULL
);
5588 /* We may have created additional version definitions if we are
5589 just linking a regular application. */
5590 verdefs
= asvinfo
.verdefs
;
5592 /* Skip anonymous version tag. */
5593 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5594 verdefs
= verdefs
->next
;
5596 if (verdefs
== NULL
&& !info
->create_default_symver
)
5597 s
->flags
|= SEC_EXCLUDE
;
5602 struct bfd_elf_version_tree
*t
;
5604 Elf_Internal_Verdef def
;
5605 Elf_Internal_Verdaux defaux
;
5606 struct bfd_link_hash_entry
*bh
;
5607 struct elf_link_hash_entry
*h
;
5613 /* Make space for the base version. */
5614 size
+= sizeof (Elf_External_Verdef
);
5615 size
+= sizeof (Elf_External_Verdaux
);
5618 /* Make space for the default version. */
5619 if (info
->create_default_symver
)
5621 size
+= sizeof (Elf_External_Verdef
);
5625 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5627 struct bfd_elf_version_deps
*n
;
5629 size
+= sizeof (Elf_External_Verdef
);
5630 size
+= sizeof (Elf_External_Verdaux
);
5633 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5634 size
+= sizeof (Elf_External_Verdaux
);
5638 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5639 if (s
->contents
== NULL
&& s
->size
!= 0)
5642 /* Fill in the version definition section. */
5646 def
.vd_version
= VER_DEF_CURRENT
;
5647 def
.vd_flags
= VER_FLG_BASE
;
5650 if (info
->create_default_symver
)
5652 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5653 def
.vd_next
= sizeof (Elf_External_Verdef
);
5657 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5658 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5659 + sizeof (Elf_External_Verdaux
));
5662 if (soname_indx
!= (bfd_size_type
) -1)
5664 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5666 def
.vd_hash
= bfd_elf_hash (soname
);
5667 defaux
.vda_name
= soname_indx
;
5674 name
= lbasename (output_bfd
->filename
);
5675 def
.vd_hash
= bfd_elf_hash (name
);
5676 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5678 if (indx
== (bfd_size_type
) -1)
5680 defaux
.vda_name
= indx
;
5682 defaux
.vda_next
= 0;
5684 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5685 (Elf_External_Verdef
*) p
);
5686 p
+= sizeof (Elf_External_Verdef
);
5687 if (info
->create_default_symver
)
5689 /* Add a symbol representing this version. */
5691 if (! (_bfd_generic_link_add_one_symbol
5692 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5694 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5696 h
= (struct elf_link_hash_entry
*) bh
;
5699 h
->type
= STT_OBJECT
;
5700 h
->verinfo
.vertree
= NULL
;
5702 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5705 /* Create a duplicate of the base version with the same
5706 aux block, but different flags. */
5709 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5711 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5712 + sizeof (Elf_External_Verdaux
));
5715 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5716 (Elf_External_Verdef
*) p
);
5717 p
+= sizeof (Elf_External_Verdef
);
5719 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5720 (Elf_External_Verdaux
*) p
);
5721 p
+= sizeof (Elf_External_Verdaux
);
5723 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5726 struct bfd_elf_version_deps
*n
;
5729 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5732 /* Add a symbol representing this version. */
5734 if (! (_bfd_generic_link_add_one_symbol
5735 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5737 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5739 h
= (struct elf_link_hash_entry
*) bh
;
5742 h
->type
= STT_OBJECT
;
5743 h
->verinfo
.vertree
= t
;
5745 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5748 def
.vd_version
= VER_DEF_CURRENT
;
5750 if (t
->globals
.list
== NULL
5751 && t
->locals
.list
== NULL
5753 def
.vd_flags
|= VER_FLG_WEAK
;
5754 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5755 def
.vd_cnt
= cdeps
+ 1;
5756 def
.vd_hash
= bfd_elf_hash (t
->name
);
5757 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5759 if (t
->next
!= NULL
)
5760 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5761 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5763 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5764 (Elf_External_Verdef
*) p
);
5765 p
+= sizeof (Elf_External_Verdef
);
5767 defaux
.vda_name
= h
->dynstr_index
;
5768 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5770 defaux
.vda_next
= 0;
5771 if (t
->deps
!= NULL
)
5772 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5773 t
->name_indx
= defaux
.vda_name
;
5775 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5776 (Elf_External_Verdaux
*) p
);
5777 p
+= sizeof (Elf_External_Verdaux
);
5779 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5781 if (n
->version_needed
== NULL
)
5783 /* This can happen if there was an error in the
5785 defaux
.vda_name
= 0;
5789 defaux
.vda_name
= n
->version_needed
->name_indx
;
5790 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5793 if (n
->next
== NULL
)
5794 defaux
.vda_next
= 0;
5796 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5798 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5799 (Elf_External_Verdaux
*) p
);
5800 p
+= sizeof (Elf_External_Verdaux
);
5804 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5805 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5808 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5811 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5813 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5816 else if (info
->flags
& DF_BIND_NOW
)
5818 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5824 if (info
->executable
)
5825 info
->flags_1
&= ~ (DF_1_INITFIRST
5828 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5832 /* Work out the size of the version reference section. */
5834 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5835 BFD_ASSERT (s
!= NULL
);
5837 struct elf_find_verdep_info sinfo
;
5839 sinfo
.output_bfd
= output_bfd
;
5841 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5842 if (sinfo
.vers
== 0)
5844 sinfo
.failed
= FALSE
;
5846 elf_link_hash_traverse (elf_hash_table (info
),
5847 _bfd_elf_link_find_version_dependencies
,
5850 if (elf_tdata (output_bfd
)->verref
== NULL
)
5851 s
->flags
|= SEC_EXCLUDE
;
5854 Elf_Internal_Verneed
*t
;
5859 /* Build the version definition section. */
5862 for (t
= elf_tdata (output_bfd
)->verref
;
5866 Elf_Internal_Vernaux
*a
;
5868 size
+= sizeof (Elf_External_Verneed
);
5870 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5871 size
+= sizeof (Elf_External_Vernaux
);
5875 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5876 if (s
->contents
== NULL
)
5880 for (t
= elf_tdata (output_bfd
)->verref
;
5885 Elf_Internal_Vernaux
*a
;
5889 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5892 t
->vn_version
= VER_NEED_CURRENT
;
5894 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5895 elf_dt_name (t
->vn_bfd
) != NULL
5896 ? elf_dt_name (t
->vn_bfd
)
5897 : lbasename (t
->vn_bfd
->filename
),
5899 if (indx
== (bfd_size_type
) -1)
5902 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5903 if (t
->vn_nextref
== NULL
)
5906 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5907 + caux
* sizeof (Elf_External_Vernaux
));
5909 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5910 (Elf_External_Verneed
*) p
);
5911 p
+= sizeof (Elf_External_Verneed
);
5913 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5915 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5916 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5917 a
->vna_nodename
, FALSE
);
5918 if (indx
== (bfd_size_type
) -1)
5921 if (a
->vna_nextptr
== NULL
)
5924 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5926 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5927 (Elf_External_Vernaux
*) p
);
5928 p
+= sizeof (Elf_External_Vernaux
);
5932 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5933 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5936 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5940 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5941 && elf_tdata (output_bfd
)->cverdefs
== 0)
5942 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5943 §ion_sym_count
) == 0)
5945 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5946 s
->flags
|= SEC_EXCLUDE
;
5952 /* Find the first non-excluded output section. We'll use its
5953 section symbol for some emitted relocs. */
5955 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
5959 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5960 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
5961 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5963 elf_hash_table (info
)->text_index_section
= s
;
5968 /* Find two non-excluded output sections, one for code, one for data.
5969 We'll use their section symbols for some emitted relocs. */
5971 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5975 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5976 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
5977 == (SEC_ALLOC
| SEC_READONLY
))
5978 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5980 elf_hash_table (info
)->text_index_section
= s
;
5984 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5985 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
5986 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5988 elf_hash_table (info
)->data_index_section
= s
;
5992 if (elf_hash_table (info
)->text_index_section
== NULL
)
5993 elf_hash_table (info
)->text_index_section
5994 = elf_hash_table (info
)->data_index_section
;
5998 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6000 const struct elf_backend_data
*bed
;
6002 if (!is_elf_hash_table (info
->hash
))
6005 bed
= get_elf_backend_data (output_bfd
);
6006 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6008 if (elf_hash_table (info
)->dynamic_sections_created
)
6012 bfd_size_type dynsymcount
;
6013 unsigned long section_sym_count
;
6014 unsigned int dtagcount
;
6016 dynobj
= elf_hash_table (info
)->dynobj
;
6018 /* Assign dynsym indicies. In a shared library we generate a
6019 section symbol for each output section, which come first.
6020 Next come all of the back-end allocated local dynamic syms,
6021 followed by the rest of the global symbols. */
6023 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6024 §ion_sym_count
);
6026 /* Work out the size of the symbol version section. */
6027 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6028 BFD_ASSERT (s
!= NULL
);
6029 if (dynsymcount
!= 0
6030 && (s
->flags
& SEC_EXCLUDE
) == 0)
6032 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6033 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6034 if (s
->contents
== NULL
)
6037 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6041 /* Set the size of the .dynsym and .hash sections. We counted
6042 the number of dynamic symbols in elf_link_add_object_symbols.
6043 We will build the contents of .dynsym and .hash when we build
6044 the final symbol table, because until then we do not know the
6045 correct value to give the symbols. We built the .dynstr
6046 section as we went along in elf_link_add_object_symbols. */
6047 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6048 BFD_ASSERT (s
!= NULL
);
6049 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6051 if (dynsymcount
!= 0)
6053 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6054 if (s
->contents
== NULL
)
6057 /* The first entry in .dynsym is a dummy symbol.
6058 Clear all the section syms, in case we don't output them all. */
6059 ++section_sym_count
;
6060 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6063 elf_hash_table (info
)->bucketcount
= 0;
6065 /* Compute the size of the hashing table. As a side effect this
6066 computes the hash values for all the names we export. */
6067 if (info
->emit_hash
)
6069 unsigned long int *hashcodes
;
6070 unsigned long int *hashcodesp
;
6072 unsigned long int nsyms
;
6074 size_t hash_entry_size
;
6076 /* Compute the hash values for all exported symbols. At the same
6077 time store the values in an array so that we could use them for
6079 amt
= dynsymcount
* sizeof (unsigned long int);
6080 hashcodes
= bfd_malloc (amt
);
6081 if (hashcodes
== NULL
)
6083 hashcodesp
= hashcodes
;
6085 /* Put all hash values in HASHCODES. */
6086 elf_link_hash_traverse (elf_hash_table (info
),
6087 elf_collect_hash_codes
, &hashcodesp
);
6089 nsyms
= hashcodesp
- hashcodes
;
6091 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6094 if (bucketcount
== 0)
6097 elf_hash_table (info
)->bucketcount
= bucketcount
;
6099 s
= bfd_get_section_by_name (dynobj
, ".hash");
6100 BFD_ASSERT (s
!= NULL
);
6101 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6102 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6103 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6104 if (s
->contents
== NULL
)
6107 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6108 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6109 s
->contents
+ hash_entry_size
);
6112 if (info
->emit_gnu_hash
)
6115 unsigned char *contents
;
6116 struct collect_gnu_hash_codes cinfo
;
6120 memset (&cinfo
, 0, sizeof (cinfo
));
6122 /* Compute the hash values for all exported symbols. At the same
6123 time store the values in an array so that we could use them for
6125 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6126 cinfo
.hashcodes
= bfd_malloc (amt
);
6127 if (cinfo
.hashcodes
== NULL
)
6130 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6131 cinfo
.min_dynindx
= -1;
6132 cinfo
.output_bfd
= output_bfd
;
6135 /* Put all hash values in HASHCODES. */
6136 elf_link_hash_traverse (elf_hash_table (info
),
6137 elf_collect_gnu_hash_codes
, &cinfo
);
6140 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6142 if (bucketcount
== 0)
6144 free (cinfo
.hashcodes
);
6148 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6149 BFD_ASSERT (s
!= NULL
);
6151 if (cinfo
.nsyms
== 0)
6153 /* Empty .gnu.hash section is special. */
6154 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6155 free (cinfo
.hashcodes
);
6156 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6157 contents
= bfd_zalloc (output_bfd
, s
->size
);
6158 if (contents
== NULL
)
6160 s
->contents
= contents
;
6161 /* 1 empty bucket. */
6162 bfd_put_32 (output_bfd
, 1, contents
);
6163 /* SYMIDX above the special symbol 0. */
6164 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6165 /* Just one word for bitmask. */
6166 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6167 /* Only hash fn bloom filter. */
6168 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6169 /* No hashes are valid - empty bitmask. */
6170 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6171 /* No hashes in the only bucket. */
6172 bfd_put_32 (output_bfd
, 0,
6173 contents
+ 16 + bed
->s
->arch_size
/ 8);
6177 unsigned long int maskwords
, maskbitslog2
;
6178 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6180 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6181 if (maskbitslog2
< 3)
6183 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6184 maskbitslog2
= maskbitslog2
+ 3;
6186 maskbitslog2
= maskbitslog2
+ 2;
6187 if (bed
->s
->arch_size
== 64)
6189 if (maskbitslog2
== 5)
6195 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6196 cinfo
.shift2
= maskbitslog2
;
6197 cinfo
.maskbits
= 1 << maskbitslog2
;
6198 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6199 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6200 amt
+= maskwords
* sizeof (bfd_vma
);
6201 cinfo
.bitmask
= bfd_malloc (amt
);
6202 if (cinfo
.bitmask
== NULL
)
6204 free (cinfo
.hashcodes
);
6208 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6209 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6210 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6211 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6213 /* Determine how often each hash bucket is used. */
6214 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6215 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6216 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6218 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6219 if (cinfo
.counts
[i
] != 0)
6221 cinfo
.indx
[i
] = cnt
;
6222 cnt
+= cinfo
.counts
[i
];
6224 BFD_ASSERT (cnt
== dynsymcount
);
6225 cinfo
.bucketcount
= bucketcount
;
6226 cinfo
.local_indx
= cinfo
.min_dynindx
;
6228 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6229 s
->size
+= cinfo
.maskbits
/ 8;
6230 contents
= bfd_zalloc (output_bfd
, s
->size
);
6231 if (contents
== NULL
)
6233 free (cinfo
.bitmask
);
6234 free (cinfo
.hashcodes
);
6238 s
->contents
= contents
;
6239 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6240 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6241 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6242 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6243 contents
+= 16 + cinfo
.maskbits
/ 8;
6245 for (i
= 0; i
< bucketcount
; ++i
)
6247 if (cinfo
.counts
[i
] == 0)
6248 bfd_put_32 (output_bfd
, 0, contents
);
6250 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6254 cinfo
.contents
= contents
;
6256 /* Renumber dynamic symbols, populate .gnu.hash section. */
6257 elf_link_hash_traverse (elf_hash_table (info
),
6258 elf_renumber_gnu_hash_syms
, &cinfo
);
6260 contents
= s
->contents
+ 16;
6261 for (i
= 0; i
< maskwords
; ++i
)
6263 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6265 contents
+= bed
->s
->arch_size
/ 8;
6268 free (cinfo
.bitmask
);
6269 free (cinfo
.hashcodes
);
6273 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6274 BFD_ASSERT (s
!= NULL
);
6276 elf_finalize_dynstr (output_bfd
, info
);
6278 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6280 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6281 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6288 /* Final phase of ELF linker. */
6290 /* A structure we use to avoid passing large numbers of arguments. */
6292 struct elf_final_link_info
6294 /* General link information. */
6295 struct bfd_link_info
*info
;
6298 /* Symbol string table. */
6299 struct bfd_strtab_hash
*symstrtab
;
6300 /* .dynsym section. */
6301 asection
*dynsym_sec
;
6302 /* .hash section. */
6304 /* symbol version section (.gnu.version). */
6305 asection
*symver_sec
;
6306 /* Buffer large enough to hold contents of any section. */
6308 /* Buffer large enough to hold external relocs of any section. */
6309 void *external_relocs
;
6310 /* Buffer large enough to hold internal relocs of any section. */
6311 Elf_Internal_Rela
*internal_relocs
;
6312 /* Buffer large enough to hold external local symbols of any input
6314 bfd_byte
*external_syms
;
6315 /* And a buffer for symbol section indices. */
6316 Elf_External_Sym_Shndx
*locsym_shndx
;
6317 /* Buffer large enough to hold internal local symbols of any input
6319 Elf_Internal_Sym
*internal_syms
;
6320 /* Array large enough to hold a symbol index for each local symbol
6321 of any input BFD. */
6323 /* Array large enough to hold a section pointer for each local
6324 symbol of any input BFD. */
6325 asection
**sections
;
6326 /* Buffer to hold swapped out symbols. */
6328 /* And one for symbol section indices. */
6329 Elf_External_Sym_Shndx
*symshndxbuf
;
6330 /* Number of swapped out symbols in buffer. */
6331 size_t symbuf_count
;
6332 /* Number of symbols which fit in symbuf. */
6334 /* And same for symshndxbuf. */
6335 size_t shndxbuf_size
;
6338 /* This struct is used to pass information to elf_link_output_extsym. */
6340 struct elf_outext_info
6343 bfd_boolean localsyms
;
6344 struct elf_final_link_info
*finfo
;
6347 /* When performing a relocatable link, the input relocations are
6348 preserved. But, if they reference global symbols, the indices
6349 referenced must be updated. Update all the relocations in
6350 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
6353 elf_link_adjust_relocs (bfd
*abfd
,
6354 Elf_Internal_Shdr
*rel_hdr
,
6356 struct elf_link_hash_entry
**rel_hash
)
6359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6361 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6362 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6363 bfd_vma r_type_mask
;
6366 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
6368 swap_in
= bed
->s
->swap_reloc_in
;
6369 swap_out
= bed
->s
->swap_reloc_out
;
6371 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
6373 swap_in
= bed
->s
->swap_reloca_in
;
6374 swap_out
= bed
->s
->swap_reloca_out
;
6379 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
6382 if (bed
->s
->arch_size
== 32)
6389 r_type_mask
= 0xffffffff;
6393 erela
= rel_hdr
->contents
;
6394 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
6396 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
6399 if (*rel_hash
== NULL
)
6402 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
6404 (*swap_in
) (abfd
, erela
, irela
);
6405 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
6406 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
6407 | (irela
[j
].r_info
& r_type_mask
));
6408 (*swap_out
) (abfd
, irela
, erela
);
6412 struct elf_link_sort_rela
6418 enum elf_reloc_type_class type
;
6419 /* We use this as an array of size int_rels_per_ext_rel. */
6420 Elf_Internal_Rela rela
[1];
6424 elf_link_sort_cmp1 (const void *A
, const void *B
)
6426 const struct elf_link_sort_rela
*a
= A
;
6427 const struct elf_link_sort_rela
*b
= B
;
6428 int relativea
, relativeb
;
6430 relativea
= a
->type
== reloc_class_relative
;
6431 relativeb
= b
->type
== reloc_class_relative
;
6433 if (relativea
< relativeb
)
6435 if (relativea
> relativeb
)
6437 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
6439 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
6441 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6443 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6449 elf_link_sort_cmp2 (const void *A
, const void *B
)
6451 const struct elf_link_sort_rela
*a
= A
;
6452 const struct elf_link_sort_rela
*b
= B
;
6455 if (a
->u
.offset
< b
->u
.offset
)
6457 if (a
->u
.offset
> b
->u
.offset
)
6459 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
6460 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
6465 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
6467 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
6473 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
6476 bfd_size_type count
, size
;
6477 size_t i
, ret
, sort_elt
, ext_size
;
6478 bfd_byte
*sort
, *s_non_relative
, *p
;
6479 struct elf_link_sort_rela
*sq
;
6480 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6481 int i2e
= bed
->s
->int_rels_per_ext_rel
;
6482 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
6483 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
6484 struct bfd_link_order
*lo
;
6487 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
6488 if (reldyn
== NULL
|| reldyn
->size
== 0)
6490 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
6491 if (reldyn
== NULL
|| reldyn
->size
== 0)
6493 ext_size
= bed
->s
->sizeof_rel
;
6494 swap_in
= bed
->s
->swap_reloc_in
;
6495 swap_out
= bed
->s
->swap_reloc_out
;
6499 ext_size
= bed
->s
->sizeof_rela
;
6500 swap_in
= bed
->s
->swap_reloca_in
;
6501 swap_out
= bed
->s
->swap_reloca_out
;
6503 count
= reldyn
->size
/ ext_size
;
6506 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6507 if (lo
->type
== bfd_indirect_link_order
)
6509 asection
*o
= lo
->u
.indirect
.section
;
6513 if (size
!= reldyn
->size
)
6516 sort_elt
= (sizeof (struct elf_link_sort_rela
)
6517 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
6518 sort
= bfd_zmalloc (sort_elt
* count
);
6521 (*info
->callbacks
->warning
)
6522 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
6526 if (bed
->s
->arch_size
== 32)
6527 r_sym_mask
= ~(bfd_vma
) 0xff;
6529 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6531 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6532 if (lo
->type
== bfd_indirect_link_order
)
6534 bfd_byte
*erel
, *erelend
;
6535 asection
*o
= lo
->u
.indirect
.section
;
6537 if (o
->contents
== NULL
&& o
->size
!= 0)
6539 /* This is a reloc section that is being handled as a normal
6540 section. See bfd_section_from_shdr. We can't combine
6541 relocs in this case. */
6546 erelend
= o
->contents
+ o
->size
;
6547 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6548 while (erel
< erelend
)
6550 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6551 (*swap_in
) (abfd
, erel
, s
->rela
);
6552 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6553 s
->u
.sym_mask
= r_sym_mask
;
6559 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6561 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6563 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6564 if (s
->type
!= reloc_class_relative
)
6570 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6571 for (; i
< count
; i
++, p
+= sort_elt
)
6573 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6574 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6576 sp
->u
.offset
= sq
->rela
->r_offset
;
6579 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6581 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6582 if (lo
->type
== bfd_indirect_link_order
)
6584 bfd_byte
*erel
, *erelend
;
6585 asection
*o
= lo
->u
.indirect
.section
;
6588 erelend
= o
->contents
+ o
->size
;
6589 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6590 while (erel
< erelend
)
6592 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6593 (*swap_out
) (abfd
, s
->rela
, erel
);
6604 /* Flush the output symbols to the file. */
6607 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6608 const struct elf_backend_data
*bed
)
6610 if (finfo
->symbuf_count
> 0)
6612 Elf_Internal_Shdr
*hdr
;
6616 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6617 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6618 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6619 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6620 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6623 hdr
->sh_size
+= amt
;
6624 finfo
->symbuf_count
= 0;
6630 /* Add a symbol to the output symbol table. */
6633 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6635 Elf_Internal_Sym
*elfsym
,
6636 asection
*input_sec
,
6637 struct elf_link_hash_entry
*h
)
6640 Elf_External_Sym_Shndx
*destshndx
;
6641 bfd_boolean (*output_symbol_hook
)
6642 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6643 struct elf_link_hash_entry
*);
6644 const struct elf_backend_data
*bed
;
6646 bed
= get_elf_backend_data (finfo
->output_bfd
);
6647 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6648 if (output_symbol_hook
!= NULL
)
6650 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6654 if (name
== NULL
|| *name
== '\0')
6655 elfsym
->st_name
= 0;
6656 else if (input_sec
->flags
& SEC_EXCLUDE
)
6657 elfsym
->st_name
= 0;
6660 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6662 if (elfsym
->st_name
== (unsigned long) -1)
6666 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6668 if (! elf_link_flush_output_syms (finfo
, bed
))
6672 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6673 destshndx
= finfo
->symshndxbuf
;
6674 if (destshndx
!= NULL
)
6676 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6680 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6681 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6682 if (destshndx
== NULL
)
6684 memset ((char *) destshndx
+ amt
, 0, amt
);
6685 finfo
->shndxbuf_size
*= 2;
6687 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6690 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6691 finfo
->symbuf_count
+= 1;
6692 bfd_get_symcount (finfo
->output_bfd
) += 1;
6697 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6700 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
6702 if (sym
->st_shndx
> SHN_HIRESERVE
)
6704 /* The gABI doesn't support dynamic symbols in output sections
6706 (*_bfd_error_handler
)
6707 (_("%B: Too many sections: %d (>= %d)"),
6708 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
6709 bfd_set_error (bfd_error_nonrepresentable_section
);
6715 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6716 allowing an unsatisfied unversioned symbol in the DSO to match a
6717 versioned symbol that would normally require an explicit version.
6718 We also handle the case that a DSO references a hidden symbol
6719 which may be satisfied by a versioned symbol in another DSO. */
6722 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6723 const struct elf_backend_data
*bed
,
6724 struct elf_link_hash_entry
*h
)
6727 struct elf_link_loaded_list
*loaded
;
6729 if (!is_elf_hash_table (info
->hash
))
6732 switch (h
->root
.type
)
6738 case bfd_link_hash_undefined
:
6739 case bfd_link_hash_undefweak
:
6740 abfd
= h
->root
.u
.undef
.abfd
;
6741 if ((abfd
->flags
& DYNAMIC
) == 0
6742 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6746 case bfd_link_hash_defined
:
6747 case bfd_link_hash_defweak
:
6748 abfd
= h
->root
.u
.def
.section
->owner
;
6751 case bfd_link_hash_common
:
6752 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6755 BFD_ASSERT (abfd
!= NULL
);
6757 for (loaded
= elf_hash_table (info
)->loaded
;
6759 loaded
= loaded
->next
)
6762 Elf_Internal_Shdr
*hdr
;
6763 bfd_size_type symcount
;
6764 bfd_size_type extsymcount
;
6765 bfd_size_type extsymoff
;
6766 Elf_Internal_Shdr
*versymhdr
;
6767 Elf_Internal_Sym
*isym
;
6768 Elf_Internal_Sym
*isymend
;
6769 Elf_Internal_Sym
*isymbuf
;
6770 Elf_External_Versym
*ever
;
6771 Elf_External_Versym
*extversym
;
6773 input
= loaded
->abfd
;
6775 /* We check each DSO for a possible hidden versioned definition. */
6777 || (input
->flags
& DYNAMIC
) == 0
6778 || elf_dynversym (input
) == 0)
6781 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6783 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6784 if (elf_bad_symtab (input
))
6786 extsymcount
= symcount
;
6791 extsymcount
= symcount
- hdr
->sh_info
;
6792 extsymoff
= hdr
->sh_info
;
6795 if (extsymcount
== 0)
6798 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6800 if (isymbuf
== NULL
)
6803 /* Read in any version definitions. */
6804 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6805 extversym
= bfd_malloc (versymhdr
->sh_size
);
6806 if (extversym
== NULL
)
6809 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6810 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6811 != versymhdr
->sh_size
))
6819 ever
= extversym
+ extsymoff
;
6820 isymend
= isymbuf
+ extsymcount
;
6821 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6824 Elf_Internal_Versym iver
;
6825 unsigned short version_index
;
6827 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6828 || isym
->st_shndx
== SHN_UNDEF
)
6831 name
= bfd_elf_string_from_elf_section (input
,
6834 if (strcmp (name
, h
->root
.root
.string
) != 0)
6837 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6839 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6841 /* If we have a non-hidden versioned sym, then it should
6842 have provided a definition for the undefined sym. */
6846 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6847 if (version_index
== 1 || version_index
== 2)
6849 /* This is the base or first version. We can use it. */
6863 /* Add an external symbol to the symbol table. This is called from
6864 the hash table traversal routine. When generating a shared object,
6865 we go through the symbol table twice. The first time we output
6866 anything that might have been forced to local scope in a version
6867 script. The second time we output the symbols that are still
6871 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6873 struct elf_outext_info
*eoinfo
= data
;
6874 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6876 Elf_Internal_Sym sym
;
6877 asection
*input_sec
;
6878 const struct elf_backend_data
*bed
;
6880 if (h
->root
.type
== bfd_link_hash_warning
)
6882 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6883 if (h
->root
.type
== bfd_link_hash_new
)
6887 /* Decide whether to output this symbol in this pass. */
6888 if (eoinfo
->localsyms
)
6890 if (!h
->forced_local
)
6895 if (h
->forced_local
)
6899 bed
= get_elf_backend_data (finfo
->output_bfd
);
6901 if (h
->root
.type
== bfd_link_hash_undefined
)
6903 /* If we have an undefined symbol reference here then it must have
6904 come from a shared library that is being linked in. (Undefined
6905 references in regular files have already been handled). */
6906 bfd_boolean ignore_undef
= FALSE
;
6908 /* Some symbols may be special in that the fact that they're
6909 undefined can be safely ignored - let backend determine that. */
6910 if (bed
->elf_backend_ignore_undef_symbol
)
6911 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
6913 /* If we are reporting errors for this situation then do so now. */
6914 if (ignore_undef
== FALSE
6917 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6918 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6920 if (! (finfo
->info
->callbacks
->undefined_symbol
6921 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6922 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6924 eoinfo
->failed
= TRUE
;
6930 /* We should also warn if a forced local symbol is referenced from
6931 shared libraries. */
6932 if (! finfo
->info
->relocatable
6933 && (! finfo
->info
->shared
)
6938 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6940 (*_bfd_error_handler
)
6941 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6943 h
->root
.u
.def
.section
== bfd_abs_section_ptr
6944 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
6945 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6947 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6948 ? "hidden" : "local",
6949 h
->root
.root
.string
);
6950 eoinfo
->failed
= TRUE
;
6954 /* We don't want to output symbols that have never been mentioned by
6955 a regular file, or that we have been told to strip. However, if
6956 h->indx is set to -2, the symbol is used by a reloc and we must
6960 else if ((h
->def_dynamic
6962 || h
->root
.type
== bfd_link_hash_new
)
6966 else if (finfo
->info
->strip
== strip_all
)
6968 else if (finfo
->info
->strip
== strip_some
6969 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6970 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6972 else if (finfo
->info
->strip_discarded
6973 && (h
->root
.type
== bfd_link_hash_defined
6974 || h
->root
.type
== bfd_link_hash_defweak
)
6975 && elf_discarded_section (h
->root
.u
.def
.section
))
6980 /* If we're stripping it, and it's not a dynamic symbol, there's
6981 nothing else to do unless it is a forced local symbol. */
6984 && !h
->forced_local
)
6988 sym
.st_size
= h
->size
;
6989 sym
.st_other
= h
->other
;
6990 if (h
->forced_local
)
6991 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6992 else if (h
->root
.type
== bfd_link_hash_undefweak
6993 || h
->root
.type
== bfd_link_hash_defweak
)
6994 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6996 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6998 switch (h
->root
.type
)
7001 case bfd_link_hash_new
:
7002 case bfd_link_hash_warning
:
7006 case bfd_link_hash_undefined
:
7007 case bfd_link_hash_undefweak
:
7008 input_sec
= bfd_und_section_ptr
;
7009 sym
.st_shndx
= SHN_UNDEF
;
7012 case bfd_link_hash_defined
:
7013 case bfd_link_hash_defweak
:
7015 input_sec
= h
->root
.u
.def
.section
;
7016 if (input_sec
->output_section
!= NULL
)
7019 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7020 input_sec
->output_section
);
7021 if (sym
.st_shndx
== SHN_BAD
)
7023 (*_bfd_error_handler
)
7024 (_("%B: could not find output section %A for input section %A"),
7025 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7026 eoinfo
->failed
= TRUE
;
7030 /* ELF symbols in relocatable files are section relative,
7031 but in nonrelocatable files they are virtual
7033 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7034 if (! finfo
->info
->relocatable
)
7036 sym
.st_value
+= input_sec
->output_section
->vma
;
7037 if (h
->type
== STT_TLS
)
7039 /* STT_TLS symbols are relative to PT_TLS segment
7041 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7042 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7048 BFD_ASSERT (input_sec
->owner
== NULL
7049 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7050 sym
.st_shndx
= SHN_UNDEF
;
7051 input_sec
= bfd_und_section_ptr
;
7056 case bfd_link_hash_common
:
7057 input_sec
= h
->root
.u
.c
.p
->section
;
7058 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7059 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7062 case bfd_link_hash_indirect
:
7063 /* These symbols are created by symbol versioning. They point
7064 to the decorated version of the name. For example, if the
7065 symbol foo@@GNU_1.2 is the default, which should be used when
7066 foo is used with no version, then we add an indirect symbol
7067 foo which points to foo@@GNU_1.2. We ignore these symbols,
7068 since the indirected symbol is already in the hash table. */
7072 /* Give the processor backend a chance to tweak the symbol value,
7073 and also to finish up anything that needs to be done for this
7074 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7075 forced local syms when non-shared is due to a historical quirk. */
7076 if ((h
->dynindx
!= -1
7078 && ((finfo
->info
->shared
7079 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7080 || h
->root
.type
!= bfd_link_hash_undefweak
))
7081 || !h
->forced_local
)
7082 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7084 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7085 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7087 eoinfo
->failed
= TRUE
;
7092 /* If we are marking the symbol as undefined, and there are no
7093 non-weak references to this symbol from a regular object, then
7094 mark the symbol as weak undefined; if there are non-weak
7095 references, mark the symbol as strong. We can't do this earlier,
7096 because it might not be marked as undefined until the
7097 finish_dynamic_symbol routine gets through with it. */
7098 if (sym
.st_shndx
== SHN_UNDEF
7100 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7101 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7105 if (h
->ref_regular_nonweak
)
7106 bindtype
= STB_GLOBAL
;
7108 bindtype
= STB_WEAK
;
7109 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7112 /* If a non-weak symbol with non-default visibility is not defined
7113 locally, it is a fatal error. */
7114 if (! finfo
->info
->relocatable
7115 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7116 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7117 && h
->root
.type
== bfd_link_hash_undefined
7120 (*_bfd_error_handler
)
7121 (_("%B: %s symbol `%s' isn't defined"),
7123 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7125 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7126 ? "internal" : "hidden",
7127 h
->root
.root
.string
);
7128 eoinfo
->failed
= TRUE
;
7132 /* If this symbol should be put in the .dynsym section, then put it
7133 there now. We already know the symbol index. We also fill in
7134 the entry in the .hash section. */
7135 if (h
->dynindx
!= -1
7136 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7140 sym
.st_name
= h
->dynstr_index
;
7141 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7142 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7144 eoinfo
->failed
= TRUE
;
7147 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7149 if (finfo
->hash_sec
!= NULL
)
7151 size_t hash_entry_size
;
7152 bfd_byte
*bucketpos
;
7157 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7158 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7161 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7162 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7163 + (bucket
+ 2) * hash_entry_size
);
7164 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7165 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7166 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7167 ((bfd_byte
*) finfo
->hash_sec
->contents
7168 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7171 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7173 Elf_Internal_Versym iversym
;
7174 Elf_External_Versym
*eversym
;
7176 if (!h
->def_regular
)
7178 if (h
->verinfo
.verdef
== NULL
)
7179 iversym
.vs_vers
= 0;
7181 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7185 if (h
->verinfo
.vertree
== NULL
)
7186 iversym
.vs_vers
= 1;
7188 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7189 if (finfo
->info
->create_default_symver
)
7194 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7196 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7197 eversym
+= h
->dynindx
;
7198 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7202 /* If we're stripping it, then it was just a dynamic symbol, and
7203 there's nothing else to do. */
7204 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7207 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7209 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7211 eoinfo
->failed
= TRUE
;
7218 /* Return TRUE if special handling is done for relocs in SEC against
7219 symbols defined in discarded sections. */
7222 elf_section_ignore_discarded_relocs (asection
*sec
)
7224 const struct elf_backend_data
*bed
;
7226 switch (sec
->sec_info_type
)
7228 case ELF_INFO_TYPE_STABS
:
7229 case ELF_INFO_TYPE_EH_FRAME
:
7235 bed
= get_elf_backend_data (sec
->owner
);
7236 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7237 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7243 /* Return a mask saying how ld should treat relocations in SEC against
7244 symbols defined in discarded sections. If this function returns
7245 COMPLAIN set, ld will issue a warning message. If this function
7246 returns PRETEND set, and the discarded section was link-once and the
7247 same size as the kept link-once section, ld will pretend that the
7248 symbol was actually defined in the kept section. Otherwise ld will
7249 zero the reloc (at least that is the intent, but some cooperation by
7250 the target dependent code is needed, particularly for REL targets). */
7253 _bfd_elf_default_action_discarded (asection
*sec
)
7255 if (sec
->flags
& SEC_DEBUGGING
)
7258 if (strcmp (".eh_frame", sec
->name
) == 0)
7261 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7264 return COMPLAIN
| PRETEND
;
7267 /* Find a match between a section and a member of a section group. */
7270 match_group_member (asection
*sec
, asection
*group
,
7271 struct bfd_link_info
*info
)
7273 asection
*first
= elf_next_in_group (group
);
7274 asection
*s
= first
;
7278 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
7281 s
= elf_next_in_group (s
);
7289 /* Check if the kept section of a discarded section SEC can be used
7290 to replace it. Return the replacement if it is OK. Otherwise return
7294 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
7298 kept
= sec
->kept_section
;
7301 if (elf_sec_group (sec
) != NULL
)
7302 kept
= match_group_member (sec
, kept
, info
);
7303 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
7309 /* Link an input file into the linker output file. This function
7310 handles all the sections and relocations of the input file at once.
7311 This is so that we only have to read the local symbols once, and
7312 don't have to keep them in memory. */
7315 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
7317 bfd_boolean (*relocate_section
)
7318 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
7319 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
7321 Elf_Internal_Shdr
*symtab_hdr
;
7324 Elf_Internal_Sym
*isymbuf
;
7325 Elf_Internal_Sym
*isym
;
7326 Elf_Internal_Sym
*isymend
;
7328 asection
**ppsection
;
7330 const struct elf_backend_data
*bed
;
7331 bfd_boolean emit_relocs
;
7332 struct elf_link_hash_entry
**sym_hashes
;
7334 output_bfd
= finfo
->output_bfd
;
7335 bed
= get_elf_backend_data (output_bfd
);
7336 relocate_section
= bed
->elf_backend_relocate_section
;
7338 /* If this is a dynamic object, we don't want to do anything here:
7339 we don't want the local symbols, and we don't want the section
7341 if ((input_bfd
->flags
& DYNAMIC
) != 0)
7344 emit_relocs
= (finfo
->info
->relocatable
7345 || finfo
->info
->emitrelocations
);
7347 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7348 if (elf_bad_symtab (input_bfd
))
7350 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7355 locsymcount
= symtab_hdr
->sh_info
;
7356 extsymoff
= symtab_hdr
->sh_info
;
7359 /* Read the local symbols. */
7360 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7361 if (isymbuf
== NULL
&& locsymcount
!= 0)
7363 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
7364 finfo
->internal_syms
,
7365 finfo
->external_syms
,
7366 finfo
->locsym_shndx
);
7367 if (isymbuf
== NULL
)
7371 /* Find local symbol sections and adjust values of symbols in
7372 SEC_MERGE sections. Write out those local symbols we know are
7373 going into the output file. */
7374 isymend
= isymbuf
+ locsymcount
;
7375 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
7377 isym
++, pindex
++, ppsection
++)
7381 Elf_Internal_Sym osym
;
7385 if (elf_bad_symtab (input_bfd
))
7387 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
7394 if (isym
->st_shndx
== SHN_UNDEF
)
7395 isec
= bfd_und_section_ptr
;
7396 else if (isym
->st_shndx
< SHN_LORESERVE
7397 || isym
->st_shndx
> SHN_HIRESERVE
)
7399 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
7401 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
7402 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
7404 _bfd_merged_section_offset (output_bfd
, &isec
,
7405 elf_section_data (isec
)->sec_info
,
7408 else if (isym
->st_shndx
== SHN_ABS
)
7409 isec
= bfd_abs_section_ptr
;
7410 else if (isym
->st_shndx
== SHN_COMMON
)
7411 isec
= bfd_com_section_ptr
;
7414 /* Don't attempt to output symbols with st_shnx in the
7415 reserved range other than SHN_ABS and SHN_COMMON. */
7422 /* Don't output the first, undefined, symbol. */
7423 if (ppsection
== finfo
->sections
)
7426 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7428 /* We never output section symbols. Instead, we use the
7429 section symbol of the corresponding section in the output
7434 /* If we are stripping all symbols, we don't want to output this
7436 if (finfo
->info
->strip
== strip_all
)
7439 /* If we are discarding all local symbols, we don't want to
7440 output this one. If we are generating a relocatable output
7441 file, then some of the local symbols may be required by
7442 relocs; we output them below as we discover that they are
7444 if (finfo
->info
->discard
== discard_all
)
7447 /* If this symbol is defined in a section which we are
7448 discarding, we don't need to keep it. */
7449 if (isym
->st_shndx
!= SHN_UNDEF
7450 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
7452 || bfd_section_removed_from_list (output_bfd
,
7453 isec
->output_section
)))
7456 /* Get the name of the symbol. */
7457 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
7462 /* See if we are discarding symbols with this name. */
7463 if ((finfo
->info
->strip
== strip_some
7464 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
7466 || (((finfo
->info
->discard
== discard_sec_merge
7467 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
7468 || finfo
->info
->discard
== discard_l
)
7469 && bfd_is_local_label_name (input_bfd
, name
)))
7472 /* If we get here, we are going to output this symbol. */
7476 /* Adjust the section index for the output file. */
7477 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
7478 isec
->output_section
);
7479 if (osym
.st_shndx
== SHN_BAD
)
7482 *pindex
= bfd_get_symcount (output_bfd
);
7484 /* ELF symbols in relocatable files are section relative, but
7485 in executable files they are virtual addresses. Note that
7486 this code assumes that all ELF sections have an associated
7487 BFD section with a reasonable value for output_offset; below
7488 we assume that they also have a reasonable value for
7489 output_section. Any special sections must be set up to meet
7490 these requirements. */
7491 osym
.st_value
+= isec
->output_offset
;
7492 if (! finfo
->info
->relocatable
)
7494 osym
.st_value
+= isec
->output_section
->vma
;
7495 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
7497 /* STT_TLS symbols are relative to PT_TLS segment base. */
7498 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7499 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7503 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
7507 /* Relocate the contents of each section. */
7508 sym_hashes
= elf_sym_hashes (input_bfd
);
7509 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7513 if (! o
->linker_mark
)
7515 /* This section was omitted from the link. */
7519 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7520 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
7523 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
7525 /* Section was created by _bfd_elf_link_create_dynamic_sections
7530 /* Get the contents of the section. They have been cached by a
7531 relaxation routine. Note that o is a section in an input
7532 file, so the contents field will not have been set by any of
7533 the routines which work on output files. */
7534 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
7535 contents
= elf_section_data (o
)->this_hdr
.contents
;
7538 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
7540 contents
= finfo
->contents
;
7541 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
7545 if ((o
->flags
& SEC_RELOC
) != 0)
7547 Elf_Internal_Rela
*internal_relocs
;
7548 bfd_vma r_type_mask
;
7551 /* Get the swapped relocs. */
7553 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7554 finfo
->internal_relocs
, FALSE
);
7555 if (internal_relocs
== NULL
7556 && o
->reloc_count
> 0)
7559 if (bed
->s
->arch_size
== 32)
7566 r_type_mask
= 0xffffffff;
7570 /* Run through the relocs looking for any against symbols
7571 from discarded sections and section symbols from
7572 removed link-once sections. Complain about relocs
7573 against discarded sections. Zero relocs against removed
7574 link-once sections. */
7575 if (!elf_section_ignore_discarded_relocs (o
))
7577 Elf_Internal_Rela
*rel
, *relend
;
7578 unsigned int action
= (*bed
->action_discarded
) (o
);
7580 rel
= internal_relocs
;
7581 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7582 for ( ; rel
< relend
; rel
++)
7584 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7585 asection
**ps
, *sec
;
7586 struct elf_link_hash_entry
*h
= NULL
;
7587 const char *sym_name
;
7589 if (r_symndx
== STN_UNDEF
)
7592 if (r_symndx
>= locsymcount
7593 || (elf_bad_symtab (input_bfd
)
7594 && finfo
->sections
[r_symndx
] == NULL
))
7596 h
= sym_hashes
[r_symndx
- extsymoff
];
7598 /* Badly formatted input files can contain relocs that
7599 reference non-existant symbols. Check here so that
7600 we do not seg fault. */
7605 sprintf_vma (buffer
, rel
->r_info
);
7606 (*_bfd_error_handler
)
7607 (_("error: %B contains a reloc (0x%s) for section %A "
7608 "that references a non-existent global symbol"),
7609 input_bfd
, o
, buffer
);
7610 bfd_set_error (bfd_error_bad_value
);
7614 while (h
->root
.type
== bfd_link_hash_indirect
7615 || h
->root
.type
== bfd_link_hash_warning
)
7616 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7618 if (h
->root
.type
!= bfd_link_hash_defined
7619 && h
->root
.type
!= bfd_link_hash_defweak
)
7622 ps
= &h
->root
.u
.def
.section
;
7623 sym_name
= h
->root
.root
.string
;
7627 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7628 ps
= &finfo
->sections
[r_symndx
];
7629 sym_name
= bfd_elf_sym_name (input_bfd
,
7634 /* Complain if the definition comes from a
7635 discarded section. */
7636 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7638 BFD_ASSERT (r_symndx
!= 0);
7639 if (action
& COMPLAIN
)
7640 (*finfo
->info
->callbacks
->einfo
)
7641 (_("%X`%s' referenced in section `%A' of %B: "
7642 "defined in discarded section `%A' of %B\n"),
7643 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
7645 /* Try to do the best we can to support buggy old
7646 versions of gcc. Pretend that the symbol is
7647 really defined in the kept linkonce section.
7648 FIXME: This is quite broken. Modifying the
7649 symbol here means we will be changing all later
7650 uses of the symbol, not just in this section. */
7651 if (action
& PRETEND
)
7655 kept
= _bfd_elf_check_kept_section (sec
,
7664 /* Remove the symbol reference from the reloc, but
7665 don't kill the reloc completely. This is so that
7666 a zero value will be written into the section,
7667 which may have non-zero contents put there by the
7668 assembler. Zero in things like an eh_frame fde
7669 pc_begin allows stack unwinders to recognize the
7671 rel
->r_info
&= r_type_mask
;
7677 /* Relocate the section by invoking a back end routine.
7679 The back end routine is responsible for adjusting the
7680 section contents as necessary, and (if using Rela relocs
7681 and generating a relocatable output file) adjusting the
7682 reloc addend as necessary.
7684 The back end routine does not have to worry about setting
7685 the reloc address or the reloc symbol index.
7687 The back end routine is given a pointer to the swapped in
7688 internal symbols, and can access the hash table entries
7689 for the external symbols via elf_sym_hashes (input_bfd).
7691 When generating relocatable output, the back end routine
7692 must handle STB_LOCAL/STT_SECTION symbols specially. The
7693 output symbol is going to be a section symbol
7694 corresponding to the output section, which will require
7695 the addend to be adjusted. */
7697 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7698 input_bfd
, o
, contents
,
7706 Elf_Internal_Rela
*irela
;
7707 Elf_Internal_Rela
*irelaend
;
7708 bfd_vma last_offset
;
7709 struct elf_link_hash_entry
**rel_hash
;
7710 struct elf_link_hash_entry
**rel_hash_list
;
7711 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7712 unsigned int next_erel
;
7713 bfd_boolean rela_normal
;
7715 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7716 rela_normal
= (bed
->rela_normal
7717 && (input_rel_hdr
->sh_entsize
7718 == bed
->s
->sizeof_rela
));
7720 /* Adjust the reloc addresses and symbol indices. */
7722 irela
= internal_relocs
;
7723 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7724 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7725 + elf_section_data (o
->output_section
)->rel_count
7726 + elf_section_data (o
->output_section
)->rel_count2
);
7727 rel_hash_list
= rel_hash
;
7728 last_offset
= o
->output_offset
;
7729 if (!finfo
->info
->relocatable
)
7730 last_offset
+= o
->output_section
->vma
;
7731 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7733 unsigned long r_symndx
;
7735 Elf_Internal_Sym sym
;
7737 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7743 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7746 if (irela
->r_offset
>= (bfd_vma
) -2)
7748 /* This is a reloc for a deleted entry or somesuch.
7749 Turn it into an R_*_NONE reloc, at the same
7750 offset as the last reloc. elf_eh_frame.c and
7751 bfd_elf_discard_info rely on reloc offsets
7753 irela
->r_offset
= last_offset
;
7755 irela
->r_addend
= 0;
7759 irela
->r_offset
+= o
->output_offset
;
7761 /* Relocs in an executable have to be virtual addresses. */
7762 if (!finfo
->info
->relocatable
)
7763 irela
->r_offset
+= o
->output_section
->vma
;
7765 last_offset
= irela
->r_offset
;
7767 r_symndx
= irela
->r_info
>> r_sym_shift
;
7768 if (r_symndx
== STN_UNDEF
)
7771 if (r_symndx
>= locsymcount
7772 || (elf_bad_symtab (input_bfd
)
7773 && finfo
->sections
[r_symndx
] == NULL
))
7775 struct elf_link_hash_entry
*rh
;
7778 /* This is a reloc against a global symbol. We
7779 have not yet output all the local symbols, so
7780 we do not know the symbol index of any global
7781 symbol. We set the rel_hash entry for this
7782 reloc to point to the global hash table entry
7783 for this symbol. The symbol index is then
7784 set at the end of bfd_elf_final_link. */
7785 indx
= r_symndx
- extsymoff
;
7786 rh
= elf_sym_hashes (input_bfd
)[indx
];
7787 while (rh
->root
.type
== bfd_link_hash_indirect
7788 || rh
->root
.type
== bfd_link_hash_warning
)
7789 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7791 /* Setting the index to -2 tells
7792 elf_link_output_extsym that this symbol is
7794 BFD_ASSERT (rh
->indx
< 0);
7802 /* This is a reloc against a local symbol. */
7805 sym
= isymbuf
[r_symndx
];
7806 sec
= finfo
->sections
[r_symndx
];
7807 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7809 /* I suppose the backend ought to fill in the
7810 section of any STT_SECTION symbol against a
7811 processor specific section. */
7813 if (bfd_is_abs_section (sec
))
7815 else if (sec
== NULL
|| sec
->owner
== NULL
)
7817 bfd_set_error (bfd_error_bad_value
);
7822 asection
*osec
= sec
->output_section
;
7824 /* If we have discarded a section, the output
7825 section will be the absolute section. In
7826 case of discarded link-once and discarded
7827 SEC_MERGE sections, use the kept section. */
7828 if (bfd_is_abs_section (osec
)
7829 && sec
->kept_section
!= NULL
7830 && sec
->kept_section
->output_section
!= NULL
)
7832 osec
= sec
->kept_section
->output_section
;
7833 irela
->r_addend
-= osec
->vma
;
7836 if (!bfd_is_abs_section (osec
))
7838 r_symndx
= osec
->target_index
;
7841 struct elf_link_hash_table
*htab
;
7844 htab
= elf_hash_table (finfo
->info
);
7845 oi
= htab
->text_index_section
;
7846 if ((osec
->flags
& SEC_READONLY
) == 0
7847 && htab
->data_index_section
!= NULL
)
7848 oi
= htab
->data_index_section
;
7852 irela
->r_addend
+= osec
->vma
- oi
->vma
;
7853 r_symndx
= oi
->target_index
;
7857 BFD_ASSERT (r_symndx
!= 0);
7861 /* Adjust the addend according to where the
7862 section winds up in the output section. */
7864 irela
->r_addend
+= sec
->output_offset
;
7868 if (finfo
->indices
[r_symndx
] == -1)
7870 unsigned long shlink
;
7874 if (finfo
->info
->strip
== strip_all
)
7876 /* You can't do ld -r -s. */
7877 bfd_set_error (bfd_error_invalid_operation
);
7881 /* This symbol was skipped earlier, but
7882 since it is needed by a reloc, we
7883 must output it now. */
7884 shlink
= symtab_hdr
->sh_link
;
7885 name
= (bfd_elf_string_from_elf_section
7886 (input_bfd
, shlink
, sym
.st_name
));
7890 osec
= sec
->output_section
;
7892 _bfd_elf_section_from_bfd_section (output_bfd
,
7894 if (sym
.st_shndx
== SHN_BAD
)
7897 sym
.st_value
+= sec
->output_offset
;
7898 if (! finfo
->info
->relocatable
)
7900 sym
.st_value
+= osec
->vma
;
7901 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7903 /* STT_TLS symbols are relative to PT_TLS
7905 BFD_ASSERT (elf_hash_table (finfo
->info
)
7907 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7912 finfo
->indices
[r_symndx
]
7913 = bfd_get_symcount (output_bfd
);
7915 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7920 r_symndx
= finfo
->indices
[r_symndx
];
7923 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7924 | (irela
->r_info
& r_type_mask
));
7927 /* Swap out the relocs. */
7928 if (input_rel_hdr
->sh_size
!= 0
7929 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
7935 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7936 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7938 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7939 * bed
->s
->int_rels_per_ext_rel
);
7940 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
7941 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
7950 /* Write out the modified section contents. */
7951 if (bed
->elf_backend_write_section
7952 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7954 /* Section written out. */
7956 else switch (o
->sec_info_type
)
7958 case ELF_INFO_TYPE_STABS
:
7959 if (! (_bfd_write_section_stabs
7961 &elf_hash_table (finfo
->info
)->stab_info
,
7962 o
, &elf_section_data (o
)->sec_info
, contents
)))
7965 case ELF_INFO_TYPE_MERGE
:
7966 if (! _bfd_write_merged_section (output_bfd
, o
,
7967 elf_section_data (o
)->sec_info
))
7970 case ELF_INFO_TYPE_EH_FRAME
:
7972 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7979 if (! (o
->flags
& SEC_EXCLUDE
)
7980 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7982 (file_ptr
) o
->output_offset
,
7993 /* Generate a reloc when linking an ELF file. This is a reloc
7994 requested by the linker, and does not come from any input file. This
7995 is used to build constructor and destructor tables when linking
7999 elf_reloc_link_order (bfd
*output_bfd
,
8000 struct bfd_link_info
*info
,
8001 asection
*output_section
,
8002 struct bfd_link_order
*link_order
)
8004 reloc_howto_type
*howto
;
8008 struct elf_link_hash_entry
**rel_hash_ptr
;
8009 Elf_Internal_Shdr
*rel_hdr
;
8010 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8011 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8015 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8018 bfd_set_error (bfd_error_bad_value
);
8022 addend
= link_order
->u
.reloc
.p
->addend
;
8024 /* Figure out the symbol index. */
8025 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8026 + elf_section_data (output_section
)->rel_count
8027 + elf_section_data (output_section
)->rel_count2
);
8028 if (link_order
->type
== bfd_section_reloc_link_order
)
8030 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8031 BFD_ASSERT (indx
!= 0);
8032 *rel_hash_ptr
= NULL
;
8036 struct elf_link_hash_entry
*h
;
8038 /* Treat a reloc against a defined symbol as though it were
8039 actually against the section. */
8040 h
= ((struct elf_link_hash_entry
*)
8041 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8042 link_order
->u
.reloc
.p
->u
.name
,
8043 FALSE
, FALSE
, TRUE
));
8045 && (h
->root
.type
== bfd_link_hash_defined
8046 || h
->root
.type
== bfd_link_hash_defweak
))
8050 section
= h
->root
.u
.def
.section
;
8051 indx
= section
->output_section
->target_index
;
8052 *rel_hash_ptr
= NULL
;
8053 /* It seems that we ought to add the symbol value to the
8054 addend here, but in practice it has already been added
8055 because it was passed to constructor_callback. */
8056 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8060 /* Setting the index to -2 tells elf_link_output_extsym that
8061 this symbol is used by a reloc. */
8068 if (! ((*info
->callbacks
->unattached_reloc
)
8069 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
8075 /* If this is an inplace reloc, we must write the addend into the
8077 if (howto
->partial_inplace
&& addend
!= 0)
8080 bfd_reloc_status_type rstat
;
8083 const char *sym_name
;
8085 size
= bfd_get_reloc_size (howto
);
8086 buf
= bfd_zmalloc (size
);
8089 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8096 case bfd_reloc_outofrange
:
8099 case bfd_reloc_overflow
:
8100 if (link_order
->type
== bfd_section_reloc_link_order
)
8101 sym_name
= bfd_section_name (output_bfd
,
8102 link_order
->u
.reloc
.p
->u
.section
);
8104 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8105 if (! ((*info
->callbacks
->reloc_overflow
)
8106 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8107 NULL
, (bfd_vma
) 0)))
8114 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
8115 link_order
->offset
, size
);
8121 /* The address of a reloc is relative to the section in a
8122 relocatable file, and is a virtual address in an executable
8124 offset
= link_order
->offset
;
8125 if (! info
->relocatable
)
8126 offset
+= output_section
->vma
;
8128 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
8130 irel
[i
].r_offset
= offset
;
8132 irel
[i
].r_addend
= 0;
8134 if (bed
->s
->arch_size
== 32)
8135 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8137 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8139 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8140 erel
= rel_hdr
->contents
;
8141 if (rel_hdr
->sh_type
== SHT_REL
)
8143 erel
+= (elf_section_data (output_section
)->rel_count
8144 * bed
->s
->sizeof_rel
);
8145 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8149 irel
[0].r_addend
= addend
;
8150 erel
+= (elf_section_data (output_section
)->rel_count
8151 * bed
->s
->sizeof_rela
);
8152 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8155 ++elf_section_data (output_section
)->rel_count
;
8161 /* Get the output vma of the section pointed to by the sh_link field. */
8164 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8166 Elf_Internal_Shdr
**elf_shdrp
;
8170 s
= p
->u
.indirect
.section
;
8171 elf_shdrp
= elf_elfsections (s
->owner
);
8172 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8173 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8175 The Intel C compiler generates SHT_IA_64_UNWIND with
8176 SHF_LINK_ORDER. But it doesn't set the sh_link or
8177 sh_info fields. Hence we could get the situation
8178 where elfsec is 0. */
8181 const struct elf_backend_data
*bed
8182 = get_elf_backend_data (s
->owner
);
8183 if (bed
->link_order_error_handler
)
8184 bed
->link_order_error_handler
8185 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8190 s
= elf_shdrp
[elfsec
]->bfd_section
;
8191 return s
->output_section
->vma
+ s
->output_offset
;
8196 /* Compare two sections based on the locations of the sections they are
8197 linked to. Used by elf_fixup_link_order. */
8200 compare_link_order (const void * a
, const void * b
)
8205 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8206 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8213 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8214 order as their linked sections. Returns false if this could not be done
8215 because an output section includes both ordered and unordered
8216 sections. Ideally we'd do this in the linker proper. */
8219 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8224 struct bfd_link_order
*p
;
8226 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8228 struct bfd_link_order
**sections
;
8229 asection
*s
, *other_sec
, *linkorder_sec
;
8233 linkorder_sec
= NULL
;
8236 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8238 if (p
->type
== bfd_indirect_link_order
)
8240 s
= p
->u
.indirect
.section
;
8242 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8243 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8244 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8245 && elfsec
< elf_numsections (sub
)
8246 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8260 if (seen_other
&& seen_linkorder
)
8262 if (other_sec
&& linkorder_sec
)
8263 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8265 linkorder_sec
->owner
, other_sec
,
8268 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
8270 bfd_set_error (bfd_error_bad_value
);
8275 if (!seen_linkorder
)
8278 sections
= (struct bfd_link_order
**)
8279 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
8282 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8284 sections
[seen_linkorder
++] = p
;
8286 /* Sort the input sections in the order of their linked section. */
8287 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
8288 compare_link_order
);
8290 /* Change the offsets of the sections. */
8292 for (n
= 0; n
< seen_linkorder
; n
++)
8294 s
= sections
[n
]->u
.indirect
.section
;
8295 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
8296 s
->output_offset
= offset
;
8297 sections
[n
]->offset
= offset
;
8298 offset
+= sections
[n
]->size
;
8305 /* Do the final step of an ELF link. */
8308 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
8310 bfd_boolean dynamic
;
8311 bfd_boolean emit_relocs
;
8313 struct elf_final_link_info finfo
;
8314 register asection
*o
;
8315 register struct bfd_link_order
*p
;
8317 bfd_size_type max_contents_size
;
8318 bfd_size_type max_external_reloc_size
;
8319 bfd_size_type max_internal_reloc_count
;
8320 bfd_size_type max_sym_count
;
8321 bfd_size_type max_sym_shndx_count
;
8323 Elf_Internal_Sym elfsym
;
8325 Elf_Internal_Shdr
*symtab_hdr
;
8326 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8327 Elf_Internal_Shdr
*symstrtab_hdr
;
8328 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8329 struct elf_outext_info eoinfo
;
8331 size_t relativecount
= 0;
8332 asection
*reldyn
= 0;
8335 if (! is_elf_hash_table (info
->hash
))
8339 abfd
->flags
|= DYNAMIC
;
8341 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
8342 dynobj
= elf_hash_table (info
)->dynobj
;
8344 emit_relocs
= (info
->relocatable
8345 || info
->emitrelocations
);
8348 finfo
.output_bfd
= abfd
;
8349 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
8350 if (finfo
.symstrtab
== NULL
)
8355 finfo
.dynsym_sec
= NULL
;
8356 finfo
.hash_sec
= NULL
;
8357 finfo
.symver_sec
= NULL
;
8361 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
8362 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
8363 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
8364 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
8365 /* Note that it is OK if symver_sec is NULL. */
8368 finfo
.contents
= NULL
;
8369 finfo
.external_relocs
= NULL
;
8370 finfo
.internal_relocs
= NULL
;
8371 finfo
.external_syms
= NULL
;
8372 finfo
.locsym_shndx
= NULL
;
8373 finfo
.internal_syms
= NULL
;
8374 finfo
.indices
= NULL
;
8375 finfo
.sections
= NULL
;
8376 finfo
.symbuf
= NULL
;
8377 finfo
.symshndxbuf
= NULL
;
8378 finfo
.symbuf_count
= 0;
8379 finfo
.shndxbuf_size
= 0;
8381 /* Count up the number of relocations we will output for each output
8382 section, so that we know the sizes of the reloc sections. We
8383 also figure out some maximum sizes. */
8384 max_contents_size
= 0;
8385 max_external_reloc_size
= 0;
8386 max_internal_reloc_count
= 0;
8388 max_sym_shndx_count
= 0;
8390 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8392 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
8395 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8397 unsigned int reloc_count
= 0;
8398 struct bfd_elf_section_data
*esdi
= NULL
;
8399 unsigned int *rel_count1
;
8401 if (p
->type
== bfd_section_reloc_link_order
8402 || p
->type
== bfd_symbol_reloc_link_order
)
8404 else if (p
->type
== bfd_indirect_link_order
)
8408 sec
= p
->u
.indirect
.section
;
8409 esdi
= elf_section_data (sec
);
8411 /* Mark all sections which are to be included in the
8412 link. This will normally be every section. We need
8413 to do this so that we can identify any sections which
8414 the linker has decided to not include. */
8415 sec
->linker_mark
= TRUE
;
8417 if (sec
->flags
& SEC_MERGE
)
8420 if (info
->relocatable
|| info
->emitrelocations
)
8421 reloc_count
= sec
->reloc_count
;
8422 else if (bed
->elf_backend_count_relocs
)
8424 Elf_Internal_Rela
* relocs
;
8426 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
8429 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
8431 if (elf_section_data (o
)->relocs
!= relocs
)
8435 if (sec
->rawsize
> max_contents_size
)
8436 max_contents_size
= sec
->rawsize
;
8437 if (sec
->size
> max_contents_size
)
8438 max_contents_size
= sec
->size
;
8440 /* We are interested in just local symbols, not all
8442 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
8443 && (sec
->owner
->flags
& DYNAMIC
) == 0)
8447 if (elf_bad_symtab (sec
->owner
))
8448 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
8449 / bed
->s
->sizeof_sym
);
8451 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
8453 if (sym_count
> max_sym_count
)
8454 max_sym_count
= sym_count
;
8456 if (sym_count
> max_sym_shndx_count
8457 && elf_symtab_shndx (sec
->owner
) != 0)
8458 max_sym_shndx_count
= sym_count
;
8460 if ((sec
->flags
& SEC_RELOC
) != 0)
8464 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
8465 if (ext_size
> max_external_reloc_size
)
8466 max_external_reloc_size
= ext_size
;
8467 if (sec
->reloc_count
> max_internal_reloc_count
)
8468 max_internal_reloc_count
= sec
->reloc_count
;
8473 if (reloc_count
== 0)
8476 o
->reloc_count
+= reloc_count
;
8478 /* MIPS may have a mix of REL and RELA relocs on sections.
8479 To support this curious ABI we keep reloc counts in
8480 elf_section_data too. We must be careful to add the
8481 relocations from the input section to the right output
8482 count. FIXME: Get rid of one count. We have
8483 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
8484 rel_count1
= &esdo
->rel_count
;
8487 bfd_boolean same_size
;
8488 bfd_size_type entsize1
;
8490 entsize1
= esdi
->rel_hdr
.sh_entsize
;
8491 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
8492 || entsize1
== bed
->s
->sizeof_rela
);
8493 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
8496 rel_count1
= &esdo
->rel_count2
;
8498 if (esdi
->rel_hdr2
!= NULL
)
8500 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
8501 unsigned int alt_count
;
8502 unsigned int *rel_count2
;
8504 BFD_ASSERT (entsize2
!= entsize1
8505 && (entsize2
== bed
->s
->sizeof_rel
8506 || entsize2
== bed
->s
->sizeof_rela
));
8508 rel_count2
= &esdo
->rel_count2
;
8510 rel_count2
= &esdo
->rel_count
;
8512 /* The following is probably too simplistic if the
8513 backend counts output relocs unusually. */
8514 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
8515 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
8516 *rel_count2
+= alt_count
;
8517 reloc_count
-= alt_count
;
8520 *rel_count1
+= reloc_count
;
8523 if (o
->reloc_count
> 0)
8524 o
->flags
|= SEC_RELOC
;
8527 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8528 set it (this is probably a bug) and if it is set
8529 assign_section_numbers will create a reloc section. */
8530 o
->flags
&=~ SEC_RELOC
;
8533 /* If the SEC_ALLOC flag is not set, force the section VMA to
8534 zero. This is done in elf_fake_sections as well, but forcing
8535 the VMA to 0 here will ensure that relocs against these
8536 sections are handled correctly. */
8537 if ((o
->flags
& SEC_ALLOC
) == 0
8538 && ! o
->user_set_vma
)
8542 if (! info
->relocatable
&& merged
)
8543 elf_link_hash_traverse (elf_hash_table (info
),
8544 _bfd_elf_link_sec_merge_syms
, abfd
);
8546 /* Figure out the file positions for everything but the symbol table
8547 and the relocs. We set symcount to force assign_section_numbers
8548 to create a symbol table. */
8549 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
8550 BFD_ASSERT (! abfd
->output_has_begun
);
8551 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
8554 /* Set sizes, and assign file positions for reloc sections. */
8555 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8557 if ((o
->flags
& SEC_RELOC
) != 0)
8559 if (!(_bfd_elf_link_size_reloc_section
8560 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
8563 if (elf_section_data (o
)->rel_hdr2
8564 && !(_bfd_elf_link_size_reloc_section
8565 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
8569 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8570 to count upwards while actually outputting the relocations. */
8571 elf_section_data (o
)->rel_count
= 0;
8572 elf_section_data (o
)->rel_count2
= 0;
8575 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8577 /* We have now assigned file positions for all the sections except
8578 .symtab and .strtab. We start the .symtab section at the current
8579 file position, and write directly to it. We build the .strtab
8580 section in memory. */
8581 bfd_get_symcount (abfd
) = 0;
8582 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8583 /* sh_name is set in prep_headers. */
8584 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8585 /* sh_flags, sh_addr and sh_size all start off zero. */
8586 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8587 /* sh_link is set in assign_section_numbers. */
8588 /* sh_info is set below. */
8589 /* sh_offset is set just below. */
8590 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8592 off
= elf_tdata (abfd
)->next_file_pos
;
8593 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8595 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8596 incorrect. We do not yet know the size of the .symtab section.
8597 We correct next_file_pos below, after we do know the size. */
8599 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8600 continuously seeking to the right position in the file. */
8601 if (! info
->keep_memory
|| max_sym_count
< 20)
8602 finfo
.symbuf_size
= 20;
8604 finfo
.symbuf_size
= max_sym_count
;
8605 amt
= finfo
.symbuf_size
;
8606 amt
*= bed
->s
->sizeof_sym
;
8607 finfo
.symbuf
= bfd_malloc (amt
);
8608 if (finfo
.symbuf
== NULL
)
8610 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8612 /* Wild guess at number of output symbols. realloc'd as needed. */
8613 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8614 finfo
.shndxbuf_size
= amt
;
8615 amt
*= sizeof (Elf_External_Sym_Shndx
);
8616 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8617 if (finfo
.symshndxbuf
== NULL
)
8621 /* Start writing out the symbol table. The first symbol is always a
8623 if (info
->strip
!= strip_all
8626 elfsym
.st_value
= 0;
8629 elfsym
.st_other
= 0;
8630 elfsym
.st_shndx
= SHN_UNDEF
;
8631 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8636 /* Output a symbol for each section. We output these even if we are
8637 discarding local symbols, since they are used for relocs. These
8638 symbols have no names. We store the index of each one in the
8639 index field of the section, so that we can find it again when
8640 outputting relocs. */
8641 if (info
->strip
!= strip_all
8645 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8646 elfsym
.st_other
= 0;
8647 elfsym
.st_value
= 0;
8648 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8650 o
= bfd_section_from_elf_index (abfd
, i
);
8653 o
->target_index
= bfd_get_symcount (abfd
);
8654 elfsym
.st_shndx
= i
;
8655 if (!info
->relocatable
)
8656 elfsym
.st_value
= o
->vma
;
8657 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8660 if (i
== SHN_LORESERVE
- 1)
8661 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8665 /* Allocate some memory to hold information read in from the input
8667 if (max_contents_size
!= 0)
8669 finfo
.contents
= bfd_malloc (max_contents_size
);
8670 if (finfo
.contents
== NULL
)
8674 if (max_external_reloc_size
!= 0)
8676 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8677 if (finfo
.external_relocs
== NULL
)
8681 if (max_internal_reloc_count
!= 0)
8683 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8684 amt
*= sizeof (Elf_Internal_Rela
);
8685 finfo
.internal_relocs
= bfd_malloc (amt
);
8686 if (finfo
.internal_relocs
== NULL
)
8690 if (max_sym_count
!= 0)
8692 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8693 finfo
.external_syms
= bfd_malloc (amt
);
8694 if (finfo
.external_syms
== NULL
)
8697 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8698 finfo
.internal_syms
= bfd_malloc (amt
);
8699 if (finfo
.internal_syms
== NULL
)
8702 amt
= max_sym_count
* sizeof (long);
8703 finfo
.indices
= bfd_malloc (amt
);
8704 if (finfo
.indices
== NULL
)
8707 amt
= max_sym_count
* sizeof (asection
*);
8708 finfo
.sections
= bfd_malloc (amt
);
8709 if (finfo
.sections
== NULL
)
8713 if (max_sym_shndx_count
!= 0)
8715 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8716 finfo
.locsym_shndx
= bfd_malloc (amt
);
8717 if (finfo
.locsym_shndx
== NULL
)
8721 if (elf_hash_table (info
)->tls_sec
)
8723 bfd_vma base
, end
= 0;
8726 for (sec
= elf_hash_table (info
)->tls_sec
;
8727 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8730 bfd_size_type size
= sec
->size
;
8733 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8735 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
8737 size
= o
->offset
+ o
->size
;
8739 end
= sec
->vma
+ size
;
8741 base
= elf_hash_table (info
)->tls_sec
->vma
;
8742 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8743 elf_hash_table (info
)->tls_size
= end
- base
;
8746 /* Reorder SHF_LINK_ORDER sections. */
8747 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8749 if (!elf_fixup_link_order (abfd
, o
))
8753 /* Since ELF permits relocations to be against local symbols, we
8754 must have the local symbols available when we do the relocations.
8755 Since we would rather only read the local symbols once, and we
8756 would rather not keep them in memory, we handle all the
8757 relocations for a single input file at the same time.
8759 Unfortunately, there is no way to know the total number of local
8760 symbols until we have seen all of them, and the local symbol
8761 indices precede the global symbol indices. This means that when
8762 we are generating relocatable output, and we see a reloc against
8763 a global symbol, we can not know the symbol index until we have
8764 finished examining all the local symbols to see which ones we are
8765 going to output. To deal with this, we keep the relocations in
8766 memory, and don't output them until the end of the link. This is
8767 an unfortunate waste of memory, but I don't see a good way around
8768 it. Fortunately, it only happens when performing a relocatable
8769 link, which is not the common case. FIXME: If keep_memory is set
8770 we could write the relocs out and then read them again; I don't
8771 know how bad the memory loss will be. */
8773 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8774 sub
->output_has_begun
= FALSE
;
8775 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8777 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8779 if (p
->type
== bfd_indirect_link_order
8780 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8781 == bfd_target_elf_flavour
)
8782 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8784 if (! sub
->output_has_begun
)
8786 if (! elf_link_input_bfd (&finfo
, sub
))
8788 sub
->output_has_begun
= TRUE
;
8791 else if (p
->type
== bfd_section_reloc_link_order
8792 || p
->type
== bfd_symbol_reloc_link_order
)
8794 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8799 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8805 /* Free symbol buffer if needed. */
8806 if (!info
->reduce_memory_overheads
)
8808 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8809 if (elf_tdata (sub
)->symbuf
)
8811 free (elf_tdata (sub
)->symbuf
);
8812 elf_tdata (sub
)->symbuf
= NULL
;
8816 /* Output any global symbols that got converted to local in a
8817 version script or due to symbol visibility. We do this in a
8818 separate step since ELF requires all local symbols to appear
8819 prior to any global symbols. FIXME: We should only do this if
8820 some global symbols were, in fact, converted to become local.
8821 FIXME: Will this work correctly with the Irix 5 linker? */
8822 eoinfo
.failed
= FALSE
;
8823 eoinfo
.finfo
= &finfo
;
8824 eoinfo
.localsyms
= TRUE
;
8825 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8830 /* If backend needs to output some local symbols not present in the hash
8831 table, do it now. */
8832 if (bed
->elf_backend_output_arch_local_syms
)
8834 typedef bfd_boolean (*out_sym_func
)
8835 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8836 struct elf_link_hash_entry
*);
8838 if (! ((*bed
->elf_backend_output_arch_local_syms
)
8839 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8843 /* That wrote out all the local symbols. Finish up the symbol table
8844 with the global symbols. Even if we want to strip everything we
8845 can, we still need to deal with those global symbols that got
8846 converted to local in a version script. */
8848 /* The sh_info field records the index of the first non local symbol. */
8849 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8852 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8854 Elf_Internal_Sym sym
;
8855 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8856 long last_local
= 0;
8858 /* Write out the section symbols for the output sections. */
8859 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8865 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8868 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8874 dynindx
= elf_section_data (s
)->dynindx
;
8877 indx
= elf_section_data (s
)->this_idx
;
8878 BFD_ASSERT (indx
> 0);
8879 sym
.st_shndx
= indx
;
8880 if (! check_dynsym (abfd
, &sym
))
8882 sym
.st_value
= s
->vma
;
8883 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8884 if (last_local
< dynindx
)
8885 last_local
= dynindx
;
8886 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8890 /* Write out the local dynsyms. */
8891 if (elf_hash_table (info
)->dynlocal
)
8893 struct elf_link_local_dynamic_entry
*e
;
8894 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8899 sym
.st_size
= e
->isym
.st_size
;
8900 sym
.st_other
= e
->isym
.st_other
;
8902 /* Copy the internal symbol as is.
8903 Note that we saved a word of storage and overwrote
8904 the original st_name with the dynstr_index. */
8907 if (e
->isym
.st_shndx
!= SHN_UNDEF
8908 && (e
->isym
.st_shndx
< SHN_LORESERVE
8909 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8911 s
= bfd_section_from_elf_index (e
->input_bfd
,
8915 elf_section_data (s
->output_section
)->this_idx
;
8916 if (! check_dynsym (abfd
, &sym
))
8918 sym
.st_value
= (s
->output_section
->vma
8920 + e
->isym
.st_value
);
8923 if (last_local
< e
->dynindx
)
8924 last_local
= e
->dynindx
;
8926 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8927 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8931 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8935 /* We get the global symbols from the hash table. */
8936 eoinfo
.failed
= FALSE
;
8937 eoinfo
.localsyms
= FALSE
;
8938 eoinfo
.finfo
= &finfo
;
8939 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8944 /* If backend needs to output some symbols not present in the hash
8945 table, do it now. */
8946 if (bed
->elf_backend_output_arch_syms
)
8948 typedef bfd_boolean (*out_sym_func
)
8949 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8950 struct elf_link_hash_entry
*);
8952 if (! ((*bed
->elf_backend_output_arch_syms
)
8953 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8957 /* Flush all symbols to the file. */
8958 if (! elf_link_flush_output_syms (&finfo
, bed
))
8961 /* Now we know the size of the symtab section. */
8962 off
+= symtab_hdr
->sh_size
;
8964 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8965 if (symtab_shndx_hdr
->sh_name
!= 0)
8967 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8968 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8969 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8970 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8971 symtab_shndx_hdr
->sh_size
= amt
;
8973 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8976 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8977 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8982 /* Finish up and write out the symbol string table (.strtab)
8984 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8985 /* sh_name was set in prep_headers. */
8986 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8987 symstrtab_hdr
->sh_flags
= 0;
8988 symstrtab_hdr
->sh_addr
= 0;
8989 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8990 symstrtab_hdr
->sh_entsize
= 0;
8991 symstrtab_hdr
->sh_link
= 0;
8992 symstrtab_hdr
->sh_info
= 0;
8993 /* sh_offset is set just below. */
8994 symstrtab_hdr
->sh_addralign
= 1;
8996 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8997 elf_tdata (abfd
)->next_file_pos
= off
;
8999 if (bfd_get_symcount (abfd
) > 0)
9001 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9002 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9006 /* Adjust the relocs to have the correct symbol indices. */
9007 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9009 if ((o
->flags
& SEC_RELOC
) == 0)
9012 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9013 elf_section_data (o
)->rel_count
,
9014 elf_section_data (o
)->rel_hashes
);
9015 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9016 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9017 elf_section_data (o
)->rel_count2
,
9018 (elf_section_data (o
)->rel_hashes
9019 + elf_section_data (o
)->rel_count
));
9021 /* Set the reloc_count field to 0 to prevent write_relocs from
9022 trying to swap the relocs out itself. */
9026 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
9027 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
9029 /* If we are linking against a dynamic object, or generating a
9030 shared library, finish up the dynamic linking information. */
9033 bfd_byte
*dyncon
, *dynconend
;
9035 /* Fix up .dynamic entries. */
9036 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9037 BFD_ASSERT (o
!= NULL
);
9039 dyncon
= o
->contents
;
9040 dynconend
= o
->contents
+ o
->size
;
9041 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9043 Elf_Internal_Dyn dyn
;
9047 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9054 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
9056 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
9058 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
9059 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
9062 dyn
.d_un
.d_val
= relativecount
;
9069 name
= info
->init_function
;
9072 name
= info
->fini_function
;
9075 struct elf_link_hash_entry
*h
;
9077 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
9078 FALSE
, FALSE
, TRUE
);
9080 && (h
->root
.type
== bfd_link_hash_defined
9081 || h
->root
.type
== bfd_link_hash_defweak
))
9083 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
9084 o
= h
->root
.u
.def
.section
;
9085 if (o
->output_section
!= NULL
)
9086 dyn
.d_un
.d_val
+= (o
->output_section
->vma
9087 + o
->output_offset
);
9090 /* The symbol is imported from another shared
9091 library and does not apply to this one. */
9099 case DT_PREINIT_ARRAYSZ
:
9100 name
= ".preinit_array";
9102 case DT_INIT_ARRAYSZ
:
9103 name
= ".init_array";
9105 case DT_FINI_ARRAYSZ
:
9106 name
= ".fini_array";
9108 o
= bfd_get_section_by_name (abfd
, name
);
9111 (*_bfd_error_handler
)
9112 (_("%B: could not find output section %s"), abfd
, name
);
9116 (*_bfd_error_handler
)
9117 (_("warning: %s section has zero size"), name
);
9118 dyn
.d_un
.d_val
= o
->size
;
9121 case DT_PREINIT_ARRAY
:
9122 name
= ".preinit_array";
9125 name
= ".init_array";
9128 name
= ".fini_array";
9144 name
= ".gnu.version_d";
9147 name
= ".gnu.version_r";
9150 name
= ".gnu.version";
9152 o
= bfd_get_section_by_name (abfd
, name
);
9155 (*_bfd_error_handler
)
9156 (_("%B: could not find output section %s"), abfd
, name
);
9159 dyn
.d_un
.d_ptr
= o
->vma
;
9166 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9171 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9173 Elf_Internal_Shdr
*hdr
;
9175 hdr
= elf_elfsections (abfd
)[i
];
9176 if (hdr
->sh_type
== type
9177 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9179 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9180 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9183 if (dyn
.d_un
.d_val
== 0
9184 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9185 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9191 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9195 /* If we have created any dynamic sections, then output them. */
9198 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9201 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9202 if (info
->warn_shared_textrel
&& info
->shared
)
9204 bfd_byte
*dyncon
, *dynconend
;
9206 /* Fix up .dynamic entries. */
9207 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9208 BFD_ASSERT (o
!= NULL
);
9210 dyncon
= o
->contents
;
9211 dynconend
= o
->contents
+ o
->size
;
9212 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9214 Elf_Internal_Dyn dyn
;
9216 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9218 if (dyn
.d_tag
== DT_TEXTREL
)
9221 (_("warning: creating a DT_TEXTREL in a shared object."));
9227 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9229 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9231 || o
->output_section
== bfd_abs_section_ptr
)
9233 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9235 /* At this point, we are only interested in sections
9236 created by _bfd_elf_link_create_dynamic_sections. */
9239 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9241 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9243 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9245 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9247 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9249 (file_ptr
) o
->output_offset
,
9255 /* The contents of the .dynstr section are actually in a
9257 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9258 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9259 || ! _bfd_elf_strtab_emit (abfd
,
9260 elf_hash_table (info
)->dynstr
))
9266 if (info
->relocatable
)
9268 bfd_boolean failed
= FALSE
;
9270 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
9275 /* If we have optimized stabs strings, output them. */
9276 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
9278 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
9282 if (info
->eh_frame_hdr
)
9284 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
9288 if (finfo
.symstrtab
!= NULL
)
9289 _bfd_stringtab_free (finfo
.symstrtab
);
9290 if (finfo
.contents
!= NULL
)
9291 free (finfo
.contents
);
9292 if (finfo
.external_relocs
!= NULL
)
9293 free (finfo
.external_relocs
);
9294 if (finfo
.internal_relocs
!= NULL
)
9295 free (finfo
.internal_relocs
);
9296 if (finfo
.external_syms
!= NULL
)
9297 free (finfo
.external_syms
);
9298 if (finfo
.locsym_shndx
!= NULL
)
9299 free (finfo
.locsym_shndx
);
9300 if (finfo
.internal_syms
!= NULL
)
9301 free (finfo
.internal_syms
);
9302 if (finfo
.indices
!= NULL
)
9303 free (finfo
.indices
);
9304 if (finfo
.sections
!= NULL
)
9305 free (finfo
.sections
);
9306 if (finfo
.symbuf
!= NULL
)
9307 free (finfo
.symbuf
);
9308 if (finfo
.symshndxbuf
!= NULL
)
9309 free (finfo
.symshndxbuf
);
9310 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9312 if ((o
->flags
& SEC_RELOC
) != 0
9313 && elf_section_data (o
)->rel_hashes
!= NULL
)
9314 free (elf_section_data (o
)->rel_hashes
);
9317 elf_tdata (abfd
)->linker
= TRUE
;
9322 if (finfo
.symstrtab
!= NULL
)
9323 _bfd_stringtab_free (finfo
.symstrtab
);
9324 if (finfo
.contents
!= NULL
)
9325 free (finfo
.contents
);
9326 if (finfo
.external_relocs
!= NULL
)
9327 free (finfo
.external_relocs
);
9328 if (finfo
.internal_relocs
!= NULL
)
9329 free (finfo
.internal_relocs
);
9330 if (finfo
.external_syms
!= NULL
)
9331 free (finfo
.external_syms
);
9332 if (finfo
.locsym_shndx
!= NULL
)
9333 free (finfo
.locsym_shndx
);
9334 if (finfo
.internal_syms
!= NULL
)
9335 free (finfo
.internal_syms
);
9336 if (finfo
.indices
!= NULL
)
9337 free (finfo
.indices
);
9338 if (finfo
.sections
!= NULL
)
9339 free (finfo
.sections
);
9340 if (finfo
.symbuf
!= NULL
)
9341 free (finfo
.symbuf
);
9342 if (finfo
.symshndxbuf
!= NULL
)
9343 free (finfo
.symshndxbuf
);
9344 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9346 if ((o
->flags
& SEC_RELOC
) != 0
9347 && elf_section_data (o
)->rel_hashes
!= NULL
)
9348 free (elf_section_data (o
)->rel_hashes
);
9354 /* Garbage collect unused sections. */
9356 typedef asection
* (*gc_mark_hook_fn
)
9357 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9358 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
9360 /* Default gc_mark_hook. */
9363 _bfd_elf_gc_mark_hook (asection
*sec
,
9364 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9365 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
9366 struct elf_link_hash_entry
*h
,
9367 Elf_Internal_Sym
*sym
)
9371 switch (h
->root
.type
)
9373 case bfd_link_hash_defined
:
9374 case bfd_link_hash_defweak
:
9375 return h
->root
.u
.def
.section
;
9377 case bfd_link_hash_common
:
9378 return h
->root
.u
.c
.p
->section
;
9385 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
9390 /* The mark phase of garbage collection. For a given section, mark
9391 it and any sections in this section's group, and all the sections
9392 which define symbols to which it refers. */
9395 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
9397 gc_mark_hook_fn gc_mark_hook
)
9401 asection
*group_sec
;
9405 /* Mark all the sections in the group. */
9406 group_sec
= elf_section_data (sec
)->next_in_group
;
9407 if (group_sec
&& !group_sec
->gc_mark
)
9408 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
9411 /* Look through the section relocs. */
9413 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
9414 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
9416 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
9417 Elf_Internal_Shdr
*symtab_hdr
;
9418 struct elf_link_hash_entry
**sym_hashes
;
9421 bfd
*input_bfd
= sec
->owner
;
9422 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
9423 Elf_Internal_Sym
*isym
= NULL
;
9426 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9427 sym_hashes
= elf_sym_hashes (input_bfd
);
9429 /* Read the local symbols. */
9430 if (elf_bad_symtab (input_bfd
))
9432 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9436 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
9438 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9439 if (isym
== NULL
&& nlocsyms
!= 0)
9441 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
9447 /* Read the relocations. */
9448 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
9450 if (relstart
== NULL
)
9455 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9457 if (bed
->s
->arch_size
== 32)
9462 for (rel
= relstart
; rel
< relend
; rel
++)
9464 unsigned long r_symndx
;
9466 struct elf_link_hash_entry
*h
;
9468 r_symndx
= rel
->r_info
>> r_sym_shift
;
9472 if (r_symndx
>= nlocsyms
9473 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
9475 h
= sym_hashes
[r_symndx
- extsymoff
];
9476 while (h
->root
.type
== bfd_link_hash_indirect
9477 || h
->root
.type
== bfd_link_hash_warning
)
9478 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9479 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
9483 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
9486 if (rsec
&& !rsec
->gc_mark
)
9488 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
9491 rsec
->gc_mark_from_eh
= 1;
9492 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
9501 if (elf_section_data (sec
)->relocs
!= relstart
)
9504 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
9506 if (! info
->keep_memory
)
9509 symtab_hdr
->contents
= (unsigned char *) isym
;
9516 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
9518 struct elf_gc_sweep_symbol_info
9520 struct bfd_link_info
*info
;
9521 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
9526 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
9528 if (h
->root
.type
== bfd_link_hash_warning
)
9529 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9531 if ((h
->root
.type
== bfd_link_hash_defined
9532 || h
->root
.type
== bfd_link_hash_defweak
)
9533 && !h
->root
.u
.def
.section
->gc_mark
9534 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
9536 struct elf_gc_sweep_symbol_info
*inf
= data
;
9537 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
9543 /* The sweep phase of garbage collection. Remove all garbage sections. */
9545 typedef bfd_boolean (*gc_sweep_hook_fn
)
9546 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
9549 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
9552 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9553 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
9554 unsigned long section_sym_count
;
9555 struct elf_gc_sweep_symbol_info sweep_info
;
9557 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9561 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9564 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9566 /* Keep debug and special sections. */
9567 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
9568 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
9574 /* Skip sweeping sections already excluded. */
9575 if (o
->flags
& SEC_EXCLUDE
)
9578 /* Since this is early in the link process, it is simple
9579 to remove a section from the output. */
9580 o
->flags
|= SEC_EXCLUDE
;
9582 if (info
->print_gc_sections
== TRUE
)
9583 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
9585 /* But we also have to update some of the relocation
9586 info we collected before. */
9588 && (o
->flags
& SEC_RELOC
) != 0
9589 && o
->reloc_count
> 0
9590 && !bfd_is_abs_section (o
->output_section
))
9592 Elf_Internal_Rela
*internal_relocs
;
9596 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
9598 if (internal_relocs
== NULL
)
9601 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
9603 if (elf_section_data (o
)->relocs
!= internal_relocs
)
9604 free (internal_relocs
);
9612 /* Remove the symbols that were in the swept sections from the dynamic
9613 symbol table. GCFIXME: Anyone know how to get them out of the
9614 static symbol table as well? */
9615 sweep_info
.info
= info
;
9616 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
9617 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
9620 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
9624 /* Propagate collected vtable information. This is called through
9625 elf_link_hash_traverse. */
9628 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
9630 if (h
->root
.type
== bfd_link_hash_warning
)
9631 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9633 /* Those that are not vtables. */
9634 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9637 /* Those vtables that do not have parents, we cannot merge. */
9638 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
9641 /* If we've already been done, exit. */
9642 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
9645 /* Make sure the parent's table is up to date. */
9646 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
9648 if (h
->vtable
->used
== NULL
)
9650 /* None of this table's entries were referenced. Re-use the
9652 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
9653 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
9658 bfd_boolean
*cu
, *pu
;
9660 /* Or the parent's entries into ours. */
9661 cu
= h
->vtable
->used
;
9663 pu
= h
->vtable
->parent
->vtable
->used
;
9666 const struct elf_backend_data
*bed
;
9667 unsigned int log_file_align
;
9669 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
9670 log_file_align
= bed
->s
->log_file_align
;
9671 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9686 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9689 bfd_vma hstart
, hend
;
9690 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9691 const struct elf_backend_data
*bed
;
9692 unsigned int log_file_align
;
9694 if (h
->root
.type
== bfd_link_hash_warning
)
9695 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9697 /* Take care of both those symbols that do not describe vtables as
9698 well as those that are not loaded. */
9699 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9702 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9703 || h
->root
.type
== bfd_link_hash_defweak
);
9705 sec
= h
->root
.u
.def
.section
;
9706 hstart
= h
->root
.u
.def
.value
;
9707 hend
= hstart
+ h
->size
;
9709 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9711 return *(bfd_boolean
*) okp
= FALSE
;
9712 bed
= get_elf_backend_data (sec
->owner
);
9713 log_file_align
= bed
->s
->log_file_align
;
9715 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9717 for (rel
= relstart
; rel
< relend
; ++rel
)
9718 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9720 /* If the entry is in use, do nothing. */
9722 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9724 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9725 if (h
->vtable
->used
[entry
])
9728 /* Otherwise, kill it. */
9729 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9735 /* Mark sections containing dynamically referenced symbols. When
9736 building shared libraries, we must assume that any visible symbol is
9740 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
9742 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
9744 if (h
->root
.type
== bfd_link_hash_warning
)
9745 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9747 if ((h
->root
.type
== bfd_link_hash_defined
9748 || h
->root
.type
== bfd_link_hash_defweak
)
9750 || (!info
->executable
9752 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
9753 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
9754 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9759 /* Do mark and sweep of unused sections. */
9762 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9764 bfd_boolean ok
= TRUE
;
9766 asection
* (*gc_mark_hook
)
9767 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9768 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9769 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9771 if (!bed
->can_gc_sections
9772 || info
->relocatable
9773 || info
->emitrelocations
9774 || !is_elf_hash_table (info
->hash
))
9776 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9780 /* Apply transitive closure to the vtable entry usage info. */
9781 elf_link_hash_traverse (elf_hash_table (info
),
9782 elf_gc_propagate_vtable_entries_used
,
9787 /* Kill the vtable relocations that were not used. */
9788 elf_link_hash_traverse (elf_hash_table (info
),
9789 elf_gc_smash_unused_vtentry_relocs
,
9794 /* Mark dynamically referenced symbols. */
9795 if (elf_hash_table (info
)->dynamic_sections_created
)
9796 elf_link_hash_traverse (elf_hash_table (info
),
9797 bed
->gc_mark_dynamic_ref
,
9800 /* Grovel through relocs to find out who stays ... */
9801 gc_mark_hook
= bed
->gc_mark_hook
;
9802 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9806 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9809 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9810 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
9811 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9815 /* ... again for sections marked from eh_frame. */
9816 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9820 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9823 /* Keep .gcc_except_table.* if the associated .text.* is
9824 marked. This isn't very nice, but the proper solution,
9825 splitting .eh_frame up and using comdat doesn't pan out
9826 easily due to needing special relocs to handle the
9827 difference of two symbols in separate sections.
9828 Don't keep code sections referenced by .eh_frame. */
9829 #define TEXT_PREFIX ".text."
9830 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
9831 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9832 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
9834 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
9837 const char *sec_name
;
9839 unsigned o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
9840 unsigned fn_name_prefix_len
= strlen (TEXT_PREFIX
);
9842 sec_name
= o
->name
+ o_name_prefix_len
;
9843 fn_name
= bfd_malloc (strlen (sec_name
) + fn_name_prefix_len
+ 1);
9844 if (fn_name
== NULL
)
9846 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
9847 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
9849 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
9853 /* If not using specially named exception table section,
9854 then keep whatever we are using. */
9855 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9860 /* ... and mark SEC_EXCLUDE for those that go. */
9861 return elf_gc_sweep (abfd
, info
);
9864 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9867 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9869 struct elf_link_hash_entry
*h
,
9872 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9873 struct elf_link_hash_entry
**search
, *child
;
9874 bfd_size_type extsymcount
;
9875 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9877 /* The sh_info field of the symtab header tells us where the
9878 external symbols start. We don't care about the local symbols at
9880 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9881 if (!elf_bad_symtab (abfd
))
9882 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9884 sym_hashes
= elf_sym_hashes (abfd
);
9885 sym_hashes_end
= sym_hashes
+ extsymcount
;
9887 /* Hunt down the child symbol, which is in this section at the same
9888 offset as the relocation. */
9889 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9891 if ((child
= *search
) != NULL
9892 && (child
->root
.type
== bfd_link_hash_defined
9893 || child
->root
.type
== bfd_link_hash_defweak
)
9894 && child
->root
.u
.def
.section
== sec
9895 && child
->root
.u
.def
.value
== offset
)
9899 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9900 abfd
, sec
, (unsigned long) offset
);
9901 bfd_set_error (bfd_error_invalid_operation
);
9907 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9913 /* This *should* only be the absolute section. It could potentially
9914 be that someone has defined a non-global vtable though, which
9915 would be bad. It isn't worth paging in the local symbols to be
9916 sure though; that case should simply be handled by the assembler. */
9918 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9921 child
->vtable
->parent
= h
;
9926 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9929 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9930 asection
*sec ATTRIBUTE_UNUSED
,
9931 struct elf_link_hash_entry
*h
,
9934 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9935 unsigned int log_file_align
= bed
->s
->log_file_align
;
9939 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9944 if (addend
>= h
->vtable
->size
)
9946 size_t size
, bytes
, file_align
;
9947 bfd_boolean
*ptr
= h
->vtable
->used
;
9949 /* While the symbol is undefined, we have to be prepared to handle
9951 file_align
= 1 << log_file_align
;
9952 if (h
->root
.type
== bfd_link_hash_undefined
)
9953 size
= addend
+ file_align
;
9959 /* Oops! We've got a reference past the defined end of
9960 the table. This is probably a bug -- shall we warn? */
9961 size
= addend
+ file_align
;
9964 size
= (size
+ file_align
- 1) & -file_align
;
9966 /* Allocate one extra entry for use as a "done" flag for the
9967 consolidation pass. */
9968 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9972 ptr
= bfd_realloc (ptr
- 1, bytes
);
9978 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9979 * sizeof (bfd_boolean
));
9980 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9984 ptr
= bfd_zmalloc (bytes
);
9989 /* And arrange for that done flag to be at index -1. */
9990 h
->vtable
->used
= ptr
+ 1;
9991 h
->vtable
->size
= size
;
9994 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9999 struct alloc_got_off_arg
{
10001 unsigned int got_elt_size
;
10004 /* We need a special top-level link routine to convert got reference counts
10005 to real got offsets. */
10008 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
10010 struct alloc_got_off_arg
*gofarg
= arg
;
10012 if (h
->root
.type
== bfd_link_hash_warning
)
10013 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10015 if (h
->got
.refcount
> 0)
10017 h
->got
.offset
= gofarg
->gotoff
;
10018 gofarg
->gotoff
+= gofarg
->got_elt_size
;
10021 h
->got
.offset
= (bfd_vma
) -1;
10026 /* And an accompanying bit to work out final got entry offsets once
10027 we're done. Should be called from final_link. */
10030 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
10031 struct bfd_link_info
*info
)
10034 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10036 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
10037 struct alloc_got_off_arg gofarg
;
10039 if (! is_elf_hash_table (info
->hash
))
10042 /* The GOT offset is relative to the .got section, but the GOT header is
10043 put into the .got.plt section, if the backend uses it. */
10044 if (bed
->want_got_plt
)
10047 gotoff
= bed
->got_header_size
;
10049 /* Do the local .got entries first. */
10050 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
10052 bfd_signed_vma
*local_got
;
10053 bfd_size_type j
, locsymcount
;
10054 Elf_Internal_Shdr
*symtab_hdr
;
10056 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
10059 local_got
= elf_local_got_refcounts (i
);
10063 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
10064 if (elf_bad_symtab (i
))
10065 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10067 locsymcount
= symtab_hdr
->sh_info
;
10069 for (j
= 0; j
< locsymcount
; ++j
)
10071 if (local_got
[j
] > 0)
10073 local_got
[j
] = gotoff
;
10074 gotoff
+= got_elt_size
;
10077 local_got
[j
] = (bfd_vma
) -1;
10081 /* Then the global .got entries. .plt refcounts are handled by
10082 adjust_dynamic_symbol */
10083 gofarg
.gotoff
= gotoff
;
10084 gofarg
.got_elt_size
= got_elt_size
;
10085 elf_link_hash_traverse (elf_hash_table (info
),
10086 elf_gc_allocate_got_offsets
,
10091 /* Many folk need no more in the way of final link than this, once
10092 got entry reference counting is enabled. */
10095 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10097 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
10100 /* Invoke the regular ELF backend linker to do all the work. */
10101 return bfd_elf_final_link (abfd
, info
);
10105 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
10107 struct elf_reloc_cookie
*rcookie
= cookie
;
10109 if (rcookie
->bad_symtab
)
10110 rcookie
->rel
= rcookie
->rels
;
10112 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
10114 unsigned long r_symndx
;
10116 if (! rcookie
->bad_symtab
)
10117 if (rcookie
->rel
->r_offset
> offset
)
10119 if (rcookie
->rel
->r_offset
!= offset
)
10122 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
10123 if (r_symndx
== SHN_UNDEF
)
10126 if (r_symndx
>= rcookie
->locsymcount
10127 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
10129 struct elf_link_hash_entry
*h
;
10131 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
10133 while (h
->root
.type
== bfd_link_hash_indirect
10134 || h
->root
.type
== bfd_link_hash_warning
)
10135 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10137 if ((h
->root
.type
== bfd_link_hash_defined
10138 || h
->root
.type
== bfd_link_hash_defweak
)
10139 && elf_discarded_section (h
->root
.u
.def
.section
))
10146 /* It's not a relocation against a global symbol,
10147 but it could be a relocation against a local
10148 symbol for a discarded section. */
10150 Elf_Internal_Sym
*isym
;
10152 /* Need to: get the symbol; get the section. */
10153 isym
= &rcookie
->locsyms
[r_symndx
];
10154 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
10156 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
10157 if (isec
!= NULL
&& elf_discarded_section (isec
))
10166 /* Discard unneeded references to discarded sections.
10167 Returns TRUE if any section's size was changed. */
10168 /* This function assumes that the relocations are in sorted order,
10169 which is true for all known assemblers. */
10172 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
10174 struct elf_reloc_cookie cookie
;
10175 asection
*stab
, *eh
;
10176 Elf_Internal_Shdr
*symtab_hdr
;
10177 const struct elf_backend_data
*bed
;
10179 unsigned int count
;
10180 bfd_boolean ret
= FALSE
;
10182 if (info
->traditional_format
10183 || !is_elf_hash_table (info
->hash
))
10186 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10188 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10191 bed
= get_elf_backend_data (abfd
);
10193 if ((abfd
->flags
& DYNAMIC
) != 0)
10196 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10197 if (info
->relocatable
10200 || bfd_is_abs_section (eh
->output_section
))))
10203 stab
= bfd_get_section_by_name (abfd
, ".stab");
10205 && (stab
->size
== 0
10206 || bfd_is_abs_section (stab
->output_section
)
10207 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10212 && bed
->elf_backend_discard_info
== NULL
)
10215 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10216 cookie
.abfd
= abfd
;
10217 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10218 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10219 if (cookie
.bad_symtab
)
10221 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10222 cookie
.extsymoff
= 0;
10226 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10227 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10230 if (bed
->s
->arch_size
== 32)
10231 cookie
.r_sym_shift
= 8;
10233 cookie
.r_sym_shift
= 32;
10235 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10236 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10238 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10239 cookie
.locsymcount
, 0,
10241 if (cookie
.locsyms
== NULL
)
10247 cookie
.rels
= NULL
;
10248 count
= stab
->reloc_count
;
10250 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
10251 info
->keep_memory
);
10252 if (cookie
.rels
!= NULL
)
10254 cookie
.rel
= cookie
.rels
;
10255 cookie
.relend
= cookie
.rels
;
10256 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10257 if (_bfd_discard_section_stabs (abfd
, stab
,
10258 elf_section_data (stab
)->sec_info
,
10259 bfd_elf_reloc_symbol_deleted_p
,
10262 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
10263 free (cookie
.rels
);
10269 cookie
.rels
= NULL
;
10270 count
= eh
->reloc_count
;
10272 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
10273 info
->keep_memory
);
10274 cookie
.rel
= cookie
.rels
;
10275 cookie
.relend
= cookie
.rels
;
10276 if (cookie
.rels
!= NULL
)
10277 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
10279 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
10280 bfd_elf_reloc_symbol_deleted_p
,
10284 if (cookie
.rels
!= NULL
10285 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
10286 free (cookie
.rels
);
10289 if (bed
->elf_backend_discard_info
!= NULL
10290 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
10293 if (cookie
.locsyms
!= NULL
10294 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
10296 if (! info
->keep_memory
)
10297 free (cookie
.locsyms
);
10299 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
10303 if (info
->eh_frame_hdr
10304 && !info
->relocatable
10305 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
10312 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
10313 struct bfd_link_info
*info
)
10316 const char *name
, *p
;
10317 struct bfd_section_already_linked
*l
;
10318 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
10321 /* A single member comdat group section may be discarded by a
10322 linkonce section. See below. */
10323 if (sec
->output_section
== bfd_abs_section_ptr
)
10326 flags
= sec
->flags
;
10328 /* Check if it belongs to a section group. */
10329 group
= elf_sec_group (sec
);
10331 /* Return if it isn't a linkonce section nor a member of a group. A
10332 comdat group section also has SEC_LINK_ONCE set. */
10333 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
10338 /* If this is the member of a single member comdat group, check if
10339 the group should be discarded. */
10340 if (elf_next_in_group (sec
) == sec
10341 && (group
->flags
& SEC_LINK_ONCE
) != 0)
10347 /* FIXME: When doing a relocatable link, we may have trouble
10348 copying relocations in other sections that refer to local symbols
10349 in the section being discarded. Those relocations will have to
10350 be converted somehow; as of this writing I'm not sure that any of
10351 the backends handle that correctly.
10353 It is tempting to instead not discard link once sections when
10354 doing a relocatable link (technically, they should be discarded
10355 whenever we are building constructors). However, that fails,
10356 because the linker winds up combining all the link once sections
10357 into a single large link once section, which defeats the purpose
10358 of having link once sections in the first place.
10360 Also, not merging link once sections in a relocatable link
10361 causes trouble for MIPS ELF, which relies on link once semantics
10362 to handle the .reginfo section correctly. */
10364 name
= bfd_get_section_name (abfd
, sec
);
10366 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
10367 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
10372 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
10374 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10376 /* We may have 3 different sections on the list: group section,
10377 comdat section and linkonce section. SEC may be a linkonce or
10378 group section. We match a group section with a group section,
10379 a linkonce section with a linkonce section, and ignore comdat
10381 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
10382 && strcmp (name
, l
->sec
->name
) == 0
10383 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
10385 /* The section has already been linked. See if we should
10386 issue a warning. */
10387 switch (flags
& SEC_LINK_DUPLICATES
)
10392 case SEC_LINK_DUPLICATES_DISCARD
:
10395 case SEC_LINK_DUPLICATES_ONE_ONLY
:
10396 (*_bfd_error_handler
)
10397 (_("%B: ignoring duplicate section `%A'"),
10401 case SEC_LINK_DUPLICATES_SAME_SIZE
:
10402 if (sec
->size
!= l
->sec
->size
)
10403 (*_bfd_error_handler
)
10404 (_("%B: duplicate section `%A' has different size"),
10408 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
10409 if (sec
->size
!= l
->sec
->size
)
10410 (*_bfd_error_handler
)
10411 (_("%B: duplicate section `%A' has different size"),
10413 else if (sec
->size
!= 0)
10415 bfd_byte
*sec_contents
, *l_sec_contents
;
10417 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
10418 (*_bfd_error_handler
)
10419 (_("%B: warning: could not read contents of section `%A'"),
10421 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
10423 (*_bfd_error_handler
)
10424 (_("%B: warning: could not read contents of section `%A'"),
10425 l
->sec
->owner
, l
->sec
);
10426 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
10427 (*_bfd_error_handler
)
10428 (_("%B: warning: duplicate section `%A' has different contents"),
10432 free (sec_contents
);
10433 if (l_sec_contents
)
10434 free (l_sec_contents
);
10439 /* Set the output_section field so that lang_add_section
10440 does not create a lang_input_section structure for this
10441 section. Since there might be a symbol in the section
10442 being discarded, we must retain a pointer to the section
10443 which we are really going to use. */
10444 sec
->output_section
= bfd_abs_section_ptr
;
10445 sec
->kept_section
= l
->sec
;
10447 if (flags
& SEC_GROUP
)
10449 asection
*first
= elf_next_in_group (sec
);
10450 asection
*s
= first
;
10454 s
->output_section
= bfd_abs_section_ptr
;
10455 /* Record which group discards it. */
10456 s
->kept_section
= l
->sec
;
10457 s
= elf_next_in_group (s
);
10458 /* These lists are circular. */
10470 /* If this is the member of a single member comdat group and the
10471 group hasn't be discarded, we check if it matches a linkonce
10472 section. We only record the discarded comdat group. Otherwise
10473 the undiscarded group will be discarded incorrectly later since
10474 itself has been recorded. */
10475 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10476 if ((l
->sec
->flags
& SEC_GROUP
) == 0
10477 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
10478 && bfd_elf_match_symbols_in_sections (l
->sec
,
10479 elf_next_in_group (sec
),
10482 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
10483 elf_next_in_group (sec
)->kept_section
= l
->sec
;
10484 group
->output_section
= bfd_abs_section_ptr
;
10491 /* There is no direct match. But for linkonce section, we should
10492 check if there is a match with comdat group member. We always
10493 record the linkonce section, discarded or not. */
10494 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
10495 if (l
->sec
->flags
& SEC_GROUP
)
10497 asection
*first
= elf_next_in_group (l
->sec
);
10500 && elf_next_in_group (first
) == first
10501 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
10503 sec
->output_section
= bfd_abs_section_ptr
;
10504 sec
->kept_section
= l
->sec
;
10509 /* This is the first section with this name. Record it. */
10510 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
10514 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
10516 return sym
->st_shndx
== SHN_COMMON
;
10520 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
10526 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
10528 return bfd_com_section_ptr
;