1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 #include "safe-ctype.h"
30 #include "libiberty.h"
33 /* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
36 struct elf_info_failed
38 struct bfd_link_info
*info
;
42 /* This structure is used to pass information to
43 _bfd_elf_link_find_version_dependencies. */
45 struct elf_find_verdep_info
47 /* General link information. */
48 struct bfd_link_info
*info
;
49 /* The number of dependencies. */
51 /* Whether we had a failure. */
55 static bfd_boolean _bfd_elf_fix_symbol_flags
56 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
58 /* Define a symbol in a dynamic linkage section. */
60 struct elf_link_hash_entry
*
61 _bfd_elf_define_linkage_sym (bfd
*abfd
,
62 struct bfd_link_info
*info
,
66 struct elf_link_hash_entry
*h
;
67 struct bfd_link_hash_entry
*bh
;
68 const struct elf_backend_data
*bed
;
70 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
73 /* Zap symbol defined in an as-needed lib that wasn't linked.
74 This is a symptom of a larger problem: Absolute symbols
75 defined in shared libraries can't be overridden, because we
76 lose the link to the bfd which is via the symbol section. */
77 h
->root
.type
= bfd_link_hash_new
;
81 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
83 get_elf_backend_data (abfd
)->collect
,
86 h
= (struct elf_link_hash_entry
*) bh
;
90 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
92 bed
= get_elf_backend_data (abfd
);
93 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
98 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
102 struct elf_link_hash_entry
*h
;
103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
104 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
106 /* This function may be called more than once. */
107 s
= bfd_get_linker_section (abfd
, ".got");
111 flags
= bed
->dynamic_sec_flags
;
113 s
= bfd_make_section_anyway_with_flags (abfd
,
114 (bed
->rela_plts_and_copies_p
115 ? ".rela.got" : ".rel.got"),
116 (bed
->dynamic_sec_flags
119 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
123 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
125 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
129 if (bed
->want_got_plt
)
131 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
133 || !bfd_set_section_alignment (abfd
, s
,
134 bed
->s
->log_file_align
))
139 /* The first bit of the global offset table is the header. */
140 s
->size
+= bed
->got_header_size
;
142 if (bed
->want_got_sym
)
144 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
145 (or .got.plt) section. We don't do this in the linker script
146 because we don't want to define the symbol if we are not creating
147 a global offset table. */
148 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
149 "_GLOBAL_OFFSET_TABLE_");
150 elf_hash_table (info
)->hgot
= h
;
158 /* Create a strtab to hold the dynamic symbol names. */
160 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
162 struct elf_link_hash_table
*hash_table
;
164 hash_table
= elf_hash_table (info
);
165 if (hash_table
->dynobj
== NULL
)
166 hash_table
->dynobj
= abfd
;
168 if (hash_table
->dynstr
== NULL
)
170 hash_table
->dynstr
= _bfd_elf_strtab_init ();
171 if (hash_table
->dynstr
== NULL
)
177 /* Create some sections which will be filled in with dynamic linking
178 information. ABFD is an input file which requires dynamic sections
179 to be created. The dynamic sections take up virtual memory space
180 when the final executable is run, so we need to create them before
181 addresses are assigned to the output sections. We work out the
182 actual contents and size of these sections later. */
185 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
189 const struct elf_backend_data
*bed
;
190 struct elf_link_hash_entry
*h
;
192 if (! is_elf_hash_table (info
->hash
))
195 if (elf_hash_table (info
)->dynamic_sections_created
)
198 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
201 abfd
= elf_hash_table (info
)->dynobj
;
202 bed
= get_elf_backend_data (abfd
);
204 flags
= bed
->dynamic_sec_flags
;
206 /* A dynamically linked executable has a .interp section, but a
207 shared library does not. */
208 if (info
->executable
)
210 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
211 flags
| SEC_READONLY
);
216 /* Create sections to hold version informations. These are removed
217 if they are not needed. */
218 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
219 flags
| SEC_READONLY
);
221 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
224 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
225 flags
| SEC_READONLY
);
227 || ! bfd_set_section_alignment (abfd
, s
, 1))
230 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
231 flags
| SEC_READONLY
);
233 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
236 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
237 flags
| SEC_READONLY
);
239 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
242 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
243 flags
| SEC_READONLY
);
247 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
249 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
252 /* The special symbol _DYNAMIC is always set to the start of the
253 .dynamic section. We could set _DYNAMIC in a linker script, but we
254 only want to define it if we are, in fact, creating a .dynamic
255 section. We don't want to define it if there is no .dynamic
256 section, since on some ELF platforms the start up code examines it
257 to decide how to initialize the process. */
258 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
259 elf_hash_table (info
)->hdynamic
= h
;
265 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
266 flags
| SEC_READONLY
);
268 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
270 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
273 if (info
->emit_gnu_hash
)
275 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
276 flags
| SEC_READONLY
);
278 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
280 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
281 4 32-bit words followed by variable count of 64-bit words, then
282 variable count of 32-bit words. */
283 if (bed
->s
->arch_size
== 64)
284 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
286 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
289 /* Let the backend create the rest of the sections. This lets the
290 backend set the right flags. The backend will normally create
291 the .got and .plt sections. */
292 if (bed
->elf_backend_create_dynamic_sections
== NULL
293 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
296 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
301 /* Create dynamic sections when linking against a dynamic object. */
304 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
306 flagword flags
, pltflags
;
307 struct elf_link_hash_entry
*h
;
309 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
310 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
312 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
313 .rel[a].bss sections. */
314 flags
= bed
->dynamic_sec_flags
;
317 if (bed
->plt_not_loaded
)
318 /* We do not clear SEC_ALLOC here because we still want the OS to
319 allocate space for the section; it's just that there's nothing
320 to read in from the object file. */
321 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
323 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
324 if (bed
->plt_readonly
)
325 pltflags
|= SEC_READONLY
;
327 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
329 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
333 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
335 if (bed
->want_plt_sym
)
337 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
338 "_PROCEDURE_LINKAGE_TABLE_");
339 elf_hash_table (info
)->hplt
= h
;
344 s
= bfd_make_section_anyway_with_flags (abfd
,
345 (bed
->rela_plts_and_copies_p
346 ? ".rela.plt" : ".rel.plt"),
347 flags
| SEC_READONLY
);
349 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
353 if (! _bfd_elf_create_got_section (abfd
, info
))
356 if (bed
->want_dynbss
)
358 /* The .dynbss section is a place to put symbols which are defined
359 by dynamic objects, are referenced by regular objects, and are
360 not functions. We must allocate space for them in the process
361 image and use a R_*_COPY reloc to tell the dynamic linker to
362 initialize them at run time. The linker script puts the .dynbss
363 section into the .bss section of the final image. */
364 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
365 (SEC_ALLOC
| SEC_LINKER_CREATED
));
369 /* The .rel[a].bss section holds copy relocs. This section is not
370 normally needed. We need to create it here, though, so that the
371 linker will map it to an output section. We can't just create it
372 only if we need it, because we will not know whether we need it
373 until we have seen all the input files, and the first time the
374 main linker code calls BFD after examining all the input files
375 (size_dynamic_sections) the input sections have already been
376 mapped to the output sections. If the section turns out not to
377 be needed, we can discard it later. We will never need this
378 section when generating a shared object, since they do not use
382 s
= bfd_make_section_anyway_with_flags (abfd
,
383 (bed
->rela_plts_and_copies_p
384 ? ".rela.bss" : ".rel.bss"),
385 flags
| SEC_READONLY
);
387 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
395 /* Record a new dynamic symbol. We record the dynamic symbols as we
396 read the input files, since we need to have a list of all of them
397 before we can determine the final sizes of the output sections.
398 Note that we may actually call this function even though we are not
399 going to output any dynamic symbols; in some cases we know that a
400 symbol should be in the dynamic symbol table, but only if there is
404 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
405 struct elf_link_hash_entry
*h
)
407 if (h
->dynindx
== -1)
409 struct elf_strtab_hash
*dynstr
;
414 /* XXX: The ABI draft says the linker must turn hidden and
415 internal symbols into STB_LOCAL symbols when producing the
416 DSO. However, if ld.so honors st_other in the dynamic table,
417 this would not be necessary. */
418 switch (ELF_ST_VISIBILITY (h
->other
))
422 if (h
->root
.type
!= bfd_link_hash_undefined
423 && h
->root
.type
!= bfd_link_hash_undefweak
)
426 if (!elf_hash_table (info
)->is_relocatable_executable
)
434 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
435 ++elf_hash_table (info
)->dynsymcount
;
437 dynstr
= elf_hash_table (info
)->dynstr
;
440 /* Create a strtab to hold the dynamic symbol names. */
441 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
446 /* We don't put any version information in the dynamic string
448 name
= h
->root
.root
.string
;
449 p
= strchr (name
, ELF_VER_CHR
);
451 /* We know that the p points into writable memory. In fact,
452 there are only a few symbols that have read-only names, being
453 those like _GLOBAL_OFFSET_TABLE_ that are created specially
454 by the backends. Most symbols will have names pointing into
455 an ELF string table read from a file, or to objalloc memory. */
458 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
463 if (indx
== (bfd_size_type
) -1)
465 h
->dynstr_index
= indx
;
471 /* Mark a symbol dynamic. */
474 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
475 struct elf_link_hash_entry
*h
,
476 Elf_Internal_Sym
*sym
)
478 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
480 /* It may be called more than once on the same H. */
481 if(h
->dynamic
|| info
->relocatable
)
484 if ((info
->dynamic_data
485 && (h
->type
== STT_OBJECT
487 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
489 && h
->root
.type
== bfd_link_hash_new
490 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
494 /* Record an assignment to a symbol made by a linker script. We need
495 this in case some dynamic object refers to this symbol. */
498 bfd_elf_record_link_assignment (bfd
*output_bfd
,
499 struct bfd_link_info
*info
,
504 struct elf_link_hash_entry
*h
, *hv
;
505 struct elf_link_hash_table
*htab
;
506 const struct elf_backend_data
*bed
;
508 if (!is_elf_hash_table (info
->hash
))
511 htab
= elf_hash_table (info
);
512 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
516 switch (h
->root
.type
)
518 case bfd_link_hash_defined
:
519 case bfd_link_hash_defweak
:
520 case bfd_link_hash_common
:
522 case bfd_link_hash_undefweak
:
523 case bfd_link_hash_undefined
:
524 /* Since we're defining the symbol, don't let it seem to have not
525 been defined. record_dynamic_symbol and size_dynamic_sections
526 may depend on this. */
527 h
->root
.type
= bfd_link_hash_new
;
528 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
529 bfd_link_repair_undef_list (&htab
->root
);
531 case bfd_link_hash_new
:
532 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
535 case bfd_link_hash_indirect
:
536 /* We had a versioned symbol in a dynamic library. We make the
537 the versioned symbol point to this one. */
538 bed
= get_elf_backend_data (output_bfd
);
540 while (hv
->root
.type
== bfd_link_hash_indirect
541 || hv
->root
.type
== bfd_link_hash_warning
)
542 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
543 /* We don't need to update h->root.u since linker will set them
545 h
->root
.type
= bfd_link_hash_undefined
;
546 hv
->root
.type
= bfd_link_hash_indirect
;
547 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
548 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
550 case bfd_link_hash_warning
:
555 /* If this symbol is being provided by the linker script, and it is
556 currently defined by a dynamic object, but not by a regular
557 object, then mark it as undefined so that the generic linker will
558 force the correct value. */
562 h
->root
.type
= bfd_link_hash_undefined
;
564 /* If this symbol is not being provided by the linker script, and it is
565 currently defined by a dynamic object, but not by a regular object,
566 then clear out any version information because the symbol will not be
567 associated with the dynamic object any more. */
571 h
->verinfo
.verdef
= NULL
;
577 bed
= get_elf_backend_data (output_bfd
);
578 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
579 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
582 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
584 if (!info
->relocatable
586 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
587 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
593 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
596 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
599 /* If this is a weak defined symbol, and we know a corresponding
600 real symbol from the same dynamic object, make sure the real
601 symbol is also made into a dynamic symbol. */
602 if (h
->u
.weakdef
!= NULL
603 && h
->u
.weakdef
->dynindx
== -1)
605 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
613 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
614 success, and 2 on a failure caused by attempting to record a symbol
615 in a discarded section, eg. a discarded link-once section symbol. */
618 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
623 struct elf_link_local_dynamic_entry
*entry
;
624 struct elf_link_hash_table
*eht
;
625 struct elf_strtab_hash
*dynstr
;
626 unsigned long dynstr_index
;
628 Elf_External_Sym_Shndx eshndx
;
629 char esym
[sizeof (Elf64_External_Sym
)];
631 if (! is_elf_hash_table (info
->hash
))
634 /* See if the entry exists already. */
635 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
636 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
639 amt
= sizeof (*entry
);
640 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
644 /* Go find the symbol, so that we can find it's name. */
645 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
646 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
648 bfd_release (input_bfd
, entry
);
652 if (entry
->isym
.st_shndx
!= SHN_UNDEF
653 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
657 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
658 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
660 /* We can still bfd_release here as nothing has done another
661 bfd_alloc. We can't do this later in this function. */
662 bfd_release (input_bfd
, entry
);
667 name
= (bfd_elf_string_from_elf_section
668 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
669 entry
->isym
.st_name
));
671 dynstr
= elf_hash_table (info
)->dynstr
;
674 /* Create a strtab to hold the dynamic symbol names. */
675 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
680 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
681 if (dynstr_index
== (unsigned long) -1)
683 entry
->isym
.st_name
= dynstr_index
;
685 eht
= elf_hash_table (info
);
687 entry
->next
= eht
->dynlocal
;
688 eht
->dynlocal
= entry
;
689 entry
->input_bfd
= input_bfd
;
690 entry
->input_indx
= input_indx
;
693 /* Whatever binding the symbol had before, it's now local. */
695 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
697 /* The dynindx will be set at the end of size_dynamic_sections. */
702 /* Return the dynindex of a local dynamic symbol. */
705 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
709 struct elf_link_local_dynamic_entry
*e
;
711 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
712 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
717 /* This function is used to renumber the dynamic symbols, if some of
718 them are removed because they are marked as local. This is called
719 via elf_link_hash_traverse. */
722 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
725 size_t *count
= (size_t *) data
;
730 if (h
->dynindx
!= -1)
731 h
->dynindx
= ++(*count
);
737 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
738 STB_LOCAL binding. */
741 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
744 size_t *count
= (size_t *) data
;
746 if (!h
->forced_local
)
749 if (h
->dynindx
!= -1)
750 h
->dynindx
= ++(*count
);
755 /* Return true if the dynamic symbol for a given section should be
756 omitted when creating a shared library. */
758 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
759 struct bfd_link_info
*info
,
762 struct elf_link_hash_table
*htab
;
764 switch (elf_section_data (p
)->this_hdr
.sh_type
)
768 /* If sh_type is yet undecided, assume it could be
769 SHT_PROGBITS/SHT_NOBITS. */
771 htab
= elf_hash_table (info
);
772 if (p
== htab
->tls_sec
)
775 if (htab
->text_index_section
!= NULL
)
776 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
778 if (strcmp (p
->name
, ".got") == 0
779 || strcmp (p
->name
, ".got.plt") == 0
780 || strcmp (p
->name
, ".plt") == 0)
784 if (htab
->dynobj
!= NULL
785 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
786 && ip
->output_section
== p
)
791 /* There shouldn't be section relative relocations
792 against any other section. */
798 /* Assign dynsym indices. In a shared library we generate a section
799 symbol for each output section, which come first. Next come symbols
800 which have been forced to local binding. Then all of the back-end
801 allocated local dynamic syms, followed by the rest of the global
805 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
806 struct bfd_link_info
*info
,
807 unsigned long *section_sym_count
)
809 unsigned long dynsymcount
= 0;
811 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
813 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
815 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
816 if ((p
->flags
& SEC_EXCLUDE
) == 0
817 && (p
->flags
& SEC_ALLOC
) != 0
818 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
819 elf_section_data (p
)->dynindx
= ++dynsymcount
;
821 elf_section_data (p
)->dynindx
= 0;
823 *section_sym_count
= dynsymcount
;
825 elf_link_hash_traverse (elf_hash_table (info
),
826 elf_link_renumber_local_hash_table_dynsyms
,
829 if (elf_hash_table (info
)->dynlocal
)
831 struct elf_link_local_dynamic_entry
*p
;
832 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
833 p
->dynindx
= ++dynsymcount
;
836 elf_link_hash_traverse (elf_hash_table (info
),
837 elf_link_renumber_hash_table_dynsyms
,
840 /* There is an unused NULL entry at the head of the table which
841 we must account for in our count. Unless there weren't any
842 symbols, which means we'll have no table at all. */
843 if (dynsymcount
!= 0)
846 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
850 /* Merge st_other field. */
853 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
854 Elf_Internal_Sym
*isym
, bfd_boolean definition
,
857 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
859 /* If st_other has a processor-specific meaning, specific
860 code might be needed here. We never merge the visibility
861 attribute with the one from a dynamic object. */
862 if (bed
->elf_backend_merge_symbol_attribute
)
863 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
866 /* If this symbol has default visibility and the user has requested
867 we not re-export it, then mark it as hidden. */
871 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
872 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
873 isym
->st_other
= (STV_HIDDEN
874 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
876 if (!dynamic
&& ELF_ST_VISIBILITY (isym
->st_other
) != 0)
878 unsigned char hvis
, symvis
, other
, nvis
;
880 /* Only merge the visibility. Leave the remainder of the
881 st_other field to elf_backend_merge_symbol_attribute. */
882 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
884 /* Combine visibilities, using the most constraining one. */
885 hvis
= ELF_ST_VISIBILITY (h
->other
);
886 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
892 nvis
= hvis
< symvis
? hvis
: symvis
;
894 h
->other
= other
| nvis
;
898 /* Mark if a symbol has a definition in a dynamic object or is
899 weak in all dynamic objects. */
902 _bfd_elf_mark_dynamic_def_weak (struct elf_link_hash_entry
*h
,
903 asection
*sec
, int bind
)
907 if (!bfd_is_und_section (sec
))
911 /* Check if this symbol is weak in all dynamic objects. If it
912 is the first time we see it in a dynamic object, we mark
913 if it is weak. Otherwise, we clear it. */
916 if (bind
== STB_WEAK
)
919 else if (bind
!= STB_WEAK
)
925 /* This function is called when we want to define a new symbol. It
926 handles the various cases which arise when we find a definition in
927 a dynamic object, or when there is already a definition in a
928 dynamic object. The new symbol is described by NAME, SYM, PSEC,
929 and PVALUE. We set SYM_HASH to the hash table entry. We set
930 OVERRIDE if the old symbol is overriding a new definition. We set
931 TYPE_CHANGE_OK if it is OK for the type to change. We set
932 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
933 change, we mean that we shouldn't warn if the type or size does
934 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
935 object is overridden by a regular object. */
938 _bfd_elf_merge_symbol (bfd
*abfd
,
939 struct bfd_link_info
*info
,
941 Elf_Internal_Sym
*sym
,
944 unsigned int *pold_alignment
,
945 struct elf_link_hash_entry
**sym_hash
,
947 bfd_boolean
*override
,
948 bfd_boolean
*type_change_ok
,
949 bfd_boolean
*size_change_ok
)
951 asection
*sec
, *oldsec
;
952 struct elf_link_hash_entry
*h
;
953 struct elf_link_hash_entry
*hi
;
954 struct elf_link_hash_entry
*flip
;
957 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
958 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
959 const struct elf_backend_data
*bed
;
965 bind
= ELF_ST_BIND (sym
->st_info
);
967 /* Silently discard TLS symbols from --just-syms. There's no way to
968 combine a static TLS block with a new TLS block for this executable. */
969 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
970 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
976 if (! bfd_is_und_section (sec
))
977 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
979 h
= ((struct elf_link_hash_entry
*)
980 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
985 bed
= get_elf_backend_data (abfd
);
987 /* This code is for coping with dynamic objects, and is only useful
988 if we are doing an ELF link. */
989 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
992 /* For merging, we only care about real symbols. But we need to make
993 sure that indirect symbol dynamic flags are updated. */
995 while (h
->root
.type
== bfd_link_hash_indirect
996 || h
->root
.type
== bfd_link_hash_warning
)
997 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
999 /* We have to check it for every instance since the first few may be
1000 refereences and not all compilers emit symbol type for undefined
1002 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1004 /* If we just created the symbol, mark it as being an ELF symbol.
1005 Other than that, there is nothing to do--there is no merge issue
1006 with a newly defined symbol--so we just return. */
1008 if (h
->root
.type
== bfd_link_hash_new
)
1014 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1017 switch (h
->root
.type
)
1024 case bfd_link_hash_undefined
:
1025 case bfd_link_hash_undefweak
:
1026 oldbfd
= h
->root
.u
.undef
.abfd
;
1030 case bfd_link_hash_defined
:
1031 case bfd_link_hash_defweak
:
1032 oldbfd
= h
->root
.u
.def
.section
->owner
;
1033 oldsec
= h
->root
.u
.def
.section
;
1036 case bfd_link_hash_common
:
1037 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1038 oldsec
= h
->root
.u
.c
.p
->section
;
1042 /* Differentiate strong and weak symbols. */
1043 newweak
= bind
== STB_WEAK
;
1044 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1045 || h
->root
.type
== bfd_link_hash_undefweak
);
1047 /* In cases involving weak versioned symbols, we may wind up trying
1048 to merge a symbol with itself. Catch that here, to avoid the
1049 confusion that results if we try to override a symbol with
1050 itself. The additional tests catch cases like
1051 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1052 dynamic object, which we do want to handle here. */
1054 && (newweak
|| oldweak
)
1055 && ((abfd
->flags
& DYNAMIC
) == 0
1056 || !h
->def_regular
))
1059 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1060 respectively, is from a dynamic object. */
1062 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1066 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1067 else if (oldsec
!= NULL
)
1069 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1070 indices used by MIPS ELF. */
1071 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1074 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1075 respectively, appear to be a definition rather than reference. */
1077 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1079 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1080 && h
->root
.type
!= bfd_link_hash_undefweak
1081 && h
->root
.type
!= bfd_link_hash_common
);
1083 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1084 respectively, appear to be a function. */
1086 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1087 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1089 oldfunc
= (h
->type
!= STT_NOTYPE
1090 && bed
->is_function_type (h
->type
));
1092 /* When we try to create a default indirect symbol from the dynamic
1093 definition with the default version, we skip it if its type and
1094 the type of existing regular definition mismatch. We only do it
1095 if the existing regular definition won't be dynamic. */
1096 if (pold_alignment
== NULL
1098 && !info
->export_dynamic
1103 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1104 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1105 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1106 && h
->type
!= STT_NOTYPE
1107 && !(newfunc
&& oldfunc
))
1113 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1114 if (oldbfd
!= NULL
&& (oldbfd
->flags
& BFD_PLUGIN
) != 0)
1115 *type_change_ok
= TRUE
;
1117 /* Check TLS symbol. We don't check undefined symbol introduced by
1119 else if (oldbfd
!= NULL
1120 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1121 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1124 bfd_boolean ntdef
, tdef
;
1125 asection
*ntsec
, *tsec
;
1127 if (h
->type
== STT_TLS
)
1147 (*_bfd_error_handler
)
1148 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1149 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1150 else if (!tdef
&& !ntdef
)
1151 (*_bfd_error_handler
)
1152 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1153 tbfd
, ntbfd
, h
->root
.root
.string
);
1155 (*_bfd_error_handler
)
1156 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1157 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1159 (*_bfd_error_handler
)
1160 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1161 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1163 bfd_set_error (bfd_error_bad_value
);
1167 /* We need to remember if a symbol has a definition in a dynamic
1168 object or is weak in all dynamic objects. Internal and hidden
1169 visibility will make it unavailable to dynamic objects. */
1172 _bfd_elf_mark_dynamic_def_weak (h
, sec
, bind
);
1174 _bfd_elf_mark_dynamic_def_weak (hi
, sec
, bind
);
1177 /* If the old symbol has non-default visibility, we ignore the new
1178 definition from a dynamic object. */
1180 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1181 && !bfd_is_und_section (sec
))
1184 /* Make sure this symbol is dynamic. */
1186 hi
->ref_dynamic
= 1;
1187 /* A protected symbol has external availability. Make sure it is
1188 recorded as dynamic.
1190 FIXME: Should we check type and size for protected symbol? */
1191 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1192 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1197 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1200 /* If the new symbol with non-default visibility comes from a
1201 relocatable file and the old definition comes from a dynamic
1202 object, we remove the old definition. */
1203 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1205 /* Handle the case where the old dynamic definition is
1206 default versioned. We need to copy the symbol info from
1207 the symbol with default version to the normal one if it
1208 was referenced before. */
1211 struct elf_link_hash_entry
*vh
= *sym_hash
;
1213 vh
->root
.type
= h
->root
.type
;
1214 h
->root
.type
= bfd_link_hash_indirect
;
1215 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1217 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1218 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1220 /* If the new symbol is hidden or internal, completely undo
1221 any dynamic link state. */
1222 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1223 h
->forced_local
= 0;
1231 /* FIXME: Should we check type and size for protected symbol? */
1241 /* If the old symbol was undefined before, then it will still be
1242 on the undefs list. If the new symbol is undefined or
1243 common, we can't make it bfd_link_hash_new here, because new
1244 undefined or common symbols will be added to the undefs list
1245 by _bfd_generic_link_add_one_symbol. Symbols may not be
1246 added twice to the undefs list. Also, if the new symbol is
1247 undefweak then we don't want to lose the strong undef. */
1248 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1250 h
->root
.type
= bfd_link_hash_undefined
;
1251 h
->root
.u
.undef
.abfd
= abfd
;
1255 h
->root
.type
= bfd_link_hash_new
;
1256 h
->root
.u
.undef
.abfd
= NULL
;
1259 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1261 /* If the new symbol is hidden or internal, completely undo
1262 any dynamic link state. */
1263 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1264 h
->forced_local
= 0;
1271 /* FIXME: Should we check type and size for protected symbol? */
1277 if (bind
== STB_GNU_UNIQUE
)
1278 h
->unique_global
= 1;
1280 /* If a new weak symbol definition comes from a regular file and the
1281 old symbol comes from a dynamic library, we treat the new one as
1282 strong. Similarly, an old weak symbol definition from a regular
1283 file is treated as strong when the new symbol comes from a dynamic
1284 library. Further, an old weak symbol from a dynamic library is
1285 treated as strong if the new symbol is from a dynamic library.
1286 This reflects the way glibc's ld.so works.
1288 Do this before setting *type_change_ok or *size_change_ok so that
1289 we warn properly when dynamic library symbols are overridden. */
1291 if (newdef
&& !newdyn
&& olddyn
)
1293 if (olddef
&& newdyn
)
1296 /* Allow changes between different types of function symbol. */
1297 if (newfunc
&& oldfunc
)
1298 *type_change_ok
= TRUE
;
1300 /* It's OK to change the type if either the existing symbol or the
1301 new symbol is weak. A type change is also OK if the old symbol
1302 is undefined and the new symbol is defined. */
1307 && h
->root
.type
== bfd_link_hash_undefined
))
1308 *type_change_ok
= TRUE
;
1310 /* It's OK to change the size if either the existing symbol or the
1311 new symbol is weak, or if the old symbol is undefined. */
1314 || h
->root
.type
== bfd_link_hash_undefined
)
1315 *size_change_ok
= TRUE
;
1317 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1318 symbol, respectively, appears to be a common symbol in a dynamic
1319 object. If a symbol appears in an uninitialized section, and is
1320 not weak, and is not a function, then it may be a common symbol
1321 which was resolved when the dynamic object was created. We want
1322 to treat such symbols specially, because they raise special
1323 considerations when setting the symbol size: if the symbol
1324 appears as a common symbol in a regular object, and the size in
1325 the regular object is larger, we must make sure that we use the
1326 larger size. This problematic case can always be avoided in C,
1327 but it must be handled correctly when using Fortran shared
1330 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1331 likewise for OLDDYNCOMMON and OLDDEF.
1333 Note that this test is just a heuristic, and that it is quite
1334 possible to have an uninitialized symbol in a shared object which
1335 is really a definition, rather than a common symbol. This could
1336 lead to some minor confusion when the symbol really is a common
1337 symbol in some regular object. However, I think it will be
1343 && (sec
->flags
& SEC_ALLOC
) != 0
1344 && (sec
->flags
& SEC_LOAD
) == 0
1347 newdyncommon
= TRUE
;
1349 newdyncommon
= FALSE
;
1353 && h
->root
.type
== bfd_link_hash_defined
1355 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1356 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1359 olddyncommon
= TRUE
;
1361 olddyncommon
= FALSE
;
1363 /* We now know everything about the old and new symbols. We ask the
1364 backend to check if we can merge them. */
1365 if (bed
->merge_symbol
1366 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1367 pold_alignment
, skip
, override
,
1368 type_change_ok
, size_change_ok
,
1369 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1371 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1375 /* If both the old and the new symbols look like common symbols in a
1376 dynamic object, set the size of the symbol to the larger of the
1381 && sym
->st_size
!= h
->size
)
1383 /* Since we think we have two common symbols, issue a multiple
1384 common warning if desired. Note that we only warn if the
1385 size is different. If the size is the same, we simply let
1386 the old symbol override the new one as normally happens with
1387 symbols defined in dynamic objects. */
1389 if (! ((*info
->callbacks
->multiple_common
)
1390 (info
, &h
->root
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1393 if (sym
->st_size
> h
->size
)
1394 h
->size
= sym
->st_size
;
1396 *size_change_ok
= TRUE
;
1399 /* If we are looking at a dynamic object, and we have found a
1400 definition, we need to see if the symbol was already defined by
1401 some other object. If so, we want to use the existing
1402 definition, and we do not want to report a multiple symbol
1403 definition error; we do this by clobbering *PSEC to be
1404 bfd_und_section_ptr.
1406 We treat a common symbol as a definition if the symbol in the
1407 shared library is a function, since common symbols always
1408 represent variables; this can cause confusion in principle, but
1409 any such confusion would seem to indicate an erroneous program or
1410 shared library. We also permit a common symbol in a regular
1411 object to override a weak symbol in a shared object. */
1416 || (h
->root
.type
== bfd_link_hash_common
1417 && (newweak
|| newfunc
))))
1421 newdyncommon
= FALSE
;
1423 *psec
= sec
= bfd_und_section_ptr
;
1424 *size_change_ok
= TRUE
;
1426 /* If we get here when the old symbol is a common symbol, then
1427 we are explicitly letting it override a weak symbol or
1428 function in a dynamic object, and we don't want to warn about
1429 a type change. If the old symbol is a defined symbol, a type
1430 change warning may still be appropriate. */
1432 if (h
->root
.type
== bfd_link_hash_common
)
1433 *type_change_ok
= TRUE
;
1436 /* Handle the special case of an old common symbol merging with a
1437 new symbol which looks like a common symbol in a shared object.
1438 We change *PSEC and *PVALUE to make the new symbol look like a
1439 common symbol, and let _bfd_generic_link_add_one_symbol do the
1443 && h
->root
.type
== bfd_link_hash_common
)
1447 newdyncommon
= FALSE
;
1448 *pvalue
= sym
->st_size
;
1449 *psec
= sec
= bed
->common_section (oldsec
);
1450 *size_change_ok
= TRUE
;
1453 /* Skip weak definitions of symbols that are already defined. */
1454 if (newdef
&& olddef
&& newweak
)
1456 /* Don't skip new non-IR weak syms. */
1457 if (!(oldbfd
!= NULL
1458 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1459 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1462 /* Merge st_other. If the symbol already has a dynamic index,
1463 but visibility says it should not be visible, turn it into a
1465 elf_merge_st_other (abfd
, h
, sym
, newdef
, newdyn
);
1466 if (h
->dynindx
!= -1)
1467 switch (ELF_ST_VISIBILITY (h
->other
))
1471 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1476 /* If the old symbol is from a dynamic object, and the new symbol is
1477 a definition which is not from a dynamic object, then the new
1478 symbol overrides the old symbol. Symbols from regular files
1479 always take precedence over symbols from dynamic objects, even if
1480 they are defined after the dynamic object in the link.
1482 As above, we again permit a common symbol in a regular object to
1483 override a definition in a shared object if the shared object
1484 symbol is a function or is weak. */
1489 || (bfd_is_com_section (sec
)
1490 && (oldweak
|| oldfunc
)))
1495 /* Change the hash table entry to undefined, and let
1496 _bfd_generic_link_add_one_symbol do the right thing with the
1499 h
->root
.type
= bfd_link_hash_undefined
;
1500 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1501 *size_change_ok
= TRUE
;
1504 olddyncommon
= FALSE
;
1506 /* We again permit a type change when a common symbol may be
1507 overriding a function. */
1509 if (bfd_is_com_section (sec
))
1513 /* If a common symbol overrides a function, make sure
1514 that it isn't defined dynamically nor has type
1517 h
->type
= STT_NOTYPE
;
1519 *type_change_ok
= TRUE
;
1522 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1525 /* This union may have been set to be non-NULL when this symbol
1526 was seen in a dynamic object. We must force the union to be
1527 NULL, so that it is correct for a regular symbol. */
1528 h
->verinfo
.vertree
= NULL
;
1531 /* Handle the special case of a new common symbol merging with an
1532 old symbol that looks like it might be a common symbol defined in
1533 a shared object. Note that we have already handled the case in
1534 which a new common symbol should simply override the definition
1535 in the shared library. */
1538 && bfd_is_com_section (sec
)
1541 /* It would be best if we could set the hash table entry to a
1542 common symbol, but we don't know what to use for the section
1543 or the alignment. */
1544 if (! ((*info
->callbacks
->multiple_common
)
1545 (info
, &h
->root
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1548 /* If the presumed common symbol in the dynamic object is
1549 larger, pretend that the new symbol has its size. */
1551 if (h
->size
> *pvalue
)
1554 /* We need to remember the alignment required by the symbol
1555 in the dynamic object. */
1556 BFD_ASSERT (pold_alignment
);
1557 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1560 olddyncommon
= FALSE
;
1562 h
->root
.type
= bfd_link_hash_undefined
;
1563 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1565 *size_change_ok
= TRUE
;
1566 *type_change_ok
= TRUE
;
1568 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1571 h
->verinfo
.vertree
= NULL
;
1576 /* Handle the case where we had a versioned symbol in a dynamic
1577 library and now find a definition in a normal object. In this
1578 case, we make the versioned symbol point to the normal one. */
1579 flip
->root
.type
= h
->root
.type
;
1580 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1581 h
->root
.type
= bfd_link_hash_indirect
;
1582 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1583 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1587 flip
->ref_dynamic
= 1;
1594 /* This function is called to create an indirect symbol from the
1595 default for the symbol with the default version if needed. The
1596 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1597 set DYNSYM if the new indirect symbol is dynamic. */
1600 _bfd_elf_add_default_symbol (bfd
*abfd
,
1601 struct bfd_link_info
*info
,
1602 struct elf_link_hash_entry
*h
,
1604 Elf_Internal_Sym
*sym
,
1607 bfd_boolean
*dynsym
,
1608 bfd_boolean override
)
1610 bfd_boolean type_change_ok
;
1611 bfd_boolean size_change_ok
;
1614 struct elf_link_hash_entry
*hi
;
1615 struct bfd_link_hash_entry
*bh
;
1616 const struct elf_backend_data
*bed
;
1617 bfd_boolean collect
;
1618 bfd_boolean dynamic
;
1620 size_t len
, shortlen
;
1623 /* If this symbol has a version, and it is the default version, we
1624 create an indirect symbol from the default name to the fully
1625 decorated name. This will cause external references which do not
1626 specify a version to be bound to this version of the symbol. */
1627 p
= strchr (name
, ELF_VER_CHR
);
1628 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1633 /* We are overridden by an old definition. We need to check if we
1634 need to create the indirect symbol from the default name. */
1635 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1637 BFD_ASSERT (hi
!= NULL
);
1640 while (hi
->root
.type
== bfd_link_hash_indirect
1641 || hi
->root
.type
== bfd_link_hash_warning
)
1643 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1649 bed
= get_elf_backend_data (abfd
);
1650 collect
= bed
->collect
;
1651 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1653 shortlen
= p
- name
;
1654 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1655 if (shortname
== NULL
)
1657 memcpy (shortname
, name
, shortlen
);
1658 shortname
[shortlen
] = '\0';
1660 /* We are going to create a new symbol. Merge it with any existing
1661 symbol with this name. For the purposes of the merge, act as
1662 though we were defining the symbol we just defined, although we
1663 actually going to define an indirect symbol. */
1664 type_change_ok
= FALSE
;
1665 size_change_ok
= FALSE
;
1667 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1668 NULL
, &hi
, &skip
, &override
,
1669 &type_change_ok
, &size_change_ok
))
1678 if (! (_bfd_generic_link_add_one_symbol
1679 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1680 0, name
, FALSE
, collect
, &bh
)))
1682 hi
= (struct elf_link_hash_entry
*) bh
;
1686 /* In this case the symbol named SHORTNAME is overriding the
1687 indirect symbol we want to add. We were planning on making
1688 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1689 is the name without a version. NAME is the fully versioned
1690 name, and it is the default version.
1692 Overriding means that we already saw a definition for the
1693 symbol SHORTNAME in a regular object, and it is overriding
1694 the symbol defined in the dynamic object.
1696 When this happens, we actually want to change NAME, the
1697 symbol we just added, to refer to SHORTNAME. This will cause
1698 references to NAME in the shared object to become references
1699 to SHORTNAME in the regular object. This is what we expect
1700 when we override a function in a shared object: that the
1701 references in the shared object will be mapped to the
1702 definition in the regular object. */
1704 while (hi
->root
.type
== bfd_link_hash_indirect
1705 || hi
->root
.type
== bfd_link_hash_warning
)
1706 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1708 h
->root
.type
= bfd_link_hash_indirect
;
1709 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1713 hi
->ref_dynamic
= 1;
1717 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1722 /* Now set HI to H, so that the following code will set the
1723 other fields correctly. */
1727 /* Check if HI is a warning symbol. */
1728 if (hi
->root
.type
== bfd_link_hash_warning
)
1729 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1731 /* If there is a duplicate definition somewhere, then HI may not
1732 point to an indirect symbol. We will have reported an error to
1733 the user in that case. */
1735 if (hi
->root
.type
== bfd_link_hash_indirect
)
1737 struct elf_link_hash_entry
*ht
;
1739 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1740 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1742 /* See if the new flags lead us to realize that the symbol must
1748 if (! info
->executable
1755 if (hi
->ref_regular
)
1761 /* We also need to define an indirection from the nondefault version
1765 len
= strlen (name
);
1766 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1767 if (shortname
== NULL
)
1769 memcpy (shortname
, name
, shortlen
);
1770 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1772 /* Once again, merge with any existing symbol. */
1773 type_change_ok
= FALSE
;
1774 size_change_ok
= FALSE
;
1776 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1777 NULL
, &hi
, &skip
, &override
,
1778 &type_change_ok
, &size_change_ok
))
1786 /* Here SHORTNAME is a versioned name, so we don't expect to see
1787 the type of override we do in the case above unless it is
1788 overridden by a versioned definition. */
1789 if (hi
->root
.type
!= bfd_link_hash_defined
1790 && hi
->root
.type
!= bfd_link_hash_defweak
)
1791 (*_bfd_error_handler
)
1792 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1798 if (! (_bfd_generic_link_add_one_symbol
1799 (info
, abfd
, shortname
, BSF_INDIRECT
,
1800 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1802 hi
= (struct elf_link_hash_entry
*) bh
;
1804 /* If there is a duplicate definition somewhere, then HI may not
1805 point to an indirect symbol. We will have reported an error
1806 to the user in that case. */
1808 if (hi
->root
.type
== bfd_link_hash_indirect
)
1810 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1812 /* See if the new flags lead us to realize that the symbol
1818 if (! info
->executable
1824 if (hi
->ref_regular
)
1834 /* This routine is used to export all defined symbols into the dynamic
1835 symbol table. It is called via elf_link_hash_traverse. */
1838 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1840 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1842 /* Ignore indirect symbols. These are added by the versioning code. */
1843 if (h
->root
.type
== bfd_link_hash_indirect
)
1846 /* Ignore this if we won't export it. */
1847 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1850 if (h
->dynindx
== -1
1851 && (h
->def_regular
|| h
->ref_regular
)
1852 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
1853 h
->root
.root
.string
))
1855 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1865 /* Look through the symbols which are defined in other shared
1866 libraries and referenced here. Update the list of version
1867 dependencies. This will be put into the .gnu.version_r section.
1868 This function is called via elf_link_hash_traverse. */
1871 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1874 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
1875 Elf_Internal_Verneed
*t
;
1876 Elf_Internal_Vernaux
*a
;
1879 /* We only care about symbols defined in shared objects with version
1884 || h
->verinfo
.verdef
== NULL
)
1887 /* See if we already know about this version. */
1888 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
1892 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1895 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1896 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1902 /* This is a new version. Add it to tree we are building. */
1907 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
1910 rinfo
->failed
= TRUE
;
1914 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1915 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
1916 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
1920 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
1923 rinfo
->failed
= TRUE
;
1927 /* Note that we are copying a string pointer here, and testing it
1928 above. If bfd_elf_string_from_elf_section is ever changed to
1929 discard the string data when low in memory, this will have to be
1931 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1933 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1934 a
->vna_nextptr
= t
->vn_auxptr
;
1936 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1939 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1946 /* Figure out appropriate versions for all the symbols. We may not
1947 have the version number script until we have read all of the input
1948 files, so until that point we don't know which symbols should be
1949 local. This function is called via elf_link_hash_traverse. */
1952 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1954 struct elf_info_failed
*sinfo
;
1955 struct bfd_link_info
*info
;
1956 const struct elf_backend_data
*bed
;
1957 struct elf_info_failed eif
;
1961 sinfo
= (struct elf_info_failed
*) data
;
1964 /* Fix the symbol flags. */
1967 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1970 sinfo
->failed
= TRUE
;
1974 /* We only need version numbers for symbols defined in regular
1976 if (!h
->def_regular
)
1979 bed
= get_elf_backend_data (info
->output_bfd
);
1980 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1981 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1983 struct bfd_elf_version_tree
*t
;
1988 /* There are two consecutive ELF_VER_CHR characters if this is
1989 not a hidden symbol. */
1991 if (*p
== ELF_VER_CHR
)
1997 /* If there is no version string, we can just return out. */
2005 /* Look for the version. If we find it, it is no longer weak. */
2006 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2008 if (strcmp (t
->name
, p
) == 0)
2012 struct bfd_elf_version_expr
*d
;
2014 len
= p
- h
->root
.root
.string
;
2015 alc
= (char *) bfd_malloc (len
);
2018 sinfo
->failed
= TRUE
;
2021 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2022 alc
[len
- 1] = '\0';
2023 if (alc
[len
- 2] == ELF_VER_CHR
)
2024 alc
[len
- 2] = '\0';
2026 h
->verinfo
.vertree
= t
;
2030 if (t
->globals
.list
!= NULL
)
2031 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2033 /* See if there is anything to force this symbol to
2035 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2037 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2040 && ! info
->export_dynamic
)
2041 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2049 /* If we are building an application, we need to create a
2050 version node for this version. */
2051 if (t
== NULL
&& info
->executable
)
2053 struct bfd_elf_version_tree
**pp
;
2056 /* If we aren't going to export this symbol, we don't need
2057 to worry about it. */
2058 if (h
->dynindx
== -1)
2062 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
, amt
);
2065 sinfo
->failed
= TRUE
;
2070 t
->name_indx
= (unsigned int) -1;
2074 /* Don't count anonymous version tag. */
2075 if (sinfo
->info
->version_info
!= NULL
2076 && sinfo
->info
->version_info
->vernum
== 0)
2078 for (pp
= &sinfo
->info
->version_info
;
2082 t
->vernum
= version_index
;
2086 h
->verinfo
.vertree
= t
;
2090 /* We could not find the version for a symbol when
2091 generating a shared archive. Return an error. */
2092 (*_bfd_error_handler
)
2093 (_("%B: version node not found for symbol %s"),
2094 info
->output_bfd
, h
->root
.root
.string
);
2095 bfd_set_error (bfd_error_bad_value
);
2096 sinfo
->failed
= TRUE
;
2104 /* If we don't have a version for this symbol, see if we can find
2106 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2111 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2112 h
->root
.root
.string
, &hide
);
2113 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2114 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2120 /* Read and swap the relocs from the section indicated by SHDR. This
2121 may be either a REL or a RELA section. The relocations are
2122 translated into RELA relocations and stored in INTERNAL_RELOCS,
2123 which should have already been allocated to contain enough space.
2124 The EXTERNAL_RELOCS are a buffer where the external form of the
2125 relocations should be stored.
2127 Returns FALSE if something goes wrong. */
2130 elf_link_read_relocs_from_section (bfd
*abfd
,
2132 Elf_Internal_Shdr
*shdr
,
2133 void *external_relocs
,
2134 Elf_Internal_Rela
*internal_relocs
)
2136 const struct elf_backend_data
*bed
;
2137 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2138 const bfd_byte
*erela
;
2139 const bfd_byte
*erelaend
;
2140 Elf_Internal_Rela
*irela
;
2141 Elf_Internal_Shdr
*symtab_hdr
;
2144 /* Position ourselves at the start of the section. */
2145 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2148 /* Read the relocations. */
2149 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2152 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2153 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2155 bed
= get_elf_backend_data (abfd
);
2157 /* Convert the external relocations to the internal format. */
2158 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2159 swap_in
= bed
->s
->swap_reloc_in
;
2160 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2161 swap_in
= bed
->s
->swap_reloca_in
;
2164 bfd_set_error (bfd_error_wrong_format
);
2168 erela
= (const bfd_byte
*) external_relocs
;
2169 erelaend
= erela
+ shdr
->sh_size
;
2170 irela
= internal_relocs
;
2171 while (erela
< erelaend
)
2175 (*swap_in
) (abfd
, erela
, irela
);
2176 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2177 if (bed
->s
->arch_size
== 64)
2181 if ((size_t) r_symndx
>= nsyms
)
2183 (*_bfd_error_handler
)
2184 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2185 " for offset 0x%lx in section `%A'"),
2187 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2188 bfd_set_error (bfd_error_bad_value
);
2192 else if (r_symndx
!= STN_UNDEF
)
2194 (*_bfd_error_handler
)
2195 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2196 " when the object file has no symbol table"),
2198 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2199 bfd_set_error (bfd_error_bad_value
);
2202 irela
+= bed
->s
->int_rels_per_ext_rel
;
2203 erela
+= shdr
->sh_entsize
;
2209 /* Read and swap the relocs for a section O. They may have been
2210 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2211 not NULL, they are used as buffers to read into. They are known to
2212 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2213 the return value is allocated using either malloc or bfd_alloc,
2214 according to the KEEP_MEMORY argument. If O has two relocation
2215 sections (both REL and RELA relocations), then the REL_HDR
2216 relocations will appear first in INTERNAL_RELOCS, followed by the
2217 RELA_HDR relocations. */
2220 _bfd_elf_link_read_relocs (bfd
*abfd
,
2222 void *external_relocs
,
2223 Elf_Internal_Rela
*internal_relocs
,
2224 bfd_boolean keep_memory
)
2226 void *alloc1
= NULL
;
2227 Elf_Internal_Rela
*alloc2
= NULL
;
2228 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2229 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2230 Elf_Internal_Rela
*internal_rela_relocs
;
2232 if (esdo
->relocs
!= NULL
)
2233 return esdo
->relocs
;
2235 if (o
->reloc_count
== 0)
2238 if (internal_relocs
== NULL
)
2242 size
= o
->reloc_count
;
2243 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2245 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2247 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2248 if (internal_relocs
== NULL
)
2252 if (external_relocs
== NULL
)
2254 bfd_size_type size
= 0;
2257 size
+= esdo
->rel
.hdr
->sh_size
;
2259 size
+= esdo
->rela
.hdr
->sh_size
;
2261 alloc1
= bfd_malloc (size
);
2264 external_relocs
= alloc1
;
2267 internal_rela_relocs
= internal_relocs
;
2270 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2274 external_relocs
= (((bfd_byte
*) external_relocs
)
2275 + esdo
->rel
.hdr
->sh_size
);
2276 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2277 * bed
->s
->int_rels_per_ext_rel
);
2281 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2283 internal_rela_relocs
)))
2286 /* Cache the results for next time, if we can. */
2288 esdo
->relocs
= internal_relocs
;
2293 /* Don't free alloc2, since if it was allocated we are passing it
2294 back (under the name of internal_relocs). */
2296 return internal_relocs
;
2304 bfd_release (abfd
, alloc2
);
2311 /* Compute the size of, and allocate space for, REL_HDR which is the
2312 section header for a section containing relocations for O. */
2315 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2316 struct bfd_elf_section_reloc_data
*reldata
)
2318 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2320 /* That allows us to calculate the size of the section. */
2321 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2323 /* The contents field must last into write_object_contents, so we
2324 allocate it with bfd_alloc rather than malloc. Also since we
2325 cannot be sure that the contents will actually be filled in,
2326 we zero the allocated space. */
2327 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2328 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2331 if (reldata
->hashes
== NULL
&& reldata
->count
)
2333 struct elf_link_hash_entry
**p
;
2335 p
= (struct elf_link_hash_entry
**)
2336 bfd_zmalloc (reldata
->count
* sizeof (struct elf_link_hash_entry
*));
2340 reldata
->hashes
= p
;
2346 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2347 originated from the section given by INPUT_REL_HDR) to the
2351 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2352 asection
*input_section
,
2353 Elf_Internal_Shdr
*input_rel_hdr
,
2354 Elf_Internal_Rela
*internal_relocs
,
2355 struct elf_link_hash_entry
**rel_hash
2358 Elf_Internal_Rela
*irela
;
2359 Elf_Internal_Rela
*irelaend
;
2361 struct bfd_elf_section_reloc_data
*output_reldata
;
2362 asection
*output_section
;
2363 const struct elf_backend_data
*bed
;
2364 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2365 struct bfd_elf_section_data
*esdo
;
2367 output_section
= input_section
->output_section
;
2369 bed
= get_elf_backend_data (output_bfd
);
2370 esdo
= elf_section_data (output_section
);
2371 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2373 output_reldata
= &esdo
->rel
;
2374 swap_out
= bed
->s
->swap_reloc_out
;
2376 else if (esdo
->rela
.hdr
2377 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2379 output_reldata
= &esdo
->rela
;
2380 swap_out
= bed
->s
->swap_reloca_out
;
2384 (*_bfd_error_handler
)
2385 (_("%B: relocation size mismatch in %B section %A"),
2386 output_bfd
, input_section
->owner
, input_section
);
2387 bfd_set_error (bfd_error_wrong_format
);
2391 erel
= output_reldata
->hdr
->contents
;
2392 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2393 irela
= internal_relocs
;
2394 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2395 * bed
->s
->int_rels_per_ext_rel
);
2396 while (irela
< irelaend
)
2398 (*swap_out
) (output_bfd
, irela
, erel
);
2399 irela
+= bed
->s
->int_rels_per_ext_rel
;
2400 erel
+= input_rel_hdr
->sh_entsize
;
2403 /* Bump the counter, so that we know where to add the next set of
2405 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2410 /* Make weak undefined symbols in PIE dynamic. */
2413 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2414 struct elf_link_hash_entry
*h
)
2418 && h
->root
.type
== bfd_link_hash_undefweak
)
2419 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2424 /* Fix up the flags for a symbol. This handles various cases which
2425 can only be fixed after all the input files are seen. This is
2426 currently called by both adjust_dynamic_symbol and
2427 assign_sym_version, which is unnecessary but perhaps more robust in
2428 the face of future changes. */
2431 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2432 struct elf_info_failed
*eif
)
2434 const struct elf_backend_data
*bed
;
2436 /* If this symbol was mentioned in a non-ELF file, try to set
2437 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2438 permit a non-ELF file to correctly refer to a symbol defined in
2439 an ELF dynamic object. */
2442 while (h
->root
.type
== bfd_link_hash_indirect
)
2443 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2445 if (h
->root
.type
!= bfd_link_hash_defined
2446 && h
->root
.type
!= bfd_link_hash_defweak
)
2449 h
->ref_regular_nonweak
= 1;
2453 if (h
->root
.u
.def
.section
->owner
!= NULL
2454 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2455 == bfd_target_elf_flavour
))
2458 h
->ref_regular_nonweak
= 1;
2464 if (h
->dynindx
== -1
2468 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2477 /* Unfortunately, NON_ELF is only correct if the symbol
2478 was first seen in a non-ELF file. Fortunately, if the symbol
2479 was first seen in an ELF file, we're probably OK unless the
2480 symbol was defined in a non-ELF file. Catch that case here.
2481 FIXME: We're still in trouble if the symbol was first seen in
2482 a dynamic object, and then later in a non-ELF regular object. */
2483 if ((h
->root
.type
== bfd_link_hash_defined
2484 || h
->root
.type
== bfd_link_hash_defweak
)
2486 && (h
->root
.u
.def
.section
->owner
!= NULL
2487 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2488 != bfd_target_elf_flavour
)
2489 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2490 && !h
->def_dynamic
)))
2494 /* Backend specific symbol fixup. */
2495 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2496 if (bed
->elf_backend_fixup_symbol
2497 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2500 /* If this is a final link, and the symbol was defined as a common
2501 symbol in a regular object file, and there was no definition in
2502 any dynamic object, then the linker will have allocated space for
2503 the symbol in a common section but the DEF_REGULAR
2504 flag will not have been set. */
2505 if (h
->root
.type
== bfd_link_hash_defined
2509 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2512 /* If -Bsymbolic was used (which means to bind references to global
2513 symbols to the definition within the shared object), and this
2514 symbol was defined in a regular object, then it actually doesn't
2515 need a PLT entry. Likewise, if the symbol has non-default
2516 visibility. If the symbol has hidden or internal visibility, we
2517 will force it local. */
2519 && eif
->info
->shared
2520 && is_elf_hash_table (eif
->info
->hash
)
2521 && (SYMBOLIC_BIND (eif
->info
, h
)
2522 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2525 bfd_boolean force_local
;
2527 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2528 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2529 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2532 /* If a weak undefined symbol has non-default visibility, we also
2533 hide it from the dynamic linker. */
2534 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2535 && h
->root
.type
== bfd_link_hash_undefweak
)
2536 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2538 /* If this is a weak defined symbol in a dynamic object, and we know
2539 the real definition in the dynamic object, copy interesting flags
2540 over to the real definition. */
2541 if (h
->u
.weakdef
!= NULL
)
2543 /* If the real definition is defined by a regular object file,
2544 don't do anything special. See the longer description in
2545 _bfd_elf_adjust_dynamic_symbol, below. */
2546 if (h
->u
.weakdef
->def_regular
)
2547 h
->u
.weakdef
= NULL
;
2550 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2552 while (h
->root
.type
== bfd_link_hash_indirect
)
2553 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2555 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2556 || h
->root
.type
== bfd_link_hash_defweak
);
2557 BFD_ASSERT (weakdef
->def_dynamic
);
2558 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2559 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2560 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2567 /* Make the backend pick a good value for a dynamic symbol. This is
2568 called via elf_link_hash_traverse, and also calls itself
2572 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2574 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2576 const struct elf_backend_data
*bed
;
2578 if (! is_elf_hash_table (eif
->info
->hash
))
2581 /* Ignore indirect symbols. These are added by the versioning code. */
2582 if (h
->root
.type
== bfd_link_hash_indirect
)
2585 /* Fix the symbol flags. */
2586 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2589 /* If this symbol does not require a PLT entry, and it is not
2590 defined by a dynamic object, or is not referenced by a regular
2591 object, ignore it. We do have to handle a weak defined symbol,
2592 even if no regular object refers to it, if we decided to add it
2593 to the dynamic symbol table. FIXME: Do we normally need to worry
2594 about symbols which are defined by one dynamic object and
2595 referenced by another one? */
2597 && h
->type
!= STT_GNU_IFUNC
2601 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2603 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2607 /* If we've already adjusted this symbol, don't do it again. This
2608 can happen via a recursive call. */
2609 if (h
->dynamic_adjusted
)
2612 /* Don't look at this symbol again. Note that we must set this
2613 after checking the above conditions, because we may look at a
2614 symbol once, decide not to do anything, and then get called
2615 recursively later after REF_REGULAR is set below. */
2616 h
->dynamic_adjusted
= 1;
2618 /* If this is a weak definition, and we know a real definition, and
2619 the real symbol is not itself defined by a regular object file,
2620 then get a good value for the real definition. We handle the
2621 real symbol first, for the convenience of the backend routine.
2623 Note that there is a confusing case here. If the real definition
2624 is defined by a regular object file, we don't get the real symbol
2625 from the dynamic object, but we do get the weak symbol. If the
2626 processor backend uses a COPY reloc, then if some routine in the
2627 dynamic object changes the real symbol, we will not see that
2628 change in the corresponding weak symbol. This is the way other
2629 ELF linkers work as well, and seems to be a result of the shared
2632 I will clarify this issue. Most SVR4 shared libraries define the
2633 variable _timezone and define timezone as a weak synonym. The
2634 tzset call changes _timezone. If you write
2635 extern int timezone;
2637 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2638 you might expect that, since timezone is a synonym for _timezone,
2639 the same number will print both times. However, if the processor
2640 backend uses a COPY reloc, then actually timezone will be copied
2641 into your process image, and, since you define _timezone
2642 yourself, _timezone will not. Thus timezone and _timezone will
2643 wind up at different memory locations. The tzset call will set
2644 _timezone, leaving timezone unchanged. */
2646 if (h
->u
.weakdef
!= NULL
)
2648 /* If we get to this point, there is an implicit reference to
2649 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2650 h
->u
.weakdef
->ref_regular
= 1;
2652 /* Ensure that the backend adjust_dynamic_symbol function sees
2653 H->U.WEAKDEF before H by recursively calling ourselves. */
2654 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2658 /* If a symbol has no type and no size and does not require a PLT
2659 entry, then we are probably about to do the wrong thing here: we
2660 are probably going to create a COPY reloc for an empty object.
2661 This case can arise when a shared object is built with assembly
2662 code, and the assembly code fails to set the symbol type. */
2664 && h
->type
== STT_NOTYPE
2666 (*_bfd_error_handler
)
2667 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2668 h
->root
.root
.string
);
2670 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2671 bed
= get_elf_backend_data (dynobj
);
2673 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2682 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2686 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2689 unsigned int power_of_two
;
2691 asection
*sec
= h
->root
.u
.def
.section
;
2693 /* The section aligment of definition is the maximum alignment
2694 requirement of symbols defined in the section. Since we don't
2695 know the symbol alignment requirement, we start with the
2696 maximum alignment and check low bits of the symbol address
2697 for the minimum alignment. */
2698 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2699 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2700 while ((h
->root
.u
.def
.value
& mask
) != 0)
2706 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2709 /* Adjust the section alignment if needed. */
2710 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2715 /* We make sure that the symbol will be aligned properly. */
2716 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2718 /* Define the symbol as being at this point in DYNBSS. */
2719 h
->root
.u
.def
.section
= dynbss
;
2720 h
->root
.u
.def
.value
= dynbss
->size
;
2722 /* Increment the size of DYNBSS to make room for the symbol. */
2723 dynbss
->size
+= h
->size
;
2728 /* Adjust all external symbols pointing into SEC_MERGE sections
2729 to reflect the object merging within the sections. */
2732 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2736 if ((h
->root
.type
== bfd_link_hash_defined
2737 || h
->root
.type
== bfd_link_hash_defweak
)
2738 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2739 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2741 bfd
*output_bfd
= (bfd
*) data
;
2743 h
->root
.u
.def
.value
=
2744 _bfd_merged_section_offset (output_bfd
,
2745 &h
->root
.u
.def
.section
,
2746 elf_section_data (sec
)->sec_info
,
2747 h
->root
.u
.def
.value
);
2753 /* Returns false if the symbol referred to by H should be considered
2754 to resolve local to the current module, and true if it should be
2755 considered to bind dynamically. */
2758 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2759 struct bfd_link_info
*info
,
2760 bfd_boolean not_local_protected
)
2762 bfd_boolean binding_stays_local_p
;
2763 const struct elf_backend_data
*bed
;
2764 struct elf_link_hash_table
*hash_table
;
2769 while (h
->root
.type
== bfd_link_hash_indirect
2770 || h
->root
.type
== bfd_link_hash_warning
)
2771 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2773 /* If it was forced local, then clearly it's not dynamic. */
2774 if (h
->dynindx
== -1)
2776 if (h
->forced_local
)
2779 /* Identify the cases where name binding rules say that a
2780 visible symbol resolves locally. */
2781 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2783 switch (ELF_ST_VISIBILITY (h
->other
))
2790 hash_table
= elf_hash_table (info
);
2791 if (!is_elf_hash_table (hash_table
))
2794 bed
= get_elf_backend_data (hash_table
->dynobj
);
2796 /* Proper resolution for function pointer equality may require
2797 that these symbols perhaps be resolved dynamically, even though
2798 we should be resolving them to the current module. */
2799 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2800 binding_stays_local_p
= TRUE
;
2807 /* If it isn't defined locally, then clearly it's dynamic. */
2808 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2811 /* Otherwise, the symbol is dynamic if binding rules don't tell
2812 us that it remains local. */
2813 return !binding_stays_local_p
;
2816 /* Return true if the symbol referred to by H should be considered
2817 to resolve local to the current module, and false otherwise. Differs
2818 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2819 undefined symbols. The two functions are virtually identical except
2820 for the place where forced_local and dynindx == -1 are tested. If
2821 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2822 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2823 the symbol is local only for defined symbols.
2824 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2825 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2826 treatment of undefined weak symbols. For those that do not make
2827 undefined weak symbols dynamic, both functions may return false. */
2830 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2831 struct bfd_link_info
*info
,
2832 bfd_boolean local_protected
)
2834 const struct elf_backend_data
*bed
;
2835 struct elf_link_hash_table
*hash_table
;
2837 /* If it's a local sym, of course we resolve locally. */
2841 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2842 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2843 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2846 /* Common symbols that become definitions don't get the DEF_REGULAR
2847 flag set, so test it first, and don't bail out. */
2848 if (ELF_COMMON_DEF_P (h
))
2850 /* If we don't have a definition in a regular file, then we can't
2851 resolve locally. The sym is either undefined or dynamic. */
2852 else if (!h
->def_regular
)
2855 /* Forced local symbols resolve locally. */
2856 if (h
->forced_local
)
2859 /* As do non-dynamic symbols. */
2860 if (h
->dynindx
== -1)
2863 /* At this point, we know the symbol is defined and dynamic. In an
2864 executable it must resolve locally, likewise when building symbolic
2865 shared libraries. */
2866 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2869 /* Now deal with defined dynamic symbols in shared libraries. Ones
2870 with default visibility might not resolve locally. */
2871 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2874 hash_table
= elf_hash_table (info
);
2875 if (!is_elf_hash_table (hash_table
))
2878 bed
= get_elf_backend_data (hash_table
->dynobj
);
2880 /* STV_PROTECTED non-function symbols are local. */
2881 if (!bed
->is_function_type (h
->type
))
2884 /* Function pointer equality tests may require that STV_PROTECTED
2885 symbols be treated as dynamic symbols. If the address of a
2886 function not defined in an executable is set to that function's
2887 plt entry in the executable, then the address of the function in
2888 a shared library must also be the plt entry in the executable. */
2889 return local_protected
;
2892 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2893 aligned. Returns the first TLS output section. */
2895 struct bfd_section
*
2896 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2898 struct bfd_section
*sec
, *tls
;
2899 unsigned int align
= 0;
2901 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2902 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2906 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2907 if (sec
->alignment_power
> align
)
2908 align
= sec
->alignment_power
;
2910 elf_hash_table (info
)->tls_sec
= tls
;
2912 /* Ensure the alignment of the first section is the largest alignment,
2913 so that the tls segment starts aligned. */
2915 tls
->alignment_power
= align
;
2920 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2922 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2923 Elf_Internal_Sym
*sym
)
2925 const struct elf_backend_data
*bed
;
2927 /* Local symbols do not count, but target specific ones might. */
2928 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2929 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2932 bed
= get_elf_backend_data (abfd
);
2933 /* Function symbols do not count. */
2934 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2937 /* If the section is undefined, then so is the symbol. */
2938 if (sym
->st_shndx
== SHN_UNDEF
)
2941 /* If the symbol is defined in the common section, then
2942 it is a common definition and so does not count. */
2943 if (bed
->common_definition (sym
))
2946 /* If the symbol is in a target specific section then we
2947 must rely upon the backend to tell us what it is. */
2948 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2949 /* FIXME - this function is not coded yet:
2951 return _bfd_is_global_symbol_definition (abfd, sym);
2953 Instead for now assume that the definition is not global,
2954 Even if this is wrong, at least the linker will behave
2955 in the same way that it used to do. */
2961 /* Search the symbol table of the archive element of the archive ABFD
2962 whose archive map contains a mention of SYMDEF, and determine if
2963 the symbol is defined in this element. */
2965 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2967 Elf_Internal_Shdr
* hdr
;
2968 bfd_size_type symcount
;
2969 bfd_size_type extsymcount
;
2970 bfd_size_type extsymoff
;
2971 Elf_Internal_Sym
*isymbuf
;
2972 Elf_Internal_Sym
*isym
;
2973 Elf_Internal_Sym
*isymend
;
2976 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2980 if (! bfd_check_format (abfd
, bfd_object
))
2983 /* If we have already included the element containing this symbol in the
2984 link then we do not need to include it again. Just claim that any symbol
2985 it contains is not a definition, so that our caller will not decide to
2986 (re)include this element. */
2987 if (abfd
->archive_pass
)
2990 /* Select the appropriate symbol table. */
2991 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2992 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2994 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2996 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2998 /* The sh_info field of the symtab header tells us where the
2999 external symbols start. We don't care about the local symbols. */
3000 if (elf_bad_symtab (abfd
))
3002 extsymcount
= symcount
;
3007 extsymcount
= symcount
- hdr
->sh_info
;
3008 extsymoff
= hdr
->sh_info
;
3011 if (extsymcount
== 0)
3014 /* Read in the symbol table. */
3015 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3017 if (isymbuf
== NULL
)
3020 /* Scan the symbol table looking for SYMDEF. */
3022 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3026 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3031 if (strcmp (name
, symdef
->name
) == 0)
3033 result
= is_global_data_symbol_definition (abfd
, isym
);
3043 /* Add an entry to the .dynamic table. */
3046 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3050 struct elf_link_hash_table
*hash_table
;
3051 const struct elf_backend_data
*bed
;
3053 bfd_size_type newsize
;
3054 bfd_byte
*newcontents
;
3055 Elf_Internal_Dyn dyn
;
3057 hash_table
= elf_hash_table (info
);
3058 if (! is_elf_hash_table (hash_table
))
3061 bed
= get_elf_backend_data (hash_table
->dynobj
);
3062 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3063 BFD_ASSERT (s
!= NULL
);
3065 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3066 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3067 if (newcontents
== NULL
)
3071 dyn
.d_un
.d_val
= val
;
3072 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3075 s
->contents
= newcontents
;
3080 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3081 otherwise just check whether one already exists. Returns -1 on error,
3082 1 if a DT_NEEDED tag already exists, and 0 on success. */
3085 elf_add_dt_needed_tag (bfd
*abfd
,
3086 struct bfd_link_info
*info
,
3090 struct elf_link_hash_table
*hash_table
;
3091 bfd_size_type oldsize
;
3092 bfd_size_type strindex
;
3094 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3097 hash_table
= elf_hash_table (info
);
3098 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3099 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3100 if (strindex
== (bfd_size_type
) -1)
3103 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3106 const struct elf_backend_data
*bed
;
3109 bed
= get_elf_backend_data (hash_table
->dynobj
);
3110 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3112 for (extdyn
= sdyn
->contents
;
3113 extdyn
< sdyn
->contents
+ sdyn
->size
;
3114 extdyn
+= bed
->s
->sizeof_dyn
)
3116 Elf_Internal_Dyn dyn
;
3118 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3119 if (dyn
.d_tag
== DT_NEEDED
3120 && dyn
.d_un
.d_val
== strindex
)
3122 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3130 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3133 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3137 /* We were just checking for existence of the tag. */
3138 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3144 on_needed_list (const char *soname
, struct bfd_link_needed_list
*needed
)
3146 for (; needed
!= NULL
; needed
= needed
->next
)
3147 if (strcmp (soname
, needed
->name
) == 0)
3153 /* Sort symbol by value, section, and size. */
3155 elf_sort_symbol (const void *arg1
, const void *arg2
)
3157 const struct elf_link_hash_entry
*h1
;
3158 const struct elf_link_hash_entry
*h2
;
3159 bfd_signed_vma vdiff
;
3161 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3162 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3163 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3165 return vdiff
> 0 ? 1 : -1;
3168 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3170 return sdiff
> 0 ? 1 : -1;
3172 vdiff
= h1
->size
- h2
->size
;
3173 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3176 /* This function is used to adjust offsets into .dynstr for
3177 dynamic symbols. This is called via elf_link_hash_traverse. */
3180 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3182 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3184 if (h
->dynindx
!= -1)
3185 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3189 /* Assign string offsets in .dynstr, update all structures referencing
3193 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3195 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3196 struct elf_link_local_dynamic_entry
*entry
;
3197 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3198 bfd
*dynobj
= hash_table
->dynobj
;
3201 const struct elf_backend_data
*bed
;
3204 _bfd_elf_strtab_finalize (dynstr
);
3205 size
= _bfd_elf_strtab_size (dynstr
);
3207 bed
= get_elf_backend_data (dynobj
);
3208 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3209 BFD_ASSERT (sdyn
!= NULL
);
3211 /* Update all .dynamic entries referencing .dynstr strings. */
3212 for (extdyn
= sdyn
->contents
;
3213 extdyn
< sdyn
->contents
+ sdyn
->size
;
3214 extdyn
+= bed
->s
->sizeof_dyn
)
3216 Elf_Internal_Dyn dyn
;
3218 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3222 dyn
.d_un
.d_val
= size
;
3232 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3237 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3240 /* Now update local dynamic symbols. */
3241 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3242 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3243 entry
->isym
.st_name
);
3245 /* And the rest of dynamic symbols. */
3246 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3248 /* Adjust version definitions. */
3249 if (elf_tdata (output_bfd
)->cverdefs
)
3254 Elf_Internal_Verdef def
;
3255 Elf_Internal_Verdaux defaux
;
3257 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3261 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3263 p
+= sizeof (Elf_External_Verdef
);
3264 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3266 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3268 _bfd_elf_swap_verdaux_in (output_bfd
,
3269 (Elf_External_Verdaux
*) p
, &defaux
);
3270 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3272 _bfd_elf_swap_verdaux_out (output_bfd
,
3273 &defaux
, (Elf_External_Verdaux
*) p
);
3274 p
+= sizeof (Elf_External_Verdaux
);
3277 while (def
.vd_next
);
3280 /* Adjust version references. */
3281 if (elf_tdata (output_bfd
)->verref
)
3286 Elf_Internal_Verneed need
;
3287 Elf_Internal_Vernaux needaux
;
3289 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3293 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3295 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3296 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3297 (Elf_External_Verneed
*) p
);
3298 p
+= sizeof (Elf_External_Verneed
);
3299 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3301 _bfd_elf_swap_vernaux_in (output_bfd
,
3302 (Elf_External_Vernaux
*) p
, &needaux
);
3303 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3305 _bfd_elf_swap_vernaux_out (output_bfd
,
3307 (Elf_External_Vernaux
*) p
);
3308 p
+= sizeof (Elf_External_Vernaux
);
3311 while (need
.vn_next
);
3317 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3318 The default is to only match when the INPUT and OUTPUT are exactly
3322 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3323 const bfd_target
*output
)
3325 return input
== output
;
3328 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3329 This version is used when different targets for the same architecture
3330 are virtually identical. */
3333 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3334 const bfd_target
*output
)
3336 const struct elf_backend_data
*obed
, *ibed
;
3338 if (input
== output
)
3341 ibed
= xvec_get_elf_backend_data (input
);
3342 obed
= xvec_get_elf_backend_data (output
);
3344 if (ibed
->arch
!= obed
->arch
)
3347 /* If both backends are using this function, deem them compatible. */
3348 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3351 /* Add symbols from an ELF object file to the linker hash table. */
3354 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3356 Elf_Internal_Ehdr
*ehdr
;
3357 Elf_Internal_Shdr
*hdr
;
3358 bfd_size_type symcount
;
3359 bfd_size_type extsymcount
;
3360 bfd_size_type extsymoff
;
3361 struct elf_link_hash_entry
**sym_hash
;
3362 bfd_boolean dynamic
;
3363 Elf_External_Versym
*extversym
= NULL
;
3364 Elf_External_Versym
*ever
;
3365 struct elf_link_hash_entry
*weaks
;
3366 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3367 bfd_size_type nondeflt_vers_cnt
= 0;
3368 Elf_Internal_Sym
*isymbuf
= NULL
;
3369 Elf_Internal_Sym
*isym
;
3370 Elf_Internal_Sym
*isymend
;
3371 const struct elf_backend_data
*bed
;
3372 bfd_boolean add_needed
;
3373 struct elf_link_hash_table
*htab
;
3375 void *alloc_mark
= NULL
;
3376 struct bfd_hash_entry
**old_table
= NULL
;
3377 unsigned int old_size
= 0;
3378 unsigned int old_count
= 0;
3379 void *old_tab
= NULL
;
3382 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3383 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3384 long old_dynsymcount
= 0;
3386 size_t hashsize
= 0;
3388 htab
= elf_hash_table (info
);
3389 bed
= get_elf_backend_data (abfd
);
3391 if ((abfd
->flags
& DYNAMIC
) == 0)
3397 /* You can't use -r against a dynamic object. Also, there's no
3398 hope of using a dynamic object which does not exactly match
3399 the format of the output file. */
3400 if (info
->relocatable
3401 || !is_elf_hash_table (htab
)
3402 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3404 if (info
->relocatable
)
3405 bfd_set_error (bfd_error_invalid_operation
);
3407 bfd_set_error (bfd_error_wrong_format
);
3412 ehdr
= elf_elfheader (abfd
);
3413 if (info
->warn_alternate_em
3414 && bed
->elf_machine_code
!= ehdr
->e_machine
3415 && ((bed
->elf_machine_alt1
!= 0
3416 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3417 || (bed
->elf_machine_alt2
!= 0
3418 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3419 info
->callbacks
->einfo
3420 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3421 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3423 /* As a GNU extension, any input sections which are named
3424 .gnu.warning.SYMBOL are treated as warning symbols for the given
3425 symbol. This differs from .gnu.warning sections, which generate
3426 warnings when they are included in an output file. */
3427 /* PR 12761: Also generate this warning when building shared libraries. */
3428 if (info
->executable
|| info
->shared
)
3432 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3436 name
= bfd_get_section_name (abfd
, s
);
3437 if (CONST_STRNEQ (name
, ".gnu.warning."))
3442 name
+= sizeof ".gnu.warning." - 1;
3444 /* If this is a shared object, then look up the symbol
3445 in the hash table. If it is there, and it is already
3446 been defined, then we will not be using the entry
3447 from this shared object, so we don't need to warn.
3448 FIXME: If we see the definition in a regular object
3449 later on, we will warn, but we shouldn't. The only
3450 fix is to keep track of what warnings we are supposed
3451 to emit, and then handle them all at the end of the
3455 struct elf_link_hash_entry
*h
;
3457 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3459 /* FIXME: What about bfd_link_hash_common? */
3461 && (h
->root
.type
== bfd_link_hash_defined
3462 || h
->root
.type
== bfd_link_hash_defweak
))
3464 /* We don't want to issue this warning. Clobber
3465 the section size so that the warning does not
3466 get copied into the output file. */
3473 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3477 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3482 if (! (_bfd_generic_link_add_one_symbol
3483 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3484 FALSE
, bed
->collect
, NULL
)))
3487 if (! info
->relocatable
)
3489 /* Clobber the section size so that the warning does
3490 not get copied into the output file. */
3493 /* Also set SEC_EXCLUDE, so that symbols defined in
3494 the warning section don't get copied to the output. */
3495 s
->flags
|= SEC_EXCLUDE
;
3504 /* If we are creating a shared library, create all the dynamic
3505 sections immediately. We need to attach them to something,
3506 so we attach them to this BFD, provided it is the right
3507 format. FIXME: If there are no input BFD's of the same
3508 format as the output, we can't make a shared library. */
3510 && is_elf_hash_table (htab
)
3511 && info
->output_bfd
->xvec
== abfd
->xvec
3512 && !htab
->dynamic_sections_created
)
3514 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3518 else if (!is_elf_hash_table (htab
))
3523 const char *soname
= NULL
;
3525 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3528 /* ld --just-symbols and dynamic objects don't mix very well.
3529 ld shouldn't allow it. */
3530 if ((s
= abfd
->sections
) != NULL
3531 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
3534 /* If this dynamic lib was specified on the command line with
3535 --as-needed in effect, then we don't want to add a DT_NEEDED
3536 tag unless the lib is actually used. Similary for libs brought
3537 in by another lib's DT_NEEDED. When --no-add-needed is used
3538 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3539 any dynamic library in DT_NEEDED tags in the dynamic lib at
3541 add_needed
= (elf_dyn_lib_class (abfd
)
3542 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3543 | DYN_NO_NEEDED
)) == 0;
3545 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3550 unsigned int elfsec
;
3551 unsigned long shlink
;
3553 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3560 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3561 if (elfsec
== SHN_BAD
)
3562 goto error_free_dyn
;
3563 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3565 for (extdyn
= dynbuf
;
3566 extdyn
< dynbuf
+ s
->size
;
3567 extdyn
+= bed
->s
->sizeof_dyn
)
3569 Elf_Internal_Dyn dyn
;
3571 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3572 if (dyn
.d_tag
== DT_SONAME
)
3574 unsigned int tagv
= dyn
.d_un
.d_val
;
3575 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3577 goto error_free_dyn
;
3579 if (dyn
.d_tag
== DT_NEEDED
)
3581 struct bfd_link_needed_list
*n
, **pn
;
3583 unsigned int tagv
= dyn
.d_un
.d_val
;
3585 amt
= sizeof (struct bfd_link_needed_list
);
3586 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3587 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3588 if (n
== NULL
|| fnm
== NULL
)
3589 goto error_free_dyn
;
3590 amt
= strlen (fnm
) + 1;
3591 anm
= (char *) bfd_alloc (abfd
, amt
);
3593 goto error_free_dyn
;
3594 memcpy (anm
, fnm
, amt
);
3598 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3602 if (dyn
.d_tag
== DT_RUNPATH
)
3604 struct bfd_link_needed_list
*n
, **pn
;
3606 unsigned int tagv
= dyn
.d_un
.d_val
;
3608 amt
= sizeof (struct bfd_link_needed_list
);
3609 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3610 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3611 if (n
== NULL
|| fnm
== NULL
)
3612 goto error_free_dyn
;
3613 amt
= strlen (fnm
) + 1;
3614 anm
= (char *) bfd_alloc (abfd
, amt
);
3616 goto error_free_dyn
;
3617 memcpy (anm
, fnm
, amt
);
3621 for (pn
= & runpath
;
3627 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3628 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3630 struct bfd_link_needed_list
*n
, **pn
;
3632 unsigned int tagv
= dyn
.d_un
.d_val
;
3634 amt
= sizeof (struct bfd_link_needed_list
);
3635 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3636 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3637 if (n
== NULL
|| fnm
== NULL
)
3638 goto error_free_dyn
;
3639 amt
= strlen (fnm
) + 1;
3640 anm
= (char *) bfd_alloc (abfd
, amt
);
3642 goto error_free_dyn
;
3643 memcpy (anm
, fnm
, amt
);
3653 if (dyn
.d_tag
== DT_AUDIT
)
3655 unsigned int tagv
= dyn
.d_un
.d_val
;
3656 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3663 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3664 frees all more recently bfd_alloc'd blocks as well. */
3670 struct bfd_link_needed_list
**pn
;
3671 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3676 /* We do not want to include any of the sections in a dynamic
3677 object in the output file. We hack by simply clobbering the
3678 list of sections in the BFD. This could be handled more
3679 cleanly by, say, a new section flag; the existing
3680 SEC_NEVER_LOAD flag is not the one we want, because that one
3681 still implies that the section takes up space in the output
3683 bfd_section_list_clear (abfd
);
3685 /* Find the name to use in a DT_NEEDED entry that refers to this
3686 object. If the object has a DT_SONAME entry, we use it.
3687 Otherwise, if the generic linker stuck something in
3688 elf_dt_name, we use that. Otherwise, we just use the file
3690 if (soname
== NULL
|| *soname
== '\0')
3692 soname
= elf_dt_name (abfd
);
3693 if (soname
== NULL
|| *soname
== '\0')
3694 soname
= bfd_get_filename (abfd
);
3697 /* Save the SONAME because sometimes the linker emulation code
3698 will need to know it. */
3699 elf_dt_name (abfd
) = soname
;
3701 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3705 /* If we have already included this dynamic object in the
3706 link, just ignore it. There is no reason to include a
3707 particular dynamic object more than once. */
3711 /* Save the DT_AUDIT entry for the linker emulation code. */
3712 elf_dt_audit (abfd
) = audit
;
3715 /* If this is a dynamic object, we always link against the .dynsym
3716 symbol table, not the .symtab symbol table. The dynamic linker
3717 will only see the .dynsym symbol table, so there is no reason to
3718 look at .symtab for a dynamic object. */
3720 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3721 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3723 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3725 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3727 /* The sh_info field of the symtab header tells us where the
3728 external symbols start. We don't care about the local symbols at
3730 if (elf_bad_symtab (abfd
))
3732 extsymcount
= symcount
;
3737 extsymcount
= symcount
- hdr
->sh_info
;
3738 extsymoff
= hdr
->sh_info
;
3742 if (extsymcount
!= 0)
3744 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3746 if (isymbuf
== NULL
)
3749 /* We store a pointer to the hash table entry for each external
3751 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3752 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
3753 if (sym_hash
== NULL
)
3754 goto error_free_sym
;
3755 elf_sym_hashes (abfd
) = sym_hash
;
3760 /* Read in any version definitions. */
3761 if (!_bfd_elf_slurp_version_tables (abfd
,
3762 info
->default_imported_symver
))
3763 goto error_free_sym
;
3765 /* Read in the symbol versions, but don't bother to convert them
3766 to internal format. */
3767 if (elf_dynversym (abfd
) != 0)
3769 Elf_Internal_Shdr
*versymhdr
;
3771 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3772 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
3773 if (extversym
== NULL
)
3774 goto error_free_sym
;
3775 amt
= versymhdr
->sh_size
;
3776 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3777 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3778 goto error_free_vers
;
3782 /* If we are loading an as-needed shared lib, save the symbol table
3783 state before we start adding symbols. If the lib turns out
3784 to be unneeded, restore the state. */
3785 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3790 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3792 struct bfd_hash_entry
*p
;
3793 struct elf_link_hash_entry
*h
;
3795 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3797 h
= (struct elf_link_hash_entry
*) p
;
3798 entsize
+= htab
->root
.table
.entsize
;
3799 if (h
->root
.type
== bfd_link_hash_warning
)
3800 entsize
+= htab
->root
.table
.entsize
;
3804 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3805 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3806 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3807 if (old_tab
== NULL
)
3808 goto error_free_vers
;
3810 /* Remember the current objalloc pointer, so that all mem for
3811 symbols added can later be reclaimed. */
3812 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3813 if (alloc_mark
== NULL
)
3814 goto error_free_vers
;
3816 /* Make a special call to the linker "notice" function to
3817 tell it that we are about to handle an as-needed lib. */
3818 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3819 notice_as_needed
, 0, NULL
))
3820 goto error_free_vers
;
3822 /* Clone the symbol table and sym hashes. Remember some
3823 pointers into the symbol table, and dynamic symbol count. */
3824 old_hash
= (char *) old_tab
+ tabsize
;
3825 old_ent
= (char *) old_hash
+ hashsize
;
3826 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3827 memcpy (old_hash
, sym_hash
, hashsize
);
3828 old_undefs
= htab
->root
.undefs
;
3829 old_undefs_tail
= htab
->root
.undefs_tail
;
3830 old_table
= htab
->root
.table
.table
;
3831 old_size
= htab
->root
.table
.size
;
3832 old_count
= htab
->root
.table
.count
;
3833 old_dynsymcount
= htab
->dynsymcount
;
3835 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3837 struct bfd_hash_entry
*p
;
3838 struct elf_link_hash_entry
*h
;
3840 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3842 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3843 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3844 h
= (struct elf_link_hash_entry
*) p
;
3845 if (h
->root
.type
== bfd_link_hash_warning
)
3847 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3848 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3855 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3856 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3858 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3862 asection
*sec
, *new_sec
;
3865 struct elf_link_hash_entry
*h
;
3866 struct elf_link_hash_entry
*hi
;
3867 bfd_boolean definition
;
3868 bfd_boolean size_change_ok
;
3869 bfd_boolean type_change_ok
;
3870 bfd_boolean new_weakdef
;
3871 bfd_boolean override
;
3873 unsigned int old_alignment
;
3875 bfd
* undef_bfd
= NULL
;
3879 flags
= BSF_NO_FLAGS
;
3881 value
= isym
->st_value
;
3883 common
= bed
->common_definition (isym
);
3885 bind
= ELF_ST_BIND (isym
->st_info
);
3889 /* This should be impossible, since ELF requires that all
3890 global symbols follow all local symbols, and that sh_info
3891 point to the first global symbol. Unfortunately, Irix 5
3896 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3904 case STB_GNU_UNIQUE
:
3905 flags
= BSF_GNU_UNIQUE
;
3909 /* Leave it up to the processor backend. */
3913 if (isym
->st_shndx
== SHN_UNDEF
)
3914 sec
= bfd_und_section_ptr
;
3915 else if (isym
->st_shndx
== SHN_ABS
)
3916 sec
= bfd_abs_section_ptr
;
3917 else if (isym
->st_shndx
== SHN_COMMON
)
3919 sec
= bfd_com_section_ptr
;
3920 /* What ELF calls the size we call the value. What ELF
3921 calls the value we call the alignment. */
3922 value
= isym
->st_size
;
3926 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3928 sec
= bfd_abs_section_ptr
;
3929 else if (discarded_section (sec
))
3931 /* Symbols from discarded section are undefined. We keep
3933 sec
= bfd_und_section_ptr
;
3934 isym
->st_shndx
= SHN_UNDEF
;
3936 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3940 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3943 goto error_free_vers
;
3945 if (isym
->st_shndx
== SHN_COMMON
3946 && (abfd
->flags
& BFD_PLUGIN
) != 0)
3948 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
3952 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
3954 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
3956 goto error_free_vers
;
3960 else if (isym
->st_shndx
== SHN_COMMON
3961 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3962 && !info
->relocatable
)
3964 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3968 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
3969 | SEC_LINKER_CREATED
);
3970 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
3972 goto error_free_vers
;
3976 else if (bed
->elf_add_symbol_hook
)
3978 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3980 goto error_free_vers
;
3982 /* The hook function sets the name to NULL if this symbol
3983 should be skipped for some reason. */
3988 /* Sanity check that all possibilities were handled. */
3991 bfd_set_error (bfd_error_bad_value
);
3992 goto error_free_vers
;
3995 if (bfd_is_und_section (sec
)
3996 || bfd_is_com_section (sec
))
4001 size_change_ok
= FALSE
;
4002 type_change_ok
= bed
->type_change_ok
;
4007 if (is_elf_hash_table (htab
))
4009 Elf_Internal_Versym iver
;
4010 unsigned int vernum
= 0;
4013 /* If this is a definition of a symbol which was previously
4014 referenced in a non-weak manner then make a note of the bfd
4015 that contained the reference. This is used if we need to
4016 refer to the source of the reference later on. */
4017 if (! bfd_is_und_section (sec
))
4019 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4022 && h
->root
.type
== bfd_link_hash_undefined
4023 && h
->root
.u
.undef
.abfd
)
4024 undef_bfd
= h
->root
.u
.undef
.abfd
;
4029 if (info
->default_imported_symver
)
4030 /* Use the default symbol version created earlier. */
4031 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4036 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4038 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4040 /* If this is a hidden symbol, or if it is not version
4041 1, we append the version name to the symbol name.
4042 However, we do not modify a non-hidden absolute symbol
4043 if it is not a function, because it might be the version
4044 symbol itself. FIXME: What if it isn't? */
4045 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4047 && (!bfd_is_abs_section (sec
)
4048 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4051 size_t namelen
, verlen
, newlen
;
4054 if (isym
->st_shndx
!= SHN_UNDEF
)
4056 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4058 else if (vernum
> 1)
4060 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4066 (*_bfd_error_handler
)
4067 (_("%B: %s: invalid version %u (max %d)"),
4069 elf_tdata (abfd
)->cverdefs
);
4070 bfd_set_error (bfd_error_bad_value
);
4071 goto error_free_vers
;
4076 /* We cannot simply test for the number of
4077 entries in the VERNEED section since the
4078 numbers for the needed versions do not start
4080 Elf_Internal_Verneed
*t
;
4083 for (t
= elf_tdata (abfd
)->verref
;
4087 Elf_Internal_Vernaux
*a
;
4089 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4091 if (a
->vna_other
== vernum
)
4093 verstr
= a
->vna_nodename
;
4102 (*_bfd_error_handler
)
4103 (_("%B: %s: invalid needed version %d"),
4104 abfd
, name
, vernum
);
4105 bfd_set_error (bfd_error_bad_value
);
4106 goto error_free_vers
;
4110 namelen
= strlen (name
);
4111 verlen
= strlen (verstr
);
4112 newlen
= namelen
+ verlen
+ 2;
4113 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4114 && isym
->st_shndx
!= SHN_UNDEF
)
4117 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4118 if (newname
== NULL
)
4119 goto error_free_vers
;
4120 memcpy (newname
, name
, namelen
);
4121 p
= newname
+ namelen
;
4123 /* If this is a defined non-hidden version symbol,
4124 we add another @ to the name. This indicates the
4125 default version of the symbol. */
4126 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4127 && isym
->st_shndx
!= SHN_UNDEF
)
4129 memcpy (p
, verstr
, verlen
+ 1);
4134 /* If necessary, make a second attempt to locate the bfd
4135 containing an unresolved, non-weak reference to the
4137 if (! bfd_is_und_section (sec
) && undef_bfd
== NULL
)
4139 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4142 && h
->root
.type
== bfd_link_hash_undefined
4143 && h
->root
.u
.undef
.abfd
)
4144 undef_bfd
= h
->root
.u
.undef
.abfd
;
4147 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
4148 &value
, &old_alignment
,
4149 sym_hash
, &skip
, &override
,
4150 &type_change_ok
, &size_change_ok
))
4151 goto error_free_vers
;
4160 while (h
->root
.type
== bfd_link_hash_indirect
4161 || h
->root
.type
== bfd_link_hash_warning
)
4162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4164 /* Remember the old alignment if this is a common symbol, so
4165 that we don't reduce the alignment later on. We can't
4166 check later, because _bfd_generic_link_add_one_symbol
4167 will set a default for the alignment which we want to
4168 override. We also remember the old bfd where the existing
4169 definition comes from. */
4170 switch (h
->root
.type
)
4175 case bfd_link_hash_defined
:
4176 case bfd_link_hash_defweak
:
4177 old_bfd
= h
->root
.u
.def
.section
->owner
;
4180 case bfd_link_hash_common
:
4181 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4182 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4186 if (elf_tdata (abfd
)->verdef
!= NULL
4190 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4193 if (! (_bfd_generic_link_add_one_symbol
4194 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4195 (struct bfd_link_hash_entry
**) sym_hash
)))
4196 goto error_free_vers
;
4199 /* We need to make sure that indirect symbol dynamic flags are
4202 while (h
->root
.type
== bfd_link_hash_indirect
4203 || h
->root
.type
== bfd_link_hash_warning
)
4204 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4207 if (is_elf_hash_table (htab
))
4208 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4210 new_weakdef
= FALSE
;
4213 && (flags
& BSF_WEAK
) != 0
4214 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4215 && is_elf_hash_table (htab
)
4216 && h
->u
.weakdef
== NULL
)
4218 /* Keep a list of all weak defined non function symbols from
4219 a dynamic object, using the weakdef field. Later in this
4220 function we will set the weakdef field to the correct
4221 value. We only put non-function symbols from dynamic
4222 objects on this list, because that happens to be the only
4223 time we need to know the normal symbol corresponding to a
4224 weak symbol, and the information is time consuming to
4225 figure out. If the weakdef field is not already NULL,
4226 then this symbol was already defined by some previous
4227 dynamic object, and we will be using that previous
4228 definition anyhow. */
4230 h
->u
.weakdef
= weaks
;
4235 /* Set the alignment of a common symbol. */
4236 if ((common
|| bfd_is_com_section (sec
))
4237 && h
->root
.type
== bfd_link_hash_common
)
4242 align
= bfd_log2 (isym
->st_value
);
4245 /* The new symbol is a common symbol in a shared object.
4246 We need to get the alignment from the section. */
4247 align
= new_sec
->alignment_power
;
4249 if (align
> old_alignment
)
4250 h
->root
.u
.c
.p
->alignment_power
= align
;
4252 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4255 if (is_elf_hash_table (htab
))
4259 /* Check the alignment when a common symbol is involved. This
4260 can change when a common symbol is overridden by a normal
4261 definition or a common symbol is ignored due to the old
4262 normal definition. We need to make sure the maximum
4263 alignment is maintained. */
4264 if ((old_alignment
|| common
)
4265 && h
->root
.type
!= bfd_link_hash_common
)
4267 unsigned int common_align
;
4268 unsigned int normal_align
;
4269 unsigned int symbol_align
;
4273 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4274 if (h
->root
.u
.def
.section
->owner
!= NULL
4275 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4277 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4278 if (normal_align
> symbol_align
)
4279 normal_align
= symbol_align
;
4282 normal_align
= symbol_align
;
4286 common_align
= old_alignment
;
4287 common_bfd
= old_bfd
;
4292 common_align
= bfd_log2 (isym
->st_value
);
4294 normal_bfd
= old_bfd
;
4297 if (normal_align
< common_align
)
4299 /* PR binutils/2735 */
4300 if (normal_bfd
== NULL
)
4301 (*_bfd_error_handler
)
4302 (_("Warning: alignment %u of common symbol `%s' in %B"
4303 " is greater than the alignment (%u) of its section %A"),
4304 common_bfd
, h
->root
.u
.def
.section
,
4305 1 << common_align
, name
, 1 << normal_align
);
4307 (*_bfd_error_handler
)
4308 (_("Warning: alignment %u of symbol `%s' in %B"
4309 " is smaller than %u in %B"),
4310 normal_bfd
, common_bfd
,
4311 1 << normal_align
, name
, 1 << common_align
);
4315 /* Remember the symbol size if it isn't undefined. */
4316 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4317 && (definition
|| h
->size
== 0))
4320 && h
->size
!= isym
->st_size
4321 && ! size_change_ok
)
4322 (*_bfd_error_handler
)
4323 (_("Warning: size of symbol `%s' changed"
4324 " from %lu in %B to %lu in %B"),
4326 name
, (unsigned long) h
->size
,
4327 (unsigned long) isym
->st_size
);
4329 h
->size
= isym
->st_size
;
4332 /* If this is a common symbol, then we always want H->SIZE
4333 to be the size of the common symbol. The code just above
4334 won't fix the size if a common symbol becomes larger. We
4335 don't warn about a size change here, because that is
4336 covered by --warn-common. Allow changed between different
4338 if (h
->root
.type
== bfd_link_hash_common
)
4339 h
->size
= h
->root
.u
.c
.size
;
4341 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4342 && (definition
|| h
->type
== STT_NOTYPE
))
4344 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4346 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4348 if (type
== STT_GNU_IFUNC
4349 && (abfd
->flags
& DYNAMIC
) != 0)
4352 if (h
->type
!= type
)
4354 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4355 (*_bfd_error_handler
)
4356 (_("Warning: type of symbol `%s' changed"
4357 " from %d to %d in %B"),
4358 abfd
, name
, h
->type
, type
);
4364 /* Merge st_other field. */
4365 elf_merge_st_other (abfd
, h
, isym
, definition
, dynamic
);
4367 /* Set a flag in the hash table entry indicating the type of
4368 reference or definition we just found. Keep a count of
4369 the number of dynamic symbols we find. A dynamic symbol
4370 is one which is referenced or defined by both a regular
4371 object and a shared object. */
4378 if (bind
!= STB_WEAK
)
4379 h
->ref_regular_nonweak
= 1;
4391 /* If the indirect symbol has been forced local, don't
4392 make the real symbol dynamic. */
4393 if ((h
== hi
|| !hi
->forced_local
)
4394 && (! info
->executable
4404 hi
->ref_dynamic
= 1;
4410 hi
->def_dynamic
= 1;
4411 hi
->dynamic_def
= 1;
4414 /* If the indirect symbol has been forced local, don't
4415 make the real symbol dynamic. */
4416 if ((h
== hi
|| !hi
->forced_local
)
4419 || (h
->u
.weakdef
!= NULL
4421 && h
->u
.weakdef
->dynindx
!= -1)))
4425 /* We don't want to make debug symbol dynamic. */
4426 if (definition
&& (sec
->flags
& SEC_DEBUGGING
) && !info
->relocatable
)
4429 /* Nor should we make plugin symbols dynamic. */
4430 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4434 h
->target_internal
= isym
->st_target_internal
;
4436 /* Check to see if we need to add an indirect symbol for
4437 the default name. */
4438 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4439 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4440 &sec
, &value
, &dynsym
,
4442 goto error_free_vers
;
4444 if (definition
&& !dynamic
)
4446 char *p
= strchr (name
, ELF_VER_CHR
);
4447 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4449 /* Queue non-default versions so that .symver x, x@FOO
4450 aliases can be checked. */
4453 amt
= ((isymend
- isym
+ 1)
4454 * sizeof (struct elf_link_hash_entry
*));
4456 (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4458 goto error_free_vers
;
4460 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4464 if (dynsym
&& h
->dynindx
== -1)
4466 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4467 goto error_free_vers
;
4468 if (h
->u
.weakdef
!= NULL
4470 && h
->u
.weakdef
->dynindx
== -1)
4472 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4473 goto error_free_vers
;
4476 else if (dynsym
&& h
->dynindx
!= -1)
4477 /* If the symbol already has a dynamic index, but
4478 visibility says it should not be visible, turn it into
4480 switch (ELF_ST_VISIBILITY (h
->other
))
4484 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4494 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4495 && !on_needed_list (elf_dt_name (abfd
), htab
->needed
))))
4498 const char *soname
= elf_dt_name (abfd
);
4500 /* A symbol from a library loaded via DT_NEEDED of some
4501 other library is referenced by a regular object.
4502 Add a DT_NEEDED entry for it. Issue an error if
4503 --no-add-needed is used and the reference was not
4505 if (undef_bfd
!= NULL
4506 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4508 (*_bfd_error_handler
)
4509 (_("%B: undefined reference to symbol '%s'"),
4511 (*_bfd_error_handler
)
4512 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4514 bfd_set_error (bfd_error_invalid_operation
);
4515 goto error_free_vers
;
4518 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4519 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4522 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4524 goto error_free_vers
;
4526 BFD_ASSERT (ret
== 0);
4531 if (extversym
!= NULL
)
4537 if (isymbuf
!= NULL
)
4543 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4547 /* Restore the symbol table. */
4548 if (bed
->as_needed_cleanup
)
4549 (*bed
->as_needed_cleanup
) (abfd
, info
);
4550 old_hash
= (char *) old_tab
+ tabsize
;
4551 old_ent
= (char *) old_hash
+ hashsize
;
4552 sym_hash
= elf_sym_hashes (abfd
);
4553 htab
->root
.table
.table
= old_table
;
4554 htab
->root
.table
.size
= old_size
;
4555 htab
->root
.table
.count
= old_count
;
4556 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4557 memcpy (sym_hash
, old_hash
, hashsize
);
4558 htab
->root
.undefs
= old_undefs
;
4559 htab
->root
.undefs_tail
= old_undefs_tail
;
4560 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4562 struct bfd_hash_entry
*p
;
4563 struct elf_link_hash_entry
*h
;
4565 unsigned int alignment_power
;
4567 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4569 h
= (struct elf_link_hash_entry
*) p
;
4570 if (h
->root
.type
== bfd_link_hash_warning
)
4571 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4572 if (h
->dynindx
>= old_dynsymcount
)
4573 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4575 /* Preserve the maximum alignment and size for common
4576 symbols even if this dynamic lib isn't on DT_NEEDED
4577 since it can still be loaded at the run-time by another
4579 if (h
->root
.type
== bfd_link_hash_common
)
4581 size
= h
->root
.u
.c
.size
;
4582 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4587 alignment_power
= 0;
4589 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4590 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4591 h
= (struct elf_link_hash_entry
*) p
;
4592 if (h
->root
.type
== bfd_link_hash_warning
)
4594 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4595 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4597 else if (h
->root
.type
== bfd_link_hash_common
)
4599 if (size
> h
->root
.u
.c
.size
)
4600 h
->root
.u
.c
.size
= size
;
4601 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4602 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4607 /* Make a special call to the linker "notice" function to
4608 tell it that symbols added for crefs may need to be removed. */
4609 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4610 notice_not_needed
, 0, NULL
))
4611 goto error_free_vers
;
4614 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4616 if (nondeflt_vers
!= NULL
)
4617 free (nondeflt_vers
);
4621 if (old_tab
!= NULL
)
4623 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4624 notice_needed
, 0, NULL
))
4625 goto error_free_vers
;
4630 /* Now that all the symbols from this input file are created, handle
4631 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4632 if (nondeflt_vers
!= NULL
)
4634 bfd_size_type cnt
, symidx
;
4636 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4638 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4639 char *shortname
, *p
;
4641 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4643 || (h
->root
.type
!= bfd_link_hash_defined
4644 && h
->root
.type
!= bfd_link_hash_defweak
))
4647 amt
= p
- h
->root
.root
.string
;
4648 shortname
= (char *) bfd_malloc (amt
+ 1);
4650 goto error_free_vers
;
4651 memcpy (shortname
, h
->root
.root
.string
, amt
);
4652 shortname
[amt
] = '\0';
4654 hi
= (struct elf_link_hash_entry
*)
4655 bfd_link_hash_lookup (&htab
->root
, shortname
,
4656 FALSE
, FALSE
, FALSE
);
4658 && hi
->root
.type
== h
->root
.type
4659 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4660 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4662 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4663 hi
->root
.type
= bfd_link_hash_indirect
;
4664 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4665 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4666 sym_hash
= elf_sym_hashes (abfd
);
4668 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4669 if (sym_hash
[symidx
] == hi
)
4671 sym_hash
[symidx
] = h
;
4677 free (nondeflt_vers
);
4678 nondeflt_vers
= NULL
;
4681 /* Now set the weakdefs field correctly for all the weak defined
4682 symbols we found. The only way to do this is to search all the
4683 symbols. Since we only need the information for non functions in
4684 dynamic objects, that's the only time we actually put anything on
4685 the list WEAKS. We need this information so that if a regular
4686 object refers to a symbol defined weakly in a dynamic object, the
4687 real symbol in the dynamic object is also put in the dynamic
4688 symbols; we also must arrange for both symbols to point to the
4689 same memory location. We could handle the general case of symbol
4690 aliasing, but a general symbol alias can only be generated in
4691 assembler code, handling it correctly would be very time
4692 consuming, and other ELF linkers don't handle general aliasing
4696 struct elf_link_hash_entry
**hpp
;
4697 struct elf_link_hash_entry
**hppend
;
4698 struct elf_link_hash_entry
**sorted_sym_hash
;
4699 struct elf_link_hash_entry
*h
;
4702 /* Since we have to search the whole symbol list for each weak
4703 defined symbol, search time for N weak defined symbols will be
4704 O(N^2). Binary search will cut it down to O(NlogN). */
4705 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4706 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4707 if (sorted_sym_hash
== NULL
)
4709 sym_hash
= sorted_sym_hash
;
4710 hpp
= elf_sym_hashes (abfd
);
4711 hppend
= hpp
+ extsymcount
;
4713 for (; hpp
< hppend
; hpp
++)
4717 && h
->root
.type
== bfd_link_hash_defined
4718 && !bed
->is_function_type (h
->type
))
4726 qsort (sorted_sym_hash
, sym_count
,
4727 sizeof (struct elf_link_hash_entry
*),
4730 while (weaks
!= NULL
)
4732 struct elf_link_hash_entry
*hlook
;
4738 weaks
= hlook
->u
.weakdef
;
4739 hlook
->u
.weakdef
= NULL
;
4741 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4742 || hlook
->root
.type
== bfd_link_hash_defweak
4743 || hlook
->root
.type
== bfd_link_hash_common
4744 || hlook
->root
.type
== bfd_link_hash_indirect
);
4745 slook
= hlook
->root
.u
.def
.section
;
4746 vlook
= hlook
->root
.u
.def
.value
;
4752 bfd_signed_vma vdiff
;
4754 h
= sorted_sym_hash
[idx
];
4755 vdiff
= vlook
- h
->root
.u
.def
.value
;
4762 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4772 /* We didn't find a value/section match. */
4776 /* With multiple aliases, or when the weak symbol is already
4777 strongly defined, we have multiple matching symbols and
4778 the binary search above may land on any of them. Step
4779 one past the matching symbol(s). */
4782 h
= sorted_sym_hash
[idx
];
4783 if (h
->root
.u
.def
.section
!= slook
4784 || h
->root
.u
.def
.value
!= vlook
)
4788 /* Now look back over the aliases. Since we sorted by size
4789 as well as value and section, we'll choose the one with
4790 the largest size. */
4793 h
= sorted_sym_hash
[idx
];
4795 /* Stop if value or section doesn't match. */
4796 if (h
->root
.u
.def
.section
!= slook
4797 || h
->root
.u
.def
.value
!= vlook
)
4799 else if (h
!= hlook
)
4801 hlook
->u
.weakdef
= h
;
4803 /* If the weak definition is in the list of dynamic
4804 symbols, make sure the real definition is put
4806 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4808 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4811 free (sorted_sym_hash
);
4816 /* If the real definition is in the list of dynamic
4817 symbols, make sure the weak definition is put
4818 there as well. If we don't do this, then the
4819 dynamic loader might not merge the entries for the
4820 real definition and the weak definition. */
4821 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4823 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4824 goto err_free_sym_hash
;
4831 free (sorted_sym_hash
);
4834 if (bed
->check_directives
4835 && !(*bed
->check_directives
) (abfd
, info
))
4838 /* If this object is the same format as the output object, and it is
4839 not a shared library, then let the backend look through the
4842 This is required to build global offset table entries and to
4843 arrange for dynamic relocs. It is not required for the
4844 particular common case of linking non PIC code, even when linking
4845 against shared libraries, but unfortunately there is no way of
4846 knowing whether an object file has been compiled PIC or not.
4847 Looking through the relocs is not particularly time consuming.
4848 The problem is that we must either (1) keep the relocs in memory,
4849 which causes the linker to require additional runtime memory or
4850 (2) read the relocs twice from the input file, which wastes time.
4851 This would be a good case for using mmap.
4853 I have no idea how to handle linking PIC code into a file of a
4854 different format. It probably can't be done. */
4856 && is_elf_hash_table (htab
)
4857 && bed
->check_relocs
!= NULL
4858 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4859 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4863 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4865 Elf_Internal_Rela
*internal_relocs
;
4868 if ((o
->flags
& SEC_RELOC
) == 0
4869 || o
->reloc_count
== 0
4870 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4871 && (o
->flags
& SEC_DEBUGGING
) != 0)
4872 || bfd_is_abs_section (o
->output_section
))
4875 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4877 if (internal_relocs
== NULL
)
4880 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4882 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4883 free (internal_relocs
);
4890 /* If this is a non-traditional link, try to optimize the handling
4891 of the .stab/.stabstr sections. */
4893 && ! info
->traditional_format
4894 && is_elf_hash_table (htab
)
4895 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4899 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4900 if (stabstr
!= NULL
)
4902 bfd_size_type string_offset
= 0;
4905 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4906 if (CONST_STRNEQ (stab
->name
, ".stab")
4907 && (!stab
->name
[5] ||
4908 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4909 && (stab
->flags
& SEC_MERGE
) == 0
4910 && !bfd_is_abs_section (stab
->output_section
))
4912 struct bfd_elf_section_data
*secdata
;
4914 secdata
= elf_section_data (stab
);
4915 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4916 stabstr
, &secdata
->sec_info
,
4919 if (secdata
->sec_info
)
4920 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
4925 if (is_elf_hash_table (htab
) && add_needed
)
4927 /* Add this bfd to the loaded list. */
4928 struct elf_link_loaded_list
*n
;
4930 n
= (struct elf_link_loaded_list
*)
4931 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4935 n
->next
= htab
->loaded
;
4942 if (old_tab
!= NULL
)
4944 if (nondeflt_vers
!= NULL
)
4945 free (nondeflt_vers
);
4946 if (extversym
!= NULL
)
4949 if (isymbuf
!= NULL
)
4955 /* Return the linker hash table entry of a symbol that might be
4956 satisfied by an archive symbol. Return -1 on error. */
4958 struct elf_link_hash_entry
*
4959 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4960 struct bfd_link_info
*info
,
4963 struct elf_link_hash_entry
*h
;
4967 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
4971 /* If this is a default version (the name contains @@), look up the
4972 symbol again with only one `@' as well as without the version.
4973 The effect is that references to the symbol with and without the
4974 version will be matched by the default symbol in the archive. */
4976 p
= strchr (name
, ELF_VER_CHR
);
4977 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4980 /* First check with only one `@'. */
4981 len
= strlen (name
);
4982 copy
= (char *) bfd_alloc (abfd
, len
);
4984 return (struct elf_link_hash_entry
*) 0 - 1;
4986 first
= p
- name
+ 1;
4987 memcpy (copy
, name
, first
);
4988 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4990 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
4993 /* We also need to check references to the symbol without the
4995 copy
[first
- 1] = '\0';
4996 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4997 FALSE
, FALSE
, TRUE
);
5000 bfd_release (abfd
, copy
);
5004 /* Add symbols from an ELF archive file to the linker hash table. We
5005 don't use _bfd_generic_link_add_archive_symbols because of a
5006 problem which arises on UnixWare. The UnixWare libc.so is an
5007 archive which includes an entry libc.so.1 which defines a bunch of
5008 symbols. The libc.so archive also includes a number of other
5009 object files, which also define symbols, some of which are the same
5010 as those defined in libc.so.1. Correct linking requires that we
5011 consider each object file in turn, and include it if it defines any
5012 symbols we need. _bfd_generic_link_add_archive_symbols does not do
5013 this; it looks through the list of undefined symbols, and includes
5014 any object file which defines them. When this algorithm is used on
5015 UnixWare, it winds up pulling in libc.so.1 early and defining a
5016 bunch of symbols. This means that some of the other objects in the
5017 archive are not included in the link, which is incorrect since they
5018 precede libc.so.1 in the archive.
5020 Fortunately, ELF archive handling is simpler than that done by
5021 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5022 oddities. In ELF, if we find a symbol in the archive map, and the
5023 symbol is currently undefined, we know that we must pull in that
5026 Unfortunately, we do have to make multiple passes over the symbol
5027 table until nothing further is resolved. */
5030 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5033 bfd_boolean
*defined
= NULL
;
5034 bfd_boolean
*included
= NULL
;
5038 const struct elf_backend_data
*bed
;
5039 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5040 (bfd
*, struct bfd_link_info
*, const char *);
5042 if (! bfd_has_map (abfd
))
5044 /* An empty archive is a special case. */
5045 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5047 bfd_set_error (bfd_error_no_armap
);
5051 /* Keep track of all symbols we know to be already defined, and all
5052 files we know to be already included. This is to speed up the
5053 second and subsequent passes. */
5054 c
= bfd_ardata (abfd
)->symdef_count
;
5058 amt
*= sizeof (bfd_boolean
);
5059 defined
= (bfd_boolean
*) bfd_zmalloc (amt
);
5060 included
= (bfd_boolean
*) bfd_zmalloc (amt
);
5061 if (defined
== NULL
|| included
== NULL
)
5064 symdefs
= bfd_ardata (abfd
)->symdefs
;
5065 bed
= get_elf_backend_data (abfd
);
5066 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5079 symdefend
= symdef
+ c
;
5080 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5082 struct elf_link_hash_entry
*h
;
5084 struct bfd_link_hash_entry
*undefs_tail
;
5087 if (defined
[i
] || included
[i
])
5089 if (symdef
->file_offset
== last
)
5095 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5096 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5102 if (h
->root
.type
== bfd_link_hash_common
)
5104 /* We currently have a common symbol. The archive map contains
5105 a reference to this symbol, so we may want to include it. We
5106 only want to include it however, if this archive element
5107 contains a definition of the symbol, not just another common
5110 Unfortunately some archivers (including GNU ar) will put
5111 declarations of common symbols into their archive maps, as
5112 well as real definitions, so we cannot just go by the archive
5113 map alone. Instead we must read in the element's symbol
5114 table and check that to see what kind of symbol definition
5116 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5119 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5121 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5126 /* We need to include this archive member. */
5127 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5128 if (element
== NULL
)
5131 if (! bfd_check_format (element
, bfd_object
))
5134 /* Doublecheck that we have not included this object
5135 already--it should be impossible, but there may be
5136 something wrong with the archive. */
5137 if (element
->archive_pass
!= 0)
5139 bfd_set_error (bfd_error_bad_value
);
5142 element
->archive_pass
= 1;
5144 undefs_tail
= info
->hash
->undefs_tail
;
5146 if (!(*info
->callbacks
5147 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5149 if (!bfd_link_add_symbols (element
, info
))
5152 /* If there are any new undefined symbols, we need to make
5153 another pass through the archive in order to see whether
5154 they can be defined. FIXME: This isn't perfect, because
5155 common symbols wind up on undefs_tail and because an
5156 undefined symbol which is defined later on in this pass
5157 does not require another pass. This isn't a bug, but it
5158 does make the code less efficient than it could be. */
5159 if (undefs_tail
!= info
->hash
->undefs_tail
)
5162 /* Look backward to mark all symbols from this object file
5163 which we have already seen in this pass. */
5167 included
[mark
] = TRUE
;
5172 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5174 /* We mark subsequent symbols from this object file as we go
5175 on through the loop. */
5176 last
= symdef
->file_offset
;
5187 if (defined
!= NULL
)
5189 if (included
!= NULL
)
5194 /* Given an ELF BFD, add symbols to the global hash table as
5198 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5200 switch (bfd_get_format (abfd
))
5203 return elf_link_add_object_symbols (abfd
, info
);
5205 return elf_link_add_archive_symbols (abfd
, info
);
5207 bfd_set_error (bfd_error_wrong_format
);
5212 struct hash_codes_info
5214 unsigned long *hashcodes
;
5218 /* This function will be called though elf_link_hash_traverse to store
5219 all hash value of the exported symbols in an array. */
5222 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5224 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5230 /* Ignore indirect symbols. These are added by the versioning code. */
5231 if (h
->dynindx
== -1)
5234 name
= h
->root
.root
.string
;
5235 p
= strchr (name
, ELF_VER_CHR
);
5238 alc
= (char *) bfd_malloc (p
- name
+ 1);
5244 memcpy (alc
, name
, p
- name
);
5245 alc
[p
- name
] = '\0';
5249 /* Compute the hash value. */
5250 ha
= bfd_elf_hash (name
);
5252 /* Store the found hash value in the array given as the argument. */
5253 *(inf
->hashcodes
)++ = ha
;
5255 /* And store it in the struct so that we can put it in the hash table
5257 h
->u
.elf_hash_value
= ha
;
5265 struct collect_gnu_hash_codes
5268 const struct elf_backend_data
*bed
;
5269 unsigned long int nsyms
;
5270 unsigned long int maskbits
;
5271 unsigned long int *hashcodes
;
5272 unsigned long int *hashval
;
5273 unsigned long int *indx
;
5274 unsigned long int *counts
;
5277 long int min_dynindx
;
5278 unsigned long int bucketcount
;
5279 unsigned long int symindx
;
5280 long int local_indx
;
5281 long int shift1
, shift2
;
5282 unsigned long int mask
;
5286 /* This function will be called though elf_link_hash_traverse to store
5287 all hash value of the exported symbols in an array. */
5290 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5292 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5298 /* Ignore indirect symbols. These are added by the versioning code. */
5299 if (h
->dynindx
== -1)
5302 /* Ignore also local symbols and undefined symbols. */
5303 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5306 name
= h
->root
.root
.string
;
5307 p
= strchr (name
, ELF_VER_CHR
);
5310 alc
= (char *) bfd_malloc (p
- name
+ 1);
5316 memcpy (alc
, name
, p
- name
);
5317 alc
[p
- name
] = '\0';
5321 /* Compute the hash value. */
5322 ha
= bfd_elf_gnu_hash (name
);
5324 /* Store the found hash value in the array for compute_bucket_count,
5325 and also for .dynsym reordering purposes. */
5326 s
->hashcodes
[s
->nsyms
] = ha
;
5327 s
->hashval
[h
->dynindx
] = ha
;
5329 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5330 s
->min_dynindx
= h
->dynindx
;
5338 /* This function will be called though elf_link_hash_traverse to do
5339 final dynaminc symbol renumbering. */
5342 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5344 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5345 unsigned long int bucket
;
5346 unsigned long int val
;
5348 /* Ignore indirect symbols. */
5349 if (h
->dynindx
== -1)
5352 /* Ignore also local symbols and undefined symbols. */
5353 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5355 if (h
->dynindx
>= s
->min_dynindx
)
5356 h
->dynindx
= s
->local_indx
++;
5360 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5361 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5362 & ((s
->maskbits
>> s
->shift1
) - 1);
5363 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5365 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5366 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5367 if (s
->counts
[bucket
] == 1)
5368 /* Last element terminates the chain. */
5370 bfd_put_32 (s
->output_bfd
, val
,
5371 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5372 --s
->counts
[bucket
];
5373 h
->dynindx
= s
->indx
[bucket
]++;
5377 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5380 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5382 return !(h
->forced_local
5383 || h
->root
.type
== bfd_link_hash_undefined
5384 || h
->root
.type
== bfd_link_hash_undefweak
5385 || ((h
->root
.type
== bfd_link_hash_defined
5386 || h
->root
.type
== bfd_link_hash_defweak
)
5387 && h
->root
.u
.def
.section
->output_section
== NULL
));
5390 /* Array used to determine the number of hash table buckets to use
5391 based on the number of symbols there are. If there are fewer than
5392 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5393 fewer than 37 we use 17 buckets, and so forth. We never use more
5394 than 32771 buckets. */
5396 static const size_t elf_buckets
[] =
5398 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5402 /* Compute bucket count for hashing table. We do not use a static set
5403 of possible tables sizes anymore. Instead we determine for all
5404 possible reasonable sizes of the table the outcome (i.e., the
5405 number of collisions etc) and choose the best solution. The
5406 weighting functions are not too simple to allow the table to grow
5407 without bounds. Instead one of the weighting factors is the size.
5408 Therefore the result is always a good payoff between few collisions
5409 (= short chain lengths) and table size. */
5411 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5412 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5413 unsigned long int nsyms
,
5416 size_t best_size
= 0;
5417 unsigned long int i
;
5419 /* We have a problem here. The following code to optimize the table
5420 size requires an integer type with more the 32 bits. If
5421 BFD_HOST_U_64_BIT is set we know about such a type. */
5422 #ifdef BFD_HOST_U_64_BIT
5427 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5428 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5429 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5430 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5431 unsigned long int *counts
;
5433 unsigned int no_improvement_count
= 0;
5435 /* Possible optimization parameters: if we have NSYMS symbols we say
5436 that the hashing table must at least have NSYMS/4 and at most
5438 minsize
= nsyms
/ 4;
5441 best_size
= maxsize
= nsyms
* 2;
5446 if ((best_size
& 31) == 0)
5450 /* Create array where we count the collisions in. We must use bfd_malloc
5451 since the size could be large. */
5453 amt
*= sizeof (unsigned long int);
5454 counts
= (unsigned long int *) bfd_malloc (amt
);
5458 /* Compute the "optimal" size for the hash table. The criteria is a
5459 minimal chain length. The minor criteria is (of course) the size
5461 for (i
= minsize
; i
< maxsize
; ++i
)
5463 /* Walk through the array of hashcodes and count the collisions. */
5464 BFD_HOST_U_64_BIT max
;
5465 unsigned long int j
;
5466 unsigned long int fact
;
5468 if (gnu_hash
&& (i
& 31) == 0)
5471 memset (counts
, '\0', i
* sizeof (unsigned long int));
5473 /* Determine how often each hash bucket is used. */
5474 for (j
= 0; j
< nsyms
; ++j
)
5475 ++counts
[hashcodes
[j
] % i
];
5477 /* For the weight function we need some information about the
5478 pagesize on the target. This is information need not be 100%
5479 accurate. Since this information is not available (so far) we
5480 define it here to a reasonable default value. If it is crucial
5481 to have a better value some day simply define this value. */
5482 # ifndef BFD_TARGET_PAGESIZE
5483 # define BFD_TARGET_PAGESIZE (4096)
5486 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5488 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5491 /* Variant 1: optimize for short chains. We add the squares
5492 of all the chain lengths (which favors many small chain
5493 over a few long chains). */
5494 for (j
= 0; j
< i
; ++j
)
5495 max
+= counts
[j
] * counts
[j
];
5497 /* This adds penalties for the overall size of the table. */
5498 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5501 /* Variant 2: Optimize a lot more for small table. Here we
5502 also add squares of the size but we also add penalties for
5503 empty slots (the +1 term). */
5504 for (j
= 0; j
< i
; ++j
)
5505 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5507 /* The overall size of the table is considered, but not as
5508 strong as in variant 1, where it is squared. */
5509 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5513 /* Compare with current best results. */
5514 if (max
< best_chlen
)
5518 no_improvement_count
= 0;
5520 /* PR 11843: Avoid futile long searches for the best bucket size
5521 when there are a large number of symbols. */
5522 else if (++no_improvement_count
== 100)
5529 #endif /* defined (BFD_HOST_U_64_BIT) */
5531 /* This is the fallback solution if no 64bit type is available or if we
5532 are not supposed to spend much time on optimizations. We select the
5533 bucket count using a fixed set of numbers. */
5534 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5536 best_size
= elf_buckets
[i
];
5537 if (nsyms
< elf_buckets
[i
+ 1])
5540 if (gnu_hash
&& best_size
< 2)
5547 /* Size any SHT_GROUP section for ld -r. */
5550 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5554 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
5555 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5556 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5561 /* Set up the sizes and contents of the ELF dynamic sections. This is
5562 called by the ELF linker emulation before_allocation routine. We
5563 must set the sizes of the sections before the linker sets the
5564 addresses of the various sections. */
5567 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5570 const char *filter_shlib
,
5572 const char *depaudit
,
5573 const char * const *auxiliary_filters
,
5574 struct bfd_link_info
*info
,
5575 asection
**sinterpptr
)
5577 bfd_size_type soname_indx
;
5579 const struct elf_backend_data
*bed
;
5580 struct elf_info_failed asvinfo
;
5584 soname_indx
= (bfd_size_type
) -1;
5586 if (!is_elf_hash_table (info
->hash
))
5589 bed
= get_elf_backend_data (output_bfd
);
5590 if (info
->execstack
)
5591 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5592 else if (info
->noexecstack
)
5593 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5597 asection
*notesec
= NULL
;
5600 for (inputobj
= info
->input_bfds
;
5602 inputobj
= inputobj
->link_next
)
5607 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5609 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5612 if (s
->flags
& SEC_CODE
)
5616 else if (bed
->default_execstack
)
5621 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5622 if (exec
&& info
->relocatable
5623 && notesec
->output_section
!= bfd_abs_section_ptr
)
5624 notesec
->output_section
->flags
|= SEC_CODE
;
5628 /* Any syms created from now on start with -1 in
5629 got.refcount/offset and plt.refcount/offset. */
5630 elf_hash_table (info
)->init_got_refcount
5631 = elf_hash_table (info
)->init_got_offset
;
5632 elf_hash_table (info
)->init_plt_refcount
5633 = elf_hash_table (info
)->init_plt_offset
;
5635 if (info
->relocatable
5636 && !_bfd_elf_size_group_sections (info
))
5639 /* The backend may have to create some sections regardless of whether
5640 we're dynamic or not. */
5641 if (bed
->elf_backend_always_size_sections
5642 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5645 dynobj
= elf_hash_table (info
)->dynobj
;
5647 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5649 struct elf_info_failed eif
;
5650 struct elf_link_hash_entry
*h
;
5652 struct bfd_elf_version_tree
*t
;
5653 struct bfd_elf_version_expr
*d
;
5655 bfd_boolean all_defined
;
5657 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5658 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5662 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5664 if (soname_indx
== (bfd_size_type
) -1
5665 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5671 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5673 info
->flags
|= DF_SYMBOLIC
;
5680 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5682 if (indx
== (bfd_size_type
) -1
5683 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5686 if (info
->new_dtags
)
5688 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5689 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5694 if (filter_shlib
!= NULL
)
5698 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5699 filter_shlib
, TRUE
);
5700 if (indx
== (bfd_size_type
) -1
5701 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5705 if (auxiliary_filters
!= NULL
)
5707 const char * const *p
;
5709 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5713 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5715 if (indx
== (bfd_size_type
) -1
5716 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5725 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5727 if (indx
== (bfd_size_type
) -1
5728 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5732 if (depaudit
!= NULL
)
5736 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5738 if (indx
== (bfd_size_type
) -1
5739 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
5746 /* If we are supposed to export all symbols into the dynamic symbol
5747 table (this is not the normal case), then do so. */
5748 if (info
->export_dynamic
5749 || (info
->executable
&& info
->dynamic
))
5751 elf_link_hash_traverse (elf_hash_table (info
),
5752 _bfd_elf_export_symbol
,
5758 /* Make all global versions with definition. */
5759 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5760 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5761 if (!d
->symver
&& d
->literal
)
5763 const char *verstr
, *name
;
5764 size_t namelen
, verlen
, newlen
;
5765 char *newname
, *p
, leading_char
;
5766 struct elf_link_hash_entry
*newh
;
5768 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
5770 namelen
= strlen (name
) + (leading_char
!= '\0');
5772 verlen
= strlen (verstr
);
5773 newlen
= namelen
+ verlen
+ 3;
5775 newname
= (char *) bfd_malloc (newlen
);
5776 if (newname
== NULL
)
5778 newname
[0] = leading_char
;
5779 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
5781 /* Check the hidden versioned definition. */
5782 p
= newname
+ namelen
;
5784 memcpy (p
, verstr
, verlen
+ 1);
5785 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5786 newname
, FALSE
, FALSE
,
5789 || (newh
->root
.type
!= bfd_link_hash_defined
5790 && newh
->root
.type
!= bfd_link_hash_defweak
))
5792 /* Check the default versioned definition. */
5794 memcpy (p
, verstr
, verlen
+ 1);
5795 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5796 newname
, FALSE
, FALSE
,
5801 /* Mark this version if there is a definition and it is
5802 not defined in a shared object. */
5804 && !newh
->def_dynamic
5805 && (newh
->root
.type
== bfd_link_hash_defined
5806 || newh
->root
.type
== bfd_link_hash_defweak
))
5810 /* Attach all the symbols to their version information. */
5811 asvinfo
.info
= info
;
5812 asvinfo
.failed
= FALSE
;
5814 elf_link_hash_traverse (elf_hash_table (info
),
5815 _bfd_elf_link_assign_sym_version
,
5820 if (!info
->allow_undefined_version
)
5822 /* Check if all global versions have a definition. */
5824 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5825 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5826 if (d
->literal
&& !d
->symver
&& !d
->script
)
5828 (*_bfd_error_handler
)
5829 (_("%s: undefined version: %s"),
5830 d
->pattern
, t
->name
);
5831 all_defined
= FALSE
;
5836 bfd_set_error (bfd_error_bad_value
);
5841 /* Find all symbols which were defined in a dynamic object and make
5842 the backend pick a reasonable value for them. */
5843 elf_link_hash_traverse (elf_hash_table (info
),
5844 _bfd_elf_adjust_dynamic_symbol
,
5849 /* Add some entries to the .dynamic section. We fill in some of the
5850 values later, in bfd_elf_final_link, but we must add the entries
5851 now so that we know the final size of the .dynamic section. */
5853 /* If there are initialization and/or finalization functions to
5854 call then add the corresponding DT_INIT/DT_FINI entries. */
5855 h
= (info
->init_function
5856 ? elf_link_hash_lookup (elf_hash_table (info
),
5857 info
->init_function
, FALSE
,
5864 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5867 h
= (info
->fini_function
5868 ? elf_link_hash_lookup (elf_hash_table (info
),
5869 info
->fini_function
, FALSE
,
5876 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5880 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5881 if (s
!= NULL
&& s
->linker_has_input
)
5883 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5884 if (! info
->executable
)
5889 for (sub
= info
->input_bfds
; sub
!= NULL
;
5890 sub
= sub
->link_next
)
5891 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5892 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5893 if (elf_section_data (o
)->this_hdr
.sh_type
5894 == SHT_PREINIT_ARRAY
)
5896 (*_bfd_error_handler
)
5897 (_("%B: .preinit_array section is not allowed in DSO"),
5902 bfd_set_error (bfd_error_nonrepresentable_section
);
5906 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5907 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5910 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5911 if (s
!= NULL
&& s
->linker_has_input
)
5913 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5914 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5917 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5918 if (s
!= NULL
&& s
->linker_has_input
)
5920 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5921 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5925 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
5926 /* If .dynstr is excluded from the link, we don't want any of
5927 these tags. Strictly, we should be checking each section
5928 individually; This quick check covers for the case where
5929 someone does a /DISCARD/ : { *(*) }. */
5930 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5932 bfd_size_type strsize
;
5934 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5935 if ((info
->emit_hash
5936 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5937 || (info
->emit_gnu_hash
5938 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5939 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5940 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5941 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5942 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5943 bed
->s
->sizeof_sym
))
5948 /* The backend must work out the sizes of all the other dynamic
5951 && bed
->elf_backend_size_dynamic_sections
!= NULL
5952 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5955 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5958 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5960 unsigned long section_sym_count
;
5961 struct bfd_elf_version_tree
*verdefs
;
5964 /* Set up the version definition section. */
5965 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
5966 BFD_ASSERT (s
!= NULL
);
5968 /* We may have created additional version definitions if we are
5969 just linking a regular application. */
5970 verdefs
= info
->version_info
;
5972 /* Skip anonymous version tag. */
5973 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5974 verdefs
= verdefs
->next
;
5976 if (verdefs
== NULL
&& !info
->create_default_symver
)
5977 s
->flags
|= SEC_EXCLUDE
;
5982 struct bfd_elf_version_tree
*t
;
5984 Elf_Internal_Verdef def
;
5985 Elf_Internal_Verdaux defaux
;
5986 struct bfd_link_hash_entry
*bh
;
5987 struct elf_link_hash_entry
*h
;
5993 /* Make space for the base version. */
5994 size
+= sizeof (Elf_External_Verdef
);
5995 size
+= sizeof (Elf_External_Verdaux
);
5998 /* Make space for the default version. */
5999 if (info
->create_default_symver
)
6001 size
+= sizeof (Elf_External_Verdef
);
6005 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6007 struct bfd_elf_version_deps
*n
;
6009 /* Don't emit base version twice. */
6013 size
+= sizeof (Elf_External_Verdef
);
6014 size
+= sizeof (Elf_External_Verdaux
);
6017 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6018 size
+= sizeof (Elf_External_Verdaux
);
6022 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6023 if (s
->contents
== NULL
&& s
->size
!= 0)
6026 /* Fill in the version definition section. */
6030 def
.vd_version
= VER_DEF_CURRENT
;
6031 def
.vd_flags
= VER_FLG_BASE
;
6034 if (info
->create_default_symver
)
6036 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6037 def
.vd_next
= sizeof (Elf_External_Verdef
);
6041 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6042 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6043 + sizeof (Elf_External_Verdaux
));
6046 if (soname_indx
!= (bfd_size_type
) -1)
6048 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6050 def
.vd_hash
= bfd_elf_hash (soname
);
6051 defaux
.vda_name
= soname_indx
;
6058 name
= lbasename (output_bfd
->filename
);
6059 def
.vd_hash
= bfd_elf_hash (name
);
6060 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6062 if (indx
== (bfd_size_type
) -1)
6064 defaux
.vda_name
= indx
;
6066 defaux
.vda_next
= 0;
6068 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6069 (Elf_External_Verdef
*) p
);
6070 p
+= sizeof (Elf_External_Verdef
);
6071 if (info
->create_default_symver
)
6073 /* Add a symbol representing this version. */
6075 if (! (_bfd_generic_link_add_one_symbol
6076 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6078 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6080 h
= (struct elf_link_hash_entry
*) bh
;
6083 h
->type
= STT_OBJECT
;
6084 h
->verinfo
.vertree
= NULL
;
6086 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6089 /* Create a duplicate of the base version with the same
6090 aux block, but different flags. */
6093 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6095 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6096 + sizeof (Elf_External_Verdaux
));
6099 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6100 (Elf_External_Verdef
*) p
);
6101 p
+= sizeof (Elf_External_Verdef
);
6103 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6104 (Elf_External_Verdaux
*) p
);
6105 p
+= sizeof (Elf_External_Verdaux
);
6107 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6110 struct bfd_elf_version_deps
*n
;
6112 /* Don't emit the base version twice. */
6117 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6120 /* Add a symbol representing this version. */
6122 if (! (_bfd_generic_link_add_one_symbol
6123 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6125 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6127 h
= (struct elf_link_hash_entry
*) bh
;
6130 h
->type
= STT_OBJECT
;
6131 h
->verinfo
.vertree
= t
;
6133 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6136 def
.vd_version
= VER_DEF_CURRENT
;
6138 if (t
->globals
.list
== NULL
6139 && t
->locals
.list
== NULL
6141 def
.vd_flags
|= VER_FLG_WEAK
;
6142 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6143 def
.vd_cnt
= cdeps
+ 1;
6144 def
.vd_hash
= bfd_elf_hash (t
->name
);
6145 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6148 /* If a basever node is next, it *must* be the last node in
6149 the chain, otherwise Verdef construction breaks. */
6150 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6151 BFD_ASSERT (t
->next
->next
== NULL
);
6153 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6154 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6155 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6157 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6158 (Elf_External_Verdef
*) p
);
6159 p
+= sizeof (Elf_External_Verdef
);
6161 defaux
.vda_name
= h
->dynstr_index
;
6162 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6164 defaux
.vda_next
= 0;
6165 if (t
->deps
!= NULL
)
6166 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6167 t
->name_indx
= defaux
.vda_name
;
6169 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6170 (Elf_External_Verdaux
*) p
);
6171 p
+= sizeof (Elf_External_Verdaux
);
6173 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6175 if (n
->version_needed
== NULL
)
6177 /* This can happen if there was an error in the
6179 defaux
.vda_name
= 0;
6183 defaux
.vda_name
= n
->version_needed
->name_indx
;
6184 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6187 if (n
->next
== NULL
)
6188 defaux
.vda_next
= 0;
6190 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6192 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6193 (Elf_External_Verdaux
*) p
);
6194 p
+= sizeof (Elf_External_Verdaux
);
6198 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6199 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6202 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6205 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6207 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6210 else if (info
->flags
& DF_BIND_NOW
)
6212 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6218 if (info
->executable
)
6219 info
->flags_1
&= ~ (DF_1_INITFIRST
6222 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6226 /* Work out the size of the version reference section. */
6228 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6229 BFD_ASSERT (s
!= NULL
);
6231 struct elf_find_verdep_info sinfo
;
6234 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6235 if (sinfo
.vers
== 0)
6237 sinfo
.failed
= FALSE
;
6239 elf_link_hash_traverse (elf_hash_table (info
),
6240 _bfd_elf_link_find_version_dependencies
,
6245 if (elf_tdata (output_bfd
)->verref
== NULL
)
6246 s
->flags
|= SEC_EXCLUDE
;
6249 Elf_Internal_Verneed
*t
;
6254 /* Build the version dependency section. */
6257 for (t
= elf_tdata (output_bfd
)->verref
;
6261 Elf_Internal_Vernaux
*a
;
6263 size
+= sizeof (Elf_External_Verneed
);
6265 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6266 size
+= sizeof (Elf_External_Vernaux
);
6270 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6271 if (s
->contents
== NULL
)
6275 for (t
= elf_tdata (output_bfd
)->verref
;
6280 Elf_Internal_Vernaux
*a
;
6284 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6287 t
->vn_version
= VER_NEED_CURRENT
;
6289 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6290 elf_dt_name (t
->vn_bfd
) != NULL
6291 ? elf_dt_name (t
->vn_bfd
)
6292 : lbasename (t
->vn_bfd
->filename
),
6294 if (indx
== (bfd_size_type
) -1)
6297 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6298 if (t
->vn_nextref
== NULL
)
6301 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6302 + caux
* sizeof (Elf_External_Vernaux
));
6304 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6305 (Elf_External_Verneed
*) p
);
6306 p
+= sizeof (Elf_External_Verneed
);
6308 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6310 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6311 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6312 a
->vna_nodename
, FALSE
);
6313 if (indx
== (bfd_size_type
) -1)
6316 if (a
->vna_nextptr
== NULL
)
6319 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6321 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6322 (Elf_External_Vernaux
*) p
);
6323 p
+= sizeof (Elf_External_Vernaux
);
6327 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6328 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6331 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6335 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6336 && elf_tdata (output_bfd
)->cverdefs
== 0)
6337 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6338 §ion_sym_count
) == 0)
6340 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6341 s
->flags
|= SEC_EXCLUDE
;
6347 /* Find the first non-excluded output section. We'll use its
6348 section symbol for some emitted relocs. */
6350 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6354 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6355 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6356 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6358 elf_hash_table (info
)->text_index_section
= s
;
6363 /* Find two non-excluded output sections, one for code, one for data.
6364 We'll use their section symbols for some emitted relocs. */
6366 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6370 /* Data first, since setting text_index_section changes
6371 _bfd_elf_link_omit_section_dynsym. */
6372 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6373 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6374 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6376 elf_hash_table (info
)->data_index_section
= s
;
6380 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6381 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6382 == (SEC_ALLOC
| SEC_READONLY
))
6383 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6385 elf_hash_table (info
)->text_index_section
= s
;
6389 if (elf_hash_table (info
)->text_index_section
== NULL
)
6390 elf_hash_table (info
)->text_index_section
6391 = elf_hash_table (info
)->data_index_section
;
6395 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6397 const struct elf_backend_data
*bed
;
6399 if (!is_elf_hash_table (info
->hash
))
6402 bed
= get_elf_backend_data (output_bfd
);
6403 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6405 if (elf_hash_table (info
)->dynamic_sections_created
)
6409 bfd_size_type dynsymcount
;
6410 unsigned long section_sym_count
;
6411 unsigned int dtagcount
;
6413 dynobj
= elf_hash_table (info
)->dynobj
;
6415 /* Assign dynsym indicies. In a shared library we generate a
6416 section symbol for each output section, which come first.
6417 Next come all of the back-end allocated local dynamic syms,
6418 followed by the rest of the global symbols. */
6420 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6421 §ion_sym_count
);
6423 /* Work out the size of the symbol version section. */
6424 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6425 BFD_ASSERT (s
!= NULL
);
6426 if (dynsymcount
!= 0
6427 && (s
->flags
& SEC_EXCLUDE
) == 0)
6429 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6430 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6431 if (s
->contents
== NULL
)
6434 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6438 /* Set the size of the .dynsym and .hash sections. We counted
6439 the number of dynamic symbols in elf_link_add_object_symbols.
6440 We will build the contents of .dynsym and .hash when we build
6441 the final symbol table, because until then we do not know the
6442 correct value to give the symbols. We built the .dynstr
6443 section as we went along in elf_link_add_object_symbols. */
6444 s
= bfd_get_linker_section (dynobj
, ".dynsym");
6445 BFD_ASSERT (s
!= NULL
);
6446 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6448 if (dynsymcount
!= 0)
6450 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6451 if (s
->contents
== NULL
)
6454 /* The first entry in .dynsym is a dummy symbol.
6455 Clear all the section syms, in case we don't output them all. */
6456 ++section_sym_count
;
6457 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6460 elf_hash_table (info
)->bucketcount
= 0;
6462 /* Compute the size of the hashing table. As a side effect this
6463 computes the hash values for all the names we export. */
6464 if (info
->emit_hash
)
6466 unsigned long int *hashcodes
;
6467 struct hash_codes_info hashinf
;
6469 unsigned long int nsyms
;
6471 size_t hash_entry_size
;
6473 /* Compute the hash values for all exported symbols. At the same
6474 time store the values in an array so that we could use them for
6476 amt
= dynsymcount
* sizeof (unsigned long int);
6477 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6478 if (hashcodes
== NULL
)
6480 hashinf
.hashcodes
= hashcodes
;
6481 hashinf
.error
= FALSE
;
6483 /* Put all hash values in HASHCODES. */
6484 elf_link_hash_traverse (elf_hash_table (info
),
6485 elf_collect_hash_codes
, &hashinf
);
6492 nsyms
= hashinf
.hashcodes
- hashcodes
;
6494 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6497 if (bucketcount
== 0)
6500 elf_hash_table (info
)->bucketcount
= bucketcount
;
6502 s
= bfd_get_linker_section (dynobj
, ".hash");
6503 BFD_ASSERT (s
!= NULL
);
6504 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6505 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6506 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6507 if (s
->contents
== NULL
)
6510 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6511 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6512 s
->contents
+ hash_entry_size
);
6515 if (info
->emit_gnu_hash
)
6518 unsigned char *contents
;
6519 struct collect_gnu_hash_codes cinfo
;
6523 memset (&cinfo
, 0, sizeof (cinfo
));
6525 /* Compute the hash values for all exported symbols. At the same
6526 time store the values in an array so that we could use them for
6528 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6529 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6530 if (cinfo
.hashcodes
== NULL
)
6533 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6534 cinfo
.min_dynindx
= -1;
6535 cinfo
.output_bfd
= output_bfd
;
6538 /* Put all hash values in HASHCODES. */
6539 elf_link_hash_traverse (elf_hash_table (info
),
6540 elf_collect_gnu_hash_codes
, &cinfo
);
6543 free (cinfo
.hashcodes
);
6548 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6550 if (bucketcount
== 0)
6552 free (cinfo
.hashcodes
);
6556 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6557 BFD_ASSERT (s
!= NULL
);
6559 if (cinfo
.nsyms
== 0)
6561 /* Empty .gnu.hash section is special. */
6562 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6563 free (cinfo
.hashcodes
);
6564 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6565 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6566 if (contents
== NULL
)
6568 s
->contents
= contents
;
6569 /* 1 empty bucket. */
6570 bfd_put_32 (output_bfd
, 1, contents
);
6571 /* SYMIDX above the special symbol 0. */
6572 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6573 /* Just one word for bitmask. */
6574 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6575 /* Only hash fn bloom filter. */
6576 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6577 /* No hashes are valid - empty bitmask. */
6578 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6579 /* No hashes in the only bucket. */
6580 bfd_put_32 (output_bfd
, 0,
6581 contents
+ 16 + bed
->s
->arch_size
/ 8);
6585 unsigned long int maskwords
, maskbitslog2
, x
;
6586 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6590 while ((x
>>= 1) != 0)
6592 if (maskbitslog2
< 3)
6594 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6595 maskbitslog2
= maskbitslog2
+ 3;
6597 maskbitslog2
= maskbitslog2
+ 2;
6598 if (bed
->s
->arch_size
== 64)
6600 if (maskbitslog2
== 5)
6606 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6607 cinfo
.shift2
= maskbitslog2
;
6608 cinfo
.maskbits
= 1 << maskbitslog2
;
6609 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6610 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6611 amt
+= maskwords
* sizeof (bfd_vma
);
6612 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6613 if (cinfo
.bitmask
== NULL
)
6615 free (cinfo
.hashcodes
);
6619 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6620 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6621 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6622 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6624 /* Determine how often each hash bucket is used. */
6625 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6626 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6627 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6629 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6630 if (cinfo
.counts
[i
] != 0)
6632 cinfo
.indx
[i
] = cnt
;
6633 cnt
+= cinfo
.counts
[i
];
6635 BFD_ASSERT (cnt
== dynsymcount
);
6636 cinfo
.bucketcount
= bucketcount
;
6637 cinfo
.local_indx
= cinfo
.min_dynindx
;
6639 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6640 s
->size
+= cinfo
.maskbits
/ 8;
6641 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6642 if (contents
== NULL
)
6644 free (cinfo
.bitmask
);
6645 free (cinfo
.hashcodes
);
6649 s
->contents
= contents
;
6650 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6651 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6652 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6653 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6654 contents
+= 16 + cinfo
.maskbits
/ 8;
6656 for (i
= 0; i
< bucketcount
; ++i
)
6658 if (cinfo
.counts
[i
] == 0)
6659 bfd_put_32 (output_bfd
, 0, contents
);
6661 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6665 cinfo
.contents
= contents
;
6667 /* Renumber dynamic symbols, populate .gnu.hash section. */
6668 elf_link_hash_traverse (elf_hash_table (info
),
6669 elf_renumber_gnu_hash_syms
, &cinfo
);
6671 contents
= s
->contents
+ 16;
6672 for (i
= 0; i
< maskwords
; ++i
)
6674 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6676 contents
+= bed
->s
->arch_size
/ 8;
6679 free (cinfo
.bitmask
);
6680 free (cinfo
.hashcodes
);
6684 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6685 BFD_ASSERT (s
!= NULL
);
6687 elf_finalize_dynstr (output_bfd
, info
);
6689 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6691 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6692 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6699 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6702 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6705 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6706 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6709 /* Finish SHF_MERGE section merging. */
6712 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6717 if (!is_elf_hash_table (info
->hash
))
6720 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6721 if ((ibfd
->flags
& DYNAMIC
) == 0)
6722 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6723 if ((sec
->flags
& SEC_MERGE
) != 0
6724 && !bfd_is_abs_section (sec
->output_section
))
6726 struct bfd_elf_section_data
*secdata
;
6728 secdata
= elf_section_data (sec
);
6729 if (! _bfd_add_merge_section (abfd
,
6730 &elf_hash_table (info
)->merge_info
,
6731 sec
, &secdata
->sec_info
))
6733 else if (secdata
->sec_info
)
6734 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6737 if (elf_hash_table (info
)->merge_info
!= NULL
)
6738 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6739 merge_sections_remove_hook
);
6743 /* Create an entry in an ELF linker hash table. */
6745 struct bfd_hash_entry
*
6746 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6747 struct bfd_hash_table
*table
,
6750 /* Allocate the structure if it has not already been allocated by a
6754 entry
= (struct bfd_hash_entry
*)
6755 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6760 /* Call the allocation method of the superclass. */
6761 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6764 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6765 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6767 /* Set local fields. */
6770 ret
->got
= htab
->init_got_refcount
;
6771 ret
->plt
= htab
->init_plt_refcount
;
6772 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6773 - offsetof (struct elf_link_hash_entry
, size
)));
6774 /* Assume that we have been called by a non-ELF symbol reader.
6775 This flag is then reset by the code which reads an ELF input
6776 file. This ensures that a symbol created by a non-ELF symbol
6777 reader will have the flag set correctly. */
6784 /* Copy data from an indirect symbol to its direct symbol, hiding the
6785 old indirect symbol. Also used for copying flags to a weakdef. */
6788 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6789 struct elf_link_hash_entry
*dir
,
6790 struct elf_link_hash_entry
*ind
)
6792 struct elf_link_hash_table
*htab
;
6794 /* Copy down any references that we may have already seen to the
6795 symbol which just became indirect. */
6797 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6798 dir
->ref_regular
|= ind
->ref_regular
;
6799 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6800 dir
->non_got_ref
|= ind
->non_got_ref
;
6801 dir
->needs_plt
|= ind
->needs_plt
;
6802 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6804 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6807 /* Copy over the global and procedure linkage table refcount entries.
6808 These may have been already set up by a check_relocs routine. */
6809 htab
= elf_hash_table (info
);
6810 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6812 if (dir
->got
.refcount
< 0)
6813 dir
->got
.refcount
= 0;
6814 dir
->got
.refcount
+= ind
->got
.refcount
;
6815 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6818 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6820 if (dir
->plt
.refcount
< 0)
6821 dir
->plt
.refcount
= 0;
6822 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6823 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6826 if (ind
->dynindx
!= -1)
6828 if (dir
->dynindx
!= -1)
6829 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6830 dir
->dynindx
= ind
->dynindx
;
6831 dir
->dynstr_index
= ind
->dynstr_index
;
6833 ind
->dynstr_index
= 0;
6838 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6839 struct elf_link_hash_entry
*h
,
6840 bfd_boolean force_local
)
6842 /* STT_GNU_IFUNC symbol must go through PLT. */
6843 if (h
->type
!= STT_GNU_IFUNC
)
6845 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6850 h
->forced_local
= 1;
6851 if (h
->dynindx
!= -1)
6854 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6860 /* Initialize an ELF linker hash table. */
6863 _bfd_elf_link_hash_table_init
6864 (struct elf_link_hash_table
*table
,
6866 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6867 struct bfd_hash_table
*,
6869 unsigned int entsize
,
6870 enum elf_target_id target_id
)
6873 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6875 memset (table
, 0, sizeof * table
);
6876 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6877 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6878 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6879 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6880 /* The first dynamic symbol is a dummy. */
6881 table
->dynsymcount
= 1;
6883 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6885 table
->root
.type
= bfd_link_elf_hash_table
;
6886 table
->hash_table_id
= target_id
;
6891 /* Create an ELF linker hash table. */
6893 struct bfd_link_hash_table
*
6894 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6896 struct elf_link_hash_table
*ret
;
6897 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6899 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
6903 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6904 sizeof (struct elf_link_hash_entry
),
6914 /* This is a hook for the ELF emulation code in the generic linker to
6915 tell the backend linker what file name to use for the DT_NEEDED
6916 entry for a dynamic object. */
6919 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6921 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6922 && bfd_get_format (abfd
) == bfd_object
)
6923 elf_dt_name (abfd
) = name
;
6927 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6930 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6931 && bfd_get_format (abfd
) == bfd_object
)
6932 lib_class
= elf_dyn_lib_class (abfd
);
6939 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6941 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6942 && bfd_get_format (abfd
) == bfd_object
)
6943 elf_dyn_lib_class (abfd
) = lib_class
;
6946 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6947 the linker ELF emulation code. */
6949 struct bfd_link_needed_list
*
6950 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6951 struct bfd_link_info
*info
)
6953 if (! is_elf_hash_table (info
->hash
))
6955 return elf_hash_table (info
)->needed
;
6958 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6959 hook for the linker ELF emulation code. */
6961 struct bfd_link_needed_list
*
6962 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6963 struct bfd_link_info
*info
)
6965 if (! is_elf_hash_table (info
->hash
))
6967 return elf_hash_table (info
)->runpath
;
6970 /* Get the name actually used for a dynamic object for a link. This
6971 is the SONAME entry if there is one. Otherwise, it is the string
6972 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6975 bfd_elf_get_dt_soname (bfd
*abfd
)
6977 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6978 && bfd_get_format (abfd
) == bfd_object
)
6979 return elf_dt_name (abfd
);
6983 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6984 the ELF linker emulation code. */
6987 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6988 struct bfd_link_needed_list
**pneeded
)
6991 bfd_byte
*dynbuf
= NULL
;
6992 unsigned int elfsec
;
6993 unsigned long shlink
;
6994 bfd_byte
*extdyn
, *extdynend
;
6996 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7000 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7001 || bfd_get_format (abfd
) != bfd_object
)
7004 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7005 if (s
== NULL
|| s
->size
== 0)
7008 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7011 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7012 if (elfsec
== SHN_BAD
)
7015 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7017 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7018 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7021 extdynend
= extdyn
+ s
->size
;
7022 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7024 Elf_Internal_Dyn dyn
;
7026 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7028 if (dyn
.d_tag
== DT_NULL
)
7031 if (dyn
.d_tag
== DT_NEEDED
)
7034 struct bfd_link_needed_list
*l
;
7035 unsigned int tagv
= dyn
.d_un
.d_val
;
7038 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7043 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7064 struct elf_symbuf_symbol
7066 unsigned long st_name
; /* Symbol name, index in string tbl */
7067 unsigned char st_info
; /* Type and binding attributes */
7068 unsigned char st_other
; /* Visibilty, and target specific */
7071 struct elf_symbuf_head
7073 struct elf_symbuf_symbol
*ssym
;
7074 bfd_size_type count
;
7075 unsigned int st_shndx
;
7082 Elf_Internal_Sym
*isym
;
7083 struct elf_symbuf_symbol
*ssym
;
7088 /* Sort references to symbols by ascending section number. */
7091 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7093 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7094 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7096 return s1
->st_shndx
- s2
->st_shndx
;
7100 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7102 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7103 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7104 return strcmp (s1
->name
, s2
->name
);
7107 static struct elf_symbuf_head
*
7108 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
7110 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7111 struct elf_symbuf_symbol
*ssym
;
7112 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7113 bfd_size_type i
, shndx_count
, total_size
;
7115 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7119 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7120 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7121 *ind
++ = &isymbuf
[i
];
7124 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7125 elf_sort_elf_symbol
);
7128 if (indbufend
> indbuf
)
7129 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7130 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7133 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7134 + (indbufend
- indbuf
) * sizeof (*ssym
));
7135 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7136 if (ssymbuf
== NULL
)
7142 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7143 ssymbuf
->ssym
= NULL
;
7144 ssymbuf
->count
= shndx_count
;
7145 ssymbuf
->st_shndx
= 0;
7146 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7148 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7151 ssymhead
->ssym
= ssym
;
7152 ssymhead
->count
= 0;
7153 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7155 ssym
->st_name
= (*ind
)->st_name
;
7156 ssym
->st_info
= (*ind
)->st_info
;
7157 ssym
->st_other
= (*ind
)->st_other
;
7160 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
7161 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7168 /* Check if 2 sections define the same set of local and global
7172 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7173 struct bfd_link_info
*info
)
7176 const struct elf_backend_data
*bed1
, *bed2
;
7177 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7178 bfd_size_type symcount1
, symcount2
;
7179 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7180 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7181 Elf_Internal_Sym
*isym
, *isymend
;
7182 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7183 bfd_size_type count1
, count2
, i
;
7184 unsigned int shndx1
, shndx2
;
7190 /* Both sections have to be in ELF. */
7191 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7192 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7195 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7198 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7199 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7200 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7203 bed1
= get_elf_backend_data (bfd1
);
7204 bed2
= get_elf_backend_data (bfd2
);
7205 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7206 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7207 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7208 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7210 if (symcount1
== 0 || symcount2
== 0)
7216 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7217 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7219 if (ssymbuf1
== NULL
)
7221 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7223 if (isymbuf1
== NULL
)
7226 if (!info
->reduce_memory_overheads
)
7227 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7228 = elf_create_symbuf (symcount1
, isymbuf1
);
7231 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7233 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7235 if (isymbuf2
== NULL
)
7238 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7239 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7240 = elf_create_symbuf (symcount2
, isymbuf2
);
7243 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7245 /* Optimized faster version. */
7246 bfd_size_type lo
, hi
, mid
;
7247 struct elf_symbol
*symp
;
7248 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7251 hi
= ssymbuf1
->count
;
7256 mid
= (lo
+ hi
) / 2;
7257 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7259 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7263 count1
= ssymbuf1
[mid
].count
;
7270 hi
= ssymbuf2
->count
;
7275 mid
= (lo
+ hi
) / 2;
7276 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7278 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7282 count2
= ssymbuf2
[mid
].count
;
7288 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7291 symtable1
= (struct elf_symbol
*)
7292 bfd_malloc (count1
* sizeof (struct elf_symbol
));
7293 symtable2
= (struct elf_symbol
*)
7294 bfd_malloc (count2
* sizeof (struct elf_symbol
));
7295 if (symtable1
== NULL
|| symtable2
== NULL
)
7299 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7300 ssym
< ssymend
; ssym
++, symp
++)
7302 symp
->u
.ssym
= ssym
;
7303 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7309 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7310 ssym
< ssymend
; ssym
++, symp
++)
7312 symp
->u
.ssym
= ssym
;
7313 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7318 /* Sort symbol by name. */
7319 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7320 elf_sym_name_compare
);
7321 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7322 elf_sym_name_compare
);
7324 for (i
= 0; i
< count1
; i
++)
7325 /* Two symbols must have the same binding, type and name. */
7326 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7327 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7328 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7335 symtable1
= (struct elf_symbol
*)
7336 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7337 symtable2
= (struct elf_symbol
*)
7338 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7339 if (symtable1
== NULL
|| symtable2
== NULL
)
7342 /* Count definitions in the section. */
7344 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7345 if (isym
->st_shndx
== shndx1
)
7346 symtable1
[count1
++].u
.isym
= isym
;
7349 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7350 if (isym
->st_shndx
== shndx2
)
7351 symtable2
[count2
++].u
.isym
= isym
;
7353 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7356 for (i
= 0; i
< count1
; i
++)
7358 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7359 symtable1
[i
].u
.isym
->st_name
);
7361 for (i
= 0; i
< count2
; i
++)
7363 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7364 symtable2
[i
].u
.isym
->st_name
);
7366 /* Sort symbol by name. */
7367 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7368 elf_sym_name_compare
);
7369 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7370 elf_sym_name_compare
);
7372 for (i
= 0; i
< count1
; i
++)
7373 /* Two symbols must have the same binding, type and name. */
7374 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7375 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7376 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7394 /* Return TRUE if 2 section types are compatible. */
7397 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7398 bfd
*bbfd
, const asection
*bsec
)
7402 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7403 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7406 return elf_section_type (asec
) == elf_section_type (bsec
);
7409 /* Final phase of ELF linker. */
7411 /* A structure we use to avoid passing large numbers of arguments. */
7413 struct elf_final_link_info
7415 /* General link information. */
7416 struct bfd_link_info
*info
;
7419 /* Symbol string table. */
7420 struct bfd_strtab_hash
*symstrtab
;
7421 /* .dynsym section. */
7422 asection
*dynsym_sec
;
7423 /* .hash section. */
7425 /* symbol version section (.gnu.version). */
7426 asection
*symver_sec
;
7427 /* Buffer large enough to hold contents of any section. */
7429 /* Buffer large enough to hold external relocs of any section. */
7430 void *external_relocs
;
7431 /* Buffer large enough to hold internal relocs of any section. */
7432 Elf_Internal_Rela
*internal_relocs
;
7433 /* Buffer large enough to hold external local symbols of any input
7435 bfd_byte
*external_syms
;
7436 /* And a buffer for symbol section indices. */
7437 Elf_External_Sym_Shndx
*locsym_shndx
;
7438 /* Buffer large enough to hold internal local symbols of any input
7440 Elf_Internal_Sym
*internal_syms
;
7441 /* Array large enough to hold a symbol index for each local symbol
7442 of any input BFD. */
7444 /* Array large enough to hold a section pointer for each local
7445 symbol of any input BFD. */
7446 asection
**sections
;
7447 /* Buffer to hold swapped out symbols. */
7449 /* And one for symbol section indices. */
7450 Elf_External_Sym_Shndx
*symshndxbuf
;
7451 /* Number of swapped out symbols in buffer. */
7452 size_t symbuf_count
;
7453 /* Number of symbols which fit in symbuf. */
7455 /* And same for symshndxbuf. */
7456 size_t shndxbuf_size
;
7457 /* Number of STT_FILE syms seen. */
7458 size_t filesym_count
;
7461 /* This struct is used to pass information to elf_link_output_extsym. */
7463 struct elf_outext_info
7466 bfd_boolean localsyms
;
7467 bfd_boolean need_second_pass
;
7468 bfd_boolean second_pass
;
7469 struct elf_final_link_info
*flinfo
;
7473 /* Support for evaluating a complex relocation.
7475 Complex relocations are generalized, self-describing relocations. The
7476 implementation of them consists of two parts: complex symbols, and the
7477 relocations themselves.
7479 The relocations are use a reserved elf-wide relocation type code (R_RELC
7480 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7481 information (start bit, end bit, word width, etc) into the addend. This
7482 information is extracted from CGEN-generated operand tables within gas.
7484 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7485 internal) representing prefix-notation expressions, including but not
7486 limited to those sorts of expressions normally encoded as addends in the
7487 addend field. The symbol mangling format is:
7490 | <unary-operator> ':' <node>
7491 | <binary-operator> ':' <node> ':' <node>
7494 <literal> := 's' <digits=N> ':' <N character symbol name>
7495 | 'S' <digits=N> ':' <N character section name>
7499 <binary-operator> := as in C
7500 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7503 set_symbol_value (bfd
*bfd_with_globals
,
7504 Elf_Internal_Sym
*isymbuf
,
7509 struct elf_link_hash_entry
**sym_hashes
;
7510 struct elf_link_hash_entry
*h
;
7511 size_t extsymoff
= locsymcount
;
7513 if (symidx
< locsymcount
)
7515 Elf_Internal_Sym
*sym
;
7517 sym
= isymbuf
+ symidx
;
7518 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7520 /* It is a local symbol: move it to the
7521 "absolute" section and give it a value. */
7522 sym
->st_shndx
= SHN_ABS
;
7523 sym
->st_value
= val
;
7526 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7530 /* It is a global symbol: set its link type
7531 to "defined" and give it a value. */
7533 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7534 h
= sym_hashes
[symidx
- extsymoff
];
7535 while (h
->root
.type
== bfd_link_hash_indirect
7536 || h
->root
.type
== bfd_link_hash_warning
)
7537 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7538 h
->root
.type
= bfd_link_hash_defined
;
7539 h
->root
.u
.def
.value
= val
;
7540 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7544 resolve_symbol (const char *name
,
7546 struct elf_final_link_info
*flinfo
,
7548 Elf_Internal_Sym
*isymbuf
,
7551 Elf_Internal_Sym
*sym
;
7552 struct bfd_link_hash_entry
*global_entry
;
7553 const char *candidate
= NULL
;
7554 Elf_Internal_Shdr
*symtab_hdr
;
7557 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7559 for (i
= 0; i
< locsymcount
; ++ i
)
7563 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7566 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7567 symtab_hdr
->sh_link
,
7570 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7571 name
, candidate
, (unsigned long) sym
->st_value
);
7573 if (candidate
&& strcmp (candidate
, name
) == 0)
7575 asection
*sec
= flinfo
->sections
[i
];
7577 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7578 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7580 printf ("Found symbol with value %8.8lx\n",
7581 (unsigned long) *result
);
7587 /* Hmm, haven't found it yet. perhaps it is a global. */
7588 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7589 FALSE
, FALSE
, TRUE
);
7593 if (global_entry
->type
== bfd_link_hash_defined
7594 || global_entry
->type
== bfd_link_hash_defweak
)
7596 *result
= (global_entry
->u
.def
.value
7597 + global_entry
->u
.def
.section
->output_section
->vma
7598 + global_entry
->u
.def
.section
->output_offset
);
7600 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7601 global_entry
->root
.string
, (unsigned long) *result
);
7610 resolve_section (const char *name
,
7617 for (curr
= sections
; curr
; curr
= curr
->next
)
7618 if (strcmp (curr
->name
, name
) == 0)
7620 *result
= curr
->vma
;
7624 /* Hmm. still haven't found it. try pseudo-section names. */
7625 for (curr
= sections
; curr
; curr
= curr
->next
)
7627 len
= strlen (curr
->name
);
7628 if (len
> strlen (name
))
7631 if (strncmp (curr
->name
, name
, len
) == 0)
7633 if (strncmp (".end", name
+ len
, 4) == 0)
7635 *result
= curr
->vma
+ curr
->size
;
7639 /* Insert more pseudo-section names here, if you like. */
7647 undefined_reference (const char *reftype
, const char *name
)
7649 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7654 eval_symbol (bfd_vma
*result
,
7657 struct elf_final_link_info
*flinfo
,
7659 Elf_Internal_Sym
*isymbuf
,
7668 const char *sym
= *symp
;
7670 bfd_boolean symbol_is_section
= FALSE
;
7675 if (len
< 1 || len
> sizeof (symbuf
))
7677 bfd_set_error (bfd_error_invalid_operation
);
7690 *result
= strtoul (sym
, (char **) symp
, 16);
7694 symbol_is_section
= TRUE
;
7697 symlen
= strtol (sym
, (char **) symp
, 10);
7698 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7700 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7702 bfd_set_error (bfd_error_invalid_operation
);
7706 memcpy (symbuf
, sym
, symlen
);
7707 symbuf
[symlen
] = '\0';
7708 *symp
= sym
+ symlen
;
7710 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7711 the symbol as a section, or vice-versa. so we're pretty liberal in our
7712 interpretation here; section means "try section first", not "must be a
7713 section", and likewise with symbol. */
7715 if (symbol_is_section
)
7717 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
)
7718 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7719 isymbuf
, locsymcount
))
7721 undefined_reference ("section", symbuf
);
7727 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7728 isymbuf
, locsymcount
)
7729 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
7732 undefined_reference ("symbol", symbuf
);
7739 /* All that remains are operators. */
7741 #define UNARY_OP(op) \
7742 if (strncmp (sym, #op, strlen (#op)) == 0) \
7744 sym += strlen (#op); \
7748 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7749 isymbuf, locsymcount, signed_p)) \
7752 *result = op ((bfd_signed_vma) a); \
7758 #define BINARY_OP(op) \
7759 if (strncmp (sym, #op, strlen (#op)) == 0) \
7761 sym += strlen (#op); \
7765 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7766 isymbuf, locsymcount, signed_p)) \
7769 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7770 isymbuf, locsymcount, signed_p)) \
7773 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7803 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7804 bfd_set_error (bfd_error_invalid_operation
);
7810 put_value (bfd_vma size
,
7811 unsigned long chunksz
,
7816 location
+= (size
- chunksz
);
7818 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7826 bfd_put_8 (input_bfd
, x
, location
);
7829 bfd_put_16 (input_bfd
, x
, location
);
7832 bfd_put_32 (input_bfd
, x
, location
);
7836 bfd_put_64 (input_bfd
, x
, location
);
7846 get_value (bfd_vma size
,
7847 unsigned long chunksz
,
7853 for (; size
; size
-= chunksz
, location
+= chunksz
)
7861 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7864 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7867 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7871 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7882 decode_complex_addend (unsigned long *start
, /* in bits */
7883 unsigned long *oplen
, /* in bits */
7884 unsigned long *len
, /* in bits */
7885 unsigned long *wordsz
, /* in bytes */
7886 unsigned long *chunksz
, /* in bytes */
7887 unsigned long *lsb0_p
,
7888 unsigned long *signed_p
,
7889 unsigned long *trunc_p
,
7890 unsigned long encoded
)
7892 * start
= encoded
& 0x3F;
7893 * len
= (encoded
>> 6) & 0x3F;
7894 * oplen
= (encoded
>> 12) & 0x3F;
7895 * wordsz
= (encoded
>> 18) & 0xF;
7896 * chunksz
= (encoded
>> 22) & 0xF;
7897 * lsb0_p
= (encoded
>> 27) & 1;
7898 * signed_p
= (encoded
>> 28) & 1;
7899 * trunc_p
= (encoded
>> 29) & 1;
7902 bfd_reloc_status_type
7903 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
7904 asection
*input_section ATTRIBUTE_UNUSED
,
7906 Elf_Internal_Rela
*rel
,
7909 bfd_vma shift
, x
, mask
;
7910 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
7911 bfd_reloc_status_type r
;
7913 /* Perform this reloc, since it is complex.
7914 (this is not to say that it necessarily refers to a complex
7915 symbol; merely that it is a self-describing CGEN based reloc.
7916 i.e. the addend has the complete reloc information (bit start, end,
7917 word size, etc) encoded within it.). */
7919 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
7920 &chunksz
, &lsb0_p
, &signed_p
,
7921 &trunc_p
, rel
->r_addend
);
7923 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7926 shift
= (start
+ 1) - len
;
7928 shift
= (8 * wordsz
) - (start
+ len
);
7930 /* FIXME: octets_per_byte. */
7931 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7934 printf ("Doing complex reloc: "
7935 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7936 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7937 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7938 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7939 oplen
, (unsigned long) x
, (unsigned long) mask
,
7940 (unsigned long) relocation
);
7945 /* Now do an overflow check. */
7946 r
= bfd_check_overflow ((signed_p
7947 ? complain_overflow_signed
7948 : complain_overflow_unsigned
),
7949 len
, 0, (8 * wordsz
),
7953 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7956 printf (" relocation: %8.8lx\n"
7957 " shifted mask: %8.8lx\n"
7958 " shifted/masked reloc: %8.8lx\n"
7959 " result: %8.8lx\n",
7960 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
7961 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
7963 /* FIXME: octets_per_byte. */
7964 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7968 /* When performing a relocatable link, the input relocations are
7969 preserved. But, if they reference global symbols, the indices
7970 referenced must be updated. Update all the relocations found in
7974 elf_link_adjust_relocs (bfd
*abfd
,
7975 struct bfd_elf_section_reloc_data
*reldata
)
7978 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7980 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7981 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7982 bfd_vma r_type_mask
;
7984 unsigned int count
= reldata
->count
;
7985 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
7987 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7989 swap_in
= bed
->s
->swap_reloc_in
;
7990 swap_out
= bed
->s
->swap_reloc_out
;
7992 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7994 swap_in
= bed
->s
->swap_reloca_in
;
7995 swap_out
= bed
->s
->swap_reloca_out
;
8000 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8003 if (bed
->s
->arch_size
== 32)
8010 r_type_mask
= 0xffffffff;
8014 erela
= reldata
->hdr
->contents
;
8015 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8017 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8020 if (*rel_hash
== NULL
)
8023 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8025 (*swap_in
) (abfd
, erela
, irela
);
8026 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8027 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8028 | (irela
[j
].r_info
& r_type_mask
));
8029 (*swap_out
) (abfd
, irela
, erela
);
8033 struct elf_link_sort_rela
8039 enum elf_reloc_type_class type
;
8040 /* We use this as an array of size int_rels_per_ext_rel. */
8041 Elf_Internal_Rela rela
[1];
8045 elf_link_sort_cmp1 (const void *A
, const void *B
)
8047 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8048 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8049 int relativea
, relativeb
;
8051 relativea
= a
->type
== reloc_class_relative
;
8052 relativeb
= b
->type
== reloc_class_relative
;
8054 if (relativea
< relativeb
)
8056 if (relativea
> relativeb
)
8058 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8060 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8062 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8064 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8070 elf_link_sort_cmp2 (const void *A
, const void *B
)
8072 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8073 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8076 if (a
->u
.offset
< b
->u
.offset
)
8078 if (a
->u
.offset
> b
->u
.offset
)
8080 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
8081 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
8086 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8088 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8094 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8096 asection
*dynamic_relocs
;
8099 bfd_size_type count
, size
;
8100 size_t i
, ret
, sort_elt
, ext_size
;
8101 bfd_byte
*sort
, *s_non_relative
, *p
;
8102 struct elf_link_sort_rela
*sq
;
8103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8104 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8105 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8106 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8107 struct bfd_link_order
*lo
;
8109 bfd_boolean use_rela
;
8111 /* Find a dynamic reloc section. */
8112 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8113 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8114 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8115 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8117 bfd_boolean use_rela_initialised
= FALSE
;
8119 /* This is just here to stop gcc from complaining.
8120 It's initialization checking code is not perfect. */
8123 /* Both sections are present. Examine the sizes
8124 of the indirect sections to help us choose. */
8125 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8126 if (lo
->type
== bfd_indirect_link_order
)
8128 asection
*o
= lo
->u
.indirect
.section
;
8130 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8132 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8133 /* Section size is divisible by both rel and rela sizes.
8134 It is of no help to us. */
8138 /* Section size is only divisible by rela. */
8139 if (use_rela_initialised
&& (use_rela
== FALSE
))
8142 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8143 bfd_set_error (bfd_error_invalid_operation
);
8149 use_rela_initialised
= TRUE
;
8153 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8155 /* Section size is only divisible by rel. */
8156 if (use_rela_initialised
&& (use_rela
== TRUE
))
8159 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8160 bfd_set_error (bfd_error_invalid_operation
);
8166 use_rela_initialised
= TRUE
;
8171 /* The section size is not divisible by either - something is wrong. */
8173 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8174 bfd_set_error (bfd_error_invalid_operation
);
8179 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8180 if (lo
->type
== bfd_indirect_link_order
)
8182 asection
*o
= lo
->u
.indirect
.section
;
8184 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8186 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8187 /* Section size is divisible by both rel and rela sizes.
8188 It is of no help to us. */
8192 /* Section size is only divisible by rela. */
8193 if (use_rela_initialised
&& (use_rela
== FALSE
))
8196 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8197 bfd_set_error (bfd_error_invalid_operation
);
8203 use_rela_initialised
= TRUE
;
8207 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8209 /* Section size is only divisible by rel. */
8210 if (use_rela_initialised
&& (use_rela
== TRUE
))
8213 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8214 bfd_set_error (bfd_error_invalid_operation
);
8220 use_rela_initialised
= TRUE
;
8225 /* The section size is not divisible by either - something is wrong. */
8227 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8228 bfd_set_error (bfd_error_invalid_operation
);
8233 if (! use_rela_initialised
)
8237 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8239 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8246 dynamic_relocs
= rela_dyn
;
8247 ext_size
= bed
->s
->sizeof_rela
;
8248 swap_in
= bed
->s
->swap_reloca_in
;
8249 swap_out
= bed
->s
->swap_reloca_out
;
8253 dynamic_relocs
= rel_dyn
;
8254 ext_size
= bed
->s
->sizeof_rel
;
8255 swap_in
= bed
->s
->swap_reloc_in
;
8256 swap_out
= bed
->s
->swap_reloc_out
;
8260 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8261 if (lo
->type
== bfd_indirect_link_order
)
8262 size
+= lo
->u
.indirect
.section
->size
;
8264 if (size
!= dynamic_relocs
->size
)
8267 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8268 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8270 count
= dynamic_relocs
->size
/ ext_size
;
8273 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8277 (*info
->callbacks
->warning
)
8278 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8282 if (bed
->s
->arch_size
== 32)
8283 r_sym_mask
= ~(bfd_vma
) 0xff;
8285 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8287 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8288 if (lo
->type
== bfd_indirect_link_order
)
8290 bfd_byte
*erel
, *erelend
;
8291 asection
*o
= lo
->u
.indirect
.section
;
8293 if (o
->contents
== NULL
&& o
->size
!= 0)
8295 /* This is a reloc section that is being handled as a normal
8296 section. See bfd_section_from_shdr. We can't combine
8297 relocs in this case. */
8302 erelend
= o
->contents
+ o
->size
;
8303 /* FIXME: octets_per_byte. */
8304 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8306 while (erel
< erelend
)
8308 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8310 (*swap_in
) (abfd
, erel
, s
->rela
);
8311 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8312 s
->u
.sym_mask
= r_sym_mask
;
8318 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8320 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8322 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8323 if (s
->type
!= reloc_class_relative
)
8329 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8330 for (; i
< count
; i
++, p
+= sort_elt
)
8332 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8333 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8335 sp
->u
.offset
= sq
->rela
->r_offset
;
8338 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8340 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8341 if (lo
->type
== bfd_indirect_link_order
)
8343 bfd_byte
*erel
, *erelend
;
8344 asection
*o
= lo
->u
.indirect
.section
;
8347 erelend
= o
->contents
+ o
->size
;
8348 /* FIXME: octets_per_byte. */
8349 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8350 while (erel
< erelend
)
8352 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8353 (*swap_out
) (abfd
, s
->rela
, erel
);
8360 *psec
= dynamic_relocs
;
8364 /* Flush the output symbols to the file. */
8367 elf_link_flush_output_syms (struct elf_final_link_info
*flinfo
,
8368 const struct elf_backend_data
*bed
)
8370 if (flinfo
->symbuf_count
> 0)
8372 Elf_Internal_Shdr
*hdr
;
8376 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
8377 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8378 amt
= flinfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8379 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) != 0
8380 || bfd_bwrite (flinfo
->symbuf
, amt
, flinfo
->output_bfd
) != amt
)
8383 hdr
->sh_size
+= amt
;
8384 flinfo
->symbuf_count
= 0;
8390 /* Add a symbol to the output symbol table. */
8393 elf_link_output_sym (struct elf_final_link_info
*flinfo
,
8395 Elf_Internal_Sym
*elfsym
,
8396 asection
*input_sec
,
8397 struct elf_link_hash_entry
*h
)
8400 Elf_External_Sym_Shndx
*destshndx
;
8401 int (*output_symbol_hook
)
8402 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8403 struct elf_link_hash_entry
*);
8404 const struct elf_backend_data
*bed
;
8406 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8407 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8408 if (output_symbol_hook
!= NULL
)
8410 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8415 if (name
== NULL
|| *name
== '\0')
8416 elfsym
->st_name
= 0;
8417 else if (input_sec
->flags
& SEC_EXCLUDE
)
8418 elfsym
->st_name
= 0;
8421 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (flinfo
->symstrtab
,
8423 if (elfsym
->st_name
== (unsigned long) -1)
8427 if (flinfo
->symbuf_count
>= flinfo
->symbuf_size
)
8429 if (! elf_link_flush_output_syms (flinfo
, bed
))
8433 dest
= flinfo
->symbuf
+ flinfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8434 destshndx
= flinfo
->symshndxbuf
;
8435 if (destshndx
!= NULL
)
8437 if (bfd_get_symcount (flinfo
->output_bfd
) >= flinfo
->shndxbuf_size
)
8441 amt
= flinfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8442 destshndx
= (Elf_External_Sym_Shndx
*) bfd_realloc (destshndx
,
8444 if (destshndx
== NULL
)
8446 flinfo
->symshndxbuf
= destshndx
;
8447 memset ((char *) destshndx
+ amt
, 0, amt
);
8448 flinfo
->shndxbuf_size
*= 2;
8450 destshndx
+= bfd_get_symcount (flinfo
->output_bfd
);
8453 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, elfsym
, dest
, destshndx
);
8454 flinfo
->symbuf_count
+= 1;
8455 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8460 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8463 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8465 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
8466 && sym
->st_shndx
< SHN_LORESERVE
)
8468 /* The gABI doesn't support dynamic symbols in output sections
8470 (*_bfd_error_handler
)
8471 (_("%B: Too many sections: %d (>= %d)"),
8472 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
8473 bfd_set_error (bfd_error_nonrepresentable_section
);
8479 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8480 allowing an unsatisfied unversioned symbol in the DSO to match a
8481 versioned symbol that would normally require an explicit version.
8482 We also handle the case that a DSO references a hidden symbol
8483 which may be satisfied by a versioned symbol in another DSO. */
8486 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8487 const struct elf_backend_data
*bed
,
8488 struct elf_link_hash_entry
*h
)
8491 struct elf_link_loaded_list
*loaded
;
8493 if (!is_elf_hash_table (info
->hash
))
8496 /* Check indirect symbol. */
8497 while (h
->root
.type
== bfd_link_hash_indirect
)
8498 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8500 switch (h
->root
.type
)
8506 case bfd_link_hash_undefined
:
8507 case bfd_link_hash_undefweak
:
8508 abfd
= h
->root
.u
.undef
.abfd
;
8509 if ((abfd
->flags
& DYNAMIC
) == 0
8510 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8514 case bfd_link_hash_defined
:
8515 case bfd_link_hash_defweak
:
8516 abfd
= h
->root
.u
.def
.section
->owner
;
8519 case bfd_link_hash_common
:
8520 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8523 BFD_ASSERT (abfd
!= NULL
);
8525 for (loaded
= elf_hash_table (info
)->loaded
;
8527 loaded
= loaded
->next
)
8530 Elf_Internal_Shdr
*hdr
;
8531 bfd_size_type symcount
;
8532 bfd_size_type extsymcount
;
8533 bfd_size_type extsymoff
;
8534 Elf_Internal_Shdr
*versymhdr
;
8535 Elf_Internal_Sym
*isym
;
8536 Elf_Internal_Sym
*isymend
;
8537 Elf_Internal_Sym
*isymbuf
;
8538 Elf_External_Versym
*ever
;
8539 Elf_External_Versym
*extversym
;
8541 input
= loaded
->abfd
;
8543 /* We check each DSO for a possible hidden versioned definition. */
8545 || (input
->flags
& DYNAMIC
) == 0
8546 || elf_dynversym (input
) == 0)
8549 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8551 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8552 if (elf_bad_symtab (input
))
8554 extsymcount
= symcount
;
8559 extsymcount
= symcount
- hdr
->sh_info
;
8560 extsymoff
= hdr
->sh_info
;
8563 if (extsymcount
== 0)
8566 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8568 if (isymbuf
== NULL
)
8571 /* Read in any version definitions. */
8572 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8573 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
8574 if (extversym
== NULL
)
8577 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8578 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8579 != versymhdr
->sh_size
))
8587 ever
= extversym
+ extsymoff
;
8588 isymend
= isymbuf
+ extsymcount
;
8589 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8592 Elf_Internal_Versym iver
;
8593 unsigned short version_index
;
8595 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8596 || isym
->st_shndx
== SHN_UNDEF
)
8599 name
= bfd_elf_string_from_elf_section (input
,
8602 if (strcmp (name
, h
->root
.root
.string
) != 0)
8605 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8607 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
8609 && h
->forced_local
))
8611 /* If we have a non-hidden versioned sym, then it should
8612 have provided a definition for the undefined sym unless
8613 it is defined in a non-shared object and forced local.
8618 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8619 if (version_index
== 1 || version_index
== 2)
8621 /* This is the base or first version. We can use it. */
8635 /* Add an external symbol to the symbol table. This is called from
8636 the hash table traversal routine. When generating a shared object,
8637 we go through the symbol table twice. The first time we output
8638 anything that might have been forced to local scope in a version
8639 script. The second time we output the symbols that are still
8643 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
8645 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
8646 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
8647 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
8649 Elf_Internal_Sym sym
;
8650 asection
*input_sec
;
8651 const struct elf_backend_data
*bed
;
8655 if (h
->root
.type
== bfd_link_hash_warning
)
8657 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8658 if (h
->root
.type
== bfd_link_hash_new
)
8662 /* Decide whether to output this symbol in this pass. */
8663 if (eoinfo
->localsyms
)
8665 if (!h
->forced_local
)
8667 if (eoinfo
->second_pass
8668 && !((h
->root
.type
== bfd_link_hash_defined
8669 || h
->root
.type
== bfd_link_hash_defweak
)
8670 && h
->root
.u
.def
.section
->output_section
!= NULL
))
8675 if (h
->forced_local
)
8679 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8681 if (h
->root
.type
== bfd_link_hash_undefined
)
8683 /* If we have an undefined symbol reference here then it must have
8684 come from a shared library that is being linked in. (Undefined
8685 references in regular files have already been handled unless
8686 they are in unreferenced sections which are removed by garbage
8688 bfd_boolean ignore_undef
= FALSE
;
8690 /* Some symbols may be special in that the fact that they're
8691 undefined can be safely ignored - let backend determine that. */
8692 if (bed
->elf_backend_ignore_undef_symbol
)
8693 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8695 /* If we are reporting errors for this situation then do so now. */
8698 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
8699 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
8700 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8702 if (!(flinfo
->info
->callbacks
->undefined_symbol
8703 (flinfo
->info
, h
->root
.root
.string
,
8704 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
8706 (flinfo
->info
->unresolved_syms_in_shared_libs
8707 == RM_GENERATE_ERROR
))))
8709 bfd_set_error (bfd_error_bad_value
);
8710 eoinfo
->failed
= TRUE
;
8716 /* We should also warn if a forced local symbol is referenced from
8717 shared libraries. */
8718 if (!flinfo
->info
->relocatable
8719 && flinfo
->info
->executable
8725 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
8729 struct elf_link_hash_entry
*hi
= h
;
8731 /* Check indirect symbol. */
8732 while (hi
->root
.type
== bfd_link_hash_indirect
)
8733 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
8735 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
8736 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
8737 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
8738 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
8740 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
8741 def_bfd
= flinfo
->output_bfd
;
8742 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
8743 def_bfd
= hi
->root
.u
.def
.section
->owner
;
8744 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, def_bfd
,
8745 h
->root
.root
.string
);
8746 bfd_set_error (bfd_error_bad_value
);
8747 eoinfo
->failed
= TRUE
;
8751 /* We don't want to output symbols that have never been mentioned by
8752 a regular file, or that we have been told to strip. However, if
8753 h->indx is set to -2, the symbol is used by a reloc and we must
8757 else if ((h
->def_dynamic
8759 || h
->root
.type
== bfd_link_hash_new
)
8763 else if (flinfo
->info
->strip
== strip_all
)
8765 else if (flinfo
->info
->strip
== strip_some
8766 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
8767 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8769 else if ((h
->root
.type
== bfd_link_hash_defined
8770 || h
->root
.type
== bfd_link_hash_defweak
)
8771 && ((flinfo
->info
->strip_discarded
8772 && discarded_section (h
->root
.u
.def
.section
))
8773 || (h
->root
.u
.def
.section
->owner
!= NULL
8774 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
8776 else if ((h
->root
.type
== bfd_link_hash_undefined
8777 || h
->root
.type
== bfd_link_hash_undefweak
)
8778 && h
->root
.u
.undef
.abfd
!= NULL
8779 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
8784 /* If we're stripping it, and it's not a dynamic symbol, there's
8785 nothing else to do unless it is a forced local symbol or a
8786 STT_GNU_IFUNC symbol. */
8789 && h
->type
!= STT_GNU_IFUNC
8790 && !h
->forced_local
)
8794 sym
.st_size
= h
->size
;
8795 sym
.st_other
= h
->other
;
8796 if (h
->forced_local
)
8798 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8799 /* Turn off visibility on local symbol. */
8800 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
8802 else if (h
->unique_global
)
8803 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, h
->type
);
8804 else if (h
->root
.type
== bfd_link_hash_undefweak
8805 || h
->root
.type
== bfd_link_hash_defweak
)
8806 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8808 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8809 sym
.st_target_internal
= h
->target_internal
;
8811 switch (h
->root
.type
)
8814 case bfd_link_hash_new
:
8815 case bfd_link_hash_warning
:
8819 case bfd_link_hash_undefined
:
8820 case bfd_link_hash_undefweak
:
8821 input_sec
= bfd_und_section_ptr
;
8822 sym
.st_shndx
= SHN_UNDEF
;
8825 case bfd_link_hash_defined
:
8826 case bfd_link_hash_defweak
:
8828 input_sec
= h
->root
.u
.def
.section
;
8829 if (input_sec
->output_section
!= NULL
)
8831 if (eoinfo
->localsyms
&& flinfo
->filesym_count
== 1)
8833 bfd_boolean second_pass_sym
8834 = (input_sec
->owner
== flinfo
->output_bfd
8835 || input_sec
->owner
== NULL
8836 || (input_sec
->flags
& SEC_LINKER_CREATED
) != 0
8837 || (input_sec
->owner
->flags
& BFD_LINKER_CREATED
) != 0);
8839 eoinfo
->need_second_pass
|= second_pass_sym
;
8840 if (eoinfo
->second_pass
!= second_pass_sym
)
8845 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
8846 input_sec
->output_section
);
8847 if (sym
.st_shndx
== SHN_BAD
)
8849 (*_bfd_error_handler
)
8850 (_("%B: could not find output section %A for input section %A"),
8851 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8852 bfd_set_error (bfd_error_nonrepresentable_section
);
8853 eoinfo
->failed
= TRUE
;
8857 /* ELF symbols in relocatable files are section relative,
8858 but in nonrelocatable files they are virtual
8860 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8861 if (!flinfo
->info
->relocatable
)
8863 sym
.st_value
+= input_sec
->output_section
->vma
;
8864 if (h
->type
== STT_TLS
)
8866 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
8867 if (tls_sec
!= NULL
)
8868 sym
.st_value
-= tls_sec
->vma
;
8871 /* The TLS section may have been garbage collected. */
8872 BFD_ASSERT (flinfo
->info
->gc_sections
8873 && !input_sec
->gc_mark
);
8880 BFD_ASSERT (input_sec
->owner
== NULL
8881 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8882 sym
.st_shndx
= SHN_UNDEF
;
8883 input_sec
= bfd_und_section_ptr
;
8888 case bfd_link_hash_common
:
8889 input_sec
= h
->root
.u
.c
.p
->section
;
8890 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8891 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8894 case bfd_link_hash_indirect
:
8895 /* These symbols are created by symbol versioning. They point
8896 to the decorated version of the name. For example, if the
8897 symbol foo@@GNU_1.2 is the default, which should be used when
8898 foo is used with no version, then we add an indirect symbol
8899 foo which points to foo@@GNU_1.2. We ignore these symbols,
8900 since the indirected symbol is already in the hash table. */
8904 /* Give the processor backend a chance to tweak the symbol value,
8905 and also to finish up anything that needs to be done for this
8906 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8907 forced local syms when non-shared is due to a historical quirk.
8908 STT_GNU_IFUNC symbol must go through PLT. */
8909 if ((h
->type
== STT_GNU_IFUNC
8911 && !flinfo
->info
->relocatable
)
8912 || ((h
->dynindx
!= -1
8914 && ((flinfo
->info
->shared
8915 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8916 || h
->root
.type
!= bfd_link_hash_undefweak
))
8917 || !h
->forced_local
)
8918 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
8920 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8921 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
8923 eoinfo
->failed
= TRUE
;
8928 /* If we are marking the symbol as undefined, and there are no
8929 non-weak references to this symbol from a regular object, then
8930 mark the symbol as weak undefined; if there are non-weak
8931 references, mark the symbol as strong. We can't do this earlier,
8932 because it might not be marked as undefined until the
8933 finish_dynamic_symbol routine gets through with it. */
8934 if (sym
.st_shndx
== SHN_UNDEF
8936 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8937 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8940 unsigned int type
= ELF_ST_TYPE (sym
.st_info
);
8942 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8943 if (type
== STT_GNU_IFUNC
)
8946 if (h
->ref_regular_nonweak
)
8947 bindtype
= STB_GLOBAL
;
8949 bindtype
= STB_WEAK
;
8950 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
8953 /* If this is a symbol defined in a dynamic library, don't use the
8954 symbol size from the dynamic library. Relinking an executable
8955 against a new library may introduce gratuitous changes in the
8956 executable's symbols if we keep the size. */
8957 if (sym
.st_shndx
== SHN_UNDEF
8962 /* If a non-weak symbol with non-default visibility is not defined
8963 locally, it is a fatal error. */
8964 if (!flinfo
->info
->relocatable
8965 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8966 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8967 && h
->root
.type
== bfd_link_hash_undefined
8972 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
8973 msg
= _("%B: protected symbol `%s' isn't defined");
8974 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
8975 msg
= _("%B: internal symbol `%s' isn't defined");
8977 msg
= _("%B: hidden symbol `%s' isn't defined");
8978 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
8979 bfd_set_error (bfd_error_bad_value
);
8980 eoinfo
->failed
= TRUE
;
8984 /* If this symbol should be put in the .dynsym section, then put it
8985 there now. We already know the symbol index. We also fill in
8986 the entry in the .hash section. */
8987 if (flinfo
->dynsym_sec
!= NULL
8989 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
8993 /* Since there is no version information in the dynamic string,
8994 if there is no version info in symbol version section, we will
8995 have a run-time problem. */
8996 if (h
->verinfo
.verdef
== NULL
)
8998 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9000 if (p
&& p
[1] != '\0')
9002 (*_bfd_error_handler
)
9003 (_("%B: No symbol version section for versioned symbol `%s'"),
9004 flinfo
->output_bfd
, h
->root
.root
.string
);
9005 eoinfo
->failed
= TRUE
;
9010 sym
.st_name
= h
->dynstr_index
;
9011 esym
= flinfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
9012 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9014 eoinfo
->failed
= TRUE
;
9017 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9019 if (flinfo
->hash_sec
!= NULL
)
9021 size_t hash_entry_size
;
9022 bfd_byte
*bucketpos
;
9027 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9028 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9031 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9032 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9033 + (bucket
+ 2) * hash_entry_size
);
9034 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9035 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9037 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9038 ((bfd_byte
*) flinfo
->hash_sec
->contents
9039 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9042 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9044 Elf_Internal_Versym iversym
;
9045 Elf_External_Versym
*eversym
;
9047 if (!h
->def_regular
)
9049 if (h
->verinfo
.verdef
== NULL
)
9050 iversym
.vs_vers
= 0;
9052 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9056 if (h
->verinfo
.vertree
== NULL
)
9057 iversym
.vs_vers
= 1;
9059 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9060 if (flinfo
->info
->create_default_symver
)
9065 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9067 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9068 eversym
+= h
->dynindx
;
9069 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9073 /* If we're stripping it, then it was just a dynamic symbol, and
9074 there's nothing else to do. */
9075 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
9078 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9079 ret
= elf_link_output_sym (flinfo
, h
->root
.root
.string
, &sym
, input_sec
, h
);
9082 eoinfo
->failed
= TRUE
;
9087 else if (h
->indx
== -2)
9093 /* Return TRUE if special handling is done for relocs in SEC against
9094 symbols defined in discarded sections. */
9097 elf_section_ignore_discarded_relocs (asection
*sec
)
9099 const struct elf_backend_data
*bed
;
9101 switch (sec
->sec_info_type
)
9103 case SEC_INFO_TYPE_STABS
:
9104 case SEC_INFO_TYPE_EH_FRAME
:
9110 bed
= get_elf_backend_data (sec
->owner
);
9111 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9112 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9118 /* Return a mask saying how ld should treat relocations in SEC against
9119 symbols defined in discarded sections. If this function returns
9120 COMPLAIN set, ld will issue a warning message. If this function
9121 returns PRETEND set, and the discarded section was link-once and the
9122 same size as the kept link-once section, ld will pretend that the
9123 symbol was actually defined in the kept section. Otherwise ld will
9124 zero the reloc (at least that is the intent, but some cooperation by
9125 the target dependent code is needed, particularly for REL targets). */
9128 _bfd_elf_default_action_discarded (asection
*sec
)
9130 if (sec
->flags
& SEC_DEBUGGING
)
9133 if (strcmp (".eh_frame", sec
->name
) == 0)
9136 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9139 return COMPLAIN
| PRETEND
;
9142 /* Find a match between a section and a member of a section group. */
9145 match_group_member (asection
*sec
, asection
*group
,
9146 struct bfd_link_info
*info
)
9148 asection
*first
= elf_next_in_group (group
);
9149 asection
*s
= first
;
9153 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9156 s
= elf_next_in_group (s
);
9164 /* Check if the kept section of a discarded section SEC can be used
9165 to replace it. Return the replacement if it is OK. Otherwise return
9169 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9173 kept
= sec
->kept_section
;
9176 if ((kept
->flags
& SEC_GROUP
) != 0)
9177 kept
= match_group_member (sec
, kept
, info
);
9179 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9180 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9182 sec
->kept_section
= kept
;
9187 /* Link an input file into the linker output file. This function
9188 handles all the sections and relocations of the input file at once.
9189 This is so that we only have to read the local symbols once, and
9190 don't have to keep them in memory. */
9193 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9195 int (*relocate_section
)
9196 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9197 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9199 Elf_Internal_Shdr
*symtab_hdr
;
9202 Elf_Internal_Sym
*isymbuf
;
9203 Elf_Internal_Sym
*isym
;
9204 Elf_Internal_Sym
*isymend
;
9206 asection
**ppsection
;
9208 const struct elf_backend_data
*bed
;
9209 struct elf_link_hash_entry
**sym_hashes
;
9210 bfd_size_type address_size
;
9211 bfd_vma r_type_mask
;
9213 bfd_boolean have_file_sym
= FALSE
;
9215 output_bfd
= flinfo
->output_bfd
;
9216 bed
= get_elf_backend_data (output_bfd
);
9217 relocate_section
= bed
->elf_backend_relocate_section
;
9219 /* If this is a dynamic object, we don't want to do anything here:
9220 we don't want the local symbols, and we don't want the section
9222 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9225 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9226 if (elf_bad_symtab (input_bfd
))
9228 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9233 locsymcount
= symtab_hdr
->sh_info
;
9234 extsymoff
= symtab_hdr
->sh_info
;
9237 /* Read the local symbols. */
9238 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9239 if (isymbuf
== NULL
&& locsymcount
!= 0)
9241 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9242 flinfo
->internal_syms
,
9243 flinfo
->external_syms
,
9244 flinfo
->locsym_shndx
);
9245 if (isymbuf
== NULL
)
9249 /* Find local symbol sections and adjust values of symbols in
9250 SEC_MERGE sections. Write out those local symbols we know are
9251 going into the output file. */
9252 isymend
= isymbuf
+ locsymcount
;
9253 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9255 isym
++, pindex
++, ppsection
++)
9259 Elf_Internal_Sym osym
;
9265 if (elf_bad_symtab (input_bfd
))
9267 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9274 if (isym
->st_shndx
== SHN_UNDEF
)
9275 isec
= bfd_und_section_ptr
;
9276 else if (isym
->st_shndx
== SHN_ABS
)
9277 isec
= bfd_abs_section_ptr
;
9278 else if (isym
->st_shndx
== SHN_COMMON
)
9279 isec
= bfd_com_section_ptr
;
9282 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9285 /* Don't attempt to output symbols with st_shnx in the
9286 reserved range other than SHN_ABS and SHN_COMMON. */
9290 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9291 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9293 _bfd_merged_section_offset (output_bfd
, &isec
,
9294 elf_section_data (isec
)->sec_info
,
9300 /* Don't output the first, undefined, symbol. */
9301 if (ppsection
== flinfo
->sections
)
9304 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9306 /* We never output section symbols. Instead, we use the
9307 section symbol of the corresponding section in the output
9312 /* If we are stripping all symbols, we don't want to output this
9314 if (flinfo
->info
->strip
== strip_all
)
9317 /* If we are discarding all local symbols, we don't want to
9318 output this one. If we are generating a relocatable output
9319 file, then some of the local symbols may be required by
9320 relocs; we output them below as we discover that they are
9322 if (flinfo
->info
->discard
== discard_all
)
9325 /* If this symbol is defined in a section which we are
9326 discarding, we don't need to keep it. */
9327 if (isym
->st_shndx
!= SHN_UNDEF
9328 && isym
->st_shndx
< SHN_LORESERVE
9329 && bfd_section_removed_from_list (output_bfd
,
9330 isec
->output_section
))
9333 /* Get the name of the symbol. */
9334 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9339 /* See if we are discarding symbols with this name. */
9340 if ((flinfo
->info
->strip
== strip_some
9341 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9343 || (((flinfo
->info
->discard
== discard_sec_merge
9344 && (isec
->flags
& SEC_MERGE
) && !flinfo
->info
->relocatable
)
9345 || flinfo
->info
->discard
== discard_l
)
9346 && bfd_is_local_label_name (input_bfd
, name
)))
9349 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
9351 have_file_sym
= TRUE
;
9352 flinfo
->filesym_count
+= 1;
9356 /* In the absence of debug info, bfd_find_nearest_line uses
9357 FILE symbols to determine the source file for local
9358 function symbols. Provide a FILE symbol here if input
9359 files lack such, so that their symbols won't be
9360 associated with a previous input file. It's not the
9361 source file, but the best we can do. */
9362 have_file_sym
= TRUE
;
9363 flinfo
->filesym_count
+= 1;
9364 memset (&osym
, 0, sizeof (osym
));
9365 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9366 osym
.st_shndx
= SHN_ABS
;
9367 if (!elf_link_output_sym (flinfo
, input_bfd
->filename
, &osym
,
9368 bfd_abs_section_ptr
, NULL
))
9374 /* Adjust the section index for the output file. */
9375 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9376 isec
->output_section
);
9377 if (osym
.st_shndx
== SHN_BAD
)
9380 /* ELF symbols in relocatable files are section relative, but
9381 in executable files they are virtual addresses. Note that
9382 this code assumes that all ELF sections have an associated
9383 BFD section with a reasonable value for output_offset; below
9384 we assume that they also have a reasonable value for
9385 output_section. Any special sections must be set up to meet
9386 these requirements. */
9387 osym
.st_value
+= isec
->output_offset
;
9388 if (!flinfo
->info
->relocatable
)
9390 osym
.st_value
+= isec
->output_section
->vma
;
9391 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9393 /* STT_TLS symbols are relative to PT_TLS segment base. */
9394 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
9395 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
9399 indx
= bfd_get_symcount (output_bfd
);
9400 ret
= elf_link_output_sym (flinfo
, name
, &osym
, isec
, NULL
);
9407 if (bed
->s
->arch_size
== 32)
9415 r_type_mask
= 0xffffffff;
9420 /* Relocate the contents of each section. */
9421 sym_hashes
= elf_sym_hashes (input_bfd
);
9422 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9426 if (! o
->linker_mark
)
9428 /* This section was omitted from the link. */
9432 if (flinfo
->info
->relocatable
9433 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
9435 /* Deal with the group signature symbol. */
9436 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
9437 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
9438 asection
*osec
= o
->output_section
;
9440 if (symndx
>= locsymcount
9441 || (elf_bad_symtab (input_bfd
)
9442 && flinfo
->sections
[symndx
] == NULL
))
9444 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
9445 while (h
->root
.type
== bfd_link_hash_indirect
9446 || h
->root
.type
== bfd_link_hash_warning
)
9447 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9448 /* Arrange for symbol to be output. */
9450 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
9452 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
9454 /* We'll use the output section target_index. */
9455 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
9456 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
9460 if (flinfo
->indices
[symndx
] == -1)
9462 /* Otherwise output the local symbol now. */
9463 Elf_Internal_Sym sym
= isymbuf
[symndx
];
9464 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
9469 name
= bfd_elf_string_from_elf_section (input_bfd
,
9470 symtab_hdr
->sh_link
,
9475 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9477 if (sym
.st_shndx
== SHN_BAD
)
9480 sym
.st_value
+= o
->output_offset
;
9482 indx
= bfd_get_symcount (output_bfd
);
9483 ret
= elf_link_output_sym (flinfo
, name
, &sym
, o
, NULL
);
9487 flinfo
->indices
[symndx
] = indx
;
9491 elf_section_data (osec
)->this_hdr
.sh_info
9492 = flinfo
->indices
[symndx
];
9496 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9497 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9500 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9502 /* Section was created by _bfd_elf_link_create_dynamic_sections
9507 /* Get the contents of the section. They have been cached by a
9508 relaxation routine. Note that o is a section in an input
9509 file, so the contents field will not have been set by any of
9510 the routines which work on output files. */
9511 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9512 contents
= elf_section_data (o
)->this_hdr
.contents
;
9515 contents
= flinfo
->contents
;
9516 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
9520 if ((o
->flags
& SEC_RELOC
) != 0)
9522 Elf_Internal_Rela
*internal_relocs
;
9523 Elf_Internal_Rela
*rel
, *relend
;
9524 int action_discarded
;
9527 /* Get the swapped relocs. */
9529 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
9530 flinfo
->internal_relocs
, FALSE
);
9531 if (internal_relocs
== NULL
9532 && o
->reloc_count
> 0)
9535 /* We need to reverse-copy input .ctors/.dtors sections if
9536 they are placed in .init_array/.finit_array for output. */
9537 if (o
->size
> address_size
9538 && ((strncmp (o
->name
, ".ctors", 6) == 0
9539 && strcmp (o
->output_section
->name
,
9540 ".init_array") == 0)
9541 || (strncmp (o
->name
, ".dtors", 6) == 0
9542 && strcmp (o
->output_section
->name
,
9543 ".fini_array") == 0))
9544 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
9546 if (o
->size
!= o
->reloc_count
* address_size
)
9548 (*_bfd_error_handler
)
9549 (_("error: %B: size of section %A is not "
9550 "multiple of address size"),
9552 bfd_set_error (bfd_error_on_input
);
9555 o
->flags
|= SEC_ELF_REVERSE_COPY
;
9558 action_discarded
= -1;
9559 if (!elf_section_ignore_discarded_relocs (o
))
9560 action_discarded
= (*bed
->action_discarded
) (o
);
9562 /* Run through the relocs evaluating complex reloc symbols and
9563 looking for relocs against symbols from discarded sections
9564 or section symbols from removed link-once sections.
9565 Complain about relocs against discarded sections. Zero
9566 relocs against removed link-once sections. */
9568 rel
= internal_relocs
;
9569 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9570 for ( ; rel
< relend
; rel
++)
9572 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9573 unsigned int s_type
;
9574 asection
**ps
, *sec
;
9575 struct elf_link_hash_entry
*h
= NULL
;
9576 const char *sym_name
;
9578 if (r_symndx
== STN_UNDEF
)
9581 if (r_symndx
>= locsymcount
9582 || (elf_bad_symtab (input_bfd
)
9583 && flinfo
->sections
[r_symndx
] == NULL
))
9585 h
= sym_hashes
[r_symndx
- extsymoff
];
9587 /* Badly formatted input files can contain relocs that
9588 reference non-existant symbols. Check here so that
9589 we do not seg fault. */
9594 sprintf_vma (buffer
, rel
->r_info
);
9595 (*_bfd_error_handler
)
9596 (_("error: %B contains a reloc (0x%s) for section %A "
9597 "that references a non-existent global symbol"),
9598 input_bfd
, o
, buffer
);
9599 bfd_set_error (bfd_error_bad_value
);
9603 while (h
->root
.type
== bfd_link_hash_indirect
9604 || h
->root
.type
== bfd_link_hash_warning
)
9605 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9610 if (h
->root
.type
== bfd_link_hash_defined
9611 || h
->root
.type
== bfd_link_hash_defweak
)
9612 ps
= &h
->root
.u
.def
.section
;
9614 sym_name
= h
->root
.root
.string
;
9618 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9620 s_type
= ELF_ST_TYPE (sym
->st_info
);
9621 ps
= &flinfo
->sections
[r_symndx
];
9622 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
9626 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
9627 && !flinfo
->info
->relocatable
)
9630 bfd_vma dot
= (rel
->r_offset
9631 + o
->output_offset
+ o
->output_section
->vma
);
9633 printf ("Encountered a complex symbol!");
9634 printf (" (input_bfd %s, section %s, reloc %ld\n",
9635 input_bfd
->filename
, o
->name
,
9636 (long) (rel
- internal_relocs
));
9637 printf (" symbol: idx %8.8lx, name %s\n",
9638 r_symndx
, sym_name
);
9639 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9640 (unsigned long) rel
->r_info
,
9641 (unsigned long) rel
->r_offset
);
9643 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
9644 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
9647 /* Symbol evaluated OK. Update to absolute value. */
9648 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
9653 if (action_discarded
!= -1 && ps
!= NULL
)
9655 /* Complain if the definition comes from a
9656 discarded section. */
9657 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
9659 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
9660 if (action_discarded
& COMPLAIN
)
9661 (*flinfo
->info
->callbacks
->einfo
)
9662 (_("%X`%s' referenced in section `%A' of %B: "
9663 "defined in discarded section `%A' of %B\n"),
9664 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9666 /* Try to do the best we can to support buggy old
9667 versions of gcc. Pretend that the symbol is
9668 really defined in the kept linkonce section.
9669 FIXME: This is quite broken. Modifying the
9670 symbol here means we will be changing all later
9671 uses of the symbol, not just in this section. */
9672 if (action_discarded
& PRETEND
)
9676 kept
= _bfd_elf_check_kept_section (sec
,
9688 /* Relocate the section by invoking a back end routine.
9690 The back end routine is responsible for adjusting the
9691 section contents as necessary, and (if using Rela relocs
9692 and generating a relocatable output file) adjusting the
9693 reloc addend as necessary.
9695 The back end routine does not have to worry about setting
9696 the reloc address or the reloc symbol index.
9698 The back end routine is given a pointer to the swapped in
9699 internal symbols, and can access the hash table entries
9700 for the external symbols via elf_sym_hashes (input_bfd).
9702 When generating relocatable output, the back end routine
9703 must handle STB_LOCAL/STT_SECTION symbols specially. The
9704 output symbol is going to be a section symbol
9705 corresponding to the output section, which will require
9706 the addend to be adjusted. */
9708 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
9709 input_bfd
, o
, contents
,
9717 || flinfo
->info
->relocatable
9718 || flinfo
->info
->emitrelocations
)
9720 Elf_Internal_Rela
*irela
;
9721 Elf_Internal_Rela
*irelaend
, *irelamid
;
9722 bfd_vma last_offset
;
9723 struct elf_link_hash_entry
**rel_hash
;
9724 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
9725 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
9726 unsigned int next_erel
;
9727 bfd_boolean rela_normal
;
9728 struct bfd_elf_section_data
*esdi
, *esdo
;
9730 esdi
= elf_section_data (o
);
9731 esdo
= elf_section_data (o
->output_section
);
9732 rela_normal
= FALSE
;
9734 /* Adjust the reloc addresses and symbol indices. */
9736 irela
= internal_relocs
;
9737 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9738 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
9739 /* We start processing the REL relocs, if any. When we reach
9740 IRELAMID in the loop, we switch to the RELA relocs. */
9742 if (esdi
->rel
.hdr
!= NULL
)
9743 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
9744 * bed
->s
->int_rels_per_ext_rel
);
9745 rel_hash_list
= rel_hash
;
9746 rela_hash_list
= NULL
;
9747 last_offset
= o
->output_offset
;
9748 if (!flinfo
->info
->relocatable
)
9749 last_offset
+= o
->output_section
->vma
;
9750 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9752 unsigned long r_symndx
;
9754 Elf_Internal_Sym sym
;
9756 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9762 if (irela
== irelamid
)
9764 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
9765 rela_hash_list
= rel_hash
;
9766 rela_normal
= bed
->rela_normal
;
9769 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9772 if (irela
->r_offset
>= (bfd_vma
) -2)
9774 /* This is a reloc for a deleted entry or somesuch.
9775 Turn it into an R_*_NONE reloc, at the same
9776 offset as the last reloc. elf_eh_frame.c and
9777 bfd_elf_discard_info rely on reloc offsets
9779 irela
->r_offset
= last_offset
;
9781 irela
->r_addend
= 0;
9785 irela
->r_offset
+= o
->output_offset
;
9787 /* Relocs in an executable have to be virtual addresses. */
9788 if (!flinfo
->info
->relocatable
)
9789 irela
->r_offset
+= o
->output_section
->vma
;
9791 last_offset
= irela
->r_offset
;
9793 r_symndx
= irela
->r_info
>> r_sym_shift
;
9794 if (r_symndx
== STN_UNDEF
)
9797 if (r_symndx
>= locsymcount
9798 || (elf_bad_symtab (input_bfd
)
9799 && flinfo
->sections
[r_symndx
] == NULL
))
9801 struct elf_link_hash_entry
*rh
;
9804 /* This is a reloc against a global symbol. We
9805 have not yet output all the local symbols, so
9806 we do not know the symbol index of any global
9807 symbol. We set the rel_hash entry for this
9808 reloc to point to the global hash table entry
9809 for this symbol. The symbol index is then
9810 set at the end of bfd_elf_final_link. */
9811 indx
= r_symndx
- extsymoff
;
9812 rh
= elf_sym_hashes (input_bfd
)[indx
];
9813 while (rh
->root
.type
== bfd_link_hash_indirect
9814 || rh
->root
.type
== bfd_link_hash_warning
)
9815 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9817 /* Setting the index to -2 tells
9818 elf_link_output_extsym that this symbol is
9820 BFD_ASSERT (rh
->indx
< 0);
9828 /* This is a reloc against a local symbol. */
9831 sym
= isymbuf
[r_symndx
];
9832 sec
= flinfo
->sections
[r_symndx
];
9833 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9835 /* I suppose the backend ought to fill in the
9836 section of any STT_SECTION symbol against a
9837 processor specific section. */
9838 r_symndx
= STN_UNDEF
;
9839 if (bfd_is_abs_section (sec
))
9841 else if (sec
== NULL
|| sec
->owner
== NULL
)
9843 bfd_set_error (bfd_error_bad_value
);
9848 asection
*osec
= sec
->output_section
;
9850 /* If we have discarded a section, the output
9851 section will be the absolute section. In
9852 case of discarded SEC_MERGE sections, use
9853 the kept section. relocate_section should
9854 have already handled discarded linkonce
9856 if (bfd_is_abs_section (osec
)
9857 && sec
->kept_section
!= NULL
9858 && sec
->kept_section
->output_section
!= NULL
)
9860 osec
= sec
->kept_section
->output_section
;
9861 irela
->r_addend
-= osec
->vma
;
9864 if (!bfd_is_abs_section (osec
))
9866 r_symndx
= osec
->target_index
;
9867 if (r_symndx
== STN_UNDEF
)
9869 irela
->r_addend
+= osec
->vma
;
9870 osec
= _bfd_nearby_section (output_bfd
, osec
,
9872 irela
->r_addend
-= osec
->vma
;
9873 r_symndx
= osec
->target_index
;
9878 /* Adjust the addend according to where the
9879 section winds up in the output section. */
9881 irela
->r_addend
+= sec
->output_offset
;
9885 if (flinfo
->indices
[r_symndx
] == -1)
9887 unsigned long shlink
;
9892 if (flinfo
->info
->strip
== strip_all
)
9894 /* You can't do ld -r -s. */
9895 bfd_set_error (bfd_error_invalid_operation
);
9899 /* This symbol was skipped earlier, but
9900 since it is needed by a reloc, we
9901 must output it now. */
9902 shlink
= symtab_hdr
->sh_link
;
9903 name
= (bfd_elf_string_from_elf_section
9904 (input_bfd
, shlink
, sym
.st_name
));
9908 osec
= sec
->output_section
;
9910 _bfd_elf_section_from_bfd_section (output_bfd
,
9912 if (sym
.st_shndx
== SHN_BAD
)
9915 sym
.st_value
+= sec
->output_offset
;
9916 if (!flinfo
->info
->relocatable
)
9918 sym
.st_value
+= osec
->vma
;
9919 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9921 /* STT_TLS symbols are relative to PT_TLS
9923 BFD_ASSERT (elf_hash_table (flinfo
->info
)
9925 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
9930 indx
= bfd_get_symcount (output_bfd
);
9931 ret
= elf_link_output_sym (flinfo
, name
, &sym
, sec
,
9936 flinfo
->indices
[r_symndx
] = indx
;
9941 r_symndx
= flinfo
->indices
[r_symndx
];
9944 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9945 | (irela
->r_info
& r_type_mask
));
9948 /* Swap out the relocs. */
9949 input_rel_hdr
= esdi
->rel
.hdr
;
9950 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
9952 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9957 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9958 * bed
->s
->int_rels_per_ext_rel
);
9959 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9962 input_rela_hdr
= esdi
->rela
.hdr
;
9963 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
9965 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9974 /* Write out the modified section contents. */
9975 if (bed
->elf_backend_write_section
9976 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
9979 /* Section written out. */
9981 else switch (o
->sec_info_type
)
9983 case SEC_INFO_TYPE_STABS
:
9984 if (! (_bfd_write_section_stabs
9986 &elf_hash_table (flinfo
->info
)->stab_info
,
9987 o
, &elf_section_data (o
)->sec_info
, contents
)))
9990 case SEC_INFO_TYPE_MERGE
:
9991 if (! _bfd_write_merged_section (output_bfd
, o
,
9992 elf_section_data (o
)->sec_info
))
9995 case SEC_INFO_TYPE_EH_FRAME
:
9997 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10004 /* FIXME: octets_per_byte. */
10005 if (! (o
->flags
& SEC_EXCLUDE
))
10007 file_ptr offset
= (file_ptr
) o
->output_offset
;
10008 bfd_size_type todo
= o
->size
;
10009 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10011 /* Reverse-copy input section to output. */
10014 todo
-= address_size
;
10015 if (! bfd_set_section_contents (output_bfd
,
10023 offset
+= address_size
;
10027 else if (! bfd_set_section_contents (output_bfd
,
10041 /* Generate a reloc when linking an ELF file. This is a reloc
10042 requested by the linker, and does not come from any input file. This
10043 is used to build constructor and destructor tables when linking
10047 elf_reloc_link_order (bfd
*output_bfd
,
10048 struct bfd_link_info
*info
,
10049 asection
*output_section
,
10050 struct bfd_link_order
*link_order
)
10052 reloc_howto_type
*howto
;
10056 struct bfd_elf_section_reloc_data
*reldata
;
10057 struct elf_link_hash_entry
**rel_hash_ptr
;
10058 Elf_Internal_Shdr
*rel_hdr
;
10059 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10060 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10063 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10065 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10068 bfd_set_error (bfd_error_bad_value
);
10072 addend
= link_order
->u
.reloc
.p
->addend
;
10075 reldata
= &esdo
->rel
;
10076 else if (esdo
->rela
.hdr
)
10077 reldata
= &esdo
->rela
;
10084 /* Figure out the symbol index. */
10085 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10086 if (link_order
->type
== bfd_section_reloc_link_order
)
10088 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10089 BFD_ASSERT (indx
!= 0);
10090 *rel_hash_ptr
= NULL
;
10094 struct elf_link_hash_entry
*h
;
10096 /* Treat a reloc against a defined symbol as though it were
10097 actually against the section. */
10098 h
= ((struct elf_link_hash_entry
*)
10099 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10100 link_order
->u
.reloc
.p
->u
.name
,
10101 FALSE
, FALSE
, TRUE
));
10103 && (h
->root
.type
== bfd_link_hash_defined
10104 || h
->root
.type
== bfd_link_hash_defweak
))
10108 section
= h
->root
.u
.def
.section
;
10109 indx
= section
->output_section
->target_index
;
10110 *rel_hash_ptr
= NULL
;
10111 /* It seems that we ought to add the symbol value to the
10112 addend here, but in practice it has already been added
10113 because it was passed to constructor_callback. */
10114 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10116 else if (h
!= NULL
)
10118 /* Setting the index to -2 tells elf_link_output_extsym that
10119 this symbol is used by a reloc. */
10126 if (! ((*info
->callbacks
->unattached_reloc
)
10127 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
10133 /* If this is an inplace reloc, we must write the addend into the
10135 if (howto
->partial_inplace
&& addend
!= 0)
10137 bfd_size_type size
;
10138 bfd_reloc_status_type rstat
;
10141 const char *sym_name
;
10143 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10144 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10147 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10154 case bfd_reloc_outofrange
:
10157 case bfd_reloc_overflow
:
10158 if (link_order
->type
== bfd_section_reloc_link_order
)
10159 sym_name
= bfd_section_name (output_bfd
,
10160 link_order
->u
.reloc
.p
->u
.section
);
10162 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10163 if (! ((*info
->callbacks
->reloc_overflow
)
10164 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
10165 NULL
, (bfd_vma
) 0)))
10172 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10173 link_order
->offset
, size
);
10179 /* The address of a reloc is relative to the section in a
10180 relocatable file, and is a virtual address in an executable
10182 offset
= link_order
->offset
;
10183 if (! info
->relocatable
)
10184 offset
+= output_section
->vma
;
10186 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10188 irel
[i
].r_offset
= offset
;
10189 irel
[i
].r_info
= 0;
10190 irel
[i
].r_addend
= 0;
10192 if (bed
->s
->arch_size
== 32)
10193 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10195 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10197 rel_hdr
= reldata
->hdr
;
10198 erel
= rel_hdr
->contents
;
10199 if (rel_hdr
->sh_type
== SHT_REL
)
10201 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10202 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10206 irel
[0].r_addend
= addend
;
10207 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10208 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10217 /* Get the output vma of the section pointed to by the sh_link field. */
10220 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10222 Elf_Internal_Shdr
**elf_shdrp
;
10226 s
= p
->u
.indirect
.section
;
10227 elf_shdrp
= elf_elfsections (s
->owner
);
10228 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10229 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10231 The Intel C compiler generates SHT_IA_64_UNWIND with
10232 SHF_LINK_ORDER. But it doesn't set the sh_link or
10233 sh_info fields. Hence we could get the situation
10234 where elfsec is 0. */
10237 const struct elf_backend_data
*bed
10238 = get_elf_backend_data (s
->owner
);
10239 if (bed
->link_order_error_handler
)
10240 bed
->link_order_error_handler
10241 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10246 s
= elf_shdrp
[elfsec
]->bfd_section
;
10247 return s
->output_section
->vma
+ s
->output_offset
;
10252 /* Compare two sections based on the locations of the sections they are
10253 linked to. Used by elf_fixup_link_order. */
10256 compare_link_order (const void * a
, const void * b
)
10261 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10262 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10265 return apos
> bpos
;
10269 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10270 order as their linked sections. Returns false if this could not be done
10271 because an output section includes both ordered and unordered
10272 sections. Ideally we'd do this in the linker proper. */
10275 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10277 int seen_linkorder
;
10280 struct bfd_link_order
*p
;
10282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10284 struct bfd_link_order
**sections
;
10285 asection
*s
, *other_sec
, *linkorder_sec
;
10289 linkorder_sec
= NULL
;
10291 seen_linkorder
= 0;
10292 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10294 if (p
->type
== bfd_indirect_link_order
)
10296 s
= p
->u
.indirect
.section
;
10298 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10299 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
10300 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
10301 && elfsec
< elf_numsections (sub
)
10302 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
10303 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
10317 if (seen_other
&& seen_linkorder
)
10319 if (other_sec
&& linkorder_sec
)
10320 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10322 linkorder_sec
->owner
, other_sec
,
10325 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
10327 bfd_set_error (bfd_error_bad_value
);
10332 if (!seen_linkorder
)
10335 sections
= (struct bfd_link_order
**)
10336 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
10337 if (sections
== NULL
)
10339 seen_linkorder
= 0;
10341 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10343 sections
[seen_linkorder
++] = p
;
10345 /* Sort the input sections in the order of their linked section. */
10346 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
10347 compare_link_order
);
10349 /* Change the offsets of the sections. */
10351 for (n
= 0; n
< seen_linkorder
; n
++)
10353 s
= sections
[n
]->u
.indirect
.section
;
10354 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
10355 s
->output_offset
= offset
;
10356 sections
[n
]->offset
= offset
;
10357 /* FIXME: octets_per_byte. */
10358 offset
+= sections
[n
]->size
;
10366 /* Do the final step of an ELF link. */
10369 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10371 bfd_boolean dynamic
;
10372 bfd_boolean emit_relocs
;
10374 struct elf_final_link_info flinfo
;
10376 struct bfd_link_order
*p
;
10378 bfd_size_type max_contents_size
;
10379 bfd_size_type max_external_reloc_size
;
10380 bfd_size_type max_internal_reloc_count
;
10381 bfd_size_type max_sym_count
;
10382 bfd_size_type max_sym_shndx_count
;
10384 Elf_Internal_Sym elfsym
;
10386 Elf_Internal_Shdr
*symtab_hdr
;
10387 Elf_Internal_Shdr
*symtab_shndx_hdr
;
10388 Elf_Internal_Shdr
*symstrtab_hdr
;
10389 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10390 struct elf_outext_info eoinfo
;
10391 bfd_boolean merged
;
10392 size_t relativecount
= 0;
10393 asection
*reldyn
= 0;
10395 asection
*attr_section
= NULL
;
10396 bfd_vma attr_size
= 0;
10397 const char *std_attrs_section
;
10399 if (! is_elf_hash_table (info
->hash
))
10403 abfd
->flags
|= DYNAMIC
;
10405 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
10406 dynobj
= elf_hash_table (info
)->dynobj
;
10408 emit_relocs
= (info
->relocatable
10409 || info
->emitrelocations
);
10411 flinfo
.info
= info
;
10412 flinfo
.output_bfd
= abfd
;
10413 flinfo
.symstrtab
= _bfd_elf_stringtab_init ();
10414 if (flinfo
.symstrtab
== NULL
)
10419 flinfo
.dynsym_sec
= NULL
;
10420 flinfo
.hash_sec
= NULL
;
10421 flinfo
.symver_sec
= NULL
;
10425 flinfo
.dynsym_sec
= bfd_get_linker_section (dynobj
, ".dynsym");
10426 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
10427 /* Note that dynsym_sec can be NULL (on VMS). */
10428 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
10429 /* Note that it is OK if symver_sec is NULL. */
10432 flinfo
.contents
= NULL
;
10433 flinfo
.external_relocs
= NULL
;
10434 flinfo
.internal_relocs
= NULL
;
10435 flinfo
.external_syms
= NULL
;
10436 flinfo
.locsym_shndx
= NULL
;
10437 flinfo
.internal_syms
= NULL
;
10438 flinfo
.indices
= NULL
;
10439 flinfo
.sections
= NULL
;
10440 flinfo
.symbuf
= NULL
;
10441 flinfo
.symshndxbuf
= NULL
;
10442 flinfo
.symbuf_count
= 0;
10443 flinfo
.shndxbuf_size
= 0;
10444 flinfo
.filesym_count
= 0;
10446 /* The object attributes have been merged. Remove the input
10447 sections from the link, and set the contents of the output
10449 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10450 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10452 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10453 || strcmp (o
->name
, ".gnu.attributes") == 0)
10455 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10457 asection
*input_section
;
10459 if (p
->type
!= bfd_indirect_link_order
)
10461 input_section
= p
->u
.indirect
.section
;
10462 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10463 elf_link_input_bfd ignores this section. */
10464 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10467 attr_size
= bfd_elf_obj_attr_size (abfd
);
10470 bfd_set_section_size (abfd
, o
, attr_size
);
10472 /* Skip this section later on. */
10473 o
->map_head
.link_order
= NULL
;
10476 o
->flags
|= SEC_EXCLUDE
;
10480 /* Count up the number of relocations we will output for each output
10481 section, so that we know the sizes of the reloc sections. We
10482 also figure out some maximum sizes. */
10483 max_contents_size
= 0;
10484 max_external_reloc_size
= 0;
10485 max_internal_reloc_count
= 0;
10487 max_sym_shndx_count
= 0;
10489 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10491 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10492 o
->reloc_count
= 0;
10494 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10496 unsigned int reloc_count
= 0;
10497 struct bfd_elf_section_data
*esdi
= NULL
;
10499 if (p
->type
== bfd_section_reloc_link_order
10500 || p
->type
== bfd_symbol_reloc_link_order
)
10502 else if (p
->type
== bfd_indirect_link_order
)
10506 sec
= p
->u
.indirect
.section
;
10507 esdi
= elf_section_data (sec
);
10509 /* Mark all sections which are to be included in the
10510 link. This will normally be every section. We need
10511 to do this so that we can identify any sections which
10512 the linker has decided to not include. */
10513 sec
->linker_mark
= TRUE
;
10515 if (sec
->flags
& SEC_MERGE
)
10518 if (esdo
->this_hdr
.sh_type
== SHT_REL
10519 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
10520 /* Some backends use reloc_count in relocation sections
10521 to count particular types of relocs. Of course,
10522 reloc sections themselves can't have relocations. */
10524 else if (info
->relocatable
|| info
->emitrelocations
)
10525 reloc_count
= sec
->reloc_count
;
10526 else if (bed
->elf_backend_count_relocs
)
10527 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
10529 if (sec
->rawsize
> max_contents_size
)
10530 max_contents_size
= sec
->rawsize
;
10531 if (sec
->size
> max_contents_size
)
10532 max_contents_size
= sec
->size
;
10534 /* We are interested in just local symbols, not all
10536 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10537 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10541 if (elf_bad_symtab (sec
->owner
))
10542 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10543 / bed
->s
->sizeof_sym
);
10545 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10547 if (sym_count
> max_sym_count
)
10548 max_sym_count
= sym_count
;
10550 if (sym_count
> max_sym_shndx_count
10551 && elf_symtab_shndx (sec
->owner
) != 0)
10552 max_sym_shndx_count
= sym_count
;
10554 if ((sec
->flags
& SEC_RELOC
) != 0)
10556 size_t ext_size
= 0;
10558 if (esdi
->rel
.hdr
!= NULL
)
10559 ext_size
= esdi
->rel
.hdr
->sh_size
;
10560 if (esdi
->rela
.hdr
!= NULL
)
10561 ext_size
+= esdi
->rela
.hdr
->sh_size
;
10563 if (ext_size
> max_external_reloc_size
)
10564 max_external_reloc_size
= ext_size
;
10565 if (sec
->reloc_count
> max_internal_reloc_count
)
10566 max_internal_reloc_count
= sec
->reloc_count
;
10571 if (reloc_count
== 0)
10574 o
->reloc_count
+= reloc_count
;
10576 if (p
->type
== bfd_indirect_link_order
10577 && (info
->relocatable
|| info
->emitrelocations
))
10580 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
10581 if (esdi
->rela
.hdr
)
10582 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
10587 esdo
->rela
.count
+= reloc_count
;
10589 esdo
->rel
.count
+= reloc_count
;
10593 if (o
->reloc_count
> 0)
10594 o
->flags
|= SEC_RELOC
;
10597 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10598 set it (this is probably a bug) and if it is set
10599 assign_section_numbers will create a reloc section. */
10600 o
->flags
&=~ SEC_RELOC
;
10603 /* If the SEC_ALLOC flag is not set, force the section VMA to
10604 zero. This is done in elf_fake_sections as well, but forcing
10605 the VMA to 0 here will ensure that relocs against these
10606 sections are handled correctly. */
10607 if ((o
->flags
& SEC_ALLOC
) == 0
10608 && ! o
->user_set_vma
)
10612 if (! info
->relocatable
&& merged
)
10613 elf_link_hash_traverse (elf_hash_table (info
),
10614 _bfd_elf_link_sec_merge_syms
, abfd
);
10616 /* Figure out the file positions for everything but the symbol table
10617 and the relocs. We set symcount to force assign_section_numbers
10618 to create a symbol table. */
10619 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10620 BFD_ASSERT (! abfd
->output_has_begun
);
10621 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10624 /* Set sizes, and assign file positions for reloc sections. */
10625 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10627 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10628 if ((o
->flags
& SEC_RELOC
) != 0)
10631 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
10635 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
10639 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10640 to count upwards while actually outputting the relocations. */
10641 esdo
->rel
.count
= 0;
10642 esdo
->rela
.count
= 0;
10645 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10647 /* We have now assigned file positions for all the sections except
10648 .symtab and .strtab. We start the .symtab section at the current
10649 file position, and write directly to it. We build the .strtab
10650 section in memory. */
10651 bfd_get_symcount (abfd
) = 0;
10652 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10653 /* sh_name is set in prep_headers. */
10654 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10655 /* sh_flags, sh_addr and sh_size all start off zero. */
10656 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10657 /* sh_link is set in assign_section_numbers. */
10658 /* sh_info is set below. */
10659 /* sh_offset is set just below. */
10660 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
10662 off
= elf_tdata (abfd
)->next_file_pos
;
10663 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10665 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10666 incorrect. We do not yet know the size of the .symtab section.
10667 We correct next_file_pos below, after we do know the size. */
10669 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10670 continuously seeking to the right position in the file. */
10671 if (! info
->keep_memory
|| max_sym_count
< 20)
10672 flinfo
.symbuf_size
= 20;
10674 flinfo
.symbuf_size
= max_sym_count
;
10675 amt
= flinfo
.symbuf_size
;
10676 amt
*= bed
->s
->sizeof_sym
;
10677 flinfo
.symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10678 if (flinfo
.symbuf
== NULL
)
10680 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
10682 /* Wild guess at number of output symbols. realloc'd as needed. */
10683 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10684 flinfo
.shndxbuf_size
= amt
;
10685 amt
*= sizeof (Elf_External_Sym_Shndx
);
10686 flinfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10687 if (flinfo
.symshndxbuf
== NULL
)
10691 /* Start writing out the symbol table. The first symbol is always a
10693 if (info
->strip
!= strip_all
10696 elfsym
.st_value
= 0;
10697 elfsym
.st_size
= 0;
10698 elfsym
.st_info
= 0;
10699 elfsym
.st_other
= 0;
10700 elfsym
.st_shndx
= SHN_UNDEF
;
10701 elfsym
.st_target_internal
= 0;
10702 if (elf_link_output_sym (&flinfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10707 /* Output a symbol for each section. We output these even if we are
10708 discarding local symbols, since they are used for relocs. These
10709 symbols have no names. We store the index of each one in the
10710 index field of the section, so that we can find it again when
10711 outputting relocs. */
10712 if (info
->strip
!= strip_all
10715 elfsym
.st_size
= 0;
10716 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10717 elfsym
.st_other
= 0;
10718 elfsym
.st_value
= 0;
10719 elfsym
.st_target_internal
= 0;
10720 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10722 o
= bfd_section_from_elf_index (abfd
, i
);
10725 o
->target_index
= bfd_get_symcount (abfd
);
10726 elfsym
.st_shndx
= i
;
10727 if (!info
->relocatable
)
10728 elfsym
.st_value
= o
->vma
;
10729 if (elf_link_output_sym (&flinfo
, NULL
, &elfsym
, o
, NULL
) != 1)
10735 /* Allocate some memory to hold information read in from the input
10737 if (max_contents_size
!= 0)
10739 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
10740 if (flinfo
.contents
== NULL
)
10744 if (max_external_reloc_size
!= 0)
10746 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10747 if (flinfo
.external_relocs
== NULL
)
10751 if (max_internal_reloc_count
!= 0)
10753 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10754 amt
*= sizeof (Elf_Internal_Rela
);
10755 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
10756 if (flinfo
.internal_relocs
== NULL
)
10760 if (max_sym_count
!= 0)
10762 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10763 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
10764 if (flinfo
.external_syms
== NULL
)
10767 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10768 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
10769 if (flinfo
.internal_syms
== NULL
)
10772 amt
= max_sym_count
* sizeof (long);
10773 flinfo
.indices
= (long int *) bfd_malloc (amt
);
10774 if (flinfo
.indices
== NULL
)
10777 amt
= max_sym_count
* sizeof (asection
*);
10778 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
10779 if (flinfo
.sections
== NULL
)
10783 if (max_sym_shndx_count
!= 0)
10785 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10786 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
10787 if (flinfo
.locsym_shndx
== NULL
)
10791 if (elf_hash_table (info
)->tls_sec
)
10793 bfd_vma base
, end
= 0;
10796 for (sec
= elf_hash_table (info
)->tls_sec
;
10797 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10800 bfd_size_type size
= sec
->size
;
10803 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10805 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
10808 size
= ord
->offset
+ ord
->size
;
10810 end
= sec
->vma
+ size
;
10812 base
= elf_hash_table (info
)->tls_sec
->vma
;
10813 /* Only align end of TLS section if static TLS doesn't have special
10814 alignment requirements. */
10815 if (bed
->static_tls_alignment
== 1)
10816 end
= align_power (end
,
10817 elf_hash_table (info
)->tls_sec
->alignment_power
);
10818 elf_hash_table (info
)->tls_size
= end
- base
;
10821 /* Reorder SHF_LINK_ORDER sections. */
10822 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10824 if (!elf_fixup_link_order (abfd
, o
))
10828 /* Since ELF permits relocations to be against local symbols, we
10829 must have the local symbols available when we do the relocations.
10830 Since we would rather only read the local symbols once, and we
10831 would rather not keep them in memory, we handle all the
10832 relocations for a single input file at the same time.
10834 Unfortunately, there is no way to know the total number of local
10835 symbols until we have seen all of them, and the local symbol
10836 indices precede the global symbol indices. This means that when
10837 we are generating relocatable output, and we see a reloc against
10838 a global symbol, we can not know the symbol index until we have
10839 finished examining all the local symbols to see which ones we are
10840 going to output. To deal with this, we keep the relocations in
10841 memory, and don't output them until the end of the link. This is
10842 an unfortunate waste of memory, but I don't see a good way around
10843 it. Fortunately, it only happens when performing a relocatable
10844 link, which is not the common case. FIXME: If keep_memory is set
10845 we could write the relocs out and then read them again; I don't
10846 know how bad the memory loss will be. */
10848 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10849 sub
->output_has_begun
= FALSE
;
10850 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10852 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10854 if (p
->type
== bfd_indirect_link_order
10855 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10856 == bfd_target_elf_flavour
)
10857 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10859 if (! sub
->output_has_begun
)
10861 if (! elf_link_input_bfd (&flinfo
, sub
))
10863 sub
->output_has_begun
= TRUE
;
10866 else if (p
->type
== bfd_section_reloc_link_order
10867 || p
->type
== bfd_symbol_reloc_link_order
)
10869 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10874 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10876 if (p
->type
== bfd_indirect_link_order
10877 && (bfd_get_flavour (sub
)
10878 == bfd_target_elf_flavour
)
10879 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
10880 != bed
->s
->elfclass
))
10882 const char *iclass
, *oclass
;
10884 if (bed
->s
->elfclass
== ELFCLASS64
)
10886 iclass
= "ELFCLASS32";
10887 oclass
= "ELFCLASS64";
10891 iclass
= "ELFCLASS64";
10892 oclass
= "ELFCLASS32";
10895 bfd_set_error (bfd_error_wrong_format
);
10896 (*_bfd_error_handler
)
10897 (_("%B: file class %s incompatible with %s"),
10898 sub
, iclass
, oclass
);
10907 /* Free symbol buffer if needed. */
10908 if (!info
->reduce_memory_overheads
)
10910 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10911 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10912 && elf_tdata (sub
)->symbuf
)
10914 free (elf_tdata (sub
)->symbuf
);
10915 elf_tdata (sub
)->symbuf
= NULL
;
10919 /* Output a FILE symbol so that following locals are not associated
10920 with the wrong input file. */
10921 memset (&elfsym
, 0, sizeof (elfsym
));
10922 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10923 elfsym
.st_shndx
= SHN_ABS
;
10925 if (flinfo
.filesym_count
> 1
10926 && !elf_link_output_sym (&flinfo
, NULL
, &elfsym
,
10927 bfd_und_section_ptr
, NULL
))
10930 /* Output any global symbols that got converted to local in a
10931 version script or due to symbol visibility. We do this in a
10932 separate step since ELF requires all local symbols to appear
10933 prior to any global symbols. FIXME: We should only do this if
10934 some global symbols were, in fact, converted to become local.
10935 FIXME: Will this work correctly with the Irix 5 linker? */
10936 eoinfo
.failed
= FALSE
;
10937 eoinfo
.flinfo
= &flinfo
;
10938 eoinfo
.localsyms
= TRUE
;
10939 eoinfo
.need_second_pass
= FALSE
;
10940 eoinfo
.second_pass
= FALSE
;
10941 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
10945 if (flinfo
.filesym_count
== 1
10946 && !elf_link_output_sym (&flinfo
, NULL
, &elfsym
,
10947 bfd_und_section_ptr
, NULL
))
10950 if (eoinfo
.need_second_pass
)
10952 eoinfo
.second_pass
= TRUE
;
10953 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
10958 /* If backend needs to output some local symbols not present in the hash
10959 table, do it now. */
10960 if (bed
->elf_backend_output_arch_local_syms
)
10962 typedef int (*out_sym_func
)
10963 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10964 struct elf_link_hash_entry
*);
10966 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10967 (abfd
, info
, &flinfo
, (out_sym_func
) elf_link_output_sym
)))
10971 /* That wrote out all the local symbols. Finish up the symbol table
10972 with the global symbols. Even if we want to strip everything we
10973 can, we still need to deal with those global symbols that got
10974 converted to local in a version script. */
10976 /* The sh_info field records the index of the first non local symbol. */
10977 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10980 && flinfo
.dynsym_sec
!= NULL
10981 && flinfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10983 Elf_Internal_Sym sym
;
10984 bfd_byte
*dynsym
= flinfo
.dynsym_sec
->contents
;
10985 long last_local
= 0;
10987 /* Write out the section symbols for the output sections. */
10988 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10994 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10996 sym
.st_target_internal
= 0;
10998 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11004 dynindx
= elf_section_data (s
)->dynindx
;
11007 indx
= elf_section_data (s
)->this_idx
;
11008 BFD_ASSERT (indx
> 0);
11009 sym
.st_shndx
= indx
;
11010 if (! check_dynsym (abfd
, &sym
))
11012 sym
.st_value
= s
->vma
;
11013 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11014 if (last_local
< dynindx
)
11015 last_local
= dynindx
;
11016 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11020 /* Write out the local dynsyms. */
11021 if (elf_hash_table (info
)->dynlocal
)
11023 struct elf_link_local_dynamic_entry
*e
;
11024 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
11029 /* Copy the internal symbol and turn off visibility.
11030 Note that we saved a word of storage and overwrote
11031 the original st_name with the dynstr_index. */
11033 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11035 s
= bfd_section_from_elf_index (e
->input_bfd
,
11040 elf_section_data (s
->output_section
)->this_idx
;
11041 if (! check_dynsym (abfd
, &sym
))
11043 sym
.st_value
= (s
->output_section
->vma
11045 + e
->isym
.st_value
);
11048 if (last_local
< e
->dynindx
)
11049 last_local
= e
->dynindx
;
11051 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11052 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11056 elf_section_data (flinfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
11060 /* We get the global symbols from the hash table. */
11061 eoinfo
.failed
= FALSE
;
11062 eoinfo
.localsyms
= FALSE
;
11063 eoinfo
.flinfo
= &flinfo
;
11064 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11068 /* If backend needs to output some symbols not present in the hash
11069 table, do it now. */
11070 if (bed
->elf_backend_output_arch_syms
)
11072 typedef int (*out_sym_func
)
11073 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11074 struct elf_link_hash_entry
*);
11076 if (! ((*bed
->elf_backend_output_arch_syms
)
11077 (abfd
, info
, &flinfo
, (out_sym_func
) elf_link_output_sym
)))
11081 /* Flush all symbols to the file. */
11082 if (! elf_link_flush_output_syms (&flinfo
, bed
))
11085 /* Now we know the size of the symtab section. */
11086 off
+= symtab_hdr
->sh_size
;
11088 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
11089 if (symtab_shndx_hdr
->sh_name
!= 0)
11091 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11092 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11093 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11094 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11095 symtab_shndx_hdr
->sh_size
= amt
;
11097 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11100 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11101 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11106 /* Finish up and write out the symbol string table (.strtab)
11108 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11109 /* sh_name was set in prep_headers. */
11110 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11111 symstrtab_hdr
->sh_flags
= 0;
11112 symstrtab_hdr
->sh_addr
= 0;
11113 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (flinfo
.symstrtab
);
11114 symstrtab_hdr
->sh_entsize
= 0;
11115 symstrtab_hdr
->sh_link
= 0;
11116 symstrtab_hdr
->sh_info
= 0;
11117 /* sh_offset is set just below. */
11118 symstrtab_hdr
->sh_addralign
= 1;
11120 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
11121 elf_tdata (abfd
)->next_file_pos
= off
;
11123 if (bfd_get_symcount (abfd
) > 0)
11125 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11126 || ! _bfd_stringtab_emit (abfd
, flinfo
.symstrtab
))
11130 /* Adjust the relocs to have the correct symbol indices. */
11131 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11133 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11134 if ((o
->flags
& SEC_RELOC
) == 0)
11137 if (esdo
->rel
.hdr
!= NULL
)
11138 elf_link_adjust_relocs (abfd
, &esdo
->rel
);
11139 if (esdo
->rela
.hdr
!= NULL
)
11140 elf_link_adjust_relocs (abfd
, &esdo
->rela
);
11142 /* Set the reloc_count field to 0 to prevent write_relocs from
11143 trying to swap the relocs out itself. */
11144 o
->reloc_count
= 0;
11147 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11148 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
11150 /* If we are linking against a dynamic object, or generating a
11151 shared library, finish up the dynamic linking information. */
11154 bfd_byte
*dyncon
, *dynconend
;
11156 /* Fix up .dynamic entries. */
11157 o
= bfd_get_linker_section (dynobj
, ".dynamic");
11158 BFD_ASSERT (o
!= NULL
);
11160 dyncon
= o
->contents
;
11161 dynconend
= o
->contents
+ o
->size
;
11162 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11164 Elf_Internal_Dyn dyn
;
11168 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11175 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
11177 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
11179 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
11180 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
11183 dyn
.d_un
.d_val
= relativecount
;
11190 name
= info
->init_function
;
11193 name
= info
->fini_function
;
11196 struct elf_link_hash_entry
*h
;
11198 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
11199 FALSE
, FALSE
, TRUE
);
11201 && (h
->root
.type
== bfd_link_hash_defined
11202 || h
->root
.type
== bfd_link_hash_defweak
))
11204 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
11205 o
= h
->root
.u
.def
.section
;
11206 if (o
->output_section
!= NULL
)
11207 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
11208 + o
->output_offset
);
11211 /* The symbol is imported from another shared
11212 library and does not apply to this one. */
11213 dyn
.d_un
.d_ptr
= 0;
11220 case DT_PREINIT_ARRAYSZ
:
11221 name
= ".preinit_array";
11223 case DT_INIT_ARRAYSZ
:
11224 name
= ".init_array";
11226 case DT_FINI_ARRAYSZ
:
11227 name
= ".fini_array";
11229 o
= bfd_get_section_by_name (abfd
, name
);
11232 (*_bfd_error_handler
)
11233 (_("%B: could not find output section %s"), abfd
, name
);
11237 (*_bfd_error_handler
)
11238 (_("warning: %s section has zero size"), name
);
11239 dyn
.d_un
.d_val
= o
->size
;
11242 case DT_PREINIT_ARRAY
:
11243 name
= ".preinit_array";
11245 case DT_INIT_ARRAY
:
11246 name
= ".init_array";
11248 case DT_FINI_ARRAY
:
11249 name
= ".fini_array";
11256 name
= ".gnu.hash";
11265 name
= ".gnu.version_d";
11268 name
= ".gnu.version_r";
11271 name
= ".gnu.version";
11273 o
= bfd_get_section_by_name (abfd
, name
);
11276 (*_bfd_error_handler
)
11277 (_("%B: could not find output section %s"), abfd
, name
);
11280 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
11282 (*_bfd_error_handler
)
11283 (_("warning: section '%s' is being made into a note"), name
);
11284 bfd_set_error (bfd_error_nonrepresentable_section
);
11287 dyn
.d_un
.d_ptr
= o
->vma
;
11294 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
11298 dyn
.d_un
.d_val
= 0;
11299 dyn
.d_un
.d_ptr
= 0;
11300 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11302 Elf_Internal_Shdr
*hdr
;
11304 hdr
= elf_elfsections (abfd
)[i
];
11305 if (hdr
->sh_type
== type
11306 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
11308 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
11309 dyn
.d_un
.d_val
+= hdr
->sh_size
;
11312 if (dyn
.d_un
.d_ptr
== 0
11313 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
11314 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
11320 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
11324 /* If we have created any dynamic sections, then output them. */
11325 if (dynobj
!= NULL
)
11327 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
11330 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11331 if (((info
->warn_shared_textrel
&& info
->shared
)
11332 || info
->error_textrel
)
11333 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
11335 bfd_byte
*dyncon
, *dynconend
;
11337 dyncon
= o
->contents
;
11338 dynconend
= o
->contents
+ o
->size
;
11339 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11341 Elf_Internal_Dyn dyn
;
11343 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11345 if (dyn
.d_tag
== DT_TEXTREL
)
11347 if (info
->error_textrel
)
11348 info
->callbacks
->einfo
11349 (_("%P%X: read-only segment has dynamic relocations.\n"));
11351 info
->callbacks
->einfo
11352 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11358 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
11360 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11362 || o
->output_section
== bfd_abs_section_ptr
)
11364 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
11366 /* At this point, we are only interested in sections
11367 created by _bfd_elf_link_create_dynamic_sections. */
11370 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
11372 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
11374 if (strcmp (o
->name
, ".dynstr") != 0)
11376 /* FIXME: octets_per_byte. */
11377 if (! bfd_set_section_contents (abfd
, o
->output_section
,
11379 (file_ptr
) o
->output_offset
,
11385 /* The contents of the .dynstr section are actually in a
11387 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
11388 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
11389 || ! _bfd_elf_strtab_emit (abfd
,
11390 elf_hash_table (info
)->dynstr
))
11396 if (info
->relocatable
)
11398 bfd_boolean failed
= FALSE
;
11400 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
11405 /* If we have optimized stabs strings, output them. */
11406 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
11408 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
11412 if (info
->eh_frame_hdr
)
11414 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
11418 if (flinfo
.symstrtab
!= NULL
)
11419 _bfd_stringtab_free (flinfo
.symstrtab
);
11420 if (flinfo
.contents
!= NULL
)
11421 free (flinfo
.contents
);
11422 if (flinfo
.external_relocs
!= NULL
)
11423 free (flinfo
.external_relocs
);
11424 if (flinfo
.internal_relocs
!= NULL
)
11425 free (flinfo
.internal_relocs
);
11426 if (flinfo
.external_syms
!= NULL
)
11427 free (flinfo
.external_syms
);
11428 if (flinfo
.locsym_shndx
!= NULL
)
11429 free (flinfo
.locsym_shndx
);
11430 if (flinfo
.internal_syms
!= NULL
)
11431 free (flinfo
.internal_syms
);
11432 if (flinfo
.indices
!= NULL
)
11433 free (flinfo
.indices
);
11434 if (flinfo
.sections
!= NULL
)
11435 free (flinfo
.sections
);
11436 if (flinfo
.symbuf
!= NULL
)
11437 free (flinfo
.symbuf
);
11438 if (flinfo
.symshndxbuf
!= NULL
)
11439 free (flinfo
.symshndxbuf
);
11440 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11442 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11443 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11444 free (esdo
->rel
.hashes
);
11445 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11446 free (esdo
->rela
.hashes
);
11449 elf_tdata (abfd
)->linker
= TRUE
;
11453 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
11454 if (contents
== NULL
)
11455 return FALSE
; /* Bail out and fail. */
11456 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11457 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11464 if (flinfo
.symstrtab
!= NULL
)
11465 _bfd_stringtab_free (flinfo
.symstrtab
);
11466 if (flinfo
.contents
!= NULL
)
11467 free (flinfo
.contents
);
11468 if (flinfo
.external_relocs
!= NULL
)
11469 free (flinfo
.external_relocs
);
11470 if (flinfo
.internal_relocs
!= NULL
)
11471 free (flinfo
.internal_relocs
);
11472 if (flinfo
.external_syms
!= NULL
)
11473 free (flinfo
.external_syms
);
11474 if (flinfo
.locsym_shndx
!= NULL
)
11475 free (flinfo
.locsym_shndx
);
11476 if (flinfo
.internal_syms
!= NULL
)
11477 free (flinfo
.internal_syms
);
11478 if (flinfo
.indices
!= NULL
)
11479 free (flinfo
.indices
);
11480 if (flinfo
.sections
!= NULL
)
11481 free (flinfo
.sections
);
11482 if (flinfo
.symbuf
!= NULL
)
11483 free (flinfo
.symbuf
);
11484 if (flinfo
.symshndxbuf
!= NULL
)
11485 free (flinfo
.symshndxbuf
);
11486 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11488 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11489 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11490 free (esdo
->rel
.hashes
);
11491 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11492 free (esdo
->rela
.hashes
);
11498 /* Initialize COOKIE for input bfd ABFD. */
11501 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
11502 struct bfd_link_info
*info
, bfd
*abfd
)
11504 Elf_Internal_Shdr
*symtab_hdr
;
11505 const struct elf_backend_data
*bed
;
11507 bed
= get_elf_backend_data (abfd
);
11508 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11510 cookie
->abfd
= abfd
;
11511 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
11512 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
11513 if (cookie
->bad_symtab
)
11515 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11516 cookie
->extsymoff
= 0;
11520 cookie
->locsymcount
= symtab_hdr
->sh_info
;
11521 cookie
->extsymoff
= symtab_hdr
->sh_info
;
11524 if (bed
->s
->arch_size
== 32)
11525 cookie
->r_sym_shift
= 8;
11527 cookie
->r_sym_shift
= 32;
11529 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11530 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
11532 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11533 cookie
->locsymcount
, 0,
11535 if (cookie
->locsyms
== NULL
)
11537 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11540 if (info
->keep_memory
)
11541 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
11546 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11549 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
11551 Elf_Internal_Shdr
*symtab_hdr
;
11553 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11554 if (cookie
->locsyms
!= NULL
11555 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
11556 free (cookie
->locsyms
);
11559 /* Initialize the relocation information in COOKIE for input section SEC
11560 of input bfd ABFD. */
11563 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11564 struct bfd_link_info
*info
, bfd
*abfd
,
11567 const struct elf_backend_data
*bed
;
11569 if (sec
->reloc_count
== 0)
11571 cookie
->rels
= NULL
;
11572 cookie
->relend
= NULL
;
11576 bed
= get_elf_backend_data (abfd
);
11578 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
11579 info
->keep_memory
);
11580 if (cookie
->rels
== NULL
)
11582 cookie
->rel
= cookie
->rels
;
11583 cookie
->relend
= (cookie
->rels
11584 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
11586 cookie
->rel
= cookie
->rels
;
11590 /* Free the memory allocated by init_reloc_cookie_rels,
11594 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11597 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
11598 free (cookie
->rels
);
11601 /* Initialize the whole of COOKIE for input section SEC. */
11604 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11605 struct bfd_link_info
*info
,
11608 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
11610 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
11615 fini_reloc_cookie (cookie
, sec
->owner
);
11620 /* Free the memory allocated by init_reloc_cookie_for_section,
11624 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11627 fini_reloc_cookie_rels (cookie
, sec
);
11628 fini_reloc_cookie (cookie
, sec
->owner
);
11631 /* Garbage collect unused sections. */
11633 /* Default gc_mark_hook. */
11636 _bfd_elf_gc_mark_hook (asection
*sec
,
11637 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11638 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11639 struct elf_link_hash_entry
*h
,
11640 Elf_Internal_Sym
*sym
)
11642 const char *sec_name
;
11646 switch (h
->root
.type
)
11648 case bfd_link_hash_defined
:
11649 case bfd_link_hash_defweak
:
11650 return h
->root
.u
.def
.section
;
11652 case bfd_link_hash_common
:
11653 return h
->root
.u
.c
.p
->section
;
11655 case bfd_link_hash_undefined
:
11656 case bfd_link_hash_undefweak
:
11657 /* To work around a glibc bug, keep all XXX input sections
11658 when there is an as yet undefined reference to __start_XXX
11659 or __stop_XXX symbols. The linker will later define such
11660 symbols for orphan input sections that have a name
11661 representable as a C identifier. */
11662 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
11663 sec_name
= h
->root
.root
.string
+ 8;
11664 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
11665 sec_name
= h
->root
.root
.string
+ 7;
11669 if (sec_name
&& *sec_name
!= '\0')
11673 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11675 sec
= bfd_get_section_by_name (i
, sec_name
);
11677 sec
->flags
|= SEC_KEEP
;
11687 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11692 /* COOKIE->rel describes a relocation against section SEC, which is
11693 a section we've decided to keep. Return the section that contains
11694 the relocation symbol, or NULL if no section contains it. */
11697 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
11698 elf_gc_mark_hook_fn gc_mark_hook
,
11699 struct elf_reloc_cookie
*cookie
)
11701 unsigned long r_symndx
;
11702 struct elf_link_hash_entry
*h
;
11704 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
11705 if (r_symndx
== STN_UNDEF
)
11708 if (r_symndx
>= cookie
->locsymcount
11709 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11711 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
11712 while (h
->root
.type
== bfd_link_hash_indirect
11713 || h
->root
.type
== bfd_link_hash_warning
)
11714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11716 /* If this symbol is weak and there is a non-weak definition, we
11717 keep the non-weak definition because many backends put
11718 dynamic reloc info on the non-weak definition for code
11719 handling copy relocs. */
11720 if (h
->u
.weakdef
!= NULL
)
11721 h
->u
.weakdef
->mark
= 1;
11722 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
11725 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
11726 &cookie
->locsyms
[r_symndx
]);
11729 /* COOKIE->rel describes a relocation against section SEC, which is
11730 a section we've decided to keep. Mark the section that contains
11731 the relocation symbol. */
11734 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
11736 elf_gc_mark_hook_fn gc_mark_hook
,
11737 struct elf_reloc_cookie
*cookie
)
11741 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
11742 if (rsec
&& !rsec
->gc_mark
)
11744 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
11745 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
11747 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11753 /* The mark phase of garbage collection. For a given section, mark
11754 it and any sections in this section's group, and all the sections
11755 which define symbols to which it refers. */
11758 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11760 elf_gc_mark_hook_fn gc_mark_hook
)
11763 asection
*group_sec
, *eh_frame
;
11767 /* Mark all the sections in the group. */
11768 group_sec
= elf_section_data (sec
)->next_in_group
;
11769 if (group_sec
&& !group_sec
->gc_mark
)
11770 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11773 /* Look through the section relocs. */
11775 eh_frame
= elf_eh_frame_section (sec
->owner
);
11776 if ((sec
->flags
& SEC_RELOC
) != 0
11777 && sec
->reloc_count
> 0
11778 && sec
!= eh_frame
)
11780 struct elf_reloc_cookie cookie
;
11782 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
11786 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
11787 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
11792 fini_reloc_cookie_for_section (&cookie
, sec
);
11796 if (ret
&& eh_frame
&& elf_fde_list (sec
))
11798 struct elf_reloc_cookie cookie
;
11800 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
11804 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
11805 gc_mark_hook
, &cookie
))
11807 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
11814 /* Keep debug and special sections. */
11817 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
11818 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
11822 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
11825 bfd_boolean some_kept
;
11827 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
11830 /* Ensure all linker created sections are kept, and see whether
11831 any other section is already marked. */
11833 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
11835 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
11837 else if (isec
->gc_mark
)
11841 /* If no section in this file will be kept, then we can
11842 toss out debug sections. */
11846 /* Keep debug and special sections like .comment when they are
11847 not part of a group, or when we have single-member groups. */
11848 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
11849 if ((elf_next_in_group (isec
) == NULL
11850 || elf_next_in_group (isec
) == isec
)
11851 && ((isec
->flags
& SEC_DEBUGGING
) != 0
11852 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0))
11858 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11860 struct elf_gc_sweep_symbol_info
11862 struct bfd_link_info
*info
;
11863 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11868 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11871 && (((h
->root
.type
== bfd_link_hash_defined
11872 || h
->root
.type
== bfd_link_hash_defweak
)
11873 && !(h
->def_regular
11874 && h
->root
.u
.def
.section
->gc_mark
))
11875 || h
->root
.type
== bfd_link_hash_undefined
11876 || h
->root
.type
== bfd_link_hash_undefweak
))
11878 struct elf_gc_sweep_symbol_info
*inf
;
11880 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
11881 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11882 h
->def_regular
= 0;
11883 h
->ref_regular
= 0;
11884 h
->ref_regular_nonweak
= 0;
11890 /* The sweep phase of garbage collection. Remove all garbage sections. */
11892 typedef bfd_boolean (*gc_sweep_hook_fn
)
11893 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11896 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11899 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11900 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11901 unsigned long section_sym_count
;
11902 struct elf_gc_sweep_symbol_info sweep_info
;
11904 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11908 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11911 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11913 /* When any section in a section group is kept, we keep all
11914 sections in the section group. If the first member of
11915 the section group is excluded, we will also exclude the
11917 if (o
->flags
& SEC_GROUP
)
11919 asection
*first
= elf_next_in_group (o
);
11920 o
->gc_mark
= first
->gc_mark
;
11926 /* Skip sweeping sections already excluded. */
11927 if (o
->flags
& SEC_EXCLUDE
)
11930 /* Since this is early in the link process, it is simple
11931 to remove a section from the output. */
11932 o
->flags
|= SEC_EXCLUDE
;
11934 if (info
->print_gc_sections
&& o
->size
!= 0)
11935 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11937 /* But we also have to update some of the relocation
11938 info we collected before. */
11940 && (o
->flags
& SEC_RELOC
) != 0
11941 && o
->reloc_count
> 0
11942 && !bfd_is_abs_section (o
->output_section
))
11944 Elf_Internal_Rela
*internal_relocs
;
11948 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11949 info
->keep_memory
);
11950 if (internal_relocs
== NULL
)
11953 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11955 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11956 free (internal_relocs
);
11964 /* Remove the symbols that were in the swept sections from the dynamic
11965 symbol table. GCFIXME: Anyone know how to get them out of the
11966 static symbol table as well? */
11967 sweep_info
.info
= info
;
11968 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11969 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11972 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11976 /* Propagate collected vtable information. This is called through
11977 elf_link_hash_traverse. */
11980 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11982 /* Those that are not vtables. */
11983 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11986 /* Those vtables that do not have parents, we cannot merge. */
11987 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11990 /* If we've already been done, exit. */
11991 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11994 /* Make sure the parent's table is up to date. */
11995 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11997 if (h
->vtable
->used
== NULL
)
11999 /* None of this table's entries were referenced. Re-use the
12001 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12002 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12007 bfd_boolean
*cu
, *pu
;
12009 /* Or the parent's entries into ours. */
12010 cu
= h
->vtable
->used
;
12012 pu
= h
->vtable
->parent
->vtable
->used
;
12015 const struct elf_backend_data
*bed
;
12016 unsigned int log_file_align
;
12018 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12019 log_file_align
= bed
->s
->log_file_align
;
12020 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12035 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12038 bfd_vma hstart
, hend
;
12039 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12040 const struct elf_backend_data
*bed
;
12041 unsigned int log_file_align
;
12043 /* Take care of both those symbols that do not describe vtables as
12044 well as those that are not loaded. */
12045 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12048 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
12049 || h
->root
.type
== bfd_link_hash_defweak
);
12051 sec
= h
->root
.u
.def
.section
;
12052 hstart
= h
->root
.u
.def
.value
;
12053 hend
= hstart
+ h
->size
;
12055 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
12057 return *(bfd_boolean
*) okp
= FALSE
;
12058 bed
= get_elf_backend_data (sec
->owner
);
12059 log_file_align
= bed
->s
->log_file_align
;
12061 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
12063 for (rel
= relstart
; rel
< relend
; ++rel
)
12064 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
12066 /* If the entry is in use, do nothing. */
12067 if (h
->vtable
->used
12068 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
12070 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
12071 if (h
->vtable
->used
[entry
])
12074 /* Otherwise, kill it. */
12075 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
12081 /* Mark sections containing dynamically referenced symbols. When
12082 building shared libraries, we must assume that any visible symbol is
12086 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
12088 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
12090 if ((h
->root
.type
== bfd_link_hash_defined
12091 || h
->root
.type
== bfd_link_hash_defweak
)
12093 || ((!info
->executable
|| info
->export_dynamic
)
12095 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
12096 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
12097 && (strchr (h
->root
.root
.string
, ELF_VER_CHR
) != NULL
12098 || !bfd_hide_sym_by_version (info
->version_info
,
12099 h
->root
.root
.string
)))))
12100 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
12105 /* Keep all sections containing symbols undefined on the command-line,
12106 and the section containing the entry symbol. */
12109 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
12111 struct bfd_sym_chain
*sym
;
12113 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
12115 struct elf_link_hash_entry
*h
;
12117 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
12118 FALSE
, FALSE
, FALSE
);
12121 && (h
->root
.type
== bfd_link_hash_defined
12122 || h
->root
.type
== bfd_link_hash_defweak
)
12123 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
12124 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
12128 /* Do mark and sweep of unused sections. */
12131 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
12133 bfd_boolean ok
= TRUE
;
12135 elf_gc_mark_hook_fn gc_mark_hook
;
12136 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12138 if (!bed
->can_gc_sections
12139 || !is_elf_hash_table (info
->hash
))
12141 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
12145 bed
->gc_keep (info
);
12147 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12148 at the .eh_frame section if we can mark the FDEs individually. */
12149 _bfd_elf_begin_eh_frame_parsing (info
);
12150 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
12153 struct elf_reloc_cookie cookie
;
12155 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
12156 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
12158 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
12159 if (elf_section_data (sec
)->sec_info
12160 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
12161 elf_eh_frame_section (sub
) = sec
;
12162 fini_reloc_cookie_for_section (&cookie
, sec
);
12163 sec
= bfd_get_next_section_by_name (sec
);
12166 _bfd_elf_end_eh_frame_parsing (info
);
12168 /* Apply transitive closure to the vtable entry usage info. */
12169 elf_link_hash_traverse (elf_hash_table (info
),
12170 elf_gc_propagate_vtable_entries_used
,
12175 /* Kill the vtable relocations that were not used. */
12176 elf_link_hash_traverse (elf_hash_table (info
),
12177 elf_gc_smash_unused_vtentry_relocs
,
12182 /* Mark dynamically referenced symbols. */
12183 if (elf_hash_table (info
)->dynamic_sections_created
)
12184 elf_link_hash_traverse (elf_hash_table (info
),
12185 bed
->gc_mark_dynamic_ref
,
12188 /* Grovel through relocs to find out who stays ... */
12189 gc_mark_hook
= bed
->gc_mark_hook
;
12190 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
12194 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
12197 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12198 Also treat note sections as a root, if the section is not part
12200 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12202 && (o
->flags
& SEC_EXCLUDE
) == 0
12203 && ((o
->flags
& SEC_KEEP
) != 0
12204 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
12205 && elf_next_in_group (o
) == NULL
)))
12207 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
12212 /* Allow the backend to mark additional target specific sections. */
12213 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
12215 /* ... and mark SEC_EXCLUDE for those that go. */
12216 return elf_gc_sweep (abfd
, info
);
12219 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12222 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
12224 struct elf_link_hash_entry
*h
,
12227 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
12228 struct elf_link_hash_entry
**search
, *child
;
12229 bfd_size_type extsymcount
;
12230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12232 /* The sh_info field of the symtab header tells us where the
12233 external symbols start. We don't care about the local symbols at
12235 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
12236 if (!elf_bad_symtab (abfd
))
12237 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
12239 sym_hashes
= elf_sym_hashes (abfd
);
12240 sym_hashes_end
= sym_hashes
+ extsymcount
;
12242 /* Hunt down the child symbol, which is in this section at the same
12243 offset as the relocation. */
12244 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
12246 if ((child
= *search
) != NULL
12247 && (child
->root
.type
== bfd_link_hash_defined
12248 || child
->root
.type
== bfd_link_hash_defweak
)
12249 && child
->root
.u
.def
.section
== sec
12250 && child
->root
.u
.def
.value
== offset
)
12254 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
12255 abfd
, sec
, (unsigned long) offset
);
12256 bfd_set_error (bfd_error_invalid_operation
);
12260 if (!child
->vtable
)
12262 child
->vtable
= (struct elf_link_virtual_table_entry
*)
12263 bfd_zalloc (abfd
, sizeof (*child
->vtable
));
12264 if (!child
->vtable
)
12269 /* This *should* only be the absolute section. It could potentially
12270 be that someone has defined a non-global vtable though, which
12271 would be bad. It isn't worth paging in the local symbols to be
12272 sure though; that case should simply be handled by the assembler. */
12274 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
12277 child
->vtable
->parent
= h
;
12282 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12285 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
12286 asection
*sec ATTRIBUTE_UNUSED
,
12287 struct elf_link_hash_entry
*h
,
12290 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12291 unsigned int log_file_align
= bed
->s
->log_file_align
;
12295 h
->vtable
= (struct elf_link_virtual_table_entry
*)
12296 bfd_zalloc (abfd
, sizeof (*h
->vtable
));
12301 if (addend
>= h
->vtable
->size
)
12303 size_t size
, bytes
, file_align
;
12304 bfd_boolean
*ptr
= h
->vtable
->used
;
12306 /* While the symbol is undefined, we have to be prepared to handle
12308 file_align
= 1 << log_file_align
;
12309 if (h
->root
.type
== bfd_link_hash_undefined
)
12310 size
= addend
+ file_align
;
12314 if (addend
>= size
)
12316 /* Oops! We've got a reference past the defined end of
12317 the table. This is probably a bug -- shall we warn? */
12318 size
= addend
+ file_align
;
12321 size
= (size
+ file_align
- 1) & -file_align
;
12323 /* Allocate one extra entry for use as a "done" flag for the
12324 consolidation pass. */
12325 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
12329 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
12335 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
12336 * sizeof (bfd_boolean
));
12337 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
12341 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
12346 /* And arrange for that done flag to be at index -1. */
12347 h
->vtable
->used
= ptr
+ 1;
12348 h
->vtable
->size
= size
;
12351 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
12356 /* Map an ELF section header flag to its corresponding string. */
12360 flagword flag_value
;
12361 } elf_flags_to_name_table
;
12363 static elf_flags_to_name_table elf_flags_to_names
[] =
12365 { "SHF_WRITE", SHF_WRITE
},
12366 { "SHF_ALLOC", SHF_ALLOC
},
12367 { "SHF_EXECINSTR", SHF_EXECINSTR
},
12368 { "SHF_MERGE", SHF_MERGE
},
12369 { "SHF_STRINGS", SHF_STRINGS
},
12370 { "SHF_INFO_LINK", SHF_INFO_LINK
},
12371 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
12372 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
12373 { "SHF_GROUP", SHF_GROUP
},
12374 { "SHF_TLS", SHF_TLS
},
12375 { "SHF_MASKOS", SHF_MASKOS
},
12376 { "SHF_EXCLUDE", SHF_EXCLUDE
},
12379 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12381 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
12382 struct flag_info
*flaginfo
,
12385 const bfd_vma sh_flags
= elf_section_flags (section
);
12387 if (!flaginfo
->flags_initialized
)
12389 bfd
*obfd
= info
->output_bfd
;
12390 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
12391 struct flag_info_list
*tf
= flaginfo
->flag_list
;
12393 int without_hex
= 0;
12395 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
12398 flagword (*lookup
) (char *);
12400 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
12401 if (lookup
!= NULL
)
12403 flagword hexval
= (*lookup
) ((char *) tf
->name
);
12407 if (tf
->with
== with_flags
)
12408 with_hex
|= hexval
;
12409 else if (tf
->with
== without_flags
)
12410 without_hex
|= hexval
;
12415 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
12417 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
12419 if (tf
->with
== with_flags
)
12420 with_hex
|= elf_flags_to_names
[i
].flag_value
;
12421 else if (tf
->with
== without_flags
)
12422 without_hex
|= elf_flags_to_names
[i
].flag_value
;
12429 info
->callbacks
->einfo
12430 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
12434 flaginfo
->flags_initialized
= TRUE
;
12435 flaginfo
->only_with_flags
|= with_hex
;
12436 flaginfo
->not_with_flags
|= without_hex
;
12439 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
12442 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
12448 struct alloc_got_off_arg
{
12450 struct bfd_link_info
*info
;
12453 /* We need a special top-level link routine to convert got reference counts
12454 to real got offsets. */
12457 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
12459 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
12460 bfd
*obfd
= gofarg
->info
->output_bfd
;
12461 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
12463 if (h
->got
.refcount
> 0)
12465 h
->got
.offset
= gofarg
->gotoff
;
12466 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
12469 h
->got
.offset
= (bfd_vma
) -1;
12474 /* And an accompanying bit to work out final got entry offsets once
12475 we're done. Should be called from final_link. */
12478 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
12479 struct bfd_link_info
*info
)
12482 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12484 struct alloc_got_off_arg gofarg
;
12486 BFD_ASSERT (abfd
== info
->output_bfd
);
12488 if (! is_elf_hash_table (info
->hash
))
12491 /* The GOT offset is relative to the .got section, but the GOT header is
12492 put into the .got.plt section, if the backend uses it. */
12493 if (bed
->want_got_plt
)
12496 gotoff
= bed
->got_header_size
;
12498 /* Do the local .got entries first. */
12499 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
12501 bfd_signed_vma
*local_got
;
12502 bfd_size_type j
, locsymcount
;
12503 Elf_Internal_Shdr
*symtab_hdr
;
12505 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
12508 local_got
= elf_local_got_refcounts (i
);
12512 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
12513 if (elf_bad_symtab (i
))
12514 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12516 locsymcount
= symtab_hdr
->sh_info
;
12518 for (j
= 0; j
< locsymcount
; ++j
)
12520 if (local_got
[j
] > 0)
12522 local_got
[j
] = gotoff
;
12523 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
12526 local_got
[j
] = (bfd_vma
) -1;
12530 /* Then the global .got entries. .plt refcounts are handled by
12531 adjust_dynamic_symbol */
12532 gofarg
.gotoff
= gotoff
;
12533 gofarg
.info
= info
;
12534 elf_link_hash_traverse (elf_hash_table (info
),
12535 elf_gc_allocate_got_offsets
,
12540 /* Many folk need no more in the way of final link than this, once
12541 got entry reference counting is enabled. */
12544 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12546 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
12549 /* Invoke the regular ELF backend linker to do all the work. */
12550 return bfd_elf_final_link (abfd
, info
);
12554 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
12556 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
12558 if (rcookie
->bad_symtab
)
12559 rcookie
->rel
= rcookie
->rels
;
12561 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
12563 unsigned long r_symndx
;
12565 if (! rcookie
->bad_symtab
)
12566 if (rcookie
->rel
->r_offset
> offset
)
12568 if (rcookie
->rel
->r_offset
!= offset
)
12571 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
12572 if (r_symndx
== STN_UNDEF
)
12575 if (r_symndx
>= rcookie
->locsymcount
12576 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12578 struct elf_link_hash_entry
*h
;
12580 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
12582 while (h
->root
.type
== bfd_link_hash_indirect
12583 || h
->root
.type
== bfd_link_hash_warning
)
12584 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12586 if ((h
->root
.type
== bfd_link_hash_defined
12587 || h
->root
.type
== bfd_link_hash_defweak
)
12588 && discarded_section (h
->root
.u
.def
.section
))
12595 /* It's not a relocation against a global symbol,
12596 but it could be a relocation against a local
12597 symbol for a discarded section. */
12599 Elf_Internal_Sym
*isym
;
12601 /* Need to: get the symbol; get the section. */
12602 isym
= &rcookie
->locsyms
[r_symndx
];
12603 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
12604 if (isec
!= NULL
&& discarded_section (isec
))
12612 /* Discard unneeded references to discarded sections.
12613 Returns TRUE if any section's size was changed. */
12614 /* This function assumes that the relocations are in sorted order,
12615 which is true for all known assemblers. */
12618 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
12620 struct elf_reloc_cookie cookie
;
12621 asection
*stab
, *eh
;
12622 const struct elf_backend_data
*bed
;
12624 bfd_boolean ret
= FALSE
;
12626 if (info
->traditional_format
12627 || !is_elf_hash_table (info
->hash
))
12630 _bfd_elf_begin_eh_frame_parsing (info
);
12631 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
12633 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
12636 bed
= get_elf_backend_data (abfd
);
12639 if (!info
->relocatable
)
12641 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
12644 || bfd_is_abs_section (eh
->output_section
)))
12645 eh
= bfd_get_next_section_by_name (eh
);
12648 stab
= bfd_get_section_by_name (abfd
, ".stab");
12650 && (stab
->size
== 0
12651 || bfd_is_abs_section (stab
->output_section
)
12652 || stab
->sec_info_type
!= SEC_INFO_TYPE_STABS
))
12657 && bed
->elf_backend_discard_info
== NULL
)
12660 if (!init_reloc_cookie (&cookie
, info
, abfd
))
12664 && stab
->reloc_count
> 0
12665 && init_reloc_cookie_rels (&cookie
, info
, abfd
, stab
))
12667 if (_bfd_discard_section_stabs (abfd
, stab
,
12668 elf_section_data (stab
)->sec_info
,
12669 bfd_elf_reloc_symbol_deleted_p
,
12672 fini_reloc_cookie_rels (&cookie
, stab
);
12676 && init_reloc_cookie_rels (&cookie
, info
, abfd
, eh
))
12678 _bfd_elf_parse_eh_frame (abfd
, info
, eh
, &cookie
);
12679 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
12680 bfd_elf_reloc_symbol_deleted_p
,
12683 fini_reloc_cookie_rels (&cookie
, eh
);
12684 eh
= bfd_get_next_section_by_name (eh
);
12687 if (bed
->elf_backend_discard_info
!= NULL
12688 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
12691 fini_reloc_cookie (&cookie
, abfd
);
12693 _bfd_elf_end_eh_frame_parsing (info
);
12695 if (info
->eh_frame_hdr
12696 && !info
->relocatable
12697 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12704 _bfd_elf_section_already_linked (bfd
*abfd
,
12706 struct bfd_link_info
*info
)
12709 const char *name
, *key
;
12710 struct bfd_section_already_linked
*l
;
12711 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12713 if (sec
->output_section
== bfd_abs_section_ptr
)
12716 flags
= sec
->flags
;
12718 /* Return if it isn't a linkonce section. A comdat group section
12719 also has SEC_LINK_ONCE set. */
12720 if ((flags
& SEC_LINK_ONCE
) == 0)
12723 /* Don't put group member sections on our list of already linked
12724 sections. They are handled as a group via their group section. */
12725 if (elf_sec_group (sec
) != NULL
)
12728 /* For a SHT_GROUP section, use the group signature as the key. */
12730 if ((flags
& SEC_GROUP
) != 0
12731 && elf_next_in_group (sec
) != NULL
12732 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
12733 key
= elf_group_name (elf_next_in_group (sec
));
12736 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
12737 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12738 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12741 /* Must be a user linkonce section that doesn't follow gcc's
12742 naming convention. In this case we won't be matching
12743 single member groups. */
12747 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
12749 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12751 /* We may have 2 different types of sections on the list: group
12752 sections with a signature of <key> (<key> is some string),
12753 and linkonce sections named .gnu.linkonce.<type>.<key>.
12754 Match like sections. LTO plugin sections are an exception.
12755 They are always named .gnu.linkonce.t.<key> and match either
12756 type of section. */
12757 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12758 && ((flags
& SEC_GROUP
) != 0
12759 || strcmp (name
, l
->sec
->name
) == 0))
12760 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
12762 /* The section has already been linked. See if we should
12763 issue a warning. */
12764 if (!_bfd_handle_already_linked (sec
, l
, info
))
12767 if (flags
& SEC_GROUP
)
12769 asection
*first
= elf_next_in_group (sec
);
12770 asection
*s
= first
;
12774 s
->output_section
= bfd_abs_section_ptr
;
12775 /* Record which group discards it. */
12776 s
->kept_section
= l
->sec
;
12777 s
= elf_next_in_group (s
);
12778 /* These lists are circular. */
12788 /* A single member comdat group section may be discarded by a
12789 linkonce section and vice versa. */
12790 if ((flags
& SEC_GROUP
) != 0)
12792 asection
*first
= elf_next_in_group (sec
);
12794 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12795 /* Check this single member group against linkonce sections. */
12796 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12797 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12798 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12800 first
->output_section
= bfd_abs_section_ptr
;
12801 first
->kept_section
= l
->sec
;
12802 sec
->output_section
= bfd_abs_section_ptr
;
12807 /* Check this linkonce section against single member groups. */
12808 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12809 if (l
->sec
->flags
& SEC_GROUP
)
12811 asection
*first
= elf_next_in_group (l
->sec
);
12814 && elf_next_in_group (first
) == first
12815 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12817 sec
->output_section
= bfd_abs_section_ptr
;
12818 sec
->kept_section
= first
;
12823 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12824 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12825 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12826 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12827 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12828 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12829 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12830 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12831 The reverse order cannot happen as there is never a bfd with only the
12832 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12833 matter as here were are looking only for cross-bfd sections. */
12835 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
12836 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12837 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12838 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
12840 if (abfd
!= l
->sec
->owner
)
12841 sec
->output_section
= bfd_abs_section_ptr
;
12845 /* This is the first section with this name. Record it. */
12846 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
12847 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
12848 return sec
->output_section
== bfd_abs_section_ptr
;
12852 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12854 return sym
->st_shndx
== SHN_COMMON
;
12858 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12864 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12866 return bfd_com_section_ptr
;
12870 _bfd_elf_default_got_elt_size (bfd
*abfd
,
12871 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12872 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
12873 bfd
*ibfd ATTRIBUTE_UNUSED
,
12874 unsigned long symndx ATTRIBUTE_UNUSED
)
12876 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12877 return bed
->s
->arch_size
/ 8;
12880 /* Routines to support the creation of dynamic relocs. */
12882 /* Returns the name of the dynamic reloc section associated with SEC. */
12884 static const char *
12885 get_dynamic_reloc_section_name (bfd
* abfd
,
12887 bfd_boolean is_rela
)
12890 const char *old_name
= bfd_get_section_name (NULL
, sec
);
12891 const char *prefix
= is_rela
? ".rela" : ".rel";
12893 if (old_name
== NULL
)
12896 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
12897 sprintf (name
, "%s%s", prefix
, old_name
);
12902 /* Returns the dynamic reloc section associated with SEC.
12903 If necessary compute the name of the dynamic reloc section based
12904 on SEC's name (looked up in ABFD's string table) and the setting
12908 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
12910 bfd_boolean is_rela
)
12912 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12914 if (reloc_sec
== NULL
)
12916 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12920 reloc_sec
= bfd_get_linker_section (abfd
, name
);
12922 if (reloc_sec
!= NULL
)
12923 elf_section_data (sec
)->sreloc
= reloc_sec
;
12930 /* Returns the dynamic reloc section associated with SEC. If the
12931 section does not exist it is created and attached to the DYNOBJ
12932 bfd and stored in the SRELOC field of SEC's elf_section_data
12935 ALIGNMENT is the alignment for the newly created section and
12936 IS_RELA defines whether the name should be .rela.<SEC's name>
12937 or .rel.<SEC's name>. The section name is looked up in the
12938 string table associated with ABFD. */
12941 _bfd_elf_make_dynamic_reloc_section (asection
* sec
,
12943 unsigned int alignment
,
12945 bfd_boolean is_rela
)
12947 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12949 if (reloc_sec
== NULL
)
12951 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12956 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
12958 if (reloc_sec
== NULL
)
12960 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
12961 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
12962 if ((sec
->flags
& SEC_ALLOC
) != 0)
12963 flags
|= SEC_ALLOC
| SEC_LOAD
;
12965 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
12966 if (reloc_sec
!= NULL
)
12968 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
12973 elf_section_data (sec
)->sreloc
= reloc_sec
;
12979 /* Copy the ELF symbol type associated with a linker hash entry. */
12981 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd ATTRIBUTE_UNUSED
,
12982 struct bfd_link_hash_entry
* hdest
,
12983 struct bfd_link_hash_entry
* hsrc
)
12985 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*)hdest
;
12986 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*)hsrc
;
12988 ehdest
->type
= ehsrc
->type
;
12989 ehdest
->target_internal
= ehsrc
->target_internal
;
12992 /* Append a RELA relocation REL to section S in BFD. */
12995 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
12997 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12998 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
12999 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
13000 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
13003 /* Append a REL relocation REL to section S in BFD. */
13006 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13008 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13009 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
13010 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
13011 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);