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 /* When we try to create a default indirect symbol from the dynamic
951 definition with the default version, we skip it if its type and
952 the type of existing regular definition mismatch. We only do it
953 if the existing regular definition won't be dynamic. */
954 if (pold_alignment
== NULL
956 && !info
->export_dynamic
961 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
962 && ELF_ST_TYPE (sym
->st_info
) != h
->type
963 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
964 && h
->type
!= STT_NOTYPE
)
970 /* Check TLS symbol. We don't check undefined symbol introduced by
972 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
973 && ELF_ST_TYPE (sym
->st_info
) != h
->type
977 bfd_boolean ntdef
, tdef
;
978 asection
*ntsec
, *tsec
;
980 if (h
->type
== STT_TLS
)
1000 (*_bfd_error_handler
)
1001 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1002 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1003 else if (!tdef
&& !ntdef
)
1004 (*_bfd_error_handler
)
1005 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1006 tbfd
, ntbfd
, h
->root
.root
.string
);
1008 (*_bfd_error_handler
)
1009 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1010 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1012 (*_bfd_error_handler
)
1013 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1014 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1016 bfd_set_error (bfd_error_bad_value
);
1020 /* We need to remember if a symbol has a definition in a dynamic
1021 object or is weak in all dynamic objects. Internal and hidden
1022 visibility will make it unavailable to dynamic objects. */
1023 if (newdyn
&& !h
->dynamic_def
)
1025 if (!bfd_is_und_section (sec
))
1029 /* Check if this symbol is weak in all dynamic objects. If it
1030 is the first time we see it in a dynamic object, we mark
1031 if it is weak. Otherwise, we clear it. */
1032 if (!h
->ref_dynamic
)
1034 if (bind
== STB_WEAK
)
1035 h
->dynamic_weak
= 1;
1037 else if (bind
!= STB_WEAK
)
1038 h
->dynamic_weak
= 0;
1042 /* If the old symbol has non-default visibility, we ignore the new
1043 definition from a dynamic object. */
1045 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1046 && !bfd_is_und_section (sec
))
1049 /* Make sure this symbol is dynamic. */
1051 /* A protected symbol has external availability. Make sure it is
1052 recorded as dynamic.
1054 FIXME: Should we check type and size for protected symbol? */
1055 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1056 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1061 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1064 /* If the new symbol with non-default visibility comes from a
1065 relocatable file and the old definition comes from a dynamic
1066 object, we remove the old definition. */
1067 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1069 /* Handle the case where the old dynamic definition is
1070 default versioned. We need to copy the symbol info from
1071 the symbol with default version to the normal one if it
1072 was referenced before. */
1075 const struct elf_backend_data
*bed
1076 = get_elf_backend_data (abfd
);
1077 struct elf_link_hash_entry
*vh
= *sym_hash
;
1078 vh
->root
.type
= h
->root
.type
;
1079 h
->root
.type
= bfd_link_hash_indirect
;
1080 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1081 /* Protected symbols will override the dynamic definition
1082 with default version. */
1083 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1085 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1086 vh
->dynamic_def
= 1;
1087 vh
->ref_dynamic
= 1;
1091 h
->root
.type
= vh
->root
.type
;
1092 vh
->ref_dynamic
= 0;
1093 /* We have to hide it here since it was made dynamic
1094 global with extra bits when the symbol info was
1095 copied from the old dynamic definition. */
1096 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1104 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1105 && bfd_is_und_section (sec
))
1107 /* If the new symbol is undefined and the old symbol was
1108 also undefined before, we need to make sure
1109 _bfd_generic_link_add_one_symbol doesn't mess
1110 up the linker hash table undefs list. Since the old
1111 definition came from a dynamic object, it is still on the
1113 h
->root
.type
= bfd_link_hash_undefined
;
1114 h
->root
.u
.undef
.abfd
= abfd
;
1118 h
->root
.type
= bfd_link_hash_new
;
1119 h
->root
.u
.undef
.abfd
= NULL
;
1128 /* FIXME: Should we check type and size for protected symbol? */
1134 /* Differentiate strong and weak symbols. */
1135 newweak
= bind
== STB_WEAK
;
1136 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1137 || h
->root
.type
== bfd_link_hash_undefweak
);
1139 /* If a new weak symbol definition comes from a regular file and the
1140 old symbol comes from a dynamic library, we treat the new one as
1141 strong. Similarly, an old weak symbol definition from a regular
1142 file is treated as strong when the new symbol comes from a dynamic
1143 library. Further, an old weak symbol from a dynamic library is
1144 treated as strong if the new symbol is from a dynamic library.
1145 This reflects the way glibc's ld.so works.
1147 Do this before setting *type_change_ok or *size_change_ok so that
1148 we warn properly when dynamic library symbols are overridden. */
1150 if (newdef
&& !newdyn
&& olddyn
)
1152 if (olddef
&& newdyn
)
1155 /* It's OK to change the type if either the existing symbol or the
1156 new symbol is weak. A type change is also OK if the old symbol
1157 is undefined and the new symbol is defined. */
1162 && h
->root
.type
== bfd_link_hash_undefined
))
1163 *type_change_ok
= TRUE
;
1165 /* It's OK to change the size if either the existing symbol or the
1166 new symbol is weak, or if the old symbol is undefined. */
1169 || h
->root
.type
== bfd_link_hash_undefined
)
1170 *size_change_ok
= TRUE
;
1172 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1173 symbol, respectively, appears to be a common symbol in a dynamic
1174 object. If a symbol appears in an uninitialized section, and is
1175 not weak, and is not a function, then it may be a common symbol
1176 which was resolved when the dynamic object was created. We want
1177 to treat such symbols specially, because they raise special
1178 considerations when setting the symbol size: if the symbol
1179 appears as a common symbol in a regular object, and the size in
1180 the regular object is larger, we must make sure that we use the
1181 larger size. This problematic case can always be avoided in C,
1182 but it must be handled correctly when using Fortran shared
1185 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1186 likewise for OLDDYNCOMMON and OLDDEF.
1188 Note that this test is just a heuristic, and that it is quite
1189 possible to have an uninitialized symbol in a shared object which
1190 is really a definition, rather than a common symbol. This could
1191 lead to some minor confusion when the symbol really is a common
1192 symbol in some regular object. However, I think it will be
1198 && (sec
->flags
& SEC_ALLOC
) != 0
1199 && (sec
->flags
& SEC_LOAD
) == 0
1201 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1202 newdyncommon
= TRUE
;
1204 newdyncommon
= FALSE
;
1208 && h
->root
.type
== bfd_link_hash_defined
1210 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1211 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1213 && h
->type
!= STT_FUNC
)
1214 olddyncommon
= TRUE
;
1216 olddyncommon
= FALSE
;
1218 /* We now know everything about the old and new symbols. We ask the
1219 backend to check if we can merge them. */
1220 bed
= get_elf_backend_data (abfd
);
1221 if (bed
->merge_symbol
1222 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1223 pold_alignment
, skip
, override
,
1224 type_change_ok
, size_change_ok
,
1225 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1227 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1231 /* If both the old and the new symbols look like common symbols in a
1232 dynamic object, set the size of the symbol to the larger of the
1237 && sym
->st_size
!= h
->size
)
1239 /* Since we think we have two common symbols, issue a multiple
1240 common warning if desired. Note that we only warn if the
1241 size is different. If the size is the same, we simply let
1242 the old symbol override the new one as normally happens with
1243 symbols defined in dynamic objects. */
1245 if (! ((*info
->callbacks
->multiple_common
)
1246 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1247 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1250 if (sym
->st_size
> h
->size
)
1251 h
->size
= sym
->st_size
;
1253 *size_change_ok
= TRUE
;
1256 /* If we are looking at a dynamic object, and we have found a
1257 definition, we need to see if the symbol was already defined by
1258 some other object. If so, we want to use the existing
1259 definition, and we do not want to report a multiple symbol
1260 definition error; we do this by clobbering *PSEC to be
1261 bfd_und_section_ptr.
1263 We treat a common symbol as a definition if the symbol in the
1264 shared library is a function, since common symbols always
1265 represent variables; this can cause confusion in principle, but
1266 any such confusion would seem to indicate an erroneous program or
1267 shared library. We also permit a common symbol in a regular
1268 object to override a weak symbol in a shared object. */
1273 || (h
->root
.type
== bfd_link_hash_common
1275 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1279 newdyncommon
= FALSE
;
1281 *psec
= sec
= bfd_und_section_ptr
;
1282 *size_change_ok
= TRUE
;
1284 /* If we get here when the old symbol is a common symbol, then
1285 we are explicitly letting it override a weak symbol or
1286 function in a dynamic object, and we don't want to warn about
1287 a type change. If the old symbol is a defined symbol, a type
1288 change warning may still be appropriate. */
1290 if (h
->root
.type
== bfd_link_hash_common
)
1291 *type_change_ok
= TRUE
;
1294 /* Handle the special case of an old common symbol merging with a
1295 new symbol which looks like a common symbol in a shared object.
1296 We change *PSEC and *PVALUE to make the new symbol look like a
1297 common symbol, and let _bfd_generic_link_add_one_symbol do the
1301 && h
->root
.type
== bfd_link_hash_common
)
1305 newdyncommon
= FALSE
;
1306 *pvalue
= sym
->st_size
;
1307 *psec
= sec
= bed
->common_section (oldsec
);
1308 *size_change_ok
= TRUE
;
1311 /* Skip weak definitions of symbols that are already defined. */
1312 if (newdef
&& olddef
&& newweak
)
1315 /* If the old symbol is from a dynamic object, and the new symbol is
1316 a definition which is not from a dynamic object, then the new
1317 symbol overrides the old symbol. Symbols from regular files
1318 always take precedence over symbols from dynamic objects, even if
1319 they are defined after the dynamic object in the link.
1321 As above, we again permit a common symbol in a regular object to
1322 override a definition in a shared object if the shared object
1323 symbol is a function or is weak. */
1328 || (bfd_is_com_section (sec
)
1330 || h
->type
== STT_FUNC
)))
1335 /* Change the hash table entry to undefined, and let
1336 _bfd_generic_link_add_one_symbol do the right thing with the
1339 h
->root
.type
= bfd_link_hash_undefined
;
1340 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1341 *size_change_ok
= TRUE
;
1344 olddyncommon
= FALSE
;
1346 /* We again permit a type change when a common symbol may be
1347 overriding a function. */
1349 if (bfd_is_com_section (sec
))
1350 *type_change_ok
= TRUE
;
1352 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1355 /* This union may have been set to be non-NULL when this symbol
1356 was seen in a dynamic object. We must force the union to be
1357 NULL, so that it is correct for a regular symbol. */
1358 h
->verinfo
.vertree
= NULL
;
1361 /* Handle the special case of a new common symbol merging with an
1362 old symbol that looks like it might be a common symbol defined in
1363 a shared object. Note that we have already handled the case in
1364 which a new common symbol should simply override the definition
1365 in the shared library. */
1368 && bfd_is_com_section (sec
)
1371 /* It would be best if we could set the hash table entry to a
1372 common symbol, but we don't know what to use for the section
1373 or the alignment. */
1374 if (! ((*info
->callbacks
->multiple_common
)
1375 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1376 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1379 /* If the presumed common symbol in the dynamic object is
1380 larger, pretend that the new symbol has its size. */
1382 if (h
->size
> *pvalue
)
1385 /* We need to remember the alignment required by the symbol
1386 in the dynamic object. */
1387 BFD_ASSERT (pold_alignment
);
1388 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1391 olddyncommon
= FALSE
;
1393 h
->root
.type
= bfd_link_hash_undefined
;
1394 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1396 *size_change_ok
= TRUE
;
1397 *type_change_ok
= TRUE
;
1399 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1402 h
->verinfo
.vertree
= NULL
;
1407 /* Handle the case where we had a versioned symbol in a dynamic
1408 library and now find a definition in a normal object. In this
1409 case, we make the versioned symbol point to the normal one. */
1410 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1411 flip
->root
.type
= h
->root
.type
;
1412 h
->root
.type
= bfd_link_hash_indirect
;
1413 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1414 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1415 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1419 flip
->ref_dynamic
= 1;
1426 /* This function is called to create an indirect symbol from the
1427 default for the symbol with the default version if needed. The
1428 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1429 set DYNSYM if the new indirect symbol is dynamic. */
1432 _bfd_elf_add_default_symbol (bfd
*abfd
,
1433 struct bfd_link_info
*info
,
1434 struct elf_link_hash_entry
*h
,
1436 Elf_Internal_Sym
*sym
,
1439 bfd_boolean
*dynsym
,
1440 bfd_boolean override
)
1442 bfd_boolean type_change_ok
;
1443 bfd_boolean size_change_ok
;
1446 struct elf_link_hash_entry
*hi
;
1447 struct bfd_link_hash_entry
*bh
;
1448 const struct elf_backend_data
*bed
;
1449 bfd_boolean collect
;
1450 bfd_boolean dynamic
;
1452 size_t len
, shortlen
;
1455 /* If this symbol has a version, and it is the default version, we
1456 create an indirect symbol from the default name to the fully
1457 decorated name. This will cause external references which do not
1458 specify a version to be bound to this version of the symbol. */
1459 p
= strchr (name
, ELF_VER_CHR
);
1460 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1465 /* We are overridden by an old definition. We need to check if we
1466 need to create the indirect symbol from the default name. */
1467 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1469 BFD_ASSERT (hi
!= NULL
);
1472 while (hi
->root
.type
== bfd_link_hash_indirect
1473 || hi
->root
.type
== bfd_link_hash_warning
)
1475 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1481 bed
= get_elf_backend_data (abfd
);
1482 collect
= bed
->collect
;
1483 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1485 shortlen
= p
- name
;
1486 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1487 if (shortname
== NULL
)
1489 memcpy (shortname
, name
, shortlen
);
1490 shortname
[shortlen
] = '\0';
1492 /* We are going to create a new symbol. Merge it with any existing
1493 symbol with this name. For the purposes of the merge, act as
1494 though we were defining the symbol we just defined, although we
1495 actually going to define an indirect symbol. */
1496 type_change_ok
= FALSE
;
1497 size_change_ok
= FALSE
;
1499 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1500 NULL
, &hi
, &skip
, &override
,
1501 &type_change_ok
, &size_change_ok
))
1510 if (! (_bfd_generic_link_add_one_symbol
1511 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1512 0, name
, FALSE
, collect
, &bh
)))
1514 hi
= (struct elf_link_hash_entry
*) bh
;
1518 /* In this case the symbol named SHORTNAME is overriding the
1519 indirect symbol we want to add. We were planning on making
1520 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1521 is the name without a version. NAME is the fully versioned
1522 name, and it is the default version.
1524 Overriding means that we already saw a definition for the
1525 symbol SHORTNAME in a regular object, and it is overriding
1526 the symbol defined in the dynamic object.
1528 When this happens, we actually want to change NAME, the
1529 symbol we just added, to refer to SHORTNAME. This will cause
1530 references to NAME in the shared object to become references
1531 to SHORTNAME in the regular object. This is what we expect
1532 when we override a function in a shared object: that the
1533 references in the shared object will be mapped to the
1534 definition in the regular object. */
1536 while (hi
->root
.type
== bfd_link_hash_indirect
1537 || hi
->root
.type
== bfd_link_hash_warning
)
1538 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1540 h
->root
.type
= bfd_link_hash_indirect
;
1541 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1545 hi
->ref_dynamic
= 1;
1549 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1554 /* Now set HI to H, so that the following code will set the
1555 other fields correctly. */
1559 /* If there is a duplicate definition somewhere, then HI may not
1560 point to an indirect symbol. We will have reported an error to
1561 the user in that case. */
1563 if (hi
->root
.type
== bfd_link_hash_indirect
)
1565 struct elf_link_hash_entry
*ht
;
1567 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1568 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1570 /* See if the new flags lead us to realize that the symbol must
1582 if (hi
->ref_regular
)
1588 /* We also need to define an indirection from the nondefault version
1592 len
= strlen (name
);
1593 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1594 if (shortname
== NULL
)
1596 memcpy (shortname
, name
, shortlen
);
1597 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1599 /* Once again, merge with any existing symbol. */
1600 type_change_ok
= FALSE
;
1601 size_change_ok
= FALSE
;
1603 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1604 NULL
, &hi
, &skip
, &override
,
1605 &type_change_ok
, &size_change_ok
))
1613 /* Here SHORTNAME is a versioned name, so we don't expect to see
1614 the type of override we do in the case above unless it is
1615 overridden by a versioned definition. */
1616 if (hi
->root
.type
!= bfd_link_hash_defined
1617 && hi
->root
.type
!= bfd_link_hash_defweak
)
1618 (*_bfd_error_handler
)
1619 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1625 if (! (_bfd_generic_link_add_one_symbol
1626 (info
, abfd
, shortname
, BSF_INDIRECT
,
1627 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1629 hi
= (struct elf_link_hash_entry
*) bh
;
1631 /* If there is a duplicate definition somewhere, then HI may not
1632 point to an indirect symbol. We will have reported an error
1633 to the user in that case. */
1635 if (hi
->root
.type
== bfd_link_hash_indirect
)
1637 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1639 /* See if the new flags lead us to realize that the symbol
1651 if (hi
->ref_regular
)
1661 /* This routine is used to export all defined symbols into the dynamic
1662 symbol table. It is called via elf_link_hash_traverse. */
1665 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1667 struct elf_info_failed
*eif
= data
;
1669 /* Ignore this if we won't export it. */
1670 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1673 /* Ignore indirect symbols. These are added by the versioning code. */
1674 if (h
->root
.type
== bfd_link_hash_indirect
)
1677 if (h
->root
.type
== bfd_link_hash_warning
)
1678 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1680 if (h
->dynindx
== -1
1684 struct bfd_elf_version_tree
*t
;
1685 struct bfd_elf_version_expr
*d
;
1687 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1689 if (t
->globals
.list
!= NULL
)
1691 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1696 if (t
->locals
.list
!= NULL
)
1698 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1707 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1718 /* Look through the symbols which are defined in other shared
1719 libraries and referenced here. Update the list of version
1720 dependencies. This will be put into the .gnu.version_r section.
1721 This function is called via elf_link_hash_traverse. */
1724 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1727 struct elf_find_verdep_info
*rinfo
= data
;
1728 Elf_Internal_Verneed
*t
;
1729 Elf_Internal_Vernaux
*a
;
1732 if (h
->root
.type
== bfd_link_hash_warning
)
1733 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1735 /* We only care about symbols defined in shared objects with version
1740 || h
->verinfo
.verdef
== NULL
)
1743 /* See if we already know about this version. */
1744 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1746 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1749 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1750 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1756 /* This is a new version. Add it to tree we are building. */
1761 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1764 rinfo
->failed
= TRUE
;
1768 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1769 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1770 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1774 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1776 /* Note that we are copying a string pointer here, and testing it
1777 above. If bfd_elf_string_from_elf_section is ever changed to
1778 discard the string data when low in memory, this will have to be
1780 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1782 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1783 a
->vna_nextptr
= t
->vn_auxptr
;
1785 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1788 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1795 /* Figure out appropriate versions for all the symbols. We may not
1796 have the version number script until we have read all of the input
1797 files, so until that point we don't know which symbols should be
1798 local. This function is called via elf_link_hash_traverse. */
1801 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1803 struct elf_assign_sym_version_info
*sinfo
;
1804 struct bfd_link_info
*info
;
1805 const struct elf_backend_data
*bed
;
1806 struct elf_info_failed eif
;
1813 if (h
->root
.type
== bfd_link_hash_warning
)
1814 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1816 /* Fix the symbol flags. */
1819 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1822 sinfo
->failed
= TRUE
;
1826 /* We only need version numbers for symbols defined in regular
1828 if (!h
->def_regular
)
1831 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1832 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1833 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1835 struct bfd_elf_version_tree
*t
;
1840 /* There are two consecutive ELF_VER_CHR characters if this is
1841 not a hidden symbol. */
1843 if (*p
== ELF_VER_CHR
)
1849 /* If there is no version string, we can just return out. */
1857 /* Look for the version. If we find it, it is no longer weak. */
1858 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1860 if (strcmp (t
->name
, p
) == 0)
1864 struct bfd_elf_version_expr
*d
;
1866 len
= p
- h
->root
.root
.string
;
1867 alc
= bfd_malloc (len
);
1870 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1871 alc
[len
- 1] = '\0';
1872 if (alc
[len
- 2] == ELF_VER_CHR
)
1873 alc
[len
- 2] = '\0';
1875 h
->verinfo
.vertree
= t
;
1879 if (t
->globals
.list
!= NULL
)
1880 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1882 /* See if there is anything to force this symbol to
1884 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1886 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1889 && ! info
->export_dynamic
)
1890 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1898 /* If we are building an application, we need to create a
1899 version node for this version. */
1900 if (t
== NULL
&& info
->executable
)
1902 struct bfd_elf_version_tree
**pp
;
1905 /* If we aren't going to export this symbol, we don't need
1906 to worry about it. */
1907 if (h
->dynindx
== -1)
1911 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1914 sinfo
->failed
= TRUE
;
1919 t
->name_indx
= (unsigned int) -1;
1923 /* Don't count anonymous version tag. */
1924 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1926 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1928 t
->vernum
= version_index
;
1932 h
->verinfo
.vertree
= t
;
1936 /* We could not find the version for a symbol when
1937 generating a shared archive. Return an error. */
1938 (*_bfd_error_handler
)
1939 (_("%B: undefined versioned symbol name %s"),
1940 sinfo
->output_bfd
, h
->root
.root
.string
);
1941 bfd_set_error (bfd_error_bad_value
);
1942 sinfo
->failed
= TRUE
;
1950 /* If we don't have a version for this symbol, see if we can find
1952 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1954 struct bfd_elf_version_tree
*t
;
1955 struct bfd_elf_version_tree
*local_ver
;
1956 struct bfd_elf_version_expr
*d
;
1958 /* See if can find what version this symbol is in. If the
1959 symbol is supposed to be local, then don't actually register
1962 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1964 if (t
->globals
.list
!= NULL
)
1966 bfd_boolean matched
;
1970 while ((d
= (*t
->match
) (&t
->globals
, d
,
1971 h
->root
.root
.string
)) != NULL
)
1976 /* There is a version without definition. Make
1977 the symbol the default definition for this
1979 h
->verinfo
.vertree
= t
;
1987 /* There is no undefined version for this symbol. Hide the
1989 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1992 if (t
->locals
.list
!= NULL
)
1995 while ((d
= (*t
->match
) (&t
->locals
, d
,
1996 h
->root
.root
.string
)) != NULL
)
1999 /* If the match is "*", keep looking for a more
2000 explicit, perhaps even global, match.
2001 XXX: Shouldn't this be !d->wildcard instead? */
2002 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2011 if (local_ver
!= NULL
)
2013 h
->verinfo
.vertree
= local_ver
;
2014 if (h
->dynindx
!= -1
2015 && ! info
->export_dynamic
)
2017 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2025 /* Read and swap the relocs from the section indicated by SHDR. This
2026 may be either a REL or a RELA section. The relocations are
2027 translated into RELA relocations and stored in INTERNAL_RELOCS,
2028 which should have already been allocated to contain enough space.
2029 The EXTERNAL_RELOCS are a buffer where the external form of the
2030 relocations should be stored.
2032 Returns FALSE if something goes wrong. */
2035 elf_link_read_relocs_from_section (bfd
*abfd
,
2037 Elf_Internal_Shdr
*shdr
,
2038 void *external_relocs
,
2039 Elf_Internal_Rela
*internal_relocs
)
2041 const struct elf_backend_data
*bed
;
2042 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2043 const bfd_byte
*erela
;
2044 const bfd_byte
*erelaend
;
2045 Elf_Internal_Rela
*irela
;
2046 Elf_Internal_Shdr
*symtab_hdr
;
2049 /* Position ourselves at the start of the section. */
2050 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2053 /* Read the relocations. */
2054 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2057 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2058 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2060 bed
= get_elf_backend_data (abfd
);
2062 /* Convert the external relocations to the internal format. */
2063 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2064 swap_in
= bed
->s
->swap_reloc_in
;
2065 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2066 swap_in
= bed
->s
->swap_reloca_in
;
2069 bfd_set_error (bfd_error_wrong_format
);
2073 erela
= external_relocs
;
2074 erelaend
= erela
+ shdr
->sh_size
;
2075 irela
= internal_relocs
;
2076 while (erela
< erelaend
)
2080 (*swap_in
) (abfd
, erela
, irela
);
2081 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2082 if (bed
->s
->arch_size
== 64)
2084 if ((size_t) r_symndx
>= nsyms
)
2086 (*_bfd_error_handler
)
2087 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2088 " for offset 0x%lx in section `%A'"),
2090 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2091 bfd_set_error (bfd_error_bad_value
);
2094 irela
+= bed
->s
->int_rels_per_ext_rel
;
2095 erela
+= shdr
->sh_entsize
;
2101 /* Read and swap the relocs for a section O. They may have been
2102 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2103 not NULL, they are used as buffers to read into. They are known to
2104 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2105 the return value is allocated using either malloc or bfd_alloc,
2106 according to the KEEP_MEMORY argument. If O has two relocation
2107 sections (both REL and RELA relocations), then the REL_HDR
2108 relocations will appear first in INTERNAL_RELOCS, followed by the
2109 REL_HDR2 relocations. */
2112 _bfd_elf_link_read_relocs (bfd
*abfd
,
2114 void *external_relocs
,
2115 Elf_Internal_Rela
*internal_relocs
,
2116 bfd_boolean keep_memory
)
2118 Elf_Internal_Shdr
*rel_hdr
;
2119 void *alloc1
= NULL
;
2120 Elf_Internal_Rela
*alloc2
= NULL
;
2121 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2123 if (elf_section_data (o
)->relocs
!= NULL
)
2124 return elf_section_data (o
)->relocs
;
2126 if (o
->reloc_count
== 0)
2129 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2131 if (internal_relocs
== NULL
)
2135 size
= o
->reloc_count
;
2136 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2138 internal_relocs
= bfd_alloc (abfd
, size
);
2140 internal_relocs
= alloc2
= bfd_malloc (size
);
2141 if (internal_relocs
== NULL
)
2145 if (external_relocs
== NULL
)
2147 bfd_size_type size
= rel_hdr
->sh_size
;
2149 if (elf_section_data (o
)->rel_hdr2
)
2150 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2151 alloc1
= bfd_malloc (size
);
2154 external_relocs
= alloc1
;
2157 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2161 if (elf_section_data (o
)->rel_hdr2
2162 && (!elf_link_read_relocs_from_section
2164 elf_section_data (o
)->rel_hdr2
,
2165 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2166 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2167 * bed
->s
->int_rels_per_ext_rel
))))
2170 /* Cache the results for next time, if we can. */
2172 elf_section_data (o
)->relocs
= internal_relocs
;
2177 /* Don't free alloc2, since if it was allocated we are passing it
2178 back (under the name of internal_relocs). */
2180 return internal_relocs
;
2190 /* Compute the size of, and allocate space for, REL_HDR which is the
2191 section header for a section containing relocations for O. */
2194 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2195 Elf_Internal_Shdr
*rel_hdr
,
2198 bfd_size_type reloc_count
;
2199 bfd_size_type num_rel_hashes
;
2201 /* Figure out how many relocations there will be. */
2202 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2203 reloc_count
= elf_section_data (o
)->rel_count
;
2205 reloc_count
= elf_section_data (o
)->rel_count2
;
2207 num_rel_hashes
= o
->reloc_count
;
2208 if (num_rel_hashes
< reloc_count
)
2209 num_rel_hashes
= reloc_count
;
2211 /* That allows us to calculate the size of the section. */
2212 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2214 /* The contents field must last into write_object_contents, so we
2215 allocate it with bfd_alloc rather than malloc. Also since we
2216 cannot be sure that the contents will actually be filled in,
2217 we zero the allocated space. */
2218 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2219 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2222 /* We only allocate one set of hash entries, so we only do it the
2223 first time we are called. */
2224 if (elf_section_data (o
)->rel_hashes
== NULL
2227 struct elf_link_hash_entry
**p
;
2229 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2233 elf_section_data (o
)->rel_hashes
= p
;
2239 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2240 originated from the section given by INPUT_REL_HDR) to the
2244 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2245 asection
*input_section
,
2246 Elf_Internal_Shdr
*input_rel_hdr
,
2247 Elf_Internal_Rela
*internal_relocs
,
2248 struct elf_link_hash_entry
**rel_hash
2251 Elf_Internal_Rela
*irela
;
2252 Elf_Internal_Rela
*irelaend
;
2254 Elf_Internal_Shdr
*output_rel_hdr
;
2255 asection
*output_section
;
2256 unsigned int *rel_countp
= NULL
;
2257 const struct elf_backend_data
*bed
;
2258 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2260 output_section
= input_section
->output_section
;
2261 output_rel_hdr
= NULL
;
2263 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2264 == input_rel_hdr
->sh_entsize
)
2266 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2267 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2269 else if (elf_section_data (output_section
)->rel_hdr2
2270 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2271 == input_rel_hdr
->sh_entsize
))
2273 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2274 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2278 (*_bfd_error_handler
)
2279 (_("%B: relocation size mismatch in %B section %A"),
2280 output_bfd
, input_section
->owner
, input_section
);
2281 bfd_set_error (bfd_error_wrong_object_format
);
2285 bed
= get_elf_backend_data (output_bfd
);
2286 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2287 swap_out
= bed
->s
->swap_reloc_out
;
2288 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2289 swap_out
= bed
->s
->swap_reloca_out
;
2293 erel
= output_rel_hdr
->contents
;
2294 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2295 irela
= internal_relocs
;
2296 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2297 * bed
->s
->int_rels_per_ext_rel
);
2298 while (irela
< irelaend
)
2300 (*swap_out
) (output_bfd
, irela
, erel
);
2301 irela
+= bed
->s
->int_rels_per_ext_rel
;
2302 erel
+= input_rel_hdr
->sh_entsize
;
2305 /* Bump the counter, so that we know where to add the next set of
2307 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2312 /* Make weak undefined symbols in PIE dynamic. */
2315 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2316 struct elf_link_hash_entry
*h
)
2320 && h
->root
.type
== bfd_link_hash_undefweak
)
2321 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2326 /* Fix up the flags for a symbol. This handles various cases which
2327 can only be fixed after all the input files are seen. This is
2328 currently called by both adjust_dynamic_symbol and
2329 assign_sym_version, which is unnecessary but perhaps more robust in
2330 the face of future changes. */
2333 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2334 struct elf_info_failed
*eif
)
2336 const struct elf_backend_data
*bed
= NULL
;
2338 /* If this symbol was mentioned in a non-ELF file, try to set
2339 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2340 permit a non-ELF file to correctly refer to a symbol defined in
2341 an ELF dynamic object. */
2344 while (h
->root
.type
== bfd_link_hash_indirect
)
2345 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2347 if (h
->root
.type
!= bfd_link_hash_defined
2348 && h
->root
.type
!= bfd_link_hash_defweak
)
2351 h
->ref_regular_nonweak
= 1;
2355 if (h
->root
.u
.def
.section
->owner
!= NULL
2356 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2357 == bfd_target_elf_flavour
))
2360 h
->ref_regular_nonweak
= 1;
2366 if (h
->dynindx
== -1
2370 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2379 /* Unfortunately, NON_ELF is only correct if the symbol
2380 was first seen in a non-ELF file. Fortunately, if the symbol
2381 was first seen in an ELF file, we're probably OK unless the
2382 symbol was defined in a non-ELF file. Catch that case here.
2383 FIXME: We're still in trouble if the symbol was first seen in
2384 a dynamic object, and then later in a non-ELF regular object. */
2385 if ((h
->root
.type
== bfd_link_hash_defined
2386 || h
->root
.type
== bfd_link_hash_defweak
)
2388 && (h
->root
.u
.def
.section
->owner
!= NULL
2389 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2390 != bfd_target_elf_flavour
)
2391 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2392 && !h
->def_dynamic
)))
2396 /* Backend specific symbol fixup. */
2397 if (elf_hash_table (eif
->info
)->dynobj
)
2399 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2400 if (bed
->elf_backend_fixup_symbol
2401 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2405 /* If this is a final link, and the symbol was defined as a common
2406 symbol in a regular object file, and there was no definition in
2407 any dynamic object, then the linker will have allocated space for
2408 the symbol in a common section but the DEF_REGULAR
2409 flag will not have been set. */
2410 if (h
->root
.type
== bfd_link_hash_defined
2414 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2417 /* If -Bsymbolic was used (which means to bind references to global
2418 symbols to the definition within the shared object), and this
2419 symbol was defined in a regular object, then it actually doesn't
2420 need a PLT entry. Likewise, if the symbol has non-default
2421 visibility. If the symbol has hidden or internal visibility, we
2422 will force it local. */
2424 && eif
->info
->shared
2425 && is_elf_hash_table (eif
->info
->hash
)
2426 && (SYMBOLIC_BIND (eif
->info
, h
)
2427 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2430 bfd_boolean force_local
;
2432 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2433 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2434 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2437 /* If a weak undefined symbol has non-default visibility, we also
2438 hide it from the dynamic linker. */
2439 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2440 && h
->root
.type
== bfd_link_hash_undefweak
)
2442 const struct elf_backend_data
*bed
;
2443 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2444 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2447 /* If this is a weak defined symbol in a dynamic object, and we know
2448 the real definition in the dynamic object, copy interesting flags
2449 over to the real definition. */
2450 if (h
->u
.weakdef
!= NULL
)
2452 struct elf_link_hash_entry
*weakdef
;
2454 weakdef
= h
->u
.weakdef
;
2455 if (h
->root
.type
== bfd_link_hash_indirect
)
2456 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2458 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2459 || h
->root
.type
== bfd_link_hash_defweak
);
2460 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2461 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2462 BFD_ASSERT (weakdef
->def_dynamic
);
2464 /* If the real definition is defined by a regular object file,
2465 don't do anything special. See the longer description in
2466 _bfd_elf_adjust_dynamic_symbol, below. */
2467 if (weakdef
->def_regular
)
2468 h
->u
.weakdef
= NULL
;
2470 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2477 /* Make the backend pick a good value for a dynamic symbol. This is
2478 called via elf_link_hash_traverse, and also calls itself
2482 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2484 struct elf_info_failed
*eif
= data
;
2486 const struct elf_backend_data
*bed
;
2488 if (! is_elf_hash_table (eif
->info
->hash
))
2491 if (h
->root
.type
== bfd_link_hash_warning
)
2493 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2494 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2496 /* When warning symbols are created, they **replace** the "real"
2497 entry in the hash table, thus we never get to see the real
2498 symbol in a hash traversal. So look at it now. */
2499 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2502 /* Ignore indirect symbols. These are added by the versioning code. */
2503 if (h
->root
.type
== bfd_link_hash_indirect
)
2506 /* Fix the symbol flags. */
2507 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2510 /* If this symbol does not require a PLT entry, and it is not
2511 defined by a dynamic object, or is not referenced by a regular
2512 object, ignore it. We do have to handle a weak defined symbol,
2513 even if no regular object refers to it, if we decided to add it
2514 to the dynamic symbol table. FIXME: Do we normally need to worry
2515 about symbols which are defined by one dynamic object and
2516 referenced by another one? */
2521 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2523 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2527 /* If we've already adjusted this symbol, don't do it again. This
2528 can happen via a recursive call. */
2529 if (h
->dynamic_adjusted
)
2532 /* Don't look at this symbol again. Note that we must set this
2533 after checking the above conditions, because we may look at a
2534 symbol once, decide not to do anything, and then get called
2535 recursively later after REF_REGULAR is set below. */
2536 h
->dynamic_adjusted
= 1;
2538 /* If this is a weak definition, and we know a real definition, and
2539 the real symbol is not itself defined by a regular object file,
2540 then get a good value for the real definition. We handle the
2541 real symbol first, for the convenience of the backend routine.
2543 Note that there is a confusing case here. If the real definition
2544 is defined by a regular object file, we don't get the real symbol
2545 from the dynamic object, but we do get the weak symbol. If the
2546 processor backend uses a COPY reloc, then if some routine in the
2547 dynamic object changes the real symbol, we will not see that
2548 change in the corresponding weak symbol. This is the way other
2549 ELF linkers work as well, and seems to be a result of the shared
2552 I will clarify this issue. Most SVR4 shared libraries define the
2553 variable _timezone and define timezone as a weak synonym. The
2554 tzset call changes _timezone. If you write
2555 extern int timezone;
2557 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2558 you might expect that, since timezone is a synonym for _timezone,
2559 the same number will print both times. However, if the processor
2560 backend uses a COPY reloc, then actually timezone will be copied
2561 into your process image, and, since you define _timezone
2562 yourself, _timezone will not. Thus timezone and _timezone will
2563 wind up at different memory locations. The tzset call will set
2564 _timezone, leaving timezone unchanged. */
2566 if (h
->u
.weakdef
!= NULL
)
2568 /* If we get to this point, we know there is an implicit
2569 reference by a regular object file via the weak symbol H.
2570 FIXME: Is this really true? What if the traversal finds
2571 H->U.WEAKDEF before it finds H? */
2572 h
->u
.weakdef
->ref_regular
= 1;
2574 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2578 /* If a symbol has no type and no size and does not require a PLT
2579 entry, then we are probably about to do the wrong thing here: we
2580 are probably going to create a COPY reloc for an empty object.
2581 This case can arise when a shared object is built with assembly
2582 code, and the assembly code fails to set the symbol type. */
2584 && h
->type
== STT_NOTYPE
2586 (*_bfd_error_handler
)
2587 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2588 h
->root
.root
.string
);
2590 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2591 bed
= get_elf_backend_data (dynobj
);
2592 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2601 /* Adjust all external symbols pointing into SEC_MERGE sections
2602 to reflect the object merging within the sections. */
2605 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2609 if (h
->root
.type
== bfd_link_hash_warning
)
2610 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2612 if ((h
->root
.type
== bfd_link_hash_defined
2613 || h
->root
.type
== bfd_link_hash_defweak
)
2614 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2615 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2617 bfd
*output_bfd
= data
;
2619 h
->root
.u
.def
.value
=
2620 _bfd_merged_section_offset (output_bfd
,
2621 &h
->root
.u
.def
.section
,
2622 elf_section_data (sec
)->sec_info
,
2623 h
->root
.u
.def
.value
);
2629 /* Returns false if the symbol referred to by H should be considered
2630 to resolve local to the current module, and true if it should be
2631 considered to bind dynamically. */
2634 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2635 struct bfd_link_info
*info
,
2636 bfd_boolean ignore_protected
)
2638 bfd_boolean binding_stays_local_p
;
2643 while (h
->root
.type
== bfd_link_hash_indirect
2644 || h
->root
.type
== bfd_link_hash_warning
)
2645 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2647 /* If it was forced local, then clearly it's not dynamic. */
2648 if (h
->dynindx
== -1)
2650 if (h
->forced_local
)
2653 /* Identify the cases where name binding rules say that a
2654 visible symbol resolves locally. */
2655 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2657 switch (ELF_ST_VISIBILITY (h
->other
))
2664 /* Proper resolution for function pointer equality may require
2665 that these symbols perhaps be resolved dynamically, even though
2666 we should be resolving them to the current module. */
2667 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2668 binding_stays_local_p
= TRUE
;
2675 /* If it isn't defined locally, then clearly it's dynamic. */
2676 if (!h
->def_regular
)
2679 /* Otherwise, the symbol is dynamic if binding rules don't tell
2680 us that it remains local. */
2681 return !binding_stays_local_p
;
2684 /* Return true if the symbol referred to by H should be considered
2685 to resolve local to the current module, and false otherwise. Differs
2686 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2687 undefined symbols and weak symbols. */
2690 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2691 struct bfd_link_info
*info
,
2692 bfd_boolean local_protected
)
2694 /* If it's a local sym, of course we resolve locally. */
2698 /* Common symbols that become definitions don't get the DEF_REGULAR
2699 flag set, so test it first, and don't bail out. */
2700 if (ELF_COMMON_DEF_P (h
))
2702 /* If we don't have a definition in a regular file, then we can't
2703 resolve locally. The sym is either undefined or dynamic. */
2704 else if (!h
->def_regular
)
2707 /* Forced local symbols resolve locally. */
2708 if (h
->forced_local
)
2711 /* As do non-dynamic symbols. */
2712 if (h
->dynindx
== -1)
2715 /* At this point, we know the symbol is defined and dynamic. In an
2716 executable it must resolve locally, likewise when building symbolic
2717 shared libraries. */
2718 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2721 /* Now deal with defined dynamic symbols in shared libraries. Ones
2722 with default visibility might not resolve locally. */
2723 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2726 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2727 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2730 /* STV_PROTECTED non-function symbols are local. */
2731 if (h
->type
!= STT_FUNC
)
2734 /* Function pointer equality tests may require that STV_PROTECTED
2735 symbols be treated as dynamic symbols, even when we know that the
2736 dynamic linker will resolve them locally. */
2737 return local_protected
;
2740 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2741 aligned. Returns the first TLS output section. */
2743 struct bfd_section
*
2744 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2746 struct bfd_section
*sec
, *tls
;
2747 unsigned int align
= 0;
2749 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2750 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2754 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2755 if (sec
->alignment_power
> align
)
2756 align
= sec
->alignment_power
;
2758 elf_hash_table (info
)->tls_sec
= tls
;
2760 /* Ensure the alignment of the first section is the largest alignment,
2761 so that the tls segment starts aligned. */
2763 tls
->alignment_power
= align
;
2768 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2770 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2771 Elf_Internal_Sym
*sym
)
2773 const struct elf_backend_data
*bed
;
2775 /* Local symbols do not count, but target specific ones might. */
2776 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2777 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2780 /* Function symbols do not count. */
2781 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2784 /* If the section is undefined, then so is the symbol. */
2785 if (sym
->st_shndx
== SHN_UNDEF
)
2788 /* If the symbol is defined in the common section, then
2789 it is a common definition and so does not count. */
2790 bed
= get_elf_backend_data (abfd
);
2791 if (bed
->common_definition (sym
))
2794 /* If the symbol is in a target specific section then we
2795 must rely upon the backend to tell us what it is. */
2796 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2797 /* FIXME - this function is not coded yet:
2799 return _bfd_is_global_symbol_definition (abfd, sym);
2801 Instead for now assume that the definition is not global,
2802 Even if this is wrong, at least the linker will behave
2803 in the same way that it used to do. */
2809 /* Search the symbol table of the archive element of the archive ABFD
2810 whose archive map contains a mention of SYMDEF, and determine if
2811 the symbol is defined in this element. */
2813 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2815 Elf_Internal_Shdr
* hdr
;
2816 bfd_size_type symcount
;
2817 bfd_size_type extsymcount
;
2818 bfd_size_type extsymoff
;
2819 Elf_Internal_Sym
*isymbuf
;
2820 Elf_Internal_Sym
*isym
;
2821 Elf_Internal_Sym
*isymend
;
2824 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2828 if (! bfd_check_format (abfd
, bfd_object
))
2831 /* If we have already included the element containing this symbol in the
2832 link then we do not need to include it again. Just claim that any symbol
2833 it contains is not a definition, so that our caller will not decide to
2834 (re)include this element. */
2835 if (abfd
->archive_pass
)
2838 /* Select the appropriate symbol table. */
2839 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2840 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2842 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2844 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2846 /* The sh_info field of the symtab header tells us where the
2847 external symbols start. We don't care about the local symbols. */
2848 if (elf_bad_symtab (abfd
))
2850 extsymcount
= symcount
;
2855 extsymcount
= symcount
- hdr
->sh_info
;
2856 extsymoff
= hdr
->sh_info
;
2859 if (extsymcount
== 0)
2862 /* Read in the symbol table. */
2863 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2865 if (isymbuf
== NULL
)
2868 /* Scan the symbol table looking for SYMDEF. */
2870 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2874 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2879 if (strcmp (name
, symdef
->name
) == 0)
2881 result
= is_global_data_symbol_definition (abfd
, isym
);
2891 /* Add an entry to the .dynamic table. */
2894 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2898 struct elf_link_hash_table
*hash_table
;
2899 const struct elf_backend_data
*bed
;
2901 bfd_size_type newsize
;
2902 bfd_byte
*newcontents
;
2903 Elf_Internal_Dyn dyn
;
2905 hash_table
= elf_hash_table (info
);
2906 if (! is_elf_hash_table (hash_table
))
2909 bed
= get_elf_backend_data (hash_table
->dynobj
);
2910 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2911 BFD_ASSERT (s
!= NULL
);
2913 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2914 newcontents
= bfd_realloc (s
->contents
, newsize
);
2915 if (newcontents
== NULL
)
2919 dyn
.d_un
.d_val
= val
;
2920 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2923 s
->contents
= newcontents
;
2928 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2929 otherwise just check whether one already exists. Returns -1 on error,
2930 1 if a DT_NEEDED tag already exists, and 0 on success. */
2933 elf_add_dt_needed_tag (bfd
*abfd
,
2934 struct bfd_link_info
*info
,
2938 struct elf_link_hash_table
*hash_table
;
2939 bfd_size_type oldsize
;
2940 bfd_size_type strindex
;
2942 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2945 hash_table
= elf_hash_table (info
);
2946 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2947 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2948 if (strindex
== (bfd_size_type
) -1)
2951 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2954 const struct elf_backend_data
*bed
;
2957 bed
= get_elf_backend_data (hash_table
->dynobj
);
2958 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2960 for (extdyn
= sdyn
->contents
;
2961 extdyn
< sdyn
->contents
+ sdyn
->size
;
2962 extdyn
+= bed
->s
->sizeof_dyn
)
2964 Elf_Internal_Dyn dyn
;
2966 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2967 if (dyn
.d_tag
== DT_NEEDED
2968 && dyn
.d_un
.d_val
== strindex
)
2970 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2978 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2981 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2985 /* We were just checking for existence of the tag. */
2986 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2991 /* Sort symbol by value and section. */
2993 elf_sort_symbol (const void *arg1
, const void *arg2
)
2995 const struct elf_link_hash_entry
*h1
;
2996 const struct elf_link_hash_entry
*h2
;
2997 bfd_signed_vma vdiff
;
2999 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3000 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3001 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3003 return vdiff
> 0 ? 1 : -1;
3006 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3008 return sdiff
> 0 ? 1 : -1;
3013 /* This function is used to adjust offsets into .dynstr for
3014 dynamic symbols. This is called via elf_link_hash_traverse. */
3017 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3019 struct elf_strtab_hash
*dynstr
= data
;
3021 if (h
->root
.type
== bfd_link_hash_warning
)
3022 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3024 if (h
->dynindx
!= -1)
3025 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3029 /* Assign string offsets in .dynstr, update all structures referencing
3033 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3035 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3036 struct elf_link_local_dynamic_entry
*entry
;
3037 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3038 bfd
*dynobj
= hash_table
->dynobj
;
3041 const struct elf_backend_data
*bed
;
3044 _bfd_elf_strtab_finalize (dynstr
);
3045 size
= _bfd_elf_strtab_size (dynstr
);
3047 bed
= get_elf_backend_data (dynobj
);
3048 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3049 BFD_ASSERT (sdyn
!= NULL
);
3051 /* Update all .dynamic entries referencing .dynstr strings. */
3052 for (extdyn
= sdyn
->contents
;
3053 extdyn
< sdyn
->contents
+ sdyn
->size
;
3054 extdyn
+= bed
->s
->sizeof_dyn
)
3056 Elf_Internal_Dyn dyn
;
3058 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3062 dyn
.d_un
.d_val
= size
;
3070 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3075 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3078 /* Now update local dynamic symbols. */
3079 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3080 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3081 entry
->isym
.st_name
);
3083 /* And the rest of dynamic symbols. */
3084 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3086 /* Adjust version definitions. */
3087 if (elf_tdata (output_bfd
)->cverdefs
)
3092 Elf_Internal_Verdef def
;
3093 Elf_Internal_Verdaux defaux
;
3095 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3099 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3101 p
+= sizeof (Elf_External_Verdef
);
3102 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3104 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3106 _bfd_elf_swap_verdaux_in (output_bfd
,
3107 (Elf_External_Verdaux
*) p
, &defaux
);
3108 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3110 _bfd_elf_swap_verdaux_out (output_bfd
,
3111 &defaux
, (Elf_External_Verdaux
*) p
);
3112 p
+= sizeof (Elf_External_Verdaux
);
3115 while (def
.vd_next
);
3118 /* Adjust version references. */
3119 if (elf_tdata (output_bfd
)->verref
)
3124 Elf_Internal_Verneed need
;
3125 Elf_Internal_Vernaux needaux
;
3127 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3131 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3133 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3134 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3135 (Elf_External_Verneed
*) p
);
3136 p
+= sizeof (Elf_External_Verneed
);
3137 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3139 _bfd_elf_swap_vernaux_in (output_bfd
,
3140 (Elf_External_Vernaux
*) p
, &needaux
);
3141 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3143 _bfd_elf_swap_vernaux_out (output_bfd
,
3145 (Elf_External_Vernaux
*) p
);
3146 p
+= sizeof (Elf_External_Vernaux
);
3149 while (need
.vn_next
);
3155 /* Add symbols from an ELF object file to the linker hash table. */
3158 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3160 Elf_Internal_Shdr
*hdr
;
3161 bfd_size_type symcount
;
3162 bfd_size_type extsymcount
;
3163 bfd_size_type extsymoff
;
3164 struct elf_link_hash_entry
**sym_hash
;
3165 bfd_boolean dynamic
;
3166 Elf_External_Versym
*extversym
= NULL
;
3167 Elf_External_Versym
*ever
;
3168 struct elf_link_hash_entry
*weaks
;
3169 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3170 bfd_size_type nondeflt_vers_cnt
= 0;
3171 Elf_Internal_Sym
*isymbuf
= NULL
;
3172 Elf_Internal_Sym
*isym
;
3173 Elf_Internal_Sym
*isymend
;
3174 const struct elf_backend_data
*bed
;
3175 bfd_boolean add_needed
;
3176 struct elf_link_hash_table
*htab
;
3178 void *alloc_mark
= NULL
;
3179 struct bfd_hash_entry
**old_table
= NULL
;
3180 unsigned int old_size
= 0;
3181 unsigned int old_count
= 0;
3182 void *old_tab
= NULL
;
3185 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3186 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3187 long old_dynsymcount
= 0;
3189 size_t hashsize
= 0;
3191 htab
= elf_hash_table (info
);
3192 bed
= get_elf_backend_data (abfd
);
3194 if ((abfd
->flags
& DYNAMIC
) == 0)
3200 /* You can't use -r against a dynamic object. Also, there's no
3201 hope of using a dynamic object which does not exactly match
3202 the format of the output file. */
3203 if (info
->relocatable
3204 || !is_elf_hash_table (htab
)
3205 || htab
->root
.creator
!= abfd
->xvec
)
3207 if (info
->relocatable
)
3208 bfd_set_error (bfd_error_invalid_operation
);
3210 bfd_set_error (bfd_error_wrong_format
);
3215 /* As a GNU extension, any input sections which are named
3216 .gnu.warning.SYMBOL are treated as warning symbols for the given
3217 symbol. This differs from .gnu.warning sections, which generate
3218 warnings when they are included in an output file. */
3219 if (info
->executable
)
3223 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3227 name
= bfd_get_section_name (abfd
, s
);
3228 if (CONST_STRNEQ (name
, ".gnu.warning."))
3233 name
+= sizeof ".gnu.warning." - 1;
3235 /* If this is a shared object, then look up the symbol
3236 in the hash table. If it is there, and it is already
3237 been defined, then we will not be using the entry
3238 from this shared object, so we don't need to warn.
3239 FIXME: If we see the definition in a regular object
3240 later on, we will warn, but we shouldn't. The only
3241 fix is to keep track of what warnings we are supposed
3242 to emit, and then handle them all at the end of the
3246 struct elf_link_hash_entry
*h
;
3248 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3250 /* FIXME: What about bfd_link_hash_common? */
3252 && (h
->root
.type
== bfd_link_hash_defined
3253 || h
->root
.type
== bfd_link_hash_defweak
))
3255 /* We don't want to issue this warning. Clobber
3256 the section size so that the warning does not
3257 get copied into the output file. */
3264 msg
= bfd_alloc (abfd
, sz
+ 1);
3268 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3273 if (! (_bfd_generic_link_add_one_symbol
3274 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3275 FALSE
, bed
->collect
, NULL
)))
3278 if (! info
->relocatable
)
3280 /* Clobber the section size so that the warning does
3281 not get copied into the output file. */
3284 /* Also set SEC_EXCLUDE, so that symbols defined in
3285 the warning section don't get copied to the output. */
3286 s
->flags
|= SEC_EXCLUDE
;
3295 /* If we are creating a shared library, create all the dynamic
3296 sections immediately. We need to attach them to something,
3297 so we attach them to this BFD, provided it is the right
3298 format. FIXME: If there are no input BFD's of the same
3299 format as the output, we can't make a shared library. */
3301 && is_elf_hash_table (htab
)
3302 && htab
->root
.creator
== abfd
->xvec
3303 && !htab
->dynamic_sections_created
)
3305 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3309 else if (!is_elf_hash_table (htab
))
3314 const char *soname
= NULL
;
3315 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3318 /* ld --just-symbols and dynamic objects don't mix very well.
3319 ld shouldn't allow it. */
3320 if ((s
= abfd
->sections
) != NULL
3321 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3324 /* If this dynamic lib was specified on the command line with
3325 --as-needed in effect, then we don't want to add a DT_NEEDED
3326 tag unless the lib is actually used. Similary for libs brought
3327 in by another lib's DT_NEEDED. When --no-add-needed is used
3328 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3329 any dynamic library in DT_NEEDED tags in the dynamic lib at
3331 add_needed
= (elf_dyn_lib_class (abfd
)
3332 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3333 | DYN_NO_NEEDED
)) == 0;
3335 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3341 unsigned long shlink
;
3343 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3344 goto error_free_dyn
;
3346 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3348 goto error_free_dyn
;
3349 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3351 for (extdyn
= dynbuf
;
3352 extdyn
< dynbuf
+ s
->size
;
3353 extdyn
+= bed
->s
->sizeof_dyn
)
3355 Elf_Internal_Dyn dyn
;
3357 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3358 if (dyn
.d_tag
== DT_SONAME
)
3360 unsigned int tagv
= dyn
.d_un
.d_val
;
3361 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3363 goto error_free_dyn
;
3365 if (dyn
.d_tag
== DT_NEEDED
)
3367 struct bfd_link_needed_list
*n
, **pn
;
3369 unsigned int tagv
= dyn
.d_un
.d_val
;
3371 amt
= sizeof (struct bfd_link_needed_list
);
3372 n
= bfd_alloc (abfd
, amt
);
3373 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3374 if (n
== NULL
|| fnm
== NULL
)
3375 goto error_free_dyn
;
3376 amt
= strlen (fnm
) + 1;
3377 anm
= bfd_alloc (abfd
, amt
);
3379 goto error_free_dyn
;
3380 memcpy (anm
, fnm
, amt
);
3384 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3388 if (dyn
.d_tag
== DT_RUNPATH
)
3390 struct bfd_link_needed_list
*n
, **pn
;
3392 unsigned int tagv
= dyn
.d_un
.d_val
;
3394 amt
= sizeof (struct bfd_link_needed_list
);
3395 n
= bfd_alloc (abfd
, amt
);
3396 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3397 if (n
== NULL
|| fnm
== NULL
)
3398 goto error_free_dyn
;
3399 amt
= strlen (fnm
) + 1;
3400 anm
= bfd_alloc (abfd
, amt
);
3402 goto error_free_dyn
;
3403 memcpy (anm
, fnm
, amt
);
3407 for (pn
= & runpath
;
3413 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3414 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3416 struct bfd_link_needed_list
*n
, **pn
;
3418 unsigned int tagv
= dyn
.d_un
.d_val
;
3420 amt
= sizeof (struct bfd_link_needed_list
);
3421 n
= bfd_alloc (abfd
, amt
);
3422 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3423 if (n
== NULL
|| fnm
== NULL
)
3424 goto error_free_dyn
;
3425 amt
= strlen (fnm
) + 1;
3426 anm
= bfd_alloc (abfd
, amt
);
3433 memcpy (anm
, fnm
, amt
);
3448 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3449 frees all more recently bfd_alloc'd blocks as well. */
3455 struct bfd_link_needed_list
**pn
;
3456 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3461 /* We do not want to include any of the sections in a dynamic
3462 object in the output file. We hack by simply clobbering the
3463 list of sections in the BFD. This could be handled more
3464 cleanly by, say, a new section flag; the existing
3465 SEC_NEVER_LOAD flag is not the one we want, because that one
3466 still implies that the section takes up space in the output
3468 bfd_section_list_clear (abfd
);
3470 /* Find the name to use in a DT_NEEDED entry that refers to this
3471 object. If the object has a DT_SONAME entry, we use it.
3472 Otherwise, if the generic linker stuck something in
3473 elf_dt_name, we use that. Otherwise, we just use the file
3475 if (soname
== NULL
|| *soname
== '\0')
3477 soname
= elf_dt_name (abfd
);
3478 if (soname
== NULL
|| *soname
== '\0')
3479 soname
= bfd_get_filename (abfd
);
3482 /* Save the SONAME because sometimes the linker emulation code
3483 will need to know it. */
3484 elf_dt_name (abfd
) = soname
;
3486 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3490 /* If we have already included this dynamic object in the
3491 link, just ignore it. There is no reason to include a
3492 particular dynamic object more than once. */
3497 /* If this is a dynamic object, we always link against the .dynsym
3498 symbol table, not the .symtab symbol table. The dynamic linker
3499 will only see the .dynsym symbol table, so there is no reason to
3500 look at .symtab for a dynamic object. */
3502 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3503 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3505 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3507 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3509 /* The sh_info field of the symtab header tells us where the
3510 external symbols start. We don't care about the local symbols at
3512 if (elf_bad_symtab (abfd
))
3514 extsymcount
= symcount
;
3519 extsymcount
= symcount
- hdr
->sh_info
;
3520 extsymoff
= hdr
->sh_info
;
3524 if (extsymcount
!= 0)
3526 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3528 if (isymbuf
== NULL
)
3531 /* We store a pointer to the hash table entry for each external
3533 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3534 sym_hash
= bfd_alloc (abfd
, amt
);
3535 if (sym_hash
== NULL
)
3536 goto error_free_sym
;
3537 elf_sym_hashes (abfd
) = sym_hash
;
3542 /* Read in any version definitions. */
3543 if (!_bfd_elf_slurp_version_tables (abfd
,
3544 info
->default_imported_symver
))
3545 goto error_free_sym
;
3547 /* Read in the symbol versions, but don't bother to convert them
3548 to internal format. */
3549 if (elf_dynversym (abfd
) != 0)
3551 Elf_Internal_Shdr
*versymhdr
;
3553 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3554 extversym
= bfd_malloc (versymhdr
->sh_size
);
3555 if (extversym
== NULL
)
3556 goto error_free_sym
;
3557 amt
= versymhdr
->sh_size
;
3558 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3559 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3560 goto error_free_vers
;
3564 /* If we are loading an as-needed shared lib, save the symbol table
3565 state before we start adding symbols. If the lib turns out
3566 to be unneeded, restore the state. */
3567 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3572 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3574 struct bfd_hash_entry
*p
;
3575 struct elf_link_hash_entry
*h
;
3577 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3579 h
= (struct elf_link_hash_entry
*) p
;
3580 entsize
+= htab
->root
.table
.entsize
;
3581 if (h
->root
.type
== bfd_link_hash_warning
)
3582 entsize
+= htab
->root
.table
.entsize
;
3586 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3587 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3588 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3589 if (old_tab
== NULL
)
3590 goto error_free_vers
;
3592 /* Remember the current objalloc pointer, so that all mem for
3593 symbols added can later be reclaimed. */
3594 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3595 if (alloc_mark
== NULL
)
3596 goto error_free_vers
;
3598 /* Make a special call to the linker "notice" function to
3599 tell it that we are about to handle an as-needed lib. */
3600 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3605 /* Clone the symbol table and sym hashes. Remember some
3606 pointers into the symbol table, and dynamic symbol count. */
3607 old_hash
= (char *) old_tab
+ tabsize
;
3608 old_ent
= (char *) old_hash
+ hashsize
;
3609 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3610 memcpy (old_hash
, sym_hash
, hashsize
);
3611 old_undefs
= htab
->root
.undefs
;
3612 old_undefs_tail
= htab
->root
.undefs_tail
;
3613 old_table
= htab
->root
.table
.table
;
3614 old_size
= htab
->root
.table
.size
;
3615 old_count
= htab
->root
.table
.count
;
3616 old_dynsymcount
= htab
->dynsymcount
;
3618 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3620 struct bfd_hash_entry
*p
;
3621 struct elf_link_hash_entry
*h
;
3623 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3625 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3626 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3627 h
= (struct elf_link_hash_entry
*) p
;
3628 if (h
->root
.type
== bfd_link_hash_warning
)
3630 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3631 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3638 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3639 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3641 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3645 asection
*sec
, *new_sec
;
3648 struct elf_link_hash_entry
*h
;
3649 bfd_boolean definition
;
3650 bfd_boolean size_change_ok
;
3651 bfd_boolean type_change_ok
;
3652 bfd_boolean new_weakdef
;
3653 bfd_boolean override
;
3655 unsigned int old_alignment
;
3660 flags
= BSF_NO_FLAGS
;
3662 value
= isym
->st_value
;
3664 common
= bed
->common_definition (isym
);
3666 bind
= ELF_ST_BIND (isym
->st_info
);
3667 if (bind
== STB_LOCAL
)
3669 /* This should be impossible, since ELF requires that all
3670 global symbols follow all local symbols, and that sh_info
3671 point to the first global symbol. Unfortunately, Irix 5
3675 else if (bind
== STB_GLOBAL
)
3677 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3680 else if (bind
== STB_WEAK
)
3684 /* Leave it up to the processor backend. */
3687 if (isym
->st_shndx
== SHN_UNDEF
)
3688 sec
= bfd_und_section_ptr
;
3689 else if (isym
->st_shndx
< SHN_LORESERVE
3690 || isym
->st_shndx
> SHN_HIRESERVE
)
3692 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3694 sec
= bfd_abs_section_ptr
;
3695 else if (sec
->kept_section
)
3697 /* Symbols from discarded section are undefined. We keep
3699 sec
= bfd_und_section_ptr
;
3700 isym
->st_shndx
= SHN_UNDEF
;
3702 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3705 else if (isym
->st_shndx
== SHN_ABS
)
3706 sec
= bfd_abs_section_ptr
;
3707 else if (isym
->st_shndx
== SHN_COMMON
)
3709 sec
= bfd_com_section_ptr
;
3710 /* What ELF calls the size we call the value. What ELF
3711 calls the value we call the alignment. */
3712 value
= isym
->st_size
;
3716 /* Leave it up to the processor backend. */
3719 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3722 goto error_free_vers
;
3724 if (isym
->st_shndx
== SHN_COMMON
3725 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3726 && !info
->relocatable
)
3728 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3732 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3735 | SEC_LINKER_CREATED
3736 | SEC_THREAD_LOCAL
));
3738 goto error_free_vers
;
3742 else if (bed
->elf_add_symbol_hook
)
3744 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3746 goto error_free_vers
;
3748 /* The hook function sets the name to NULL if this symbol
3749 should be skipped for some reason. */
3754 /* Sanity check that all possibilities were handled. */
3757 bfd_set_error (bfd_error_bad_value
);
3758 goto error_free_vers
;
3761 if (bfd_is_und_section (sec
)
3762 || bfd_is_com_section (sec
))
3767 size_change_ok
= FALSE
;
3768 type_change_ok
= bed
->type_change_ok
;
3773 if (is_elf_hash_table (htab
))
3775 Elf_Internal_Versym iver
;
3776 unsigned int vernum
= 0;
3781 if (info
->default_imported_symver
)
3782 /* Use the default symbol version created earlier. */
3783 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3788 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3790 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3792 /* If this is a hidden symbol, or if it is not version
3793 1, we append the version name to the symbol name.
3794 However, we do not modify a non-hidden absolute symbol
3795 if it is not a function, because it might be the version
3796 symbol itself. FIXME: What if it isn't? */
3797 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3798 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3799 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3802 size_t namelen
, verlen
, newlen
;
3805 if (isym
->st_shndx
!= SHN_UNDEF
)
3807 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3809 else if (vernum
> 1)
3811 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3817 (*_bfd_error_handler
)
3818 (_("%B: %s: invalid version %u (max %d)"),
3820 elf_tdata (abfd
)->cverdefs
);
3821 bfd_set_error (bfd_error_bad_value
);
3822 goto error_free_vers
;
3827 /* We cannot simply test for the number of
3828 entries in the VERNEED section since the
3829 numbers for the needed versions do not start
3831 Elf_Internal_Verneed
*t
;
3834 for (t
= elf_tdata (abfd
)->verref
;
3838 Elf_Internal_Vernaux
*a
;
3840 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3842 if (a
->vna_other
== vernum
)
3844 verstr
= a
->vna_nodename
;
3853 (*_bfd_error_handler
)
3854 (_("%B: %s: invalid needed version %d"),
3855 abfd
, name
, vernum
);
3856 bfd_set_error (bfd_error_bad_value
);
3857 goto error_free_vers
;
3861 namelen
= strlen (name
);
3862 verlen
= strlen (verstr
);
3863 newlen
= namelen
+ verlen
+ 2;
3864 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3865 && isym
->st_shndx
!= SHN_UNDEF
)
3868 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3869 if (newname
== NULL
)
3870 goto error_free_vers
;
3871 memcpy (newname
, name
, namelen
);
3872 p
= newname
+ namelen
;
3874 /* If this is a defined non-hidden version symbol,
3875 we add another @ to the name. This indicates the
3876 default version of the symbol. */
3877 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3878 && isym
->st_shndx
!= SHN_UNDEF
)
3880 memcpy (p
, verstr
, verlen
+ 1);
3885 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3886 &value
, &old_alignment
,
3887 sym_hash
, &skip
, &override
,
3888 &type_change_ok
, &size_change_ok
))
3889 goto error_free_vers
;
3898 while (h
->root
.type
== bfd_link_hash_indirect
3899 || h
->root
.type
== bfd_link_hash_warning
)
3900 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3902 /* Remember the old alignment if this is a common symbol, so
3903 that we don't reduce the alignment later on. We can't
3904 check later, because _bfd_generic_link_add_one_symbol
3905 will set a default for the alignment which we want to
3906 override. We also remember the old bfd where the existing
3907 definition comes from. */
3908 switch (h
->root
.type
)
3913 case bfd_link_hash_defined
:
3914 case bfd_link_hash_defweak
:
3915 old_bfd
= h
->root
.u
.def
.section
->owner
;
3918 case bfd_link_hash_common
:
3919 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3920 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3924 if (elf_tdata (abfd
)->verdef
!= NULL
3928 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3931 if (! (_bfd_generic_link_add_one_symbol
3932 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3933 (struct bfd_link_hash_entry
**) sym_hash
)))
3934 goto error_free_vers
;
3937 while (h
->root
.type
== bfd_link_hash_indirect
3938 || h
->root
.type
== bfd_link_hash_warning
)
3939 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3942 new_weakdef
= FALSE
;
3945 && (flags
& BSF_WEAK
) != 0
3946 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3947 && is_elf_hash_table (htab
)
3948 && h
->u
.weakdef
== NULL
)
3950 /* Keep a list of all weak defined non function symbols from
3951 a dynamic object, using the weakdef field. Later in this
3952 function we will set the weakdef field to the correct
3953 value. We only put non-function symbols from dynamic
3954 objects on this list, because that happens to be the only
3955 time we need to know the normal symbol corresponding to a
3956 weak symbol, and the information is time consuming to
3957 figure out. If the weakdef field is not already NULL,
3958 then this symbol was already defined by some previous
3959 dynamic object, and we will be using that previous
3960 definition anyhow. */
3962 h
->u
.weakdef
= weaks
;
3967 /* Set the alignment of a common symbol. */
3968 if ((common
|| bfd_is_com_section (sec
))
3969 && h
->root
.type
== bfd_link_hash_common
)
3974 align
= bfd_log2 (isym
->st_value
);
3977 /* The new symbol is a common symbol in a shared object.
3978 We need to get the alignment from the section. */
3979 align
= new_sec
->alignment_power
;
3981 if (align
> old_alignment
3982 /* Permit an alignment power of zero if an alignment of one
3983 is specified and no other alignments have been specified. */
3984 || (isym
->st_value
== 1 && old_alignment
== 0))
3985 h
->root
.u
.c
.p
->alignment_power
= align
;
3987 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3990 if (is_elf_hash_table (htab
))
3994 /* Check the alignment when a common symbol is involved. This
3995 can change when a common symbol is overridden by a normal
3996 definition or a common symbol is ignored due to the old
3997 normal definition. We need to make sure the maximum
3998 alignment is maintained. */
3999 if ((old_alignment
|| common
)
4000 && h
->root
.type
!= bfd_link_hash_common
)
4002 unsigned int common_align
;
4003 unsigned int normal_align
;
4004 unsigned int symbol_align
;
4008 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4009 if (h
->root
.u
.def
.section
->owner
!= NULL
4010 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4012 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4013 if (normal_align
> symbol_align
)
4014 normal_align
= symbol_align
;
4017 normal_align
= symbol_align
;
4021 common_align
= old_alignment
;
4022 common_bfd
= old_bfd
;
4027 common_align
= bfd_log2 (isym
->st_value
);
4029 normal_bfd
= old_bfd
;
4032 if (normal_align
< common_align
)
4034 /* PR binutils/2735 */
4035 if (normal_bfd
== NULL
)
4036 (*_bfd_error_handler
)
4037 (_("Warning: alignment %u of common symbol `%s' in %B"
4038 " is greater than the alignment (%u) of its section %A"),
4039 common_bfd
, h
->root
.u
.def
.section
,
4040 1 << common_align
, name
, 1 << normal_align
);
4042 (*_bfd_error_handler
)
4043 (_("Warning: alignment %u of symbol `%s' in %B"
4044 " is smaller than %u in %B"),
4045 normal_bfd
, common_bfd
,
4046 1 << normal_align
, name
, 1 << common_align
);
4050 /* Remember the symbol size if it isn't undefined. */
4051 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4052 && (definition
|| h
->size
== 0))
4055 && h
->size
!= isym
->st_size
4056 && ! size_change_ok
)
4057 (*_bfd_error_handler
)
4058 (_("Warning: size of symbol `%s' changed"
4059 " from %lu in %B to %lu in %B"),
4061 name
, (unsigned long) h
->size
,
4062 (unsigned long) isym
->st_size
);
4064 h
->size
= isym
->st_size
;
4067 /* If this is a common symbol, then we always want H->SIZE
4068 to be the size of the common symbol. The code just above
4069 won't fix the size if a common symbol becomes larger. We
4070 don't warn about a size change here, because that is
4071 covered by --warn-common. */
4072 if (h
->root
.type
== bfd_link_hash_common
)
4073 h
->size
= h
->root
.u
.c
.size
;
4075 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4076 && (definition
|| h
->type
== STT_NOTYPE
))
4078 if (h
->type
!= STT_NOTYPE
4079 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4080 && ! type_change_ok
)
4081 (*_bfd_error_handler
)
4082 (_("Warning: type of symbol `%s' changed"
4083 " from %d to %d in %B"),
4084 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4086 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4089 /* If st_other has a processor-specific meaning, specific
4090 code might be needed here. We never merge the visibility
4091 attribute with the one from a dynamic object. */
4092 if (bed
->elf_backend_merge_symbol_attribute
)
4093 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4096 /* If this symbol has default visibility and the user has requested
4097 we not re-export it, then mark it as hidden. */
4098 if (definition
&& !dynamic
4100 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4101 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4102 isym
->st_other
= (STV_HIDDEN
4103 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4105 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4107 unsigned char hvis
, symvis
, other
, nvis
;
4109 /* Only merge the visibility. Leave the remainder of the
4110 st_other field to elf_backend_merge_symbol_attribute. */
4111 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4113 /* Combine visibilities, using the most constraining one. */
4114 hvis
= ELF_ST_VISIBILITY (h
->other
);
4115 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4121 nvis
= hvis
< symvis
? hvis
: symvis
;
4123 h
->other
= other
| nvis
;
4126 /* Set a flag in the hash table entry indicating the type of
4127 reference or definition we just found. Keep a count of
4128 the number of dynamic symbols we find. A dynamic symbol
4129 is one which is referenced or defined by both a regular
4130 object and a shared object. */
4137 if (bind
!= STB_WEAK
)
4138 h
->ref_regular_nonweak
= 1;
4142 if (! info
->executable
4155 || (h
->u
.weakdef
!= NULL
4157 && h
->u
.weakdef
->dynindx
!= -1))
4161 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4163 /* We don't want to make debug symbol dynamic. */
4164 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4168 /* Check to see if we need to add an indirect symbol for
4169 the default name. */
4170 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4171 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4172 &sec
, &value
, &dynsym
,
4174 goto error_free_vers
;
4176 if (definition
&& !dynamic
)
4178 char *p
= strchr (name
, ELF_VER_CHR
);
4179 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4181 /* Queue non-default versions so that .symver x, x@FOO
4182 aliases can be checked. */
4185 amt
= ((isymend
- isym
+ 1)
4186 * sizeof (struct elf_link_hash_entry
*));
4187 nondeflt_vers
= bfd_malloc (amt
);
4189 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4193 if (dynsym
&& h
->dynindx
== -1)
4195 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4196 goto error_free_vers
;
4197 if (h
->u
.weakdef
!= NULL
4199 && h
->u
.weakdef
->dynindx
== -1)
4201 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4202 goto error_free_vers
;
4205 else if (dynsym
&& h
->dynindx
!= -1)
4206 /* If the symbol already has a dynamic index, but
4207 visibility says it should not be visible, turn it into
4209 switch (ELF_ST_VISIBILITY (h
->other
))
4213 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4224 const char *soname
= elf_dt_name (abfd
);
4226 /* A symbol from a library loaded via DT_NEEDED of some
4227 other library is referenced by a regular object.
4228 Add a DT_NEEDED entry for it. Issue an error if
4229 --no-add-needed is used. */
4230 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4232 (*_bfd_error_handler
)
4233 (_("%s: invalid DSO for symbol `%s' definition"),
4235 bfd_set_error (bfd_error_bad_value
);
4236 goto error_free_vers
;
4239 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4242 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4244 goto error_free_vers
;
4246 BFD_ASSERT (ret
== 0);
4251 if (extversym
!= NULL
)
4257 if (isymbuf
!= NULL
)
4263 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4267 /* Restore the symbol table. */
4268 if (bed
->as_needed_cleanup
)
4269 (*bed
->as_needed_cleanup
) (abfd
, info
);
4270 old_hash
= (char *) old_tab
+ tabsize
;
4271 old_ent
= (char *) old_hash
+ hashsize
;
4272 sym_hash
= elf_sym_hashes (abfd
);
4273 htab
->root
.table
.table
= old_table
;
4274 htab
->root
.table
.size
= old_size
;
4275 htab
->root
.table
.count
= old_count
;
4276 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4277 memcpy (sym_hash
, old_hash
, hashsize
);
4278 htab
->root
.undefs
= old_undefs
;
4279 htab
->root
.undefs_tail
= old_undefs_tail
;
4280 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4282 struct bfd_hash_entry
*p
;
4283 struct elf_link_hash_entry
*h
;
4285 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4287 h
= (struct elf_link_hash_entry
*) p
;
4288 if (h
->root
.type
== bfd_link_hash_warning
)
4289 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4290 if (h
->dynindx
>= old_dynsymcount
)
4291 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4293 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4294 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4295 h
= (struct elf_link_hash_entry
*) p
;
4296 if (h
->root
.type
== bfd_link_hash_warning
)
4298 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4299 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4304 /* Make a special call to the linker "notice" function to
4305 tell it that symbols added for crefs may need to be removed. */
4306 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4311 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4313 if (nondeflt_vers
!= NULL
)
4314 free (nondeflt_vers
);
4318 if (old_tab
!= NULL
)
4320 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4327 /* Now that all the symbols from this input file are created, handle
4328 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4329 if (nondeflt_vers
!= NULL
)
4331 bfd_size_type cnt
, symidx
;
4333 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4335 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4336 char *shortname
, *p
;
4338 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4340 || (h
->root
.type
!= bfd_link_hash_defined
4341 && h
->root
.type
!= bfd_link_hash_defweak
))
4344 amt
= p
- h
->root
.root
.string
;
4345 shortname
= bfd_malloc (amt
+ 1);
4346 memcpy (shortname
, h
->root
.root
.string
, amt
);
4347 shortname
[amt
] = '\0';
4349 hi
= (struct elf_link_hash_entry
*)
4350 bfd_link_hash_lookup (&htab
->root
, shortname
,
4351 FALSE
, FALSE
, FALSE
);
4353 && hi
->root
.type
== h
->root
.type
4354 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4355 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4357 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4358 hi
->root
.type
= bfd_link_hash_indirect
;
4359 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4360 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4361 sym_hash
= elf_sym_hashes (abfd
);
4363 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4364 if (sym_hash
[symidx
] == hi
)
4366 sym_hash
[symidx
] = h
;
4372 free (nondeflt_vers
);
4373 nondeflt_vers
= NULL
;
4376 /* Now set the weakdefs field correctly for all the weak defined
4377 symbols we found. The only way to do this is to search all the
4378 symbols. Since we only need the information for non functions in
4379 dynamic objects, that's the only time we actually put anything on
4380 the list WEAKS. We need this information so that if a regular
4381 object refers to a symbol defined weakly in a dynamic object, the
4382 real symbol in the dynamic object is also put in the dynamic
4383 symbols; we also must arrange for both symbols to point to the
4384 same memory location. We could handle the general case of symbol
4385 aliasing, but a general symbol alias can only be generated in
4386 assembler code, handling it correctly would be very time
4387 consuming, and other ELF linkers don't handle general aliasing
4391 struct elf_link_hash_entry
**hpp
;
4392 struct elf_link_hash_entry
**hppend
;
4393 struct elf_link_hash_entry
**sorted_sym_hash
;
4394 struct elf_link_hash_entry
*h
;
4397 /* Since we have to search the whole symbol list for each weak
4398 defined symbol, search time for N weak defined symbols will be
4399 O(N^2). Binary search will cut it down to O(NlogN). */
4400 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4401 sorted_sym_hash
= bfd_malloc (amt
);
4402 if (sorted_sym_hash
== NULL
)
4404 sym_hash
= sorted_sym_hash
;
4405 hpp
= elf_sym_hashes (abfd
);
4406 hppend
= hpp
+ extsymcount
;
4408 for (; hpp
< hppend
; hpp
++)
4412 && h
->root
.type
== bfd_link_hash_defined
4413 && h
->type
!= STT_FUNC
)
4421 qsort (sorted_sym_hash
, sym_count
,
4422 sizeof (struct elf_link_hash_entry
*),
4425 while (weaks
!= NULL
)
4427 struct elf_link_hash_entry
*hlook
;
4434 weaks
= hlook
->u
.weakdef
;
4435 hlook
->u
.weakdef
= NULL
;
4437 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4438 || hlook
->root
.type
== bfd_link_hash_defweak
4439 || hlook
->root
.type
== bfd_link_hash_common
4440 || hlook
->root
.type
== bfd_link_hash_indirect
);
4441 slook
= hlook
->root
.u
.def
.section
;
4442 vlook
= hlook
->root
.u
.def
.value
;
4449 bfd_signed_vma vdiff
;
4451 h
= sorted_sym_hash
[idx
];
4452 vdiff
= vlook
- h
->root
.u
.def
.value
;
4459 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4472 /* We didn't find a value/section match. */
4476 for (i
= ilook
; i
< sym_count
; i
++)
4478 h
= sorted_sym_hash
[i
];
4480 /* Stop if value or section doesn't match. */
4481 if (h
->root
.u
.def
.value
!= vlook
4482 || h
->root
.u
.def
.section
!= slook
)
4484 else if (h
!= hlook
)
4486 hlook
->u
.weakdef
= h
;
4488 /* If the weak definition is in the list of dynamic
4489 symbols, make sure the real definition is put
4491 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4493 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4497 /* If the real definition is in the list of dynamic
4498 symbols, make sure the weak definition is put
4499 there as well. If we don't do this, then the
4500 dynamic loader might not merge the entries for the
4501 real definition and the weak definition. */
4502 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4504 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4512 free (sorted_sym_hash
);
4515 if (bed
->check_directives
)
4516 (*bed
->check_directives
) (abfd
, info
);
4518 /* If this object is the same format as the output object, and it is
4519 not a shared library, then let the backend look through the
4522 This is required to build global offset table entries and to
4523 arrange for dynamic relocs. It is not required for the
4524 particular common case of linking non PIC code, even when linking
4525 against shared libraries, but unfortunately there is no way of
4526 knowing whether an object file has been compiled PIC or not.
4527 Looking through the relocs is not particularly time consuming.
4528 The problem is that we must either (1) keep the relocs in memory,
4529 which causes the linker to require additional runtime memory or
4530 (2) read the relocs twice from the input file, which wastes time.
4531 This would be a good case for using mmap.
4533 I have no idea how to handle linking PIC code into a file of a
4534 different format. It probably can't be done. */
4536 && is_elf_hash_table (htab
)
4537 && htab
->root
.creator
== abfd
->xvec
4538 && bed
->check_relocs
!= NULL
)
4542 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4544 Elf_Internal_Rela
*internal_relocs
;
4547 if ((o
->flags
& SEC_RELOC
) == 0
4548 || o
->reloc_count
== 0
4549 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4550 && (o
->flags
& SEC_DEBUGGING
) != 0)
4551 || bfd_is_abs_section (o
->output_section
))
4554 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4556 if (internal_relocs
== NULL
)
4559 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4561 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4562 free (internal_relocs
);
4569 /* If this is a non-traditional link, try to optimize the handling
4570 of the .stab/.stabstr sections. */
4572 && ! info
->traditional_format
4573 && is_elf_hash_table (htab
)
4574 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4578 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4579 if (stabstr
!= NULL
)
4581 bfd_size_type string_offset
= 0;
4584 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4585 if (CONST_STRNEQ (stab
->name
, ".stab")
4586 && (!stab
->name
[5] ||
4587 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4588 && (stab
->flags
& SEC_MERGE
) == 0
4589 && !bfd_is_abs_section (stab
->output_section
))
4591 struct bfd_elf_section_data
*secdata
;
4593 secdata
= elf_section_data (stab
);
4594 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4595 stabstr
, &secdata
->sec_info
,
4598 if (secdata
->sec_info
)
4599 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4604 if (is_elf_hash_table (htab
) && add_needed
)
4606 /* Add this bfd to the loaded list. */
4607 struct elf_link_loaded_list
*n
;
4609 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4613 n
->next
= htab
->loaded
;
4620 if (old_tab
!= NULL
)
4622 if (nondeflt_vers
!= NULL
)
4623 free (nondeflt_vers
);
4624 if (extversym
!= NULL
)
4627 if (isymbuf
!= NULL
)
4633 /* Return the linker hash table entry of a symbol that might be
4634 satisfied by an archive symbol. Return -1 on error. */
4636 struct elf_link_hash_entry
*
4637 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4638 struct bfd_link_info
*info
,
4641 struct elf_link_hash_entry
*h
;
4645 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4649 /* If this is a default version (the name contains @@), look up the
4650 symbol again with only one `@' as well as without the version.
4651 The effect is that references to the symbol with and without the
4652 version will be matched by the default symbol in the archive. */
4654 p
= strchr (name
, ELF_VER_CHR
);
4655 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4658 /* First check with only one `@'. */
4659 len
= strlen (name
);
4660 copy
= bfd_alloc (abfd
, len
);
4662 return (struct elf_link_hash_entry
*) 0 - 1;
4664 first
= p
- name
+ 1;
4665 memcpy (copy
, name
, first
);
4666 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4668 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4671 /* We also need to check references to the symbol without the
4673 copy
[first
- 1] = '\0';
4674 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4675 FALSE
, FALSE
, FALSE
);
4678 bfd_release (abfd
, copy
);
4682 /* Add symbols from an ELF archive file to the linker hash table. We
4683 don't use _bfd_generic_link_add_archive_symbols because of a
4684 problem which arises on UnixWare. The UnixWare libc.so is an
4685 archive which includes an entry libc.so.1 which defines a bunch of
4686 symbols. The libc.so archive also includes a number of other
4687 object files, which also define symbols, some of which are the same
4688 as those defined in libc.so.1. Correct linking requires that we
4689 consider each object file in turn, and include it if it defines any
4690 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4691 this; it looks through the list of undefined symbols, and includes
4692 any object file which defines them. When this algorithm is used on
4693 UnixWare, it winds up pulling in libc.so.1 early and defining a
4694 bunch of symbols. This means that some of the other objects in the
4695 archive are not included in the link, which is incorrect since they
4696 precede libc.so.1 in the archive.
4698 Fortunately, ELF archive handling is simpler than that done by
4699 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4700 oddities. In ELF, if we find a symbol in the archive map, and the
4701 symbol is currently undefined, we know that we must pull in that
4704 Unfortunately, we do have to make multiple passes over the symbol
4705 table until nothing further is resolved. */
4708 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4711 bfd_boolean
*defined
= NULL
;
4712 bfd_boolean
*included
= NULL
;
4716 const struct elf_backend_data
*bed
;
4717 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4718 (bfd
*, struct bfd_link_info
*, const char *);
4720 if (! bfd_has_map (abfd
))
4722 /* An empty archive is a special case. */
4723 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4725 bfd_set_error (bfd_error_no_armap
);
4729 /* Keep track of all symbols we know to be already defined, and all
4730 files we know to be already included. This is to speed up the
4731 second and subsequent passes. */
4732 c
= bfd_ardata (abfd
)->symdef_count
;
4736 amt
*= sizeof (bfd_boolean
);
4737 defined
= bfd_zmalloc (amt
);
4738 included
= bfd_zmalloc (amt
);
4739 if (defined
== NULL
|| included
== NULL
)
4742 symdefs
= bfd_ardata (abfd
)->symdefs
;
4743 bed
= get_elf_backend_data (abfd
);
4744 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4757 symdefend
= symdef
+ c
;
4758 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4760 struct elf_link_hash_entry
*h
;
4762 struct bfd_link_hash_entry
*undefs_tail
;
4765 if (defined
[i
] || included
[i
])
4767 if (symdef
->file_offset
== last
)
4773 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4774 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4780 if (h
->root
.type
== bfd_link_hash_common
)
4782 /* We currently have a common symbol. The archive map contains
4783 a reference to this symbol, so we may want to include it. We
4784 only want to include it however, if this archive element
4785 contains a definition of the symbol, not just another common
4788 Unfortunately some archivers (including GNU ar) will put
4789 declarations of common symbols into their archive maps, as
4790 well as real definitions, so we cannot just go by the archive
4791 map alone. Instead we must read in the element's symbol
4792 table and check that to see what kind of symbol definition
4794 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4797 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4799 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4804 /* We need to include this archive member. */
4805 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4806 if (element
== NULL
)
4809 if (! bfd_check_format (element
, bfd_object
))
4812 /* Doublecheck that we have not included this object
4813 already--it should be impossible, but there may be
4814 something wrong with the archive. */
4815 if (element
->archive_pass
!= 0)
4817 bfd_set_error (bfd_error_bad_value
);
4820 element
->archive_pass
= 1;
4822 undefs_tail
= info
->hash
->undefs_tail
;
4824 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4827 if (! bfd_link_add_symbols (element
, info
))
4830 /* If there are any new undefined symbols, we need to make
4831 another pass through the archive in order to see whether
4832 they can be defined. FIXME: This isn't perfect, because
4833 common symbols wind up on undefs_tail and because an
4834 undefined symbol which is defined later on in this pass
4835 does not require another pass. This isn't a bug, but it
4836 does make the code less efficient than it could be. */
4837 if (undefs_tail
!= info
->hash
->undefs_tail
)
4840 /* Look backward to mark all symbols from this object file
4841 which we have already seen in this pass. */
4845 included
[mark
] = TRUE
;
4850 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4852 /* We mark subsequent symbols from this object file as we go
4853 on through the loop. */
4854 last
= symdef
->file_offset
;
4865 if (defined
!= NULL
)
4867 if (included
!= NULL
)
4872 /* Given an ELF BFD, add symbols to the global hash table as
4876 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4878 switch (bfd_get_format (abfd
))
4881 return elf_link_add_object_symbols (abfd
, info
);
4883 return elf_link_add_archive_symbols (abfd
, info
);
4885 bfd_set_error (bfd_error_wrong_format
);
4890 /* This function will be called though elf_link_hash_traverse to store
4891 all hash value of the exported symbols in an array. */
4894 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4896 unsigned long **valuep
= data
;
4902 if (h
->root
.type
== bfd_link_hash_warning
)
4903 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4905 /* Ignore indirect symbols. These are added by the versioning code. */
4906 if (h
->dynindx
== -1)
4909 name
= h
->root
.root
.string
;
4910 p
= strchr (name
, ELF_VER_CHR
);
4913 alc
= bfd_malloc (p
- name
+ 1);
4914 memcpy (alc
, name
, p
- name
);
4915 alc
[p
- name
] = '\0';
4919 /* Compute the hash value. */
4920 ha
= bfd_elf_hash (name
);
4922 /* Store the found hash value in the array given as the argument. */
4925 /* And store it in the struct so that we can put it in the hash table
4927 h
->u
.elf_hash_value
= ha
;
4935 struct collect_gnu_hash_codes
4938 const struct elf_backend_data
*bed
;
4939 unsigned long int nsyms
;
4940 unsigned long int maskbits
;
4941 unsigned long int *hashcodes
;
4942 unsigned long int *hashval
;
4943 unsigned long int *indx
;
4944 unsigned long int *counts
;
4947 long int min_dynindx
;
4948 unsigned long int bucketcount
;
4949 unsigned long int symindx
;
4950 long int local_indx
;
4951 long int shift1
, shift2
;
4952 unsigned long int mask
;
4955 /* This function will be called though elf_link_hash_traverse to store
4956 all hash value of the exported symbols in an array. */
4959 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4961 struct collect_gnu_hash_codes
*s
= data
;
4967 if (h
->root
.type
== bfd_link_hash_warning
)
4968 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4970 /* Ignore indirect symbols. These are added by the versioning code. */
4971 if (h
->dynindx
== -1)
4974 /* Ignore also local symbols and undefined symbols. */
4975 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4978 name
= h
->root
.root
.string
;
4979 p
= strchr (name
, ELF_VER_CHR
);
4982 alc
= bfd_malloc (p
- name
+ 1);
4983 memcpy (alc
, name
, p
- name
);
4984 alc
[p
- name
] = '\0';
4988 /* Compute the hash value. */
4989 ha
= bfd_elf_gnu_hash (name
);
4991 /* Store the found hash value in the array for compute_bucket_count,
4992 and also for .dynsym reordering purposes. */
4993 s
->hashcodes
[s
->nsyms
] = ha
;
4994 s
->hashval
[h
->dynindx
] = ha
;
4996 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
4997 s
->min_dynindx
= h
->dynindx
;
5005 /* This function will be called though elf_link_hash_traverse to do
5006 final dynaminc symbol renumbering. */
5009 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5011 struct collect_gnu_hash_codes
*s
= data
;
5012 unsigned long int bucket
;
5013 unsigned long int val
;
5015 if (h
->root
.type
== bfd_link_hash_warning
)
5016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5018 /* Ignore indirect symbols. */
5019 if (h
->dynindx
== -1)
5022 /* Ignore also local symbols and undefined symbols. */
5023 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5025 if (h
->dynindx
>= s
->min_dynindx
)
5026 h
->dynindx
= s
->local_indx
++;
5030 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5031 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5032 & ((s
->maskbits
>> s
->shift1
) - 1);
5033 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5035 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5036 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5037 if (s
->counts
[bucket
] == 1)
5038 /* Last element terminates the chain. */
5040 bfd_put_32 (s
->output_bfd
, val
,
5041 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5042 --s
->counts
[bucket
];
5043 h
->dynindx
= s
->indx
[bucket
]++;
5047 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5050 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5052 return !(h
->forced_local
5053 || h
->root
.type
== bfd_link_hash_undefined
5054 || h
->root
.type
== bfd_link_hash_undefweak
5055 || ((h
->root
.type
== bfd_link_hash_defined
5056 || h
->root
.type
== bfd_link_hash_defweak
)
5057 && h
->root
.u
.def
.section
->output_section
== NULL
));
5060 /* Array used to determine the number of hash table buckets to use
5061 based on the number of symbols there are. If there are fewer than
5062 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5063 fewer than 37 we use 17 buckets, and so forth. We never use more
5064 than 32771 buckets. */
5066 static const size_t elf_buckets
[] =
5068 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5072 /* Compute bucket count for hashing table. We do not use a static set
5073 of possible tables sizes anymore. Instead we determine for all
5074 possible reasonable sizes of the table the outcome (i.e., the
5075 number of collisions etc) and choose the best solution. The
5076 weighting functions are not too simple to allow the table to grow
5077 without bounds. Instead one of the weighting factors is the size.
5078 Therefore the result is always a good payoff between few collisions
5079 (= short chain lengths) and table size. */
5081 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5082 unsigned long int nsyms
, int gnu_hash
)
5084 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5085 size_t best_size
= 0;
5086 unsigned long int i
;
5089 /* We have a problem here. The following code to optimize the table
5090 size requires an integer type with more the 32 bits. If
5091 BFD_HOST_U_64_BIT is set we know about such a type. */
5092 #ifdef BFD_HOST_U_64_BIT
5097 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5098 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5099 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5100 unsigned long int *counts
;
5102 /* Possible optimization parameters: if we have NSYMS symbols we say
5103 that the hashing table must at least have NSYMS/4 and at most
5105 minsize
= nsyms
/ 4;
5108 best_size
= maxsize
= nsyms
* 2;
5113 if ((best_size
& 31) == 0)
5117 /* Create array where we count the collisions in. We must use bfd_malloc
5118 since the size could be large. */
5120 amt
*= sizeof (unsigned long int);
5121 counts
= bfd_malloc (amt
);
5125 /* Compute the "optimal" size for the hash table. The criteria is a
5126 minimal chain length. The minor criteria is (of course) the size
5128 for (i
= minsize
; i
< maxsize
; ++i
)
5130 /* Walk through the array of hashcodes and count the collisions. */
5131 BFD_HOST_U_64_BIT max
;
5132 unsigned long int j
;
5133 unsigned long int fact
;
5135 if (gnu_hash
&& (i
& 31) == 0)
5138 memset (counts
, '\0', i
* sizeof (unsigned long int));
5140 /* Determine how often each hash bucket is used. */
5141 for (j
= 0; j
< nsyms
; ++j
)
5142 ++counts
[hashcodes
[j
] % i
];
5144 /* For the weight function we need some information about the
5145 pagesize on the target. This is information need not be 100%
5146 accurate. Since this information is not available (so far) we
5147 define it here to a reasonable default value. If it is crucial
5148 to have a better value some day simply define this value. */
5149 # ifndef BFD_TARGET_PAGESIZE
5150 # define BFD_TARGET_PAGESIZE (4096)
5153 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5155 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5158 /* Variant 1: optimize for short chains. We add the squares
5159 of all the chain lengths (which favors many small chain
5160 over a few long chains). */
5161 for (j
= 0; j
< i
; ++j
)
5162 max
+= counts
[j
] * counts
[j
];
5164 /* This adds penalties for the overall size of the table. */
5165 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5168 /* Variant 2: Optimize a lot more for small table. Here we
5169 also add squares of the size but we also add penalties for
5170 empty slots (the +1 term). */
5171 for (j
= 0; j
< i
; ++j
)
5172 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5174 /* The overall size of the table is considered, but not as
5175 strong as in variant 1, where it is squared. */
5176 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5180 /* Compare with current best results. */
5181 if (max
< best_chlen
)
5191 #endif /* defined (BFD_HOST_U_64_BIT) */
5193 /* This is the fallback solution if no 64bit type is available or if we
5194 are not supposed to spend much time on optimizations. We select the
5195 bucket count using a fixed set of numbers. */
5196 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5198 best_size
= elf_buckets
[i
];
5199 if (nsyms
< elf_buckets
[i
+ 1])
5202 if (gnu_hash
&& best_size
< 2)
5209 /* Set up the sizes and contents of the ELF dynamic sections. This is
5210 called by the ELF linker emulation before_allocation routine. We
5211 must set the sizes of the sections before the linker sets the
5212 addresses of the various sections. */
5215 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5218 const char *filter_shlib
,
5219 const char * const *auxiliary_filters
,
5220 struct bfd_link_info
*info
,
5221 asection
**sinterpptr
,
5222 struct bfd_elf_version_tree
*verdefs
)
5224 bfd_size_type soname_indx
;
5226 const struct elf_backend_data
*bed
;
5227 struct elf_assign_sym_version_info asvinfo
;
5231 soname_indx
= (bfd_size_type
) -1;
5233 if (!is_elf_hash_table (info
->hash
))
5236 elf_tdata (output_bfd
)->relro
= info
->relro
;
5237 if (info
->execstack
)
5238 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5239 else if (info
->noexecstack
)
5240 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5244 asection
*notesec
= NULL
;
5247 for (inputobj
= info
->input_bfds
;
5249 inputobj
= inputobj
->link_next
)
5253 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5255 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5258 if (s
->flags
& SEC_CODE
)
5267 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5268 if (exec
&& info
->relocatable
5269 && notesec
->output_section
!= bfd_abs_section_ptr
)
5270 notesec
->output_section
->flags
|= SEC_CODE
;
5274 /* Any syms created from now on start with -1 in
5275 got.refcount/offset and plt.refcount/offset. */
5276 elf_hash_table (info
)->init_got_refcount
5277 = elf_hash_table (info
)->init_got_offset
;
5278 elf_hash_table (info
)->init_plt_refcount
5279 = elf_hash_table (info
)->init_plt_offset
;
5281 /* The backend may have to create some sections regardless of whether
5282 we're dynamic or not. */
5283 bed
= get_elf_backend_data (output_bfd
);
5284 if (bed
->elf_backend_always_size_sections
5285 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5288 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5291 dynobj
= elf_hash_table (info
)->dynobj
;
5293 /* If there were no dynamic objects in the link, there is nothing to
5298 if (elf_hash_table (info
)->dynamic_sections_created
)
5300 struct elf_info_failed eif
;
5301 struct elf_link_hash_entry
*h
;
5303 struct bfd_elf_version_tree
*t
;
5304 struct bfd_elf_version_expr
*d
;
5306 bfd_boolean all_defined
;
5308 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5309 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5313 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5315 if (soname_indx
== (bfd_size_type
) -1
5316 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5322 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5324 info
->flags
|= DF_SYMBOLIC
;
5331 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5333 if (indx
== (bfd_size_type
) -1
5334 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5337 if (info
->new_dtags
)
5339 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5340 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5345 if (filter_shlib
!= NULL
)
5349 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5350 filter_shlib
, TRUE
);
5351 if (indx
== (bfd_size_type
) -1
5352 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5356 if (auxiliary_filters
!= NULL
)
5358 const char * const *p
;
5360 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5364 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5366 if (indx
== (bfd_size_type
) -1
5367 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5373 eif
.verdefs
= verdefs
;
5376 /* If we are supposed to export all symbols into the dynamic symbol
5377 table (this is not the normal case), then do so. */
5378 if (info
->export_dynamic
5379 || (info
->executable
&& info
->dynamic
))
5381 elf_link_hash_traverse (elf_hash_table (info
),
5382 _bfd_elf_export_symbol
,
5388 /* Make all global versions with definition. */
5389 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5390 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5391 if (!d
->symver
&& d
->symbol
)
5393 const char *verstr
, *name
;
5394 size_t namelen
, verlen
, newlen
;
5396 struct elf_link_hash_entry
*newh
;
5399 namelen
= strlen (name
);
5401 verlen
= strlen (verstr
);
5402 newlen
= namelen
+ verlen
+ 3;
5404 newname
= bfd_malloc (newlen
);
5405 if (newname
== NULL
)
5407 memcpy (newname
, name
, namelen
);
5409 /* Check the hidden versioned definition. */
5410 p
= newname
+ namelen
;
5412 memcpy (p
, verstr
, verlen
+ 1);
5413 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5414 newname
, FALSE
, FALSE
,
5417 || (newh
->root
.type
!= bfd_link_hash_defined
5418 && newh
->root
.type
!= bfd_link_hash_defweak
))
5420 /* Check the default versioned definition. */
5422 memcpy (p
, verstr
, verlen
+ 1);
5423 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5424 newname
, FALSE
, FALSE
,
5429 /* Mark this version if there is a definition and it is
5430 not defined in a shared object. */
5432 && !newh
->def_dynamic
5433 && (newh
->root
.type
== bfd_link_hash_defined
5434 || newh
->root
.type
== bfd_link_hash_defweak
))
5438 /* Attach all the symbols to their version information. */
5439 asvinfo
.output_bfd
= output_bfd
;
5440 asvinfo
.info
= info
;
5441 asvinfo
.verdefs
= verdefs
;
5442 asvinfo
.failed
= FALSE
;
5444 elf_link_hash_traverse (elf_hash_table (info
),
5445 _bfd_elf_link_assign_sym_version
,
5450 if (!info
->allow_undefined_version
)
5452 /* Check if all global versions have a definition. */
5454 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5455 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5456 if (!d
->symver
&& !d
->script
)
5458 (*_bfd_error_handler
)
5459 (_("%s: undefined version: %s"),
5460 d
->pattern
, t
->name
);
5461 all_defined
= FALSE
;
5466 bfd_set_error (bfd_error_bad_value
);
5471 /* Find all symbols which were defined in a dynamic object and make
5472 the backend pick a reasonable value for them. */
5473 elf_link_hash_traverse (elf_hash_table (info
),
5474 _bfd_elf_adjust_dynamic_symbol
,
5479 /* Add some entries to the .dynamic section. We fill in some of the
5480 values later, in bfd_elf_final_link, but we must add the entries
5481 now so that we know the final size of the .dynamic section. */
5483 /* If there are initialization and/or finalization functions to
5484 call then add the corresponding DT_INIT/DT_FINI entries. */
5485 h
= (info
->init_function
5486 ? elf_link_hash_lookup (elf_hash_table (info
),
5487 info
->init_function
, FALSE
,
5494 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5497 h
= (info
->fini_function
5498 ? elf_link_hash_lookup (elf_hash_table (info
),
5499 info
->fini_function
, FALSE
,
5506 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5510 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5511 if (s
!= NULL
&& s
->linker_has_input
)
5513 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5514 if (! info
->executable
)
5519 for (sub
= info
->input_bfds
; sub
!= NULL
;
5520 sub
= sub
->link_next
)
5521 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5522 if (elf_section_data (o
)->this_hdr
.sh_type
5523 == SHT_PREINIT_ARRAY
)
5525 (*_bfd_error_handler
)
5526 (_("%B: .preinit_array section is not allowed in DSO"),
5531 bfd_set_error (bfd_error_nonrepresentable_section
);
5535 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5536 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5539 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5540 if (s
!= NULL
&& s
->linker_has_input
)
5542 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5543 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5546 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5547 if (s
!= NULL
&& s
->linker_has_input
)
5549 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5550 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5554 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5555 /* If .dynstr is excluded from the link, we don't want any of
5556 these tags. Strictly, we should be checking each section
5557 individually; This quick check covers for the case where
5558 someone does a /DISCARD/ : { *(*) }. */
5559 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5561 bfd_size_type strsize
;
5563 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5564 if ((info
->emit_hash
5565 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5566 || (info
->emit_gnu_hash
5567 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5568 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5569 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5570 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5571 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5572 bed
->s
->sizeof_sym
))
5577 /* The backend must work out the sizes of all the other dynamic
5579 if (bed
->elf_backend_size_dynamic_sections
5580 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5583 if (elf_hash_table (info
)->dynamic_sections_created
)
5585 unsigned long section_sym_count
;
5588 /* Set up the version definition section. */
5589 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5590 BFD_ASSERT (s
!= NULL
);
5592 /* We may have created additional version definitions if we are
5593 just linking a regular application. */
5594 verdefs
= asvinfo
.verdefs
;
5596 /* Skip anonymous version tag. */
5597 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5598 verdefs
= verdefs
->next
;
5600 if (verdefs
== NULL
&& !info
->create_default_symver
)
5601 s
->flags
|= SEC_EXCLUDE
;
5606 struct bfd_elf_version_tree
*t
;
5608 Elf_Internal_Verdef def
;
5609 Elf_Internal_Verdaux defaux
;
5610 struct bfd_link_hash_entry
*bh
;
5611 struct elf_link_hash_entry
*h
;
5617 /* Make space for the base version. */
5618 size
+= sizeof (Elf_External_Verdef
);
5619 size
+= sizeof (Elf_External_Verdaux
);
5622 /* Make space for the default version. */
5623 if (info
->create_default_symver
)
5625 size
+= sizeof (Elf_External_Verdef
);
5629 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5631 struct bfd_elf_version_deps
*n
;
5633 size
+= sizeof (Elf_External_Verdef
);
5634 size
+= sizeof (Elf_External_Verdaux
);
5637 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5638 size
+= sizeof (Elf_External_Verdaux
);
5642 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5643 if (s
->contents
== NULL
&& s
->size
!= 0)
5646 /* Fill in the version definition section. */
5650 def
.vd_version
= VER_DEF_CURRENT
;
5651 def
.vd_flags
= VER_FLG_BASE
;
5654 if (info
->create_default_symver
)
5656 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5657 def
.vd_next
= sizeof (Elf_External_Verdef
);
5661 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5662 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5663 + sizeof (Elf_External_Verdaux
));
5666 if (soname_indx
!= (bfd_size_type
) -1)
5668 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5670 def
.vd_hash
= bfd_elf_hash (soname
);
5671 defaux
.vda_name
= soname_indx
;
5678 name
= lbasename (output_bfd
->filename
);
5679 def
.vd_hash
= bfd_elf_hash (name
);
5680 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5682 if (indx
== (bfd_size_type
) -1)
5684 defaux
.vda_name
= indx
;
5686 defaux
.vda_next
= 0;
5688 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5689 (Elf_External_Verdef
*) p
);
5690 p
+= sizeof (Elf_External_Verdef
);
5691 if (info
->create_default_symver
)
5693 /* Add a symbol representing this version. */
5695 if (! (_bfd_generic_link_add_one_symbol
5696 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5698 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5700 h
= (struct elf_link_hash_entry
*) bh
;
5703 h
->type
= STT_OBJECT
;
5704 h
->verinfo
.vertree
= NULL
;
5706 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5709 /* Create a duplicate of the base version with the same
5710 aux block, but different flags. */
5713 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5715 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5716 + sizeof (Elf_External_Verdaux
));
5719 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5720 (Elf_External_Verdef
*) p
);
5721 p
+= sizeof (Elf_External_Verdef
);
5723 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5724 (Elf_External_Verdaux
*) p
);
5725 p
+= sizeof (Elf_External_Verdaux
);
5727 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5730 struct bfd_elf_version_deps
*n
;
5733 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5736 /* Add a symbol representing this version. */
5738 if (! (_bfd_generic_link_add_one_symbol
5739 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5741 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5743 h
= (struct elf_link_hash_entry
*) bh
;
5746 h
->type
= STT_OBJECT
;
5747 h
->verinfo
.vertree
= t
;
5749 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5752 def
.vd_version
= VER_DEF_CURRENT
;
5754 if (t
->globals
.list
== NULL
5755 && t
->locals
.list
== NULL
5757 def
.vd_flags
|= VER_FLG_WEAK
;
5758 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5759 def
.vd_cnt
= cdeps
+ 1;
5760 def
.vd_hash
= bfd_elf_hash (t
->name
);
5761 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5763 if (t
->next
!= NULL
)
5764 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5765 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5767 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5768 (Elf_External_Verdef
*) p
);
5769 p
+= sizeof (Elf_External_Verdef
);
5771 defaux
.vda_name
= h
->dynstr_index
;
5772 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5774 defaux
.vda_next
= 0;
5775 if (t
->deps
!= NULL
)
5776 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5777 t
->name_indx
= defaux
.vda_name
;
5779 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5780 (Elf_External_Verdaux
*) p
);
5781 p
+= sizeof (Elf_External_Verdaux
);
5783 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5785 if (n
->version_needed
== NULL
)
5787 /* This can happen if there was an error in the
5789 defaux
.vda_name
= 0;
5793 defaux
.vda_name
= n
->version_needed
->name_indx
;
5794 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5797 if (n
->next
== NULL
)
5798 defaux
.vda_next
= 0;
5800 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5802 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5803 (Elf_External_Verdaux
*) p
);
5804 p
+= sizeof (Elf_External_Verdaux
);
5808 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5809 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5812 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5815 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5817 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5820 else if (info
->flags
& DF_BIND_NOW
)
5822 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5828 if (info
->executable
)
5829 info
->flags_1
&= ~ (DF_1_INITFIRST
5832 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5836 /* Work out the size of the version reference section. */
5838 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5839 BFD_ASSERT (s
!= NULL
);
5841 struct elf_find_verdep_info sinfo
;
5843 sinfo
.output_bfd
= output_bfd
;
5845 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5846 if (sinfo
.vers
== 0)
5848 sinfo
.failed
= FALSE
;
5850 elf_link_hash_traverse (elf_hash_table (info
),
5851 _bfd_elf_link_find_version_dependencies
,
5854 if (elf_tdata (output_bfd
)->verref
== NULL
)
5855 s
->flags
|= SEC_EXCLUDE
;
5858 Elf_Internal_Verneed
*t
;
5863 /* Build the version definition section. */
5866 for (t
= elf_tdata (output_bfd
)->verref
;
5870 Elf_Internal_Vernaux
*a
;
5872 size
+= sizeof (Elf_External_Verneed
);
5874 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5875 size
+= sizeof (Elf_External_Vernaux
);
5879 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5880 if (s
->contents
== NULL
)
5884 for (t
= elf_tdata (output_bfd
)->verref
;
5889 Elf_Internal_Vernaux
*a
;
5893 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5896 t
->vn_version
= VER_NEED_CURRENT
;
5898 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5899 elf_dt_name (t
->vn_bfd
) != NULL
5900 ? elf_dt_name (t
->vn_bfd
)
5901 : lbasename (t
->vn_bfd
->filename
),
5903 if (indx
== (bfd_size_type
) -1)
5906 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5907 if (t
->vn_nextref
== NULL
)
5910 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5911 + caux
* sizeof (Elf_External_Vernaux
));
5913 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5914 (Elf_External_Verneed
*) p
);
5915 p
+= sizeof (Elf_External_Verneed
);
5917 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5919 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5920 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5921 a
->vna_nodename
, FALSE
);
5922 if (indx
== (bfd_size_type
) -1)
5925 if (a
->vna_nextptr
== NULL
)
5928 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5930 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5931 (Elf_External_Vernaux
*) p
);
5932 p
+= sizeof (Elf_External_Vernaux
);
5936 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5937 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5940 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5944 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5945 && elf_tdata (output_bfd
)->cverdefs
== 0)
5946 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5947 §ion_sym_count
) == 0)
5949 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5950 s
->flags
|= SEC_EXCLUDE
;
5956 /* Find the first non-excluded output section. We'll use its
5957 section symbol for some emitted relocs. */
5959 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
5963 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5964 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
5965 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5967 elf_hash_table (info
)->text_index_section
= s
;
5972 /* Find two non-excluded output sections, one for code, one for data.
5973 We'll use their section symbols for some emitted relocs. */
5975 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5979 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5980 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
5981 == (SEC_ALLOC
| SEC_READONLY
))
5982 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5984 elf_hash_table (info
)->text_index_section
= s
;
5988 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5989 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
5990 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5992 elf_hash_table (info
)->data_index_section
= s
;
5996 if (elf_hash_table (info
)->text_index_section
== NULL
)
5997 elf_hash_table (info
)->text_index_section
5998 = elf_hash_table (info
)->data_index_section
;
6002 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6004 const struct elf_backend_data
*bed
;
6006 if (!is_elf_hash_table (info
->hash
))
6009 bed
= get_elf_backend_data (output_bfd
);
6010 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6012 if (elf_hash_table (info
)->dynamic_sections_created
)
6016 bfd_size_type dynsymcount
;
6017 unsigned long section_sym_count
;
6018 unsigned int dtagcount
;
6020 dynobj
= elf_hash_table (info
)->dynobj
;
6022 /* Assign dynsym indicies. In a shared library we generate a
6023 section symbol for each output section, which come first.
6024 Next come all of the back-end allocated local dynamic syms,
6025 followed by the rest of the global symbols. */
6027 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6028 §ion_sym_count
);
6030 /* Work out the size of the symbol version section. */
6031 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6032 BFD_ASSERT (s
!= NULL
);
6033 if (dynsymcount
!= 0
6034 && (s
->flags
& SEC_EXCLUDE
) == 0)
6036 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6037 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6038 if (s
->contents
== NULL
)
6041 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6045 /* Set the size of the .dynsym and .hash sections. We counted
6046 the number of dynamic symbols in elf_link_add_object_symbols.
6047 We will build the contents of .dynsym and .hash when we build
6048 the final symbol table, because until then we do not know the
6049 correct value to give the symbols. We built the .dynstr
6050 section as we went along in elf_link_add_object_symbols. */
6051 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6052 BFD_ASSERT (s
!= NULL
);
6053 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6055 if (dynsymcount
!= 0)
6057 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6058 if (s
->contents
== NULL
)
6061 /* The first entry in .dynsym is a dummy symbol.
6062 Clear all the section syms, in case we don't output them all. */
6063 ++section_sym_count
;
6064 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6067 elf_hash_table (info
)->bucketcount
= 0;
6069 /* Compute the size of the hashing table. As a side effect this
6070 computes the hash values for all the names we export. */
6071 if (info
->emit_hash
)
6073 unsigned long int *hashcodes
;
6074 unsigned long int *hashcodesp
;
6076 unsigned long int nsyms
;
6078 size_t hash_entry_size
;
6080 /* Compute the hash values for all exported symbols. At the same
6081 time store the values in an array so that we could use them for
6083 amt
= dynsymcount
* sizeof (unsigned long int);
6084 hashcodes
= bfd_malloc (amt
);
6085 if (hashcodes
== NULL
)
6087 hashcodesp
= hashcodes
;
6089 /* Put all hash values in HASHCODES. */
6090 elf_link_hash_traverse (elf_hash_table (info
),
6091 elf_collect_hash_codes
, &hashcodesp
);
6093 nsyms
= hashcodesp
- hashcodes
;
6095 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6098 if (bucketcount
== 0)
6101 elf_hash_table (info
)->bucketcount
= bucketcount
;
6103 s
= bfd_get_section_by_name (dynobj
, ".hash");
6104 BFD_ASSERT (s
!= NULL
);
6105 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6106 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6107 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6108 if (s
->contents
== NULL
)
6111 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6112 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6113 s
->contents
+ hash_entry_size
);
6116 if (info
->emit_gnu_hash
)
6119 unsigned char *contents
;
6120 struct collect_gnu_hash_codes cinfo
;
6124 memset (&cinfo
, 0, sizeof (cinfo
));
6126 /* Compute the hash values for all exported symbols. At the same
6127 time store the values in an array so that we could use them for
6129 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6130 cinfo
.hashcodes
= bfd_malloc (amt
);
6131 if (cinfo
.hashcodes
== NULL
)
6134 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6135 cinfo
.min_dynindx
= -1;
6136 cinfo
.output_bfd
= output_bfd
;
6139 /* Put all hash values in HASHCODES. */
6140 elf_link_hash_traverse (elf_hash_table (info
),
6141 elf_collect_gnu_hash_codes
, &cinfo
);
6144 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6146 if (bucketcount
== 0)
6148 free (cinfo
.hashcodes
);
6152 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6153 BFD_ASSERT (s
!= NULL
);
6155 if (cinfo
.nsyms
== 0)
6157 /* Empty .gnu.hash section is special. */
6158 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6159 free (cinfo
.hashcodes
);
6160 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6161 contents
= bfd_zalloc (output_bfd
, s
->size
);
6162 if (contents
== NULL
)
6164 s
->contents
= contents
;
6165 /* 1 empty bucket. */
6166 bfd_put_32 (output_bfd
, 1, contents
);
6167 /* SYMIDX above the special symbol 0. */
6168 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6169 /* Just one word for bitmask. */
6170 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6171 /* Only hash fn bloom filter. */
6172 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6173 /* No hashes are valid - empty bitmask. */
6174 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6175 /* No hashes in the only bucket. */
6176 bfd_put_32 (output_bfd
, 0,
6177 contents
+ 16 + bed
->s
->arch_size
/ 8);
6181 unsigned long int maskwords
, maskbitslog2
;
6182 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6184 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6185 if (maskbitslog2
< 3)
6187 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6188 maskbitslog2
= maskbitslog2
+ 3;
6190 maskbitslog2
= maskbitslog2
+ 2;
6191 if (bed
->s
->arch_size
== 64)
6193 if (maskbitslog2
== 5)
6199 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6200 cinfo
.shift2
= maskbitslog2
;
6201 cinfo
.maskbits
= 1 << maskbitslog2
;
6202 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6203 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6204 amt
+= maskwords
* sizeof (bfd_vma
);
6205 cinfo
.bitmask
= bfd_malloc (amt
);
6206 if (cinfo
.bitmask
== NULL
)
6208 free (cinfo
.hashcodes
);
6212 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6213 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6214 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6215 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6217 /* Determine how often each hash bucket is used. */
6218 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6219 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6220 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6222 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6223 if (cinfo
.counts
[i
] != 0)
6225 cinfo
.indx
[i
] = cnt
;
6226 cnt
+= cinfo
.counts
[i
];
6228 BFD_ASSERT (cnt
== dynsymcount
);
6229 cinfo
.bucketcount
= bucketcount
;
6230 cinfo
.local_indx
= cinfo
.min_dynindx
;
6232 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6233 s
->size
+= cinfo
.maskbits
/ 8;
6234 contents
= bfd_zalloc (output_bfd
, s
->size
);
6235 if (contents
== NULL
)
6237 free (cinfo
.bitmask
);
6238 free (cinfo
.hashcodes
);
6242 s
->contents
= contents
;
6243 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6244 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6245 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6246 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6247 contents
+= 16 + cinfo
.maskbits
/ 8;
6249 for (i
= 0; i
< bucketcount
; ++i
)
6251 if (cinfo
.counts
[i
] == 0)
6252 bfd_put_32 (output_bfd
, 0, contents
);
6254 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6258 cinfo
.contents
= contents
;
6260 /* Renumber dynamic symbols, populate .gnu.hash section. */
6261 elf_link_hash_traverse (elf_hash_table (info
),
6262 elf_renumber_gnu_hash_syms
, &cinfo
);
6264 contents
= s
->contents
+ 16;
6265 for (i
= 0; i
< maskwords
; ++i
)
6267 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6269 contents
+= bed
->s
->arch_size
/ 8;
6272 free (cinfo
.bitmask
);
6273 free (cinfo
.hashcodes
);
6277 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6278 BFD_ASSERT (s
!= NULL
);
6280 elf_finalize_dynstr (output_bfd
, info
);
6282 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6284 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6285 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6292 /* Final phase of ELF linker. */
6294 /* A structure we use to avoid passing large numbers of arguments. */
6296 struct elf_final_link_info
6298 /* General link information. */
6299 struct bfd_link_info
*info
;
6302 /* Symbol string table. */
6303 struct bfd_strtab_hash
*symstrtab
;
6304 /* .dynsym section. */
6305 asection
*dynsym_sec
;
6306 /* .hash section. */
6308 /* symbol version section (.gnu.version). */
6309 asection
*symver_sec
;
6310 /* Buffer large enough to hold contents of any section. */
6312 /* Buffer large enough to hold external relocs of any section. */
6313 void *external_relocs
;
6314 /* Buffer large enough to hold internal relocs of any section. */
6315 Elf_Internal_Rela
*internal_relocs
;
6316 /* Buffer large enough to hold external local symbols of any input
6318 bfd_byte
*external_syms
;
6319 /* And a buffer for symbol section indices. */
6320 Elf_External_Sym_Shndx
*locsym_shndx
;
6321 /* Buffer large enough to hold internal local symbols of any input
6323 Elf_Internal_Sym
*internal_syms
;
6324 /* Array large enough to hold a symbol index for each local symbol
6325 of any input BFD. */
6327 /* Array large enough to hold a section pointer for each local
6328 symbol of any input BFD. */
6329 asection
**sections
;
6330 /* Buffer to hold swapped out symbols. */
6332 /* And one for symbol section indices. */
6333 Elf_External_Sym_Shndx
*symshndxbuf
;
6334 /* Number of swapped out symbols in buffer. */
6335 size_t symbuf_count
;
6336 /* Number of symbols which fit in symbuf. */
6338 /* And same for symshndxbuf. */
6339 size_t shndxbuf_size
;
6342 /* This struct is used to pass information to elf_link_output_extsym. */
6344 struct elf_outext_info
6347 bfd_boolean localsyms
;
6348 struct elf_final_link_info
*finfo
;
6352 /* Support for evaluating a complex relocation.
6354 Complex relocations are generalized, self-describing relocations. The
6355 implementation of them consists of two parts: complex symbols, and the
6356 relocations themselves.
6358 The relocations are use a reserved elf-wide relocation type code (R_RELC
6359 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6360 information (start bit, end bit, word width, etc) into the addend. This
6361 information is extracted from CGEN-generated operand tables within gas.
6363 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6364 internal) representing prefix-notation expressions, including but not
6365 limited to those sorts of expressions normally encoded as addends in the
6366 addend field. The symbol mangling format is:
6369 | <unary-operator> ':' <node>
6370 | <binary-operator> ':' <node> ':' <node>
6373 <literal> := 's' <digits=N> ':' <N character symbol name>
6374 | 'S' <digits=N> ':' <N character section name>
6378 <binary-operator> := as in C
6379 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6382 set_symbol_value (bfd
* bfd_with_globals
,
6383 struct elf_final_link_info
* finfo
,
6387 bfd_boolean is_local
;
6388 Elf_Internal_Sym
* sym
;
6389 struct elf_link_hash_entry
** sym_hashes
;
6390 struct elf_link_hash_entry
* h
;
6392 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
6393 sym
= finfo
->internal_syms
+ symidx
;
6394 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
6398 /* It is a local symbol: move it to the
6399 "absolute" section and give it a value. */
6400 sym
->st_shndx
= SHN_ABS
;
6401 sym
->st_value
= val
;
6405 /* It is a global symbol: set its link type
6406 to "defined" and give it a value. */
6407 h
= sym_hashes
[symidx
];
6408 while (h
->root
.type
== bfd_link_hash_indirect
6409 || h
->root
.type
== bfd_link_hash_warning
)
6410 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6411 h
->root
.type
= bfd_link_hash_defined
;
6412 h
->root
.u
.def
.value
= val
;
6413 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6418 resolve_symbol (const char * name
,
6420 struct elf_final_link_info
* finfo
,
6424 Elf_Internal_Sym
* sym
;
6425 struct bfd_link_hash_entry
* global_entry
;
6426 const char * candidate
= NULL
;
6427 Elf_Internal_Shdr
* symtab_hdr
;
6428 asection
* sec
= NULL
;
6431 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6433 for (i
= 0; i
< locsymcount
; ++ i
)
6435 sym
= finfo
->internal_syms
+ i
;
6436 sec
= finfo
->sections
[i
];
6438 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
6441 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
6442 symtab_hdr
->sh_link
,
6445 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6446 name
, candidate
, (unsigned int)sym
->st_value
);
6448 if (candidate
&& strcmp (candidate
, name
) == 0)
6450 * result
= sym
->st_value
;
6452 if (sym
->st_shndx
> SHN_UNDEF
&&
6453 sym
->st_shndx
< SHN_LORESERVE
)
6456 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6457 sec
->output_section
->name
,
6458 (unsigned int)sec
->output_section
->vma
,
6459 (unsigned int)sec
->output_offset
);
6461 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
6464 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
6470 /* Hmm, haven't found it yet. perhaps it is a global. */
6471 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
6475 if (global_entry
->type
== bfd_link_hash_defined
6476 || global_entry
->type
== bfd_link_hash_defweak
)
6478 * result
= global_entry
->u
.def
.value
6479 + global_entry
->u
.def
.section
->output_section
->vma
6480 + global_entry
->u
.def
.section
->output_offset
;
6482 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6483 global_entry
->root
.string
, (unsigned int)*result
);
6488 if (global_entry
->type
== bfd_link_hash_common
)
6490 *result
= global_entry
->u
.def
.value
+
6491 bfd_com_section_ptr
->output_section
->vma
+
6492 bfd_com_section_ptr
->output_offset
;
6494 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6495 global_entry
->root
.string
, (unsigned int)*result
);
6504 resolve_section (const char * name
,
6505 asection
* sections
,
6511 for (curr
= sections
; curr
; curr
= curr
->next
)
6512 if (strcmp (curr
->name
, name
) == 0)
6514 *result
= curr
->vma
;
6518 /* Hmm. still haven't found it. try pseudo-section names. */
6519 for (curr
= sections
; curr
; curr
= curr
->next
)
6521 len
= strlen (curr
->name
);
6522 if (len
> strlen (name
))
6525 if (strncmp (curr
->name
, name
, len
) == 0)
6527 if (strncmp (".end", name
+ len
, 4) == 0)
6529 *result
= curr
->vma
+ curr
->size
;
6533 /* Insert more pseudo-section names here, if you like. */
6541 undefined_reference (const char * reftype
,
6544 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
6548 eval_symbol (bfd_vma
* result
,
6552 struct elf_final_link_info
* finfo
,
6554 bfd_vma section_offset
,
6562 const int bufsz
= 4096;
6563 char symbuf
[bufsz
];
6564 const char * symend
;
6565 bfd_boolean symbol_is_section
= FALSE
;
6570 if (len
< 1 || len
> bufsz
)
6572 bfd_set_error (bfd_error_invalid_operation
);
6579 * result
= addr
+ section_offset
;
6580 * advanced
= sym
+ 1;
6585 * result
= strtoul (sym
, advanced
, 16);
6589 symbol_is_section
= TRUE
;
6592 symlen
= strtol (sym
, &sym
, 10);
6593 ++ sym
; /* Skip the trailing ':'. */
6595 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
6597 bfd_set_error (bfd_error_invalid_operation
);
6601 memcpy (symbuf
, sym
, symlen
);
6602 symbuf
[symlen
] = '\0';
6603 * advanced
= sym
+ symlen
;
6605 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6606 the symbol as a section, or vice-versa. so we're pretty liberal in our
6607 interpretation here; section means "try section first", not "must be a
6608 section", and likewise with symbol. */
6610 if (symbol_is_section
)
6612 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
6613 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
6615 undefined_reference ("section", symbuf
);
6621 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
6622 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
6625 undefined_reference ("symbol", symbuf
);
6632 /* All that remains are operators. */
6634 #define UNARY_OP(op) \
6635 if (strncmp (sym, #op, strlen (#op)) == 0) \
6637 sym += strlen (#op); \
6640 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6641 section_offset, locsymcount, signed_p) \
6645 * result = op ((signed)a); \
6652 #define BINARY_OP(op) \
6653 if (strncmp (sym, #op, strlen (#op)) == 0) \
6655 sym += strlen (#op); \
6658 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6659 section_offset, locsymcount, signed_p) \
6663 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6664 section_offset, locsymcount, signed_p) \
6668 * result = ((signed) a) op ((signed) b); \
6670 * result = a op b; \
6699 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
6700 bfd_set_error (bfd_error_invalid_operation
);
6705 /* Entry point to evaluator, called from elf_link_input_bfd. */
6708 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
6709 struct elf_final_link_info
* finfo
,
6712 const struct elf_backend_data
* bed
;
6713 Elf_Internal_Shdr
* symtab_hdr
;
6714 struct elf_link_hash_entry
** sym_hashes
;
6715 asection
* reloc_sec
;
6716 bfd_boolean result
= TRUE
;
6718 /* For each section, we're going to check and see if it has any
6719 complex relocations, and we're going to evaluate any of them
6722 if (finfo
->info
->relocatable
)
6725 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6726 sym_hashes
= elf_sym_hashes (input_bfd
);
6727 bed
= get_elf_backend_data (input_bfd
);
6729 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
6731 Elf_Internal_Rela
* internal_relocs
;
6734 /* This section was omitted from the link. */
6735 if (! reloc_sec
->linker_mark
)
6738 /* Only process sections containing relocs. */
6739 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
6742 if (reloc_sec
->reloc_count
== 0)
6745 /* Read in the relocs for this section. */
6747 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
6748 (Elf_Internal_Rela
*) NULL
,
6750 if (internal_relocs
== NULL
)
6753 for (i
= reloc_sec
->reloc_count
; i
--;)
6755 Elf_Internal_Rela
* rel
;
6757 unsigned long index
;
6758 Elf_Internal_Sym
* sym
;
6760 bfd_vma section_offset
;
6764 rel
= internal_relocs
+ i
;
6765 section_offset
= reloc_sec
->output_section
->vma
6766 + reloc_sec
->output_offset
;
6767 addr
= rel
->r_offset
;
6769 index
= ELF32_R_SYM (rel
->r_info
);
6770 if (bed
->s
->arch_size
== 64)
6773 if (index
== STN_UNDEF
)
6776 if (index
< locsymcount
)
6778 /* The symbol is local. */
6779 sym
= finfo
->internal_syms
+ index
;
6781 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6782 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
6783 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
6786 sym_name
= bfd_elf_string_from_elf_section
6787 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
6789 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
6793 /* The symbol is global. */
6794 struct elf_link_hash_entry
* h
;
6796 if (elf_bad_symtab (input_bfd
))
6799 h
= sym_hashes
[index
- locsymcount
];
6800 while ( h
->root
.type
== bfd_link_hash_indirect
6801 || h
->root
.type
== bfd_link_hash_warning
)
6802 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6804 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
6807 signed_p
= (h
->type
== STT_SRELC
);
6808 sym_name
= (char *) h
->root
.root
.string
;
6811 printf ("Encountered a complex symbol!");
6812 printf (" (input_bfd %s, section %s, reloc %ld\n",
6813 input_bfd
->filename
, reloc_sec
->name
, i
);
6814 printf (" symbol: idx %8.8lx, name %s\n",
6816 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6818 printf (" Evaluating '%s' ...\n ", sym_name
);
6820 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
6821 finfo
, addr
, section_offset
, locsymcount
,
6823 /* Symbol evaluated OK. Update to absolute value. */
6824 set_symbol_value (input_bfd
, finfo
, index
, result
);
6830 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
6831 free (internal_relocs
);
6834 /* If nothing went wrong, then we adjusted
6835 everything we wanted to adjust. */
6840 put_value (bfd_vma size
,
6841 unsigned long chunksz
,
6844 bfd_byte
* location
)
6846 location
+= (size
- chunksz
);
6848 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
6856 bfd_put_8 (input_bfd
, x
, location
);
6859 bfd_put_16 (input_bfd
, x
, location
);
6862 bfd_put_32 (input_bfd
, x
, location
);
6866 bfd_put_64 (input_bfd
, x
, location
);
6876 get_value (bfd_vma size
,
6877 unsigned long chunksz
,
6879 bfd_byte
* location
)
6883 for (; size
; size
-= chunksz
, location
+= chunksz
)
6891 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
6894 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
6897 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
6901 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
6912 decode_complex_addend
6913 (unsigned long * start
, /* in bits */
6914 unsigned long * oplen
, /* in bits */
6915 unsigned long * len
, /* in bits */
6916 unsigned long * wordsz
, /* in bytes */
6917 unsigned long * chunksz
, /* in bytes */
6918 unsigned long * lsb0_p
,
6919 unsigned long * signed_p
,
6920 unsigned long * trunc_p
,
6921 unsigned long encoded
)
6923 * start
= encoded
& 0x3F;
6924 * len
= (encoded
>> 6) & 0x3F;
6925 * oplen
= (encoded
>> 12) & 0x3F;
6926 * wordsz
= (encoded
>> 18) & 0xF;
6927 * chunksz
= (encoded
>> 22) & 0xF;
6928 * lsb0_p
= (encoded
>> 27) & 1;
6929 * signed_p
= (encoded
>> 28) & 1;
6930 * trunc_p
= (encoded
>> 29) & 1;
6934 bfd_elf_perform_complex_relocation
6935 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
6936 struct bfd_link_info
* info
,
6938 asection
* input_section
,
6939 bfd_byte
* contents
,
6940 Elf_Internal_Rela
* rel
,
6941 Elf_Internal_Sym
* local_syms
,
6942 asection
** local_sections
)
6944 const struct elf_backend_data
* bed
;
6945 Elf_Internal_Shdr
* symtab_hdr
;
6947 bfd_vma relocation
= 0, shift
, x
;
6948 unsigned long r_symndx
;
6950 unsigned long start
, oplen
, len
, wordsz
,
6951 chunksz
, lsb0_p
, signed_p
, trunc_p
;
6953 /* Perform this reloc, since it is complex.
6954 (this is not to say that it necessarily refers to a complex
6955 symbol; merely that it is a self-describing CGEN based reloc.
6956 i.e. the addend has the complete reloc information (bit start, end,
6957 word size, etc) encoded within it.). */
6958 r_symndx
= ELF32_R_SYM (rel
->r_info
);
6959 bed
= get_elf_backend_data (input_bfd
);
6960 if (bed
->s
->arch_size
== 64)
6964 printf ("Performing complex relocation %ld...\n", r_symndx
);
6967 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6968 if (r_symndx
< symtab_hdr
->sh_info
)
6970 /* The symbol is local. */
6971 Elf_Internal_Sym
* sym
;
6973 sym
= local_syms
+ r_symndx
;
6974 sec
= local_sections
[r_symndx
];
6975 relocation
= sym
->st_value
;
6976 if (sym
->st_shndx
> SHN_UNDEF
&&
6977 sym
->st_shndx
< SHN_LORESERVE
)
6978 relocation
+= (sec
->output_offset
+
6979 sec
->output_section
->vma
);
6983 /* The symbol is global. */
6984 struct elf_link_hash_entry
**sym_hashes
;
6985 struct elf_link_hash_entry
* h
;
6987 sym_hashes
= elf_sym_hashes (input_bfd
);
6988 h
= sym_hashes
[r_symndx
];
6990 while (h
->root
.type
== bfd_link_hash_indirect
6991 || h
->root
.type
== bfd_link_hash_warning
)
6992 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6994 if (h
->root
.type
== bfd_link_hash_defined
6995 || h
->root
.type
== bfd_link_hash_defweak
)
6997 sec
= h
->root
.u
.def
.section
;
6998 relocation
= h
->root
.u
.def
.value
;
7000 if (! bfd_is_abs_section (sec
))
7001 relocation
+= (sec
->output_section
->vma
7002 + sec
->output_offset
);
7004 if (h
->root
.type
== bfd_link_hash_undefined
7005 && !((*info
->callbacks
->undefined_symbol
)
7006 (info
, h
->root
.root
.string
, input_bfd
,
7007 input_section
, rel
->r_offset
,
7008 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7009 || ELF_ST_VISIBILITY (h
->other
))))
7013 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7014 & chunksz
, & lsb0_p
, & signed_p
,
7015 & trunc_p
, rel
->r_addend
);
7017 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7020 shift
= (start
+ 1) - len
;
7022 shift
= (8 * wordsz
) - (start
+ len
);
7024 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7027 printf ("Doing complex reloc: "
7028 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7029 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7030 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7031 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7032 oplen
, x
, mask
, relocation
);
7037 /* Now do an overflow check. */
7038 if (bfd_check_overflow ((signed_p
?
7039 complain_overflow_signed
:
7040 complain_overflow_unsigned
),
7041 len
, 0, (8 * wordsz
),
7042 relocation
) == bfd_reloc_overflow
)
7043 (*_bfd_error_handler
)
7044 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7046 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7047 relocation
, (signed_p
? "(signed) " : ""), mask
);
7051 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7054 printf (" relocation: %8.8lx\n"
7055 " shifted mask: %8.8lx\n"
7056 " shifted/masked reloc: %8.8lx\n"
7057 " result: %8.8lx\n",
7058 relocation
, (mask
<< shift
),
7059 ((relocation
& mask
) << shift
), x
);
7061 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7064 /* When performing a relocatable link, the input relocations are
7065 preserved. But, if they reference global symbols, the indices
7066 referenced must be updated. Update all the relocations in
7067 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7070 elf_link_adjust_relocs (bfd
*abfd
,
7071 Elf_Internal_Shdr
*rel_hdr
,
7073 struct elf_link_hash_entry
**rel_hash
)
7076 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7078 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7079 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7080 bfd_vma r_type_mask
;
7083 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7085 swap_in
= bed
->s
->swap_reloc_in
;
7086 swap_out
= bed
->s
->swap_reloc_out
;
7088 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7090 swap_in
= bed
->s
->swap_reloca_in
;
7091 swap_out
= bed
->s
->swap_reloca_out
;
7096 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7099 if (bed
->s
->arch_size
== 32)
7106 r_type_mask
= 0xffffffff;
7110 erela
= rel_hdr
->contents
;
7111 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7113 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7116 if (*rel_hash
== NULL
)
7119 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7121 (*swap_in
) (abfd
, erela
, irela
);
7122 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7123 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7124 | (irela
[j
].r_info
& r_type_mask
));
7125 (*swap_out
) (abfd
, irela
, erela
);
7129 struct elf_link_sort_rela
7135 enum elf_reloc_type_class type
;
7136 /* We use this as an array of size int_rels_per_ext_rel. */
7137 Elf_Internal_Rela rela
[1];
7141 elf_link_sort_cmp1 (const void *A
, const void *B
)
7143 const struct elf_link_sort_rela
*a
= A
;
7144 const struct elf_link_sort_rela
*b
= B
;
7145 int relativea
, relativeb
;
7147 relativea
= a
->type
== reloc_class_relative
;
7148 relativeb
= b
->type
== reloc_class_relative
;
7150 if (relativea
< relativeb
)
7152 if (relativea
> relativeb
)
7154 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7156 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7158 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7160 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7166 elf_link_sort_cmp2 (const void *A
, const void *B
)
7168 const struct elf_link_sort_rela
*a
= A
;
7169 const struct elf_link_sort_rela
*b
= B
;
7172 if (a
->u
.offset
< b
->u
.offset
)
7174 if (a
->u
.offset
> b
->u
.offset
)
7176 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7177 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7182 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7184 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7190 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7193 bfd_size_type count
, size
;
7194 size_t i
, ret
, sort_elt
, ext_size
;
7195 bfd_byte
*sort
, *s_non_relative
, *p
;
7196 struct elf_link_sort_rela
*sq
;
7197 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7198 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7199 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7200 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7201 struct bfd_link_order
*lo
;
7204 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7205 if (reldyn
== NULL
|| reldyn
->size
== 0)
7207 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7208 if (reldyn
== NULL
|| reldyn
->size
== 0)
7210 ext_size
= bed
->s
->sizeof_rel
;
7211 swap_in
= bed
->s
->swap_reloc_in
;
7212 swap_out
= bed
->s
->swap_reloc_out
;
7216 ext_size
= bed
->s
->sizeof_rela
;
7217 swap_in
= bed
->s
->swap_reloca_in
;
7218 swap_out
= bed
->s
->swap_reloca_out
;
7220 count
= reldyn
->size
/ ext_size
;
7223 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7224 if (lo
->type
== bfd_indirect_link_order
)
7226 asection
*o
= lo
->u
.indirect
.section
;
7230 if (size
!= reldyn
->size
)
7233 sort_elt
= (sizeof (struct elf_link_sort_rela
)
7234 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
7235 sort
= bfd_zmalloc (sort_elt
* count
);
7238 (*info
->callbacks
->warning
)
7239 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
7243 if (bed
->s
->arch_size
== 32)
7244 r_sym_mask
= ~(bfd_vma
) 0xff;
7246 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
7248 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7249 if (lo
->type
== bfd_indirect_link_order
)
7251 bfd_byte
*erel
, *erelend
;
7252 asection
*o
= lo
->u
.indirect
.section
;
7254 if (o
->contents
== NULL
&& o
->size
!= 0)
7256 /* This is a reloc section that is being handled as a normal
7257 section. See bfd_section_from_shdr. We can't combine
7258 relocs in this case. */
7263 erelend
= o
->contents
+ o
->size
;
7264 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7265 while (erel
< erelend
)
7267 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7268 (*swap_in
) (abfd
, erel
, s
->rela
);
7269 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
7270 s
->u
.sym_mask
= r_sym_mask
;
7276 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
7278 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
7280 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7281 if (s
->type
!= reloc_class_relative
)
7287 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
7288 for (; i
< count
; i
++, p
+= sort_elt
)
7290 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
7291 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
7293 sp
->u
.offset
= sq
->rela
->r_offset
;
7296 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
7298 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7299 if (lo
->type
== bfd_indirect_link_order
)
7301 bfd_byte
*erel
, *erelend
;
7302 asection
*o
= lo
->u
.indirect
.section
;
7305 erelend
= o
->contents
+ o
->size
;
7306 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7307 while (erel
< erelend
)
7309 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7310 (*swap_out
) (abfd
, s
->rela
, erel
);
7321 /* Flush the output symbols to the file. */
7324 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
7325 const struct elf_backend_data
*bed
)
7327 if (finfo
->symbuf_count
> 0)
7329 Elf_Internal_Shdr
*hdr
;
7333 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
7334 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
7335 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7336 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
7337 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
7340 hdr
->sh_size
+= amt
;
7341 finfo
->symbuf_count
= 0;
7347 /* Add a symbol to the output symbol table. */
7350 elf_link_output_sym (struct elf_final_link_info
*finfo
,
7352 Elf_Internal_Sym
*elfsym
,
7353 asection
*input_sec
,
7354 struct elf_link_hash_entry
*h
)
7357 Elf_External_Sym_Shndx
*destshndx
;
7358 bfd_boolean (*output_symbol_hook
)
7359 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
7360 struct elf_link_hash_entry
*);
7361 const struct elf_backend_data
*bed
;
7363 bed
= get_elf_backend_data (finfo
->output_bfd
);
7364 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
7365 if (output_symbol_hook
!= NULL
)
7367 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
7371 if (name
== NULL
|| *name
== '\0')
7372 elfsym
->st_name
= 0;
7373 else if (input_sec
->flags
& SEC_EXCLUDE
)
7374 elfsym
->st_name
= 0;
7377 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
7379 if (elfsym
->st_name
== (unsigned long) -1)
7383 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
7385 if (! elf_link_flush_output_syms (finfo
, bed
))
7389 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7390 destshndx
= finfo
->symshndxbuf
;
7391 if (destshndx
!= NULL
)
7393 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
7397 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
7398 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
7399 if (destshndx
== NULL
)
7401 memset ((char *) destshndx
+ amt
, 0, amt
);
7402 finfo
->shndxbuf_size
*= 2;
7404 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
7407 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
7408 finfo
->symbuf_count
+= 1;
7409 bfd_get_symcount (finfo
->output_bfd
) += 1;
7414 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7417 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
7419 if (sym
->st_shndx
> SHN_HIRESERVE
)
7421 /* The gABI doesn't support dynamic symbols in output sections
7423 (*_bfd_error_handler
)
7424 (_("%B: Too many sections: %d (>= %d)"),
7425 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
7426 bfd_set_error (bfd_error_nonrepresentable_section
);
7432 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7433 allowing an unsatisfied unversioned symbol in the DSO to match a
7434 versioned symbol that would normally require an explicit version.
7435 We also handle the case that a DSO references a hidden symbol
7436 which may be satisfied by a versioned symbol in another DSO. */
7439 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
7440 const struct elf_backend_data
*bed
,
7441 struct elf_link_hash_entry
*h
)
7444 struct elf_link_loaded_list
*loaded
;
7446 if (!is_elf_hash_table (info
->hash
))
7449 switch (h
->root
.type
)
7455 case bfd_link_hash_undefined
:
7456 case bfd_link_hash_undefweak
:
7457 abfd
= h
->root
.u
.undef
.abfd
;
7458 if ((abfd
->flags
& DYNAMIC
) == 0
7459 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
7463 case bfd_link_hash_defined
:
7464 case bfd_link_hash_defweak
:
7465 abfd
= h
->root
.u
.def
.section
->owner
;
7468 case bfd_link_hash_common
:
7469 abfd
= h
->root
.u
.c
.p
->section
->owner
;
7472 BFD_ASSERT (abfd
!= NULL
);
7474 for (loaded
= elf_hash_table (info
)->loaded
;
7476 loaded
= loaded
->next
)
7479 Elf_Internal_Shdr
*hdr
;
7480 bfd_size_type symcount
;
7481 bfd_size_type extsymcount
;
7482 bfd_size_type extsymoff
;
7483 Elf_Internal_Shdr
*versymhdr
;
7484 Elf_Internal_Sym
*isym
;
7485 Elf_Internal_Sym
*isymend
;
7486 Elf_Internal_Sym
*isymbuf
;
7487 Elf_External_Versym
*ever
;
7488 Elf_External_Versym
*extversym
;
7490 input
= loaded
->abfd
;
7492 /* We check each DSO for a possible hidden versioned definition. */
7494 || (input
->flags
& DYNAMIC
) == 0
7495 || elf_dynversym (input
) == 0)
7498 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
7500 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7501 if (elf_bad_symtab (input
))
7503 extsymcount
= symcount
;
7508 extsymcount
= symcount
- hdr
->sh_info
;
7509 extsymoff
= hdr
->sh_info
;
7512 if (extsymcount
== 0)
7515 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
7517 if (isymbuf
== NULL
)
7520 /* Read in any version definitions. */
7521 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
7522 extversym
= bfd_malloc (versymhdr
->sh_size
);
7523 if (extversym
== NULL
)
7526 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
7527 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
7528 != versymhdr
->sh_size
))
7536 ever
= extversym
+ extsymoff
;
7537 isymend
= isymbuf
+ extsymcount
;
7538 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
7541 Elf_Internal_Versym iver
;
7542 unsigned short version_index
;
7544 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
7545 || isym
->st_shndx
== SHN_UNDEF
)
7548 name
= bfd_elf_string_from_elf_section (input
,
7551 if (strcmp (name
, h
->root
.root
.string
) != 0)
7554 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
7556 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
7558 /* If we have a non-hidden versioned sym, then it should
7559 have provided a definition for the undefined sym. */
7563 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
7564 if (version_index
== 1 || version_index
== 2)
7566 /* This is the base or first version. We can use it. */
7580 /* Add an external symbol to the symbol table. This is called from
7581 the hash table traversal routine. When generating a shared object,
7582 we go through the symbol table twice. The first time we output
7583 anything that might have been forced to local scope in a version
7584 script. The second time we output the symbols that are still
7588 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
7590 struct elf_outext_info
*eoinfo
= data
;
7591 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
7593 Elf_Internal_Sym sym
;
7594 asection
*input_sec
;
7595 const struct elf_backend_data
*bed
;
7597 if (h
->root
.type
== bfd_link_hash_warning
)
7599 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7600 if (h
->root
.type
== bfd_link_hash_new
)
7604 /* Decide whether to output this symbol in this pass. */
7605 if (eoinfo
->localsyms
)
7607 if (!h
->forced_local
)
7612 if (h
->forced_local
)
7616 bed
= get_elf_backend_data (finfo
->output_bfd
);
7618 if (h
->root
.type
== bfd_link_hash_undefined
)
7620 /* If we have an undefined symbol reference here then it must have
7621 come from a shared library that is being linked in. (Undefined
7622 references in regular files have already been handled). */
7623 bfd_boolean ignore_undef
= FALSE
;
7625 /* Some symbols may be special in that the fact that they're
7626 undefined can be safely ignored - let backend determine that. */
7627 if (bed
->elf_backend_ignore_undef_symbol
)
7628 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
7630 /* If we are reporting errors for this situation then do so now. */
7631 if (ignore_undef
== FALSE
7634 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
7635 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
7637 if (! (finfo
->info
->callbacks
->undefined_symbol
7638 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
7639 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
7641 eoinfo
->failed
= TRUE
;
7647 /* We should also warn if a forced local symbol is referenced from
7648 shared libraries. */
7649 if (! finfo
->info
->relocatable
7650 && (! finfo
->info
->shared
)
7655 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
7657 (*_bfd_error_handler
)
7658 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7660 h
->root
.u
.def
.section
== bfd_abs_section_ptr
7661 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
7662 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
7664 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
7665 ? "hidden" : "local",
7666 h
->root
.root
.string
);
7667 eoinfo
->failed
= TRUE
;
7671 /* We don't want to output symbols that have never been mentioned by
7672 a regular file, or that we have been told to strip. However, if
7673 h->indx is set to -2, the symbol is used by a reloc and we must
7677 else if ((h
->def_dynamic
7679 || h
->root
.type
== bfd_link_hash_new
)
7683 else if (finfo
->info
->strip
== strip_all
)
7685 else if (finfo
->info
->strip
== strip_some
7686 && bfd_hash_lookup (finfo
->info
->keep_hash
,
7687 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
7689 else if (finfo
->info
->strip_discarded
7690 && (h
->root
.type
== bfd_link_hash_defined
7691 || h
->root
.type
== bfd_link_hash_defweak
)
7692 && elf_discarded_section (h
->root
.u
.def
.section
))
7697 /* If we're stripping it, and it's not a dynamic symbol, there's
7698 nothing else to do unless it is a forced local symbol. */
7701 && !h
->forced_local
)
7705 sym
.st_size
= h
->size
;
7706 sym
.st_other
= h
->other
;
7707 if (h
->forced_local
)
7708 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
7709 else if (h
->root
.type
== bfd_link_hash_undefweak
7710 || h
->root
.type
== bfd_link_hash_defweak
)
7711 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
7713 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
7715 switch (h
->root
.type
)
7718 case bfd_link_hash_new
:
7719 case bfd_link_hash_warning
:
7723 case bfd_link_hash_undefined
:
7724 case bfd_link_hash_undefweak
:
7725 input_sec
= bfd_und_section_ptr
;
7726 sym
.st_shndx
= SHN_UNDEF
;
7729 case bfd_link_hash_defined
:
7730 case bfd_link_hash_defweak
:
7732 input_sec
= h
->root
.u
.def
.section
;
7733 if (input_sec
->output_section
!= NULL
)
7736 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7737 input_sec
->output_section
);
7738 if (sym
.st_shndx
== SHN_BAD
)
7740 (*_bfd_error_handler
)
7741 (_("%B: could not find output section %A for input section %A"),
7742 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7743 eoinfo
->failed
= TRUE
;
7747 /* ELF symbols in relocatable files are section relative,
7748 but in nonrelocatable files they are virtual
7750 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7751 if (! finfo
->info
->relocatable
)
7753 sym
.st_value
+= input_sec
->output_section
->vma
;
7754 if (h
->type
== STT_TLS
)
7756 /* STT_TLS symbols are relative to PT_TLS segment
7758 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7759 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7765 BFD_ASSERT (input_sec
->owner
== NULL
7766 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7767 sym
.st_shndx
= SHN_UNDEF
;
7768 input_sec
= bfd_und_section_ptr
;
7773 case bfd_link_hash_common
:
7774 input_sec
= h
->root
.u
.c
.p
->section
;
7775 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7776 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7779 case bfd_link_hash_indirect
:
7780 /* These symbols are created by symbol versioning. They point
7781 to the decorated version of the name. For example, if the
7782 symbol foo@@GNU_1.2 is the default, which should be used when
7783 foo is used with no version, then we add an indirect symbol
7784 foo which points to foo@@GNU_1.2. We ignore these symbols,
7785 since the indirected symbol is already in the hash table. */
7789 /* Give the processor backend a chance to tweak the symbol value,
7790 and also to finish up anything that needs to be done for this
7791 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7792 forced local syms when non-shared is due to a historical quirk. */
7793 if ((h
->dynindx
!= -1
7795 && ((finfo
->info
->shared
7796 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7797 || h
->root
.type
!= bfd_link_hash_undefweak
))
7798 || !h
->forced_local
)
7799 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7801 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7802 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7804 eoinfo
->failed
= TRUE
;
7809 /* If we are marking the symbol as undefined, and there are no
7810 non-weak references to this symbol from a regular object, then
7811 mark the symbol as weak undefined; if there are non-weak
7812 references, mark the symbol as strong. We can't do this earlier,
7813 because it might not be marked as undefined until the
7814 finish_dynamic_symbol routine gets through with it. */
7815 if (sym
.st_shndx
== SHN_UNDEF
7817 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7818 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7822 if (h
->ref_regular_nonweak
)
7823 bindtype
= STB_GLOBAL
;
7825 bindtype
= STB_WEAK
;
7826 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7829 /* If a non-weak symbol with non-default visibility is not defined
7830 locally, it is a fatal error. */
7831 if (! finfo
->info
->relocatable
7832 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7833 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7834 && h
->root
.type
== bfd_link_hash_undefined
7837 (*_bfd_error_handler
)
7838 (_("%B: %s symbol `%s' isn't defined"),
7840 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7842 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7843 ? "internal" : "hidden",
7844 h
->root
.root
.string
);
7845 eoinfo
->failed
= TRUE
;
7849 /* If this symbol should be put in the .dynsym section, then put it
7850 there now. We already know the symbol index. We also fill in
7851 the entry in the .hash section. */
7852 if (h
->dynindx
!= -1
7853 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7857 sym
.st_name
= h
->dynstr_index
;
7858 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7859 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7861 eoinfo
->failed
= TRUE
;
7864 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7866 if (finfo
->hash_sec
!= NULL
)
7868 size_t hash_entry_size
;
7869 bfd_byte
*bucketpos
;
7874 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7875 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7878 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7879 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7880 + (bucket
+ 2) * hash_entry_size
);
7881 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7882 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7883 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7884 ((bfd_byte
*) finfo
->hash_sec
->contents
7885 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7888 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7890 Elf_Internal_Versym iversym
;
7891 Elf_External_Versym
*eversym
;
7893 if (!h
->def_regular
)
7895 if (h
->verinfo
.verdef
== NULL
)
7896 iversym
.vs_vers
= 0;
7898 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7902 if (h
->verinfo
.vertree
== NULL
)
7903 iversym
.vs_vers
= 1;
7905 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7906 if (finfo
->info
->create_default_symver
)
7911 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7913 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7914 eversym
+= h
->dynindx
;
7915 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7919 /* If we're stripping it, then it was just a dynamic symbol, and
7920 there's nothing else to do. */
7921 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7924 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7926 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7928 eoinfo
->failed
= TRUE
;
7935 /* Return TRUE if special handling is done for relocs in SEC against
7936 symbols defined in discarded sections. */
7939 elf_section_ignore_discarded_relocs (asection
*sec
)
7941 const struct elf_backend_data
*bed
;
7943 switch (sec
->sec_info_type
)
7945 case ELF_INFO_TYPE_STABS
:
7946 case ELF_INFO_TYPE_EH_FRAME
:
7952 bed
= get_elf_backend_data (sec
->owner
);
7953 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7954 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7960 /* Return a mask saying how ld should treat relocations in SEC against
7961 symbols defined in discarded sections. If this function returns
7962 COMPLAIN set, ld will issue a warning message. If this function
7963 returns PRETEND set, and the discarded section was link-once and the
7964 same size as the kept link-once section, ld will pretend that the
7965 symbol was actually defined in the kept section. Otherwise ld will
7966 zero the reloc (at least that is the intent, but some cooperation by
7967 the target dependent code is needed, particularly for REL targets). */
7970 _bfd_elf_default_action_discarded (asection
*sec
)
7972 if (sec
->flags
& SEC_DEBUGGING
)
7975 if (strcmp (".eh_frame", sec
->name
) == 0)
7978 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7981 return COMPLAIN
| PRETEND
;
7984 /* Find a match between a section and a member of a section group. */
7987 match_group_member (asection
*sec
, asection
*group
,
7988 struct bfd_link_info
*info
)
7990 asection
*first
= elf_next_in_group (group
);
7991 asection
*s
= first
;
7995 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
7998 s
= elf_next_in_group (s
);
8006 /* Check if the kept section of a discarded section SEC can be used
8007 to replace it. Return the replacement if it is OK. Otherwise return
8011 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8015 kept
= sec
->kept_section
;
8018 if ((kept
->flags
& SEC_GROUP
) != 0)
8019 kept
= match_group_member (sec
, kept
, info
);
8020 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8022 sec
->kept_section
= kept
;
8027 /* Link an input file into the linker output file. This function
8028 handles all the sections and relocations of the input file at once.
8029 This is so that we only have to read the local symbols once, and
8030 don't have to keep them in memory. */
8033 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8035 bfd_boolean (*relocate_section
)
8036 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8037 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8039 Elf_Internal_Shdr
*symtab_hdr
;
8042 Elf_Internal_Sym
*isymbuf
;
8043 Elf_Internal_Sym
*isym
;
8044 Elf_Internal_Sym
*isymend
;
8046 asection
**ppsection
;
8048 const struct elf_backend_data
*bed
;
8049 bfd_boolean emit_relocs
;
8050 struct elf_link_hash_entry
**sym_hashes
;
8052 output_bfd
= finfo
->output_bfd
;
8053 bed
= get_elf_backend_data (output_bfd
);
8054 relocate_section
= bed
->elf_backend_relocate_section
;
8056 /* If this is a dynamic object, we don't want to do anything here:
8057 we don't want the local symbols, and we don't want the section
8059 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8062 emit_relocs
= (finfo
->info
->relocatable
8063 || finfo
->info
->emitrelocations
);
8065 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8066 if (elf_bad_symtab (input_bfd
))
8068 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8073 locsymcount
= symtab_hdr
->sh_info
;
8074 extsymoff
= symtab_hdr
->sh_info
;
8077 /* Read the local symbols. */
8078 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8079 if (isymbuf
== NULL
&& locsymcount
!= 0)
8081 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8082 finfo
->internal_syms
,
8083 finfo
->external_syms
,
8084 finfo
->locsym_shndx
);
8085 if (isymbuf
== NULL
)
8088 /* evaluate_complex_relocation_symbols looks for symbols in
8089 finfo->internal_syms. */
8090 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
8092 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8093 finfo
->internal_syms
,
8094 finfo
->external_syms
,
8095 finfo
->locsym_shndx
);
8098 /* Find local symbol sections and adjust values of symbols in
8099 SEC_MERGE sections. Write out those local symbols we know are
8100 going into the output file. */
8101 isymend
= isymbuf
+ locsymcount
;
8102 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8104 isym
++, pindex
++, ppsection
++)
8108 Elf_Internal_Sym osym
;
8112 if (elf_bad_symtab (input_bfd
))
8114 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8121 if (isym
->st_shndx
== SHN_UNDEF
)
8122 isec
= bfd_und_section_ptr
;
8123 else if (isym
->st_shndx
< SHN_LORESERVE
8124 || isym
->st_shndx
> SHN_HIRESERVE
)
8126 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8128 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8129 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8131 _bfd_merged_section_offset (output_bfd
, &isec
,
8132 elf_section_data (isec
)->sec_info
,
8135 else if (isym
->st_shndx
== SHN_ABS
)
8136 isec
= bfd_abs_section_ptr
;
8137 else if (isym
->st_shndx
== SHN_COMMON
)
8138 isec
= bfd_com_section_ptr
;
8141 /* Don't attempt to output symbols with st_shnx in the
8142 reserved range other than SHN_ABS and SHN_COMMON. */
8149 /* Don't output the first, undefined, symbol. */
8150 if (ppsection
== finfo
->sections
)
8153 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8155 /* We never output section symbols. Instead, we use the
8156 section symbol of the corresponding section in the output
8161 /* If we are stripping all symbols, we don't want to output this
8163 if (finfo
->info
->strip
== strip_all
)
8166 /* If we are discarding all local symbols, we don't want to
8167 output this one. If we are generating a relocatable output
8168 file, then some of the local symbols may be required by
8169 relocs; we output them below as we discover that they are
8171 if (finfo
->info
->discard
== discard_all
)
8174 /* If this symbol is defined in a section which we are
8175 discarding, we don't need to keep it. */
8176 if (isym
->st_shndx
!= SHN_UNDEF
8177 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8179 || bfd_section_removed_from_list (output_bfd
,
8180 isec
->output_section
)))
8183 /* Get the name of the symbol. */
8184 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
8189 /* See if we are discarding symbols with this name. */
8190 if ((finfo
->info
->strip
== strip_some
8191 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
8193 || (((finfo
->info
->discard
== discard_sec_merge
8194 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
8195 || finfo
->info
->discard
== discard_l
)
8196 && bfd_is_local_label_name (input_bfd
, name
)))
8199 /* If we get here, we are going to output this symbol. */
8203 /* Adjust the section index for the output file. */
8204 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
8205 isec
->output_section
);
8206 if (osym
.st_shndx
== SHN_BAD
)
8209 *pindex
= bfd_get_symcount (output_bfd
);
8211 /* ELF symbols in relocatable files are section relative, but
8212 in executable files they are virtual addresses. Note that
8213 this code assumes that all ELF sections have an associated
8214 BFD section with a reasonable value for output_offset; below
8215 we assume that they also have a reasonable value for
8216 output_section. Any special sections must be set up to meet
8217 these requirements. */
8218 osym
.st_value
+= isec
->output_offset
;
8219 if (! finfo
->info
->relocatable
)
8221 osym
.st_value
+= isec
->output_section
->vma
;
8222 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
8224 /* STT_TLS symbols are relative to PT_TLS segment base. */
8225 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8226 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8230 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
8234 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
8237 /* Relocate the contents of each section. */
8238 sym_hashes
= elf_sym_hashes (input_bfd
);
8239 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
8243 if (! o
->linker_mark
)
8245 /* This section was omitted from the link. */
8249 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8250 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
8253 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
8255 /* Section was created by _bfd_elf_link_create_dynamic_sections
8260 /* Get the contents of the section. They have been cached by a
8261 relaxation routine. Note that o is a section in an input
8262 file, so the contents field will not have been set by any of
8263 the routines which work on output files. */
8264 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
8265 contents
= elf_section_data (o
)->this_hdr
.contents
;
8268 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
8270 contents
= finfo
->contents
;
8271 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
8275 if ((o
->flags
& SEC_RELOC
) != 0)
8277 Elf_Internal_Rela
*internal_relocs
;
8278 bfd_vma r_type_mask
;
8281 /* Get the swapped relocs. */
8283 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
8284 finfo
->internal_relocs
, FALSE
);
8285 if (internal_relocs
== NULL
8286 && o
->reloc_count
> 0)
8289 if (bed
->s
->arch_size
== 32)
8296 r_type_mask
= 0xffffffff;
8300 /* Run through the relocs looking for any against symbols
8301 from discarded sections and section symbols from
8302 removed link-once sections. Complain about relocs
8303 against discarded sections. Zero relocs against removed
8304 link-once sections. */
8305 if (!elf_section_ignore_discarded_relocs (o
))
8307 Elf_Internal_Rela
*rel
, *relend
;
8308 unsigned int action
= (*bed
->action_discarded
) (o
);
8310 rel
= internal_relocs
;
8311 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8312 for ( ; rel
< relend
; rel
++)
8314 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
8315 asection
**ps
, *sec
;
8316 struct elf_link_hash_entry
*h
= NULL
;
8317 const char *sym_name
;
8319 if (r_symndx
== STN_UNDEF
)
8322 if (r_symndx
>= locsymcount
8323 || (elf_bad_symtab (input_bfd
)
8324 && finfo
->sections
[r_symndx
] == NULL
))
8326 h
= sym_hashes
[r_symndx
- extsymoff
];
8328 /* Badly formatted input files can contain relocs that
8329 reference non-existant symbols. Check here so that
8330 we do not seg fault. */
8335 sprintf_vma (buffer
, rel
->r_info
);
8336 (*_bfd_error_handler
)
8337 (_("error: %B contains a reloc (0x%s) for section %A "
8338 "that references a non-existent global symbol"),
8339 input_bfd
, o
, buffer
);
8340 bfd_set_error (bfd_error_bad_value
);
8344 while (h
->root
.type
== bfd_link_hash_indirect
8345 || h
->root
.type
== bfd_link_hash_warning
)
8346 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8348 if (h
->root
.type
!= bfd_link_hash_defined
8349 && h
->root
.type
!= bfd_link_hash_defweak
)
8352 ps
= &h
->root
.u
.def
.section
;
8353 sym_name
= h
->root
.root
.string
;
8357 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
8358 ps
= &finfo
->sections
[r_symndx
];
8359 sym_name
= bfd_elf_sym_name (input_bfd
,
8364 /* Complain if the definition comes from a
8365 discarded section. */
8366 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
8368 BFD_ASSERT (r_symndx
!= 0);
8369 if (action
& COMPLAIN
)
8370 (*finfo
->info
->callbacks
->einfo
)
8371 (_("%X`%s' referenced in section `%A' of %B: "
8372 "defined in discarded section `%A' of %B\n"),
8373 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
8375 /* Try to do the best we can to support buggy old
8376 versions of gcc. Pretend that the symbol is
8377 really defined in the kept linkonce section.
8378 FIXME: This is quite broken. Modifying the
8379 symbol here means we will be changing all later
8380 uses of the symbol, not just in this section. */
8381 if (action
& PRETEND
)
8385 kept
= _bfd_elf_check_kept_section (sec
,
8394 /* Remove the symbol reference from the reloc, but
8395 don't kill the reloc completely. This is so that
8396 a zero value will be written into the section,
8397 which may have non-zero contents put there by the
8398 assembler. Zero in things like an eh_frame fde
8399 pc_begin allows stack unwinders to recognize the
8401 rel
->r_info
&= r_type_mask
;
8407 /* Relocate the section by invoking a back end routine.
8409 The back end routine is responsible for adjusting the
8410 section contents as necessary, and (if using Rela relocs
8411 and generating a relocatable output file) adjusting the
8412 reloc addend as necessary.
8414 The back end routine does not have to worry about setting
8415 the reloc address or the reloc symbol index.
8417 The back end routine is given a pointer to the swapped in
8418 internal symbols, and can access the hash table entries
8419 for the external symbols via elf_sym_hashes (input_bfd).
8421 When generating relocatable output, the back end routine
8422 must handle STB_LOCAL/STT_SECTION symbols specially. The
8423 output symbol is going to be a section symbol
8424 corresponding to the output section, which will require
8425 the addend to be adjusted. */
8427 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
8428 input_bfd
, o
, contents
,
8436 Elf_Internal_Rela
*irela
;
8437 Elf_Internal_Rela
*irelaend
;
8438 bfd_vma last_offset
;
8439 struct elf_link_hash_entry
**rel_hash
;
8440 struct elf_link_hash_entry
**rel_hash_list
;
8441 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
8442 unsigned int next_erel
;
8443 bfd_boolean rela_normal
;
8445 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
8446 rela_normal
= (bed
->rela_normal
8447 && (input_rel_hdr
->sh_entsize
8448 == bed
->s
->sizeof_rela
));
8450 /* Adjust the reloc addresses and symbol indices. */
8452 irela
= internal_relocs
;
8453 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8454 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
8455 + elf_section_data (o
->output_section
)->rel_count
8456 + elf_section_data (o
->output_section
)->rel_count2
);
8457 rel_hash_list
= rel_hash
;
8458 last_offset
= o
->output_offset
;
8459 if (!finfo
->info
->relocatable
)
8460 last_offset
+= o
->output_section
->vma
;
8461 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
8463 unsigned long r_symndx
;
8465 Elf_Internal_Sym sym
;
8467 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
8473 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
8476 if (irela
->r_offset
>= (bfd_vma
) -2)
8478 /* This is a reloc for a deleted entry or somesuch.
8479 Turn it into an R_*_NONE reloc, at the same
8480 offset as the last reloc. elf_eh_frame.c and
8481 bfd_elf_discard_info rely on reloc offsets
8483 irela
->r_offset
= last_offset
;
8485 irela
->r_addend
= 0;
8489 irela
->r_offset
+= o
->output_offset
;
8491 /* Relocs in an executable have to be virtual addresses. */
8492 if (!finfo
->info
->relocatable
)
8493 irela
->r_offset
+= o
->output_section
->vma
;
8495 last_offset
= irela
->r_offset
;
8497 r_symndx
= irela
->r_info
>> r_sym_shift
;
8498 if (r_symndx
== STN_UNDEF
)
8501 if (r_symndx
>= locsymcount
8502 || (elf_bad_symtab (input_bfd
)
8503 && finfo
->sections
[r_symndx
] == NULL
))
8505 struct elf_link_hash_entry
*rh
;
8508 /* This is a reloc against a global symbol. We
8509 have not yet output all the local symbols, so
8510 we do not know the symbol index of any global
8511 symbol. We set the rel_hash entry for this
8512 reloc to point to the global hash table entry
8513 for this symbol. The symbol index is then
8514 set at the end of bfd_elf_final_link. */
8515 indx
= r_symndx
- extsymoff
;
8516 rh
= elf_sym_hashes (input_bfd
)[indx
];
8517 while (rh
->root
.type
== bfd_link_hash_indirect
8518 || rh
->root
.type
== bfd_link_hash_warning
)
8519 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
8521 /* Setting the index to -2 tells
8522 elf_link_output_extsym that this symbol is
8524 BFD_ASSERT (rh
->indx
< 0);
8532 /* This is a reloc against a local symbol. */
8535 sym
= isymbuf
[r_symndx
];
8536 sec
= finfo
->sections
[r_symndx
];
8537 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
8539 /* I suppose the backend ought to fill in the
8540 section of any STT_SECTION symbol against a
8541 processor specific section. */
8543 if (bfd_is_abs_section (sec
))
8545 else if (sec
== NULL
|| sec
->owner
== NULL
)
8547 bfd_set_error (bfd_error_bad_value
);
8552 asection
*osec
= sec
->output_section
;
8554 /* If we have discarded a section, the output
8555 section will be the absolute section. In
8556 case of discarded link-once and discarded
8557 SEC_MERGE sections, use the kept section. */
8558 if (bfd_is_abs_section (osec
)
8559 && sec
->kept_section
!= NULL
8560 && sec
->kept_section
->output_section
!= NULL
)
8562 osec
= sec
->kept_section
->output_section
;
8563 irela
->r_addend
-= osec
->vma
;
8566 if (!bfd_is_abs_section (osec
))
8568 r_symndx
= osec
->target_index
;
8571 struct elf_link_hash_table
*htab
;
8574 htab
= elf_hash_table (finfo
->info
);
8575 oi
= htab
->text_index_section
;
8576 if ((osec
->flags
& SEC_READONLY
) == 0
8577 && htab
->data_index_section
!= NULL
)
8578 oi
= htab
->data_index_section
;
8582 irela
->r_addend
+= osec
->vma
- oi
->vma
;
8583 r_symndx
= oi
->target_index
;
8587 BFD_ASSERT (r_symndx
!= 0);
8591 /* Adjust the addend according to where the
8592 section winds up in the output section. */
8594 irela
->r_addend
+= sec
->output_offset
;
8598 if (finfo
->indices
[r_symndx
] == -1)
8600 unsigned long shlink
;
8604 if (finfo
->info
->strip
== strip_all
)
8606 /* You can't do ld -r -s. */
8607 bfd_set_error (bfd_error_invalid_operation
);
8611 /* This symbol was skipped earlier, but
8612 since it is needed by a reloc, we
8613 must output it now. */
8614 shlink
= symtab_hdr
->sh_link
;
8615 name
= (bfd_elf_string_from_elf_section
8616 (input_bfd
, shlink
, sym
.st_name
));
8620 osec
= sec
->output_section
;
8622 _bfd_elf_section_from_bfd_section (output_bfd
,
8624 if (sym
.st_shndx
== SHN_BAD
)
8627 sym
.st_value
+= sec
->output_offset
;
8628 if (! finfo
->info
->relocatable
)
8630 sym
.st_value
+= osec
->vma
;
8631 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
8633 /* STT_TLS symbols are relative to PT_TLS
8635 BFD_ASSERT (elf_hash_table (finfo
->info
)
8637 sym
.st_value
-= (elf_hash_table (finfo
->info
)
8642 finfo
->indices
[r_symndx
]
8643 = bfd_get_symcount (output_bfd
);
8645 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
8650 r_symndx
= finfo
->indices
[r_symndx
];
8653 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
8654 | (irela
->r_info
& r_type_mask
));
8657 /* Swap out the relocs. */
8658 if (input_rel_hdr
->sh_size
!= 0
8659 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
8665 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
8666 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
8668 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
8669 * bed
->s
->int_rels_per_ext_rel
);
8670 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
8671 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
8680 /* Write out the modified section contents. */
8681 if (bed
->elf_backend_write_section
8682 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
8685 /* Section written out. */
8687 else switch (o
->sec_info_type
)
8689 case ELF_INFO_TYPE_STABS
:
8690 if (! (_bfd_write_section_stabs
8692 &elf_hash_table (finfo
->info
)->stab_info
,
8693 o
, &elf_section_data (o
)->sec_info
, contents
)))
8696 case ELF_INFO_TYPE_MERGE
:
8697 if (! _bfd_write_merged_section (output_bfd
, o
,
8698 elf_section_data (o
)->sec_info
))
8701 case ELF_INFO_TYPE_EH_FRAME
:
8703 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
8710 if (! (o
->flags
& SEC_EXCLUDE
)
8711 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
8713 (file_ptr
) o
->output_offset
,
8724 /* Generate a reloc when linking an ELF file. This is a reloc
8725 requested by the linker, and does not come from any input file. This
8726 is used to build constructor and destructor tables when linking
8730 elf_reloc_link_order (bfd
*output_bfd
,
8731 struct bfd_link_info
*info
,
8732 asection
*output_section
,
8733 struct bfd_link_order
*link_order
)
8735 reloc_howto_type
*howto
;
8739 struct elf_link_hash_entry
**rel_hash_ptr
;
8740 Elf_Internal_Shdr
*rel_hdr
;
8741 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8742 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8746 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8749 bfd_set_error (bfd_error_bad_value
);
8753 addend
= link_order
->u
.reloc
.p
->addend
;
8755 /* Figure out the symbol index. */
8756 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8757 + elf_section_data (output_section
)->rel_count
8758 + elf_section_data (output_section
)->rel_count2
);
8759 if (link_order
->type
== bfd_section_reloc_link_order
)
8761 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8762 BFD_ASSERT (indx
!= 0);
8763 *rel_hash_ptr
= NULL
;
8767 struct elf_link_hash_entry
*h
;
8769 /* Treat a reloc against a defined symbol as though it were
8770 actually against the section. */
8771 h
= ((struct elf_link_hash_entry
*)
8772 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8773 link_order
->u
.reloc
.p
->u
.name
,
8774 FALSE
, FALSE
, TRUE
));
8776 && (h
->root
.type
== bfd_link_hash_defined
8777 || h
->root
.type
== bfd_link_hash_defweak
))
8781 section
= h
->root
.u
.def
.section
;
8782 indx
= section
->output_section
->target_index
;
8783 *rel_hash_ptr
= NULL
;
8784 /* It seems that we ought to add the symbol value to the
8785 addend here, but in practice it has already been added
8786 because it was passed to constructor_callback. */
8787 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8791 /* Setting the index to -2 tells elf_link_output_extsym that
8792 this symbol is used by a reloc. */
8799 if (! ((*info
->callbacks
->unattached_reloc
)
8800 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
8806 /* If this is an inplace reloc, we must write the addend into the
8808 if (howto
->partial_inplace
&& addend
!= 0)
8811 bfd_reloc_status_type rstat
;
8814 const char *sym_name
;
8816 size
= bfd_get_reloc_size (howto
);
8817 buf
= bfd_zmalloc (size
);
8820 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8827 case bfd_reloc_outofrange
:
8830 case bfd_reloc_overflow
:
8831 if (link_order
->type
== bfd_section_reloc_link_order
)
8832 sym_name
= bfd_section_name (output_bfd
,
8833 link_order
->u
.reloc
.p
->u
.section
);
8835 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8836 if (! ((*info
->callbacks
->reloc_overflow
)
8837 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8838 NULL
, (bfd_vma
) 0)))
8845 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
8846 link_order
->offset
, size
);
8852 /* The address of a reloc is relative to the section in a
8853 relocatable file, and is a virtual address in an executable
8855 offset
= link_order
->offset
;
8856 if (! info
->relocatable
)
8857 offset
+= output_section
->vma
;
8859 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
8861 irel
[i
].r_offset
= offset
;
8863 irel
[i
].r_addend
= 0;
8865 if (bed
->s
->arch_size
== 32)
8866 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8868 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8870 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8871 erel
= rel_hdr
->contents
;
8872 if (rel_hdr
->sh_type
== SHT_REL
)
8874 erel
+= (elf_section_data (output_section
)->rel_count
8875 * bed
->s
->sizeof_rel
);
8876 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8880 irel
[0].r_addend
= addend
;
8881 erel
+= (elf_section_data (output_section
)->rel_count
8882 * bed
->s
->sizeof_rela
);
8883 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8886 ++elf_section_data (output_section
)->rel_count
;
8892 /* Get the output vma of the section pointed to by the sh_link field. */
8895 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8897 Elf_Internal_Shdr
**elf_shdrp
;
8901 s
= p
->u
.indirect
.section
;
8902 elf_shdrp
= elf_elfsections (s
->owner
);
8903 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8904 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8906 The Intel C compiler generates SHT_IA_64_UNWIND with
8907 SHF_LINK_ORDER. But it doesn't set the sh_link or
8908 sh_info fields. Hence we could get the situation
8909 where elfsec is 0. */
8912 const struct elf_backend_data
*bed
8913 = get_elf_backend_data (s
->owner
);
8914 if (bed
->link_order_error_handler
)
8915 bed
->link_order_error_handler
8916 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8921 s
= elf_shdrp
[elfsec
]->bfd_section
;
8922 return s
->output_section
->vma
+ s
->output_offset
;
8927 /* Compare two sections based on the locations of the sections they are
8928 linked to. Used by elf_fixup_link_order. */
8931 compare_link_order (const void * a
, const void * b
)
8936 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8937 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8944 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8945 order as their linked sections. Returns false if this could not be done
8946 because an output section includes both ordered and unordered
8947 sections. Ideally we'd do this in the linker proper. */
8950 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8955 struct bfd_link_order
*p
;
8957 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8959 struct bfd_link_order
**sections
;
8960 asection
*s
, *other_sec
, *linkorder_sec
;
8964 linkorder_sec
= NULL
;
8967 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8969 if (p
->type
== bfd_indirect_link_order
)
8971 s
= p
->u
.indirect
.section
;
8973 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8974 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8975 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8976 && elfsec
< elf_numsections (sub
)
8977 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8991 if (seen_other
&& seen_linkorder
)
8993 if (other_sec
&& linkorder_sec
)
8994 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8996 linkorder_sec
->owner
, other_sec
,
8999 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9001 bfd_set_error (bfd_error_bad_value
);
9006 if (!seen_linkorder
)
9009 sections
= (struct bfd_link_order
**)
9010 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9013 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9015 sections
[seen_linkorder
++] = p
;
9017 /* Sort the input sections in the order of their linked section. */
9018 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9019 compare_link_order
);
9021 /* Change the offsets of the sections. */
9023 for (n
= 0; n
< seen_linkorder
; n
++)
9025 s
= sections
[n
]->u
.indirect
.section
;
9026 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9027 s
->output_offset
= offset
;
9028 sections
[n
]->offset
= offset
;
9029 offset
+= sections
[n
]->size
;
9036 /* Do the final step of an ELF link. */
9039 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9041 bfd_boolean dynamic
;
9042 bfd_boolean emit_relocs
;
9044 struct elf_final_link_info finfo
;
9045 register asection
*o
;
9046 register struct bfd_link_order
*p
;
9048 bfd_size_type max_contents_size
;
9049 bfd_size_type max_external_reloc_size
;
9050 bfd_size_type max_internal_reloc_count
;
9051 bfd_size_type max_sym_count
;
9052 bfd_size_type max_sym_shndx_count
;
9054 Elf_Internal_Sym elfsym
;
9056 Elf_Internal_Shdr
*symtab_hdr
;
9057 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9058 Elf_Internal_Shdr
*symstrtab_hdr
;
9059 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9060 struct elf_outext_info eoinfo
;
9062 size_t relativecount
= 0;
9063 asection
*reldyn
= 0;
9066 if (! is_elf_hash_table (info
->hash
))
9070 abfd
->flags
|= DYNAMIC
;
9072 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9073 dynobj
= elf_hash_table (info
)->dynobj
;
9075 emit_relocs
= (info
->relocatable
9076 || info
->emitrelocations
);
9079 finfo
.output_bfd
= abfd
;
9080 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9081 if (finfo
.symstrtab
== NULL
)
9086 finfo
.dynsym_sec
= NULL
;
9087 finfo
.hash_sec
= NULL
;
9088 finfo
.symver_sec
= NULL
;
9092 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9093 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9094 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9095 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9096 /* Note that it is OK if symver_sec is NULL. */
9099 finfo
.contents
= NULL
;
9100 finfo
.external_relocs
= NULL
;
9101 finfo
.internal_relocs
= NULL
;
9102 finfo
.external_syms
= NULL
;
9103 finfo
.locsym_shndx
= NULL
;
9104 finfo
.internal_syms
= NULL
;
9105 finfo
.indices
= NULL
;
9106 finfo
.sections
= NULL
;
9107 finfo
.symbuf
= NULL
;
9108 finfo
.symshndxbuf
= NULL
;
9109 finfo
.symbuf_count
= 0;
9110 finfo
.shndxbuf_size
= 0;
9112 /* Count up the number of relocations we will output for each output
9113 section, so that we know the sizes of the reloc sections. We
9114 also figure out some maximum sizes. */
9115 max_contents_size
= 0;
9116 max_external_reloc_size
= 0;
9117 max_internal_reloc_count
= 0;
9119 max_sym_shndx_count
= 0;
9121 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9123 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
9126 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9128 unsigned int reloc_count
= 0;
9129 struct bfd_elf_section_data
*esdi
= NULL
;
9130 unsigned int *rel_count1
;
9132 if (p
->type
== bfd_section_reloc_link_order
9133 || p
->type
== bfd_symbol_reloc_link_order
)
9135 else if (p
->type
== bfd_indirect_link_order
)
9139 sec
= p
->u
.indirect
.section
;
9140 esdi
= elf_section_data (sec
);
9142 /* Mark all sections which are to be included in the
9143 link. This will normally be every section. We need
9144 to do this so that we can identify any sections which
9145 the linker has decided to not include. */
9146 sec
->linker_mark
= TRUE
;
9148 if (sec
->flags
& SEC_MERGE
)
9151 if (info
->relocatable
|| info
->emitrelocations
)
9152 reloc_count
= sec
->reloc_count
;
9153 else if (bed
->elf_backend_count_relocs
)
9155 Elf_Internal_Rela
* relocs
;
9157 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
9160 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
9162 if (elf_section_data (o
)->relocs
!= relocs
)
9166 if (sec
->rawsize
> max_contents_size
)
9167 max_contents_size
= sec
->rawsize
;
9168 if (sec
->size
> max_contents_size
)
9169 max_contents_size
= sec
->size
;
9171 /* We are interested in just local symbols, not all
9173 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
9174 && (sec
->owner
->flags
& DYNAMIC
) == 0)
9178 if (elf_bad_symtab (sec
->owner
))
9179 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
9180 / bed
->s
->sizeof_sym
);
9182 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
9184 if (sym_count
> max_sym_count
)
9185 max_sym_count
= sym_count
;
9187 if (sym_count
> max_sym_shndx_count
9188 && elf_symtab_shndx (sec
->owner
) != 0)
9189 max_sym_shndx_count
= sym_count
;
9191 if ((sec
->flags
& SEC_RELOC
) != 0)
9195 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
9196 if (ext_size
> max_external_reloc_size
)
9197 max_external_reloc_size
= ext_size
;
9198 if (sec
->reloc_count
> max_internal_reloc_count
)
9199 max_internal_reloc_count
= sec
->reloc_count
;
9204 if (reloc_count
== 0)
9207 o
->reloc_count
+= reloc_count
;
9209 /* MIPS may have a mix of REL and RELA relocs on sections.
9210 To support this curious ABI we keep reloc counts in
9211 elf_section_data too. We must be careful to add the
9212 relocations from the input section to the right output
9213 count. FIXME: Get rid of one count. We have
9214 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9215 rel_count1
= &esdo
->rel_count
;
9218 bfd_boolean same_size
;
9219 bfd_size_type entsize1
;
9221 entsize1
= esdi
->rel_hdr
.sh_entsize
;
9222 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
9223 || entsize1
== bed
->s
->sizeof_rela
);
9224 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
9227 rel_count1
= &esdo
->rel_count2
;
9229 if (esdi
->rel_hdr2
!= NULL
)
9231 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
9232 unsigned int alt_count
;
9233 unsigned int *rel_count2
;
9235 BFD_ASSERT (entsize2
!= entsize1
9236 && (entsize2
== bed
->s
->sizeof_rel
9237 || entsize2
== bed
->s
->sizeof_rela
));
9239 rel_count2
= &esdo
->rel_count2
;
9241 rel_count2
= &esdo
->rel_count
;
9243 /* The following is probably too simplistic if the
9244 backend counts output relocs unusually. */
9245 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
9246 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
9247 *rel_count2
+= alt_count
;
9248 reloc_count
-= alt_count
;
9251 *rel_count1
+= reloc_count
;
9254 if (o
->reloc_count
> 0)
9255 o
->flags
|= SEC_RELOC
;
9258 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9259 set it (this is probably a bug) and if it is set
9260 assign_section_numbers will create a reloc section. */
9261 o
->flags
&=~ SEC_RELOC
;
9264 /* If the SEC_ALLOC flag is not set, force the section VMA to
9265 zero. This is done in elf_fake_sections as well, but forcing
9266 the VMA to 0 here will ensure that relocs against these
9267 sections are handled correctly. */
9268 if ((o
->flags
& SEC_ALLOC
) == 0
9269 && ! o
->user_set_vma
)
9273 if (! info
->relocatable
&& merged
)
9274 elf_link_hash_traverse (elf_hash_table (info
),
9275 _bfd_elf_link_sec_merge_syms
, abfd
);
9277 /* Figure out the file positions for everything but the symbol table
9278 and the relocs. We set symcount to force assign_section_numbers
9279 to create a symbol table. */
9280 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
9281 BFD_ASSERT (! abfd
->output_has_begun
);
9282 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
9285 /* Set sizes, and assign file positions for reloc sections. */
9286 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9288 if ((o
->flags
& SEC_RELOC
) != 0)
9290 if (!(_bfd_elf_link_size_reloc_section
9291 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
9294 if (elf_section_data (o
)->rel_hdr2
9295 && !(_bfd_elf_link_size_reloc_section
9296 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
9300 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9301 to count upwards while actually outputting the relocations. */
9302 elf_section_data (o
)->rel_count
= 0;
9303 elf_section_data (o
)->rel_count2
= 0;
9306 _bfd_elf_assign_file_positions_for_relocs (abfd
);
9308 /* We have now assigned file positions for all the sections except
9309 .symtab and .strtab. We start the .symtab section at the current
9310 file position, and write directly to it. We build the .strtab
9311 section in memory. */
9312 bfd_get_symcount (abfd
) = 0;
9313 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9314 /* sh_name is set in prep_headers. */
9315 symtab_hdr
->sh_type
= SHT_SYMTAB
;
9316 /* sh_flags, sh_addr and sh_size all start off zero. */
9317 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
9318 /* sh_link is set in assign_section_numbers. */
9319 /* sh_info is set below. */
9320 /* sh_offset is set just below. */
9321 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
9323 off
= elf_tdata (abfd
)->next_file_pos
;
9324 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
9326 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9327 incorrect. We do not yet know the size of the .symtab section.
9328 We correct next_file_pos below, after we do know the size. */
9330 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9331 continuously seeking to the right position in the file. */
9332 if (! info
->keep_memory
|| max_sym_count
< 20)
9333 finfo
.symbuf_size
= 20;
9335 finfo
.symbuf_size
= max_sym_count
;
9336 amt
= finfo
.symbuf_size
;
9337 amt
*= bed
->s
->sizeof_sym
;
9338 finfo
.symbuf
= bfd_malloc (amt
);
9339 if (finfo
.symbuf
== NULL
)
9341 if (elf_numsections (abfd
) > SHN_LORESERVE
)
9343 /* Wild guess at number of output symbols. realloc'd as needed. */
9344 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
9345 finfo
.shndxbuf_size
= amt
;
9346 amt
*= sizeof (Elf_External_Sym_Shndx
);
9347 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
9348 if (finfo
.symshndxbuf
== NULL
)
9352 /* Start writing out the symbol table. The first symbol is always a
9354 if (info
->strip
!= strip_all
9357 elfsym
.st_value
= 0;
9360 elfsym
.st_other
= 0;
9361 elfsym
.st_shndx
= SHN_UNDEF
;
9362 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
9367 /* Output a symbol for each section. We output these even if we are
9368 discarding local symbols, since they are used for relocs. These
9369 symbols have no names. We store the index of each one in the
9370 index field of the section, so that we can find it again when
9371 outputting relocs. */
9372 if (info
->strip
!= strip_all
9376 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9377 elfsym
.st_other
= 0;
9378 elfsym
.st_value
= 0;
9379 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9381 o
= bfd_section_from_elf_index (abfd
, i
);
9384 o
->target_index
= bfd_get_symcount (abfd
);
9385 elfsym
.st_shndx
= i
;
9386 if (!info
->relocatable
)
9387 elfsym
.st_value
= o
->vma
;
9388 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
9391 if (i
== SHN_LORESERVE
- 1)
9392 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
9396 /* Allocate some memory to hold information read in from the input
9398 if (max_contents_size
!= 0)
9400 finfo
.contents
= bfd_malloc (max_contents_size
);
9401 if (finfo
.contents
== NULL
)
9405 if (max_external_reloc_size
!= 0)
9407 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
9408 if (finfo
.external_relocs
== NULL
)
9412 if (max_internal_reloc_count
!= 0)
9414 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9415 amt
*= sizeof (Elf_Internal_Rela
);
9416 finfo
.internal_relocs
= bfd_malloc (amt
);
9417 if (finfo
.internal_relocs
== NULL
)
9421 if (max_sym_count
!= 0)
9423 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
9424 finfo
.external_syms
= bfd_malloc (amt
);
9425 if (finfo
.external_syms
== NULL
)
9428 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
9429 finfo
.internal_syms
= bfd_malloc (amt
);
9430 if (finfo
.internal_syms
== NULL
)
9433 amt
= max_sym_count
* sizeof (long);
9434 finfo
.indices
= bfd_malloc (amt
);
9435 if (finfo
.indices
== NULL
)
9438 amt
= max_sym_count
* sizeof (asection
*);
9439 finfo
.sections
= bfd_malloc (amt
);
9440 if (finfo
.sections
== NULL
)
9444 if (max_sym_shndx_count
!= 0)
9446 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
9447 finfo
.locsym_shndx
= bfd_malloc (amt
);
9448 if (finfo
.locsym_shndx
== NULL
)
9452 if (elf_hash_table (info
)->tls_sec
)
9454 bfd_vma base
, end
= 0;
9457 for (sec
= elf_hash_table (info
)->tls_sec
;
9458 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
9461 bfd_size_type size
= sec
->size
;
9464 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
9466 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
9468 size
= o
->offset
+ o
->size
;
9470 end
= sec
->vma
+ size
;
9472 base
= elf_hash_table (info
)->tls_sec
->vma
;
9473 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
9474 elf_hash_table (info
)->tls_size
= end
- base
;
9477 /* Reorder SHF_LINK_ORDER sections. */
9478 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9480 if (!elf_fixup_link_order (abfd
, o
))
9484 /* Since ELF permits relocations to be against local symbols, we
9485 must have the local symbols available when we do the relocations.
9486 Since we would rather only read the local symbols once, and we
9487 would rather not keep them in memory, we handle all the
9488 relocations for a single input file at the same time.
9490 Unfortunately, there is no way to know the total number of local
9491 symbols until we have seen all of them, and the local symbol
9492 indices precede the global symbol indices. This means that when
9493 we are generating relocatable output, and we see a reloc against
9494 a global symbol, we can not know the symbol index until we have
9495 finished examining all the local symbols to see which ones we are
9496 going to output. To deal with this, we keep the relocations in
9497 memory, and don't output them until the end of the link. This is
9498 an unfortunate waste of memory, but I don't see a good way around
9499 it. Fortunately, it only happens when performing a relocatable
9500 link, which is not the common case. FIXME: If keep_memory is set
9501 we could write the relocs out and then read them again; I don't
9502 know how bad the memory loss will be. */
9504 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9505 sub
->output_has_begun
= FALSE
;
9506 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9508 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9510 if (p
->type
== bfd_indirect_link_order
9511 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
9512 == bfd_target_elf_flavour
)
9513 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
9515 if (! sub
->output_has_begun
)
9517 if (! elf_link_input_bfd (&finfo
, sub
))
9519 sub
->output_has_begun
= TRUE
;
9522 else if (p
->type
== bfd_section_reloc_link_order
9523 || p
->type
== bfd_symbol_reloc_link_order
)
9525 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
9530 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
9536 /* Free symbol buffer if needed. */
9537 if (!info
->reduce_memory_overheads
)
9539 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9540 if (elf_tdata (sub
)->symbuf
)
9542 free (elf_tdata (sub
)->symbuf
);
9543 elf_tdata (sub
)->symbuf
= NULL
;
9547 /* Output any global symbols that got converted to local in a
9548 version script or due to symbol visibility. We do this in a
9549 separate step since ELF requires all local symbols to appear
9550 prior to any global symbols. FIXME: We should only do this if
9551 some global symbols were, in fact, converted to become local.
9552 FIXME: Will this work correctly with the Irix 5 linker? */
9553 eoinfo
.failed
= FALSE
;
9554 eoinfo
.finfo
= &finfo
;
9555 eoinfo
.localsyms
= TRUE
;
9556 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9561 /* If backend needs to output some local symbols not present in the hash
9562 table, do it now. */
9563 if (bed
->elf_backend_output_arch_local_syms
)
9565 typedef bfd_boolean (*out_sym_func
)
9566 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9567 struct elf_link_hash_entry
*);
9569 if (! ((*bed
->elf_backend_output_arch_local_syms
)
9570 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9574 /* That wrote out all the local symbols. Finish up the symbol table
9575 with the global symbols. Even if we want to strip everything we
9576 can, we still need to deal with those global symbols that got
9577 converted to local in a version script. */
9579 /* The sh_info field records the index of the first non local symbol. */
9580 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
9583 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
9585 Elf_Internal_Sym sym
;
9586 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
9587 long last_local
= 0;
9589 /* Write out the section symbols for the output sections. */
9590 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
9596 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9599 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9605 dynindx
= elf_section_data (s
)->dynindx
;
9608 indx
= elf_section_data (s
)->this_idx
;
9609 BFD_ASSERT (indx
> 0);
9610 sym
.st_shndx
= indx
;
9611 if (! check_dynsym (abfd
, &sym
))
9613 sym
.st_value
= s
->vma
;
9614 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
9615 if (last_local
< dynindx
)
9616 last_local
= dynindx
;
9617 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9621 /* Write out the local dynsyms. */
9622 if (elf_hash_table (info
)->dynlocal
)
9624 struct elf_link_local_dynamic_entry
*e
;
9625 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
9630 sym
.st_size
= e
->isym
.st_size
;
9631 sym
.st_other
= e
->isym
.st_other
;
9633 /* Copy the internal symbol as is.
9634 Note that we saved a word of storage and overwrote
9635 the original st_name with the dynstr_index. */
9638 if (e
->isym
.st_shndx
!= SHN_UNDEF
9639 && (e
->isym
.st_shndx
< SHN_LORESERVE
9640 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
9642 s
= bfd_section_from_elf_index (e
->input_bfd
,
9646 elf_section_data (s
->output_section
)->this_idx
;
9647 if (! check_dynsym (abfd
, &sym
))
9649 sym
.st_value
= (s
->output_section
->vma
9651 + e
->isym
.st_value
);
9654 if (last_local
< e
->dynindx
)
9655 last_local
= e
->dynindx
;
9657 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
9658 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9662 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
9666 /* We get the global symbols from the hash table. */
9667 eoinfo
.failed
= FALSE
;
9668 eoinfo
.localsyms
= FALSE
;
9669 eoinfo
.finfo
= &finfo
;
9670 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9675 /* If backend needs to output some symbols not present in the hash
9676 table, do it now. */
9677 if (bed
->elf_backend_output_arch_syms
)
9679 typedef bfd_boolean (*out_sym_func
)
9680 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9681 struct elf_link_hash_entry
*);
9683 if (! ((*bed
->elf_backend_output_arch_syms
)
9684 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9688 /* Flush all symbols to the file. */
9689 if (! elf_link_flush_output_syms (&finfo
, bed
))
9692 /* Now we know the size of the symtab section. */
9693 off
+= symtab_hdr
->sh_size
;
9695 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
9696 if (symtab_shndx_hdr
->sh_name
!= 0)
9698 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
9699 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
9700 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
9701 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
9702 symtab_shndx_hdr
->sh_size
= amt
;
9704 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
9707 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
9708 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
9713 /* Finish up and write out the symbol string table (.strtab)
9715 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
9716 /* sh_name was set in prep_headers. */
9717 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9718 symstrtab_hdr
->sh_flags
= 0;
9719 symstrtab_hdr
->sh_addr
= 0;
9720 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
9721 symstrtab_hdr
->sh_entsize
= 0;
9722 symstrtab_hdr
->sh_link
= 0;
9723 symstrtab_hdr
->sh_info
= 0;
9724 /* sh_offset is set just below. */
9725 symstrtab_hdr
->sh_addralign
= 1;
9727 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
9728 elf_tdata (abfd
)->next_file_pos
= off
;
9730 if (bfd_get_symcount (abfd
) > 0)
9732 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9733 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9737 /* Adjust the relocs to have the correct symbol indices. */
9738 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9740 if ((o
->flags
& SEC_RELOC
) == 0)
9743 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9744 elf_section_data (o
)->rel_count
,
9745 elf_section_data (o
)->rel_hashes
);
9746 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9747 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9748 elf_section_data (o
)->rel_count2
,
9749 (elf_section_data (o
)->rel_hashes
9750 + elf_section_data (o
)->rel_count
));
9752 /* Set the reloc_count field to 0 to prevent write_relocs from
9753 trying to swap the relocs out itself. */
9757 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
9758 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
9760 /* If we are linking against a dynamic object, or generating a
9761 shared library, finish up the dynamic linking information. */
9764 bfd_byte
*dyncon
, *dynconend
;
9766 /* Fix up .dynamic entries. */
9767 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9768 BFD_ASSERT (o
!= NULL
);
9770 dyncon
= o
->contents
;
9771 dynconend
= o
->contents
+ o
->size
;
9772 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9774 Elf_Internal_Dyn dyn
;
9778 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9785 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
9787 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
9789 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
9790 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
9793 dyn
.d_un
.d_val
= relativecount
;
9800 name
= info
->init_function
;
9803 name
= info
->fini_function
;
9806 struct elf_link_hash_entry
*h
;
9808 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
9809 FALSE
, FALSE
, TRUE
);
9811 && (h
->root
.type
== bfd_link_hash_defined
9812 || h
->root
.type
== bfd_link_hash_defweak
))
9814 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
9815 o
= h
->root
.u
.def
.section
;
9816 if (o
->output_section
!= NULL
)
9817 dyn
.d_un
.d_val
+= (o
->output_section
->vma
9818 + o
->output_offset
);
9821 /* The symbol is imported from another shared
9822 library and does not apply to this one. */
9830 case DT_PREINIT_ARRAYSZ
:
9831 name
= ".preinit_array";
9833 case DT_INIT_ARRAYSZ
:
9834 name
= ".init_array";
9836 case DT_FINI_ARRAYSZ
:
9837 name
= ".fini_array";
9839 o
= bfd_get_section_by_name (abfd
, name
);
9842 (*_bfd_error_handler
)
9843 (_("%B: could not find output section %s"), abfd
, name
);
9847 (*_bfd_error_handler
)
9848 (_("warning: %s section has zero size"), name
);
9849 dyn
.d_un
.d_val
= o
->size
;
9852 case DT_PREINIT_ARRAY
:
9853 name
= ".preinit_array";
9856 name
= ".init_array";
9859 name
= ".fini_array";
9875 name
= ".gnu.version_d";
9878 name
= ".gnu.version_r";
9881 name
= ".gnu.version";
9883 o
= bfd_get_section_by_name (abfd
, name
);
9886 (*_bfd_error_handler
)
9887 (_("%B: could not find output section %s"), abfd
, name
);
9890 dyn
.d_un
.d_ptr
= o
->vma
;
9897 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9902 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9904 Elf_Internal_Shdr
*hdr
;
9906 hdr
= elf_elfsections (abfd
)[i
];
9907 if (hdr
->sh_type
== type
9908 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9910 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9911 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9914 if (dyn
.d_un
.d_val
== 0
9915 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9916 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9922 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9926 /* If we have created any dynamic sections, then output them. */
9929 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9932 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9933 if (info
->warn_shared_textrel
&& info
->shared
)
9935 bfd_byte
*dyncon
, *dynconend
;
9937 /* Fix up .dynamic entries. */
9938 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9939 BFD_ASSERT (o
!= NULL
);
9941 dyncon
= o
->contents
;
9942 dynconend
= o
->contents
+ o
->size
;
9943 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9945 Elf_Internal_Dyn dyn
;
9947 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9949 if (dyn
.d_tag
== DT_TEXTREL
)
9952 (_("warning: creating a DT_TEXTREL in a shared object."));
9958 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9960 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9962 || o
->output_section
== bfd_abs_section_ptr
)
9964 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9966 /* At this point, we are only interested in sections
9967 created by _bfd_elf_link_create_dynamic_sections. */
9970 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9972 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9974 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9976 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9978 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9980 (file_ptr
) o
->output_offset
,
9986 /* The contents of the .dynstr section are actually in a
9988 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9989 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9990 || ! _bfd_elf_strtab_emit (abfd
,
9991 elf_hash_table (info
)->dynstr
))
9997 if (info
->relocatable
)
9999 bfd_boolean failed
= FALSE
;
10001 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10006 /* If we have optimized stabs strings, output them. */
10007 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10009 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10013 if (info
->eh_frame_hdr
)
10015 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10019 if (finfo
.symstrtab
!= NULL
)
10020 _bfd_stringtab_free (finfo
.symstrtab
);
10021 if (finfo
.contents
!= NULL
)
10022 free (finfo
.contents
);
10023 if (finfo
.external_relocs
!= NULL
)
10024 free (finfo
.external_relocs
);
10025 if (finfo
.internal_relocs
!= NULL
)
10026 free (finfo
.internal_relocs
);
10027 if (finfo
.external_syms
!= NULL
)
10028 free (finfo
.external_syms
);
10029 if (finfo
.locsym_shndx
!= NULL
)
10030 free (finfo
.locsym_shndx
);
10031 if (finfo
.internal_syms
!= NULL
)
10032 free (finfo
.internal_syms
);
10033 if (finfo
.indices
!= NULL
)
10034 free (finfo
.indices
);
10035 if (finfo
.sections
!= NULL
)
10036 free (finfo
.sections
);
10037 if (finfo
.symbuf
!= NULL
)
10038 free (finfo
.symbuf
);
10039 if (finfo
.symshndxbuf
!= NULL
)
10040 free (finfo
.symshndxbuf
);
10041 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10043 if ((o
->flags
& SEC_RELOC
) != 0
10044 && elf_section_data (o
)->rel_hashes
!= NULL
)
10045 free (elf_section_data (o
)->rel_hashes
);
10048 elf_tdata (abfd
)->linker
= TRUE
;
10053 if (finfo
.symstrtab
!= NULL
)
10054 _bfd_stringtab_free (finfo
.symstrtab
);
10055 if (finfo
.contents
!= NULL
)
10056 free (finfo
.contents
);
10057 if (finfo
.external_relocs
!= NULL
)
10058 free (finfo
.external_relocs
);
10059 if (finfo
.internal_relocs
!= NULL
)
10060 free (finfo
.internal_relocs
);
10061 if (finfo
.external_syms
!= NULL
)
10062 free (finfo
.external_syms
);
10063 if (finfo
.locsym_shndx
!= NULL
)
10064 free (finfo
.locsym_shndx
);
10065 if (finfo
.internal_syms
!= NULL
)
10066 free (finfo
.internal_syms
);
10067 if (finfo
.indices
!= NULL
)
10068 free (finfo
.indices
);
10069 if (finfo
.sections
!= NULL
)
10070 free (finfo
.sections
);
10071 if (finfo
.symbuf
!= NULL
)
10072 free (finfo
.symbuf
);
10073 if (finfo
.symshndxbuf
!= NULL
)
10074 free (finfo
.symshndxbuf
);
10075 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10077 if ((o
->flags
& SEC_RELOC
) != 0
10078 && elf_section_data (o
)->rel_hashes
!= NULL
)
10079 free (elf_section_data (o
)->rel_hashes
);
10085 /* Garbage collect unused sections. */
10087 typedef asection
* (*gc_mark_hook_fn
)
10088 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
10089 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
10091 /* Default gc_mark_hook. */
10094 _bfd_elf_gc_mark_hook (asection
*sec
,
10095 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10096 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
10097 struct elf_link_hash_entry
*h
,
10098 Elf_Internal_Sym
*sym
)
10102 switch (h
->root
.type
)
10104 case bfd_link_hash_defined
:
10105 case bfd_link_hash_defweak
:
10106 return h
->root
.u
.def
.section
;
10108 case bfd_link_hash_common
:
10109 return h
->root
.u
.c
.p
->section
;
10116 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
10121 /* The mark phase of garbage collection. For a given section, mark
10122 it and any sections in this section's group, and all the sections
10123 which define symbols to which it refers. */
10126 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
10128 gc_mark_hook_fn gc_mark_hook
)
10132 asection
*group_sec
;
10136 /* Mark all the sections in the group. */
10137 group_sec
= elf_section_data (sec
)->next_in_group
;
10138 if (group_sec
&& !group_sec
->gc_mark
)
10139 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
10142 /* Look through the section relocs. */
10144 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
10145 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
10147 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
10148 Elf_Internal_Shdr
*symtab_hdr
;
10149 struct elf_link_hash_entry
**sym_hashes
;
10152 bfd
*input_bfd
= sec
->owner
;
10153 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
10154 Elf_Internal_Sym
*isym
= NULL
;
10157 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10158 sym_hashes
= elf_sym_hashes (input_bfd
);
10160 /* Read the local symbols. */
10161 if (elf_bad_symtab (input_bfd
))
10163 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10167 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
10169 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10170 if (isym
== NULL
&& nlocsyms
!= 0)
10172 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
10178 /* Read the relocations. */
10179 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
10180 info
->keep_memory
);
10181 if (relstart
== NULL
)
10186 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10188 if (bed
->s
->arch_size
== 32)
10193 for (rel
= relstart
; rel
< relend
; rel
++)
10195 unsigned long r_symndx
;
10197 struct elf_link_hash_entry
*h
;
10199 r_symndx
= rel
->r_info
>> r_sym_shift
;
10203 if (r_symndx
>= nlocsyms
10204 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
10206 h
= sym_hashes
[r_symndx
- extsymoff
];
10207 while (h
->root
.type
== bfd_link_hash_indirect
10208 || h
->root
.type
== bfd_link_hash_warning
)
10209 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10210 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
10214 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
10217 if (rsec
&& !rsec
->gc_mark
)
10219 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
10222 rsec
->gc_mark_from_eh
= 1;
10223 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
10232 if (elf_section_data (sec
)->relocs
!= relstart
)
10235 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
10237 if (! info
->keep_memory
)
10240 symtab_hdr
->contents
= (unsigned char *) isym
;
10247 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10249 struct elf_gc_sweep_symbol_info
10251 struct bfd_link_info
*info
;
10252 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
10257 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
10259 if (h
->root
.type
== bfd_link_hash_warning
)
10260 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10262 if ((h
->root
.type
== bfd_link_hash_defined
10263 || h
->root
.type
== bfd_link_hash_defweak
)
10264 && !h
->root
.u
.def
.section
->gc_mark
10265 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
10267 struct elf_gc_sweep_symbol_info
*inf
= data
;
10268 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
10274 /* The sweep phase of garbage collection. Remove all garbage sections. */
10276 typedef bfd_boolean (*gc_sweep_hook_fn
)
10277 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
10280 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
10283 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10284 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
10285 unsigned long section_sym_count
;
10286 struct elf_gc_sweep_symbol_info sweep_info
;
10288 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10292 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10295 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10297 /* Keep debug and special sections. */
10298 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
10299 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
10305 /* Skip sweeping sections already excluded. */
10306 if (o
->flags
& SEC_EXCLUDE
)
10309 /* Since this is early in the link process, it is simple
10310 to remove a section from the output. */
10311 o
->flags
|= SEC_EXCLUDE
;
10313 if (info
->print_gc_sections
== TRUE
)
10314 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
10316 /* But we also have to update some of the relocation
10317 info we collected before. */
10319 && (o
->flags
& SEC_RELOC
) != 0
10320 && o
->reloc_count
> 0
10321 && !bfd_is_abs_section (o
->output_section
))
10323 Elf_Internal_Rela
*internal_relocs
;
10327 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
10328 info
->keep_memory
);
10329 if (internal_relocs
== NULL
)
10332 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
10334 if (elf_section_data (o
)->relocs
!= internal_relocs
)
10335 free (internal_relocs
);
10343 /* Remove the symbols that were in the swept sections from the dynamic
10344 symbol table. GCFIXME: Anyone know how to get them out of the
10345 static symbol table as well? */
10346 sweep_info
.info
= info
;
10347 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
10348 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
10351 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
10355 /* Propagate collected vtable information. This is called through
10356 elf_link_hash_traverse. */
10359 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
10361 if (h
->root
.type
== bfd_link_hash_warning
)
10362 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10364 /* Those that are not vtables. */
10365 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10368 /* Those vtables that do not have parents, we cannot merge. */
10369 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
10372 /* If we've already been done, exit. */
10373 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
10376 /* Make sure the parent's table is up to date. */
10377 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
10379 if (h
->vtable
->used
== NULL
)
10381 /* None of this table's entries were referenced. Re-use the
10383 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
10384 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
10389 bfd_boolean
*cu
, *pu
;
10391 /* Or the parent's entries into ours. */
10392 cu
= h
->vtable
->used
;
10394 pu
= h
->vtable
->parent
->vtable
->used
;
10397 const struct elf_backend_data
*bed
;
10398 unsigned int log_file_align
;
10400 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
10401 log_file_align
= bed
->s
->log_file_align
;
10402 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
10417 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
10420 bfd_vma hstart
, hend
;
10421 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
10422 const struct elf_backend_data
*bed
;
10423 unsigned int log_file_align
;
10425 if (h
->root
.type
== bfd_link_hash_warning
)
10426 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10428 /* Take care of both those symbols that do not describe vtables as
10429 well as those that are not loaded. */
10430 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10433 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
10434 || h
->root
.type
== bfd_link_hash_defweak
);
10436 sec
= h
->root
.u
.def
.section
;
10437 hstart
= h
->root
.u
.def
.value
;
10438 hend
= hstart
+ h
->size
;
10440 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
10442 return *(bfd_boolean
*) okp
= FALSE
;
10443 bed
= get_elf_backend_data (sec
->owner
);
10444 log_file_align
= bed
->s
->log_file_align
;
10446 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10448 for (rel
= relstart
; rel
< relend
; ++rel
)
10449 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
10451 /* If the entry is in use, do nothing. */
10452 if (h
->vtable
->used
10453 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
10455 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
10456 if (h
->vtable
->used
[entry
])
10459 /* Otherwise, kill it. */
10460 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
10466 /* Mark sections containing dynamically referenced symbols. When
10467 building shared libraries, we must assume that any visible symbol is
10471 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
10473 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
10475 if (h
->root
.type
== bfd_link_hash_warning
)
10476 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10478 if ((h
->root
.type
== bfd_link_hash_defined
10479 || h
->root
.type
== bfd_link_hash_defweak
)
10481 || (!info
->executable
10483 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
10484 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
10485 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
10490 /* Do mark and sweep of unused sections. */
10493 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
10495 bfd_boolean ok
= TRUE
;
10497 asection
* (*gc_mark_hook
)
10498 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
10499 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
10500 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10502 if (!bed
->can_gc_sections
10503 || info
->relocatable
10504 || info
->emitrelocations
10505 || !is_elf_hash_table (info
->hash
))
10507 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
10511 /* Apply transitive closure to the vtable entry usage info. */
10512 elf_link_hash_traverse (elf_hash_table (info
),
10513 elf_gc_propagate_vtable_entries_used
,
10518 /* Kill the vtable relocations that were not used. */
10519 elf_link_hash_traverse (elf_hash_table (info
),
10520 elf_gc_smash_unused_vtentry_relocs
,
10525 /* Mark dynamically referenced symbols. */
10526 if (elf_hash_table (info
)->dynamic_sections_created
)
10527 elf_link_hash_traverse (elf_hash_table (info
),
10528 bed
->gc_mark_dynamic_ref
,
10531 /* Grovel through relocs to find out who stays ... */
10532 gc_mark_hook
= bed
->gc_mark_hook
;
10533 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10537 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10540 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10541 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
10542 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10546 /* ... again for sections marked from eh_frame. */
10547 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10551 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10554 /* Keep .gcc_except_table.* if the associated .text.* (or the
10555 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10556 marked. This isn't very nice, but the proper solution,
10557 splitting .eh_frame up and using comdat doesn't pan out
10558 easily due to needing special relocs to handle the
10559 difference of two symbols in separate sections.
10560 Don't keep code sections referenced by .eh_frame. */
10561 #define TEXT_PREFIX ".text."
10562 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10563 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10564 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10565 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
10567 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
10570 const char *sec_name
;
10572 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
10574 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
10575 sec_name
= o
->name
+ o_name_prefix_len
;
10576 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
10577 tmp
= strlen (TEXT_PREFIX2
);
10578 if (tmp
> fn_name_prefix_len
)
10579 fn_name_prefix_len
= tmp
;
10581 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
10582 if (fn_name
== NULL
)
10585 /* Try the first prefix. */
10586 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
10587 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10589 /* Try the second prefix. */
10590 if (fn_text
== NULL
)
10592 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
10593 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10597 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
10601 /* If not using specially named exception table section,
10602 then keep whatever we are using. */
10603 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10608 /* ... and mark SEC_EXCLUDE for those that go. */
10609 return elf_gc_sweep (abfd
, info
);
10612 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10615 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
10617 struct elf_link_hash_entry
*h
,
10620 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
10621 struct elf_link_hash_entry
**search
, *child
;
10622 bfd_size_type extsymcount
;
10623 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10625 /* The sh_info field of the symtab header tells us where the
10626 external symbols start. We don't care about the local symbols at
10628 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
10629 if (!elf_bad_symtab (abfd
))
10630 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
10632 sym_hashes
= elf_sym_hashes (abfd
);
10633 sym_hashes_end
= sym_hashes
+ extsymcount
;
10635 /* Hunt down the child symbol, which is in this section at the same
10636 offset as the relocation. */
10637 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
10639 if ((child
= *search
) != NULL
10640 && (child
->root
.type
== bfd_link_hash_defined
10641 || child
->root
.type
== bfd_link_hash_defweak
)
10642 && child
->root
.u
.def
.section
== sec
10643 && child
->root
.u
.def
.value
== offset
)
10647 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
10648 abfd
, sec
, (unsigned long) offset
);
10649 bfd_set_error (bfd_error_invalid_operation
);
10653 if (!child
->vtable
)
10655 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
10656 if (!child
->vtable
)
10661 /* This *should* only be the absolute section. It could potentially
10662 be that someone has defined a non-global vtable though, which
10663 would be bad. It isn't worth paging in the local symbols to be
10664 sure though; that case should simply be handled by the assembler. */
10666 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
10669 child
->vtable
->parent
= h
;
10674 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10677 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
10678 asection
*sec ATTRIBUTE_UNUSED
,
10679 struct elf_link_hash_entry
*h
,
10682 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10683 unsigned int log_file_align
= bed
->s
->log_file_align
;
10687 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
10692 if (addend
>= h
->vtable
->size
)
10694 size_t size
, bytes
, file_align
;
10695 bfd_boolean
*ptr
= h
->vtable
->used
;
10697 /* While the symbol is undefined, we have to be prepared to handle
10699 file_align
= 1 << log_file_align
;
10700 if (h
->root
.type
== bfd_link_hash_undefined
)
10701 size
= addend
+ file_align
;
10705 if (addend
>= size
)
10707 /* Oops! We've got a reference past the defined end of
10708 the table. This is probably a bug -- shall we warn? */
10709 size
= addend
+ file_align
;
10712 size
= (size
+ file_align
- 1) & -file_align
;
10714 /* Allocate one extra entry for use as a "done" flag for the
10715 consolidation pass. */
10716 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
10720 ptr
= bfd_realloc (ptr
- 1, bytes
);
10726 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
10727 * sizeof (bfd_boolean
));
10728 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
10732 ptr
= bfd_zmalloc (bytes
);
10737 /* And arrange for that done flag to be at index -1. */
10738 h
->vtable
->used
= ptr
+ 1;
10739 h
->vtable
->size
= size
;
10742 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
10747 struct alloc_got_off_arg
{
10749 unsigned int got_elt_size
;
10752 /* We need a special top-level link routine to convert got reference counts
10753 to real got offsets. */
10756 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
10758 struct alloc_got_off_arg
*gofarg
= arg
;
10760 if (h
->root
.type
== bfd_link_hash_warning
)
10761 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10763 if (h
->got
.refcount
> 0)
10765 h
->got
.offset
= gofarg
->gotoff
;
10766 gofarg
->gotoff
+= gofarg
->got_elt_size
;
10769 h
->got
.offset
= (bfd_vma
) -1;
10774 /* And an accompanying bit to work out final got entry offsets once
10775 we're done. Should be called from final_link. */
10778 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
10779 struct bfd_link_info
*info
)
10782 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10784 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
10785 struct alloc_got_off_arg gofarg
;
10787 if (! is_elf_hash_table (info
->hash
))
10790 /* The GOT offset is relative to the .got section, but the GOT header is
10791 put into the .got.plt section, if the backend uses it. */
10792 if (bed
->want_got_plt
)
10795 gotoff
= bed
->got_header_size
;
10797 /* Do the local .got entries first. */
10798 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
10800 bfd_signed_vma
*local_got
;
10801 bfd_size_type j
, locsymcount
;
10802 Elf_Internal_Shdr
*symtab_hdr
;
10804 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
10807 local_got
= elf_local_got_refcounts (i
);
10811 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
10812 if (elf_bad_symtab (i
))
10813 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10815 locsymcount
= symtab_hdr
->sh_info
;
10817 for (j
= 0; j
< locsymcount
; ++j
)
10819 if (local_got
[j
] > 0)
10821 local_got
[j
] = gotoff
;
10822 gotoff
+= got_elt_size
;
10825 local_got
[j
] = (bfd_vma
) -1;
10829 /* Then the global .got entries. .plt refcounts are handled by
10830 adjust_dynamic_symbol */
10831 gofarg
.gotoff
= gotoff
;
10832 gofarg
.got_elt_size
= got_elt_size
;
10833 elf_link_hash_traverse (elf_hash_table (info
),
10834 elf_gc_allocate_got_offsets
,
10839 /* Many folk need no more in the way of final link than this, once
10840 got entry reference counting is enabled. */
10843 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10845 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
10848 /* Invoke the regular ELF backend linker to do all the work. */
10849 return bfd_elf_final_link (abfd
, info
);
10853 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
10855 struct elf_reloc_cookie
*rcookie
= cookie
;
10857 if (rcookie
->bad_symtab
)
10858 rcookie
->rel
= rcookie
->rels
;
10860 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
10862 unsigned long r_symndx
;
10864 if (! rcookie
->bad_symtab
)
10865 if (rcookie
->rel
->r_offset
> offset
)
10867 if (rcookie
->rel
->r_offset
!= offset
)
10870 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
10871 if (r_symndx
== SHN_UNDEF
)
10874 if (r_symndx
>= rcookie
->locsymcount
10875 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
10877 struct elf_link_hash_entry
*h
;
10879 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
10881 while (h
->root
.type
== bfd_link_hash_indirect
10882 || h
->root
.type
== bfd_link_hash_warning
)
10883 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10885 if ((h
->root
.type
== bfd_link_hash_defined
10886 || h
->root
.type
== bfd_link_hash_defweak
)
10887 && elf_discarded_section (h
->root
.u
.def
.section
))
10894 /* It's not a relocation against a global symbol,
10895 but it could be a relocation against a local
10896 symbol for a discarded section. */
10898 Elf_Internal_Sym
*isym
;
10900 /* Need to: get the symbol; get the section. */
10901 isym
= &rcookie
->locsyms
[r_symndx
];
10902 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
10904 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
10905 if (isec
!= NULL
&& elf_discarded_section (isec
))
10914 /* Discard unneeded references to discarded sections.
10915 Returns TRUE if any section's size was changed. */
10916 /* This function assumes that the relocations are in sorted order,
10917 which is true for all known assemblers. */
10920 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
10922 struct elf_reloc_cookie cookie
;
10923 asection
*stab
, *eh
;
10924 Elf_Internal_Shdr
*symtab_hdr
;
10925 const struct elf_backend_data
*bed
;
10927 unsigned int count
;
10928 bfd_boolean ret
= FALSE
;
10930 if (info
->traditional_format
10931 || !is_elf_hash_table (info
->hash
))
10934 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10936 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10939 bed
= get_elf_backend_data (abfd
);
10941 if ((abfd
->flags
& DYNAMIC
) != 0)
10945 if (!info
->relocatable
)
10947 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10950 || bfd_is_abs_section (eh
->output_section
)))
10954 stab
= bfd_get_section_by_name (abfd
, ".stab");
10956 && (stab
->size
== 0
10957 || bfd_is_abs_section (stab
->output_section
)
10958 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10963 && bed
->elf_backend_discard_info
== NULL
)
10966 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10967 cookie
.abfd
= abfd
;
10968 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10969 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10970 if (cookie
.bad_symtab
)
10972 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10973 cookie
.extsymoff
= 0;
10977 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10978 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10981 if (bed
->s
->arch_size
== 32)
10982 cookie
.r_sym_shift
= 8;
10984 cookie
.r_sym_shift
= 32;
10986 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10987 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10989 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10990 cookie
.locsymcount
, 0,
10992 if (cookie
.locsyms
== NULL
)
10998 cookie
.rels
= NULL
;
10999 count
= stab
->reloc_count
;
11001 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
11002 info
->keep_memory
);
11003 if (cookie
.rels
!= NULL
)
11005 cookie
.rel
= cookie
.rels
;
11006 cookie
.relend
= cookie
.rels
;
11007 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11008 if (_bfd_discard_section_stabs (abfd
, stab
,
11009 elf_section_data (stab
)->sec_info
,
11010 bfd_elf_reloc_symbol_deleted_p
,
11013 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
11014 free (cookie
.rels
);
11020 cookie
.rels
= NULL
;
11021 count
= eh
->reloc_count
;
11023 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
11024 info
->keep_memory
);
11025 cookie
.rel
= cookie
.rels
;
11026 cookie
.relend
= cookie
.rels
;
11027 if (cookie
.rels
!= NULL
)
11028 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11030 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11031 bfd_elf_reloc_symbol_deleted_p
,
11035 if (cookie
.rels
!= NULL
11036 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
11037 free (cookie
.rels
);
11040 if (bed
->elf_backend_discard_info
!= NULL
11041 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11044 if (cookie
.locsyms
!= NULL
11045 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
11047 if (! info
->keep_memory
)
11048 free (cookie
.locsyms
);
11050 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
11054 if (info
->eh_frame_hdr
11055 && !info
->relocatable
11056 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
11063 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
11064 struct bfd_link_info
*info
)
11067 const char *name
, *p
;
11068 struct bfd_section_already_linked
*l
;
11069 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
11071 if (sec
->output_section
== bfd_abs_section_ptr
)
11074 flags
= sec
->flags
;
11076 /* Return if it isn't a linkonce section. A comdat group section
11077 also has SEC_LINK_ONCE set. */
11078 if ((flags
& SEC_LINK_ONCE
) == 0)
11081 /* Don't put group member sections on our list of already linked
11082 sections. They are handled as a group via their group section. */
11083 if (elf_sec_group (sec
) != NULL
)
11086 /* FIXME: When doing a relocatable link, we may have trouble
11087 copying relocations in other sections that refer to local symbols
11088 in the section being discarded. Those relocations will have to
11089 be converted somehow; as of this writing I'm not sure that any of
11090 the backends handle that correctly.
11092 It is tempting to instead not discard link once sections when
11093 doing a relocatable link (technically, they should be discarded
11094 whenever we are building constructors). However, that fails,
11095 because the linker winds up combining all the link once sections
11096 into a single large link once section, which defeats the purpose
11097 of having link once sections in the first place.
11099 Also, not merging link once sections in a relocatable link
11100 causes trouble for MIPS ELF, which relies on link once semantics
11101 to handle the .reginfo section correctly. */
11103 name
= bfd_get_section_name (abfd
, sec
);
11105 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
11106 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
11111 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
11113 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11115 /* We may have 2 different types of sections on the list: group
11116 sections and linkonce sections. Match like sections. */
11117 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
11118 && strcmp (name
, l
->sec
->name
) == 0
11119 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
11121 /* The section has already been linked. See if we should
11122 issue a warning. */
11123 switch (flags
& SEC_LINK_DUPLICATES
)
11128 case SEC_LINK_DUPLICATES_DISCARD
:
11131 case SEC_LINK_DUPLICATES_ONE_ONLY
:
11132 (*_bfd_error_handler
)
11133 (_("%B: ignoring duplicate section `%A'"),
11137 case SEC_LINK_DUPLICATES_SAME_SIZE
:
11138 if (sec
->size
!= l
->sec
->size
)
11139 (*_bfd_error_handler
)
11140 (_("%B: duplicate section `%A' has different size"),
11144 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
11145 if (sec
->size
!= l
->sec
->size
)
11146 (*_bfd_error_handler
)
11147 (_("%B: duplicate section `%A' has different size"),
11149 else if (sec
->size
!= 0)
11151 bfd_byte
*sec_contents
, *l_sec_contents
;
11153 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
11154 (*_bfd_error_handler
)
11155 (_("%B: warning: could not read contents of section `%A'"),
11157 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
11159 (*_bfd_error_handler
)
11160 (_("%B: warning: could not read contents of section `%A'"),
11161 l
->sec
->owner
, l
->sec
);
11162 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
11163 (*_bfd_error_handler
)
11164 (_("%B: warning: duplicate section `%A' has different contents"),
11168 free (sec_contents
);
11169 if (l_sec_contents
)
11170 free (l_sec_contents
);
11175 /* Set the output_section field so that lang_add_section
11176 does not create a lang_input_section structure for this
11177 section. Since there might be a symbol in the section
11178 being discarded, we must retain a pointer to the section
11179 which we are really going to use. */
11180 sec
->output_section
= bfd_abs_section_ptr
;
11181 sec
->kept_section
= l
->sec
;
11183 if (flags
& SEC_GROUP
)
11185 asection
*first
= elf_next_in_group (sec
);
11186 asection
*s
= first
;
11190 s
->output_section
= bfd_abs_section_ptr
;
11191 /* Record which group discards it. */
11192 s
->kept_section
= l
->sec
;
11193 s
= elf_next_in_group (s
);
11194 /* These lists are circular. */
11204 /* A single member comdat group section may be discarded by a
11205 linkonce section and vice versa. */
11207 if ((flags
& SEC_GROUP
) != 0)
11209 asection
*first
= elf_next_in_group (sec
);
11211 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
11212 /* Check this single member group against linkonce sections. */
11213 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11214 if ((l
->sec
->flags
& SEC_GROUP
) == 0
11215 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
11216 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
11218 first
->output_section
= bfd_abs_section_ptr
;
11219 first
->kept_section
= l
->sec
;
11220 sec
->output_section
= bfd_abs_section_ptr
;
11225 /* Check this linkonce section against single member groups. */
11226 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11227 if (l
->sec
->flags
& SEC_GROUP
)
11229 asection
*first
= elf_next_in_group (l
->sec
);
11232 && elf_next_in_group (first
) == first
11233 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
11235 sec
->output_section
= bfd_abs_section_ptr
;
11236 sec
->kept_section
= first
;
11241 /* This is the first section with this name. Record it. */
11242 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
11246 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
11248 return sym
->st_shndx
== SHN_COMMON
;
11252 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
11258 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
11260 return bfd_com_section_ptr
;