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 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 /* Define a symbol in a dynamic linkage section. */
33 struct elf_link_hash_entry
*
34 _bfd_elf_define_linkage_sym (bfd
*abfd
,
35 struct bfd_link_info
*info
,
39 struct elf_link_hash_entry
*h
;
40 struct bfd_link_hash_entry
*bh
;
41 const struct elf_backend_data
*bed
;
43 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h
->root
.type
= bfd_link_hash_new
;
54 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
56 get_elf_backend_data (abfd
)->collect
,
59 h
= (struct elf_link_hash_entry
*) bh
;
62 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
64 bed
= get_elf_backend_data (abfd
);
65 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
70 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
74 struct elf_link_hash_entry
*h
;
75 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
78 /* This function may be called more than once. */
79 s
= bfd_get_section_by_name (abfd
, ".got");
80 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
83 switch (bed
->s
->arch_size
)
94 bfd_set_error (bfd_error_bad_value
);
98 flags
= bed
->dynamic_sec_flags
;
100 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
102 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
105 if (bed
->want_got_plt
)
107 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
109 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
113 if (bed
->want_got_sym
)
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info
)->hgot
= h
;
125 /* The first bit of the global offset table is the header. */
126 s
->size
+= bed
->got_header_size
;
131 /* Create a strtab to hold the dynamic symbol names. */
133 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
135 struct elf_link_hash_table
*hash_table
;
137 hash_table
= elf_hash_table (info
);
138 if (hash_table
->dynobj
== NULL
)
139 hash_table
->dynobj
= abfd
;
141 if (hash_table
->dynstr
== NULL
)
143 hash_table
->dynstr
= _bfd_elf_strtab_init ();
144 if (hash_table
->dynstr
== NULL
)
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
158 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
161 register asection
*s
;
162 const struct elf_backend_data
*bed
;
164 if (! is_elf_hash_table (info
->hash
))
167 if (elf_hash_table (info
)->dynamic_sections_created
)
170 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
173 abfd
= elf_hash_table (info
)->dynobj
;
174 bed
= get_elf_backend_data (abfd
);
176 flags
= bed
->dynamic_sec_flags
;
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info
->executable
)
182 s
= bfd_make_section_with_flags (abfd
, ".interp",
183 flags
| SEC_READONLY
);
188 /* Create sections to hold version informations. These are removed
189 if they are not needed. */
190 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
191 flags
| SEC_READONLY
);
193 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
196 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
197 flags
| SEC_READONLY
);
199 || ! bfd_set_section_alignment (abfd
, s
, 1))
202 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
203 flags
| SEC_READONLY
);
205 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
208 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
209 flags
| SEC_READONLY
);
211 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
214 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
215 flags
| SEC_READONLY
);
219 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
221 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
224 /* The special symbol _DYNAMIC is always set to the start of the
225 .dynamic section. We could set _DYNAMIC in a linker script, but we
226 only want to define it if we are, in fact, creating a .dynamic
227 section. We don't want to define it if there is no .dynamic
228 section, since on some ELF platforms the start up code examines it
229 to decide how to initialize the process. */
230 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
235 s
= bfd_make_section_with_flags (abfd
, ".hash", flags
| SEC_READONLY
);
237 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
239 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
242 if (info
->emit_gnu_hash
)
244 s
= bfd_make_section_with_flags (abfd
, ".gnu.hash",
245 flags
| SEC_READONLY
);
247 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
249 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
250 4 32-bit words followed by variable count of 64-bit words, then
251 variable count of 32-bit words. */
252 if (bed
->s
->arch_size
== 64)
253 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
255 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
258 /* Let the backend create the rest of the sections. This lets the
259 backend set the right flags. The backend will normally create
260 the .got and .plt sections. */
261 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
264 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
269 /* Create dynamic sections when linking against a dynamic object. */
272 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
274 flagword flags
, pltflags
;
275 struct elf_link_hash_entry
*h
;
277 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
279 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
280 .rel[a].bss sections. */
281 flags
= bed
->dynamic_sec_flags
;
284 if (bed
->plt_not_loaded
)
285 /* We do not clear SEC_ALLOC here because we still want the OS to
286 allocate space for the section; it's just that there's nothing
287 to read in from the object file. */
288 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
290 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
291 if (bed
->plt_readonly
)
292 pltflags
|= SEC_READONLY
;
294 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
296 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
299 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 if (bed
->want_plt_sym
)
303 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
304 "_PROCEDURE_LINKAGE_TABLE_");
305 elf_hash_table (info
)->hplt
= h
;
310 s
= bfd_make_section_with_flags (abfd
,
311 (bed
->default_use_rela_p
312 ? ".rela.plt" : ".rel.plt"),
313 flags
| SEC_READONLY
);
315 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
318 if (! _bfd_elf_create_got_section (abfd
, info
))
321 if (bed
->want_dynbss
)
323 /* The .dynbss section is a place to put symbols which are defined
324 by dynamic objects, are referenced by regular objects, and are
325 not functions. We must allocate space for them in the process
326 image and use a R_*_COPY reloc to tell the dynamic linker to
327 initialize them at run time. The linker script puts the .dynbss
328 section into the .bss section of the final image. */
329 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
331 | SEC_LINKER_CREATED
));
335 /* The .rel[a].bss section holds copy relocs. This section is not
336 normally needed. We need to create it here, though, so that the
337 linker will map it to an output section. We can't just create it
338 only if we need it, because we will not know whether we need it
339 until we have seen all the input files, and the first time the
340 main linker code calls BFD after examining all the input files
341 (size_dynamic_sections) the input sections have already been
342 mapped to the output sections. If the section turns out not to
343 be needed, we can discard it later. We will never need this
344 section when generating a shared object, since they do not use
348 s
= bfd_make_section_with_flags (abfd
,
349 (bed
->default_use_rela_p
350 ? ".rela.bss" : ".rel.bss"),
351 flags
| SEC_READONLY
);
353 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
361 /* Record a new dynamic symbol. We record the dynamic symbols as we
362 read the input files, since we need to have a list of all of them
363 before we can determine the final sizes of the output sections.
364 Note that we may actually call this function even though we are not
365 going to output any dynamic symbols; in some cases we know that a
366 symbol should be in the dynamic symbol table, but only if there is
370 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
371 struct elf_link_hash_entry
*h
)
373 if (h
->dynindx
== -1)
375 struct elf_strtab_hash
*dynstr
;
380 /* XXX: The ABI draft says the linker must turn hidden and
381 internal symbols into STB_LOCAL symbols when producing the
382 DSO. However, if ld.so honors st_other in the dynamic table,
383 this would not be necessary. */
384 switch (ELF_ST_VISIBILITY (h
->other
))
388 if (h
->root
.type
!= bfd_link_hash_undefined
389 && h
->root
.type
!= bfd_link_hash_undefweak
)
392 if (!elf_hash_table (info
)->is_relocatable_executable
)
400 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
401 ++elf_hash_table (info
)->dynsymcount
;
403 dynstr
= elf_hash_table (info
)->dynstr
;
406 /* Create a strtab to hold the dynamic symbol names. */
407 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
412 /* We don't put any version information in the dynamic string
414 name
= h
->root
.root
.string
;
415 p
= strchr (name
, ELF_VER_CHR
);
417 /* We know that the p points into writable memory. In fact,
418 there are only a few symbols that have read-only names, being
419 those like _GLOBAL_OFFSET_TABLE_ that are created specially
420 by the backends. Most symbols will have names pointing into
421 an ELF string table read from a file, or to objalloc memory. */
424 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
429 if (indx
== (bfd_size_type
) -1)
431 h
->dynstr_index
= indx
;
437 /* Mark a symbol dynamic. */
440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
441 struct elf_link_hash_entry
*h
,
442 Elf_Internal_Sym
*sym
)
444 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
446 /* It may be called more than once on the same H. */
447 if(h
->dynamic
|| info
->relocatable
)
450 if ((info
->dynamic_data
451 && (h
->type
== STT_OBJECT
453 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
455 && h
->root
.type
== bfd_link_hash_new
456 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
460 /* Record an assignment to a symbol made by a linker script. We need
461 this in case some dynamic object refers to this symbol. */
464 bfd_elf_record_link_assignment (bfd
*output_bfd
,
465 struct bfd_link_info
*info
,
470 struct elf_link_hash_entry
*h
;
471 struct elf_link_hash_table
*htab
;
473 if (!is_elf_hash_table (info
->hash
))
476 htab
= elf_hash_table (info
);
477 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
481 /* Since we're defining the symbol, don't let it seem to have not
482 been defined. record_dynamic_symbol and size_dynamic_sections
483 may depend on this. */
484 if (h
->root
.type
== bfd_link_hash_undefweak
485 || h
->root
.type
== bfd_link_hash_undefined
)
487 h
->root
.type
= bfd_link_hash_new
;
488 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
489 bfd_link_repair_undef_list (&htab
->root
);
492 if (h
->root
.type
== bfd_link_hash_new
)
494 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
498 /* If this symbol is being provided by the linker script, and it is
499 currently defined by a dynamic object, but not by a regular
500 object, then mark it as undefined so that the generic linker will
501 force the correct value. */
505 h
->root
.type
= bfd_link_hash_undefined
;
507 /* If this symbol is not being provided by the linker script, and it is
508 currently defined by a dynamic object, but not by a regular object,
509 then clear out any version information because the symbol will not be
510 associated with the dynamic object any more. */
514 h
->verinfo
.verdef
= NULL
;
518 if (provide
&& hidden
)
520 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
522 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
523 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
526 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
528 if (!info
->relocatable
530 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
531 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
537 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
540 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
543 /* If this is a weak defined symbol, and we know a corresponding
544 real symbol from the same dynamic object, make sure the real
545 symbol is also made into a dynamic symbol. */
546 if (h
->u
.weakdef
!= NULL
547 && h
->u
.weakdef
->dynindx
== -1)
549 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
557 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
558 success, and 2 on a failure caused by attempting to record a symbol
559 in a discarded section, eg. a discarded link-once section symbol. */
562 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
567 struct elf_link_local_dynamic_entry
*entry
;
568 struct elf_link_hash_table
*eht
;
569 struct elf_strtab_hash
*dynstr
;
570 unsigned long dynstr_index
;
572 Elf_External_Sym_Shndx eshndx
;
573 char esym
[sizeof (Elf64_External_Sym
)];
575 if (! is_elf_hash_table (info
->hash
))
578 /* See if the entry exists already. */
579 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
580 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
583 amt
= sizeof (*entry
);
584 entry
= bfd_alloc (input_bfd
, amt
);
588 /* Go find the symbol, so that we can find it's name. */
589 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
590 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
592 bfd_release (input_bfd
, entry
);
596 if (entry
->isym
.st_shndx
!= SHN_UNDEF
597 && (entry
->isym
.st_shndx
< SHN_LORESERVE
598 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
602 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
603 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
605 /* We can still bfd_release here as nothing has done another
606 bfd_alloc. We can't do this later in this function. */
607 bfd_release (input_bfd
, entry
);
612 name
= (bfd_elf_string_from_elf_section
613 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
614 entry
->isym
.st_name
));
616 dynstr
= elf_hash_table (info
)->dynstr
;
619 /* Create a strtab to hold the dynamic symbol names. */
620 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
625 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
626 if (dynstr_index
== (unsigned long) -1)
628 entry
->isym
.st_name
= dynstr_index
;
630 eht
= elf_hash_table (info
);
632 entry
->next
= eht
->dynlocal
;
633 eht
->dynlocal
= entry
;
634 entry
->input_bfd
= input_bfd
;
635 entry
->input_indx
= input_indx
;
638 /* Whatever binding the symbol had before, it's now local. */
640 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
642 /* The dynindx will be set at the end of size_dynamic_sections. */
647 /* Return the dynindex of a local dynamic symbol. */
650 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
654 struct elf_link_local_dynamic_entry
*e
;
656 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
657 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
662 /* This function is used to renumber the dynamic symbols, if some of
663 them are removed because they are marked as local. This is called
664 via elf_link_hash_traverse. */
667 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
670 size_t *count
= data
;
672 if (h
->root
.type
== bfd_link_hash_warning
)
673 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
678 if (h
->dynindx
!= -1)
679 h
->dynindx
= ++(*count
);
685 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
686 STB_LOCAL binding. */
689 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
692 size_t *count
= data
;
694 if (h
->root
.type
== bfd_link_hash_warning
)
695 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
697 if (!h
->forced_local
)
700 if (h
->dynindx
!= -1)
701 h
->dynindx
= ++(*count
);
706 /* Return true if the dynamic symbol for a given section should be
707 omitted when creating a shared library. */
709 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
710 struct bfd_link_info
*info
,
713 struct elf_link_hash_table
*htab
;
715 switch (elf_section_data (p
)->this_hdr
.sh_type
)
719 /* If sh_type is yet undecided, assume it could be
720 SHT_PROGBITS/SHT_NOBITS. */
722 htab
= elf_hash_table (info
);
723 if (p
== htab
->tls_sec
)
726 if (htab
->text_index_section
!= NULL
)
727 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
729 if (strcmp (p
->name
, ".got") == 0
730 || strcmp (p
->name
, ".got.plt") == 0
731 || strcmp (p
->name
, ".plt") == 0)
735 if (htab
->dynobj
!= NULL
736 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
737 && (ip
->flags
& SEC_LINKER_CREATED
)
738 && ip
->output_section
== p
)
743 /* There shouldn't be section relative relocations
744 against any other section. */
750 /* Assign dynsym indices. In a shared library we generate a section
751 symbol for each output section, which come first. Next come symbols
752 which have been forced to local binding. Then all of the back-end
753 allocated local dynamic syms, followed by the rest of the global
757 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
758 struct bfd_link_info
*info
,
759 unsigned long *section_sym_count
)
761 unsigned long dynsymcount
= 0;
763 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
765 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
767 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
768 if ((p
->flags
& SEC_EXCLUDE
) == 0
769 && (p
->flags
& SEC_ALLOC
) != 0
770 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
771 elf_section_data (p
)->dynindx
= ++dynsymcount
;
773 elf_section_data (p
)->dynindx
= 0;
775 *section_sym_count
= dynsymcount
;
777 elf_link_hash_traverse (elf_hash_table (info
),
778 elf_link_renumber_local_hash_table_dynsyms
,
781 if (elf_hash_table (info
)->dynlocal
)
783 struct elf_link_local_dynamic_entry
*p
;
784 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
785 p
->dynindx
= ++dynsymcount
;
788 elf_link_hash_traverse (elf_hash_table (info
),
789 elf_link_renumber_hash_table_dynsyms
,
792 /* There is an unused NULL entry at the head of the table which
793 we must account for in our count. Unless there weren't any
794 symbols, which means we'll have no table at all. */
795 if (dynsymcount
!= 0)
798 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
802 /* This function is called when we want to define a new symbol. It
803 handles the various cases which arise when we find a definition in
804 a dynamic object, or when there is already a definition in a
805 dynamic object. The new symbol is described by NAME, SYM, PSEC,
806 and PVALUE. We set SYM_HASH to the hash table entry. We set
807 OVERRIDE if the old symbol is overriding a new definition. We set
808 TYPE_CHANGE_OK if it is OK for the type to change. We set
809 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
810 change, we mean that we shouldn't warn if the type or size does
811 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
812 object is overridden by a regular object. */
815 _bfd_elf_merge_symbol (bfd
*abfd
,
816 struct bfd_link_info
*info
,
818 Elf_Internal_Sym
*sym
,
821 unsigned int *pold_alignment
,
822 struct elf_link_hash_entry
**sym_hash
,
824 bfd_boolean
*override
,
825 bfd_boolean
*type_change_ok
,
826 bfd_boolean
*size_change_ok
)
828 asection
*sec
, *oldsec
;
829 struct elf_link_hash_entry
*h
;
830 struct elf_link_hash_entry
*flip
;
833 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
834 bfd_boolean newweak
, oldweak
;
835 const struct elf_backend_data
*bed
;
841 bind
= ELF_ST_BIND (sym
->st_info
);
843 /* Silently discard TLS symbols from --just-syms. There's no way to
844 combine a static TLS block with a new TLS block for this executable. */
845 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
846 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
852 if (! bfd_is_und_section (sec
))
853 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
855 h
= ((struct elf_link_hash_entry
*)
856 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
861 /* This code is for coping with dynamic objects, and is only useful
862 if we are doing an ELF link. */
863 if (info
->hash
->creator
!= abfd
->xvec
)
866 /* For merging, we only care about real symbols. */
868 while (h
->root
.type
== bfd_link_hash_indirect
869 || h
->root
.type
== bfd_link_hash_warning
)
870 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
872 /* We have to check it for every instance since the first few may be
873 refereences and not all compilers emit symbol type for undefined
875 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
877 /* If we just created the symbol, mark it as being an ELF symbol.
878 Other than that, there is nothing to do--there is no merge issue
879 with a newly defined symbol--so we just return. */
881 if (h
->root
.type
== bfd_link_hash_new
)
887 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
890 switch (h
->root
.type
)
897 case bfd_link_hash_undefined
:
898 case bfd_link_hash_undefweak
:
899 oldbfd
= h
->root
.u
.undef
.abfd
;
903 case bfd_link_hash_defined
:
904 case bfd_link_hash_defweak
:
905 oldbfd
= h
->root
.u
.def
.section
->owner
;
906 oldsec
= h
->root
.u
.def
.section
;
909 case bfd_link_hash_common
:
910 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
911 oldsec
= h
->root
.u
.c
.p
->section
;
915 /* In cases involving weak versioned symbols, we may wind up trying
916 to merge a symbol with itself. Catch that here, to avoid the
917 confusion that results if we try to override a symbol with
918 itself. The additional tests catch cases like
919 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
920 dynamic object, which we do want to handle here. */
922 && ((abfd
->flags
& DYNAMIC
) == 0
926 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
927 respectively, is from a dynamic object. */
929 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
933 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
934 else if (oldsec
!= NULL
)
936 /* This handles the special SHN_MIPS_{TEXT,DATA} section
937 indices used by MIPS ELF. */
938 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
941 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
942 respectively, appear to be a definition rather than reference. */
944 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
946 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
947 && h
->root
.type
!= bfd_link_hash_undefweak
948 && h
->root
.type
!= bfd_link_hash_common
);
950 bed
= get_elf_backend_data (abfd
);
951 /* When we try to create a default indirect symbol from the dynamic
952 definition with the default version, we skip it if its type and
953 the type of existing regular definition mismatch. We only do it
954 if the existing regular definition won't be dynamic. */
955 if (pold_alignment
== NULL
957 && !info
->export_dynamic
962 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
963 && ELF_ST_TYPE (sym
->st_info
) != h
->type
964 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
965 && h
->type
!= STT_NOTYPE
966 && !(bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
967 && bed
->is_function_type (h
->type
)))
973 /* Check TLS symbol. We don't check undefined symbol introduced by
975 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
976 && ELF_ST_TYPE (sym
->st_info
) != h
->type
980 bfd_boolean ntdef
, tdef
;
981 asection
*ntsec
, *tsec
;
983 if (h
->type
== STT_TLS
)
1003 (*_bfd_error_handler
)
1004 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1005 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1006 else if (!tdef
&& !ntdef
)
1007 (*_bfd_error_handler
)
1008 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1009 tbfd
, ntbfd
, h
->root
.root
.string
);
1011 (*_bfd_error_handler
)
1012 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1013 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1015 (*_bfd_error_handler
)
1016 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1017 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1019 bfd_set_error (bfd_error_bad_value
);
1023 /* We need to remember if a symbol has a definition in a dynamic
1024 object or is weak in all dynamic objects. Internal and hidden
1025 visibility will make it unavailable to dynamic objects. */
1026 if (newdyn
&& !h
->dynamic_def
)
1028 if (!bfd_is_und_section (sec
))
1032 /* Check if this symbol is weak in all dynamic objects. If it
1033 is the first time we see it in a dynamic object, we mark
1034 if it is weak. Otherwise, we clear it. */
1035 if (!h
->ref_dynamic
)
1037 if (bind
== STB_WEAK
)
1038 h
->dynamic_weak
= 1;
1040 else if (bind
!= STB_WEAK
)
1041 h
->dynamic_weak
= 0;
1045 /* If the old symbol has non-default visibility, we ignore the new
1046 definition from a dynamic object. */
1048 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1049 && !bfd_is_und_section (sec
))
1052 /* Make sure this symbol is dynamic. */
1054 /* A protected symbol has external availability. Make sure it is
1055 recorded as dynamic.
1057 FIXME: Should we check type and size for protected symbol? */
1058 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1059 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1064 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1067 /* If the new symbol with non-default visibility comes from a
1068 relocatable file and the old definition comes from a dynamic
1069 object, we remove the old definition. */
1070 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1072 /* Handle the case where the old dynamic definition is
1073 default versioned. We need to copy the symbol info from
1074 the symbol with default version to the normal one if it
1075 was referenced before. */
1078 const struct elf_backend_data
*bed
1079 = get_elf_backend_data (abfd
);
1080 struct elf_link_hash_entry
*vh
= *sym_hash
;
1081 vh
->root
.type
= h
->root
.type
;
1082 h
->root
.type
= bfd_link_hash_indirect
;
1083 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1084 /* Protected symbols will override the dynamic definition
1085 with default version. */
1086 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1088 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1089 vh
->dynamic_def
= 1;
1090 vh
->ref_dynamic
= 1;
1094 h
->root
.type
= vh
->root
.type
;
1095 vh
->ref_dynamic
= 0;
1096 /* We have to hide it here since it was made dynamic
1097 global with extra bits when the symbol info was
1098 copied from the old dynamic definition. */
1099 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1107 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1108 && bfd_is_und_section (sec
))
1110 /* If the new symbol is undefined and the old symbol was
1111 also undefined before, we need to make sure
1112 _bfd_generic_link_add_one_symbol doesn't mess
1113 up the linker hash table undefs list. Since the old
1114 definition came from a dynamic object, it is still on the
1116 h
->root
.type
= bfd_link_hash_undefined
;
1117 h
->root
.u
.undef
.abfd
= abfd
;
1121 h
->root
.type
= bfd_link_hash_new
;
1122 h
->root
.u
.undef
.abfd
= NULL
;
1131 /* FIXME: Should we check type and size for protected symbol? */
1137 /* Differentiate strong and weak symbols. */
1138 newweak
= bind
== STB_WEAK
;
1139 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1140 || h
->root
.type
== bfd_link_hash_undefweak
);
1142 /* If a new weak symbol definition comes from a regular file and the
1143 old symbol comes from a dynamic library, we treat the new one as
1144 strong. Similarly, an old weak symbol definition from a regular
1145 file is treated as strong when the new symbol comes from a dynamic
1146 library. Further, an old weak symbol from a dynamic library is
1147 treated as strong if the new symbol is from a dynamic library.
1148 This reflects the way glibc's ld.so works.
1150 Do this before setting *type_change_ok or *size_change_ok so that
1151 we warn properly when dynamic library symbols are overridden. */
1153 if (newdef
&& !newdyn
&& olddyn
)
1155 if (olddef
&& newdyn
)
1158 /* Allow changes between different types of funciton symbol. */
1159 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))
1160 && bed
->is_function_type (h
->type
))
1161 *type_change_ok
= TRUE
;
1163 /* It's OK to change the type if either the existing symbol or the
1164 new symbol is weak. A type change is also OK if the old symbol
1165 is undefined and the new symbol is defined. */
1170 && h
->root
.type
== bfd_link_hash_undefined
))
1171 *type_change_ok
= TRUE
;
1173 /* It's OK to change the size if either the existing symbol or the
1174 new symbol is weak, or if the old symbol is undefined. */
1177 || h
->root
.type
== bfd_link_hash_undefined
)
1178 *size_change_ok
= TRUE
;
1180 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1181 symbol, respectively, appears to be a common symbol in a dynamic
1182 object. If a symbol appears in an uninitialized section, and is
1183 not weak, and is not a function, then it may be a common symbol
1184 which was resolved when the dynamic object was created. We want
1185 to treat such symbols specially, because they raise special
1186 considerations when setting the symbol size: if the symbol
1187 appears as a common symbol in a regular object, and the size in
1188 the regular object is larger, we must make sure that we use the
1189 larger size. This problematic case can always be avoided in C,
1190 but it must be handled correctly when using Fortran shared
1193 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1194 likewise for OLDDYNCOMMON and OLDDEF.
1196 Note that this test is just a heuristic, and that it is quite
1197 possible to have an uninitialized symbol in a shared object which
1198 is really a definition, rather than a common symbol. This could
1199 lead to some minor confusion when the symbol really is a common
1200 symbol in some regular object. However, I think it will be
1206 && (sec
->flags
& SEC_ALLOC
) != 0
1207 && (sec
->flags
& SEC_LOAD
) == 0
1209 && !bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
1210 newdyncommon
= TRUE
;
1212 newdyncommon
= FALSE
;
1216 && h
->root
.type
== bfd_link_hash_defined
1218 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1219 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1221 && !bed
->is_function_type (h
->type
))
1222 olddyncommon
= TRUE
;
1224 olddyncommon
= FALSE
;
1226 /* We now know everything about the old and new symbols. We ask the
1227 backend to check if we can merge them. */
1228 if (bed
->merge_symbol
1229 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1230 pold_alignment
, skip
, override
,
1231 type_change_ok
, size_change_ok
,
1232 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1234 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1238 /* If both the old and the new symbols look like common symbols in a
1239 dynamic object, set the size of the symbol to the larger of the
1244 && sym
->st_size
!= h
->size
)
1246 /* Since we think we have two common symbols, issue a multiple
1247 common warning if desired. Note that we only warn if the
1248 size is different. If the size is the same, we simply let
1249 the old symbol override the new one as normally happens with
1250 symbols defined in dynamic objects. */
1252 if (! ((*info
->callbacks
->multiple_common
)
1253 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1254 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1257 if (sym
->st_size
> h
->size
)
1258 h
->size
= sym
->st_size
;
1260 *size_change_ok
= TRUE
;
1263 /* If we are looking at a dynamic object, and we have found a
1264 definition, we need to see if the symbol was already defined by
1265 some other object. If so, we want to use the existing
1266 definition, and we do not want to report a multiple symbol
1267 definition error; we do this by clobbering *PSEC to be
1268 bfd_und_section_ptr.
1270 We treat a common symbol as a definition if the symbol in the
1271 shared library is a function, since common symbols always
1272 represent variables; this can cause confusion in principle, but
1273 any such confusion would seem to indicate an erroneous program or
1274 shared library. We also permit a common symbol in a regular
1275 object to override a weak symbol in a shared object. */
1280 || (h
->root
.type
== bfd_link_hash_common
1282 || bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
))))))
1286 newdyncommon
= FALSE
;
1288 *psec
= sec
= bfd_und_section_ptr
;
1289 *size_change_ok
= TRUE
;
1291 /* If we get here when the old symbol is a common symbol, then
1292 we are explicitly letting it override a weak symbol or
1293 function in a dynamic object, and we don't want to warn about
1294 a type change. If the old symbol is a defined symbol, a type
1295 change warning may still be appropriate. */
1297 if (h
->root
.type
== bfd_link_hash_common
)
1298 *type_change_ok
= TRUE
;
1301 /* Handle the special case of an old common symbol merging with a
1302 new symbol which looks like a common symbol in a shared object.
1303 We change *PSEC and *PVALUE to make the new symbol look like a
1304 common symbol, and let _bfd_generic_link_add_one_symbol do the
1308 && h
->root
.type
== bfd_link_hash_common
)
1312 newdyncommon
= FALSE
;
1313 *pvalue
= sym
->st_size
;
1314 *psec
= sec
= bed
->common_section (oldsec
);
1315 *size_change_ok
= TRUE
;
1318 /* Skip weak definitions of symbols that are already defined. */
1319 if (newdef
&& olddef
&& newweak
)
1322 /* If the old symbol is from a dynamic object, and the new symbol is
1323 a definition which is not from a dynamic object, then the new
1324 symbol overrides the old symbol. Symbols from regular files
1325 always take precedence over symbols from dynamic objects, even if
1326 they are defined after the dynamic object in the link.
1328 As above, we again permit a common symbol in a regular object to
1329 override a definition in a shared object if the shared object
1330 symbol is a function or is weak. */
1335 || (bfd_is_com_section (sec
)
1337 || bed
->is_function_type (h
->type
))))
1342 /* Change the hash table entry to undefined, and let
1343 _bfd_generic_link_add_one_symbol do the right thing with the
1346 h
->root
.type
= bfd_link_hash_undefined
;
1347 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1348 *size_change_ok
= TRUE
;
1351 olddyncommon
= FALSE
;
1353 /* We again permit a type change when a common symbol may be
1354 overriding a function. */
1356 if (bfd_is_com_section (sec
))
1357 *type_change_ok
= TRUE
;
1359 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1362 /* This union may have been set to be non-NULL when this symbol
1363 was seen in a dynamic object. We must force the union to be
1364 NULL, so that it is correct for a regular symbol. */
1365 h
->verinfo
.vertree
= NULL
;
1368 /* Handle the special case of a new common symbol merging with an
1369 old symbol that looks like it might be a common symbol defined in
1370 a shared object. Note that we have already handled the case in
1371 which a new common symbol should simply override the definition
1372 in the shared library. */
1375 && bfd_is_com_section (sec
)
1378 /* It would be best if we could set the hash table entry to a
1379 common symbol, but we don't know what to use for the section
1380 or the alignment. */
1381 if (! ((*info
->callbacks
->multiple_common
)
1382 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1383 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1386 /* If the presumed common symbol in the dynamic object is
1387 larger, pretend that the new symbol has its size. */
1389 if (h
->size
> *pvalue
)
1392 /* We need to remember the alignment required by the symbol
1393 in the dynamic object. */
1394 BFD_ASSERT (pold_alignment
);
1395 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1398 olddyncommon
= FALSE
;
1400 h
->root
.type
= bfd_link_hash_undefined
;
1401 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1403 *size_change_ok
= TRUE
;
1404 *type_change_ok
= TRUE
;
1406 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1409 h
->verinfo
.vertree
= NULL
;
1414 /* Handle the case where we had a versioned symbol in a dynamic
1415 library and now find a definition in a normal object. In this
1416 case, we make the versioned symbol point to the normal one. */
1417 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1418 flip
->root
.type
= h
->root
.type
;
1419 h
->root
.type
= bfd_link_hash_indirect
;
1420 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1421 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1422 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1426 flip
->ref_dynamic
= 1;
1433 /* This function is called to create an indirect symbol from the
1434 default for the symbol with the default version if needed. The
1435 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1436 set DYNSYM if the new indirect symbol is dynamic. */
1439 _bfd_elf_add_default_symbol (bfd
*abfd
,
1440 struct bfd_link_info
*info
,
1441 struct elf_link_hash_entry
*h
,
1443 Elf_Internal_Sym
*sym
,
1446 bfd_boolean
*dynsym
,
1447 bfd_boolean override
)
1449 bfd_boolean type_change_ok
;
1450 bfd_boolean size_change_ok
;
1453 struct elf_link_hash_entry
*hi
;
1454 struct bfd_link_hash_entry
*bh
;
1455 const struct elf_backend_data
*bed
;
1456 bfd_boolean collect
;
1457 bfd_boolean dynamic
;
1459 size_t len
, shortlen
;
1462 /* If this symbol has a version, and it is the default version, we
1463 create an indirect symbol from the default name to the fully
1464 decorated name. This will cause external references which do not
1465 specify a version to be bound to this version of the symbol. */
1466 p
= strchr (name
, ELF_VER_CHR
);
1467 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1472 /* We are overridden by an old definition. We need to check if we
1473 need to create the indirect symbol from the default name. */
1474 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1476 BFD_ASSERT (hi
!= NULL
);
1479 while (hi
->root
.type
== bfd_link_hash_indirect
1480 || hi
->root
.type
== bfd_link_hash_warning
)
1482 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1488 bed
= get_elf_backend_data (abfd
);
1489 collect
= bed
->collect
;
1490 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1492 shortlen
= p
- name
;
1493 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1494 if (shortname
== NULL
)
1496 memcpy (shortname
, name
, shortlen
);
1497 shortname
[shortlen
] = '\0';
1499 /* We are going to create a new symbol. Merge it with any existing
1500 symbol with this name. For the purposes of the merge, act as
1501 though we were defining the symbol we just defined, although we
1502 actually going to define an indirect symbol. */
1503 type_change_ok
= FALSE
;
1504 size_change_ok
= FALSE
;
1506 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1507 NULL
, &hi
, &skip
, &override
,
1508 &type_change_ok
, &size_change_ok
))
1517 if (! (_bfd_generic_link_add_one_symbol
1518 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1519 0, name
, FALSE
, collect
, &bh
)))
1521 hi
= (struct elf_link_hash_entry
*) bh
;
1525 /* In this case the symbol named SHORTNAME is overriding the
1526 indirect symbol we want to add. We were planning on making
1527 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1528 is the name without a version. NAME is the fully versioned
1529 name, and it is the default version.
1531 Overriding means that we already saw a definition for the
1532 symbol SHORTNAME in a regular object, and it is overriding
1533 the symbol defined in the dynamic object.
1535 When this happens, we actually want to change NAME, the
1536 symbol we just added, to refer to SHORTNAME. This will cause
1537 references to NAME in the shared object to become references
1538 to SHORTNAME in the regular object. This is what we expect
1539 when we override a function in a shared object: that the
1540 references in the shared object will be mapped to the
1541 definition in the regular object. */
1543 while (hi
->root
.type
== bfd_link_hash_indirect
1544 || hi
->root
.type
== bfd_link_hash_warning
)
1545 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1547 h
->root
.type
= bfd_link_hash_indirect
;
1548 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1552 hi
->ref_dynamic
= 1;
1556 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1561 /* Now set HI to H, so that the following code will set the
1562 other fields correctly. */
1566 /* Check if HI is a warning symbol. */
1567 if (hi
->root
.type
== bfd_link_hash_warning
)
1568 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1570 /* If there is a duplicate definition somewhere, then HI may not
1571 point to an indirect symbol. We will have reported an error to
1572 the user in that case. */
1574 if (hi
->root
.type
== bfd_link_hash_indirect
)
1576 struct elf_link_hash_entry
*ht
;
1578 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1579 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1581 /* See if the new flags lead us to realize that the symbol must
1593 if (hi
->ref_regular
)
1599 /* We also need to define an indirection from the nondefault version
1603 len
= strlen (name
);
1604 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1605 if (shortname
== NULL
)
1607 memcpy (shortname
, name
, shortlen
);
1608 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1610 /* Once again, merge with any existing symbol. */
1611 type_change_ok
= FALSE
;
1612 size_change_ok
= FALSE
;
1614 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1615 NULL
, &hi
, &skip
, &override
,
1616 &type_change_ok
, &size_change_ok
))
1624 /* Here SHORTNAME is a versioned name, so we don't expect to see
1625 the type of override we do in the case above unless it is
1626 overridden by a versioned definition. */
1627 if (hi
->root
.type
!= bfd_link_hash_defined
1628 && hi
->root
.type
!= bfd_link_hash_defweak
)
1629 (*_bfd_error_handler
)
1630 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1636 if (! (_bfd_generic_link_add_one_symbol
1637 (info
, abfd
, shortname
, BSF_INDIRECT
,
1638 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1640 hi
= (struct elf_link_hash_entry
*) bh
;
1642 /* If there is a duplicate definition somewhere, then HI may not
1643 point to an indirect symbol. We will have reported an error
1644 to the user in that case. */
1646 if (hi
->root
.type
== bfd_link_hash_indirect
)
1648 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1650 /* See if the new flags lead us to realize that the symbol
1662 if (hi
->ref_regular
)
1672 /* This routine is used to export all defined symbols into the dynamic
1673 symbol table. It is called via elf_link_hash_traverse. */
1676 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1678 struct elf_info_failed
*eif
= data
;
1680 /* Ignore this if we won't export it. */
1681 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1684 /* Ignore indirect symbols. These are added by the versioning code. */
1685 if (h
->root
.type
== bfd_link_hash_indirect
)
1688 if (h
->root
.type
== bfd_link_hash_warning
)
1689 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1691 if (h
->dynindx
== -1
1695 struct bfd_elf_version_tree
*t
;
1696 struct bfd_elf_version_expr
*d
;
1698 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1700 if (t
->globals
.list
!= NULL
)
1702 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1707 if (t
->locals
.list
!= NULL
)
1709 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1718 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1729 /* Look through the symbols which are defined in other shared
1730 libraries and referenced here. Update the list of version
1731 dependencies. This will be put into the .gnu.version_r section.
1732 This function is called via elf_link_hash_traverse. */
1735 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1738 struct elf_find_verdep_info
*rinfo
= data
;
1739 Elf_Internal_Verneed
*t
;
1740 Elf_Internal_Vernaux
*a
;
1743 if (h
->root
.type
== bfd_link_hash_warning
)
1744 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1746 /* We only care about symbols defined in shared objects with version
1751 || h
->verinfo
.verdef
== NULL
)
1754 /* See if we already know about this version. */
1755 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1757 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1760 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1761 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1767 /* This is a new version. Add it to tree we are building. */
1772 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1775 rinfo
->failed
= TRUE
;
1779 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1780 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1781 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1785 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1787 /* Note that we are copying a string pointer here, and testing it
1788 above. If bfd_elf_string_from_elf_section is ever changed to
1789 discard the string data when low in memory, this will have to be
1791 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1793 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1794 a
->vna_nextptr
= t
->vn_auxptr
;
1796 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1799 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1806 /* Figure out appropriate versions for all the symbols. We may not
1807 have the version number script until we have read all of the input
1808 files, so until that point we don't know which symbols should be
1809 local. This function is called via elf_link_hash_traverse. */
1812 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1814 struct elf_assign_sym_version_info
*sinfo
;
1815 struct bfd_link_info
*info
;
1816 const struct elf_backend_data
*bed
;
1817 struct elf_info_failed eif
;
1824 if (h
->root
.type
== bfd_link_hash_warning
)
1825 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1827 /* Fix the symbol flags. */
1830 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1833 sinfo
->failed
= TRUE
;
1837 /* We only need version numbers for symbols defined in regular
1839 if (!h
->def_regular
)
1842 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1843 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1844 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1846 struct bfd_elf_version_tree
*t
;
1851 /* There are two consecutive ELF_VER_CHR characters if this is
1852 not a hidden symbol. */
1854 if (*p
== ELF_VER_CHR
)
1860 /* If there is no version string, we can just return out. */
1868 /* Look for the version. If we find it, it is no longer weak. */
1869 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1871 if (strcmp (t
->name
, p
) == 0)
1875 struct bfd_elf_version_expr
*d
;
1877 len
= p
- h
->root
.root
.string
;
1878 alc
= bfd_malloc (len
);
1881 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1882 alc
[len
- 1] = '\0';
1883 if (alc
[len
- 2] == ELF_VER_CHR
)
1884 alc
[len
- 2] = '\0';
1886 h
->verinfo
.vertree
= t
;
1890 if (t
->globals
.list
!= NULL
)
1891 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1893 /* See if there is anything to force this symbol to
1895 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1897 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1900 && ! info
->export_dynamic
)
1901 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1909 /* If we are building an application, we need to create a
1910 version node for this version. */
1911 if (t
== NULL
&& info
->executable
)
1913 struct bfd_elf_version_tree
**pp
;
1916 /* If we aren't going to export this symbol, we don't need
1917 to worry about it. */
1918 if (h
->dynindx
== -1)
1922 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1925 sinfo
->failed
= TRUE
;
1930 t
->name_indx
= (unsigned int) -1;
1934 /* Don't count anonymous version tag. */
1935 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1937 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1939 t
->vernum
= version_index
;
1943 h
->verinfo
.vertree
= t
;
1947 /* We could not find the version for a symbol when
1948 generating a shared archive. Return an error. */
1949 (*_bfd_error_handler
)
1950 (_("%B: undefined versioned symbol name %s"),
1951 sinfo
->output_bfd
, h
->root
.root
.string
);
1952 bfd_set_error (bfd_error_bad_value
);
1953 sinfo
->failed
= TRUE
;
1961 /* If we don't have a version for this symbol, see if we can find
1963 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1965 struct bfd_elf_version_tree
*t
;
1966 struct bfd_elf_version_tree
*local_ver
;
1967 struct bfd_elf_version_expr
*d
;
1969 /* See if can find what version this symbol is in. If the
1970 symbol is supposed to be local, then don't actually register
1973 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1975 if (t
->globals
.list
!= NULL
)
1977 bfd_boolean matched
;
1981 while ((d
= (*t
->match
) (&t
->globals
, d
,
1982 h
->root
.root
.string
)) != NULL
)
1987 /* There is a version without definition. Make
1988 the symbol the default definition for this
1990 h
->verinfo
.vertree
= t
;
1998 /* There is no undefined version for this symbol. Hide the
2000 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2003 if (t
->locals
.list
!= NULL
)
2006 while ((d
= (*t
->match
) (&t
->locals
, d
,
2007 h
->root
.root
.string
)) != NULL
)
2010 /* If the match is "*", keep looking for a more
2011 explicit, perhaps even global, match.
2012 XXX: Shouldn't this be !d->wildcard instead? */
2013 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2022 if (local_ver
!= NULL
)
2024 h
->verinfo
.vertree
= local_ver
;
2025 if (h
->dynindx
!= -1
2026 && ! info
->export_dynamic
)
2028 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2036 /* Read and swap the relocs from the section indicated by SHDR. This
2037 may be either a REL or a RELA section. The relocations are
2038 translated into RELA relocations and stored in INTERNAL_RELOCS,
2039 which should have already been allocated to contain enough space.
2040 The EXTERNAL_RELOCS are a buffer where the external form of the
2041 relocations should be stored.
2043 Returns FALSE if something goes wrong. */
2046 elf_link_read_relocs_from_section (bfd
*abfd
,
2048 Elf_Internal_Shdr
*shdr
,
2049 void *external_relocs
,
2050 Elf_Internal_Rela
*internal_relocs
)
2052 const struct elf_backend_data
*bed
;
2053 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2054 const bfd_byte
*erela
;
2055 const bfd_byte
*erelaend
;
2056 Elf_Internal_Rela
*irela
;
2057 Elf_Internal_Shdr
*symtab_hdr
;
2060 /* Position ourselves at the start of the section. */
2061 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2064 /* Read the relocations. */
2065 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2068 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2069 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2071 bed
= get_elf_backend_data (abfd
);
2073 /* Convert the external relocations to the internal format. */
2074 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2075 swap_in
= bed
->s
->swap_reloc_in
;
2076 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2077 swap_in
= bed
->s
->swap_reloca_in
;
2080 bfd_set_error (bfd_error_wrong_format
);
2084 erela
= external_relocs
;
2085 erelaend
= erela
+ shdr
->sh_size
;
2086 irela
= internal_relocs
;
2087 while (erela
< erelaend
)
2091 (*swap_in
) (abfd
, erela
, irela
);
2092 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2093 if (bed
->s
->arch_size
== 64)
2095 if ((size_t) r_symndx
>= nsyms
)
2097 (*_bfd_error_handler
)
2098 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2099 " for offset 0x%lx in section `%A'"),
2101 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2102 bfd_set_error (bfd_error_bad_value
);
2105 irela
+= bed
->s
->int_rels_per_ext_rel
;
2106 erela
+= shdr
->sh_entsize
;
2112 /* Read and swap the relocs for a section O. They may have been
2113 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2114 not NULL, they are used as buffers to read into. They are known to
2115 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2116 the return value is allocated using either malloc or bfd_alloc,
2117 according to the KEEP_MEMORY argument. If O has two relocation
2118 sections (both REL and RELA relocations), then the REL_HDR
2119 relocations will appear first in INTERNAL_RELOCS, followed by the
2120 REL_HDR2 relocations. */
2123 _bfd_elf_link_read_relocs (bfd
*abfd
,
2125 void *external_relocs
,
2126 Elf_Internal_Rela
*internal_relocs
,
2127 bfd_boolean keep_memory
)
2129 Elf_Internal_Shdr
*rel_hdr
;
2130 void *alloc1
= NULL
;
2131 Elf_Internal_Rela
*alloc2
= NULL
;
2132 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2134 if (elf_section_data (o
)->relocs
!= NULL
)
2135 return elf_section_data (o
)->relocs
;
2137 if (o
->reloc_count
== 0)
2140 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2142 if (internal_relocs
== NULL
)
2146 size
= o
->reloc_count
;
2147 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2149 internal_relocs
= bfd_alloc (abfd
, size
);
2151 internal_relocs
= alloc2
= bfd_malloc (size
);
2152 if (internal_relocs
== NULL
)
2156 if (external_relocs
== NULL
)
2158 bfd_size_type size
= rel_hdr
->sh_size
;
2160 if (elf_section_data (o
)->rel_hdr2
)
2161 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2162 alloc1
= bfd_malloc (size
);
2165 external_relocs
= alloc1
;
2168 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2172 if (elf_section_data (o
)->rel_hdr2
2173 && (!elf_link_read_relocs_from_section
2175 elf_section_data (o
)->rel_hdr2
,
2176 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2177 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2178 * bed
->s
->int_rels_per_ext_rel
))))
2181 /* Cache the results for next time, if we can. */
2183 elf_section_data (o
)->relocs
= internal_relocs
;
2188 /* Don't free alloc2, since if it was allocated we are passing it
2189 back (under the name of internal_relocs). */
2191 return internal_relocs
;
2201 /* Compute the size of, and allocate space for, REL_HDR which is the
2202 section header for a section containing relocations for O. */
2205 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2206 Elf_Internal_Shdr
*rel_hdr
,
2209 bfd_size_type reloc_count
;
2210 bfd_size_type num_rel_hashes
;
2212 /* Figure out how many relocations there will be. */
2213 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2214 reloc_count
= elf_section_data (o
)->rel_count
;
2216 reloc_count
= elf_section_data (o
)->rel_count2
;
2218 num_rel_hashes
= o
->reloc_count
;
2219 if (num_rel_hashes
< reloc_count
)
2220 num_rel_hashes
= reloc_count
;
2222 /* That allows us to calculate the size of the section. */
2223 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2225 /* The contents field must last into write_object_contents, so we
2226 allocate it with bfd_alloc rather than malloc. Also since we
2227 cannot be sure that the contents will actually be filled in,
2228 we zero the allocated space. */
2229 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2230 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2233 /* We only allocate one set of hash entries, so we only do it the
2234 first time we are called. */
2235 if (elf_section_data (o
)->rel_hashes
== NULL
2238 struct elf_link_hash_entry
**p
;
2240 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2244 elf_section_data (o
)->rel_hashes
= p
;
2250 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2251 originated from the section given by INPUT_REL_HDR) to the
2255 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2256 asection
*input_section
,
2257 Elf_Internal_Shdr
*input_rel_hdr
,
2258 Elf_Internal_Rela
*internal_relocs
,
2259 struct elf_link_hash_entry
**rel_hash
2262 Elf_Internal_Rela
*irela
;
2263 Elf_Internal_Rela
*irelaend
;
2265 Elf_Internal_Shdr
*output_rel_hdr
;
2266 asection
*output_section
;
2267 unsigned int *rel_countp
= NULL
;
2268 const struct elf_backend_data
*bed
;
2269 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2271 output_section
= input_section
->output_section
;
2272 output_rel_hdr
= NULL
;
2274 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2275 == input_rel_hdr
->sh_entsize
)
2277 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2278 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2280 else if (elf_section_data (output_section
)->rel_hdr2
2281 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2282 == input_rel_hdr
->sh_entsize
))
2284 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2285 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2289 (*_bfd_error_handler
)
2290 (_("%B: relocation size mismatch in %B section %A"),
2291 output_bfd
, input_section
->owner
, input_section
);
2292 bfd_set_error (bfd_error_wrong_object_format
);
2296 bed
= get_elf_backend_data (output_bfd
);
2297 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2298 swap_out
= bed
->s
->swap_reloc_out
;
2299 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2300 swap_out
= bed
->s
->swap_reloca_out
;
2304 erel
= output_rel_hdr
->contents
;
2305 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2306 irela
= internal_relocs
;
2307 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2308 * bed
->s
->int_rels_per_ext_rel
);
2309 while (irela
< irelaend
)
2311 (*swap_out
) (output_bfd
, irela
, erel
);
2312 irela
+= bed
->s
->int_rels_per_ext_rel
;
2313 erel
+= input_rel_hdr
->sh_entsize
;
2316 /* Bump the counter, so that we know where to add the next set of
2318 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2323 /* Make weak undefined symbols in PIE dynamic. */
2326 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2327 struct elf_link_hash_entry
*h
)
2331 && h
->root
.type
== bfd_link_hash_undefweak
)
2332 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2337 /* Fix up the flags for a symbol. This handles various cases which
2338 can only be fixed after all the input files are seen. This is
2339 currently called by both adjust_dynamic_symbol and
2340 assign_sym_version, which is unnecessary but perhaps more robust in
2341 the face of future changes. */
2344 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2345 struct elf_info_failed
*eif
)
2347 const struct elf_backend_data
*bed
= NULL
;
2349 /* If this symbol was mentioned in a non-ELF file, try to set
2350 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2351 permit a non-ELF file to correctly refer to a symbol defined in
2352 an ELF dynamic object. */
2355 while (h
->root
.type
== bfd_link_hash_indirect
)
2356 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2358 if (h
->root
.type
!= bfd_link_hash_defined
2359 && h
->root
.type
!= bfd_link_hash_defweak
)
2362 h
->ref_regular_nonweak
= 1;
2366 if (h
->root
.u
.def
.section
->owner
!= NULL
2367 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2368 == bfd_target_elf_flavour
))
2371 h
->ref_regular_nonweak
= 1;
2377 if (h
->dynindx
== -1
2381 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2390 /* Unfortunately, NON_ELF is only correct if the symbol
2391 was first seen in a non-ELF file. Fortunately, if the symbol
2392 was first seen in an ELF file, we're probably OK unless the
2393 symbol was defined in a non-ELF file. Catch that case here.
2394 FIXME: We're still in trouble if the symbol was first seen in
2395 a dynamic object, and then later in a non-ELF regular object. */
2396 if ((h
->root
.type
== bfd_link_hash_defined
2397 || h
->root
.type
== bfd_link_hash_defweak
)
2399 && (h
->root
.u
.def
.section
->owner
!= NULL
2400 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2401 != bfd_target_elf_flavour
)
2402 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2403 && !h
->def_dynamic
)))
2407 /* Backend specific symbol fixup. */
2408 if (elf_hash_table (eif
->info
)->dynobj
)
2410 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2411 if (bed
->elf_backend_fixup_symbol
2412 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2416 /* If this is a final link, and the symbol was defined as a common
2417 symbol in a regular object file, and there was no definition in
2418 any dynamic object, then the linker will have allocated space for
2419 the symbol in a common section but the DEF_REGULAR
2420 flag will not have been set. */
2421 if (h
->root
.type
== bfd_link_hash_defined
2425 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2428 /* If -Bsymbolic was used (which means to bind references to global
2429 symbols to the definition within the shared object), and this
2430 symbol was defined in a regular object, then it actually doesn't
2431 need a PLT entry. Likewise, if the symbol has non-default
2432 visibility. If the symbol has hidden or internal visibility, we
2433 will force it local. */
2435 && eif
->info
->shared
2436 && is_elf_hash_table (eif
->info
->hash
)
2437 && (SYMBOLIC_BIND (eif
->info
, h
)
2438 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2441 bfd_boolean force_local
;
2443 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2444 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2445 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2448 /* If a weak undefined symbol has non-default visibility, we also
2449 hide it from the dynamic linker. */
2450 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2451 && h
->root
.type
== bfd_link_hash_undefweak
)
2453 const struct elf_backend_data
*bed
;
2454 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2455 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2458 /* If this is a weak defined symbol in a dynamic object, and we know
2459 the real definition in the dynamic object, copy interesting flags
2460 over to the real definition. */
2461 if (h
->u
.weakdef
!= NULL
)
2463 struct elf_link_hash_entry
*weakdef
;
2465 weakdef
= h
->u
.weakdef
;
2466 if (h
->root
.type
== bfd_link_hash_indirect
)
2467 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2469 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2470 || h
->root
.type
== bfd_link_hash_defweak
);
2471 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2472 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2473 BFD_ASSERT (weakdef
->def_dynamic
);
2475 /* If the real definition is defined by a regular object file,
2476 don't do anything special. See the longer description in
2477 _bfd_elf_adjust_dynamic_symbol, below. */
2478 if (weakdef
->def_regular
)
2479 h
->u
.weakdef
= NULL
;
2481 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2488 /* Make the backend pick a good value for a dynamic symbol. This is
2489 called via elf_link_hash_traverse, and also calls itself
2493 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2495 struct elf_info_failed
*eif
= data
;
2497 const struct elf_backend_data
*bed
;
2499 if (! is_elf_hash_table (eif
->info
->hash
))
2502 if (h
->root
.type
== bfd_link_hash_warning
)
2504 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2505 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2507 /* When warning symbols are created, they **replace** the "real"
2508 entry in the hash table, thus we never get to see the real
2509 symbol in a hash traversal. So look at it now. */
2510 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2513 /* Ignore indirect symbols. These are added by the versioning code. */
2514 if (h
->root
.type
== bfd_link_hash_indirect
)
2517 /* Fix the symbol flags. */
2518 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2521 /* If this symbol does not require a PLT entry, and it is not
2522 defined by a dynamic object, or is not referenced by a regular
2523 object, ignore it. We do have to handle a weak defined symbol,
2524 even if no regular object refers to it, if we decided to add it
2525 to the dynamic symbol table. FIXME: Do we normally need to worry
2526 about symbols which are defined by one dynamic object and
2527 referenced by another one? */
2532 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2534 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2538 /* If we've already adjusted this symbol, don't do it again. This
2539 can happen via a recursive call. */
2540 if (h
->dynamic_adjusted
)
2543 /* Don't look at this symbol again. Note that we must set this
2544 after checking the above conditions, because we may look at a
2545 symbol once, decide not to do anything, and then get called
2546 recursively later after REF_REGULAR is set below. */
2547 h
->dynamic_adjusted
= 1;
2549 /* If this is a weak definition, and we know a real definition, and
2550 the real symbol is not itself defined by a regular object file,
2551 then get a good value for the real definition. We handle the
2552 real symbol first, for the convenience of the backend routine.
2554 Note that there is a confusing case here. If the real definition
2555 is defined by a regular object file, we don't get the real symbol
2556 from the dynamic object, but we do get the weak symbol. If the
2557 processor backend uses a COPY reloc, then if some routine in the
2558 dynamic object changes the real symbol, we will not see that
2559 change in the corresponding weak symbol. This is the way other
2560 ELF linkers work as well, and seems to be a result of the shared
2563 I will clarify this issue. Most SVR4 shared libraries define the
2564 variable _timezone and define timezone as a weak synonym. The
2565 tzset call changes _timezone. If you write
2566 extern int timezone;
2568 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2569 you might expect that, since timezone is a synonym for _timezone,
2570 the same number will print both times. However, if the processor
2571 backend uses a COPY reloc, then actually timezone will be copied
2572 into your process image, and, since you define _timezone
2573 yourself, _timezone will not. Thus timezone and _timezone will
2574 wind up at different memory locations. The tzset call will set
2575 _timezone, leaving timezone unchanged. */
2577 if (h
->u
.weakdef
!= NULL
)
2579 /* If we get to this point, we know there is an implicit
2580 reference by a regular object file via the weak symbol H.
2581 FIXME: Is this really true? What if the traversal finds
2582 H->U.WEAKDEF before it finds H? */
2583 h
->u
.weakdef
->ref_regular
= 1;
2585 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2589 /* If a symbol has no type and no size and does not require a PLT
2590 entry, then we are probably about to do the wrong thing here: we
2591 are probably going to create a COPY reloc for an empty object.
2592 This case can arise when a shared object is built with assembly
2593 code, and the assembly code fails to set the symbol type. */
2595 && h
->type
== STT_NOTYPE
2597 (*_bfd_error_handler
)
2598 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2599 h
->root
.root
.string
);
2601 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2602 bed
= get_elf_backend_data (dynobj
);
2603 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2612 /* Adjust all external symbols pointing into SEC_MERGE sections
2613 to reflect the object merging within the sections. */
2616 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2620 if (h
->root
.type
== bfd_link_hash_warning
)
2621 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2623 if ((h
->root
.type
== bfd_link_hash_defined
2624 || h
->root
.type
== bfd_link_hash_defweak
)
2625 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2626 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2628 bfd
*output_bfd
= data
;
2630 h
->root
.u
.def
.value
=
2631 _bfd_merged_section_offset (output_bfd
,
2632 &h
->root
.u
.def
.section
,
2633 elf_section_data (sec
)->sec_info
,
2634 h
->root
.u
.def
.value
);
2640 /* Returns false if the symbol referred to by H should be considered
2641 to resolve local to the current module, and true if it should be
2642 considered to bind dynamically. */
2645 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2646 struct bfd_link_info
*info
,
2647 bfd_boolean ignore_protected
)
2649 bfd_boolean binding_stays_local_p
;
2650 const struct elf_backend_data
*bed
;
2651 struct elf_link_hash_table
*hash_table
;
2656 while (h
->root
.type
== bfd_link_hash_indirect
2657 || h
->root
.type
== bfd_link_hash_warning
)
2658 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2660 /* If it was forced local, then clearly it's not dynamic. */
2661 if (h
->dynindx
== -1)
2663 if (h
->forced_local
)
2666 /* Identify the cases where name binding rules say that a
2667 visible symbol resolves locally. */
2668 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2670 switch (ELF_ST_VISIBILITY (h
->other
))
2677 hash_table
= elf_hash_table (info
);
2678 if (!is_elf_hash_table (hash_table
))
2681 bed
= get_elf_backend_data (hash_table
->dynobj
);
2683 /* Proper resolution for function pointer equality may require
2684 that these symbols perhaps be resolved dynamically, even though
2685 we should be resolving them to the current module. */
2686 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2687 binding_stays_local_p
= TRUE
;
2694 /* If it isn't defined locally, then clearly it's dynamic. */
2695 if (!h
->def_regular
)
2698 /* Otherwise, the symbol is dynamic if binding rules don't tell
2699 us that it remains local. */
2700 return !binding_stays_local_p
;
2703 /* Return true if the symbol referred to by H should be considered
2704 to resolve local to the current module, and false otherwise. Differs
2705 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2706 undefined symbols and weak symbols. */
2709 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2710 struct bfd_link_info
*info
,
2711 bfd_boolean local_protected
)
2713 const struct elf_backend_data
*bed
;
2714 struct elf_link_hash_table
*hash_table
;
2716 /* If it's a local sym, of course we resolve locally. */
2720 /* Common symbols that become definitions don't get the DEF_REGULAR
2721 flag set, so test it first, and don't bail out. */
2722 if (ELF_COMMON_DEF_P (h
))
2724 /* If we don't have a definition in a regular file, then we can't
2725 resolve locally. The sym is either undefined or dynamic. */
2726 else if (!h
->def_regular
)
2729 /* Forced local symbols resolve locally. */
2730 if (h
->forced_local
)
2733 /* As do non-dynamic symbols. */
2734 if (h
->dynindx
== -1)
2737 /* At this point, we know the symbol is defined and dynamic. In an
2738 executable it must resolve locally, likewise when building symbolic
2739 shared libraries. */
2740 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2743 /* Now deal with defined dynamic symbols in shared libraries. Ones
2744 with default visibility might not resolve locally. */
2745 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2748 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2749 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2752 hash_table
= elf_hash_table (info
);
2753 if (!is_elf_hash_table (hash_table
))
2756 bed
= get_elf_backend_data (hash_table
->dynobj
);
2758 /* STV_PROTECTED non-function symbols are local. */
2759 if (!bed
->is_function_type (h
->type
))
2762 /* Function pointer equality tests may require that STV_PROTECTED
2763 symbols be treated as dynamic symbols, even when we know that the
2764 dynamic linker will resolve them locally. */
2765 return local_protected
;
2768 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2769 aligned. Returns the first TLS output section. */
2771 struct bfd_section
*
2772 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2774 struct bfd_section
*sec
, *tls
;
2775 unsigned int align
= 0;
2777 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2778 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2782 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2783 if (sec
->alignment_power
> align
)
2784 align
= sec
->alignment_power
;
2786 elf_hash_table (info
)->tls_sec
= tls
;
2788 /* Ensure the alignment of the first section is the largest alignment,
2789 so that the tls segment starts aligned. */
2791 tls
->alignment_power
= align
;
2796 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2798 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2799 Elf_Internal_Sym
*sym
)
2801 const struct elf_backend_data
*bed
;
2803 /* Local symbols do not count, but target specific ones might. */
2804 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2805 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2808 bed
= get_elf_backend_data (abfd
);
2809 /* Function symbols do not count. */
2810 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2813 /* If the section is undefined, then so is the symbol. */
2814 if (sym
->st_shndx
== SHN_UNDEF
)
2817 /* If the symbol is defined in the common section, then
2818 it is a common definition and so does not count. */
2819 if (bed
->common_definition (sym
))
2822 /* If the symbol is in a target specific section then we
2823 must rely upon the backend to tell us what it is. */
2824 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2825 /* FIXME - this function is not coded yet:
2827 return _bfd_is_global_symbol_definition (abfd, sym);
2829 Instead for now assume that the definition is not global,
2830 Even if this is wrong, at least the linker will behave
2831 in the same way that it used to do. */
2837 /* Search the symbol table of the archive element of the archive ABFD
2838 whose archive map contains a mention of SYMDEF, and determine if
2839 the symbol is defined in this element. */
2841 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2843 Elf_Internal_Shdr
* hdr
;
2844 bfd_size_type symcount
;
2845 bfd_size_type extsymcount
;
2846 bfd_size_type extsymoff
;
2847 Elf_Internal_Sym
*isymbuf
;
2848 Elf_Internal_Sym
*isym
;
2849 Elf_Internal_Sym
*isymend
;
2852 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2856 if (! bfd_check_format (abfd
, bfd_object
))
2859 /* If we have already included the element containing this symbol in the
2860 link then we do not need to include it again. Just claim that any symbol
2861 it contains is not a definition, so that our caller will not decide to
2862 (re)include this element. */
2863 if (abfd
->archive_pass
)
2866 /* Select the appropriate symbol table. */
2867 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2868 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2870 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2872 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2874 /* The sh_info field of the symtab header tells us where the
2875 external symbols start. We don't care about the local symbols. */
2876 if (elf_bad_symtab (abfd
))
2878 extsymcount
= symcount
;
2883 extsymcount
= symcount
- hdr
->sh_info
;
2884 extsymoff
= hdr
->sh_info
;
2887 if (extsymcount
== 0)
2890 /* Read in the symbol table. */
2891 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2893 if (isymbuf
== NULL
)
2896 /* Scan the symbol table looking for SYMDEF. */
2898 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2902 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2907 if (strcmp (name
, symdef
->name
) == 0)
2909 result
= is_global_data_symbol_definition (abfd
, isym
);
2919 /* Add an entry to the .dynamic table. */
2922 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2926 struct elf_link_hash_table
*hash_table
;
2927 const struct elf_backend_data
*bed
;
2929 bfd_size_type newsize
;
2930 bfd_byte
*newcontents
;
2931 Elf_Internal_Dyn dyn
;
2933 hash_table
= elf_hash_table (info
);
2934 if (! is_elf_hash_table (hash_table
))
2937 bed
= get_elf_backend_data (hash_table
->dynobj
);
2938 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2939 BFD_ASSERT (s
!= NULL
);
2941 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2942 newcontents
= bfd_realloc (s
->contents
, newsize
);
2943 if (newcontents
== NULL
)
2947 dyn
.d_un
.d_val
= val
;
2948 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2951 s
->contents
= newcontents
;
2956 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2957 otherwise just check whether one already exists. Returns -1 on error,
2958 1 if a DT_NEEDED tag already exists, and 0 on success. */
2961 elf_add_dt_needed_tag (bfd
*abfd
,
2962 struct bfd_link_info
*info
,
2966 struct elf_link_hash_table
*hash_table
;
2967 bfd_size_type oldsize
;
2968 bfd_size_type strindex
;
2970 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2973 hash_table
= elf_hash_table (info
);
2974 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2975 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2976 if (strindex
== (bfd_size_type
) -1)
2979 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2982 const struct elf_backend_data
*bed
;
2985 bed
= get_elf_backend_data (hash_table
->dynobj
);
2986 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2988 for (extdyn
= sdyn
->contents
;
2989 extdyn
< sdyn
->contents
+ sdyn
->size
;
2990 extdyn
+= bed
->s
->sizeof_dyn
)
2992 Elf_Internal_Dyn dyn
;
2994 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2995 if (dyn
.d_tag
== DT_NEEDED
2996 && dyn
.d_un
.d_val
== strindex
)
2998 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3006 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3009 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3013 /* We were just checking for existence of the tag. */
3014 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3019 /* Sort symbol by value and section. */
3021 elf_sort_symbol (const void *arg1
, const void *arg2
)
3023 const struct elf_link_hash_entry
*h1
;
3024 const struct elf_link_hash_entry
*h2
;
3025 bfd_signed_vma vdiff
;
3027 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3028 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3029 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3031 return vdiff
> 0 ? 1 : -1;
3034 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3036 return sdiff
> 0 ? 1 : -1;
3041 /* This function is used to adjust offsets into .dynstr for
3042 dynamic symbols. This is called via elf_link_hash_traverse. */
3045 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3047 struct elf_strtab_hash
*dynstr
= data
;
3049 if (h
->root
.type
== bfd_link_hash_warning
)
3050 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3052 if (h
->dynindx
!= -1)
3053 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3057 /* Assign string offsets in .dynstr, update all structures referencing
3061 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3063 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3064 struct elf_link_local_dynamic_entry
*entry
;
3065 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3066 bfd
*dynobj
= hash_table
->dynobj
;
3069 const struct elf_backend_data
*bed
;
3072 _bfd_elf_strtab_finalize (dynstr
);
3073 size
= _bfd_elf_strtab_size (dynstr
);
3075 bed
= get_elf_backend_data (dynobj
);
3076 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3077 BFD_ASSERT (sdyn
!= NULL
);
3079 /* Update all .dynamic entries referencing .dynstr strings. */
3080 for (extdyn
= sdyn
->contents
;
3081 extdyn
< sdyn
->contents
+ sdyn
->size
;
3082 extdyn
+= bed
->s
->sizeof_dyn
)
3084 Elf_Internal_Dyn dyn
;
3086 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3090 dyn
.d_un
.d_val
= size
;
3098 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3103 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3106 /* Now update local dynamic symbols. */
3107 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3108 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3109 entry
->isym
.st_name
);
3111 /* And the rest of dynamic symbols. */
3112 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3114 /* Adjust version definitions. */
3115 if (elf_tdata (output_bfd
)->cverdefs
)
3120 Elf_Internal_Verdef def
;
3121 Elf_Internal_Verdaux defaux
;
3123 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3127 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3129 p
+= sizeof (Elf_External_Verdef
);
3130 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3132 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3134 _bfd_elf_swap_verdaux_in (output_bfd
,
3135 (Elf_External_Verdaux
*) p
, &defaux
);
3136 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3138 _bfd_elf_swap_verdaux_out (output_bfd
,
3139 &defaux
, (Elf_External_Verdaux
*) p
);
3140 p
+= sizeof (Elf_External_Verdaux
);
3143 while (def
.vd_next
);
3146 /* Adjust version references. */
3147 if (elf_tdata (output_bfd
)->verref
)
3152 Elf_Internal_Verneed need
;
3153 Elf_Internal_Vernaux needaux
;
3155 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3159 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3161 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3162 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3163 (Elf_External_Verneed
*) p
);
3164 p
+= sizeof (Elf_External_Verneed
);
3165 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3167 _bfd_elf_swap_vernaux_in (output_bfd
,
3168 (Elf_External_Vernaux
*) p
, &needaux
);
3169 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3171 _bfd_elf_swap_vernaux_out (output_bfd
,
3173 (Elf_External_Vernaux
*) p
);
3174 p
+= sizeof (Elf_External_Vernaux
);
3177 while (need
.vn_next
);
3183 /* Add symbols from an ELF object file to the linker hash table. */
3186 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3188 Elf_Internal_Shdr
*hdr
;
3189 bfd_size_type symcount
;
3190 bfd_size_type extsymcount
;
3191 bfd_size_type extsymoff
;
3192 struct elf_link_hash_entry
**sym_hash
;
3193 bfd_boolean dynamic
;
3194 Elf_External_Versym
*extversym
= NULL
;
3195 Elf_External_Versym
*ever
;
3196 struct elf_link_hash_entry
*weaks
;
3197 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3198 bfd_size_type nondeflt_vers_cnt
= 0;
3199 Elf_Internal_Sym
*isymbuf
= NULL
;
3200 Elf_Internal_Sym
*isym
;
3201 Elf_Internal_Sym
*isymend
;
3202 const struct elf_backend_data
*bed
;
3203 bfd_boolean add_needed
;
3204 struct elf_link_hash_table
*htab
;
3206 void *alloc_mark
= NULL
;
3207 struct bfd_hash_entry
**old_table
= NULL
;
3208 unsigned int old_size
= 0;
3209 unsigned int old_count
= 0;
3210 void *old_tab
= NULL
;
3213 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3214 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3215 long old_dynsymcount
= 0;
3217 size_t hashsize
= 0;
3219 htab
= elf_hash_table (info
);
3220 bed
= get_elf_backend_data (abfd
);
3222 if ((abfd
->flags
& DYNAMIC
) == 0)
3228 /* You can't use -r against a dynamic object. Also, there's no
3229 hope of using a dynamic object which does not exactly match
3230 the format of the output file. */
3231 if (info
->relocatable
3232 || !is_elf_hash_table (htab
)
3233 || htab
->root
.creator
!= abfd
->xvec
)
3235 if (info
->relocatable
)
3236 bfd_set_error (bfd_error_invalid_operation
);
3238 bfd_set_error (bfd_error_wrong_format
);
3243 /* As a GNU extension, any input sections which are named
3244 .gnu.warning.SYMBOL are treated as warning symbols for the given
3245 symbol. This differs from .gnu.warning sections, which generate
3246 warnings when they are included in an output file. */
3247 if (info
->executable
)
3251 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3255 name
= bfd_get_section_name (abfd
, s
);
3256 if (CONST_STRNEQ (name
, ".gnu.warning."))
3261 name
+= sizeof ".gnu.warning." - 1;
3263 /* If this is a shared object, then look up the symbol
3264 in the hash table. If it is there, and it is already
3265 been defined, then we will not be using the entry
3266 from this shared object, so we don't need to warn.
3267 FIXME: If we see the definition in a regular object
3268 later on, we will warn, but we shouldn't. The only
3269 fix is to keep track of what warnings we are supposed
3270 to emit, and then handle them all at the end of the
3274 struct elf_link_hash_entry
*h
;
3276 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3278 /* FIXME: What about bfd_link_hash_common? */
3280 && (h
->root
.type
== bfd_link_hash_defined
3281 || h
->root
.type
== bfd_link_hash_defweak
))
3283 /* We don't want to issue this warning. Clobber
3284 the section size so that the warning does not
3285 get copied into the output file. */
3292 msg
= bfd_alloc (abfd
, sz
+ 1);
3296 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3301 if (! (_bfd_generic_link_add_one_symbol
3302 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3303 FALSE
, bed
->collect
, NULL
)))
3306 if (! info
->relocatable
)
3308 /* Clobber the section size so that the warning does
3309 not get copied into the output file. */
3312 /* Also set SEC_EXCLUDE, so that symbols defined in
3313 the warning section don't get copied to the output. */
3314 s
->flags
|= SEC_EXCLUDE
;
3323 /* If we are creating a shared library, create all the dynamic
3324 sections immediately. We need to attach them to something,
3325 so we attach them to this BFD, provided it is the right
3326 format. FIXME: If there are no input BFD's of the same
3327 format as the output, we can't make a shared library. */
3329 && is_elf_hash_table (htab
)
3330 && htab
->root
.creator
== abfd
->xvec
3331 && !htab
->dynamic_sections_created
)
3333 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3337 else if (!is_elf_hash_table (htab
))
3342 const char *soname
= NULL
;
3343 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3346 /* ld --just-symbols and dynamic objects don't mix very well.
3347 ld shouldn't allow it. */
3348 if ((s
= abfd
->sections
) != NULL
3349 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3352 /* If this dynamic lib was specified on the command line with
3353 --as-needed in effect, then we don't want to add a DT_NEEDED
3354 tag unless the lib is actually used. Similary for libs brought
3355 in by another lib's DT_NEEDED. When --no-add-needed is used
3356 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3357 any dynamic library in DT_NEEDED tags in the dynamic lib at
3359 add_needed
= (elf_dyn_lib_class (abfd
)
3360 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3361 | DYN_NO_NEEDED
)) == 0;
3363 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3369 unsigned long shlink
;
3371 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3372 goto error_free_dyn
;
3374 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3376 goto error_free_dyn
;
3377 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3379 for (extdyn
= dynbuf
;
3380 extdyn
< dynbuf
+ s
->size
;
3381 extdyn
+= bed
->s
->sizeof_dyn
)
3383 Elf_Internal_Dyn dyn
;
3385 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3386 if (dyn
.d_tag
== DT_SONAME
)
3388 unsigned int tagv
= dyn
.d_un
.d_val
;
3389 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3391 goto error_free_dyn
;
3393 if (dyn
.d_tag
== DT_NEEDED
)
3395 struct bfd_link_needed_list
*n
, **pn
;
3397 unsigned int tagv
= dyn
.d_un
.d_val
;
3399 amt
= sizeof (struct bfd_link_needed_list
);
3400 n
= bfd_alloc (abfd
, amt
);
3401 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3402 if (n
== NULL
|| fnm
== NULL
)
3403 goto error_free_dyn
;
3404 amt
= strlen (fnm
) + 1;
3405 anm
= bfd_alloc (abfd
, amt
);
3407 goto error_free_dyn
;
3408 memcpy (anm
, fnm
, amt
);
3412 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3416 if (dyn
.d_tag
== DT_RUNPATH
)
3418 struct bfd_link_needed_list
*n
, **pn
;
3420 unsigned int tagv
= dyn
.d_un
.d_val
;
3422 amt
= sizeof (struct bfd_link_needed_list
);
3423 n
= bfd_alloc (abfd
, amt
);
3424 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3425 if (n
== NULL
|| fnm
== NULL
)
3426 goto error_free_dyn
;
3427 amt
= strlen (fnm
) + 1;
3428 anm
= bfd_alloc (abfd
, amt
);
3430 goto error_free_dyn
;
3431 memcpy (anm
, fnm
, amt
);
3435 for (pn
= & runpath
;
3441 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3442 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3444 struct bfd_link_needed_list
*n
, **pn
;
3446 unsigned int tagv
= dyn
.d_un
.d_val
;
3448 amt
= sizeof (struct bfd_link_needed_list
);
3449 n
= bfd_alloc (abfd
, amt
);
3450 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3451 if (n
== NULL
|| fnm
== NULL
)
3452 goto error_free_dyn
;
3453 amt
= strlen (fnm
) + 1;
3454 anm
= bfd_alloc (abfd
, amt
);
3461 memcpy (anm
, fnm
, amt
);
3476 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3477 frees all more recently bfd_alloc'd blocks as well. */
3483 struct bfd_link_needed_list
**pn
;
3484 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3489 /* We do not want to include any of the sections in a dynamic
3490 object in the output file. We hack by simply clobbering the
3491 list of sections in the BFD. This could be handled more
3492 cleanly by, say, a new section flag; the existing
3493 SEC_NEVER_LOAD flag is not the one we want, because that one
3494 still implies that the section takes up space in the output
3496 bfd_section_list_clear (abfd
);
3498 /* Find the name to use in a DT_NEEDED entry that refers to this
3499 object. If the object has a DT_SONAME entry, we use it.
3500 Otherwise, if the generic linker stuck something in
3501 elf_dt_name, we use that. Otherwise, we just use the file
3503 if (soname
== NULL
|| *soname
== '\0')
3505 soname
= elf_dt_name (abfd
);
3506 if (soname
== NULL
|| *soname
== '\0')
3507 soname
= bfd_get_filename (abfd
);
3510 /* Save the SONAME because sometimes the linker emulation code
3511 will need to know it. */
3512 elf_dt_name (abfd
) = soname
;
3514 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3518 /* If we have already included this dynamic object in the
3519 link, just ignore it. There is no reason to include a
3520 particular dynamic object more than once. */
3525 /* If this is a dynamic object, we always link against the .dynsym
3526 symbol table, not the .symtab symbol table. The dynamic linker
3527 will only see the .dynsym symbol table, so there is no reason to
3528 look at .symtab for a dynamic object. */
3530 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3531 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3533 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3535 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3537 /* The sh_info field of the symtab header tells us where the
3538 external symbols start. We don't care about the local symbols at
3540 if (elf_bad_symtab (abfd
))
3542 extsymcount
= symcount
;
3547 extsymcount
= symcount
- hdr
->sh_info
;
3548 extsymoff
= hdr
->sh_info
;
3552 if (extsymcount
!= 0)
3554 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3556 if (isymbuf
== NULL
)
3559 /* We store a pointer to the hash table entry for each external
3561 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3562 sym_hash
= bfd_alloc (abfd
, amt
);
3563 if (sym_hash
== NULL
)
3564 goto error_free_sym
;
3565 elf_sym_hashes (abfd
) = sym_hash
;
3570 /* Read in any version definitions. */
3571 if (!_bfd_elf_slurp_version_tables (abfd
,
3572 info
->default_imported_symver
))
3573 goto error_free_sym
;
3575 /* Read in the symbol versions, but don't bother to convert them
3576 to internal format. */
3577 if (elf_dynversym (abfd
) != 0)
3579 Elf_Internal_Shdr
*versymhdr
;
3581 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3582 extversym
= bfd_malloc (versymhdr
->sh_size
);
3583 if (extversym
== NULL
)
3584 goto error_free_sym
;
3585 amt
= versymhdr
->sh_size
;
3586 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3587 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3588 goto error_free_vers
;
3592 /* If we are loading an as-needed shared lib, save the symbol table
3593 state before we start adding symbols. If the lib turns out
3594 to be unneeded, restore the state. */
3595 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3600 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3602 struct bfd_hash_entry
*p
;
3603 struct elf_link_hash_entry
*h
;
3605 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3607 h
= (struct elf_link_hash_entry
*) p
;
3608 entsize
+= htab
->root
.table
.entsize
;
3609 if (h
->root
.type
== bfd_link_hash_warning
)
3610 entsize
+= htab
->root
.table
.entsize
;
3614 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3615 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3616 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3617 if (old_tab
== NULL
)
3618 goto error_free_vers
;
3620 /* Remember the current objalloc pointer, so that all mem for
3621 symbols added can later be reclaimed. */
3622 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3623 if (alloc_mark
== NULL
)
3624 goto error_free_vers
;
3626 /* Make a special call to the linker "notice" function to
3627 tell it that we are about to handle an as-needed lib. */
3628 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3633 /* Clone the symbol table and sym hashes. Remember some
3634 pointers into the symbol table, and dynamic symbol count. */
3635 old_hash
= (char *) old_tab
+ tabsize
;
3636 old_ent
= (char *) old_hash
+ hashsize
;
3637 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3638 memcpy (old_hash
, sym_hash
, hashsize
);
3639 old_undefs
= htab
->root
.undefs
;
3640 old_undefs_tail
= htab
->root
.undefs_tail
;
3641 old_table
= htab
->root
.table
.table
;
3642 old_size
= htab
->root
.table
.size
;
3643 old_count
= htab
->root
.table
.count
;
3644 old_dynsymcount
= htab
->dynsymcount
;
3646 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3648 struct bfd_hash_entry
*p
;
3649 struct elf_link_hash_entry
*h
;
3651 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3653 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3654 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3655 h
= (struct elf_link_hash_entry
*) p
;
3656 if (h
->root
.type
== bfd_link_hash_warning
)
3658 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3659 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3666 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3667 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3669 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3673 asection
*sec
, *new_sec
;
3676 struct elf_link_hash_entry
*h
;
3677 bfd_boolean definition
;
3678 bfd_boolean size_change_ok
;
3679 bfd_boolean type_change_ok
;
3680 bfd_boolean new_weakdef
;
3681 bfd_boolean override
;
3683 unsigned int old_alignment
;
3688 flags
= BSF_NO_FLAGS
;
3690 value
= isym
->st_value
;
3692 common
= bed
->common_definition (isym
);
3694 bind
= ELF_ST_BIND (isym
->st_info
);
3695 if (bind
== STB_LOCAL
)
3697 /* This should be impossible, since ELF requires that all
3698 global symbols follow all local symbols, and that sh_info
3699 point to the first global symbol. Unfortunately, Irix 5
3703 else if (bind
== STB_GLOBAL
)
3705 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3708 else if (bind
== STB_WEAK
)
3712 /* Leave it up to the processor backend. */
3715 if (isym
->st_shndx
== SHN_UNDEF
)
3716 sec
= bfd_und_section_ptr
;
3717 else if (isym
->st_shndx
< SHN_LORESERVE
3718 || isym
->st_shndx
> SHN_HIRESERVE
)
3720 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3722 sec
= bfd_abs_section_ptr
;
3723 else if (sec
->kept_section
)
3725 /* Symbols from discarded section are undefined. We keep
3727 sec
= bfd_und_section_ptr
;
3728 isym
->st_shndx
= SHN_UNDEF
;
3730 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3733 else if (isym
->st_shndx
== SHN_ABS
)
3734 sec
= bfd_abs_section_ptr
;
3735 else if (isym
->st_shndx
== SHN_COMMON
)
3737 sec
= bfd_com_section_ptr
;
3738 /* What ELF calls the size we call the value. What ELF
3739 calls the value we call the alignment. */
3740 value
= isym
->st_size
;
3744 /* Leave it up to the processor backend. */
3747 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3750 goto error_free_vers
;
3752 if (isym
->st_shndx
== SHN_COMMON
3753 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3754 && !info
->relocatable
)
3756 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3760 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3763 | SEC_LINKER_CREATED
3764 | SEC_THREAD_LOCAL
));
3766 goto error_free_vers
;
3770 else if (bed
->elf_add_symbol_hook
)
3772 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3774 goto error_free_vers
;
3776 /* The hook function sets the name to NULL if this symbol
3777 should be skipped for some reason. */
3782 /* Sanity check that all possibilities were handled. */
3785 bfd_set_error (bfd_error_bad_value
);
3786 goto error_free_vers
;
3789 if (bfd_is_und_section (sec
)
3790 || bfd_is_com_section (sec
))
3795 size_change_ok
= FALSE
;
3796 type_change_ok
= bed
->type_change_ok
;
3801 if (is_elf_hash_table (htab
))
3803 Elf_Internal_Versym iver
;
3804 unsigned int vernum
= 0;
3809 if (info
->default_imported_symver
)
3810 /* Use the default symbol version created earlier. */
3811 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3816 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3818 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3820 /* If this is a hidden symbol, or if it is not version
3821 1, we append the version name to the symbol name.
3822 However, we do not modify a non-hidden absolute symbol
3823 if it is not a function, because it might be the version
3824 symbol itself. FIXME: What if it isn't? */
3825 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3827 && (!bfd_is_abs_section (sec
)
3828 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3831 size_t namelen
, verlen
, newlen
;
3834 if (isym
->st_shndx
!= SHN_UNDEF
)
3836 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3838 else if (vernum
> 1)
3840 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3846 (*_bfd_error_handler
)
3847 (_("%B: %s: invalid version %u (max %d)"),
3849 elf_tdata (abfd
)->cverdefs
);
3850 bfd_set_error (bfd_error_bad_value
);
3851 goto error_free_vers
;
3856 /* We cannot simply test for the number of
3857 entries in the VERNEED section since the
3858 numbers for the needed versions do not start
3860 Elf_Internal_Verneed
*t
;
3863 for (t
= elf_tdata (abfd
)->verref
;
3867 Elf_Internal_Vernaux
*a
;
3869 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3871 if (a
->vna_other
== vernum
)
3873 verstr
= a
->vna_nodename
;
3882 (*_bfd_error_handler
)
3883 (_("%B: %s: invalid needed version %d"),
3884 abfd
, name
, vernum
);
3885 bfd_set_error (bfd_error_bad_value
);
3886 goto error_free_vers
;
3890 namelen
= strlen (name
);
3891 verlen
= strlen (verstr
);
3892 newlen
= namelen
+ verlen
+ 2;
3893 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3894 && isym
->st_shndx
!= SHN_UNDEF
)
3897 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3898 if (newname
== NULL
)
3899 goto error_free_vers
;
3900 memcpy (newname
, name
, namelen
);
3901 p
= newname
+ namelen
;
3903 /* If this is a defined non-hidden version symbol,
3904 we add another @ to the name. This indicates the
3905 default version of the symbol. */
3906 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3907 && isym
->st_shndx
!= SHN_UNDEF
)
3909 memcpy (p
, verstr
, verlen
+ 1);
3914 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3915 &value
, &old_alignment
,
3916 sym_hash
, &skip
, &override
,
3917 &type_change_ok
, &size_change_ok
))
3918 goto error_free_vers
;
3927 while (h
->root
.type
== bfd_link_hash_indirect
3928 || h
->root
.type
== bfd_link_hash_warning
)
3929 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3931 /* Remember the old alignment if this is a common symbol, so
3932 that we don't reduce the alignment later on. We can't
3933 check later, because _bfd_generic_link_add_one_symbol
3934 will set a default for the alignment which we want to
3935 override. We also remember the old bfd where the existing
3936 definition comes from. */
3937 switch (h
->root
.type
)
3942 case bfd_link_hash_defined
:
3943 case bfd_link_hash_defweak
:
3944 old_bfd
= h
->root
.u
.def
.section
->owner
;
3947 case bfd_link_hash_common
:
3948 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3949 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3953 if (elf_tdata (abfd
)->verdef
!= NULL
3957 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3960 if (! (_bfd_generic_link_add_one_symbol
3961 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3962 (struct bfd_link_hash_entry
**) sym_hash
)))
3963 goto error_free_vers
;
3966 while (h
->root
.type
== bfd_link_hash_indirect
3967 || h
->root
.type
== bfd_link_hash_warning
)
3968 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3971 new_weakdef
= FALSE
;
3974 && (flags
& BSF_WEAK
) != 0
3975 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
3976 && is_elf_hash_table (htab
)
3977 && h
->u
.weakdef
== NULL
)
3979 /* Keep a list of all weak defined non function symbols from
3980 a dynamic object, using the weakdef field. Later in this
3981 function we will set the weakdef field to the correct
3982 value. We only put non-function symbols from dynamic
3983 objects on this list, because that happens to be the only
3984 time we need to know the normal symbol corresponding to a
3985 weak symbol, and the information is time consuming to
3986 figure out. If the weakdef field is not already NULL,
3987 then this symbol was already defined by some previous
3988 dynamic object, and we will be using that previous
3989 definition anyhow. */
3991 h
->u
.weakdef
= weaks
;
3996 /* Set the alignment of a common symbol. */
3997 if ((common
|| bfd_is_com_section (sec
))
3998 && h
->root
.type
== bfd_link_hash_common
)
4003 align
= bfd_log2 (isym
->st_value
);
4006 /* The new symbol is a common symbol in a shared object.
4007 We need to get the alignment from the section. */
4008 align
= new_sec
->alignment_power
;
4010 if (align
> old_alignment
4011 /* Permit an alignment power of zero if an alignment of one
4012 is specified and no other alignments have been specified. */
4013 || (isym
->st_value
== 1 && old_alignment
== 0))
4014 h
->root
.u
.c
.p
->alignment_power
= align
;
4016 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4019 if (is_elf_hash_table (htab
))
4023 /* Check the alignment when a common symbol is involved. This
4024 can change when a common symbol is overridden by a normal
4025 definition or a common symbol is ignored due to the old
4026 normal definition. We need to make sure the maximum
4027 alignment is maintained. */
4028 if ((old_alignment
|| common
)
4029 && h
->root
.type
!= bfd_link_hash_common
)
4031 unsigned int common_align
;
4032 unsigned int normal_align
;
4033 unsigned int symbol_align
;
4037 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4038 if (h
->root
.u
.def
.section
->owner
!= NULL
4039 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4041 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4042 if (normal_align
> symbol_align
)
4043 normal_align
= symbol_align
;
4046 normal_align
= symbol_align
;
4050 common_align
= old_alignment
;
4051 common_bfd
= old_bfd
;
4056 common_align
= bfd_log2 (isym
->st_value
);
4058 normal_bfd
= old_bfd
;
4061 if (normal_align
< common_align
)
4063 /* PR binutils/2735 */
4064 if (normal_bfd
== NULL
)
4065 (*_bfd_error_handler
)
4066 (_("Warning: alignment %u of common symbol `%s' in %B"
4067 " is greater than the alignment (%u) of its section %A"),
4068 common_bfd
, h
->root
.u
.def
.section
,
4069 1 << common_align
, name
, 1 << normal_align
);
4071 (*_bfd_error_handler
)
4072 (_("Warning: alignment %u of symbol `%s' in %B"
4073 " is smaller than %u in %B"),
4074 normal_bfd
, common_bfd
,
4075 1 << normal_align
, name
, 1 << common_align
);
4079 /* Remember the symbol size if it isn't undefined. */
4080 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4081 && (definition
|| h
->size
== 0))
4084 && h
->size
!= isym
->st_size
4085 && ! size_change_ok
)
4086 (*_bfd_error_handler
)
4087 (_("Warning: size of symbol `%s' changed"
4088 " from %lu in %B to %lu in %B"),
4090 name
, (unsigned long) h
->size
,
4091 (unsigned long) isym
->st_size
);
4093 h
->size
= isym
->st_size
;
4096 /* If this is a common symbol, then we always want H->SIZE
4097 to be the size of the common symbol. The code just above
4098 won't fix the size if a common symbol becomes larger. We
4099 don't warn about a size change here, because that is
4100 covered by --warn-common. Allow changed between different
4102 if (h
->root
.type
== bfd_link_hash_common
)
4103 h
->size
= h
->root
.u
.c
.size
;
4105 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4106 && (definition
|| h
->type
== STT_NOTYPE
))
4108 if (h
->type
!= STT_NOTYPE
4109 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4110 && ! type_change_ok
)
4111 (*_bfd_error_handler
)
4112 (_("Warning: type of symbol `%s' changed"
4113 " from %d to %d in %B"),
4114 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4116 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4119 /* If st_other has a processor-specific meaning, specific
4120 code might be needed here. We never merge the visibility
4121 attribute with the one from a dynamic object. */
4122 if (bed
->elf_backend_merge_symbol_attribute
)
4123 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4126 /* If this symbol has default visibility and the user has requested
4127 we not re-export it, then mark it as hidden. */
4128 if (definition
&& !dynamic
4130 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4131 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4132 isym
->st_other
= (STV_HIDDEN
4133 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4135 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4137 unsigned char hvis
, symvis
, other
, nvis
;
4139 /* Only merge the visibility. Leave the remainder of the
4140 st_other field to elf_backend_merge_symbol_attribute. */
4141 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4143 /* Combine visibilities, using the most constraining one. */
4144 hvis
= ELF_ST_VISIBILITY (h
->other
);
4145 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4151 nvis
= hvis
< symvis
? hvis
: symvis
;
4153 h
->other
= other
| nvis
;
4156 /* Set a flag in the hash table entry indicating the type of
4157 reference or definition we just found. Keep a count of
4158 the number of dynamic symbols we find. A dynamic symbol
4159 is one which is referenced or defined by both a regular
4160 object and a shared object. */
4167 if (bind
!= STB_WEAK
)
4168 h
->ref_regular_nonweak
= 1;
4172 if (! info
->executable
4185 || (h
->u
.weakdef
!= NULL
4187 && h
->u
.weakdef
->dynindx
!= -1))
4191 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4193 /* We don't want to make debug symbol dynamic. */
4194 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4198 /* Check to see if we need to add an indirect symbol for
4199 the default name. */
4200 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4201 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4202 &sec
, &value
, &dynsym
,
4204 goto error_free_vers
;
4206 if (definition
&& !dynamic
)
4208 char *p
= strchr (name
, ELF_VER_CHR
);
4209 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4211 /* Queue non-default versions so that .symver x, x@FOO
4212 aliases can be checked. */
4215 amt
= ((isymend
- isym
+ 1)
4216 * sizeof (struct elf_link_hash_entry
*));
4217 nondeflt_vers
= bfd_malloc (amt
);
4219 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4223 if (dynsym
&& h
->dynindx
== -1)
4225 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4226 goto error_free_vers
;
4227 if (h
->u
.weakdef
!= NULL
4229 && h
->u
.weakdef
->dynindx
== -1)
4231 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4232 goto error_free_vers
;
4235 else if (dynsym
&& h
->dynindx
!= -1)
4236 /* If the symbol already has a dynamic index, but
4237 visibility says it should not be visible, turn it into
4239 switch (ELF_ST_VISIBILITY (h
->other
))
4243 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4254 const char *soname
= elf_dt_name (abfd
);
4256 /* A symbol from a library loaded via DT_NEEDED of some
4257 other library is referenced by a regular object.
4258 Add a DT_NEEDED entry for it. Issue an error if
4259 --no-add-needed is used. */
4260 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4262 (*_bfd_error_handler
)
4263 (_("%s: invalid DSO for symbol `%s' definition"),
4265 bfd_set_error (bfd_error_bad_value
);
4266 goto error_free_vers
;
4269 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4272 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4274 goto error_free_vers
;
4276 BFD_ASSERT (ret
== 0);
4281 if (extversym
!= NULL
)
4287 if (isymbuf
!= NULL
)
4293 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4297 /* Restore the symbol table. */
4298 if (bed
->as_needed_cleanup
)
4299 (*bed
->as_needed_cleanup
) (abfd
, info
);
4300 old_hash
= (char *) old_tab
+ tabsize
;
4301 old_ent
= (char *) old_hash
+ hashsize
;
4302 sym_hash
= elf_sym_hashes (abfd
);
4303 htab
->root
.table
.table
= old_table
;
4304 htab
->root
.table
.size
= old_size
;
4305 htab
->root
.table
.count
= old_count
;
4306 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4307 memcpy (sym_hash
, old_hash
, hashsize
);
4308 htab
->root
.undefs
= old_undefs
;
4309 htab
->root
.undefs_tail
= old_undefs_tail
;
4310 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4312 struct bfd_hash_entry
*p
;
4313 struct elf_link_hash_entry
*h
;
4315 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4317 h
= (struct elf_link_hash_entry
*) p
;
4318 if (h
->root
.type
== bfd_link_hash_warning
)
4319 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4320 if (h
->dynindx
>= old_dynsymcount
)
4321 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4323 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4324 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4325 h
= (struct elf_link_hash_entry
*) p
;
4326 if (h
->root
.type
== bfd_link_hash_warning
)
4328 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4329 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4334 /* Make a special call to the linker "notice" function to
4335 tell it that symbols added for crefs may need to be removed. */
4336 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4341 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4343 if (nondeflt_vers
!= NULL
)
4344 free (nondeflt_vers
);
4348 if (old_tab
!= NULL
)
4350 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4357 /* Now that all the symbols from this input file are created, handle
4358 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4359 if (nondeflt_vers
!= NULL
)
4361 bfd_size_type cnt
, symidx
;
4363 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4365 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4366 char *shortname
, *p
;
4368 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4370 || (h
->root
.type
!= bfd_link_hash_defined
4371 && h
->root
.type
!= bfd_link_hash_defweak
))
4374 amt
= p
- h
->root
.root
.string
;
4375 shortname
= bfd_malloc (amt
+ 1);
4376 memcpy (shortname
, h
->root
.root
.string
, amt
);
4377 shortname
[amt
] = '\0';
4379 hi
= (struct elf_link_hash_entry
*)
4380 bfd_link_hash_lookup (&htab
->root
, shortname
,
4381 FALSE
, FALSE
, FALSE
);
4383 && hi
->root
.type
== h
->root
.type
4384 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4385 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4387 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4388 hi
->root
.type
= bfd_link_hash_indirect
;
4389 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4390 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4391 sym_hash
= elf_sym_hashes (abfd
);
4393 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4394 if (sym_hash
[symidx
] == hi
)
4396 sym_hash
[symidx
] = h
;
4402 free (nondeflt_vers
);
4403 nondeflt_vers
= NULL
;
4406 /* Now set the weakdefs field correctly for all the weak defined
4407 symbols we found. The only way to do this is to search all the
4408 symbols. Since we only need the information for non functions in
4409 dynamic objects, that's the only time we actually put anything on
4410 the list WEAKS. We need this information so that if a regular
4411 object refers to a symbol defined weakly in a dynamic object, the
4412 real symbol in the dynamic object is also put in the dynamic
4413 symbols; we also must arrange for both symbols to point to the
4414 same memory location. We could handle the general case of symbol
4415 aliasing, but a general symbol alias can only be generated in
4416 assembler code, handling it correctly would be very time
4417 consuming, and other ELF linkers don't handle general aliasing
4421 struct elf_link_hash_entry
**hpp
;
4422 struct elf_link_hash_entry
**hppend
;
4423 struct elf_link_hash_entry
**sorted_sym_hash
;
4424 struct elf_link_hash_entry
*h
;
4427 /* Since we have to search the whole symbol list for each weak
4428 defined symbol, search time for N weak defined symbols will be
4429 O(N^2). Binary search will cut it down to O(NlogN). */
4430 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4431 sorted_sym_hash
= bfd_malloc (amt
);
4432 if (sorted_sym_hash
== NULL
)
4434 sym_hash
= sorted_sym_hash
;
4435 hpp
= elf_sym_hashes (abfd
);
4436 hppend
= hpp
+ extsymcount
;
4438 for (; hpp
< hppend
; hpp
++)
4442 && h
->root
.type
== bfd_link_hash_defined
4443 && !bed
->is_function_type (h
->type
))
4451 qsort (sorted_sym_hash
, sym_count
,
4452 sizeof (struct elf_link_hash_entry
*),
4455 while (weaks
!= NULL
)
4457 struct elf_link_hash_entry
*hlook
;
4464 weaks
= hlook
->u
.weakdef
;
4465 hlook
->u
.weakdef
= NULL
;
4467 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4468 || hlook
->root
.type
== bfd_link_hash_defweak
4469 || hlook
->root
.type
== bfd_link_hash_common
4470 || hlook
->root
.type
== bfd_link_hash_indirect
);
4471 slook
= hlook
->root
.u
.def
.section
;
4472 vlook
= hlook
->root
.u
.def
.value
;
4479 bfd_signed_vma vdiff
;
4481 h
= sorted_sym_hash
[idx
];
4482 vdiff
= vlook
- h
->root
.u
.def
.value
;
4489 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4502 /* We didn't find a value/section match. */
4506 for (i
= ilook
; i
< sym_count
; i
++)
4508 h
= sorted_sym_hash
[i
];
4510 /* Stop if value or section doesn't match. */
4511 if (h
->root
.u
.def
.value
!= vlook
4512 || h
->root
.u
.def
.section
!= slook
)
4514 else if (h
!= hlook
)
4516 hlook
->u
.weakdef
= h
;
4518 /* If the weak definition is in the list of dynamic
4519 symbols, make sure the real definition is put
4521 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4523 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4527 /* If the real definition is in the list of dynamic
4528 symbols, make sure the weak definition is put
4529 there as well. If we don't do this, then the
4530 dynamic loader might not merge the entries for the
4531 real definition and the weak definition. */
4532 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4534 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4542 free (sorted_sym_hash
);
4545 if (bed
->check_directives
)
4546 (*bed
->check_directives
) (abfd
, info
);
4548 /* If this object is the same format as the output object, and it is
4549 not a shared library, then let the backend look through the
4552 This is required to build global offset table entries and to
4553 arrange for dynamic relocs. It is not required for the
4554 particular common case of linking non PIC code, even when linking
4555 against shared libraries, but unfortunately there is no way of
4556 knowing whether an object file has been compiled PIC or not.
4557 Looking through the relocs is not particularly time consuming.
4558 The problem is that we must either (1) keep the relocs in memory,
4559 which causes the linker to require additional runtime memory or
4560 (2) read the relocs twice from the input file, which wastes time.
4561 This would be a good case for using mmap.
4563 I have no idea how to handle linking PIC code into a file of a
4564 different format. It probably can't be done. */
4566 && is_elf_hash_table (htab
)
4567 && htab
->root
.creator
== abfd
->xvec
4568 && bed
->check_relocs
!= NULL
)
4572 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4574 Elf_Internal_Rela
*internal_relocs
;
4577 if ((o
->flags
& SEC_RELOC
) == 0
4578 || o
->reloc_count
== 0
4579 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4580 && (o
->flags
& SEC_DEBUGGING
) != 0)
4581 || bfd_is_abs_section (o
->output_section
))
4584 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4586 if (internal_relocs
== NULL
)
4589 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4591 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4592 free (internal_relocs
);
4599 /* If this is a non-traditional link, try to optimize the handling
4600 of the .stab/.stabstr sections. */
4602 && ! info
->traditional_format
4603 && is_elf_hash_table (htab
)
4604 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4608 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4609 if (stabstr
!= NULL
)
4611 bfd_size_type string_offset
= 0;
4614 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4615 if (CONST_STRNEQ (stab
->name
, ".stab")
4616 && (!stab
->name
[5] ||
4617 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4618 && (stab
->flags
& SEC_MERGE
) == 0
4619 && !bfd_is_abs_section (stab
->output_section
))
4621 struct bfd_elf_section_data
*secdata
;
4623 secdata
= elf_section_data (stab
);
4624 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4625 stabstr
, &secdata
->sec_info
,
4628 if (secdata
->sec_info
)
4629 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4634 if (is_elf_hash_table (htab
) && add_needed
)
4636 /* Add this bfd to the loaded list. */
4637 struct elf_link_loaded_list
*n
;
4639 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4643 n
->next
= htab
->loaded
;
4650 if (old_tab
!= NULL
)
4652 if (nondeflt_vers
!= NULL
)
4653 free (nondeflt_vers
);
4654 if (extversym
!= NULL
)
4657 if (isymbuf
!= NULL
)
4663 /* Return the linker hash table entry of a symbol that might be
4664 satisfied by an archive symbol. Return -1 on error. */
4666 struct elf_link_hash_entry
*
4667 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4668 struct bfd_link_info
*info
,
4671 struct elf_link_hash_entry
*h
;
4675 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4679 /* If this is a default version (the name contains @@), look up the
4680 symbol again with only one `@' as well as without the version.
4681 The effect is that references to the symbol with and without the
4682 version will be matched by the default symbol in the archive. */
4684 p
= strchr (name
, ELF_VER_CHR
);
4685 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4688 /* First check with only one `@'. */
4689 len
= strlen (name
);
4690 copy
= bfd_alloc (abfd
, len
);
4692 return (struct elf_link_hash_entry
*) 0 - 1;
4694 first
= p
- name
+ 1;
4695 memcpy (copy
, name
, first
);
4696 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4698 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4701 /* We also need to check references to the symbol without the
4703 copy
[first
- 1] = '\0';
4704 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4705 FALSE
, FALSE
, FALSE
);
4708 bfd_release (abfd
, copy
);
4712 /* Add symbols from an ELF archive file to the linker hash table. We
4713 don't use _bfd_generic_link_add_archive_symbols because of a
4714 problem which arises on UnixWare. The UnixWare libc.so is an
4715 archive which includes an entry libc.so.1 which defines a bunch of
4716 symbols. The libc.so archive also includes a number of other
4717 object files, which also define symbols, some of which are the same
4718 as those defined in libc.so.1. Correct linking requires that we
4719 consider each object file in turn, and include it if it defines any
4720 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4721 this; it looks through the list of undefined symbols, and includes
4722 any object file which defines them. When this algorithm is used on
4723 UnixWare, it winds up pulling in libc.so.1 early and defining a
4724 bunch of symbols. This means that some of the other objects in the
4725 archive are not included in the link, which is incorrect since they
4726 precede libc.so.1 in the archive.
4728 Fortunately, ELF archive handling is simpler than that done by
4729 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4730 oddities. In ELF, if we find a symbol in the archive map, and the
4731 symbol is currently undefined, we know that we must pull in that
4734 Unfortunately, we do have to make multiple passes over the symbol
4735 table until nothing further is resolved. */
4738 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4741 bfd_boolean
*defined
= NULL
;
4742 bfd_boolean
*included
= NULL
;
4746 const struct elf_backend_data
*bed
;
4747 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4748 (bfd
*, struct bfd_link_info
*, const char *);
4750 if (! bfd_has_map (abfd
))
4752 /* An empty archive is a special case. */
4753 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4755 bfd_set_error (bfd_error_no_armap
);
4759 /* Keep track of all symbols we know to be already defined, and all
4760 files we know to be already included. This is to speed up the
4761 second and subsequent passes. */
4762 c
= bfd_ardata (abfd
)->symdef_count
;
4766 amt
*= sizeof (bfd_boolean
);
4767 defined
= bfd_zmalloc (amt
);
4768 included
= bfd_zmalloc (amt
);
4769 if (defined
== NULL
|| included
== NULL
)
4772 symdefs
= bfd_ardata (abfd
)->symdefs
;
4773 bed
= get_elf_backend_data (abfd
);
4774 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4787 symdefend
= symdef
+ c
;
4788 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4790 struct elf_link_hash_entry
*h
;
4792 struct bfd_link_hash_entry
*undefs_tail
;
4795 if (defined
[i
] || included
[i
])
4797 if (symdef
->file_offset
== last
)
4803 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4804 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4810 if (h
->root
.type
== bfd_link_hash_common
)
4812 /* We currently have a common symbol. The archive map contains
4813 a reference to this symbol, so we may want to include it. We
4814 only want to include it however, if this archive element
4815 contains a definition of the symbol, not just another common
4818 Unfortunately some archivers (including GNU ar) will put
4819 declarations of common symbols into their archive maps, as
4820 well as real definitions, so we cannot just go by the archive
4821 map alone. Instead we must read in the element's symbol
4822 table and check that to see what kind of symbol definition
4824 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4827 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4829 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4834 /* We need to include this archive member. */
4835 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4836 if (element
== NULL
)
4839 if (! bfd_check_format (element
, bfd_object
))
4842 /* Doublecheck that we have not included this object
4843 already--it should be impossible, but there may be
4844 something wrong with the archive. */
4845 if (element
->archive_pass
!= 0)
4847 bfd_set_error (bfd_error_bad_value
);
4850 element
->archive_pass
= 1;
4852 undefs_tail
= info
->hash
->undefs_tail
;
4854 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4857 if (! bfd_link_add_symbols (element
, info
))
4860 /* If there are any new undefined symbols, we need to make
4861 another pass through the archive in order to see whether
4862 they can be defined. FIXME: This isn't perfect, because
4863 common symbols wind up on undefs_tail and because an
4864 undefined symbol which is defined later on in this pass
4865 does not require another pass. This isn't a bug, but it
4866 does make the code less efficient than it could be. */
4867 if (undefs_tail
!= info
->hash
->undefs_tail
)
4870 /* Look backward to mark all symbols from this object file
4871 which we have already seen in this pass. */
4875 included
[mark
] = TRUE
;
4880 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4882 /* We mark subsequent symbols from this object file as we go
4883 on through the loop. */
4884 last
= symdef
->file_offset
;
4895 if (defined
!= NULL
)
4897 if (included
!= NULL
)
4902 /* Given an ELF BFD, add symbols to the global hash table as
4906 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4908 switch (bfd_get_format (abfd
))
4911 return elf_link_add_object_symbols (abfd
, info
);
4913 return elf_link_add_archive_symbols (abfd
, info
);
4915 bfd_set_error (bfd_error_wrong_format
);
4920 /* This function will be called though elf_link_hash_traverse to store
4921 all hash value of the exported symbols in an array. */
4924 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4926 unsigned long **valuep
= data
;
4932 if (h
->root
.type
== bfd_link_hash_warning
)
4933 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4935 /* Ignore indirect symbols. These are added by the versioning code. */
4936 if (h
->dynindx
== -1)
4939 name
= h
->root
.root
.string
;
4940 p
= strchr (name
, ELF_VER_CHR
);
4943 alc
= bfd_malloc (p
- name
+ 1);
4944 memcpy (alc
, name
, p
- name
);
4945 alc
[p
- name
] = '\0';
4949 /* Compute the hash value. */
4950 ha
= bfd_elf_hash (name
);
4952 /* Store the found hash value in the array given as the argument. */
4955 /* And store it in the struct so that we can put it in the hash table
4957 h
->u
.elf_hash_value
= ha
;
4965 struct collect_gnu_hash_codes
4968 const struct elf_backend_data
*bed
;
4969 unsigned long int nsyms
;
4970 unsigned long int maskbits
;
4971 unsigned long int *hashcodes
;
4972 unsigned long int *hashval
;
4973 unsigned long int *indx
;
4974 unsigned long int *counts
;
4977 long int min_dynindx
;
4978 unsigned long int bucketcount
;
4979 unsigned long int symindx
;
4980 long int local_indx
;
4981 long int shift1
, shift2
;
4982 unsigned long int mask
;
4985 /* This function will be called though elf_link_hash_traverse to store
4986 all hash value of the exported symbols in an array. */
4989 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4991 struct collect_gnu_hash_codes
*s
= data
;
4997 if (h
->root
.type
== bfd_link_hash_warning
)
4998 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5000 /* Ignore indirect symbols. These are added by the versioning code. */
5001 if (h
->dynindx
== -1)
5004 /* Ignore also local symbols and undefined symbols. */
5005 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5008 name
= h
->root
.root
.string
;
5009 p
= strchr (name
, ELF_VER_CHR
);
5012 alc
= bfd_malloc (p
- name
+ 1);
5013 memcpy (alc
, name
, p
- name
);
5014 alc
[p
- name
] = '\0';
5018 /* Compute the hash value. */
5019 ha
= bfd_elf_gnu_hash (name
);
5021 /* Store the found hash value in the array for compute_bucket_count,
5022 and also for .dynsym reordering purposes. */
5023 s
->hashcodes
[s
->nsyms
] = ha
;
5024 s
->hashval
[h
->dynindx
] = ha
;
5026 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5027 s
->min_dynindx
= h
->dynindx
;
5035 /* This function will be called though elf_link_hash_traverse to do
5036 final dynaminc symbol renumbering. */
5039 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5041 struct collect_gnu_hash_codes
*s
= data
;
5042 unsigned long int bucket
;
5043 unsigned long int val
;
5045 if (h
->root
.type
== bfd_link_hash_warning
)
5046 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5048 /* Ignore indirect symbols. */
5049 if (h
->dynindx
== -1)
5052 /* Ignore also local symbols and undefined symbols. */
5053 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5055 if (h
->dynindx
>= s
->min_dynindx
)
5056 h
->dynindx
= s
->local_indx
++;
5060 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5061 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5062 & ((s
->maskbits
>> s
->shift1
) - 1);
5063 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5065 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5066 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5067 if (s
->counts
[bucket
] == 1)
5068 /* Last element terminates the chain. */
5070 bfd_put_32 (s
->output_bfd
, val
,
5071 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5072 --s
->counts
[bucket
];
5073 h
->dynindx
= s
->indx
[bucket
]++;
5077 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5080 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5082 return !(h
->forced_local
5083 || h
->root
.type
== bfd_link_hash_undefined
5084 || h
->root
.type
== bfd_link_hash_undefweak
5085 || ((h
->root
.type
== bfd_link_hash_defined
5086 || h
->root
.type
== bfd_link_hash_defweak
)
5087 && h
->root
.u
.def
.section
->output_section
== NULL
));
5090 /* Array used to determine the number of hash table buckets to use
5091 based on the number of symbols there are. If there are fewer than
5092 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5093 fewer than 37 we use 17 buckets, and so forth. We never use more
5094 than 32771 buckets. */
5096 static const size_t elf_buckets
[] =
5098 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5102 /* Compute bucket count for hashing table. We do not use a static set
5103 of possible tables sizes anymore. Instead we determine for all
5104 possible reasonable sizes of the table the outcome (i.e., the
5105 number of collisions etc) and choose the best solution. The
5106 weighting functions are not too simple to allow the table to grow
5107 without bounds. Instead one of the weighting factors is the size.
5108 Therefore the result is always a good payoff between few collisions
5109 (= short chain lengths) and table size. */
5111 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5112 unsigned long int nsyms
, int gnu_hash
)
5114 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5115 size_t best_size
= 0;
5116 unsigned long int i
;
5119 /* We have a problem here. The following code to optimize the table
5120 size requires an integer type with more the 32 bits. If
5121 BFD_HOST_U_64_BIT is set we know about such a type. */
5122 #ifdef BFD_HOST_U_64_BIT
5127 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5128 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5129 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5130 unsigned long int *counts
;
5132 /* Possible optimization parameters: if we have NSYMS symbols we say
5133 that the hashing table must at least have NSYMS/4 and at most
5135 minsize
= nsyms
/ 4;
5138 best_size
= maxsize
= nsyms
* 2;
5143 if ((best_size
& 31) == 0)
5147 /* Create array where we count the collisions in. We must use bfd_malloc
5148 since the size could be large. */
5150 amt
*= sizeof (unsigned long int);
5151 counts
= bfd_malloc (amt
);
5155 /* Compute the "optimal" size for the hash table. The criteria is a
5156 minimal chain length. The minor criteria is (of course) the size
5158 for (i
= minsize
; i
< maxsize
; ++i
)
5160 /* Walk through the array of hashcodes and count the collisions. */
5161 BFD_HOST_U_64_BIT max
;
5162 unsigned long int j
;
5163 unsigned long int fact
;
5165 if (gnu_hash
&& (i
& 31) == 0)
5168 memset (counts
, '\0', i
* sizeof (unsigned long int));
5170 /* Determine how often each hash bucket is used. */
5171 for (j
= 0; j
< nsyms
; ++j
)
5172 ++counts
[hashcodes
[j
] % i
];
5174 /* For the weight function we need some information about the
5175 pagesize on the target. This is information need not be 100%
5176 accurate. Since this information is not available (so far) we
5177 define it here to a reasonable default value. If it is crucial
5178 to have a better value some day simply define this value. */
5179 # ifndef BFD_TARGET_PAGESIZE
5180 # define BFD_TARGET_PAGESIZE (4096)
5183 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5185 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5188 /* Variant 1: optimize for short chains. We add the squares
5189 of all the chain lengths (which favors many small chain
5190 over a few long chains). */
5191 for (j
= 0; j
< i
; ++j
)
5192 max
+= counts
[j
] * counts
[j
];
5194 /* This adds penalties for the overall size of the table. */
5195 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5198 /* Variant 2: Optimize a lot more for small table. Here we
5199 also add squares of the size but we also add penalties for
5200 empty slots (the +1 term). */
5201 for (j
= 0; j
< i
; ++j
)
5202 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5204 /* The overall size of the table is considered, but not as
5205 strong as in variant 1, where it is squared. */
5206 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5210 /* Compare with current best results. */
5211 if (max
< best_chlen
)
5221 #endif /* defined (BFD_HOST_U_64_BIT) */
5223 /* This is the fallback solution if no 64bit type is available or if we
5224 are not supposed to spend much time on optimizations. We select the
5225 bucket count using a fixed set of numbers. */
5226 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5228 best_size
= elf_buckets
[i
];
5229 if (nsyms
< elf_buckets
[i
+ 1])
5232 if (gnu_hash
&& best_size
< 2)
5239 /* Set up the sizes and contents of the ELF dynamic sections. This is
5240 called by the ELF linker emulation before_allocation routine. We
5241 must set the sizes of the sections before the linker sets the
5242 addresses of the various sections. */
5245 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5248 const char *filter_shlib
,
5249 const char * const *auxiliary_filters
,
5250 struct bfd_link_info
*info
,
5251 asection
**sinterpptr
,
5252 struct bfd_elf_version_tree
*verdefs
)
5254 bfd_size_type soname_indx
;
5256 const struct elf_backend_data
*bed
;
5257 struct elf_assign_sym_version_info asvinfo
;
5261 soname_indx
= (bfd_size_type
) -1;
5263 if (!is_elf_hash_table (info
->hash
))
5266 bed
= get_elf_backend_data (output_bfd
);
5267 elf_tdata (output_bfd
)->relro
= info
->relro
;
5268 if (info
->execstack
)
5269 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5270 else if (info
->noexecstack
)
5271 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5275 asection
*notesec
= NULL
;
5278 for (inputobj
= info
->input_bfds
;
5280 inputobj
= inputobj
->link_next
)
5284 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5286 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5289 if (s
->flags
& SEC_CODE
)
5293 else if (bed
->default_execstack
)
5298 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5299 if (exec
&& info
->relocatable
5300 && notesec
->output_section
!= bfd_abs_section_ptr
)
5301 notesec
->output_section
->flags
|= SEC_CODE
;
5305 /* Any syms created from now on start with -1 in
5306 got.refcount/offset and plt.refcount/offset. */
5307 elf_hash_table (info
)->init_got_refcount
5308 = elf_hash_table (info
)->init_got_offset
;
5309 elf_hash_table (info
)->init_plt_refcount
5310 = elf_hash_table (info
)->init_plt_offset
;
5312 /* The backend may have to create some sections regardless of whether
5313 we're dynamic or not. */
5314 if (bed
->elf_backend_always_size_sections
5315 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5318 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5321 dynobj
= elf_hash_table (info
)->dynobj
;
5323 /* If there were no dynamic objects in the link, there is nothing to
5328 if (elf_hash_table (info
)->dynamic_sections_created
)
5330 struct elf_info_failed eif
;
5331 struct elf_link_hash_entry
*h
;
5333 struct bfd_elf_version_tree
*t
;
5334 struct bfd_elf_version_expr
*d
;
5336 bfd_boolean all_defined
;
5338 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5339 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5343 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5345 if (soname_indx
== (bfd_size_type
) -1
5346 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5352 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5354 info
->flags
|= DF_SYMBOLIC
;
5361 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5363 if (indx
== (bfd_size_type
) -1
5364 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5367 if (info
->new_dtags
)
5369 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5370 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5375 if (filter_shlib
!= NULL
)
5379 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5380 filter_shlib
, TRUE
);
5381 if (indx
== (bfd_size_type
) -1
5382 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5386 if (auxiliary_filters
!= NULL
)
5388 const char * const *p
;
5390 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5394 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5396 if (indx
== (bfd_size_type
) -1
5397 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5403 eif
.verdefs
= verdefs
;
5406 /* If we are supposed to export all symbols into the dynamic symbol
5407 table (this is not the normal case), then do so. */
5408 if (info
->export_dynamic
5409 || (info
->executable
&& info
->dynamic
))
5411 elf_link_hash_traverse (elf_hash_table (info
),
5412 _bfd_elf_export_symbol
,
5418 /* Make all global versions with definition. */
5419 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5420 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5421 if (!d
->symver
&& d
->symbol
)
5423 const char *verstr
, *name
;
5424 size_t namelen
, verlen
, newlen
;
5426 struct elf_link_hash_entry
*newh
;
5429 namelen
= strlen (name
);
5431 verlen
= strlen (verstr
);
5432 newlen
= namelen
+ verlen
+ 3;
5434 newname
= bfd_malloc (newlen
);
5435 if (newname
== NULL
)
5437 memcpy (newname
, name
, namelen
);
5439 /* Check the hidden versioned definition. */
5440 p
= newname
+ namelen
;
5442 memcpy (p
, verstr
, verlen
+ 1);
5443 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5444 newname
, FALSE
, FALSE
,
5447 || (newh
->root
.type
!= bfd_link_hash_defined
5448 && newh
->root
.type
!= bfd_link_hash_defweak
))
5450 /* Check the default versioned definition. */
5452 memcpy (p
, verstr
, verlen
+ 1);
5453 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5454 newname
, FALSE
, FALSE
,
5459 /* Mark this version if there is a definition and it is
5460 not defined in a shared object. */
5462 && !newh
->def_dynamic
5463 && (newh
->root
.type
== bfd_link_hash_defined
5464 || newh
->root
.type
== bfd_link_hash_defweak
))
5468 /* Attach all the symbols to their version information. */
5469 asvinfo
.output_bfd
= output_bfd
;
5470 asvinfo
.info
= info
;
5471 asvinfo
.verdefs
= verdefs
;
5472 asvinfo
.failed
= FALSE
;
5474 elf_link_hash_traverse (elf_hash_table (info
),
5475 _bfd_elf_link_assign_sym_version
,
5480 if (!info
->allow_undefined_version
)
5482 /* Check if all global versions have a definition. */
5484 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5485 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5486 if (!d
->symver
&& !d
->script
)
5488 (*_bfd_error_handler
)
5489 (_("%s: undefined version: %s"),
5490 d
->pattern
, t
->name
);
5491 all_defined
= FALSE
;
5496 bfd_set_error (bfd_error_bad_value
);
5501 /* Find all symbols which were defined in a dynamic object and make
5502 the backend pick a reasonable value for them. */
5503 elf_link_hash_traverse (elf_hash_table (info
),
5504 _bfd_elf_adjust_dynamic_symbol
,
5509 /* Add some entries to the .dynamic section. We fill in some of the
5510 values later, in bfd_elf_final_link, but we must add the entries
5511 now so that we know the final size of the .dynamic section. */
5513 /* If there are initialization and/or finalization functions to
5514 call then add the corresponding DT_INIT/DT_FINI entries. */
5515 h
= (info
->init_function
5516 ? elf_link_hash_lookup (elf_hash_table (info
),
5517 info
->init_function
, FALSE
,
5524 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5527 h
= (info
->fini_function
5528 ? elf_link_hash_lookup (elf_hash_table (info
),
5529 info
->fini_function
, FALSE
,
5536 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5540 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5541 if (s
!= NULL
&& s
->linker_has_input
)
5543 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5544 if (! info
->executable
)
5549 for (sub
= info
->input_bfds
; sub
!= NULL
;
5550 sub
= sub
->link_next
)
5551 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5552 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5553 if (elf_section_data (o
)->this_hdr
.sh_type
5554 == SHT_PREINIT_ARRAY
)
5556 (*_bfd_error_handler
)
5557 (_("%B: .preinit_array section is not allowed in DSO"),
5562 bfd_set_error (bfd_error_nonrepresentable_section
);
5566 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5567 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5570 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5571 if (s
!= NULL
&& s
->linker_has_input
)
5573 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5574 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5577 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5578 if (s
!= NULL
&& s
->linker_has_input
)
5580 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5581 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5585 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5586 /* If .dynstr is excluded from the link, we don't want any of
5587 these tags. Strictly, we should be checking each section
5588 individually; This quick check covers for the case where
5589 someone does a /DISCARD/ : { *(*) }. */
5590 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5592 bfd_size_type strsize
;
5594 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5595 if ((info
->emit_hash
5596 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5597 || (info
->emit_gnu_hash
5598 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5599 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5600 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5601 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5602 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5603 bed
->s
->sizeof_sym
))
5608 /* The backend must work out the sizes of all the other dynamic
5610 if (bed
->elf_backend_size_dynamic_sections
5611 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5614 if (elf_hash_table (info
)->dynamic_sections_created
)
5616 unsigned long section_sym_count
;
5619 /* Set up the version definition section. */
5620 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5621 BFD_ASSERT (s
!= NULL
);
5623 /* We may have created additional version definitions if we are
5624 just linking a regular application. */
5625 verdefs
= asvinfo
.verdefs
;
5627 /* Skip anonymous version tag. */
5628 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5629 verdefs
= verdefs
->next
;
5631 if (verdefs
== NULL
&& !info
->create_default_symver
)
5632 s
->flags
|= SEC_EXCLUDE
;
5637 struct bfd_elf_version_tree
*t
;
5639 Elf_Internal_Verdef def
;
5640 Elf_Internal_Verdaux defaux
;
5641 struct bfd_link_hash_entry
*bh
;
5642 struct elf_link_hash_entry
*h
;
5648 /* Make space for the base version. */
5649 size
+= sizeof (Elf_External_Verdef
);
5650 size
+= sizeof (Elf_External_Verdaux
);
5653 /* Make space for the default version. */
5654 if (info
->create_default_symver
)
5656 size
+= sizeof (Elf_External_Verdef
);
5660 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5662 struct bfd_elf_version_deps
*n
;
5664 size
+= sizeof (Elf_External_Verdef
);
5665 size
+= sizeof (Elf_External_Verdaux
);
5668 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5669 size
+= sizeof (Elf_External_Verdaux
);
5673 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5674 if (s
->contents
== NULL
&& s
->size
!= 0)
5677 /* Fill in the version definition section. */
5681 def
.vd_version
= VER_DEF_CURRENT
;
5682 def
.vd_flags
= VER_FLG_BASE
;
5685 if (info
->create_default_symver
)
5687 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5688 def
.vd_next
= sizeof (Elf_External_Verdef
);
5692 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5693 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5694 + sizeof (Elf_External_Verdaux
));
5697 if (soname_indx
!= (bfd_size_type
) -1)
5699 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5701 def
.vd_hash
= bfd_elf_hash (soname
);
5702 defaux
.vda_name
= soname_indx
;
5709 name
= lbasename (output_bfd
->filename
);
5710 def
.vd_hash
= bfd_elf_hash (name
);
5711 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5713 if (indx
== (bfd_size_type
) -1)
5715 defaux
.vda_name
= indx
;
5717 defaux
.vda_next
= 0;
5719 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5720 (Elf_External_Verdef
*) p
);
5721 p
+= sizeof (Elf_External_Verdef
);
5722 if (info
->create_default_symver
)
5724 /* Add a symbol representing this version. */
5726 if (! (_bfd_generic_link_add_one_symbol
5727 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5729 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5731 h
= (struct elf_link_hash_entry
*) bh
;
5734 h
->type
= STT_OBJECT
;
5735 h
->verinfo
.vertree
= NULL
;
5737 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5740 /* Create a duplicate of the base version with the same
5741 aux block, but different flags. */
5744 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5746 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5747 + sizeof (Elf_External_Verdaux
));
5750 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5751 (Elf_External_Verdef
*) p
);
5752 p
+= sizeof (Elf_External_Verdef
);
5754 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5755 (Elf_External_Verdaux
*) p
);
5756 p
+= sizeof (Elf_External_Verdaux
);
5758 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5761 struct bfd_elf_version_deps
*n
;
5764 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5767 /* Add a symbol representing this version. */
5769 if (! (_bfd_generic_link_add_one_symbol
5770 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5772 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5774 h
= (struct elf_link_hash_entry
*) bh
;
5777 h
->type
= STT_OBJECT
;
5778 h
->verinfo
.vertree
= t
;
5780 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5783 def
.vd_version
= VER_DEF_CURRENT
;
5785 if (t
->globals
.list
== NULL
5786 && t
->locals
.list
== NULL
5788 def
.vd_flags
|= VER_FLG_WEAK
;
5789 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5790 def
.vd_cnt
= cdeps
+ 1;
5791 def
.vd_hash
= bfd_elf_hash (t
->name
);
5792 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5794 if (t
->next
!= NULL
)
5795 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5796 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5798 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5799 (Elf_External_Verdef
*) p
);
5800 p
+= sizeof (Elf_External_Verdef
);
5802 defaux
.vda_name
= h
->dynstr_index
;
5803 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5805 defaux
.vda_next
= 0;
5806 if (t
->deps
!= NULL
)
5807 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5808 t
->name_indx
= defaux
.vda_name
;
5810 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5811 (Elf_External_Verdaux
*) p
);
5812 p
+= sizeof (Elf_External_Verdaux
);
5814 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5816 if (n
->version_needed
== NULL
)
5818 /* This can happen if there was an error in the
5820 defaux
.vda_name
= 0;
5824 defaux
.vda_name
= n
->version_needed
->name_indx
;
5825 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5828 if (n
->next
== NULL
)
5829 defaux
.vda_next
= 0;
5831 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5833 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5834 (Elf_External_Verdaux
*) p
);
5835 p
+= sizeof (Elf_External_Verdaux
);
5839 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5840 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5843 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5846 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5848 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5851 else if (info
->flags
& DF_BIND_NOW
)
5853 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5859 if (info
->executable
)
5860 info
->flags_1
&= ~ (DF_1_INITFIRST
5863 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5867 /* Work out the size of the version reference section. */
5869 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5870 BFD_ASSERT (s
!= NULL
);
5872 struct elf_find_verdep_info sinfo
;
5874 sinfo
.output_bfd
= output_bfd
;
5876 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5877 if (sinfo
.vers
== 0)
5879 sinfo
.failed
= FALSE
;
5881 elf_link_hash_traverse (elf_hash_table (info
),
5882 _bfd_elf_link_find_version_dependencies
,
5885 if (elf_tdata (output_bfd
)->verref
== NULL
)
5886 s
->flags
|= SEC_EXCLUDE
;
5889 Elf_Internal_Verneed
*t
;
5894 /* Build the version definition section. */
5897 for (t
= elf_tdata (output_bfd
)->verref
;
5901 Elf_Internal_Vernaux
*a
;
5903 size
+= sizeof (Elf_External_Verneed
);
5905 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5906 size
+= sizeof (Elf_External_Vernaux
);
5910 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5911 if (s
->contents
== NULL
)
5915 for (t
= elf_tdata (output_bfd
)->verref
;
5920 Elf_Internal_Vernaux
*a
;
5924 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5927 t
->vn_version
= VER_NEED_CURRENT
;
5929 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5930 elf_dt_name (t
->vn_bfd
) != NULL
5931 ? elf_dt_name (t
->vn_bfd
)
5932 : lbasename (t
->vn_bfd
->filename
),
5934 if (indx
== (bfd_size_type
) -1)
5937 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5938 if (t
->vn_nextref
== NULL
)
5941 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5942 + caux
* sizeof (Elf_External_Vernaux
));
5944 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5945 (Elf_External_Verneed
*) p
);
5946 p
+= sizeof (Elf_External_Verneed
);
5948 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5950 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5951 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5952 a
->vna_nodename
, FALSE
);
5953 if (indx
== (bfd_size_type
) -1)
5956 if (a
->vna_nextptr
== NULL
)
5959 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5961 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5962 (Elf_External_Vernaux
*) p
);
5963 p
+= sizeof (Elf_External_Vernaux
);
5967 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5968 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5971 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5975 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5976 && elf_tdata (output_bfd
)->cverdefs
== 0)
5977 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5978 §ion_sym_count
) == 0)
5980 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5981 s
->flags
|= SEC_EXCLUDE
;
5987 /* Find the first non-excluded output section. We'll use its
5988 section symbol for some emitted relocs. */
5990 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
5994 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5995 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
5996 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5998 elf_hash_table (info
)->text_index_section
= s
;
6003 /* Find two non-excluded output sections, one for code, one for data.
6004 We'll use their section symbols for some emitted relocs. */
6006 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6010 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6011 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6012 == (SEC_ALLOC
| SEC_READONLY
))
6013 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6015 elf_hash_table (info
)->text_index_section
= s
;
6019 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6020 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6021 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6023 elf_hash_table (info
)->data_index_section
= s
;
6027 if (elf_hash_table (info
)->text_index_section
== NULL
)
6028 elf_hash_table (info
)->text_index_section
6029 = elf_hash_table (info
)->data_index_section
;
6033 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6035 const struct elf_backend_data
*bed
;
6037 if (!is_elf_hash_table (info
->hash
))
6040 bed
= get_elf_backend_data (output_bfd
);
6041 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6043 if (elf_hash_table (info
)->dynamic_sections_created
)
6047 bfd_size_type dynsymcount
;
6048 unsigned long section_sym_count
;
6049 unsigned int dtagcount
;
6051 dynobj
= elf_hash_table (info
)->dynobj
;
6053 /* Assign dynsym indicies. In a shared library we generate a
6054 section symbol for each output section, which come first.
6055 Next come all of the back-end allocated local dynamic syms,
6056 followed by the rest of the global symbols. */
6058 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6059 §ion_sym_count
);
6061 /* Work out the size of the symbol version section. */
6062 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6063 BFD_ASSERT (s
!= NULL
);
6064 if (dynsymcount
!= 0
6065 && (s
->flags
& SEC_EXCLUDE
) == 0)
6067 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6068 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6069 if (s
->contents
== NULL
)
6072 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6076 /* Set the size of the .dynsym and .hash sections. We counted
6077 the number of dynamic symbols in elf_link_add_object_symbols.
6078 We will build the contents of .dynsym and .hash when we build
6079 the final symbol table, because until then we do not know the
6080 correct value to give the symbols. We built the .dynstr
6081 section as we went along in elf_link_add_object_symbols. */
6082 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6083 BFD_ASSERT (s
!= NULL
);
6084 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6086 if (dynsymcount
!= 0)
6088 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6089 if (s
->contents
== NULL
)
6092 /* The first entry in .dynsym is a dummy symbol.
6093 Clear all the section syms, in case we don't output them all. */
6094 ++section_sym_count
;
6095 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6098 elf_hash_table (info
)->bucketcount
= 0;
6100 /* Compute the size of the hashing table. As a side effect this
6101 computes the hash values for all the names we export. */
6102 if (info
->emit_hash
)
6104 unsigned long int *hashcodes
;
6105 unsigned long int *hashcodesp
;
6107 unsigned long int nsyms
;
6109 size_t hash_entry_size
;
6111 /* Compute the hash values for all exported symbols. At the same
6112 time store the values in an array so that we could use them for
6114 amt
= dynsymcount
* sizeof (unsigned long int);
6115 hashcodes
= bfd_malloc (amt
);
6116 if (hashcodes
== NULL
)
6118 hashcodesp
= hashcodes
;
6120 /* Put all hash values in HASHCODES. */
6121 elf_link_hash_traverse (elf_hash_table (info
),
6122 elf_collect_hash_codes
, &hashcodesp
);
6124 nsyms
= hashcodesp
- hashcodes
;
6126 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6129 if (bucketcount
== 0)
6132 elf_hash_table (info
)->bucketcount
= bucketcount
;
6134 s
= bfd_get_section_by_name (dynobj
, ".hash");
6135 BFD_ASSERT (s
!= NULL
);
6136 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6137 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6138 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6139 if (s
->contents
== NULL
)
6142 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6143 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6144 s
->contents
+ hash_entry_size
);
6147 if (info
->emit_gnu_hash
)
6150 unsigned char *contents
;
6151 struct collect_gnu_hash_codes cinfo
;
6155 memset (&cinfo
, 0, sizeof (cinfo
));
6157 /* Compute the hash values for all exported symbols. At the same
6158 time store the values in an array so that we could use them for
6160 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6161 cinfo
.hashcodes
= bfd_malloc (amt
);
6162 if (cinfo
.hashcodes
== NULL
)
6165 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6166 cinfo
.min_dynindx
= -1;
6167 cinfo
.output_bfd
= output_bfd
;
6170 /* Put all hash values in HASHCODES. */
6171 elf_link_hash_traverse (elf_hash_table (info
),
6172 elf_collect_gnu_hash_codes
, &cinfo
);
6175 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6177 if (bucketcount
== 0)
6179 free (cinfo
.hashcodes
);
6183 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6184 BFD_ASSERT (s
!= NULL
);
6186 if (cinfo
.nsyms
== 0)
6188 /* Empty .gnu.hash section is special. */
6189 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6190 free (cinfo
.hashcodes
);
6191 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6192 contents
= bfd_zalloc (output_bfd
, s
->size
);
6193 if (contents
== NULL
)
6195 s
->contents
= contents
;
6196 /* 1 empty bucket. */
6197 bfd_put_32 (output_bfd
, 1, contents
);
6198 /* SYMIDX above the special symbol 0. */
6199 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6200 /* Just one word for bitmask. */
6201 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6202 /* Only hash fn bloom filter. */
6203 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6204 /* No hashes are valid - empty bitmask. */
6205 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6206 /* No hashes in the only bucket. */
6207 bfd_put_32 (output_bfd
, 0,
6208 contents
+ 16 + bed
->s
->arch_size
/ 8);
6212 unsigned long int maskwords
, maskbitslog2
;
6213 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6215 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6216 if (maskbitslog2
< 3)
6218 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6219 maskbitslog2
= maskbitslog2
+ 3;
6221 maskbitslog2
= maskbitslog2
+ 2;
6222 if (bed
->s
->arch_size
== 64)
6224 if (maskbitslog2
== 5)
6230 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6231 cinfo
.shift2
= maskbitslog2
;
6232 cinfo
.maskbits
= 1 << maskbitslog2
;
6233 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6234 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6235 amt
+= maskwords
* sizeof (bfd_vma
);
6236 cinfo
.bitmask
= bfd_malloc (amt
);
6237 if (cinfo
.bitmask
== NULL
)
6239 free (cinfo
.hashcodes
);
6243 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6244 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6245 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6246 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6248 /* Determine how often each hash bucket is used. */
6249 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6250 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6251 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6253 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6254 if (cinfo
.counts
[i
] != 0)
6256 cinfo
.indx
[i
] = cnt
;
6257 cnt
+= cinfo
.counts
[i
];
6259 BFD_ASSERT (cnt
== dynsymcount
);
6260 cinfo
.bucketcount
= bucketcount
;
6261 cinfo
.local_indx
= cinfo
.min_dynindx
;
6263 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6264 s
->size
+= cinfo
.maskbits
/ 8;
6265 contents
= bfd_zalloc (output_bfd
, s
->size
);
6266 if (contents
== NULL
)
6268 free (cinfo
.bitmask
);
6269 free (cinfo
.hashcodes
);
6273 s
->contents
= contents
;
6274 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6275 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6276 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6277 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6278 contents
+= 16 + cinfo
.maskbits
/ 8;
6280 for (i
= 0; i
< bucketcount
; ++i
)
6282 if (cinfo
.counts
[i
] == 0)
6283 bfd_put_32 (output_bfd
, 0, contents
);
6285 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6289 cinfo
.contents
= contents
;
6291 /* Renumber dynamic symbols, populate .gnu.hash section. */
6292 elf_link_hash_traverse (elf_hash_table (info
),
6293 elf_renumber_gnu_hash_syms
, &cinfo
);
6295 contents
= s
->contents
+ 16;
6296 for (i
= 0; i
< maskwords
; ++i
)
6298 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6300 contents
+= bed
->s
->arch_size
/ 8;
6303 free (cinfo
.bitmask
);
6304 free (cinfo
.hashcodes
);
6308 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6309 BFD_ASSERT (s
!= NULL
);
6311 elf_finalize_dynstr (output_bfd
, info
);
6313 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6315 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6316 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6323 /* Final phase of ELF linker. */
6325 /* A structure we use to avoid passing large numbers of arguments. */
6327 struct elf_final_link_info
6329 /* General link information. */
6330 struct bfd_link_info
*info
;
6333 /* Symbol string table. */
6334 struct bfd_strtab_hash
*symstrtab
;
6335 /* .dynsym section. */
6336 asection
*dynsym_sec
;
6337 /* .hash section. */
6339 /* symbol version section (.gnu.version). */
6340 asection
*symver_sec
;
6341 /* Buffer large enough to hold contents of any section. */
6343 /* Buffer large enough to hold external relocs of any section. */
6344 void *external_relocs
;
6345 /* Buffer large enough to hold internal relocs of any section. */
6346 Elf_Internal_Rela
*internal_relocs
;
6347 /* Buffer large enough to hold external local symbols of any input
6349 bfd_byte
*external_syms
;
6350 /* And a buffer for symbol section indices. */
6351 Elf_External_Sym_Shndx
*locsym_shndx
;
6352 /* Buffer large enough to hold internal local symbols of any input
6354 Elf_Internal_Sym
*internal_syms
;
6355 /* Array large enough to hold a symbol index for each local symbol
6356 of any input BFD. */
6358 /* Array large enough to hold a section pointer for each local
6359 symbol of any input BFD. */
6360 asection
**sections
;
6361 /* Buffer to hold swapped out symbols. */
6363 /* And one for symbol section indices. */
6364 Elf_External_Sym_Shndx
*symshndxbuf
;
6365 /* Number of swapped out symbols in buffer. */
6366 size_t symbuf_count
;
6367 /* Number of symbols which fit in symbuf. */
6369 /* And same for symshndxbuf. */
6370 size_t shndxbuf_size
;
6373 /* This struct is used to pass information to elf_link_output_extsym. */
6375 struct elf_outext_info
6378 bfd_boolean localsyms
;
6379 struct elf_final_link_info
*finfo
;
6383 /* Support for evaluating a complex relocation.
6385 Complex relocations are generalized, self-describing relocations. The
6386 implementation of them consists of two parts: complex symbols, and the
6387 relocations themselves.
6389 The relocations are use a reserved elf-wide relocation type code (R_RELC
6390 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6391 information (start bit, end bit, word width, etc) into the addend. This
6392 information is extracted from CGEN-generated operand tables within gas.
6394 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6395 internal) representing prefix-notation expressions, including but not
6396 limited to those sorts of expressions normally encoded as addends in the
6397 addend field. The symbol mangling format is:
6400 | <unary-operator> ':' <node>
6401 | <binary-operator> ':' <node> ':' <node>
6404 <literal> := 's' <digits=N> ':' <N character symbol name>
6405 | 'S' <digits=N> ':' <N character section name>
6409 <binary-operator> := as in C
6410 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6413 set_symbol_value (bfd
* bfd_with_globals
,
6414 struct elf_final_link_info
* finfo
,
6418 bfd_boolean is_local
;
6419 Elf_Internal_Sym
* sym
;
6420 struct elf_link_hash_entry
** sym_hashes
;
6421 struct elf_link_hash_entry
* h
;
6423 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
6424 sym
= finfo
->internal_syms
+ symidx
;
6425 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
6429 /* It is a local symbol: move it to the
6430 "absolute" section and give it a value. */
6431 sym
->st_shndx
= SHN_ABS
;
6432 sym
->st_value
= val
;
6436 /* It is a global symbol: set its link type
6437 to "defined" and give it a value. */
6438 h
= sym_hashes
[symidx
];
6439 while (h
->root
.type
== bfd_link_hash_indirect
6440 || h
->root
.type
== bfd_link_hash_warning
)
6441 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6442 h
->root
.type
= bfd_link_hash_defined
;
6443 h
->root
.u
.def
.value
= val
;
6444 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6449 resolve_symbol (const char * name
,
6451 struct elf_final_link_info
* finfo
,
6455 Elf_Internal_Sym
* sym
;
6456 struct bfd_link_hash_entry
* global_entry
;
6457 const char * candidate
= NULL
;
6458 Elf_Internal_Shdr
* symtab_hdr
;
6459 asection
* sec
= NULL
;
6462 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6464 for (i
= 0; i
< locsymcount
; ++ i
)
6466 sym
= finfo
->internal_syms
+ i
;
6467 sec
= finfo
->sections
[i
];
6469 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
6472 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
6473 symtab_hdr
->sh_link
,
6476 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6477 name
, candidate
, (unsigned int)sym
->st_value
);
6479 if (candidate
&& strcmp (candidate
, name
) == 0)
6481 * result
= sym
->st_value
;
6483 if (sym
->st_shndx
> SHN_UNDEF
&&
6484 sym
->st_shndx
< SHN_LORESERVE
)
6487 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6488 sec
->output_section
->name
,
6489 (unsigned int)sec
->output_section
->vma
,
6490 (unsigned int)sec
->output_offset
);
6492 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
6495 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
6501 /* Hmm, haven't found it yet. perhaps it is a global. */
6502 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
6506 if (global_entry
->type
== bfd_link_hash_defined
6507 || global_entry
->type
== bfd_link_hash_defweak
)
6509 * result
= global_entry
->u
.def
.value
6510 + global_entry
->u
.def
.section
->output_section
->vma
6511 + global_entry
->u
.def
.section
->output_offset
;
6513 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6514 global_entry
->root
.string
, (unsigned int)*result
);
6519 if (global_entry
->type
== bfd_link_hash_common
)
6521 *result
= global_entry
->u
.def
.value
+
6522 bfd_com_section_ptr
->output_section
->vma
+
6523 bfd_com_section_ptr
->output_offset
;
6525 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6526 global_entry
->root
.string
, (unsigned int)*result
);
6535 resolve_section (const char * name
,
6536 asection
* sections
,
6542 for (curr
= sections
; curr
; curr
= curr
->next
)
6543 if (strcmp (curr
->name
, name
) == 0)
6545 *result
= curr
->vma
;
6549 /* Hmm. still haven't found it. try pseudo-section names. */
6550 for (curr
= sections
; curr
; curr
= curr
->next
)
6552 len
= strlen (curr
->name
);
6553 if (len
> strlen (name
))
6556 if (strncmp (curr
->name
, name
, len
) == 0)
6558 if (strncmp (".end", name
+ len
, 4) == 0)
6560 *result
= curr
->vma
+ curr
->size
;
6564 /* Insert more pseudo-section names here, if you like. */
6572 undefined_reference (const char * reftype
,
6575 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
6579 eval_symbol (bfd_vma
* result
,
6583 struct elf_final_link_info
* finfo
,
6585 bfd_vma section_offset
,
6593 const int bufsz
= 4096;
6594 char symbuf
[bufsz
];
6595 const char * symend
;
6596 bfd_boolean symbol_is_section
= FALSE
;
6601 if (len
< 1 || len
> bufsz
)
6603 bfd_set_error (bfd_error_invalid_operation
);
6610 * result
= addr
+ section_offset
;
6611 * advanced
= sym
+ 1;
6616 * result
= strtoul (sym
, advanced
, 16);
6620 symbol_is_section
= TRUE
;
6623 symlen
= strtol (sym
, &sym
, 10);
6624 ++ sym
; /* Skip the trailing ':'. */
6626 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
6628 bfd_set_error (bfd_error_invalid_operation
);
6632 memcpy (symbuf
, sym
, symlen
);
6633 symbuf
[symlen
] = '\0';
6634 * advanced
= sym
+ symlen
;
6636 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6637 the symbol as a section, or vice-versa. so we're pretty liberal in our
6638 interpretation here; section means "try section first", not "must be a
6639 section", and likewise with symbol. */
6641 if (symbol_is_section
)
6643 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
6644 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
6646 undefined_reference ("section", symbuf
);
6652 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
6653 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
6656 undefined_reference ("symbol", symbuf
);
6663 /* All that remains are operators. */
6665 #define UNARY_OP(op) \
6666 if (strncmp (sym, #op, strlen (#op)) == 0) \
6668 sym += strlen (#op); \
6671 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6672 section_offset, locsymcount, signed_p) \
6676 * result = op ((signed)a); \
6683 #define BINARY_OP(op) \
6684 if (strncmp (sym, #op, strlen (#op)) == 0) \
6686 sym += strlen (#op); \
6689 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6690 section_offset, locsymcount, signed_p) \
6694 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6695 section_offset, locsymcount, signed_p) \
6699 * result = ((signed) a) op ((signed) b); \
6701 * result = a op b; \
6730 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
6731 bfd_set_error (bfd_error_invalid_operation
);
6736 /* Entry point to evaluator, called from elf_link_input_bfd. */
6739 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
6740 struct elf_final_link_info
* finfo
,
6743 const struct elf_backend_data
* bed
;
6744 Elf_Internal_Shdr
* symtab_hdr
;
6745 struct elf_link_hash_entry
** sym_hashes
;
6746 asection
* reloc_sec
;
6747 bfd_boolean result
= TRUE
;
6749 /* For each section, we're going to check and see if it has any
6750 complex relocations, and we're going to evaluate any of them
6753 if (finfo
->info
->relocatable
)
6756 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6757 sym_hashes
= elf_sym_hashes (input_bfd
);
6758 bed
= get_elf_backend_data (input_bfd
);
6760 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
6762 Elf_Internal_Rela
* internal_relocs
;
6765 /* This section was omitted from the link. */
6766 if (! reloc_sec
->linker_mark
)
6769 /* Only process sections containing relocs. */
6770 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
6773 if (reloc_sec
->reloc_count
== 0)
6776 /* Read in the relocs for this section. */
6778 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
6779 (Elf_Internal_Rela
*) NULL
,
6781 if (internal_relocs
== NULL
)
6784 for (i
= reloc_sec
->reloc_count
; i
--;)
6786 Elf_Internal_Rela
* rel
;
6789 Elf_Internal_Sym
* sym
;
6791 bfd_vma section_offset
;
6795 rel
= internal_relocs
+ i
;
6796 section_offset
= reloc_sec
->output_section
->vma
6797 + reloc_sec
->output_offset
;
6798 addr
= rel
->r_offset
;
6800 index
= ELF32_R_SYM (rel
->r_info
);
6801 if (bed
->s
->arch_size
== 64)
6804 if (index
== STN_UNDEF
)
6807 if (index
< locsymcount
)
6809 /* The symbol is local. */
6810 sym
= finfo
->internal_syms
+ index
;
6812 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6813 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
6814 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
6817 sym_name
= bfd_elf_string_from_elf_section
6818 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
6820 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
6824 /* The symbol is global. */
6825 struct elf_link_hash_entry
* h
;
6827 if (elf_bad_symtab (input_bfd
))
6830 h
= sym_hashes
[index
- locsymcount
];
6831 while ( h
->root
.type
== bfd_link_hash_indirect
6832 || h
->root
.type
== bfd_link_hash_warning
)
6833 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6835 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
6838 signed_p
= (h
->type
== STT_SRELC
);
6839 sym_name
= (char *) h
->root
.root
.string
;
6842 printf ("Encountered a complex symbol!");
6843 printf (" (input_bfd %s, section %s, reloc %ld\n",
6844 input_bfd
->filename
, reloc_sec
->name
, i
);
6845 printf (" symbol: idx %8.8lx, name %s\n",
6847 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6849 printf (" Evaluating '%s' ...\n ", sym_name
);
6851 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
6852 finfo
, addr
, section_offset
, locsymcount
,
6854 /* Symbol evaluated OK. Update to absolute value. */
6855 set_symbol_value (input_bfd
, finfo
, index
, result
);
6861 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
6862 free (internal_relocs
);
6865 /* If nothing went wrong, then we adjusted
6866 everything we wanted to adjust. */
6871 put_value (bfd_vma size
,
6872 unsigned long chunksz
,
6875 bfd_byte
* location
)
6877 location
+= (size
- chunksz
);
6879 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
6887 bfd_put_8 (input_bfd
, x
, location
);
6890 bfd_put_16 (input_bfd
, x
, location
);
6893 bfd_put_32 (input_bfd
, x
, location
);
6897 bfd_put_64 (input_bfd
, x
, location
);
6907 get_value (bfd_vma size
,
6908 unsigned long chunksz
,
6910 bfd_byte
* location
)
6914 for (; size
; size
-= chunksz
, location
+= chunksz
)
6922 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
6925 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
6928 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
6932 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
6943 decode_complex_addend
6944 (unsigned long * start
, /* in bits */
6945 unsigned long * oplen
, /* in bits */
6946 unsigned long * len
, /* in bits */
6947 unsigned long * wordsz
, /* in bytes */
6948 unsigned long * chunksz
, /* in bytes */
6949 unsigned long * lsb0_p
,
6950 unsigned long * signed_p
,
6951 unsigned long * trunc_p
,
6952 unsigned long encoded
)
6954 * start
= encoded
& 0x3F;
6955 * len
= (encoded
>> 6) & 0x3F;
6956 * oplen
= (encoded
>> 12) & 0x3F;
6957 * wordsz
= (encoded
>> 18) & 0xF;
6958 * chunksz
= (encoded
>> 22) & 0xF;
6959 * lsb0_p
= (encoded
>> 27) & 1;
6960 * signed_p
= (encoded
>> 28) & 1;
6961 * trunc_p
= (encoded
>> 29) & 1;
6965 bfd_elf_perform_complex_relocation
6966 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
6967 struct bfd_link_info
* info
,
6969 asection
* input_section
,
6970 bfd_byte
* contents
,
6971 Elf_Internal_Rela
* rel
,
6972 Elf_Internal_Sym
* local_syms
,
6973 asection
** local_sections
)
6975 const struct elf_backend_data
* bed
;
6976 Elf_Internal_Shdr
* symtab_hdr
;
6978 bfd_vma relocation
= 0, shift
, x
;
6981 unsigned long start
, oplen
, len
, wordsz
,
6982 chunksz
, lsb0_p
, signed_p
, trunc_p
;
6984 /* Perform this reloc, since it is complex.
6985 (this is not to say that it necessarily refers to a complex
6986 symbol; merely that it is a self-describing CGEN based reloc.
6987 i.e. the addend has the complete reloc information (bit start, end,
6988 word size, etc) encoded within it.). */
6989 r_symndx
= ELF32_R_SYM (rel
->r_info
);
6990 bed
= get_elf_backend_data (input_bfd
);
6991 if (bed
->s
->arch_size
== 64)
6995 printf ("Performing complex relocation %ld...\n", r_symndx
);
6998 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6999 if (r_symndx
< symtab_hdr
->sh_info
)
7001 /* The symbol is local. */
7002 Elf_Internal_Sym
* sym
;
7004 sym
= local_syms
+ r_symndx
;
7005 sec
= local_sections
[r_symndx
];
7006 relocation
= sym
->st_value
;
7007 if (sym
->st_shndx
> SHN_UNDEF
&&
7008 sym
->st_shndx
< SHN_LORESERVE
)
7009 relocation
+= (sec
->output_offset
+
7010 sec
->output_section
->vma
);
7014 /* The symbol is global. */
7015 struct elf_link_hash_entry
**sym_hashes
;
7016 struct elf_link_hash_entry
* h
;
7018 sym_hashes
= elf_sym_hashes (input_bfd
);
7019 h
= sym_hashes
[r_symndx
];
7021 while (h
->root
.type
== bfd_link_hash_indirect
7022 || h
->root
.type
== bfd_link_hash_warning
)
7023 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7025 if (h
->root
.type
== bfd_link_hash_defined
7026 || h
->root
.type
== bfd_link_hash_defweak
)
7028 sec
= h
->root
.u
.def
.section
;
7029 relocation
= h
->root
.u
.def
.value
;
7031 if (! bfd_is_abs_section (sec
))
7032 relocation
+= (sec
->output_section
->vma
7033 + sec
->output_offset
);
7035 if (h
->root
.type
== bfd_link_hash_undefined
7036 && !((*info
->callbacks
->undefined_symbol
)
7037 (info
, h
->root
.root
.string
, input_bfd
,
7038 input_section
, rel
->r_offset
,
7039 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7040 || ELF_ST_VISIBILITY (h
->other
))))
7044 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7045 & chunksz
, & lsb0_p
, & signed_p
,
7046 & trunc_p
, rel
->r_addend
);
7048 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7051 shift
= (start
+ 1) - len
;
7053 shift
= (8 * wordsz
) - (start
+ len
);
7055 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7058 printf ("Doing complex reloc: "
7059 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7060 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7061 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7062 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7063 oplen
, x
, mask
, relocation
);
7068 /* Now do an overflow check. */
7069 if (bfd_check_overflow ((signed_p
?
7070 complain_overflow_signed
:
7071 complain_overflow_unsigned
),
7072 len
, 0, (8 * wordsz
),
7073 relocation
) == bfd_reloc_overflow
)
7074 (*_bfd_error_handler
)
7075 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7077 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7078 relocation
, (signed_p
? "(signed) " : ""), mask
);
7082 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7085 printf (" relocation: %8.8lx\n"
7086 " shifted mask: %8.8lx\n"
7087 " shifted/masked reloc: %8.8lx\n"
7088 " result: %8.8lx\n",
7089 relocation
, (mask
<< shift
),
7090 ((relocation
& mask
) << shift
), x
);
7092 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7095 /* When performing a relocatable link, the input relocations are
7096 preserved. But, if they reference global symbols, the indices
7097 referenced must be updated. Update all the relocations in
7098 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7101 elf_link_adjust_relocs (bfd
*abfd
,
7102 Elf_Internal_Shdr
*rel_hdr
,
7104 struct elf_link_hash_entry
**rel_hash
)
7107 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7109 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7110 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7111 bfd_vma r_type_mask
;
7114 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7116 swap_in
= bed
->s
->swap_reloc_in
;
7117 swap_out
= bed
->s
->swap_reloc_out
;
7119 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7121 swap_in
= bed
->s
->swap_reloca_in
;
7122 swap_out
= bed
->s
->swap_reloca_out
;
7127 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7130 if (bed
->s
->arch_size
== 32)
7137 r_type_mask
= 0xffffffff;
7141 erela
= rel_hdr
->contents
;
7142 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7144 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7147 if (*rel_hash
== NULL
)
7150 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7152 (*swap_in
) (abfd
, erela
, irela
);
7153 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7154 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7155 | (irela
[j
].r_info
& r_type_mask
));
7156 (*swap_out
) (abfd
, irela
, erela
);
7160 struct elf_link_sort_rela
7166 enum elf_reloc_type_class type
;
7167 /* We use this as an array of size int_rels_per_ext_rel. */
7168 Elf_Internal_Rela rela
[1];
7172 elf_link_sort_cmp1 (const void *A
, const void *B
)
7174 const struct elf_link_sort_rela
*a
= A
;
7175 const struct elf_link_sort_rela
*b
= B
;
7176 int relativea
, relativeb
;
7178 relativea
= a
->type
== reloc_class_relative
;
7179 relativeb
= b
->type
== reloc_class_relative
;
7181 if (relativea
< relativeb
)
7183 if (relativea
> relativeb
)
7185 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7187 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7189 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7191 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7197 elf_link_sort_cmp2 (const void *A
, const void *B
)
7199 const struct elf_link_sort_rela
*a
= A
;
7200 const struct elf_link_sort_rela
*b
= B
;
7203 if (a
->u
.offset
< b
->u
.offset
)
7205 if (a
->u
.offset
> b
->u
.offset
)
7207 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7208 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7213 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7215 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7221 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7223 asection
*dynamic_relocs
;
7224 asection
* rela_dyn
;
7226 bfd_size_type count
, size
;
7227 size_t i
, ret
, sort_elt
, ext_size
;
7228 bfd_byte
*sort
, *s_non_relative
, *p
;
7229 struct elf_link_sort_rela
*sq
;
7230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7231 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7232 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7233 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7234 struct bfd_link_order
*lo
;
7236 bfd_boolean use_rela
;
7238 /* Find a dynamic reloc section. */
7239 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7240 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7241 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
7242 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7244 bfd_boolean use_rela_initialised
= FALSE
;
7246 /* This is just here to stop gcc from complaining.
7247 It's initialization checking code is not perfect. */
7250 /* Both sections are present. Examine the sizes
7251 of the indirect sections to help us choose. */
7252 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7253 if (lo
->type
== bfd_indirect_link_order
)
7255 asection
*o
= lo
->u
.indirect
.section
;
7257 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7259 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7260 /* Section size is divisible by both rel and rela sizes.
7261 It is of no help to us. */
7265 /* Section size is only divisible by rela. */
7266 if (use_rela_initialised
&& (use_rela
== FALSE
))
7269 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7270 bfd_set_error (bfd_error_invalid_operation
);
7276 use_rela_initialised
= TRUE
;
7280 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7282 /* Section size is only divisible by rel. */
7283 if (use_rela_initialised
&& (use_rela
== TRUE
))
7286 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7287 bfd_set_error (bfd_error_invalid_operation
);
7293 use_rela_initialised
= TRUE
;
7298 /* The section size is not divisible by either - something is wrong. */
7300 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7301 bfd_set_error (bfd_error_invalid_operation
);
7306 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7307 if (lo
->type
== bfd_indirect_link_order
)
7309 asection
*o
= lo
->u
.indirect
.section
;
7311 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7313 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7314 /* Section size is divisible by both rel and rela sizes.
7315 It is of no help to us. */
7319 /* Section size is only divisible by rela. */
7320 if (use_rela_initialised
&& (use_rela
== FALSE
))
7323 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7324 bfd_set_error (bfd_error_invalid_operation
);
7330 use_rela_initialised
= TRUE
;
7334 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7336 /* Section size is only divisible by rel. */
7337 if (use_rela_initialised
&& (use_rela
== TRUE
))
7340 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7341 bfd_set_error (bfd_error_invalid_operation
);
7347 use_rela_initialised
= TRUE
;
7352 /* The section size is not divisible by either - something is wrong. */
7354 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7355 bfd_set_error (bfd_error_invalid_operation
);
7360 if (! use_rela_initialised
)
7364 else if (rela_dyn
== NULL
|| rela_dyn
->size
== 0)
7371 dynamic_relocs
= rela_dyn
;
7372 ext_size
= bed
->s
->sizeof_rela
;
7373 swap_in
= bed
->s
->swap_reloca_in
;
7374 swap_out
= bed
->s
->swap_reloca_out
;
7378 dynamic_relocs
= rel_dyn
;
7379 ext_size
= bed
->s
->sizeof_rel
;
7380 swap_in
= bed
->s
->swap_reloc_in
;
7381 swap_out
= bed
->s
->swap_reloc_out
;
7385 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7386 if (lo
->type
== bfd_indirect_link_order
)
7387 size
+= lo
->u
.indirect
.section
->size
;
7389 if (size
!= dynamic_relocs
->size
)
7392 sort_elt
= (sizeof (struct elf_link_sort_rela
)
7393 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
7395 count
= dynamic_relocs
->size
/ ext_size
;
7396 sort
= bfd_zmalloc (sort_elt
* count
);
7400 (*info
->callbacks
->warning
)
7401 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
7405 if (bed
->s
->arch_size
== 32)
7406 r_sym_mask
= ~(bfd_vma
) 0xff;
7408 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
7410 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7411 if (lo
->type
== bfd_indirect_link_order
)
7413 bfd_byte
*erel
, *erelend
;
7414 asection
*o
= lo
->u
.indirect
.section
;
7416 if (o
->contents
== NULL
&& o
->size
!= 0)
7418 /* This is a reloc section that is being handled as a normal
7419 section. See bfd_section_from_shdr. We can't combine
7420 relocs in this case. */
7425 erelend
= o
->contents
+ o
->size
;
7426 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7428 while (erel
< erelend
)
7430 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7432 (*swap_in
) (abfd
, erel
, s
->rela
);
7433 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
7434 s
->u
.sym_mask
= r_sym_mask
;
7440 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
7442 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
7444 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7445 if (s
->type
!= reloc_class_relative
)
7451 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
7452 for (; i
< count
; i
++, p
+= sort_elt
)
7454 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
7455 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
7457 sp
->u
.offset
= sq
->rela
->r_offset
;
7460 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
7462 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7463 if (lo
->type
== bfd_indirect_link_order
)
7465 bfd_byte
*erel
, *erelend
;
7466 asection
*o
= lo
->u
.indirect
.section
;
7469 erelend
= o
->contents
+ o
->size
;
7470 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7471 while (erel
< erelend
)
7473 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7474 (*swap_out
) (abfd
, s
->rela
, erel
);
7481 *psec
= dynamic_relocs
;
7485 /* Flush the output symbols to the file. */
7488 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
7489 const struct elf_backend_data
*bed
)
7491 if (finfo
->symbuf_count
> 0)
7493 Elf_Internal_Shdr
*hdr
;
7497 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
7498 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
7499 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7500 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
7501 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
7504 hdr
->sh_size
+= amt
;
7505 finfo
->symbuf_count
= 0;
7511 /* Add a symbol to the output symbol table. */
7514 elf_link_output_sym (struct elf_final_link_info
*finfo
,
7516 Elf_Internal_Sym
*elfsym
,
7517 asection
*input_sec
,
7518 struct elf_link_hash_entry
*h
)
7521 Elf_External_Sym_Shndx
*destshndx
;
7522 bfd_boolean (*output_symbol_hook
)
7523 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
7524 struct elf_link_hash_entry
*);
7525 const struct elf_backend_data
*bed
;
7527 bed
= get_elf_backend_data (finfo
->output_bfd
);
7528 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
7529 if (output_symbol_hook
!= NULL
)
7531 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
7535 if (name
== NULL
|| *name
== '\0')
7536 elfsym
->st_name
= 0;
7537 else if (input_sec
->flags
& SEC_EXCLUDE
)
7538 elfsym
->st_name
= 0;
7541 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
7543 if (elfsym
->st_name
== (unsigned long) -1)
7547 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
7549 if (! elf_link_flush_output_syms (finfo
, bed
))
7553 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7554 destshndx
= finfo
->symshndxbuf
;
7555 if (destshndx
!= NULL
)
7557 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
7561 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
7562 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
7563 if (destshndx
== NULL
)
7565 memset ((char *) destshndx
+ amt
, 0, amt
);
7566 finfo
->shndxbuf_size
*= 2;
7568 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
7571 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
7572 finfo
->symbuf_count
+= 1;
7573 bfd_get_symcount (finfo
->output_bfd
) += 1;
7578 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7581 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
7583 if (sym
->st_shndx
> SHN_HIRESERVE
)
7585 /* The gABI doesn't support dynamic symbols in output sections
7587 (*_bfd_error_handler
)
7588 (_("%B: Too many sections: %d (>= %d)"),
7589 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
7590 bfd_set_error (bfd_error_nonrepresentable_section
);
7596 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7597 allowing an unsatisfied unversioned symbol in the DSO to match a
7598 versioned symbol that would normally require an explicit version.
7599 We also handle the case that a DSO references a hidden symbol
7600 which may be satisfied by a versioned symbol in another DSO. */
7603 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
7604 const struct elf_backend_data
*bed
,
7605 struct elf_link_hash_entry
*h
)
7608 struct elf_link_loaded_list
*loaded
;
7610 if (!is_elf_hash_table (info
->hash
))
7613 switch (h
->root
.type
)
7619 case bfd_link_hash_undefined
:
7620 case bfd_link_hash_undefweak
:
7621 abfd
= h
->root
.u
.undef
.abfd
;
7622 if ((abfd
->flags
& DYNAMIC
) == 0
7623 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
7627 case bfd_link_hash_defined
:
7628 case bfd_link_hash_defweak
:
7629 abfd
= h
->root
.u
.def
.section
->owner
;
7632 case bfd_link_hash_common
:
7633 abfd
= h
->root
.u
.c
.p
->section
->owner
;
7636 BFD_ASSERT (abfd
!= NULL
);
7638 for (loaded
= elf_hash_table (info
)->loaded
;
7640 loaded
= loaded
->next
)
7643 Elf_Internal_Shdr
*hdr
;
7644 bfd_size_type symcount
;
7645 bfd_size_type extsymcount
;
7646 bfd_size_type extsymoff
;
7647 Elf_Internal_Shdr
*versymhdr
;
7648 Elf_Internal_Sym
*isym
;
7649 Elf_Internal_Sym
*isymend
;
7650 Elf_Internal_Sym
*isymbuf
;
7651 Elf_External_Versym
*ever
;
7652 Elf_External_Versym
*extversym
;
7654 input
= loaded
->abfd
;
7656 /* We check each DSO for a possible hidden versioned definition. */
7658 || (input
->flags
& DYNAMIC
) == 0
7659 || elf_dynversym (input
) == 0)
7662 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
7664 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7665 if (elf_bad_symtab (input
))
7667 extsymcount
= symcount
;
7672 extsymcount
= symcount
- hdr
->sh_info
;
7673 extsymoff
= hdr
->sh_info
;
7676 if (extsymcount
== 0)
7679 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
7681 if (isymbuf
== NULL
)
7684 /* Read in any version definitions. */
7685 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
7686 extversym
= bfd_malloc (versymhdr
->sh_size
);
7687 if (extversym
== NULL
)
7690 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
7691 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
7692 != versymhdr
->sh_size
))
7700 ever
= extversym
+ extsymoff
;
7701 isymend
= isymbuf
+ extsymcount
;
7702 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
7705 Elf_Internal_Versym iver
;
7706 unsigned short version_index
;
7708 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
7709 || isym
->st_shndx
== SHN_UNDEF
)
7712 name
= bfd_elf_string_from_elf_section (input
,
7715 if (strcmp (name
, h
->root
.root
.string
) != 0)
7718 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
7720 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
7722 /* If we have a non-hidden versioned sym, then it should
7723 have provided a definition for the undefined sym. */
7727 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
7728 if (version_index
== 1 || version_index
== 2)
7730 /* This is the base or first version. We can use it. */
7744 /* Add an external symbol to the symbol table. This is called from
7745 the hash table traversal routine. When generating a shared object,
7746 we go through the symbol table twice. The first time we output
7747 anything that might have been forced to local scope in a version
7748 script. The second time we output the symbols that are still
7752 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
7754 struct elf_outext_info
*eoinfo
= data
;
7755 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
7757 Elf_Internal_Sym sym
;
7758 asection
*input_sec
;
7759 const struct elf_backend_data
*bed
;
7761 if (h
->root
.type
== bfd_link_hash_warning
)
7763 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7764 if (h
->root
.type
== bfd_link_hash_new
)
7768 /* Decide whether to output this symbol in this pass. */
7769 if (eoinfo
->localsyms
)
7771 if (!h
->forced_local
)
7776 if (h
->forced_local
)
7780 bed
= get_elf_backend_data (finfo
->output_bfd
);
7782 if (h
->root
.type
== bfd_link_hash_undefined
)
7784 /* If we have an undefined symbol reference here then it must have
7785 come from a shared library that is being linked in. (Undefined
7786 references in regular files have already been handled). */
7787 bfd_boolean ignore_undef
= FALSE
;
7789 /* Some symbols may be special in that the fact that they're
7790 undefined can be safely ignored - let backend determine that. */
7791 if (bed
->elf_backend_ignore_undef_symbol
)
7792 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
7794 /* If we are reporting errors for this situation then do so now. */
7795 if (ignore_undef
== FALSE
7798 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
7799 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
7801 if (! (finfo
->info
->callbacks
->undefined_symbol
7802 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
7803 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
7805 eoinfo
->failed
= TRUE
;
7811 /* We should also warn if a forced local symbol is referenced from
7812 shared libraries. */
7813 if (! finfo
->info
->relocatable
7814 && (! finfo
->info
->shared
)
7819 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
7821 (*_bfd_error_handler
)
7822 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7824 h
->root
.u
.def
.section
== bfd_abs_section_ptr
7825 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
7826 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
7828 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
7829 ? "hidden" : "local",
7830 h
->root
.root
.string
);
7831 eoinfo
->failed
= TRUE
;
7835 /* We don't want to output symbols that have never been mentioned by
7836 a regular file, or that we have been told to strip. However, if
7837 h->indx is set to -2, the symbol is used by a reloc and we must
7841 else if ((h
->def_dynamic
7843 || h
->root
.type
== bfd_link_hash_new
)
7847 else if (finfo
->info
->strip
== strip_all
)
7849 else if (finfo
->info
->strip
== strip_some
7850 && bfd_hash_lookup (finfo
->info
->keep_hash
,
7851 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
7853 else if (finfo
->info
->strip_discarded
7854 && (h
->root
.type
== bfd_link_hash_defined
7855 || h
->root
.type
== bfd_link_hash_defweak
)
7856 && elf_discarded_section (h
->root
.u
.def
.section
))
7861 /* If we're stripping it, and it's not a dynamic symbol, there's
7862 nothing else to do unless it is a forced local symbol. */
7865 && !h
->forced_local
)
7869 sym
.st_size
= h
->size
;
7870 sym
.st_other
= h
->other
;
7871 if (h
->forced_local
)
7872 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
7873 else if (h
->root
.type
== bfd_link_hash_undefweak
7874 || h
->root
.type
== bfd_link_hash_defweak
)
7875 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
7877 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
7879 switch (h
->root
.type
)
7882 case bfd_link_hash_new
:
7883 case bfd_link_hash_warning
:
7887 case bfd_link_hash_undefined
:
7888 case bfd_link_hash_undefweak
:
7889 input_sec
= bfd_und_section_ptr
;
7890 sym
.st_shndx
= SHN_UNDEF
;
7893 case bfd_link_hash_defined
:
7894 case bfd_link_hash_defweak
:
7896 input_sec
= h
->root
.u
.def
.section
;
7897 if (input_sec
->output_section
!= NULL
)
7900 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7901 input_sec
->output_section
);
7902 if (sym
.st_shndx
== SHN_BAD
)
7904 (*_bfd_error_handler
)
7905 (_("%B: could not find output section %A for input section %A"),
7906 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7907 eoinfo
->failed
= TRUE
;
7911 /* ELF symbols in relocatable files are section relative,
7912 but in nonrelocatable files they are virtual
7914 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7915 if (! finfo
->info
->relocatable
)
7917 sym
.st_value
+= input_sec
->output_section
->vma
;
7918 if (h
->type
== STT_TLS
)
7920 /* STT_TLS symbols are relative to PT_TLS segment
7922 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7923 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7929 BFD_ASSERT (input_sec
->owner
== NULL
7930 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7931 sym
.st_shndx
= SHN_UNDEF
;
7932 input_sec
= bfd_und_section_ptr
;
7937 case bfd_link_hash_common
:
7938 input_sec
= h
->root
.u
.c
.p
->section
;
7939 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7940 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7943 case bfd_link_hash_indirect
:
7944 /* These symbols are created by symbol versioning. They point
7945 to the decorated version of the name. For example, if the
7946 symbol foo@@GNU_1.2 is the default, which should be used when
7947 foo is used with no version, then we add an indirect symbol
7948 foo which points to foo@@GNU_1.2. We ignore these symbols,
7949 since the indirected symbol is already in the hash table. */
7953 /* Give the processor backend a chance to tweak the symbol value,
7954 and also to finish up anything that needs to be done for this
7955 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7956 forced local syms when non-shared is due to a historical quirk. */
7957 if ((h
->dynindx
!= -1
7959 && ((finfo
->info
->shared
7960 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7961 || h
->root
.type
!= bfd_link_hash_undefweak
))
7962 || !h
->forced_local
)
7963 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7965 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7966 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7968 eoinfo
->failed
= TRUE
;
7973 /* If we are marking the symbol as undefined, and there are no
7974 non-weak references to this symbol from a regular object, then
7975 mark the symbol as weak undefined; if there are non-weak
7976 references, mark the symbol as strong. We can't do this earlier,
7977 because it might not be marked as undefined until the
7978 finish_dynamic_symbol routine gets through with it. */
7979 if (sym
.st_shndx
== SHN_UNDEF
7981 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7982 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7986 if (h
->ref_regular_nonweak
)
7987 bindtype
= STB_GLOBAL
;
7989 bindtype
= STB_WEAK
;
7990 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7993 /* If a non-weak symbol with non-default visibility is not defined
7994 locally, it is a fatal error. */
7995 if (! finfo
->info
->relocatable
7996 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7997 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7998 && h
->root
.type
== bfd_link_hash_undefined
8001 (*_bfd_error_handler
)
8002 (_("%B: %s symbol `%s' isn't defined"),
8004 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8006 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8007 ? "internal" : "hidden",
8008 h
->root
.root
.string
);
8009 eoinfo
->failed
= TRUE
;
8013 /* If this symbol should be put in the .dynsym section, then put it
8014 there now. We already know the symbol index. We also fill in
8015 the entry in the .hash section. */
8016 if (h
->dynindx
!= -1
8017 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8021 sym
.st_name
= h
->dynstr_index
;
8022 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8023 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8025 eoinfo
->failed
= TRUE
;
8028 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8030 if (finfo
->hash_sec
!= NULL
)
8032 size_t hash_entry_size
;
8033 bfd_byte
*bucketpos
;
8038 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8039 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8042 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8043 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8044 + (bucket
+ 2) * hash_entry_size
);
8045 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8046 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8047 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8048 ((bfd_byte
*) finfo
->hash_sec
->contents
8049 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8052 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8054 Elf_Internal_Versym iversym
;
8055 Elf_External_Versym
*eversym
;
8057 if (!h
->def_regular
)
8059 if (h
->verinfo
.verdef
== NULL
)
8060 iversym
.vs_vers
= 0;
8062 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8066 if (h
->verinfo
.vertree
== NULL
)
8067 iversym
.vs_vers
= 1;
8069 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8070 if (finfo
->info
->create_default_symver
)
8075 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8077 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8078 eversym
+= h
->dynindx
;
8079 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8083 /* If we're stripping it, then it was just a dynamic symbol, and
8084 there's nothing else to do. */
8085 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8088 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8090 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8092 eoinfo
->failed
= TRUE
;
8099 /* Return TRUE if special handling is done for relocs in SEC against
8100 symbols defined in discarded sections. */
8103 elf_section_ignore_discarded_relocs (asection
*sec
)
8105 const struct elf_backend_data
*bed
;
8107 switch (sec
->sec_info_type
)
8109 case ELF_INFO_TYPE_STABS
:
8110 case ELF_INFO_TYPE_EH_FRAME
:
8116 bed
= get_elf_backend_data (sec
->owner
);
8117 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8118 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8124 /* Return a mask saying how ld should treat relocations in SEC against
8125 symbols defined in discarded sections. If this function returns
8126 COMPLAIN set, ld will issue a warning message. If this function
8127 returns PRETEND set, and the discarded section was link-once and the
8128 same size as the kept link-once section, ld will pretend that the
8129 symbol was actually defined in the kept section. Otherwise ld will
8130 zero the reloc (at least that is the intent, but some cooperation by
8131 the target dependent code is needed, particularly for REL targets). */
8134 _bfd_elf_default_action_discarded (asection
*sec
)
8136 if (sec
->flags
& SEC_DEBUGGING
)
8139 if (strcmp (".eh_frame", sec
->name
) == 0)
8142 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8145 return COMPLAIN
| PRETEND
;
8148 /* Find a match between a section and a member of a section group. */
8151 match_group_member (asection
*sec
, asection
*group
,
8152 struct bfd_link_info
*info
)
8154 asection
*first
= elf_next_in_group (group
);
8155 asection
*s
= first
;
8159 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8162 s
= elf_next_in_group (s
);
8170 /* Check if the kept section of a discarded section SEC can be used
8171 to replace it. Return the replacement if it is OK. Otherwise return
8175 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8179 kept
= sec
->kept_section
;
8182 if ((kept
->flags
& SEC_GROUP
) != 0)
8183 kept
= match_group_member (sec
, kept
, info
);
8184 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8186 sec
->kept_section
= kept
;
8191 /* Link an input file into the linker output file. This function
8192 handles all the sections and relocations of the input file at once.
8193 This is so that we only have to read the local symbols once, and
8194 don't have to keep them in memory. */
8197 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8199 bfd_boolean (*relocate_section
)
8200 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8201 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8203 Elf_Internal_Shdr
*symtab_hdr
;
8206 Elf_Internal_Sym
*isymbuf
;
8207 Elf_Internal_Sym
*isym
;
8208 Elf_Internal_Sym
*isymend
;
8210 asection
**ppsection
;
8212 const struct elf_backend_data
*bed
;
8213 bfd_boolean emit_relocs
;
8214 struct elf_link_hash_entry
**sym_hashes
;
8216 output_bfd
= finfo
->output_bfd
;
8217 bed
= get_elf_backend_data (output_bfd
);
8218 relocate_section
= bed
->elf_backend_relocate_section
;
8220 /* If this is a dynamic object, we don't want to do anything here:
8221 we don't want the local symbols, and we don't want the section
8223 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8226 emit_relocs
= (finfo
->info
->relocatable
8227 || finfo
->info
->emitrelocations
);
8229 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8230 if (elf_bad_symtab (input_bfd
))
8232 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8237 locsymcount
= symtab_hdr
->sh_info
;
8238 extsymoff
= symtab_hdr
->sh_info
;
8241 /* Read the local symbols. */
8242 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8243 if (isymbuf
== NULL
&& locsymcount
!= 0)
8245 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8246 finfo
->internal_syms
,
8247 finfo
->external_syms
,
8248 finfo
->locsym_shndx
);
8249 if (isymbuf
== NULL
)
8252 /* evaluate_complex_relocation_symbols looks for symbols in
8253 finfo->internal_syms. */
8254 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
8256 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8257 finfo
->internal_syms
,
8258 finfo
->external_syms
,
8259 finfo
->locsym_shndx
);
8262 /* Find local symbol sections and adjust values of symbols in
8263 SEC_MERGE sections. Write out those local symbols we know are
8264 going into the output file. */
8265 isymend
= isymbuf
+ locsymcount
;
8266 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8268 isym
++, pindex
++, ppsection
++)
8272 Elf_Internal_Sym osym
;
8276 if (elf_bad_symtab (input_bfd
))
8278 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8285 if (isym
->st_shndx
== SHN_UNDEF
)
8286 isec
= bfd_und_section_ptr
;
8287 else if (isym
->st_shndx
< SHN_LORESERVE
8288 || isym
->st_shndx
> SHN_HIRESERVE
)
8290 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8292 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8293 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8295 _bfd_merged_section_offset (output_bfd
, &isec
,
8296 elf_section_data (isec
)->sec_info
,
8299 else if (isym
->st_shndx
== SHN_ABS
)
8300 isec
= bfd_abs_section_ptr
;
8301 else if (isym
->st_shndx
== SHN_COMMON
)
8302 isec
= bfd_com_section_ptr
;
8305 /* Don't attempt to output symbols with st_shnx in the
8306 reserved range other than SHN_ABS and SHN_COMMON. */
8313 /* Don't output the first, undefined, symbol. */
8314 if (ppsection
== finfo
->sections
)
8317 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8319 /* We never output section symbols. Instead, we use the
8320 section symbol of the corresponding section in the output
8325 /* If we are stripping all symbols, we don't want to output this
8327 if (finfo
->info
->strip
== strip_all
)
8330 /* If we are discarding all local symbols, we don't want to
8331 output this one. If we are generating a relocatable output
8332 file, then some of the local symbols may be required by
8333 relocs; we output them below as we discover that they are
8335 if (finfo
->info
->discard
== discard_all
)
8338 /* If this symbol is defined in a section which we are
8339 discarding, we don't need to keep it. */
8340 if (isym
->st_shndx
!= SHN_UNDEF
8341 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8343 || bfd_section_removed_from_list (output_bfd
,
8344 isec
->output_section
)))
8347 /* Get the name of the symbol. */
8348 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
8353 /* See if we are discarding symbols with this name. */
8354 if ((finfo
->info
->strip
== strip_some
8355 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
8357 || (((finfo
->info
->discard
== discard_sec_merge
8358 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
8359 || finfo
->info
->discard
== discard_l
)
8360 && bfd_is_local_label_name (input_bfd
, name
)))
8363 /* If we get here, we are going to output this symbol. */
8367 /* Adjust the section index for the output file. */
8368 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
8369 isec
->output_section
);
8370 if (osym
.st_shndx
== SHN_BAD
)
8373 *pindex
= bfd_get_symcount (output_bfd
);
8375 /* ELF symbols in relocatable files are section relative, but
8376 in executable files they are virtual addresses. Note that
8377 this code assumes that all ELF sections have an associated
8378 BFD section with a reasonable value for output_offset; below
8379 we assume that they also have a reasonable value for
8380 output_section. Any special sections must be set up to meet
8381 these requirements. */
8382 osym
.st_value
+= isec
->output_offset
;
8383 if (! finfo
->info
->relocatable
)
8385 osym
.st_value
+= isec
->output_section
->vma
;
8386 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
8388 /* STT_TLS symbols are relative to PT_TLS segment base. */
8389 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8390 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8394 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
8398 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
8401 /* Relocate the contents of each section. */
8402 sym_hashes
= elf_sym_hashes (input_bfd
);
8403 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
8407 if (! o
->linker_mark
)
8409 /* This section was omitted from the link. */
8413 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8414 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
8417 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
8419 /* Section was created by _bfd_elf_link_create_dynamic_sections
8424 /* Get the contents of the section. They have been cached by a
8425 relaxation routine. Note that o is a section in an input
8426 file, so the contents field will not have been set by any of
8427 the routines which work on output files. */
8428 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
8429 contents
= elf_section_data (o
)->this_hdr
.contents
;
8432 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
8434 contents
= finfo
->contents
;
8435 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
8439 if ((o
->flags
& SEC_RELOC
) != 0)
8441 Elf_Internal_Rela
*internal_relocs
;
8442 bfd_vma r_type_mask
;
8445 /* Get the swapped relocs. */
8447 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
8448 finfo
->internal_relocs
, FALSE
);
8449 if (internal_relocs
== NULL
8450 && o
->reloc_count
> 0)
8453 if (bed
->s
->arch_size
== 32)
8460 r_type_mask
= 0xffffffff;
8464 /* Run through the relocs looking for any against symbols
8465 from discarded sections and section symbols from
8466 removed link-once sections. Complain about relocs
8467 against discarded sections. Zero relocs against removed
8468 link-once sections. */
8469 if (!elf_section_ignore_discarded_relocs (o
))
8471 Elf_Internal_Rela
*rel
, *relend
;
8472 unsigned int action
= (*bed
->action_discarded
) (o
);
8474 rel
= internal_relocs
;
8475 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8476 for ( ; rel
< relend
; rel
++)
8478 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
8479 asection
**ps
, *sec
;
8480 struct elf_link_hash_entry
*h
= NULL
;
8481 const char *sym_name
;
8483 if (r_symndx
== STN_UNDEF
)
8486 if (r_symndx
>= locsymcount
8487 || (elf_bad_symtab (input_bfd
)
8488 && finfo
->sections
[r_symndx
] == NULL
))
8490 h
= sym_hashes
[r_symndx
- extsymoff
];
8492 /* Badly formatted input files can contain relocs that
8493 reference non-existant symbols. Check here so that
8494 we do not seg fault. */
8499 sprintf_vma (buffer
, rel
->r_info
);
8500 (*_bfd_error_handler
)
8501 (_("error: %B contains a reloc (0x%s) for section %A "
8502 "that references a non-existent global symbol"),
8503 input_bfd
, o
, buffer
);
8504 bfd_set_error (bfd_error_bad_value
);
8508 while (h
->root
.type
== bfd_link_hash_indirect
8509 || h
->root
.type
== bfd_link_hash_warning
)
8510 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8512 if (h
->root
.type
!= bfd_link_hash_defined
8513 && h
->root
.type
!= bfd_link_hash_defweak
)
8516 ps
= &h
->root
.u
.def
.section
;
8517 sym_name
= h
->root
.root
.string
;
8521 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
8522 ps
= &finfo
->sections
[r_symndx
];
8523 sym_name
= bfd_elf_sym_name (input_bfd
,
8528 /* Complain if the definition comes from a
8529 discarded section. */
8530 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
8532 BFD_ASSERT (r_symndx
!= 0);
8533 if (action
& COMPLAIN
)
8534 (*finfo
->info
->callbacks
->einfo
)
8535 (_("%X`%s' referenced in section `%A' of %B: "
8536 "defined in discarded section `%A' of %B\n"),
8537 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
8539 /* Try to do the best we can to support buggy old
8540 versions of gcc. Pretend that the symbol is
8541 really defined in the kept linkonce section.
8542 FIXME: This is quite broken. Modifying the
8543 symbol here means we will be changing all later
8544 uses of the symbol, not just in this section. */
8545 if (action
& PRETEND
)
8549 kept
= _bfd_elf_check_kept_section (sec
,
8561 /* Relocate the section by invoking a back end routine.
8563 The back end routine is responsible for adjusting the
8564 section contents as necessary, and (if using Rela relocs
8565 and generating a relocatable output file) adjusting the
8566 reloc addend as necessary.
8568 The back end routine does not have to worry about setting
8569 the reloc address or the reloc symbol index.
8571 The back end routine is given a pointer to the swapped in
8572 internal symbols, and can access the hash table entries
8573 for the external symbols via elf_sym_hashes (input_bfd).
8575 When generating relocatable output, the back end routine
8576 must handle STB_LOCAL/STT_SECTION symbols specially. The
8577 output symbol is going to be a section symbol
8578 corresponding to the output section, which will require
8579 the addend to be adjusted. */
8581 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
8582 input_bfd
, o
, contents
,
8590 Elf_Internal_Rela
*irela
;
8591 Elf_Internal_Rela
*irelaend
;
8592 bfd_vma last_offset
;
8593 struct elf_link_hash_entry
**rel_hash
;
8594 struct elf_link_hash_entry
**rel_hash_list
;
8595 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
8596 unsigned int next_erel
;
8597 bfd_boolean rela_normal
;
8599 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
8600 rela_normal
= (bed
->rela_normal
8601 && (input_rel_hdr
->sh_entsize
8602 == bed
->s
->sizeof_rela
));
8604 /* Adjust the reloc addresses and symbol indices. */
8606 irela
= internal_relocs
;
8607 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8608 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
8609 + elf_section_data (o
->output_section
)->rel_count
8610 + elf_section_data (o
->output_section
)->rel_count2
);
8611 rel_hash_list
= rel_hash
;
8612 last_offset
= o
->output_offset
;
8613 if (!finfo
->info
->relocatable
)
8614 last_offset
+= o
->output_section
->vma
;
8615 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
8617 unsigned long r_symndx
;
8619 Elf_Internal_Sym sym
;
8621 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
8627 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
8630 if (irela
->r_offset
>= (bfd_vma
) -2)
8632 /* This is a reloc for a deleted entry or somesuch.
8633 Turn it into an R_*_NONE reloc, at the same
8634 offset as the last reloc. elf_eh_frame.c and
8635 bfd_elf_discard_info rely on reloc offsets
8637 irela
->r_offset
= last_offset
;
8639 irela
->r_addend
= 0;
8643 irela
->r_offset
+= o
->output_offset
;
8645 /* Relocs in an executable have to be virtual addresses. */
8646 if (!finfo
->info
->relocatable
)
8647 irela
->r_offset
+= o
->output_section
->vma
;
8649 last_offset
= irela
->r_offset
;
8651 r_symndx
= irela
->r_info
>> r_sym_shift
;
8652 if (r_symndx
== STN_UNDEF
)
8655 if (r_symndx
>= locsymcount
8656 || (elf_bad_symtab (input_bfd
)
8657 && finfo
->sections
[r_symndx
] == NULL
))
8659 struct elf_link_hash_entry
*rh
;
8662 /* This is a reloc against a global symbol. We
8663 have not yet output all the local symbols, so
8664 we do not know the symbol index of any global
8665 symbol. We set the rel_hash entry for this
8666 reloc to point to the global hash table entry
8667 for this symbol. The symbol index is then
8668 set at the end of bfd_elf_final_link. */
8669 indx
= r_symndx
- extsymoff
;
8670 rh
= elf_sym_hashes (input_bfd
)[indx
];
8671 while (rh
->root
.type
== bfd_link_hash_indirect
8672 || rh
->root
.type
== bfd_link_hash_warning
)
8673 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
8675 /* Setting the index to -2 tells
8676 elf_link_output_extsym that this symbol is
8678 BFD_ASSERT (rh
->indx
< 0);
8686 /* This is a reloc against a local symbol. */
8689 sym
= isymbuf
[r_symndx
];
8690 sec
= finfo
->sections
[r_symndx
];
8691 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
8693 /* I suppose the backend ought to fill in the
8694 section of any STT_SECTION symbol against a
8695 processor specific section. */
8697 if (bfd_is_abs_section (sec
))
8699 else if (sec
== NULL
|| sec
->owner
== NULL
)
8701 bfd_set_error (bfd_error_bad_value
);
8706 asection
*osec
= sec
->output_section
;
8708 /* If we have discarded a section, the output
8709 section will be the absolute section. In
8710 case of discarded SEC_MERGE sections, use
8711 the kept section. relocate_section should
8712 have already handled discarded linkonce
8714 if (bfd_is_abs_section (osec
)
8715 && sec
->kept_section
!= NULL
8716 && sec
->kept_section
->output_section
!= NULL
)
8718 osec
= sec
->kept_section
->output_section
;
8719 irela
->r_addend
-= osec
->vma
;
8722 if (!bfd_is_abs_section (osec
))
8724 r_symndx
= osec
->target_index
;
8727 struct elf_link_hash_table
*htab
;
8730 htab
= elf_hash_table (finfo
->info
);
8731 oi
= htab
->text_index_section
;
8732 if ((osec
->flags
& SEC_READONLY
) == 0
8733 && htab
->data_index_section
!= NULL
)
8734 oi
= htab
->data_index_section
;
8738 irela
->r_addend
+= osec
->vma
- oi
->vma
;
8739 r_symndx
= oi
->target_index
;
8743 BFD_ASSERT (r_symndx
!= 0);
8747 /* Adjust the addend according to where the
8748 section winds up in the output section. */
8750 irela
->r_addend
+= sec
->output_offset
;
8754 if (finfo
->indices
[r_symndx
] == -1)
8756 unsigned long shlink
;
8760 if (finfo
->info
->strip
== strip_all
)
8762 /* You can't do ld -r -s. */
8763 bfd_set_error (bfd_error_invalid_operation
);
8767 /* This symbol was skipped earlier, but
8768 since it is needed by a reloc, we
8769 must output it now. */
8770 shlink
= symtab_hdr
->sh_link
;
8771 name
= (bfd_elf_string_from_elf_section
8772 (input_bfd
, shlink
, sym
.st_name
));
8776 osec
= sec
->output_section
;
8778 _bfd_elf_section_from_bfd_section (output_bfd
,
8780 if (sym
.st_shndx
== SHN_BAD
)
8783 sym
.st_value
+= sec
->output_offset
;
8784 if (! finfo
->info
->relocatable
)
8786 sym
.st_value
+= osec
->vma
;
8787 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
8789 /* STT_TLS symbols are relative to PT_TLS
8791 BFD_ASSERT (elf_hash_table (finfo
->info
)
8793 sym
.st_value
-= (elf_hash_table (finfo
->info
)
8798 finfo
->indices
[r_symndx
]
8799 = bfd_get_symcount (output_bfd
);
8801 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
8806 r_symndx
= finfo
->indices
[r_symndx
];
8809 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
8810 | (irela
->r_info
& r_type_mask
));
8813 /* Swap out the relocs. */
8814 if (input_rel_hdr
->sh_size
!= 0
8815 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
8821 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
8822 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
8824 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
8825 * bed
->s
->int_rels_per_ext_rel
);
8826 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
8827 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
8836 /* Write out the modified section contents. */
8837 if (bed
->elf_backend_write_section
8838 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
8841 /* Section written out. */
8843 else switch (o
->sec_info_type
)
8845 case ELF_INFO_TYPE_STABS
:
8846 if (! (_bfd_write_section_stabs
8848 &elf_hash_table (finfo
->info
)->stab_info
,
8849 o
, &elf_section_data (o
)->sec_info
, contents
)))
8852 case ELF_INFO_TYPE_MERGE
:
8853 if (! _bfd_write_merged_section (output_bfd
, o
,
8854 elf_section_data (o
)->sec_info
))
8857 case ELF_INFO_TYPE_EH_FRAME
:
8859 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
8866 if (! (o
->flags
& SEC_EXCLUDE
)
8867 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
8869 (file_ptr
) o
->output_offset
,
8880 /* Generate a reloc when linking an ELF file. This is a reloc
8881 requested by the linker, and does not come from any input file. This
8882 is used to build constructor and destructor tables when linking
8886 elf_reloc_link_order (bfd
*output_bfd
,
8887 struct bfd_link_info
*info
,
8888 asection
*output_section
,
8889 struct bfd_link_order
*link_order
)
8891 reloc_howto_type
*howto
;
8895 struct elf_link_hash_entry
**rel_hash_ptr
;
8896 Elf_Internal_Shdr
*rel_hdr
;
8897 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8898 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8902 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8905 bfd_set_error (bfd_error_bad_value
);
8909 addend
= link_order
->u
.reloc
.p
->addend
;
8911 /* Figure out the symbol index. */
8912 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8913 + elf_section_data (output_section
)->rel_count
8914 + elf_section_data (output_section
)->rel_count2
);
8915 if (link_order
->type
== bfd_section_reloc_link_order
)
8917 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8918 BFD_ASSERT (indx
!= 0);
8919 *rel_hash_ptr
= NULL
;
8923 struct elf_link_hash_entry
*h
;
8925 /* Treat a reloc against a defined symbol as though it were
8926 actually against the section. */
8927 h
= ((struct elf_link_hash_entry
*)
8928 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8929 link_order
->u
.reloc
.p
->u
.name
,
8930 FALSE
, FALSE
, TRUE
));
8932 && (h
->root
.type
== bfd_link_hash_defined
8933 || h
->root
.type
== bfd_link_hash_defweak
))
8937 section
= h
->root
.u
.def
.section
;
8938 indx
= section
->output_section
->target_index
;
8939 *rel_hash_ptr
= NULL
;
8940 /* It seems that we ought to add the symbol value to the
8941 addend here, but in practice it has already been added
8942 because it was passed to constructor_callback. */
8943 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8947 /* Setting the index to -2 tells elf_link_output_extsym that
8948 this symbol is used by a reloc. */
8955 if (! ((*info
->callbacks
->unattached_reloc
)
8956 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
8962 /* If this is an inplace reloc, we must write the addend into the
8964 if (howto
->partial_inplace
&& addend
!= 0)
8967 bfd_reloc_status_type rstat
;
8970 const char *sym_name
;
8972 size
= bfd_get_reloc_size (howto
);
8973 buf
= bfd_zmalloc (size
);
8976 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8983 case bfd_reloc_outofrange
:
8986 case bfd_reloc_overflow
:
8987 if (link_order
->type
== bfd_section_reloc_link_order
)
8988 sym_name
= bfd_section_name (output_bfd
,
8989 link_order
->u
.reloc
.p
->u
.section
);
8991 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8992 if (! ((*info
->callbacks
->reloc_overflow
)
8993 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8994 NULL
, (bfd_vma
) 0)))
9001 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9002 link_order
->offset
, size
);
9008 /* The address of a reloc is relative to the section in a
9009 relocatable file, and is a virtual address in an executable
9011 offset
= link_order
->offset
;
9012 if (! info
->relocatable
)
9013 offset
+= output_section
->vma
;
9015 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9017 irel
[i
].r_offset
= offset
;
9019 irel
[i
].r_addend
= 0;
9021 if (bed
->s
->arch_size
== 32)
9022 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9024 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9026 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9027 erel
= rel_hdr
->contents
;
9028 if (rel_hdr
->sh_type
== SHT_REL
)
9030 erel
+= (elf_section_data (output_section
)->rel_count
9031 * bed
->s
->sizeof_rel
);
9032 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9036 irel
[0].r_addend
= addend
;
9037 erel
+= (elf_section_data (output_section
)->rel_count
9038 * bed
->s
->sizeof_rela
);
9039 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9042 ++elf_section_data (output_section
)->rel_count
;
9048 /* Get the output vma of the section pointed to by the sh_link field. */
9051 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9053 Elf_Internal_Shdr
**elf_shdrp
;
9057 s
= p
->u
.indirect
.section
;
9058 elf_shdrp
= elf_elfsections (s
->owner
);
9059 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9060 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9062 The Intel C compiler generates SHT_IA_64_UNWIND with
9063 SHF_LINK_ORDER. But it doesn't set the sh_link or
9064 sh_info fields. Hence we could get the situation
9065 where elfsec is 0. */
9068 const struct elf_backend_data
*bed
9069 = get_elf_backend_data (s
->owner
);
9070 if (bed
->link_order_error_handler
)
9071 bed
->link_order_error_handler
9072 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9077 s
= elf_shdrp
[elfsec
]->bfd_section
;
9078 return s
->output_section
->vma
+ s
->output_offset
;
9083 /* Compare two sections based on the locations of the sections they are
9084 linked to. Used by elf_fixup_link_order. */
9087 compare_link_order (const void * a
, const void * b
)
9092 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9093 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9100 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9101 order as their linked sections. Returns false if this could not be done
9102 because an output section includes both ordered and unordered
9103 sections. Ideally we'd do this in the linker proper. */
9106 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9111 struct bfd_link_order
*p
;
9113 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9115 struct bfd_link_order
**sections
;
9116 asection
*s
, *other_sec
, *linkorder_sec
;
9120 linkorder_sec
= NULL
;
9123 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9125 if (p
->type
== bfd_indirect_link_order
)
9127 s
= p
->u
.indirect
.section
;
9129 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9130 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9131 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9132 && elfsec
< elf_numsections (sub
)
9133 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
9147 if (seen_other
&& seen_linkorder
)
9149 if (other_sec
&& linkorder_sec
)
9150 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9152 linkorder_sec
->owner
, other_sec
,
9155 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9157 bfd_set_error (bfd_error_bad_value
);
9162 if (!seen_linkorder
)
9165 sections
= (struct bfd_link_order
**)
9166 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9169 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9171 sections
[seen_linkorder
++] = p
;
9173 /* Sort the input sections in the order of their linked section. */
9174 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9175 compare_link_order
);
9177 /* Change the offsets of the sections. */
9179 for (n
= 0; n
< seen_linkorder
; n
++)
9181 s
= sections
[n
]->u
.indirect
.section
;
9182 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9183 s
->output_offset
= offset
;
9184 sections
[n
]->offset
= offset
;
9185 offset
+= sections
[n
]->size
;
9192 /* Do the final step of an ELF link. */
9195 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9197 bfd_boolean dynamic
;
9198 bfd_boolean emit_relocs
;
9200 struct elf_final_link_info finfo
;
9201 register asection
*o
;
9202 register struct bfd_link_order
*p
;
9204 bfd_size_type max_contents_size
;
9205 bfd_size_type max_external_reloc_size
;
9206 bfd_size_type max_internal_reloc_count
;
9207 bfd_size_type max_sym_count
;
9208 bfd_size_type max_sym_shndx_count
;
9210 Elf_Internal_Sym elfsym
;
9212 Elf_Internal_Shdr
*symtab_hdr
;
9213 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9214 Elf_Internal_Shdr
*symstrtab_hdr
;
9215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9216 struct elf_outext_info eoinfo
;
9218 size_t relativecount
= 0;
9219 asection
*reldyn
= 0;
9222 if (! is_elf_hash_table (info
->hash
))
9226 abfd
->flags
|= DYNAMIC
;
9228 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9229 dynobj
= elf_hash_table (info
)->dynobj
;
9231 emit_relocs
= (info
->relocatable
9232 || info
->emitrelocations
);
9235 finfo
.output_bfd
= abfd
;
9236 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9237 if (finfo
.symstrtab
== NULL
)
9242 finfo
.dynsym_sec
= NULL
;
9243 finfo
.hash_sec
= NULL
;
9244 finfo
.symver_sec
= NULL
;
9248 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9249 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9250 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9251 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9252 /* Note that it is OK if symver_sec is NULL. */
9255 finfo
.contents
= NULL
;
9256 finfo
.external_relocs
= NULL
;
9257 finfo
.internal_relocs
= NULL
;
9258 finfo
.external_syms
= NULL
;
9259 finfo
.locsym_shndx
= NULL
;
9260 finfo
.internal_syms
= NULL
;
9261 finfo
.indices
= NULL
;
9262 finfo
.sections
= NULL
;
9263 finfo
.symbuf
= NULL
;
9264 finfo
.symshndxbuf
= NULL
;
9265 finfo
.symbuf_count
= 0;
9266 finfo
.shndxbuf_size
= 0;
9268 /* Count up the number of relocations we will output for each output
9269 section, so that we know the sizes of the reloc sections. We
9270 also figure out some maximum sizes. */
9271 max_contents_size
= 0;
9272 max_external_reloc_size
= 0;
9273 max_internal_reloc_count
= 0;
9275 max_sym_shndx_count
= 0;
9277 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9279 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
9282 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9284 unsigned int reloc_count
= 0;
9285 struct bfd_elf_section_data
*esdi
= NULL
;
9286 unsigned int *rel_count1
;
9288 if (p
->type
== bfd_section_reloc_link_order
9289 || p
->type
== bfd_symbol_reloc_link_order
)
9291 else if (p
->type
== bfd_indirect_link_order
)
9295 sec
= p
->u
.indirect
.section
;
9296 esdi
= elf_section_data (sec
);
9298 /* Mark all sections which are to be included in the
9299 link. This will normally be every section. We need
9300 to do this so that we can identify any sections which
9301 the linker has decided to not include. */
9302 sec
->linker_mark
= TRUE
;
9304 if (sec
->flags
& SEC_MERGE
)
9307 if (info
->relocatable
|| info
->emitrelocations
)
9308 reloc_count
= sec
->reloc_count
;
9309 else if (bed
->elf_backend_count_relocs
)
9311 Elf_Internal_Rela
* relocs
;
9313 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
9316 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
9318 if (elf_section_data (o
)->relocs
!= relocs
)
9322 if (sec
->rawsize
> max_contents_size
)
9323 max_contents_size
= sec
->rawsize
;
9324 if (sec
->size
> max_contents_size
)
9325 max_contents_size
= sec
->size
;
9327 /* We are interested in just local symbols, not all
9329 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
9330 && (sec
->owner
->flags
& DYNAMIC
) == 0)
9334 if (elf_bad_symtab (sec
->owner
))
9335 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
9336 / bed
->s
->sizeof_sym
);
9338 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
9340 if (sym_count
> max_sym_count
)
9341 max_sym_count
= sym_count
;
9343 if (sym_count
> max_sym_shndx_count
9344 && elf_symtab_shndx (sec
->owner
) != 0)
9345 max_sym_shndx_count
= sym_count
;
9347 if ((sec
->flags
& SEC_RELOC
) != 0)
9351 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
9352 if (ext_size
> max_external_reloc_size
)
9353 max_external_reloc_size
= ext_size
;
9354 if (sec
->reloc_count
> max_internal_reloc_count
)
9355 max_internal_reloc_count
= sec
->reloc_count
;
9360 if (reloc_count
== 0)
9363 o
->reloc_count
+= reloc_count
;
9365 /* MIPS may have a mix of REL and RELA relocs on sections.
9366 To support this curious ABI we keep reloc counts in
9367 elf_section_data too. We must be careful to add the
9368 relocations from the input section to the right output
9369 count. FIXME: Get rid of one count. We have
9370 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9371 rel_count1
= &esdo
->rel_count
;
9374 bfd_boolean same_size
;
9375 bfd_size_type entsize1
;
9377 entsize1
= esdi
->rel_hdr
.sh_entsize
;
9378 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
9379 || entsize1
== bed
->s
->sizeof_rela
);
9380 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
9383 rel_count1
= &esdo
->rel_count2
;
9385 if (esdi
->rel_hdr2
!= NULL
)
9387 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
9388 unsigned int alt_count
;
9389 unsigned int *rel_count2
;
9391 BFD_ASSERT (entsize2
!= entsize1
9392 && (entsize2
== bed
->s
->sizeof_rel
9393 || entsize2
== bed
->s
->sizeof_rela
));
9395 rel_count2
= &esdo
->rel_count2
;
9397 rel_count2
= &esdo
->rel_count
;
9399 /* The following is probably too simplistic if the
9400 backend counts output relocs unusually. */
9401 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
9402 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
9403 *rel_count2
+= alt_count
;
9404 reloc_count
-= alt_count
;
9407 *rel_count1
+= reloc_count
;
9410 if (o
->reloc_count
> 0)
9411 o
->flags
|= SEC_RELOC
;
9414 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9415 set it (this is probably a bug) and if it is set
9416 assign_section_numbers will create a reloc section. */
9417 o
->flags
&=~ SEC_RELOC
;
9420 /* If the SEC_ALLOC flag is not set, force the section VMA to
9421 zero. This is done in elf_fake_sections as well, but forcing
9422 the VMA to 0 here will ensure that relocs against these
9423 sections are handled correctly. */
9424 if ((o
->flags
& SEC_ALLOC
) == 0
9425 && ! o
->user_set_vma
)
9429 if (! info
->relocatable
&& merged
)
9430 elf_link_hash_traverse (elf_hash_table (info
),
9431 _bfd_elf_link_sec_merge_syms
, abfd
);
9433 /* Figure out the file positions for everything but the symbol table
9434 and the relocs. We set symcount to force assign_section_numbers
9435 to create a symbol table. */
9436 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
9437 BFD_ASSERT (! abfd
->output_has_begun
);
9438 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
9441 /* Set sizes, and assign file positions for reloc sections. */
9442 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9444 if ((o
->flags
& SEC_RELOC
) != 0)
9446 if (!(_bfd_elf_link_size_reloc_section
9447 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
9450 if (elf_section_data (o
)->rel_hdr2
9451 && !(_bfd_elf_link_size_reloc_section
9452 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
9456 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9457 to count upwards while actually outputting the relocations. */
9458 elf_section_data (o
)->rel_count
= 0;
9459 elf_section_data (o
)->rel_count2
= 0;
9462 _bfd_elf_assign_file_positions_for_relocs (abfd
);
9464 /* We have now assigned file positions for all the sections except
9465 .symtab and .strtab. We start the .symtab section at the current
9466 file position, and write directly to it. We build the .strtab
9467 section in memory. */
9468 bfd_get_symcount (abfd
) = 0;
9469 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9470 /* sh_name is set in prep_headers. */
9471 symtab_hdr
->sh_type
= SHT_SYMTAB
;
9472 /* sh_flags, sh_addr and sh_size all start off zero. */
9473 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
9474 /* sh_link is set in assign_section_numbers. */
9475 /* sh_info is set below. */
9476 /* sh_offset is set just below. */
9477 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
9479 off
= elf_tdata (abfd
)->next_file_pos
;
9480 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
9482 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9483 incorrect. We do not yet know the size of the .symtab section.
9484 We correct next_file_pos below, after we do know the size. */
9486 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9487 continuously seeking to the right position in the file. */
9488 if (! info
->keep_memory
|| max_sym_count
< 20)
9489 finfo
.symbuf_size
= 20;
9491 finfo
.symbuf_size
= max_sym_count
;
9492 amt
= finfo
.symbuf_size
;
9493 amt
*= bed
->s
->sizeof_sym
;
9494 finfo
.symbuf
= bfd_malloc (amt
);
9495 if (finfo
.symbuf
== NULL
)
9497 if (elf_numsections (abfd
) > SHN_LORESERVE
)
9499 /* Wild guess at number of output symbols. realloc'd as needed. */
9500 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
9501 finfo
.shndxbuf_size
= amt
;
9502 amt
*= sizeof (Elf_External_Sym_Shndx
);
9503 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
9504 if (finfo
.symshndxbuf
== NULL
)
9508 /* Start writing out the symbol table. The first symbol is always a
9510 if (info
->strip
!= strip_all
9513 elfsym
.st_value
= 0;
9516 elfsym
.st_other
= 0;
9517 elfsym
.st_shndx
= SHN_UNDEF
;
9518 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
9523 /* Output a symbol for each section. We output these even if we are
9524 discarding local symbols, since they are used for relocs. These
9525 symbols have no names. We store the index of each one in the
9526 index field of the section, so that we can find it again when
9527 outputting relocs. */
9528 if (info
->strip
!= strip_all
9532 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9533 elfsym
.st_other
= 0;
9534 elfsym
.st_value
= 0;
9535 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9537 o
= bfd_section_from_elf_index (abfd
, i
);
9540 o
->target_index
= bfd_get_symcount (abfd
);
9541 elfsym
.st_shndx
= i
;
9542 if (!info
->relocatable
)
9543 elfsym
.st_value
= o
->vma
;
9544 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
9547 if (i
== SHN_LORESERVE
- 1)
9548 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
9552 /* Allocate some memory to hold information read in from the input
9554 if (max_contents_size
!= 0)
9556 finfo
.contents
= bfd_malloc (max_contents_size
);
9557 if (finfo
.contents
== NULL
)
9561 if (max_external_reloc_size
!= 0)
9563 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
9564 if (finfo
.external_relocs
== NULL
)
9568 if (max_internal_reloc_count
!= 0)
9570 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9571 amt
*= sizeof (Elf_Internal_Rela
);
9572 finfo
.internal_relocs
= bfd_malloc (amt
);
9573 if (finfo
.internal_relocs
== NULL
)
9577 if (max_sym_count
!= 0)
9579 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
9580 finfo
.external_syms
= bfd_malloc (amt
);
9581 if (finfo
.external_syms
== NULL
)
9584 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
9585 finfo
.internal_syms
= bfd_malloc (amt
);
9586 if (finfo
.internal_syms
== NULL
)
9589 amt
= max_sym_count
* sizeof (long);
9590 finfo
.indices
= bfd_malloc (amt
);
9591 if (finfo
.indices
== NULL
)
9594 amt
= max_sym_count
* sizeof (asection
*);
9595 finfo
.sections
= bfd_malloc (amt
);
9596 if (finfo
.sections
== NULL
)
9600 if (max_sym_shndx_count
!= 0)
9602 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
9603 finfo
.locsym_shndx
= bfd_malloc (amt
);
9604 if (finfo
.locsym_shndx
== NULL
)
9608 if (elf_hash_table (info
)->tls_sec
)
9610 bfd_vma base
, end
= 0;
9613 for (sec
= elf_hash_table (info
)->tls_sec
;
9614 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
9617 bfd_size_type size
= sec
->size
;
9620 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
9622 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
9624 size
= o
->offset
+ o
->size
;
9626 end
= sec
->vma
+ size
;
9628 base
= elf_hash_table (info
)->tls_sec
->vma
;
9629 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
9630 elf_hash_table (info
)->tls_size
= end
- base
;
9633 /* Reorder SHF_LINK_ORDER sections. */
9634 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9636 if (!elf_fixup_link_order (abfd
, o
))
9640 /* Since ELF permits relocations to be against local symbols, we
9641 must have the local symbols available when we do the relocations.
9642 Since we would rather only read the local symbols once, and we
9643 would rather not keep them in memory, we handle all the
9644 relocations for a single input file at the same time.
9646 Unfortunately, there is no way to know the total number of local
9647 symbols until we have seen all of them, and the local symbol
9648 indices precede the global symbol indices. This means that when
9649 we are generating relocatable output, and we see a reloc against
9650 a global symbol, we can not know the symbol index until we have
9651 finished examining all the local symbols to see which ones we are
9652 going to output. To deal with this, we keep the relocations in
9653 memory, and don't output them until the end of the link. This is
9654 an unfortunate waste of memory, but I don't see a good way around
9655 it. Fortunately, it only happens when performing a relocatable
9656 link, which is not the common case. FIXME: If keep_memory is set
9657 we could write the relocs out and then read them again; I don't
9658 know how bad the memory loss will be. */
9660 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9661 sub
->output_has_begun
= FALSE
;
9662 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9664 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9666 if (p
->type
== bfd_indirect_link_order
9667 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
9668 == bfd_target_elf_flavour
)
9669 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
9671 if (! sub
->output_has_begun
)
9673 if (! elf_link_input_bfd (&finfo
, sub
))
9675 sub
->output_has_begun
= TRUE
;
9678 else if (p
->type
== bfd_section_reloc_link_order
9679 || p
->type
== bfd_symbol_reloc_link_order
)
9681 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
9686 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
9692 /* Free symbol buffer if needed. */
9693 if (!info
->reduce_memory_overheads
)
9695 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9696 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9697 && elf_tdata (sub
)->symbuf
)
9699 free (elf_tdata (sub
)->symbuf
);
9700 elf_tdata (sub
)->symbuf
= NULL
;
9704 /* Output any global symbols that got converted to local in a
9705 version script or due to symbol visibility. We do this in a
9706 separate step since ELF requires all local symbols to appear
9707 prior to any global symbols. FIXME: We should only do this if
9708 some global symbols were, in fact, converted to become local.
9709 FIXME: Will this work correctly with the Irix 5 linker? */
9710 eoinfo
.failed
= FALSE
;
9711 eoinfo
.finfo
= &finfo
;
9712 eoinfo
.localsyms
= TRUE
;
9713 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9718 /* If backend needs to output some local symbols not present in the hash
9719 table, do it now. */
9720 if (bed
->elf_backend_output_arch_local_syms
)
9722 typedef bfd_boolean (*out_sym_func
)
9723 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9724 struct elf_link_hash_entry
*);
9726 if (! ((*bed
->elf_backend_output_arch_local_syms
)
9727 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9731 /* That wrote out all the local symbols. Finish up the symbol table
9732 with the global symbols. Even if we want to strip everything we
9733 can, we still need to deal with those global symbols that got
9734 converted to local in a version script. */
9736 /* The sh_info field records the index of the first non local symbol. */
9737 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
9740 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
9742 Elf_Internal_Sym sym
;
9743 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
9744 long last_local
= 0;
9746 /* Write out the section symbols for the output sections. */
9747 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
9753 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9756 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9762 dynindx
= elf_section_data (s
)->dynindx
;
9765 indx
= elf_section_data (s
)->this_idx
;
9766 BFD_ASSERT (indx
> 0);
9767 sym
.st_shndx
= indx
;
9768 if (! check_dynsym (abfd
, &sym
))
9770 sym
.st_value
= s
->vma
;
9771 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
9772 if (last_local
< dynindx
)
9773 last_local
= dynindx
;
9774 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9778 /* Write out the local dynsyms. */
9779 if (elf_hash_table (info
)->dynlocal
)
9781 struct elf_link_local_dynamic_entry
*e
;
9782 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
9787 sym
.st_size
= e
->isym
.st_size
;
9788 sym
.st_other
= e
->isym
.st_other
;
9790 /* Copy the internal symbol as is.
9791 Note that we saved a word of storage and overwrote
9792 the original st_name with the dynstr_index. */
9795 if (e
->isym
.st_shndx
!= SHN_UNDEF
9796 && (e
->isym
.st_shndx
< SHN_LORESERVE
9797 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
9799 s
= bfd_section_from_elf_index (e
->input_bfd
,
9803 elf_section_data (s
->output_section
)->this_idx
;
9804 if (! check_dynsym (abfd
, &sym
))
9806 sym
.st_value
= (s
->output_section
->vma
9808 + e
->isym
.st_value
);
9811 if (last_local
< e
->dynindx
)
9812 last_local
= e
->dynindx
;
9814 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
9815 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9819 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
9823 /* We get the global symbols from the hash table. */
9824 eoinfo
.failed
= FALSE
;
9825 eoinfo
.localsyms
= FALSE
;
9826 eoinfo
.finfo
= &finfo
;
9827 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9832 /* If backend needs to output some symbols not present in the hash
9833 table, do it now. */
9834 if (bed
->elf_backend_output_arch_syms
)
9836 typedef bfd_boolean (*out_sym_func
)
9837 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9838 struct elf_link_hash_entry
*);
9840 if (! ((*bed
->elf_backend_output_arch_syms
)
9841 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9845 /* Flush all symbols to the file. */
9846 if (! elf_link_flush_output_syms (&finfo
, bed
))
9849 /* Now we know the size of the symtab section. */
9850 off
+= symtab_hdr
->sh_size
;
9852 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
9853 if (symtab_shndx_hdr
->sh_name
!= 0)
9855 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
9856 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
9857 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
9858 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
9859 symtab_shndx_hdr
->sh_size
= amt
;
9861 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
9864 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
9865 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
9870 /* Finish up and write out the symbol string table (.strtab)
9872 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
9873 /* sh_name was set in prep_headers. */
9874 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9875 symstrtab_hdr
->sh_flags
= 0;
9876 symstrtab_hdr
->sh_addr
= 0;
9877 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
9878 symstrtab_hdr
->sh_entsize
= 0;
9879 symstrtab_hdr
->sh_link
= 0;
9880 symstrtab_hdr
->sh_info
= 0;
9881 /* sh_offset is set just below. */
9882 symstrtab_hdr
->sh_addralign
= 1;
9884 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
9885 elf_tdata (abfd
)->next_file_pos
= off
;
9887 if (bfd_get_symcount (abfd
) > 0)
9889 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9890 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9894 /* Adjust the relocs to have the correct symbol indices. */
9895 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9897 if ((o
->flags
& SEC_RELOC
) == 0)
9900 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9901 elf_section_data (o
)->rel_count
,
9902 elf_section_data (o
)->rel_hashes
);
9903 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9904 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9905 elf_section_data (o
)->rel_count2
,
9906 (elf_section_data (o
)->rel_hashes
9907 + elf_section_data (o
)->rel_count
));
9909 /* Set the reloc_count field to 0 to prevent write_relocs from
9910 trying to swap the relocs out itself. */
9914 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
9915 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
9917 /* If we are linking against a dynamic object, or generating a
9918 shared library, finish up the dynamic linking information. */
9921 bfd_byte
*dyncon
, *dynconend
;
9923 /* Fix up .dynamic entries. */
9924 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9925 BFD_ASSERT (o
!= NULL
);
9927 dyncon
= o
->contents
;
9928 dynconend
= o
->contents
+ o
->size
;
9929 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9931 Elf_Internal_Dyn dyn
;
9935 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9942 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
9944 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
9946 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
9947 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
9950 dyn
.d_un
.d_val
= relativecount
;
9957 name
= info
->init_function
;
9960 name
= info
->fini_function
;
9963 struct elf_link_hash_entry
*h
;
9965 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
9966 FALSE
, FALSE
, TRUE
);
9968 && (h
->root
.type
== bfd_link_hash_defined
9969 || h
->root
.type
== bfd_link_hash_defweak
))
9971 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
9972 o
= h
->root
.u
.def
.section
;
9973 if (o
->output_section
!= NULL
)
9974 dyn
.d_un
.d_val
+= (o
->output_section
->vma
9975 + o
->output_offset
);
9978 /* The symbol is imported from another shared
9979 library and does not apply to this one. */
9987 case DT_PREINIT_ARRAYSZ
:
9988 name
= ".preinit_array";
9990 case DT_INIT_ARRAYSZ
:
9991 name
= ".init_array";
9993 case DT_FINI_ARRAYSZ
:
9994 name
= ".fini_array";
9996 o
= bfd_get_section_by_name (abfd
, name
);
9999 (*_bfd_error_handler
)
10000 (_("%B: could not find output section %s"), abfd
, name
);
10004 (*_bfd_error_handler
)
10005 (_("warning: %s section has zero size"), name
);
10006 dyn
.d_un
.d_val
= o
->size
;
10009 case DT_PREINIT_ARRAY
:
10010 name
= ".preinit_array";
10012 case DT_INIT_ARRAY
:
10013 name
= ".init_array";
10015 case DT_FINI_ARRAY
:
10016 name
= ".fini_array";
10023 name
= ".gnu.hash";
10032 name
= ".gnu.version_d";
10035 name
= ".gnu.version_r";
10038 name
= ".gnu.version";
10040 o
= bfd_get_section_by_name (abfd
, name
);
10043 (*_bfd_error_handler
)
10044 (_("%B: could not find output section %s"), abfd
, name
);
10047 dyn
.d_un
.d_ptr
= o
->vma
;
10054 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10058 dyn
.d_un
.d_val
= 0;
10059 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10061 Elf_Internal_Shdr
*hdr
;
10063 hdr
= elf_elfsections (abfd
)[i
];
10064 if (hdr
->sh_type
== type
10065 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10067 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10068 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10071 if (dyn
.d_un
.d_val
== 0
10072 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
10073 dyn
.d_un
.d_val
= hdr
->sh_addr
;
10079 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10083 /* If we have created any dynamic sections, then output them. */
10084 if (dynobj
!= NULL
)
10086 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10089 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10090 if (info
->warn_shared_textrel
&& info
->shared
)
10092 bfd_byte
*dyncon
, *dynconend
;
10094 /* Fix up .dynamic entries. */
10095 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10096 BFD_ASSERT (o
!= NULL
);
10098 dyncon
= o
->contents
;
10099 dynconend
= o
->contents
+ o
->size
;
10100 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10102 Elf_Internal_Dyn dyn
;
10104 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10106 if (dyn
.d_tag
== DT_TEXTREL
)
10108 info
->callbacks
->einfo
10109 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10115 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10117 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10119 || o
->output_section
== bfd_abs_section_ptr
)
10121 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10123 /* At this point, we are only interested in sections
10124 created by _bfd_elf_link_create_dynamic_sections. */
10127 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10129 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10131 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10133 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10135 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10137 (file_ptr
) o
->output_offset
,
10143 /* The contents of the .dynstr section are actually in a
10145 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10146 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10147 || ! _bfd_elf_strtab_emit (abfd
,
10148 elf_hash_table (info
)->dynstr
))
10154 if (info
->relocatable
)
10156 bfd_boolean failed
= FALSE
;
10158 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10163 /* If we have optimized stabs strings, output them. */
10164 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10166 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10170 if (info
->eh_frame_hdr
)
10172 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10176 if (finfo
.symstrtab
!= NULL
)
10177 _bfd_stringtab_free (finfo
.symstrtab
);
10178 if (finfo
.contents
!= NULL
)
10179 free (finfo
.contents
);
10180 if (finfo
.external_relocs
!= NULL
)
10181 free (finfo
.external_relocs
);
10182 if (finfo
.internal_relocs
!= NULL
)
10183 free (finfo
.internal_relocs
);
10184 if (finfo
.external_syms
!= NULL
)
10185 free (finfo
.external_syms
);
10186 if (finfo
.locsym_shndx
!= NULL
)
10187 free (finfo
.locsym_shndx
);
10188 if (finfo
.internal_syms
!= NULL
)
10189 free (finfo
.internal_syms
);
10190 if (finfo
.indices
!= NULL
)
10191 free (finfo
.indices
);
10192 if (finfo
.sections
!= NULL
)
10193 free (finfo
.sections
);
10194 if (finfo
.symbuf
!= NULL
)
10195 free (finfo
.symbuf
);
10196 if (finfo
.symshndxbuf
!= NULL
)
10197 free (finfo
.symshndxbuf
);
10198 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10200 if ((o
->flags
& SEC_RELOC
) != 0
10201 && elf_section_data (o
)->rel_hashes
!= NULL
)
10202 free (elf_section_data (o
)->rel_hashes
);
10205 elf_tdata (abfd
)->linker
= TRUE
;
10210 if (finfo
.symstrtab
!= NULL
)
10211 _bfd_stringtab_free (finfo
.symstrtab
);
10212 if (finfo
.contents
!= NULL
)
10213 free (finfo
.contents
);
10214 if (finfo
.external_relocs
!= NULL
)
10215 free (finfo
.external_relocs
);
10216 if (finfo
.internal_relocs
!= NULL
)
10217 free (finfo
.internal_relocs
);
10218 if (finfo
.external_syms
!= NULL
)
10219 free (finfo
.external_syms
);
10220 if (finfo
.locsym_shndx
!= NULL
)
10221 free (finfo
.locsym_shndx
);
10222 if (finfo
.internal_syms
!= NULL
)
10223 free (finfo
.internal_syms
);
10224 if (finfo
.indices
!= NULL
)
10225 free (finfo
.indices
);
10226 if (finfo
.sections
!= NULL
)
10227 free (finfo
.sections
);
10228 if (finfo
.symbuf
!= NULL
)
10229 free (finfo
.symbuf
);
10230 if (finfo
.symshndxbuf
!= NULL
)
10231 free (finfo
.symshndxbuf
);
10232 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10234 if ((o
->flags
& SEC_RELOC
) != 0
10235 && elf_section_data (o
)->rel_hashes
!= NULL
)
10236 free (elf_section_data (o
)->rel_hashes
);
10242 /* Garbage collect unused sections. */
10244 /* Default gc_mark_hook. */
10247 _bfd_elf_gc_mark_hook (asection
*sec
,
10248 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10249 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
10250 struct elf_link_hash_entry
*h
,
10251 Elf_Internal_Sym
*sym
)
10255 switch (h
->root
.type
)
10257 case bfd_link_hash_defined
:
10258 case bfd_link_hash_defweak
:
10259 return h
->root
.u
.def
.section
;
10261 case bfd_link_hash_common
:
10262 return h
->root
.u
.c
.p
->section
;
10269 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
10274 /* The mark phase of garbage collection. For a given section, mark
10275 it and any sections in this section's group, and all the sections
10276 which define symbols to which it refers. */
10279 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
10281 elf_gc_mark_hook_fn gc_mark_hook
)
10285 asection
*group_sec
;
10289 /* Mark all the sections in the group. */
10290 group_sec
= elf_section_data (sec
)->next_in_group
;
10291 if (group_sec
&& !group_sec
->gc_mark
)
10292 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
10295 /* Look through the section relocs. */
10297 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
10298 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
10300 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
10301 Elf_Internal_Shdr
*symtab_hdr
;
10302 struct elf_link_hash_entry
**sym_hashes
;
10305 bfd
*input_bfd
= sec
->owner
;
10306 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
10307 Elf_Internal_Sym
*isym
= NULL
;
10310 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10311 sym_hashes
= elf_sym_hashes (input_bfd
);
10313 /* Read the local symbols. */
10314 if (elf_bad_symtab (input_bfd
))
10316 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10320 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
10322 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10323 if (isym
== NULL
&& nlocsyms
!= 0)
10325 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
10331 /* Read the relocations. */
10332 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
10333 info
->keep_memory
);
10334 if (relstart
== NULL
)
10339 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10341 if (bed
->s
->arch_size
== 32)
10346 for (rel
= relstart
; rel
< relend
; rel
++)
10348 unsigned long r_symndx
;
10350 struct elf_link_hash_entry
*h
;
10352 r_symndx
= rel
->r_info
>> r_sym_shift
;
10356 if (r_symndx
>= nlocsyms
10357 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
10359 h
= sym_hashes
[r_symndx
- extsymoff
];
10360 while (h
->root
.type
== bfd_link_hash_indirect
10361 || h
->root
.type
== bfd_link_hash_warning
)
10362 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10363 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
10367 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
10370 if (rsec
&& !rsec
->gc_mark
)
10372 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
10375 rsec
->gc_mark_from_eh
= 1;
10376 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
10385 if (elf_section_data (sec
)->relocs
!= relstart
)
10388 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
10390 if (! info
->keep_memory
)
10393 symtab_hdr
->contents
= (unsigned char *) isym
;
10400 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10402 struct elf_gc_sweep_symbol_info
10404 struct bfd_link_info
*info
;
10405 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
10410 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
10412 if (h
->root
.type
== bfd_link_hash_warning
)
10413 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10415 if ((h
->root
.type
== bfd_link_hash_defined
10416 || h
->root
.type
== bfd_link_hash_defweak
)
10417 && !h
->root
.u
.def
.section
->gc_mark
10418 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
10420 struct elf_gc_sweep_symbol_info
*inf
= data
;
10421 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
10427 /* The sweep phase of garbage collection. Remove all garbage sections. */
10429 typedef bfd_boolean (*gc_sweep_hook_fn
)
10430 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
10433 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
10436 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10437 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
10438 unsigned long section_sym_count
;
10439 struct elf_gc_sweep_symbol_info sweep_info
;
10441 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10445 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10448 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10450 /* Keep debug and special sections. */
10451 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
10452 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
10458 /* Skip sweeping sections already excluded. */
10459 if (o
->flags
& SEC_EXCLUDE
)
10462 /* Since this is early in the link process, it is simple
10463 to remove a section from the output. */
10464 o
->flags
|= SEC_EXCLUDE
;
10466 if (info
->print_gc_sections
== TRUE
)
10467 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
10469 /* But we also have to update some of the relocation
10470 info we collected before. */
10472 && (o
->flags
& SEC_RELOC
) != 0
10473 && o
->reloc_count
> 0
10474 && !bfd_is_abs_section (o
->output_section
))
10476 Elf_Internal_Rela
*internal_relocs
;
10480 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
10481 info
->keep_memory
);
10482 if (internal_relocs
== NULL
)
10485 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
10487 if (elf_section_data (o
)->relocs
!= internal_relocs
)
10488 free (internal_relocs
);
10496 /* Remove the symbols that were in the swept sections from the dynamic
10497 symbol table. GCFIXME: Anyone know how to get them out of the
10498 static symbol table as well? */
10499 sweep_info
.info
= info
;
10500 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
10501 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
10504 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
10508 /* Propagate collected vtable information. This is called through
10509 elf_link_hash_traverse. */
10512 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
10514 if (h
->root
.type
== bfd_link_hash_warning
)
10515 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10517 /* Those that are not vtables. */
10518 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10521 /* Those vtables that do not have parents, we cannot merge. */
10522 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
10525 /* If we've already been done, exit. */
10526 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
10529 /* Make sure the parent's table is up to date. */
10530 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
10532 if (h
->vtable
->used
== NULL
)
10534 /* None of this table's entries were referenced. Re-use the
10536 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
10537 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
10542 bfd_boolean
*cu
, *pu
;
10544 /* Or the parent's entries into ours. */
10545 cu
= h
->vtable
->used
;
10547 pu
= h
->vtable
->parent
->vtable
->used
;
10550 const struct elf_backend_data
*bed
;
10551 unsigned int log_file_align
;
10553 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
10554 log_file_align
= bed
->s
->log_file_align
;
10555 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
10570 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
10573 bfd_vma hstart
, hend
;
10574 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
10575 const struct elf_backend_data
*bed
;
10576 unsigned int log_file_align
;
10578 if (h
->root
.type
== bfd_link_hash_warning
)
10579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10581 /* Take care of both those symbols that do not describe vtables as
10582 well as those that are not loaded. */
10583 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10586 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
10587 || h
->root
.type
== bfd_link_hash_defweak
);
10589 sec
= h
->root
.u
.def
.section
;
10590 hstart
= h
->root
.u
.def
.value
;
10591 hend
= hstart
+ h
->size
;
10593 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
10595 return *(bfd_boolean
*) okp
= FALSE
;
10596 bed
= get_elf_backend_data (sec
->owner
);
10597 log_file_align
= bed
->s
->log_file_align
;
10599 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10601 for (rel
= relstart
; rel
< relend
; ++rel
)
10602 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
10604 /* If the entry is in use, do nothing. */
10605 if (h
->vtable
->used
10606 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
10608 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
10609 if (h
->vtable
->used
[entry
])
10612 /* Otherwise, kill it. */
10613 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
10619 /* Mark sections containing dynamically referenced symbols. When
10620 building shared libraries, we must assume that any visible symbol is
10624 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
10626 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
10628 if (h
->root
.type
== bfd_link_hash_warning
)
10629 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10631 if ((h
->root
.type
== bfd_link_hash_defined
10632 || h
->root
.type
== bfd_link_hash_defweak
)
10634 || (!info
->executable
10636 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
10637 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
10638 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
10643 /* Do mark and sweep of unused sections. */
10646 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
10648 bfd_boolean ok
= TRUE
;
10650 elf_gc_mark_hook_fn gc_mark_hook
;
10651 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10653 if (!bed
->can_gc_sections
10654 || info
->relocatable
10655 || info
->emitrelocations
10656 || !is_elf_hash_table (info
->hash
))
10658 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
10662 /* Apply transitive closure to the vtable entry usage info. */
10663 elf_link_hash_traverse (elf_hash_table (info
),
10664 elf_gc_propagate_vtable_entries_used
,
10669 /* Kill the vtable relocations that were not used. */
10670 elf_link_hash_traverse (elf_hash_table (info
),
10671 elf_gc_smash_unused_vtentry_relocs
,
10676 /* Mark dynamically referenced symbols. */
10677 if (elf_hash_table (info
)->dynamic_sections_created
)
10678 elf_link_hash_traverse (elf_hash_table (info
),
10679 bed
->gc_mark_dynamic_ref
,
10682 /* Grovel through relocs to find out who stays ... */
10683 gc_mark_hook
= bed
->gc_mark_hook
;
10684 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10688 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10691 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10692 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
10693 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10697 /* Allow the backend to mark additional target specific sections. */
10698 if (bed
->gc_mark_extra_sections
)
10699 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
10701 /* ... again for sections marked from eh_frame. */
10702 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10706 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10709 /* Keep .gcc_except_table.* if the associated .text.* (or the
10710 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10711 marked. This isn't very nice, but the proper solution,
10712 splitting .eh_frame up and using comdat doesn't pan out
10713 easily due to needing special relocs to handle the
10714 difference of two symbols in separate sections.
10715 Don't keep code sections referenced by .eh_frame. */
10716 #define TEXT_PREFIX ".text."
10717 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10718 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10719 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10720 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
10722 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
10725 const char *sec_name
;
10727 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
10729 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
10730 sec_name
= o
->name
+ o_name_prefix_len
;
10731 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
10732 tmp
= strlen (TEXT_PREFIX2
);
10733 if (tmp
> fn_name_prefix_len
)
10734 fn_name_prefix_len
= tmp
;
10736 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
10737 if (fn_name
== NULL
)
10740 /* Try the first prefix. */
10741 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
10742 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10744 /* Try the second prefix. */
10745 if (fn_text
== NULL
)
10747 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
10748 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10752 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
10756 /* If not using specially named exception table section,
10757 then keep whatever we are using. */
10758 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10763 /* ... and mark SEC_EXCLUDE for those that go. */
10764 return elf_gc_sweep (abfd
, info
);
10767 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10770 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
10772 struct elf_link_hash_entry
*h
,
10775 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
10776 struct elf_link_hash_entry
**search
, *child
;
10777 bfd_size_type extsymcount
;
10778 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10780 /* The sh_info field of the symtab header tells us where the
10781 external symbols start. We don't care about the local symbols at
10783 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
10784 if (!elf_bad_symtab (abfd
))
10785 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
10787 sym_hashes
= elf_sym_hashes (abfd
);
10788 sym_hashes_end
= sym_hashes
+ extsymcount
;
10790 /* Hunt down the child symbol, which is in this section at the same
10791 offset as the relocation. */
10792 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
10794 if ((child
= *search
) != NULL
10795 && (child
->root
.type
== bfd_link_hash_defined
10796 || child
->root
.type
== bfd_link_hash_defweak
)
10797 && child
->root
.u
.def
.section
== sec
10798 && child
->root
.u
.def
.value
== offset
)
10802 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
10803 abfd
, sec
, (unsigned long) offset
);
10804 bfd_set_error (bfd_error_invalid_operation
);
10808 if (!child
->vtable
)
10810 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
10811 if (!child
->vtable
)
10816 /* This *should* only be the absolute section. It could potentially
10817 be that someone has defined a non-global vtable though, which
10818 would be bad. It isn't worth paging in the local symbols to be
10819 sure though; that case should simply be handled by the assembler. */
10821 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
10824 child
->vtable
->parent
= h
;
10829 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10832 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
10833 asection
*sec ATTRIBUTE_UNUSED
,
10834 struct elf_link_hash_entry
*h
,
10837 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10838 unsigned int log_file_align
= bed
->s
->log_file_align
;
10842 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
10847 if (addend
>= h
->vtable
->size
)
10849 size_t size
, bytes
, file_align
;
10850 bfd_boolean
*ptr
= h
->vtable
->used
;
10852 /* While the symbol is undefined, we have to be prepared to handle
10854 file_align
= 1 << log_file_align
;
10855 if (h
->root
.type
== bfd_link_hash_undefined
)
10856 size
= addend
+ file_align
;
10860 if (addend
>= size
)
10862 /* Oops! We've got a reference past the defined end of
10863 the table. This is probably a bug -- shall we warn? */
10864 size
= addend
+ file_align
;
10867 size
= (size
+ file_align
- 1) & -file_align
;
10869 /* Allocate one extra entry for use as a "done" flag for the
10870 consolidation pass. */
10871 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
10875 ptr
= bfd_realloc (ptr
- 1, bytes
);
10881 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
10882 * sizeof (bfd_boolean
));
10883 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
10887 ptr
= bfd_zmalloc (bytes
);
10892 /* And arrange for that done flag to be at index -1. */
10893 h
->vtable
->used
= ptr
+ 1;
10894 h
->vtable
->size
= size
;
10897 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
10902 struct alloc_got_off_arg
{
10904 unsigned int got_elt_size
;
10907 /* We need a special top-level link routine to convert got reference counts
10908 to real got offsets. */
10911 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
10913 struct alloc_got_off_arg
*gofarg
= arg
;
10915 if (h
->root
.type
== bfd_link_hash_warning
)
10916 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10918 if (h
->got
.refcount
> 0)
10920 h
->got
.offset
= gofarg
->gotoff
;
10921 gofarg
->gotoff
+= gofarg
->got_elt_size
;
10924 h
->got
.offset
= (bfd_vma
) -1;
10929 /* And an accompanying bit to work out final got entry offsets once
10930 we're done. Should be called from final_link. */
10933 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
10934 struct bfd_link_info
*info
)
10937 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10939 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
10940 struct alloc_got_off_arg gofarg
;
10942 if (! is_elf_hash_table (info
->hash
))
10945 /* The GOT offset is relative to the .got section, but the GOT header is
10946 put into the .got.plt section, if the backend uses it. */
10947 if (bed
->want_got_plt
)
10950 gotoff
= bed
->got_header_size
;
10952 /* Do the local .got entries first. */
10953 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
10955 bfd_signed_vma
*local_got
;
10956 bfd_size_type j
, locsymcount
;
10957 Elf_Internal_Shdr
*symtab_hdr
;
10959 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
10962 local_got
= elf_local_got_refcounts (i
);
10966 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
10967 if (elf_bad_symtab (i
))
10968 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10970 locsymcount
= symtab_hdr
->sh_info
;
10972 for (j
= 0; j
< locsymcount
; ++j
)
10974 if (local_got
[j
] > 0)
10976 local_got
[j
] = gotoff
;
10977 gotoff
+= got_elt_size
;
10980 local_got
[j
] = (bfd_vma
) -1;
10984 /* Then the global .got entries. .plt refcounts are handled by
10985 adjust_dynamic_symbol */
10986 gofarg
.gotoff
= gotoff
;
10987 gofarg
.got_elt_size
= got_elt_size
;
10988 elf_link_hash_traverse (elf_hash_table (info
),
10989 elf_gc_allocate_got_offsets
,
10994 /* Many folk need no more in the way of final link than this, once
10995 got entry reference counting is enabled. */
10998 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11000 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11003 /* Invoke the regular ELF backend linker to do all the work. */
11004 return bfd_elf_final_link (abfd
, info
);
11008 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11010 struct elf_reloc_cookie
*rcookie
= cookie
;
11012 if (rcookie
->bad_symtab
)
11013 rcookie
->rel
= rcookie
->rels
;
11015 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11017 unsigned long r_symndx
;
11019 if (! rcookie
->bad_symtab
)
11020 if (rcookie
->rel
->r_offset
> offset
)
11022 if (rcookie
->rel
->r_offset
!= offset
)
11025 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11026 if (r_symndx
== SHN_UNDEF
)
11029 if (r_symndx
>= rcookie
->locsymcount
11030 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11032 struct elf_link_hash_entry
*h
;
11034 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11036 while (h
->root
.type
== bfd_link_hash_indirect
11037 || h
->root
.type
== bfd_link_hash_warning
)
11038 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11040 if ((h
->root
.type
== bfd_link_hash_defined
11041 || h
->root
.type
== bfd_link_hash_defweak
)
11042 && elf_discarded_section (h
->root
.u
.def
.section
))
11049 /* It's not a relocation against a global symbol,
11050 but it could be a relocation against a local
11051 symbol for a discarded section. */
11053 Elf_Internal_Sym
*isym
;
11055 /* Need to: get the symbol; get the section. */
11056 isym
= &rcookie
->locsyms
[r_symndx
];
11057 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
11059 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11060 if (isec
!= NULL
&& elf_discarded_section (isec
))
11069 /* Discard unneeded references to discarded sections.
11070 Returns TRUE if any section's size was changed. */
11071 /* This function assumes that the relocations are in sorted order,
11072 which is true for all known assemblers. */
11075 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11077 struct elf_reloc_cookie cookie
;
11078 asection
*stab
, *eh
;
11079 Elf_Internal_Shdr
*symtab_hdr
;
11080 const struct elf_backend_data
*bed
;
11082 unsigned int count
;
11083 bfd_boolean ret
= FALSE
;
11085 if (info
->traditional_format
11086 || !is_elf_hash_table (info
->hash
))
11089 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11091 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11094 bed
= get_elf_backend_data (abfd
);
11096 if ((abfd
->flags
& DYNAMIC
) != 0)
11100 if (!info
->relocatable
)
11102 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
11105 || bfd_is_abs_section (eh
->output_section
)))
11109 stab
= bfd_get_section_by_name (abfd
, ".stab");
11111 && (stab
->size
== 0
11112 || bfd_is_abs_section (stab
->output_section
)
11113 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
11118 && bed
->elf_backend_discard_info
== NULL
)
11121 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11122 cookie
.abfd
= abfd
;
11123 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
11124 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
11125 if (cookie
.bad_symtab
)
11127 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11128 cookie
.extsymoff
= 0;
11132 cookie
.locsymcount
= symtab_hdr
->sh_info
;
11133 cookie
.extsymoff
= symtab_hdr
->sh_info
;
11136 if (bed
->s
->arch_size
== 32)
11137 cookie
.r_sym_shift
= 8;
11139 cookie
.r_sym_shift
= 32;
11141 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11142 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
11144 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11145 cookie
.locsymcount
, 0,
11147 if (cookie
.locsyms
== NULL
)
11153 cookie
.rels
= NULL
;
11154 count
= stab
->reloc_count
;
11156 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
11157 info
->keep_memory
);
11158 if (cookie
.rels
!= NULL
)
11160 cookie
.rel
= cookie
.rels
;
11161 cookie
.relend
= cookie
.rels
;
11162 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11163 if (_bfd_discard_section_stabs (abfd
, stab
,
11164 elf_section_data (stab
)->sec_info
,
11165 bfd_elf_reloc_symbol_deleted_p
,
11168 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
11169 free (cookie
.rels
);
11175 cookie
.rels
= NULL
;
11176 count
= eh
->reloc_count
;
11178 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
11179 info
->keep_memory
);
11180 cookie
.rel
= cookie
.rels
;
11181 cookie
.relend
= cookie
.rels
;
11182 if (cookie
.rels
!= NULL
)
11183 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11185 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11186 bfd_elf_reloc_symbol_deleted_p
,
11190 if (cookie
.rels
!= NULL
11191 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
11192 free (cookie
.rels
);
11195 if (bed
->elf_backend_discard_info
!= NULL
11196 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11199 if (cookie
.locsyms
!= NULL
11200 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
11202 if (! info
->keep_memory
)
11203 free (cookie
.locsyms
);
11205 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
11209 if (info
->eh_frame_hdr
11210 && !info
->relocatable
11211 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
11218 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
11219 struct bfd_link_info
*info
)
11222 const char *name
, *p
;
11223 struct bfd_section_already_linked
*l
;
11224 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
11226 if (sec
->output_section
== bfd_abs_section_ptr
)
11229 flags
= sec
->flags
;
11231 /* Return if it isn't a linkonce section. A comdat group section
11232 also has SEC_LINK_ONCE set. */
11233 if ((flags
& SEC_LINK_ONCE
) == 0)
11236 /* Don't put group member sections on our list of already linked
11237 sections. They are handled as a group via their group section. */
11238 if (elf_sec_group (sec
) != NULL
)
11241 /* FIXME: When doing a relocatable link, we may have trouble
11242 copying relocations in other sections that refer to local symbols
11243 in the section being discarded. Those relocations will have to
11244 be converted somehow; as of this writing I'm not sure that any of
11245 the backends handle that correctly.
11247 It is tempting to instead not discard link once sections when
11248 doing a relocatable link (technically, they should be discarded
11249 whenever we are building constructors). However, that fails,
11250 because the linker winds up combining all the link once sections
11251 into a single large link once section, which defeats the purpose
11252 of having link once sections in the first place.
11254 Also, not merging link once sections in a relocatable link
11255 causes trouble for MIPS ELF, which relies on link once semantics
11256 to handle the .reginfo section correctly. */
11258 name
= bfd_get_section_name (abfd
, sec
);
11260 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
11261 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
11266 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
11268 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11270 /* We may have 2 different types of sections on the list: group
11271 sections and linkonce sections. Match like sections. */
11272 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
11273 && strcmp (name
, l
->sec
->name
) == 0
11274 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
11276 /* The section has already been linked. See if we should
11277 issue a warning. */
11278 switch (flags
& SEC_LINK_DUPLICATES
)
11283 case SEC_LINK_DUPLICATES_DISCARD
:
11286 case SEC_LINK_DUPLICATES_ONE_ONLY
:
11287 (*_bfd_error_handler
)
11288 (_("%B: ignoring duplicate section `%A'"),
11292 case SEC_LINK_DUPLICATES_SAME_SIZE
:
11293 if (sec
->size
!= l
->sec
->size
)
11294 (*_bfd_error_handler
)
11295 (_("%B: duplicate section `%A' has different size"),
11299 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
11300 if (sec
->size
!= l
->sec
->size
)
11301 (*_bfd_error_handler
)
11302 (_("%B: duplicate section `%A' has different size"),
11304 else if (sec
->size
!= 0)
11306 bfd_byte
*sec_contents
, *l_sec_contents
;
11308 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
11309 (*_bfd_error_handler
)
11310 (_("%B: warning: could not read contents of section `%A'"),
11312 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
11314 (*_bfd_error_handler
)
11315 (_("%B: warning: could not read contents of section `%A'"),
11316 l
->sec
->owner
, l
->sec
);
11317 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
11318 (*_bfd_error_handler
)
11319 (_("%B: warning: duplicate section `%A' has different contents"),
11323 free (sec_contents
);
11324 if (l_sec_contents
)
11325 free (l_sec_contents
);
11330 /* Set the output_section field so that lang_add_section
11331 does not create a lang_input_section structure for this
11332 section. Since there might be a symbol in the section
11333 being discarded, we must retain a pointer to the section
11334 which we are really going to use. */
11335 sec
->output_section
= bfd_abs_section_ptr
;
11336 sec
->kept_section
= l
->sec
;
11338 if (flags
& SEC_GROUP
)
11340 asection
*first
= elf_next_in_group (sec
);
11341 asection
*s
= first
;
11345 s
->output_section
= bfd_abs_section_ptr
;
11346 /* Record which group discards it. */
11347 s
->kept_section
= l
->sec
;
11348 s
= elf_next_in_group (s
);
11349 /* These lists are circular. */
11359 /* A single member comdat group section may be discarded by a
11360 linkonce section and vice versa. */
11362 if ((flags
& SEC_GROUP
) != 0)
11364 asection
*first
= elf_next_in_group (sec
);
11366 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
11367 /* Check this single member group against linkonce sections. */
11368 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11369 if ((l
->sec
->flags
& SEC_GROUP
) == 0
11370 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
11371 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
11373 first
->output_section
= bfd_abs_section_ptr
;
11374 first
->kept_section
= l
->sec
;
11375 sec
->output_section
= bfd_abs_section_ptr
;
11380 /* Check this linkonce section against single member groups. */
11381 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11382 if (l
->sec
->flags
& SEC_GROUP
)
11384 asection
*first
= elf_next_in_group (l
->sec
);
11387 && elf_next_in_group (first
) == first
11388 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
11390 sec
->output_section
= bfd_abs_section_ptr
;
11391 sec
->kept_section
= first
;
11396 /* This is the first section with this name. Record it. */
11397 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
11401 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
11403 return sym
->st_shndx
== SHN_COMMON
;
11407 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
11413 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
11415 return bfd_com_section_ptr
;