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 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5526 if (elf_section_data (o
)->this_hdr
.sh_type
5527 == SHT_PREINIT_ARRAY
)
5529 (*_bfd_error_handler
)
5530 (_("%B: .preinit_array section is not allowed in DSO"),
5535 bfd_set_error (bfd_error_nonrepresentable_section
);
5539 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5540 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5543 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5544 if (s
!= NULL
&& s
->linker_has_input
)
5546 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5547 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5550 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5551 if (s
!= NULL
&& s
->linker_has_input
)
5553 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5554 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5558 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5559 /* If .dynstr is excluded from the link, we don't want any of
5560 these tags. Strictly, we should be checking each section
5561 individually; This quick check covers for the case where
5562 someone does a /DISCARD/ : { *(*) }. */
5563 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5565 bfd_size_type strsize
;
5567 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5568 if ((info
->emit_hash
5569 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5570 || (info
->emit_gnu_hash
5571 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5572 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5573 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5574 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5575 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5576 bed
->s
->sizeof_sym
))
5581 /* The backend must work out the sizes of all the other dynamic
5583 if (bed
->elf_backend_size_dynamic_sections
5584 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5587 if (elf_hash_table (info
)->dynamic_sections_created
)
5589 unsigned long section_sym_count
;
5592 /* Set up the version definition section. */
5593 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5594 BFD_ASSERT (s
!= NULL
);
5596 /* We may have created additional version definitions if we are
5597 just linking a regular application. */
5598 verdefs
= asvinfo
.verdefs
;
5600 /* Skip anonymous version tag. */
5601 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5602 verdefs
= verdefs
->next
;
5604 if (verdefs
== NULL
&& !info
->create_default_symver
)
5605 s
->flags
|= SEC_EXCLUDE
;
5610 struct bfd_elf_version_tree
*t
;
5612 Elf_Internal_Verdef def
;
5613 Elf_Internal_Verdaux defaux
;
5614 struct bfd_link_hash_entry
*bh
;
5615 struct elf_link_hash_entry
*h
;
5621 /* Make space for the base version. */
5622 size
+= sizeof (Elf_External_Verdef
);
5623 size
+= sizeof (Elf_External_Verdaux
);
5626 /* Make space for the default version. */
5627 if (info
->create_default_symver
)
5629 size
+= sizeof (Elf_External_Verdef
);
5633 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5635 struct bfd_elf_version_deps
*n
;
5637 size
+= sizeof (Elf_External_Verdef
);
5638 size
+= sizeof (Elf_External_Verdaux
);
5641 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5642 size
+= sizeof (Elf_External_Verdaux
);
5646 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5647 if (s
->contents
== NULL
&& s
->size
!= 0)
5650 /* Fill in the version definition section. */
5654 def
.vd_version
= VER_DEF_CURRENT
;
5655 def
.vd_flags
= VER_FLG_BASE
;
5658 if (info
->create_default_symver
)
5660 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5661 def
.vd_next
= sizeof (Elf_External_Verdef
);
5665 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5666 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5667 + sizeof (Elf_External_Verdaux
));
5670 if (soname_indx
!= (bfd_size_type
) -1)
5672 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5674 def
.vd_hash
= bfd_elf_hash (soname
);
5675 defaux
.vda_name
= soname_indx
;
5682 name
= lbasename (output_bfd
->filename
);
5683 def
.vd_hash
= bfd_elf_hash (name
);
5684 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5686 if (indx
== (bfd_size_type
) -1)
5688 defaux
.vda_name
= indx
;
5690 defaux
.vda_next
= 0;
5692 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5693 (Elf_External_Verdef
*) p
);
5694 p
+= sizeof (Elf_External_Verdef
);
5695 if (info
->create_default_symver
)
5697 /* Add a symbol representing this version. */
5699 if (! (_bfd_generic_link_add_one_symbol
5700 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5702 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5704 h
= (struct elf_link_hash_entry
*) bh
;
5707 h
->type
= STT_OBJECT
;
5708 h
->verinfo
.vertree
= NULL
;
5710 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5713 /* Create a duplicate of the base version with the same
5714 aux block, but different flags. */
5717 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5719 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5720 + sizeof (Elf_External_Verdaux
));
5723 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5724 (Elf_External_Verdef
*) p
);
5725 p
+= sizeof (Elf_External_Verdef
);
5727 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5728 (Elf_External_Verdaux
*) p
);
5729 p
+= sizeof (Elf_External_Verdaux
);
5731 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5734 struct bfd_elf_version_deps
*n
;
5737 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5740 /* Add a symbol representing this version. */
5742 if (! (_bfd_generic_link_add_one_symbol
5743 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5745 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5747 h
= (struct elf_link_hash_entry
*) bh
;
5750 h
->type
= STT_OBJECT
;
5751 h
->verinfo
.vertree
= t
;
5753 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5756 def
.vd_version
= VER_DEF_CURRENT
;
5758 if (t
->globals
.list
== NULL
5759 && t
->locals
.list
== NULL
5761 def
.vd_flags
|= VER_FLG_WEAK
;
5762 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5763 def
.vd_cnt
= cdeps
+ 1;
5764 def
.vd_hash
= bfd_elf_hash (t
->name
);
5765 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5767 if (t
->next
!= NULL
)
5768 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5769 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5771 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5772 (Elf_External_Verdef
*) p
);
5773 p
+= sizeof (Elf_External_Verdef
);
5775 defaux
.vda_name
= h
->dynstr_index
;
5776 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5778 defaux
.vda_next
= 0;
5779 if (t
->deps
!= NULL
)
5780 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5781 t
->name_indx
= defaux
.vda_name
;
5783 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5784 (Elf_External_Verdaux
*) p
);
5785 p
+= sizeof (Elf_External_Verdaux
);
5787 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5789 if (n
->version_needed
== NULL
)
5791 /* This can happen if there was an error in the
5793 defaux
.vda_name
= 0;
5797 defaux
.vda_name
= n
->version_needed
->name_indx
;
5798 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5801 if (n
->next
== NULL
)
5802 defaux
.vda_next
= 0;
5804 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5806 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5807 (Elf_External_Verdaux
*) p
);
5808 p
+= sizeof (Elf_External_Verdaux
);
5812 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5813 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5816 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5819 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5821 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5824 else if (info
->flags
& DF_BIND_NOW
)
5826 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5832 if (info
->executable
)
5833 info
->flags_1
&= ~ (DF_1_INITFIRST
5836 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5840 /* Work out the size of the version reference section. */
5842 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5843 BFD_ASSERT (s
!= NULL
);
5845 struct elf_find_verdep_info sinfo
;
5847 sinfo
.output_bfd
= output_bfd
;
5849 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5850 if (sinfo
.vers
== 0)
5852 sinfo
.failed
= FALSE
;
5854 elf_link_hash_traverse (elf_hash_table (info
),
5855 _bfd_elf_link_find_version_dependencies
,
5858 if (elf_tdata (output_bfd
)->verref
== NULL
)
5859 s
->flags
|= SEC_EXCLUDE
;
5862 Elf_Internal_Verneed
*t
;
5867 /* Build the version definition section. */
5870 for (t
= elf_tdata (output_bfd
)->verref
;
5874 Elf_Internal_Vernaux
*a
;
5876 size
+= sizeof (Elf_External_Verneed
);
5878 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5879 size
+= sizeof (Elf_External_Vernaux
);
5883 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5884 if (s
->contents
== NULL
)
5888 for (t
= elf_tdata (output_bfd
)->verref
;
5893 Elf_Internal_Vernaux
*a
;
5897 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5900 t
->vn_version
= VER_NEED_CURRENT
;
5902 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5903 elf_dt_name (t
->vn_bfd
) != NULL
5904 ? elf_dt_name (t
->vn_bfd
)
5905 : lbasename (t
->vn_bfd
->filename
),
5907 if (indx
== (bfd_size_type
) -1)
5910 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5911 if (t
->vn_nextref
== NULL
)
5914 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5915 + caux
* sizeof (Elf_External_Vernaux
));
5917 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5918 (Elf_External_Verneed
*) p
);
5919 p
+= sizeof (Elf_External_Verneed
);
5921 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5923 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5924 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5925 a
->vna_nodename
, FALSE
);
5926 if (indx
== (bfd_size_type
) -1)
5929 if (a
->vna_nextptr
== NULL
)
5932 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5934 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5935 (Elf_External_Vernaux
*) p
);
5936 p
+= sizeof (Elf_External_Vernaux
);
5940 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5941 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5944 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5948 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5949 && elf_tdata (output_bfd
)->cverdefs
== 0)
5950 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5951 §ion_sym_count
) == 0)
5953 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5954 s
->flags
|= SEC_EXCLUDE
;
5960 /* Find the first non-excluded output section. We'll use its
5961 section symbol for some emitted relocs. */
5963 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
5967 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5968 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
5969 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5971 elf_hash_table (info
)->text_index_section
= s
;
5976 /* Find two non-excluded output sections, one for code, one for data.
5977 We'll use their section symbols for some emitted relocs. */
5979 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5983 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5984 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
5985 == (SEC_ALLOC
| SEC_READONLY
))
5986 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5988 elf_hash_table (info
)->text_index_section
= s
;
5992 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5993 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
5994 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
5996 elf_hash_table (info
)->data_index_section
= s
;
6000 if (elf_hash_table (info
)->text_index_section
== NULL
)
6001 elf_hash_table (info
)->text_index_section
6002 = elf_hash_table (info
)->data_index_section
;
6006 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6008 const struct elf_backend_data
*bed
;
6010 if (!is_elf_hash_table (info
->hash
))
6013 bed
= get_elf_backend_data (output_bfd
);
6014 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6016 if (elf_hash_table (info
)->dynamic_sections_created
)
6020 bfd_size_type dynsymcount
;
6021 unsigned long section_sym_count
;
6022 unsigned int dtagcount
;
6024 dynobj
= elf_hash_table (info
)->dynobj
;
6026 /* Assign dynsym indicies. In a shared library we generate a
6027 section symbol for each output section, which come first.
6028 Next come all of the back-end allocated local dynamic syms,
6029 followed by the rest of the global symbols. */
6031 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6032 §ion_sym_count
);
6034 /* Work out the size of the symbol version section. */
6035 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6036 BFD_ASSERT (s
!= NULL
);
6037 if (dynsymcount
!= 0
6038 && (s
->flags
& SEC_EXCLUDE
) == 0)
6040 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6041 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6042 if (s
->contents
== NULL
)
6045 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6049 /* Set the size of the .dynsym and .hash sections. We counted
6050 the number of dynamic symbols in elf_link_add_object_symbols.
6051 We will build the contents of .dynsym and .hash when we build
6052 the final symbol table, because until then we do not know the
6053 correct value to give the symbols. We built the .dynstr
6054 section as we went along in elf_link_add_object_symbols. */
6055 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6056 BFD_ASSERT (s
!= NULL
);
6057 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6059 if (dynsymcount
!= 0)
6061 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6062 if (s
->contents
== NULL
)
6065 /* The first entry in .dynsym is a dummy symbol.
6066 Clear all the section syms, in case we don't output them all. */
6067 ++section_sym_count
;
6068 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6071 elf_hash_table (info
)->bucketcount
= 0;
6073 /* Compute the size of the hashing table. As a side effect this
6074 computes the hash values for all the names we export. */
6075 if (info
->emit_hash
)
6077 unsigned long int *hashcodes
;
6078 unsigned long int *hashcodesp
;
6080 unsigned long int nsyms
;
6082 size_t hash_entry_size
;
6084 /* Compute the hash values for all exported symbols. At the same
6085 time store the values in an array so that we could use them for
6087 amt
= dynsymcount
* sizeof (unsigned long int);
6088 hashcodes
= bfd_malloc (amt
);
6089 if (hashcodes
== NULL
)
6091 hashcodesp
= hashcodes
;
6093 /* Put all hash values in HASHCODES. */
6094 elf_link_hash_traverse (elf_hash_table (info
),
6095 elf_collect_hash_codes
, &hashcodesp
);
6097 nsyms
= hashcodesp
- hashcodes
;
6099 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6102 if (bucketcount
== 0)
6105 elf_hash_table (info
)->bucketcount
= bucketcount
;
6107 s
= bfd_get_section_by_name (dynobj
, ".hash");
6108 BFD_ASSERT (s
!= NULL
);
6109 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6110 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6111 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6112 if (s
->contents
== NULL
)
6115 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6116 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6117 s
->contents
+ hash_entry_size
);
6120 if (info
->emit_gnu_hash
)
6123 unsigned char *contents
;
6124 struct collect_gnu_hash_codes cinfo
;
6128 memset (&cinfo
, 0, sizeof (cinfo
));
6130 /* Compute the hash values for all exported symbols. At the same
6131 time store the values in an array so that we could use them for
6133 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6134 cinfo
.hashcodes
= bfd_malloc (amt
);
6135 if (cinfo
.hashcodes
== NULL
)
6138 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6139 cinfo
.min_dynindx
= -1;
6140 cinfo
.output_bfd
= output_bfd
;
6143 /* Put all hash values in HASHCODES. */
6144 elf_link_hash_traverse (elf_hash_table (info
),
6145 elf_collect_gnu_hash_codes
, &cinfo
);
6148 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6150 if (bucketcount
== 0)
6152 free (cinfo
.hashcodes
);
6156 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6157 BFD_ASSERT (s
!= NULL
);
6159 if (cinfo
.nsyms
== 0)
6161 /* Empty .gnu.hash section is special. */
6162 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6163 free (cinfo
.hashcodes
);
6164 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6165 contents
= bfd_zalloc (output_bfd
, s
->size
);
6166 if (contents
== NULL
)
6168 s
->contents
= contents
;
6169 /* 1 empty bucket. */
6170 bfd_put_32 (output_bfd
, 1, contents
);
6171 /* SYMIDX above the special symbol 0. */
6172 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6173 /* Just one word for bitmask. */
6174 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6175 /* Only hash fn bloom filter. */
6176 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6177 /* No hashes are valid - empty bitmask. */
6178 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6179 /* No hashes in the only bucket. */
6180 bfd_put_32 (output_bfd
, 0,
6181 contents
+ 16 + bed
->s
->arch_size
/ 8);
6185 unsigned long int maskwords
, maskbitslog2
;
6186 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6188 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6189 if (maskbitslog2
< 3)
6191 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6192 maskbitslog2
= maskbitslog2
+ 3;
6194 maskbitslog2
= maskbitslog2
+ 2;
6195 if (bed
->s
->arch_size
== 64)
6197 if (maskbitslog2
== 5)
6203 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6204 cinfo
.shift2
= maskbitslog2
;
6205 cinfo
.maskbits
= 1 << maskbitslog2
;
6206 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6207 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6208 amt
+= maskwords
* sizeof (bfd_vma
);
6209 cinfo
.bitmask
= bfd_malloc (amt
);
6210 if (cinfo
.bitmask
== NULL
)
6212 free (cinfo
.hashcodes
);
6216 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6217 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6218 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6219 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6221 /* Determine how often each hash bucket is used. */
6222 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6223 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6224 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6226 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6227 if (cinfo
.counts
[i
] != 0)
6229 cinfo
.indx
[i
] = cnt
;
6230 cnt
+= cinfo
.counts
[i
];
6232 BFD_ASSERT (cnt
== dynsymcount
);
6233 cinfo
.bucketcount
= bucketcount
;
6234 cinfo
.local_indx
= cinfo
.min_dynindx
;
6236 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6237 s
->size
+= cinfo
.maskbits
/ 8;
6238 contents
= bfd_zalloc (output_bfd
, s
->size
);
6239 if (contents
== NULL
)
6241 free (cinfo
.bitmask
);
6242 free (cinfo
.hashcodes
);
6246 s
->contents
= contents
;
6247 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6248 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6249 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6250 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6251 contents
+= 16 + cinfo
.maskbits
/ 8;
6253 for (i
= 0; i
< bucketcount
; ++i
)
6255 if (cinfo
.counts
[i
] == 0)
6256 bfd_put_32 (output_bfd
, 0, contents
);
6258 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6262 cinfo
.contents
= contents
;
6264 /* Renumber dynamic symbols, populate .gnu.hash section. */
6265 elf_link_hash_traverse (elf_hash_table (info
),
6266 elf_renumber_gnu_hash_syms
, &cinfo
);
6268 contents
= s
->contents
+ 16;
6269 for (i
= 0; i
< maskwords
; ++i
)
6271 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6273 contents
+= bed
->s
->arch_size
/ 8;
6276 free (cinfo
.bitmask
);
6277 free (cinfo
.hashcodes
);
6281 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6282 BFD_ASSERT (s
!= NULL
);
6284 elf_finalize_dynstr (output_bfd
, info
);
6286 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6288 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6289 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6296 /* Final phase of ELF linker. */
6298 /* A structure we use to avoid passing large numbers of arguments. */
6300 struct elf_final_link_info
6302 /* General link information. */
6303 struct bfd_link_info
*info
;
6306 /* Symbol string table. */
6307 struct bfd_strtab_hash
*symstrtab
;
6308 /* .dynsym section. */
6309 asection
*dynsym_sec
;
6310 /* .hash section. */
6312 /* symbol version section (.gnu.version). */
6313 asection
*symver_sec
;
6314 /* Buffer large enough to hold contents of any section. */
6316 /* Buffer large enough to hold external relocs of any section. */
6317 void *external_relocs
;
6318 /* Buffer large enough to hold internal relocs of any section. */
6319 Elf_Internal_Rela
*internal_relocs
;
6320 /* Buffer large enough to hold external local symbols of any input
6322 bfd_byte
*external_syms
;
6323 /* And a buffer for symbol section indices. */
6324 Elf_External_Sym_Shndx
*locsym_shndx
;
6325 /* Buffer large enough to hold internal local symbols of any input
6327 Elf_Internal_Sym
*internal_syms
;
6328 /* Array large enough to hold a symbol index for each local symbol
6329 of any input BFD. */
6331 /* Array large enough to hold a section pointer for each local
6332 symbol of any input BFD. */
6333 asection
**sections
;
6334 /* Buffer to hold swapped out symbols. */
6336 /* And one for symbol section indices. */
6337 Elf_External_Sym_Shndx
*symshndxbuf
;
6338 /* Number of swapped out symbols in buffer. */
6339 size_t symbuf_count
;
6340 /* Number of symbols which fit in symbuf. */
6342 /* And same for symshndxbuf. */
6343 size_t shndxbuf_size
;
6346 /* This struct is used to pass information to elf_link_output_extsym. */
6348 struct elf_outext_info
6351 bfd_boolean localsyms
;
6352 struct elf_final_link_info
*finfo
;
6356 /* Support for evaluating a complex relocation.
6358 Complex relocations are generalized, self-describing relocations. The
6359 implementation of them consists of two parts: complex symbols, and the
6360 relocations themselves.
6362 The relocations are use a reserved elf-wide relocation type code (R_RELC
6363 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6364 information (start bit, end bit, word width, etc) into the addend. This
6365 information is extracted from CGEN-generated operand tables within gas.
6367 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6368 internal) representing prefix-notation expressions, including but not
6369 limited to those sorts of expressions normally encoded as addends in the
6370 addend field. The symbol mangling format is:
6373 | <unary-operator> ':' <node>
6374 | <binary-operator> ':' <node> ':' <node>
6377 <literal> := 's' <digits=N> ':' <N character symbol name>
6378 | 'S' <digits=N> ':' <N character section name>
6382 <binary-operator> := as in C
6383 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6386 set_symbol_value (bfd
* bfd_with_globals
,
6387 struct elf_final_link_info
* finfo
,
6391 bfd_boolean is_local
;
6392 Elf_Internal_Sym
* sym
;
6393 struct elf_link_hash_entry
** sym_hashes
;
6394 struct elf_link_hash_entry
* h
;
6396 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
6397 sym
= finfo
->internal_syms
+ symidx
;
6398 is_local
= ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
;
6402 /* It is a local symbol: move it to the
6403 "absolute" section and give it a value. */
6404 sym
->st_shndx
= SHN_ABS
;
6405 sym
->st_value
= val
;
6409 /* It is a global symbol: set its link type
6410 to "defined" and give it a value. */
6411 h
= sym_hashes
[symidx
];
6412 while (h
->root
.type
== bfd_link_hash_indirect
6413 || h
->root
.type
== bfd_link_hash_warning
)
6414 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6415 h
->root
.type
= bfd_link_hash_defined
;
6416 h
->root
.u
.def
.value
= val
;
6417 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6422 resolve_symbol (const char * name
,
6424 struct elf_final_link_info
* finfo
,
6428 Elf_Internal_Sym
* sym
;
6429 struct bfd_link_hash_entry
* global_entry
;
6430 const char * candidate
= NULL
;
6431 Elf_Internal_Shdr
* symtab_hdr
;
6432 asection
* sec
= NULL
;
6435 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6437 for (i
= 0; i
< locsymcount
; ++ i
)
6439 sym
= finfo
->internal_syms
+ i
;
6440 sec
= finfo
->sections
[i
];
6442 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
6445 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
6446 symtab_hdr
->sh_link
,
6449 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6450 name
, candidate
, (unsigned int)sym
->st_value
);
6452 if (candidate
&& strcmp (candidate
, name
) == 0)
6454 * result
= sym
->st_value
;
6456 if (sym
->st_shndx
> SHN_UNDEF
&&
6457 sym
->st_shndx
< SHN_LORESERVE
)
6460 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6461 sec
->output_section
->name
,
6462 (unsigned int)sec
->output_section
->vma
,
6463 (unsigned int)sec
->output_offset
);
6465 * result
+= sec
->output_offset
+ sec
->output_section
->vma
;
6468 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result
);
6474 /* Hmm, haven't found it yet. perhaps it is a global. */
6475 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
, FALSE
, FALSE
, TRUE
);
6479 if (global_entry
->type
== bfd_link_hash_defined
6480 || global_entry
->type
== bfd_link_hash_defweak
)
6482 * result
= global_entry
->u
.def
.value
6483 + global_entry
->u
.def
.section
->output_section
->vma
6484 + global_entry
->u
.def
.section
->output_offset
;
6486 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6487 global_entry
->root
.string
, (unsigned int)*result
);
6492 if (global_entry
->type
== bfd_link_hash_common
)
6494 *result
= global_entry
->u
.def
.value
+
6495 bfd_com_section_ptr
->output_section
->vma
+
6496 bfd_com_section_ptr
->output_offset
;
6498 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6499 global_entry
->root
.string
, (unsigned int)*result
);
6508 resolve_section (const char * name
,
6509 asection
* sections
,
6515 for (curr
= sections
; curr
; curr
= curr
->next
)
6516 if (strcmp (curr
->name
, name
) == 0)
6518 *result
= curr
->vma
;
6522 /* Hmm. still haven't found it. try pseudo-section names. */
6523 for (curr
= sections
; curr
; curr
= curr
->next
)
6525 len
= strlen (curr
->name
);
6526 if (len
> strlen (name
))
6529 if (strncmp (curr
->name
, name
, len
) == 0)
6531 if (strncmp (".end", name
+ len
, 4) == 0)
6533 *result
= curr
->vma
+ curr
->size
;
6537 /* Insert more pseudo-section names here, if you like. */
6545 undefined_reference (const char * reftype
,
6548 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype
, name
);
6552 eval_symbol (bfd_vma
* result
,
6556 struct elf_final_link_info
* finfo
,
6558 bfd_vma section_offset
,
6566 const int bufsz
= 4096;
6567 char symbuf
[bufsz
];
6568 const char * symend
;
6569 bfd_boolean symbol_is_section
= FALSE
;
6574 if (len
< 1 || len
> bufsz
)
6576 bfd_set_error (bfd_error_invalid_operation
);
6583 * result
= addr
+ section_offset
;
6584 * advanced
= sym
+ 1;
6589 * result
= strtoul (sym
, advanced
, 16);
6593 symbol_is_section
= TRUE
;
6596 symlen
= strtol (sym
, &sym
, 10);
6597 ++ sym
; /* Skip the trailing ':'. */
6599 if ((symend
< sym
) || ((symlen
+ 1) > bufsz
))
6601 bfd_set_error (bfd_error_invalid_operation
);
6605 memcpy (symbuf
, sym
, symlen
);
6606 symbuf
[symlen
] = '\0';
6607 * advanced
= sym
+ symlen
;
6609 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6610 the symbol as a section, or vice-versa. so we're pretty liberal in our
6611 interpretation here; section means "try section first", not "must be a
6612 section", and likewise with symbol. */
6614 if (symbol_is_section
)
6616 if ((resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
) != TRUE
)
6617 && (resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
))
6619 undefined_reference ("section", symbuf
);
6625 if ((resolve_symbol (symbuf
, input_bfd
, finfo
, result
, locsymcount
) != TRUE
)
6626 && (resolve_section (symbuf
, finfo
->output_bfd
->sections
,
6629 undefined_reference ("symbol", symbuf
);
6636 /* All that remains are operators. */
6638 #define UNARY_OP(op) \
6639 if (strncmp (sym, #op, strlen (#op)) == 0) \
6641 sym += strlen (#op); \
6644 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6645 section_offset, locsymcount, signed_p) \
6649 * result = op ((signed)a); \
6656 #define BINARY_OP(op) \
6657 if (strncmp (sym, #op, strlen (#op)) == 0) \
6659 sym += strlen (#op); \
6662 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6663 section_offset, locsymcount, signed_p) \
6667 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6668 section_offset, locsymcount, signed_p) \
6672 * result = ((signed) a) op ((signed) b); \
6674 * result = a op b; \
6703 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
6704 bfd_set_error (bfd_error_invalid_operation
);
6709 /* Entry point to evaluator, called from elf_link_input_bfd. */
6712 evaluate_complex_relocation_symbols (bfd
* input_bfd
,
6713 struct elf_final_link_info
* finfo
,
6716 const struct elf_backend_data
* bed
;
6717 Elf_Internal_Shdr
* symtab_hdr
;
6718 struct elf_link_hash_entry
** sym_hashes
;
6719 asection
* reloc_sec
;
6720 bfd_boolean result
= TRUE
;
6722 /* For each section, we're going to check and see if it has any
6723 complex relocations, and we're going to evaluate any of them
6726 if (finfo
->info
->relocatable
)
6729 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6730 sym_hashes
= elf_sym_hashes (input_bfd
);
6731 bed
= get_elf_backend_data (input_bfd
);
6733 for (reloc_sec
= input_bfd
->sections
; reloc_sec
; reloc_sec
= reloc_sec
->next
)
6735 Elf_Internal_Rela
* internal_relocs
;
6738 /* This section was omitted from the link. */
6739 if (! reloc_sec
->linker_mark
)
6742 /* Only process sections containing relocs. */
6743 if ((reloc_sec
->flags
& SEC_RELOC
) == 0)
6746 if (reloc_sec
->reloc_count
== 0)
6749 /* Read in the relocs for this section. */
6751 = _bfd_elf_link_read_relocs (input_bfd
, reloc_sec
, NULL
,
6752 (Elf_Internal_Rela
*) NULL
,
6754 if (internal_relocs
== NULL
)
6757 for (i
= reloc_sec
->reloc_count
; i
--;)
6759 Elf_Internal_Rela
* rel
;
6762 Elf_Internal_Sym
* sym
;
6764 bfd_vma section_offset
;
6768 rel
= internal_relocs
+ i
;
6769 section_offset
= reloc_sec
->output_section
->vma
6770 + reloc_sec
->output_offset
;
6771 addr
= rel
->r_offset
;
6773 index
= ELF32_R_SYM (rel
->r_info
);
6774 if (bed
->s
->arch_size
== 64)
6777 if (index
== STN_UNDEF
)
6780 if (index
< locsymcount
)
6782 /* The symbol is local. */
6783 sym
= finfo
->internal_syms
+ index
;
6785 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6786 if ((ELF_ST_TYPE (sym
->st_info
) != STT_RELC
) &&
6787 (ELF_ST_TYPE (sym
->st_info
) != STT_SRELC
))
6790 sym_name
= bfd_elf_string_from_elf_section
6791 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
6793 signed_p
= (ELF_ST_TYPE (sym
->st_info
) == STT_SRELC
);
6797 /* The symbol is global. */
6798 struct elf_link_hash_entry
* h
;
6800 if (elf_bad_symtab (input_bfd
))
6803 h
= sym_hashes
[index
- locsymcount
];
6804 while ( h
->root
.type
== bfd_link_hash_indirect
6805 || h
->root
.type
== bfd_link_hash_warning
)
6806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6808 if (h
->type
!= STT_RELC
&& h
->type
!= STT_SRELC
)
6811 signed_p
= (h
->type
== STT_SRELC
);
6812 sym_name
= (char *) h
->root
.root
.string
;
6815 printf ("Encountered a complex symbol!");
6816 printf (" (input_bfd %s, section %s, reloc %ld\n",
6817 input_bfd
->filename
, reloc_sec
->name
, i
);
6818 printf (" symbol: idx %8.8lx, name %s\n",
6820 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6822 printf (" Evaluating '%s' ...\n ", sym_name
);
6824 if (eval_symbol (& result
, sym_name
, & sym_name
, input_bfd
,
6825 finfo
, addr
, section_offset
, locsymcount
,
6827 /* Symbol evaluated OK. Update to absolute value. */
6828 set_symbol_value (input_bfd
, finfo
, index
, result
);
6834 if (internal_relocs
!= elf_section_data (reloc_sec
)->relocs
)
6835 free (internal_relocs
);
6838 /* If nothing went wrong, then we adjusted
6839 everything we wanted to adjust. */
6844 put_value (bfd_vma size
,
6845 unsigned long chunksz
,
6848 bfd_byte
* location
)
6850 location
+= (size
- chunksz
);
6852 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
6860 bfd_put_8 (input_bfd
, x
, location
);
6863 bfd_put_16 (input_bfd
, x
, location
);
6866 bfd_put_32 (input_bfd
, x
, location
);
6870 bfd_put_64 (input_bfd
, x
, location
);
6880 get_value (bfd_vma size
,
6881 unsigned long chunksz
,
6883 bfd_byte
* location
)
6887 for (; size
; size
-= chunksz
, location
+= chunksz
)
6895 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
6898 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
6901 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
6905 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
6916 decode_complex_addend
6917 (unsigned long * start
, /* in bits */
6918 unsigned long * oplen
, /* in bits */
6919 unsigned long * len
, /* in bits */
6920 unsigned long * wordsz
, /* in bytes */
6921 unsigned long * chunksz
, /* in bytes */
6922 unsigned long * lsb0_p
,
6923 unsigned long * signed_p
,
6924 unsigned long * trunc_p
,
6925 unsigned long encoded
)
6927 * start
= encoded
& 0x3F;
6928 * len
= (encoded
>> 6) & 0x3F;
6929 * oplen
= (encoded
>> 12) & 0x3F;
6930 * wordsz
= (encoded
>> 18) & 0xF;
6931 * chunksz
= (encoded
>> 22) & 0xF;
6932 * lsb0_p
= (encoded
>> 27) & 1;
6933 * signed_p
= (encoded
>> 28) & 1;
6934 * trunc_p
= (encoded
>> 29) & 1;
6938 bfd_elf_perform_complex_relocation
6939 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
6940 struct bfd_link_info
* info
,
6942 asection
* input_section
,
6943 bfd_byte
* contents
,
6944 Elf_Internal_Rela
* rel
,
6945 Elf_Internal_Sym
* local_syms
,
6946 asection
** local_sections
)
6948 const struct elf_backend_data
* bed
;
6949 Elf_Internal_Shdr
* symtab_hdr
;
6951 bfd_vma relocation
= 0, shift
, x
;
6954 unsigned long start
, oplen
, len
, wordsz
,
6955 chunksz
, lsb0_p
, signed_p
, trunc_p
;
6957 /* Perform this reloc, since it is complex.
6958 (this is not to say that it necessarily refers to a complex
6959 symbol; merely that it is a self-describing CGEN based reloc.
6960 i.e. the addend has the complete reloc information (bit start, end,
6961 word size, etc) encoded within it.). */
6962 r_symndx
= ELF32_R_SYM (rel
->r_info
);
6963 bed
= get_elf_backend_data (input_bfd
);
6964 if (bed
->s
->arch_size
== 64)
6968 printf ("Performing complex relocation %ld...\n", r_symndx
);
6971 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
6972 if (r_symndx
< symtab_hdr
->sh_info
)
6974 /* The symbol is local. */
6975 Elf_Internal_Sym
* sym
;
6977 sym
= local_syms
+ r_symndx
;
6978 sec
= local_sections
[r_symndx
];
6979 relocation
= sym
->st_value
;
6980 if (sym
->st_shndx
> SHN_UNDEF
&&
6981 sym
->st_shndx
< SHN_LORESERVE
)
6982 relocation
+= (sec
->output_offset
+
6983 sec
->output_section
->vma
);
6987 /* The symbol is global. */
6988 struct elf_link_hash_entry
**sym_hashes
;
6989 struct elf_link_hash_entry
* h
;
6991 sym_hashes
= elf_sym_hashes (input_bfd
);
6992 h
= sym_hashes
[r_symndx
];
6994 while (h
->root
.type
== bfd_link_hash_indirect
6995 || h
->root
.type
== bfd_link_hash_warning
)
6996 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6998 if (h
->root
.type
== bfd_link_hash_defined
6999 || h
->root
.type
== bfd_link_hash_defweak
)
7001 sec
= h
->root
.u
.def
.section
;
7002 relocation
= h
->root
.u
.def
.value
;
7004 if (! bfd_is_abs_section (sec
))
7005 relocation
+= (sec
->output_section
->vma
7006 + sec
->output_offset
);
7008 if (h
->root
.type
== bfd_link_hash_undefined
7009 && !((*info
->callbacks
->undefined_symbol
)
7010 (info
, h
->root
.root
.string
, input_bfd
,
7011 input_section
, rel
->r_offset
,
7012 info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
7013 || ELF_ST_VISIBILITY (h
->other
))))
7017 decode_complex_addend (& start
, & oplen
, & len
, & wordsz
,
7018 & chunksz
, & lsb0_p
, & signed_p
,
7019 & trunc_p
, rel
->r_addend
);
7021 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7024 shift
= (start
+ 1) - len
;
7026 shift
= (8 * wordsz
) - (start
+ len
);
7028 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7031 printf ("Doing complex reloc: "
7032 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7033 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7034 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7035 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7036 oplen
, x
, mask
, relocation
);
7041 /* Now do an overflow check. */
7042 if (bfd_check_overflow ((signed_p
?
7043 complain_overflow_signed
:
7044 complain_overflow_unsigned
),
7045 len
, 0, (8 * wordsz
),
7046 relocation
) == bfd_reloc_overflow
)
7047 (*_bfd_error_handler
)
7048 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7050 input_bfd
->filename
, input_section
->name
, rel
->r_offset
,
7051 relocation
, (signed_p
? "(signed) " : ""), mask
);
7055 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7058 printf (" relocation: %8.8lx\n"
7059 " shifted mask: %8.8lx\n"
7060 " shifted/masked reloc: %8.8lx\n"
7061 " result: %8.8lx\n",
7062 relocation
, (mask
<< shift
),
7063 ((relocation
& mask
) << shift
), x
);
7065 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7068 /* When performing a relocatable link, the input relocations are
7069 preserved. But, if they reference global symbols, the indices
7070 referenced must be updated. Update all the relocations in
7071 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7074 elf_link_adjust_relocs (bfd
*abfd
,
7075 Elf_Internal_Shdr
*rel_hdr
,
7077 struct elf_link_hash_entry
**rel_hash
)
7080 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7082 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7083 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7084 bfd_vma r_type_mask
;
7087 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7089 swap_in
= bed
->s
->swap_reloc_in
;
7090 swap_out
= bed
->s
->swap_reloc_out
;
7092 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7094 swap_in
= bed
->s
->swap_reloca_in
;
7095 swap_out
= bed
->s
->swap_reloca_out
;
7100 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7103 if (bed
->s
->arch_size
== 32)
7110 r_type_mask
= 0xffffffff;
7114 erela
= rel_hdr
->contents
;
7115 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7117 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7120 if (*rel_hash
== NULL
)
7123 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7125 (*swap_in
) (abfd
, erela
, irela
);
7126 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7127 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7128 | (irela
[j
].r_info
& r_type_mask
));
7129 (*swap_out
) (abfd
, irela
, erela
);
7133 struct elf_link_sort_rela
7139 enum elf_reloc_type_class type
;
7140 /* We use this as an array of size int_rels_per_ext_rel. */
7141 Elf_Internal_Rela rela
[1];
7145 elf_link_sort_cmp1 (const void *A
, const void *B
)
7147 const struct elf_link_sort_rela
*a
= A
;
7148 const struct elf_link_sort_rela
*b
= B
;
7149 int relativea
, relativeb
;
7151 relativea
= a
->type
== reloc_class_relative
;
7152 relativeb
= b
->type
== reloc_class_relative
;
7154 if (relativea
< relativeb
)
7156 if (relativea
> relativeb
)
7158 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7160 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7162 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7164 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7170 elf_link_sort_cmp2 (const void *A
, const void *B
)
7172 const struct elf_link_sort_rela
*a
= A
;
7173 const struct elf_link_sort_rela
*b
= B
;
7176 if (a
->u
.offset
< b
->u
.offset
)
7178 if (a
->u
.offset
> b
->u
.offset
)
7180 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7181 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7186 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7188 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7194 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7197 bfd_size_type count
, size
;
7198 size_t i
, ret
, sort_elt
, ext_size
;
7199 bfd_byte
*sort
, *s_non_relative
, *p
;
7200 struct elf_link_sort_rela
*sq
;
7201 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7202 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7203 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7204 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7205 struct bfd_link_order
*lo
;
7208 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7209 if (reldyn
== NULL
|| reldyn
->size
== 0)
7211 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7212 if (reldyn
== NULL
|| reldyn
->size
== 0)
7214 ext_size
= bed
->s
->sizeof_rel
;
7215 swap_in
= bed
->s
->swap_reloc_in
;
7216 swap_out
= bed
->s
->swap_reloc_out
;
7220 ext_size
= bed
->s
->sizeof_rela
;
7221 swap_in
= bed
->s
->swap_reloca_in
;
7222 swap_out
= bed
->s
->swap_reloca_out
;
7224 count
= reldyn
->size
/ ext_size
;
7227 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7228 if (lo
->type
== bfd_indirect_link_order
)
7230 asection
*o
= lo
->u
.indirect
.section
;
7234 if (size
!= reldyn
->size
)
7237 sort_elt
= (sizeof (struct elf_link_sort_rela
)
7238 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
7239 sort
= bfd_zmalloc (sort_elt
* count
);
7242 (*info
->callbacks
->warning
)
7243 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
7247 if (bed
->s
->arch_size
== 32)
7248 r_sym_mask
= ~(bfd_vma
) 0xff;
7250 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
7252 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7253 if (lo
->type
== bfd_indirect_link_order
)
7255 bfd_byte
*erel
, *erelend
;
7256 asection
*o
= lo
->u
.indirect
.section
;
7258 if (o
->contents
== NULL
&& o
->size
!= 0)
7260 /* This is a reloc section that is being handled as a normal
7261 section. See bfd_section_from_shdr. We can't combine
7262 relocs in this case. */
7267 erelend
= o
->contents
+ o
->size
;
7268 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7269 while (erel
< erelend
)
7271 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7272 (*swap_in
) (abfd
, erel
, s
->rela
);
7273 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
7274 s
->u
.sym_mask
= r_sym_mask
;
7280 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
7282 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
7284 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7285 if (s
->type
!= reloc_class_relative
)
7291 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
7292 for (; i
< count
; i
++, p
+= sort_elt
)
7294 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
7295 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
7297 sp
->u
.offset
= sq
->rela
->r_offset
;
7300 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
7302 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7303 if (lo
->type
== bfd_indirect_link_order
)
7305 bfd_byte
*erel
, *erelend
;
7306 asection
*o
= lo
->u
.indirect
.section
;
7309 erelend
= o
->contents
+ o
->size
;
7310 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
7311 while (erel
< erelend
)
7313 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
7314 (*swap_out
) (abfd
, s
->rela
, erel
);
7325 /* Flush the output symbols to the file. */
7328 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
7329 const struct elf_backend_data
*bed
)
7331 if (finfo
->symbuf_count
> 0)
7333 Elf_Internal_Shdr
*hdr
;
7337 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
7338 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
7339 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7340 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
7341 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
7344 hdr
->sh_size
+= amt
;
7345 finfo
->symbuf_count
= 0;
7351 /* Add a symbol to the output symbol table. */
7354 elf_link_output_sym (struct elf_final_link_info
*finfo
,
7356 Elf_Internal_Sym
*elfsym
,
7357 asection
*input_sec
,
7358 struct elf_link_hash_entry
*h
)
7361 Elf_External_Sym_Shndx
*destshndx
;
7362 bfd_boolean (*output_symbol_hook
)
7363 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
7364 struct elf_link_hash_entry
*);
7365 const struct elf_backend_data
*bed
;
7367 bed
= get_elf_backend_data (finfo
->output_bfd
);
7368 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
7369 if (output_symbol_hook
!= NULL
)
7371 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
7375 if (name
== NULL
|| *name
== '\0')
7376 elfsym
->st_name
= 0;
7377 else if (input_sec
->flags
& SEC_EXCLUDE
)
7378 elfsym
->st_name
= 0;
7381 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
7383 if (elfsym
->st_name
== (unsigned long) -1)
7387 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
7389 if (! elf_link_flush_output_syms (finfo
, bed
))
7393 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
7394 destshndx
= finfo
->symshndxbuf
;
7395 if (destshndx
!= NULL
)
7397 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
7401 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
7402 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
7403 if (destshndx
== NULL
)
7405 memset ((char *) destshndx
+ amt
, 0, amt
);
7406 finfo
->shndxbuf_size
*= 2;
7408 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
7411 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
7412 finfo
->symbuf_count
+= 1;
7413 bfd_get_symcount (finfo
->output_bfd
) += 1;
7418 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7421 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
7423 if (sym
->st_shndx
> SHN_HIRESERVE
)
7425 /* The gABI doesn't support dynamic symbols in output sections
7427 (*_bfd_error_handler
)
7428 (_("%B: Too many sections: %d (>= %d)"),
7429 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
);
7430 bfd_set_error (bfd_error_nonrepresentable_section
);
7436 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7437 allowing an unsatisfied unversioned symbol in the DSO to match a
7438 versioned symbol that would normally require an explicit version.
7439 We also handle the case that a DSO references a hidden symbol
7440 which may be satisfied by a versioned symbol in another DSO. */
7443 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
7444 const struct elf_backend_data
*bed
,
7445 struct elf_link_hash_entry
*h
)
7448 struct elf_link_loaded_list
*loaded
;
7450 if (!is_elf_hash_table (info
->hash
))
7453 switch (h
->root
.type
)
7459 case bfd_link_hash_undefined
:
7460 case bfd_link_hash_undefweak
:
7461 abfd
= h
->root
.u
.undef
.abfd
;
7462 if ((abfd
->flags
& DYNAMIC
) == 0
7463 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
7467 case bfd_link_hash_defined
:
7468 case bfd_link_hash_defweak
:
7469 abfd
= h
->root
.u
.def
.section
->owner
;
7472 case bfd_link_hash_common
:
7473 abfd
= h
->root
.u
.c
.p
->section
->owner
;
7476 BFD_ASSERT (abfd
!= NULL
);
7478 for (loaded
= elf_hash_table (info
)->loaded
;
7480 loaded
= loaded
->next
)
7483 Elf_Internal_Shdr
*hdr
;
7484 bfd_size_type symcount
;
7485 bfd_size_type extsymcount
;
7486 bfd_size_type extsymoff
;
7487 Elf_Internal_Shdr
*versymhdr
;
7488 Elf_Internal_Sym
*isym
;
7489 Elf_Internal_Sym
*isymend
;
7490 Elf_Internal_Sym
*isymbuf
;
7491 Elf_External_Versym
*ever
;
7492 Elf_External_Versym
*extversym
;
7494 input
= loaded
->abfd
;
7496 /* We check each DSO for a possible hidden versioned definition. */
7498 || (input
->flags
& DYNAMIC
) == 0
7499 || elf_dynversym (input
) == 0)
7502 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
7504 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
7505 if (elf_bad_symtab (input
))
7507 extsymcount
= symcount
;
7512 extsymcount
= symcount
- hdr
->sh_info
;
7513 extsymoff
= hdr
->sh_info
;
7516 if (extsymcount
== 0)
7519 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
7521 if (isymbuf
== NULL
)
7524 /* Read in any version definitions. */
7525 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
7526 extversym
= bfd_malloc (versymhdr
->sh_size
);
7527 if (extversym
== NULL
)
7530 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
7531 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
7532 != versymhdr
->sh_size
))
7540 ever
= extversym
+ extsymoff
;
7541 isymend
= isymbuf
+ extsymcount
;
7542 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
7545 Elf_Internal_Versym iver
;
7546 unsigned short version_index
;
7548 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
7549 || isym
->st_shndx
== SHN_UNDEF
)
7552 name
= bfd_elf_string_from_elf_section (input
,
7555 if (strcmp (name
, h
->root
.root
.string
) != 0)
7558 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
7560 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
7562 /* If we have a non-hidden versioned sym, then it should
7563 have provided a definition for the undefined sym. */
7567 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
7568 if (version_index
== 1 || version_index
== 2)
7570 /* This is the base or first version. We can use it. */
7584 /* Add an external symbol to the symbol table. This is called from
7585 the hash table traversal routine. When generating a shared object,
7586 we go through the symbol table twice. The first time we output
7587 anything that might have been forced to local scope in a version
7588 script. The second time we output the symbols that are still
7592 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
7594 struct elf_outext_info
*eoinfo
= data
;
7595 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
7597 Elf_Internal_Sym sym
;
7598 asection
*input_sec
;
7599 const struct elf_backend_data
*bed
;
7601 if (h
->root
.type
== bfd_link_hash_warning
)
7603 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7604 if (h
->root
.type
== bfd_link_hash_new
)
7608 /* Decide whether to output this symbol in this pass. */
7609 if (eoinfo
->localsyms
)
7611 if (!h
->forced_local
)
7616 if (h
->forced_local
)
7620 bed
= get_elf_backend_data (finfo
->output_bfd
);
7622 if (h
->root
.type
== bfd_link_hash_undefined
)
7624 /* If we have an undefined symbol reference here then it must have
7625 come from a shared library that is being linked in. (Undefined
7626 references in regular files have already been handled). */
7627 bfd_boolean ignore_undef
= FALSE
;
7629 /* Some symbols may be special in that the fact that they're
7630 undefined can be safely ignored - let backend determine that. */
7631 if (bed
->elf_backend_ignore_undef_symbol
)
7632 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
7634 /* If we are reporting errors for this situation then do so now. */
7635 if (ignore_undef
== FALSE
7638 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
7639 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
7641 if (! (finfo
->info
->callbacks
->undefined_symbol
7642 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
7643 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
7645 eoinfo
->failed
= TRUE
;
7651 /* We should also warn if a forced local symbol is referenced from
7652 shared libraries. */
7653 if (! finfo
->info
->relocatable
7654 && (! finfo
->info
->shared
)
7659 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
7661 (*_bfd_error_handler
)
7662 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7664 h
->root
.u
.def
.section
== bfd_abs_section_ptr
7665 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
7666 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
7668 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
7669 ? "hidden" : "local",
7670 h
->root
.root
.string
);
7671 eoinfo
->failed
= TRUE
;
7675 /* We don't want to output symbols that have never been mentioned by
7676 a regular file, or that we have been told to strip. However, if
7677 h->indx is set to -2, the symbol is used by a reloc and we must
7681 else if ((h
->def_dynamic
7683 || h
->root
.type
== bfd_link_hash_new
)
7687 else if (finfo
->info
->strip
== strip_all
)
7689 else if (finfo
->info
->strip
== strip_some
7690 && bfd_hash_lookup (finfo
->info
->keep_hash
,
7691 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
7693 else if (finfo
->info
->strip_discarded
7694 && (h
->root
.type
== bfd_link_hash_defined
7695 || h
->root
.type
== bfd_link_hash_defweak
)
7696 && elf_discarded_section (h
->root
.u
.def
.section
))
7701 /* If we're stripping it, and it's not a dynamic symbol, there's
7702 nothing else to do unless it is a forced local symbol. */
7705 && !h
->forced_local
)
7709 sym
.st_size
= h
->size
;
7710 sym
.st_other
= h
->other
;
7711 if (h
->forced_local
)
7712 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
7713 else if (h
->root
.type
== bfd_link_hash_undefweak
7714 || h
->root
.type
== bfd_link_hash_defweak
)
7715 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
7717 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
7719 switch (h
->root
.type
)
7722 case bfd_link_hash_new
:
7723 case bfd_link_hash_warning
:
7727 case bfd_link_hash_undefined
:
7728 case bfd_link_hash_undefweak
:
7729 input_sec
= bfd_und_section_ptr
;
7730 sym
.st_shndx
= SHN_UNDEF
;
7733 case bfd_link_hash_defined
:
7734 case bfd_link_hash_defweak
:
7736 input_sec
= h
->root
.u
.def
.section
;
7737 if (input_sec
->output_section
!= NULL
)
7740 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
7741 input_sec
->output_section
);
7742 if (sym
.st_shndx
== SHN_BAD
)
7744 (*_bfd_error_handler
)
7745 (_("%B: could not find output section %A for input section %A"),
7746 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
7747 eoinfo
->failed
= TRUE
;
7751 /* ELF symbols in relocatable files are section relative,
7752 but in nonrelocatable files they are virtual
7754 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
7755 if (! finfo
->info
->relocatable
)
7757 sym
.st_value
+= input_sec
->output_section
->vma
;
7758 if (h
->type
== STT_TLS
)
7760 /* STT_TLS symbols are relative to PT_TLS segment
7762 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
7763 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
7769 BFD_ASSERT (input_sec
->owner
== NULL
7770 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
7771 sym
.st_shndx
= SHN_UNDEF
;
7772 input_sec
= bfd_und_section_ptr
;
7777 case bfd_link_hash_common
:
7778 input_sec
= h
->root
.u
.c
.p
->section
;
7779 sym
.st_shndx
= bed
->common_section_index (input_sec
);
7780 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
7783 case bfd_link_hash_indirect
:
7784 /* These symbols are created by symbol versioning. They point
7785 to the decorated version of the name. For example, if the
7786 symbol foo@@GNU_1.2 is the default, which should be used when
7787 foo is used with no version, then we add an indirect symbol
7788 foo which points to foo@@GNU_1.2. We ignore these symbols,
7789 since the indirected symbol is already in the hash table. */
7793 /* Give the processor backend a chance to tweak the symbol value,
7794 and also to finish up anything that needs to be done for this
7795 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
7796 forced local syms when non-shared is due to a historical quirk. */
7797 if ((h
->dynindx
!= -1
7799 && ((finfo
->info
->shared
7800 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
7801 || h
->root
.type
!= bfd_link_hash_undefweak
))
7802 || !h
->forced_local
)
7803 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7805 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
7806 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
7808 eoinfo
->failed
= TRUE
;
7813 /* If we are marking the symbol as undefined, and there are no
7814 non-weak references to this symbol from a regular object, then
7815 mark the symbol as weak undefined; if there are non-weak
7816 references, mark the symbol as strong. We can't do this earlier,
7817 because it might not be marked as undefined until the
7818 finish_dynamic_symbol routine gets through with it. */
7819 if (sym
.st_shndx
== SHN_UNDEF
7821 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
7822 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
7826 if (h
->ref_regular_nonweak
)
7827 bindtype
= STB_GLOBAL
;
7829 bindtype
= STB_WEAK
;
7830 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
7833 /* If a non-weak symbol with non-default visibility is not defined
7834 locally, it is a fatal error. */
7835 if (! finfo
->info
->relocatable
7836 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
7837 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
7838 && h
->root
.type
== bfd_link_hash_undefined
7841 (*_bfd_error_handler
)
7842 (_("%B: %s symbol `%s' isn't defined"),
7844 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
7846 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
7847 ? "internal" : "hidden",
7848 h
->root
.root
.string
);
7849 eoinfo
->failed
= TRUE
;
7853 /* If this symbol should be put in the .dynsym section, then put it
7854 there now. We already know the symbol index. We also fill in
7855 the entry in the .hash section. */
7856 if (h
->dynindx
!= -1
7857 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
7861 sym
.st_name
= h
->dynstr_index
;
7862 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
7863 if (! check_dynsym (finfo
->output_bfd
, &sym
))
7865 eoinfo
->failed
= TRUE
;
7868 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
7870 if (finfo
->hash_sec
!= NULL
)
7872 size_t hash_entry_size
;
7873 bfd_byte
*bucketpos
;
7878 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
7879 bucket
= h
->u
.elf_hash_value
% bucketcount
;
7882 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
7883 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
7884 + (bucket
+ 2) * hash_entry_size
);
7885 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
7886 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
7887 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
7888 ((bfd_byte
*) finfo
->hash_sec
->contents
7889 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
7892 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
7894 Elf_Internal_Versym iversym
;
7895 Elf_External_Versym
*eversym
;
7897 if (!h
->def_regular
)
7899 if (h
->verinfo
.verdef
== NULL
)
7900 iversym
.vs_vers
= 0;
7902 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
7906 if (h
->verinfo
.vertree
== NULL
)
7907 iversym
.vs_vers
= 1;
7909 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
7910 if (finfo
->info
->create_default_symver
)
7915 iversym
.vs_vers
|= VERSYM_HIDDEN
;
7917 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
7918 eversym
+= h
->dynindx
;
7919 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
7923 /* If we're stripping it, then it was just a dynamic symbol, and
7924 there's nothing else to do. */
7925 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
7928 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
7930 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
7932 eoinfo
->failed
= TRUE
;
7939 /* Return TRUE if special handling is done for relocs in SEC against
7940 symbols defined in discarded sections. */
7943 elf_section_ignore_discarded_relocs (asection
*sec
)
7945 const struct elf_backend_data
*bed
;
7947 switch (sec
->sec_info_type
)
7949 case ELF_INFO_TYPE_STABS
:
7950 case ELF_INFO_TYPE_EH_FRAME
:
7956 bed
= get_elf_backend_data (sec
->owner
);
7957 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
7958 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
7964 /* Return a mask saying how ld should treat relocations in SEC against
7965 symbols defined in discarded sections. If this function returns
7966 COMPLAIN set, ld will issue a warning message. If this function
7967 returns PRETEND set, and the discarded section was link-once and the
7968 same size as the kept link-once section, ld will pretend that the
7969 symbol was actually defined in the kept section. Otherwise ld will
7970 zero the reloc (at least that is the intent, but some cooperation by
7971 the target dependent code is needed, particularly for REL targets). */
7974 _bfd_elf_default_action_discarded (asection
*sec
)
7976 if (sec
->flags
& SEC_DEBUGGING
)
7979 if (strcmp (".eh_frame", sec
->name
) == 0)
7982 if (strcmp (".gcc_except_table", sec
->name
) == 0)
7985 return COMPLAIN
| PRETEND
;
7988 /* Find a match between a section and a member of a section group. */
7991 match_group_member (asection
*sec
, asection
*group
,
7992 struct bfd_link_info
*info
)
7994 asection
*first
= elf_next_in_group (group
);
7995 asection
*s
= first
;
7999 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8002 s
= elf_next_in_group (s
);
8010 /* Check if the kept section of a discarded section SEC can be used
8011 to replace it. Return the replacement if it is OK. Otherwise return
8015 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8019 kept
= sec
->kept_section
;
8022 if ((kept
->flags
& SEC_GROUP
) != 0)
8023 kept
= match_group_member (sec
, kept
, info
);
8024 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
8026 sec
->kept_section
= kept
;
8031 /* Link an input file into the linker output file. This function
8032 handles all the sections and relocations of the input file at once.
8033 This is so that we only have to read the local symbols once, and
8034 don't have to keep them in memory. */
8037 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8039 bfd_boolean (*relocate_section
)
8040 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8041 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8043 Elf_Internal_Shdr
*symtab_hdr
;
8046 Elf_Internal_Sym
*isymbuf
;
8047 Elf_Internal_Sym
*isym
;
8048 Elf_Internal_Sym
*isymend
;
8050 asection
**ppsection
;
8052 const struct elf_backend_data
*bed
;
8053 bfd_boolean emit_relocs
;
8054 struct elf_link_hash_entry
**sym_hashes
;
8056 output_bfd
= finfo
->output_bfd
;
8057 bed
= get_elf_backend_data (output_bfd
);
8058 relocate_section
= bed
->elf_backend_relocate_section
;
8060 /* If this is a dynamic object, we don't want to do anything here:
8061 we don't want the local symbols, and we don't want the section
8063 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8066 emit_relocs
= (finfo
->info
->relocatable
8067 || finfo
->info
->emitrelocations
);
8069 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8070 if (elf_bad_symtab (input_bfd
))
8072 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8077 locsymcount
= symtab_hdr
->sh_info
;
8078 extsymoff
= symtab_hdr
->sh_info
;
8081 /* Read the local symbols. */
8082 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8083 if (isymbuf
== NULL
&& locsymcount
!= 0)
8085 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8086 finfo
->internal_syms
,
8087 finfo
->external_syms
,
8088 finfo
->locsym_shndx
);
8089 if (isymbuf
== NULL
)
8092 /* evaluate_complex_relocation_symbols looks for symbols in
8093 finfo->internal_syms. */
8094 else if (isymbuf
!= NULL
&& locsymcount
!= 0)
8096 bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8097 finfo
->internal_syms
,
8098 finfo
->external_syms
,
8099 finfo
->locsym_shndx
);
8102 /* Find local symbol sections and adjust values of symbols in
8103 SEC_MERGE sections. Write out those local symbols we know are
8104 going into the output file. */
8105 isymend
= isymbuf
+ locsymcount
;
8106 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8108 isym
++, pindex
++, ppsection
++)
8112 Elf_Internal_Sym osym
;
8116 if (elf_bad_symtab (input_bfd
))
8118 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8125 if (isym
->st_shndx
== SHN_UNDEF
)
8126 isec
= bfd_und_section_ptr
;
8127 else if (isym
->st_shndx
< SHN_LORESERVE
8128 || isym
->st_shndx
> SHN_HIRESERVE
)
8130 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8132 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8133 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8135 _bfd_merged_section_offset (output_bfd
, &isec
,
8136 elf_section_data (isec
)->sec_info
,
8139 else if (isym
->st_shndx
== SHN_ABS
)
8140 isec
= bfd_abs_section_ptr
;
8141 else if (isym
->st_shndx
== SHN_COMMON
)
8142 isec
= bfd_com_section_ptr
;
8145 /* Don't attempt to output symbols with st_shnx in the
8146 reserved range other than SHN_ABS and SHN_COMMON. */
8153 /* Don't output the first, undefined, symbol. */
8154 if (ppsection
== finfo
->sections
)
8157 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8159 /* We never output section symbols. Instead, we use the
8160 section symbol of the corresponding section in the output
8165 /* If we are stripping all symbols, we don't want to output this
8167 if (finfo
->info
->strip
== strip_all
)
8170 /* If we are discarding all local symbols, we don't want to
8171 output this one. If we are generating a relocatable output
8172 file, then some of the local symbols may be required by
8173 relocs; we output them below as we discover that they are
8175 if (finfo
->info
->discard
== discard_all
)
8178 /* If this symbol is defined in a section which we are
8179 discarding, we don't need to keep it. */
8180 if (isym
->st_shndx
!= SHN_UNDEF
8181 && (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8183 || bfd_section_removed_from_list (output_bfd
,
8184 isec
->output_section
)))
8187 /* Get the name of the symbol. */
8188 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
8193 /* See if we are discarding symbols with this name. */
8194 if ((finfo
->info
->strip
== strip_some
8195 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
8197 || (((finfo
->info
->discard
== discard_sec_merge
8198 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
8199 || finfo
->info
->discard
== discard_l
)
8200 && bfd_is_local_label_name (input_bfd
, name
)))
8203 /* If we get here, we are going to output this symbol. */
8207 /* Adjust the section index for the output file. */
8208 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
8209 isec
->output_section
);
8210 if (osym
.st_shndx
== SHN_BAD
)
8213 *pindex
= bfd_get_symcount (output_bfd
);
8215 /* ELF symbols in relocatable files are section relative, but
8216 in executable files they are virtual addresses. Note that
8217 this code assumes that all ELF sections have an associated
8218 BFD section with a reasonable value for output_offset; below
8219 we assume that they also have a reasonable value for
8220 output_section. Any special sections must be set up to meet
8221 these requirements. */
8222 osym
.st_value
+= isec
->output_offset
;
8223 if (! finfo
->info
->relocatable
)
8225 osym
.st_value
+= isec
->output_section
->vma
;
8226 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
8228 /* STT_TLS symbols are relative to PT_TLS segment base. */
8229 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
8230 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
8234 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
8238 if (! evaluate_complex_relocation_symbols (input_bfd
, finfo
, locsymcount
))
8241 /* Relocate the contents of each section. */
8242 sym_hashes
= elf_sym_hashes (input_bfd
);
8243 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
8247 if (! o
->linker_mark
)
8249 /* This section was omitted from the link. */
8253 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8254 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
8257 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
8259 /* Section was created by _bfd_elf_link_create_dynamic_sections
8264 /* Get the contents of the section. They have been cached by a
8265 relaxation routine. Note that o is a section in an input
8266 file, so the contents field will not have been set by any of
8267 the routines which work on output files. */
8268 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
8269 contents
= elf_section_data (o
)->this_hdr
.contents
;
8272 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
8274 contents
= finfo
->contents
;
8275 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
8279 if ((o
->flags
& SEC_RELOC
) != 0)
8281 Elf_Internal_Rela
*internal_relocs
;
8282 bfd_vma r_type_mask
;
8285 /* Get the swapped relocs. */
8287 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
8288 finfo
->internal_relocs
, FALSE
);
8289 if (internal_relocs
== NULL
8290 && o
->reloc_count
> 0)
8293 if (bed
->s
->arch_size
== 32)
8300 r_type_mask
= 0xffffffff;
8304 /* Run through the relocs looking for any against symbols
8305 from discarded sections and section symbols from
8306 removed link-once sections. Complain about relocs
8307 against discarded sections. Zero relocs against removed
8308 link-once sections. */
8309 if (!elf_section_ignore_discarded_relocs (o
))
8311 Elf_Internal_Rela
*rel
, *relend
;
8312 unsigned int action
= (*bed
->action_discarded
) (o
);
8314 rel
= internal_relocs
;
8315 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8316 for ( ; rel
< relend
; rel
++)
8318 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
8319 asection
**ps
, *sec
;
8320 struct elf_link_hash_entry
*h
= NULL
;
8321 const char *sym_name
;
8323 if (r_symndx
== STN_UNDEF
)
8326 if (r_symndx
>= locsymcount
8327 || (elf_bad_symtab (input_bfd
)
8328 && finfo
->sections
[r_symndx
] == NULL
))
8330 h
= sym_hashes
[r_symndx
- extsymoff
];
8332 /* Badly formatted input files can contain relocs that
8333 reference non-existant symbols. Check here so that
8334 we do not seg fault. */
8339 sprintf_vma (buffer
, rel
->r_info
);
8340 (*_bfd_error_handler
)
8341 (_("error: %B contains a reloc (0x%s) for section %A "
8342 "that references a non-existent global symbol"),
8343 input_bfd
, o
, buffer
);
8344 bfd_set_error (bfd_error_bad_value
);
8348 while (h
->root
.type
== bfd_link_hash_indirect
8349 || h
->root
.type
== bfd_link_hash_warning
)
8350 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8352 if (h
->root
.type
!= bfd_link_hash_defined
8353 && h
->root
.type
!= bfd_link_hash_defweak
)
8356 ps
= &h
->root
.u
.def
.section
;
8357 sym_name
= h
->root
.root
.string
;
8361 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
8362 ps
= &finfo
->sections
[r_symndx
];
8363 sym_name
= bfd_elf_sym_name (input_bfd
,
8368 /* Complain if the definition comes from a
8369 discarded section. */
8370 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
8372 BFD_ASSERT (r_symndx
!= 0);
8373 if (action
& COMPLAIN
)
8374 (*finfo
->info
->callbacks
->einfo
)
8375 (_("%X`%s' referenced in section `%A' of %B: "
8376 "defined in discarded section `%A' of %B\n"),
8377 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
8379 /* Try to do the best we can to support buggy old
8380 versions of gcc. Pretend that the symbol is
8381 really defined in the kept linkonce section.
8382 FIXME: This is quite broken. Modifying the
8383 symbol here means we will be changing all later
8384 uses of the symbol, not just in this section. */
8385 if (action
& PRETEND
)
8389 kept
= _bfd_elf_check_kept_section (sec
,
8401 /* Relocate the section by invoking a back end routine.
8403 The back end routine is responsible for adjusting the
8404 section contents as necessary, and (if using Rela relocs
8405 and generating a relocatable output file) adjusting the
8406 reloc addend as necessary.
8408 The back end routine does not have to worry about setting
8409 the reloc address or the reloc symbol index.
8411 The back end routine is given a pointer to the swapped in
8412 internal symbols, and can access the hash table entries
8413 for the external symbols via elf_sym_hashes (input_bfd).
8415 When generating relocatable output, the back end routine
8416 must handle STB_LOCAL/STT_SECTION symbols specially. The
8417 output symbol is going to be a section symbol
8418 corresponding to the output section, which will require
8419 the addend to be adjusted. */
8421 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
8422 input_bfd
, o
, contents
,
8430 Elf_Internal_Rela
*irela
;
8431 Elf_Internal_Rela
*irelaend
;
8432 bfd_vma last_offset
;
8433 struct elf_link_hash_entry
**rel_hash
;
8434 struct elf_link_hash_entry
**rel_hash_list
;
8435 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
8436 unsigned int next_erel
;
8437 bfd_boolean rela_normal
;
8439 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
8440 rela_normal
= (bed
->rela_normal
8441 && (input_rel_hdr
->sh_entsize
8442 == bed
->s
->sizeof_rela
));
8444 /* Adjust the reloc addresses and symbol indices. */
8446 irela
= internal_relocs
;
8447 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8448 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
8449 + elf_section_data (o
->output_section
)->rel_count
8450 + elf_section_data (o
->output_section
)->rel_count2
);
8451 rel_hash_list
= rel_hash
;
8452 last_offset
= o
->output_offset
;
8453 if (!finfo
->info
->relocatable
)
8454 last_offset
+= o
->output_section
->vma
;
8455 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
8457 unsigned long r_symndx
;
8459 Elf_Internal_Sym sym
;
8461 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
8467 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
8470 if (irela
->r_offset
>= (bfd_vma
) -2)
8472 /* This is a reloc for a deleted entry or somesuch.
8473 Turn it into an R_*_NONE reloc, at the same
8474 offset as the last reloc. elf_eh_frame.c and
8475 bfd_elf_discard_info rely on reloc offsets
8477 irela
->r_offset
= last_offset
;
8479 irela
->r_addend
= 0;
8483 irela
->r_offset
+= o
->output_offset
;
8485 /* Relocs in an executable have to be virtual addresses. */
8486 if (!finfo
->info
->relocatable
)
8487 irela
->r_offset
+= o
->output_section
->vma
;
8489 last_offset
= irela
->r_offset
;
8491 r_symndx
= irela
->r_info
>> r_sym_shift
;
8492 if (r_symndx
== STN_UNDEF
)
8495 if (r_symndx
>= locsymcount
8496 || (elf_bad_symtab (input_bfd
)
8497 && finfo
->sections
[r_symndx
] == NULL
))
8499 struct elf_link_hash_entry
*rh
;
8502 /* This is a reloc against a global symbol. We
8503 have not yet output all the local symbols, so
8504 we do not know the symbol index of any global
8505 symbol. We set the rel_hash entry for this
8506 reloc to point to the global hash table entry
8507 for this symbol. The symbol index is then
8508 set at the end of bfd_elf_final_link. */
8509 indx
= r_symndx
- extsymoff
;
8510 rh
= elf_sym_hashes (input_bfd
)[indx
];
8511 while (rh
->root
.type
== bfd_link_hash_indirect
8512 || rh
->root
.type
== bfd_link_hash_warning
)
8513 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
8515 /* Setting the index to -2 tells
8516 elf_link_output_extsym that this symbol is
8518 BFD_ASSERT (rh
->indx
< 0);
8526 /* This is a reloc against a local symbol. */
8529 sym
= isymbuf
[r_symndx
];
8530 sec
= finfo
->sections
[r_symndx
];
8531 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
8533 /* I suppose the backend ought to fill in the
8534 section of any STT_SECTION symbol against a
8535 processor specific section. */
8537 if (bfd_is_abs_section (sec
))
8539 else if (sec
== NULL
|| sec
->owner
== NULL
)
8541 bfd_set_error (bfd_error_bad_value
);
8546 asection
*osec
= sec
->output_section
;
8548 /* If we have discarded a section, the output
8549 section will be the absolute section. In
8550 case of discarded SEC_MERGE sections, use
8551 the kept section. relocate_section should
8552 have already handled discarded linkonce
8554 if (bfd_is_abs_section (osec
)
8555 && sec
->kept_section
!= NULL
8556 && sec
->kept_section
->output_section
!= NULL
)
8558 osec
= sec
->kept_section
->output_section
;
8559 irela
->r_addend
-= osec
->vma
;
8562 if (!bfd_is_abs_section (osec
))
8564 r_symndx
= osec
->target_index
;
8567 struct elf_link_hash_table
*htab
;
8570 htab
= elf_hash_table (finfo
->info
);
8571 oi
= htab
->text_index_section
;
8572 if ((osec
->flags
& SEC_READONLY
) == 0
8573 && htab
->data_index_section
!= NULL
)
8574 oi
= htab
->data_index_section
;
8578 irela
->r_addend
+= osec
->vma
- oi
->vma
;
8579 r_symndx
= oi
->target_index
;
8583 BFD_ASSERT (r_symndx
!= 0);
8587 /* Adjust the addend according to where the
8588 section winds up in the output section. */
8590 irela
->r_addend
+= sec
->output_offset
;
8594 if (finfo
->indices
[r_symndx
] == -1)
8596 unsigned long shlink
;
8600 if (finfo
->info
->strip
== strip_all
)
8602 /* You can't do ld -r -s. */
8603 bfd_set_error (bfd_error_invalid_operation
);
8607 /* This symbol was skipped earlier, but
8608 since it is needed by a reloc, we
8609 must output it now. */
8610 shlink
= symtab_hdr
->sh_link
;
8611 name
= (bfd_elf_string_from_elf_section
8612 (input_bfd
, shlink
, sym
.st_name
));
8616 osec
= sec
->output_section
;
8618 _bfd_elf_section_from_bfd_section (output_bfd
,
8620 if (sym
.st_shndx
== SHN_BAD
)
8623 sym
.st_value
+= sec
->output_offset
;
8624 if (! finfo
->info
->relocatable
)
8626 sym
.st_value
+= osec
->vma
;
8627 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
8629 /* STT_TLS symbols are relative to PT_TLS
8631 BFD_ASSERT (elf_hash_table (finfo
->info
)
8633 sym
.st_value
-= (elf_hash_table (finfo
->info
)
8638 finfo
->indices
[r_symndx
]
8639 = bfd_get_symcount (output_bfd
);
8641 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
8646 r_symndx
= finfo
->indices
[r_symndx
];
8649 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
8650 | (irela
->r_info
& r_type_mask
));
8653 /* Swap out the relocs. */
8654 if (input_rel_hdr
->sh_size
!= 0
8655 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
8661 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
8662 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
8664 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
8665 * bed
->s
->int_rels_per_ext_rel
);
8666 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
8667 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
8676 /* Write out the modified section contents. */
8677 if (bed
->elf_backend_write_section
8678 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
8681 /* Section written out. */
8683 else switch (o
->sec_info_type
)
8685 case ELF_INFO_TYPE_STABS
:
8686 if (! (_bfd_write_section_stabs
8688 &elf_hash_table (finfo
->info
)->stab_info
,
8689 o
, &elf_section_data (o
)->sec_info
, contents
)))
8692 case ELF_INFO_TYPE_MERGE
:
8693 if (! _bfd_write_merged_section (output_bfd
, o
,
8694 elf_section_data (o
)->sec_info
))
8697 case ELF_INFO_TYPE_EH_FRAME
:
8699 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
8706 if (! (o
->flags
& SEC_EXCLUDE
)
8707 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
8709 (file_ptr
) o
->output_offset
,
8720 /* Generate a reloc when linking an ELF file. This is a reloc
8721 requested by the linker, and does not come from any input file. This
8722 is used to build constructor and destructor tables when linking
8726 elf_reloc_link_order (bfd
*output_bfd
,
8727 struct bfd_link_info
*info
,
8728 asection
*output_section
,
8729 struct bfd_link_order
*link_order
)
8731 reloc_howto_type
*howto
;
8735 struct elf_link_hash_entry
**rel_hash_ptr
;
8736 Elf_Internal_Shdr
*rel_hdr
;
8737 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
8738 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
8742 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
8745 bfd_set_error (bfd_error_bad_value
);
8749 addend
= link_order
->u
.reloc
.p
->addend
;
8751 /* Figure out the symbol index. */
8752 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
8753 + elf_section_data (output_section
)->rel_count
8754 + elf_section_data (output_section
)->rel_count2
);
8755 if (link_order
->type
== bfd_section_reloc_link_order
)
8757 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
8758 BFD_ASSERT (indx
!= 0);
8759 *rel_hash_ptr
= NULL
;
8763 struct elf_link_hash_entry
*h
;
8765 /* Treat a reloc against a defined symbol as though it were
8766 actually against the section. */
8767 h
= ((struct elf_link_hash_entry
*)
8768 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
8769 link_order
->u
.reloc
.p
->u
.name
,
8770 FALSE
, FALSE
, TRUE
));
8772 && (h
->root
.type
== bfd_link_hash_defined
8773 || h
->root
.type
== bfd_link_hash_defweak
))
8777 section
= h
->root
.u
.def
.section
;
8778 indx
= section
->output_section
->target_index
;
8779 *rel_hash_ptr
= NULL
;
8780 /* It seems that we ought to add the symbol value to the
8781 addend here, but in practice it has already been added
8782 because it was passed to constructor_callback. */
8783 addend
+= section
->output_section
->vma
+ section
->output_offset
;
8787 /* Setting the index to -2 tells elf_link_output_extsym that
8788 this symbol is used by a reloc. */
8795 if (! ((*info
->callbacks
->unattached_reloc
)
8796 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
8802 /* If this is an inplace reloc, we must write the addend into the
8804 if (howto
->partial_inplace
&& addend
!= 0)
8807 bfd_reloc_status_type rstat
;
8810 const char *sym_name
;
8812 size
= bfd_get_reloc_size (howto
);
8813 buf
= bfd_zmalloc (size
);
8816 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
8823 case bfd_reloc_outofrange
:
8826 case bfd_reloc_overflow
:
8827 if (link_order
->type
== bfd_section_reloc_link_order
)
8828 sym_name
= bfd_section_name (output_bfd
,
8829 link_order
->u
.reloc
.p
->u
.section
);
8831 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
8832 if (! ((*info
->callbacks
->reloc_overflow
)
8833 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
8834 NULL
, (bfd_vma
) 0)))
8841 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
8842 link_order
->offset
, size
);
8848 /* The address of a reloc is relative to the section in a
8849 relocatable file, and is a virtual address in an executable
8851 offset
= link_order
->offset
;
8852 if (! info
->relocatable
)
8853 offset
+= output_section
->vma
;
8855 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
8857 irel
[i
].r_offset
= offset
;
8859 irel
[i
].r_addend
= 0;
8861 if (bed
->s
->arch_size
== 32)
8862 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
8864 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
8866 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
8867 erel
= rel_hdr
->contents
;
8868 if (rel_hdr
->sh_type
== SHT_REL
)
8870 erel
+= (elf_section_data (output_section
)->rel_count
8871 * bed
->s
->sizeof_rel
);
8872 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
8876 irel
[0].r_addend
= addend
;
8877 erel
+= (elf_section_data (output_section
)->rel_count
8878 * bed
->s
->sizeof_rela
);
8879 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
8882 ++elf_section_data (output_section
)->rel_count
;
8888 /* Get the output vma of the section pointed to by the sh_link field. */
8891 elf_get_linked_section_vma (struct bfd_link_order
*p
)
8893 Elf_Internal_Shdr
**elf_shdrp
;
8897 s
= p
->u
.indirect
.section
;
8898 elf_shdrp
= elf_elfsections (s
->owner
);
8899 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
8900 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
8902 The Intel C compiler generates SHT_IA_64_UNWIND with
8903 SHF_LINK_ORDER. But it doesn't set the sh_link or
8904 sh_info fields. Hence we could get the situation
8905 where elfsec is 0. */
8908 const struct elf_backend_data
*bed
8909 = get_elf_backend_data (s
->owner
);
8910 if (bed
->link_order_error_handler
)
8911 bed
->link_order_error_handler
8912 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
8917 s
= elf_shdrp
[elfsec
]->bfd_section
;
8918 return s
->output_section
->vma
+ s
->output_offset
;
8923 /* Compare two sections based on the locations of the sections they are
8924 linked to. Used by elf_fixup_link_order. */
8927 compare_link_order (const void * a
, const void * b
)
8932 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
8933 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
8940 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8941 order as their linked sections. Returns false if this could not be done
8942 because an output section includes both ordered and unordered
8943 sections. Ideally we'd do this in the linker proper. */
8946 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
8951 struct bfd_link_order
*p
;
8953 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8955 struct bfd_link_order
**sections
;
8956 asection
*s
, *other_sec
, *linkorder_sec
;
8960 linkorder_sec
= NULL
;
8963 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8965 if (p
->type
== bfd_indirect_link_order
)
8967 s
= p
->u
.indirect
.section
;
8969 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
8970 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
8971 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
8972 && elfsec
< elf_numsections (sub
)
8973 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
8987 if (seen_other
&& seen_linkorder
)
8989 if (other_sec
&& linkorder_sec
)
8990 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8992 linkorder_sec
->owner
, other_sec
,
8995 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
8997 bfd_set_error (bfd_error_bad_value
);
9002 if (!seen_linkorder
)
9005 sections
= (struct bfd_link_order
**)
9006 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9009 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9011 sections
[seen_linkorder
++] = p
;
9013 /* Sort the input sections in the order of their linked section. */
9014 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9015 compare_link_order
);
9017 /* Change the offsets of the sections. */
9019 for (n
= 0; n
< seen_linkorder
; n
++)
9021 s
= sections
[n
]->u
.indirect
.section
;
9022 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
9023 s
->output_offset
= offset
;
9024 sections
[n
]->offset
= offset
;
9025 offset
+= sections
[n
]->size
;
9032 /* Do the final step of an ELF link. */
9035 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9037 bfd_boolean dynamic
;
9038 bfd_boolean emit_relocs
;
9040 struct elf_final_link_info finfo
;
9041 register asection
*o
;
9042 register struct bfd_link_order
*p
;
9044 bfd_size_type max_contents_size
;
9045 bfd_size_type max_external_reloc_size
;
9046 bfd_size_type max_internal_reloc_count
;
9047 bfd_size_type max_sym_count
;
9048 bfd_size_type max_sym_shndx_count
;
9050 Elf_Internal_Sym elfsym
;
9052 Elf_Internal_Shdr
*symtab_hdr
;
9053 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9054 Elf_Internal_Shdr
*symstrtab_hdr
;
9055 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9056 struct elf_outext_info eoinfo
;
9058 size_t relativecount
= 0;
9059 asection
*reldyn
= 0;
9062 if (! is_elf_hash_table (info
->hash
))
9066 abfd
->flags
|= DYNAMIC
;
9068 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9069 dynobj
= elf_hash_table (info
)->dynobj
;
9071 emit_relocs
= (info
->relocatable
9072 || info
->emitrelocations
);
9075 finfo
.output_bfd
= abfd
;
9076 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
9077 if (finfo
.symstrtab
== NULL
)
9082 finfo
.dynsym_sec
= NULL
;
9083 finfo
.hash_sec
= NULL
;
9084 finfo
.symver_sec
= NULL
;
9088 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
9089 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
9090 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
9091 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
9092 /* Note that it is OK if symver_sec is NULL. */
9095 finfo
.contents
= NULL
;
9096 finfo
.external_relocs
= NULL
;
9097 finfo
.internal_relocs
= NULL
;
9098 finfo
.external_syms
= NULL
;
9099 finfo
.locsym_shndx
= NULL
;
9100 finfo
.internal_syms
= NULL
;
9101 finfo
.indices
= NULL
;
9102 finfo
.sections
= NULL
;
9103 finfo
.symbuf
= NULL
;
9104 finfo
.symshndxbuf
= NULL
;
9105 finfo
.symbuf_count
= 0;
9106 finfo
.shndxbuf_size
= 0;
9108 /* Count up the number of relocations we will output for each output
9109 section, so that we know the sizes of the reloc sections. We
9110 also figure out some maximum sizes. */
9111 max_contents_size
= 0;
9112 max_external_reloc_size
= 0;
9113 max_internal_reloc_count
= 0;
9115 max_sym_shndx_count
= 0;
9117 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9119 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
9122 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9124 unsigned int reloc_count
= 0;
9125 struct bfd_elf_section_data
*esdi
= NULL
;
9126 unsigned int *rel_count1
;
9128 if (p
->type
== bfd_section_reloc_link_order
9129 || p
->type
== bfd_symbol_reloc_link_order
)
9131 else if (p
->type
== bfd_indirect_link_order
)
9135 sec
= p
->u
.indirect
.section
;
9136 esdi
= elf_section_data (sec
);
9138 /* Mark all sections which are to be included in the
9139 link. This will normally be every section. We need
9140 to do this so that we can identify any sections which
9141 the linker has decided to not include. */
9142 sec
->linker_mark
= TRUE
;
9144 if (sec
->flags
& SEC_MERGE
)
9147 if (info
->relocatable
|| info
->emitrelocations
)
9148 reloc_count
= sec
->reloc_count
;
9149 else if (bed
->elf_backend_count_relocs
)
9151 Elf_Internal_Rela
* relocs
;
9153 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
9156 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
9158 if (elf_section_data (o
)->relocs
!= relocs
)
9162 if (sec
->rawsize
> max_contents_size
)
9163 max_contents_size
= sec
->rawsize
;
9164 if (sec
->size
> max_contents_size
)
9165 max_contents_size
= sec
->size
;
9167 /* We are interested in just local symbols, not all
9169 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
9170 && (sec
->owner
->flags
& DYNAMIC
) == 0)
9174 if (elf_bad_symtab (sec
->owner
))
9175 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
9176 / bed
->s
->sizeof_sym
);
9178 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
9180 if (sym_count
> max_sym_count
)
9181 max_sym_count
= sym_count
;
9183 if (sym_count
> max_sym_shndx_count
9184 && elf_symtab_shndx (sec
->owner
) != 0)
9185 max_sym_shndx_count
= sym_count
;
9187 if ((sec
->flags
& SEC_RELOC
) != 0)
9191 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
9192 if (ext_size
> max_external_reloc_size
)
9193 max_external_reloc_size
= ext_size
;
9194 if (sec
->reloc_count
> max_internal_reloc_count
)
9195 max_internal_reloc_count
= sec
->reloc_count
;
9200 if (reloc_count
== 0)
9203 o
->reloc_count
+= reloc_count
;
9205 /* MIPS may have a mix of REL and RELA relocs on sections.
9206 To support this curious ABI we keep reloc counts in
9207 elf_section_data too. We must be careful to add the
9208 relocations from the input section to the right output
9209 count. FIXME: Get rid of one count. We have
9210 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9211 rel_count1
= &esdo
->rel_count
;
9214 bfd_boolean same_size
;
9215 bfd_size_type entsize1
;
9217 entsize1
= esdi
->rel_hdr
.sh_entsize
;
9218 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
9219 || entsize1
== bed
->s
->sizeof_rela
);
9220 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
9223 rel_count1
= &esdo
->rel_count2
;
9225 if (esdi
->rel_hdr2
!= NULL
)
9227 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
9228 unsigned int alt_count
;
9229 unsigned int *rel_count2
;
9231 BFD_ASSERT (entsize2
!= entsize1
9232 && (entsize2
== bed
->s
->sizeof_rel
9233 || entsize2
== bed
->s
->sizeof_rela
));
9235 rel_count2
= &esdo
->rel_count2
;
9237 rel_count2
= &esdo
->rel_count
;
9239 /* The following is probably too simplistic if the
9240 backend counts output relocs unusually. */
9241 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
9242 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
9243 *rel_count2
+= alt_count
;
9244 reloc_count
-= alt_count
;
9247 *rel_count1
+= reloc_count
;
9250 if (o
->reloc_count
> 0)
9251 o
->flags
|= SEC_RELOC
;
9254 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9255 set it (this is probably a bug) and if it is set
9256 assign_section_numbers will create a reloc section. */
9257 o
->flags
&=~ SEC_RELOC
;
9260 /* If the SEC_ALLOC flag is not set, force the section VMA to
9261 zero. This is done in elf_fake_sections as well, but forcing
9262 the VMA to 0 here will ensure that relocs against these
9263 sections are handled correctly. */
9264 if ((o
->flags
& SEC_ALLOC
) == 0
9265 && ! o
->user_set_vma
)
9269 if (! info
->relocatable
&& merged
)
9270 elf_link_hash_traverse (elf_hash_table (info
),
9271 _bfd_elf_link_sec_merge_syms
, abfd
);
9273 /* Figure out the file positions for everything but the symbol table
9274 and the relocs. We set symcount to force assign_section_numbers
9275 to create a symbol table. */
9276 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
9277 BFD_ASSERT (! abfd
->output_has_begun
);
9278 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
9281 /* Set sizes, and assign file positions for reloc sections. */
9282 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9284 if ((o
->flags
& SEC_RELOC
) != 0)
9286 if (!(_bfd_elf_link_size_reloc_section
9287 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
9290 if (elf_section_data (o
)->rel_hdr2
9291 && !(_bfd_elf_link_size_reloc_section
9292 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
9296 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9297 to count upwards while actually outputting the relocations. */
9298 elf_section_data (o
)->rel_count
= 0;
9299 elf_section_data (o
)->rel_count2
= 0;
9302 _bfd_elf_assign_file_positions_for_relocs (abfd
);
9304 /* We have now assigned file positions for all the sections except
9305 .symtab and .strtab. We start the .symtab section at the current
9306 file position, and write directly to it. We build the .strtab
9307 section in memory. */
9308 bfd_get_symcount (abfd
) = 0;
9309 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9310 /* sh_name is set in prep_headers. */
9311 symtab_hdr
->sh_type
= SHT_SYMTAB
;
9312 /* sh_flags, sh_addr and sh_size all start off zero. */
9313 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
9314 /* sh_link is set in assign_section_numbers. */
9315 /* sh_info is set below. */
9316 /* sh_offset is set just below. */
9317 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
9319 off
= elf_tdata (abfd
)->next_file_pos
;
9320 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
9322 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9323 incorrect. We do not yet know the size of the .symtab section.
9324 We correct next_file_pos below, after we do know the size. */
9326 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9327 continuously seeking to the right position in the file. */
9328 if (! info
->keep_memory
|| max_sym_count
< 20)
9329 finfo
.symbuf_size
= 20;
9331 finfo
.symbuf_size
= max_sym_count
;
9332 amt
= finfo
.symbuf_size
;
9333 amt
*= bed
->s
->sizeof_sym
;
9334 finfo
.symbuf
= bfd_malloc (amt
);
9335 if (finfo
.symbuf
== NULL
)
9337 if (elf_numsections (abfd
) > SHN_LORESERVE
)
9339 /* Wild guess at number of output symbols. realloc'd as needed. */
9340 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
9341 finfo
.shndxbuf_size
= amt
;
9342 amt
*= sizeof (Elf_External_Sym_Shndx
);
9343 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
9344 if (finfo
.symshndxbuf
== NULL
)
9348 /* Start writing out the symbol table. The first symbol is always a
9350 if (info
->strip
!= strip_all
9353 elfsym
.st_value
= 0;
9356 elfsym
.st_other
= 0;
9357 elfsym
.st_shndx
= SHN_UNDEF
;
9358 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
9363 /* Output a symbol for each section. We output these even if we are
9364 discarding local symbols, since they are used for relocs. These
9365 symbols have no names. We store the index of each one in the
9366 index field of the section, so that we can find it again when
9367 outputting relocs. */
9368 if (info
->strip
!= strip_all
9372 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9373 elfsym
.st_other
= 0;
9374 elfsym
.st_value
= 0;
9375 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9377 o
= bfd_section_from_elf_index (abfd
, i
);
9380 o
->target_index
= bfd_get_symcount (abfd
);
9381 elfsym
.st_shndx
= i
;
9382 if (!info
->relocatable
)
9383 elfsym
.st_value
= o
->vma
;
9384 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
9387 if (i
== SHN_LORESERVE
- 1)
9388 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
9392 /* Allocate some memory to hold information read in from the input
9394 if (max_contents_size
!= 0)
9396 finfo
.contents
= bfd_malloc (max_contents_size
);
9397 if (finfo
.contents
== NULL
)
9401 if (max_external_reloc_size
!= 0)
9403 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
9404 if (finfo
.external_relocs
== NULL
)
9408 if (max_internal_reloc_count
!= 0)
9410 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9411 amt
*= sizeof (Elf_Internal_Rela
);
9412 finfo
.internal_relocs
= bfd_malloc (amt
);
9413 if (finfo
.internal_relocs
== NULL
)
9417 if (max_sym_count
!= 0)
9419 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
9420 finfo
.external_syms
= bfd_malloc (amt
);
9421 if (finfo
.external_syms
== NULL
)
9424 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
9425 finfo
.internal_syms
= bfd_malloc (amt
);
9426 if (finfo
.internal_syms
== NULL
)
9429 amt
= max_sym_count
* sizeof (long);
9430 finfo
.indices
= bfd_malloc (amt
);
9431 if (finfo
.indices
== NULL
)
9434 amt
= max_sym_count
* sizeof (asection
*);
9435 finfo
.sections
= bfd_malloc (amt
);
9436 if (finfo
.sections
== NULL
)
9440 if (max_sym_shndx_count
!= 0)
9442 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
9443 finfo
.locsym_shndx
= bfd_malloc (amt
);
9444 if (finfo
.locsym_shndx
== NULL
)
9448 if (elf_hash_table (info
)->tls_sec
)
9450 bfd_vma base
, end
= 0;
9453 for (sec
= elf_hash_table (info
)->tls_sec
;
9454 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
9457 bfd_size_type size
= sec
->size
;
9460 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
9462 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
9464 size
= o
->offset
+ o
->size
;
9466 end
= sec
->vma
+ size
;
9468 base
= elf_hash_table (info
)->tls_sec
->vma
;
9469 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
9470 elf_hash_table (info
)->tls_size
= end
- base
;
9473 /* Reorder SHF_LINK_ORDER sections. */
9474 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9476 if (!elf_fixup_link_order (abfd
, o
))
9480 /* Since ELF permits relocations to be against local symbols, we
9481 must have the local symbols available when we do the relocations.
9482 Since we would rather only read the local symbols once, and we
9483 would rather not keep them in memory, we handle all the
9484 relocations for a single input file at the same time.
9486 Unfortunately, there is no way to know the total number of local
9487 symbols until we have seen all of them, and the local symbol
9488 indices precede the global symbol indices. This means that when
9489 we are generating relocatable output, and we see a reloc against
9490 a global symbol, we can not know the symbol index until we have
9491 finished examining all the local symbols to see which ones we are
9492 going to output. To deal with this, we keep the relocations in
9493 memory, and don't output them until the end of the link. This is
9494 an unfortunate waste of memory, but I don't see a good way around
9495 it. Fortunately, it only happens when performing a relocatable
9496 link, which is not the common case. FIXME: If keep_memory is set
9497 we could write the relocs out and then read them again; I don't
9498 know how bad the memory loss will be. */
9500 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9501 sub
->output_has_begun
= FALSE
;
9502 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9504 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9506 if (p
->type
== bfd_indirect_link_order
9507 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
9508 == bfd_target_elf_flavour
)
9509 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
9511 if (! sub
->output_has_begun
)
9513 if (! elf_link_input_bfd (&finfo
, sub
))
9515 sub
->output_has_begun
= TRUE
;
9518 else if (p
->type
== bfd_section_reloc_link_order
9519 || p
->type
== bfd_symbol_reloc_link_order
)
9521 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
9526 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
9532 /* Free symbol buffer if needed. */
9533 if (!info
->reduce_memory_overheads
)
9535 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9536 if (elf_tdata (sub
)->symbuf
)
9538 free (elf_tdata (sub
)->symbuf
);
9539 elf_tdata (sub
)->symbuf
= NULL
;
9543 /* Output any global symbols that got converted to local in a
9544 version script or due to symbol visibility. We do this in a
9545 separate step since ELF requires all local symbols to appear
9546 prior to any global symbols. FIXME: We should only do this if
9547 some global symbols were, in fact, converted to become local.
9548 FIXME: Will this work correctly with the Irix 5 linker? */
9549 eoinfo
.failed
= FALSE
;
9550 eoinfo
.finfo
= &finfo
;
9551 eoinfo
.localsyms
= TRUE
;
9552 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9557 /* If backend needs to output some local symbols not present in the hash
9558 table, do it now. */
9559 if (bed
->elf_backend_output_arch_local_syms
)
9561 typedef bfd_boolean (*out_sym_func
)
9562 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9563 struct elf_link_hash_entry
*);
9565 if (! ((*bed
->elf_backend_output_arch_local_syms
)
9566 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9570 /* That wrote out all the local symbols. Finish up the symbol table
9571 with the global symbols. Even if we want to strip everything we
9572 can, we still need to deal with those global symbols that got
9573 converted to local in a version script. */
9575 /* The sh_info field records the index of the first non local symbol. */
9576 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
9579 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
9581 Elf_Internal_Sym sym
;
9582 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
9583 long last_local
= 0;
9585 /* Write out the section symbols for the output sections. */
9586 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
9592 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
9595 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
9601 dynindx
= elf_section_data (s
)->dynindx
;
9604 indx
= elf_section_data (s
)->this_idx
;
9605 BFD_ASSERT (indx
> 0);
9606 sym
.st_shndx
= indx
;
9607 if (! check_dynsym (abfd
, &sym
))
9609 sym
.st_value
= s
->vma
;
9610 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
9611 if (last_local
< dynindx
)
9612 last_local
= dynindx
;
9613 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9617 /* Write out the local dynsyms. */
9618 if (elf_hash_table (info
)->dynlocal
)
9620 struct elf_link_local_dynamic_entry
*e
;
9621 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
9626 sym
.st_size
= e
->isym
.st_size
;
9627 sym
.st_other
= e
->isym
.st_other
;
9629 /* Copy the internal symbol as is.
9630 Note that we saved a word of storage and overwrote
9631 the original st_name with the dynstr_index. */
9634 if (e
->isym
.st_shndx
!= SHN_UNDEF
9635 && (e
->isym
.st_shndx
< SHN_LORESERVE
9636 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
9638 s
= bfd_section_from_elf_index (e
->input_bfd
,
9642 elf_section_data (s
->output_section
)->this_idx
;
9643 if (! check_dynsym (abfd
, &sym
))
9645 sym
.st_value
= (s
->output_section
->vma
9647 + e
->isym
.st_value
);
9650 if (last_local
< e
->dynindx
)
9651 last_local
= e
->dynindx
;
9653 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
9654 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
9658 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
9662 /* We get the global symbols from the hash table. */
9663 eoinfo
.failed
= FALSE
;
9664 eoinfo
.localsyms
= FALSE
;
9665 eoinfo
.finfo
= &finfo
;
9666 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
9671 /* If backend needs to output some symbols not present in the hash
9672 table, do it now. */
9673 if (bed
->elf_backend_output_arch_syms
)
9675 typedef bfd_boolean (*out_sym_func
)
9676 (void *, const char *, Elf_Internal_Sym
*, asection
*,
9677 struct elf_link_hash_entry
*);
9679 if (! ((*bed
->elf_backend_output_arch_syms
)
9680 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
9684 /* Flush all symbols to the file. */
9685 if (! elf_link_flush_output_syms (&finfo
, bed
))
9688 /* Now we know the size of the symtab section. */
9689 off
+= symtab_hdr
->sh_size
;
9691 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
9692 if (symtab_shndx_hdr
->sh_name
!= 0)
9694 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
9695 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
9696 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
9697 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
9698 symtab_shndx_hdr
->sh_size
= amt
;
9700 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
9703 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
9704 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
9709 /* Finish up and write out the symbol string table (.strtab)
9711 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
9712 /* sh_name was set in prep_headers. */
9713 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
9714 symstrtab_hdr
->sh_flags
= 0;
9715 symstrtab_hdr
->sh_addr
= 0;
9716 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
9717 symstrtab_hdr
->sh_entsize
= 0;
9718 symstrtab_hdr
->sh_link
= 0;
9719 symstrtab_hdr
->sh_info
= 0;
9720 /* sh_offset is set just below. */
9721 symstrtab_hdr
->sh_addralign
= 1;
9723 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
9724 elf_tdata (abfd
)->next_file_pos
= off
;
9726 if (bfd_get_symcount (abfd
) > 0)
9728 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
9729 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
9733 /* Adjust the relocs to have the correct symbol indices. */
9734 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
9736 if ((o
->flags
& SEC_RELOC
) == 0)
9739 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
9740 elf_section_data (o
)->rel_count
,
9741 elf_section_data (o
)->rel_hashes
);
9742 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
9743 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
9744 elf_section_data (o
)->rel_count2
,
9745 (elf_section_data (o
)->rel_hashes
9746 + elf_section_data (o
)->rel_count
));
9748 /* Set the reloc_count field to 0 to prevent write_relocs from
9749 trying to swap the relocs out itself. */
9753 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
9754 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
9756 /* If we are linking against a dynamic object, or generating a
9757 shared library, finish up the dynamic linking information. */
9760 bfd_byte
*dyncon
, *dynconend
;
9762 /* Fix up .dynamic entries. */
9763 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9764 BFD_ASSERT (o
!= NULL
);
9766 dyncon
= o
->contents
;
9767 dynconend
= o
->contents
+ o
->size
;
9768 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9770 Elf_Internal_Dyn dyn
;
9774 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9781 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
9783 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
9785 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
9786 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
9789 dyn
.d_un
.d_val
= relativecount
;
9796 name
= info
->init_function
;
9799 name
= info
->fini_function
;
9802 struct elf_link_hash_entry
*h
;
9804 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
9805 FALSE
, FALSE
, TRUE
);
9807 && (h
->root
.type
== bfd_link_hash_defined
9808 || h
->root
.type
== bfd_link_hash_defweak
))
9810 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
9811 o
= h
->root
.u
.def
.section
;
9812 if (o
->output_section
!= NULL
)
9813 dyn
.d_un
.d_val
+= (o
->output_section
->vma
9814 + o
->output_offset
);
9817 /* The symbol is imported from another shared
9818 library and does not apply to this one. */
9826 case DT_PREINIT_ARRAYSZ
:
9827 name
= ".preinit_array";
9829 case DT_INIT_ARRAYSZ
:
9830 name
= ".init_array";
9832 case DT_FINI_ARRAYSZ
:
9833 name
= ".fini_array";
9835 o
= bfd_get_section_by_name (abfd
, name
);
9838 (*_bfd_error_handler
)
9839 (_("%B: could not find output section %s"), abfd
, name
);
9843 (*_bfd_error_handler
)
9844 (_("warning: %s section has zero size"), name
);
9845 dyn
.d_un
.d_val
= o
->size
;
9848 case DT_PREINIT_ARRAY
:
9849 name
= ".preinit_array";
9852 name
= ".init_array";
9855 name
= ".fini_array";
9871 name
= ".gnu.version_d";
9874 name
= ".gnu.version_r";
9877 name
= ".gnu.version";
9879 o
= bfd_get_section_by_name (abfd
, name
);
9882 (*_bfd_error_handler
)
9883 (_("%B: could not find output section %s"), abfd
, name
);
9886 dyn
.d_un
.d_ptr
= o
->vma
;
9893 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
9898 for (i
= 1; i
< elf_numsections (abfd
); i
++)
9900 Elf_Internal_Shdr
*hdr
;
9902 hdr
= elf_elfsections (abfd
)[i
];
9903 if (hdr
->sh_type
== type
9904 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
9906 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
9907 dyn
.d_un
.d_val
+= hdr
->sh_size
;
9910 if (dyn
.d_un
.d_val
== 0
9911 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
9912 dyn
.d_un
.d_val
= hdr
->sh_addr
;
9918 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
9922 /* If we have created any dynamic sections, then output them. */
9925 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
9928 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9929 if (info
->warn_shared_textrel
&& info
->shared
)
9931 bfd_byte
*dyncon
, *dynconend
;
9933 /* Fix up .dynamic entries. */
9934 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
9935 BFD_ASSERT (o
!= NULL
);
9937 dyncon
= o
->contents
;
9938 dynconend
= o
->contents
+ o
->size
;
9939 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
9941 Elf_Internal_Dyn dyn
;
9943 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
9945 if (dyn
.d_tag
== DT_TEXTREL
)
9947 info
->callbacks
->einfo
9948 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
9954 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
9956 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9958 || o
->output_section
== bfd_abs_section_ptr
)
9960 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
9962 /* At this point, we are only interested in sections
9963 created by _bfd_elf_link_create_dynamic_sections. */
9966 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
9968 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
9970 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
9972 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
9974 if (! bfd_set_section_contents (abfd
, o
->output_section
,
9976 (file_ptr
) o
->output_offset
,
9982 /* The contents of the .dynstr section are actually in a
9984 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
9985 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
9986 || ! _bfd_elf_strtab_emit (abfd
,
9987 elf_hash_table (info
)->dynstr
))
9993 if (info
->relocatable
)
9995 bfd_boolean failed
= FALSE
;
9997 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10002 /* If we have optimized stabs strings, output them. */
10003 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10005 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10009 if (info
->eh_frame_hdr
)
10011 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10015 if (finfo
.symstrtab
!= NULL
)
10016 _bfd_stringtab_free (finfo
.symstrtab
);
10017 if (finfo
.contents
!= NULL
)
10018 free (finfo
.contents
);
10019 if (finfo
.external_relocs
!= NULL
)
10020 free (finfo
.external_relocs
);
10021 if (finfo
.internal_relocs
!= NULL
)
10022 free (finfo
.internal_relocs
);
10023 if (finfo
.external_syms
!= NULL
)
10024 free (finfo
.external_syms
);
10025 if (finfo
.locsym_shndx
!= NULL
)
10026 free (finfo
.locsym_shndx
);
10027 if (finfo
.internal_syms
!= NULL
)
10028 free (finfo
.internal_syms
);
10029 if (finfo
.indices
!= NULL
)
10030 free (finfo
.indices
);
10031 if (finfo
.sections
!= NULL
)
10032 free (finfo
.sections
);
10033 if (finfo
.symbuf
!= NULL
)
10034 free (finfo
.symbuf
);
10035 if (finfo
.symshndxbuf
!= NULL
)
10036 free (finfo
.symshndxbuf
);
10037 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10039 if ((o
->flags
& SEC_RELOC
) != 0
10040 && elf_section_data (o
)->rel_hashes
!= NULL
)
10041 free (elf_section_data (o
)->rel_hashes
);
10044 elf_tdata (abfd
)->linker
= TRUE
;
10049 if (finfo
.symstrtab
!= NULL
)
10050 _bfd_stringtab_free (finfo
.symstrtab
);
10051 if (finfo
.contents
!= NULL
)
10052 free (finfo
.contents
);
10053 if (finfo
.external_relocs
!= NULL
)
10054 free (finfo
.external_relocs
);
10055 if (finfo
.internal_relocs
!= NULL
)
10056 free (finfo
.internal_relocs
);
10057 if (finfo
.external_syms
!= NULL
)
10058 free (finfo
.external_syms
);
10059 if (finfo
.locsym_shndx
!= NULL
)
10060 free (finfo
.locsym_shndx
);
10061 if (finfo
.internal_syms
!= NULL
)
10062 free (finfo
.internal_syms
);
10063 if (finfo
.indices
!= NULL
)
10064 free (finfo
.indices
);
10065 if (finfo
.sections
!= NULL
)
10066 free (finfo
.sections
);
10067 if (finfo
.symbuf
!= NULL
)
10068 free (finfo
.symbuf
);
10069 if (finfo
.symshndxbuf
!= NULL
)
10070 free (finfo
.symshndxbuf
);
10071 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10073 if ((o
->flags
& SEC_RELOC
) != 0
10074 && elf_section_data (o
)->rel_hashes
!= NULL
)
10075 free (elf_section_data (o
)->rel_hashes
);
10081 /* Garbage collect unused sections. */
10083 /* Default gc_mark_hook. */
10086 _bfd_elf_gc_mark_hook (asection
*sec
,
10087 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10088 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
10089 struct elf_link_hash_entry
*h
,
10090 Elf_Internal_Sym
*sym
)
10094 switch (h
->root
.type
)
10096 case bfd_link_hash_defined
:
10097 case bfd_link_hash_defweak
:
10098 return h
->root
.u
.def
.section
;
10100 case bfd_link_hash_common
:
10101 return h
->root
.u
.c
.p
->section
;
10108 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
10113 /* The mark phase of garbage collection. For a given section, mark
10114 it and any sections in this section's group, and all the sections
10115 which define symbols to which it refers. */
10118 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
10120 elf_gc_mark_hook_fn gc_mark_hook
)
10124 asection
*group_sec
;
10128 /* Mark all the sections in the group. */
10129 group_sec
= elf_section_data (sec
)->next_in_group
;
10130 if (group_sec
&& !group_sec
->gc_mark
)
10131 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
10134 /* Look through the section relocs. */
10136 is_eh
= strcmp (sec
->name
, ".eh_frame") == 0;
10137 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
10139 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
10140 Elf_Internal_Shdr
*symtab_hdr
;
10141 struct elf_link_hash_entry
**sym_hashes
;
10144 bfd
*input_bfd
= sec
->owner
;
10145 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
10146 Elf_Internal_Sym
*isym
= NULL
;
10149 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10150 sym_hashes
= elf_sym_hashes (input_bfd
);
10152 /* Read the local symbols. */
10153 if (elf_bad_symtab (input_bfd
))
10155 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10159 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
10161 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10162 if (isym
== NULL
&& nlocsyms
!= 0)
10164 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
10170 /* Read the relocations. */
10171 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
10172 info
->keep_memory
);
10173 if (relstart
== NULL
)
10178 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10180 if (bed
->s
->arch_size
== 32)
10185 for (rel
= relstart
; rel
< relend
; rel
++)
10187 unsigned long r_symndx
;
10189 struct elf_link_hash_entry
*h
;
10191 r_symndx
= rel
->r_info
>> r_sym_shift
;
10195 if (r_symndx
>= nlocsyms
10196 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
10198 h
= sym_hashes
[r_symndx
- extsymoff
];
10199 while (h
->root
.type
== bfd_link_hash_indirect
10200 || h
->root
.type
== bfd_link_hash_warning
)
10201 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10202 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
10206 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
10209 if (rsec
&& !rsec
->gc_mark
)
10211 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
10214 rsec
->gc_mark_from_eh
= 1;
10215 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
10224 if (elf_section_data (sec
)->relocs
!= relstart
)
10227 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
10229 if (! info
->keep_memory
)
10232 symtab_hdr
->contents
= (unsigned char *) isym
;
10239 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10241 struct elf_gc_sweep_symbol_info
10243 struct bfd_link_info
*info
;
10244 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
10249 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
10251 if (h
->root
.type
== bfd_link_hash_warning
)
10252 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10254 if ((h
->root
.type
== bfd_link_hash_defined
10255 || h
->root
.type
== bfd_link_hash_defweak
)
10256 && !h
->root
.u
.def
.section
->gc_mark
10257 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
10259 struct elf_gc_sweep_symbol_info
*inf
= data
;
10260 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
10266 /* The sweep phase of garbage collection. Remove all garbage sections. */
10268 typedef bfd_boolean (*gc_sweep_hook_fn
)
10269 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
10272 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
10275 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10276 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
10277 unsigned long section_sym_count
;
10278 struct elf_gc_sweep_symbol_info sweep_info
;
10280 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10284 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10287 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10289 /* Keep debug and special sections. */
10290 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
10291 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
10297 /* Skip sweeping sections already excluded. */
10298 if (o
->flags
& SEC_EXCLUDE
)
10301 /* Since this is early in the link process, it is simple
10302 to remove a section from the output. */
10303 o
->flags
|= SEC_EXCLUDE
;
10305 if (info
->print_gc_sections
== TRUE
)
10306 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
10308 /* But we also have to update some of the relocation
10309 info we collected before. */
10311 && (o
->flags
& SEC_RELOC
) != 0
10312 && o
->reloc_count
> 0
10313 && !bfd_is_abs_section (o
->output_section
))
10315 Elf_Internal_Rela
*internal_relocs
;
10319 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
10320 info
->keep_memory
);
10321 if (internal_relocs
== NULL
)
10324 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
10326 if (elf_section_data (o
)->relocs
!= internal_relocs
)
10327 free (internal_relocs
);
10335 /* Remove the symbols that were in the swept sections from the dynamic
10336 symbol table. GCFIXME: Anyone know how to get them out of the
10337 static symbol table as well? */
10338 sweep_info
.info
= info
;
10339 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
10340 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
10343 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
10347 /* Propagate collected vtable information. This is called through
10348 elf_link_hash_traverse. */
10351 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
10353 if (h
->root
.type
== bfd_link_hash_warning
)
10354 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10356 /* Those that are not vtables. */
10357 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10360 /* Those vtables that do not have parents, we cannot merge. */
10361 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
10364 /* If we've already been done, exit. */
10365 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
10368 /* Make sure the parent's table is up to date. */
10369 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
10371 if (h
->vtable
->used
== NULL
)
10373 /* None of this table's entries were referenced. Re-use the
10375 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
10376 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
10381 bfd_boolean
*cu
, *pu
;
10383 /* Or the parent's entries into ours. */
10384 cu
= h
->vtable
->used
;
10386 pu
= h
->vtable
->parent
->vtable
->used
;
10389 const struct elf_backend_data
*bed
;
10390 unsigned int log_file_align
;
10392 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
10393 log_file_align
= bed
->s
->log_file_align
;
10394 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
10409 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
10412 bfd_vma hstart
, hend
;
10413 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
10414 const struct elf_backend_data
*bed
;
10415 unsigned int log_file_align
;
10417 if (h
->root
.type
== bfd_link_hash_warning
)
10418 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10420 /* Take care of both those symbols that do not describe vtables as
10421 well as those that are not loaded. */
10422 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
10425 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
10426 || h
->root
.type
== bfd_link_hash_defweak
);
10428 sec
= h
->root
.u
.def
.section
;
10429 hstart
= h
->root
.u
.def
.value
;
10430 hend
= hstart
+ h
->size
;
10432 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
10434 return *(bfd_boolean
*) okp
= FALSE
;
10435 bed
= get_elf_backend_data (sec
->owner
);
10436 log_file_align
= bed
->s
->log_file_align
;
10438 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10440 for (rel
= relstart
; rel
< relend
; ++rel
)
10441 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
10443 /* If the entry is in use, do nothing. */
10444 if (h
->vtable
->used
10445 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
10447 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
10448 if (h
->vtable
->used
[entry
])
10451 /* Otherwise, kill it. */
10452 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
10458 /* Mark sections containing dynamically referenced symbols. When
10459 building shared libraries, we must assume that any visible symbol is
10463 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
10465 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
10467 if (h
->root
.type
== bfd_link_hash_warning
)
10468 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10470 if ((h
->root
.type
== bfd_link_hash_defined
10471 || h
->root
.type
== bfd_link_hash_defweak
)
10473 || (!info
->executable
10475 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
10476 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
10477 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
10482 /* Do mark and sweep of unused sections. */
10485 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
10487 bfd_boolean ok
= TRUE
;
10489 elf_gc_mark_hook_fn gc_mark_hook
;
10490 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10492 if (!bed
->can_gc_sections
10493 || info
->relocatable
10494 || info
->emitrelocations
10495 || !is_elf_hash_table (info
->hash
))
10497 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
10501 /* Apply transitive closure to the vtable entry usage info. */
10502 elf_link_hash_traverse (elf_hash_table (info
),
10503 elf_gc_propagate_vtable_entries_used
,
10508 /* Kill the vtable relocations that were not used. */
10509 elf_link_hash_traverse (elf_hash_table (info
),
10510 elf_gc_smash_unused_vtentry_relocs
,
10515 /* Mark dynamically referenced symbols. */
10516 if (elf_hash_table (info
)->dynamic_sections_created
)
10517 elf_link_hash_traverse (elf_hash_table (info
),
10518 bed
->gc_mark_dynamic_ref
,
10521 /* Grovel through relocs to find out who stays ... */
10522 gc_mark_hook
= bed
->gc_mark_hook
;
10523 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10527 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10530 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10531 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
10532 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10536 /* Allow the backend to mark additional target specific sections. */
10537 if (bed
->gc_mark_extra_sections
)
10538 bed
->gc_mark_extra_sections(info
, gc_mark_hook
);
10540 /* ... again for sections marked from eh_frame. */
10541 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10545 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
10548 /* Keep .gcc_except_table.* if the associated .text.* (or the
10549 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10550 marked. This isn't very nice, but the proper solution,
10551 splitting .eh_frame up and using comdat doesn't pan out
10552 easily due to needing special relocs to handle the
10553 difference of two symbols in separate sections.
10554 Don't keep code sections referenced by .eh_frame. */
10555 #define TEXT_PREFIX ".text."
10556 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10557 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10558 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
10559 if (!o
->gc_mark
&& o
->gc_mark_from_eh
&& (o
->flags
& SEC_CODE
) == 0)
10561 if (CONST_STRNEQ (o
->name
, GCC_EXCEPT_TABLE_PREFIX
))
10564 const char *sec_name
;
10566 unsigned o_name_prefix_len
, fn_name_prefix_len
, tmp
;
10568 o_name_prefix_len
= strlen (GCC_EXCEPT_TABLE_PREFIX
);
10569 sec_name
= o
->name
+ o_name_prefix_len
;
10570 fn_name_prefix_len
= strlen (TEXT_PREFIX
);
10571 tmp
= strlen (TEXT_PREFIX2
);
10572 if (tmp
> fn_name_prefix_len
)
10573 fn_name_prefix_len
= tmp
;
10575 = bfd_malloc (fn_name_prefix_len
+ strlen (sec_name
) + 1);
10576 if (fn_name
== NULL
)
10579 /* Try the first prefix. */
10580 sprintf (fn_name
, "%s%s", TEXT_PREFIX
, sec_name
);
10581 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10583 /* Try the second prefix. */
10584 if (fn_text
== NULL
)
10586 sprintf (fn_name
, "%s%s", TEXT_PREFIX2
, sec_name
);
10587 fn_text
= bfd_get_section_by_name (sub
, fn_name
);
10591 if (fn_text
== NULL
|| !fn_text
->gc_mark
)
10595 /* If not using specially named exception table section,
10596 then keep whatever we are using. */
10597 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
10602 /* ... and mark SEC_EXCLUDE for those that go. */
10603 return elf_gc_sweep (abfd
, info
);
10606 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10609 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
10611 struct elf_link_hash_entry
*h
,
10614 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
10615 struct elf_link_hash_entry
**search
, *child
;
10616 bfd_size_type extsymcount
;
10617 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10619 /* The sh_info field of the symtab header tells us where the
10620 external symbols start. We don't care about the local symbols at
10622 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
10623 if (!elf_bad_symtab (abfd
))
10624 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
10626 sym_hashes
= elf_sym_hashes (abfd
);
10627 sym_hashes_end
= sym_hashes
+ extsymcount
;
10629 /* Hunt down the child symbol, which is in this section at the same
10630 offset as the relocation. */
10631 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
10633 if ((child
= *search
) != NULL
10634 && (child
->root
.type
== bfd_link_hash_defined
10635 || child
->root
.type
== bfd_link_hash_defweak
)
10636 && child
->root
.u
.def
.section
== sec
10637 && child
->root
.u
.def
.value
== offset
)
10641 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
10642 abfd
, sec
, (unsigned long) offset
);
10643 bfd_set_error (bfd_error_invalid_operation
);
10647 if (!child
->vtable
)
10649 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
10650 if (!child
->vtable
)
10655 /* This *should* only be the absolute section. It could potentially
10656 be that someone has defined a non-global vtable though, which
10657 would be bad. It isn't worth paging in the local symbols to be
10658 sure though; that case should simply be handled by the assembler. */
10660 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
10663 child
->vtable
->parent
= h
;
10668 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10671 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
10672 asection
*sec ATTRIBUTE_UNUSED
,
10673 struct elf_link_hash_entry
*h
,
10676 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10677 unsigned int log_file_align
= bed
->s
->log_file_align
;
10681 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
10686 if (addend
>= h
->vtable
->size
)
10688 size_t size
, bytes
, file_align
;
10689 bfd_boolean
*ptr
= h
->vtable
->used
;
10691 /* While the symbol is undefined, we have to be prepared to handle
10693 file_align
= 1 << log_file_align
;
10694 if (h
->root
.type
== bfd_link_hash_undefined
)
10695 size
= addend
+ file_align
;
10699 if (addend
>= size
)
10701 /* Oops! We've got a reference past the defined end of
10702 the table. This is probably a bug -- shall we warn? */
10703 size
= addend
+ file_align
;
10706 size
= (size
+ file_align
- 1) & -file_align
;
10708 /* Allocate one extra entry for use as a "done" flag for the
10709 consolidation pass. */
10710 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
10714 ptr
= bfd_realloc (ptr
- 1, bytes
);
10720 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
10721 * sizeof (bfd_boolean
));
10722 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
10726 ptr
= bfd_zmalloc (bytes
);
10731 /* And arrange for that done flag to be at index -1. */
10732 h
->vtable
->used
= ptr
+ 1;
10733 h
->vtable
->size
= size
;
10736 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
10741 struct alloc_got_off_arg
{
10743 unsigned int got_elt_size
;
10746 /* We need a special top-level link routine to convert got reference counts
10747 to real got offsets. */
10750 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
10752 struct alloc_got_off_arg
*gofarg
= arg
;
10754 if (h
->root
.type
== bfd_link_hash_warning
)
10755 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10757 if (h
->got
.refcount
> 0)
10759 h
->got
.offset
= gofarg
->gotoff
;
10760 gofarg
->gotoff
+= gofarg
->got_elt_size
;
10763 h
->got
.offset
= (bfd_vma
) -1;
10768 /* And an accompanying bit to work out final got entry offsets once
10769 we're done. Should be called from final_link. */
10772 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
10773 struct bfd_link_info
*info
)
10776 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10778 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
10779 struct alloc_got_off_arg gofarg
;
10781 if (! is_elf_hash_table (info
->hash
))
10784 /* The GOT offset is relative to the .got section, but the GOT header is
10785 put into the .got.plt section, if the backend uses it. */
10786 if (bed
->want_got_plt
)
10789 gotoff
= bed
->got_header_size
;
10791 /* Do the local .got entries first. */
10792 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
10794 bfd_signed_vma
*local_got
;
10795 bfd_size_type j
, locsymcount
;
10796 Elf_Internal_Shdr
*symtab_hdr
;
10798 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
10801 local_got
= elf_local_got_refcounts (i
);
10805 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
10806 if (elf_bad_symtab (i
))
10807 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10809 locsymcount
= symtab_hdr
->sh_info
;
10811 for (j
= 0; j
< locsymcount
; ++j
)
10813 if (local_got
[j
] > 0)
10815 local_got
[j
] = gotoff
;
10816 gotoff
+= got_elt_size
;
10819 local_got
[j
] = (bfd_vma
) -1;
10823 /* Then the global .got entries. .plt refcounts are handled by
10824 adjust_dynamic_symbol */
10825 gofarg
.gotoff
= gotoff
;
10826 gofarg
.got_elt_size
= got_elt_size
;
10827 elf_link_hash_traverse (elf_hash_table (info
),
10828 elf_gc_allocate_got_offsets
,
10833 /* Many folk need no more in the way of final link than this, once
10834 got entry reference counting is enabled. */
10837 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10839 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
10842 /* Invoke the regular ELF backend linker to do all the work. */
10843 return bfd_elf_final_link (abfd
, info
);
10847 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
10849 struct elf_reloc_cookie
*rcookie
= cookie
;
10851 if (rcookie
->bad_symtab
)
10852 rcookie
->rel
= rcookie
->rels
;
10854 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
10856 unsigned long r_symndx
;
10858 if (! rcookie
->bad_symtab
)
10859 if (rcookie
->rel
->r_offset
> offset
)
10861 if (rcookie
->rel
->r_offset
!= offset
)
10864 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
10865 if (r_symndx
== SHN_UNDEF
)
10868 if (r_symndx
>= rcookie
->locsymcount
10869 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
10871 struct elf_link_hash_entry
*h
;
10873 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
10875 while (h
->root
.type
== bfd_link_hash_indirect
10876 || h
->root
.type
== bfd_link_hash_warning
)
10877 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10879 if ((h
->root
.type
== bfd_link_hash_defined
10880 || h
->root
.type
== bfd_link_hash_defweak
)
10881 && elf_discarded_section (h
->root
.u
.def
.section
))
10888 /* It's not a relocation against a global symbol,
10889 but it could be a relocation against a local
10890 symbol for a discarded section. */
10892 Elf_Internal_Sym
*isym
;
10894 /* Need to: get the symbol; get the section. */
10895 isym
= &rcookie
->locsyms
[r_symndx
];
10896 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
10898 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
10899 if (isec
!= NULL
&& elf_discarded_section (isec
))
10908 /* Discard unneeded references to discarded sections.
10909 Returns TRUE if any section's size was changed. */
10910 /* This function assumes that the relocations are in sorted order,
10911 which is true for all known assemblers. */
10914 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
10916 struct elf_reloc_cookie cookie
;
10917 asection
*stab
, *eh
;
10918 Elf_Internal_Shdr
*symtab_hdr
;
10919 const struct elf_backend_data
*bed
;
10921 unsigned int count
;
10922 bfd_boolean ret
= FALSE
;
10924 if (info
->traditional_format
10925 || !is_elf_hash_table (info
->hash
))
10928 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
10930 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10933 bed
= get_elf_backend_data (abfd
);
10935 if ((abfd
->flags
& DYNAMIC
) != 0)
10939 if (!info
->relocatable
)
10941 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
10944 || bfd_is_abs_section (eh
->output_section
)))
10948 stab
= bfd_get_section_by_name (abfd
, ".stab");
10950 && (stab
->size
== 0
10951 || bfd_is_abs_section (stab
->output_section
)
10952 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
10957 && bed
->elf_backend_discard_info
== NULL
)
10960 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10961 cookie
.abfd
= abfd
;
10962 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
10963 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
10964 if (cookie
.bad_symtab
)
10966 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10967 cookie
.extsymoff
= 0;
10971 cookie
.locsymcount
= symtab_hdr
->sh_info
;
10972 cookie
.extsymoff
= symtab_hdr
->sh_info
;
10975 if (bed
->s
->arch_size
== 32)
10976 cookie
.r_sym_shift
= 8;
10978 cookie
.r_sym_shift
= 32;
10980 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10981 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
10983 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
10984 cookie
.locsymcount
, 0,
10986 if (cookie
.locsyms
== NULL
)
10992 cookie
.rels
= NULL
;
10993 count
= stab
->reloc_count
;
10995 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
10996 info
->keep_memory
);
10997 if (cookie
.rels
!= NULL
)
10999 cookie
.rel
= cookie
.rels
;
11000 cookie
.relend
= cookie
.rels
;
11001 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11002 if (_bfd_discard_section_stabs (abfd
, stab
,
11003 elf_section_data (stab
)->sec_info
,
11004 bfd_elf_reloc_symbol_deleted_p
,
11007 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
11008 free (cookie
.rels
);
11014 cookie
.rels
= NULL
;
11015 count
= eh
->reloc_count
;
11017 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
11018 info
->keep_memory
);
11019 cookie
.rel
= cookie
.rels
;
11020 cookie
.relend
= cookie
.rels
;
11021 if (cookie
.rels
!= NULL
)
11022 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
11024 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
11025 bfd_elf_reloc_symbol_deleted_p
,
11029 if (cookie
.rels
!= NULL
11030 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
11031 free (cookie
.rels
);
11034 if (bed
->elf_backend_discard_info
!= NULL
11035 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
11038 if (cookie
.locsyms
!= NULL
11039 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
11041 if (! info
->keep_memory
)
11042 free (cookie
.locsyms
);
11044 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
11048 if (info
->eh_frame_hdr
11049 && !info
->relocatable
11050 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
11057 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
*sec
,
11058 struct bfd_link_info
*info
)
11061 const char *name
, *p
;
11062 struct bfd_section_already_linked
*l
;
11063 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
11065 if (sec
->output_section
== bfd_abs_section_ptr
)
11068 flags
= sec
->flags
;
11070 /* Return if it isn't a linkonce section. A comdat group section
11071 also has SEC_LINK_ONCE set. */
11072 if ((flags
& SEC_LINK_ONCE
) == 0)
11075 /* Don't put group member sections on our list of already linked
11076 sections. They are handled as a group via their group section. */
11077 if (elf_sec_group (sec
) != NULL
)
11080 /* FIXME: When doing a relocatable link, we may have trouble
11081 copying relocations in other sections that refer to local symbols
11082 in the section being discarded. Those relocations will have to
11083 be converted somehow; as of this writing I'm not sure that any of
11084 the backends handle that correctly.
11086 It is tempting to instead not discard link once sections when
11087 doing a relocatable link (technically, they should be discarded
11088 whenever we are building constructors). However, that fails,
11089 because the linker winds up combining all the link once sections
11090 into a single large link once section, which defeats the purpose
11091 of having link once sections in the first place.
11093 Also, not merging link once sections in a relocatable link
11094 causes trouble for MIPS ELF, which relies on link once semantics
11095 to handle the .reginfo section correctly. */
11097 name
= bfd_get_section_name (abfd
, sec
);
11099 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
11100 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
11105 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
11107 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11109 /* We may have 2 different types of sections on the list: group
11110 sections and linkonce sections. Match like sections. */
11111 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
11112 && strcmp (name
, l
->sec
->name
) == 0
11113 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
11115 /* The section has already been linked. See if we should
11116 issue a warning. */
11117 switch (flags
& SEC_LINK_DUPLICATES
)
11122 case SEC_LINK_DUPLICATES_DISCARD
:
11125 case SEC_LINK_DUPLICATES_ONE_ONLY
:
11126 (*_bfd_error_handler
)
11127 (_("%B: ignoring duplicate section `%A'"),
11131 case SEC_LINK_DUPLICATES_SAME_SIZE
:
11132 if (sec
->size
!= l
->sec
->size
)
11133 (*_bfd_error_handler
)
11134 (_("%B: duplicate section `%A' has different size"),
11138 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
11139 if (sec
->size
!= l
->sec
->size
)
11140 (*_bfd_error_handler
)
11141 (_("%B: duplicate section `%A' has different size"),
11143 else if (sec
->size
!= 0)
11145 bfd_byte
*sec_contents
, *l_sec_contents
;
11147 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
11148 (*_bfd_error_handler
)
11149 (_("%B: warning: could not read contents of section `%A'"),
11151 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
11153 (*_bfd_error_handler
)
11154 (_("%B: warning: could not read contents of section `%A'"),
11155 l
->sec
->owner
, l
->sec
);
11156 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
11157 (*_bfd_error_handler
)
11158 (_("%B: warning: duplicate section `%A' has different contents"),
11162 free (sec_contents
);
11163 if (l_sec_contents
)
11164 free (l_sec_contents
);
11169 /* Set the output_section field so that lang_add_section
11170 does not create a lang_input_section structure for this
11171 section. Since there might be a symbol in the section
11172 being discarded, we must retain a pointer to the section
11173 which we are really going to use. */
11174 sec
->output_section
= bfd_abs_section_ptr
;
11175 sec
->kept_section
= l
->sec
;
11177 if (flags
& SEC_GROUP
)
11179 asection
*first
= elf_next_in_group (sec
);
11180 asection
*s
= first
;
11184 s
->output_section
= bfd_abs_section_ptr
;
11185 /* Record which group discards it. */
11186 s
->kept_section
= l
->sec
;
11187 s
= elf_next_in_group (s
);
11188 /* These lists are circular. */
11198 /* A single member comdat group section may be discarded by a
11199 linkonce section and vice versa. */
11201 if ((flags
& SEC_GROUP
) != 0)
11203 asection
*first
= elf_next_in_group (sec
);
11205 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
11206 /* Check this single member group against linkonce sections. */
11207 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11208 if ((l
->sec
->flags
& SEC_GROUP
) == 0
11209 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
11210 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
11212 first
->output_section
= bfd_abs_section_ptr
;
11213 first
->kept_section
= l
->sec
;
11214 sec
->output_section
= bfd_abs_section_ptr
;
11219 /* Check this linkonce section against single member groups. */
11220 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
11221 if (l
->sec
->flags
& SEC_GROUP
)
11223 asection
*first
= elf_next_in_group (l
->sec
);
11226 && elf_next_in_group (first
) == first
11227 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
11229 sec
->output_section
= bfd_abs_section_ptr
;
11230 sec
->kept_section
= first
;
11235 /* This is the first section with this name. Record it. */
11236 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
11240 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
11242 return sym
->st_shndx
== SHN_COMMON
;
11246 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
11252 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
11254 return bfd_com_section_ptr
;