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 /* Check if HI is a warning symbol. */
1560 if (hi
->root
.type
== bfd_link_hash_warning
)
1561 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1563 /* If there is a duplicate definition somewhere, then HI may not
1564 point to an indirect symbol. We will have reported an error to
1565 the user in that case. */
1567 if (hi
->root
.type
== bfd_link_hash_indirect
)
1569 struct elf_link_hash_entry
*ht
;
1571 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1572 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1574 /* See if the new flags lead us to realize that the symbol must
1586 if (hi
->ref_regular
)
1592 /* We also need to define an indirection from the nondefault version
1596 len
= strlen (name
);
1597 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1598 if (shortname
== NULL
)
1600 memcpy (shortname
, name
, shortlen
);
1601 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1603 /* Once again, merge with any existing symbol. */
1604 type_change_ok
= FALSE
;
1605 size_change_ok
= FALSE
;
1607 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1608 NULL
, &hi
, &skip
, &override
,
1609 &type_change_ok
, &size_change_ok
))
1617 /* Here SHORTNAME is a versioned name, so we don't expect to see
1618 the type of override we do in the case above unless it is
1619 overridden by a versioned definition. */
1620 if (hi
->root
.type
!= bfd_link_hash_defined
1621 && hi
->root
.type
!= bfd_link_hash_defweak
)
1622 (*_bfd_error_handler
)
1623 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1629 if (! (_bfd_generic_link_add_one_symbol
1630 (info
, abfd
, shortname
, BSF_INDIRECT
,
1631 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1633 hi
= (struct elf_link_hash_entry
*) bh
;
1635 /* If there is a duplicate definition somewhere, then HI may not
1636 point to an indirect symbol. We will have reported an error
1637 to the user in that case. */
1639 if (hi
->root
.type
== bfd_link_hash_indirect
)
1641 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1643 /* See if the new flags lead us to realize that the symbol
1655 if (hi
->ref_regular
)
1665 /* This routine is used to export all defined symbols into the dynamic
1666 symbol table. It is called via elf_link_hash_traverse. */
1669 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1671 struct elf_info_failed
*eif
= data
;
1673 /* Ignore this if we won't export it. */
1674 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1677 /* Ignore indirect symbols. These are added by the versioning code. */
1678 if (h
->root
.type
== bfd_link_hash_indirect
)
1681 if (h
->root
.type
== bfd_link_hash_warning
)
1682 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1684 if (h
->dynindx
== -1
1688 struct bfd_elf_version_tree
*t
;
1689 struct bfd_elf_version_expr
*d
;
1691 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1693 if (t
->globals
.list
!= NULL
)
1695 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1700 if (t
->locals
.list
!= NULL
)
1702 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1711 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1722 /* Look through the symbols which are defined in other shared
1723 libraries and referenced here. Update the list of version
1724 dependencies. This will be put into the .gnu.version_r section.
1725 This function is called via elf_link_hash_traverse. */
1728 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1731 struct elf_find_verdep_info
*rinfo
= data
;
1732 Elf_Internal_Verneed
*t
;
1733 Elf_Internal_Vernaux
*a
;
1736 if (h
->root
.type
== bfd_link_hash_warning
)
1737 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1739 /* We only care about symbols defined in shared objects with version
1744 || h
->verinfo
.verdef
== NULL
)
1747 /* See if we already know about this version. */
1748 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1750 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1753 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1754 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1760 /* This is a new version. Add it to tree we are building. */
1765 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1768 rinfo
->failed
= TRUE
;
1772 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1773 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1774 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1778 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1780 /* Note that we are copying a string pointer here, and testing it
1781 above. If bfd_elf_string_from_elf_section is ever changed to
1782 discard the string data when low in memory, this will have to be
1784 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1786 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1787 a
->vna_nextptr
= t
->vn_auxptr
;
1789 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1792 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1799 /* Figure out appropriate versions for all the symbols. We may not
1800 have the version number script until we have read all of the input
1801 files, so until that point we don't know which symbols should be
1802 local. This function is called via elf_link_hash_traverse. */
1805 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1807 struct elf_assign_sym_version_info
*sinfo
;
1808 struct bfd_link_info
*info
;
1809 const struct elf_backend_data
*bed
;
1810 struct elf_info_failed eif
;
1817 if (h
->root
.type
== bfd_link_hash_warning
)
1818 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1820 /* Fix the symbol flags. */
1823 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1826 sinfo
->failed
= TRUE
;
1830 /* We only need version numbers for symbols defined in regular
1832 if (!h
->def_regular
)
1835 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1836 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1837 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1839 struct bfd_elf_version_tree
*t
;
1844 /* There are two consecutive ELF_VER_CHR characters if this is
1845 not a hidden symbol. */
1847 if (*p
== ELF_VER_CHR
)
1853 /* If there is no version string, we can just return out. */
1861 /* Look for the version. If we find it, it is no longer weak. */
1862 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1864 if (strcmp (t
->name
, p
) == 0)
1868 struct bfd_elf_version_expr
*d
;
1870 len
= p
- h
->root
.root
.string
;
1871 alc
= bfd_malloc (len
);
1874 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1875 alc
[len
- 1] = '\0';
1876 if (alc
[len
- 2] == ELF_VER_CHR
)
1877 alc
[len
- 2] = '\0';
1879 h
->verinfo
.vertree
= t
;
1883 if (t
->globals
.list
!= NULL
)
1884 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1886 /* See if there is anything to force this symbol to
1888 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1890 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1893 && ! info
->export_dynamic
)
1894 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1902 /* If we are building an application, we need to create a
1903 version node for this version. */
1904 if (t
== NULL
&& info
->executable
)
1906 struct bfd_elf_version_tree
**pp
;
1909 /* If we aren't going to export this symbol, we don't need
1910 to worry about it. */
1911 if (h
->dynindx
== -1)
1915 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1918 sinfo
->failed
= TRUE
;
1923 t
->name_indx
= (unsigned int) -1;
1927 /* Don't count anonymous version tag. */
1928 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1930 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1932 t
->vernum
= version_index
;
1936 h
->verinfo
.vertree
= t
;
1940 /* We could not find the version for a symbol when
1941 generating a shared archive. Return an error. */
1942 (*_bfd_error_handler
)
1943 (_("%B: undefined versioned symbol name %s"),
1944 sinfo
->output_bfd
, h
->root
.root
.string
);
1945 bfd_set_error (bfd_error_bad_value
);
1946 sinfo
->failed
= TRUE
;
1954 /* If we don't have a version for this symbol, see if we can find
1956 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1958 struct bfd_elf_version_tree
*t
;
1959 struct bfd_elf_version_tree
*local_ver
;
1960 struct bfd_elf_version_expr
*d
;
1962 /* See if can find what version this symbol is in. If the
1963 symbol is supposed to be local, then don't actually register
1966 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1968 if (t
->globals
.list
!= NULL
)
1970 bfd_boolean matched
;
1974 while ((d
= (*t
->match
) (&t
->globals
, d
,
1975 h
->root
.root
.string
)) != NULL
)
1980 /* There is a version without definition. Make
1981 the symbol the default definition for this
1983 h
->verinfo
.vertree
= t
;
1991 /* There is no undefined version for this symbol. Hide the
1993 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1996 if (t
->locals
.list
!= NULL
)
1999 while ((d
= (*t
->match
) (&t
->locals
, d
,
2000 h
->root
.root
.string
)) != NULL
)
2003 /* If the match is "*", keep looking for a more
2004 explicit, perhaps even global, match.
2005 XXX: Shouldn't this be !d->wildcard instead? */
2006 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
2015 if (local_ver
!= NULL
)
2017 h
->verinfo
.vertree
= local_ver
;
2018 if (h
->dynindx
!= -1
2019 && ! info
->export_dynamic
)
2021 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2029 /* Read and swap the relocs from the section indicated by SHDR. This
2030 may be either a REL or a RELA section. The relocations are
2031 translated into RELA relocations and stored in INTERNAL_RELOCS,
2032 which should have already been allocated to contain enough space.
2033 The EXTERNAL_RELOCS are a buffer where the external form of the
2034 relocations should be stored.
2036 Returns FALSE if something goes wrong. */
2039 elf_link_read_relocs_from_section (bfd
*abfd
,
2041 Elf_Internal_Shdr
*shdr
,
2042 void *external_relocs
,
2043 Elf_Internal_Rela
*internal_relocs
)
2045 const struct elf_backend_data
*bed
;
2046 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2047 const bfd_byte
*erela
;
2048 const bfd_byte
*erelaend
;
2049 Elf_Internal_Rela
*irela
;
2050 Elf_Internal_Shdr
*symtab_hdr
;
2053 /* Position ourselves at the start of the section. */
2054 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2057 /* Read the relocations. */
2058 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2061 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2062 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2064 bed
= get_elf_backend_data (abfd
);
2066 /* Convert the external relocations to the internal format. */
2067 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2068 swap_in
= bed
->s
->swap_reloc_in
;
2069 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2070 swap_in
= bed
->s
->swap_reloca_in
;
2073 bfd_set_error (bfd_error_wrong_format
);
2077 erela
= external_relocs
;
2078 erelaend
= erela
+ shdr
->sh_size
;
2079 irela
= internal_relocs
;
2080 while (erela
< erelaend
)
2084 (*swap_in
) (abfd
, erela
, irela
);
2085 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2086 if (bed
->s
->arch_size
== 64)
2088 if ((size_t) r_symndx
>= nsyms
)
2090 (*_bfd_error_handler
)
2091 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2092 " for offset 0x%lx in section `%A'"),
2094 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2095 bfd_set_error (bfd_error_bad_value
);
2098 irela
+= bed
->s
->int_rels_per_ext_rel
;
2099 erela
+= shdr
->sh_entsize
;
2105 /* Read and swap the relocs for a section O. They may have been
2106 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2107 not NULL, they are used as buffers to read into. They are known to
2108 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2109 the return value is allocated using either malloc or bfd_alloc,
2110 according to the KEEP_MEMORY argument. If O has two relocation
2111 sections (both REL and RELA relocations), then the REL_HDR
2112 relocations will appear first in INTERNAL_RELOCS, followed by the
2113 REL_HDR2 relocations. */
2116 _bfd_elf_link_read_relocs (bfd
*abfd
,
2118 void *external_relocs
,
2119 Elf_Internal_Rela
*internal_relocs
,
2120 bfd_boolean keep_memory
)
2122 Elf_Internal_Shdr
*rel_hdr
;
2123 void *alloc1
= NULL
;
2124 Elf_Internal_Rela
*alloc2
= NULL
;
2125 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2127 if (elf_section_data (o
)->relocs
!= NULL
)
2128 return elf_section_data (o
)->relocs
;
2130 if (o
->reloc_count
== 0)
2133 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2135 if (internal_relocs
== NULL
)
2139 size
= o
->reloc_count
;
2140 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2142 internal_relocs
= bfd_alloc (abfd
, size
);
2144 internal_relocs
= alloc2
= bfd_malloc (size
);
2145 if (internal_relocs
== NULL
)
2149 if (external_relocs
== NULL
)
2151 bfd_size_type size
= rel_hdr
->sh_size
;
2153 if (elf_section_data (o
)->rel_hdr2
)
2154 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2155 alloc1
= bfd_malloc (size
);
2158 external_relocs
= alloc1
;
2161 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2165 if (elf_section_data (o
)->rel_hdr2
2166 && (!elf_link_read_relocs_from_section
2168 elf_section_data (o
)->rel_hdr2
,
2169 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2170 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2171 * bed
->s
->int_rels_per_ext_rel
))))
2174 /* Cache the results for next time, if we can. */
2176 elf_section_data (o
)->relocs
= internal_relocs
;
2181 /* Don't free alloc2, since if it was allocated we are passing it
2182 back (under the name of internal_relocs). */
2184 return internal_relocs
;
2194 /* Compute the size of, and allocate space for, REL_HDR which is the
2195 section header for a section containing relocations for O. */
2198 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2199 Elf_Internal_Shdr
*rel_hdr
,
2202 bfd_size_type reloc_count
;
2203 bfd_size_type num_rel_hashes
;
2205 /* Figure out how many relocations there will be. */
2206 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2207 reloc_count
= elf_section_data (o
)->rel_count
;
2209 reloc_count
= elf_section_data (o
)->rel_count2
;
2211 num_rel_hashes
= o
->reloc_count
;
2212 if (num_rel_hashes
< reloc_count
)
2213 num_rel_hashes
= reloc_count
;
2215 /* That allows us to calculate the size of the section. */
2216 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2218 /* The contents field must last into write_object_contents, so we
2219 allocate it with bfd_alloc rather than malloc. Also since we
2220 cannot be sure that the contents will actually be filled in,
2221 we zero the allocated space. */
2222 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2223 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2226 /* We only allocate one set of hash entries, so we only do it the
2227 first time we are called. */
2228 if (elf_section_data (o
)->rel_hashes
== NULL
2231 struct elf_link_hash_entry
**p
;
2233 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2237 elf_section_data (o
)->rel_hashes
= p
;
2243 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2244 originated from the section given by INPUT_REL_HDR) to the
2248 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2249 asection
*input_section
,
2250 Elf_Internal_Shdr
*input_rel_hdr
,
2251 Elf_Internal_Rela
*internal_relocs
,
2252 struct elf_link_hash_entry
**rel_hash
2255 Elf_Internal_Rela
*irela
;
2256 Elf_Internal_Rela
*irelaend
;
2258 Elf_Internal_Shdr
*output_rel_hdr
;
2259 asection
*output_section
;
2260 unsigned int *rel_countp
= NULL
;
2261 const struct elf_backend_data
*bed
;
2262 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2264 output_section
= input_section
->output_section
;
2265 output_rel_hdr
= NULL
;
2267 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2268 == input_rel_hdr
->sh_entsize
)
2270 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2271 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2273 else if (elf_section_data (output_section
)->rel_hdr2
2274 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2275 == input_rel_hdr
->sh_entsize
))
2277 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2278 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2282 (*_bfd_error_handler
)
2283 (_("%B: relocation size mismatch in %B section %A"),
2284 output_bfd
, input_section
->owner
, input_section
);
2285 bfd_set_error (bfd_error_wrong_object_format
);
2289 bed
= get_elf_backend_data (output_bfd
);
2290 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2291 swap_out
= bed
->s
->swap_reloc_out
;
2292 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2293 swap_out
= bed
->s
->swap_reloca_out
;
2297 erel
= output_rel_hdr
->contents
;
2298 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2299 irela
= internal_relocs
;
2300 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2301 * bed
->s
->int_rels_per_ext_rel
);
2302 while (irela
< irelaend
)
2304 (*swap_out
) (output_bfd
, irela
, erel
);
2305 irela
+= bed
->s
->int_rels_per_ext_rel
;
2306 erel
+= input_rel_hdr
->sh_entsize
;
2309 /* Bump the counter, so that we know where to add the next set of
2311 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2316 /* Make weak undefined symbols in PIE dynamic. */
2319 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2320 struct elf_link_hash_entry
*h
)
2324 && h
->root
.type
== bfd_link_hash_undefweak
)
2325 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2330 /* Fix up the flags for a symbol. This handles various cases which
2331 can only be fixed after all the input files are seen. This is
2332 currently called by both adjust_dynamic_symbol and
2333 assign_sym_version, which is unnecessary but perhaps more robust in
2334 the face of future changes. */
2337 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2338 struct elf_info_failed
*eif
)
2340 const struct elf_backend_data
*bed
= NULL
;
2342 /* If this symbol was mentioned in a non-ELF file, try to set
2343 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2344 permit a non-ELF file to correctly refer to a symbol defined in
2345 an ELF dynamic object. */
2348 while (h
->root
.type
== bfd_link_hash_indirect
)
2349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2351 if (h
->root
.type
!= bfd_link_hash_defined
2352 && h
->root
.type
!= bfd_link_hash_defweak
)
2355 h
->ref_regular_nonweak
= 1;
2359 if (h
->root
.u
.def
.section
->owner
!= NULL
2360 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2361 == bfd_target_elf_flavour
))
2364 h
->ref_regular_nonweak
= 1;
2370 if (h
->dynindx
== -1
2374 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2383 /* Unfortunately, NON_ELF is only correct if the symbol
2384 was first seen in a non-ELF file. Fortunately, if the symbol
2385 was first seen in an ELF file, we're probably OK unless the
2386 symbol was defined in a non-ELF file. Catch that case here.
2387 FIXME: We're still in trouble if the symbol was first seen in
2388 a dynamic object, and then later in a non-ELF regular object. */
2389 if ((h
->root
.type
== bfd_link_hash_defined
2390 || h
->root
.type
== bfd_link_hash_defweak
)
2392 && (h
->root
.u
.def
.section
->owner
!= NULL
2393 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2394 != bfd_target_elf_flavour
)
2395 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2396 && !h
->def_dynamic
)))
2400 /* Backend specific symbol fixup. */
2401 if (elf_hash_table (eif
->info
)->dynobj
)
2403 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2404 if (bed
->elf_backend_fixup_symbol
2405 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2409 /* If this is a final link, and the symbol was defined as a common
2410 symbol in a regular object file, and there was no definition in
2411 any dynamic object, then the linker will have allocated space for
2412 the symbol in a common section but the DEF_REGULAR
2413 flag will not have been set. */
2414 if (h
->root
.type
== bfd_link_hash_defined
2418 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2421 /* If -Bsymbolic was used (which means to bind references to global
2422 symbols to the definition within the shared object), and this
2423 symbol was defined in a regular object, then it actually doesn't
2424 need a PLT entry. Likewise, if the symbol has non-default
2425 visibility. If the symbol has hidden or internal visibility, we
2426 will force it local. */
2428 && eif
->info
->shared
2429 && is_elf_hash_table (eif
->info
->hash
)
2430 && (SYMBOLIC_BIND (eif
->info
, h
)
2431 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2434 bfd_boolean force_local
;
2436 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2437 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2438 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2441 /* If a weak undefined symbol has non-default visibility, we also
2442 hide it from the dynamic linker. */
2443 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2444 && h
->root
.type
== bfd_link_hash_undefweak
)
2446 const struct elf_backend_data
*bed
;
2447 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2448 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2451 /* If this is a weak defined symbol in a dynamic object, and we know
2452 the real definition in the dynamic object, copy interesting flags
2453 over to the real definition. */
2454 if (h
->u
.weakdef
!= NULL
)
2456 struct elf_link_hash_entry
*weakdef
;
2458 weakdef
= h
->u
.weakdef
;
2459 if (h
->root
.type
== bfd_link_hash_indirect
)
2460 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2462 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2463 || h
->root
.type
== bfd_link_hash_defweak
);
2464 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2465 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2466 BFD_ASSERT (weakdef
->def_dynamic
);
2468 /* If the real definition is defined by a regular object file,
2469 don't do anything special. See the longer description in
2470 _bfd_elf_adjust_dynamic_symbol, below. */
2471 if (weakdef
->def_regular
)
2472 h
->u
.weakdef
= NULL
;
2474 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
,
2481 /* Make the backend pick a good value for a dynamic symbol. This is
2482 called via elf_link_hash_traverse, and also calls itself
2486 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2488 struct elf_info_failed
*eif
= data
;
2490 const struct elf_backend_data
*bed
;
2492 if (! is_elf_hash_table (eif
->info
->hash
))
2495 if (h
->root
.type
== bfd_link_hash_warning
)
2497 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2498 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2500 /* When warning symbols are created, they **replace** the "real"
2501 entry in the hash table, thus we never get to see the real
2502 symbol in a hash traversal. So look at it now. */
2503 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2506 /* Ignore indirect symbols. These are added by the versioning code. */
2507 if (h
->root
.type
== bfd_link_hash_indirect
)
2510 /* Fix the symbol flags. */
2511 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2514 /* If this symbol does not require a PLT entry, and it is not
2515 defined by a dynamic object, or is not referenced by a regular
2516 object, ignore it. We do have to handle a weak defined symbol,
2517 even if no regular object refers to it, if we decided to add it
2518 to the dynamic symbol table. FIXME: Do we normally need to worry
2519 about symbols which are defined by one dynamic object and
2520 referenced by another one? */
2525 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2527 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2531 /* If we've already adjusted this symbol, don't do it again. This
2532 can happen via a recursive call. */
2533 if (h
->dynamic_adjusted
)
2536 /* Don't look at this symbol again. Note that we must set this
2537 after checking the above conditions, because we may look at a
2538 symbol once, decide not to do anything, and then get called
2539 recursively later after REF_REGULAR is set below. */
2540 h
->dynamic_adjusted
= 1;
2542 /* If this is a weak definition, and we know a real definition, and
2543 the real symbol is not itself defined by a regular object file,
2544 then get a good value for the real definition. We handle the
2545 real symbol first, for the convenience of the backend routine.
2547 Note that there is a confusing case here. If the real definition
2548 is defined by a regular object file, we don't get the real symbol
2549 from the dynamic object, but we do get the weak symbol. If the
2550 processor backend uses a COPY reloc, then if some routine in the
2551 dynamic object changes the real symbol, we will not see that
2552 change in the corresponding weak symbol. This is the way other
2553 ELF linkers work as well, and seems to be a result of the shared
2556 I will clarify this issue. Most SVR4 shared libraries define the
2557 variable _timezone and define timezone as a weak synonym. The
2558 tzset call changes _timezone. If you write
2559 extern int timezone;
2561 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2562 you might expect that, since timezone is a synonym for _timezone,
2563 the same number will print both times. However, if the processor
2564 backend uses a COPY reloc, then actually timezone will be copied
2565 into your process image, and, since you define _timezone
2566 yourself, _timezone will not. Thus timezone and _timezone will
2567 wind up at different memory locations. The tzset call will set
2568 _timezone, leaving timezone unchanged. */
2570 if (h
->u
.weakdef
!= NULL
)
2572 /* If we get to this point, we know there is an implicit
2573 reference by a regular object file via the weak symbol H.
2574 FIXME: Is this really true? What if the traversal finds
2575 H->U.WEAKDEF before it finds H? */
2576 h
->u
.weakdef
->ref_regular
= 1;
2578 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2582 /* If a symbol has no type and no size and does not require a PLT
2583 entry, then we are probably about to do the wrong thing here: we
2584 are probably going to create a COPY reloc for an empty object.
2585 This case can arise when a shared object is built with assembly
2586 code, and the assembly code fails to set the symbol type. */
2588 && h
->type
== STT_NOTYPE
2590 (*_bfd_error_handler
)
2591 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2592 h
->root
.root
.string
);
2594 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2595 bed
= get_elf_backend_data (dynobj
);
2596 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2605 /* Adjust all external symbols pointing into SEC_MERGE sections
2606 to reflect the object merging within the sections. */
2609 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2613 if (h
->root
.type
== bfd_link_hash_warning
)
2614 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2616 if ((h
->root
.type
== bfd_link_hash_defined
2617 || h
->root
.type
== bfd_link_hash_defweak
)
2618 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2619 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2621 bfd
*output_bfd
= data
;
2623 h
->root
.u
.def
.value
=
2624 _bfd_merged_section_offset (output_bfd
,
2625 &h
->root
.u
.def
.section
,
2626 elf_section_data (sec
)->sec_info
,
2627 h
->root
.u
.def
.value
);
2633 /* Returns false if the symbol referred to by H should be considered
2634 to resolve local to the current module, and true if it should be
2635 considered to bind dynamically. */
2638 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2639 struct bfd_link_info
*info
,
2640 bfd_boolean ignore_protected
)
2642 bfd_boolean binding_stays_local_p
;
2647 while (h
->root
.type
== bfd_link_hash_indirect
2648 || h
->root
.type
== bfd_link_hash_warning
)
2649 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2651 /* If it was forced local, then clearly it's not dynamic. */
2652 if (h
->dynindx
== -1)
2654 if (h
->forced_local
)
2657 /* Identify the cases where name binding rules say that a
2658 visible symbol resolves locally. */
2659 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2661 switch (ELF_ST_VISIBILITY (h
->other
))
2668 /* Proper resolution for function pointer equality may require
2669 that these symbols perhaps be resolved dynamically, even though
2670 we should be resolving them to the current module. */
2671 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2672 binding_stays_local_p
= TRUE
;
2679 /* If it isn't defined locally, then clearly it's dynamic. */
2680 if (!h
->def_regular
)
2683 /* Otherwise, the symbol is dynamic if binding rules don't tell
2684 us that it remains local. */
2685 return !binding_stays_local_p
;
2688 /* Return true if the symbol referred to by H should be considered
2689 to resolve local to the current module, and false otherwise. Differs
2690 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2691 undefined symbols and weak symbols. */
2694 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2695 struct bfd_link_info
*info
,
2696 bfd_boolean local_protected
)
2698 /* If it's a local sym, of course we resolve locally. */
2702 /* Common symbols that become definitions don't get the DEF_REGULAR
2703 flag set, so test it first, and don't bail out. */
2704 if (ELF_COMMON_DEF_P (h
))
2706 /* If we don't have a definition in a regular file, then we can't
2707 resolve locally. The sym is either undefined or dynamic. */
2708 else if (!h
->def_regular
)
2711 /* Forced local symbols resolve locally. */
2712 if (h
->forced_local
)
2715 /* As do non-dynamic symbols. */
2716 if (h
->dynindx
== -1)
2719 /* At this point, we know the symbol is defined and dynamic. In an
2720 executable it must resolve locally, likewise when building symbolic
2721 shared libraries. */
2722 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2725 /* Now deal with defined dynamic symbols in shared libraries. Ones
2726 with default visibility might not resolve locally. */
2727 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2730 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2731 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2734 /* STV_PROTECTED non-function symbols are local. */
2735 if (h
->type
!= STT_FUNC
)
2738 /* Function pointer equality tests may require that STV_PROTECTED
2739 symbols be treated as dynamic symbols, even when we know that the
2740 dynamic linker will resolve them locally. */
2741 return local_protected
;
2744 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2745 aligned. Returns the first TLS output section. */
2747 struct bfd_section
*
2748 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2750 struct bfd_section
*sec
, *tls
;
2751 unsigned int align
= 0;
2753 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2754 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2758 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2759 if (sec
->alignment_power
> align
)
2760 align
= sec
->alignment_power
;
2762 elf_hash_table (info
)->tls_sec
= tls
;
2764 /* Ensure the alignment of the first section is the largest alignment,
2765 so that the tls segment starts aligned. */
2767 tls
->alignment_power
= align
;
2772 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2774 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2775 Elf_Internal_Sym
*sym
)
2777 const struct elf_backend_data
*bed
;
2779 /* Local symbols do not count, but target specific ones might. */
2780 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2781 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2784 /* Function symbols do not count. */
2785 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2788 /* If the section is undefined, then so is the symbol. */
2789 if (sym
->st_shndx
== SHN_UNDEF
)
2792 /* If the symbol is defined in the common section, then
2793 it is a common definition and so does not count. */
2794 bed
= get_elf_backend_data (abfd
);
2795 if (bed
->common_definition (sym
))
2798 /* If the symbol is in a target specific section then we
2799 must rely upon the backend to tell us what it is. */
2800 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2801 /* FIXME - this function is not coded yet:
2803 return _bfd_is_global_symbol_definition (abfd, sym);
2805 Instead for now assume that the definition is not global,
2806 Even if this is wrong, at least the linker will behave
2807 in the same way that it used to do. */
2813 /* Search the symbol table of the archive element of the archive ABFD
2814 whose archive map contains a mention of SYMDEF, and determine if
2815 the symbol is defined in this element. */
2817 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2819 Elf_Internal_Shdr
* hdr
;
2820 bfd_size_type symcount
;
2821 bfd_size_type extsymcount
;
2822 bfd_size_type extsymoff
;
2823 Elf_Internal_Sym
*isymbuf
;
2824 Elf_Internal_Sym
*isym
;
2825 Elf_Internal_Sym
*isymend
;
2828 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2832 if (! bfd_check_format (abfd
, bfd_object
))
2835 /* If we have already included the element containing this symbol in the
2836 link then we do not need to include it again. Just claim that any symbol
2837 it contains is not a definition, so that our caller will not decide to
2838 (re)include this element. */
2839 if (abfd
->archive_pass
)
2842 /* Select the appropriate symbol table. */
2843 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2844 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2846 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2848 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2850 /* The sh_info field of the symtab header tells us where the
2851 external symbols start. We don't care about the local symbols. */
2852 if (elf_bad_symtab (abfd
))
2854 extsymcount
= symcount
;
2859 extsymcount
= symcount
- hdr
->sh_info
;
2860 extsymoff
= hdr
->sh_info
;
2863 if (extsymcount
== 0)
2866 /* Read in the symbol table. */
2867 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2869 if (isymbuf
== NULL
)
2872 /* Scan the symbol table looking for SYMDEF. */
2874 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2878 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2883 if (strcmp (name
, symdef
->name
) == 0)
2885 result
= is_global_data_symbol_definition (abfd
, isym
);
2895 /* Add an entry to the .dynamic table. */
2898 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2902 struct elf_link_hash_table
*hash_table
;
2903 const struct elf_backend_data
*bed
;
2905 bfd_size_type newsize
;
2906 bfd_byte
*newcontents
;
2907 Elf_Internal_Dyn dyn
;
2909 hash_table
= elf_hash_table (info
);
2910 if (! is_elf_hash_table (hash_table
))
2913 bed
= get_elf_backend_data (hash_table
->dynobj
);
2914 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2915 BFD_ASSERT (s
!= NULL
);
2917 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2918 newcontents
= bfd_realloc (s
->contents
, newsize
);
2919 if (newcontents
== NULL
)
2923 dyn
.d_un
.d_val
= val
;
2924 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2927 s
->contents
= newcontents
;
2932 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2933 otherwise just check whether one already exists. Returns -1 on error,
2934 1 if a DT_NEEDED tag already exists, and 0 on success. */
2937 elf_add_dt_needed_tag (bfd
*abfd
,
2938 struct bfd_link_info
*info
,
2942 struct elf_link_hash_table
*hash_table
;
2943 bfd_size_type oldsize
;
2944 bfd_size_type strindex
;
2946 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2949 hash_table
= elf_hash_table (info
);
2950 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2951 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2952 if (strindex
== (bfd_size_type
) -1)
2955 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2958 const struct elf_backend_data
*bed
;
2961 bed
= get_elf_backend_data (hash_table
->dynobj
);
2962 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2964 for (extdyn
= sdyn
->contents
;
2965 extdyn
< sdyn
->contents
+ sdyn
->size
;
2966 extdyn
+= bed
->s
->sizeof_dyn
)
2968 Elf_Internal_Dyn dyn
;
2970 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2971 if (dyn
.d_tag
== DT_NEEDED
2972 && dyn
.d_un
.d_val
== strindex
)
2974 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2982 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2985 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2989 /* We were just checking for existence of the tag. */
2990 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2995 /* Sort symbol by value and section. */
2997 elf_sort_symbol (const void *arg1
, const void *arg2
)
2999 const struct elf_link_hash_entry
*h1
;
3000 const struct elf_link_hash_entry
*h2
;
3001 bfd_signed_vma vdiff
;
3003 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3004 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3005 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3007 return vdiff
> 0 ? 1 : -1;
3010 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3012 return sdiff
> 0 ? 1 : -1;
3017 /* This function is used to adjust offsets into .dynstr for
3018 dynamic symbols. This is called via elf_link_hash_traverse. */
3021 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3023 struct elf_strtab_hash
*dynstr
= data
;
3025 if (h
->root
.type
== bfd_link_hash_warning
)
3026 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3028 if (h
->dynindx
!= -1)
3029 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3033 /* Assign string offsets in .dynstr, update all structures referencing
3037 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3039 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3040 struct elf_link_local_dynamic_entry
*entry
;
3041 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3042 bfd
*dynobj
= hash_table
->dynobj
;
3045 const struct elf_backend_data
*bed
;
3048 _bfd_elf_strtab_finalize (dynstr
);
3049 size
= _bfd_elf_strtab_size (dynstr
);
3051 bed
= get_elf_backend_data (dynobj
);
3052 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3053 BFD_ASSERT (sdyn
!= NULL
);
3055 /* Update all .dynamic entries referencing .dynstr strings. */
3056 for (extdyn
= sdyn
->contents
;
3057 extdyn
< sdyn
->contents
+ sdyn
->size
;
3058 extdyn
+= bed
->s
->sizeof_dyn
)
3060 Elf_Internal_Dyn dyn
;
3062 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3066 dyn
.d_un
.d_val
= size
;
3074 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3079 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3082 /* Now update local dynamic symbols. */
3083 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3084 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3085 entry
->isym
.st_name
);
3087 /* And the rest of dynamic symbols. */
3088 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3090 /* Adjust version definitions. */
3091 if (elf_tdata (output_bfd
)->cverdefs
)
3096 Elf_Internal_Verdef def
;
3097 Elf_Internal_Verdaux defaux
;
3099 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3103 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3105 p
+= sizeof (Elf_External_Verdef
);
3106 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3108 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3110 _bfd_elf_swap_verdaux_in (output_bfd
,
3111 (Elf_External_Verdaux
*) p
, &defaux
);
3112 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3114 _bfd_elf_swap_verdaux_out (output_bfd
,
3115 &defaux
, (Elf_External_Verdaux
*) p
);
3116 p
+= sizeof (Elf_External_Verdaux
);
3119 while (def
.vd_next
);
3122 /* Adjust version references. */
3123 if (elf_tdata (output_bfd
)->verref
)
3128 Elf_Internal_Verneed need
;
3129 Elf_Internal_Vernaux needaux
;
3131 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3135 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3137 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3138 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3139 (Elf_External_Verneed
*) p
);
3140 p
+= sizeof (Elf_External_Verneed
);
3141 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3143 _bfd_elf_swap_vernaux_in (output_bfd
,
3144 (Elf_External_Vernaux
*) p
, &needaux
);
3145 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3147 _bfd_elf_swap_vernaux_out (output_bfd
,
3149 (Elf_External_Vernaux
*) p
);
3150 p
+= sizeof (Elf_External_Vernaux
);
3153 while (need
.vn_next
);
3159 /* Add symbols from an ELF object file to the linker hash table. */
3162 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3164 Elf_Internal_Shdr
*hdr
;
3165 bfd_size_type symcount
;
3166 bfd_size_type extsymcount
;
3167 bfd_size_type extsymoff
;
3168 struct elf_link_hash_entry
**sym_hash
;
3169 bfd_boolean dynamic
;
3170 Elf_External_Versym
*extversym
= NULL
;
3171 Elf_External_Versym
*ever
;
3172 struct elf_link_hash_entry
*weaks
;
3173 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3174 bfd_size_type nondeflt_vers_cnt
= 0;
3175 Elf_Internal_Sym
*isymbuf
= NULL
;
3176 Elf_Internal_Sym
*isym
;
3177 Elf_Internal_Sym
*isymend
;
3178 const struct elf_backend_data
*bed
;
3179 bfd_boolean add_needed
;
3180 struct elf_link_hash_table
*htab
;
3182 void *alloc_mark
= NULL
;
3183 struct bfd_hash_entry
**old_table
= NULL
;
3184 unsigned int old_size
= 0;
3185 unsigned int old_count
= 0;
3186 void *old_tab
= NULL
;
3189 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3190 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3191 long old_dynsymcount
= 0;
3193 size_t hashsize
= 0;
3195 htab
= elf_hash_table (info
);
3196 bed
= get_elf_backend_data (abfd
);
3198 if ((abfd
->flags
& DYNAMIC
) == 0)
3204 /* You can't use -r against a dynamic object. Also, there's no
3205 hope of using a dynamic object which does not exactly match
3206 the format of the output file. */
3207 if (info
->relocatable
3208 || !is_elf_hash_table (htab
)
3209 || htab
->root
.creator
!= abfd
->xvec
)
3211 if (info
->relocatable
)
3212 bfd_set_error (bfd_error_invalid_operation
);
3214 bfd_set_error (bfd_error_wrong_format
);
3219 /* As a GNU extension, any input sections which are named
3220 .gnu.warning.SYMBOL are treated as warning symbols for the given
3221 symbol. This differs from .gnu.warning sections, which generate
3222 warnings when they are included in an output file. */
3223 if (info
->executable
)
3227 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3231 name
= bfd_get_section_name (abfd
, s
);
3232 if (CONST_STRNEQ (name
, ".gnu.warning."))
3237 name
+= sizeof ".gnu.warning." - 1;
3239 /* If this is a shared object, then look up the symbol
3240 in the hash table. If it is there, and it is already
3241 been defined, then we will not be using the entry
3242 from this shared object, so we don't need to warn.
3243 FIXME: If we see the definition in a regular object
3244 later on, we will warn, but we shouldn't. The only
3245 fix is to keep track of what warnings we are supposed
3246 to emit, and then handle them all at the end of the
3250 struct elf_link_hash_entry
*h
;
3252 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3254 /* FIXME: What about bfd_link_hash_common? */
3256 && (h
->root
.type
== bfd_link_hash_defined
3257 || h
->root
.type
== bfd_link_hash_defweak
))
3259 /* We don't want to issue this warning. Clobber
3260 the section size so that the warning does not
3261 get copied into the output file. */
3268 msg
= bfd_alloc (abfd
, sz
+ 1);
3272 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3277 if (! (_bfd_generic_link_add_one_symbol
3278 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3279 FALSE
, bed
->collect
, NULL
)))
3282 if (! info
->relocatable
)
3284 /* Clobber the section size so that the warning does
3285 not get copied into the output file. */
3288 /* Also set SEC_EXCLUDE, so that symbols defined in
3289 the warning section don't get copied to the output. */
3290 s
->flags
|= SEC_EXCLUDE
;
3299 /* If we are creating a shared library, create all the dynamic
3300 sections immediately. We need to attach them to something,
3301 so we attach them to this BFD, provided it is the right
3302 format. FIXME: If there are no input BFD's of the same
3303 format as the output, we can't make a shared library. */
3305 && is_elf_hash_table (htab
)
3306 && htab
->root
.creator
== abfd
->xvec
3307 && !htab
->dynamic_sections_created
)
3309 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3313 else if (!is_elf_hash_table (htab
))
3318 const char *soname
= NULL
;
3319 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3322 /* ld --just-symbols and dynamic objects don't mix very well.
3323 ld shouldn't allow it. */
3324 if ((s
= abfd
->sections
) != NULL
3325 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3328 /* If this dynamic lib was specified on the command line with
3329 --as-needed in effect, then we don't want to add a DT_NEEDED
3330 tag unless the lib is actually used. Similary for libs brought
3331 in by another lib's DT_NEEDED. When --no-add-needed is used
3332 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3333 any dynamic library in DT_NEEDED tags in the dynamic lib at
3335 add_needed
= (elf_dyn_lib_class (abfd
)
3336 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3337 | DYN_NO_NEEDED
)) == 0;
3339 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3345 unsigned long shlink
;
3347 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3348 goto error_free_dyn
;
3350 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3352 goto error_free_dyn
;
3353 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3355 for (extdyn
= dynbuf
;
3356 extdyn
< dynbuf
+ s
->size
;
3357 extdyn
+= bed
->s
->sizeof_dyn
)
3359 Elf_Internal_Dyn dyn
;
3361 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3362 if (dyn
.d_tag
== DT_SONAME
)
3364 unsigned int tagv
= dyn
.d_un
.d_val
;
3365 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3367 goto error_free_dyn
;
3369 if (dyn
.d_tag
== DT_NEEDED
)
3371 struct bfd_link_needed_list
*n
, **pn
;
3373 unsigned int tagv
= dyn
.d_un
.d_val
;
3375 amt
= sizeof (struct bfd_link_needed_list
);
3376 n
= bfd_alloc (abfd
, amt
);
3377 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3378 if (n
== NULL
|| fnm
== NULL
)
3379 goto error_free_dyn
;
3380 amt
= strlen (fnm
) + 1;
3381 anm
= bfd_alloc (abfd
, amt
);
3383 goto error_free_dyn
;
3384 memcpy (anm
, fnm
, amt
);
3388 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3392 if (dyn
.d_tag
== DT_RUNPATH
)
3394 struct bfd_link_needed_list
*n
, **pn
;
3396 unsigned int tagv
= dyn
.d_un
.d_val
;
3398 amt
= sizeof (struct bfd_link_needed_list
);
3399 n
= bfd_alloc (abfd
, amt
);
3400 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3401 if (n
== NULL
|| fnm
== NULL
)
3402 goto error_free_dyn
;
3403 amt
= strlen (fnm
) + 1;
3404 anm
= bfd_alloc (abfd
, amt
);
3406 goto error_free_dyn
;
3407 memcpy (anm
, fnm
, amt
);
3411 for (pn
= & runpath
;
3417 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3418 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3420 struct bfd_link_needed_list
*n
, **pn
;
3422 unsigned int tagv
= dyn
.d_un
.d_val
;
3424 amt
= sizeof (struct bfd_link_needed_list
);
3425 n
= bfd_alloc (abfd
, amt
);
3426 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3427 if (n
== NULL
|| fnm
== NULL
)
3428 goto error_free_dyn
;
3429 amt
= strlen (fnm
) + 1;
3430 anm
= bfd_alloc (abfd
, amt
);
3437 memcpy (anm
, fnm
, amt
);
3452 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3453 frees all more recently bfd_alloc'd blocks as well. */
3459 struct bfd_link_needed_list
**pn
;
3460 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3465 /* We do not want to include any of the sections in a dynamic
3466 object in the output file. We hack by simply clobbering the
3467 list of sections in the BFD. This could be handled more
3468 cleanly by, say, a new section flag; the existing
3469 SEC_NEVER_LOAD flag is not the one we want, because that one
3470 still implies that the section takes up space in the output
3472 bfd_section_list_clear (abfd
);
3474 /* Find the name to use in a DT_NEEDED entry that refers to this
3475 object. If the object has a DT_SONAME entry, we use it.
3476 Otherwise, if the generic linker stuck something in
3477 elf_dt_name, we use that. Otherwise, we just use the file
3479 if (soname
== NULL
|| *soname
== '\0')
3481 soname
= elf_dt_name (abfd
);
3482 if (soname
== NULL
|| *soname
== '\0')
3483 soname
= bfd_get_filename (abfd
);
3486 /* Save the SONAME because sometimes the linker emulation code
3487 will need to know it. */
3488 elf_dt_name (abfd
) = soname
;
3490 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3494 /* If we have already included this dynamic object in the
3495 link, just ignore it. There is no reason to include a
3496 particular dynamic object more than once. */
3501 /* If this is a dynamic object, we always link against the .dynsym
3502 symbol table, not the .symtab symbol table. The dynamic linker
3503 will only see the .dynsym symbol table, so there is no reason to
3504 look at .symtab for a dynamic object. */
3506 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3507 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3509 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3511 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3513 /* The sh_info field of the symtab header tells us where the
3514 external symbols start. We don't care about the local symbols at
3516 if (elf_bad_symtab (abfd
))
3518 extsymcount
= symcount
;
3523 extsymcount
= symcount
- hdr
->sh_info
;
3524 extsymoff
= hdr
->sh_info
;
3528 if (extsymcount
!= 0)
3530 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3532 if (isymbuf
== NULL
)
3535 /* We store a pointer to the hash table entry for each external
3537 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3538 sym_hash
= bfd_alloc (abfd
, amt
);
3539 if (sym_hash
== NULL
)
3540 goto error_free_sym
;
3541 elf_sym_hashes (abfd
) = sym_hash
;
3546 /* Read in any version definitions. */
3547 if (!_bfd_elf_slurp_version_tables (abfd
,
3548 info
->default_imported_symver
))
3549 goto error_free_sym
;
3551 /* Read in the symbol versions, but don't bother to convert them
3552 to internal format. */
3553 if (elf_dynversym (abfd
) != 0)
3555 Elf_Internal_Shdr
*versymhdr
;
3557 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3558 extversym
= bfd_malloc (versymhdr
->sh_size
);
3559 if (extversym
== NULL
)
3560 goto error_free_sym
;
3561 amt
= versymhdr
->sh_size
;
3562 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3563 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3564 goto error_free_vers
;
3568 /* If we are loading an as-needed shared lib, save the symbol table
3569 state before we start adding symbols. If the lib turns out
3570 to be unneeded, restore the state. */
3571 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3576 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3578 struct bfd_hash_entry
*p
;
3579 struct elf_link_hash_entry
*h
;
3581 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3583 h
= (struct elf_link_hash_entry
*) p
;
3584 entsize
+= htab
->root
.table
.entsize
;
3585 if (h
->root
.type
== bfd_link_hash_warning
)
3586 entsize
+= htab
->root
.table
.entsize
;
3590 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3591 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3592 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3593 if (old_tab
== NULL
)
3594 goto error_free_vers
;
3596 /* Remember the current objalloc pointer, so that all mem for
3597 symbols added can later be reclaimed. */
3598 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3599 if (alloc_mark
== NULL
)
3600 goto error_free_vers
;
3602 /* Make a special call to the linker "notice" function to
3603 tell it that we are about to handle an as-needed lib. */
3604 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3609 /* Clone the symbol table and sym hashes. Remember some
3610 pointers into the symbol table, and dynamic symbol count. */
3611 old_hash
= (char *) old_tab
+ tabsize
;
3612 old_ent
= (char *) old_hash
+ hashsize
;
3613 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3614 memcpy (old_hash
, sym_hash
, hashsize
);
3615 old_undefs
= htab
->root
.undefs
;
3616 old_undefs_tail
= htab
->root
.undefs_tail
;
3617 old_table
= htab
->root
.table
.table
;
3618 old_size
= htab
->root
.table
.size
;
3619 old_count
= htab
->root
.table
.count
;
3620 old_dynsymcount
= htab
->dynsymcount
;
3622 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3624 struct bfd_hash_entry
*p
;
3625 struct elf_link_hash_entry
*h
;
3627 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3629 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3630 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3631 h
= (struct elf_link_hash_entry
*) p
;
3632 if (h
->root
.type
== bfd_link_hash_warning
)
3634 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3635 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3642 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3643 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3645 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3649 asection
*sec
, *new_sec
;
3652 struct elf_link_hash_entry
*h
;
3653 bfd_boolean definition
;
3654 bfd_boolean size_change_ok
;
3655 bfd_boolean type_change_ok
;
3656 bfd_boolean new_weakdef
;
3657 bfd_boolean override
;
3659 unsigned int old_alignment
;
3664 flags
= BSF_NO_FLAGS
;
3666 value
= isym
->st_value
;
3668 common
= bed
->common_definition (isym
);
3670 bind
= ELF_ST_BIND (isym
->st_info
);
3671 if (bind
== STB_LOCAL
)
3673 /* This should be impossible, since ELF requires that all
3674 global symbols follow all local symbols, and that sh_info
3675 point to the first global symbol. Unfortunately, Irix 5
3679 else if (bind
== STB_GLOBAL
)
3681 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3684 else if (bind
== STB_WEAK
)
3688 /* Leave it up to the processor backend. */
3691 if (isym
->st_shndx
== SHN_UNDEF
)
3692 sec
= bfd_und_section_ptr
;
3693 else if (isym
->st_shndx
< SHN_LORESERVE
3694 || isym
->st_shndx
> SHN_HIRESERVE
)
3696 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3698 sec
= bfd_abs_section_ptr
;
3699 else if (sec
->kept_section
)
3701 /* Symbols from discarded section are undefined. We keep
3703 sec
= bfd_und_section_ptr
;
3704 isym
->st_shndx
= SHN_UNDEF
;
3706 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3709 else if (isym
->st_shndx
== SHN_ABS
)
3710 sec
= bfd_abs_section_ptr
;
3711 else if (isym
->st_shndx
== SHN_COMMON
)
3713 sec
= bfd_com_section_ptr
;
3714 /* What ELF calls the size we call the value. What ELF
3715 calls the value we call the alignment. */
3716 value
= isym
->st_size
;
3720 /* Leave it up to the processor backend. */
3723 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3726 goto error_free_vers
;
3728 if (isym
->st_shndx
== SHN_COMMON
3729 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3730 && !info
->relocatable
)
3732 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3736 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3739 | SEC_LINKER_CREATED
3740 | SEC_THREAD_LOCAL
));
3742 goto error_free_vers
;
3746 else if (bed
->elf_add_symbol_hook
)
3748 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3750 goto error_free_vers
;
3752 /* The hook function sets the name to NULL if this symbol
3753 should be skipped for some reason. */
3758 /* Sanity check that all possibilities were handled. */
3761 bfd_set_error (bfd_error_bad_value
);
3762 goto error_free_vers
;
3765 if (bfd_is_und_section (sec
)
3766 || bfd_is_com_section (sec
))
3771 size_change_ok
= FALSE
;
3772 type_change_ok
= bed
->type_change_ok
;
3777 if (is_elf_hash_table (htab
))
3779 Elf_Internal_Versym iver
;
3780 unsigned int vernum
= 0;
3785 if (info
->default_imported_symver
)
3786 /* Use the default symbol version created earlier. */
3787 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3792 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3794 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3796 /* If this is a hidden symbol, or if it is not version
3797 1, we append the version name to the symbol name.
3798 However, we do not modify a non-hidden absolute symbol
3799 if it is not a function, because it might be the version
3800 symbol itself. FIXME: What if it isn't? */
3801 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3802 || (vernum
> 1 && (! bfd_is_abs_section (sec
)
3803 || ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)))
3806 size_t namelen
, verlen
, newlen
;
3809 if (isym
->st_shndx
!= SHN_UNDEF
)
3811 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3813 else if (vernum
> 1)
3815 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3821 (*_bfd_error_handler
)
3822 (_("%B: %s: invalid version %u (max %d)"),
3824 elf_tdata (abfd
)->cverdefs
);
3825 bfd_set_error (bfd_error_bad_value
);
3826 goto error_free_vers
;
3831 /* We cannot simply test for the number of
3832 entries in the VERNEED section since the
3833 numbers for the needed versions do not start
3835 Elf_Internal_Verneed
*t
;
3838 for (t
= elf_tdata (abfd
)->verref
;
3842 Elf_Internal_Vernaux
*a
;
3844 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3846 if (a
->vna_other
== vernum
)
3848 verstr
= a
->vna_nodename
;
3857 (*_bfd_error_handler
)
3858 (_("%B: %s: invalid needed version %d"),
3859 abfd
, name
, vernum
);
3860 bfd_set_error (bfd_error_bad_value
);
3861 goto error_free_vers
;
3865 namelen
= strlen (name
);
3866 verlen
= strlen (verstr
);
3867 newlen
= namelen
+ verlen
+ 2;
3868 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3869 && isym
->st_shndx
!= SHN_UNDEF
)
3872 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
3873 if (newname
== NULL
)
3874 goto error_free_vers
;
3875 memcpy (newname
, name
, namelen
);
3876 p
= newname
+ namelen
;
3878 /* If this is a defined non-hidden version symbol,
3879 we add another @ to the name. This indicates the
3880 default version of the symbol. */
3881 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3882 && isym
->st_shndx
!= SHN_UNDEF
)
3884 memcpy (p
, verstr
, verlen
+ 1);
3889 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3890 &value
, &old_alignment
,
3891 sym_hash
, &skip
, &override
,
3892 &type_change_ok
, &size_change_ok
))
3893 goto error_free_vers
;
3902 while (h
->root
.type
== bfd_link_hash_indirect
3903 || h
->root
.type
== bfd_link_hash_warning
)
3904 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3906 /* Remember the old alignment if this is a common symbol, so
3907 that we don't reduce the alignment later on. We can't
3908 check later, because _bfd_generic_link_add_one_symbol
3909 will set a default for the alignment which we want to
3910 override. We also remember the old bfd where the existing
3911 definition comes from. */
3912 switch (h
->root
.type
)
3917 case bfd_link_hash_defined
:
3918 case bfd_link_hash_defweak
:
3919 old_bfd
= h
->root
.u
.def
.section
->owner
;
3922 case bfd_link_hash_common
:
3923 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3924 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3928 if (elf_tdata (abfd
)->verdef
!= NULL
3932 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3935 if (! (_bfd_generic_link_add_one_symbol
3936 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
3937 (struct bfd_link_hash_entry
**) sym_hash
)))
3938 goto error_free_vers
;
3941 while (h
->root
.type
== bfd_link_hash_indirect
3942 || h
->root
.type
== bfd_link_hash_warning
)
3943 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3946 new_weakdef
= FALSE
;
3949 && (flags
& BSF_WEAK
) != 0
3950 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3951 && is_elf_hash_table (htab
)
3952 && h
->u
.weakdef
== NULL
)
3954 /* Keep a list of all weak defined non function symbols from
3955 a dynamic object, using the weakdef field. Later in this
3956 function we will set the weakdef field to the correct
3957 value. We only put non-function symbols from dynamic
3958 objects on this list, because that happens to be the only
3959 time we need to know the normal symbol corresponding to a
3960 weak symbol, and the information is time consuming to
3961 figure out. If the weakdef field is not already NULL,
3962 then this symbol was already defined by some previous
3963 dynamic object, and we will be using that previous
3964 definition anyhow. */
3966 h
->u
.weakdef
= weaks
;
3971 /* Set the alignment of a common symbol. */
3972 if ((common
|| bfd_is_com_section (sec
))
3973 && h
->root
.type
== bfd_link_hash_common
)
3978 align
= bfd_log2 (isym
->st_value
);
3981 /* The new symbol is a common symbol in a shared object.
3982 We need to get the alignment from the section. */
3983 align
= new_sec
->alignment_power
;
3985 if (align
> old_alignment
3986 /* Permit an alignment power of zero if an alignment of one
3987 is specified and no other alignments have been specified. */
3988 || (isym
->st_value
== 1 && old_alignment
== 0))
3989 h
->root
.u
.c
.p
->alignment_power
= align
;
3991 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3994 if (is_elf_hash_table (htab
))
3998 /* Check the alignment when a common symbol is involved. This
3999 can change when a common symbol is overridden by a normal
4000 definition or a common symbol is ignored due to the old
4001 normal definition. We need to make sure the maximum
4002 alignment is maintained. */
4003 if ((old_alignment
|| common
)
4004 && h
->root
.type
!= bfd_link_hash_common
)
4006 unsigned int common_align
;
4007 unsigned int normal_align
;
4008 unsigned int symbol_align
;
4012 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4013 if (h
->root
.u
.def
.section
->owner
!= NULL
4014 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4016 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4017 if (normal_align
> symbol_align
)
4018 normal_align
= symbol_align
;
4021 normal_align
= symbol_align
;
4025 common_align
= old_alignment
;
4026 common_bfd
= old_bfd
;
4031 common_align
= bfd_log2 (isym
->st_value
);
4033 normal_bfd
= old_bfd
;
4036 if (normal_align
< common_align
)
4038 /* PR binutils/2735 */
4039 if (normal_bfd
== NULL
)
4040 (*_bfd_error_handler
)
4041 (_("Warning: alignment %u of common symbol `%s' in %B"
4042 " is greater than the alignment (%u) of its section %A"),
4043 common_bfd
, h
->root
.u
.def
.section
,
4044 1 << common_align
, name
, 1 << normal_align
);
4046 (*_bfd_error_handler
)
4047 (_("Warning: alignment %u of symbol `%s' in %B"
4048 " is smaller than %u in %B"),
4049 normal_bfd
, common_bfd
,
4050 1 << normal_align
, name
, 1 << common_align
);
4054 /* Remember the symbol size if it isn't undefined. */
4055 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4056 && (definition
|| h
->size
== 0))
4059 && h
->size
!= isym
->st_size
4060 && ! size_change_ok
)
4061 (*_bfd_error_handler
)
4062 (_("Warning: size of symbol `%s' changed"
4063 " from %lu in %B to %lu in %B"),
4065 name
, (unsigned long) h
->size
,
4066 (unsigned long) isym
->st_size
);
4068 h
->size
= isym
->st_size
;
4071 /* If this is a common symbol, then we always want H->SIZE
4072 to be the size of the common symbol. The code just above
4073 won't fix the size if a common symbol becomes larger. We
4074 don't warn about a size change here, because that is
4075 covered by --warn-common. */
4076 if (h
->root
.type
== bfd_link_hash_common
)
4077 h
->size
= h
->root
.u
.c
.size
;
4079 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4080 && (definition
|| h
->type
== STT_NOTYPE
))
4082 if (h
->type
!= STT_NOTYPE
4083 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4084 && ! type_change_ok
)
4085 (*_bfd_error_handler
)
4086 (_("Warning: type of symbol `%s' changed"
4087 " from %d to %d in %B"),
4088 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4090 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4093 /* If st_other has a processor-specific meaning, specific
4094 code might be needed here. We never merge the visibility
4095 attribute with the one from a dynamic object. */
4096 if (bed
->elf_backend_merge_symbol_attribute
)
4097 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4100 /* If this symbol has default visibility and the user has requested
4101 we not re-export it, then mark it as hidden. */
4102 if (definition
&& !dynamic
4104 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4105 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4106 isym
->st_other
= (STV_HIDDEN
4107 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4109 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4111 unsigned char hvis
, symvis
, other
, nvis
;
4113 /* Only merge the visibility. Leave the remainder of the
4114 st_other field to elf_backend_merge_symbol_attribute. */
4115 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4117 /* Combine visibilities, using the most constraining one. */
4118 hvis
= ELF_ST_VISIBILITY (h
->other
);
4119 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4125 nvis
= hvis
< symvis
? hvis
: symvis
;
4127 h
->other
= other
| nvis
;
4130 /* Set a flag in the hash table entry indicating the type of
4131 reference or definition we just found. Keep a count of
4132 the number of dynamic symbols we find. A dynamic symbol
4133 is one which is referenced or defined by both a regular
4134 object and a shared object. */
4141 if (bind
!= STB_WEAK
)
4142 h
->ref_regular_nonweak
= 1;
4146 if (! info
->executable
4159 || (h
->u
.weakdef
!= NULL
4161 && h
->u
.weakdef
->dynindx
!= -1))
4165 if (definition
&& (sec
->flags
& SEC_DEBUGGING
))
4167 /* We don't want to make debug symbol dynamic. */
4168 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4172 /* Check to see if we need to add an indirect symbol for
4173 the default name. */
4174 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4175 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4176 &sec
, &value
, &dynsym
,
4178 goto error_free_vers
;
4180 if (definition
&& !dynamic
)
4182 char *p
= strchr (name
, ELF_VER_CHR
);
4183 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4185 /* Queue non-default versions so that .symver x, x@FOO
4186 aliases can be checked. */
4189 amt
= ((isymend
- isym
+ 1)
4190 * sizeof (struct elf_link_hash_entry
*));
4191 nondeflt_vers
= bfd_malloc (amt
);
4193 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4197 if (dynsym
&& h
->dynindx
== -1)
4199 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4200 goto error_free_vers
;
4201 if (h
->u
.weakdef
!= NULL
4203 && h
->u
.weakdef
->dynindx
== -1)
4205 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4206 goto error_free_vers
;
4209 else if (dynsym
&& h
->dynindx
!= -1)
4210 /* If the symbol already has a dynamic index, but
4211 visibility says it should not be visible, turn it into
4213 switch (ELF_ST_VISIBILITY (h
->other
))
4217 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4228 const char *soname
= elf_dt_name (abfd
);
4230 /* A symbol from a library loaded via DT_NEEDED of some
4231 other library is referenced by a regular object.
4232 Add a DT_NEEDED entry for it. Issue an error if
4233 --no-add-needed is used. */
4234 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4236 (*_bfd_error_handler
)
4237 (_("%s: invalid DSO for symbol `%s' definition"),
4239 bfd_set_error (bfd_error_bad_value
);
4240 goto error_free_vers
;
4243 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4246 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4248 goto error_free_vers
;
4250 BFD_ASSERT (ret
== 0);
4255 if (extversym
!= NULL
)
4261 if (isymbuf
!= NULL
)
4267 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4271 /* Restore the symbol table. */
4272 if (bed
->as_needed_cleanup
)
4273 (*bed
->as_needed_cleanup
) (abfd
, info
);
4274 old_hash
= (char *) old_tab
+ tabsize
;
4275 old_ent
= (char *) old_hash
+ hashsize
;
4276 sym_hash
= elf_sym_hashes (abfd
);
4277 htab
->root
.table
.table
= old_table
;
4278 htab
->root
.table
.size
= old_size
;
4279 htab
->root
.table
.count
= old_count
;
4280 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4281 memcpy (sym_hash
, old_hash
, hashsize
);
4282 htab
->root
.undefs
= old_undefs
;
4283 htab
->root
.undefs_tail
= old_undefs_tail
;
4284 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4286 struct bfd_hash_entry
*p
;
4287 struct elf_link_hash_entry
*h
;
4289 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4291 h
= (struct elf_link_hash_entry
*) p
;
4292 if (h
->root
.type
== bfd_link_hash_warning
)
4293 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4294 if (h
->dynindx
>= old_dynsymcount
)
4295 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4297 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4298 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4299 h
= (struct elf_link_hash_entry
*) p
;
4300 if (h
->root
.type
== bfd_link_hash_warning
)
4302 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4303 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4308 /* Make a special call to the linker "notice" function to
4309 tell it that symbols added for crefs may need to be removed. */
4310 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4315 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4317 if (nondeflt_vers
!= NULL
)
4318 free (nondeflt_vers
);
4322 if (old_tab
!= NULL
)
4324 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4331 /* Now that all the symbols from this input file are created, handle
4332 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4333 if (nondeflt_vers
!= NULL
)
4335 bfd_size_type cnt
, symidx
;
4337 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4339 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4340 char *shortname
, *p
;
4342 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4344 || (h
->root
.type
!= bfd_link_hash_defined
4345 && h
->root
.type
!= bfd_link_hash_defweak
))
4348 amt
= p
- h
->root
.root
.string
;
4349 shortname
= bfd_malloc (amt
+ 1);
4350 memcpy (shortname
, h
->root
.root
.string
, amt
);
4351 shortname
[amt
] = '\0';
4353 hi
= (struct elf_link_hash_entry
*)
4354 bfd_link_hash_lookup (&htab
->root
, shortname
,
4355 FALSE
, FALSE
, FALSE
);
4357 && hi
->root
.type
== h
->root
.type
4358 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4359 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4361 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4362 hi
->root
.type
= bfd_link_hash_indirect
;
4363 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4364 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4365 sym_hash
= elf_sym_hashes (abfd
);
4367 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4368 if (sym_hash
[symidx
] == hi
)
4370 sym_hash
[symidx
] = h
;
4376 free (nondeflt_vers
);
4377 nondeflt_vers
= NULL
;
4380 /* Now set the weakdefs field correctly for all the weak defined
4381 symbols we found. The only way to do this is to search all the
4382 symbols. Since we only need the information for non functions in
4383 dynamic objects, that's the only time we actually put anything on
4384 the list WEAKS. We need this information so that if a regular
4385 object refers to a symbol defined weakly in a dynamic object, the
4386 real symbol in the dynamic object is also put in the dynamic
4387 symbols; we also must arrange for both symbols to point to the
4388 same memory location. We could handle the general case of symbol
4389 aliasing, but a general symbol alias can only be generated in
4390 assembler code, handling it correctly would be very time
4391 consuming, and other ELF linkers don't handle general aliasing
4395 struct elf_link_hash_entry
**hpp
;
4396 struct elf_link_hash_entry
**hppend
;
4397 struct elf_link_hash_entry
**sorted_sym_hash
;
4398 struct elf_link_hash_entry
*h
;
4401 /* Since we have to search the whole symbol list for each weak
4402 defined symbol, search time for N weak defined symbols will be
4403 O(N^2). Binary search will cut it down to O(NlogN). */
4404 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4405 sorted_sym_hash
= bfd_malloc (amt
);
4406 if (sorted_sym_hash
== NULL
)
4408 sym_hash
= sorted_sym_hash
;
4409 hpp
= elf_sym_hashes (abfd
);
4410 hppend
= hpp
+ extsymcount
;
4412 for (; hpp
< hppend
; hpp
++)
4416 && h
->root
.type
== bfd_link_hash_defined
4417 && h
->type
!= STT_FUNC
)
4425 qsort (sorted_sym_hash
, sym_count
,
4426 sizeof (struct elf_link_hash_entry
*),
4429 while (weaks
!= NULL
)
4431 struct elf_link_hash_entry
*hlook
;
4438 weaks
= hlook
->u
.weakdef
;
4439 hlook
->u
.weakdef
= NULL
;
4441 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4442 || hlook
->root
.type
== bfd_link_hash_defweak
4443 || hlook
->root
.type
== bfd_link_hash_common
4444 || hlook
->root
.type
== bfd_link_hash_indirect
);
4445 slook
= hlook
->root
.u
.def
.section
;
4446 vlook
= hlook
->root
.u
.def
.value
;
4453 bfd_signed_vma vdiff
;
4455 h
= sorted_sym_hash
[idx
];
4456 vdiff
= vlook
- h
->root
.u
.def
.value
;
4463 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4476 /* We didn't find a value/section match. */
4480 for (i
= ilook
; i
< sym_count
; i
++)
4482 h
= sorted_sym_hash
[i
];
4484 /* Stop if value or section doesn't match. */
4485 if (h
->root
.u
.def
.value
!= vlook
4486 || h
->root
.u
.def
.section
!= slook
)
4488 else if (h
!= hlook
)
4490 hlook
->u
.weakdef
= h
;
4492 /* If the weak definition is in the list of dynamic
4493 symbols, make sure the real definition is put
4495 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4497 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4501 /* If the real definition is in the list of dynamic
4502 symbols, make sure the weak definition is put
4503 there as well. If we don't do this, then the
4504 dynamic loader might not merge the entries for the
4505 real definition and the weak definition. */
4506 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4508 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4516 free (sorted_sym_hash
);
4519 if (bed
->check_directives
)
4520 (*bed
->check_directives
) (abfd
, info
);
4522 /* If this object is the same format as the output object, and it is
4523 not a shared library, then let the backend look through the
4526 This is required to build global offset table entries and to
4527 arrange for dynamic relocs. It is not required for the
4528 particular common case of linking non PIC code, even when linking
4529 against shared libraries, but unfortunately there is no way of
4530 knowing whether an object file has been compiled PIC or not.
4531 Looking through the relocs is not particularly time consuming.
4532 The problem is that we must either (1) keep the relocs in memory,
4533 which causes the linker to require additional runtime memory or
4534 (2) read the relocs twice from the input file, which wastes time.
4535 This would be a good case for using mmap.
4537 I have no idea how to handle linking PIC code into a file of a
4538 different format. It probably can't be done. */
4540 && is_elf_hash_table (htab
)
4541 && htab
->root
.creator
== abfd
->xvec
4542 && bed
->check_relocs
!= NULL
)
4546 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4548 Elf_Internal_Rela
*internal_relocs
;
4551 if ((o
->flags
& SEC_RELOC
) == 0
4552 || o
->reloc_count
== 0
4553 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4554 && (o
->flags
& SEC_DEBUGGING
) != 0)
4555 || bfd_is_abs_section (o
->output_section
))
4558 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4560 if (internal_relocs
== NULL
)
4563 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4565 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4566 free (internal_relocs
);
4573 /* If this is a non-traditional link, try to optimize the handling
4574 of the .stab/.stabstr sections. */
4576 && ! info
->traditional_format
4577 && is_elf_hash_table (htab
)
4578 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4582 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4583 if (stabstr
!= NULL
)
4585 bfd_size_type string_offset
= 0;
4588 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4589 if (CONST_STRNEQ (stab
->name
, ".stab")
4590 && (!stab
->name
[5] ||
4591 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4592 && (stab
->flags
& SEC_MERGE
) == 0
4593 && !bfd_is_abs_section (stab
->output_section
))
4595 struct bfd_elf_section_data
*secdata
;
4597 secdata
= elf_section_data (stab
);
4598 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4599 stabstr
, &secdata
->sec_info
,
4602 if (secdata
->sec_info
)
4603 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4608 if (is_elf_hash_table (htab
) && add_needed
)
4610 /* Add this bfd to the loaded list. */
4611 struct elf_link_loaded_list
*n
;
4613 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4617 n
->next
= htab
->loaded
;
4624 if (old_tab
!= NULL
)
4626 if (nondeflt_vers
!= NULL
)
4627 free (nondeflt_vers
);
4628 if (extversym
!= NULL
)
4631 if (isymbuf
!= NULL
)
4637 /* Return the linker hash table entry of a symbol that might be
4638 satisfied by an archive symbol. Return -1 on error. */
4640 struct elf_link_hash_entry
*
4641 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4642 struct bfd_link_info
*info
,
4645 struct elf_link_hash_entry
*h
;
4649 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4653 /* If this is a default version (the name contains @@), look up the
4654 symbol again with only one `@' as well as without the version.
4655 The effect is that references to the symbol with and without the
4656 version will be matched by the default symbol in the archive. */
4658 p
= strchr (name
, ELF_VER_CHR
);
4659 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4662 /* First check with only one `@'. */
4663 len
= strlen (name
);
4664 copy
= bfd_alloc (abfd
, len
);
4666 return (struct elf_link_hash_entry
*) 0 - 1;
4668 first
= p
- name
+ 1;
4669 memcpy (copy
, name
, first
);
4670 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4672 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4675 /* We also need to check references to the symbol without the
4677 copy
[first
- 1] = '\0';
4678 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4679 FALSE
, FALSE
, FALSE
);
4682 bfd_release (abfd
, copy
);
4686 /* Add symbols from an ELF archive file to the linker hash table. We
4687 don't use _bfd_generic_link_add_archive_symbols because of a
4688 problem which arises on UnixWare. The UnixWare libc.so is an
4689 archive which includes an entry libc.so.1 which defines a bunch of
4690 symbols. The libc.so archive also includes a number of other
4691 object files, which also define symbols, some of which are the same
4692 as those defined in libc.so.1. Correct linking requires that we
4693 consider each object file in turn, and include it if it defines any
4694 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4695 this; it looks through the list of undefined symbols, and includes
4696 any object file which defines them. When this algorithm is used on
4697 UnixWare, it winds up pulling in libc.so.1 early and defining a
4698 bunch of symbols. This means that some of the other objects in the
4699 archive are not included in the link, which is incorrect since they
4700 precede libc.so.1 in the archive.
4702 Fortunately, ELF archive handling is simpler than that done by
4703 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4704 oddities. In ELF, if we find a symbol in the archive map, and the
4705 symbol is currently undefined, we know that we must pull in that
4708 Unfortunately, we do have to make multiple passes over the symbol
4709 table until nothing further is resolved. */
4712 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4715 bfd_boolean
*defined
= NULL
;
4716 bfd_boolean
*included
= NULL
;
4720 const struct elf_backend_data
*bed
;
4721 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4722 (bfd
*, struct bfd_link_info
*, const char *);
4724 if (! bfd_has_map (abfd
))
4726 /* An empty archive is a special case. */
4727 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4729 bfd_set_error (bfd_error_no_armap
);
4733 /* Keep track of all symbols we know to be already defined, and all
4734 files we know to be already included. This is to speed up the
4735 second and subsequent passes. */
4736 c
= bfd_ardata (abfd
)->symdef_count
;
4740 amt
*= sizeof (bfd_boolean
);
4741 defined
= bfd_zmalloc (amt
);
4742 included
= bfd_zmalloc (amt
);
4743 if (defined
== NULL
|| included
== NULL
)
4746 symdefs
= bfd_ardata (abfd
)->symdefs
;
4747 bed
= get_elf_backend_data (abfd
);
4748 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4761 symdefend
= symdef
+ c
;
4762 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4764 struct elf_link_hash_entry
*h
;
4766 struct bfd_link_hash_entry
*undefs_tail
;
4769 if (defined
[i
] || included
[i
])
4771 if (symdef
->file_offset
== last
)
4777 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4778 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4784 if (h
->root
.type
== bfd_link_hash_common
)
4786 /* We currently have a common symbol. The archive map contains
4787 a reference to this symbol, so we may want to include it. We
4788 only want to include it however, if this archive element
4789 contains a definition of the symbol, not just another common
4792 Unfortunately some archivers (including GNU ar) will put
4793 declarations of common symbols into their archive maps, as
4794 well as real definitions, so we cannot just go by the archive
4795 map alone. Instead we must read in the element's symbol
4796 table and check that to see what kind of symbol definition
4798 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4801 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4803 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4808 /* We need to include this archive member. */
4809 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4810 if (element
== NULL
)
4813 if (! bfd_check_format (element
, bfd_object
))
4816 /* Doublecheck that we have not included this object
4817 already--it should be impossible, but there may be
4818 something wrong with the archive. */
4819 if (element
->archive_pass
!= 0)
4821 bfd_set_error (bfd_error_bad_value
);
4824 element
->archive_pass
= 1;
4826 undefs_tail
= info
->hash
->undefs_tail
;
4828 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4831 if (! bfd_link_add_symbols (element
, info
))
4834 /* If there are any new undefined symbols, we need to make
4835 another pass through the archive in order to see whether
4836 they can be defined. FIXME: This isn't perfect, because
4837 common symbols wind up on undefs_tail and because an
4838 undefined symbol which is defined later on in this pass
4839 does not require another pass. This isn't a bug, but it
4840 does make the code less efficient than it could be. */
4841 if (undefs_tail
!= info
->hash
->undefs_tail
)
4844 /* Look backward to mark all symbols from this object file
4845 which we have already seen in this pass. */
4849 included
[mark
] = TRUE
;
4854 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4856 /* We mark subsequent symbols from this object file as we go
4857 on through the loop. */
4858 last
= symdef
->file_offset
;
4869 if (defined
!= NULL
)
4871 if (included
!= NULL
)
4876 /* Given an ELF BFD, add symbols to the global hash table as
4880 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4882 switch (bfd_get_format (abfd
))
4885 return elf_link_add_object_symbols (abfd
, info
);
4887 return elf_link_add_archive_symbols (abfd
, info
);
4889 bfd_set_error (bfd_error_wrong_format
);
4894 /* This function will be called though elf_link_hash_traverse to store
4895 all hash value of the exported symbols in an array. */
4898 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4900 unsigned long **valuep
= data
;
4906 if (h
->root
.type
== bfd_link_hash_warning
)
4907 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4909 /* Ignore indirect symbols. These are added by the versioning code. */
4910 if (h
->dynindx
== -1)
4913 name
= h
->root
.root
.string
;
4914 p
= strchr (name
, ELF_VER_CHR
);
4917 alc
= bfd_malloc (p
- name
+ 1);
4918 memcpy (alc
, name
, p
- name
);
4919 alc
[p
- name
] = '\0';
4923 /* Compute the hash value. */
4924 ha
= bfd_elf_hash (name
);
4926 /* Store the found hash value in the array given as the argument. */
4929 /* And store it in the struct so that we can put it in the hash table
4931 h
->u
.elf_hash_value
= ha
;
4939 struct collect_gnu_hash_codes
4942 const struct elf_backend_data
*bed
;
4943 unsigned long int nsyms
;
4944 unsigned long int maskbits
;
4945 unsigned long int *hashcodes
;
4946 unsigned long int *hashval
;
4947 unsigned long int *indx
;
4948 unsigned long int *counts
;
4951 long int min_dynindx
;
4952 unsigned long int bucketcount
;
4953 unsigned long int symindx
;
4954 long int local_indx
;
4955 long int shift1
, shift2
;
4956 unsigned long int mask
;
4959 /* This function will be called though elf_link_hash_traverse to store
4960 all hash value of the exported symbols in an array. */
4963 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4965 struct collect_gnu_hash_codes
*s
= data
;
4971 if (h
->root
.type
== bfd_link_hash_warning
)
4972 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4974 /* Ignore indirect symbols. These are added by the versioning code. */
4975 if (h
->dynindx
== -1)
4978 /* Ignore also local symbols and undefined symbols. */
4979 if (! (*s
->bed
->elf_hash_symbol
) (h
))
4982 name
= h
->root
.root
.string
;
4983 p
= strchr (name
, ELF_VER_CHR
);
4986 alc
= bfd_malloc (p
- name
+ 1);
4987 memcpy (alc
, name
, p
- name
);
4988 alc
[p
- name
] = '\0';
4992 /* Compute the hash value. */
4993 ha
= bfd_elf_gnu_hash (name
);
4995 /* Store the found hash value in the array for compute_bucket_count,
4996 and also for .dynsym reordering purposes. */
4997 s
->hashcodes
[s
->nsyms
] = ha
;
4998 s
->hashval
[h
->dynindx
] = ha
;
5000 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5001 s
->min_dynindx
= h
->dynindx
;
5009 /* This function will be called though elf_link_hash_traverse to do
5010 final dynaminc symbol renumbering. */
5013 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5015 struct collect_gnu_hash_codes
*s
= data
;
5016 unsigned long int bucket
;
5017 unsigned long int val
;
5019 if (h
->root
.type
== bfd_link_hash_warning
)
5020 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5022 /* Ignore indirect symbols. */
5023 if (h
->dynindx
== -1)
5026 /* Ignore also local symbols and undefined symbols. */
5027 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5029 if (h
->dynindx
>= s
->min_dynindx
)
5030 h
->dynindx
= s
->local_indx
++;
5034 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5035 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5036 & ((s
->maskbits
>> s
->shift1
) - 1);
5037 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5039 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5040 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5041 if (s
->counts
[bucket
] == 1)
5042 /* Last element terminates the chain. */
5044 bfd_put_32 (s
->output_bfd
, val
,
5045 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5046 --s
->counts
[bucket
];
5047 h
->dynindx
= s
->indx
[bucket
]++;
5051 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5054 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5056 return !(h
->forced_local
5057 || h
->root
.type
== bfd_link_hash_undefined
5058 || h
->root
.type
== bfd_link_hash_undefweak
5059 || ((h
->root
.type
== bfd_link_hash_defined
5060 || h
->root
.type
== bfd_link_hash_defweak
)
5061 && h
->root
.u
.def
.section
->output_section
== NULL
));
5064 /* Array used to determine the number of hash table buckets to use
5065 based on the number of symbols there are. If there are fewer than
5066 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5067 fewer than 37 we use 17 buckets, and so forth. We never use more
5068 than 32771 buckets. */
5070 static const size_t elf_buckets
[] =
5072 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5076 /* Compute bucket count for hashing table. We do not use a static set
5077 of possible tables sizes anymore. Instead we determine for all
5078 possible reasonable sizes of the table the outcome (i.e., the
5079 number of collisions etc) and choose the best solution. The
5080 weighting functions are not too simple to allow the table to grow
5081 without bounds. Instead one of the weighting factors is the size.
5082 Therefore the result is always a good payoff between few collisions
5083 (= short chain lengths) and table size. */
5085 compute_bucket_count (struct bfd_link_info
*info
, unsigned long int *hashcodes
,
5086 unsigned long int nsyms
, int gnu_hash
)
5088 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5089 size_t best_size
= 0;
5090 unsigned long int i
;
5093 /* We have a problem here. The following code to optimize the table
5094 size requires an integer type with more the 32 bits. If
5095 BFD_HOST_U_64_BIT is set we know about such a type. */
5096 #ifdef BFD_HOST_U_64_BIT
5101 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5102 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5103 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5104 unsigned long int *counts
;
5106 /* Possible optimization parameters: if we have NSYMS symbols we say
5107 that the hashing table must at least have NSYMS/4 and at most
5109 minsize
= nsyms
/ 4;
5112 best_size
= maxsize
= nsyms
* 2;
5117 if ((best_size
& 31) == 0)
5121 /* Create array where we count the collisions in. We must use bfd_malloc
5122 since the size could be large. */
5124 amt
*= sizeof (unsigned long int);
5125 counts
= bfd_malloc (amt
);
5129 /* Compute the "optimal" size for the hash table. The criteria is a
5130 minimal chain length. The minor criteria is (of course) the size
5132 for (i
= minsize
; i
< maxsize
; ++i
)
5134 /* Walk through the array of hashcodes and count the collisions. */
5135 BFD_HOST_U_64_BIT max
;
5136 unsigned long int j
;
5137 unsigned long int fact
;
5139 if (gnu_hash
&& (i
& 31) == 0)
5142 memset (counts
, '\0', i
* sizeof (unsigned long int));
5144 /* Determine how often each hash bucket is used. */
5145 for (j
= 0; j
< nsyms
; ++j
)
5146 ++counts
[hashcodes
[j
] % i
];
5148 /* For the weight function we need some information about the
5149 pagesize on the target. This is information need not be 100%
5150 accurate. Since this information is not available (so far) we
5151 define it here to a reasonable default value. If it is crucial
5152 to have a better value some day simply define this value. */
5153 # ifndef BFD_TARGET_PAGESIZE
5154 # define BFD_TARGET_PAGESIZE (4096)
5157 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5159 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5162 /* Variant 1: optimize for short chains. We add the squares
5163 of all the chain lengths (which favors many small chain
5164 over a few long chains). */
5165 for (j
= 0; j
< i
; ++j
)
5166 max
+= counts
[j
] * counts
[j
];
5168 /* This adds penalties for the overall size of the table. */
5169 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5172 /* Variant 2: Optimize a lot more for small table. Here we
5173 also add squares of the size but we also add penalties for
5174 empty slots (the +1 term). */
5175 for (j
= 0; j
< i
; ++j
)
5176 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5178 /* The overall size of the table is considered, but not as
5179 strong as in variant 1, where it is squared. */
5180 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5184 /* Compare with current best results. */
5185 if (max
< best_chlen
)
5195 #endif /* defined (BFD_HOST_U_64_BIT) */
5197 /* This is the fallback solution if no 64bit type is available or if we
5198 are not supposed to spend much time on optimizations. We select the
5199 bucket count using a fixed set of numbers. */
5200 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5202 best_size
= elf_buckets
[i
];
5203 if (nsyms
< elf_buckets
[i
+ 1])
5206 if (gnu_hash
&& best_size
< 2)
5213 /* Set up the sizes and contents of the ELF dynamic sections. This is
5214 called by the ELF linker emulation before_allocation routine. We
5215 must set the sizes of the sections before the linker sets the
5216 addresses of the various sections. */
5219 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5222 const char *filter_shlib
,
5223 const char * const *auxiliary_filters
,
5224 struct bfd_link_info
*info
,
5225 asection
**sinterpptr
,
5226 struct bfd_elf_version_tree
*verdefs
)
5228 bfd_size_type soname_indx
;
5230 const struct elf_backend_data
*bed
;
5231 struct elf_assign_sym_version_info asvinfo
;
5235 soname_indx
= (bfd_size_type
) -1;
5237 if (!is_elf_hash_table (info
->hash
))
5240 bed
= get_elf_backend_data (output_bfd
);
5241 elf_tdata (output_bfd
)->relro
= info
->relro
;
5242 if (info
->execstack
)
5243 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5244 else if (info
->noexecstack
)
5245 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5249 asection
*notesec
= NULL
;
5252 for (inputobj
= info
->input_bfds
;
5254 inputobj
= inputobj
->link_next
)
5258 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
5260 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5263 if (s
->flags
& SEC_CODE
)
5267 else if (bed
->default_execstack
)
5272 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5273 if (exec
&& info
->relocatable
5274 && notesec
->output_section
!= bfd_abs_section_ptr
)
5275 notesec
->output_section
->flags
|= SEC_CODE
;
5279 /* Any syms created from now on start with -1 in
5280 got.refcount/offset and plt.refcount/offset. */
5281 elf_hash_table (info
)->init_got_refcount
5282 = elf_hash_table (info
)->init_got_offset
;
5283 elf_hash_table (info
)->init_plt_refcount
5284 = elf_hash_table (info
)->init_plt_offset
;
5286 /* The backend may have to create some sections regardless of whether
5287 we're dynamic or not. */
5288 if (bed
->elf_backend_always_size_sections
5289 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5292 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5295 dynobj
= elf_hash_table (info
)->dynobj
;
5297 /* If there were no dynamic objects in the link, there is nothing to
5302 if (elf_hash_table (info
)->dynamic_sections_created
)
5304 struct elf_info_failed eif
;
5305 struct elf_link_hash_entry
*h
;
5307 struct bfd_elf_version_tree
*t
;
5308 struct bfd_elf_version_expr
*d
;
5310 bfd_boolean all_defined
;
5312 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5313 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5317 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5319 if (soname_indx
== (bfd_size_type
) -1
5320 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5326 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5328 info
->flags
|= DF_SYMBOLIC
;
5335 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5337 if (indx
== (bfd_size_type
) -1
5338 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5341 if (info
->new_dtags
)
5343 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5344 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5349 if (filter_shlib
!= NULL
)
5353 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5354 filter_shlib
, TRUE
);
5355 if (indx
== (bfd_size_type
) -1
5356 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5360 if (auxiliary_filters
!= NULL
)
5362 const char * const *p
;
5364 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5368 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5370 if (indx
== (bfd_size_type
) -1
5371 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5377 eif
.verdefs
= verdefs
;
5380 /* If we are supposed to export all symbols into the dynamic symbol
5381 table (this is not the normal case), then do so. */
5382 if (info
->export_dynamic
5383 || (info
->executable
&& info
->dynamic
))
5385 elf_link_hash_traverse (elf_hash_table (info
),
5386 _bfd_elf_export_symbol
,
5392 /* Make all global versions with definition. */
5393 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5394 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5395 if (!d
->symver
&& d
->symbol
)
5397 const char *verstr
, *name
;
5398 size_t namelen
, verlen
, newlen
;
5400 struct elf_link_hash_entry
*newh
;
5403 namelen
= strlen (name
);
5405 verlen
= strlen (verstr
);
5406 newlen
= namelen
+ verlen
+ 3;
5408 newname
= bfd_malloc (newlen
);
5409 if (newname
== NULL
)
5411 memcpy (newname
, name
, namelen
);
5413 /* Check the hidden versioned definition. */
5414 p
= newname
+ namelen
;
5416 memcpy (p
, verstr
, verlen
+ 1);
5417 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5418 newname
, FALSE
, FALSE
,
5421 || (newh
->root
.type
!= bfd_link_hash_defined
5422 && newh
->root
.type
!= bfd_link_hash_defweak
))
5424 /* Check the default versioned definition. */
5426 memcpy (p
, verstr
, verlen
+ 1);
5427 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5428 newname
, FALSE
, FALSE
,
5433 /* Mark this version if there is a definition and it is
5434 not defined in a shared object. */
5436 && !newh
->def_dynamic
5437 && (newh
->root
.type
== bfd_link_hash_defined
5438 || newh
->root
.type
== bfd_link_hash_defweak
))
5442 /* Attach all the symbols to their version information. */
5443 asvinfo
.output_bfd
= output_bfd
;
5444 asvinfo
.info
= info
;
5445 asvinfo
.verdefs
= verdefs
;
5446 asvinfo
.failed
= FALSE
;
5448 elf_link_hash_traverse (elf_hash_table (info
),
5449 _bfd_elf_link_assign_sym_version
,
5454 if (!info
->allow_undefined_version
)
5456 /* Check if all global versions have a definition. */
5458 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5459 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5460 if (!d
->symver
&& !d
->script
)
5462 (*_bfd_error_handler
)
5463 (_("%s: undefined version: %s"),
5464 d
->pattern
, t
->name
);
5465 all_defined
= FALSE
;
5470 bfd_set_error (bfd_error_bad_value
);
5475 /* Find all symbols which were defined in a dynamic object and make
5476 the backend pick a reasonable value for them. */
5477 elf_link_hash_traverse (elf_hash_table (info
),
5478 _bfd_elf_adjust_dynamic_symbol
,
5483 /* Add some entries to the .dynamic section. We fill in some of the
5484 values later, in bfd_elf_final_link, but we must add the entries
5485 now so that we know the final size of the .dynamic section. */
5487 /* If there are initialization and/or finalization functions to
5488 call then add the corresponding DT_INIT/DT_FINI entries. */
5489 h
= (info
->init_function
5490 ? elf_link_hash_lookup (elf_hash_table (info
),
5491 info
->init_function
, FALSE
,
5498 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5501 h
= (info
->fini_function
5502 ? elf_link_hash_lookup (elf_hash_table (info
),
5503 info
->fini_function
, FALSE
,
5510 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5514 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5515 if (s
!= NULL
&& s
->linker_has_input
)
5517 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5518 if (! info
->executable
)
5523 for (sub
= info
->input_bfds
; sub
!= NULL
;
5524 sub
= sub
->link_next
)
5525 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5526 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5527 if (elf_section_data (o
)->this_hdr
.sh_type
5528 == SHT_PREINIT_ARRAY
)
5530 (*_bfd_error_handler
)
5531 (_("%B: .preinit_array section is not allowed in DSO"),
5536 bfd_set_error (bfd_error_nonrepresentable_section
);
5540 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5541 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5544 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5545 if (s
!= NULL
&& s
->linker_has_input
)
5547 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5548 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5551 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5552 if (s
!= NULL
&& s
->linker_has_input
)
5554 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5555 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5559 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5560 /* If .dynstr is excluded from the link, we don't want any of
5561 these tags. Strictly, we should be checking each section
5562 individually; This quick check covers for the case where
5563 someone does a /DISCARD/ : { *(*) }. */
5564 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5566 bfd_size_type strsize
;
5568 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5569 if ((info
->emit_hash
5570 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5571 || (info
->emit_gnu_hash
5572 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5573 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5574 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5575 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5576 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5577 bed
->s
->sizeof_sym
))
5582 /* The backend must work out the sizes of all the other dynamic
5584 if (bed
->elf_backend_size_dynamic_sections
5585 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5588 if (elf_hash_table (info
)->dynamic_sections_created
)
5590 unsigned long section_sym_count
;
5593 /* Set up the version definition section. */
5594 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5595 BFD_ASSERT (s
!= NULL
);
5597 /* We may have created additional version definitions if we are
5598 just linking a regular application. */
5599 verdefs
= asvinfo
.verdefs
;
5601 /* Skip anonymous version tag. */
5602 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5603 verdefs
= verdefs
->next
;
5605 if (verdefs
== NULL
&& !info
->create_default_symver
)
5606 s
->flags
|= SEC_EXCLUDE
;
5611 struct bfd_elf_version_tree
*t
;
5613 Elf_Internal_Verdef def
;
5614 Elf_Internal_Verdaux defaux
;
5615 struct bfd_link_hash_entry
*bh
;
5616 struct elf_link_hash_entry
*h
;
5622 /* Make space for the base version. */
5623 size
+= sizeof (Elf_External_Verdef
);
5624 size
+= sizeof (Elf_External_Verdaux
);
5627 /* Make space for the default version. */
5628 if (info
->create_default_symver
)
5630 size
+= sizeof (Elf_External_Verdef
);
5634 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5636 struct bfd_elf_version_deps
*n
;
5638 size
+= sizeof (Elf_External_Verdef
);
5639 size
+= sizeof (Elf_External_Verdaux
);
5642 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5643 size
+= sizeof (Elf_External_Verdaux
);
5647 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5648 if (s
->contents
== NULL
&& s
->size
!= 0)
5651 /* Fill in the version definition section. */
5655 def
.vd_version
= VER_DEF_CURRENT
;
5656 def
.vd_flags
= VER_FLG_BASE
;
5659 if (info
->create_default_symver
)
5661 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5662 def
.vd_next
= sizeof (Elf_External_Verdef
);
5666 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5667 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5668 + sizeof (Elf_External_Verdaux
));
5671 if (soname_indx
!= (bfd_size_type
) -1)
5673 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5675 def
.vd_hash
= bfd_elf_hash (soname
);
5676 defaux
.vda_name
= soname_indx
;
5683 name
= lbasename (output_bfd
->filename
);
5684 def
.vd_hash
= bfd_elf_hash (name
);
5685 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5687 if (indx
== (bfd_size_type
) -1)
5689 defaux
.vda_name
= indx
;
5691 defaux
.vda_next
= 0;
5693 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5694 (Elf_External_Verdef
*) p
);
5695 p
+= sizeof (Elf_External_Verdef
);
5696 if (info
->create_default_symver
)
5698 /* Add a symbol representing this version. */
5700 if (! (_bfd_generic_link_add_one_symbol
5701 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5703 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5705 h
= (struct elf_link_hash_entry
*) bh
;
5708 h
->type
= STT_OBJECT
;
5709 h
->verinfo
.vertree
= NULL
;
5711 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5714 /* Create a duplicate of the base version with the same
5715 aux block, but different flags. */
5718 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5720 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5721 + sizeof (Elf_External_Verdaux
));
5724 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5725 (Elf_External_Verdef
*) p
);
5726 p
+= sizeof (Elf_External_Verdef
);
5728 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5729 (Elf_External_Verdaux
*) p
);
5730 p
+= sizeof (Elf_External_Verdaux
);
5732 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5735 struct bfd_elf_version_deps
*n
;
5738 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5741 /* Add a symbol representing this version. */
5743 if (! (_bfd_generic_link_add_one_symbol
5744 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5746 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5748 h
= (struct elf_link_hash_entry
*) bh
;
5751 h
->type
= STT_OBJECT
;
5752 h
->verinfo
.vertree
= t
;
5754 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5757 def
.vd_version
= VER_DEF_CURRENT
;
5759 if (t
->globals
.list
== NULL
5760 && t
->locals
.list
== NULL
5762 def
.vd_flags
|= VER_FLG_WEAK
;
5763 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5764 def
.vd_cnt
= cdeps
+ 1;
5765 def
.vd_hash
= bfd_elf_hash (t
->name
);
5766 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5768 if (t
->next
!= NULL
)
5769 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5770 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5772 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5773 (Elf_External_Verdef
*) p
);
5774 p
+= sizeof (Elf_External_Verdef
);
5776 defaux
.vda_name
= h
->dynstr_index
;
5777 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5779 defaux
.vda_next
= 0;
5780 if (t
->deps
!= NULL
)
5781 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5782 t
->name_indx
= defaux
.vda_name
;
5784 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5785 (Elf_External_Verdaux
*) p
);
5786 p
+= sizeof (Elf_External_Verdaux
);
5788 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5790 if (n
->version_needed
== NULL
)
5792 /* This can happen if there was an error in the
5794 defaux
.vda_name
= 0;
5798 defaux
.vda_name
= n
->version_needed
->name_indx
;
5799 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5802 if (n
->next
== NULL
)
5803 defaux
.vda_next
= 0;
5805 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5807 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5808 (Elf_External_Verdaux
*) p
);
5809 p
+= sizeof (Elf_External_Verdaux
);
5813 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5814 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5817 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5820 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5822 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5825 else if (info
->flags
& DF_BIND_NOW
)
5827 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5833 if (info
->executable
)
5834 info
->flags_1
&= ~ (DF_1_INITFIRST
5837 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5841 /* Work out the size of the version reference section. */
5843 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5844 BFD_ASSERT (s
!= NULL
);
5846 struct elf_find_verdep_info sinfo
;
5848 sinfo
.output_bfd
= output_bfd
;
5850 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5851 if (sinfo
.vers
== 0)
5853 sinfo
.failed
= FALSE
;
5855 elf_link_hash_traverse (elf_hash_table (info
),
5856 _bfd_elf_link_find_version_dependencies
,
5859 if (elf_tdata (output_bfd
)->verref
== NULL
)
5860 s
->flags
|= SEC_EXCLUDE
;
5863 Elf_Internal_Verneed
*t
;
5868 /* Build the version definition section. */
5871 for (t
= elf_tdata (output_bfd
)->verref
;
5875 Elf_Internal_Vernaux
*a
;
5877 size
+= sizeof (Elf_External_Verneed
);
5879 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5880 size
+= sizeof (Elf_External_Vernaux
);
5884 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5885 if (s
->contents
== NULL
)
5889 for (t
= elf_tdata (output_bfd
)->verref
;
5894 Elf_Internal_Vernaux
*a
;
5898 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5901 t
->vn_version
= VER_NEED_CURRENT
;
5903 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5904 elf_dt_name (t
->vn_bfd
) != NULL
5905 ? elf_dt_name (t
->vn_bfd
)
5906 : lbasename (t
->vn_bfd
->filename
),
5908 if (indx
== (bfd_size_type
) -1)
5911 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5912 if (t
->vn_nextref
== NULL
)
5915 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5916 + caux
* sizeof (Elf_External_Vernaux
));
5918 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5919 (Elf_External_Verneed
*) p
);
5920 p
+= sizeof (Elf_External_Verneed
);
5922 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5924 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5925 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5926 a
->vna_nodename
, FALSE
);
5927 if (indx
== (bfd_size_type
) -1)
5930 if (a
->vna_nextptr
== NULL
)
5933 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5935 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5936 (Elf_External_Vernaux
*) p
);
5937 p
+= sizeof (Elf_External_Vernaux
);
5941 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5942 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5945 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5949 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5950 && elf_tdata (output_bfd
)->cverdefs
== 0)
5951 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5952 §ion_sym_count
) == 0)
5954 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5955 s
->flags
|= SEC_EXCLUDE
;
5961 /* Find the first non-excluded output section. We'll use its
5962 section symbol for some emitted relocs. */
5964 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
5968 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5969 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
5970 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5972 elf_hash_table (info
)->text_index_section
= s
;
5977 /* Find two non-excluded output sections, one for code, one for data.
5978 We'll use their section symbols for some emitted relocs. */
5980 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5984 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5985 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
5986 == (SEC_ALLOC
| SEC_READONLY
))
5987 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5989 elf_hash_table (info
)->text_index_section
= s
;
5993 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5994 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
5995 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5997 elf_hash_table (info
)->data_index_section
= s
;
6001 if (elf_hash_table (info
)->text_index_section
== NULL
)
6002 elf_hash_table (info
)->text_index_section
6003 = elf_hash_table (info
)->data_index_section
;
6007 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6009 const struct elf_backend_data
*bed
;
6011 if (!is_elf_hash_table (info
->hash
))
6014 bed
= get_elf_backend_data (output_bfd
);
6015 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6017 if (elf_hash_table (info
)->dynamic_sections_created
)
6021 bfd_size_type dynsymcount
;
6022 unsigned long section_sym_count
;
6023 unsigned int dtagcount
;
6025 dynobj
= elf_hash_table (info
)->dynobj
;
6027 /* Assign dynsym indicies. In a shared library we generate a
6028 section symbol for each output section, which come first.
6029 Next come all of the back-end allocated local dynamic syms,
6030 followed by the rest of the global symbols. */
6032 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6033 §ion_sym_count
);
6035 /* Work out the size of the symbol version section. */
6036 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6037 BFD_ASSERT (s
!= NULL
);
6038 if (dynsymcount
!= 0
6039 && (s
->flags
& SEC_EXCLUDE
) == 0)
6041 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6042 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6043 if (s
->contents
== NULL
)
6046 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6050 /* Set the size of the .dynsym and .hash sections. We counted
6051 the number of dynamic symbols in elf_link_add_object_symbols.
6052 We will build the contents of .dynsym and .hash when we build
6053 the final symbol table, because until then we do not know the
6054 correct value to give the symbols. We built the .dynstr
6055 section as we went along in elf_link_add_object_symbols. */
6056 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6057 BFD_ASSERT (s
!= NULL
);
6058 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6060 if (dynsymcount
!= 0)
6062 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6063 if (s
->contents
== NULL
)
6066 /* The first entry in .dynsym is a dummy symbol.
6067 Clear all the section syms, in case we don't output them all. */
6068 ++section_sym_count
;
6069 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6072 elf_hash_table (info
)->bucketcount
= 0;
6074 /* Compute the size of the hashing table. As a side effect this
6075 computes the hash values for all the names we export. */
6076 if (info
->emit_hash
)
6078 unsigned long int *hashcodes
;
6079 unsigned long int *hashcodesp
;
6081 unsigned long int nsyms
;
6083 size_t hash_entry_size
;
6085 /* Compute the hash values for all exported symbols. At the same
6086 time store the values in an array so that we could use them for
6088 amt
= dynsymcount
* sizeof (unsigned long int);
6089 hashcodes
= bfd_malloc (amt
);
6090 if (hashcodes
== NULL
)
6092 hashcodesp
= hashcodes
;
6094 /* Put all hash values in HASHCODES. */
6095 elf_link_hash_traverse (elf_hash_table (info
),
6096 elf_collect_hash_codes
, &hashcodesp
);
6098 nsyms
= hashcodesp
- hashcodes
;
6100 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6103 if (bucketcount
== 0)
6106 elf_hash_table (info
)->bucketcount
= bucketcount
;
6108 s
= bfd_get_section_by_name (dynobj
, ".hash");
6109 BFD_ASSERT (s
!= NULL
);
6110 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6111 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6112 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6113 if (s
->contents
== NULL
)
6116 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6117 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6118 s
->contents
+ hash_entry_size
);
6121 if (info
->emit_gnu_hash
)
6124 unsigned char *contents
;
6125 struct collect_gnu_hash_codes cinfo
;
6129 memset (&cinfo
, 0, sizeof (cinfo
));
6131 /* Compute the hash values for all exported symbols. At the same
6132 time store the values in an array so that we could use them for
6134 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6135 cinfo
.hashcodes
= bfd_malloc (amt
);
6136 if (cinfo
.hashcodes
== NULL
)
6139 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6140 cinfo
.min_dynindx
= -1;
6141 cinfo
.output_bfd
= output_bfd
;
6144 /* Put all hash values in HASHCODES. */
6145 elf_link_hash_traverse (elf_hash_table (info
),
6146 elf_collect_gnu_hash_codes
, &cinfo
);
6149 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6151 if (bucketcount
== 0)
6153 free (cinfo
.hashcodes
);
6157 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6158 BFD_ASSERT (s
!= NULL
);
6160 if (cinfo
.nsyms
== 0)
6162 /* Empty .gnu.hash section is special. */
6163 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6164 free (cinfo
.hashcodes
);
6165 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6166 contents
= bfd_zalloc (output_bfd
, s
->size
);
6167 if (contents
== NULL
)
6169 s
->contents
= contents
;
6170 /* 1 empty bucket. */
6171 bfd_put_32 (output_bfd
, 1, contents
);
6172 /* SYMIDX above the special symbol 0. */
6173 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6174 /* Just one word for bitmask. */
6175 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6176 /* Only hash fn bloom filter. */
6177 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6178 /* No hashes are valid - empty bitmask. */
6179 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6180 /* No hashes in the only bucket. */
6181 bfd_put_32 (output_bfd
, 0,
6182 contents
+ 16 + bed
->s
->arch_size
/ 8);
6186 unsigned long int maskwords
, maskbitslog2
;
6187 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6189 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6190 if (maskbitslog2
< 3)
6192 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6193 maskbitslog2
= maskbitslog2
+ 3;
6195 maskbitslog2
= maskbitslog2
+ 2;
6196 if (bed
->s
->arch_size
== 64)
6198 if (maskbitslog2
== 5)
6204 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6205 cinfo
.shift2
= maskbitslog2
;
6206 cinfo
.maskbits
= 1 << maskbitslog2
;
6207 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6208 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6209 amt
+= maskwords
* sizeof (bfd_vma
);
6210 cinfo
.bitmask
= bfd_malloc (amt
);
6211 if (cinfo
.bitmask
== NULL
)
6213 free (cinfo
.hashcodes
);
6217 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6218 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6219 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6220 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6222 /* Determine how often each hash bucket is used. */
6223 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6224 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6225 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6227 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6228 if (cinfo
.counts
[i
] != 0)
6230 cinfo
.indx
[i
] = cnt
;
6231 cnt
+= cinfo
.counts
[i
];
6233 BFD_ASSERT (cnt
== dynsymcount
);
6234 cinfo
.bucketcount
= bucketcount
;
6235 cinfo
.local_indx
= cinfo
.min_dynindx
;
6237 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6238 s
->size
+= cinfo
.maskbits
/ 8;
6239 contents
= bfd_zalloc (output_bfd
, s
->size
);
6240 if (contents
== NULL
)
6242 free (cinfo
.bitmask
);
6243 free (cinfo
.hashcodes
);
6247 s
->contents
= contents
;
6248 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6249 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6250 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6251 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6252 contents
+= 16 + cinfo
.maskbits
/ 8;
6254 for (i
= 0; i
< bucketcount
; ++i
)
6256 if (cinfo
.counts
[i
] == 0)
6257 bfd_put_32 (output_bfd
, 0, contents
);
6259 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6263 cinfo
.contents
= contents
;
6265 /* Renumber dynamic symbols, populate .gnu.hash section. */
6266 elf_link_hash_traverse (elf_hash_table (info
),
6267 elf_renumber_gnu_hash_syms
, &cinfo
);
6269 contents
= s
->contents
+ 16;
6270 for (i
= 0; i
< maskwords
; ++i
)
6272 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6274 contents
+= bed
->s
->arch_size
/ 8;
6277 free (cinfo
.bitmask
);
6278 free (cinfo
.hashcodes
);
6282 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6283 BFD_ASSERT (s
!= NULL
);
6285 elf_finalize_dynstr (output_bfd
, info
);
6287 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6289 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6290 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6297 /* Final phase of ELF linker. */
6299 /* A structure we use to avoid passing large numbers of arguments. */
6301 struct elf_final_link_info
6303 /* General link information. */
6304 struct bfd_link_info
*info
;
6307 /* Symbol string table. */
6308 struct bfd_strtab_hash
*symstrtab
;
6309 /* .dynsym section. */
6310 asection
*dynsym_sec
;
6311 /* .hash section. */
6313 /* symbol version section (.gnu.version). */
6314 asection
*symver_sec
;
6315 /* Buffer large enough to hold contents of any section. */
6317 /* Buffer large enough to hold external relocs of any section. */
6318 void *external_relocs
;
6319 /* Buffer large enough to hold internal relocs of any section. */
6320 Elf_Internal_Rela
*internal_relocs
;
6321 /* Buffer large enough to hold external local symbols of any input
6323 bfd_byte
*external_syms
;
6324 /* And a buffer for symbol section indices. */
6325 Elf_External_Sym_Shndx
*locsym_shndx
;
6326 /* Buffer large enough to hold internal local symbols of any input
6328 Elf_Internal_Sym
*internal_syms
;
6329 /* Array large enough to hold a symbol index for each local symbol
6330 of any input BFD. */
6332 /* Array large enough to hold a section pointer for each local
6333 symbol of any input BFD. */
6334 asection
**sections
;
6335 /* Buffer to hold swapped out symbols. */
6337 /* And one for symbol section indices. */
6338 Elf_External_Sym_Shndx
*symshndxbuf
;
6339 /* Number of swapped out symbols in buffer. */
6340 size_t symbuf_count
;
6341 /* Number of symbols which fit in symbuf. */
6343 /* And same for symshndxbuf. */
6344 size_t shndxbuf_size
;
6347 /* This struct is used to pass information to elf_link_output_extsym. */
6349 struct elf_outext_info
6352 bfd_boolean localsyms
;
6353 struct elf_final_link_info
*finfo
;
6357 /* Support for evaluating a complex relocation.
6359 Complex relocations are generalized, self-describing relocations. The
6360 implementation of them consists of two parts: complex symbols, and the
6361 relocations themselves.
6363 The relocations are use a reserved elf-wide relocation type code (R_RELC
6364 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6365 information (start bit, end bit, word width, etc) into the addend. This
6366 information is extracted from CGEN-generated operand tables within gas.
6368 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6369 internal) representing prefix-notation expressions, including but not
6370 limited to those sorts of expressions normally encoded as addends in the
6371 addend field. The symbol mangling format is:
6374 | <unary-operator> ':' <node>
6375 | <binary-operator> ':' <node> ':' <node>
6378 <literal> := 's' <digits=N> ':' <N character symbol name>
6379 | 'S' <digits=N> ':' <N character section name>
6383 <binary-operator> := as in C
6384 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6387 set_symbol_value (bfd
* bfd_with_globals
,
6388 struct elf_final_link_info
* finfo
,
6392 bfd_boolean is_local
;
6393 Elf_Internal_Sym
* sym
;
6394 struct elf_link_hash_entry
** sym_hashes
;
6395 struct elf_link_hash_entry
* h
;
6397 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
6398 sym
= finfo
->internal_syms
+ symidx
;
6399 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
6403 /* It is a local symbol: move it to the
6404 "absolute" section and give it a value. */
6405 sym
->st_shndx
= SHN_ABS
;
6406 sym
->st_value
= val
;
6410 /* It is a global symbol: set its link type
6411 to "defined" and give it a value. */
6412 h
= sym_hashes
[symidx
];
6413 while (h
->root
.type
== bfd_link_hash_indirect
6414 || h
->root
.type
== bfd_link_hash_warning
)
6415 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6416 h
->root
.type
= bfd_link_hash_defined
;
6417 h
->root
.u
.def
.value
= val
;
6418 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6423 resolve_symbol (const char * name
,
6425 struct elf_final_link_info
* finfo
,
6429 Elf_Internal_Sym
* sym
;
6430 struct bfd_link_hash_entry
* global_entry
;
6431 const char * candidate
= NULL
;
6432 Elf_Internal_Shdr
* symtab_hdr
;
6433 asection
* sec
= NULL
;
6436 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6438 for (i
= 0; i
< locsymcount
; ++ i
)
6440 sym
= finfo
->internal_syms
+ i
;
6441 sec
= finfo
->sections
[i
];
6443 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
6446 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
6447 symtab_hdr
->sh_link
,
6450 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6451 name
, candidate
, (unsigned int)sym
->st_value
);
6453 if (candidate
&& strcmp (candidate
, name
) == 0)
6455 * result
= sym
->st_value
;
6457 if (sym
->st_shndx
> SHN_UNDEF
&&
6458 sym
->st_shndx
< SHN_LORESERVE
)
6461 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6462 sec
->output_section
->name
,
6463 (unsigned int)sec
->output_section
->vma
,
6464 (unsigned int)sec
->output_offset
);
6466 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
6469 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
6475 /* Hmm, haven't found it yet. perhaps it is a global. */
6476 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
6480 if (global_entry
->type
== bfd_link_hash_defined
6481 || global_entry
->type
== bfd_link_hash_defweak
)
6483 * result
= global_entry
->u
.def
.value
6484 + global_entry
->u
.def
.section
->output_section
->vma
6485 + global_entry
->u
.def
.section
->output_offset
;
6487 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6488 global_entry
->root
.string
, (unsigned int)*result
);
6493 if (global_entry
->type
== bfd_link_hash_common
)
6495 *result
= global_entry
->u
.def
.value
+
6496 bfd_com_section_ptr
->output_section
->vma
+
6497 bfd_com_section_ptr
->output_offset
;
6499 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6500 global_entry
->root
.string
, (unsigned int)*result
);
6509 resolve_section (const char * name
,
6510 asection
* sections
,
6516 for (curr
= sections
; curr
; curr
= curr
->next
)
6517 if (strcmp (curr
->name
, name
) == 0)
6519 *result
= curr
->vma
;
6523 /* Hmm. still haven't found it. try pseudo-section names. */
6524 for (curr
= sections
; curr
; curr
= curr
->next
)
6526 len
= strlen (curr
->name
);
6527 if (len
> strlen (name
))
6530 if (strncmp (curr
->name
, name
, len
) == 0)
6532 if (strncmp (".end", name
+ len
, 4) == 0)
6534 *result
= curr
->vma
+ curr
->size
;
6538 /* Insert more pseudo-section names here, if you like. */
6546 undefined_reference (const char * reftype
,
6549 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
6553 eval_symbol (bfd_vma
* result
,
6557 struct elf_final_link_info
* finfo
,
6559 bfd_vma section_offset
,
6567 const int bufsz
= 4096;
6568 char symbuf
[bufsz
];
6569 const char * symend
;
6570 bfd_boolean symbol_is_section
= FALSE
;
6575 if (len
< 1 || len
> bufsz
)
6577 bfd_set_error (bfd_error_invalid_operation
);
6584 * result
= addr
+ section_offset
;
6585 * advanced
= sym
+ 1;
6590 * result
= strtoul (sym
, advanced
, 16);
6594 symbol_is_section
= TRUE
;
6597 symlen
= strtol (sym
, &sym
, 10);
6598 ++ sym
; /* Skip the trailing ':'. */
6600 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
6602 bfd_set_error (bfd_error_invalid_operation
);
6606 memcpy (symbuf
, sym
, symlen
);
6607 symbuf
[symlen
] = '\0';
6608 * advanced
= sym
+ symlen
;
6610 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6611 the symbol as a section, or vice-versa. so we're pretty liberal in our
6612 interpretation here; section means "try section first", not "must be a
6613 section", and likewise with symbol. */
6615 if (symbol_is_section
)
6617 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
6618 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
6620 undefined_reference ("section", symbuf
);
6626 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
6627 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
6630 undefined_reference ("symbol", symbuf
);
6637 /* All that remains are operators. */
6639 #define UNARY_OP(op) \
6640 if (strncmp (sym, #op, strlen (#op)) == 0) \
6642 sym += strlen (#op); \
6645 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6646 section_offset, locsymcount, signed_p) \
6650 * result = op ((signed)a); \
6657 #define BINARY_OP(op) \
6658 if (strncmp (sym, #op, strlen (#op)) == 0) \
6660 sym += strlen (#op); \
6663 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6664 section_offset, locsymcount, signed_p) \
6668 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6669 section_offset, locsymcount, signed_p) \
6673 * result = ((signed) a) op ((signed) b); \
6675 * result = a op b; \
6704 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
6705 bfd_set_error (bfd_error_invalid_operation
);
6710 /* Entry point to evaluator, called from elf_link_input_bfd. */
6713 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
6714 struct elf_final_link_info
* finfo
,
6717 const struct elf_backend_data
* bed
;
6718 Elf_Internal_Shdr
* symtab_hdr
;
6719 struct elf_link_hash_entry
** sym_hashes
;
6720 asection
* reloc_sec
;
6721 bfd_boolean result
= TRUE
;
6723 /* For each section, we're going to check and see if it has any
6724 complex relocations, and we're going to evaluate any of them
6727 if (finfo
->info
->relocatable
)
6730 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6731 sym_hashes
= elf_sym_hashes (input_bfd
);
6732 bed
= get_elf_backend_data (input_bfd
);
6734 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
6736 Elf_Internal_Rela
* internal_relocs
;
6739 /* This section was omitted from the link. */
6740 if (! reloc_sec
->linker_mark
)
6743 /* Only process sections containing relocs. */
6744 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
6747 if (reloc_sec
->reloc_count
== 0)
6750 /* Read in the relocs for this section. */
6752 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
6753 (Elf_Internal_Rela
*) NULL
,
6755 if (internal_relocs
== NULL
)
6758 for (i
= reloc_sec
->reloc_count
; i
--;)
6760 Elf_Internal_Rela
* rel
;
6763 Elf_Internal_Sym
* sym
;
6765 bfd_vma section_offset
;
6769 rel
= internal_relocs
+ i
;
6770 section_offset
= reloc_sec
->output_section
->vma
6771 + reloc_sec
->output_offset
;
6772 addr
= rel
->r_offset
;
6774 index
= ELF32_R_SYM (rel
->r_info
);
6775 if (bed
->s
->arch_size
== 64)
6778 if (index
== STN_UNDEF
)
6781 if (index
< locsymcount
)
6783 /* The symbol is local. */
6784 sym
= finfo
->internal_syms
+ index
;
6786 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6787 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
6788 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
6791 sym_name
= bfd_elf_string_from_elf_section
6792 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
6794 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
6798 /* The symbol is global. */
6799 struct elf_link_hash_entry
* h
;
6801 if (elf_bad_symtab (input_bfd
))
6804 h
= sym_hashes
[index
- locsymcount
];
6805 while ( h
->root
.type
== bfd_link_hash_indirect
6806 || h
->root
.type
== bfd_link_hash_warning
)
6807 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6809 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
6812 signed_p
= (h
->type
== STT_SRELC
);
6813 sym_name
= (char *) h
->root
.root
.string
;
6816 printf ("Encountered a complex symbol!");
6817 printf (" (input_bfd %s, section %s, reloc %ld\n",
6818 input_bfd
->filename
, reloc_sec
->name
, i
);
6819 printf (" symbol: idx %8.8lx, name %s\n",
6821 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6823 printf (" Evaluating '%s' ...\n ", sym_name
);
6825 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
6826 finfo
, addr
, section_offset
, locsymcount
,
6828 /* Symbol evaluated OK. Update to absolute value. */
6829 set_symbol_value (input_bfd
, finfo
, index
, result
);
6835 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
6836 free (internal_relocs
);
6839 /* If nothing went wrong, then we adjusted
6840 everything we wanted to adjust. */
6845 put_value (bfd_vma size
,
6846 unsigned long chunksz
,
6849 bfd_byte
* location
)
6851 location
+= (size
- chunksz
);
6853 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
6861 bfd_put_8 (input_bfd
, x
, location
);
6864 bfd_put_16 (input_bfd
, x
, location
);
6867 bfd_put_32 (input_bfd
, x
, location
);
6871 bfd_put_64 (input_bfd
, x
, location
);
6881 get_value (bfd_vma size
,
6882 unsigned long chunksz
,
6884 bfd_byte
* location
)
6888 for (; size
; size
-= chunksz
, location
+= chunksz
)
6896 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
6899 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
6902 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
6906 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
6917 decode_complex_addend
6918 (unsigned long * start
, /* in bits */
6919 unsigned long * oplen
, /* in bits */
6920 unsigned long * len
, /* in bits */
6921 unsigned long * wordsz
, /* in bytes */
6922 unsigned long * chunksz
, /* in bytes */
6923 unsigned long * lsb0_p
,
6924 unsigned long * signed_p
,
6925 unsigned long * trunc_p
,
6926 unsigned long encoded
)
6928 * start
= encoded
& 0x3F;
6929 * len
= (encoded
>> 6) & 0x3F;
6930 * oplen
= (encoded
>> 12) & 0x3F;
6931 * wordsz
= (encoded
>> 18) & 0xF;
6932 * chunksz
= (encoded
>> 22) & 0xF;
6933 * lsb0_p
= (encoded
>> 27) & 1;
6934 * signed_p
= (encoded
>> 28) & 1;
6935 * trunc_p
= (encoded
>> 29) & 1;
6939 bfd_elf_perform_complex_relocation
6940 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
6941 struct bfd_link_info
* info
,
6943 asection
* input_section
,
6944 bfd_byte
* contents
,
6945 Elf_Internal_Rela
* rel
,
6946 Elf_Internal_Sym
* local_syms
,
6947 asection
** local_sections
)
6949 const struct elf_backend_data
* bed
;
6950 Elf_Internal_Shdr
* symtab_hdr
;
6952 bfd_vma relocation
= 0, shift
, x
;
6955 unsigned long start
, oplen
, len
, wordsz
,
6956 chunksz
, lsb0_p
, signed_p
, trunc_p
;
6958 /* Perform this reloc, since it is complex.
6959 (this is not to say that it necessarily refers to a complex
6960 symbol; merely that it is a self-describing CGEN based reloc.
6961 i.e. the addend has the complete reloc information (bit start, end,
6962 word size, etc) encoded within it.). */
6963 r_symndx
= ELF32_R_SYM (rel
->r_info
);
6964 bed
= get_elf_backend_data (input_bfd
);
6965 if (bed
->s
->arch_size
== 64)
6969 printf ("Performing complex relocation %ld...\n", r_symndx
);
6972 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6973 if (r_symndx
< symtab_hdr
->sh_info
)
6975 /* The symbol is local. */
6976 Elf_Internal_Sym
* sym
;
6978 sym
= local_syms
+ r_symndx
;
6979 sec
= local_sections
[r_symndx
];
6980 relocation
= sym
->st_value
;
6981 if (sym
->st_shndx
> SHN_UNDEF
&&
6982 sym
->st_shndx
< SHN_LORESERVE
)
6983 relocation
+= (sec
->output_offset
+
6984 sec
->output_section
->vma
);
6988 /* The symbol is global. */
6989 struct elf_link_hash_entry
**sym_hashes
;
6990 struct elf_link_hash_entry
* h
;
6992 sym_hashes
= elf_sym_hashes (input_bfd
);
6993 h
= sym_hashes
[r_symndx
];
6995 while (h
->root
.type
== bfd_link_hash_indirect
6996 || h
->root
.type
== bfd_link_hash_warning
)
6997 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6999 if (h
->root
.type
== bfd_link_hash_defined
7000 || h
->root
.type
== bfd_link_hash_defweak
)
7002 sec
= h
->root
.u
.def
.section
;
7003 relocation
= h
->root
.u
.def
.value
;
7005 if (! bfd_is_abs_section (sec
))
7006 relocation
+= (sec
->output_section
->vma
7007 + sec
->output_offset
);
7009 if (h
->root
.type
== bfd_link_hash_undefined
7010 && !((*info
->callbacks
->undefined_symbol
)
7011 (info
, h
->root
.root
.string
, input_bfd
,
7012 input_section
, rel
->r_offset
,
7013 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7014 || ELF_ST_VISIBILITY (h
->other
))))
7018 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7019 & chunksz
, & lsb0_p
, & signed_p
,
7020 & trunc_p
, rel
->r_addend
);
7022 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7025 shift
= (start
+ 1) - len
;
7027 shift
= (8 * wordsz
) - (start
+ len
);
7029 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7032 printf ("Doing complex reloc: "
7033 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7034 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7035 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7036 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7037 oplen
, x
, mask
, relocation
);
7042 /* Now do an overflow check. */
7043 if (bfd_check_overflow ((signed_p
?
7044 complain_overflow_signed
:
7045 complain_overflow_unsigned
),
7046 len
, 0, (8 * wordsz
),
7047 relocation
) == bfd_reloc_overflow
)
7048 (*_bfd_error_handler
)
7049 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7051 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7052 relocation
, (signed_p
? "(signed) " : ""), mask
);
7056 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7059 printf (" relocation: %8.8lx\n"
7060 " shifted mask: %8.8lx\n"
7061 " shifted/masked reloc: %8.8lx\n"
7062 " result: %8.8lx\n",
7063 relocation
, (mask
<< shift
),
7064 ((relocation
& mask
) << shift
), x
);
7066 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7069 /* When performing a relocatable link, the input relocations are
7070 preserved. But, if they reference global symbols, the indices
7071 referenced must be updated. Update all the relocations in
7072 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7075 elf_link_adjust_relocs (bfd
*abfd
,
7076 Elf_Internal_Shdr
*rel_hdr
,
7078 struct elf_link_hash_entry
**rel_hash
)
7081 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7083 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7084 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7085 bfd_vma r_type_mask
;
7088 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7090 swap_in
= bed
->s
->swap_reloc_in
;
7091 swap_out
= bed
->s
->swap_reloc_out
;
7093 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7095 swap_in
= bed
->s
->swap_reloca_in
;
7096 swap_out
= bed
->s
->swap_reloca_out
;
7101 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7104 if (bed
->s
->arch_size
== 32)
7111 r_type_mask
= 0xffffffff;
7115 erela
= rel_hdr
->contents
;
7116 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7118 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7121 if (*rel_hash
== NULL
)
7124 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7126 (*swap_in
) (abfd
, erela
, irela
);
7127 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7128 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7129 | (irela
[j
].r_info
& r_type_mask
));
7130 (*swap_out
) (abfd
, irela
, erela
);
7134 struct elf_link_sort_rela
7140 enum elf_reloc_type_class type
;
7141 /* We use this as an array of size int_rels_per_ext_rel. */
7142 Elf_Internal_Rela rela
[1];
7146 elf_link_sort_cmp1 (const void *A
, const void *B
)
7148 const struct elf_link_sort_rela
*a
= A
;
7149 const struct elf_link_sort_rela
*b
= B
;
7150 int relativea
, relativeb
;
7152 relativea
= a
->type
== reloc_class_relative
;
7153 relativeb
= b
->type
== reloc_class_relative
;
7155 if (relativea
< relativeb
)
7157 if (relativea
> relativeb
)
7159 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7161 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7163 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7165 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7171 elf_link_sort_cmp2 (const void *A
, const void *B
)
7173 const struct elf_link_sort_rela
*a
= A
;
7174 const struct elf_link_sort_rela
*b
= B
;
7177 if (a
->u
.offset
< b
->u
.offset
)
7179 if (a
->u
.offset
> b
->u
.offset
)
7181 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7182 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7187 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7189 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7195 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7198 bfd_size_type count
, size
;
7199 size_t i
, ret
, sort_elt
, ext_size
;
7200 bfd_byte
*sort
, *s_non_relative
, *p
;
7201 struct elf_link_sort_rela
*sq
;
7202 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7203 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7204 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7205 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7206 struct bfd_link_order
*lo
;
7209 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7210 if (reldyn
== NULL
|| reldyn
->size
== 0)
7212 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7213 if (reldyn
== NULL
|| reldyn
->size
== 0)
7215 ext_size
= bed
->s
->sizeof_rel
;
7216 swap_in
= bed
->s
->swap_reloc_in
;
7217 swap_out
= bed
->s
->swap_reloc_out
;
7221 ext_size
= bed
->s
->sizeof_rela
;
7222 swap_in
= bed
->s
->swap_reloca_in
;
7223 swap_out
= bed
->s
->swap_reloca_out
;
7225 count
= reldyn
->size
/ ext_size
;
7228 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7229 if (lo
->type
== bfd_indirect_link_order
)
7231 asection
*o
= lo
->u
.indirect
.section
;
7235 if (size
!= reldyn
->size
)
7238 sort_elt
= (sizeof (struct elf_link_sort_rela
)
7239 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
7240 sort
= bfd_zmalloc (sort_elt
* count
);
7243 (*info
->callbacks
->warning
)
7244 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
7248 if (bed
->s
->arch_size
== 32)
7249 r_sym_mask
= ~(bfd_vma
) 0xff;
7251 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
7253 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7254 if (lo
->type
== bfd_indirect_link_order
)
7256 bfd_byte
*erel
, *erelend
;
7257 asection
*o
= lo
->u
.indirect
.section
;
7259 if (o
->contents
== NULL
&& o
->size
!= 0)
7261 /* This is a reloc section that is being handled as a normal
7262 section. See bfd_section_from_shdr. We can't combine
7263 relocs in this case. */
7268 erelend
= o
->contents
+ o
->size
;
7269 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7270 while (erel
< erelend
)
7272 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7273 (*swap_in
) (abfd
, erel
, s
->rela
);
7274 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
7275 s
->u
.sym_mask
= r_sym_mask
;
7281 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
7283 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
7285 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7286 if (s
->type
!= reloc_class_relative
)
7292 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
7293 for (; i
< count
; i
++, p
+= sort_elt
)
7295 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
7296 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
7298 sp
->u
.offset
= sq
->rela
->r_offset
;
7301 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
7303 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7304 if (lo
->type
== bfd_indirect_link_order
)
7306 bfd_byte
*erel
, *erelend
;
7307 asection
*o
= lo
->u
.indirect
.section
;
7310 erelend
= o
->contents
+ o
->size
;
7311 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7312 while (erel
< erelend
)
7314 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7315 (*swap_out
) (abfd
, s
->rela
, erel
);
7326 /* Flush the output symbols to the file. */
7329 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
7330 const struct elf_backend_data
*bed
)
7332 if (finfo
->symbuf_count
> 0)
7334 Elf_Internal_Shdr
*hdr
;
7338 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
7339 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
7340 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7341 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
7342 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
7345 hdr
->sh_size
+= amt
;
7346 finfo
->symbuf_count
= 0;
7352 /* Add a symbol to the output symbol table. */
7355 elf_link_output_sym (struct elf_final_link_info
*finfo
,
7357 Elf_Internal_Sym
*elfsym
,
7358 asection
*input_sec
,
7359 struct elf_link_hash_entry
*h
)
7362 Elf_External_Sym_Shndx
*destshndx
;
7363 bfd_boolean (*output_symbol_hook
)
7364 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
7365 struct elf_link_hash_entry
*);
7366 const struct elf_backend_data
*bed
;
7368 bed
= get_elf_backend_data (finfo
->output_bfd
);
7369 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
7370 if (output_symbol_hook
!= NULL
)
7372 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
7376 if (name
== NULL
|| *name
== '\0')
7377 elfsym
->st_name
= 0;
7378 else if (input_sec
->flags
& SEC_EXCLUDE
)
7379 elfsym
->st_name
= 0;
7382 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
7384 if (elfsym
->st_name
== (unsigned long) -1)
7388 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
7390 if (! elf_link_flush_output_syms (finfo
, bed
))
7394 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7395 destshndx
= finfo
->symshndxbuf
;
7396 if (destshndx
!= NULL
)
7398 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
7402 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
7403 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
7404 if (destshndx
== NULL
)
7406 memset ((char *) destshndx
+ amt
, 0, amt
);
7407 finfo
->shndxbuf_size
*= 2;
7409 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
7412 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
7413 finfo
->symbuf_count
+= 1;
7414 bfd_get_symcount (finfo
->output_bfd
) += 1;
7419 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7422 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
7424 if (sym
->st_shndx
> SHN_HIRESERVE
)
7426 /* The gABI doesn't support dynamic symbols in output sections
7428 (*_bfd_error_handler
)
7429 (_("%B: Too many sections: %d (>= %d)"),
7430 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
7431 bfd_set_error (bfd_error_nonrepresentable_section
);
7437 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7438 allowing an unsatisfied unversioned symbol in the DSO to match a
7439 versioned symbol that would normally require an explicit version.
7440 We also handle the case that a DSO references a hidden symbol
7441 which may be satisfied by a versioned symbol in another DSO. */
7444 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
7445 const struct elf_backend_data
*bed
,
7446 struct elf_link_hash_entry
*h
)
7449 struct elf_link_loaded_list
*loaded
;
7451 if (!is_elf_hash_table (info
->hash
))
7454 switch (h
->root
.type
)
7460 case bfd_link_hash_undefined
:
7461 case bfd_link_hash_undefweak
:
7462 abfd
= h
->root
.u
.undef
.abfd
;
7463 if ((abfd
->flags
& DYNAMIC
) == 0
7464 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
7468 case bfd_link_hash_defined
:
7469 case bfd_link_hash_defweak
:
7470 abfd
= h
->root
.u
.def
.section
->owner
;
7473 case bfd_link_hash_common
:
7474 abfd
= h
->root
.u
.c
.p
->section
->owner
;
7477 BFD_ASSERT (abfd
!= NULL
);
7479 for (loaded
= elf_hash_table (info
)->loaded
;
7481 loaded
= loaded
->next
)
7484 Elf_Internal_Shdr
*hdr
;
7485 bfd_size_type symcount
;
7486 bfd_size_type extsymcount
;
7487 bfd_size_type extsymoff
;
7488 Elf_Internal_Shdr
*versymhdr
;
7489 Elf_Internal_Sym
*isym
;
7490 Elf_Internal_Sym
*isymend
;
7491 Elf_Internal_Sym
*isymbuf
;
7492 Elf_External_Versym
*ever
;
7493 Elf_External_Versym
*extversym
;
7495 input
= loaded
->abfd
;
7497 /* We check each DSO for a possible hidden versioned definition. */
7499 || (input
->flags
& DYNAMIC
) == 0
7500 || elf_dynversym (input
) == 0)
7503 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
7505 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7506 if (elf_bad_symtab (input
))
7508 extsymcount
= symcount
;
7513 extsymcount
= symcount
- hdr
->sh_info
;
7514 extsymoff
= hdr
->sh_info
;
7517 if (extsymcount
== 0)
7520 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
7522 if (isymbuf
== NULL
)
7525 /* Read in any version definitions. */
7526 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
7527 extversym
= bfd_malloc (versymhdr
->sh_size
);
7528 if (extversym
== NULL
)
7531 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
7532 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
7533 != versymhdr
->sh_size
))
7541 ever
= extversym
+ extsymoff
;
7542 isymend
= isymbuf
+ extsymcount
;
7543 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
7546 Elf_Internal_Versym iver
;
7547 unsigned short version_index
;
7549 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
7550 || isym
->st_shndx
== SHN_UNDEF
)
7553 name
= bfd_elf_string_from_elf_section (input
,
7556 if (strcmp (name
, h
->root
.root
.string
) != 0)
7559 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
7561 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
7563 /* If we have a non-hidden versioned sym, then it should
7564 have provided a definition for the undefined sym. */
7568 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
7569 if (version_index
== 1 || version_index
== 2)
7571 /* This is the base or first version. We can use it. */
7585 /* Add an external symbol to the symbol table. This is called from
7586 the hash table traversal routine. When generating a shared object,
7587 we go through the symbol table twice. The first time we output
7588 anything that might have been forced to local scope in a version
7589 script. The second time we output the symbols that are still
7593 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
7595 struct elf_outext_info
*eoinfo
= data
;
7596 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
7598 Elf_Internal_Sym sym
;
7599 asection
*input_sec
;
7600 const struct elf_backend_data
*bed
;
7602 if (h
->root
.type
== bfd_link_hash_warning
)
7604 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7605 if (h
->root
.type
== bfd_link_hash_new
)
7609 /* Decide whether to output this symbol in this pass. */
7610 if (eoinfo
->localsyms
)
7612 if (!h
->forced_local
)
7617 if (h
->forced_local
)
7621 bed
= get_elf_backend_data (finfo
->output_bfd
);
7623 if (h
->root
.type
== bfd_link_hash_undefined
)
7625 /* If we have an undefined symbol reference here then it must have
7626 come from a shared library that is being linked in. (Undefined
7627 references in regular files have already been handled). */
7628 bfd_boolean ignore_undef
= FALSE
;
7630 /* Some symbols may be special in that the fact that they're
7631 undefined can be safely ignored - let backend determine that. */
7632 if (bed
->elf_backend_ignore_undef_symbol
)
7633 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
7635 /* If we are reporting errors for this situation then do so now. */
7636 if (ignore_undef
== FALSE
7639 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
7640 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
7642 if (! (finfo
->info
->callbacks
->undefined_symbol
7643 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
7644 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
7646 eoinfo
->failed
= TRUE
;
7652 /* We should also warn if a forced local symbol is referenced from
7653 shared libraries. */
7654 if (! finfo
->info
->relocatable
7655 && (! finfo
->info
->shared
)
7660 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
7662 (*_bfd_error_handler
)
7663 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7665 h
->root
.u
.def
.section
== bfd_abs_section_ptr
7666 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
7667 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
7669 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
7670 ? "hidden" : "local",
7671 h
->root
.root
.string
);
7672 eoinfo
->failed
= TRUE
;
7676 /* We don't want to output symbols that have never been mentioned by
7677 a regular file, or that we have been told to strip. However, if
7678 h->indx is set to -2, the symbol is used by a reloc and we must
7682 else if ((h
->def_dynamic
7684 || h
->root
.type
== bfd_link_hash_new
)
7688 else if (finfo
->info
->strip
== strip_all
)
7690 else if (finfo
->info
->strip
== strip_some
7691 && bfd_hash_lookup (finfo
->info
->keep_hash
,
7692 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
7694 else if (finfo
->info
->strip_discarded
7695 && (h
->root
.type
== bfd_link_hash_defined
7696 || h
->root
.type
== bfd_link_hash_defweak
)
7697 && elf_discarded_section (h
->root
.u
.def
.section
))
7702 /* If we're stripping it, and it's not a dynamic symbol, there's
7703 nothing else to do unless it is a forced local symbol. */
7706 && !h
->forced_local
)
7710 sym
.st_size
= h
->size
;
7711 sym
.st_other
= h
->other
;
7712 if (h
->forced_local
)
7713 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
7714 else if (h
->root
.type
== bfd_link_hash_undefweak
7715 || h
->root
.type
== bfd_link_hash_defweak
)
7716 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
7718 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
7720 switch (h
->root
.type
)
7723 case bfd_link_hash_new
:
7724 case bfd_link_hash_warning
:
7728 case bfd_link_hash_undefined
:
7729 case bfd_link_hash_undefweak
:
7730 input_sec
= bfd_und_section_ptr
;
7731 sym
.st_shndx
= SHN_UNDEF
;
7734 case bfd_link_hash_defined
:
7735 case bfd_link_hash_defweak
:
7737 input_sec
= h
->root
.u
.def
.section
;
7738 if (input_sec
->output_section
!= NULL
)
7741 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7742 input_sec
->output_section
);
7743 if (sym
.st_shndx
== SHN_BAD
)
7745 (*_bfd_error_handler
)
7746 (_("%B: could not find output section %A for input section %A"),
7747 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7748 eoinfo
->failed
= TRUE
;
7752 /* ELF symbols in relocatable files are section relative,
7753 but in nonrelocatable files they are virtual
7755 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7756 if (! finfo
->info
->relocatable
)
7758 sym
.st_value
+= input_sec
->output_section
->vma
;
7759 if (h
->type
== STT_TLS
)
7761 /* STT_TLS symbols are relative to PT_TLS segment
7763 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7764 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7770 BFD_ASSERT (input_sec
->owner
== NULL
7771 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7772 sym
.st_shndx
= SHN_UNDEF
;
7773 input_sec
= bfd_und_section_ptr
;
7778 case bfd_link_hash_common
:
7779 input_sec
= h
->root
.u
.c
.p
->section
;
7780 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7781 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7784 case bfd_link_hash_indirect
:
7785 /* These symbols are created by symbol versioning. They point
7786 to the decorated version of the name. For example, if the
7787 symbol foo@@GNU_1.2 is the default, which should be used when
7788 foo is used with no version, then we add an indirect symbol
7789 foo which points to foo@@GNU_1.2. We ignore these symbols,
7790 since the indirected symbol is already in the hash table. */
7794 /* Give the processor backend a chance to tweak the symbol value,
7795 and also to finish up anything that needs to be done for this
7796 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7797 forced local syms when non-shared is due to a historical quirk. */
7798 if ((h
->dynindx
!= -1
7800 && ((finfo
->info
->shared
7801 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7802 || h
->root
.type
!= bfd_link_hash_undefweak
))
7803 || !h
->forced_local
)
7804 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7806 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7807 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7809 eoinfo
->failed
= TRUE
;
7814 /* If we are marking the symbol as undefined, and there are no
7815 non-weak references to this symbol from a regular object, then
7816 mark the symbol as weak undefined; if there are non-weak
7817 references, mark the symbol as strong. We can't do this earlier,
7818 because it might not be marked as undefined until the
7819 finish_dynamic_symbol routine gets through with it. */
7820 if (sym
.st_shndx
== SHN_UNDEF
7822 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7823 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7827 if (h
->ref_regular_nonweak
)
7828 bindtype
= STB_GLOBAL
;
7830 bindtype
= STB_WEAK
;
7831 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7834 /* If a non-weak symbol with non-default visibility is not defined
7835 locally, it is a fatal error. */
7836 if (! finfo
->info
->relocatable
7837 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7838 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7839 && h
->root
.type
== bfd_link_hash_undefined
7842 (*_bfd_error_handler
)
7843 (_("%B: %s symbol `%s' isn't defined"),
7845 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7847 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7848 ? "internal" : "hidden",
7849 h
->root
.root
.string
);
7850 eoinfo
->failed
= TRUE
;
7854 /* If this symbol should be put in the .dynsym section, then put it
7855 there now. We already know the symbol index. We also fill in
7856 the entry in the .hash section. */
7857 if (h
->dynindx
!= -1
7858 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7862 sym
.st_name
= h
->dynstr_index
;
7863 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7864 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7866 eoinfo
->failed
= TRUE
;
7869 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7871 if (finfo
->hash_sec
!= NULL
)
7873 size_t hash_entry_size
;
7874 bfd_byte
*bucketpos
;
7879 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7880 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7883 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7884 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7885 + (bucket
+ 2) * hash_entry_size
);
7886 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7887 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7888 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7889 ((bfd_byte
*) finfo
->hash_sec
->contents
7890 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7893 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7895 Elf_Internal_Versym iversym
;
7896 Elf_External_Versym
*eversym
;
7898 if (!h
->def_regular
)
7900 if (h
->verinfo
.verdef
== NULL
)
7901 iversym
.vs_vers
= 0;
7903 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7907 if (h
->verinfo
.vertree
== NULL
)
7908 iversym
.vs_vers
= 1;
7910 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7911 if (finfo
->info
->create_default_symver
)
7916 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7918 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7919 eversym
+= h
->dynindx
;
7920 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7924 /* If we're stripping it, then it was just a dynamic symbol, and
7925 there's nothing else to do. */
7926 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7929 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7931 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7933 eoinfo
->failed
= TRUE
;
7940 /* Return TRUE if special handling is done for relocs in SEC against
7941 symbols defined in discarded sections. */
7944 elf_section_ignore_discarded_relocs (asection
*sec
)
7946 const struct elf_backend_data
*bed
;
7948 switch (sec
->sec_info_type
)
7950 case ELF_INFO_TYPE_STABS
:
7951 case ELF_INFO_TYPE_EH_FRAME
:
7957 bed
= get_elf_backend_data (sec
->owner
);
7958 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7959 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7965 /* Return a mask saying how ld should treat relocations in SEC against
7966 symbols defined in discarded sections. If this function returns
7967 COMPLAIN set, ld will issue a warning message. If this function
7968 returns PRETEND set, and the discarded section was link-once and the
7969 same size as the kept link-once section, ld will pretend that the
7970 symbol was actually defined in the kept section. Otherwise ld will
7971 zero the reloc (at least that is the intent, but some cooperation by
7972 the target dependent code is needed, particularly for REL targets). */
7975 _bfd_elf_default_action_discarded (asection
*sec
)
7977 if (sec
->flags
& SEC_DEBUGGING
)
7980 if (strcmp (".eh_frame", sec
->name
) == 0)
7983 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7986 return COMPLAIN
| PRETEND
;
7989 /* Find a match between a section and a member of a section group. */
7992 match_group_member (asection
*sec
, asection
*group
,
7993 struct bfd_link_info
*info
)
7995 asection
*first
= elf_next_in_group (group
);
7996 asection
*s
= first
;
8000 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8003 s
= elf_next_in_group (s
);
8011 /* Check if the kept section of a discarded section SEC can be used
8012 to replace it. Return the replacement if it is OK. Otherwise return
8016 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8020 kept
= sec
->kept_section
;
8023 if ((kept
->flags
& SEC_GROUP
) != 0)
8024 kept
= match_group_member (sec
, kept
, info
);
8025 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8027 sec
->kept_section
= kept
;
8032 /* Link an input file into the linker output file. This function
8033 handles all the sections and relocations of the input file at once.
8034 This is so that we only have to read the local symbols once, and
8035 don't have to keep them in memory. */
8038 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8040 bfd_boolean (*relocate_section
)
8041 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8042 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8044 Elf_Internal_Shdr
*symtab_hdr
;
8047 Elf_Internal_Sym
*isymbuf
;
8048 Elf_Internal_Sym
*isym
;
8049 Elf_Internal_Sym
*isymend
;
8051 asection
**ppsection
;
8053 const struct elf_backend_data
*bed
;
8054 bfd_boolean emit_relocs
;
8055 struct elf_link_hash_entry
**sym_hashes
;
8057 output_bfd
= finfo
->output_bfd
;
8058 bed
= get_elf_backend_data (output_bfd
);
8059 relocate_section
= bed
->elf_backend_relocate_section
;
8061 /* If this is a dynamic object, we don't want to do anything here:
8062 we don't want the local symbols, and we don't want the section
8064 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8067 emit_relocs
= (finfo
->info
->relocatable
8068 || finfo
->info
->emitrelocations
);
8070 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8071 if (elf_bad_symtab (input_bfd
))
8073 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8078 locsymcount
= symtab_hdr
->sh_info
;
8079 extsymoff
= symtab_hdr
->sh_info
;
8082 /* Read the local symbols. */
8083 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8084 if (isymbuf
== NULL
&& locsymcount
!= 0)
8086 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8087 finfo
->internal_syms
,
8088 finfo
->external_syms
,
8089 finfo
->locsym_shndx
);
8090 if (isymbuf
== NULL
)
8093 /* evaluate_complex_relocation_symbols looks for symbols in
8094 finfo->internal_syms. */
8095 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
8097 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8098 finfo
->internal_syms
,
8099 finfo
->external_syms
,
8100 finfo
->locsym_shndx
);
8103 /* Find local symbol sections and adjust values of symbols in
8104 SEC_MERGE sections. Write out those local symbols we know are
8105 going into the output file. */
8106 isymend
= isymbuf
+ locsymcount
;
8107 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8109 isym
++, pindex
++, ppsection
++)
8113 Elf_Internal_Sym osym
;
8117 if (elf_bad_symtab (input_bfd
))
8119 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8126 if (isym
->st_shndx
== SHN_UNDEF
)
8127 isec
= bfd_und_section_ptr
;
8128 else if (isym
->st_shndx
< SHN_LORESERVE
8129 || isym
->st_shndx
> SHN_HIRESERVE
)
8131 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8133 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8134 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8136 _bfd_merged_section_offset (output_bfd
, &isec
,
8137 elf_section_data (isec
)->sec_info
,
8140 else if (isym
->st_shndx
== SHN_ABS
)
8141 isec
= bfd_abs_section_ptr
;
8142 else if (isym
->st_shndx
== SHN_COMMON
)
8143 isec
= bfd_com_section_ptr
;
8146 /* Don't attempt to output symbols with st_shnx in the
8147 reserved range other than SHN_ABS and SHN_COMMON. */
8154 /* Don't output the first, undefined, symbol. */
8155 if (ppsection
== finfo
->sections
)
8158 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8160 /* We never output section symbols. Instead, we use the
8161 section symbol of the corresponding section in the output
8166 /* If we are stripping all symbols, we don't want to output this
8168 if (finfo
->info
->strip
== strip_all
)
8171 /* If we are discarding all local symbols, we don't want to
8172 output this one. If we are generating a relocatable output
8173 file, then some of the local symbols may be required by
8174 relocs; we output them below as we discover that they are
8176 if (finfo
->info
->discard
== discard_all
)
8179 /* If this symbol is defined in a section which we are
8180 discarding, we don't need to keep it. */
8181 if (isym
->st_shndx
!= SHN_UNDEF
8182 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8184 || bfd_section_removed_from_list (output_bfd
,
8185 isec
->output_section
)))
8188 /* Get the name of the symbol. */
8189 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
8194 /* See if we are discarding symbols with this name. */
8195 if ((finfo
->info
->strip
== strip_some
8196 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
8198 || (((finfo
->info
->discard
== discard_sec_merge
8199 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
8200 || finfo
->info
->discard
== discard_l
)
8201 && bfd_is_local_label_name (input_bfd
, name
)))
8204 /* If we get here, we are going to output this symbol. */
8208 /* Adjust the section index for the output file. */
8209 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
8210 isec
->output_section
);
8211 if (osym
.st_shndx
== SHN_BAD
)
8214 *pindex
= bfd_get_symcount (output_bfd
);
8216 /* ELF symbols in relocatable files are section relative, but
8217 in executable files they are virtual addresses. Note that
8218 this code assumes that all ELF sections have an associated
8219 BFD section with a reasonable value for output_offset; below
8220 we assume that they also have a reasonable value for
8221 output_section. Any special sections must be set up to meet
8222 these requirements. */
8223 osym
.st_value
+= isec
->output_offset
;
8224 if (! finfo
->info
->relocatable
)
8226 osym
.st_value
+= isec
->output_section
->vma
;
8227 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
8229 /* STT_TLS symbols are relative to PT_TLS segment base. */
8230 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8231 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8235 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
8239 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
8242 /* Relocate the contents of each section. */
8243 sym_hashes
= elf_sym_hashes (input_bfd
);
8244 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
8248 if (! o
->linker_mark
)
8250 /* This section was omitted from the link. */
8254 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8255 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
8258 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
8260 /* Section was created by _bfd_elf_link_create_dynamic_sections
8265 /* Get the contents of the section. They have been cached by a
8266 relaxation routine. Note that o is a section in an input
8267 file, so the contents field will not have been set by any of
8268 the routines which work on output files. */
8269 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
8270 contents
= elf_section_data (o
)->this_hdr
.contents
;
8273 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
8275 contents
= finfo
->contents
;
8276 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
8280 if ((o
->flags
& SEC_RELOC
) != 0)
8282 Elf_Internal_Rela
*internal_relocs
;
8283 bfd_vma r_type_mask
;
8286 /* Get the swapped relocs. */
8288 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
8289 finfo
->internal_relocs
, FALSE
);
8290 if (internal_relocs
== NULL
8291 && o
->reloc_count
> 0)
8294 if (bed
->s
->arch_size
== 32)
8301 r_type_mask
= 0xffffffff;
8305 /* Run through the relocs looking for any against symbols
8306 from discarded sections and section symbols from
8307 removed link-once sections. Complain about relocs
8308 against discarded sections. Zero relocs against removed
8309 link-once sections. */
8310 if (!elf_section_ignore_discarded_relocs (o
))
8312 Elf_Internal_Rela
*rel
, *relend
;
8313 unsigned int action
= (*bed
->action_discarded
) (o
);
8315 rel
= internal_relocs
;
8316 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8317 for ( ; rel
< relend
; rel
++)
8319 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
8320 asection
**ps
, *sec
;
8321 struct elf_link_hash_entry
*h
= NULL
;
8322 const char *sym_name
;
8324 if (r_symndx
== STN_UNDEF
)
8327 if (r_symndx
>= locsymcount
8328 || (elf_bad_symtab (input_bfd
)
8329 && finfo
->sections
[r_symndx
] == NULL
))
8331 h
= sym_hashes
[r_symndx
- extsymoff
];
8333 /* Badly formatted input files can contain relocs that
8334 reference non-existant symbols. Check here so that
8335 we do not seg fault. */
8340 sprintf_vma (buffer
, rel
->r_info
);
8341 (*_bfd_error_handler
)
8342 (_("error: %B contains a reloc (0x%s) for section %A "
8343 "that references a non-existent global symbol"),
8344 input_bfd
, o
, buffer
);
8345 bfd_set_error (bfd_error_bad_value
);
8349 while (h
->root
.type
== bfd_link_hash_indirect
8350 || h
->root
.type
== bfd_link_hash_warning
)
8351 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8353 if (h
->root
.type
!= bfd_link_hash_defined
8354 && h
->root
.type
!= bfd_link_hash_defweak
)
8357 ps
= &h
->root
.u
.def
.section
;
8358 sym_name
= h
->root
.root
.string
;
8362 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
8363 ps
= &finfo
->sections
[r_symndx
];
8364 sym_name
= bfd_elf_sym_name (input_bfd
,
8369 /* Complain if the definition comes from a
8370 discarded section. */
8371 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
8373 BFD_ASSERT (r_symndx
!= 0);
8374 if (action
& COMPLAIN
)
8375 (*finfo
->info
->callbacks
->einfo
)
8376 (_("%X`%s' referenced in section `%A' of %B: "
8377 "defined in discarded section `%A' of %B\n"),
8378 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
8380 /* Try to do the best we can to support buggy old
8381 versions of gcc. Pretend that the symbol is
8382 really defined in the kept linkonce section.
8383 FIXME: This is quite broken. Modifying the
8384 symbol here means we will be changing all later
8385 uses of the symbol, not just in this section. */
8386 if (action
& PRETEND
)
8390 kept
= _bfd_elf_check_kept_section (sec
,
8402 /* Relocate the section by invoking a back end routine.
8404 The back end routine is responsible for adjusting the
8405 section contents as necessary, and (if using Rela relocs
8406 and generating a relocatable output file) adjusting the
8407 reloc addend as necessary.
8409 The back end routine does not have to worry about setting
8410 the reloc address or the reloc symbol index.
8412 The back end routine is given a pointer to the swapped in
8413 internal symbols, and can access the hash table entries
8414 for the external symbols via elf_sym_hashes (input_bfd).
8416 When generating relocatable output, the back end routine
8417 must handle STB_LOCAL/STT_SECTION symbols specially. The
8418 output symbol is going to be a section symbol
8419 corresponding to the output section, which will require
8420 the addend to be adjusted. */
8422 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
8423 input_bfd
, o
, contents
,
8431 Elf_Internal_Rela
*irela
;
8432 Elf_Internal_Rela
*irelaend
;
8433 bfd_vma last_offset
;
8434 struct elf_link_hash_entry
**rel_hash
;
8435 struct elf_link_hash_entry
**rel_hash_list
;
8436 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
8437 unsigned int next_erel
;
8438 bfd_boolean rela_normal
;
8440 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
8441 rela_normal
= (bed
->rela_normal
8442 && (input_rel_hdr
->sh_entsize
8443 == bed
->s
->sizeof_rela
));
8445 /* Adjust the reloc addresses and symbol indices. */
8447 irela
= internal_relocs
;
8448 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8449 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
8450 + elf_section_data (o
->output_section
)->rel_count
8451 + elf_section_data (o
->output_section
)->rel_count2
);
8452 rel_hash_list
= rel_hash
;
8453 last_offset
= o
->output_offset
;
8454 if (!finfo
->info
->relocatable
)
8455 last_offset
+= o
->output_section
->vma
;
8456 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
8458 unsigned long r_symndx
;
8460 Elf_Internal_Sym sym
;
8462 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
8468 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
8471 if (irela
->r_offset
>= (bfd_vma
) -2)
8473 /* This is a reloc for a deleted entry or somesuch.
8474 Turn it into an R_*_NONE reloc, at the same
8475 offset as the last reloc. elf_eh_frame.c and
8476 bfd_elf_discard_info rely on reloc offsets
8478 irela
->r_offset
= last_offset
;
8480 irela
->r_addend
= 0;
8484 irela
->r_offset
+= o
->output_offset
;
8486 /* Relocs in an executable have to be virtual addresses. */
8487 if (!finfo
->info
->relocatable
)
8488 irela
->r_offset
+= o
->output_section
->vma
;
8490 last_offset
= irela
->r_offset
;
8492 r_symndx
= irela
->r_info
>> r_sym_shift
;
8493 if (r_symndx
== STN_UNDEF
)
8496 if (r_symndx
>= locsymcount
8497 || (elf_bad_symtab (input_bfd
)
8498 && finfo
->sections
[r_symndx
] == NULL
))
8500 struct elf_link_hash_entry
*rh
;
8503 /* This is a reloc against a global symbol. We
8504 have not yet output all the local symbols, so
8505 we do not know the symbol index of any global
8506 symbol. We set the rel_hash entry for this
8507 reloc to point to the global hash table entry
8508 for this symbol. The symbol index is then
8509 set at the end of bfd_elf_final_link. */
8510 indx
= r_symndx
- extsymoff
;
8511 rh
= elf_sym_hashes (input_bfd
)[indx
];
8512 while (rh
->root
.type
== bfd_link_hash_indirect
8513 || rh
->root
.type
== bfd_link_hash_warning
)
8514 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
8516 /* Setting the index to -2 tells
8517 elf_link_output_extsym that this symbol is
8519 BFD_ASSERT (rh
->indx
< 0);
8527 /* This is a reloc against a local symbol. */
8530 sym
= isymbuf
[r_symndx
];
8531 sec
= finfo
->sections
[r_symndx
];
8532 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
8534 /* I suppose the backend ought to fill in the
8535 section of any STT_SECTION symbol against a
8536 processor specific section. */
8538 if (bfd_is_abs_section (sec
))
8540 else if (sec
== NULL
|| sec
->owner
== NULL
)
8542 bfd_set_error (bfd_error_bad_value
);
8547 asection
*osec
= sec
->output_section
;
8549 /* If we have discarded a section, the output
8550 section will be the absolute section. In
8551 case of discarded SEC_MERGE sections, use
8552 the kept section. relocate_section should
8553 have already handled discarded linkonce
8555 if (bfd_is_abs_section (osec
)
8556 && sec
->kept_section
!= NULL
8557 && sec
->kept_section
->output_section
!= NULL
)
8559 osec
= sec
->kept_section
->output_section
;
8560 irela
->r_addend
-= osec
->vma
;
8563 if (!bfd_is_abs_section (osec
))
8565 r_symndx
= osec
->target_index
;
8568 struct elf_link_hash_table
*htab
;
8571 htab
= elf_hash_table (finfo
->info
);
8572 oi
= htab
->text_index_section
;
8573 if ((osec
->flags
& SEC_READONLY
) == 0
8574 && htab
->data_index_section
!= NULL
)
8575 oi
= htab
->data_index_section
;
8579 irela
->r_addend
+= osec
->vma
- oi
->vma
;
8580 r_symndx
= oi
->target_index
;
8584 BFD_ASSERT (r_symndx
!= 0);
8588 /* Adjust the addend according to where the
8589 section winds up in the output section. */
8591 irela
->r_addend
+= sec
->output_offset
;
8595 if (finfo
->indices
[r_symndx
] == -1)
8597 unsigned long shlink
;
8601 if (finfo
->info
->strip
== strip_all
)
8603 /* You can't do ld -r -s. */
8604 bfd_set_error (bfd_error_invalid_operation
);
8608 /* This symbol was skipped earlier, but
8609 since it is needed by a reloc, we
8610 must output it now. */
8611 shlink
= symtab_hdr
->sh_link
;
8612 name
= (bfd_elf_string_from_elf_section
8613 (input_bfd
, shlink
, sym
.st_name
));
8617 osec
= sec
->output_section
;
8619 _bfd_elf_section_from_bfd_section (output_bfd
,
8621 if (sym
.st_shndx
== SHN_BAD
)
8624 sym
.st_value
+= sec
->output_offset
;
8625 if (! finfo
->info
->relocatable
)
8627 sym
.st_value
+= osec
->vma
;
8628 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
8630 /* STT_TLS symbols are relative to PT_TLS
8632 BFD_ASSERT (elf_hash_table (finfo
->info
)
8634 sym
.st_value
-= (elf_hash_table (finfo
->info
)
8639 finfo
->indices
[r_symndx
]
8640 = bfd_get_symcount (output_bfd
);
8642 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
8647 r_symndx
= finfo
->indices
[r_symndx
];
8650 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
8651 | (irela
->r_info
& r_type_mask
));
8654 /* Swap out the relocs. */
8655 if (input_rel_hdr
->sh_size
!= 0
8656 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
8662 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
8663 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
8665 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
8666 * bed
->s
->int_rels_per_ext_rel
);
8667 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
8668 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
8677 /* Write out the modified section contents. */
8678 if (bed
->elf_backend_write_section
8679 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
8682 /* Section written out. */
8684 else switch (o
->sec_info_type
)
8686 case ELF_INFO_TYPE_STABS
:
8687 if (! (_bfd_write_section_stabs
8689 &elf_hash_table (finfo
->info
)->stab_info
,
8690 o
, &elf_section_data (o
)->sec_info
, contents
)))
8693 case ELF_INFO_TYPE_MERGE
:
8694 if (! _bfd_write_merged_section (output_bfd
, o
,
8695 elf_section_data (o
)->sec_info
))
8698 case ELF_INFO_TYPE_EH_FRAME
:
8700 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
8707 if (! (o
->flags
& SEC_EXCLUDE
)
8708 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
8710 (file_ptr
) o
->output_offset
,
8721 /* Generate a reloc when linking an ELF file. This is a reloc
8722 requested by the linker, and does not come from any input file. This
8723 is used to build constructor and destructor tables when linking
8727 elf_reloc_link_order (bfd
*output_bfd
,
8728 struct bfd_link_info
*info
,
8729 asection
*output_section
,
8730 struct bfd_link_order
*link_order
)
8732 reloc_howto_type
*howto
;
8736 struct elf_link_hash_entry
**rel_hash_ptr
;
8737 Elf_Internal_Shdr
*rel_hdr
;
8738 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8739 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8743 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8746 bfd_set_error (bfd_error_bad_value
);
8750 addend
= link_order
->u
.reloc
.p
->addend
;
8752 /* Figure out the symbol index. */
8753 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8754 + elf_section_data (output_section
)->rel_count
8755 + elf_section_data (output_section
)->rel_count2
);
8756 if (link_order
->type
== bfd_section_reloc_link_order
)
8758 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8759 BFD_ASSERT (indx
!= 0);
8760 *rel_hash_ptr
= NULL
;
8764 struct elf_link_hash_entry
*h
;
8766 /* Treat a reloc against a defined symbol as though it were
8767 actually against the section. */
8768 h
= ((struct elf_link_hash_entry
*)
8769 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8770 link_order
->u
.reloc
.p
->u
.name
,
8771 FALSE
, FALSE
, TRUE
));
8773 && (h
->root
.type
== bfd_link_hash_defined
8774 || h
->root
.type
== bfd_link_hash_defweak
))
8778 section
= h
->root
.u
.def
.section
;
8779 indx
= section
->output_section
->target_index
;
8780 *rel_hash_ptr
= NULL
;
8781 /* It seems that we ought to add the symbol value to the
8782 addend here, but in practice it has already been added
8783 because it was passed to constructor_callback. */
8784 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8788 /* Setting the index to -2 tells elf_link_output_extsym that
8789 this symbol is used by a reloc. */
8796 if (! ((*info
->callbacks
->unattached_reloc
)
8797 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
8803 /* If this is an inplace reloc, we must write the addend into the
8805 if (howto
->partial_inplace
&& addend
!= 0)
8808 bfd_reloc_status_type rstat
;
8811 const char *sym_name
;
8813 size
= bfd_get_reloc_size (howto
);
8814 buf
= bfd_zmalloc (size
);
8817 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8824 case bfd_reloc_outofrange
:
8827 case bfd_reloc_overflow
:
8828 if (link_order
->type
== bfd_section_reloc_link_order
)
8829 sym_name
= bfd_section_name (output_bfd
,
8830 link_order
->u
.reloc
.p
->u
.section
);
8832 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8833 if (! ((*info
->callbacks
->reloc_overflow
)
8834 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8835 NULL
, (bfd_vma
) 0)))
8842 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
8843 link_order
->offset
, size
);
8849 /* The address of a reloc is relative to the section in a
8850 relocatable file, and is a virtual address in an executable
8852 offset
= link_order
->offset
;
8853 if (! info
->relocatable
)
8854 offset
+= output_section
->vma
;
8856 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
8858 irel
[i
].r_offset
= offset
;
8860 irel
[i
].r_addend
= 0;
8862 if (bed
->s
->arch_size
== 32)
8863 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8865 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8867 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8868 erel
= rel_hdr
->contents
;
8869 if (rel_hdr
->sh_type
== SHT_REL
)
8871 erel
+= (elf_section_data (output_section
)->rel_count
8872 * bed
->s
->sizeof_rel
);
8873 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8877 irel
[0].r_addend
= addend
;
8878 erel
+= (elf_section_data (output_section
)->rel_count
8879 * bed
->s
->sizeof_rela
);
8880 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8883 ++elf_section_data (output_section
)->rel_count
;
8889 /* Get the output vma of the section pointed to by the sh_link field. */
8892 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8894 Elf_Internal_Shdr
**elf_shdrp
;
8898 s
= p
->u
.indirect
.section
;
8899 elf_shdrp
= elf_elfsections (s
->owner
);
8900 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8901 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8903 The Intel C compiler generates SHT_IA_64_UNWIND with
8904 SHF_LINK_ORDER. But it doesn't set the sh_link or
8905 sh_info fields. Hence we could get the situation
8906 where elfsec is 0. */
8909 const struct elf_backend_data
*bed
8910 = get_elf_backend_data (s
->owner
);
8911 if (bed
->link_order_error_handler
)
8912 bed
->link_order_error_handler
8913 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8918 s
= elf_shdrp
[elfsec
]->bfd_section
;
8919 return s
->output_section
->vma
+ s
->output_offset
;
8924 /* Compare two sections based on the locations of the sections they are
8925 linked to. Used by elf_fixup_link_order. */
8928 compare_link_order (const void * a
, const void * b
)
8933 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8934 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8941 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8942 order as their linked sections. Returns false if this could not be done
8943 because an output section includes both ordered and unordered
8944 sections. Ideally we'd do this in the linker proper. */
8947 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8952 struct bfd_link_order
*p
;
8954 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8956 struct bfd_link_order
**sections
;
8957 asection
*s
, *other_sec
, *linkorder_sec
;
8961 linkorder_sec
= NULL
;
8964 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8966 if (p
->type
== bfd_indirect_link_order
)
8968 s
= p
->u
.indirect
.section
;
8970 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8971 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8972 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8973 && elfsec
< elf_numsections (sub
)
8974 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8988 if (seen_other
&& seen_linkorder
)
8990 if (other_sec
&& linkorder_sec
)
8991 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8993 linkorder_sec
->owner
, other_sec
,
8996 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
8998 bfd_set_error (bfd_error_bad_value
);
9003 if (!seen_linkorder
)
9006 sections
= (struct bfd_link_order
**)
9007 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9010 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9012 sections
[seen_linkorder
++] = p
;
9014 /* Sort the input sections in the order of their linked section. */
9015 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9016 compare_link_order
);
9018 /* Change the offsets of the sections. */
9020 for (n
= 0; n
< seen_linkorder
; n
++)
9022 s
= sections
[n
]->u
.indirect
.section
;
9023 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9024 s
->output_offset
= offset
;
9025 sections
[n
]->offset
= offset
;
9026 offset
+= sections
[n
]->size
;
9033 /* Do the final step of an ELF link. */
9036 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9038 bfd_boolean dynamic
;
9039 bfd_boolean emit_relocs
;
9041 struct elf_final_link_info finfo
;
9042 register asection
*o
;
9043 register struct bfd_link_order
*p
;
9045 bfd_size_type max_contents_size
;
9046 bfd_size_type max_external_reloc_size
;
9047 bfd_size_type max_internal_reloc_count
;
9048 bfd_size_type max_sym_count
;
9049 bfd_size_type max_sym_shndx_count
;
9051 Elf_Internal_Sym elfsym
;
9053 Elf_Internal_Shdr
*symtab_hdr
;
9054 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9055 Elf_Internal_Shdr
*symstrtab_hdr
;
9056 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9057 struct elf_outext_info eoinfo
;
9059 size_t relativecount
= 0;
9060 asection
*reldyn
= 0;
9063 if (! is_elf_hash_table (info
->hash
))
9067 abfd
->flags
|= DYNAMIC
;
9069 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9070 dynobj
= elf_hash_table (info
)->dynobj
;
9072 emit_relocs
= (info
->relocatable
9073 || info
->emitrelocations
);
9076 finfo
.output_bfd
= abfd
;
9077 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9078 if (finfo
.symstrtab
== NULL
)
9083 finfo
.dynsym_sec
= NULL
;
9084 finfo
.hash_sec
= NULL
;
9085 finfo
.symver_sec
= NULL
;
9089 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9090 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9091 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9092 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9093 /* Note that it is OK if symver_sec is NULL. */
9096 finfo
.contents
= NULL
;
9097 finfo
.external_relocs
= NULL
;
9098 finfo
.internal_relocs
= NULL
;
9099 finfo
.external_syms
= NULL
;
9100 finfo
.locsym_shndx
= NULL
;
9101 finfo
.internal_syms
= NULL
;
9102 finfo
.indices
= NULL
;
9103 finfo
.sections
= NULL
;
9104 finfo
.symbuf
= NULL
;
9105 finfo
.symshndxbuf
= NULL
;
9106 finfo
.symbuf_count
= 0;
9107 finfo
.shndxbuf_size
= 0;
9109 /* Count up the number of relocations we will output for each output
9110 section, so that we know the sizes of the reloc sections. We
9111 also figure out some maximum sizes. */
9112 max_contents_size
= 0;
9113 max_external_reloc_size
= 0;
9114 max_internal_reloc_count
= 0;
9116 max_sym_shndx_count
= 0;
9118 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9120 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
9123 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9125 unsigned int reloc_count
= 0;
9126 struct bfd_elf_section_data
*esdi
= NULL
;
9127 unsigned int *rel_count1
;
9129 if (p
->type
== bfd_section_reloc_link_order
9130 || p
->type
== bfd_symbol_reloc_link_order
)
9132 else if (p
->type
== bfd_indirect_link_order
)
9136 sec
= p
->u
.indirect
.section
;
9137 esdi
= elf_section_data (sec
);
9139 /* Mark all sections which are to be included in the
9140 link. This will normally be every section. We need
9141 to do this so that we can identify any sections which
9142 the linker has decided to not include. */
9143 sec
->linker_mark
= TRUE
;
9145 if (sec
->flags
& SEC_MERGE
)
9148 if (info
->relocatable
|| info
->emitrelocations
)
9149 reloc_count
= sec
->reloc_count
;
9150 else if (bed
->elf_backend_count_relocs
)
9152 Elf_Internal_Rela
* relocs
;
9154 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
9157 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
9159 if (elf_section_data (o
)->relocs
!= relocs
)
9163 if (sec
->rawsize
> max_contents_size
)
9164 max_contents_size
= sec
->rawsize
;
9165 if (sec
->size
> max_contents_size
)
9166 max_contents_size
= sec
->size
;
9168 /* We are interested in just local symbols, not all
9170 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
9171 && (sec
->owner
->flags
& DYNAMIC
) == 0)
9175 if (elf_bad_symtab (sec
->owner
))
9176 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
9177 / bed
->s
->sizeof_sym
);
9179 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
9181 if (sym_count
> max_sym_count
)
9182 max_sym_count
= sym_count
;
9184 if (sym_count
> max_sym_shndx_count
9185 && elf_symtab_shndx (sec
->owner
) != 0)
9186 max_sym_shndx_count
= sym_count
;
9188 if ((sec
->flags
& SEC_RELOC
) != 0)
9192 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
9193 if (ext_size
> max_external_reloc_size
)
9194 max_external_reloc_size
= ext_size
;
9195 if (sec
->reloc_count
> max_internal_reloc_count
)
9196 max_internal_reloc_count
= sec
->reloc_count
;
9201 if (reloc_count
== 0)
9204 o
->reloc_count
+= reloc_count
;
9206 /* MIPS may have a mix of REL and RELA relocs on sections.
9207 To support this curious ABI we keep reloc counts in
9208 elf_section_data too. We must be careful to add the
9209 relocations from the input section to the right output
9210 count. FIXME: Get rid of one count. We have
9211 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9212 rel_count1
= &esdo
->rel_count
;
9215 bfd_boolean same_size
;
9216 bfd_size_type entsize1
;
9218 entsize1
= esdi
->rel_hdr
.sh_entsize
;
9219 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
9220 || entsize1
== bed
->s
->sizeof_rela
);
9221 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
9224 rel_count1
= &esdo
->rel_count2
;
9226 if (esdi
->rel_hdr2
!= NULL
)
9228 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
9229 unsigned int alt_count
;
9230 unsigned int *rel_count2
;
9232 BFD_ASSERT (entsize2
!= entsize1
9233 && (entsize2
== bed
->s
->sizeof_rel
9234 || entsize2
== bed
->s
->sizeof_rela
));
9236 rel_count2
= &esdo
->rel_count2
;
9238 rel_count2
= &esdo
->rel_count
;
9240 /* The following is probably too simplistic if the
9241 backend counts output relocs unusually. */
9242 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
9243 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
9244 *rel_count2
+= alt_count
;
9245 reloc_count
-= alt_count
;
9248 *rel_count1
+= reloc_count
;
9251 if (o
->reloc_count
> 0)
9252 o
->flags
|= SEC_RELOC
;
9255 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9256 set it (this is probably a bug) and if it is set
9257 assign_section_numbers will create a reloc section. */
9258 o
->flags
&=~ SEC_RELOC
;
9261 /* If the SEC_ALLOC flag is not set, force the section VMA to
9262 zero. This is done in elf_fake_sections as well, but forcing
9263 the VMA to 0 here will ensure that relocs against these
9264 sections are handled correctly. */
9265 if ((o
->flags
& SEC_ALLOC
) == 0
9266 && ! o
->user_set_vma
)
9270 if (! info
->relocatable
&& merged
)
9271 elf_link_hash_traverse (elf_hash_table (info
),
9272 _bfd_elf_link_sec_merge_syms
, abfd
);
9274 /* Figure out the file positions for everything but the symbol table
9275 and the relocs. We set symcount to force assign_section_numbers
9276 to create a symbol table. */
9277 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
9278 BFD_ASSERT (! abfd
->output_has_begun
);
9279 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
9282 /* Set sizes, and assign file positions for reloc sections. */
9283 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9285 if ((o
->flags
& SEC_RELOC
) != 0)
9287 if (!(_bfd_elf_link_size_reloc_section
9288 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
9291 if (elf_section_data (o
)->rel_hdr2
9292 && !(_bfd_elf_link_size_reloc_section
9293 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
9297 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9298 to count upwards while actually outputting the relocations. */
9299 elf_section_data (o
)->rel_count
= 0;
9300 elf_section_data (o
)->rel_count2
= 0;
9303 _bfd_elf_assign_file_positions_for_relocs (abfd
);
9305 /* We have now assigned file positions for all the sections except
9306 .symtab and .strtab. We start the .symtab section at the current
9307 file position, and write directly to it. We build the .strtab
9308 section in memory. */
9309 bfd_get_symcount (abfd
) = 0;
9310 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9311 /* sh_name is set in prep_headers. */
9312 symtab_hdr
->sh_type
= SHT_SYMTAB
;
9313 /* sh_flags, sh_addr and sh_size all start off zero. */
9314 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
9315 /* sh_link is set in assign_section_numbers. */
9316 /* sh_info is set below. */
9317 /* sh_offset is set just below. */
9318 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
9320 off
= elf_tdata (abfd
)->next_file_pos
;
9321 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
9323 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9324 incorrect. We do not yet know the size of the .symtab section.
9325 We correct next_file_pos below, after we do know the size. */
9327 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9328 continuously seeking to the right position in the file. */
9329 if (! info
->keep_memory
|| max_sym_count
< 20)
9330 finfo
.symbuf_size
= 20;
9332 finfo
.symbuf_size
= max_sym_count
;
9333 amt
= finfo
.symbuf_size
;
9334 amt
*= bed
->s
->sizeof_sym
;
9335 finfo
.symbuf
= bfd_malloc (amt
);
9336 if (finfo
.symbuf
== NULL
)
9338 if (elf_numsections (abfd
) > SHN_LORESERVE
)
9340 /* Wild guess at number of output symbols. realloc'd as needed. */
9341 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
9342 finfo
.shndxbuf_size
= amt
;
9343 amt
*= sizeof (Elf_External_Sym_Shndx
);
9344 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
9345 if (finfo
.symshndxbuf
== NULL
)
9349 /* Start writing out the symbol table. The first symbol is always a
9351 if (info
->strip
!= strip_all
9354 elfsym
.st_value
= 0;
9357 elfsym
.st_other
= 0;
9358 elfsym
.st_shndx
= SHN_UNDEF
;
9359 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
9364 /* Output a symbol for each section. We output these even if we are
9365 discarding local symbols, since they are used for relocs. These
9366 symbols have no names. We store the index of each one in the
9367 index field of the section, so that we can find it again when
9368 outputting relocs. */
9369 if (info
->strip
!= strip_all
9373 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9374 elfsym
.st_other
= 0;
9375 elfsym
.st_value
= 0;
9376 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9378 o
= bfd_section_from_elf_index (abfd
, i
);
9381 o
->target_index
= bfd_get_symcount (abfd
);
9382 elfsym
.st_shndx
= i
;
9383 if (!info
->relocatable
)
9384 elfsym
.st_value
= o
->vma
;
9385 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
9388 if (i
== SHN_LORESERVE
- 1)
9389 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
9393 /* Allocate some memory to hold information read in from the input
9395 if (max_contents_size
!= 0)
9397 finfo
.contents
= bfd_malloc (max_contents_size
);
9398 if (finfo
.contents
== NULL
)
9402 if (max_external_reloc_size
!= 0)
9404 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
9405 if (finfo
.external_relocs
== NULL
)
9409 if (max_internal_reloc_count
!= 0)
9411 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9412 amt
*= sizeof (Elf_Internal_Rela
);
9413 finfo
.internal_relocs
= bfd_malloc (amt
);
9414 if (finfo
.internal_relocs
== NULL
)
9418 if (max_sym_count
!= 0)
9420 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
9421 finfo
.external_syms
= bfd_malloc (amt
);
9422 if (finfo
.external_syms
== NULL
)
9425 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
9426 finfo
.internal_syms
= bfd_malloc (amt
);
9427 if (finfo
.internal_syms
== NULL
)
9430 amt
= max_sym_count
* sizeof (long);
9431 finfo
.indices
= bfd_malloc (amt
);
9432 if (finfo
.indices
== NULL
)
9435 amt
= max_sym_count
* sizeof (asection
*);
9436 finfo
.sections
= bfd_malloc (amt
);
9437 if (finfo
.sections
== NULL
)
9441 if (max_sym_shndx_count
!= 0)
9443 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
9444 finfo
.locsym_shndx
= bfd_malloc (amt
);
9445 if (finfo
.locsym_shndx
== NULL
)
9449 if (elf_hash_table (info
)->tls_sec
)
9451 bfd_vma base
, end
= 0;
9454 for (sec
= elf_hash_table (info
)->tls_sec
;
9455 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
9458 bfd_size_type size
= sec
->size
;
9461 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
9463 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
9465 size
= o
->offset
+ o
->size
;
9467 end
= sec
->vma
+ size
;
9469 base
= elf_hash_table (info
)->tls_sec
->vma
;
9470 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
9471 elf_hash_table (info
)->tls_size
= end
- base
;
9474 /* Reorder SHF_LINK_ORDER sections. */
9475 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9477 if (!elf_fixup_link_order (abfd
, o
))
9481 /* Since ELF permits relocations to be against local symbols, we
9482 must have the local symbols available when we do the relocations.
9483 Since we would rather only read the local symbols once, and we
9484 would rather not keep them in memory, we handle all the
9485 relocations for a single input file at the same time.
9487 Unfortunately, there is no way to know the total number of local
9488 symbols until we have seen all of them, and the local symbol
9489 indices precede the global symbol indices. This means that when
9490 we are generating relocatable output, and we see a reloc against
9491 a global symbol, we can not know the symbol index until we have
9492 finished examining all the local symbols to see which ones we are
9493 going to output. To deal with this, we keep the relocations in
9494 memory, and don't output them until the end of the link. This is
9495 an unfortunate waste of memory, but I don't see a good way around
9496 it. Fortunately, it only happens when performing a relocatable
9497 link, which is not the common case. FIXME: If keep_memory is set
9498 we could write the relocs out and then read them again; I don't
9499 know how bad the memory loss will be. */
9501 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9502 sub
->output_has_begun
= FALSE
;
9503 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9505 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9507 if (p
->type
== bfd_indirect_link_order
9508 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
9509 == bfd_target_elf_flavour
)
9510 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
9512 if (! sub
->output_has_begun
)
9514 if (! elf_link_input_bfd (&finfo
, sub
))
9516 sub
->output_has_begun
= TRUE
;
9519 else if (p
->type
== bfd_section_reloc_link_order
9520 || p
->type
== bfd_symbol_reloc_link_order
)
9522 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
9527 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
9533 /* Free symbol buffer if needed. */
9534 if (!info
->reduce_memory_overheads
)
9536 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9537 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9538 && elf_tdata (sub
)->symbuf
)
9540 free (elf_tdata (sub
)->symbuf
);
9541 elf_tdata (sub
)->symbuf
= NULL
;
9545 /* Output any global symbols that got converted to local in a
9546 version script or due to symbol visibility. We do this in a
9547 separate step since ELF requires all local symbols to appear
9548 prior to any global symbols. FIXME: We should only do this if
9549 some global symbols were, in fact, converted to become local.
9550 FIXME: Will this work correctly with the Irix 5 linker? */
9551 eoinfo
.failed
= FALSE
;
9552 eoinfo
.finfo
= &finfo
;
9553 eoinfo
.localsyms
= TRUE
;
9554 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9559 /* If backend needs to output some local symbols not present in the hash
9560 table, do it now. */
9561 if (bed
->elf_backend_output_arch_local_syms
)
9563 typedef bfd_boolean (*out_sym_func
)
9564 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9565 struct elf_link_hash_entry
*);
9567 if (! ((*bed
->elf_backend_output_arch_local_syms
)
9568 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9572 /* That wrote out all the local symbols. Finish up the symbol table
9573 with the global symbols. Even if we want to strip everything we
9574 can, we still need to deal with those global symbols that got
9575 converted to local in a version script. */
9577 /* The sh_info field records the index of the first non local symbol. */
9578 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
9581 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
9583 Elf_Internal_Sym sym
;
9584 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
9585 long last_local
= 0;
9587 /* Write out the section symbols for the output sections. */
9588 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
9594 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9597 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9603 dynindx
= elf_section_data (s
)->dynindx
;
9606 indx
= elf_section_data (s
)->this_idx
;
9607 BFD_ASSERT (indx
> 0);
9608 sym
.st_shndx
= indx
;
9609 if (! check_dynsym (abfd
, &sym
))
9611 sym
.st_value
= s
->vma
;
9612 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
9613 if (last_local
< dynindx
)
9614 last_local
= dynindx
;
9615 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9619 /* Write out the local dynsyms. */
9620 if (elf_hash_table (info
)->dynlocal
)
9622 struct elf_link_local_dynamic_entry
*e
;
9623 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
9628 sym
.st_size
= e
->isym
.st_size
;
9629 sym
.st_other
= e
->isym
.st_other
;
9631 /* Copy the internal symbol as is.
9632 Note that we saved a word of storage and overwrote
9633 the original st_name with the dynstr_index. */
9636 if (e
->isym
.st_shndx
!= SHN_UNDEF
9637 && (e
->isym
.st_shndx
< SHN_LORESERVE
9638 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
9640 s
= bfd_section_from_elf_index (e
->input_bfd
,
9644 elf_section_data (s
->output_section
)->this_idx
;
9645 if (! check_dynsym (abfd
, &sym
))
9647 sym
.st_value
= (s
->output_section
->vma
9649 + e
->isym
.st_value
);
9652 if (last_local
< e
->dynindx
)
9653 last_local
= e
->dynindx
;
9655 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
9656 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9660 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
9664 /* We get the global symbols from the hash table. */
9665 eoinfo
.failed
= FALSE
;
9666 eoinfo
.localsyms
= FALSE
;
9667 eoinfo
.finfo
= &finfo
;
9668 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9673 /* If backend needs to output some symbols not present in the hash
9674 table, do it now. */
9675 if (bed
->elf_backend_output_arch_syms
)
9677 typedef bfd_boolean (*out_sym_func
)
9678 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9679 struct elf_link_hash_entry
*);
9681 if (! ((*bed
->elf_backend_output_arch_syms
)
9682 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9686 /* Flush all symbols to the file. */
9687 if (! elf_link_flush_output_syms (&finfo
, bed
))
9690 /* Now we know the size of the symtab section. */
9691 off
+= symtab_hdr
->sh_size
;
9693 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
9694 if (symtab_shndx_hdr
->sh_name
!= 0)
9696 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
9697 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
9698 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
9699 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
9700 symtab_shndx_hdr
->sh_size
= amt
;
9702 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
9705 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
9706 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
9711 /* Finish up and write out the symbol string table (.strtab)
9713 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
9714 /* sh_name was set in prep_headers. */
9715 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9716 symstrtab_hdr
->sh_flags
= 0;
9717 symstrtab_hdr
->sh_addr
= 0;
9718 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
9719 symstrtab_hdr
->sh_entsize
= 0;
9720 symstrtab_hdr
->sh_link
= 0;
9721 symstrtab_hdr
->sh_info
= 0;
9722 /* sh_offset is set just below. */
9723 symstrtab_hdr
->sh_addralign
= 1;
9725 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
9726 elf_tdata (abfd
)->next_file_pos
= off
;
9728 if (bfd_get_symcount (abfd
) > 0)
9730 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9731 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9735 /* Adjust the relocs to have the correct symbol indices. */
9736 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9738 if ((o
->flags
& SEC_RELOC
) == 0)
9741 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9742 elf_section_data (o
)->rel_count
,
9743 elf_section_data (o
)->rel_hashes
);
9744 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9745 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9746 elf_section_data (o
)->rel_count2
,
9747 (elf_section_data (o
)->rel_hashes
9748 + elf_section_data (o
)->rel_count
));
9750 /* Set the reloc_count field to 0 to prevent write_relocs from
9751 trying to swap the relocs out itself. */
9755 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
9756 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
9758 /* If we are linking against a dynamic object, or generating a
9759 shared library, finish up the dynamic linking information. */
9762 bfd_byte
*dyncon
, *dynconend
;
9764 /* Fix up .dynamic entries. */
9765 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9766 BFD_ASSERT (o
!= NULL
);
9768 dyncon
= o
->contents
;
9769 dynconend
= o
->contents
+ o
->size
;
9770 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9772 Elf_Internal_Dyn dyn
;
9776 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9783 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
9785 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
9787 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
9788 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
9791 dyn
.d_un
.d_val
= relativecount
;
9798 name
= info
->init_function
;
9801 name
= info
->fini_function
;
9804 struct elf_link_hash_entry
*h
;
9806 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
9807 FALSE
, FALSE
, TRUE
);
9809 && (h
->root
.type
== bfd_link_hash_defined
9810 || h
->root
.type
== bfd_link_hash_defweak
))
9812 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
9813 o
= h
->root
.u
.def
.section
;
9814 if (o
->output_section
!= NULL
)
9815 dyn
.d_un
.d_val
+= (o
->output_section
->vma
9816 + o
->output_offset
);
9819 /* The symbol is imported from another shared
9820 library and does not apply to this one. */
9828 case DT_PREINIT_ARRAYSZ
:
9829 name
= ".preinit_array";
9831 case DT_INIT_ARRAYSZ
:
9832 name
= ".init_array";
9834 case DT_FINI_ARRAYSZ
:
9835 name
= ".fini_array";
9837 o
= bfd_get_section_by_name (abfd
, name
);
9840 (*_bfd_error_handler
)
9841 (_("%B: could not find output section %s"), abfd
, name
);
9845 (*_bfd_error_handler
)
9846 (_("warning: %s section has zero size"), name
);
9847 dyn
.d_un
.d_val
= o
->size
;
9850 case DT_PREINIT_ARRAY
:
9851 name
= ".preinit_array";
9854 name
= ".init_array";
9857 name
= ".fini_array";
9873 name
= ".gnu.version_d";
9876 name
= ".gnu.version_r";
9879 name
= ".gnu.version";
9881 o
= bfd_get_section_by_name (abfd
, name
);
9884 (*_bfd_error_handler
)
9885 (_("%B: could not find output section %s"), abfd
, name
);
9888 dyn
.d_un
.d_ptr
= o
->vma
;
9895 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9900 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9902 Elf_Internal_Shdr
*hdr
;
9904 hdr
= elf_elfsections (abfd
)[i
];
9905 if (hdr
->sh_type
== type
9906 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9908 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9909 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9912 if (dyn
.d_un
.d_val
== 0
9913 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9914 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9920 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9924 /* If we have created any dynamic sections, then output them. */
9927 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9930 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9931 if (info
->warn_shared_textrel
&& info
->shared
)
9933 bfd_byte
*dyncon
, *dynconend
;
9935 /* Fix up .dynamic entries. */
9936 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9937 BFD_ASSERT (o
!= NULL
);
9939 dyncon
= o
->contents
;
9940 dynconend
= o
->contents
+ o
->size
;
9941 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9943 Elf_Internal_Dyn dyn
;
9945 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9947 if (dyn
.d_tag
== DT_TEXTREL
)
9949 info
->callbacks
->einfo
9950 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
9956 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9958 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9960 || o
->output_section
== bfd_abs_section_ptr
)
9962 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9964 /* At this point, we are only interested in sections
9965 created by _bfd_elf_link_create_dynamic_sections. */
9968 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9970 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9972 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9974 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9976 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9978 (file_ptr
) o
->output_offset
,
9984 /* The contents of the .dynstr section are actually in a
9986 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9987 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9988 || ! _bfd_elf_strtab_emit (abfd
,
9989 elf_hash_table (info
)->dynstr
))
9995 if (info
->relocatable
)
9997 bfd_boolean failed
= FALSE
;
9999 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10004 /* If we have optimized stabs strings, output them. */
10005 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10007 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10011 if (info
->eh_frame_hdr
)
10013 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10017 if (finfo
.symstrtab
!= NULL
)
10018 _bfd_stringtab_free (finfo
.symstrtab
);
10019 if (finfo
.contents
!= NULL
)
10020 free (finfo
.contents
);
10021 if (finfo
.external_relocs
!= NULL
)
10022 free (finfo
.external_relocs
);
10023 if (finfo
.internal_relocs
!= NULL
)
10024 free (finfo
.internal_relocs
);
10025 if (finfo
.external_syms
!= NULL
)
10026 free (finfo
.external_syms
);
10027 if (finfo
.locsym_shndx
!= NULL
)
10028 free (finfo
.locsym_shndx
);
10029 if (finfo
.internal_syms
!= NULL
)
10030 free (finfo
.internal_syms
);
10031 if (finfo
.indices
!= NULL
)
10032 free (finfo
.indices
);
10033 if (finfo
.sections
!= NULL
)
10034 free (finfo
.sections
);
10035 if (finfo
.symbuf
!= NULL
)
10036 free (finfo
.symbuf
);
10037 if (finfo
.symshndxbuf
!= NULL
)
10038 free (finfo
.symshndxbuf
);
10039 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10041 if ((o
->flags
& SEC_RELOC
) != 0
10042 && elf_section_data (o
)->rel_hashes
!= NULL
)
10043 free (elf_section_data (o
)->rel_hashes
);
10046 elf_tdata (abfd
)->linker
= TRUE
;
10051 if (finfo
.symstrtab
!= NULL
)
10052 _bfd_stringtab_free (finfo
.symstrtab
);
10053 if (finfo
.contents
!= NULL
)
10054 free (finfo
.contents
);
10055 if (finfo
.external_relocs
!= NULL
)
10056 free (finfo
.external_relocs
);
10057 if (finfo
.internal_relocs
!= NULL
)
10058 free (finfo
.internal_relocs
);
10059 if (finfo
.external_syms
!= NULL
)
10060 free (finfo
.external_syms
);
10061 if (finfo
.locsym_shndx
!= NULL
)
10062 free (finfo
.locsym_shndx
);
10063 if (finfo
.internal_syms
!= NULL
)
10064 free (finfo
.internal_syms
);
10065 if (finfo
.indices
!= NULL
)
10066 free (finfo
.indices
);
10067 if (finfo
.sections
!= NULL
)
10068 free (finfo
.sections
);
10069 if (finfo
.symbuf
!= NULL
)
10070 free (finfo
.symbuf
);
10071 if (finfo
.symshndxbuf
!= NULL
)
10072 free (finfo
.symshndxbuf
);
10073 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10075 if ((o
->flags
& SEC_RELOC
) != 0
10076 && elf_section_data (o
)->rel_hashes
!= NULL
)
10077 free (elf_section_data (o
)->rel_hashes
);
10083 /* Garbage collect unused sections. */
10085 /* Default gc_mark_hook. */
10088 _bfd_elf_gc_mark_hook (asection
*sec
,
10089 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10090 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
10091 struct elf_link_hash_entry
*h
,
10092 Elf_Internal_Sym
*sym
)
10096 switch (h
->root
.type
)
10098 case bfd_link_hash_defined
:
10099 case bfd_link_hash_defweak
:
10100 return h
->root
.u
.def
.section
;
10102 case bfd_link_hash_common
:
10103 return h
->root
.u
.c
.p
->section
;
10110 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
10115 /* The mark phase of garbage collection. For a given section, mark
10116 it and any sections in this section's group, and all the sections
10117 which define symbols to which it refers. */
10120 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
10122 elf_gc_mark_hook_fn gc_mark_hook
)
10126 asection
*group_sec
;
10130 /* Mark all the sections in the group. */
10131 group_sec
= elf_section_data (sec
)->next_in_group
;
10132 if (group_sec
&& !group_sec
->gc_mark
)
10133 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
10136 /* Look through the section relocs. */
10138 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
10139 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
10141 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
10142 Elf_Internal_Shdr
*symtab_hdr
;
10143 struct elf_link_hash_entry
**sym_hashes
;
10146 bfd
*input_bfd
= sec
->owner
;
10147 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
10148 Elf_Internal_Sym
*isym
= NULL
;
10151 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10152 sym_hashes
= elf_sym_hashes (input_bfd
);
10154 /* Read the local symbols. */
10155 if (elf_bad_symtab (input_bfd
))
10157 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10161 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
10163 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10164 if (isym
== NULL
&& nlocsyms
!= 0)
10166 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
10172 /* Read the relocations. */
10173 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
10174 info
->keep_memory
);
10175 if (relstart
== NULL
)
10180 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10182 if (bed
->s
->arch_size
== 32)
10187 for (rel
= relstart
; rel
< relend
; rel
++)
10189 unsigned long r_symndx
;
10191 struct elf_link_hash_entry
*h
;
10193 r_symndx
= rel
->r_info
>> r_sym_shift
;
10197 if (r_symndx
>= nlocsyms
10198 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
10200 h
= sym_hashes
[r_symndx
- extsymoff
];
10201 while (h
->root
.type
== bfd_link_hash_indirect
10202 || h
->root
.type
== bfd_link_hash_warning
)
10203 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10204 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
10208 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
10211 if (rsec
&& !rsec
->gc_mark
)
10213 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
10216 rsec
->gc_mark_from_eh
= 1;
10217 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
10226 if (elf_section_data (sec
)->relocs
!= relstart
)
10229 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
10231 if (! info
->keep_memory
)
10234 symtab_hdr
->contents
= (unsigned char *) isym
;
10241 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10243 struct elf_gc_sweep_symbol_info
10245 struct bfd_link_info
*info
;
10246 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
10251 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
10253 if (h
->root
.type
== bfd_link_hash_warning
)
10254 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10256 if ((h
->root
.type
== bfd_link_hash_defined
10257 || h
->root
.type
== bfd_link_hash_defweak
)
10258 && !h
->root
.u
.def
.section
->gc_mark
10259 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
10261 struct elf_gc_sweep_symbol_info
*inf
= data
;
10262 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
10268 /* The sweep phase of garbage collection. Remove all garbage sections. */
10270 typedef bfd_boolean (*gc_sweep_hook_fn
)
10271 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
10274 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
10277 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10278 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
10279 unsigned long section_sym_count
;
10280 struct elf_gc_sweep_symbol_info sweep_info
;
10282 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10286 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10289 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10291 /* Keep debug and special sections. */
10292 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
10293 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
10299 /* Skip sweeping sections already excluded. */
10300 if (o
->flags
& SEC_EXCLUDE
)
10303 /* Since this is early in the link process, it is simple
10304 to remove a section from the output. */
10305 o
->flags
|= SEC_EXCLUDE
;
10307 if (info
->print_gc_sections
== TRUE
)
10308 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
10310 /* But we also have to update some of the relocation
10311 info we collected before. */
10313 && (o
->flags
& SEC_RELOC
) != 0
10314 && o
->reloc_count
> 0
10315 && !bfd_is_abs_section (o
->output_section
))
10317 Elf_Internal_Rela
*internal_relocs
;
10321 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
10322 info
->keep_memory
);
10323 if (internal_relocs
== NULL
)
10326 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
10328 if (elf_section_data (o
)->relocs
!= internal_relocs
)
10329 free (internal_relocs
);
10337 /* Remove the symbols that were in the swept sections from the dynamic
10338 symbol table. GCFIXME: Anyone know how to get them out of the
10339 static symbol table as well? */
10340 sweep_info
.info
= info
;
10341 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
10342 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
10345 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
10349 /* Propagate collected vtable information. This is called through
10350 elf_link_hash_traverse. */
10353 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
10355 if (h
->root
.type
== bfd_link_hash_warning
)
10356 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10358 /* Those that are not vtables. */
10359 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10362 /* Those vtables that do not have parents, we cannot merge. */
10363 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
10366 /* If we've already been done, exit. */
10367 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
10370 /* Make sure the parent's table is up to date. */
10371 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
10373 if (h
->vtable
->used
== NULL
)
10375 /* None of this table's entries were referenced. Re-use the
10377 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
10378 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
10383 bfd_boolean
*cu
, *pu
;
10385 /* Or the parent's entries into ours. */
10386 cu
= h
->vtable
->used
;
10388 pu
= h
->vtable
->parent
->vtable
->used
;
10391 const struct elf_backend_data
*bed
;
10392 unsigned int log_file_align
;
10394 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
10395 log_file_align
= bed
->s
->log_file_align
;
10396 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
10411 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
10414 bfd_vma hstart
, hend
;
10415 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
10416 const struct elf_backend_data
*bed
;
10417 unsigned int log_file_align
;
10419 if (h
->root
.type
== bfd_link_hash_warning
)
10420 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10422 /* Take care of both those symbols that do not describe vtables as
10423 well as those that are not loaded. */
10424 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10427 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
10428 || h
->root
.type
== bfd_link_hash_defweak
);
10430 sec
= h
->root
.u
.def
.section
;
10431 hstart
= h
->root
.u
.def
.value
;
10432 hend
= hstart
+ h
->size
;
10434 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
10436 return *(bfd_boolean
*) okp
= FALSE
;
10437 bed
= get_elf_backend_data (sec
->owner
);
10438 log_file_align
= bed
->s
->log_file_align
;
10440 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10442 for (rel
= relstart
; rel
< relend
; ++rel
)
10443 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
10445 /* If the entry is in use, do nothing. */
10446 if (h
->vtable
->used
10447 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
10449 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
10450 if (h
->vtable
->used
[entry
])
10453 /* Otherwise, kill it. */
10454 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
10460 /* Mark sections containing dynamically referenced symbols. When
10461 building shared libraries, we must assume that any visible symbol is
10465 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
10467 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
10469 if (h
->root
.type
== bfd_link_hash_warning
)
10470 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10472 if ((h
->root
.type
== bfd_link_hash_defined
10473 || h
->root
.type
== bfd_link_hash_defweak
)
10475 || (!info
->executable
10477 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
10478 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
10479 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
10484 /* Do mark and sweep of unused sections. */
10487 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
10489 bfd_boolean ok
= TRUE
;
10491 elf_gc_mark_hook_fn gc_mark_hook
;
10492 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10494 if (!bed
->can_gc_sections
10495 || info
->relocatable
10496 || info
->emitrelocations
10497 || !is_elf_hash_table (info
->hash
))
10499 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
10503 /* Apply transitive closure to the vtable entry usage info. */
10504 elf_link_hash_traverse (elf_hash_table (info
),
10505 elf_gc_propagate_vtable_entries_used
,
10510 /* Kill the vtable relocations that were not used. */
10511 elf_link_hash_traverse (elf_hash_table (info
),
10512 elf_gc_smash_unused_vtentry_relocs
,
10517 /* Mark dynamically referenced symbols. */
10518 if (elf_hash_table (info
)->dynamic_sections_created
)
10519 elf_link_hash_traverse (elf_hash_table (info
),
10520 bed
->gc_mark_dynamic_ref
,
10523 /* Grovel through relocs to find out who stays ... */
10524 gc_mark_hook
= bed
->gc_mark_hook
;
10525 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10529 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10532 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10533 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
10534 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10538 /* Allow the backend to mark additional target specific sections. */
10539 if (bed
->gc_mark_extra_sections
)
10540 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
10542 /* ... again for sections marked from eh_frame. */
10543 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10547 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10550 /* Keep .gcc_except_table.* if the associated .text.* (or the
10551 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10552 marked. This isn't very nice, but the proper solution,
10553 splitting .eh_frame up and using comdat doesn't pan out
10554 easily due to needing special relocs to handle the
10555 difference of two symbols in separate sections.
10556 Don't keep code sections referenced by .eh_frame. */
10557 #define TEXT_PREFIX ".text."
10558 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10559 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10560 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10561 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
10563 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
10566 const char *sec_name
;
10568 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
10570 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
10571 sec_name
= o
->name
+ o_name_prefix_len
;
10572 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
10573 tmp
= strlen (TEXT_PREFIX2
);
10574 if (tmp
> fn_name_prefix_len
)
10575 fn_name_prefix_len
= tmp
;
10577 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
10578 if (fn_name
== NULL
)
10581 /* Try the first prefix. */
10582 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
10583 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10585 /* Try the second prefix. */
10586 if (fn_text
== NULL
)
10588 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
10589 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10593 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
10597 /* If not using specially named exception table section,
10598 then keep whatever we are using. */
10599 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10604 /* ... and mark SEC_EXCLUDE for those that go. */
10605 return elf_gc_sweep (abfd
, info
);
10608 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10611 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
10613 struct elf_link_hash_entry
*h
,
10616 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
10617 struct elf_link_hash_entry
**search
, *child
;
10618 bfd_size_type extsymcount
;
10619 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10621 /* The sh_info field of the symtab header tells us where the
10622 external symbols start. We don't care about the local symbols at
10624 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
10625 if (!elf_bad_symtab (abfd
))
10626 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
10628 sym_hashes
= elf_sym_hashes (abfd
);
10629 sym_hashes_end
= sym_hashes
+ extsymcount
;
10631 /* Hunt down the child symbol, which is in this section at the same
10632 offset as the relocation. */
10633 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
10635 if ((child
= *search
) != NULL
10636 && (child
->root
.type
== bfd_link_hash_defined
10637 || child
->root
.type
== bfd_link_hash_defweak
)
10638 && child
->root
.u
.def
.section
== sec
10639 && child
->root
.u
.def
.value
== offset
)
10643 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
10644 abfd
, sec
, (unsigned long) offset
);
10645 bfd_set_error (bfd_error_invalid_operation
);
10649 if (!child
->vtable
)
10651 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
10652 if (!child
->vtable
)
10657 /* This *should* only be the absolute section. It could potentially
10658 be that someone has defined a non-global vtable though, which
10659 would be bad. It isn't worth paging in the local symbols to be
10660 sure though; that case should simply be handled by the assembler. */
10662 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
10665 child
->vtable
->parent
= h
;
10670 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10673 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
10674 asection
*sec ATTRIBUTE_UNUSED
,
10675 struct elf_link_hash_entry
*h
,
10678 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10679 unsigned int log_file_align
= bed
->s
->log_file_align
;
10683 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
10688 if (addend
>= h
->vtable
->size
)
10690 size_t size
, bytes
, file_align
;
10691 bfd_boolean
*ptr
= h
->vtable
->used
;
10693 /* While the symbol is undefined, we have to be prepared to handle
10695 file_align
= 1 << log_file_align
;
10696 if (h
->root
.type
== bfd_link_hash_undefined
)
10697 size
= addend
+ file_align
;
10701 if (addend
>= size
)
10703 /* Oops! We've got a reference past the defined end of
10704 the table. This is probably a bug -- shall we warn? */
10705 size
= addend
+ file_align
;
10708 size
= (size
+ file_align
- 1) & -file_align
;
10710 /* Allocate one extra entry for use as a "done" flag for the
10711 consolidation pass. */
10712 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
10716 ptr
= bfd_realloc (ptr
- 1, bytes
);
10722 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
10723 * sizeof (bfd_boolean
));
10724 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
10728 ptr
= bfd_zmalloc (bytes
);
10733 /* And arrange for that done flag to be at index -1. */
10734 h
->vtable
->used
= ptr
+ 1;
10735 h
->vtable
->size
= size
;
10738 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
10743 struct alloc_got_off_arg
{
10745 unsigned int got_elt_size
;
10748 /* We need a special top-level link routine to convert got reference counts
10749 to real got offsets. */
10752 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
10754 struct alloc_got_off_arg
*gofarg
= arg
;
10756 if (h
->root
.type
== bfd_link_hash_warning
)
10757 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10759 if (h
->got
.refcount
> 0)
10761 h
->got
.offset
= gofarg
->gotoff
;
10762 gofarg
->gotoff
+= gofarg
->got_elt_size
;
10765 h
->got
.offset
= (bfd_vma
) -1;
10770 /* And an accompanying bit to work out final got entry offsets once
10771 we're done. Should be called from final_link. */
10774 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
10775 struct bfd_link_info
*info
)
10778 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10780 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
10781 struct alloc_got_off_arg gofarg
;
10783 if (! is_elf_hash_table (info
->hash
))
10786 /* The GOT offset is relative to the .got section, but the GOT header is
10787 put into the .got.plt section, if the backend uses it. */
10788 if (bed
->want_got_plt
)
10791 gotoff
= bed
->got_header_size
;
10793 /* Do the local .got entries first. */
10794 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
10796 bfd_signed_vma
*local_got
;
10797 bfd_size_type j
, locsymcount
;
10798 Elf_Internal_Shdr
*symtab_hdr
;
10800 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
10803 local_got
= elf_local_got_refcounts (i
);
10807 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
10808 if (elf_bad_symtab (i
))
10809 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10811 locsymcount
= symtab_hdr
->sh_info
;
10813 for (j
= 0; j
< locsymcount
; ++j
)
10815 if (local_got
[j
] > 0)
10817 local_got
[j
] = gotoff
;
10818 gotoff
+= got_elt_size
;
10821 local_got
[j
] = (bfd_vma
) -1;
10825 /* Then the global .got entries. .plt refcounts are handled by
10826 adjust_dynamic_symbol */
10827 gofarg
.gotoff
= gotoff
;
10828 gofarg
.got_elt_size
= got_elt_size
;
10829 elf_link_hash_traverse (elf_hash_table (info
),
10830 elf_gc_allocate_got_offsets
,
10835 /* Many folk need no more in the way of final link than this, once
10836 got entry reference counting is enabled. */
10839 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10841 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
10844 /* Invoke the regular ELF backend linker to do all the work. */
10845 return bfd_elf_final_link (abfd
, info
);
10849 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
10851 struct elf_reloc_cookie
*rcookie
= cookie
;
10853 if (rcookie
->bad_symtab
)
10854 rcookie
->rel
= rcookie
->rels
;
10856 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
10858 unsigned long r_symndx
;
10860 if (! rcookie
->bad_symtab
)
10861 if (rcookie
->rel
->r_offset
> offset
)
10863 if (rcookie
->rel
->r_offset
!= offset
)
10866 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
10867 if (r_symndx
== SHN_UNDEF
)
10870 if (r_symndx
>= rcookie
->locsymcount
10871 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
10873 struct elf_link_hash_entry
*h
;
10875 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
10877 while (h
->root
.type
== bfd_link_hash_indirect
10878 || h
->root
.type
== bfd_link_hash_warning
)
10879 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10881 if ((h
->root
.type
== bfd_link_hash_defined
10882 || h
->root
.type
== bfd_link_hash_defweak
)
10883 && elf_discarded_section (h
->root
.u
.def
.section
))
10890 /* It's not a relocation against a global symbol,
10891 but it could be a relocation against a local
10892 symbol for a discarded section. */
10894 Elf_Internal_Sym
*isym
;
10896 /* Need to: get the symbol; get the section. */
10897 isym
= &rcookie
->locsyms
[r_symndx
];
10898 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
10900 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
10901 if (isec
!= NULL
&& elf_discarded_section (isec
))
10910 /* Discard unneeded references to discarded sections.
10911 Returns TRUE if any section's size was changed. */
10912 /* This function assumes that the relocations are in sorted order,
10913 which is true for all known assemblers. */
10916 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
10918 struct elf_reloc_cookie cookie
;
10919 asection
*stab
, *eh
;
10920 Elf_Internal_Shdr
*symtab_hdr
;
10921 const struct elf_backend_data
*bed
;
10923 unsigned int count
;
10924 bfd_boolean ret
= FALSE
;
10926 if (info
->traditional_format
10927 || !is_elf_hash_table (info
->hash
))
10930 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10932 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10935 bed
= get_elf_backend_data (abfd
);
10937 if ((abfd
->flags
& DYNAMIC
) != 0)
10941 if (!info
->relocatable
)
10943 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10946 || bfd_is_abs_section (eh
->output_section
)))
10950 stab
= bfd_get_section_by_name (abfd
, ".stab");
10952 && (stab
->size
== 0
10953 || bfd_is_abs_section (stab
->output_section
)
10954 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10959 && bed
->elf_backend_discard_info
== NULL
)
10962 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10963 cookie
.abfd
= abfd
;
10964 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10965 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10966 if (cookie
.bad_symtab
)
10968 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10969 cookie
.extsymoff
= 0;
10973 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10974 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10977 if (bed
->s
->arch_size
== 32)
10978 cookie
.r_sym_shift
= 8;
10980 cookie
.r_sym_shift
= 32;
10982 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10983 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10985 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10986 cookie
.locsymcount
, 0,
10988 if (cookie
.locsyms
== NULL
)
10994 cookie
.rels
= NULL
;
10995 count
= stab
->reloc_count
;
10997 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
10998 info
->keep_memory
);
10999 if (cookie
.rels
!= NULL
)
11001 cookie
.rel
= cookie
.rels
;
11002 cookie
.relend
= cookie
.rels
;
11003 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11004 if (_bfd_discard_section_stabs (abfd
, stab
,
11005 elf_section_data (stab
)->sec_info
,
11006 bfd_elf_reloc_symbol_deleted_p
,
11009 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
11010 free (cookie
.rels
);
11016 cookie
.rels
= NULL
;
11017 count
= eh
->reloc_count
;
11019 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
11020 info
->keep_memory
);
11021 cookie
.rel
= cookie
.rels
;
11022 cookie
.relend
= cookie
.rels
;
11023 if (cookie
.rels
!= NULL
)
11024 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11026 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11027 bfd_elf_reloc_symbol_deleted_p
,
11031 if (cookie
.rels
!= NULL
11032 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
11033 free (cookie
.rels
);
11036 if (bed
->elf_backend_discard_info
!= NULL
11037 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11040 if (cookie
.locsyms
!= NULL
11041 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
11043 if (! info
->keep_memory
)
11044 free (cookie
.locsyms
);
11046 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
11050 if (info
->eh_frame_hdr
11051 && !info
->relocatable
11052 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
11059 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
11060 struct bfd_link_info
*info
)
11063 const char *name
, *p
;
11064 struct bfd_section_already_linked
*l
;
11065 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
11067 if (sec
->output_section
== bfd_abs_section_ptr
)
11070 flags
= sec
->flags
;
11072 /* Return if it isn't a linkonce section. A comdat group section
11073 also has SEC_LINK_ONCE set. */
11074 if ((flags
& SEC_LINK_ONCE
) == 0)
11077 /* Don't put group member sections on our list of already linked
11078 sections. They are handled as a group via their group section. */
11079 if (elf_sec_group (sec
) != NULL
)
11082 /* FIXME: When doing a relocatable link, we may have trouble
11083 copying relocations in other sections that refer to local symbols
11084 in the section being discarded. Those relocations will have to
11085 be converted somehow; as of this writing I'm not sure that any of
11086 the backends handle that correctly.
11088 It is tempting to instead not discard link once sections when
11089 doing a relocatable link (technically, they should be discarded
11090 whenever we are building constructors). However, that fails,
11091 because the linker winds up combining all the link once sections
11092 into a single large link once section, which defeats the purpose
11093 of having link once sections in the first place.
11095 Also, not merging link once sections in a relocatable link
11096 causes trouble for MIPS ELF, which relies on link once semantics
11097 to handle the .reginfo section correctly. */
11099 name
= bfd_get_section_name (abfd
, sec
);
11101 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
11102 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
11107 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
11109 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11111 /* We may have 2 different types of sections on the list: group
11112 sections and linkonce sections. Match like sections. */
11113 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
11114 && strcmp (name
, l
->sec
->name
) == 0
11115 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
11117 /* The section has already been linked. See if we should
11118 issue a warning. */
11119 switch (flags
& SEC_LINK_DUPLICATES
)
11124 case SEC_LINK_DUPLICATES_DISCARD
:
11127 case SEC_LINK_DUPLICATES_ONE_ONLY
:
11128 (*_bfd_error_handler
)
11129 (_("%B: ignoring duplicate section `%A'"),
11133 case SEC_LINK_DUPLICATES_SAME_SIZE
:
11134 if (sec
->size
!= l
->sec
->size
)
11135 (*_bfd_error_handler
)
11136 (_("%B: duplicate section `%A' has different size"),
11140 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
11141 if (sec
->size
!= l
->sec
->size
)
11142 (*_bfd_error_handler
)
11143 (_("%B: duplicate section `%A' has different size"),
11145 else if (sec
->size
!= 0)
11147 bfd_byte
*sec_contents
, *l_sec_contents
;
11149 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
11150 (*_bfd_error_handler
)
11151 (_("%B: warning: could not read contents of section `%A'"),
11153 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
11155 (*_bfd_error_handler
)
11156 (_("%B: warning: could not read contents of section `%A'"),
11157 l
->sec
->owner
, l
->sec
);
11158 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
11159 (*_bfd_error_handler
)
11160 (_("%B: warning: duplicate section `%A' has different contents"),
11164 free (sec_contents
);
11165 if (l_sec_contents
)
11166 free (l_sec_contents
);
11171 /* Set the output_section field so that lang_add_section
11172 does not create a lang_input_section structure for this
11173 section. Since there might be a symbol in the section
11174 being discarded, we must retain a pointer to the section
11175 which we are really going to use. */
11176 sec
->output_section
= bfd_abs_section_ptr
;
11177 sec
->kept_section
= l
->sec
;
11179 if (flags
& SEC_GROUP
)
11181 asection
*first
= elf_next_in_group (sec
);
11182 asection
*s
= first
;
11186 s
->output_section
= bfd_abs_section_ptr
;
11187 /* Record which group discards it. */
11188 s
->kept_section
= l
->sec
;
11189 s
= elf_next_in_group (s
);
11190 /* These lists are circular. */
11200 /* A single member comdat group section may be discarded by a
11201 linkonce section and vice versa. */
11203 if ((flags
& SEC_GROUP
) != 0)
11205 asection
*first
= elf_next_in_group (sec
);
11207 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
11208 /* Check this single member group against linkonce sections. */
11209 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11210 if ((l
->sec
->flags
& SEC_GROUP
) == 0
11211 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
11212 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
11214 first
->output_section
= bfd_abs_section_ptr
;
11215 first
->kept_section
= l
->sec
;
11216 sec
->output_section
= bfd_abs_section_ptr
;
11221 /* Check this linkonce section against single member groups. */
11222 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11223 if (l
->sec
->flags
& SEC_GROUP
)
11225 asection
*first
= elf_next_in_group (l
->sec
);
11228 && elf_next_in_group (first
) == first
11229 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
11231 sec
->output_section
= bfd_abs_section_ptr
;
11232 sec
->kept_section
= first
;
11237 /* This is the first section with this name. Record it. */
11238 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
11242 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
11244 return sym
->st_shndx
== SHN_COMMON
;
11248 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
11254 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
11256 return bfd_com_section_ptr
;