1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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 3 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,
20 MA 02110-1301, USA. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry
*
35 _bfd_elf_define_linkage_sym (bfd
*abfd
,
36 struct bfd_link_info
*info
,
40 struct elf_link_hash_entry
*h
;
41 struct bfd_link_hash_entry
*bh
;
42 const struct elf_backend_data
*bed
;
44 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h
->root
.type
= bfd_link_hash_new
;
55 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
57 get_elf_backend_data (abfd
)->collect
,
60 h
= (struct elf_link_hash_entry
*) bh
;
63 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
65 bed
= get_elf_backend_data (abfd
);
66 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
71 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
75 struct elf_link_hash_entry
*h
;
76 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
79 /* This function may be called more than once. */
80 s
= bfd_get_section_by_name (abfd
, ".got");
81 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
84 switch (bed
->s
->arch_size
)
95 bfd_set_error (bfd_error_bad_value
);
99 flags
= bed
->dynamic_sec_flags
;
101 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
103 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
106 if (bed
->want_got_plt
)
108 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
110 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
114 if (bed
->want_got_sym
)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info
)->hgot
= h
;
126 /* The first bit of the global offset table is the header. */
127 s
->size
+= bed
->got_header_size
;
132 /* Create a strtab to hold the dynamic symbol names. */
134 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
136 struct elf_link_hash_table
*hash_table
;
138 hash_table
= elf_hash_table (info
);
139 if (hash_table
->dynobj
== NULL
)
140 hash_table
->dynobj
= abfd
;
142 if (hash_table
->dynstr
== NULL
)
144 hash_table
->dynstr
= _bfd_elf_strtab_init ();
145 if (hash_table
->dynstr
== NULL
)
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
159 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
162 register asection
*s
;
163 const struct elf_backend_data
*bed
;
165 if (! is_elf_hash_table (info
->hash
))
168 if (elf_hash_table (info
)->dynamic_sections_created
)
171 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
174 abfd
= elf_hash_table (info
)->dynobj
;
175 bed
= get_elf_backend_data (abfd
);
177 flags
= bed
->dynamic_sec_flags
;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info
->executable
)
183 s
= bfd_make_section_with_flags (abfd
, ".interp",
184 flags
| SEC_READONLY
);
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
192 flags
| SEC_READONLY
);
194 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
197 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
198 flags
| SEC_READONLY
);
200 || ! bfd_set_section_alignment (abfd
, s
, 1))
203 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
204 flags
| SEC_READONLY
);
206 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
209 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
210 flags
| SEC_READONLY
);
212 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
215 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
216 flags
| SEC_READONLY
);
220 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
222 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
236 s
= bfd_make_section_with_flags (abfd
, ".hash", flags
| SEC_READONLY
);
238 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
240 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
243 if (info
->emit_gnu_hash
)
245 s
= bfd_make_section_with_flags (abfd
, ".gnu.hash",
246 flags
| SEC_READONLY
);
248 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed
->s
->arch_size
== 64)
254 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
256 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
265 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
270 /* Create dynamic sections when linking against a dynamic object. */
273 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
275 flagword flags
, pltflags
;
276 struct elf_link_hash_entry
*h
;
278 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags
= bed
->dynamic_sec_flags
;
285 if (bed
->plt_not_loaded
)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
291 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
292 if (bed
->plt_readonly
)
293 pltflags
|= SEC_READONLY
;
295 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
297 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
302 if (bed
->want_plt_sym
)
304 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info
)->hplt
= h
;
311 s
= bfd_make_section_with_flags (abfd
,
312 (bed
->default_use_rela_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags
| SEC_READONLY
);
316 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
319 if (! _bfd_elf_create_got_section (abfd
, info
))
322 if (bed
->want_dynbss
)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
332 | SEC_LINKER_CREATED
));
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
349 s
= bfd_make_section_with_flags (abfd
,
350 (bed
->default_use_rela_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags
| SEC_READONLY
);
354 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
372 struct elf_link_hash_entry
*h
)
374 if (h
->dynindx
== -1)
376 struct elf_strtab_hash
*dynstr
;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h
->other
))
389 if (h
->root
.type
!= bfd_link_hash_undefined
390 && h
->root
.type
!= bfd_link_hash_undefweak
)
393 if (!elf_hash_table (info
)->is_relocatable_executable
)
401 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
402 ++elf_hash_table (info
)->dynsymcount
;
404 dynstr
= elf_hash_table (info
)->dynstr
;
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
413 /* We don't put any version information in the dynamic string
415 name
= h
->root
.root
.string
;
416 p
= strchr (name
, ELF_VER_CHR
);
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
425 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
430 if (indx
== (bfd_size_type
) -1)
432 h
->dynstr_index
= indx
;
438 /* Mark a symbol dynamic. */
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
442 struct elf_link_hash_entry
*h
,
443 Elf_Internal_Sym
*sym
)
445 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
447 /* It may be called more than once on the same H. */
448 if(h
->dynamic
|| info
->relocatable
)
451 if ((info
->dynamic_data
452 && (h
->type
== STT_OBJECT
454 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
456 && h
->root
.type
== bfd_link_hash_new
457 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
465 bfd_elf_record_link_assignment (bfd
*output_bfd
,
466 struct bfd_link_info
*info
,
471 struct elf_link_hash_entry
*h
;
472 struct elf_link_hash_table
*htab
;
474 if (!is_elf_hash_table (info
->hash
))
477 htab
= elf_hash_table (info
);
478 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
482 /* Since we're defining the symbol, don't let it seem to have not
483 been defined. record_dynamic_symbol and size_dynamic_sections
484 may depend on this. */
485 if (h
->root
.type
== bfd_link_hash_undefweak
486 || h
->root
.type
== bfd_link_hash_undefined
)
488 h
->root
.type
= bfd_link_hash_new
;
489 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
490 bfd_link_repair_undef_list (&htab
->root
);
493 if (h
->root
.type
== bfd_link_hash_new
)
495 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
499 /* If this symbol is being provided by the linker script, and it is
500 currently defined by a dynamic object, but not by a regular
501 object, then mark it as undefined so that the generic linker will
502 force the correct value. */
506 h
->root
.type
= bfd_link_hash_undefined
;
508 /* If this symbol is not being provided by the linker script, and it is
509 currently defined by a dynamic object, but not by a regular object,
510 then clear out any version information because the symbol will not be
511 associated with the dynamic object any more. */
515 h
->verinfo
.verdef
= NULL
;
519 if (provide
&& hidden
)
521 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
523 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
524 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
527 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
529 if (!info
->relocatable
531 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
532 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
538 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
541 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
544 /* If this is a weak defined symbol, and we know a corresponding
545 real symbol from the same dynamic object, make sure the real
546 symbol is also made into a dynamic symbol. */
547 if (h
->u
.weakdef
!= NULL
548 && h
->u
.weakdef
->dynindx
== -1)
550 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
558 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
559 success, and 2 on a failure caused by attempting to record a symbol
560 in a discarded section, eg. a discarded link-once section symbol. */
563 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
568 struct elf_link_local_dynamic_entry
*entry
;
569 struct elf_link_hash_table
*eht
;
570 struct elf_strtab_hash
*dynstr
;
571 unsigned long dynstr_index
;
573 Elf_External_Sym_Shndx eshndx
;
574 char esym
[sizeof (Elf64_External_Sym
)];
576 if (! is_elf_hash_table (info
->hash
))
579 /* See if the entry exists already. */
580 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
581 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
584 amt
= sizeof (*entry
);
585 entry
= bfd_alloc (input_bfd
, amt
);
589 /* Go find the symbol, so that we can find it's name. */
590 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
591 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
593 bfd_release (input_bfd
, entry
);
597 if (entry
->isym
.st_shndx
!= SHN_UNDEF
598 && (entry
->isym
.st_shndx
< SHN_LORESERVE
599 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
603 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
604 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
606 /* We can still bfd_release here as nothing has done another
607 bfd_alloc. We can't do this later in this function. */
608 bfd_release (input_bfd
, entry
);
613 name
= (bfd_elf_string_from_elf_section
614 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
615 entry
->isym
.st_name
));
617 dynstr
= elf_hash_table (info
)->dynstr
;
620 /* Create a strtab to hold the dynamic symbol names. */
621 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
626 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
627 if (dynstr_index
== (unsigned long) -1)
629 entry
->isym
.st_name
= dynstr_index
;
631 eht
= elf_hash_table (info
);
633 entry
->next
= eht
->dynlocal
;
634 eht
->dynlocal
= entry
;
635 entry
->input_bfd
= input_bfd
;
636 entry
->input_indx
= input_indx
;
639 /* Whatever binding the symbol had before, it's now local. */
641 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
643 /* The dynindx will be set at the end of size_dynamic_sections. */
648 /* Return the dynindex of a local dynamic symbol. */
651 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
655 struct elf_link_local_dynamic_entry
*e
;
657 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
658 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
663 /* This function is used to renumber the dynamic symbols, if some of
664 them are removed because they are marked as local. This is called
665 via elf_link_hash_traverse. */
668 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
671 size_t *count
= data
;
673 if (h
->root
.type
== bfd_link_hash_warning
)
674 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
679 if (h
->dynindx
!= -1)
680 h
->dynindx
= ++(*count
);
686 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
687 STB_LOCAL binding. */
690 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
693 size_t *count
= data
;
695 if (h
->root
.type
== bfd_link_hash_warning
)
696 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
698 if (!h
->forced_local
)
701 if (h
->dynindx
!= -1)
702 h
->dynindx
= ++(*count
);
707 /* Return true if the dynamic symbol for a given section should be
708 omitted when creating a shared library. */
710 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
711 struct bfd_link_info
*info
,
714 struct elf_link_hash_table
*htab
;
716 switch (elf_section_data (p
)->this_hdr
.sh_type
)
720 /* If sh_type is yet undecided, assume it could be
721 SHT_PROGBITS/SHT_NOBITS. */
723 htab
= elf_hash_table (info
);
724 if (p
== htab
->tls_sec
)
727 if (htab
->text_index_section
!= NULL
)
728 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
730 if (strcmp (p
->name
, ".got") == 0
731 || strcmp (p
->name
, ".got.plt") == 0
732 || strcmp (p
->name
, ".plt") == 0)
736 if (htab
->dynobj
!= NULL
737 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
738 && (ip
->flags
& SEC_LINKER_CREATED
)
739 && ip
->output_section
== p
)
744 /* There shouldn't be section relative relocations
745 against any other section. */
751 /* Assign dynsym indices. In a shared library we generate a section
752 symbol for each output section, which come first. Next come symbols
753 which have been forced to local binding. Then all of the back-end
754 allocated local dynamic syms, followed by the rest of the global
758 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
759 struct bfd_link_info
*info
,
760 unsigned long *section_sym_count
)
762 unsigned long dynsymcount
= 0;
764 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
766 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
768 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
769 if ((p
->flags
& SEC_EXCLUDE
) == 0
770 && (p
->flags
& SEC_ALLOC
) != 0
771 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
772 elf_section_data (p
)->dynindx
= ++dynsymcount
;
774 elf_section_data (p
)->dynindx
= 0;
776 *section_sym_count
= dynsymcount
;
778 elf_link_hash_traverse (elf_hash_table (info
),
779 elf_link_renumber_local_hash_table_dynsyms
,
782 if (elf_hash_table (info
)->dynlocal
)
784 struct elf_link_local_dynamic_entry
*p
;
785 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
786 p
->dynindx
= ++dynsymcount
;
789 elf_link_hash_traverse (elf_hash_table (info
),
790 elf_link_renumber_hash_table_dynsyms
,
793 /* There is an unused NULL entry at the head of the table which
794 we must account for in our count. Unless there weren't any
795 symbols, which means we'll have no table at all. */
796 if (dynsymcount
!= 0)
799 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
803 /* This function is called when we want to define a new symbol. It
804 handles the various cases which arise when we find a definition in
805 a dynamic object, or when there is already a definition in a
806 dynamic object. The new symbol is described by NAME, SYM, PSEC,
807 and PVALUE. We set SYM_HASH to the hash table entry. We set
808 OVERRIDE if the old symbol is overriding a new definition. We set
809 TYPE_CHANGE_OK if it is OK for the type to change. We set
810 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
811 change, we mean that we shouldn't warn if the type or size does
812 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
813 object is overridden by a regular object. */
816 _bfd_elf_merge_symbol (bfd
*abfd
,
817 struct bfd_link_info
*info
,
819 Elf_Internal_Sym
*sym
,
822 unsigned int *pold_alignment
,
823 struct elf_link_hash_entry
**sym_hash
,
825 bfd_boolean
*override
,
826 bfd_boolean
*type_change_ok
,
827 bfd_boolean
*size_change_ok
)
829 asection
*sec
, *oldsec
;
830 struct elf_link_hash_entry
*h
;
831 struct elf_link_hash_entry
*flip
;
834 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
835 bfd_boolean newweak
, oldweak
;
836 const struct elf_backend_data
*bed
;
842 bind
= ELF_ST_BIND (sym
->st_info
);
844 /* Silently discard TLS symbols from --just-syms. There's no way to
845 combine a static TLS block with a new TLS block for this executable. */
846 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
847 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
853 if (! bfd_is_und_section (sec
))
854 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
856 h
= ((struct elf_link_hash_entry
*)
857 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
862 /* This code is for coping with dynamic objects, and is only useful
863 if we are doing an ELF link. */
864 if (info
->hash
->creator
!= abfd
->xvec
)
867 /* For merging, we only care about real symbols. */
869 while (h
->root
.type
== bfd_link_hash_indirect
870 || h
->root
.type
== bfd_link_hash_warning
)
871 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
873 /* We have to check it for every instance since the first few may be
874 refereences and not all compilers emit symbol type for undefined
876 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
878 /* If we just created the symbol, mark it as being an ELF symbol.
879 Other than that, there is nothing to do--there is no merge issue
880 with a newly defined symbol--so we just return. */
882 if (h
->root
.type
== bfd_link_hash_new
)
888 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
891 switch (h
->root
.type
)
898 case bfd_link_hash_undefined
:
899 case bfd_link_hash_undefweak
:
900 oldbfd
= h
->root
.u
.undef
.abfd
;
904 case bfd_link_hash_defined
:
905 case bfd_link_hash_defweak
:
906 oldbfd
= h
->root
.u
.def
.section
->owner
;
907 oldsec
= h
->root
.u
.def
.section
;
910 case bfd_link_hash_common
:
911 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
912 oldsec
= h
->root
.u
.c
.p
->section
;
916 /* In cases involving weak versioned symbols, we may wind up trying
917 to merge a symbol with itself. Catch that here, to avoid the
918 confusion that results if we try to override a symbol with
919 itself. The additional tests catch cases like
920 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
921 dynamic object, which we do want to handle here. */
923 && ((abfd
->flags
& DYNAMIC
) == 0
927 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
928 respectively, is from a dynamic object. */
930 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
934 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
935 else if (oldsec
!= NULL
)
937 /* This handles the special SHN_MIPS_{TEXT,DATA} section
938 indices used by MIPS ELF. */
939 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
942 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
943 respectively, appear to be a definition rather than reference. */
945 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
947 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
948 && h
->root
.type
!= bfd_link_hash_undefweak
949 && h
->root
.type
!= bfd_link_hash_common
);
951 bed
= get_elf_backend_data (abfd
);
952 /* When we try to create a default indirect symbol from the dynamic
953 definition with the default version, we skip it if its type and
954 the type of existing regular definition mismatch. We only do it
955 if the existing regular definition won't be dynamic. */
956 if (pold_alignment
== NULL
958 && !info
->export_dynamic
963 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
964 && ELF_ST_TYPE (sym
->st_info
) != h
->type
965 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
966 && h
->type
!= STT_NOTYPE
967 && !(bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
968 && bed
->is_function_type (h
->type
)))
974 /* Check TLS symbol. We don't check undefined symbol introduced by
976 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
977 && ELF_ST_TYPE (sym
->st_info
) != h
->type
981 bfd_boolean ntdef
, tdef
;
982 asection
*ntsec
, *tsec
;
984 if (h
->type
== STT_TLS
)
1004 (*_bfd_error_handler
)
1005 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1006 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1007 else if (!tdef
&& !ntdef
)
1008 (*_bfd_error_handler
)
1009 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1010 tbfd
, ntbfd
, h
->root
.root
.string
);
1012 (*_bfd_error_handler
)
1013 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1014 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1016 (*_bfd_error_handler
)
1017 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1018 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1020 bfd_set_error (bfd_error_bad_value
);
1024 /* We need to remember if a symbol has a definition in a dynamic
1025 object or is weak in all dynamic objects. Internal and hidden
1026 visibility will make it unavailable to dynamic objects. */
1027 if (newdyn
&& !h
->dynamic_def
)
1029 if (!bfd_is_und_section (sec
))
1033 /* Check if this symbol is weak in all dynamic objects. If it
1034 is the first time we see it in a dynamic object, we mark
1035 if it is weak. Otherwise, we clear it. */
1036 if (!h
->ref_dynamic
)
1038 if (bind
== STB_WEAK
)
1039 h
->dynamic_weak
= 1;
1041 else if (bind
!= STB_WEAK
)
1042 h
->dynamic_weak
= 0;
1046 /* If the old symbol has non-default visibility, we ignore the new
1047 definition from a dynamic object. */
1049 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1050 && !bfd_is_und_section (sec
))
1053 /* Make sure this symbol is dynamic. */
1055 /* A protected symbol has external availability. Make sure it is
1056 recorded as dynamic.
1058 FIXME: Should we check type and size for protected symbol? */
1059 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1060 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1065 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1068 /* If the new symbol with non-default visibility comes from a
1069 relocatable file and the old definition comes from a dynamic
1070 object, we remove the old definition. */
1071 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1073 /* Handle the case where the old dynamic definition is
1074 default versioned. We need to copy the symbol info from
1075 the symbol with default version to the normal one if it
1076 was referenced before. */
1079 const struct elf_backend_data
*bed
1080 = get_elf_backend_data (abfd
);
1081 struct elf_link_hash_entry
*vh
= *sym_hash
;
1082 vh
->root
.type
= h
->root
.type
;
1083 h
->root
.type
= bfd_link_hash_indirect
;
1084 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1085 /* Protected symbols will override the dynamic definition
1086 with default version. */
1087 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1089 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1090 vh
->dynamic_def
= 1;
1091 vh
->ref_dynamic
= 1;
1095 h
->root
.type
= vh
->root
.type
;
1096 vh
->ref_dynamic
= 0;
1097 /* We have to hide it here since it was made dynamic
1098 global with extra bits when the symbol info was
1099 copied from the old dynamic definition. */
1100 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1108 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1109 && bfd_is_und_section (sec
))
1111 /* If the new symbol is undefined and the old symbol was
1112 also undefined before, we need to make sure
1113 _bfd_generic_link_add_one_symbol doesn't mess
1114 up the linker hash table undefs list. Since the old
1115 definition came from a dynamic object, it is still on the
1117 h
->root
.type
= bfd_link_hash_undefined
;
1118 h
->root
.u
.undef
.abfd
= abfd
;
1122 h
->root
.type
= bfd_link_hash_new
;
1123 h
->root
.u
.undef
.abfd
= NULL
;
1132 /* FIXME: Should we check type and size for protected symbol? */
1138 /* Differentiate strong and weak symbols. */
1139 newweak
= bind
== STB_WEAK
;
1140 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1141 || h
->root
.type
== bfd_link_hash_undefweak
);
1143 /* If a new weak symbol definition comes from a regular file and the
1144 old symbol comes from a dynamic library, we treat the new one as
1145 strong. Similarly, an old weak symbol definition from a regular
1146 file is treated as strong when the new symbol comes from a dynamic
1147 library. Further, an old weak symbol from a dynamic library is
1148 treated as strong if the new symbol is from a dynamic library.
1149 This reflects the way glibc's ld.so works.
1151 Do this before setting *type_change_ok or *size_change_ok so that
1152 we warn properly when dynamic library symbols are overridden. */
1154 if (newdef
&& !newdyn
&& olddyn
)
1156 if (olddef
&& newdyn
)
1159 /* Allow changes between different types of funciton symbol. */
1160 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
1161 && bed
->is_function_type (h
->type
))
1162 *type_change_ok
= TRUE
;
1164 /* It's OK to change the type if either the existing symbol or the
1165 new symbol is weak. A type change is also OK if the old symbol
1166 is undefined and the new symbol is defined. */
1171 && h
->root
.type
== bfd_link_hash_undefined
))
1172 *type_change_ok
= TRUE
;
1174 /* It's OK to change the size if either the existing symbol or the
1175 new symbol is weak, or if the old symbol is undefined. */
1178 || h
->root
.type
== bfd_link_hash_undefined
)
1179 *size_change_ok
= TRUE
;
1181 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1182 symbol, respectively, appears to be a common symbol in a dynamic
1183 object. If a symbol appears in an uninitialized section, and is
1184 not weak, and is not a function, then it may be a common symbol
1185 which was resolved when the dynamic object was created. We want
1186 to treat such symbols specially, because they raise special
1187 considerations when setting the symbol size: if the symbol
1188 appears as a common symbol in a regular object, and the size in
1189 the regular object is larger, we must make sure that we use the
1190 larger size. This problematic case can always be avoided in C,
1191 but it must be handled correctly when using Fortran shared
1194 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1195 likewise for OLDDYNCOMMON and OLDDEF.
1197 Note that this test is just a heuristic, and that it is quite
1198 possible to have an uninitialized symbol in a shared object which
1199 is really a definition, rather than a common symbol. This could
1200 lead to some minor confusion when the symbol really is a common
1201 symbol in some regular object. However, I think it will be
1207 && (sec
->flags
& SEC_ALLOC
) != 0
1208 && (sec
->flags
& SEC_LOAD
) == 0
1210 && !bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
1211 newdyncommon
= TRUE
;
1213 newdyncommon
= FALSE
;
1217 && h
->root
.type
== bfd_link_hash_defined
1219 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1220 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1222 && !bed
->is_function_type (h
->type
))
1223 olddyncommon
= TRUE
;
1225 olddyncommon
= FALSE
;
1227 /* We now know everything about the old and new symbols. We ask the
1228 backend to check if we can merge them. */
1229 if (bed
->merge_symbol
1230 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1231 pold_alignment
, skip
, override
,
1232 type_change_ok
, size_change_ok
,
1233 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1235 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1239 /* If both the old and the new symbols look like common symbols in a
1240 dynamic object, set the size of the symbol to the larger of the
1245 && sym
->st_size
!= h
->size
)
1247 /* Since we think we have two common symbols, issue a multiple
1248 common warning if desired. Note that we only warn if the
1249 size is different. If the size is the same, we simply let
1250 the old symbol override the new one as normally happens with
1251 symbols defined in dynamic objects. */
1253 if (! ((*info
->callbacks
->multiple_common
)
1254 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1255 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1258 if (sym
->st_size
> h
->size
)
1259 h
->size
= sym
->st_size
;
1261 *size_change_ok
= TRUE
;
1264 /* If we are looking at a dynamic object, and we have found a
1265 definition, we need to see if the symbol was already defined by
1266 some other object. If so, we want to use the existing
1267 definition, and we do not want to report a multiple symbol
1268 definition error; we do this by clobbering *PSEC to be
1269 bfd_und_section_ptr.
1271 We treat a common symbol as a definition if the symbol in the
1272 shared library is a function, since common symbols always
1273 represent variables; this can cause confusion in principle, but
1274 any such confusion would seem to indicate an erroneous program or
1275 shared library. We also permit a common symbol in a regular
1276 object to override a weak symbol in a shared object. */
1281 || (h
->root
.type
== bfd_link_hash_common
1283 || bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))))))
1287 newdyncommon
= FALSE
;
1289 *psec
= sec
= bfd_und_section_ptr
;
1290 *size_change_ok
= TRUE
;
1292 /* If we get here when the old symbol is a common symbol, then
1293 we are explicitly letting it override a weak symbol or
1294 function in a dynamic object, and we don't want to warn about
1295 a type change. If the old symbol is a defined symbol, a type
1296 change warning may still be appropriate. */
1298 if (h
->root
.type
== bfd_link_hash_common
)
1299 *type_change_ok
= TRUE
;
1302 /* Handle the special case of an old common symbol merging with a
1303 new symbol which looks like a common symbol in a shared object.
1304 We change *PSEC and *PVALUE to make the new symbol look like a
1305 common symbol, and let _bfd_generic_link_add_one_symbol do the
1309 && h
->root
.type
== bfd_link_hash_common
)
1313 newdyncommon
= FALSE
;
1314 *pvalue
= sym
->st_size
;
1315 *psec
= sec
= bed
->common_section (oldsec
);
1316 *size_change_ok
= TRUE
;
1319 /* Skip weak definitions of symbols that are already defined. */
1320 if (newdef
&& olddef
&& newweak
)
1323 /* If the old symbol is from a dynamic object, and the new symbol is
1324 a definition which is not from a dynamic object, then the new
1325 symbol overrides the old symbol. Symbols from regular files
1326 always take precedence over symbols from dynamic objects, even if
1327 they are defined after the dynamic object in the link.
1329 As above, we again permit a common symbol in a regular object to
1330 override a definition in a shared object if the shared object
1331 symbol is a function or is weak. */
1336 || (bfd_is_com_section (sec
)
1338 || bed
->is_function_type (h
->type
))))
1343 /* Change the hash table entry to undefined, and let
1344 _bfd_generic_link_add_one_symbol do the right thing with the
1347 h
->root
.type
= bfd_link_hash_undefined
;
1348 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1349 *size_change_ok
= TRUE
;
1352 olddyncommon
= FALSE
;
1354 /* We again permit a type change when a common symbol may be
1355 overriding a function. */
1357 if (bfd_is_com_section (sec
))
1358 *type_change_ok
= TRUE
;
1360 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1363 /* This union may have been set to be non-NULL when this symbol
1364 was seen in a dynamic object. We must force the union to be
1365 NULL, so that it is correct for a regular symbol. */
1366 h
->verinfo
.vertree
= NULL
;
1369 /* Handle the special case of a new common symbol merging with an
1370 old symbol that looks like it might be a common symbol defined in
1371 a shared object. Note that we have already handled the case in
1372 which a new common symbol should simply override the definition
1373 in the shared library. */
1376 && bfd_is_com_section (sec
)
1379 /* It would be best if we could set the hash table entry to a
1380 common symbol, but we don't know what to use for the section
1381 or the alignment. */
1382 if (! ((*info
->callbacks
->multiple_common
)
1383 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1384 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1387 /* If the presumed common symbol in the dynamic object is
1388 larger, pretend that the new symbol has its size. */
1390 if (h
->size
> *pvalue
)
1393 /* We need to remember the alignment required by the symbol
1394 in the dynamic object. */
1395 BFD_ASSERT (pold_alignment
);
1396 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1399 olddyncommon
= FALSE
;
1401 h
->root
.type
= bfd_link_hash_undefined
;
1402 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1404 *size_change_ok
= TRUE
;
1405 *type_change_ok
= TRUE
;
1407 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1410 h
->verinfo
.vertree
= NULL
;
1415 /* Handle the case where we had a versioned symbol in a dynamic
1416 library and now find a definition in a normal object. In this
1417 case, we make the versioned symbol point to the normal one. */
1418 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1419 flip
->root
.type
= h
->root
.type
;
1420 h
->root
.type
= bfd_link_hash_indirect
;
1421 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1422 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1423 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1427 flip
->ref_dynamic
= 1;
1434 /* This function is called to create an indirect symbol from the
1435 default for the symbol with the default version if needed. The
1436 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1437 set DYNSYM if the new indirect symbol is dynamic. */
1440 _bfd_elf_add_default_symbol (bfd
*abfd
,
1441 struct bfd_link_info
*info
,
1442 struct elf_link_hash_entry
*h
,
1444 Elf_Internal_Sym
*sym
,
1447 bfd_boolean
*dynsym
,
1448 bfd_boolean override
)
1450 bfd_boolean type_change_ok
;
1451 bfd_boolean size_change_ok
;
1454 struct elf_link_hash_entry
*hi
;
1455 struct bfd_link_hash_entry
*bh
;
1456 const struct elf_backend_data
*bed
;
1457 bfd_boolean collect
;
1458 bfd_boolean dynamic
;
1460 size_t len
, shortlen
;
1463 /* If this symbol has a version, and it is the default version, we
1464 create an indirect symbol from the default name to the fully
1465 decorated name. This will cause external references which do not
1466 specify a version to be bound to this version of the symbol. */
1467 p
= strchr (name
, ELF_VER_CHR
);
1468 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1473 /* We are overridden by an old definition. We need to check if we
1474 need to create the indirect symbol from the default name. */
1475 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1477 BFD_ASSERT (hi
!= NULL
);
1480 while (hi
->root
.type
== bfd_link_hash_indirect
1481 || hi
->root
.type
== bfd_link_hash_warning
)
1483 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1489 bed
= get_elf_backend_data (abfd
);
1490 collect
= bed
->collect
;
1491 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1493 shortlen
= p
- name
;
1494 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1495 if (shortname
== NULL
)
1497 memcpy (shortname
, name
, shortlen
);
1498 shortname
[shortlen
] = '\0';
1500 /* We are going to create a new symbol. Merge it with any existing
1501 symbol with this name. For the purposes of the merge, act as
1502 though we were defining the symbol we just defined, although we
1503 actually going to define an indirect symbol. */
1504 type_change_ok
= FALSE
;
1505 size_change_ok
= FALSE
;
1507 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1508 NULL
, &hi
, &skip
, &override
,
1509 &type_change_ok
, &size_change_ok
))
1518 if (! (_bfd_generic_link_add_one_symbol
1519 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1520 0, name
, FALSE
, collect
, &bh
)))
1522 hi
= (struct elf_link_hash_entry
*) bh
;
1526 /* In this case the symbol named SHORTNAME is overriding the
1527 indirect symbol we want to add. We were planning on making
1528 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1529 is the name without a version. NAME is the fully versioned
1530 name, and it is the default version.
1532 Overriding means that we already saw a definition for the
1533 symbol SHORTNAME in a regular object, and it is overriding
1534 the symbol defined in the dynamic object.
1536 When this happens, we actually want to change NAME, the
1537 symbol we just added, to refer to SHORTNAME. This will cause
1538 references to NAME in the shared object to become references
1539 to SHORTNAME in the regular object. This is what we expect
1540 when we override a function in a shared object: that the
1541 references in the shared object will be mapped to the
1542 definition in the regular object. */
1544 while (hi
->root
.type
== bfd_link_hash_indirect
1545 || hi
->root
.type
== bfd_link_hash_warning
)
1546 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1548 h
->root
.type
= bfd_link_hash_indirect
;
1549 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1553 hi
->ref_dynamic
= 1;
1557 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1562 /* Now set HI to H, so that the following code will set the
1563 other fields correctly. */
1567 /* Check if HI is a warning symbol. */
1568 if (hi
->root
.type
== bfd_link_hash_warning
)
1569 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1571 /* If there is a duplicate definition somewhere, then HI may not
1572 point to an indirect symbol. We will have reported an error to
1573 the user in that case. */
1575 if (hi
->root
.type
== bfd_link_hash_indirect
)
1577 struct elf_link_hash_entry
*ht
;
1579 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1580 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1582 /* See if the new flags lead us to realize that the symbol must
1594 if (hi
->ref_regular
)
1600 /* We also need to define an indirection from the nondefault version
1604 len
= strlen (name
);
1605 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1606 if (shortname
== NULL
)
1608 memcpy (shortname
, name
, shortlen
);
1609 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1611 /* Once again, merge with any existing symbol. */
1612 type_change_ok
= FALSE
;
1613 size_change_ok
= FALSE
;
1615 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1616 NULL
, &hi
, &skip
, &override
,
1617 &type_change_ok
, &size_change_ok
))
1625 /* Here SHORTNAME is a versioned name, so we don't expect to see
1626 the type of override we do in the case above unless it is
1627 overridden by a versioned definition. */
1628 if (hi
->root
.type
!= bfd_link_hash_defined
1629 && hi
->root
.type
!= bfd_link_hash_defweak
)
1630 (*_bfd_error_handler
)
1631 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1637 if (! (_bfd_generic_link_add_one_symbol
1638 (info
, abfd
, shortname
, BSF_INDIRECT
,
1639 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1641 hi
= (struct elf_link_hash_entry
*) bh
;
1643 /* If there is a duplicate definition somewhere, then HI may not
1644 point to an indirect symbol. We will have reported an error
1645 to the user in that case. */
1647 if (hi
->root
.type
== bfd_link_hash_indirect
)
1649 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1651 /* See if the new flags lead us to realize that the symbol
1663 if (hi
->ref_regular
)
1673 /* This routine is used to export all defined symbols into the dynamic
1674 symbol table. It is called via elf_link_hash_traverse. */
1677 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1679 struct elf_info_failed
*eif
= data
;
1681 /* Ignore this if we won't export it. */
1682 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1685 /* Ignore indirect symbols. These are added by the versioning code. */
1686 if (h
->root
.type
== bfd_link_hash_indirect
)
1689 if (h
->root
.type
== bfd_link_hash_warning
)
1690 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1692 if (h
->dynindx
== -1
1696 struct bfd_elf_version_tree
*t
;
1697 struct bfd_elf_version_expr
*d
;
1699 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1701 if (t
->globals
.list
!= NULL
)
1703 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1708 if (t
->locals
.list
!= NULL
)
1710 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1719 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1730 /* Look through the symbols which are defined in other shared
1731 libraries and referenced here. Update the list of version
1732 dependencies. This will be put into the .gnu.version_r section.
1733 This function is called via elf_link_hash_traverse. */
1736 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1739 struct elf_find_verdep_info
*rinfo
= data
;
1740 Elf_Internal_Verneed
*t
;
1741 Elf_Internal_Vernaux
*a
;
1744 if (h
->root
.type
== bfd_link_hash_warning
)
1745 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1747 /* We only care about symbols defined in shared objects with version
1752 || h
->verinfo
.verdef
== NULL
)
1755 /* See if we already know about this version. */
1756 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1758 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1761 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1762 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1768 /* This is a new version. Add it to tree we are building. */
1773 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1776 rinfo
->failed
= TRUE
;
1780 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1781 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1782 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1786 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1788 /* Note that we are copying a string pointer here, and testing it
1789 above. If bfd_elf_string_from_elf_section is ever changed to
1790 discard the string data when low in memory, this will have to be
1792 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1794 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1795 a
->vna_nextptr
= t
->vn_auxptr
;
1797 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1800 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1807 /* Figure out appropriate versions for all the symbols. We may not
1808 have the version number script until we have read all of the input
1809 files, so until that point we don't know which symbols should be
1810 local. This function is called via elf_link_hash_traverse. */
1813 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1815 struct elf_assign_sym_version_info
*sinfo
;
1816 struct bfd_link_info
*info
;
1817 const struct elf_backend_data
*bed
;
1818 struct elf_info_failed eif
;
1825 if (h
->root
.type
== bfd_link_hash_warning
)
1826 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1828 /* Fix the symbol flags. */
1831 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1834 sinfo
->failed
= TRUE
;
1838 /* We only need version numbers for symbols defined in regular
1840 if (!h
->def_regular
)
1843 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1844 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1845 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1847 struct bfd_elf_version_tree
*t
;
1852 /* There are two consecutive ELF_VER_CHR characters if this is
1853 not a hidden symbol. */
1855 if (*p
== ELF_VER_CHR
)
1861 /* If there is no version string, we can just return out. */
1869 /* Look for the version. If we find it, it is no longer weak. */
1870 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1872 if (strcmp (t
->name
, p
) == 0)
1876 struct bfd_elf_version_expr
*d
;
1878 len
= p
- h
->root
.root
.string
;
1879 alc
= bfd_malloc (len
);
1882 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1883 alc
[len
- 1] = '\0';
1884 if (alc
[len
- 2] == ELF_VER_CHR
)
1885 alc
[len
- 2] = '\0';
1887 h
->verinfo
.vertree
= t
;
1891 if (t
->globals
.list
!= NULL
)
1892 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1894 /* See if there is anything to force this symbol to
1896 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1898 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1901 && ! info
->export_dynamic
)
1902 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1910 /* If we are building an application, we need to create a
1911 version node for this version. */
1912 if (t
== NULL
&& info
->executable
)
1914 struct bfd_elf_version_tree
**pp
;
1917 /* If we aren't going to export this symbol, we don't need
1918 to worry about it. */
1919 if (h
->dynindx
== -1)
1923 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1926 sinfo
->failed
= TRUE
;
1931 t
->name_indx
= (unsigned int) -1;
1935 /* Don't count anonymous version tag. */
1936 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1938 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1940 t
->vernum
= version_index
;
1944 h
->verinfo
.vertree
= t
;
1948 /* We could not find the version for a symbol when
1949 generating a shared archive. Return an error. */
1950 (*_bfd_error_handler
)
1951 (_("%B: version node not found for symbol %s"),
1952 sinfo
->output_bfd
, h
->root
.root
.string
);
1953 bfd_set_error (bfd_error_bad_value
);
1954 sinfo
->failed
= TRUE
;
1962 /* If we don't have a version for this symbol, see if we can find
1964 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1966 struct bfd_elf_version_tree
*t
;
1967 struct bfd_elf_version_tree
*local_ver
;
1968 struct bfd_elf_version_expr
*d
;
1970 /* See if can find what version this symbol is in. If the
1971 symbol is supposed to be local, then don't actually register
1974 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1976 if (t
->globals
.list
!= NULL
)
1978 bfd_boolean matched
;
1982 while ((d
= (*t
->match
) (&t
->globals
, d
,
1983 h
->root
.root
.string
)) != NULL
)
1988 /* There is a version without definition. Make
1989 the symbol the default definition for this
1991 h
->verinfo
.vertree
= t
;
1999 /* There is no undefined version for this symbol. Hide the
2001 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2004 if (t
->locals
.list
!= NULL
)
2007 while ((d
= (*t
->match
) (&t
->locals
, d
,
2008 h
->root
.root
.string
)) != NULL
)
2011 /* If the match is "*", keep looking for a more
2012 explicit, perhaps even global, match.
2013 XXX: Shouldn't this be !d->wildcard instead? */
2014 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2023 if (local_ver
!= NULL
)
2025 h
->verinfo
.vertree
= local_ver
;
2026 if (h
->dynindx
!= -1
2027 && ! info
->export_dynamic
)
2029 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2037 /* Read and swap the relocs from the section indicated by SHDR. This
2038 may be either a REL or a RELA section. The relocations are
2039 translated into RELA relocations and stored in INTERNAL_RELOCS,
2040 which should have already been allocated to contain enough space.
2041 The EXTERNAL_RELOCS are a buffer where the external form of the
2042 relocations should be stored.
2044 Returns FALSE if something goes wrong. */
2047 elf_link_read_relocs_from_section (bfd
*abfd
,
2049 Elf_Internal_Shdr
*shdr
,
2050 void *external_relocs
,
2051 Elf_Internal_Rela
*internal_relocs
)
2053 const struct elf_backend_data
*bed
;
2054 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2055 const bfd_byte
*erela
;
2056 const bfd_byte
*erelaend
;
2057 Elf_Internal_Rela
*irela
;
2058 Elf_Internal_Shdr
*symtab_hdr
;
2061 /* Position ourselves at the start of the section. */
2062 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2065 /* Read the relocations. */
2066 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2069 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2070 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2072 bed
= get_elf_backend_data (abfd
);
2074 /* Convert the external relocations to the internal format. */
2075 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2076 swap_in
= bed
->s
->swap_reloc_in
;
2077 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2078 swap_in
= bed
->s
->swap_reloca_in
;
2081 bfd_set_error (bfd_error_wrong_format
);
2085 erela
= external_relocs
;
2086 erelaend
= erela
+ shdr
->sh_size
;
2087 irela
= internal_relocs
;
2088 while (erela
< erelaend
)
2092 (*swap_in
) (abfd
, erela
, irela
);
2093 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2094 if (bed
->s
->arch_size
== 64)
2096 if ((size_t) r_symndx
>= nsyms
)
2098 (*_bfd_error_handler
)
2099 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2100 " for offset 0x%lx in section `%A'"),
2102 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2103 bfd_set_error (bfd_error_bad_value
);
2106 irela
+= bed
->s
->int_rels_per_ext_rel
;
2107 erela
+= shdr
->sh_entsize
;
2113 /* Read and swap the relocs for a section O. They may have been
2114 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2115 not NULL, they are used as buffers to read into. They are known to
2116 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2117 the return value is allocated using either malloc or bfd_alloc,
2118 according to the KEEP_MEMORY argument. If O has two relocation
2119 sections (both REL and RELA relocations), then the REL_HDR
2120 relocations will appear first in INTERNAL_RELOCS, followed by the
2121 REL_HDR2 relocations. */
2124 _bfd_elf_link_read_relocs (bfd
*abfd
,
2126 void *external_relocs
,
2127 Elf_Internal_Rela
*internal_relocs
,
2128 bfd_boolean keep_memory
)
2130 Elf_Internal_Shdr
*rel_hdr
;
2131 void *alloc1
= NULL
;
2132 Elf_Internal_Rela
*alloc2
= NULL
;
2133 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2135 if (elf_section_data (o
)->relocs
!= NULL
)
2136 return elf_section_data (o
)->relocs
;
2138 if (o
->reloc_count
== 0)
2141 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2143 if (internal_relocs
== NULL
)
2147 size
= o
->reloc_count
;
2148 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2150 internal_relocs
= bfd_alloc (abfd
, size
);
2152 internal_relocs
= alloc2
= bfd_malloc (size
);
2153 if (internal_relocs
== NULL
)
2157 if (external_relocs
== NULL
)
2159 bfd_size_type size
= rel_hdr
->sh_size
;
2161 if (elf_section_data (o
)->rel_hdr2
)
2162 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2163 alloc1
= bfd_malloc (size
);
2166 external_relocs
= alloc1
;
2169 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2173 if (elf_section_data (o
)->rel_hdr2
2174 && (!elf_link_read_relocs_from_section
2176 elf_section_data (o
)->rel_hdr2
,
2177 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2178 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2179 * bed
->s
->int_rels_per_ext_rel
))))
2182 /* Cache the results for next time, if we can. */
2184 elf_section_data (o
)->relocs
= internal_relocs
;
2189 /* Don't free alloc2, since if it was allocated we are passing it
2190 back (under the name of internal_relocs). */
2192 return internal_relocs
;
2202 /* Compute the size of, and allocate space for, REL_HDR which is the
2203 section header for a section containing relocations for O. */
2206 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2207 Elf_Internal_Shdr
*rel_hdr
,
2210 bfd_size_type reloc_count
;
2211 bfd_size_type num_rel_hashes
;
2213 /* Figure out how many relocations there will be. */
2214 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2215 reloc_count
= elf_section_data (o
)->rel_count
;
2217 reloc_count
= elf_section_data (o
)->rel_count2
;
2219 num_rel_hashes
= o
->reloc_count
;
2220 if (num_rel_hashes
< reloc_count
)
2221 num_rel_hashes
= reloc_count
;
2223 /* That allows us to calculate the size of the section. */
2224 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2226 /* The contents field must last into write_object_contents, so we
2227 allocate it with bfd_alloc rather than malloc. Also since we
2228 cannot be sure that the contents will actually be filled in,
2229 we zero the allocated space. */
2230 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2231 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2234 /* We only allocate one set of hash entries, so we only do it the
2235 first time we are called. */
2236 if (elf_section_data (o
)->rel_hashes
== NULL
2239 struct elf_link_hash_entry
**p
;
2241 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2245 elf_section_data (o
)->rel_hashes
= p
;
2251 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2252 originated from the section given by INPUT_REL_HDR) to the
2256 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2257 asection
*input_section
,
2258 Elf_Internal_Shdr
*input_rel_hdr
,
2259 Elf_Internal_Rela
*internal_relocs
,
2260 struct elf_link_hash_entry
**rel_hash
2263 Elf_Internal_Rela
*irela
;
2264 Elf_Internal_Rela
*irelaend
;
2266 Elf_Internal_Shdr
*output_rel_hdr
;
2267 asection
*output_section
;
2268 unsigned int *rel_countp
= NULL
;
2269 const struct elf_backend_data
*bed
;
2270 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2272 output_section
= input_section
->output_section
;
2273 output_rel_hdr
= NULL
;
2275 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2276 == input_rel_hdr
->sh_entsize
)
2278 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2279 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2281 else if (elf_section_data (output_section
)->rel_hdr2
2282 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2283 == input_rel_hdr
->sh_entsize
))
2285 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2286 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2290 (*_bfd_error_handler
)
2291 (_("%B: relocation size mismatch in %B section %A"),
2292 output_bfd
, input_section
->owner
, input_section
);
2293 bfd_set_error (bfd_error_wrong_object_format
);
2297 bed
= get_elf_backend_data (output_bfd
);
2298 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2299 swap_out
= bed
->s
->swap_reloc_out
;
2300 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2301 swap_out
= bed
->s
->swap_reloca_out
;
2305 erel
= output_rel_hdr
->contents
;
2306 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2307 irela
= internal_relocs
;
2308 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2309 * bed
->s
->int_rels_per_ext_rel
);
2310 while (irela
< irelaend
)
2312 (*swap_out
) (output_bfd
, irela
, erel
);
2313 irela
+= bed
->s
->int_rels_per_ext_rel
;
2314 erel
+= input_rel_hdr
->sh_entsize
;
2317 /* Bump the counter, so that we know where to add the next set of
2319 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2324 /* Make weak undefined symbols in PIE dynamic. */
2327 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2328 struct elf_link_hash_entry
*h
)
2332 && h
->root
.type
== bfd_link_hash_undefweak
)
2333 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2338 /* Fix up the flags for a symbol. This handles various cases which
2339 can only be fixed after all the input files are seen. This is
2340 currently called by both adjust_dynamic_symbol and
2341 assign_sym_version, which is unnecessary but perhaps more robust in
2342 the face of future changes. */
2345 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2346 struct elf_info_failed
*eif
)
2348 const struct elf_backend_data
*bed
= NULL
;
2350 /* If this symbol was mentioned in a non-ELF file, try to set
2351 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2352 permit a non-ELF file to correctly refer to a symbol defined in
2353 an ELF dynamic object. */
2356 while (h
->root
.type
== bfd_link_hash_indirect
)
2357 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2359 if (h
->root
.type
!= bfd_link_hash_defined
2360 && h
->root
.type
!= bfd_link_hash_defweak
)
2363 h
->ref_regular_nonweak
= 1;
2367 if (h
->root
.u
.def
.section
->owner
!= NULL
2368 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2369 == bfd_target_elf_flavour
))
2372 h
->ref_regular_nonweak
= 1;
2378 if (h
->dynindx
== -1
2382 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2391 /* Unfortunately, NON_ELF is only correct if the symbol
2392 was first seen in a non-ELF file. Fortunately, if the symbol
2393 was first seen in an ELF file, we're probably OK unless the
2394 symbol was defined in a non-ELF file. Catch that case here.
2395 FIXME: We're still in trouble if the symbol was first seen in
2396 a dynamic object, and then later in a non-ELF regular object. */
2397 if ((h
->root
.type
== bfd_link_hash_defined
2398 || h
->root
.type
== bfd_link_hash_defweak
)
2400 && (h
->root
.u
.def
.section
->owner
!= NULL
2401 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2402 != bfd_target_elf_flavour
)
2403 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2404 && !h
->def_dynamic
)))
2408 /* Backend specific symbol fixup. */
2409 if (elf_hash_table (eif
->info
)->dynobj
)
2411 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2412 if (bed
->elf_backend_fixup_symbol
2413 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2417 /* If this is a final link, and the symbol was defined as a common
2418 symbol in a regular object file, and there was no definition in
2419 any dynamic object, then the linker will have allocated space for
2420 the symbol in a common section but the DEF_REGULAR
2421 flag will not have been set. */
2422 if (h
->root
.type
== bfd_link_hash_defined
2426 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2429 /* If -Bsymbolic was used (which means to bind references to global
2430 symbols to the definition within the shared object), and this
2431 symbol was defined in a regular object, then it actually doesn't
2432 need a PLT entry. Likewise, if the symbol has non-default
2433 visibility. If the symbol has hidden or internal visibility, we
2434 will force it local. */
2436 && eif
->info
->shared
2437 && is_elf_hash_table (eif
->info
->hash
)
2438 && (SYMBOLIC_BIND (eif
->info
, h
)
2439 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2442 bfd_boolean force_local
;
2444 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2445 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2446 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2449 /* If a weak undefined symbol has non-default visibility, we also
2450 hide it from the dynamic linker. */
2451 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2452 && h
->root
.type
== bfd_link_hash_undefweak
)
2454 const struct elf_backend_data
*bed
;
2455 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2456 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2459 /* If this is a weak defined symbol in a dynamic object, and we know
2460 the real definition in the dynamic object, copy interesting flags
2461 over to the real definition. */
2462 if (h
->u
.weakdef
!= NULL
)
2464 struct elf_link_hash_entry
*weakdef
;
2466 weakdef
= h
->u
.weakdef
;
2467 if (h
->root
.type
== bfd_link_hash_indirect
)
2468 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2470 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2471 || h
->root
.type
== bfd_link_hash_defweak
);
2472 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2473 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2474 BFD_ASSERT (weakdef
->def_dynamic
);
2476 /* If the real definition is defined by a regular object file,
2477 don't do anything special. See the longer description in
2478 _bfd_elf_adjust_dynamic_symbol, below. */
2479 if (weakdef
->def_regular
)
2480 h
->u
.weakdef
= NULL
;
2482 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2489 /* Make the backend pick a good value for a dynamic symbol. This is
2490 called via elf_link_hash_traverse, and also calls itself
2494 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2496 struct elf_info_failed
*eif
= data
;
2498 const struct elf_backend_data
*bed
;
2500 if (! is_elf_hash_table (eif
->info
->hash
))
2503 if (h
->root
.type
== bfd_link_hash_warning
)
2505 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2506 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2508 /* When warning symbols are created, they **replace** the "real"
2509 entry in the hash table, thus we never get to see the real
2510 symbol in a hash traversal. So look at it now. */
2511 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2514 /* Ignore indirect symbols. These are added by the versioning code. */
2515 if (h
->root
.type
== bfd_link_hash_indirect
)
2518 /* Fix the symbol flags. */
2519 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2522 /* If this symbol does not require a PLT entry, and it is not
2523 defined by a dynamic object, or is not referenced by a regular
2524 object, ignore it. We do have to handle a weak defined symbol,
2525 even if no regular object refers to it, if we decided to add it
2526 to the dynamic symbol table. FIXME: Do we normally need to worry
2527 about symbols which are defined by one dynamic object and
2528 referenced by another one? */
2533 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2535 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2539 /* If we've already adjusted this symbol, don't do it again. This
2540 can happen via a recursive call. */
2541 if (h
->dynamic_adjusted
)
2544 /* Don't look at this symbol again. Note that we must set this
2545 after checking the above conditions, because we may look at a
2546 symbol once, decide not to do anything, and then get called
2547 recursively later after REF_REGULAR is set below. */
2548 h
->dynamic_adjusted
= 1;
2550 /* If this is a weak definition, and we know a real definition, and
2551 the real symbol is not itself defined by a regular object file,
2552 then get a good value for the real definition. We handle the
2553 real symbol first, for the convenience of the backend routine.
2555 Note that there is a confusing case here. If the real definition
2556 is defined by a regular object file, we don't get the real symbol
2557 from the dynamic object, but we do get the weak symbol. If the
2558 processor backend uses a COPY reloc, then if some routine in the
2559 dynamic object changes the real symbol, we will not see that
2560 change in the corresponding weak symbol. This is the way other
2561 ELF linkers work as well, and seems to be a result of the shared
2564 I will clarify this issue. Most SVR4 shared libraries define the
2565 variable _timezone and define timezone as a weak synonym. The
2566 tzset call changes _timezone. If you write
2567 extern int timezone;
2569 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2570 you might expect that, since timezone is a synonym for _timezone,
2571 the same number will print both times. However, if the processor
2572 backend uses a COPY reloc, then actually timezone will be copied
2573 into your process image, and, since you define _timezone
2574 yourself, _timezone will not. Thus timezone and _timezone will
2575 wind up at different memory locations. The tzset call will set
2576 _timezone, leaving timezone unchanged. */
2578 if (h
->u
.weakdef
!= NULL
)
2580 /* If we get to this point, we know there is an implicit
2581 reference by a regular object file via the weak symbol H.
2582 FIXME: Is this really true? What if the traversal finds
2583 H->U.WEAKDEF before it finds H? */
2584 h
->u
.weakdef
->ref_regular
= 1;
2586 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2590 /* If a symbol has no type and no size and does not require a PLT
2591 entry, then we are probably about to do the wrong thing here: we
2592 are probably going to create a COPY reloc for an empty object.
2593 This case can arise when a shared object is built with assembly
2594 code, and the assembly code fails to set the symbol type. */
2596 && h
->type
== STT_NOTYPE
2598 (*_bfd_error_handler
)
2599 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2600 h
->root
.root
.string
);
2602 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2603 bed
= get_elf_backend_data (dynobj
);
2604 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2613 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2617 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2620 unsigned int power_of_two
;
2622 asection
*sec
= h
->root
.u
.def
.section
;
2624 /* The section aligment of definition is the maximum alignment
2625 requirement of symbols defined in the section. Since we don't
2626 know the symbol alignment requirement, we start with the
2627 maximum alignment and check low bits of the symbol address
2628 for the minimum alignment. */
2629 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2630 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2631 while ((h
->root
.u
.def
.value
& mask
) != 0)
2637 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2640 /* Adjust the section alignment if needed. */
2641 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2646 /* We make sure that the symbol will be aligned properly. */
2647 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2649 /* Define the symbol as being at this point in DYNBSS. */
2650 h
->root
.u
.def
.section
= dynbss
;
2651 h
->root
.u
.def
.value
= dynbss
->size
;
2653 /* Increment the size of DYNBSS to make room for the symbol. */
2654 dynbss
->size
+= h
->size
;
2659 /* Adjust all external symbols pointing into SEC_MERGE sections
2660 to reflect the object merging within the sections. */
2663 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2667 if (h
->root
.type
== bfd_link_hash_warning
)
2668 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2670 if ((h
->root
.type
== bfd_link_hash_defined
2671 || h
->root
.type
== bfd_link_hash_defweak
)
2672 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2673 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2675 bfd
*output_bfd
= data
;
2677 h
->root
.u
.def
.value
=
2678 _bfd_merged_section_offset (output_bfd
,
2679 &h
->root
.u
.def
.section
,
2680 elf_section_data (sec
)->sec_info
,
2681 h
->root
.u
.def
.value
);
2687 /* Returns false if the symbol referred to by H should be considered
2688 to resolve local to the current module, and true if it should be
2689 considered to bind dynamically. */
2692 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2693 struct bfd_link_info
*info
,
2694 bfd_boolean ignore_protected
)
2696 bfd_boolean binding_stays_local_p
;
2697 const struct elf_backend_data
*bed
;
2698 struct elf_link_hash_table
*hash_table
;
2703 while (h
->root
.type
== bfd_link_hash_indirect
2704 || h
->root
.type
== bfd_link_hash_warning
)
2705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2707 /* If it was forced local, then clearly it's not dynamic. */
2708 if (h
->dynindx
== -1)
2710 if (h
->forced_local
)
2713 /* Identify the cases where name binding rules say that a
2714 visible symbol resolves locally. */
2715 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2717 switch (ELF_ST_VISIBILITY (h
->other
))
2724 hash_table
= elf_hash_table (info
);
2725 if (!is_elf_hash_table (hash_table
))
2728 bed
= get_elf_backend_data (hash_table
->dynobj
);
2730 /* Proper resolution for function pointer equality may require
2731 that these symbols perhaps be resolved dynamically, even though
2732 we should be resolving them to the current module. */
2733 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2734 binding_stays_local_p
= TRUE
;
2741 /* If it isn't defined locally, then clearly it's dynamic. */
2742 if (!h
->def_regular
)
2745 /* Otherwise, the symbol is dynamic if binding rules don't tell
2746 us that it remains local. */
2747 return !binding_stays_local_p
;
2750 /* Return true if the symbol referred to by H should be considered
2751 to resolve local to the current module, and false otherwise. Differs
2752 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2753 undefined symbols and weak symbols. */
2756 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2757 struct bfd_link_info
*info
,
2758 bfd_boolean local_protected
)
2760 const struct elf_backend_data
*bed
;
2761 struct elf_link_hash_table
*hash_table
;
2763 /* If it's a local sym, of course we resolve locally. */
2767 /* Common symbols that become definitions don't get the DEF_REGULAR
2768 flag set, so test it first, and don't bail out. */
2769 if (ELF_COMMON_DEF_P (h
))
2771 /* If we don't have a definition in a regular file, then we can't
2772 resolve locally. The sym is either undefined or dynamic. */
2773 else if (!h
->def_regular
)
2776 /* Forced local symbols resolve locally. */
2777 if (h
->forced_local
)
2780 /* As do non-dynamic symbols. */
2781 if (h
->dynindx
== -1)
2784 /* At this point, we know the symbol is defined and dynamic. In an
2785 executable it must resolve locally, likewise when building symbolic
2786 shared libraries. */
2787 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2790 /* Now deal with defined dynamic symbols in shared libraries. Ones
2791 with default visibility might not resolve locally. */
2792 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2795 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2796 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2799 hash_table
= elf_hash_table (info
);
2800 if (!is_elf_hash_table (hash_table
))
2803 bed
= get_elf_backend_data (hash_table
->dynobj
);
2805 /* STV_PROTECTED non-function symbols are local. */
2806 if (!bed
->is_function_type (h
->type
))
2809 /* Function pointer equality tests may require that STV_PROTECTED
2810 symbols be treated as dynamic symbols, even when we know that the
2811 dynamic linker will resolve them locally. */
2812 return local_protected
;
2815 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2816 aligned. Returns the first TLS output section. */
2818 struct bfd_section
*
2819 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2821 struct bfd_section
*sec
, *tls
;
2822 unsigned int align
= 0;
2824 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2825 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2829 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2830 if (sec
->alignment_power
> align
)
2831 align
= sec
->alignment_power
;
2833 elf_hash_table (info
)->tls_sec
= tls
;
2835 /* Ensure the alignment of the first section is the largest alignment,
2836 so that the tls segment starts aligned. */
2838 tls
->alignment_power
= align
;
2843 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2845 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2846 Elf_Internal_Sym
*sym
)
2848 const struct elf_backend_data
*bed
;
2850 /* Local symbols do not count, but target specific ones might. */
2851 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2852 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2855 bed
= get_elf_backend_data (abfd
);
2856 /* Function symbols do not count. */
2857 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2860 /* If the section is undefined, then so is the symbol. */
2861 if (sym
->st_shndx
== SHN_UNDEF
)
2864 /* If the symbol is defined in the common section, then
2865 it is a common definition and so does not count. */
2866 if (bed
->common_definition (sym
))
2869 /* If the symbol is in a target specific section then we
2870 must rely upon the backend to tell us what it is. */
2871 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2872 /* FIXME - this function is not coded yet:
2874 return _bfd_is_global_symbol_definition (abfd, sym);
2876 Instead for now assume that the definition is not global,
2877 Even if this is wrong, at least the linker will behave
2878 in the same way that it used to do. */
2884 /* Search the symbol table of the archive element of the archive ABFD
2885 whose archive map contains a mention of SYMDEF, and determine if
2886 the symbol is defined in this element. */
2888 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2890 Elf_Internal_Shdr
* hdr
;
2891 bfd_size_type symcount
;
2892 bfd_size_type extsymcount
;
2893 bfd_size_type extsymoff
;
2894 Elf_Internal_Sym
*isymbuf
;
2895 Elf_Internal_Sym
*isym
;
2896 Elf_Internal_Sym
*isymend
;
2899 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2903 if (! bfd_check_format (abfd
, bfd_object
))
2906 /* If we have already included the element containing this symbol in the
2907 link then we do not need to include it again. Just claim that any symbol
2908 it contains is not a definition, so that our caller will not decide to
2909 (re)include this element. */
2910 if (abfd
->archive_pass
)
2913 /* Select the appropriate symbol table. */
2914 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2915 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2917 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2919 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2921 /* The sh_info field of the symtab header tells us where the
2922 external symbols start. We don't care about the local symbols. */
2923 if (elf_bad_symtab (abfd
))
2925 extsymcount
= symcount
;
2930 extsymcount
= symcount
- hdr
->sh_info
;
2931 extsymoff
= hdr
->sh_info
;
2934 if (extsymcount
== 0)
2937 /* Read in the symbol table. */
2938 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2940 if (isymbuf
== NULL
)
2943 /* Scan the symbol table looking for SYMDEF. */
2945 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2949 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2954 if (strcmp (name
, symdef
->name
) == 0)
2956 result
= is_global_data_symbol_definition (abfd
, isym
);
2966 /* Add an entry to the .dynamic table. */
2969 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2973 struct elf_link_hash_table
*hash_table
;
2974 const struct elf_backend_data
*bed
;
2976 bfd_size_type newsize
;
2977 bfd_byte
*newcontents
;
2978 Elf_Internal_Dyn dyn
;
2980 hash_table
= elf_hash_table (info
);
2981 if (! is_elf_hash_table (hash_table
))
2984 bed
= get_elf_backend_data (hash_table
->dynobj
);
2985 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2986 BFD_ASSERT (s
!= NULL
);
2988 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2989 newcontents
= bfd_realloc (s
->contents
, newsize
);
2990 if (newcontents
== NULL
)
2994 dyn
.d_un
.d_val
= val
;
2995 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2998 s
->contents
= newcontents
;
3003 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3004 otherwise just check whether one already exists. Returns -1 on error,
3005 1 if a DT_NEEDED tag already exists, and 0 on success. */
3008 elf_add_dt_needed_tag (bfd
*abfd
,
3009 struct bfd_link_info
*info
,
3013 struct elf_link_hash_table
*hash_table
;
3014 bfd_size_type oldsize
;
3015 bfd_size_type strindex
;
3017 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3020 hash_table
= elf_hash_table (info
);
3021 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3022 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3023 if (strindex
== (bfd_size_type
) -1)
3026 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3029 const struct elf_backend_data
*bed
;
3032 bed
= get_elf_backend_data (hash_table
->dynobj
);
3033 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3035 for (extdyn
= sdyn
->contents
;
3036 extdyn
< sdyn
->contents
+ sdyn
->size
;
3037 extdyn
+= bed
->s
->sizeof_dyn
)
3039 Elf_Internal_Dyn dyn
;
3041 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3042 if (dyn
.d_tag
== DT_NEEDED
3043 && dyn
.d_un
.d_val
== strindex
)
3045 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3053 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3056 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3060 /* We were just checking for existence of the tag. */
3061 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3066 /* Sort symbol by value and section. */
3068 elf_sort_symbol (const void *arg1
, const void *arg2
)
3070 const struct elf_link_hash_entry
*h1
;
3071 const struct elf_link_hash_entry
*h2
;
3072 bfd_signed_vma vdiff
;
3074 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3075 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3076 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3078 return vdiff
> 0 ? 1 : -1;
3081 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3083 return sdiff
> 0 ? 1 : -1;
3088 /* This function is used to adjust offsets into .dynstr for
3089 dynamic symbols. This is called via elf_link_hash_traverse. */
3092 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3094 struct elf_strtab_hash
*dynstr
= data
;
3096 if (h
->root
.type
== bfd_link_hash_warning
)
3097 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3099 if (h
->dynindx
!= -1)
3100 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3104 /* Assign string offsets in .dynstr, update all structures referencing
3108 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3110 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3111 struct elf_link_local_dynamic_entry
*entry
;
3112 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3113 bfd
*dynobj
= hash_table
->dynobj
;
3116 const struct elf_backend_data
*bed
;
3119 _bfd_elf_strtab_finalize (dynstr
);
3120 size
= _bfd_elf_strtab_size (dynstr
);
3122 bed
= get_elf_backend_data (dynobj
);
3123 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3124 BFD_ASSERT (sdyn
!= NULL
);
3126 /* Update all .dynamic entries referencing .dynstr strings. */
3127 for (extdyn
= sdyn
->contents
;
3128 extdyn
< sdyn
->contents
+ sdyn
->size
;
3129 extdyn
+= bed
->s
->sizeof_dyn
)
3131 Elf_Internal_Dyn dyn
;
3133 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3137 dyn
.d_un
.d_val
= size
;
3145 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3150 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3153 /* Now update local dynamic symbols. */
3154 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3155 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3156 entry
->isym
.st_name
);
3158 /* And the rest of dynamic symbols. */
3159 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3161 /* Adjust version definitions. */
3162 if (elf_tdata (output_bfd
)->cverdefs
)
3167 Elf_Internal_Verdef def
;
3168 Elf_Internal_Verdaux defaux
;
3170 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3174 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3176 p
+= sizeof (Elf_External_Verdef
);
3177 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3179 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3181 _bfd_elf_swap_verdaux_in (output_bfd
,
3182 (Elf_External_Verdaux
*) p
, &defaux
);
3183 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3185 _bfd_elf_swap_verdaux_out (output_bfd
,
3186 &defaux
, (Elf_External_Verdaux
*) p
);
3187 p
+= sizeof (Elf_External_Verdaux
);
3190 while (def
.vd_next
);
3193 /* Adjust version references. */
3194 if (elf_tdata (output_bfd
)->verref
)
3199 Elf_Internal_Verneed need
;
3200 Elf_Internal_Vernaux needaux
;
3202 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3206 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3208 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3209 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3210 (Elf_External_Verneed
*) p
);
3211 p
+= sizeof (Elf_External_Verneed
);
3212 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3214 _bfd_elf_swap_vernaux_in (output_bfd
,
3215 (Elf_External_Vernaux
*) p
, &needaux
);
3216 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3218 _bfd_elf_swap_vernaux_out (output_bfd
,
3220 (Elf_External_Vernaux
*) p
);
3221 p
+= sizeof (Elf_External_Vernaux
);
3224 while (need
.vn_next
);
3230 /* Add symbols from an ELF object file to the linker hash table. */
3233 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3235 Elf_Internal_Shdr
*hdr
;
3236 bfd_size_type symcount
;
3237 bfd_size_type extsymcount
;
3238 bfd_size_type extsymoff
;
3239 struct elf_link_hash_entry
**sym_hash
;
3240 bfd_boolean dynamic
;
3241 Elf_External_Versym
*extversym
= NULL
;
3242 Elf_External_Versym
*ever
;
3243 struct elf_link_hash_entry
*weaks
;
3244 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3245 bfd_size_type nondeflt_vers_cnt
= 0;
3246 Elf_Internal_Sym
*isymbuf
= NULL
;
3247 Elf_Internal_Sym
*isym
;
3248 Elf_Internal_Sym
*isymend
;
3249 const struct elf_backend_data
*bed
;
3250 bfd_boolean add_needed
;
3251 struct elf_link_hash_table
*htab
;
3253 void *alloc_mark
= NULL
;
3254 struct bfd_hash_entry
**old_table
= NULL
;
3255 unsigned int old_size
= 0;
3256 unsigned int old_count
= 0;
3257 void *old_tab
= NULL
;
3260 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3261 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3262 long old_dynsymcount
= 0;
3264 size_t hashsize
= 0;
3266 htab
= elf_hash_table (info
);
3267 bed
= get_elf_backend_data (abfd
);
3269 if ((abfd
->flags
& DYNAMIC
) == 0)
3275 /* You can't use -r against a dynamic object. Also, there's no
3276 hope of using a dynamic object which does not exactly match
3277 the format of the output file. */
3278 if (info
->relocatable
3279 || !is_elf_hash_table (htab
)
3280 || htab
->root
.creator
!= abfd
->xvec
)
3282 if (info
->relocatable
)
3283 bfd_set_error (bfd_error_invalid_operation
);
3285 bfd_set_error (bfd_error_wrong_format
);
3290 /* As a GNU extension, any input sections which are named
3291 .gnu.warning.SYMBOL are treated as warning symbols for the given
3292 symbol. This differs from .gnu.warning sections, which generate
3293 warnings when they are included in an output file. */
3294 if (info
->executable
)
3298 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3302 name
= bfd_get_section_name (abfd
, s
);
3303 if (CONST_STRNEQ (name
, ".gnu.warning."))
3308 name
+= sizeof ".gnu.warning." - 1;
3310 /* If this is a shared object, then look up the symbol
3311 in the hash table. If it is there, and it is already
3312 been defined, then we will not be using the entry
3313 from this shared object, so we don't need to warn.
3314 FIXME: If we see the definition in a regular object
3315 later on, we will warn, but we shouldn't. The only
3316 fix is to keep track of what warnings we are supposed
3317 to emit, and then handle them all at the end of the
3321 struct elf_link_hash_entry
*h
;
3323 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3325 /* FIXME: What about bfd_link_hash_common? */
3327 && (h
->root
.type
== bfd_link_hash_defined
3328 || h
->root
.type
== bfd_link_hash_defweak
))
3330 /* We don't want to issue this warning. Clobber
3331 the section size so that the warning does not
3332 get copied into the output file. */
3339 msg
= bfd_alloc (abfd
, sz
+ 1);
3343 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3348 if (! (_bfd_generic_link_add_one_symbol
3349 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3350 FALSE
, bed
->collect
, NULL
)))
3353 if (! info
->relocatable
)
3355 /* Clobber the section size so that the warning does
3356 not get copied into the output file. */
3359 /* Also set SEC_EXCLUDE, so that symbols defined in
3360 the warning section don't get copied to the output. */
3361 s
->flags
|= SEC_EXCLUDE
;
3370 /* If we are creating a shared library, create all the dynamic
3371 sections immediately. We need to attach them to something,
3372 so we attach them to this BFD, provided it is the right
3373 format. FIXME: If there are no input BFD's of the same
3374 format as the output, we can't make a shared library. */
3376 && is_elf_hash_table (htab
)
3377 && htab
->root
.creator
== abfd
->xvec
3378 && !htab
->dynamic_sections_created
)
3380 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3384 else if (!is_elf_hash_table (htab
))
3389 const char *soname
= NULL
;
3390 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3393 /* ld --just-symbols and dynamic objects don't mix very well.
3394 ld shouldn't allow it. */
3395 if ((s
= abfd
->sections
) != NULL
3396 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3399 /* If this dynamic lib was specified on the command line with
3400 --as-needed in effect, then we don't want to add a DT_NEEDED
3401 tag unless the lib is actually used. Similary for libs brought
3402 in by another lib's DT_NEEDED. When --no-add-needed is used
3403 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3404 any dynamic library in DT_NEEDED tags in the dynamic lib at
3406 add_needed
= (elf_dyn_lib_class (abfd
)
3407 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3408 | DYN_NO_NEEDED
)) == 0;
3410 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3416 unsigned long shlink
;
3418 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3419 goto error_free_dyn
;
3421 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3423 goto error_free_dyn
;
3424 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3426 for (extdyn
= dynbuf
;
3427 extdyn
< dynbuf
+ s
->size
;
3428 extdyn
+= bed
->s
->sizeof_dyn
)
3430 Elf_Internal_Dyn dyn
;
3432 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3433 if (dyn
.d_tag
== DT_SONAME
)
3435 unsigned int tagv
= dyn
.d_un
.d_val
;
3436 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3438 goto error_free_dyn
;
3440 if (dyn
.d_tag
== DT_NEEDED
)
3442 struct bfd_link_needed_list
*n
, **pn
;
3444 unsigned int tagv
= dyn
.d_un
.d_val
;
3446 amt
= sizeof (struct bfd_link_needed_list
);
3447 n
= bfd_alloc (abfd
, amt
);
3448 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3449 if (n
== NULL
|| fnm
== NULL
)
3450 goto error_free_dyn
;
3451 amt
= strlen (fnm
) + 1;
3452 anm
= bfd_alloc (abfd
, amt
);
3454 goto error_free_dyn
;
3455 memcpy (anm
, fnm
, amt
);
3459 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3463 if (dyn
.d_tag
== DT_RUNPATH
)
3465 struct bfd_link_needed_list
*n
, **pn
;
3467 unsigned int tagv
= dyn
.d_un
.d_val
;
3469 amt
= sizeof (struct bfd_link_needed_list
);
3470 n
= bfd_alloc (abfd
, amt
);
3471 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3472 if (n
== NULL
|| fnm
== NULL
)
3473 goto error_free_dyn
;
3474 amt
= strlen (fnm
) + 1;
3475 anm
= bfd_alloc (abfd
, amt
);
3477 goto error_free_dyn
;
3478 memcpy (anm
, fnm
, amt
);
3482 for (pn
= & runpath
;
3488 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3489 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3491 struct bfd_link_needed_list
*n
, **pn
;
3493 unsigned int tagv
= dyn
.d_un
.d_val
;
3495 amt
= sizeof (struct bfd_link_needed_list
);
3496 n
= bfd_alloc (abfd
, amt
);
3497 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3498 if (n
== NULL
|| fnm
== NULL
)
3499 goto error_free_dyn
;
3500 amt
= strlen (fnm
) + 1;
3501 anm
= bfd_alloc (abfd
, amt
);
3508 memcpy (anm
, fnm
, amt
);
3523 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3524 frees all more recently bfd_alloc'd blocks as well. */
3530 struct bfd_link_needed_list
**pn
;
3531 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3536 /* We do not want to include any of the sections in a dynamic
3537 object in the output file. We hack by simply clobbering the
3538 list of sections in the BFD. This could be handled more
3539 cleanly by, say, a new section flag; the existing
3540 SEC_NEVER_LOAD flag is not the one we want, because that one
3541 still implies that the section takes up space in the output
3543 bfd_section_list_clear (abfd
);
3545 /* Find the name to use in a DT_NEEDED entry that refers to this
3546 object. If the object has a DT_SONAME entry, we use it.
3547 Otherwise, if the generic linker stuck something in
3548 elf_dt_name, we use that. Otherwise, we just use the file
3550 if (soname
== NULL
|| *soname
== '\0')
3552 soname
= elf_dt_name (abfd
);
3553 if (soname
== NULL
|| *soname
== '\0')
3554 soname
= bfd_get_filename (abfd
);
3557 /* Save the SONAME because sometimes the linker emulation code
3558 will need to know it. */
3559 elf_dt_name (abfd
) = soname
;
3561 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3565 /* If we have already included this dynamic object in the
3566 link, just ignore it. There is no reason to include a
3567 particular dynamic object more than once. */
3572 /* If this is a dynamic object, we always link against the .dynsym
3573 symbol table, not the .symtab symbol table. The dynamic linker
3574 will only see the .dynsym symbol table, so there is no reason to
3575 look at .symtab for a dynamic object. */
3577 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3578 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3580 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3582 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3584 /* The sh_info field of the symtab header tells us where the
3585 external symbols start. We don't care about the local symbols at
3587 if (elf_bad_symtab (abfd
))
3589 extsymcount
= symcount
;
3594 extsymcount
= symcount
- hdr
->sh_info
;
3595 extsymoff
= hdr
->sh_info
;
3599 if (extsymcount
!= 0)
3601 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3603 if (isymbuf
== NULL
)
3606 /* We store a pointer to the hash table entry for each external
3608 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3609 sym_hash
= bfd_alloc (abfd
, amt
);
3610 if (sym_hash
== NULL
)
3611 goto error_free_sym
;
3612 elf_sym_hashes (abfd
) = sym_hash
;
3617 /* Read in any version definitions. */
3618 if (!_bfd_elf_slurp_version_tables (abfd
,
3619 info
->default_imported_symver
))
3620 goto error_free_sym
;
3622 /* Read in the symbol versions, but don't bother to convert them
3623 to internal format. */
3624 if (elf_dynversym (abfd
) != 0)
3626 Elf_Internal_Shdr
*versymhdr
;
3628 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3629 extversym
= bfd_malloc (versymhdr
->sh_size
);
3630 if (extversym
== NULL
)
3631 goto error_free_sym
;
3632 amt
= versymhdr
->sh_size
;
3633 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3634 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3635 goto error_free_vers
;
3639 /* If we are loading an as-needed shared lib, save the symbol table
3640 state before we start adding symbols. If the lib turns out
3641 to be unneeded, restore the state. */
3642 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3647 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3649 struct bfd_hash_entry
*p
;
3650 struct elf_link_hash_entry
*h
;
3652 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3654 h
= (struct elf_link_hash_entry
*) p
;
3655 entsize
+= htab
->root
.table
.entsize
;
3656 if (h
->root
.type
== bfd_link_hash_warning
)
3657 entsize
+= htab
->root
.table
.entsize
;
3661 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3662 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3663 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3664 if (old_tab
== NULL
)
3665 goto error_free_vers
;
3667 /* Remember the current objalloc pointer, so that all mem for
3668 symbols added can later be reclaimed. */
3669 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3670 if (alloc_mark
== NULL
)
3671 goto error_free_vers
;
3673 /* Make a special call to the linker "notice" function to
3674 tell it that we are about to handle an as-needed lib. */
3675 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3680 /* Clone the symbol table and sym hashes. Remember some
3681 pointers into the symbol table, and dynamic symbol count. */
3682 old_hash
= (char *) old_tab
+ tabsize
;
3683 old_ent
= (char *) old_hash
+ hashsize
;
3684 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3685 memcpy (old_hash
, sym_hash
, hashsize
);
3686 old_undefs
= htab
->root
.undefs
;
3687 old_undefs_tail
= htab
->root
.undefs_tail
;
3688 old_table
= htab
->root
.table
.table
;
3689 old_size
= htab
->root
.table
.size
;
3690 old_count
= htab
->root
.table
.count
;
3691 old_dynsymcount
= htab
->dynsymcount
;
3693 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3695 struct bfd_hash_entry
*p
;
3696 struct elf_link_hash_entry
*h
;
3698 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3700 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3701 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3702 h
= (struct elf_link_hash_entry
*) p
;
3703 if (h
->root
.type
== bfd_link_hash_warning
)
3705 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3706 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3713 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3714 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3716 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3720 asection
*sec
, *new_sec
;
3723 struct elf_link_hash_entry
*h
;
3724 bfd_boolean definition
;
3725 bfd_boolean size_change_ok
;
3726 bfd_boolean type_change_ok
;
3727 bfd_boolean new_weakdef
;
3728 bfd_boolean override
;
3730 unsigned int old_alignment
;
3735 flags
= BSF_NO_FLAGS
;
3737 value
= isym
->st_value
;
3739 common
= bed
->common_definition (isym
);
3741 bind
= ELF_ST_BIND (isym
->st_info
);
3742 if (bind
== STB_LOCAL
)
3744 /* This should be impossible, since ELF requires that all
3745 global symbols follow all local symbols, and that sh_info
3746 point to the first global symbol. Unfortunately, Irix 5
3750 else if (bind
== STB_GLOBAL
)
3752 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3755 else if (bind
== STB_WEAK
)
3759 /* Leave it up to the processor backend. */
3762 if (isym
->st_shndx
== SHN_UNDEF
)
3763 sec
= bfd_und_section_ptr
;
3764 else if (isym
->st_shndx
< SHN_LORESERVE
3765 || isym
->st_shndx
> SHN_HIRESERVE
)
3767 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3769 sec
= bfd_abs_section_ptr
;
3770 else if (sec
->kept_section
)
3772 /* Symbols from discarded section are undefined. We keep
3774 sec
= bfd_und_section_ptr
;
3775 isym
->st_shndx
= SHN_UNDEF
;
3777 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3780 else if (isym
->st_shndx
== SHN_ABS
)
3781 sec
= bfd_abs_section_ptr
;
3782 else if (isym
->st_shndx
== SHN_COMMON
)
3784 sec
= bfd_com_section_ptr
;
3785 /* What ELF calls the size we call the value. What ELF
3786 calls the value we call the alignment. */
3787 value
= isym
->st_size
;
3791 /* Leave it up to the processor backend. */
3794 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3797 goto error_free_vers
;
3799 if (isym
->st_shndx
== SHN_COMMON
3800 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3801 && !info
->relocatable
)
3803 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3807 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3810 | SEC_LINKER_CREATED
3811 | SEC_THREAD_LOCAL
));
3813 goto error_free_vers
;
3817 else if (bed
->elf_add_symbol_hook
)
3819 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3821 goto error_free_vers
;
3823 /* The hook function sets the name to NULL if this symbol
3824 should be skipped for some reason. */
3829 /* Sanity check that all possibilities were handled. */
3832 bfd_set_error (bfd_error_bad_value
);
3833 goto error_free_vers
;
3836 if (bfd_is_und_section (sec
)
3837 || bfd_is_com_section (sec
))
3842 size_change_ok
= FALSE
;
3843 type_change_ok
= bed
->type_change_ok
;
3848 if (is_elf_hash_table (htab
))
3850 Elf_Internal_Versym iver
;
3851 unsigned int vernum
= 0;
3856 if (info
->default_imported_symver
)
3857 /* Use the default symbol version created earlier. */
3858 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3863 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3865 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3867 /* If this is a hidden symbol, or if it is not version
3868 1, we append the version name to the symbol name.
3869 However, we do not modify a non-hidden absolute symbol
3870 if it is not a function, because it might be the version
3871 symbol itself. FIXME: What if it isn't? */
3872 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3874 && (!bfd_is_abs_section (sec
)
3875 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3878 size_t namelen
, verlen
, newlen
;
3881 if (isym
->st_shndx
!= SHN_UNDEF
)
3883 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3885 else if (vernum
> 1)
3887 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3893 (*_bfd_error_handler
)
3894 (_("%B: %s: invalid version %u (max %d)"),
3896 elf_tdata (abfd
)->cverdefs
);
3897 bfd_set_error (bfd_error_bad_value
);
3898 goto error_free_vers
;
3903 /* We cannot simply test for the number of
3904 entries in the VERNEED section since the
3905 numbers for the needed versions do not start
3907 Elf_Internal_Verneed
*t
;
3910 for (t
= elf_tdata (abfd
)->verref
;
3914 Elf_Internal_Vernaux
*a
;
3916 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3918 if (a
->vna_other
== vernum
)
3920 verstr
= a
->vna_nodename
;
3929 (*_bfd_error_handler
)
3930 (_("%B: %s: invalid needed version %d"),
3931 abfd
, name
, vernum
);
3932 bfd_set_error (bfd_error_bad_value
);
3933 goto error_free_vers
;
3937 namelen
= strlen (name
);
3938 verlen
= strlen (verstr
);
3939 newlen
= namelen
+ verlen
+ 2;
3940 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3941 && isym
->st_shndx
!= SHN_UNDEF
)
3944 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3945 if (newname
== NULL
)
3946 goto error_free_vers
;
3947 memcpy (newname
, name
, namelen
);
3948 p
= newname
+ namelen
;
3950 /* If this is a defined non-hidden version symbol,
3951 we add another @ to the name. This indicates the
3952 default version of the symbol. */
3953 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3954 && isym
->st_shndx
!= SHN_UNDEF
)
3956 memcpy (p
, verstr
, verlen
+ 1);
3961 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3962 &value
, &old_alignment
,
3963 sym_hash
, &skip
, &override
,
3964 &type_change_ok
, &size_change_ok
))
3965 goto error_free_vers
;
3974 while (h
->root
.type
== bfd_link_hash_indirect
3975 || h
->root
.type
== bfd_link_hash_warning
)
3976 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3978 /* Remember the old alignment if this is a common symbol, so
3979 that we don't reduce the alignment later on. We can't
3980 check later, because _bfd_generic_link_add_one_symbol
3981 will set a default for the alignment which we want to
3982 override. We also remember the old bfd where the existing
3983 definition comes from. */
3984 switch (h
->root
.type
)
3989 case bfd_link_hash_defined
:
3990 case bfd_link_hash_defweak
:
3991 old_bfd
= h
->root
.u
.def
.section
->owner
;
3994 case bfd_link_hash_common
:
3995 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3996 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4000 if (elf_tdata (abfd
)->verdef
!= NULL
4004 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4007 if (! (_bfd_generic_link_add_one_symbol
4008 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4009 (struct bfd_link_hash_entry
**) sym_hash
)))
4010 goto error_free_vers
;
4013 while (h
->root
.type
== bfd_link_hash_indirect
4014 || h
->root
.type
== bfd_link_hash_warning
)
4015 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4018 new_weakdef
= FALSE
;
4021 && (flags
& BSF_WEAK
) != 0
4022 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4023 && is_elf_hash_table (htab
)
4024 && h
->u
.weakdef
== NULL
)
4026 /* Keep a list of all weak defined non function symbols from
4027 a dynamic object, using the weakdef field. Later in this
4028 function we will set the weakdef field to the correct
4029 value. We only put non-function symbols from dynamic
4030 objects on this list, because that happens to be the only
4031 time we need to know the normal symbol corresponding to a
4032 weak symbol, and the information is time consuming to
4033 figure out. If the weakdef field is not already NULL,
4034 then this symbol was already defined by some previous
4035 dynamic object, and we will be using that previous
4036 definition anyhow. */
4038 h
->u
.weakdef
= weaks
;
4043 /* Set the alignment of a common symbol. */
4044 if ((common
|| bfd_is_com_section (sec
))
4045 && h
->root
.type
== bfd_link_hash_common
)
4050 align
= bfd_log2 (isym
->st_value
);
4053 /* The new symbol is a common symbol in a shared object.
4054 We need to get the alignment from the section. */
4055 align
= new_sec
->alignment_power
;
4057 if (align
> old_alignment
4058 /* Permit an alignment power of zero if an alignment of one
4059 is specified and no other alignments have been specified. */
4060 || (isym
->st_value
== 1 && old_alignment
== 0))
4061 h
->root
.u
.c
.p
->alignment_power
= align
;
4063 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4066 if (is_elf_hash_table (htab
))
4070 /* Check the alignment when a common symbol is involved. This
4071 can change when a common symbol is overridden by a normal
4072 definition or a common symbol is ignored due to the old
4073 normal definition. We need to make sure the maximum
4074 alignment is maintained. */
4075 if ((old_alignment
|| common
)
4076 && h
->root
.type
!= bfd_link_hash_common
)
4078 unsigned int common_align
;
4079 unsigned int normal_align
;
4080 unsigned int symbol_align
;
4084 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4085 if (h
->root
.u
.def
.section
->owner
!= NULL
4086 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4088 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4089 if (normal_align
> symbol_align
)
4090 normal_align
= symbol_align
;
4093 normal_align
= symbol_align
;
4097 common_align
= old_alignment
;
4098 common_bfd
= old_bfd
;
4103 common_align
= bfd_log2 (isym
->st_value
);
4105 normal_bfd
= old_bfd
;
4108 if (normal_align
< common_align
)
4110 /* PR binutils/2735 */
4111 if (normal_bfd
== NULL
)
4112 (*_bfd_error_handler
)
4113 (_("Warning: alignment %u of common symbol `%s' in %B"
4114 " is greater than the alignment (%u) of its section %A"),
4115 common_bfd
, h
->root
.u
.def
.section
,
4116 1 << common_align
, name
, 1 << normal_align
);
4118 (*_bfd_error_handler
)
4119 (_("Warning: alignment %u of symbol `%s' in %B"
4120 " is smaller than %u in %B"),
4121 normal_bfd
, common_bfd
,
4122 1 << normal_align
, name
, 1 << common_align
);
4126 /* Remember the symbol size if it isn't undefined. */
4127 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4128 && (definition
|| h
->size
== 0))
4131 && h
->size
!= isym
->st_size
4132 && ! size_change_ok
)
4133 (*_bfd_error_handler
)
4134 (_("Warning: size of symbol `%s' changed"
4135 " from %lu in %B to %lu in %B"),
4137 name
, (unsigned long) h
->size
,
4138 (unsigned long) isym
->st_size
);
4140 h
->size
= isym
->st_size
;
4143 /* If this is a common symbol, then we always want H->SIZE
4144 to be the size of the common symbol. The code just above
4145 won't fix the size if a common symbol becomes larger. We
4146 don't warn about a size change here, because that is
4147 covered by --warn-common. Allow changed between different
4149 if (h
->root
.type
== bfd_link_hash_common
)
4150 h
->size
= h
->root
.u
.c
.size
;
4152 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4153 && (definition
|| h
->type
== STT_NOTYPE
))
4155 if (h
->type
!= STT_NOTYPE
4156 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4157 && ! type_change_ok
)
4158 (*_bfd_error_handler
)
4159 (_("Warning: type of symbol `%s' changed"
4160 " from %d to %d in %B"),
4161 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4163 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4166 /* If st_other has a processor-specific meaning, specific
4167 code might be needed here. We never merge the visibility
4168 attribute with the one from a dynamic object. */
4169 if (bed
->elf_backend_merge_symbol_attribute
)
4170 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4173 /* If this symbol has default visibility and the user has requested
4174 we not re-export it, then mark it as hidden. */
4175 if (definition
&& !dynamic
4177 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4178 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4179 isym
->st_other
= (STV_HIDDEN
4180 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4182 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4184 unsigned char hvis
, symvis
, other
, nvis
;
4186 /* Only merge the visibility. Leave the remainder of the
4187 st_other field to elf_backend_merge_symbol_attribute. */
4188 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4190 /* Combine visibilities, using the most constraining one. */
4191 hvis
= ELF_ST_VISIBILITY (h
->other
);
4192 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4198 nvis
= hvis
< symvis
? hvis
: symvis
;
4200 h
->other
= other
| nvis
;
4203 /* Set a flag in the hash table entry indicating the type of
4204 reference or definition we just found. Keep a count of
4205 the number of dynamic symbols we find. A dynamic symbol
4206 is one which is referenced or defined by both a regular
4207 object and a shared object. */
4214 if (bind
!= STB_WEAK
)
4215 h
->ref_regular_nonweak
= 1;
4219 if (! info
->executable
4232 || (h
->u
.weakdef
!= NULL
4234 && h
->u
.weakdef
->dynindx
!= -1))
4238 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4240 /* We don't want to make debug symbol dynamic. */
4241 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4245 /* Check to see if we need to add an indirect symbol for
4246 the default name. */
4247 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4248 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4249 &sec
, &value
, &dynsym
,
4251 goto error_free_vers
;
4253 if (definition
&& !dynamic
)
4255 char *p
= strchr (name
, ELF_VER_CHR
);
4256 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4258 /* Queue non-default versions so that .symver x, x@FOO
4259 aliases can be checked. */
4262 amt
= ((isymend
- isym
+ 1)
4263 * sizeof (struct elf_link_hash_entry
*));
4264 nondeflt_vers
= bfd_malloc (amt
);
4266 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4270 if (dynsym
&& h
->dynindx
== -1)
4272 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4273 goto error_free_vers
;
4274 if (h
->u
.weakdef
!= NULL
4276 && h
->u
.weakdef
->dynindx
== -1)
4278 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4279 goto error_free_vers
;
4282 else if (dynsym
&& h
->dynindx
!= -1)
4283 /* If the symbol already has a dynamic index, but
4284 visibility says it should not be visible, turn it into
4286 switch (ELF_ST_VISIBILITY (h
->other
))
4290 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4301 const char *soname
= elf_dt_name (abfd
);
4303 /* A symbol from a library loaded via DT_NEEDED of some
4304 other library is referenced by a regular object.
4305 Add a DT_NEEDED entry for it. Issue an error if
4306 --no-add-needed is used. */
4307 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4309 (*_bfd_error_handler
)
4310 (_("%s: invalid DSO for symbol `%s' definition"),
4312 bfd_set_error (bfd_error_bad_value
);
4313 goto error_free_vers
;
4316 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4319 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4321 goto error_free_vers
;
4323 BFD_ASSERT (ret
== 0);
4328 if (extversym
!= NULL
)
4334 if (isymbuf
!= NULL
)
4340 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4344 /* Restore the symbol table. */
4345 if (bed
->as_needed_cleanup
)
4346 (*bed
->as_needed_cleanup
) (abfd
, info
);
4347 old_hash
= (char *) old_tab
+ tabsize
;
4348 old_ent
= (char *) old_hash
+ hashsize
;
4349 sym_hash
= elf_sym_hashes (abfd
);
4350 htab
->root
.table
.table
= old_table
;
4351 htab
->root
.table
.size
= old_size
;
4352 htab
->root
.table
.count
= old_count
;
4353 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4354 memcpy (sym_hash
, old_hash
, hashsize
);
4355 htab
->root
.undefs
= old_undefs
;
4356 htab
->root
.undefs_tail
= old_undefs_tail
;
4357 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4359 struct bfd_hash_entry
*p
;
4360 struct elf_link_hash_entry
*h
;
4362 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4364 h
= (struct elf_link_hash_entry
*) p
;
4365 if (h
->root
.type
== bfd_link_hash_warning
)
4366 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4367 if (h
->dynindx
>= old_dynsymcount
)
4368 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4370 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4371 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4372 h
= (struct elf_link_hash_entry
*) p
;
4373 if (h
->root
.type
== bfd_link_hash_warning
)
4375 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4376 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4381 /* Make a special call to the linker "notice" function to
4382 tell it that symbols added for crefs may need to be removed. */
4383 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4388 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4390 if (nondeflt_vers
!= NULL
)
4391 free (nondeflt_vers
);
4395 if (old_tab
!= NULL
)
4397 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4404 /* Now that all the symbols from this input file are created, handle
4405 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4406 if (nondeflt_vers
!= NULL
)
4408 bfd_size_type cnt
, symidx
;
4410 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4412 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4413 char *shortname
, *p
;
4415 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4417 || (h
->root
.type
!= bfd_link_hash_defined
4418 && h
->root
.type
!= bfd_link_hash_defweak
))
4421 amt
= p
- h
->root
.root
.string
;
4422 shortname
= bfd_malloc (amt
+ 1);
4423 memcpy (shortname
, h
->root
.root
.string
, amt
);
4424 shortname
[amt
] = '\0';
4426 hi
= (struct elf_link_hash_entry
*)
4427 bfd_link_hash_lookup (&htab
->root
, shortname
,
4428 FALSE
, FALSE
, FALSE
);
4430 && hi
->root
.type
== h
->root
.type
4431 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4432 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4434 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4435 hi
->root
.type
= bfd_link_hash_indirect
;
4436 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4437 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4438 sym_hash
= elf_sym_hashes (abfd
);
4440 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4441 if (sym_hash
[symidx
] == hi
)
4443 sym_hash
[symidx
] = h
;
4449 free (nondeflt_vers
);
4450 nondeflt_vers
= NULL
;
4453 /* Now set the weakdefs field correctly for all the weak defined
4454 symbols we found. The only way to do this is to search all the
4455 symbols. Since we only need the information for non functions in
4456 dynamic objects, that's the only time we actually put anything on
4457 the list WEAKS. We need this information so that if a regular
4458 object refers to a symbol defined weakly in a dynamic object, the
4459 real symbol in the dynamic object is also put in the dynamic
4460 symbols; we also must arrange for both symbols to point to the
4461 same memory location. We could handle the general case of symbol
4462 aliasing, but a general symbol alias can only be generated in
4463 assembler code, handling it correctly would be very time
4464 consuming, and other ELF linkers don't handle general aliasing
4468 struct elf_link_hash_entry
**hpp
;
4469 struct elf_link_hash_entry
**hppend
;
4470 struct elf_link_hash_entry
**sorted_sym_hash
;
4471 struct elf_link_hash_entry
*h
;
4474 /* Since we have to search the whole symbol list for each weak
4475 defined symbol, search time for N weak defined symbols will be
4476 O(N^2). Binary search will cut it down to O(NlogN). */
4477 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4478 sorted_sym_hash
= bfd_malloc (amt
);
4479 if (sorted_sym_hash
== NULL
)
4481 sym_hash
= sorted_sym_hash
;
4482 hpp
= elf_sym_hashes (abfd
);
4483 hppend
= hpp
+ extsymcount
;
4485 for (; hpp
< hppend
; hpp
++)
4489 && h
->root
.type
== bfd_link_hash_defined
4490 && !bed
->is_function_type (h
->type
))
4498 qsort (sorted_sym_hash
, sym_count
,
4499 sizeof (struct elf_link_hash_entry
*),
4502 while (weaks
!= NULL
)
4504 struct elf_link_hash_entry
*hlook
;
4511 weaks
= hlook
->u
.weakdef
;
4512 hlook
->u
.weakdef
= NULL
;
4514 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4515 || hlook
->root
.type
== bfd_link_hash_defweak
4516 || hlook
->root
.type
== bfd_link_hash_common
4517 || hlook
->root
.type
== bfd_link_hash_indirect
);
4518 slook
= hlook
->root
.u
.def
.section
;
4519 vlook
= hlook
->root
.u
.def
.value
;
4526 bfd_signed_vma vdiff
;
4528 h
= sorted_sym_hash
[idx
];
4529 vdiff
= vlook
- h
->root
.u
.def
.value
;
4536 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4549 /* We didn't find a value/section match. */
4553 for (i
= ilook
; i
< sym_count
; i
++)
4555 h
= sorted_sym_hash
[i
];
4557 /* Stop if value or section doesn't match. */
4558 if (h
->root
.u
.def
.value
!= vlook
4559 || h
->root
.u
.def
.section
!= slook
)
4561 else if (h
!= hlook
)
4563 hlook
->u
.weakdef
= h
;
4565 /* If the weak definition is in the list of dynamic
4566 symbols, make sure the real definition is put
4568 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4570 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4574 /* If the real definition is in the list of dynamic
4575 symbols, make sure the weak definition is put
4576 there as well. If we don't do this, then the
4577 dynamic loader might not merge the entries for the
4578 real definition and the weak definition. */
4579 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4581 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4589 free (sorted_sym_hash
);
4592 if (bed
->check_directives
)
4593 (*bed
->check_directives
) (abfd
, info
);
4595 /* If this object is the same format as the output object, and it is
4596 not a shared library, then let the backend look through the
4599 This is required to build global offset table entries and to
4600 arrange for dynamic relocs. It is not required for the
4601 particular common case of linking non PIC code, even when linking
4602 against shared libraries, but unfortunately there is no way of
4603 knowing whether an object file has been compiled PIC or not.
4604 Looking through the relocs is not particularly time consuming.
4605 The problem is that we must either (1) keep the relocs in memory,
4606 which causes the linker to require additional runtime memory or
4607 (2) read the relocs twice from the input file, which wastes time.
4608 This would be a good case for using mmap.
4610 I have no idea how to handle linking PIC code into a file of a
4611 different format. It probably can't be done. */
4613 && is_elf_hash_table (htab
)
4614 && htab
->root
.creator
== abfd
->xvec
4615 && bed
->check_relocs
!= NULL
)
4619 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4621 Elf_Internal_Rela
*internal_relocs
;
4624 if ((o
->flags
& SEC_RELOC
) == 0
4625 || o
->reloc_count
== 0
4626 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4627 && (o
->flags
& SEC_DEBUGGING
) != 0)
4628 || bfd_is_abs_section (o
->output_section
))
4631 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4633 if (internal_relocs
== NULL
)
4636 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4638 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4639 free (internal_relocs
);
4646 /* If this is a non-traditional link, try to optimize the handling
4647 of the .stab/.stabstr sections. */
4649 && ! info
->traditional_format
4650 && is_elf_hash_table (htab
)
4651 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4655 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4656 if (stabstr
!= NULL
)
4658 bfd_size_type string_offset
= 0;
4661 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4662 if (CONST_STRNEQ (stab
->name
, ".stab")
4663 && (!stab
->name
[5] ||
4664 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4665 && (stab
->flags
& SEC_MERGE
) == 0
4666 && !bfd_is_abs_section (stab
->output_section
))
4668 struct bfd_elf_section_data
*secdata
;
4670 secdata
= elf_section_data (stab
);
4671 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4672 stabstr
, &secdata
->sec_info
,
4675 if (secdata
->sec_info
)
4676 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4681 if (is_elf_hash_table (htab
) && add_needed
)
4683 /* Add this bfd to the loaded list. */
4684 struct elf_link_loaded_list
*n
;
4686 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4690 n
->next
= htab
->loaded
;
4697 if (old_tab
!= NULL
)
4699 if (nondeflt_vers
!= NULL
)
4700 free (nondeflt_vers
);
4701 if (extversym
!= NULL
)
4704 if (isymbuf
!= NULL
)
4710 /* Return the linker hash table entry of a symbol that might be
4711 satisfied by an archive symbol. Return -1 on error. */
4713 struct elf_link_hash_entry
*
4714 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4715 struct bfd_link_info
*info
,
4718 struct elf_link_hash_entry
*h
;
4722 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4726 /* If this is a default version (the name contains @@), look up the
4727 symbol again with only one `@' as well as without the version.
4728 The effect is that references to the symbol with and without the
4729 version will be matched by the default symbol in the archive. */
4731 p
= strchr (name
, ELF_VER_CHR
);
4732 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4735 /* First check with only one `@'. */
4736 len
= strlen (name
);
4737 copy
= bfd_alloc (abfd
, len
);
4739 return (struct elf_link_hash_entry
*) 0 - 1;
4741 first
= p
- name
+ 1;
4742 memcpy (copy
, name
, first
);
4743 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4745 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4748 /* We also need to check references to the symbol without the
4750 copy
[first
- 1] = '\0';
4751 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4752 FALSE
, FALSE
, FALSE
);
4755 bfd_release (abfd
, copy
);
4759 /* Add symbols from an ELF archive file to the linker hash table. We
4760 don't use _bfd_generic_link_add_archive_symbols because of a
4761 problem which arises on UnixWare. The UnixWare libc.so is an
4762 archive which includes an entry libc.so.1 which defines a bunch of
4763 symbols. The libc.so archive also includes a number of other
4764 object files, which also define symbols, some of which are the same
4765 as those defined in libc.so.1. Correct linking requires that we
4766 consider each object file in turn, and include it if it defines any
4767 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4768 this; it looks through the list of undefined symbols, and includes
4769 any object file which defines them. When this algorithm is used on
4770 UnixWare, it winds up pulling in libc.so.1 early and defining a
4771 bunch of symbols. This means that some of the other objects in the
4772 archive are not included in the link, which is incorrect since they
4773 precede libc.so.1 in the archive.
4775 Fortunately, ELF archive handling is simpler than that done by
4776 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4777 oddities. In ELF, if we find a symbol in the archive map, and the
4778 symbol is currently undefined, we know that we must pull in that
4781 Unfortunately, we do have to make multiple passes over the symbol
4782 table until nothing further is resolved. */
4785 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4788 bfd_boolean
*defined
= NULL
;
4789 bfd_boolean
*included
= NULL
;
4793 const struct elf_backend_data
*bed
;
4794 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4795 (bfd
*, struct bfd_link_info
*, const char *);
4797 if (! bfd_has_map (abfd
))
4799 /* An empty archive is a special case. */
4800 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4802 bfd_set_error (bfd_error_no_armap
);
4806 /* Keep track of all symbols we know to be already defined, and all
4807 files we know to be already included. This is to speed up the
4808 second and subsequent passes. */
4809 c
= bfd_ardata (abfd
)->symdef_count
;
4813 amt
*= sizeof (bfd_boolean
);
4814 defined
= bfd_zmalloc (amt
);
4815 included
= bfd_zmalloc (amt
);
4816 if (defined
== NULL
|| included
== NULL
)
4819 symdefs
= bfd_ardata (abfd
)->symdefs
;
4820 bed
= get_elf_backend_data (abfd
);
4821 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4834 symdefend
= symdef
+ c
;
4835 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4837 struct elf_link_hash_entry
*h
;
4839 struct bfd_link_hash_entry
*undefs_tail
;
4842 if (defined
[i
] || included
[i
])
4844 if (symdef
->file_offset
== last
)
4850 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4851 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4857 if (h
->root
.type
== bfd_link_hash_common
)
4859 /* We currently have a common symbol. The archive map contains
4860 a reference to this symbol, so we may want to include it. We
4861 only want to include it however, if this archive element
4862 contains a definition of the symbol, not just another common
4865 Unfortunately some archivers (including GNU ar) will put
4866 declarations of common symbols into their archive maps, as
4867 well as real definitions, so we cannot just go by the archive
4868 map alone. Instead we must read in the element's symbol
4869 table and check that to see what kind of symbol definition
4871 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4874 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4876 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4881 /* We need to include this archive member. */
4882 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4883 if (element
== NULL
)
4886 if (! bfd_check_format (element
, bfd_object
))
4889 /* Doublecheck that we have not included this object
4890 already--it should be impossible, but there may be
4891 something wrong with the archive. */
4892 if (element
->archive_pass
!= 0)
4894 bfd_set_error (bfd_error_bad_value
);
4897 element
->archive_pass
= 1;
4899 undefs_tail
= info
->hash
->undefs_tail
;
4901 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4904 if (! bfd_link_add_symbols (element
, info
))
4907 /* If there are any new undefined symbols, we need to make
4908 another pass through the archive in order to see whether
4909 they can be defined. FIXME: This isn't perfect, because
4910 common symbols wind up on undefs_tail and because an
4911 undefined symbol which is defined later on in this pass
4912 does not require another pass. This isn't a bug, but it
4913 does make the code less efficient than it could be. */
4914 if (undefs_tail
!= info
->hash
->undefs_tail
)
4917 /* Look backward to mark all symbols from this object file
4918 which we have already seen in this pass. */
4922 included
[mark
] = TRUE
;
4927 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4929 /* We mark subsequent symbols from this object file as we go
4930 on through the loop. */
4931 last
= symdef
->file_offset
;
4942 if (defined
!= NULL
)
4944 if (included
!= NULL
)
4949 /* Given an ELF BFD, add symbols to the global hash table as
4953 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4955 switch (bfd_get_format (abfd
))
4958 return elf_link_add_object_symbols (abfd
, info
);
4960 return elf_link_add_archive_symbols (abfd
, info
);
4962 bfd_set_error (bfd_error_wrong_format
);
4967 /* This function will be called though elf_link_hash_traverse to store
4968 all hash value of the exported symbols in an array. */
4971 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4973 unsigned long **valuep
= data
;
4979 if (h
->root
.type
== bfd_link_hash_warning
)
4980 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4982 /* Ignore indirect symbols. These are added by the versioning code. */
4983 if (h
->dynindx
== -1)
4986 name
= h
->root
.root
.string
;
4987 p
= strchr (name
, ELF_VER_CHR
);
4990 alc
= bfd_malloc (p
- name
+ 1);
4991 memcpy (alc
, name
, p
- name
);
4992 alc
[p
- name
] = '\0';
4996 /* Compute the hash value. */
4997 ha
= bfd_elf_hash (name
);
4999 /* Store the found hash value in the array given as the argument. */
5002 /* And store it in the struct so that we can put it in the hash table
5004 h
->u
.elf_hash_value
= ha
;
5012 struct collect_gnu_hash_codes
5015 const struct elf_backend_data
*bed
;
5016 unsigned long int nsyms
;
5017 unsigned long int maskbits
;
5018 unsigned long int *hashcodes
;
5019 unsigned long int *hashval
;
5020 unsigned long int *indx
;
5021 unsigned long int *counts
;
5024 long int min_dynindx
;
5025 unsigned long int bucketcount
;
5026 unsigned long int symindx
;
5027 long int local_indx
;
5028 long int shift1
, shift2
;
5029 unsigned long int mask
;
5032 /* This function will be called though elf_link_hash_traverse to store
5033 all hash value of the exported symbols in an array. */
5036 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5038 struct collect_gnu_hash_codes
*s
= data
;
5044 if (h
->root
.type
== bfd_link_hash_warning
)
5045 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5047 /* Ignore indirect symbols. These are added by the versioning code. */
5048 if (h
->dynindx
== -1)
5051 /* Ignore also local symbols and undefined symbols. */
5052 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5055 name
= h
->root
.root
.string
;
5056 p
= strchr (name
, ELF_VER_CHR
);
5059 alc
= bfd_malloc (p
- name
+ 1);
5060 memcpy (alc
, name
, p
- name
);
5061 alc
[p
- name
] = '\0';
5065 /* Compute the hash value. */
5066 ha
= bfd_elf_gnu_hash (name
);
5068 /* Store the found hash value in the array for compute_bucket_count,
5069 and also for .dynsym reordering purposes. */
5070 s
->hashcodes
[s
->nsyms
] = ha
;
5071 s
->hashval
[h
->dynindx
] = ha
;
5073 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5074 s
->min_dynindx
= h
->dynindx
;
5082 /* This function will be called though elf_link_hash_traverse to do
5083 final dynaminc symbol renumbering. */
5086 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5088 struct collect_gnu_hash_codes
*s
= data
;
5089 unsigned long int bucket
;
5090 unsigned long int val
;
5092 if (h
->root
.type
== bfd_link_hash_warning
)
5093 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5095 /* Ignore indirect symbols. */
5096 if (h
->dynindx
== -1)
5099 /* Ignore also local symbols and undefined symbols. */
5100 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5102 if (h
->dynindx
>= s
->min_dynindx
)
5103 h
->dynindx
= s
->local_indx
++;
5107 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5108 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5109 & ((s
->maskbits
>> s
->shift1
) - 1);
5110 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5112 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5113 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5114 if (s
->counts
[bucket
] == 1)
5115 /* Last element terminates the chain. */
5117 bfd_put_32 (s
->output_bfd
, val
,
5118 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5119 --s
->counts
[bucket
];
5120 h
->dynindx
= s
->indx
[bucket
]++;
5124 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5127 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5129 return !(h
->forced_local
5130 || h
->root
.type
== bfd_link_hash_undefined
5131 || h
->root
.type
== bfd_link_hash_undefweak
5132 || ((h
->root
.type
== bfd_link_hash_defined
5133 || h
->root
.type
== bfd_link_hash_defweak
)
5134 && h
->root
.u
.def
.section
->output_section
== NULL
));
5137 /* Array used to determine the number of hash table buckets to use
5138 based on the number of symbols there are. If there are fewer than
5139 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5140 fewer than 37 we use 17 buckets, and so forth. We never use more
5141 than 32771 buckets. */
5143 static const size_t elf_buckets
[] =
5145 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5149 /* Compute bucket count for hashing table. We do not use a static set
5150 of possible tables sizes anymore. Instead we determine for all
5151 possible reasonable sizes of the table the outcome (i.e., the
5152 number of collisions etc) and choose the best solution. The
5153 weighting functions are not too simple to allow the table to grow
5154 without bounds. Instead one of the weighting factors is the size.
5155 Therefore the result is always a good payoff between few collisions
5156 (= short chain lengths) and table size. */
5158 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5159 unsigned long int nsyms
, int gnu_hash
)
5161 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5162 size_t best_size
= 0;
5163 unsigned long int i
;
5166 /* We have a problem here. The following code to optimize the table
5167 size requires an integer type with more the 32 bits. If
5168 BFD_HOST_U_64_BIT is set we know about such a type. */
5169 #ifdef BFD_HOST_U_64_BIT
5174 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5175 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5176 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5177 unsigned long int *counts
;
5179 /* Possible optimization parameters: if we have NSYMS symbols we say
5180 that the hashing table must at least have NSYMS/4 and at most
5182 minsize
= nsyms
/ 4;
5185 best_size
= maxsize
= nsyms
* 2;
5190 if ((best_size
& 31) == 0)
5194 /* Create array where we count the collisions in. We must use bfd_malloc
5195 since the size could be large. */
5197 amt
*= sizeof (unsigned long int);
5198 counts
= bfd_malloc (amt
);
5202 /* Compute the "optimal" size for the hash table. The criteria is a
5203 minimal chain length. The minor criteria is (of course) the size
5205 for (i
= minsize
; i
< maxsize
; ++i
)
5207 /* Walk through the array of hashcodes and count the collisions. */
5208 BFD_HOST_U_64_BIT max
;
5209 unsigned long int j
;
5210 unsigned long int fact
;
5212 if (gnu_hash
&& (i
& 31) == 0)
5215 memset (counts
, '\0', i
* sizeof (unsigned long int));
5217 /* Determine how often each hash bucket is used. */
5218 for (j
= 0; j
< nsyms
; ++j
)
5219 ++counts
[hashcodes
[j
] % i
];
5221 /* For the weight function we need some information about the
5222 pagesize on the target. This is information need not be 100%
5223 accurate. Since this information is not available (so far) we
5224 define it here to a reasonable default value. If it is crucial
5225 to have a better value some day simply define this value. */
5226 # ifndef BFD_TARGET_PAGESIZE
5227 # define BFD_TARGET_PAGESIZE (4096)
5230 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5232 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5235 /* Variant 1: optimize for short chains. We add the squares
5236 of all the chain lengths (which favors many small chain
5237 over a few long chains). */
5238 for (j
= 0; j
< i
; ++j
)
5239 max
+= counts
[j
] * counts
[j
];
5241 /* This adds penalties for the overall size of the table. */
5242 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5245 /* Variant 2: Optimize a lot more for small table. Here we
5246 also add squares of the size but we also add penalties for
5247 empty slots (the +1 term). */
5248 for (j
= 0; j
< i
; ++j
)
5249 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5251 /* The overall size of the table is considered, but not as
5252 strong as in variant 1, where it is squared. */
5253 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5257 /* Compare with current best results. */
5258 if (max
< best_chlen
)
5268 #endif /* defined (BFD_HOST_U_64_BIT) */
5270 /* This is the fallback solution if no 64bit type is available or if we
5271 are not supposed to spend much time on optimizations. We select the
5272 bucket count using a fixed set of numbers. */
5273 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5275 best_size
= elf_buckets
[i
];
5276 if (nsyms
< elf_buckets
[i
+ 1])
5279 if (gnu_hash
&& best_size
< 2)
5286 /* Set up the sizes and contents of the ELF dynamic sections. This is
5287 called by the ELF linker emulation before_allocation routine. We
5288 must set the sizes of the sections before the linker sets the
5289 addresses of the various sections. */
5292 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5295 const char *filter_shlib
,
5296 const char * const *auxiliary_filters
,
5297 struct bfd_link_info
*info
,
5298 asection
**sinterpptr
,
5299 struct bfd_elf_version_tree
*verdefs
)
5301 bfd_size_type soname_indx
;
5303 const struct elf_backend_data
*bed
;
5304 struct elf_assign_sym_version_info asvinfo
;
5308 soname_indx
= (bfd_size_type
) -1;
5310 if (!is_elf_hash_table (info
->hash
))
5313 bed
= get_elf_backend_data (output_bfd
);
5314 elf_tdata (output_bfd
)->relro
= info
->relro
;
5315 if (info
->execstack
)
5316 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5317 else if (info
->noexecstack
)
5318 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5322 asection
*notesec
= NULL
;
5325 for (inputobj
= info
->input_bfds
;
5327 inputobj
= inputobj
->link_next
)
5331 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5333 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5336 if (s
->flags
& SEC_CODE
)
5340 else if (bed
->default_execstack
)
5345 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5346 if (exec
&& info
->relocatable
5347 && notesec
->output_section
!= bfd_abs_section_ptr
)
5348 notesec
->output_section
->flags
|= SEC_CODE
;
5352 /* Any syms created from now on start with -1 in
5353 got.refcount/offset and plt.refcount/offset. */
5354 elf_hash_table (info
)->init_got_refcount
5355 = elf_hash_table (info
)->init_got_offset
;
5356 elf_hash_table (info
)->init_plt_refcount
5357 = elf_hash_table (info
)->init_plt_offset
;
5359 /* The backend may have to create some sections regardless of whether
5360 we're dynamic or not. */
5361 if (bed
->elf_backend_always_size_sections
5362 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5365 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5368 dynobj
= elf_hash_table (info
)->dynobj
;
5370 /* If there were no dynamic objects in the link, there is nothing to
5375 if (elf_hash_table (info
)->dynamic_sections_created
)
5377 struct elf_info_failed eif
;
5378 struct elf_link_hash_entry
*h
;
5380 struct bfd_elf_version_tree
*t
;
5381 struct bfd_elf_version_expr
*d
;
5383 bfd_boolean all_defined
;
5385 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5386 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5390 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5392 if (soname_indx
== (bfd_size_type
) -1
5393 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5399 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5401 info
->flags
|= DF_SYMBOLIC
;
5408 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5410 if (indx
== (bfd_size_type
) -1
5411 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5414 if (info
->new_dtags
)
5416 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5417 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5422 if (filter_shlib
!= NULL
)
5426 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5427 filter_shlib
, TRUE
);
5428 if (indx
== (bfd_size_type
) -1
5429 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5433 if (auxiliary_filters
!= NULL
)
5435 const char * const *p
;
5437 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5441 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5443 if (indx
== (bfd_size_type
) -1
5444 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5450 eif
.verdefs
= verdefs
;
5453 /* If we are supposed to export all symbols into the dynamic symbol
5454 table (this is not the normal case), then do so. */
5455 if (info
->export_dynamic
5456 || (info
->executable
&& info
->dynamic
))
5458 elf_link_hash_traverse (elf_hash_table (info
),
5459 _bfd_elf_export_symbol
,
5465 /* Make all global versions with definition. */
5466 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5467 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5468 if (!d
->symver
&& d
->symbol
)
5470 const char *verstr
, *name
;
5471 size_t namelen
, verlen
, newlen
;
5473 struct elf_link_hash_entry
*newh
;
5476 namelen
= strlen (name
);
5478 verlen
= strlen (verstr
);
5479 newlen
= namelen
+ verlen
+ 3;
5481 newname
= bfd_malloc (newlen
);
5482 if (newname
== NULL
)
5484 memcpy (newname
, name
, namelen
);
5486 /* Check the hidden versioned definition. */
5487 p
= newname
+ namelen
;
5489 memcpy (p
, verstr
, verlen
+ 1);
5490 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5491 newname
, FALSE
, FALSE
,
5494 || (newh
->root
.type
!= bfd_link_hash_defined
5495 && newh
->root
.type
!= bfd_link_hash_defweak
))
5497 /* Check the default versioned definition. */
5499 memcpy (p
, verstr
, verlen
+ 1);
5500 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5501 newname
, FALSE
, FALSE
,
5506 /* Mark this version if there is a definition and it is
5507 not defined in a shared object. */
5509 && !newh
->def_dynamic
5510 && (newh
->root
.type
== bfd_link_hash_defined
5511 || newh
->root
.type
== bfd_link_hash_defweak
))
5515 /* Attach all the symbols to their version information. */
5516 asvinfo
.output_bfd
= output_bfd
;
5517 asvinfo
.info
= info
;
5518 asvinfo
.verdefs
= verdefs
;
5519 asvinfo
.failed
= FALSE
;
5521 elf_link_hash_traverse (elf_hash_table (info
),
5522 _bfd_elf_link_assign_sym_version
,
5527 if (!info
->allow_undefined_version
)
5529 /* Check if all global versions have a definition. */
5531 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5532 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5533 if (!d
->symver
&& !d
->script
)
5535 (*_bfd_error_handler
)
5536 (_("%s: undefined version: %s"),
5537 d
->pattern
, t
->name
);
5538 all_defined
= FALSE
;
5543 bfd_set_error (bfd_error_bad_value
);
5548 /* Find all symbols which were defined in a dynamic object and make
5549 the backend pick a reasonable value for them. */
5550 elf_link_hash_traverse (elf_hash_table (info
),
5551 _bfd_elf_adjust_dynamic_symbol
,
5556 /* Add some entries to the .dynamic section. We fill in some of the
5557 values later, in bfd_elf_final_link, but we must add the entries
5558 now so that we know the final size of the .dynamic section. */
5560 /* If there are initialization and/or finalization functions to
5561 call then add the corresponding DT_INIT/DT_FINI entries. */
5562 h
= (info
->init_function
5563 ? elf_link_hash_lookup (elf_hash_table (info
),
5564 info
->init_function
, FALSE
,
5571 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5574 h
= (info
->fini_function
5575 ? elf_link_hash_lookup (elf_hash_table (info
),
5576 info
->fini_function
, FALSE
,
5583 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5587 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5588 if (s
!= NULL
&& s
->linker_has_input
)
5590 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5591 if (! info
->executable
)
5596 for (sub
= info
->input_bfds
; sub
!= NULL
;
5597 sub
= sub
->link_next
)
5598 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5599 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5600 if (elf_section_data (o
)->this_hdr
.sh_type
5601 == SHT_PREINIT_ARRAY
)
5603 (*_bfd_error_handler
)
5604 (_("%B: .preinit_array section is not allowed in DSO"),
5609 bfd_set_error (bfd_error_nonrepresentable_section
);
5613 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5614 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5617 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5618 if (s
!= NULL
&& s
->linker_has_input
)
5620 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5621 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5624 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5625 if (s
!= NULL
&& s
->linker_has_input
)
5627 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5628 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5632 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5633 /* If .dynstr is excluded from the link, we don't want any of
5634 these tags. Strictly, we should be checking each section
5635 individually; This quick check covers for the case where
5636 someone does a /DISCARD/ : { *(*) }. */
5637 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5639 bfd_size_type strsize
;
5641 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5642 if ((info
->emit_hash
5643 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5644 || (info
->emit_gnu_hash
5645 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5646 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5647 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5648 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5649 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5650 bed
->s
->sizeof_sym
))
5655 /* The backend must work out the sizes of all the other dynamic
5657 if (bed
->elf_backend_size_dynamic_sections
5658 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5661 if (elf_hash_table (info
)->dynamic_sections_created
)
5663 unsigned long section_sym_count
;
5666 /* Set up the version definition section. */
5667 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5668 BFD_ASSERT (s
!= NULL
);
5670 /* We may have created additional version definitions if we are
5671 just linking a regular application. */
5672 verdefs
= asvinfo
.verdefs
;
5674 /* Skip anonymous version tag. */
5675 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5676 verdefs
= verdefs
->next
;
5678 if (verdefs
== NULL
&& !info
->create_default_symver
)
5679 s
->flags
|= SEC_EXCLUDE
;
5684 struct bfd_elf_version_tree
*t
;
5686 Elf_Internal_Verdef def
;
5687 Elf_Internal_Verdaux defaux
;
5688 struct bfd_link_hash_entry
*bh
;
5689 struct elf_link_hash_entry
*h
;
5695 /* Make space for the base version. */
5696 size
+= sizeof (Elf_External_Verdef
);
5697 size
+= sizeof (Elf_External_Verdaux
);
5700 /* Make space for the default version. */
5701 if (info
->create_default_symver
)
5703 size
+= sizeof (Elf_External_Verdef
);
5707 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5709 struct bfd_elf_version_deps
*n
;
5711 size
+= sizeof (Elf_External_Verdef
);
5712 size
+= sizeof (Elf_External_Verdaux
);
5715 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5716 size
+= sizeof (Elf_External_Verdaux
);
5720 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5721 if (s
->contents
== NULL
&& s
->size
!= 0)
5724 /* Fill in the version definition section. */
5728 def
.vd_version
= VER_DEF_CURRENT
;
5729 def
.vd_flags
= VER_FLG_BASE
;
5732 if (info
->create_default_symver
)
5734 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5735 def
.vd_next
= sizeof (Elf_External_Verdef
);
5739 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5740 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5741 + sizeof (Elf_External_Verdaux
));
5744 if (soname_indx
!= (bfd_size_type
) -1)
5746 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5748 def
.vd_hash
= bfd_elf_hash (soname
);
5749 defaux
.vda_name
= soname_indx
;
5756 name
= lbasename (output_bfd
->filename
);
5757 def
.vd_hash
= bfd_elf_hash (name
);
5758 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5760 if (indx
== (bfd_size_type
) -1)
5762 defaux
.vda_name
= indx
;
5764 defaux
.vda_next
= 0;
5766 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5767 (Elf_External_Verdef
*) p
);
5768 p
+= sizeof (Elf_External_Verdef
);
5769 if (info
->create_default_symver
)
5771 /* Add a symbol representing this version. */
5773 if (! (_bfd_generic_link_add_one_symbol
5774 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5776 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5778 h
= (struct elf_link_hash_entry
*) bh
;
5781 h
->type
= STT_OBJECT
;
5782 h
->verinfo
.vertree
= NULL
;
5784 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5787 /* Create a duplicate of the base version with the same
5788 aux block, but different flags. */
5791 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5793 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5794 + sizeof (Elf_External_Verdaux
));
5797 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5798 (Elf_External_Verdef
*) p
);
5799 p
+= sizeof (Elf_External_Verdef
);
5801 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5802 (Elf_External_Verdaux
*) p
);
5803 p
+= sizeof (Elf_External_Verdaux
);
5805 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5808 struct bfd_elf_version_deps
*n
;
5811 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5814 /* Add a symbol representing this version. */
5816 if (! (_bfd_generic_link_add_one_symbol
5817 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5819 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5821 h
= (struct elf_link_hash_entry
*) bh
;
5824 h
->type
= STT_OBJECT
;
5825 h
->verinfo
.vertree
= t
;
5827 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5830 def
.vd_version
= VER_DEF_CURRENT
;
5832 if (t
->globals
.list
== NULL
5833 && t
->locals
.list
== NULL
5835 def
.vd_flags
|= VER_FLG_WEAK
;
5836 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5837 def
.vd_cnt
= cdeps
+ 1;
5838 def
.vd_hash
= bfd_elf_hash (t
->name
);
5839 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5841 if (t
->next
!= NULL
)
5842 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5843 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5845 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5846 (Elf_External_Verdef
*) p
);
5847 p
+= sizeof (Elf_External_Verdef
);
5849 defaux
.vda_name
= h
->dynstr_index
;
5850 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5852 defaux
.vda_next
= 0;
5853 if (t
->deps
!= NULL
)
5854 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5855 t
->name_indx
= defaux
.vda_name
;
5857 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5858 (Elf_External_Verdaux
*) p
);
5859 p
+= sizeof (Elf_External_Verdaux
);
5861 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5863 if (n
->version_needed
== NULL
)
5865 /* This can happen if there was an error in the
5867 defaux
.vda_name
= 0;
5871 defaux
.vda_name
= n
->version_needed
->name_indx
;
5872 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5875 if (n
->next
== NULL
)
5876 defaux
.vda_next
= 0;
5878 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5880 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5881 (Elf_External_Verdaux
*) p
);
5882 p
+= sizeof (Elf_External_Verdaux
);
5886 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5887 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5890 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5893 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5895 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5898 else if (info
->flags
& DF_BIND_NOW
)
5900 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5906 if (info
->executable
)
5907 info
->flags_1
&= ~ (DF_1_INITFIRST
5910 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5914 /* Work out the size of the version reference section. */
5916 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5917 BFD_ASSERT (s
!= NULL
);
5919 struct elf_find_verdep_info sinfo
;
5921 sinfo
.output_bfd
= output_bfd
;
5923 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5924 if (sinfo
.vers
== 0)
5926 sinfo
.failed
= FALSE
;
5928 elf_link_hash_traverse (elf_hash_table (info
),
5929 _bfd_elf_link_find_version_dependencies
,
5932 if (elf_tdata (output_bfd
)->verref
== NULL
)
5933 s
->flags
|= SEC_EXCLUDE
;
5936 Elf_Internal_Verneed
*t
;
5941 /* Build the version definition section. */
5944 for (t
= elf_tdata (output_bfd
)->verref
;
5948 Elf_Internal_Vernaux
*a
;
5950 size
+= sizeof (Elf_External_Verneed
);
5952 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5953 size
+= sizeof (Elf_External_Vernaux
);
5957 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5958 if (s
->contents
== NULL
)
5962 for (t
= elf_tdata (output_bfd
)->verref
;
5967 Elf_Internal_Vernaux
*a
;
5971 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5974 t
->vn_version
= VER_NEED_CURRENT
;
5976 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5977 elf_dt_name (t
->vn_bfd
) != NULL
5978 ? elf_dt_name (t
->vn_bfd
)
5979 : lbasename (t
->vn_bfd
->filename
),
5981 if (indx
== (bfd_size_type
) -1)
5984 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5985 if (t
->vn_nextref
== NULL
)
5988 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5989 + caux
* sizeof (Elf_External_Vernaux
));
5991 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5992 (Elf_External_Verneed
*) p
);
5993 p
+= sizeof (Elf_External_Verneed
);
5995 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5997 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5998 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5999 a
->vna_nodename
, FALSE
);
6000 if (indx
== (bfd_size_type
) -1)
6003 if (a
->vna_nextptr
== NULL
)
6006 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6008 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6009 (Elf_External_Vernaux
*) p
);
6010 p
+= sizeof (Elf_External_Vernaux
);
6014 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6015 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6018 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6022 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6023 && elf_tdata (output_bfd
)->cverdefs
== 0)
6024 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6025 §ion_sym_count
) == 0)
6027 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6028 s
->flags
|= SEC_EXCLUDE
;
6034 /* Find the first non-excluded output section. We'll use its
6035 section symbol for some emitted relocs. */
6037 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6041 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6042 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6043 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6045 elf_hash_table (info
)->text_index_section
= s
;
6050 /* Find two non-excluded output sections, one for code, one for data.
6051 We'll use their section symbols for some emitted relocs. */
6053 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6057 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6058 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6059 == (SEC_ALLOC
| SEC_READONLY
))
6060 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6062 elf_hash_table (info
)->text_index_section
= s
;
6066 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6067 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6068 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6070 elf_hash_table (info
)->data_index_section
= s
;
6074 if (elf_hash_table (info
)->text_index_section
== NULL
)
6075 elf_hash_table (info
)->text_index_section
6076 = elf_hash_table (info
)->data_index_section
;
6080 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6082 const struct elf_backend_data
*bed
;
6084 if (!is_elf_hash_table (info
->hash
))
6087 bed
= get_elf_backend_data (output_bfd
);
6088 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6090 if (elf_hash_table (info
)->dynamic_sections_created
)
6094 bfd_size_type dynsymcount
;
6095 unsigned long section_sym_count
;
6096 unsigned int dtagcount
;
6098 dynobj
= elf_hash_table (info
)->dynobj
;
6100 /* Assign dynsym indicies. In a shared library we generate a
6101 section symbol for each output section, which come first.
6102 Next come all of the back-end allocated local dynamic syms,
6103 followed by the rest of the global symbols. */
6105 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6106 §ion_sym_count
);
6108 /* Work out the size of the symbol version section. */
6109 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6110 BFD_ASSERT (s
!= NULL
);
6111 if (dynsymcount
!= 0
6112 && (s
->flags
& SEC_EXCLUDE
) == 0)
6114 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6115 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6116 if (s
->contents
== NULL
)
6119 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6123 /* Set the size of the .dynsym and .hash sections. We counted
6124 the number of dynamic symbols in elf_link_add_object_symbols.
6125 We will build the contents of .dynsym and .hash when we build
6126 the final symbol table, because until then we do not know the
6127 correct value to give the symbols. We built the .dynstr
6128 section as we went along in elf_link_add_object_symbols. */
6129 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6130 BFD_ASSERT (s
!= NULL
);
6131 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6133 if (dynsymcount
!= 0)
6135 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6136 if (s
->contents
== NULL
)
6139 /* The first entry in .dynsym is a dummy symbol.
6140 Clear all the section syms, in case we don't output them all. */
6141 ++section_sym_count
;
6142 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6145 elf_hash_table (info
)->bucketcount
= 0;
6147 /* Compute the size of the hashing table. As a side effect this
6148 computes the hash values for all the names we export. */
6149 if (info
->emit_hash
)
6151 unsigned long int *hashcodes
;
6152 unsigned long int *hashcodesp
;
6154 unsigned long int nsyms
;
6156 size_t hash_entry_size
;
6158 /* Compute the hash values for all exported symbols. At the same
6159 time store the values in an array so that we could use them for
6161 amt
= dynsymcount
* sizeof (unsigned long int);
6162 hashcodes
= bfd_malloc (amt
);
6163 if (hashcodes
== NULL
)
6165 hashcodesp
= hashcodes
;
6167 /* Put all hash values in HASHCODES. */
6168 elf_link_hash_traverse (elf_hash_table (info
),
6169 elf_collect_hash_codes
, &hashcodesp
);
6171 nsyms
= hashcodesp
- hashcodes
;
6173 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6176 if (bucketcount
== 0)
6179 elf_hash_table (info
)->bucketcount
= bucketcount
;
6181 s
= bfd_get_section_by_name (dynobj
, ".hash");
6182 BFD_ASSERT (s
!= NULL
);
6183 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6184 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6185 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6186 if (s
->contents
== NULL
)
6189 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6190 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6191 s
->contents
+ hash_entry_size
);
6194 if (info
->emit_gnu_hash
)
6197 unsigned char *contents
;
6198 struct collect_gnu_hash_codes cinfo
;
6202 memset (&cinfo
, 0, sizeof (cinfo
));
6204 /* Compute the hash values for all exported symbols. At the same
6205 time store the values in an array so that we could use them for
6207 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6208 cinfo
.hashcodes
= bfd_malloc (amt
);
6209 if (cinfo
.hashcodes
== NULL
)
6212 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6213 cinfo
.min_dynindx
= -1;
6214 cinfo
.output_bfd
= output_bfd
;
6217 /* Put all hash values in HASHCODES. */
6218 elf_link_hash_traverse (elf_hash_table (info
),
6219 elf_collect_gnu_hash_codes
, &cinfo
);
6222 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6224 if (bucketcount
== 0)
6226 free (cinfo
.hashcodes
);
6230 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6231 BFD_ASSERT (s
!= NULL
);
6233 if (cinfo
.nsyms
== 0)
6235 /* Empty .gnu.hash section is special. */
6236 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6237 free (cinfo
.hashcodes
);
6238 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6239 contents
= bfd_zalloc (output_bfd
, s
->size
);
6240 if (contents
== NULL
)
6242 s
->contents
= contents
;
6243 /* 1 empty bucket. */
6244 bfd_put_32 (output_bfd
, 1, contents
);
6245 /* SYMIDX above the special symbol 0. */
6246 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6247 /* Just one word for bitmask. */
6248 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6249 /* Only hash fn bloom filter. */
6250 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6251 /* No hashes are valid - empty bitmask. */
6252 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6253 /* No hashes in the only bucket. */
6254 bfd_put_32 (output_bfd
, 0,
6255 contents
+ 16 + bed
->s
->arch_size
/ 8);
6259 unsigned long int maskwords
, maskbitslog2
;
6260 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6262 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6263 if (maskbitslog2
< 3)
6265 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6266 maskbitslog2
= maskbitslog2
+ 3;
6268 maskbitslog2
= maskbitslog2
+ 2;
6269 if (bed
->s
->arch_size
== 64)
6271 if (maskbitslog2
== 5)
6277 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6278 cinfo
.shift2
= maskbitslog2
;
6279 cinfo
.maskbits
= 1 << maskbitslog2
;
6280 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6281 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6282 amt
+= maskwords
* sizeof (bfd_vma
);
6283 cinfo
.bitmask
= bfd_malloc (amt
);
6284 if (cinfo
.bitmask
== NULL
)
6286 free (cinfo
.hashcodes
);
6290 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6291 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6292 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6293 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6295 /* Determine how often each hash bucket is used. */
6296 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6297 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6298 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6300 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6301 if (cinfo
.counts
[i
] != 0)
6303 cinfo
.indx
[i
] = cnt
;
6304 cnt
+= cinfo
.counts
[i
];
6306 BFD_ASSERT (cnt
== dynsymcount
);
6307 cinfo
.bucketcount
= bucketcount
;
6308 cinfo
.local_indx
= cinfo
.min_dynindx
;
6310 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6311 s
->size
+= cinfo
.maskbits
/ 8;
6312 contents
= bfd_zalloc (output_bfd
, s
->size
);
6313 if (contents
== NULL
)
6315 free (cinfo
.bitmask
);
6316 free (cinfo
.hashcodes
);
6320 s
->contents
= contents
;
6321 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6322 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6323 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6324 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6325 contents
+= 16 + cinfo
.maskbits
/ 8;
6327 for (i
= 0; i
< bucketcount
; ++i
)
6329 if (cinfo
.counts
[i
] == 0)
6330 bfd_put_32 (output_bfd
, 0, contents
);
6332 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6336 cinfo
.contents
= contents
;
6338 /* Renumber dynamic symbols, populate .gnu.hash section. */
6339 elf_link_hash_traverse (elf_hash_table (info
),
6340 elf_renumber_gnu_hash_syms
, &cinfo
);
6342 contents
= s
->contents
+ 16;
6343 for (i
= 0; i
< maskwords
; ++i
)
6345 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6347 contents
+= bed
->s
->arch_size
/ 8;
6350 free (cinfo
.bitmask
);
6351 free (cinfo
.hashcodes
);
6355 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6356 BFD_ASSERT (s
!= NULL
);
6358 elf_finalize_dynstr (output_bfd
, info
);
6360 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6362 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6363 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6370 /* Final phase of ELF linker. */
6372 /* A structure we use to avoid passing large numbers of arguments. */
6374 struct elf_final_link_info
6376 /* General link information. */
6377 struct bfd_link_info
*info
;
6380 /* Symbol string table. */
6381 struct bfd_strtab_hash
*symstrtab
;
6382 /* .dynsym section. */
6383 asection
*dynsym_sec
;
6384 /* .hash section. */
6386 /* symbol version section (.gnu.version). */
6387 asection
*symver_sec
;
6388 /* Buffer large enough to hold contents of any section. */
6390 /* Buffer large enough to hold external relocs of any section. */
6391 void *external_relocs
;
6392 /* Buffer large enough to hold internal relocs of any section. */
6393 Elf_Internal_Rela
*internal_relocs
;
6394 /* Buffer large enough to hold external local symbols of any input
6396 bfd_byte
*external_syms
;
6397 /* And a buffer for symbol section indices. */
6398 Elf_External_Sym_Shndx
*locsym_shndx
;
6399 /* Buffer large enough to hold internal local symbols of any input
6401 Elf_Internal_Sym
*internal_syms
;
6402 /* Array large enough to hold a symbol index for each local symbol
6403 of any input BFD. */
6405 /* Array large enough to hold a section pointer for each local
6406 symbol of any input BFD. */
6407 asection
**sections
;
6408 /* Buffer to hold swapped out symbols. */
6410 /* And one for symbol section indices. */
6411 Elf_External_Sym_Shndx
*symshndxbuf
;
6412 /* Number of swapped out symbols in buffer. */
6413 size_t symbuf_count
;
6414 /* Number of symbols which fit in symbuf. */
6416 /* And same for symshndxbuf. */
6417 size_t shndxbuf_size
;
6420 /* This struct is used to pass information to elf_link_output_extsym. */
6422 struct elf_outext_info
6425 bfd_boolean localsyms
;
6426 struct elf_final_link_info
*finfo
;
6430 /* Support for evaluating a complex relocation.
6432 Complex relocations are generalized, self-describing relocations. The
6433 implementation of them consists of two parts: complex symbols, and the
6434 relocations themselves.
6436 The relocations are use a reserved elf-wide relocation type code (R_RELC
6437 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6438 information (start bit, end bit, word width, etc) into the addend. This
6439 information is extracted from CGEN-generated operand tables within gas.
6441 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6442 internal) representing prefix-notation expressions, including but not
6443 limited to those sorts of expressions normally encoded as addends in the
6444 addend field. The symbol mangling format is:
6447 | <unary-operator> ':' <node>
6448 | <binary-operator> ':' <node> ':' <node>
6451 <literal> := 's' <digits=N> ':' <N character symbol name>
6452 | 'S' <digits=N> ':' <N character section name>
6456 <binary-operator> := as in C
6457 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6460 set_symbol_value (bfd
* bfd_with_globals
,
6461 struct elf_final_link_info
* finfo
,
6465 bfd_boolean is_local
;
6466 Elf_Internal_Sym
* sym
;
6467 struct elf_link_hash_entry
** sym_hashes
;
6468 struct elf_link_hash_entry
* h
;
6470 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
6471 sym
= finfo
->internal_syms
+ symidx
;
6472 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
6476 /* It is a local symbol: move it to the
6477 "absolute" section and give it a value. */
6478 sym
->st_shndx
= SHN_ABS
;
6479 sym
->st_value
= val
;
6483 /* It is a global symbol: set its link type
6484 to "defined" and give it a value. */
6485 h
= sym_hashes
[symidx
];
6486 while (h
->root
.type
== bfd_link_hash_indirect
6487 || h
->root
.type
== bfd_link_hash_warning
)
6488 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6489 h
->root
.type
= bfd_link_hash_defined
;
6490 h
->root
.u
.def
.value
= val
;
6491 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6496 resolve_symbol (const char * name
,
6498 struct elf_final_link_info
* finfo
,
6502 Elf_Internal_Sym
* sym
;
6503 struct bfd_link_hash_entry
* global_entry
;
6504 const char * candidate
= NULL
;
6505 Elf_Internal_Shdr
* symtab_hdr
;
6506 asection
* sec
= NULL
;
6509 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6511 for (i
= 0; i
< locsymcount
; ++ i
)
6513 sym
= finfo
->internal_syms
+ i
;
6514 sec
= finfo
->sections
[i
];
6516 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
6519 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
6520 symtab_hdr
->sh_link
,
6523 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6524 name
, candidate
, (unsigned int)sym
->st_value
);
6526 if (candidate
&& strcmp (candidate
, name
) == 0)
6528 * result
= sym
->st_value
;
6530 if (sym
->st_shndx
> SHN_UNDEF
&&
6531 sym
->st_shndx
< SHN_LORESERVE
)
6534 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6535 sec
->output_section
->name
,
6536 (unsigned int)sec
->output_section
->vma
,
6537 (unsigned int)sec
->output_offset
);
6539 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
6542 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
6548 /* Hmm, haven't found it yet. perhaps it is a global. */
6549 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
6553 if (global_entry
->type
== bfd_link_hash_defined
6554 || global_entry
->type
== bfd_link_hash_defweak
)
6556 * result
= global_entry
->u
.def
.value
6557 + global_entry
->u
.def
.section
->output_section
->vma
6558 + global_entry
->u
.def
.section
->output_offset
;
6560 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6561 global_entry
->root
.string
, (unsigned int)*result
);
6566 if (global_entry
->type
== bfd_link_hash_common
)
6568 *result
= global_entry
->u
.def
.value
+
6569 bfd_com_section_ptr
->output_section
->vma
+
6570 bfd_com_section_ptr
->output_offset
;
6572 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6573 global_entry
->root
.string
, (unsigned int)*result
);
6582 resolve_section (const char * name
,
6583 asection
* sections
,
6589 for (curr
= sections
; curr
; curr
= curr
->next
)
6590 if (strcmp (curr
->name
, name
) == 0)
6592 *result
= curr
->vma
;
6596 /* Hmm. still haven't found it. try pseudo-section names. */
6597 for (curr
= sections
; curr
; curr
= curr
->next
)
6599 len
= strlen (curr
->name
);
6600 if (len
> strlen (name
))
6603 if (strncmp (curr
->name
, name
, len
) == 0)
6605 if (strncmp (".end", name
+ len
, 4) == 0)
6607 *result
= curr
->vma
+ curr
->size
;
6611 /* Insert more pseudo-section names here, if you like. */
6619 undefined_reference (const char * reftype
,
6622 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
6626 eval_symbol (bfd_vma
* result
,
6630 struct elf_final_link_info
* finfo
,
6632 bfd_vma section_offset
,
6640 const int bufsz
= 4096;
6641 char symbuf
[bufsz
];
6642 const char * symend
;
6643 bfd_boolean symbol_is_section
= FALSE
;
6648 if (len
< 1 || len
> bufsz
)
6650 bfd_set_error (bfd_error_invalid_operation
);
6657 * result
= addr
+ section_offset
;
6658 * advanced
= sym
+ 1;
6663 * result
= strtoul (sym
, advanced
, 16);
6667 symbol_is_section
= TRUE
;
6670 symlen
= strtol (sym
, &sym
, 10);
6671 ++ sym
; /* Skip the trailing ':'. */
6673 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
6675 bfd_set_error (bfd_error_invalid_operation
);
6679 memcpy (symbuf
, sym
, symlen
);
6680 symbuf
[symlen
] = '\0';
6681 * advanced
= sym
+ symlen
;
6683 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6684 the symbol as a section, or vice-versa. so we're pretty liberal in our
6685 interpretation here; section means "try section first", not "must be a
6686 section", and likewise with symbol. */
6688 if (symbol_is_section
)
6690 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
6691 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
6693 undefined_reference ("section", symbuf
);
6699 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
6700 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
6703 undefined_reference ("symbol", symbuf
);
6710 /* All that remains are operators. */
6712 #define UNARY_OP(op) \
6713 if (strncmp (sym, #op, strlen (#op)) == 0) \
6715 sym += strlen (#op); \
6718 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6719 section_offset, locsymcount, signed_p) \
6723 * result = op ((signed)a); \
6730 #define BINARY_OP(op) \
6731 if (strncmp (sym, #op, strlen (#op)) == 0) \
6733 sym += strlen (#op); \
6736 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6737 section_offset, locsymcount, signed_p) \
6741 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6742 section_offset, locsymcount, signed_p) \
6746 * result = ((signed) a) op ((signed) b); \
6748 * result = a op b; \
6777 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
6778 bfd_set_error (bfd_error_invalid_operation
);
6783 /* Entry point to evaluator, called from elf_link_input_bfd. */
6786 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
6787 struct elf_final_link_info
* finfo
,
6790 const struct elf_backend_data
* bed
;
6791 Elf_Internal_Shdr
* symtab_hdr
;
6792 struct elf_link_hash_entry
** sym_hashes
;
6793 asection
* reloc_sec
;
6794 bfd_boolean result
= TRUE
;
6796 /* For each section, we're going to check and see if it has any
6797 complex relocations, and we're going to evaluate any of them
6800 if (finfo
->info
->relocatable
)
6803 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6804 sym_hashes
= elf_sym_hashes (input_bfd
);
6805 bed
= get_elf_backend_data (input_bfd
);
6807 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
6809 Elf_Internal_Rela
* internal_relocs
;
6812 /* This section was omitted from the link. */
6813 if (! reloc_sec
->linker_mark
)
6816 /* Only process sections containing relocs. */
6817 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
6820 if (reloc_sec
->reloc_count
== 0)
6823 /* Read in the relocs for this section. */
6825 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
6826 (Elf_Internal_Rela
*) NULL
,
6828 if (internal_relocs
== NULL
)
6831 for (i
= reloc_sec
->reloc_count
; i
--;)
6833 Elf_Internal_Rela
* rel
;
6836 Elf_Internal_Sym
* sym
;
6838 bfd_vma section_offset
;
6842 rel
= internal_relocs
+ i
;
6843 section_offset
= reloc_sec
->output_section
->vma
6844 + reloc_sec
->output_offset
;
6845 addr
= rel
->r_offset
;
6847 index
= ELF32_R_SYM (rel
->r_info
);
6848 if (bed
->s
->arch_size
== 64)
6851 if (index
== STN_UNDEF
)
6854 if (index
< locsymcount
)
6856 /* The symbol is local. */
6857 sym
= finfo
->internal_syms
+ index
;
6859 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6860 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
6861 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
6864 sym_name
= bfd_elf_string_from_elf_section
6865 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
6867 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
6871 /* The symbol is global. */
6872 struct elf_link_hash_entry
* h
;
6874 if (elf_bad_symtab (input_bfd
))
6877 h
= sym_hashes
[index
- locsymcount
];
6878 while ( h
->root
.type
== bfd_link_hash_indirect
6879 || h
->root
.type
== bfd_link_hash_warning
)
6880 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6882 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
6885 signed_p
= (h
->type
== STT_SRELC
);
6886 sym_name
= (char *) h
->root
.root
.string
;
6889 printf ("Encountered a complex symbol!");
6890 printf (" (input_bfd %s, section %s, reloc %ld\n",
6891 input_bfd
->filename
, reloc_sec
->name
, i
);
6892 printf (" symbol: idx %8.8lx, name %s\n",
6894 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6896 printf (" Evaluating '%s' ...\n ", sym_name
);
6898 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
6899 finfo
, addr
, section_offset
, locsymcount
,
6901 /* Symbol evaluated OK. Update to absolute value. */
6902 set_symbol_value (input_bfd
, finfo
, index
, result
);
6908 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
6909 free (internal_relocs
);
6912 /* If nothing went wrong, then we adjusted
6913 everything we wanted to adjust. */
6918 put_value (bfd_vma size
,
6919 unsigned long chunksz
,
6922 bfd_byte
* location
)
6924 location
+= (size
- chunksz
);
6926 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
6934 bfd_put_8 (input_bfd
, x
, location
);
6937 bfd_put_16 (input_bfd
, x
, location
);
6940 bfd_put_32 (input_bfd
, x
, location
);
6944 bfd_put_64 (input_bfd
, x
, location
);
6954 get_value (bfd_vma size
,
6955 unsigned long chunksz
,
6957 bfd_byte
* location
)
6961 for (; size
; size
-= chunksz
, location
+= chunksz
)
6969 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
6972 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
6975 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
6979 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
6990 decode_complex_addend
6991 (unsigned long * start
, /* in bits */
6992 unsigned long * oplen
, /* in bits */
6993 unsigned long * len
, /* in bits */
6994 unsigned long * wordsz
, /* in bytes */
6995 unsigned long * chunksz
, /* in bytes */
6996 unsigned long * lsb0_p
,
6997 unsigned long * signed_p
,
6998 unsigned long * trunc_p
,
6999 unsigned long encoded
)
7001 * start
= encoded
& 0x3F;
7002 * len
= (encoded
>> 6) & 0x3F;
7003 * oplen
= (encoded
>> 12) & 0x3F;
7004 * wordsz
= (encoded
>> 18) & 0xF;
7005 * chunksz
= (encoded
>> 22) & 0xF;
7006 * lsb0_p
= (encoded
>> 27) & 1;
7007 * signed_p
= (encoded
>> 28) & 1;
7008 * trunc_p
= (encoded
>> 29) & 1;
7012 bfd_elf_perform_complex_relocation
7013 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
7014 struct bfd_link_info
* info
,
7016 asection
* input_section
,
7017 bfd_byte
* contents
,
7018 Elf_Internal_Rela
* rel
,
7019 Elf_Internal_Sym
* local_syms
,
7020 asection
** local_sections
)
7022 const struct elf_backend_data
* bed
;
7023 Elf_Internal_Shdr
* symtab_hdr
;
7025 bfd_vma relocation
= 0, shift
, x
;
7028 unsigned long start
, oplen
, len
, wordsz
,
7029 chunksz
, lsb0_p
, signed_p
, trunc_p
;
7031 /* Perform this reloc, since it is complex.
7032 (this is not to say that it necessarily refers to a complex
7033 symbol; merely that it is a self-describing CGEN based reloc.
7034 i.e. the addend has the complete reloc information (bit start, end,
7035 word size, etc) encoded within it.). */
7036 r_symndx
= ELF32_R_SYM (rel
->r_info
);
7037 bed
= get_elf_backend_data (input_bfd
);
7038 if (bed
->s
->arch_size
== 64)
7042 printf ("Performing complex relocation %ld...\n", r_symndx
);
7045 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7046 if (r_symndx
< symtab_hdr
->sh_info
)
7048 /* The symbol is local. */
7049 Elf_Internal_Sym
* sym
;
7051 sym
= local_syms
+ r_symndx
;
7052 sec
= local_sections
[r_symndx
];
7053 relocation
= sym
->st_value
;
7054 if (sym
->st_shndx
> SHN_UNDEF
&&
7055 sym
->st_shndx
< SHN_LORESERVE
)
7056 relocation
+= (sec
->output_offset
+
7057 sec
->output_section
->vma
);
7061 /* The symbol is global. */
7062 struct elf_link_hash_entry
**sym_hashes
;
7063 struct elf_link_hash_entry
* h
;
7065 sym_hashes
= elf_sym_hashes (input_bfd
);
7066 h
= sym_hashes
[r_symndx
];
7068 while (h
->root
.type
== bfd_link_hash_indirect
7069 || h
->root
.type
== bfd_link_hash_warning
)
7070 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7072 if (h
->root
.type
== bfd_link_hash_defined
7073 || h
->root
.type
== bfd_link_hash_defweak
)
7075 sec
= h
->root
.u
.def
.section
;
7076 relocation
= h
->root
.u
.def
.value
;
7078 if (! bfd_is_abs_section (sec
))
7079 relocation
+= (sec
->output_section
->vma
7080 + sec
->output_offset
);
7082 if (h
->root
.type
== bfd_link_hash_undefined
7083 && !((*info
->callbacks
->undefined_symbol
)
7084 (info
, h
->root
.root
.string
, input_bfd
,
7085 input_section
, rel
->r_offset
,
7086 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7087 || ELF_ST_VISIBILITY (h
->other
))))
7091 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7092 & chunksz
, & lsb0_p
, & signed_p
,
7093 & trunc_p
, rel
->r_addend
);
7095 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7098 shift
= (start
+ 1) - len
;
7100 shift
= (8 * wordsz
) - (start
+ len
);
7102 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7105 printf ("Doing complex reloc: "
7106 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7107 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7108 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7109 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7110 oplen
, x
, mask
, relocation
);
7115 /* Now do an overflow check. */
7116 if (bfd_check_overflow ((signed_p
?
7117 complain_overflow_signed
:
7118 complain_overflow_unsigned
),
7119 len
, 0, (8 * wordsz
),
7120 relocation
) == bfd_reloc_overflow
)
7121 (*_bfd_error_handler
)
7122 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7124 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7125 relocation
, (signed_p
? "(signed) " : ""), mask
);
7129 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7132 printf (" relocation: %8.8lx\n"
7133 " shifted mask: %8.8lx\n"
7134 " shifted/masked reloc: %8.8lx\n"
7135 " result: %8.8lx\n",
7136 relocation
, (mask
<< shift
),
7137 ((relocation
& mask
) << shift
), x
);
7139 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7142 /* When performing a relocatable link, the input relocations are
7143 preserved. But, if they reference global symbols, the indices
7144 referenced must be updated. Update all the relocations in
7145 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7148 elf_link_adjust_relocs (bfd
*abfd
,
7149 Elf_Internal_Shdr
*rel_hdr
,
7151 struct elf_link_hash_entry
**rel_hash
)
7154 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7156 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7157 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7158 bfd_vma r_type_mask
;
7161 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7163 swap_in
= bed
->s
->swap_reloc_in
;
7164 swap_out
= bed
->s
->swap_reloc_out
;
7166 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7168 swap_in
= bed
->s
->swap_reloca_in
;
7169 swap_out
= bed
->s
->swap_reloca_out
;
7174 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7177 if (bed
->s
->arch_size
== 32)
7184 r_type_mask
= 0xffffffff;
7188 erela
= rel_hdr
->contents
;
7189 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7191 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7194 if (*rel_hash
== NULL
)
7197 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7199 (*swap_in
) (abfd
, erela
, irela
);
7200 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7201 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7202 | (irela
[j
].r_info
& r_type_mask
));
7203 (*swap_out
) (abfd
, irela
, erela
);
7207 struct elf_link_sort_rela
7213 enum elf_reloc_type_class type
;
7214 /* We use this as an array of size int_rels_per_ext_rel. */
7215 Elf_Internal_Rela rela
[1];
7219 elf_link_sort_cmp1 (const void *A
, const void *B
)
7221 const struct elf_link_sort_rela
*a
= A
;
7222 const struct elf_link_sort_rela
*b
= B
;
7223 int relativea
, relativeb
;
7225 relativea
= a
->type
== reloc_class_relative
;
7226 relativeb
= b
->type
== reloc_class_relative
;
7228 if (relativea
< relativeb
)
7230 if (relativea
> relativeb
)
7232 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7234 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7236 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7238 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7244 elf_link_sort_cmp2 (const void *A
, const void *B
)
7246 const struct elf_link_sort_rela
*a
= A
;
7247 const struct elf_link_sort_rela
*b
= B
;
7250 if (a
->u
.offset
< b
->u
.offset
)
7252 if (a
->u
.offset
> b
->u
.offset
)
7254 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7255 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7260 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7262 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7268 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7270 asection
*dynamic_relocs
;
7273 bfd_size_type count
, size
;
7274 size_t i
, ret
, sort_elt
, ext_size
;
7275 bfd_byte
*sort
, *s_non_relative
, *p
;
7276 struct elf_link_sort_rela
*sq
;
7277 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7278 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7279 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7280 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7281 struct bfd_link_order
*lo
;
7283 bfd_boolean use_rela
;
7285 /* Find a dynamic reloc section. */
7286 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7287 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7288 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
7289 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7291 bfd_boolean use_rela_initialised
= FALSE
;
7293 /* This is just here to stop gcc from complaining.
7294 It's initialization checking code is not perfect. */
7297 /* Both sections are present. Examine the sizes
7298 of the indirect sections to help us choose. */
7299 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7300 if (lo
->type
== bfd_indirect_link_order
)
7302 asection
*o
= lo
->u
.indirect
.section
;
7304 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7306 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7307 /* Section size is divisible by both rel and rela sizes.
7308 It is of no help to us. */
7312 /* Section size is only divisible by rela. */
7313 if (use_rela_initialised
&& (use_rela
== FALSE
))
7316 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7317 bfd_set_error (bfd_error_invalid_operation
);
7323 use_rela_initialised
= TRUE
;
7327 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7329 /* Section size is only divisible by rel. */
7330 if (use_rela_initialised
&& (use_rela
== TRUE
))
7333 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7334 bfd_set_error (bfd_error_invalid_operation
);
7340 use_rela_initialised
= TRUE
;
7345 /* The section size is not divisible by either - something is wrong. */
7347 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7348 bfd_set_error (bfd_error_invalid_operation
);
7353 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7354 if (lo
->type
== bfd_indirect_link_order
)
7356 asection
*o
= lo
->u
.indirect
.section
;
7358 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7360 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7361 /* Section size is divisible by both rel and rela sizes.
7362 It is of no help to us. */
7366 /* Section size is only divisible by rela. */
7367 if (use_rela_initialised
&& (use_rela
== FALSE
))
7370 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7371 bfd_set_error (bfd_error_invalid_operation
);
7377 use_rela_initialised
= TRUE
;
7381 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7383 /* Section size is only divisible by rel. */
7384 if (use_rela_initialised
&& (use_rela
== TRUE
))
7387 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7388 bfd_set_error (bfd_error_invalid_operation
);
7394 use_rela_initialised
= TRUE
;
7399 /* The section size is not divisible by either - something is wrong. */
7401 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7402 bfd_set_error (bfd_error_invalid_operation
);
7407 if (! use_rela_initialised
)
7411 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
7413 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7420 dynamic_relocs
= rela_dyn
;
7421 ext_size
= bed
->s
->sizeof_rela
;
7422 swap_in
= bed
->s
->swap_reloca_in
;
7423 swap_out
= bed
->s
->swap_reloca_out
;
7427 dynamic_relocs
= rel_dyn
;
7428 ext_size
= bed
->s
->sizeof_rel
;
7429 swap_in
= bed
->s
->swap_reloc_in
;
7430 swap_out
= bed
->s
->swap_reloc_out
;
7434 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7435 if (lo
->type
== bfd_indirect_link_order
)
7436 size
+= lo
->u
.indirect
.section
->size
;
7438 if (size
!= dynamic_relocs
->size
)
7441 sort_elt
= (sizeof (struct elf_link_sort_rela
)
7442 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
7444 count
= dynamic_relocs
->size
/ ext_size
;
7445 sort
= bfd_zmalloc (sort_elt
* count
);
7449 (*info
->callbacks
->warning
)
7450 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
7454 if (bed
->s
->arch_size
== 32)
7455 r_sym_mask
= ~(bfd_vma
) 0xff;
7457 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
7459 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7460 if (lo
->type
== bfd_indirect_link_order
)
7462 bfd_byte
*erel
, *erelend
;
7463 asection
*o
= lo
->u
.indirect
.section
;
7465 if (o
->contents
== NULL
&& o
->size
!= 0)
7467 /* This is a reloc section that is being handled as a normal
7468 section. See bfd_section_from_shdr. We can't combine
7469 relocs in this case. */
7474 erelend
= o
->contents
+ o
->size
;
7475 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7477 while (erel
< erelend
)
7479 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7481 (*swap_in
) (abfd
, erel
, s
->rela
);
7482 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
7483 s
->u
.sym_mask
= r_sym_mask
;
7489 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
7491 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
7493 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7494 if (s
->type
!= reloc_class_relative
)
7500 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
7501 for (; i
< count
; i
++, p
+= sort_elt
)
7503 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
7504 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
7506 sp
->u
.offset
= sq
->rela
->r_offset
;
7509 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
7511 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7512 if (lo
->type
== bfd_indirect_link_order
)
7514 bfd_byte
*erel
, *erelend
;
7515 asection
*o
= lo
->u
.indirect
.section
;
7518 erelend
= o
->contents
+ o
->size
;
7519 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7520 while (erel
< erelend
)
7522 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7523 (*swap_out
) (abfd
, s
->rela
, erel
);
7530 *psec
= dynamic_relocs
;
7534 /* Flush the output symbols to the file. */
7537 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
7538 const struct elf_backend_data
*bed
)
7540 if (finfo
->symbuf_count
> 0)
7542 Elf_Internal_Shdr
*hdr
;
7546 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
7547 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
7548 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7549 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
7550 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
7553 hdr
->sh_size
+= amt
;
7554 finfo
->symbuf_count
= 0;
7560 /* Add a symbol to the output symbol table. */
7563 elf_link_output_sym (struct elf_final_link_info
*finfo
,
7565 Elf_Internal_Sym
*elfsym
,
7566 asection
*input_sec
,
7567 struct elf_link_hash_entry
*h
)
7570 Elf_External_Sym_Shndx
*destshndx
;
7571 bfd_boolean (*output_symbol_hook
)
7572 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
7573 struct elf_link_hash_entry
*);
7574 const struct elf_backend_data
*bed
;
7576 bed
= get_elf_backend_data (finfo
->output_bfd
);
7577 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
7578 if (output_symbol_hook
!= NULL
)
7580 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
7584 if (name
== NULL
|| *name
== '\0')
7585 elfsym
->st_name
= 0;
7586 else if (input_sec
->flags
& SEC_EXCLUDE
)
7587 elfsym
->st_name
= 0;
7590 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
7592 if (elfsym
->st_name
== (unsigned long) -1)
7596 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
7598 if (! elf_link_flush_output_syms (finfo
, bed
))
7602 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7603 destshndx
= finfo
->symshndxbuf
;
7604 if (destshndx
!= NULL
)
7606 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
7610 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
7611 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
7612 if (destshndx
== NULL
)
7614 memset ((char *) destshndx
+ amt
, 0, amt
);
7615 finfo
->shndxbuf_size
*= 2;
7617 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
7620 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
7621 finfo
->symbuf_count
+= 1;
7622 bfd_get_symcount (finfo
->output_bfd
) += 1;
7627 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7630 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
7632 if (sym
->st_shndx
> SHN_HIRESERVE
)
7634 /* The gABI doesn't support dynamic symbols in output sections
7636 (*_bfd_error_handler
)
7637 (_("%B: Too many sections: %d (>= %d)"),
7638 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
7639 bfd_set_error (bfd_error_nonrepresentable_section
);
7645 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7646 allowing an unsatisfied unversioned symbol in the DSO to match a
7647 versioned symbol that would normally require an explicit version.
7648 We also handle the case that a DSO references a hidden symbol
7649 which may be satisfied by a versioned symbol in another DSO. */
7652 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
7653 const struct elf_backend_data
*bed
,
7654 struct elf_link_hash_entry
*h
)
7657 struct elf_link_loaded_list
*loaded
;
7659 if (!is_elf_hash_table (info
->hash
))
7662 switch (h
->root
.type
)
7668 case bfd_link_hash_undefined
:
7669 case bfd_link_hash_undefweak
:
7670 abfd
= h
->root
.u
.undef
.abfd
;
7671 if ((abfd
->flags
& DYNAMIC
) == 0
7672 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
7676 case bfd_link_hash_defined
:
7677 case bfd_link_hash_defweak
:
7678 abfd
= h
->root
.u
.def
.section
->owner
;
7681 case bfd_link_hash_common
:
7682 abfd
= h
->root
.u
.c
.p
->section
->owner
;
7685 BFD_ASSERT (abfd
!= NULL
);
7687 for (loaded
= elf_hash_table (info
)->loaded
;
7689 loaded
= loaded
->next
)
7692 Elf_Internal_Shdr
*hdr
;
7693 bfd_size_type symcount
;
7694 bfd_size_type extsymcount
;
7695 bfd_size_type extsymoff
;
7696 Elf_Internal_Shdr
*versymhdr
;
7697 Elf_Internal_Sym
*isym
;
7698 Elf_Internal_Sym
*isymend
;
7699 Elf_Internal_Sym
*isymbuf
;
7700 Elf_External_Versym
*ever
;
7701 Elf_External_Versym
*extversym
;
7703 input
= loaded
->abfd
;
7705 /* We check each DSO for a possible hidden versioned definition. */
7707 || (input
->flags
& DYNAMIC
) == 0
7708 || elf_dynversym (input
) == 0)
7711 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
7713 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7714 if (elf_bad_symtab (input
))
7716 extsymcount
= symcount
;
7721 extsymcount
= symcount
- hdr
->sh_info
;
7722 extsymoff
= hdr
->sh_info
;
7725 if (extsymcount
== 0)
7728 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
7730 if (isymbuf
== NULL
)
7733 /* Read in any version definitions. */
7734 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
7735 extversym
= bfd_malloc (versymhdr
->sh_size
);
7736 if (extversym
== NULL
)
7739 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
7740 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
7741 != versymhdr
->sh_size
))
7749 ever
= extversym
+ extsymoff
;
7750 isymend
= isymbuf
+ extsymcount
;
7751 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
7754 Elf_Internal_Versym iver
;
7755 unsigned short version_index
;
7757 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
7758 || isym
->st_shndx
== SHN_UNDEF
)
7761 name
= bfd_elf_string_from_elf_section (input
,
7764 if (strcmp (name
, h
->root
.root
.string
) != 0)
7767 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
7769 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
7771 /* If we have a non-hidden versioned sym, then it should
7772 have provided a definition for the undefined sym. */
7776 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
7777 if (version_index
== 1 || version_index
== 2)
7779 /* This is the base or first version. We can use it. */
7793 /* Add an external symbol to the symbol table. This is called from
7794 the hash table traversal routine. When generating a shared object,
7795 we go through the symbol table twice. The first time we output
7796 anything that might have been forced to local scope in a version
7797 script. The second time we output the symbols that are still
7801 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
7803 struct elf_outext_info
*eoinfo
= data
;
7804 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
7806 Elf_Internal_Sym sym
;
7807 asection
*input_sec
;
7808 const struct elf_backend_data
*bed
;
7810 if (h
->root
.type
== bfd_link_hash_warning
)
7812 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7813 if (h
->root
.type
== bfd_link_hash_new
)
7817 /* Decide whether to output this symbol in this pass. */
7818 if (eoinfo
->localsyms
)
7820 if (!h
->forced_local
)
7825 if (h
->forced_local
)
7829 bed
= get_elf_backend_data (finfo
->output_bfd
);
7831 if (h
->root
.type
== bfd_link_hash_undefined
)
7833 /* If we have an undefined symbol reference here then it must have
7834 come from a shared library that is being linked in. (Undefined
7835 references in regular files have already been handled). */
7836 bfd_boolean ignore_undef
= FALSE
;
7838 /* Some symbols may be special in that the fact that they're
7839 undefined can be safely ignored - let backend determine that. */
7840 if (bed
->elf_backend_ignore_undef_symbol
)
7841 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
7843 /* If we are reporting errors for this situation then do so now. */
7844 if (ignore_undef
== FALSE
7847 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
7848 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
7850 if (! (finfo
->info
->callbacks
->undefined_symbol
7851 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
7852 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
7854 eoinfo
->failed
= TRUE
;
7860 /* We should also warn if a forced local symbol is referenced from
7861 shared libraries. */
7862 if (! finfo
->info
->relocatable
7863 && (! finfo
->info
->shared
)
7868 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
7870 (*_bfd_error_handler
)
7871 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7873 h
->root
.u
.def
.section
== bfd_abs_section_ptr
7874 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
7875 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
7877 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
7878 ? "hidden" : "local",
7879 h
->root
.root
.string
);
7880 eoinfo
->failed
= TRUE
;
7884 /* We don't want to output symbols that have never been mentioned by
7885 a regular file, or that we have been told to strip. However, if
7886 h->indx is set to -2, the symbol is used by a reloc and we must
7890 else if ((h
->def_dynamic
7892 || h
->root
.type
== bfd_link_hash_new
)
7896 else if (finfo
->info
->strip
== strip_all
)
7898 else if (finfo
->info
->strip
== strip_some
7899 && bfd_hash_lookup (finfo
->info
->keep_hash
,
7900 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
7902 else if (finfo
->info
->strip_discarded
7903 && (h
->root
.type
== bfd_link_hash_defined
7904 || h
->root
.type
== bfd_link_hash_defweak
)
7905 && elf_discarded_section (h
->root
.u
.def
.section
))
7910 /* If we're stripping it, and it's not a dynamic symbol, there's
7911 nothing else to do unless it is a forced local symbol. */
7914 && !h
->forced_local
)
7918 sym
.st_size
= h
->size
;
7919 sym
.st_other
= h
->other
;
7920 if (h
->forced_local
)
7921 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
7922 else if (h
->root
.type
== bfd_link_hash_undefweak
7923 || h
->root
.type
== bfd_link_hash_defweak
)
7924 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
7926 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
7928 switch (h
->root
.type
)
7931 case bfd_link_hash_new
:
7932 case bfd_link_hash_warning
:
7936 case bfd_link_hash_undefined
:
7937 case bfd_link_hash_undefweak
:
7938 input_sec
= bfd_und_section_ptr
;
7939 sym
.st_shndx
= SHN_UNDEF
;
7942 case bfd_link_hash_defined
:
7943 case bfd_link_hash_defweak
:
7945 input_sec
= h
->root
.u
.def
.section
;
7946 if (input_sec
->output_section
!= NULL
)
7949 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7950 input_sec
->output_section
);
7951 if (sym
.st_shndx
== SHN_BAD
)
7953 (*_bfd_error_handler
)
7954 (_("%B: could not find output section %A for input section %A"),
7955 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7956 eoinfo
->failed
= TRUE
;
7960 /* ELF symbols in relocatable files are section relative,
7961 but in nonrelocatable files they are virtual
7963 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7964 if (! finfo
->info
->relocatable
)
7966 sym
.st_value
+= input_sec
->output_section
->vma
;
7967 if (h
->type
== STT_TLS
)
7969 /* STT_TLS symbols are relative to PT_TLS segment
7971 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7972 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7978 BFD_ASSERT (input_sec
->owner
== NULL
7979 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7980 sym
.st_shndx
= SHN_UNDEF
;
7981 input_sec
= bfd_und_section_ptr
;
7986 case bfd_link_hash_common
:
7987 input_sec
= h
->root
.u
.c
.p
->section
;
7988 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7989 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7992 case bfd_link_hash_indirect
:
7993 /* These symbols are created by symbol versioning. They point
7994 to the decorated version of the name. For example, if the
7995 symbol foo@@GNU_1.2 is the default, which should be used when
7996 foo is used with no version, then we add an indirect symbol
7997 foo which points to foo@@GNU_1.2. We ignore these symbols,
7998 since the indirected symbol is already in the hash table. */
8002 /* Give the processor backend a chance to tweak the symbol value,
8003 and also to finish up anything that needs to be done for this
8004 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8005 forced local syms when non-shared is due to a historical quirk. */
8006 if ((h
->dynindx
!= -1
8008 && ((finfo
->info
->shared
8009 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8010 || h
->root
.type
!= bfd_link_hash_undefweak
))
8011 || !h
->forced_local
)
8012 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8014 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8015 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8017 eoinfo
->failed
= TRUE
;
8022 /* If we are marking the symbol as undefined, and there are no
8023 non-weak references to this symbol from a regular object, then
8024 mark the symbol as weak undefined; if there are non-weak
8025 references, mark the symbol as strong. We can't do this earlier,
8026 because it might not be marked as undefined until the
8027 finish_dynamic_symbol routine gets through with it. */
8028 if (sym
.st_shndx
== SHN_UNDEF
8030 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8031 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8035 if (h
->ref_regular_nonweak
)
8036 bindtype
= STB_GLOBAL
;
8038 bindtype
= STB_WEAK
;
8039 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8042 /* If a non-weak symbol with non-default visibility is not defined
8043 locally, it is a fatal error. */
8044 if (! finfo
->info
->relocatable
8045 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8046 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8047 && h
->root
.type
== bfd_link_hash_undefined
8050 (*_bfd_error_handler
)
8051 (_("%B: %s symbol `%s' isn't defined"),
8053 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8055 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8056 ? "internal" : "hidden",
8057 h
->root
.root
.string
);
8058 eoinfo
->failed
= TRUE
;
8062 /* If this symbol should be put in the .dynsym section, then put it
8063 there now. We already know the symbol index. We also fill in
8064 the entry in the .hash section. */
8065 if (h
->dynindx
!= -1
8066 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8070 sym
.st_name
= h
->dynstr_index
;
8071 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8072 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8074 eoinfo
->failed
= TRUE
;
8077 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8079 if (finfo
->hash_sec
!= NULL
)
8081 size_t hash_entry_size
;
8082 bfd_byte
*bucketpos
;
8087 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8088 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8091 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8092 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8093 + (bucket
+ 2) * hash_entry_size
);
8094 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8095 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8096 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8097 ((bfd_byte
*) finfo
->hash_sec
->contents
8098 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8101 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8103 Elf_Internal_Versym iversym
;
8104 Elf_External_Versym
*eversym
;
8106 if (!h
->def_regular
)
8108 if (h
->verinfo
.verdef
== NULL
)
8109 iversym
.vs_vers
= 0;
8111 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8115 if (h
->verinfo
.vertree
== NULL
)
8116 iversym
.vs_vers
= 1;
8118 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8119 if (finfo
->info
->create_default_symver
)
8124 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8126 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8127 eversym
+= h
->dynindx
;
8128 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8132 /* If we're stripping it, then it was just a dynamic symbol, and
8133 there's nothing else to do. */
8134 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8137 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8139 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8141 eoinfo
->failed
= TRUE
;
8148 /* Return TRUE if special handling is done for relocs in SEC against
8149 symbols defined in discarded sections. */
8152 elf_section_ignore_discarded_relocs (asection
*sec
)
8154 const struct elf_backend_data
*bed
;
8156 switch (sec
->sec_info_type
)
8158 case ELF_INFO_TYPE_STABS
:
8159 case ELF_INFO_TYPE_EH_FRAME
:
8165 bed
= get_elf_backend_data (sec
->owner
);
8166 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8167 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8173 /* Return a mask saying how ld should treat relocations in SEC against
8174 symbols defined in discarded sections. If this function returns
8175 COMPLAIN set, ld will issue a warning message. If this function
8176 returns PRETEND set, and the discarded section was link-once and the
8177 same size as the kept link-once section, ld will pretend that the
8178 symbol was actually defined in the kept section. Otherwise ld will
8179 zero the reloc (at least that is the intent, but some cooperation by
8180 the target dependent code is needed, particularly for REL targets). */
8183 _bfd_elf_default_action_discarded (asection
*sec
)
8185 if (sec
->flags
& SEC_DEBUGGING
)
8188 if (strcmp (".eh_frame", sec
->name
) == 0)
8191 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8194 return COMPLAIN
| PRETEND
;
8197 /* Find a match between a section and a member of a section group. */
8200 match_group_member (asection
*sec
, asection
*group
,
8201 struct bfd_link_info
*info
)
8203 asection
*first
= elf_next_in_group (group
);
8204 asection
*s
= first
;
8208 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8211 s
= elf_next_in_group (s
);
8219 /* Check if the kept section of a discarded section SEC can be used
8220 to replace it. Return the replacement if it is OK. Otherwise return
8224 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8228 kept
= sec
->kept_section
;
8231 if ((kept
->flags
& SEC_GROUP
) != 0)
8232 kept
= match_group_member (sec
, kept
, info
);
8233 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8235 sec
->kept_section
= kept
;
8240 /* Link an input file into the linker output file. This function
8241 handles all the sections and relocations of the input file at once.
8242 This is so that we only have to read the local symbols once, and
8243 don't have to keep them in memory. */
8246 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8248 int (*relocate_section
)
8249 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8250 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8252 Elf_Internal_Shdr
*symtab_hdr
;
8255 Elf_Internal_Sym
*isymbuf
;
8256 Elf_Internal_Sym
*isym
;
8257 Elf_Internal_Sym
*isymend
;
8259 asection
**ppsection
;
8261 const struct elf_backend_data
*bed
;
8262 struct elf_link_hash_entry
**sym_hashes
;
8264 output_bfd
= finfo
->output_bfd
;
8265 bed
= get_elf_backend_data (output_bfd
);
8266 relocate_section
= bed
->elf_backend_relocate_section
;
8268 /* If this is a dynamic object, we don't want to do anything here:
8269 we don't want the local symbols, and we don't want the section
8271 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8274 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8275 if (elf_bad_symtab (input_bfd
))
8277 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8282 locsymcount
= symtab_hdr
->sh_info
;
8283 extsymoff
= symtab_hdr
->sh_info
;
8286 /* Read the local symbols. */
8287 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8288 if (isymbuf
== NULL
&& locsymcount
!= 0)
8290 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8291 finfo
->internal_syms
,
8292 finfo
->external_syms
,
8293 finfo
->locsym_shndx
);
8294 if (isymbuf
== NULL
)
8297 /* evaluate_complex_relocation_symbols looks for symbols in
8298 finfo->internal_syms. */
8299 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
8301 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8302 finfo
->internal_syms
,
8303 finfo
->external_syms
,
8304 finfo
->locsym_shndx
);
8307 /* Find local symbol sections and adjust values of symbols in
8308 SEC_MERGE sections. Write out those local symbols we know are
8309 going into the output file. */
8310 isymend
= isymbuf
+ locsymcount
;
8311 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8313 isym
++, pindex
++, ppsection
++)
8317 Elf_Internal_Sym osym
;
8321 if (elf_bad_symtab (input_bfd
))
8323 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8330 if (isym
->st_shndx
== SHN_UNDEF
)
8331 isec
= bfd_und_section_ptr
;
8332 else if (isym
->st_shndx
< SHN_LORESERVE
8333 || isym
->st_shndx
> SHN_HIRESERVE
)
8335 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8337 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8338 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8340 _bfd_merged_section_offset (output_bfd
, &isec
,
8341 elf_section_data (isec
)->sec_info
,
8344 else if (isym
->st_shndx
== SHN_ABS
)
8345 isec
= bfd_abs_section_ptr
;
8346 else if (isym
->st_shndx
== SHN_COMMON
)
8347 isec
= bfd_com_section_ptr
;
8350 /* Don't attempt to output symbols with st_shnx in the
8351 reserved range other than SHN_ABS and SHN_COMMON. */
8358 /* Don't output the first, undefined, symbol. */
8359 if (ppsection
== finfo
->sections
)
8362 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8364 /* We never output section symbols. Instead, we use the
8365 section symbol of the corresponding section in the output
8370 /* If we are stripping all symbols, we don't want to output this
8372 if (finfo
->info
->strip
== strip_all
)
8375 /* If we are discarding all local symbols, we don't want to
8376 output this one. If we are generating a relocatable output
8377 file, then some of the local symbols may be required by
8378 relocs; we output them below as we discover that they are
8380 if (finfo
->info
->discard
== discard_all
)
8383 /* If this symbol is defined in a section which we are
8384 discarding, we don't need to keep it. */
8385 if (isym
->st_shndx
!= SHN_UNDEF
8386 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8388 || bfd_section_removed_from_list (output_bfd
,
8389 isec
->output_section
)))
8392 /* Get the name of the symbol. */
8393 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
8398 /* See if we are discarding symbols with this name. */
8399 if ((finfo
->info
->strip
== strip_some
8400 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
8402 || (((finfo
->info
->discard
== discard_sec_merge
8403 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
8404 || finfo
->info
->discard
== discard_l
)
8405 && bfd_is_local_label_name (input_bfd
, name
)))
8408 /* If we get here, we are going to output this symbol. */
8412 /* Adjust the section index for the output file. */
8413 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
8414 isec
->output_section
);
8415 if (osym
.st_shndx
== SHN_BAD
)
8418 *pindex
= bfd_get_symcount (output_bfd
);
8420 /* ELF symbols in relocatable files are section relative, but
8421 in executable files they are virtual addresses. Note that
8422 this code assumes that all ELF sections have an associated
8423 BFD section with a reasonable value for output_offset; below
8424 we assume that they also have a reasonable value for
8425 output_section. Any special sections must be set up to meet
8426 these requirements. */
8427 osym
.st_value
+= isec
->output_offset
;
8428 if (! finfo
->info
->relocatable
)
8430 osym
.st_value
+= isec
->output_section
->vma
;
8431 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
8433 /* STT_TLS symbols are relative to PT_TLS segment base. */
8434 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8435 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8439 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
8443 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
8446 /* Relocate the contents of each section. */
8447 sym_hashes
= elf_sym_hashes (input_bfd
);
8448 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
8452 if (! o
->linker_mark
)
8454 /* This section was omitted from the link. */
8458 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8459 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
8462 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
8464 /* Section was created by _bfd_elf_link_create_dynamic_sections
8469 /* Get the contents of the section. They have been cached by a
8470 relaxation routine. Note that o is a section in an input
8471 file, so the contents field will not have been set by any of
8472 the routines which work on output files. */
8473 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
8474 contents
= elf_section_data (o
)->this_hdr
.contents
;
8477 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
8479 contents
= finfo
->contents
;
8480 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
8484 if ((o
->flags
& SEC_RELOC
) != 0)
8486 Elf_Internal_Rela
*internal_relocs
;
8487 bfd_vma r_type_mask
;
8491 /* Get the swapped relocs. */
8493 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
8494 finfo
->internal_relocs
, FALSE
);
8495 if (internal_relocs
== NULL
8496 && o
->reloc_count
> 0)
8499 if (bed
->s
->arch_size
== 32)
8506 r_type_mask
= 0xffffffff;
8510 /* Run through the relocs looking for any against symbols
8511 from discarded sections and section symbols from
8512 removed link-once sections. Complain about relocs
8513 against discarded sections. Zero relocs against removed
8514 link-once sections. */
8515 if (!elf_section_ignore_discarded_relocs (o
))
8517 Elf_Internal_Rela
*rel
, *relend
;
8518 unsigned int action
= (*bed
->action_discarded
) (o
);
8520 rel
= internal_relocs
;
8521 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8522 for ( ; rel
< relend
; rel
++)
8524 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
8525 asection
**ps
, *sec
;
8526 struct elf_link_hash_entry
*h
= NULL
;
8527 const char *sym_name
;
8529 if (r_symndx
== STN_UNDEF
)
8532 if (r_symndx
>= locsymcount
8533 || (elf_bad_symtab (input_bfd
)
8534 && finfo
->sections
[r_symndx
] == NULL
))
8536 h
= sym_hashes
[r_symndx
- extsymoff
];
8538 /* Badly formatted input files can contain relocs that
8539 reference non-existant symbols. Check here so that
8540 we do not seg fault. */
8545 sprintf_vma (buffer
, rel
->r_info
);
8546 (*_bfd_error_handler
)
8547 (_("error: %B contains a reloc (0x%s) for section %A "
8548 "that references a non-existent global symbol"),
8549 input_bfd
, o
, buffer
);
8550 bfd_set_error (bfd_error_bad_value
);
8554 while (h
->root
.type
== bfd_link_hash_indirect
8555 || h
->root
.type
== bfd_link_hash_warning
)
8556 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8558 if (h
->root
.type
!= bfd_link_hash_defined
8559 && h
->root
.type
!= bfd_link_hash_defweak
)
8562 ps
= &h
->root
.u
.def
.section
;
8563 sym_name
= h
->root
.root
.string
;
8567 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
8568 ps
= &finfo
->sections
[r_symndx
];
8569 sym_name
= bfd_elf_sym_name (input_bfd
,
8574 /* Complain if the definition comes from a
8575 discarded section. */
8576 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
8578 BFD_ASSERT (r_symndx
!= 0);
8579 if (action
& COMPLAIN
)
8580 (*finfo
->info
->callbacks
->einfo
)
8581 (_("%X`%s' referenced in section `%A' of %B: "
8582 "defined in discarded section `%A' of %B\n"),
8583 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
8585 /* Try to do the best we can to support buggy old
8586 versions of gcc. Pretend that the symbol is
8587 really defined in the kept linkonce section.
8588 FIXME: This is quite broken. Modifying the
8589 symbol here means we will be changing all later
8590 uses of the symbol, not just in this section. */
8591 if (action
& PRETEND
)
8595 kept
= _bfd_elf_check_kept_section (sec
,
8607 /* Relocate the section by invoking a back end routine.
8609 The back end routine is responsible for adjusting the
8610 section contents as necessary, and (if using Rela relocs
8611 and generating a relocatable output file) adjusting the
8612 reloc addend as necessary.
8614 The back end routine does not have to worry about setting
8615 the reloc address or the reloc symbol index.
8617 The back end routine is given a pointer to the swapped in
8618 internal symbols, and can access the hash table entries
8619 for the external symbols via elf_sym_hashes (input_bfd).
8621 When generating relocatable output, the back end routine
8622 must handle STB_LOCAL/STT_SECTION symbols specially. The
8623 output symbol is going to be a section symbol
8624 corresponding to the output section, which will require
8625 the addend to be adjusted. */
8627 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
8628 input_bfd
, o
, contents
,
8636 || finfo
->info
->relocatable
8637 || finfo
->info
->emitrelocations
)
8639 Elf_Internal_Rela
*irela
;
8640 Elf_Internal_Rela
*irelaend
;
8641 bfd_vma last_offset
;
8642 struct elf_link_hash_entry
**rel_hash
;
8643 struct elf_link_hash_entry
**rel_hash_list
;
8644 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
8645 unsigned int next_erel
;
8646 bfd_boolean rela_normal
;
8648 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
8649 rela_normal
= (bed
->rela_normal
8650 && (input_rel_hdr
->sh_entsize
8651 == bed
->s
->sizeof_rela
));
8653 /* Adjust the reloc addresses and symbol indices. */
8655 irela
= internal_relocs
;
8656 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8657 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
8658 + elf_section_data (o
->output_section
)->rel_count
8659 + elf_section_data (o
->output_section
)->rel_count2
);
8660 rel_hash_list
= rel_hash
;
8661 last_offset
= o
->output_offset
;
8662 if (!finfo
->info
->relocatable
)
8663 last_offset
+= o
->output_section
->vma
;
8664 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
8666 unsigned long r_symndx
;
8668 Elf_Internal_Sym sym
;
8670 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
8676 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
8679 if (irela
->r_offset
>= (bfd_vma
) -2)
8681 /* This is a reloc for a deleted entry or somesuch.
8682 Turn it into an R_*_NONE reloc, at the same
8683 offset as the last reloc. elf_eh_frame.c and
8684 bfd_elf_discard_info rely on reloc offsets
8686 irela
->r_offset
= last_offset
;
8688 irela
->r_addend
= 0;
8692 irela
->r_offset
+= o
->output_offset
;
8694 /* Relocs in an executable have to be virtual addresses. */
8695 if (!finfo
->info
->relocatable
)
8696 irela
->r_offset
+= o
->output_section
->vma
;
8698 last_offset
= irela
->r_offset
;
8700 r_symndx
= irela
->r_info
>> r_sym_shift
;
8701 if (r_symndx
== STN_UNDEF
)
8704 if (r_symndx
>= locsymcount
8705 || (elf_bad_symtab (input_bfd
)
8706 && finfo
->sections
[r_symndx
] == NULL
))
8708 struct elf_link_hash_entry
*rh
;
8711 /* This is a reloc against a global symbol. We
8712 have not yet output all the local symbols, so
8713 we do not know the symbol index of any global
8714 symbol. We set the rel_hash entry for this
8715 reloc to point to the global hash table entry
8716 for this symbol. The symbol index is then
8717 set at the end of bfd_elf_final_link. */
8718 indx
= r_symndx
- extsymoff
;
8719 rh
= elf_sym_hashes (input_bfd
)[indx
];
8720 while (rh
->root
.type
== bfd_link_hash_indirect
8721 || rh
->root
.type
== bfd_link_hash_warning
)
8722 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
8724 /* Setting the index to -2 tells
8725 elf_link_output_extsym that this symbol is
8727 BFD_ASSERT (rh
->indx
< 0);
8735 /* This is a reloc against a local symbol. */
8738 sym
= isymbuf
[r_symndx
];
8739 sec
= finfo
->sections
[r_symndx
];
8740 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
8742 /* I suppose the backend ought to fill in the
8743 section of any STT_SECTION symbol against a
8744 processor specific section. */
8746 if (bfd_is_abs_section (sec
))
8748 else if (sec
== NULL
|| sec
->owner
== NULL
)
8750 bfd_set_error (bfd_error_bad_value
);
8755 asection
*osec
= sec
->output_section
;
8757 /* If we have discarded a section, the output
8758 section will be the absolute section. In
8759 case of discarded SEC_MERGE sections, use
8760 the kept section. relocate_section should
8761 have already handled discarded linkonce
8763 if (bfd_is_abs_section (osec
)
8764 && sec
->kept_section
!= NULL
8765 && sec
->kept_section
->output_section
!= NULL
)
8767 osec
= sec
->kept_section
->output_section
;
8768 irela
->r_addend
-= osec
->vma
;
8771 if (!bfd_is_abs_section (osec
))
8773 r_symndx
= osec
->target_index
;
8776 struct elf_link_hash_table
*htab
;
8779 htab
= elf_hash_table (finfo
->info
);
8780 oi
= htab
->text_index_section
;
8781 if ((osec
->flags
& SEC_READONLY
) == 0
8782 && htab
->data_index_section
!= NULL
)
8783 oi
= htab
->data_index_section
;
8787 irela
->r_addend
+= osec
->vma
- oi
->vma
;
8788 r_symndx
= oi
->target_index
;
8792 BFD_ASSERT (r_symndx
!= 0);
8796 /* Adjust the addend according to where the
8797 section winds up in the output section. */
8799 irela
->r_addend
+= sec
->output_offset
;
8803 if (finfo
->indices
[r_symndx
] == -1)
8805 unsigned long shlink
;
8809 if (finfo
->info
->strip
== strip_all
)
8811 /* You can't do ld -r -s. */
8812 bfd_set_error (bfd_error_invalid_operation
);
8816 /* This symbol was skipped earlier, but
8817 since it is needed by a reloc, we
8818 must output it now. */
8819 shlink
= symtab_hdr
->sh_link
;
8820 name
= (bfd_elf_string_from_elf_section
8821 (input_bfd
, shlink
, sym
.st_name
));
8825 osec
= sec
->output_section
;
8827 _bfd_elf_section_from_bfd_section (output_bfd
,
8829 if (sym
.st_shndx
== SHN_BAD
)
8832 sym
.st_value
+= sec
->output_offset
;
8833 if (! finfo
->info
->relocatable
)
8835 sym
.st_value
+= osec
->vma
;
8836 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
8838 /* STT_TLS symbols are relative to PT_TLS
8840 BFD_ASSERT (elf_hash_table (finfo
->info
)
8842 sym
.st_value
-= (elf_hash_table (finfo
->info
)
8847 finfo
->indices
[r_symndx
]
8848 = bfd_get_symcount (output_bfd
);
8850 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
8855 r_symndx
= finfo
->indices
[r_symndx
];
8858 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
8859 | (irela
->r_info
& r_type_mask
));
8862 /* Swap out the relocs. */
8863 if (input_rel_hdr
->sh_size
!= 0
8864 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
8870 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
8871 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
8873 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
8874 * bed
->s
->int_rels_per_ext_rel
);
8875 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
8876 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
8885 /* Write out the modified section contents. */
8886 if (bed
->elf_backend_write_section
8887 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
8890 /* Section written out. */
8892 else switch (o
->sec_info_type
)
8894 case ELF_INFO_TYPE_STABS
:
8895 if (! (_bfd_write_section_stabs
8897 &elf_hash_table (finfo
->info
)->stab_info
,
8898 o
, &elf_section_data (o
)->sec_info
, contents
)))
8901 case ELF_INFO_TYPE_MERGE
:
8902 if (! _bfd_write_merged_section (output_bfd
, o
,
8903 elf_section_data (o
)->sec_info
))
8906 case ELF_INFO_TYPE_EH_FRAME
:
8908 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
8915 if (! (o
->flags
& SEC_EXCLUDE
)
8916 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
8918 (file_ptr
) o
->output_offset
,
8929 /* Generate a reloc when linking an ELF file. This is a reloc
8930 requested by the linker, and does not come from any input file. This
8931 is used to build constructor and destructor tables when linking
8935 elf_reloc_link_order (bfd
*output_bfd
,
8936 struct bfd_link_info
*info
,
8937 asection
*output_section
,
8938 struct bfd_link_order
*link_order
)
8940 reloc_howto_type
*howto
;
8944 struct elf_link_hash_entry
**rel_hash_ptr
;
8945 Elf_Internal_Shdr
*rel_hdr
;
8946 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8947 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8951 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8954 bfd_set_error (bfd_error_bad_value
);
8958 addend
= link_order
->u
.reloc
.p
->addend
;
8960 /* Figure out the symbol index. */
8961 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8962 + elf_section_data (output_section
)->rel_count
8963 + elf_section_data (output_section
)->rel_count2
);
8964 if (link_order
->type
== bfd_section_reloc_link_order
)
8966 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8967 BFD_ASSERT (indx
!= 0);
8968 *rel_hash_ptr
= NULL
;
8972 struct elf_link_hash_entry
*h
;
8974 /* Treat a reloc against a defined symbol as though it were
8975 actually against the section. */
8976 h
= ((struct elf_link_hash_entry
*)
8977 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8978 link_order
->u
.reloc
.p
->u
.name
,
8979 FALSE
, FALSE
, TRUE
));
8981 && (h
->root
.type
== bfd_link_hash_defined
8982 || h
->root
.type
== bfd_link_hash_defweak
))
8986 section
= h
->root
.u
.def
.section
;
8987 indx
= section
->output_section
->target_index
;
8988 *rel_hash_ptr
= NULL
;
8989 /* It seems that we ought to add the symbol value to the
8990 addend here, but in practice it has already been added
8991 because it was passed to constructor_callback. */
8992 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8996 /* Setting the index to -2 tells elf_link_output_extsym that
8997 this symbol is used by a reloc. */
9004 if (! ((*info
->callbacks
->unattached_reloc
)
9005 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9011 /* If this is an inplace reloc, we must write the addend into the
9013 if (howto
->partial_inplace
&& addend
!= 0)
9016 bfd_reloc_status_type rstat
;
9019 const char *sym_name
;
9021 size
= bfd_get_reloc_size (howto
);
9022 buf
= bfd_zmalloc (size
);
9025 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9032 case bfd_reloc_outofrange
:
9035 case bfd_reloc_overflow
:
9036 if (link_order
->type
== bfd_section_reloc_link_order
)
9037 sym_name
= bfd_section_name (output_bfd
,
9038 link_order
->u
.reloc
.p
->u
.section
);
9040 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9041 if (! ((*info
->callbacks
->reloc_overflow
)
9042 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9043 NULL
, (bfd_vma
) 0)))
9050 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9051 link_order
->offset
, size
);
9057 /* The address of a reloc is relative to the section in a
9058 relocatable file, and is a virtual address in an executable
9060 offset
= link_order
->offset
;
9061 if (! info
->relocatable
)
9062 offset
+= output_section
->vma
;
9064 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9066 irel
[i
].r_offset
= offset
;
9068 irel
[i
].r_addend
= 0;
9070 if (bed
->s
->arch_size
== 32)
9071 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9073 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9075 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9076 erel
= rel_hdr
->contents
;
9077 if (rel_hdr
->sh_type
== SHT_REL
)
9079 erel
+= (elf_section_data (output_section
)->rel_count
9080 * bed
->s
->sizeof_rel
);
9081 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9085 irel
[0].r_addend
= addend
;
9086 erel
+= (elf_section_data (output_section
)->rel_count
9087 * bed
->s
->sizeof_rela
);
9088 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9091 ++elf_section_data (output_section
)->rel_count
;
9097 /* Get the output vma of the section pointed to by the sh_link field. */
9100 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9102 Elf_Internal_Shdr
**elf_shdrp
;
9106 s
= p
->u
.indirect
.section
;
9107 elf_shdrp
= elf_elfsections (s
->owner
);
9108 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9109 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9111 The Intel C compiler generates SHT_IA_64_UNWIND with
9112 SHF_LINK_ORDER. But it doesn't set the sh_link or
9113 sh_info fields. Hence we could get the situation
9114 where elfsec is 0. */
9117 const struct elf_backend_data
*bed
9118 = get_elf_backend_data (s
->owner
);
9119 if (bed
->link_order_error_handler
)
9120 bed
->link_order_error_handler
9121 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9126 s
= elf_shdrp
[elfsec
]->bfd_section
;
9127 return s
->output_section
->vma
+ s
->output_offset
;
9132 /* Compare two sections based on the locations of the sections they are
9133 linked to. Used by elf_fixup_link_order. */
9136 compare_link_order (const void * a
, const void * b
)
9141 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9142 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9149 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9150 order as their linked sections. Returns false if this could not be done
9151 because an output section includes both ordered and unordered
9152 sections. Ideally we'd do this in the linker proper. */
9155 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9160 struct bfd_link_order
*p
;
9162 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9164 struct bfd_link_order
**sections
;
9165 asection
*s
, *other_sec
, *linkorder_sec
;
9169 linkorder_sec
= NULL
;
9172 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9174 if (p
->type
== bfd_indirect_link_order
)
9176 s
= p
->u
.indirect
.section
;
9178 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9179 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9180 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9181 && elfsec
< elf_numsections (sub
)
9182 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
9196 if (seen_other
&& seen_linkorder
)
9198 if (other_sec
&& linkorder_sec
)
9199 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9201 linkorder_sec
->owner
, other_sec
,
9204 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9206 bfd_set_error (bfd_error_bad_value
);
9211 if (!seen_linkorder
)
9214 sections
= (struct bfd_link_order
**)
9215 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9218 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9220 sections
[seen_linkorder
++] = p
;
9222 /* Sort the input sections in the order of their linked section. */
9223 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9224 compare_link_order
);
9226 /* Change the offsets of the sections. */
9228 for (n
= 0; n
< seen_linkorder
; n
++)
9230 s
= sections
[n
]->u
.indirect
.section
;
9231 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9232 s
->output_offset
= offset
;
9233 sections
[n
]->offset
= offset
;
9234 offset
+= sections
[n
]->size
;
9241 /* Do the final step of an ELF link. */
9244 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9246 bfd_boolean dynamic
;
9247 bfd_boolean emit_relocs
;
9249 struct elf_final_link_info finfo
;
9250 register asection
*o
;
9251 register struct bfd_link_order
*p
;
9253 bfd_size_type max_contents_size
;
9254 bfd_size_type max_external_reloc_size
;
9255 bfd_size_type max_internal_reloc_count
;
9256 bfd_size_type max_sym_count
;
9257 bfd_size_type max_sym_shndx_count
;
9259 Elf_Internal_Sym elfsym
;
9261 Elf_Internal_Shdr
*symtab_hdr
;
9262 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9263 Elf_Internal_Shdr
*symstrtab_hdr
;
9264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9265 struct elf_outext_info eoinfo
;
9267 size_t relativecount
= 0;
9268 asection
*reldyn
= 0;
9270 asection
*attr_section
= NULL
;
9271 bfd_vma attr_size
= 0;
9272 const char *std_attrs_section
;
9274 if (! is_elf_hash_table (info
->hash
))
9278 abfd
->flags
|= DYNAMIC
;
9280 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9281 dynobj
= elf_hash_table (info
)->dynobj
;
9283 emit_relocs
= (info
->relocatable
9284 || info
->emitrelocations
);
9287 finfo
.output_bfd
= abfd
;
9288 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9289 if (finfo
.symstrtab
== NULL
)
9294 finfo
.dynsym_sec
= NULL
;
9295 finfo
.hash_sec
= NULL
;
9296 finfo
.symver_sec
= NULL
;
9300 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9301 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9302 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9303 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9304 /* Note that it is OK if symver_sec is NULL. */
9307 finfo
.contents
= NULL
;
9308 finfo
.external_relocs
= NULL
;
9309 finfo
.internal_relocs
= NULL
;
9310 finfo
.external_syms
= NULL
;
9311 finfo
.locsym_shndx
= NULL
;
9312 finfo
.internal_syms
= NULL
;
9313 finfo
.indices
= NULL
;
9314 finfo
.sections
= NULL
;
9315 finfo
.symbuf
= NULL
;
9316 finfo
.symshndxbuf
= NULL
;
9317 finfo
.symbuf_count
= 0;
9318 finfo
.shndxbuf_size
= 0;
9320 /* The object attributes have been merged. Remove the input
9321 sections from the link, and set the contents of the output
9323 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
9324 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9326 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
9327 || strcmp (o
->name
, ".gnu.attributes") == 0)
9329 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9331 asection
*input_section
;
9333 if (p
->type
!= bfd_indirect_link_order
)
9335 input_section
= p
->u
.indirect
.section
;
9336 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9337 elf_link_input_bfd ignores this section. */
9338 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
9341 attr_size
= bfd_elf_obj_attr_size (abfd
);
9344 bfd_set_section_size (abfd
, o
, attr_size
);
9346 /* Skip this section later on. */
9347 o
->map_head
.link_order
= NULL
;
9350 o
->flags
|= SEC_EXCLUDE
;
9354 /* Count up the number of relocations we will output for each output
9355 section, so that we know the sizes of the reloc sections. We
9356 also figure out some maximum sizes. */
9357 max_contents_size
= 0;
9358 max_external_reloc_size
= 0;
9359 max_internal_reloc_count
= 0;
9361 max_sym_shndx_count
= 0;
9363 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9365 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
9368 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9370 unsigned int reloc_count
= 0;
9371 struct bfd_elf_section_data
*esdi
= NULL
;
9372 unsigned int *rel_count1
;
9374 if (p
->type
== bfd_section_reloc_link_order
9375 || p
->type
== bfd_symbol_reloc_link_order
)
9377 else if (p
->type
== bfd_indirect_link_order
)
9381 sec
= p
->u
.indirect
.section
;
9382 esdi
= elf_section_data (sec
);
9384 /* Mark all sections which are to be included in the
9385 link. This will normally be every section. We need
9386 to do this so that we can identify any sections which
9387 the linker has decided to not include. */
9388 sec
->linker_mark
= TRUE
;
9390 if (sec
->flags
& SEC_MERGE
)
9393 if (info
->relocatable
|| info
->emitrelocations
)
9394 reloc_count
= sec
->reloc_count
;
9395 else if (bed
->elf_backend_count_relocs
)
9397 Elf_Internal_Rela
* relocs
;
9399 relocs
= _bfd_elf_link_read_relocs (sec
->owner
, sec
,
9406 = (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
9408 if (elf_section_data (sec
)->relocs
!= relocs
)
9413 if (sec
->rawsize
> max_contents_size
)
9414 max_contents_size
= sec
->rawsize
;
9415 if (sec
->size
> max_contents_size
)
9416 max_contents_size
= sec
->size
;
9418 /* We are interested in just local symbols, not all
9420 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
9421 && (sec
->owner
->flags
& DYNAMIC
) == 0)
9425 if (elf_bad_symtab (sec
->owner
))
9426 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
9427 / bed
->s
->sizeof_sym
);
9429 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
9431 if (sym_count
> max_sym_count
)
9432 max_sym_count
= sym_count
;
9434 if (sym_count
> max_sym_shndx_count
9435 && elf_symtab_shndx (sec
->owner
) != 0)
9436 max_sym_shndx_count
= sym_count
;
9438 if ((sec
->flags
& SEC_RELOC
) != 0)
9442 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
9443 if (ext_size
> max_external_reloc_size
)
9444 max_external_reloc_size
= ext_size
;
9445 if (sec
->reloc_count
> max_internal_reloc_count
)
9446 max_internal_reloc_count
= sec
->reloc_count
;
9451 if (reloc_count
== 0)
9454 o
->reloc_count
+= reloc_count
;
9456 /* MIPS may have a mix of REL and RELA relocs on sections.
9457 To support this curious ABI we keep reloc counts in
9458 elf_section_data too. We must be careful to add the
9459 relocations from the input section to the right output
9460 count. FIXME: Get rid of one count. We have
9461 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9462 rel_count1
= &esdo
->rel_count
;
9465 bfd_boolean same_size
;
9466 bfd_size_type entsize1
;
9468 entsize1
= esdi
->rel_hdr
.sh_entsize
;
9469 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
9470 || entsize1
== bed
->s
->sizeof_rela
);
9471 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
9474 rel_count1
= &esdo
->rel_count2
;
9476 if (esdi
->rel_hdr2
!= NULL
)
9478 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
9479 unsigned int alt_count
;
9480 unsigned int *rel_count2
;
9482 BFD_ASSERT (entsize2
!= entsize1
9483 && (entsize2
== bed
->s
->sizeof_rel
9484 || entsize2
== bed
->s
->sizeof_rela
));
9486 rel_count2
= &esdo
->rel_count2
;
9488 rel_count2
= &esdo
->rel_count
;
9490 /* The following is probably too simplistic if the
9491 backend counts output relocs unusually. */
9492 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
9493 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
9494 *rel_count2
+= alt_count
;
9495 reloc_count
-= alt_count
;
9498 *rel_count1
+= reloc_count
;
9501 if (o
->reloc_count
> 0)
9502 o
->flags
|= SEC_RELOC
;
9505 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9506 set it (this is probably a bug) and if it is set
9507 assign_section_numbers will create a reloc section. */
9508 o
->flags
&=~ SEC_RELOC
;
9511 /* If the SEC_ALLOC flag is not set, force the section VMA to
9512 zero. This is done in elf_fake_sections as well, but forcing
9513 the VMA to 0 here will ensure that relocs against these
9514 sections are handled correctly. */
9515 if ((o
->flags
& SEC_ALLOC
) == 0
9516 && ! o
->user_set_vma
)
9520 if (! info
->relocatable
&& merged
)
9521 elf_link_hash_traverse (elf_hash_table (info
),
9522 _bfd_elf_link_sec_merge_syms
, abfd
);
9524 /* Figure out the file positions for everything but the symbol table
9525 and the relocs. We set symcount to force assign_section_numbers
9526 to create a symbol table. */
9527 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
9528 BFD_ASSERT (! abfd
->output_has_begun
);
9529 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
9532 /* Set sizes, and assign file positions for reloc sections. */
9533 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9535 if ((o
->flags
& SEC_RELOC
) != 0)
9537 if (!(_bfd_elf_link_size_reloc_section
9538 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
9541 if (elf_section_data (o
)->rel_hdr2
9542 && !(_bfd_elf_link_size_reloc_section
9543 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
9547 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9548 to count upwards while actually outputting the relocations. */
9549 elf_section_data (o
)->rel_count
= 0;
9550 elf_section_data (o
)->rel_count2
= 0;
9553 _bfd_elf_assign_file_positions_for_relocs (abfd
);
9555 /* We have now assigned file positions for all the sections except
9556 .symtab and .strtab. We start the .symtab section at the current
9557 file position, and write directly to it. We build the .strtab
9558 section in memory. */
9559 bfd_get_symcount (abfd
) = 0;
9560 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9561 /* sh_name is set in prep_headers. */
9562 symtab_hdr
->sh_type
= SHT_SYMTAB
;
9563 /* sh_flags, sh_addr and sh_size all start off zero. */
9564 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
9565 /* sh_link is set in assign_section_numbers. */
9566 /* sh_info is set below. */
9567 /* sh_offset is set just below. */
9568 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
9570 off
= elf_tdata (abfd
)->next_file_pos
;
9571 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
9573 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9574 incorrect. We do not yet know the size of the .symtab section.
9575 We correct next_file_pos below, after we do know the size. */
9577 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9578 continuously seeking to the right position in the file. */
9579 if (! info
->keep_memory
|| max_sym_count
< 20)
9580 finfo
.symbuf_size
= 20;
9582 finfo
.symbuf_size
= max_sym_count
;
9583 amt
= finfo
.symbuf_size
;
9584 amt
*= bed
->s
->sizeof_sym
;
9585 finfo
.symbuf
= bfd_malloc (amt
);
9586 if (finfo
.symbuf
== NULL
)
9588 if (elf_numsections (abfd
) > SHN_LORESERVE
)
9590 /* Wild guess at number of output symbols. realloc'd as needed. */
9591 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
9592 finfo
.shndxbuf_size
= amt
;
9593 amt
*= sizeof (Elf_External_Sym_Shndx
);
9594 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
9595 if (finfo
.symshndxbuf
== NULL
)
9599 /* Start writing out the symbol table. The first symbol is always a
9601 if (info
->strip
!= strip_all
9604 elfsym
.st_value
= 0;
9607 elfsym
.st_other
= 0;
9608 elfsym
.st_shndx
= SHN_UNDEF
;
9609 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
9614 /* Output a symbol for each section. We output these even if we are
9615 discarding local symbols, since they are used for relocs. These
9616 symbols have no names. We store the index of each one in the
9617 index field of the section, so that we can find it again when
9618 outputting relocs. */
9619 if (info
->strip
!= strip_all
9623 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9624 elfsym
.st_other
= 0;
9625 elfsym
.st_value
= 0;
9626 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9628 o
= bfd_section_from_elf_index (abfd
, i
);
9631 o
->target_index
= bfd_get_symcount (abfd
);
9632 elfsym
.st_shndx
= i
;
9633 if (!info
->relocatable
)
9634 elfsym
.st_value
= o
->vma
;
9635 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
9638 if (i
== SHN_LORESERVE
- 1)
9639 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
9643 /* Allocate some memory to hold information read in from the input
9645 if (max_contents_size
!= 0)
9647 finfo
.contents
= bfd_malloc (max_contents_size
);
9648 if (finfo
.contents
== NULL
)
9652 if (max_external_reloc_size
!= 0)
9654 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
9655 if (finfo
.external_relocs
== NULL
)
9659 if (max_internal_reloc_count
!= 0)
9661 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9662 amt
*= sizeof (Elf_Internal_Rela
);
9663 finfo
.internal_relocs
= bfd_malloc (amt
);
9664 if (finfo
.internal_relocs
== NULL
)
9668 if (max_sym_count
!= 0)
9670 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
9671 finfo
.external_syms
= bfd_malloc (amt
);
9672 if (finfo
.external_syms
== NULL
)
9675 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
9676 finfo
.internal_syms
= bfd_malloc (amt
);
9677 if (finfo
.internal_syms
== NULL
)
9680 amt
= max_sym_count
* sizeof (long);
9681 finfo
.indices
= bfd_malloc (amt
);
9682 if (finfo
.indices
== NULL
)
9685 amt
= max_sym_count
* sizeof (asection
*);
9686 finfo
.sections
= bfd_malloc (amt
);
9687 if (finfo
.sections
== NULL
)
9691 if (max_sym_shndx_count
!= 0)
9693 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
9694 finfo
.locsym_shndx
= bfd_malloc (amt
);
9695 if (finfo
.locsym_shndx
== NULL
)
9699 if (elf_hash_table (info
)->tls_sec
)
9701 bfd_vma base
, end
= 0;
9704 for (sec
= elf_hash_table (info
)->tls_sec
;
9705 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
9708 bfd_size_type size
= sec
->size
;
9711 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
9713 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
9715 size
= o
->offset
+ o
->size
;
9717 end
= sec
->vma
+ size
;
9719 base
= elf_hash_table (info
)->tls_sec
->vma
;
9720 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
9721 elf_hash_table (info
)->tls_size
= end
- base
;
9724 /* Reorder SHF_LINK_ORDER sections. */
9725 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9727 if (!elf_fixup_link_order (abfd
, o
))
9731 /* Since ELF permits relocations to be against local symbols, we
9732 must have the local symbols available when we do the relocations.
9733 Since we would rather only read the local symbols once, and we
9734 would rather not keep them in memory, we handle all the
9735 relocations for a single input file at the same time.
9737 Unfortunately, there is no way to know the total number of local
9738 symbols until we have seen all of them, and the local symbol
9739 indices precede the global symbol indices. This means that when
9740 we are generating relocatable output, and we see a reloc against
9741 a global symbol, we can not know the symbol index until we have
9742 finished examining all the local symbols to see which ones we are
9743 going to output. To deal with this, we keep the relocations in
9744 memory, and don't output them until the end of the link. This is
9745 an unfortunate waste of memory, but I don't see a good way around
9746 it. Fortunately, it only happens when performing a relocatable
9747 link, which is not the common case. FIXME: If keep_memory is set
9748 we could write the relocs out and then read them again; I don't
9749 know how bad the memory loss will be. */
9751 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9752 sub
->output_has_begun
= FALSE
;
9753 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9755 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9757 if (p
->type
== bfd_indirect_link_order
9758 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
9759 == bfd_target_elf_flavour
)
9760 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
9762 if (! sub
->output_has_begun
)
9764 if (! elf_link_input_bfd (&finfo
, sub
))
9766 sub
->output_has_begun
= TRUE
;
9769 else if (p
->type
== bfd_section_reloc_link_order
9770 || p
->type
== bfd_symbol_reloc_link_order
)
9772 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
9777 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
9783 /* Free symbol buffer if needed. */
9784 if (!info
->reduce_memory_overheads
)
9786 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9787 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9788 && elf_tdata (sub
)->symbuf
)
9790 free (elf_tdata (sub
)->symbuf
);
9791 elf_tdata (sub
)->symbuf
= NULL
;
9795 /* Output any global symbols that got converted to local in a
9796 version script or due to symbol visibility. We do this in a
9797 separate step since ELF requires all local symbols to appear
9798 prior to any global symbols. FIXME: We should only do this if
9799 some global symbols were, in fact, converted to become local.
9800 FIXME: Will this work correctly with the Irix 5 linker? */
9801 eoinfo
.failed
= FALSE
;
9802 eoinfo
.finfo
= &finfo
;
9803 eoinfo
.localsyms
= TRUE
;
9804 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9809 /* If backend needs to output some local symbols not present in the hash
9810 table, do it now. */
9811 if (bed
->elf_backend_output_arch_local_syms
)
9813 typedef bfd_boolean (*out_sym_func
)
9814 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9815 struct elf_link_hash_entry
*);
9817 if (! ((*bed
->elf_backend_output_arch_local_syms
)
9818 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9822 /* That wrote out all the local symbols. Finish up the symbol table
9823 with the global symbols. Even if we want to strip everything we
9824 can, we still need to deal with those global symbols that got
9825 converted to local in a version script. */
9827 /* The sh_info field records the index of the first non local symbol. */
9828 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
9831 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
9833 Elf_Internal_Sym sym
;
9834 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
9835 long last_local
= 0;
9837 /* Write out the section symbols for the output sections. */
9838 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
9844 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9847 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9853 dynindx
= elf_section_data (s
)->dynindx
;
9856 indx
= elf_section_data (s
)->this_idx
;
9857 BFD_ASSERT (indx
> 0);
9858 sym
.st_shndx
= indx
;
9859 if (! check_dynsym (abfd
, &sym
))
9861 sym
.st_value
= s
->vma
;
9862 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
9863 if (last_local
< dynindx
)
9864 last_local
= dynindx
;
9865 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9869 /* Write out the local dynsyms. */
9870 if (elf_hash_table (info
)->dynlocal
)
9872 struct elf_link_local_dynamic_entry
*e
;
9873 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
9878 sym
.st_size
= e
->isym
.st_size
;
9879 sym
.st_other
= e
->isym
.st_other
;
9881 /* Copy the internal symbol as is.
9882 Note that we saved a word of storage and overwrote
9883 the original st_name with the dynstr_index. */
9886 if (e
->isym
.st_shndx
!= SHN_UNDEF
9887 && (e
->isym
.st_shndx
< SHN_LORESERVE
9888 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
9890 s
= bfd_section_from_elf_index (e
->input_bfd
,
9894 elf_section_data (s
->output_section
)->this_idx
;
9895 if (! check_dynsym (abfd
, &sym
))
9897 sym
.st_value
= (s
->output_section
->vma
9899 + e
->isym
.st_value
);
9902 if (last_local
< e
->dynindx
)
9903 last_local
= e
->dynindx
;
9905 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
9906 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9910 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
9914 /* We get the global symbols from the hash table. */
9915 eoinfo
.failed
= FALSE
;
9916 eoinfo
.localsyms
= FALSE
;
9917 eoinfo
.finfo
= &finfo
;
9918 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9923 /* If backend needs to output some symbols not present in the hash
9924 table, do it now. */
9925 if (bed
->elf_backend_output_arch_syms
)
9927 typedef bfd_boolean (*out_sym_func
)
9928 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9929 struct elf_link_hash_entry
*);
9931 if (! ((*bed
->elf_backend_output_arch_syms
)
9932 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9936 /* Flush all symbols to the file. */
9937 if (! elf_link_flush_output_syms (&finfo
, bed
))
9940 /* Now we know the size of the symtab section. */
9941 off
+= symtab_hdr
->sh_size
;
9943 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
9944 if (symtab_shndx_hdr
->sh_name
!= 0)
9946 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
9947 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
9948 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
9949 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
9950 symtab_shndx_hdr
->sh_size
= amt
;
9952 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
9955 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
9956 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
9961 /* Finish up and write out the symbol string table (.strtab)
9963 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
9964 /* sh_name was set in prep_headers. */
9965 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9966 symstrtab_hdr
->sh_flags
= 0;
9967 symstrtab_hdr
->sh_addr
= 0;
9968 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
9969 symstrtab_hdr
->sh_entsize
= 0;
9970 symstrtab_hdr
->sh_link
= 0;
9971 symstrtab_hdr
->sh_info
= 0;
9972 /* sh_offset is set just below. */
9973 symstrtab_hdr
->sh_addralign
= 1;
9975 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
9976 elf_tdata (abfd
)->next_file_pos
= off
;
9978 if (bfd_get_symcount (abfd
) > 0)
9980 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9981 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9985 /* Adjust the relocs to have the correct symbol indices. */
9986 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9988 if ((o
->flags
& SEC_RELOC
) == 0)
9991 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9992 elf_section_data (o
)->rel_count
,
9993 elf_section_data (o
)->rel_hashes
);
9994 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9995 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9996 elf_section_data (o
)->rel_count2
,
9997 (elf_section_data (o
)->rel_hashes
9998 + elf_section_data (o
)->rel_count
));
10000 /* Set the reloc_count field to 0 to prevent write_relocs from
10001 trying to swap the relocs out itself. */
10002 o
->reloc_count
= 0;
10005 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10006 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10008 /* If we are linking against a dynamic object, or generating a
10009 shared library, finish up the dynamic linking information. */
10012 bfd_byte
*dyncon
, *dynconend
;
10014 /* Fix up .dynamic entries. */
10015 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10016 BFD_ASSERT (o
!= NULL
);
10018 dyncon
= o
->contents
;
10019 dynconend
= o
->contents
+ o
->size
;
10020 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10022 Elf_Internal_Dyn dyn
;
10026 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10033 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10035 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10037 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10038 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10041 dyn
.d_un
.d_val
= relativecount
;
10048 name
= info
->init_function
;
10051 name
= info
->fini_function
;
10054 struct elf_link_hash_entry
*h
;
10056 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10057 FALSE
, FALSE
, TRUE
);
10059 && (h
->root
.type
== bfd_link_hash_defined
10060 || h
->root
.type
== bfd_link_hash_defweak
))
10062 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
10063 o
= h
->root
.u
.def
.section
;
10064 if (o
->output_section
!= NULL
)
10065 dyn
.d_un
.d_val
+= (o
->output_section
->vma
10066 + o
->output_offset
);
10069 /* The symbol is imported from another shared
10070 library and does not apply to this one. */
10071 dyn
.d_un
.d_val
= 0;
10078 case DT_PREINIT_ARRAYSZ
:
10079 name
= ".preinit_array";
10081 case DT_INIT_ARRAYSZ
:
10082 name
= ".init_array";
10084 case DT_FINI_ARRAYSZ
:
10085 name
= ".fini_array";
10087 o
= bfd_get_section_by_name (abfd
, name
);
10090 (*_bfd_error_handler
)
10091 (_("%B: could not find output section %s"), abfd
, name
);
10095 (*_bfd_error_handler
)
10096 (_("warning: %s section has zero size"), name
);
10097 dyn
.d_un
.d_val
= o
->size
;
10100 case DT_PREINIT_ARRAY
:
10101 name
= ".preinit_array";
10103 case DT_INIT_ARRAY
:
10104 name
= ".init_array";
10106 case DT_FINI_ARRAY
:
10107 name
= ".fini_array";
10114 name
= ".gnu.hash";
10123 name
= ".gnu.version_d";
10126 name
= ".gnu.version_r";
10129 name
= ".gnu.version";
10131 o
= bfd_get_section_by_name (abfd
, name
);
10134 (*_bfd_error_handler
)
10135 (_("%B: could not find output section %s"), abfd
, name
);
10138 dyn
.d_un
.d_ptr
= o
->vma
;
10145 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10149 dyn
.d_un
.d_val
= 0;
10150 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10152 Elf_Internal_Shdr
*hdr
;
10154 hdr
= elf_elfsections (abfd
)[i
];
10155 if (hdr
->sh_type
== type
10156 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10158 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10159 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10162 if (dyn
.d_un
.d_val
== 0
10163 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
10164 dyn
.d_un
.d_val
= hdr
->sh_addr
;
10170 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10174 /* If we have created any dynamic sections, then output them. */
10175 if (dynobj
!= NULL
)
10177 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10180 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10181 if (info
->warn_shared_textrel
&& info
->shared
)
10183 bfd_byte
*dyncon
, *dynconend
;
10185 /* Fix up .dynamic entries. */
10186 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10187 BFD_ASSERT (o
!= NULL
);
10189 dyncon
= o
->contents
;
10190 dynconend
= o
->contents
+ o
->size
;
10191 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10193 Elf_Internal_Dyn dyn
;
10195 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10197 if (dyn
.d_tag
== DT_TEXTREL
)
10199 info
->callbacks
->einfo
10200 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10206 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10208 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10210 || o
->output_section
== bfd_abs_section_ptr
)
10212 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10214 /* At this point, we are only interested in sections
10215 created by _bfd_elf_link_create_dynamic_sections. */
10218 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10220 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10222 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10224 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10226 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10228 (file_ptr
) o
->output_offset
,
10234 /* The contents of the .dynstr section are actually in a
10236 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10237 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10238 || ! _bfd_elf_strtab_emit (abfd
,
10239 elf_hash_table (info
)->dynstr
))
10245 if (info
->relocatable
)
10247 bfd_boolean failed
= FALSE
;
10249 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10254 /* If we have optimized stabs strings, output them. */
10255 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10257 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10261 if (info
->eh_frame_hdr
)
10263 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10267 if (finfo
.symstrtab
!= NULL
)
10268 _bfd_stringtab_free (finfo
.symstrtab
);
10269 if (finfo
.contents
!= NULL
)
10270 free (finfo
.contents
);
10271 if (finfo
.external_relocs
!= NULL
)
10272 free (finfo
.external_relocs
);
10273 if (finfo
.internal_relocs
!= NULL
)
10274 free (finfo
.internal_relocs
);
10275 if (finfo
.external_syms
!= NULL
)
10276 free (finfo
.external_syms
);
10277 if (finfo
.locsym_shndx
!= NULL
)
10278 free (finfo
.locsym_shndx
);
10279 if (finfo
.internal_syms
!= NULL
)
10280 free (finfo
.internal_syms
);
10281 if (finfo
.indices
!= NULL
)
10282 free (finfo
.indices
);
10283 if (finfo
.sections
!= NULL
)
10284 free (finfo
.sections
);
10285 if (finfo
.symbuf
!= NULL
)
10286 free (finfo
.symbuf
);
10287 if (finfo
.symshndxbuf
!= NULL
)
10288 free (finfo
.symshndxbuf
);
10289 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10291 if ((o
->flags
& SEC_RELOC
) != 0
10292 && elf_section_data (o
)->rel_hashes
!= NULL
)
10293 free (elf_section_data (o
)->rel_hashes
);
10296 elf_tdata (abfd
)->linker
= TRUE
;
10300 bfd_byte
*contents
= bfd_malloc (attr_size
);
10301 if (contents
== NULL
)
10303 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
10304 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
10311 if (finfo
.symstrtab
!= NULL
)
10312 _bfd_stringtab_free (finfo
.symstrtab
);
10313 if (finfo
.contents
!= NULL
)
10314 free (finfo
.contents
);
10315 if (finfo
.external_relocs
!= NULL
)
10316 free (finfo
.external_relocs
);
10317 if (finfo
.internal_relocs
!= NULL
)
10318 free (finfo
.internal_relocs
);
10319 if (finfo
.external_syms
!= NULL
)
10320 free (finfo
.external_syms
);
10321 if (finfo
.locsym_shndx
!= NULL
)
10322 free (finfo
.locsym_shndx
);
10323 if (finfo
.internal_syms
!= NULL
)
10324 free (finfo
.internal_syms
);
10325 if (finfo
.indices
!= NULL
)
10326 free (finfo
.indices
);
10327 if (finfo
.sections
!= NULL
)
10328 free (finfo
.sections
);
10329 if (finfo
.symbuf
!= NULL
)
10330 free (finfo
.symbuf
);
10331 if (finfo
.symshndxbuf
!= NULL
)
10332 free (finfo
.symshndxbuf
);
10333 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10335 if ((o
->flags
& SEC_RELOC
) != 0
10336 && elf_section_data (o
)->rel_hashes
!= NULL
)
10337 free (elf_section_data (o
)->rel_hashes
);
10343 /* Garbage collect unused sections. */
10345 /* Default gc_mark_hook. */
10348 _bfd_elf_gc_mark_hook (asection
*sec
,
10349 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10350 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
10351 struct elf_link_hash_entry
*h
,
10352 Elf_Internal_Sym
*sym
)
10356 switch (h
->root
.type
)
10358 case bfd_link_hash_defined
:
10359 case bfd_link_hash_defweak
:
10360 return h
->root
.u
.def
.section
;
10362 case bfd_link_hash_common
:
10363 return h
->root
.u
.c
.p
->section
;
10370 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
10375 /* The mark phase of garbage collection. For a given section, mark
10376 it and any sections in this section's group, and all the sections
10377 which define symbols to which it refers. */
10380 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
10382 elf_gc_mark_hook_fn gc_mark_hook
)
10386 asection
*group_sec
;
10390 /* Mark all the sections in the group. */
10391 group_sec
= elf_section_data (sec
)->next_in_group
;
10392 if (group_sec
&& !group_sec
->gc_mark
)
10393 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
10396 /* Look through the section relocs. */
10398 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
10399 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
10401 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
10402 Elf_Internal_Shdr
*symtab_hdr
;
10403 struct elf_link_hash_entry
**sym_hashes
;
10406 bfd
*input_bfd
= sec
->owner
;
10407 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
10408 Elf_Internal_Sym
*isym
= NULL
;
10411 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10412 sym_hashes
= elf_sym_hashes (input_bfd
);
10414 /* Read the local symbols. */
10415 if (elf_bad_symtab (input_bfd
))
10417 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10421 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
10423 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10424 if (isym
== NULL
&& nlocsyms
!= 0)
10426 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
10432 /* Read the relocations. */
10433 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
10434 info
->keep_memory
);
10435 if (relstart
== NULL
)
10440 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10442 if (bed
->s
->arch_size
== 32)
10447 for (rel
= relstart
; rel
< relend
; rel
++)
10449 unsigned long r_symndx
;
10451 struct elf_link_hash_entry
*h
;
10453 r_symndx
= rel
->r_info
>> r_sym_shift
;
10457 if (r_symndx
>= nlocsyms
10458 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
10460 h
= sym_hashes
[r_symndx
- extsymoff
];
10461 while (h
->root
.type
== bfd_link_hash_indirect
10462 || h
->root
.type
== bfd_link_hash_warning
)
10463 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10464 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
10468 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
10471 if (rsec
&& !rsec
->gc_mark
)
10473 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
10476 rsec
->gc_mark_from_eh
= 1;
10477 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
10486 if (elf_section_data (sec
)->relocs
!= relstart
)
10489 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
10491 if (! info
->keep_memory
)
10494 symtab_hdr
->contents
= (unsigned char *) isym
;
10501 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10503 struct elf_gc_sweep_symbol_info
10505 struct bfd_link_info
*info
;
10506 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
10511 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
10513 if (h
->root
.type
== bfd_link_hash_warning
)
10514 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10516 if ((h
->root
.type
== bfd_link_hash_defined
10517 || h
->root
.type
== bfd_link_hash_defweak
)
10518 && !h
->root
.u
.def
.section
->gc_mark
10519 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
10521 struct elf_gc_sweep_symbol_info
*inf
= data
;
10522 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
10528 /* The sweep phase of garbage collection. Remove all garbage sections. */
10530 typedef bfd_boolean (*gc_sweep_hook_fn
)
10531 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
10534 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
10537 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10538 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
10539 unsigned long section_sym_count
;
10540 struct elf_gc_sweep_symbol_info sweep_info
;
10542 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10546 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10549 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10551 /* Keep debug and special sections. */
10552 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
10553 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
10559 /* Skip sweeping sections already excluded. */
10560 if (o
->flags
& SEC_EXCLUDE
)
10563 /* Since this is early in the link process, it is simple
10564 to remove a section from the output. */
10565 o
->flags
|= SEC_EXCLUDE
;
10567 if (info
->print_gc_sections
&& o
->size
!= 0)
10568 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
10570 /* But we also have to update some of the relocation
10571 info we collected before. */
10573 && (o
->flags
& SEC_RELOC
) != 0
10574 && o
->reloc_count
> 0
10575 && !bfd_is_abs_section (o
->output_section
))
10577 Elf_Internal_Rela
*internal_relocs
;
10581 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
10582 info
->keep_memory
);
10583 if (internal_relocs
== NULL
)
10586 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
10588 if (elf_section_data (o
)->relocs
!= internal_relocs
)
10589 free (internal_relocs
);
10597 /* Remove the symbols that were in the swept sections from the dynamic
10598 symbol table. GCFIXME: Anyone know how to get them out of the
10599 static symbol table as well? */
10600 sweep_info
.info
= info
;
10601 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
10602 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
10605 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
10609 /* Propagate collected vtable information. This is called through
10610 elf_link_hash_traverse. */
10613 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
10615 if (h
->root
.type
== bfd_link_hash_warning
)
10616 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10618 /* Those that are not vtables. */
10619 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10622 /* Those vtables that do not have parents, we cannot merge. */
10623 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
10626 /* If we've already been done, exit. */
10627 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
10630 /* Make sure the parent's table is up to date. */
10631 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
10633 if (h
->vtable
->used
== NULL
)
10635 /* None of this table's entries were referenced. Re-use the
10637 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
10638 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
10643 bfd_boolean
*cu
, *pu
;
10645 /* Or the parent's entries into ours. */
10646 cu
= h
->vtable
->used
;
10648 pu
= h
->vtable
->parent
->vtable
->used
;
10651 const struct elf_backend_data
*bed
;
10652 unsigned int log_file_align
;
10654 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
10655 log_file_align
= bed
->s
->log_file_align
;
10656 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
10671 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
10674 bfd_vma hstart
, hend
;
10675 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
10676 const struct elf_backend_data
*bed
;
10677 unsigned int log_file_align
;
10679 if (h
->root
.type
== bfd_link_hash_warning
)
10680 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10682 /* Take care of both those symbols that do not describe vtables as
10683 well as those that are not loaded. */
10684 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10687 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
10688 || h
->root
.type
== bfd_link_hash_defweak
);
10690 sec
= h
->root
.u
.def
.section
;
10691 hstart
= h
->root
.u
.def
.value
;
10692 hend
= hstart
+ h
->size
;
10694 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
10696 return *(bfd_boolean
*) okp
= FALSE
;
10697 bed
= get_elf_backend_data (sec
->owner
);
10698 log_file_align
= bed
->s
->log_file_align
;
10700 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10702 for (rel
= relstart
; rel
< relend
; ++rel
)
10703 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
10705 /* If the entry is in use, do nothing. */
10706 if (h
->vtable
->used
10707 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
10709 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
10710 if (h
->vtable
->used
[entry
])
10713 /* Otherwise, kill it. */
10714 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
10720 /* Mark sections containing dynamically referenced symbols. When
10721 building shared libraries, we must assume that any visible symbol is
10725 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
10727 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
10729 if (h
->root
.type
== bfd_link_hash_warning
)
10730 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10732 if ((h
->root
.type
== bfd_link_hash_defined
10733 || h
->root
.type
== bfd_link_hash_defweak
)
10735 || (!info
->executable
10737 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
10738 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
10739 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
10744 /* Do mark and sweep of unused sections. */
10747 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
10749 bfd_boolean ok
= TRUE
;
10751 elf_gc_mark_hook_fn gc_mark_hook
;
10752 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10754 if (!bed
->can_gc_sections
10755 || info
->relocatable
10756 || info
->emitrelocations
10757 || !is_elf_hash_table (info
->hash
))
10759 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
10763 /* Apply transitive closure to the vtable entry usage info. */
10764 elf_link_hash_traverse (elf_hash_table (info
),
10765 elf_gc_propagate_vtable_entries_used
,
10770 /* Kill the vtable relocations that were not used. */
10771 elf_link_hash_traverse (elf_hash_table (info
),
10772 elf_gc_smash_unused_vtentry_relocs
,
10777 /* Mark dynamically referenced symbols. */
10778 if (elf_hash_table (info
)->dynamic_sections_created
)
10779 elf_link_hash_traverse (elf_hash_table (info
),
10780 bed
->gc_mark_dynamic_ref
,
10783 /* Grovel through relocs to find out who stays ... */
10784 gc_mark_hook
= bed
->gc_mark_hook
;
10785 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10789 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10792 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10793 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
10794 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10798 /* Allow the backend to mark additional target specific sections. */
10799 if (bed
->gc_mark_extra_sections
)
10800 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
10802 /* ... again for sections marked from eh_frame. */
10803 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10807 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10810 /* Keep .gcc_except_table.* if the associated .text.* (or the
10811 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10812 marked. This isn't very nice, but the proper solution,
10813 splitting .eh_frame up and using comdat doesn't pan out
10814 easily due to needing special relocs to handle the
10815 difference of two symbols in separate sections.
10816 Don't keep code sections referenced by .eh_frame. */
10817 #define TEXT_PREFIX ".text."
10818 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10819 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10820 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10821 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
10823 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
10826 const char *sec_name
;
10828 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
10830 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
10831 sec_name
= o
->name
+ o_name_prefix_len
;
10832 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
10833 tmp
= strlen (TEXT_PREFIX2
);
10834 if (tmp
> fn_name_prefix_len
)
10835 fn_name_prefix_len
= tmp
;
10837 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
10838 if (fn_name
== NULL
)
10841 /* Try the first prefix. */
10842 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
10843 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10845 /* Try the second prefix. */
10846 if (fn_text
== NULL
)
10848 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
10849 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10853 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
10857 /* If not using specially named exception table section,
10858 then keep whatever we are using. */
10859 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10864 /* ... and mark SEC_EXCLUDE for those that go. */
10865 return elf_gc_sweep (abfd
, info
);
10868 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10871 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
10873 struct elf_link_hash_entry
*h
,
10876 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
10877 struct elf_link_hash_entry
**search
, *child
;
10878 bfd_size_type extsymcount
;
10879 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10881 /* The sh_info field of the symtab header tells us where the
10882 external symbols start. We don't care about the local symbols at
10884 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
10885 if (!elf_bad_symtab (abfd
))
10886 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
10888 sym_hashes
= elf_sym_hashes (abfd
);
10889 sym_hashes_end
= sym_hashes
+ extsymcount
;
10891 /* Hunt down the child symbol, which is in this section at the same
10892 offset as the relocation. */
10893 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
10895 if ((child
= *search
) != NULL
10896 && (child
->root
.type
== bfd_link_hash_defined
10897 || child
->root
.type
== bfd_link_hash_defweak
)
10898 && child
->root
.u
.def
.section
== sec
10899 && child
->root
.u
.def
.value
== offset
)
10903 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
10904 abfd
, sec
, (unsigned long) offset
);
10905 bfd_set_error (bfd_error_invalid_operation
);
10909 if (!child
->vtable
)
10911 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
10912 if (!child
->vtable
)
10917 /* This *should* only be the absolute section. It could potentially
10918 be that someone has defined a non-global vtable though, which
10919 would be bad. It isn't worth paging in the local symbols to be
10920 sure though; that case should simply be handled by the assembler. */
10922 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
10925 child
->vtable
->parent
= h
;
10930 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10933 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
10934 asection
*sec ATTRIBUTE_UNUSED
,
10935 struct elf_link_hash_entry
*h
,
10938 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10939 unsigned int log_file_align
= bed
->s
->log_file_align
;
10943 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
10948 if (addend
>= h
->vtable
->size
)
10950 size_t size
, bytes
, file_align
;
10951 bfd_boolean
*ptr
= h
->vtable
->used
;
10953 /* While the symbol is undefined, we have to be prepared to handle
10955 file_align
= 1 << log_file_align
;
10956 if (h
->root
.type
== bfd_link_hash_undefined
)
10957 size
= addend
+ file_align
;
10961 if (addend
>= size
)
10963 /* Oops! We've got a reference past the defined end of
10964 the table. This is probably a bug -- shall we warn? */
10965 size
= addend
+ file_align
;
10968 size
= (size
+ file_align
- 1) & -file_align
;
10970 /* Allocate one extra entry for use as a "done" flag for the
10971 consolidation pass. */
10972 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
10976 ptr
= bfd_realloc (ptr
- 1, bytes
);
10982 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
10983 * sizeof (bfd_boolean
));
10984 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
10988 ptr
= bfd_zmalloc (bytes
);
10993 /* And arrange for that done flag to be at index -1. */
10994 h
->vtable
->used
= ptr
+ 1;
10995 h
->vtable
->size
= size
;
10998 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11003 struct alloc_got_off_arg
{
11005 unsigned int got_elt_size
;
11008 /* We need a special top-level link routine to convert got reference counts
11009 to real got offsets. */
11012 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11014 struct alloc_got_off_arg
*gofarg
= arg
;
11016 if (h
->root
.type
== bfd_link_hash_warning
)
11017 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11019 if (h
->got
.refcount
> 0)
11021 h
->got
.offset
= gofarg
->gotoff
;
11022 gofarg
->gotoff
+= gofarg
->got_elt_size
;
11025 h
->got
.offset
= (bfd_vma
) -1;
11030 /* And an accompanying bit to work out final got entry offsets once
11031 we're done. Should be called from final_link. */
11034 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11035 struct bfd_link_info
*info
)
11038 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11040 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
11041 struct alloc_got_off_arg gofarg
;
11043 if (! is_elf_hash_table (info
->hash
))
11046 /* The GOT offset is relative to the .got section, but the GOT header is
11047 put into the .got.plt section, if the backend uses it. */
11048 if (bed
->want_got_plt
)
11051 gotoff
= bed
->got_header_size
;
11053 /* Do the local .got entries first. */
11054 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11056 bfd_signed_vma
*local_got
;
11057 bfd_size_type j
, locsymcount
;
11058 Elf_Internal_Shdr
*symtab_hdr
;
11060 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11063 local_got
= elf_local_got_refcounts (i
);
11067 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11068 if (elf_bad_symtab (i
))
11069 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11071 locsymcount
= symtab_hdr
->sh_info
;
11073 for (j
= 0; j
< locsymcount
; ++j
)
11075 if (local_got
[j
] > 0)
11077 local_got
[j
] = gotoff
;
11078 gotoff
+= got_elt_size
;
11081 local_got
[j
] = (bfd_vma
) -1;
11085 /* Then the global .got entries. .plt refcounts are handled by
11086 adjust_dynamic_symbol */
11087 gofarg
.gotoff
= gotoff
;
11088 gofarg
.got_elt_size
= got_elt_size
;
11089 elf_link_hash_traverse (elf_hash_table (info
),
11090 elf_gc_allocate_got_offsets
,
11095 /* Many folk need no more in the way of final link than this, once
11096 got entry reference counting is enabled. */
11099 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11101 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11104 /* Invoke the regular ELF backend linker to do all the work. */
11105 return bfd_elf_final_link (abfd
, info
);
11109 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11111 struct elf_reloc_cookie
*rcookie
= cookie
;
11113 if (rcookie
->bad_symtab
)
11114 rcookie
->rel
= rcookie
->rels
;
11116 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11118 unsigned long r_symndx
;
11120 if (! rcookie
->bad_symtab
)
11121 if (rcookie
->rel
->r_offset
> offset
)
11123 if (rcookie
->rel
->r_offset
!= offset
)
11126 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11127 if (r_symndx
== SHN_UNDEF
)
11130 if (r_symndx
>= rcookie
->locsymcount
11131 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11133 struct elf_link_hash_entry
*h
;
11135 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11137 while (h
->root
.type
== bfd_link_hash_indirect
11138 || h
->root
.type
== bfd_link_hash_warning
)
11139 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11141 if ((h
->root
.type
== bfd_link_hash_defined
11142 || h
->root
.type
== bfd_link_hash_defweak
)
11143 && elf_discarded_section (h
->root
.u
.def
.section
))
11150 /* It's not a relocation against a global symbol,
11151 but it could be a relocation against a local
11152 symbol for a discarded section. */
11154 Elf_Internal_Sym
*isym
;
11156 /* Need to: get the symbol; get the section. */
11157 isym
= &rcookie
->locsyms
[r_symndx
];
11158 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
11160 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11161 if (isec
!= NULL
&& elf_discarded_section (isec
))
11170 /* Discard unneeded references to discarded sections.
11171 Returns TRUE if any section's size was changed. */
11172 /* This function assumes that the relocations are in sorted order,
11173 which is true for all known assemblers. */
11176 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11178 struct elf_reloc_cookie cookie
;
11179 asection
*stab
, *eh
;
11180 Elf_Internal_Shdr
*symtab_hdr
;
11181 const struct elf_backend_data
*bed
;
11183 unsigned int count
;
11184 bfd_boolean ret
= FALSE
;
11186 if (info
->traditional_format
11187 || !is_elf_hash_table (info
->hash
))
11190 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11192 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11195 bed
= get_elf_backend_data (abfd
);
11197 if ((abfd
->flags
& DYNAMIC
) != 0)
11201 if (!info
->relocatable
)
11203 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
11206 || bfd_is_abs_section (eh
->output_section
)))
11210 stab
= bfd_get_section_by_name (abfd
, ".stab");
11212 && (stab
->size
== 0
11213 || bfd_is_abs_section (stab
->output_section
)
11214 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
11219 && bed
->elf_backend_discard_info
== NULL
)
11222 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11223 cookie
.abfd
= abfd
;
11224 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
11225 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
11226 if (cookie
.bad_symtab
)
11228 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11229 cookie
.extsymoff
= 0;
11233 cookie
.locsymcount
= symtab_hdr
->sh_info
;
11234 cookie
.extsymoff
= symtab_hdr
->sh_info
;
11237 if (bed
->s
->arch_size
== 32)
11238 cookie
.r_sym_shift
= 8;
11240 cookie
.r_sym_shift
= 32;
11242 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11243 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
11245 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11246 cookie
.locsymcount
, 0,
11248 if (cookie
.locsyms
== NULL
)
11250 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11257 cookie
.rels
= NULL
;
11258 count
= stab
->reloc_count
;
11260 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
11261 info
->keep_memory
);
11262 if (cookie
.rels
!= NULL
)
11264 cookie
.rel
= cookie
.rels
;
11265 cookie
.relend
= cookie
.rels
;
11266 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11267 if (_bfd_discard_section_stabs (abfd
, stab
,
11268 elf_section_data (stab
)->sec_info
,
11269 bfd_elf_reloc_symbol_deleted_p
,
11272 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
11273 free (cookie
.rels
);
11279 cookie
.rels
= NULL
;
11280 count
= eh
->reloc_count
;
11282 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
11283 info
->keep_memory
);
11284 cookie
.rel
= cookie
.rels
;
11285 cookie
.relend
= cookie
.rels
;
11286 if (cookie
.rels
!= NULL
)
11287 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11289 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11290 bfd_elf_reloc_symbol_deleted_p
,
11294 if (cookie
.rels
!= NULL
11295 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
11296 free (cookie
.rels
);
11299 if (bed
->elf_backend_discard_info
!= NULL
11300 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11303 if (cookie
.locsyms
!= NULL
11304 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
11306 if (! info
->keep_memory
)
11307 free (cookie
.locsyms
);
11309 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
11313 if (info
->eh_frame_hdr
11314 && !info
->relocatable
11315 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
11322 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
11323 struct bfd_link_info
*info
)
11326 const char *name
, *p
;
11327 struct bfd_section_already_linked
*l
;
11328 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
11330 if (sec
->output_section
== bfd_abs_section_ptr
)
11333 flags
= sec
->flags
;
11335 /* Return if it isn't a linkonce section. A comdat group section
11336 also has SEC_LINK_ONCE set. */
11337 if ((flags
& SEC_LINK_ONCE
) == 0)
11340 /* Don't put group member sections on our list of already linked
11341 sections. They are handled as a group via their group section. */
11342 if (elf_sec_group (sec
) != NULL
)
11345 /* FIXME: When doing a relocatable link, we may have trouble
11346 copying relocations in other sections that refer to local symbols
11347 in the section being discarded. Those relocations will have to
11348 be converted somehow; as of this writing I'm not sure that any of
11349 the backends handle that correctly.
11351 It is tempting to instead not discard link once sections when
11352 doing a relocatable link (technically, they should be discarded
11353 whenever we are building constructors). However, that fails,
11354 because the linker winds up combining all the link once sections
11355 into a single large link once section, which defeats the purpose
11356 of having link once sections in the first place.
11358 Also, not merging link once sections in a relocatable link
11359 causes trouble for MIPS ELF, which relies on link once semantics
11360 to handle the .reginfo section correctly. */
11362 name
= bfd_get_section_name (abfd
, sec
);
11364 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
11365 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
11370 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
11372 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11374 /* We may have 2 different types of sections on the list: group
11375 sections and linkonce sections. Match like sections. */
11376 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
11377 && strcmp (name
, l
->sec
->name
) == 0
11378 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
11380 /* The section has already been linked. See if we should
11381 issue a warning. */
11382 switch (flags
& SEC_LINK_DUPLICATES
)
11387 case SEC_LINK_DUPLICATES_DISCARD
:
11390 case SEC_LINK_DUPLICATES_ONE_ONLY
:
11391 (*_bfd_error_handler
)
11392 (_("%B: ignoring duplicate section `%A'"),
11396 case SEC_LINK_DUPLICATES_SAME_SIZE
:
11397 if (sec
->size
!= l
->sec
->size
)
11398 (*_bfd_error_handler
)
11399 (_("%B: duplicate section `%A' has different size"),
11403 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
11404 if (sec
->size
!= l
->sec
->size
)
11405 (*_bfd_error_handler
)
11406 (_("%B: duplicate section `%A' has different size"),
11408 else if (sec
->size
!= 0)
11410 bfd_byte
*sec_contents
, *l_sec_contents
;
11412 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
11413 (*_bfd_error_handler
)
11414 (_("%B: warning: could not read contents of section `%A'"),
11416 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
11418 (*_bfd_error_handler
)
11419 (_("%B: warning: could not read contents of section `%A'"),
11420 l
->sec
->owner
, l
->sec
);
11421 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
11422 (*_bfd_error_handler
)
11423 (_("%B: warning: duplicate section `%A' has different contents"),
11427 free (sec_contents
);
11428 if (l_sec_contents
)
11429 free (l_sec_contents
);
11434 /* Set the output_section field so that lang_add_section
11435 does not create a lang_input_section structure for this
11436 section. Since there might be a symbol in the section
11437 being discarded, we must retain a pointer to the section
11438 which we are really going to use. */
11439 sec
->output_section
= bfd_abs_section_ptr
;
11440 sec
->kept_section
= l
->sec
;
11442 if (flags
& SEC_GROUP
)
11444 asection
*first
= elf_next_in_group (sec
);
11445 asection
*s
= first
;
11449 s
->output_section
= bfd_abs_section_ptr
;
11450 /* Record which group discards it. */
11451 s
->kept_section
= l
->sec
;
11452 s
= elf_next_in_group (s
);
11453 /* These lists are circular. */
11463 /* A single member comdat group section may be discarded by a
11464 linkonce section and vice versa. */
11466 if ((flags
& SEC_GROUP
) != 0)
11468 asection
*first
= elf_next_in_group (sec
);
11470 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
11471 /* Check this single member group against linkonce sections. */
11472 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11473 if ((l
->sec
->flags
& SEC_GROUP
) == 0
11474 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
11475 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
11477 first
->output_section
= bfd_abs_section_ptr
;
11478 first
->kept_section
= l
->sec
;
11479 sec
->output_section
= bfd_abs_section_ptr
;
11484 /* Check this linkonce section against single member groups. */
11485 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11486 if (l
->sec
->flags
& SEC_GROUP
)
11488 asection
*first
= elf_next_in_group (l
->sec
);
11491 && elf_next_in_group (first
) == first
11492 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
11494 sec
->output_section
= bfd_abs_section_ptr
;
11495 sec
->kept_section
= first
;
11500 /* This is the first section with this name. Record it. */
11501 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
11505 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
11507 return sym
->st_shndx
== SHN_COMMON
;
11511 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
11517 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
11519 return bfd_com_section_ptr
;