1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info
*info
;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info
*info
;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
63 unsigned long r_symndx
,
66 if (r_symndx
>= cookie
->locsymcount
67 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
69 struct elf_link_hash_entry
*h
;
71 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
73 while (h
->root
.type
== bfd_link_hash_indirect
74 || h
->root
.type
== bfd_link_hash_warning
)
75 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
77 if ((h
->root
.type
== bfd_link_hash_defined
78 || h
->root
.type
== bfd_link_hash_defweak
)
79 && discarded_section (h
->root
.u
.def
.section
))
80 return h
->root
.u
.def
.section
;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym
*isym
;
92 /* Need to: get the symbol; get the section. */
93 isym
= &cookie
->locsyms
[r_symndx
];
94 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
96 && discard
? discarded_section (isec
) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry
*
105 _bfd_elf_define_linkage_sym (bfd
*abfd
,
106 struct bfd_link_info
*info
,
110 struct elf_link_hash_entry
*h
;
111 struct bfd_link_hash_entry
*bh
;
112 const struct elf_backend_data
*bed
;
114 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h
->root
.type
= bfd_link_hash_new
;
125 bed
= get_elf_backend_data (abfd
);
126 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
127 sec
, 0, NULL
, FALSE
, bed
->collect
,
130 h
= (struct elf_link_hash_entry
*) bh
;
133 h
->root
.linker_def
= 1;
134 h
->type
= STT_OBJECT
;
135 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
136 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
138 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
143 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
147 struct elf_link_hash_entry
*h
;
148 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
149 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
151 /* This function may be called more than once. */
152 if (htab
->sgot
!= NULL
)
155 flags
= bed
->dynamic_sec_flags
;
157 s
= bfd_make_section_anyway_with_flags (abfd
,
158 (bed
->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed
->dynamic_sec_flags
163 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
167 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
169 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
173 if (bed
->want_got_plt
)
175 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
177 || !bfd_set_section_alignment (abfd
, s
,
178 bed
->s
->log_file_align
))
183 /* The first bit of the global offset table is the header. */
184 s
->size
+= bed
->got_header_size
;
186 if (bed
->want_got_sym
)
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info
)->hgot
= h
;
202 /* Create a strtab to hold the dynamic symbol names. */
204 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
206 struct elf_link_hash_table
*hash_table
;
208 hash_table
= elf_hash_table (info
);
209 if (hash_table
->dynobj
== NULL
)
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
218 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
220 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
226 hash_table
->dynobj
= abfd
;
229 if (hash_table
->dynstr
== NULL
)
231 hash_table
->dynstr
= _bfd_elf_strtab_init ();
232 if (hash_table
->dynstr
== NULL
)
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
246 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
250 const struct elf_backend_data
*bed
;
251 struct elf_link_hash_entry
*h
;
253 if (! is_elf_hash_table (info
->hash
))
256 if (elf_hash_table (info
)->dynamic_sections_created
)
259 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
262 abfd
= elf_hash_table (info
)->dynobj
;
263 bed
= get_elf_backend_data (abfd
);
265 flags
= bed
->dynamic_sec_flags
;
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info
) && !info
->nointerp
)
271 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
272 flags
| SEC_READONLY
);
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
280 flags
| SEC_READONLY
);
282 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
285 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
286 flags
| SEC_READONLY
);
288 || ! bfd_set_section_alignment (abfd
, s
, 1))
291 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
292 flags
| SEC_READONLY
);
294 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
297 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
298 flags
| SEC_READONLY
);
300 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
302 elf_hash_table (info
)->dynsym
= s
;
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
305 flags
| SEC_READONLY
);
309 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
311 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
321 elf_hash_table (info
)->hdynamic
= h
;
327 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
328 flags
| SEC_READONLY
);
330 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
332 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
335 if (info
->emit_gnu_hash
)
337 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
338 flags
| SEC_READONLY
);
340 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed
->s
->arch_size
== 64)
346 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
348 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed
->elf_backend_create_dynamic_sections
== NULL
355 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
358 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
363 /* Create dynamic sections when linking against a dynamic object. */
366 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
368 flagword flags
, pltflags
;
369 struct elf_link_hash_entry
*h
;
371 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
372 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags
= bed
->dynamic_sec_flags
;
379 if (bed
->plt_not_loaded
)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
385 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
386 if (bed
->plt_readonly
)
387 pltflags
|= SEC_READONLY
;
389 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
391 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 if (bed
->want_plt_sym
)
399 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info
)->hplt
= h
;
406 s
= bfd_make_section_anyway_with_flags (abfd
,
407 (bed
->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags
| SEC_READONLY
);
411 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
415 if (! _bfd_elf_create_got_section (abfd
, info
))
418 if (bed
->want_dynbss
)
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
427 (SEC_ALLOC
| SEC_LINKER_CREATED
));
432 if (bed
->want_dynrelro
)
434 /* Similarly, but for symbols that were originally in read-only
436 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
437 (SEC_ALLOC
| SEC_READONLY
439 | SEC_LINKER_CREATED
));
445 /* The .rel[a].bss section holds copy relocs. This section is not
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
456 if (bfd_link_executable (info
))
458 s
= bfd_make_section_anyway_with_flags (abfd
,
459 (bed
->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags
| SEC_READONLY
);
463 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
467 if (bed
->want_dynrelro
)
469 s
= (bfd_make_section_anyway_with_flags
470 (abfd
, (bed
->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags
| SEC_READONLY
));
474 || ! bfd_set_section_alignment (abfd
, s
,
475 bed
->s
->log_file_align
))
477 htab
->sreldynrelro
= s
;
485 /* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
494 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
495 struct elf_link_hash_entry
*h
)
497 if (h
->dynindx
== -1)
499 struct elf_strtab_hash
*dynstr
;
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h
->other
))
512 if (h
->root
.type
!= bfd_link_hash_undefined
513 && h
->root
.type
!= bfd_link_hash_undefweak
)
516 if (!elf_hash_table (info
)->is_relocatable_executable
)
524 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
525 ++elf_hash_table (info
)->dynsymcount
;
527 dynstr
= elf_hash_table (info
)->dynstr
;
530 /* Create a strtab to hold the dynamic symbol names. */
531 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
536 /* We don't put any version information in the dynamic string
538 name
= h
->root
.root
.string
;
539 p
= strchr (name
, ELF_VER_CHR
);
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
548 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
553 if (indx
== (size_t) -1)
555 h
->dynstr_index
= indx
;
561 /* Mark a symbol dynamic. */
564 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
565 struct elf_link_hash_entry
*h
,
566 Elf_Internal_Sym
*sym
)
568 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
570 /* It may be called more than once on the same H. */
571 if(h
->dynamic
|| bfd_link_relocatable (info
))
574 if ((info
->dynamic_data
575 && (h
->type
== STT_OBJECT
576 || h
->type
== STT_COMMON
578 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
579 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
581 && h
->root
.type
== bfd_link_hash_new
582 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
586 /* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
590 bfd_elf_record_link_assignment (bfd
*output_bfd
,
591 struct bfd_link_info
*info
,
596 struct elf_link_hash_entry
*h
, *hv
;
597 struct elf_link_hash_table
*htab
;
598 const struct elf_backend_data
*bed
;
600 if (!is_elf_hash_table (info
->hash
))
603 htab
= elf_hash_table (info
);
604 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
608 if (h
->root
.type
== bfd_link_hash_warning
)
609 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
611 if (h
->versioned
== unknown
)
613 /* Set versioned if symbol version is unknown. */
614 char *version
= strrchr (name
, ELF_VER_CHR
);
617 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
618 h
->versioned
= versioned_hidden
;
620 h
->versioned
= versioned
;
624 switch (h
->root
.type
)
626 case bfd_link_hash_defined
:
627 case bfd_link_hash_defweak
:
628 case bfd_link_hash_common
:
630 case bfd_link_hash_undefweak
:
631 case bfd_link_hash_undefined
:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
635 h
->root
.type
= bfd_link_hash_new
;
636 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
637 bfd_link_repair_undef_list (&htab
->root
);
639 case bfd_link_hash_new
:
640 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
643 case bfd_link_hash_indirect
:
644 /* We had a versioned symbol in a dynamic library. We make the
645 the versioned symbol point to this one. */
646 bed
= get_elf_backend_data (output_bfd
);
648 while (hv
->root
.type
== bfd_link_hash_indirect
649 || hv
->root
.type
== bfd_link_hash_warning
)
650 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
651 /* We don't need to update h->root.u since linker will set them
653 h
->root
.type
= bfd_link_hash_undefined
;
654 hv
->root
.type
= bfd_link_hash_indirect
;
655 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
656 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
670 h
->root
.type
= bfd_link_hash_undefined
;
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
674 then undo any forced local marking that may have been set by input
675 section garbage collection and clear out any version information
676 because the symbol will not be associated with the dynamic object
683 h
->verinfo
.verdef
= NULL
;
690 bed
= get_elf_backend_data (output_bfd
);
691 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
692 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
693 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
698 if (!bfd_link_relocatable (info
)
700 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
706 || bfd_link_dll (info
)
707 || elf_hash_table (info
)->is_relocatable_executable
)
710 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
716 if (h
->u
.weakdef
!= NULL
717 && h
->u
.weakdef
->dynindx
== -1)
719 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
727 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
732 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
737 struct elf_link_local_dynamic_entry
*entry
;
738 struct elf_link_hash_table
*eht
;
739 struct elf_strtab_hash
*dynstr
;
742 Elf_External_Sym_Shndx eshndx
;
743 char esym
[sizeof (Elf64_External_Sym
)];
745 if (! is_elf_hash_table (info
->hash
))
748 /* See if the entry exists already. */
749 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
750 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
753 amt
= sizeof (*entry
);
754 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
760 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
762 bfd_release (input_bfd
, entry
);
766 if (entry
->isym
.st_shndx
!= SHN_UNDEF
767 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
771 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
772 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd
, entry
);
781 name
= (bfd_elf_string_from_elf_section
782 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
783 entry
->isym
.st_name
));
785 dynstr
= elf_hash_table (info
)->dynstr
;
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
794 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
795 if (dynstr_index
== (size_t) -1)
797 entry
->isym
.st_name
= dynstr_index
;
799 eht
= elf_hash_table (info
);
801 entry
->next
= eht
->dynlocal
;
802 eht
->dynlocal
= entry
;
803 entry
->input_bfd
= input_bfd
;
804 entry
->input_indx
= input_indx
;
807 /* Whatever binding the symbol had before, it's now local. */
809 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
811 /* The dynindx will be set at the end of size_dynamic_sections. */
816 /* Return the dynindex of a local dynamic symbol. */
819 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
823 struct elf_link_local_dynamic_entry
*e
;
825 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
826 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
831 /* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
836 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
839 size_t *count
= (size_t *) data
;
844 if (h
->dynindx
!= -1)
845 h
->dynindx
= ++(*count
);
851 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
855 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
858 size_t *count
= (size_t *) data
;
860 if (!h
->forced_local
)
863 if (h
->dynindx
!= -1)
864 h
->dynindx
= ++(*count
);
869 /* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
872 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
873 struct bfd_link_info
*info
,
876 struct elf_link_hash_table
*htab
;
879 switch (elf_section_data (p
)->this_hdr
.sh_type
)
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
886 htab
= elf_hash_table (info
);
887 if (p
== htab
->tls_sec
)
890 if (htab
->text_index_section
!= NULL
)
891 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
893 return (htab
->dynobj
!= NULL
894 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
895 && ip
->output_section
== p
);
897 /* There shouldn't be section relative relocations
898 against any other section. */
904 /* Assign dynsym indices. In a shared library we generate a section
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
911 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
912 struct bfd_link_info
*info
,
913 unsigned long *section_sym_count
)
915 unsigned long dynsymcount
= 0;
917 if (bfd_link_pic (info
)
918 || elf_hash_table (info
)->is_relocatable_executable
)
920 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
922 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
923 if ((p
->flags
& SEC_EXCLUDE
) == 0
924 && (p
->flags
& SEC_ALLOC
) != 0
925 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
926 elf_section_data (p
)->dynindx
= ++dynsymcount
;
928 elf_section_data (p
)->dynindx
= 0;
930 *section_sym_count
= dynsymcount
;
932 elf_link_hash_traverse (elf_hash_table (info
),
933 elf_link_renumber_local_hash_table_dynsyms
,
936 if (elf_hash_table (info
)->dynlocal
)
938 struct elf_link_local_dynamic_entry
*p
;
939 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
940 p
->dynindx
= ++dynsymcount
;
942 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
944 elf_link_hash_traverse (elf_hash_table (info
),
945 elf_link_renumber_hash_table_dynsyms
,
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
954 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
958 /* Merge st_other field. */
961 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
962 const Elf_Internal_Sym
*isym
, asection
*sec
,
963 bfd_boolean definition
, bfd_boolean dynamic
)
965 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
967 /* If st_other has a processor-specific meaning, specific
968 code might be needed here. */
969 if (bed
->elf_backend_merge_symbol_attribute
)
970 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
975 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
976 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis
- 1 < hvis
- 1)
981 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
984 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
985 && (sec
->flags
& SEC_READONLY
) == 0)
986 h
->protected_def
= 1;
989 /* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
1003 _bfd_elf_merge_symbol (bfd
*abfd
,
1004 struct bfd_link_info
*info
,
1006 Elf_Internal_Sym
*sym
,
1009 struct elf_link_hash_entry
**sym_hash
,
1011 bfd_boolean
*pold_weak
,
1012 unsigned int *pold_alignment
,
1014 bfd_boolean
*override
,
1015 bfd_boolean
*type_change_ok
,
1016 bfd_boolean
*size_change_ok
,
1017 bfd_boolean
*matched
)
1019 asection
*sec
, *oldsec
;
1020 struct elf_link_hash_entry
*h
;
1021 struct elf_link_hash_entry
*hi
;
1022 struct elf_link_hash_entry
*flip
;
1025 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1026 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1027 const struct elf_backend_data
*bed
;
1034 bind
= ELF_ST_BIND (sym
->st_info
);
1036 if (! bfd_is_und_section (sec
))
1037 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1039 h
= ((struct elf_link_hash_entry
*)
1040 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1045 bed
= get_elf_backend_data (abfd
);
1047 /* NEW_VERSION is the symbol version of the new symbol. */
1048 if (h
->versioned
!= unversioned
)
1050 /* Symbol version is unknown or versioned. */
1051 new_version
= strrchr (name
, ELF_VER_CHR
);
1054 if (h
->versioned
== unknown
)
1056 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1057 h
->versioned
= versioned_hidden
;
1059 h
->versioned
= versioned
;
1062 if (new_version
[0] == '\0')
1066 h
->versioned
= unversioned
;
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1074 while (h
->root
.type
== bfd_link_hash_indirect
1075 || h
->root
.type
== bfd_link_hash_warning
)
1076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1080 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1084 /* OLD_HIDDEN is true if the existing symbol is only visible
1085 to the symbol with the same symbol version. NEW_HIDDEN is
1086 true if the new symbol is only visible to the symbol with
1087 the same symbol version. */
1088 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1089 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1090 if (!old_hidden
&& !new_hidden
)
1091 /* The new symbol matches the existing symbol if both
1096 /* OLD_VERSION is the symbol version of the existing
1100 if (h
->versioned
>= versioned
)
1101 old_version
= strrchr (h
->root
.root
.string
,
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched
= (old_version
== new_version
1109 || (old_version
!= NULL
1110 && new_version
!= NULL
1111 && strcmp (old_version
, new_version
) == 0));
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1121 switch (h
->root
.type
)
1126 case bfd_link_hash_undefined
:
1127 case bfd_link_hash_undefweak
:
1128 oldbfd
= h
->root
.u
.undef
.abfd
;
1131 case bfd_link_hash_defined
:
1132 case bfd_link_hash_defweak
:
1133 oldbfd
= h
->root
.u
.def
.section
->owner
;
1134 oldsec
= h
->root
.u
.def
.section
;
1137 case bfd_link_hash_common
:
1138 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1139 oldsec
= h
->root
.u
.c
.p
->section
;
1141 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1144 if (poldbfd
&& *poldbfd
== NULL
)
1147 /* Differentiate strong and weak symbols. */
1148 newweak
= bind
== STB_WEAK
;
1149 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1150 || h
->root
.type
== bfd_link_hash_undefweak
);
1152 *pold_weak
= oldweak
;
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1159 /* We have to check it for every instance since the first few may be
1160 references and not all compilers emit symbol type for undefined
1162 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1167 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1178 if (bfd_is_und_section (sec
))
1180 if (bind
!= STB_WEAK
)
1182 h
->ref_dynamic_nonweak
= 1;
1183 hi
->ref_dynamic_nonweak
= 1;
1188 /* Update the existing symbol only if they match. */
1191 hi
->dynamic_def
= 1;
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1199 if (h
->root
.type
== bfd_link_hash_new
)
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1212 && (newweak
|| oldweak
)
1213 && ((abfd
->flags
& DYNAMIC
) == 0
1214 || !h
->def_regular
))
1219 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1220 else if (oldsec
!= NULL
)
1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1223 indices used by MIPS ELF. */
1224 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1230 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1232 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1233 && h
->root
.type
!= bfd_link_hash_undefweak
1234 && h
->root
.type
!= bfd_link_hash_common
);
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1239 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1240 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1242 oldfunc
= (h
->type
!= STT_NOTYPE
1243 && bed
->is_function_type (h
->type
));
1245 /* If creating a default indirect symbol ("foo" or "foo@") from a
1246 dynamic versioned definition ("foo@@") skip doing so if there is
1247 an existing regular definition with a different type. We don't
1248 want, for example, a "time" variable in the executable overriding
1249 a "time" function in a shared library. */
1250 if (pold_alignment
== NULL
1254 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1255 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1256 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1257 && h
->type
!= STT_NOTYPE
1258 && !(newfunc
&& oldfunc
))
1264 /* Check TLS symbols. We don't check undefined symbols introduced
1265 by "ld -u" which have no type (and oldbfd NULL), and we don't
1266 check symbols from plugins because they also have no type. */
1268 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1269 && (abfd
->flags
& BFD_PLUGIN
) == 0
1270 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1271 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1274 bfd_boolean ntdef
, tdef
;
1275 asection
*ntsec
, *tsec
;
1277 if (h
->type
== STT_TLS
)
1298 /* xgettext:c-format */
1299 (_("%s: TLS definition in %B section %A "
1300 "mismatches non-TLS definition in %B section %A"),
1301 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1302 else if (!tdef
&& !ntdef
)
1304 /* xgettext:c-format */
1305 (_("%s: TLS reference in %B "
1306 "mismatches non-TLS reference in %B"),
1307 tbfd
, ntbfd
, h
->root
.root
.string
);
1310 /* xgettext:c-format */
1311 (_("%s: TLS definition in %B section %A "
1312 "mismatches non-TLS reference in %B"),
1313 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1316 /* xgettext:c-format */
1317 (_("%s: TLS reference in %B "
1318 "mismatches non-TLS definition in %B section %A"),
1319 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1321 bfd_set_error (bfd_error_bad_value
);
1325 /* If the old symbol has non-default visibility, we ignore the new
1326 definition from a dynamic object. */
1328 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1329 && !bfd_is_und_section (sec
))
1332 /* Make sure this symbol is dynamic. */
1334 hi
->ref_dynamic
= 1;
1335 /* A protected symbol has external availability. Make sure it is
1336 recorded as dynamic.
1338 FIXME: Should we check type and size for protected symbol? */
1339 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1340 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1345 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1348 /* If the new symbol with non-default visibility comes from a
1349 relocatable file and the old definition comes from a dynamic
1350 object, we remove the old definition. */
1351 if (hi
->root
.type
== bfd_link_hash_indirect
)
1353 /* Handle the case where the old dynamic definition is
1354 default versioned. We need to copy the symbol info from
1355 the symbol with default version to the normal one if it
1356 was referenced before. */
1359 hi
->root
.type
= h
->root
.type
;
1360 h
->root
.type
= bfd_link_hash_indirect
;
1361 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1363 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1364 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1366 /* If the new symbol is hidden or internal, completely undo
1367 any dynamic link state. */
1368 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1369 h
->forced_local
= 0;
1376 /* FIXME: Should we check type and size for protected symbol? */
1386 /* If the old symbol was undefined before, then it will still be
1387 on the undefs list. If the new symbol is undefined or
1388 common, we can't make it bfd_link_hash_new here, because new
1389 undefined or common symbols will be added to the undefs list
1390 by _bfd_generic_link_add_one_symbol. Symbols may not be
1391 added twice to the undefs list. Also, if the new symbol is
1392 undefweak then we don't want to lose the strong undef. */
1393 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1395 h
->root
.type
= bfd_link_hash_undefined
;
1396 h
->root
.u
.undef
.abfd
= abfd
;
1400 h
->root
.type
= bfd_link_hash_new
;
1401 h
->root
.u
.undef
.abfd
= NULL
;
1404 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1406 /* If the new symbol is hidden or internal, completely undo
1407 any dynamic link state. */
1408 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1409 h
->forced_local
= 0;
1415 /* FIXME: Should we check type and size for protected symbol? */
1421 /* If a new weak symbol definition comes from a regular file and the
1422 old symbol comes from a dynamic library, we treat the new one as
1423 strong. Similarly, an old weak symbol definition from a regular
1424 file is treated as strong when the new symbol comes from a dynamic
1425 library. Further, an old weak symbol from a dynamic library is
1426 treated as strong if the new symbol is from a dynamic library.
1427 This reflects the way glibc's ld.so works.
1429 Do this before setting *type_change_ok or *size_change_ok so that
1430 we warn properly when dynamic library symbols are overridden. */
1432 if (newdef
&& !newdyn
&& olddyn
)
1434 if (olddef
&& newdyn
)
1437 /* Allow changes between different types of function symbol. */
1438 if (newfunc
&& oldfunc
)
1439 *type_change_ok
= TRUE
;
1441 /* It's OK to change the type if either the existing symbol or the
1442 new symbol is weak. A type change is also OK if the old symbol
1443 is undefined and the new symbol is defined. */
1448 && h
->root
.type
== bfd_link_hash_undefined
))
1449 *type_change_ok
= TRUE
;
1451 /* It's OK to change the size if either the existing symbol or the
1452 new symbol is weak, or if the old symbol is undefined. */
1455 || h
->root
.type
== bfd_link_hash_undefined
)
1456 *size_change_ok
= TRUE
;
1458 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1459 symbol, respectively, appears to be a common symbol in a dynamic
1460 object. If a symbol appears in an uninitialized section, and is
1461 not weak, and is not a function, then it may be a common symbol
1462 which was resolved when the dynamic object was created. We want
1463 to treat such symbols specially, because they raise special
1464 considerations when setting the symbol size: if the symbol
1465 appears as a common symbol in a regular object, and the size in
1466 the regular object is larger, we must make sure that we use the
1467 larger size. This problematic case can always be avoided in C,
1468 but it must be handled correctly when using Fortran shared
1471 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1472 likewise for OLDDYNCOMMON and OLDDEF.
1474 Note that this test is just a heuristic, and that it is quite
1475 possible to have an uninitialized symbol in a shared object which
1476 is really a definition, rather than a common symbol. This could
1477 lead to some minor confusion when the symbol really is a common
1478 symbol in some regular object. However, I think it will be
1484 && (sec
->flags
& SEC_ALLOC
) != 0
1485 && (sec
->flags
& SEC_LOAD
) == 0
1488 newdyncommon
= TRUE
;
1490 newdyncommon
= FALSE
;
1494 && h
->root
.type
== bfd_link_hash_defined
1496 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1497 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1500 olddyncommon
= TRUE
;
1502 olddyncommon
= FALSE
;
1504 /* We now know everything about the old and new symbols. We ask the
1505 backend to check if we can merge them. */
1506 if (bed
->merge_symbol
!= NULL
)
1508 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1513 /* If both the old and the new symbols look like common symbols in a
1514 dynamic object, set the size of the symbol to the larger of the
1519 && sym
->st_size
!= h
->size
)
1521 /* Since we think we have two common symbols, issue a multiple
1522 common warning if desired. Note that we only warn if the
1523 size is different. If the size is the same, we simply let
1524 the old symbol override the new one as normally happens with
1525 symbols defined in dynamic objects. */
1527 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1528 bfd_link_hash_common
, sym
->st_size
);
1529 if (sym
->st_size
> h
->size
)
1530 h
->size
= sym
->st_size
;
1532 *size_change_ok
= TRUE
;
1535 /* If we are looking at a dynamic object, and we have found a
1536 definition, we need to see if the symbol was already defined by
1537 some other object. If so, we want to use the existing
1538 definition, and we do not want to report a multiple symbol
1539 definition error; we do this by clobbering *PSEC to be
1540 bfd_und_section_ptr.
1542 We treat a common symbol as a definition if the symbol in the
1543 shared library is a function, since common symbols always
1544 represent variables; this can cause confusion in principle, but
1545 any such confusion would seem to indicate an erroneous program or
1546 shared library. We also permit a common symbol in a regular
1547 object to override a weak symbol in a shared object. A common
1548 symbol in executable also overrides a symbol in a shared object. */
1553 || (h
->root
.type
== bfd_link_hash_common
1556 || (!olddyn
&& bfd_link_executable (info
))))))
1560 newdyncommon
= FALSE
;
1562 *psec
= sec
= bfd_und_section_ptr
;
1563 *size_change_ok
= TRUE
;
1565 /* If we get here when the old symbol is a common symbol, then
1566 we are explicitly letting it override a weak symbol or
1567 function in a dynamic object, and we don't want to warn about
1568 a type change. If the old symbol is a defined symbol, a type
1569 change warning may still be appropriate. */
1571 if (h
->root
.type
== bfd_link_hash_common
)
1572 *type_change_ok
= TRUE
;
1575 /* Handle the special case of an old common symbol merging with a
1576 new symbol which looks like a common symbol in a shared object.
1577 We change *PSEC and *PVALUE to make the new symbol look like a
1578 common symbol, and let _bfd_generic_link_add_one_symbol do the
1582 && h
->root
.type
== bfd_link_hash_common
)
1586 newdyncommon
= FALSE
;
1587 *pvalue
= sym
->st_size
;
1588 *psec
= sec
= bed
->common_section (oldsec
);
1589 *size_change_ok
= TRUE
;
1592 /* Skip weak definitions of symbols that are already defined. */
1593 if (newdef
&& olddef
&& newweak
)
1595 /* Don't skip new non-IR weak syms. */
1596 if (!(oldbfd
!= NULL
1597 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1598 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1604 /* Merge st_other. If the symbol already has a dynamic index,
1605 but visibility says it should not be visible, turn it into a
1607 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1608 if (h
->dynindx
!= -1)
1609 switch (ELF_ST_VISIBILITY (h
->other
))
1613 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1618 /* If the old symbol is from a dynamic object, and the new symbol is
1619 a definition which is not from a dynamic object, then the new
1620 symbol overrides the old symbol. Symbols from regular files
1621 always take precedence over symbols from dynamic objects, even if
1622 they are defined after the dynamic object in the link.
1624 As above, we again permit a common symbol in a regular object to
1625 override a definition in a shared object if the shared object
1626 symbol is a function or is weak. */
1631 || (bfd_is_com_section (sec
)
1632 && (oldweak
|| oldfunc
)))
1637 /* Change the hash table entry to undefined, and let
1638 _bfd_generic_link_add_one_symbol do the right thing with the
1641 h
->root
.type
= bfd_link_hash_undefined
;
1642 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1643 *size_change_ok
= TRUE
;
1646 olddyncommon
= FALSE
;
1648 /* We again permit a type change when a common symbol may be
1649 overriding a function. */
1651 if (bfd_is_com_section (sec
))
1655 /* If a common symbol overrides a function, make sure
1656 that it isn't defined dynamically nor has type
1659 h
->type
= STT_NOTYPE
;
1661 *type_change_ok
= TRUE
;
1664 if (hi
->root
.type
== bfd_link_hash_indirect
)
1667 /* This union may have been set to be non-NULL when this symbol
1668 was seen in a dynamic object. We must force the union to be
1669 NULL, so that it is correct for a regular symbol. */
1670 h
->verinfo
.vertree
= NULL
;
1673 /* Handle the special case of a new common symbol merging with an
1674 old symbol that looks like it might be a common symbol defined in
1675 a shared object. Note that we have already handled the case in
1676 which a new common symbol should simply override the definition
1677 in the shared library. */
1680 && bfd_is_com_section (sec
)
1683 /* It would be best if we could set the hash table entry to a
1684 common symbol, but we don't know what to use for the section
1685 or the alignment. */
1686 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1687 bfd_link_hash_common
, sym
->st_size
);
1689 /* If the presumed common symbol in the dynamic object is
1690 larger, pretend that the new symbol has its size. */
1692 if (h
->size
> *pvalue
)
1695 /* We need to remember the alignment required by the symbol
1696 in the dynamic object. */
1697 BFD_ASSERT (pold_alignment
);
1698 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1701 olddyncommon
= FALSE
;
1703 h
->root
.type
= bfd_link_hash_undefined
;
1704 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1706 *size_change_ok
= TRUE
;
1707 *type_change_ok
= TRUE
;
1709 if (hi
->root
.type
== bfd_link_hash_indirect
)
1712 h
->verinfo
.vertree
= NULL
;
1717 /* Handle the case where we had a versioned symbol in a dynamic
1718 library and now find a definition in a normal object. In this
1719 case, we make the versioned symbol point to the normal one. */
1720 flip
->root
.type
= h
->root
.type
;
1721 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1722 h
->root
.type
= bfd_link_hash_indirect
;
1723 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1724 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1728 flip
->ref_dynamic
= 1;
1735 /* This function is called to create an indirect symbol from the
1736 default for the symbol with the default version if needed. The
1737 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1738 set DYNSYM if the new indirect symbol is dynamic. */
1741 _bfd_elf_add_default_symbol (bfd
*abfd
,
1742 struct bfd_link_info
*info
,
1743 struct elf_link_hash_entry
*h
,
1745 Elf_Internal_Sym
*sym
,
1749 bfd_boolean
*dynsym
)
1751 bfd_boolean type_change_ok
;
1752 bfd_boolean size_change_ok
;
1755 struct elf_link_hash_entry
*hi
;
1756 struct bfd_link_hash_entry
*bh
;
1757 const struct elf_backend_data
*bed
;
1758 bfd_boolean collect
;
1759 bfd_boolean dynamic
;
1760 bfd_boolean override
;
1762 size_t len
, shortlen
;
1764 bfd_boolean matched
;
1766 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1769 /* If this symbol has a version, and it is the default version, we
1770 create an indirect symbol from the default name to the fully
1771 decorated name. This will cause external references which do not
1772 specify a version to be bound to this version of the symbol. */
1773 p
= strchr (name
, ELF_VER_CHR
);
1774 if (h
->versioned
== unknown
)
1778 h
->versioned
= unversioned
;
1783 if (p
[1] != ELF_VER_CHR
)
1785 h
->versioned
= versioned_hidden
;
1789 h
->versioned
= versioned
;
1794 /* PR ld/19073: We may see an unversioned definition after the
1800 bed
= get_elf_backend_data (abfd
);
1801 collect
= bed
->collect
;
1802 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1804 shortlen
= p
- name
;
1805 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1806 if (shortname
== NULL
)
1808 memcpy (shortname
, name
, shortlen
);
1809 shortname
[shortlen
] = '\0';
1811 /* We are going to create a new symbol. Merge it with any existing
1812 symbol with this name. For the purposes of the merge, act as
1813 though we were defining the symbol we just defined, although we
1814 actually going to define an indirect symbol. */
1815 type_change_ok
= FALSE
;
1816 size_change_ok
= FALSE
;
1819 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1820 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1821 &type_change_ok
, &size_change_ok
, &matched
))
1827 if (hi
->def_regular
)
1829 /* If the undecorated symbol will have a version added by a
1830 script different to H, then don't indirect to/from the
1831 undecorated symbol. This isn't ideal because we may not yet
1832 have seen symbol versions, if given by a script on the
1833 command line rather than via --version-script. */
1834 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1839 = bfd_find_version_for_sym (info
->version_info
,
1840 hi
->root
.root
.string
, &hide
);
1841 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1843 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1847 if (hi
->verinfo
.vertree
!= NULL
1848 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1854 /* Add the default symbol if not performing a relocatable link. */
1855 if (! bfd_link_relocatable (info
))
1858 if (! (_bfd_generic_link_add_one_symbol
1859 (info
, abfd
, shortname
, BSF_INDIRECT
,
1860 bfd_ind_section_ptr
,
1861 0, name
, FALSE
, collect
, &bh
)))
1863 hi
= (struct elf_link_hash_entry
*) bh
;
1868 /* In this case the symbol named SHORTNAME is overriding the
1869 indirect symbol we want to add. We were planning on making
1870 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1871 is the name without a version. NAME is the fully versioned
1872 name, and it is the default version.
1874 Overriding means that we already saw a definition for the
1875 symbol SHORTNAME in a regular object, and it is overriding
1876 the symbol defined in the dynamic object.
1878 When this happens, we actually want to change NAME, the
1879 symbol we just added, to refer to SHORTNAME. This will cause
1880 references to NAME in the shared object to become references
1881 to SHORTNAME in the regular object. This is what we expect
1882 when we override a function in a shared object: that the
1883 references in the shared object will be mapped to the
1884 definition in the regular object. */
1886 while (hi
->root
.type
== bfd_link_hash_indirect
1887 || hi
->root
.type
== bfd_link_hash_warning
)
1888 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1890 h
->root
.type
= bfd_link_hash_indirect
;
1891 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1895 hi
->ref_dynamic
= 1;
1899 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1904 /* Now set HI to H, so that the following code will set the
1905 other fields correctly. */
1909 /* Check if HI is a warning symbol. */
1910 if (hi
->root
.type
== bfd_link_hash_warning
)
1911 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1913 /* If there is a duplicate definition somewhere, then HI may not
1914 point to an indirect symbol. We will have reported an error to
1915 the user in that case. */
1917 if (hi
->root
.type
== bfd_link_hash_indirect
)
1919 struct elf_link_hash_entry
*ht
;
1921 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1922 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1924 /* A reference to the SHORTNAME symbol from a dynamic library
1925 will be satisfied by the versioned symbol at runtime. In
1926 effect, we have a reference to the versioned symbol. */
1927 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1928 hi
->dynamic_def
|= ht
->dynamic_def
;
1930 /* See if the new flags lead us to realize that the symbol must
1936 if (! bfd_link_executable (info
)
1943 if (hi
->ref_regular
)
1949 /* We also need to define an indirection from the nondefault version
1953 len
= strlen (name
);
1954 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1955 if (shortname
== NULL
)
1957 memcpy (shortname
, name
, shortlen
);
1958 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1960 /* Once again, merge with any existing symbol. */
1961 type_change_ok
= FALSE
;
1962 size_change_ok
= FALSE
;
1964 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1965 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1966 &type_change_ok
, &size_change_ok
, &matched
))
1974 /* Here SHORTNAME is a versioned name, so we don't expect to see
1975 the type of override we do in the case above unless it is
1976 overridden by a versioned definition. */
1977 if (hi
->root
.type
!= bfd_link_hash_defined
1978 && hi
->root
.type
!= bfd_link_hash_defweak
)
1980 /* xgettext:c-format */
1981 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1987 if (! (_bfd_generic_link_add_one_symbol
1988 (info
, abfd
, shortname
, BSF_INDIRECT
,
1989 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1991 hi
= (struct elf_link_hash_entry
*) bh
;
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error
1995 to the user in that case. */
1997 if (hi
->root
.type
== bfd_link_hash_indirect
)
1999 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2000 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2001 hi
->dynamic_def
|= h
->dynamic_def
;
2003 /* See if the new flags lead us to realize that the symbol
2009 if (! bfd_link_executable (info
)
2015 if (hi
->ref_regular
)
2025 /* This routine is used to export all defined symbols into the dynamic
2026 symbol table. It is called via elf_link_hash_traverse. */
2029 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2031 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2033 /* Ignore indirect symbols. These are added by the versioning code. */
2034 if (h
->root
.type
== bfd_link_hash_indirect
)
2037 /* Ignore this if we won't export it. */
2038 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2041 if (h
->dynindx
== -1
2042 && (h
->def_regular
|| h
->ref_regular
)
2043 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2044 h
->root
.root
.string
))
2046 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2056 /* Look through the symbols which are defined in other shared
2057 libraries and referenced here. Update the list of version
2058 dependencies. This will be put into the .gnu.version_r section.
2059 This function is called via elf_link_hash_traverse. */
2062 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2065 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2066 Elf_Internal_Verneed
*t
;
2067 Elf_Internal_Vernaux
*a
;
2070 /* We only care about symbols defined in shared objects with version
2075 || h
->verinfo
.verdef
== NULL
2076 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2077 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2080 /* See if we already know about this version. */
2081 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2085 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2088 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2089 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2095 /* This is a new version. Add it to tree we are building. */
2100 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2103 rinfo
->failed
= TRUE
;
2107 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2108 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2109 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2113 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2116 rinfo
->failed
= TRUE
;
2120 /* Note that we are copying a string pointer here, and testing it
2121 above. If bfd_elf_string_from_elf_section is ever changed to
2122 discard the string data when low in memory, this will have to be
2124 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2126 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2127 a
->vna_nextptr
= t
->vn_auxptr
;
2129 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2132 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2139 /* Figure out appropriate versions for all the symbols. We may not
2140 have the version number script until we have read all of the input
2141 files, so until that point we don't know which symbols should be
2142 local. This function is called via elf_link_hash_traverse. */
2145 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2147 struct elf_info_failed
*sinfo
;
2148 struct bfd_link_info
*info
;
2149 const struct elf_backend_data
*bed
;
2150 struct elf_info_failed eif
;
2153 sinfo
= (struct elf_info_failed
*) data
;
2156 /* Fix the symbol flags. */
2159 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2162 sinfo
->failed
= TRUE
;
2166 /* We only need version numbers for symbols defined in regular
2168 if (!h
->def_regular
)
2171 bed
= get_elf_backend_data (info
->output_bfd
);
2172 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2173 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2175 struct bfd_elf_version_tree
*t
;
2178 if (*p
== ELF_VER_CHR
)
2181 /* If there is no version string, we can just return out. */
2185 /* Look for the version. If we find it, it is no longer weak. */
2186 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2188 if (strcmp (t
->name
, p
) == 0)
2192 struct bfd_elf_version_expr
*d
;
2194 len
= p
- h
->root
.root
.string
;
2195 alc
= (char *) bfd_malloc (len
);
2198 sinfo
->failed
= TRUE
;
2201 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2202 alc
[len
- 1] = '\0';
2203 if (alc
[len
- 2] == ELF_VER_CHR
)
2204 alc
[len
- 2] = '\0';
2206 h
->verinfo
.vertree
= t
;
2210 if (t
->globals
.list
!= NULL
)
2211 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2213 /* See if there is anything to force this symbol to
2215 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2217 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2220 && ! info
->export_dynamic
)
2221 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2229 /* If we are building an application, we need to create a
2230 version node for this version. */
2231 if (t
== NULL
&& bfd_link_executable (info
))
2233 struct bfd_elf_version_tree
**pp
;
2236 /* If we aren't going to export this symbol, we don't need
2237 to worry about it. */
2238 if (h
->dynindx
== -1)
2241 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2245 sinfo
->failed
= TRUE
;
2250 t
->name_indx
= (unsigned int) -1;
2254 /* Don't count anonymous version tag. */
2255 if (sinfo
->info
->version_info
!= NULL
2256 && sinfo
->info
->version_info
->vernum
== 0)
2258 for (pp
= &sinfo
->info
->version_info
;
2262 t
->vernum
= version_index
;
2266 h
->verinfo
.vertree
= t
;
2270 /* We could not find the version for a symbol when
2271 generating a shared archive. Return an error. */
2273 /* xgettext:c-format */
2274 (_("%B: version node not found for symbol %s"),
2275 info
->output_bfd
, h
->root
.root
.string
);
2276 bfd_set_error (bfd_error_bad_value
);
2277 sinfo
->failed
= TRUE
;
2282 /* If we don't have a version for this symbol, see if we can find
2284 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2289 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2290 h
->root
.root
.string
, &hide
);
2291 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2292 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2298 /* Read and swap the relocs from the section indicated by SHDR. This
2299 may be either a REL or a RELA section. The relocations are
2300 translated into RELA relocations and stored in INTERNAL_RELOCS,
2301 which should have already been allocated to contain enough space.
2302 The EXTERNAL_RELOCS are a buffer where the external form of the
2303 relocations should be stored.
2305 Returns FALSE if something goes wrong. */
2308 elf_link_read_relocs_from_section (bfd
*abfd
,
2310 Elf_Internal_Shdr
*shdr
,
2311 void *external_relocs
,
2312 Elf_Internal_Rela
*internal_relocs
)
2314 const struct elf_backend_data
*bed
;
2315 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2316 const bfd_byte
*erela
;
2317 const bfd_byte
*erelaend
;
2318 Elf_Internal_Rela
*irela
;
2319 Elf_Internal_Shdr
*symtab_hdr
;
2322 /* Position ourselves at the start of the section. */
2323 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2326 /* Read the relocations. */
2327 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2330 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2331 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2333 bed
= get_elf_backend_data (abfd
);
2335 /* Convert the external relocations to the internal format. */
2336 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2337 swap_in
= bed
->s
->swap_reloc_in
;
2338 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2339 swap_in
= bed
->s
->swap_reloca_in
;
2342 bfd_set_error (bfd_error_wrong_format
);
2346 erela
= (const bfd_byte
*) external_relocs
;
2347 erelaend
= erela
+ shdr
->sh_size
;
2348 irela
= internal_relocs
;
2349 while (erela
< erelaend
)
2353 (*swap_in
) (abfd
, erela
, irela
);
2354 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2355 if (bed
->s
->arch_size
== 64)
2359 if ((size_t) r_symndx
>= nsyms
)
2362 /* xgettext:c-format */
2363 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2364 " for offset 0x%lx in section `%A'"),
2366 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2367 bfd_set_error (bfd_error_bad_value
);
2371 else if (r_symndx
!= STN_UNDEF
)
2374 /* xgettext:c-format */
2375 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2376 " when the object file has no symbol table"),
2378 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2379 bfd_set_error (bfd_error_bad_value
);
2382 irela
+= bed
->s
->int_rels_per_ext_rel
;
2383 erela
+= shdr
->sh_entsize
;
2389 /* Read and swap the relocs for a section O. They may have been
2390 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2391 not NULL, they are used as buffers to read into. They are known to
2392 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2393 the return value is allocated using either malloc or bfd_alloc,
2394 according to the KEEP_MEMORY argument. If O has two relocation
2395 sections (both REL and RELA relocations), then the REL_HDR
2396 relocations will appear first in INTERNAL_RELOCS, followed by the
2397 RELA_HDR relocations. */
2400 _bfd_elf_link_read_relocs (bfd
*abfd
,
2402 void *external_relocs
,
2403 Elf_Internal_Rela
*internal_relocs
,
2404 bfd_boolean keep_memory
)
2406 void *alloc1
= NULL
;
2407 Elf_Internal_Rela
*alloc2
= NULL
;
2408 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2409 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2410 Elf_Internal_Rela
*internal_rela_relocs
;
2412 if (esdo
->relocs
!= NULL
)
2413 return esdo
->relocs
;
2415 if (o
->reloc_count
== 0)
2418 if (internal_relocs
== NULL
)
2422 size
= o
->reloc_count
;
2423 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2425 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2427 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2428 if (internal_relocs
== NULL
)
2432 if (external_relocs
== NULL
)
2434 bfd_size_type size
= 0;
2437 size
+= esdo
->rel
.hdr
->sh_size
;
2439 size
+= esdo
->rela
.hdr
->sh_size
;
2441 alloc1
= bfd_malloc (size
);
2444 external_relocs
= alloc1
;
2447 internal_rela_relocs
= internal_relocs
;
2450 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2454 external_relocs
= (((bfd_byte
*) external_relocs
)
2455 + esdo
->rel
.hdr
->sh_size
);
2456 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2457 * bed
->s
->int_rels_per_ext_rel
);
2461 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2463 internal_rela_relocs
)))
2466 /* Cache the results for next time, if we can. */
2468 esdo
->relocs
= internal_relocs
;
2473 /* Don't free alloc2, since if it was allocated we are passing it
2474 back (under the name of internal_relocs). */
2476 return internal_relocs
;
2484 bfd_release (abfd
, alloc2
);
2491 /* Compute the size of, and allocate space for, REL_HDR which is the
2492 section header for a section containing relocations for O. */
2495 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2496 struct bfd_elf_section_reloc_data
*reldata
)
2498 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2500 /* That allows us to calculate the size of the section. */
2501 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2503 /* The contents field must last into write_object_contents, so we
2504 allocate it with bfd_alloc rather than malloc. Also since we
2505 cannot be sure that the contents will actually be filled in,
2506 we zero the allocated space. */
2507 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2508 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2511 if (reldata
->hashes
== NULL
&& reldata
->count
)
2513 struct elf_link_hash_entry
**p
;
2515 p
= ((struct elf_link_hash_entry
**)
2516 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2520 reldata
->hashes
= p
;
2526 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2527 originated from the section given by INPUT_REL_HDR) to the
2531 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2532 asection
*input_section
,
2533 Elf_Internal_Shdr
*input_rel_hdr
,
2534 Elf_Internal_Rela
*internal_relocs
,
2535 struct elf_link_hash_entry
**rel_hash
2538 Elf_Internal_Rela
*irela
;
2539 Elf_Internal_Rela
*irelaend
;
2541 struct bfd_elf_section_reloc_data
*output_reldata
;
2542 asection
*output_section
;
2543 const struct elf_backend_data
*bed
;
2544 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2545 struct bfd_elf_section_data
*esdo
;
2547 output_section
= input_section
->output_section
;
2549 bed
= get_elf_backend_data (output_bfd
);
2550 esdo
= elf_section_data (output_section
);
2551 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2553 output_reldata
= &esdo
->rel
;
2554 swap_out
= bed
->s
->swap_reloc_out
;
2556 else if (esdo
->rela
.hdr
2557 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2559 output_reldata
= &esdo
->rela
;
2560 swap_out
= bed
->s
->swap_reloca_out
;
2565 /* xgettext:c-format */
2566 (_("%B: relocation size mismatch in %B section %A"),
2567 output_bfd
, input_section
->owner
, input_section
);
2568 bfd_set_error (bfd_error_wrong_format
);
2572 erel
= output_reldata
->hdr
->contents
;
2573 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2574 irela
= internal_relocs
;
2575 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2576 * bed
->s
->int_rels_per_ext_rel
);
2577 while (irela
< irelaend
)
2579 (*swap_out
) (output_bfd
, irela
, erel
);
2580 irela
+= bed
->s
->int_rels_per_ext_rel
;
2581 erel
+= input_rel_hdr
->sh_entsize
;
2584 /* Bump the counter, so that we know where to add the next set of
2586 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2591 /* Make weak undefined symbols in PIE dynamic. */
2594 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2595 struct elf_link_hash_entry
*h
)
2597 if (bfd_link_pie (info
)
2599 && h
->root
.type
== bfd_link_hash_undefweak
)
2600 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2605 /* Fix up the flags for a symbol. This handles various cases which
2606 can only be fixed after all the input files are seen. This is
2607 currently called by both adjust_dynamic_symbol and
2608 assign_sym_version, which is unnecessary but perhaps more robust in
2609 the face of future changes. */
2612 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2613 struct elf_info_failed
*eif
)
2615 const struct elf_backend_data
*bed
;
2617 /* If this symbol was mentioned in a non-ELF file, try to set
2618 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2619 permit a non-ELF file to correctly refer to a symbol defined in
2620 an ELF dynamic object. */
2623 while (h
->root
.type
== bfd_link_hash_indirect
)
2624 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2626 if (h
->root
.type
!= bfd_link_hash_defined
2627 && h
->root
.type
!= bfd_link_hash_defweak
)
2630 h
->ref_regular_nonweak
= 1;
2634 if (h
->root
.u
.def
.section
->owner
!= NULL
2635 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2636 == bfd_target_elf_flavour
))
2639 h
->ref_regular_nonweak
= 1;
2645 if (h
->dynindx
== -1
2649 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2658 /* Unfortunately, NON_ELF is only correct if the symbol
2659 was first seen in a non-ELF file. Fortunately, if the symbol
2660 was first seen in an ELF file, we're probably OK unless the
2661 symbol was defined in a non-ELF file. Catch that case here.
2662 FIXME: We're still in trouble if the symbol was first seen in
2663 a dynamic object, and then later in a non-ELF regular object. */
2664 if ((h
->root
.type
== bfd_link_hash_defined
2665 || h
->root
.type
== bfd_link_hash_defweak
)
2667 && (h
->root
.u
.def
.section
->owner
!= NULL
2668 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2669 != bfd_target_elf_flavour
)
2670 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2671 && !h
->def_dynamic
)))
2675 /* Backend specific symbol fixup. */
2676 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2677 if (bed
->elf_backend_fixup_symbol
2678 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2681 /* If this is a final link, and the symbol was defined as a common
2682 symbol in a regular object file, and there was no definition in
2683 any dynamic object, then the linker will have allocated space for
2684 the symbol in a common section but the DEF_REGULAR
2685 flag will not have been set. */
2686 if (h
->root
.type
== bfd_link_hash_defined
2690 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2693 /* If a weak undefined symbol has non-default visibility, we also
2694 hide it from the dynamic linker. */
2695 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2696 && h
->root
.type
== bfd_link_hash_undefweak
)
2697 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2699 /* A hidden versioned symbol in executable should be forced local if
2700 it is is locally defined, not referenced by shared library and not
2702 else if (bfd_link_executable (eif
->info
)
2703 && h
->versioned
== versioned_hidden
2704 && !eif
->info
->export_dynamic
2708 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2710 /* If -Bsymbolic was used (which means to bind references to global
2711 symbols to the definition within the shared object), and this
2712 symbol was defined in a regular object, then it actually doesn't
2713 need a PLT entry. Likewise, if the symbol has non-default
2714 visibility. If the symbol has hidden or internal visibility, we
2715 will force it local. */
2716 else if (h
->needs_plt
2717 && bfd_link_pic (eif
->info
)
2718 && is_elf_hash_table (eif
->info
->hash
)
2719 && (SYMBOLIC_BIND (eif
->info
, h
)
2720 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2723 bfd_boolean force_local
;
2725 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2726 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2727 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2730 /* If this is a weak defined symbol in a dynamic object, and we know
2731 the real definition in the dynamic object, copy interesting flags
2732 over to the real definition. */
2733 if (h
->u
.weakdef
!= NULL
)
2735 /* If the real definition is defined by a regular object file,
2736 don't do anything special. See the longer description in
2737 _bfd_elf_adjust_dynamic_symbol, below. */
2738 if (h
->u
.weakdef
->def_regular
)
2739 h
->u
.weakdef
= NULL
;
2742 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2744 while (h
->root
.type
== bfd_link_hash_indirect
)
2745 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2747 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2748 || h
->root
.type
== bfd_link_hash_defweak
);
2749 BFD_ASSERT (weakdef
->def_dynamic
);
2750 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2751 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2752 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2759 /* Make the backend pick a good value for a dynamic symbol. This is
2760 called via elf_link_hash_traverse, and also calls itself
2764 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2766 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2768 const struct elf_backend_data
*bed
;
2770 if (! is_elf_hash_table (eif
->info
->hash
))
2773 /* Ignore indirect symbols. These are added by the versioning code. */
2774 if (h
->root
.type
== bfd_link_hash_indirect
)
2777 /* Fix the symbol flags. */
2778 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2781 /* If this symbol does not require a PLT entry, and it is not
2782 defined by a dynamic object, or is not referenced by a regular
2783 object, ignore it. We do have to handle a weak defined symbol,
2784 even if no regular object refers to it, if we decided to add it
2785 to the dynamic symbol table. FIXME: Do we normally need to worry
2786 about symbols which are defined by one dynamic object and
2787 referenced by another one? */
2789 && h
->type
!= STT_GNU_IFUNC
2793 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2795 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2799 /* If we've already adjusted this symbol, don't do it again. This
2800 can happen via a recursive call. */
2801 if (h
->dynamic_adjusted
)
2804 /* Don't look at this symbol again. Note that we must set this
2805 after checking the above conditions, because we may look at a
2806 symbol once, decide not to do anything, and then get called
2807 recursively later after REF_REGULAR is set below. */
2808 h
->dynamic_adjusted
= 1;
2810 /* If this is a weak definition, and we know a real definition, and
2811 the real symbol is not itself defined by a regular object file,
2812 then get a good value for the real definition. We handle the
2813 real symbol first, for the convenience of the backend routine.
2815 Note that there is a confusing case here. If the real definition
2816 is defined by a regular object file, we don't get the real symbol
2817 from the dynamic object, but we do get the weak symbol. If the
2818 processor backend uses a COPY reloc, then if some routine in the
2819 dynamic object changes the real symbol, we will not see that
2820 change in the corresponding weak symbol. This is the way other
2821 ELF linkers work as well, and seems to be a result of the shared
2824 I will clarify this issue. Most SVR4 shared libraries define the
2825 variable _timezone and define timezone as a weak synonym. The
2826 tzset call changes _timezone. If you write
2827 extern int timezone;
2829 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2830 you might expect that, since timezone is a synonym for _timezone,
2831 the same number will print both times. However, if the processor
2832 backend uses a COPY reloc, then actually timezone will be copied
2833 into your process image, and, since you define _timezone
2834 yourself, _timezone will not. Thus timezone and _timezone will
2835 wind up at different memory locations. The tzset call will set
2836 _timezone, leaving timezone unchanged. */
2838 if (h
->u
.weakdef
!= NULL
)
2840 /* If we get to this point, there is an implicit reference to
2841 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2842 h
->u
.weakdef
->ref_regular
= 1;
2844 /* Ensure that the backend adjust_dynamic_symbol function sees
2845 H->U.WEAKDEF before H by recursively calling ourselves. */
2846 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2850 /* If a symbol has no type and no size and does not require a PLT
2851 entry, then we are probably about to do the wrong thing here: we
2852 are probably going to create a COPY reloc for an empty object.
2853 This case can arise when a shared object is built with assembly
2854 code, and the assembly code fails to set the symbol type. */
2856 && h
->type
== STT_NOTYPE
2859 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2860 h
->root
.root
.string
);
2862 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2863 bed
= get_elf_backend_data (dynobj
);
2865 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2874 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2878 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2879 struct elf_link_hash_entry
*h
,
2882 unsigned int power_of_two
;
2884 asection
*sec
= h
->root
.u
.def
.section
;
2886 /* The section aligment of definition is the maximum alignment
2887 requirement of symbols defined in the section. Since we don't
2888 know the symbol alignment requirement, we start with the
2889 maximum alignment and check low bits of the symbol address
2890 for the minimum alignment. */
2891 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2892 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2893 while ((h
->root
.u
.def
.value
& mask
) != 0)
2899 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2902 /* Adjust the section alignment if needed. */
2903 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2908 /* We make sure that the symbol will be aligned properly. */
2909 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2911 /* Define the symbol as being at this point in DYNBSS. */
2912 h
->root
.u
.def
.section
= dynbss
;
2913 h
->root
.u
.def
.value
= dynbss
->size
;
2915 /* Increment the size of DYNBSS to make room for the symbol. */
2916 dynbss
->size
+= h
->size
;
2918 /* No error if extern_protected_data is true. */
2919 if (h
->protected_def
2920 && (!info
->extern_protected_data
2921 || (info
->extern_protected_data
< 0
2922 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2923 info
->callbacks
->einfo
2924 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2925 h
->root
.root
.string
);
2930 /* Adjust all external symbols pointing into SEC_MERGE sections
2931 to reflect the object merging within the sections. */
2934 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2938 if ((h
->root
.type
== bfd_link_hash_defined
2939 || h
->root
.type
== bfd_link_hash_defweak
)
2940 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2941 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2943 bfd
*output_bfd
= (bfd
*) data
;
2945 h
->root
.u
.def
.value
=
2946 _bfd_merged_section_offset (output_bfd
,
2947 &h
->root
.u
.def
.section
,
2948 elf_section_data (sec
)->sec_info
,
2949 h
->root
.u
.def
.value
);
2955 /* Returns false if the symbol referred to by H should be considered
2956 to resolve local to the current module, and true if it should be
2957 considered to bind dynamically. */
2960 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2961 struct bfd_link_info
*info
,
2962 bfd_boolean not_local_protected
)
2964 bfd_boolean binding_stays_local_p
;
2965 const struct elf_backend_data
*bed
;
2966 struct elf_link_hash_table
*hash_table
;
2971 while (h
->root
.type
== bfd_link_hash_indirect
2972 || h
->root
.type
== bfd_link_hash_warning
)
2973 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2975 /* If it was forced local, then clearly it's not dynamic. */
2976 if (h
->dynindx
== -1)
2978 if (h
->forced_local
)
2981 /* Identify the cases where name binding rules say that a
2982 visible symbol resolves locally. */
2983 binding_stays_local_p
= (bfd_link_executable (info
)
2984 || SYMBOLIC_BIND (info
, h
));
2986 switch (ELF_ST_VISIBILITY (h
->other
))
2993 hash_table
= elf_hash_table (info
);
2994 if (!is_elf_hash_table (hash_table
))
2997 bed
= get_elf_backend_data (hash_table
->dynobj
);
2999 /* Proper resolution for function pointer equality may require
3000 that these symbols perhaps be resolved dynamically, even though
3001 we should be resolving them to the current module. */
3002 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3003 binding_stays_local_p
= TRUE
;
3010 /* If it isn't defined locally, then clearly it's dynamic. */
3011 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3014 /* Otherwise, the symbol is dynamic if binding rules don't tell
3015 us that it remains local. */
3016 return !binding_stays_local_p
;
3019 /* Return true if the symbol referred to by H should be considered
3020 to resolve local to the current module, and false otherwise. Differs
3021 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3022 undefined symbols. The two functions are virtually identical except
3023 for the place where forced_local and dynindx == -1 are tested. If
3024 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3025 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3026 the symbol is local only for defined symbols.
3027 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3028 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3029 treatment of undefined weak symbols. For those that do not make
3030 undefined weak symbols dynamic, both functions may return false. */
3033 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3034 struct bfd_link_info
*info
,
3035 bfd_boolean local_protected
)
3037 const struct elf_backend_data
*bed
;
3038 struct elf_link_hash_table
*hash_table
;
3040 /* If it's a local sym, of course we resolve locally. */
3044 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3045 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3046 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3049 /* Common symbols that become definitions don't get the DEF_REGULAR
3050 flag set, so test it first, and don't bail out. */
3051 if (ELF_COMMON_DEF_P (h
))
3053 /* If we don't have a definition in a regular file, then we can't
3054 resolve locally. The sym is either undefined or dynamic. */
3055 else if (!h
->def_regular
)
3058 /* Forced local symbols resolve locally. */
3059 if (h
->forced_local
)
3062 /* As do non-dynamic symbols. */
3063 if (h
->dynindx
== -1)
3066 /* At this point, we know the symbol is defined and dynamic. In an
3067 executable it must resolve locally, likewise when building symbolic
3068 shared libraries. */
3069 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3072 /* Now deal with defined dynamic symbols in shared libraries. Ones
3073 with default visibility might not resolve locally. */
3074 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3077 hash_table
= elf_hash_table (info
);
3078 if (!is_elf_hash_table (hash_table
))
3081 bed
= get_elf_backend_data (hash_table
->dynobj
);
3083 /* If extern_protected_data is false, STV_PROTECTED non-function
3084 symbols are local. */
3085 if ((!info
->extern_protected_data
3086 || (info
->extern_protected_data
< 0
3087 && !bed
->extern_protected_data
))
3088 && !bed
->is_function_type (h
->type
))
3091 /* Function pointer equality tests may require that STV_PROTECTED
3092 symbols be treated as dynamic symbols. If the address of a
3093 function not defined in an executable is set to that function's
3094 plt entry in the executable, then the address of the function in
3095 a shared library must also be the plt entry in the executable. */
3096 return local_protected
;
3099 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3100 aligned. Returns the first TLS output section. */
3102 struct bfd_section
*
3103 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3105 struct bfd_section
*sec
, *tls
;
3106 unsigned int align
= 0;
3108 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3109 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3113 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3114 if (sec
->alignment_power
> align
)
3115 align
= sec
->alignment_power
;
3117 elf_hash_table (info
)->tls_sec
= tls
;
3119 /* Ensure the alignment of the first section is the largest alignment,
3120 so that the tls segment starts aligned. */
3122 tls
->alignment_power
= align
;
3127 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3129 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3130 Elf_Internal_Sym
*sym
)
3132 const struct elf_backend_data
*bed
;
3134 /* Local symbols do not count, but target specific ones might. */
3135 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3136 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3139 bed
= get_elf_backend_data (abfd
);
3140 /* Function symbols do not count. */
3141 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3144 /* If the section is undefined, then so is the symbol. */
3145 if (sym
->st_shndx
== SHN_UNDEF
)
3148 /* If the symbol is defined in the common section, then
3149 it is a common definition and so does not count. */
3150 if (bed
->common_definition (sym
))
3153 /* If the symbol is in a target specific section then we
3154 must rely upon the backend to tell us what it is. */
3155 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3156 /* FIXME - this function is not coded yet:
3158 return _bfd_is_global_symbol_definition (abfd, sym);
3160 Instead for now assume that the definition is not global,
3161 Even if this is wrong, at least the linker will behave
3162 in the same way that it used to do. */
3168 /* Search the symbol table of the archive element of the archive ABFD
3169 whose archive map contains a mention of SYMDEF, and determine if
3170 the symbol is defined in this element. */
3172 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3174 Elf_Internal_Shdr
* hdr
;
3178 Elf_Internal_Sym
*isymbuf
;
3179 Elf_Internal_Sym
*isym
;
3180 Elf_Internal_Sym
*isymend
;
3183 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3187 if (! bfd_check_format (abfd
, bfd_object
))
3190 /* Select the appropriate symbol table. If we don't know if the
3191 object file is an IR object, give linker LTO plugin a chance to
3192 get the correct symbol table. */
3193 if (abfd
->plugin_format
== bfd_plugin_yes
3194 #if BFD_SUPPORTS_PLUGINS
3195 || (abfd
->plugin_format
== bfd_plugin_unknown
3196 && bfd_link_plugin_object_p (abfd
))
3200 /* Use the IR symbol table if the object has been claimed by
3202 abfd
= abfd
->plugin_dummy_bfd
;
3203 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3205 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3206 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3208 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3210 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3212 /* The sh_info field of the symtab header tells us where the
3213 external symbols start. We don't care about the local symbols. */
3214 if (elf_bad_symtab (abfd
))
3216 extsymcount
= symcount
;
3221 extsymcount
= symcount
- hdr
->sh_info
;
3222 extsymoff
= hdr
->sh_info
;
3225 if (extsymcount
== 0)
3228 /* Read in the symbol table. */
3229 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3231 if (isymbuf
== NULL
)
3234 /* Scan the symbol table looking for SYMDEF. */
3236 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3240 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3245 if (strcmp (name
, symdef
->name
) == 0)
3247 result
= is_global_data_symbol_definition (abfd
, isym
);
3257 /* Add an entry to the .dynamic table. */
3260 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3264 struct elf_link_hash_table
*hash_table
;
3265 const struct elf_backend_data
*bed
;
3267 bfd_size_type newsize
;
3268 bfd_byte
*newcontents
;
3269 Elf_Internal_Dyn dyn
;
3271 hash_table
= elf_hash_table (info
);
3272 if (! is_elf_hash_table (hash_table
))
3275 bed
= get_elf_backend_data (hash_table
->dynobj
);
3276 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3277 BFD_ASSERT (s
!= NULL
);
3279 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3280 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3281 if (newcontents
== NULL
)
3285 dyn
.d_un
.d_val
= val
;
3286 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3289 s
->contents
= newcontents
;
3294 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3295 otherwise just check whether one already exists. Returns -1 on error,
3296 1 if a DT_NEEDED tag already exists, and 0 on success. */
3299 elf_add_dt_needed_tag (bfd
*abfd
,
3300 struct bfd_link_info
*info
,
3304 struct elf_link_hash_table
*hash_table
;
3307 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3310 hash_table
= elf_hash_table (info
);
3311 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3312 if (strindex
== (size_t) -1)
3315 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3318 const struct elf_backend_data
*bed
;
3321 bed
= get_elf_backend_data (hash_table
->dynobj
);
3322 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3324 for (extdyn
= sdyn
->contents
;
3325 extdyn
< sdyn
->contents
+ sdyn
->size
;
3326 extdyn
+= bed
->s
->sizeof_dyn
)
3328 Elf_Internal_Dyn dyn
;
3330 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3331 if (dyn
.d_tag
== DT_NEEDED
3332 && dyn
.d_un
.d_val
== strindex
)
3334 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3342 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3345 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3349 /* We were just checking for existence of the tag. */
3350 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3355 /* Return true if SONAME is on the needed list between NEEDED and STOP
3356 (or the end of list if STOP is NULL), and needed by a library that
3360 on_needed_list (const char *soname
,
3361 struct bfd_link_needed_list
*needed
,
3362 struct bfd_link_needed_list
*stop
)
3364 struct bfd_link_needed_list
*look
;
3365 for (look
= needed
; look
!= stop
; look
= look
->next
)
3366 if (strcmp (soname
, look
->name
) == 0
3367 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3368 /* If needed by a library that itself is not directly
3369 needed, recursively check whether that library is
3370 indirectly needed. Since we add DT_NEEDED entries to
3371 the end of the list, library dependencies appear after
3372 the library. Therefore search prior to the current
3373 LOOK, preventing possible infinite recursion. */
3374 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3380 /* Sort symbol by value, section, and size. */
3382 elf_sort_symbol (const void *arg1
, const void *arg2
)
3384 const struct elf_link_hash_entry
*h1
;
3385 const struct elf_link_hash_entry
*h2
;
3386 bfd_signed_vma vdiff
;
3388 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3389 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3390 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3392 return vdiff
> 0 ? 1 : -1;
3395 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3397 return sdiff
> 0 ? 1 : -1;
3399 vdiff
= h1
->size
- h2
->size
;
3400 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3403 /* This function is used to adjust offsets into .dynstr for
3404 dynamic symbols. This is called via elf_link_hash_traverse. */
3407 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3409 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3411 if (h
->dynindx
!= -1)
3412 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3416 /* Assign string offsets in .dynstr, update all structures referencing
3420 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3422 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3423 struct elf_link_local_dynamic_entry
*entry
;
3424 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3425 bfd
*dynobj
= hash_table
->dynobj
;
3428 const struct elf_backend_data
*bed
;
3431 _bfd_elf_strtab_finalize (dynstr
);
3432 size
= _bfd_elf_strtab_size (dynstr
);
3434 bed
= get_elf_backend_data (dynobj
);
3435 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3436 BFD_ASSERT (sdyn
!= NULL
);
3438 /* Update all .dynamic entries referencing .dynstr strings. */
3439 for (extdyn
= sdyn
->contents
;
3440 extdyn
< sdyn
->contents
+ sdyn
->size
;
3441 extdyn
+= bed
->s
->sizeof_dyn
)
3443 Elf_Internal_Dyn dyn
;
3445 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3449 dyn
.d_un
.d_val
= size
;
3459 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3464 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3467 /* Now update local dynamic symbols. */
3468 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3469 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3470 entry
->isym
.st_name
);
3472 /* And the rest of dynamic symbols. */
3473 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3475 /* Adjust version definitions. */
3476 if (elf_tdata (output_bfd
)->cverdefs
)
3481 Elf_Internal_Verdef def
;
3482 Elf_Internal_Verdaux defaux
;
3484 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3488 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3490 p
+= sizeof (Elf_External_Verdef
);
3491 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3493 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3495 _bfd_elf_swap_verdaux_in (output_bfd
,
3496 (Elf_External_Verdaux
*) p
, &defaux
);
3497 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3499 _bfd_elf_swap_verdaux_out (output_bfd
,
3500 &defaux
, (Elf_External_Verdaux
*) p
);
3501 p
+= sizeof (Elf_External_Verdaux
);
3504 while (def
.vd_next
);
3507 /* Adjust version references. */
3508 if (elf_tdata (output_bfd
)->verref
)
3513 Elf_Internal_Verneed need
;
3514 Elf_Internal_Vernaux needaux
;
3516 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3520 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3522 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3523 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3524 (Elf_External_Verneed
*) p
);
3525 p
+= sizeof (Elf_External_Verneed
);
3526 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3528 _bfd_elf_swap_vernaux_in (output_bfd
,
3529 (Elf_External_Vernaux
*) p
, &needaux
);
3530 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3532 _bfd_elf_swap_vernaux_out (output_bfd
,
3534 (Elf_External_Vernaux
*) p
);
3535 p
+= sizeof (Elf_External_Vernaux
);
3538 while (need
.vn_next
);
3544 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3545 The default is to only match when the INPUT and OUTPUT are exactly
3549 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3550 const bfd_target
*output
)
3552 return input
== output
;
3555 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3556 This version is used when different targets for the same architecture
3557 are virtually identical. */
3560 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3561 const bfd_target
*output
)
3563 const struct elf_backend_data
*obed
, *ibed
;
3565 if (input
== output
)
3568 ibed
= xvec_get_elf_backend_data (input
);
3569 obed
= xvec_get_elf_backend_data (output
);
3571 if (ibed
->arch
!= obed
->arch
)
3574 /* If both backends are using this function, deem them compatible. */
3575 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3578 /* Make a special call to the linker "notice" function to tell it that
3579 we are about to handle an as-needed lib, or have finished
3580 processing the lib. */
3583 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3584 struct bfd_link_info
*info
,
3585 enum notice_asneeded_action act
)
3587 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3590 /* Check relocations an ELF object file. */
3593 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3595 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3596 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3598 /* If this object is the same format as the output object, and it is
3599 not a shared library, then let the backend look through the
3602 This is required to build global offset table entries and to
3603 arrange for dynamic relocs. It is not required for the
3604 particular common case of linking non PIC code, even when linking
3605 against shared libraries, but unfortunately there is no way of
3606 knowing whether an object file has been compiled PIC or not.
3607 Looking through the relocs is not particularly time consuming.
3608 The problem is that we must either (1) keep the relocs in memory,
3609 which causes the linker to require additional runtime memory or
3610 (2) read the relocs twice from the input file, which wastes time.
3611 This would be a good case for using mmap.
3613 I have no idea how to handle linking PIC code into a file of a
3614 different format. It probably can't be done. */
3615 if ((abfd
->flags
& DYNAMIC
) == 0
3616 && is_elf_hash_table (htab
)
3617 && bed
->check_relocs
!= NULL
3618 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3619 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3623 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3625 Elf_Internal_Rela
*internal_relocs
;
3628 /* Don't check relocations in excluded sections. */
3629 if ((o
->flags
& SEC_RELOC
) == 0
3630 || (o
->flags
& SEC_EXCLUDE
) != 0
3631 || o
->reloc_count
== 0
3632 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3633 && (o
->flags
& SEC_DEBUGGING
) != 0)
3634 || bfd_is_abs_section (o
->output_section
))
3637 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3639 if (internal_relocs
== NULL
)
3642 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3644 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3645 free (internal_relocs
);
3655 /* Add symbols from an ELF object file to the linker hash table. */
3658 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3660 Elf_Internal_Ehdr
*ehdr
;
3661 Elf_Internal_Shdr
*hdr
;
3665 struct elf_link_hash_entry
**sym_hash
;
3666 bfd_boolean dynamic
;
3667 Elf_External_Versym
*extversym
= NULL
;
3668 Elf_External_Versym
*ever
;
3669 struct elf_link_hash_entry
*weaks
;
3670 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3671 size_t nondeflt_vers_cnt
= 0;
3672 Elf_Internal_Sym
*isymbuf
= NULL
;
3673 Elf_Internal_Sym
*isym
;
3674 Elf_Internal_Sym
*isymend
;
3675 const struct elf_backend_data
*bed
;
3676 bfd_boolean add_needed
;
3677 struct elf_link_hash_table
*htab
;
3679 void *alloc_mark
= NULL
;
3680 struct bfd_hash_entry
**old_table
= NULL
;
3681 unsigned int old_size
= 0;
3682 unsigned int old_count
= 0;
3683 void *old_tab
= NULL
;
3685 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3686 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3687 void *old_strtab
= NULL
;
3690 bfd_boolean just_syms
;
3692 htab
= elf_hash_table (info
);
3693 bed
= get_elf_backend_data (abfd
);
3695 if ((abfd
->flags
& DYNAMIC
) == 0)
3701 /* You can't use -r against a dynamic object. Also, there's no
3702 hope of using a dynamic object which does not exactly match
3703 the format of the output file. */
3704 if (bfd_link_relocatable (info
)
3705 || !is_elf_hash_table (htab
)
3706 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3708 if (bfd_link_relocatable (info
))
3709 bfd_set_error (bfd_error_invalid_operation
);
3711 bfd_set_error (bfd_error_wrong_format
);
3716 ehdr
= elf_elfheader (abfd
);
3717 if (info
->warn_alternate_em
3718 && bed
->elf_machine_code
!= ehdr
->e_machine
3719 && ((bed
->elf_machine_alt1
!= 0
3720 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3721 || (bed
->elf_machine_alt2
!= 0
3722 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3723 info
->callbacks
->einfo
3724 /* xgettext:c-format */
3725 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3726 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3728 /* As a GNU extension, any input sections which are named
3729 .gnu.warning.SYMBOL are treated as warning symbols for the given
3730 symbol. This differs from .gnu.warning sections, which generate
3731 warnings when they are included in an output file. */
3732 /* PR 12761: Also generate this warning when building shared libraries. */
3733 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3737 name
= bfd_get_section_name (abfd
, s
);
3738 if (CONST_STRNEQ (name
, ".gnu.warning."))
3743 name
+= sizeof ".gnu.warning." - 1;
3745 /* If this is a shared object, then look up the symbol
3746 in the hash table. If it is there, and it is already
3747 been defined, then we will not be using the entry
3748 from this shared object, so we don't need to warn.
3749 FIXME: If we see the definition in a regular object
3750 later on, we will warn, but we shouldn't. The only
3751 fix is to keep track of what warnings we are supposed
3752 to emit, and then handle them all at the end of the
3756 struct elf_link_hash_entry
*h
;
3758 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3760 /* FIXME: What about bfd_link_hash_common? */
3762 && (h
->root
.type
== bfd_link_hash_defined
3763 || h
->root
.type
== bfd_link_hash_defweak
))
3768 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3772 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3777 if (! (_bfd_generic_link_add_one_symbol
3778 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3779 FALSE
, bed
->collect
, NULL
)))
3782 if (bfd_link_executable (info
))
3784 /* Clobber the section size so that the warning does
3785 not get copied into the output file. */
3788 /* Also set SEC_EXCLUDE, so that symbols defined in
3789 the warning section don't get copied to the output. */
3790 s
->flags
|= SEC_EXCLUDE
;
3795 just_syms
= ((s
= abfd
->sections
) != NULL
3796 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3801 /* If we are creating a shared library, create all the dynamic
3802 sections immediately. We need to attach them to something,
3803 so we attach them to this BFD, provided it is the right
3804 format and is not from ld --just-symbols. Always create the
3805 dynamic sections for -E/--dynamic-list. FIXME: If there
3806 are no input BFD's of the same format as the output, we can't
3807 make a shared library. */
3809 && (bfd_link_pic (info
)
3810 || (!bfd_link_relocatable (info
)
3811 && (info
->export_dynamic
|| info
->dynamic
)))
3812 && is_elf_hash_table (htab
)
3813 && info
->output_bfd
->xvec
== abfd
->xvec
3814 && !htab
->dynamic_sections_created
)
3816 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3820 else if (!is_elf_hash_table (htab
))
3824 const char *soname
= NULL
;
3826 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3827 const Elf_Internal_Phdr
*phdr
;
3830 /* ld --just-symbols and dynamic objects don't mix very well.
3831 ld shouldn't allow it. */
3835 /* If this dynamic lib was specified on the command line with
3836 --as-needed in effect, then we don't want to add a DT_NEEDED
3837 tag unless the lib is actually used. Similary for libs brought
3838 in by another lib's DT_NEEDED. When --no-add-needed is used
3839 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3840 any dynamic library in DT_NEEDED tags in the dynamic lib at
3842 add_needed
= (elf_dyn_lib_class (abfd
)
3843 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3844 | DYN_NO_NEEDED
)) == 0;
3846 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3851 unsigned int elfsec
;
3852 unsigned long shlink
;
3854 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3861 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3862 if (elfsec
== SHN_BAD
)
3863 goto error_free_dyn
;
3864 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3866 for (extdyn
= dynbuf
;
3867 extdyn
< dynbuf
+ s
->size
;
3868 extdyn
+= bed
->s
->sizeof_dyn
)
3870 Elf_Internal_Dyn dyn
;
3872 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3873 if (dyn
.d_tag
== DT_SONAME
)
3875 unsigned int tagv
= dyn
.d_un
.d_val
;
3876 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3878 goto error_free_dyn
;
3880 if (dyn
.d_tag
== DT_NEEDED
)
3882 struct bfd_link_needed_list
*n
, **pn
;
3884 unsigned int tagv
= dyn
.d_un
.d_val
;
3886 amt
= sizeof (struct bfd_link_needed_list
);
3887 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3888 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3889 if (n
== NULL
|| fnm
== NULL
)
3890 goto error_free_dyn
;
3891 amt
= strlen (fnm
) + 1;
3892 anm
= (char *) bfd_alloc (abfd
, amt
);
3894 goto error_free_dyn
;
3895 memcpy (anm
, fnm
, amt
);
3899 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3903 if (dyn
.d_tag
== DT_RUNPATH
)
3905 struct bfd_link_needed_list
*n
, **pn
;
3907 unsigned int tagv
= dyn
.d_un
.d_val
;
3909 amt
= sizeof (struct bfd_link_needed_list
);
3910 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3911 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3912 if (n
== NULL
|| fnm
== NULL
)
3913 goto error_free_dyn
;
3914 amt
= strlen (fnm
) + 1;
3915 anm
= (char *) bfd_alloc (abfd
, amt
);
3917 goto error_free_dyn
;
3918 memcpy (anm
, fnm
, amt
);
3922 for (pn
= & runpath
;
3928 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3929 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3931 struct bfd_link_needed_list
*n
, **pn
;
3933 unsigned int tagv
= dyn
.d_un
.d_val
;
3935 amt
= sizeof (struct bfd_link_needed_list
);
3936 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3937 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3938 if (n
== NULL
|| fnm
== NULL
)
3939 goto error_free_dyn
;
3940 amt
= strlen (fnm
) + 1;
3941 anm
= (char *) bfd_alloc (abfd
, amt
);
3943 goto error_free_dyn
;
3944 memcpy (anm
, fnm
, amt
);
3954 if (dyn
.d_tag
== DT_AUDIT
)
3956 unsigned int tagv
= dyn
.d_un
.d_val
;
3957 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3964 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3965 frees all more recently bfd_alloc'd blocks as well. */
3971 struct bfd_link_needed_list
**pn
;
3972 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3977 /* If we have a PT_GNU_RELRO program header, mark as read-only
3978 all sections contained fully therein. This makes relro
3979 shared library sections appear as they will at run-time. */
3980 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
3981 while (--phdr
>= elf_tdata (abfd
)->phdr
)
3982 if (phdr
->p_type
== PT_GNU_RELRO
)
3984 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3985 if ((s
->flags
& SEC_ALLOC
) != 0
3986 && s
->vma
>= phdr
->p_vaddr
3987 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
3988 s
->flags
|= SEC_READONLY
;
3992 /* We do not want to include any of the sections in a dynamic
3993 object in the output file. We hack by simply clobbering the
3994 list of sections in the BFD. This could be handled more
3995 cleanly by, say, a new section flag; the existing
3996 SEC_NEVER_LOAD flag is not the one we want, because that one
3997 still implies that the section takes up space in the output
3999 bfd_section_list_clear (abfd
);
4001 /* Find the name to use in a DT_NEEDED entry that refers to this
4002 object. If the object has a DT_SONAME entry, we use it.
4003 Otherwise, if the generic linker stuck something in
4004 elf_dt_name, we use that. Otherwise, we just use the file
4006 if (soname
== NULL
|| *soname
== '\0')
4008 soname
= elf_dt_name (abfd
);
4009 if (soname
== NULL
|| *soname
== '\0')
4010 soname
= bfd_get_filename (abfd
);
4013 /* Save the SONAME because sometimes the linker emulation code
4014 will need to know it. */
4015 elf_dt_name (abfd
) = soname
;
4017 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4021 /* If we have already included this dynamic object in the
4022 link, just ignore it. There is no reason to include a
4023 particular dynamic object more than once. */
4027 /* Save the DT_AUDIT entry for the linker emulation code. */
4028 elf_dt_audit (abfd
) = audit
;
4031 /* If this is a dynamic object, we always link against the .dynsym
4032 symbol table, not the .symtab symbol table. The dynamic linker
4033 will only see the .dynsym symbol table, so there is no reason to
4034 look at .symtab for a dynamic object. */
4036 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4037 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4039 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4041 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4043 /* The sh_info field of the symtab header tells us where the
4044 external symbols start. We don't care about the local symbols at
4046 if (elf_bad_symtab (abfd
))
4048 extsymcount
= symcount
;
4053 extsymcount
= symcount
- hdr
->sh_info
;
4054 extsymoff
= hdr
->sh_info
;
4057 sym_hash
= elf_sym_hashes (abfd
);
4058 if (extsymcount
!= 0)
4060 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4062 if (isymbuf
== NULL
)
4065 if (sym_hash
== NULL
)
4067 /* We store a pointer to the hash table entry for each
4070 amt
*= sizeof (struct elf_link_hash_entry
*);
4071 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4072 if (sym_hash
== NULL
)
4073 goto error_free_sym
;
4074 elf_sym_hashes (abfd
) = sym_hash
;
4080 /* Read in any version definitions. */
4081 if (!_bfd_elf_slurp_version_tables (abfd
,
4082 info
->default_imported_symver
))
4083 goto error_free_sym
;
4085 /* Read in the symbol versions, but don't bother to convert them
4086 to internal format. */
4087 if (elf_dynversym (abfd
) != 0)
4089 Elf_Internal_Shdr
*versymhdr
;
4091 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4092 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4093 if (extversym
== NULL
)
4094 goto error_free_sym
;
4095 amt
= versymhdr
->sh_size
;
4096 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4097 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4098 goto error_free_vers
;
4102 /* If we are loading an as-needed shared lib, save the symbol table
4103 state before we start adding symbols. If the lib turns out
4104 to be unneeded, restore the state. */
4105 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4110 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4112 struct bfd_hash_entry
*p
;
4113 struct elf_link_hash_entry
*h
;
4115 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4117 h
= (struct elf_link_hash_entry
*) p
;
4118 entsize
+= htab
->root
.table
.entsize
;
4119 if (h
->root
.type
== bfd_link_hash_warning
)
4120 entsize
+= htab
->root
.table
.entsize
;
4124 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4125 old_tab
= bfd_malloc (tabsize
+ entsize
);
4126 if (old_tab
== NULL
)
4127 goto error_free_vers
;
4129 /* Remember the current objalloc pointer, so that all mem for
4130 symbols added can later be reclaimed. */
4131 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4132 if (alloc_mark
== NULL
)
4133 goto error_free_vers
;
4135 /* Make a special call to the linker "notice" function to
4136 tell it that we are about to handle an as-needed lib. */
4137 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4138 goto error_free_vers
;
4140 /* Clone the symbol table. Remember some pointers into the
4141 symbol table, and dynamic symbol count. */
4142 old_ent
= (char *) old_tab
+ tabsize
;
4143 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4144 old_undefs
= htab
->root
.undefs
;
4145 old_undefs_tail
= htab
->root
.undefs_tail
;
4146 old_table
= htab
->root
.table
.table
;
4147 old_size
= htab
->root
.table
.size
;
4148 old_count
= htab
->root
.table
.count
;
4149 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4150 if (old_strtab
== NULL
)
4151 goto error_free_vers
;
4153 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4155 struct bfd_hash_entry
*p
;
4156 struct elf_link_hash_entry
*h
;
4158 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4160 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4161 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4162 h
= (struct elf_link_hash_entry
*) p
;
4163 if (h
->root
.type
== bfd_link_hash_warning
)
4165 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4166 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4173 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4174 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4176 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4180 asection
*sec
, *new_sec
;
4183 struct elf_link_hash_entry
*h
;
4184 struct elf_link_hash_entry
*hi
;
4185 bfd_boolean definition
;
4186 bfd_boolean size_change_ok
;
4187 bfd_boolean type_change_ok
;
4188 bfd_boolean new_weakdef
;
4189 bfd_boolean new_weak
;
4190 bfd_boolean old_weak
;
4191 bfd_boolean override
;
4193 bfd_boolean discarded
;
4194 unsigned int old_alignment
;
4196 bfd_boolean matched
;
4200 flags
= BSF_NO_FLAGS
;
4202 value
= isym
->st_value
;
4203 common
= bed
->common_definition (isym
);
4206 bind
= ELF_ST_BIND (isym
->st_info
);
4210 /* This should be impossible, since ELF requires that all
4211 global symbols follow all local symbols, and that sh_info
4212 point to the first global symbol. Unfortunately, Irix 5
4217 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4225 case STB_GNU_UNIQUE
:
4226 flags
= BSF_GNU_UNIQUE
;
4230 /* Leave it up to the processor backend. */
4234 if (isym
->st_shndx
== SHN_UNDEF
)
4235 sec
= bfd_und_section_ptr
;
4236 else if (isym
->st_shndx
== SHN_ABS
)
4237 sec
= bfd_abs_section_ptr
;
4238 else if (isym
->st_shndx
== SHN_COMMON
)
4240 sec
= bfd_com_section_ptr
;
4241 /* What ELF calls the size we call the value. What ELF
4242 calls the value we call the alignment. */
4243 value
= isym
->st_size
;
4247 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4249 sec
= bfd_abs_section_ptr
;
4250 else if (discarded_section (sec
))
4252 /* Symbols from discarded section are undefined. We keep
4254 sec
= bfd_und_section_ptr
;
4256 isym
->st_shndx
= SHN_UNDEF
;
4258 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4262 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4265 goto error_free_vers
;
4267 if (isym
->st_shndx
== SHN_COMMON
4268 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4270 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4274 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4276 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4278 goto error_free_vers
;
4282 else if (isym
->st_shndx
== SHN_COMMON
4283 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4284 && !bfd_link_relocatable (info
))
4286 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4290 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4291 | SEC_LINKER_CREATED
);
4292 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4294 goto error_free_vers
;
4298 else if (bed
->elf_add_symbol_hook
)
4300 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4302 goto error_free_vers
;
4304 /* The hook function sets the name to NULL if this symbol
4305 should be skipped for some reason. */
4310 /* Sanity check that all possibilities were handled. */
4313 bfd_set_error (bfd_error_bad_value
);
4314 goto error_free_vers
;
4317 /* Silently discard TLS symbols from --just-syms. There's
4318 no way to combine a static TLS block with a new TLS block
4319 for this executable. */
4320 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4321 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4324 if (bfd_is_und_section (sec
)
4325 || bfd_is_com_section (sec
))
4330 size_change_ok
= FALSE
;
4331 type_change_ok
= bed
->type_change_ok
;
4338 if (is_elf_hash_table (htab
))
4340 Elf_Internal_Versym iver
;
4341 unsigned int vernum
= 0;
4346 if (info
->default_imported_symver
)
4347 /* Use the default symbol version created earlier. */
4348 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4353 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4355 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4357 /* If this is a hidden symbol, or if it is not version
4358 1, we append the version name to the symbol name.
4359 However, we do not modify a non-hidden absolute symbol
4360 if it is not a function, because it might be the version
4361 symbol itself. FIXME: What if it isn't? */
4362 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4364 && (!bfd_is_abs_section (sec
)
4365 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4368 size_t namelen
, verlen
, newlen
;
4371 if (isym
->st_shndx
!= SHN_UNDEF
)
4373 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4375 else if (vernum
> 1)
4377 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4384 /* xgettext:c-format */
4385 (_("%B: %s: invalid version %u (max %d)"),
4387 elf_tdata (abfd
)->cverdefs
);
4388 bfd_set_error (bfd_error_bad_value
);
4389 goto error_free_vers
;
4394 /* We cannot simply test for the number of
4395 entries in the VERNEED section since the
4396 numbers for the needed versions do not start
4398 Elf_Internal_Verneed
*t
;
4401 for (t
= elf_tdata (abfd
)->verref
;
4405 Elf_Internal_Vernaux
*a
;
4407 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4409 if (a
->vna_other
== vernum
)
4411 verstr
= a
->vna_nodename
;
4421 /* xgettext:c-format */
4422 (_("%B: %s: invalid needed version %d"),
4423 abfd
, name
, vernum
);
4424 bfd_set_error (bfd_error_bad_value
);
4425 goto error_free_vers
;
4429 namelen
= strlen (name
);
4430 verlen
= strlen (verstr
);
4431 newlen
= namelen
+ verlen
+ 2;
4432 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4433 && isym
->st_shndx
!= SHN_UNDEF
)
4436 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4437 if (newname
== NULL
)
4438 goto error_free_vers
;
4439 memcpy (newname
, name
, namelen
);
4440 p
= newname
+ namelen
;
4442 /* If this is a defined non-hidden version symbol,
4443 we add another @ to the name. This indicates the
4444 default version of the symbol. */
4445 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4446 && isym
->st_shndx
!= SHN_UNDEF
)
4448 memcpy (p
, verstr
, verlen
+ 1);
4453 /* If this symbol has default visibility and the user has
4454 requested we not re-export it, then mark it as hidden. */
4455 if (!bfd_is_und_section (sec
)
4458 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4459 isym
->st_other
= (STV_HIDDEN
4460 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4462 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4463 sym_hash
, &old_bfd
, &old_weak
,
4464 &old_alignment
, &skip
, &override
,
4465 &type_change_ok
, &size_change_ok
,
4467 goto error_free_vers
;
4472 /* Override a definition only if the new symbol matches the
4474 if (override
&& matched
)
4478 while (h
->root
.type
== bfd_link_hash_indirect
4479 || h
->root
.type
== bfd_link_hash_warning
)
4480 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4482 if (elf_tdata (abfd
)->verdef
!= NULL
4485 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4488 if (! (_bfd_generic_link_add_one_symbol
4489 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4490 (struct bfd_link_hash_entry
**) sym_hash
)))
4491 goto error_free_vers
;
4493 if ((flags
& BSF_GNU_UNIQUE
)
4494 && (abfd
->flags
& DYNAMIC
) == 0
4495 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4496 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4499 /* We need to make sure that indirect symbol dynamic flags are
4502 while (h
->root
.type
== bfd_link_hash_indirect
4503 || h
->root
.type
== bfd_link_hash_warning
)
4504 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4506 /* Setting the index to -3 tells elf_link_output_extsym that
4507 this symbol is defined in a discarded section. */
4513 new_weak
= (flags
& BSF_WEAK
) != 0;
4514 new_weakdef
= FALSE
;
4518 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4519 && is_elf_hash_table (htab
)
4520 && h
->u
.weakdef
== NULL
)
4522 /* Keep a list of all weak defined non function symbols from
4523 a dynamic object, using the weakdef field. Later in this
4524 function we will set the weakdef field to the correct
4525 value. We only put non-function symbols from dynamic
4526 objects on this list, because that happens to be the only
4527 time we need to know the normal symbol corresponding to a
4528 weak symbol, and the information is time consuming to
4529 figure out. If the weakdef field is not already NULL,
4530 then this symbol was already defined by some previous
4531 dynamic object, and we will be using that previous
4532 definition anyhow. */
4534 h
->u
.weakdef
= weaks
;
4539 /* Set the alignment of a common symbol. */
4540 if ((common
|| bfd_is_com_section (sec
))
4541 && h
->root
.type
== bfd_link_hash_common
)
4546 align
= bfd_log2 (isym
->st_value
);
4549 /* The new symbol is a common symbol in a shared object.
4550 We need to get the alignment from the section. */
4551 align
= new_sec
->alignment_power
;
4553 if (align
> old_alignment
)
4554 h
->root
.u
.c
.p
->alignment_power
= align
;
4556 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4559 if (is_elf_hash_table (htab
))
4561 /* Set a flag in the hash table entry indicating the type of
4562 reference or definition we just found. A dynamic symbol
4563 is one which is referenced or defined by both a regular
4564 object and a shared object. */
4565 bfd_boolean dynsym
= FALSE
;
4567 /* Plugin symbols aren't normal. Don't set def_regular or
4568 ref_regular for them, or make them dynamic. */
4569 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4576 if (bind
!= STB_WEAK
)
4577 h
->ref_regular_nonweak
= 1;
4589 /* If the indirect symbol has been forced local, don't
4590 make the real symbol dynamic. */
4591 if ((h
== hi
|| !hi
->forced_local
)
4592 && (bfd_link_dll (info
)
4602 hi
->ref_dynamic
= 1;
4607 hi
->def_dynamic
= 1;
4610 /* If the indirect symbol has been forced local, don't
4611 make the real symbol dynamic. */
4612 if ((h
== hi
|| !hi
->forced_local
)
4615 || (h
->u
.weakdef
!= NULL
4617 && h
->u
.weakdef
->dynindx
!= -1)))
4621 /* Check to see if we need to add an indirect symbol for
4622 the default name. */
4624 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4625 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4626 sec
, value
, &old_bfd
, &dynsym
))
4627 goto error_free_vers
;
4629 /* Check the alignment when a common symbol is involved. This
4630 can change when a common symbol is overridden by a normal
4631 definition or a common symbol is ignored due to the old
4632 normal definition. We need to make sure the maximum
4633 alignment is maintained. */
4634 if ((old_alignment
|| common
)
4635 && h
->root
.type
!= bfd_link_hash_common
)
4637 unsigned int common_align
;
4638 unsigned int normal_align
;
4639 unsigned int symbol_align
;
4643 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4644 || h
->root
.type
== bfd_link_hash_defweak
);
4646 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4647 if (h
->root
.u
.def
.section
->owner
!= NULL
4648 && (h
->root
.u
.def
.section
->owner
->flags
4649 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4651 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4652 if (normal_align
> symbol_align
)
4653 normal_align
= symbol_align
;
4656 normal_align
= symbol_align
;
4660 common_align
= old_alignment
;
4661 common_bfd
= old_bfd
;
4666 common_align
= bfd_log2 (isym
->st_value
);
4668 normal_bfd
= old_bfd
;
4671 if (normal_align
< common_align
)
4673 /* PR binutils/2735 */
4674 if (normal_bfd
== NULL
)
4676 /* xgettext:c-format */
4677 (_("Warning: alignment %u of common symbol `%s' in %B is"
4678 " greater than the alignment (%u) of its section %A"),
4679 common_bfd
, h
->root
.u
.def
.section
,
4680 1 << common_align
, name
, 1 << normal_align
);
4683 /* xgettext:c-format */
4684 (_("Warning: alignment %u of symbol `%s' in %B"
4685 " is smaller than %u in %B"),
4686 normal_bfd
, common_bfd
,
4687 1 << normal_align
, name
, 1 << common_align
);
4691 /* Remember the symbol size if it isn't undefined. */
4692 if (isym
->st_size
!= 0
4693 && isym
->st_shndx
!= SHN_UNDEF
4694 && (definition
|| h
->size
== 0))
4697 && h
->size
!= isym
->st_size
4698 && ! size_change_ok
)
4700 /* xgettext:c-format */
4701 (_("Warning: size of symbol `%s' changed"
4702 " from %lu in %B to %lu in %B"),
4704 name
, (unsigned long) h
->size
,
4705 (unsigned long) isym
->st_size
);
4707 h
->size
= isym
->st_size
;
4710 /* If this is a common symbol, then we always want H->SIZE
4711 to be the size of the common symbol. The code just above
4712 won't fix the size if a common symbol becomes larger. We
4713 don't warn about a size change here, because that is
4714 covered by --warn-common. Allow changes between different
4716 if (h
->root
.type
== bfd_link_hash_common
)
4717 h
->size
= h
->root
.u
.c
.size
;
4719 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4720 && ((definition
&& !new_weak
)
4721 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4722 || h
->type
== STT_NOTYPE
))
4724 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4726 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4728 if (type
== STT_GNU_IFUNC
4729 && (abfd
->flags
& DYNAMIC
) != 0)
4732 if (h
->type
!= type
)
4734 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4735 /* xgettext:c-format */
4737 (_("Warning: type of symbol `%s' changed"
4738 " from %d to %d in %B"),
4739 abfd
, name
, h
->type
, type
);
4745 /* Merge st_other field. */
4746 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4748 /* We don't want to make debug symbol dynamic. */
4750 && (sec
->flags
& SEC_DEBUGGING
)
4751 && !bfd_link_relocatable (info
))
4754 /* Nor should we make plugin symbols dynamic. */
4755 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4760 h
->target_internal
= isym
->st_target_internal
;
4761 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4764 if (definition
&& !dynamic
)
4766 char *p
= strchr (name
, ELF_VER_CHR
);
4767 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4769 /* Queue non-default versions so that .symver x, x@FOO
4770 aliases can be checked. */
4773 amt
= ((isymend
- isym
+ 1)
4774 * sizeof (struct elf_link_hash_entry
*));
4776 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4778 goto error_free_vers
;
4780 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4784 if (dynsym
&& h
->dynindx
== -1)
4786 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4787 goto error_free_vers
;
4788 if (h
->u
.weakdef
!= NULL
4790 && h
->u
.weakdef
->dynindx
== -1)
4792 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4793 goto error_free_vers
;
4796 else if (h
->dynindx
!= -1)
4797 /* If the symbol already has a dynamic index, but
4798 visibility says it should not be visible, turn it into
4800 switch (ELF_ST_VISIBILITY (h
->other
))
4804 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4809 /* Don't add DT_NEEDED for references from the dummy bfd nor
4810 for unmatched symbol. */
4815 && h
->ref_regular_nonweak
4817 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4818 || (h
->ref_dynamic_nonweak
4819 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4820 && !on_needed_list (elf_dt_name (abfd
),
4821 htab
->needed
, NULL
))))
4824 const char *soname
= elf_dt_name (abfd
);
4826 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4827 h
->root
.root
.string
);
4829 /* A symbol from a library loaded via DT_NEEDED of some
4830 other library is referenced by a regular object.
4831 Add a DT_NEEDED entry for it. Issue an error if
4832 --no-add-needed is used and the reference was not
4835 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4838 /* xgettext:c-format */
4839 (_("%B: undefined reference to symbol '%s'"),
4841 bfd_set_error (bfd_error_missing_dso
);
4842 goto error_free_vers
;
4845 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4846 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4849 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4851 goto error_free_vers
;
4853 BFD_ASSERT (ret
== 0);
4858 if (extversym
!= NULL
)
4864 if (isymbuf
!= NULL
)
4870 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4874 /* Restore the symbol table. */
4875 old_ent
= (char *) old_tab
+ tabsize
;
4876 memset (elf_sym_hashes (abfd
), 0,
4877 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4878 htab
->root
.table
.table
= old_table
;
4879 htab
->root
.table
.size
= old_size
;
4880 htab
->root
.table
.count
= old_count
;
4881 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4882 htab
->root
.undefs
= old_undefs
;
4883 htab
->root
.undefs_tail
= old_undefs_tail
;
4884 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4887 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4889 struct bfd_hash_entry
*p
;
4890 struct elf_link_hash_entry
*h
;
4892 unsigned int alignment_power
;
4894 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4896 h
= (struct elf_link_hash_entry
*) p
;
4897 if (h
->root
.type
== bfd_link_hash_warning
)
4898 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4900 /* Preserve the maximum alignment and size for common
4901 symbols even if this dynamic lib isn't on DT_NEEDED
4902 since it can still be loaded at run time by another
4904 if (h
->root
.type
== bfd_link_hash_common
)
4906 size
= h
->root
.u
.c
.size
;
4907 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4912 alignment_power
= 0;
4914 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4915 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4916 h
= (struct elf_link_hash_entry
*) p
;
4917 if (h
->root
.type
== bfd_link_hash_warning
)
4919 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4920 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4921 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4923 if (h
->root
.type
== bfd_link_hash_common
)
4925 if (size
> h
->root
.u
.c
.size
)
4926 h
->root
.u
.c
.size
= size
;
4927 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4928 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4933 /* Make a special call to the linker "notice" function to
4934 tell it that symbols added for crefs may need to be removed. */
4935 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4936 goto error_free_vers
;
4939 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4941 if (nondeflt_vers
!= NULL
)
4942 free (nondeflt_vers
);
4946 if (old_tab
!= NULL
)
4948 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4949 goto error_free_vers
;
4954 /* Now that all the symbols from this input file are created, if
4955 not performing a relocatable link, handle .symver foo, foo@BAR
4956 such that any relocs against foo become foo@BAR. */
4957 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4961 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4963 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4964 char *shortname
, *p
;
4966 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4968 || (h
->root
.type
!= bfd_link_hash_defined
4969 && h
->root
.type
!= bfd_link_hash_defweak
))
4972 amt
= p
- h
->root
.root
.string
;
4973 shortname
= (char *) bfd_malloc (amt
+ 1);
4975 goto error_free_vers
;
4976 memcpy (shortname
, h
->root
.root
.string
, amt
);
4977 shortname
[amt
] = '\0';
4979 hi
= (struct elf_link_hash_entry
*)
4980 bfd_link_hash_lookup (&htab
->root
, shortname
,
4981 FALSE
, FALSE
, FALSE
);
4983 && hi
->root
.type
== h
->root
.type
4984 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4985 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4987 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4988 hi
->root
.type
= bfd_link_hash_indirect
;
4989 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4990 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4991 sym_hash
= elf_sym_hashes (abfd
);
4993 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4994 if (sym_hash
[symidx
] == hi
)
4996 sym_hash
[symidx
] = h
;
5002 free (nondeflt_vers
);
5003 nondeflt_vers
= NULL
;
5006 /* Now set the weakdefs field correctly for all the weak defined
5007 symbols we found. The only way to do this is to search all the
5008 symbols. Since we only need the information for non functions in
5009 dynamic objects, that's the only time we actually put anything on
5010 the list WEAKS. We need this information so that if a regular
5011 object refers to a symbol defined weakly in a dynamic object, the
5012 real symbol in the dynamic object is also put in the dynamic
5013 symbols; we also must arrange for both symbols to point to the
5014 same memory location. We could handle the general case of symbol
5015 aliasing, but a general symbol alias can only be generated in
5016 assembler code, handling it correctly would be very time
5017 consuming, and other ELF linkers don't handle general aliasing
5021 struct elf_link_hash_entry
**hpp
;
5022 struct elf_link_hash_entry
**hppend
;
5023 struct elf_link_hash_entry
**sorted_sym_hash
;
5024 struct elf_link_hash_entry
*h
;
5027 /* Since we have to search the whole symbol list for each weak
5028 defined symbol, search time for N weak defined symbols will be
5029 O(N^2). Binary search will cut it down to O(NlogN). */
5031 amt
*= sizeof (struct elf_link_hash_entry
*);
5032 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5033 if (sorted_sym_hash
== NULL
)
5035 sym_hash
= sorted_sym_hash
;
5036 hpp
= elf_sym_hashes (abfd
);
5037 hppend
= hpp
+ extsymcount
;
5039 for (; hpp
< hppend
; hpp
++)
5043 && h
->root
.type
== bfd_link_hash_defined
5044 && !bed
->is_function_type (h
->type
))
5052 qsort (sorted_sym_hash
, sym_count
,
5053 sizeof (struct elf_link_hash_entry
*),
5056 while (weaks
!= NULL
)
5058 struct elf_link_hash_entry
*hlook
;
5061 size_t i
, j
, idx
= 0;
5064 weaks
= hlook
->u
.weakdef
;
5065 hlook
->u
.weakdef
= NULL
;
5067 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5068 || hlook
->root
.type
== bfd_link_hash_defweak
5069 || hlook
->root
.type
== bfd_link_hash_common
5070 || hlook
->root
.type
== bfd_link_hash_indirect
);
5071 slook
= hlook
->root
.u
.def
.section
;
5072 vlook
= hlook
->root
.u
.def
.value
;
5078 bfd_signed_vma vdiff
;
5080 h
= sorted_sym_hash
[idx
];
5081 vdiff
= vlook
- h
->root
.u
.def
.value
;
5088 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5098 /* We didn't find a value/section match. */
5102 /* With multiple aliases, or when the weak symbol is already
5103 strongly defined, we have multiple matching symbols and
5104 the binary search above may land on any of them. Step
5105 one past the matching symbol(s). */
5108 h
= sorted_sym_hash
[idx
];
5109 if (h
->root
.u
.def
.section
!= slook
5110 || h
->root
.u
.def
.value
!= vlook
)
5114 /* Now look back over the aliases. Since we sorted by size
5115 as well as value and section, we'll choose the one with
5116 the largest size. */
5119 h
= sorted_sym_hash
[idx
];
5121 /* Stop if value or section doesn't match. */
5122 if (h
->root
.u
.def
.section
!= slook
5123 || h
->root
.u
.def
.value
!= vlook
)
5125 else if (h
!= hlook
)
5127 hlook
->u
.weakdef
= h
;
5129 /* If the weak definition is in the list of dynamic
5130 symbols, make sure the real definition is put
5132 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5134 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5137 free (sorted_sym_hash
);
5142 /* If the real definition is in the list of dynamic
5143 symbols, make sure the weak definition is put
5144 there as well. If we don't do this, then the
5145 dynamic loader might not merge the entries for the
5146 real definition and the weak definition. */
5147 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5149 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5150 goto err_free_sym_hash
;
5157 free (sorted_sym_hash
);
5160 if (bed
->check_directives
5161 && !(*bed
->check_directives
) (abfd
, info
))
5164 if (!info
->check_relocs_after_open_input
5165 && !_bfd_elf_link_check_relocs (abfd
, info
))
5168 /* If this is a non-traditional link, try to optimize the handling
5169 of the .stab/.stabstr sections. */
5171 && ! info
->traditional_format
5172 && is_elf_hash_table (htab
)
5173 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5177 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5178 if (stabstr
!= NULL
)
5180 bfd_size_type string_offset
= 0;
5183 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5184 if (CONST_STRNEQ (stab
->name
, ".stab")
5185 && (!stab
->name
[5] ||
5186 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5187 && (stab
->flags
& SEC_MERGE
) == 0
5188 && !bfd_is_abs_section (stab
->output_section
))
5190 struct bfd_elf_section_data
*secdata
;
5192 secdata
= elf_section_data (stab
);
5193 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5194 stabstr
, &secdata
->sec_info
,
5197 if (secdata
->sec_info
)
5198 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5203 if (is_elf_hash_table (htab
) && add_needed
)
5205 /* Add this bfd to the loaded list. */
5206 struct elf_link_loaded_list
*n
;
5208 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5212 n
->next
= htab
->loaded
;
5219 if (old_tab
!= NULL
)
5221 if (old_strtab
!= NULL
)
5223 if (nondeflt_vers
!= NULL
)
5224 free (nondeflt_vers
);
5225 if (extversym
!= NULL
)
5228 if (isymbuf
!= NULL
)
5234 /* Return the linker hash table entry of a symbol that might be
5235 satisfied by an archive symbol. Return -1 on error. */
5237 struct elf_link_hash_entry
*
5238 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5239 struct bfd_link_info
*info
,
5242 struct elf_link_hash_entry
*h
;
5246 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5250 /* If this is a default version (the name contains @@), look up the
5251 symbol again with only one `@' as well as without the version.
5252 The effect is that references to the symbol with and without the
5253 version will be matched by the default symbol in the archive. */
5255 p
= strchr (name
, ELF_VER_CHR
);
5256 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5259 /* First check with only one `@'. */
5260 len
= strlen (name
);
5261 copy
= (char *) bfd_alloc (abfd
, len
);
5263 return (struct elf_link_hash_entry
*) 0 - 1;
5265 first
= p
- name
+ 1;
5266 memcpy (copy
, name
, first
);
5267 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5269 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5272 /* We also need to check references to the symbol without the
5274 copy
[first
- 1] = '\0';
5275 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5276 FALSE
, FALSE
, TRUE
);
5279 bfd_release (abfd
, copy
);
5283 /* Add symbols from an ELF archive file to the linker hash table. We
5284 don't use _bfd_generic_link_add_archive_symbols because we need to
5285 handle versioned symbols.
5287 Fortunately, ELF archive handling is simpler than that done by
5288 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5289 oddities. In ELF, if we find a symbol in the archive map, and the
5290 symbol is currently undefined, we know that we must pull in that
5293 Unfortunately, we do have to make multiple passes over the symbol
5294 table until nothing further is resolved. */
5297 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5300 unsigned char *included
= NULL
;
5304 const struct elf_backend_data
*bed
;
5305 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5306 (bfd
*, struct bfd_link_info
*, const char *);
5308 if (! bfd_has_map (abfd
))
5310 /* An empty archive is a special case. */
5311 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5313 bfd_set_error (bfd_error_no_armap
);
5317 /* Keep track of all symbols we know to be already defined, and all
5318 files we know to be already included. This is to speed up the
5319 second and subsequent passes. */
5320 c
= bfd_ardata (abfd
)->symdef_count
;
5324 amt
*= sizeof (*included
);
5325 included
= (unsigned char *) bfd_zmalloc (amt
);
5326 if (included
== NULL
)
5329 symdefs
= bfd_ardata (abfd
)->symdefs
;
5330 bed
= get_elf_backend_data (abfd
);
5331 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5344 symdefend
= symdef
+ c
;
5345 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5347 struct elf_link_hash_entry
*h
;
5349 struct bfd_link_hash_entry
*undefs_tail
;
5354 if (symdef
->file_offset
== last
)
5360 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5361 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5367 if (h
->root
.type
== bfd_link_hash_common
)
5369 /* We currently have a common symbol. The archive map contains
5370 a reference to this symbol, so we may want to include it. We
5371 only want to include it however, if this archive element
5372 contains a definition of the symbol, not just another common
5375 Unfortunately some archivers (including GNU ar) will put
5376 declarations of common symbols into their archive maps, as
5377 well as real definitions, so we cannot just go by the archive
5378 map alone. Instead we must read in the element's symbol
5379 table and check that to see what kind of symbol definition
5381 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5384 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5386 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5387 /* Symbol must be defined. Don't check it again. */
5392 /* We need to include this archive member. */
5393 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5394 if (element
== NULL
)
5397 if (! bfd_check_format (element
, bfd_object
))
5400 undefs_tail
= info
->hash
->undefs_tail
;
5402 if (!(*info
->callbacks
5403 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5405 if (!bfd_link_add_symbols (element
, info
))
5408 /* If there are any new undefined symbols, we need to make
5409 another pass through the archive in order to see whether
5410 they can be defined. FIXME: This isn't perfect, because
5411 common symbols wind up on undefs_tail and because an
5412 undefined symbol which is defined later on in this pass
5413 does not require another pass. This isn't a bug, but it
5414 does make the code less efficient than it could be. */
5415 if (undefs_tail
!= info
->hash
->undefs_tail
)
5418 /* Look backward to mark all symbols from this object file
5419 which we have already seen in this pass. */
5423 included
[mark
] = TRUE
;
5428 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5430 /* We mark subsequent symbols from this object file as we go
5431 on through the loop. */
5432 last
= symdef
->file_offset
;
5442 if (included
!= NULL
)
5447 /* Given an ELF BFD, add symbols to the global hash table as
5451 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5453 switch (bfd_get_format (abfd
))
5456 return elf_link_add_object_symbols (abfd
, info
);
5458 return elf_link_add_archive_symbols (abfd
, info
);
5460 bfd_set_error (bfd_error_wrong_format
);
5465 struct hash_codes_info
5467 unsigned long *hashcodes
;
5471 /* This function will be called though elf_link_hash_traverse to store
5472 all hash value of the exported symbols in an array. */
5475 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5477 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5482 /* Ignore indirect symbols. These are added by the versioning code. */
5483 if (h
->dynindx
== -1)
5486 name
= h
->root
.root
.string
;
5487 if (h
->versioned
>= versioned
)
5489 char *p
= strchr (name
, ELF_VER_CHR
);
5492 alc
= (char *) bfd_malloc (p
- name
+ 1);
5498 memcpy (alc
, name
, p
- name
);
5499 alc
[p
- name
] = '\0';
5504 /* Compute the hash value. */
5505 ha
= bfd_elf_hash (name
);
5507 /* Store the found hash value in the array given as the argument. */
5508 *(inf
->hashcodes
)++ = ha
;
5510 /* And store it in the struct so that we can put it in the hash table
5512 h
->u
.elf_hash_value
= ha
;
5520 struct collect_gnu_hash_codes
5523 const struct elf_backend_data
*bed
;
5524 unsigned long int nsyms
;
5525 unsigned long int maskbits
;
5526 unsigned long int *hashcodes
;
5527 unsigned long int *hashval
;
5528 unsigned long int *indx
;
5529 unsigned long int *counts
;
5532 long int min_dynindx
;
5533 unsigned long int bucketcount
;
5534 unsigned long int symindx
;
5535 long int local_indx
;
5536 long int shift1
, shift2
;
5537 unsigned long int mask
;
5541 /* This function will be called though elf_link_hash_traverse to store
5542 all hash value of the exported symbols in an array. */
5545 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5547 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5552 /* Ignore indirect symbols. These are added by the versioning code. */
5553 if (h
->dynindx
== -1)
5556 /* Ignore also local symbols and undefined symbols. */
5557 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5560 name
= h
->root
.root
.string
;
5561 if (h
->versioned
>= versioned
)
5563 char *p
= strchr (name
, ELF_VER_CHR
);
5566 alc
= (char *) bfd_malloc (p
- name
+ 1);
5572 memcpy (alc
, name
, p
- name
);
5573 alc
[p
- name
] = '\0';
5578 /* Compute the hash value. */
5579 ha
= bfd_elf_gnu_hash (name
);
5581 /* Store the found hash value in the array for compute_bucket_count,
5582 and also for .dynsym reordering purposes. */
5583 s
->hashcodes
[s
->nsyms
] = ha
;
5584 s
->hashval
[h
->dynindx
] = ha
;
5586 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5587 s
->min_dynindx
= h
->dynindx
;
5595 /* This function will be called though elf_link_hash_traverse to do
5596 final dynaminc symbol renumbering. */
5599 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5601 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5602 unsigned long int bucket
;
5603 unsigned long int val
;
5605 /* Ignore indirect symbols. */
5606 if (h
->dynindx
== -1)
5609 /* Ignore also local symbols and undefined symbols. */
5610 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5612 if (h
->dynindx
>= s
->min_dynindx
)
5613 h
->dynindx
= s
->local_indx
++;
5617 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5618 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5619 & ((s
->maskbits
>> s
->shift1
) - 1);
5620 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5622 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5623 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5624 if (s
->counts
[bucket
] == 1)
5625 /* Last element terminates the chain. */
5627 bfd_put_32 (s
->output_bfd
, val
,
5628 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5629 --s
->counts
[bucket
];
5630 h
->dynindx
= s
->indx
[bucket
]++;
5634 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5637 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5639 return !(h
->forced_local
5640 || h
->root
.type
== bfd_link_hash_undefined
5641 || h
->root
.type
== bfd_link_hash_undefweak
5642 || ((h
->root
.type
== bfd_link_hash_defined
5643 || h
->root
.type
== bfd_link_hash_defweak
)
5644 && h
->root
.u
.def
.section
->output_section
== NULL
));
5647 /* Array used to determine the number of hash table buckets to use
5648 based on the number of symbols there are. If there are fewer than
5649 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5650 fewer than 37 we use 17 buckets, and so forth. We never use more
5651 than 32771 buckets. */
5653 static const size_t elf_buckets
[] =
5655 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5659 /* Compute bucket count for hashing table. We do not use a static set
5660 of possible tables sizes anymore. Instead we determine for all
5661 possible reasonable sizes of the table the outcome (i.e., the
5662 number of collisions etc) and choose the best solution. The
5663 weighting functions are not too simple to allow the table to grow
5664 without bounds. Instead one of the weighting factors is the size.
5665 Therefore the result is always a good payoff between few collisions
5666 (= short chain lengths) and table size. */
5668 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5669 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5670 unsigned long int nsyms
,
5673 size_t best_size
= 0;
5674 unsigned long int i
;
5676 /* We have a problem here. The following code to optimize the table
5677 size requires an integer type with more the 32 bits. If
5678 BFD_HOST_U_64_BIT is set we know about such a type. */
5679 #ifdef BFD_HOST_U_64_BIT
5684 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5685 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5686 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5687 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5688 unsigned long int *counts
;
5690 unsigned int no_improvement_count
= 0;
5692 /* Possible optimization parameters: if we have NSYMS symbols we say
5693 that the hashing table must at least have NSYMS/4 and at most
5695 minsize
= nsyms
/ 4;
5698 best_size
= maxsize
= nsyms
* 2;
5703 if ((best_size
& 31) == 0)
5707 /* Create array where we count the collisions in. We must use bfd_malloc
5708 since the size could be large. */
5710 amt
*= sizeof (unsigned long int);
5711 counts
= (unsigned long int *) bfd_malloc (amt
);
5715 /* Compute the "optimal" size for the hash table. The criteria is a
5716 minimal chain length. The minor criteria is (of course) the size
5718 for (i
= minsize
; i
< maxsize
; ++i
)
5720 /* Walk through the array of hashcodes and count the collisions. */
5721 BFD_HOST_U_64_BIT max
;
5722 unsigned long int j
;
5723 unsigned long int fact
;
5725 if (gnu_hash
&& (i
& 31) == 0)
5728 memset (counts
, '\0', i
* sizeof (unsigned long int));
5730 /* Determine how often each hash bucket is used. */
5731 for (j
= 0; j
< nsyms
; ++j
)
5732 ++counts
[hashcodes
[j
] % i
];
5734 /* For the weight function we need some information about the
5735 pagesize on the target. This is information need not be 100%
5736 accurate. Since this information is not available (so far) we
5737 define it here to a reasonable default value. If it is crucial
5738 to have a better value some day simply define this value. */
5739 # ifndef BFD_TARGET_PAGESIZE
5740 # define BFD_TARGET_PAGESIZE (4096)
5743 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5745 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5748 /* Variant 1: optimize for short chains. We add the squares
5749 of all the chain lengths (which favors many small chain
5750 over a few long chains). */
5751 for (j
= 0; j
< i
; ++j
)
5752 max
+= counts
[j
] * counts
[j
];
5754 /* This adds penalties for the overall size of the table. */
5755 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5758 /* Variant 2: Optimize a lot more for small table. Here we
5759 also add squares of the size but we also add penalties for
5760 empty slots (the +1 term). */
5761 for (j
= 0; j
< i
; ++j
)
5762 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5764 /* The overall size of the table is considered, but not as
5765 strong as in variant 1, where it is squared. */
5766 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5770 /* Compare with current best results. */
5771 if (max
< best_chlen
)
5775 no_improvement_count
= 0;
5777 /* PR 11843: Avoid futile long searches for the best bucket size
5778 when there are a large number of symbols. */
5779 else if (++no_improvement_count
== 100)
5786 #endif /* defined (BFD_HOST_U_64_BIT) */
5788 /* This is the fallback solution if no 64bit type is available or if we
5789 are not supposed to spend much time on optimizations. We select the
5790 bucket count using a fixed set of numbers. */
5791 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5793 best_size
= elf_buckets
[i
];
5794 if (nsyms
< elf_buckets
[i
+ 1])
5797 if (gnu_hash
&& best_size
< 2)
5804 /* Size any SHT_GROUP section for ld -r. */
5807 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5811 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5812 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5813 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5818 /* Set a default stack segment size. The value in INFO wins. If it
5819 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5820 undefined it is initialized. */
5823 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5824 struct bfd_link_info
*info
,
5825 const char *legacy_symbol
,
5826 bfd_vma default_size
)
5828 struct elf_link_hash_entry
*h
= NULL
;
5830 /* Look for legacy symbol. */
5832 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5833 FALSE
, FALSE
, FALSE
);
5834 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5835 || h
->root
.type
== bfd_link_hash_defweak
)
5837 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5839 /* The symbol has no type if specified on the command line. */
5840 h
->type
= STT_OBJECT
;
5841 if (info
->stacksize
)
5842 /* xgettext:c-format */
5843 _bfd_error_handler (_("%B: stack size specified and %s set"),
5844 output_bfd
, legacy_symbol
);
5845 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5846 /* xgettext:c-format */
5847 _bfd_error_handler (_("%B: %s not absolute"),
5848 output_bfd
, legacy_symbol
);
5850 info
->stacksize
= h
->root
.u
.def
.value
;
5853 if (!info
->stacksize
)
5854 /* If the user didn't set a size, or explicitly inhibit the
5855 size, set it now. */
5856 info
->stacksize
= default_size
;
5858 /* Provide the legacy symbol, if it is referenced. */
5859 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5860 || h
->root
.type
== bfd_link_hash_undefweak
))
5862 struct bfd_link_hash_entry
*bh
= NULL
;
5864 if (!(_bfd_generic_link_add_one_symbol
5865 (info
, output_bfd
, legacy_symbol
,
5866 BSF_GLOBAL
, bfd_abs_section_ptr
,
5867 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5868 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5871 h
= (struct elf_link_hash_entry
*) bh
;
5873 h
->type
= STT_OBJECT
;
5879 /* Set up the sizes and contents of the ELF dynamic sections. This is
5880 called by the ELF linker emulation before_allocation routine. We
5881 must set the sizes of the sections before the linker sets the
5882 addresses of the various sections. */
5885 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5888 const char *filter_shlib
,
5890 const char *depaudit
,
5891 const char * const *auxiliary_filters
,
5892 struct bfd_link_info
*info
,
5893 asection
**sinterpptr
)
5897 const struct elf_backend_data
*bed
;
5898 struct elf_info_failed asvinfo
;
5902 soname_indx
= (size_t) -1;
5904 if (!is_elf_hash_table (info
->hash
))
5907 bed
= get_elf_backend_data (output_bfd
);
5909 /* Any syms created from now on start with -1 in
5910 got.refcount/offset and plt.refcount/offset. */
5911 elf_hash_table (info
)->init_got_refcount
5912 = elf_hash_table (info
)->init_got_offset
;
5913 elf_hash_table (info
)->init_plt_refcount
5914 = elf_hash_table (info
)->init_plt_offset
;
5916 if (bfd_link_relocatable (info
)
5917 && !_bfd_elf_size_group_sections (info
))
5920 /* The backend may have to create some sections regardless of whether
5921 we're dynamic or not. */
5922 if (bed
->elf_backend_always_size_sections
5923 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5926 /* Determine any GNU_STACK segment requirements, after the backend
5927 has had a chance to set a default segment size. */
5928 if (info
->execstack
)
5929 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5930 else if (info
->noexecstack
)
5931 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5935 asection
*notesec
= NULL
;
5938 for (inputobj
= info
->input_bfds
;
5940 inputobj
= inputobj
->link
.next
)
5945 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5947 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5950 if (s
->flags
& SEC_CODE
)
5954 else if (bed
->default_execstack
)
5957 if (notesec
|| info
->stacksize
> 0)
5958 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5959 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5960 && notesec
->output_section
!= bfd_abs_section_ptr
)
5961 notesec
->output_section
->flags
|= SEC_CODE
;
5964 dynobj
= elf_hash_table (info
)->dynobj
;
5966 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5968 struct elf_info_failed eif
;
5969 struct elf_link_hash_entry
*h
;
5971 struct bfd_elf_version_tree
*t
;
5972 struct bfd_elf_version_expr
*d
;
5974 bfd_boolean all_defined
;
5976 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5977 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5981 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5983 if (soname_indx
== (size_t) -1
5984 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5990 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5992 info
->flags
|= DF_SYMBOLIC
;
6000 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6002 if (indx
== (size_t) -1)
6005 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6006 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6010 if (filter_shlib
!= NULL
)
6014 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6015 filter_shlib
, TRUE
);
6016 if (indx
== (size_t) -1
6017 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6021 if (auxiliary_filters
!= NULL
)
6023 const char * const *p
;
6025 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6029 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6031 if (indx
== (size_t) -1
6032 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6041 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6043 if (indx
== (size_t) -1
6044 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6048 if (depaudit
!= NULL
)
6052 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6054 if (indx
== (size_t) -1
6055 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6062 /* If we are supposed to export all symbols into the dynamic symbol
6063 table (this is not the normal case), then do so. */
6064 if (info
->export_dynamic
6065 || (bfd_link_executable (info
) && info
->dynamic
))
6067 elf_link_hash_traverse (elf_hash_table (info
),
6068 _bfd_elf_export_symbol
,
6074 /* Make all global versions with definition. */
6075 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6076 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6077 if (!d
->symver
&& d
->literal
)
6079 const char *verstr
, *name
;
6080 size_t namelen
, verlen
, newlen
;
6081 char *newname
, *p
, leading_char
;
6082 struct elf_link_hash_entry
*newh
;
6084 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6086 namelen
= strlen (name
) + (leading_char
!= '\0');
6088 verlen
= strlen (verstr
);
6089 newlen
= namelen
+ verlen
+ 3;
6091 newname
= (char *) bfd_malloc (newlen
);
6092 if (newname
== NULL
)
6094 newname
[0] = leading_char
;
6095 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6097 /* Check the hidden versioned definition. */
6098 p
= newname
+ namelen
;
6100 memcpy (p
, verstr
, verlen
+ 1);
6101 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6102 newname
, FALSE
, FALSE
,
6105 || (newh
->root
.type
!= bfd_link_hash_defined
6106 && newh
->root
.type
!= bfd_link_hash_defweak
))
6108 /* Check the default versioned definition. */
6110 memcpy (p
, verstr
, verlen
+ 1);
6111 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6112 newname
, FALSE
, FALSE
,
6117 /* Mark this version if there is a definition and it is
6118 not defined in a shared object. */
6120 && !newh
->def_dynamic
6121 && (newh
->root
.type
== bfd_link_hash_defined
6122 || newh
->root
.type
== bfd_link_hash_defweak
))
6126 /* Attach all the symbols to their version information. */
6127 asvinfo
.info
= info
;
6128 asvinfo
.failed
= FALSE
;
6130 elf_link_hash_traverse (elf_hash_table (info
),
6131 _bfd_elf_link_assign_sym_version
,
6136 if (!info
->allow_undefined_version
)
6138 /* Check if all global versions have a definition. */
6140 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6141 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6142 if (d
->literal
&& !d
->symver
&& !d
->script
)
6145 (_("%s: undefined version: %s"),
6146 d
->pattern
, t
->name
);
6147 all_defined
= FALSE
;
6152 bfd_set_error (bfd_error_bad_value
);
6157 /* Find all symbols which were defined in a dynamic object and make
6158 the backend pick a reasonable value for them. */
6159 elf_link_hash_traverse (elf_hash_table (info
),
6160 _bfd_elf_adjust_dynamic_symbol
,
6165 /* Add some entries to the .dynamic section. We fill in some of the
6166 values later, in bfd_elf_final_link, but we must add the entries
6167 now so that we know the final size of the .dynamic section. */
6169 /* If there are initialization and/or finalization functions to
6170 call then add the corresponding DT_INIT/DT_FINI entries. */
6171 h
= (info
->init_function
6172 ? elf_link_hash_lookup (elf_hash_table (info
),
6173 info
->init_function
, FALSE
,
6180 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6183 h
= (info
->fini_function
6184 ? elf_link_hash_lookup (elf_hash_table (info
),
6185 info
->fini_function
, FALSE
,
6192 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6196 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6197 if (s
!= NULL
&& s
->linker_has_input
)
6199 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6200 if (! bfd_link_executable (info
))
6205 for (sub
= info
->input_bfds
; sub
!= NULL
;
6206 sub
= sub
->link
.next
)
6207 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6208 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6209 if (elf_section_data (o
)->this_hdr
.sh_type
6210 == SHT_PREINIT_ARRAY
)
6213 (_("%B: .preinit_array section is not allowed in DSO"),
6218 bfd_set_error (bfd_error_nonrepresentable_section
);
6222 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6223 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6226 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6227 if (s
!= NULL
&& s
->linker_has_input
)
6229 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6230 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6233 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6234 if (s
!= NULL
&& s
->linker_has_input
)
6236 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6237 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6241 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6242 /* If .dynstr is excluded from the link, we don't want any of
6243 these tags. Strictly, we should be checking each section
6244 individually; This quick check covers for the case where
6245 someone does a /DISCARD/ : { *(*) }. */
6246 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6248 bfd_size_type strsize
;
6250 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6251 if ((info
->emit_hash
6252 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6253 || (info
->emit_gnu_hash
6254 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6255 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6256 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6257 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6258 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6259 bed
->s
->sizeof_sym
))
6264 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6267 /* The backend must work out the sizes of all the other dynamic
6270 && bed
->elf_backend_size_dynamic_sections
!= NULL
6271 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6274 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6276 unsigned long section_sym_count
;
6277 struct bfd_elf_version_tree
*verdefs
;
6280 /* Set up the version definition section. */
6281 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6282 BFD_ASSERT (s
!= NULL
);
6284 /* We may have created additional version definitions if we are
6285 just linking a regular application. */
6286 verdefs
= info
->version_info
;
6288 /* Skip anonymous version tag. */
6289 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6290 verdefs
= verdefs
->next
;
6292 if (verdefs
== NULL
&& !info
->create_default_symver
)
6293 s
->flags
|= SEC_EXCLUDE
;
6298 struct bfd_elf_version_tree
*t
;
6300 Elf_Internal_Verdef def
;
6301 Elf_Internal_Verdaux defaux
;
6302 struct bfd_link_hash_entry
*bh
;
6303 struct elf_link_hash_entry
*h
;
6309 /* Make space for the base version. */
6310 size
+= sizeof (Elf_External_Verdef
);
6311 size
+= sizeof (Elf_External_Verdaux
);
6314 /* Make space for the default version. */
6315 if (info
->create_default_symver
)
6317 size
+= sizeof (Elf_External_Verdef
);
6321 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6323 struct bfd_elf_version_deps
*n
;
6325 /* Don't emit base version twice. */
6329 size
+= sizeof (Elf_External_Verdef
);
6330 size
+= sizeof (Elf_External_Verdaux
);
6333 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6334 size
+= sizeof (Elf_External_Verdaux
);
6338 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6339 if (s
->contents
== NULL
&& s
->size
!= 0)
6342 /* Fill in the version definition section. */
6346 def
.vd_version
= VER_DEF_CURRENT
;
6347 def
.vd_flags
= VER_FLG_BASE
;
6350 if (info
->create_default_symver
)
6352 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6353 def
.vd_next
= sizeof (Elf_External_Verdef
);
6357 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6358 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6359 + sizeof (Elf_External_Verdaux
));
6362 if (soname_indx
!= (size_t) -1)
6364 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6366 def
.vd_hash
= bfd_elf_hash (soname
);
6367 defaux
.vda_name
= soname_indx
;
6374 name
= lbasename (output_bfd
->filename
);
6375 def
.vd_hash
= bfd_elf_hash (name
);
6376 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6378 if (indx
== (size_t) -1)
6380 defaux
.vda_name
= indx
;
6382 defaux
.vda_next
= 0;
6384 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6385 (Elf_External_Verdef
*) p
);
6386 p
+= sizeof (Elf_External_Verdef
);
6387 if (info
->create_default_symver
)
6389 /* Add a symbol representing this version. */
6391 if (! (_bfd_generic_link_add_one_symbol
6392 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6394 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6396 h
= (struct elf_link_hash_entry
*) bh
;
6399 h
->type
= STT_OBJECT
;
6400 h
->verinfo
.vertree
= NULL
;
6402 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6405 /* Create a duplicate of the base version with the same
6406 aux block, but different flags. */
6409 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6411 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6412 + sizeof (Elf_External_Verdaux
));
6415 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6416 (Elf_External_Verdef
*) p
);
6417 p
+= sizeof (Elf_External_Verdef
);
6419 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6420 (Elf_External_Verdaux
*) p
);
6421 p
+= sizeof (Elf_External_Verdaux
);
6423 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6426 struct bfd_elf_version_deps
*n
;
6428 /* Don't emit the base version twice. */
6433 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6436 /* Add a symbol representing this version. */
6438 if (! (_bfd_generic_link_add_one_symbol
6439 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6441 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6443 h
= (struct elf_link_hash_entry
*) bh
;
6446 h
->type
= STT_OBJECT
;
6447 h
->verinfo
.vertree
= t
;
6449 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6452 def
.vd_version
= VER_DEF_CURRENT
;
6454 if (t
->globals
.list
== NULL
6455 && t
->locals
.list
== NULL
6457 def
.vd_flags
|= VER_FLG_WEAK
;
6458 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6459 def
.vd_cnt
= cdeps
+ 1;
6460 def
.vd_hash
= bfd_elf_hash (t
->name
);
6461 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6464 /* If a basever node is next, it *must* be the last node in
6465 the chain, otherwise Verdef construction breaks. */
6466 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6467 BFD_ASSERT (t
->next
->next
== NULL
);
6469 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6470 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6471 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6473 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6474 (Elf_External_Verdef
*) p
);
6475 p
+= sizeof (Elf_External_Verdef
);
6477 defaux
.vda_name
= h
->dynstr_index
;
6478 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6480 defaux
.vda_next
= 0;
6481 if (t
->deps
!= NULL
)
6482 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6483 t
->name_indx
= defaux
.vda_name
;
6485 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6486 (Elf_External_Verdaux
*) p
);
6487 p
+= sizeof (Elf_External_Verdaux
);
6489 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6491 if (n
->version_needed
== NULL
)
6493 /* This can happen if there was an error in the
6495 defaux
.vda_name
= 0;
6499 defaux
.vda_name
= n
->version_needed
->name_indx
;
6500 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6503 if (n
->next
== NULL
)
6504 defaux
.vda_next
= 0;
6506 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6508 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6509 (Elf_External_Verdaux
*) p
);
6510 p
+= sizeof (Elf_External_Verdaux
);
6514 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6515 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6518 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6521 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6523 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6526 else if (info
->flags
& DF_BIND_NOW
)
6528 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6534 if (bfd_link_executable (info
))
6535 info
->flags_1
&= ~ (DF_1_INITFIRST
6538 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6542 /* Work out the size of the version reference section. */
6544 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6545 BFD_ASSERT (s
!= NULL
);
6547 struct elf_find_verdep_info sinfo
;
6550 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6551 if (sinfo
.vers
== 0)
6553 sinfo
.failed
= FALSE
;
6555 elf_link_hash_traverse (elf_hash_table (info
),
6556 _bfd_elf_link_find_version_dependencies
,
6561 if (elf_tdata (output_bfd
)->verref
== NULL
)
6562 s
->flags
|= SEC_EXCLUDE
;
6565 Elf_Internal_Verneed
*t
;
6570 /* Build the version dependency section. */
6573 for (t
= elf_tdata (output_bfd
)->verref
;
6577 Elf_Internal_Vernaux
*a
;
6579 size
+= sizeof (Elf_External_Verneed
);
6581 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6582 size
+= sizeof (Elf_External_Vernaux
);
6586 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6587 if (s
->contents
== NULL
)
6591 for (t
= elf_tdata (output_bfd
)->verref
;
6596 Elf_Internal_Vernaux
*a
;
6600 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6603 t
->vn_version
= VER_NEED_CURRENT
;
6605 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6606 elf_dt_name (t
->vn_bfd
) != NULL
6607 ? elf_dt_name (t
->vn_bfd
)
6608 : lbasename (t
->vn_bfd
->filename
),
6610 if (indx
== (size_t) -1)
6613 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6614 if (t
->vn_nextref
== NULL
)
6617 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6618 + caux
* sizeof (Elf_External_Vernaux
));
6620 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6621 (Elf_External_Verneed
*) p
);
6622 p
+= sizeof (Elf_External_Verneed
);
6624 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6626 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6627 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6628 a
->vna_nodename
, FALSE
);
6629 if (indx
== (size_t) -1)
6632 if (a
->vna_nextptr
== NULL
)
6635 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6637 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6638 (Elf_External_Vernaux
*) p
);
6639 p
+= sizeof (Elf_External_Vernaux
);
6643 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6644 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6647 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6651 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6652 && elf_tdata (output_bfd
)->cverdefs
== 0)
6653 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6654 §ion_sym_count
) == 0)
6656 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6657 s
->flags
|= SEC_EXCLUDE
;
6663 /* Find the first non-excluded output section. We'll use its
6664 section symbol for some emitted relocs. */
6666 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6670 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6671 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6672 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6674 elf_hash_table (info
)->text_index_section
= s
;
6679 /* Find two non-excluded output sections, one for code, one for data.
6680 We'll use their section symbols for some emitted relocs. */
6682 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6686 /* Data first, since setting text_index_section changes
6687 _bfd_elf_link_omit_section_dynsym. */
6688 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6689 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6690 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6692 elf_hash_table (info
)->data_index_section
= s
;
6696 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6697 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6698 == (SEC_ALLOC
| SEC_READONLY
))
6699 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6701 elf_hash_table (info
)->text_index_section
= s
;
6705 if (elf_hash_table (info
)->text_index_section
== NULL
)
6706 elf_hash_table (info
)->text_index_section
6707 = elf_hash_table (info
)->data_index_section
;
6711 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6713 const struct elf_backend_data
*bed
;
6715 if (!is_elf_hash_table (info
->hash
))
6718 bed
= get_elf_backend_data (output_bfd
);
6719 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6721 if (elf_hash_table (info
)->dynamic_sections_created
)
6725 bfd_size_type dynsymcount
;
6726 unsigned long section_sym_count
;
6727 unsigned int dtagcount
;
6729 dynobj
= elf_hash_table (info
)->dynobj
;
6731 /* Assign dynsym indicies. In a shared library we generate a
6732 section symbol for each output section, which come first.
6733 Next come all of the back-end allocated local dynamic syms,
6734 followed by the rest of the global symbols. */
6736 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6737 §ion_sym_count
);
6739 /* Work out the size of the symbol version section. */
6740 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6741 BFD_ASSERT (s
!= NULL
);
6742 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6744 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6745 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6746 if (s
->contents
== NULL
)
6749 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6753 /* Set the size of the .dynsym and .hash sections. We counted
6754 the number of dynamic symbols in elf_link_add_object_symbols.
6755 We will build the contents of .dynsym and .hash when we build
6756 the final symbol table, because until then we do not know the
6757 correct value to give the symbols. We built the .dynstr
6758 section as we went along in elf_link_add_object_symbols. */
6759 s
= elf_hash_table (info
)->dynsym
;
6760 BFD_ASSERT (s
!= NULL
);
6761 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6763 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6764 if (s
->contents
== NULL
)
6767 /* The first entry in .dynsym is a dummy symbol. Clear all the
6768 section syms, in case we don't output them all. */
6769 ++section_sym_count
;
6770 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6772 elf_hash_table (info
)->bucketcount
= 0;
6774 /* Compute the size of the hashing table. As a side effect this
6775 computes the hash values for all the names we export. */
6776 if (info
->emit_hash
)
6778 unsigned long int *hashcodes
;
6779 struct hash_codes_info hashinf
;
6781 unsigned long int nsyms
;
6783 size_t hash_entry_size
;
6785 /* Compute the hash values for all exported symbols. At the same
6786 time store the values in an array so that we could use them for
6788 amt
= dynsymcount
* sizeof (unsigned long int);
6789 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6790 if (hashcodes
== NULL
)
6792 hashinf
.hashcodes
= hashcodes
;
6793 hashinf
.error
= FALSE
;
6795 /* Put all hash values in HASHCODES. */
6796 elf_link_hash_traverse (elf_hash_table (info
),
6797 elf_collect_hash_codes
, &hashinf
);
6804 nsyms
= hashinf
.hashcodes
- hashcodes
;
6806 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6809 if (bucketcount
== 0)
6812 elf_hash_table (info
)->bucketcount
= bucketcount
;
6814 s
= bfd_get_linker_section (dynobj
, ".hash");
6815 BFD_ASSERT (s
!= NULL
);
6816 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6817 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6818 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6819 if (s
->contents
== NULL
)
6822 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6823 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6824 s
->contents
+ hash_entry_size
);
6827 if (info
->emit_gnu_hash
)
6830 unsigned char *contents
;
6831 struct collect_gnu_hash_codes cinfo
;
6835 memset (&cinfo
, 0, sizeof (cinfo
));
6837 /* Compute the hash values for all exported symbols. At the same
6838 time store the values in an array so that we could use them for
6840 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6841 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6842 if (cinfo
.hashcodes
== NULL
)
6845 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6846 cinfo
.min_dynindx
= -1;
6847 cinfo
.output_bfd
= output_bfd
;
6850 /* Put all hash values in HASHCODES. */
6851 elf_link_hash_traverse (elf_hash_table (info
),
6852 elf_collect_gnu_hash_codes
, &cinfo
);
6855 free (cinfo
.hashcodes
);
6860 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6862 if (bucketcount
== 0)
6864 free (cinfo
.hashcodes
);
6868 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6869 BFD_ASSERT (s
!= NULL
);
6871 if (cinfo
.nsyms
== 0)
6873 /* Empty .gnu.hash section is special. */
6874 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6875 free (cinfo
.hashcodes
);
6876 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6877 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6878 if (contents
== NULL
)
6880 s
->contents
= contents
;
6881 /* 1 empty bucket. */
6882 bfd_put_32 (output_bfd
, 1, contents
);
6883 /* SYMIDX above the special symbol 0. */
6884 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6885 /* Just one word for bitmask. */
6886 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6887 /* Only hash fn bloom filter. */
6888 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6889 /* No hashes are valid - empty bitmask. */
6890 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6891 /* No hashes in the only bucket. */
6892 bfd_put_32 (output_bfd
, 0,
6893 contents
+ 16 + bed
->s
->arch_size
/ 8);
6897 unsigned long int maskwords
, maskbitslog2
, x
;
6898 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6902 while ((x
>>= 1) != 0)
6904 if (maskbitslog2
< 3)
6906 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6907 maskbitslog2
= maskbitslog2
+ 3;
6909 maskbitslog2
= maskbitslog2
+ 2;
6910 if (bed
->s
->arch_size
== 64)
6912 if (maskbitslog2
== 5)
6918 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6919 cinfo
.shift2
= maskbitslog2
;
6920 cinfo
.maskbits
= 1 << maskbitslog2
;
6921 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6922 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6923 amt
+= maskwords
* sizeof (bfd_vma
);
6924 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6925 if (cinfo
.bitmask
== NULL
)
6927 free (cinfo
.hashcodes
);
6931 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6932 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6933 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6934 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6936 /* Determine how often each hash bucket is used. */
6937 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6938 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6939 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6941 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6942 if (cinfo
.counts
[i
] != 0)
6944 cinfo
.indx
[i
] = cnt
;
6945 cnt
+= cinfo
.counts
[i
];
6947 BFD_ASSERT (cnt
== dynsymcount
);
6948 cinfo
.bucketcount
= bucketcount
;
6949 cinfo
.local_indx
= cinfo
.min_dynindx
;
6951 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6952 s
->size
+= cinfo
.maskbits
/ 8;
6953 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6954 if (contents
== NULL
)
6956 free (cinfo
.bitmask
);
6957 free (cinfo
.hashcodes
);
6961 s
->contents
= contents
;
6962 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6963 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6964 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6965 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6966 contents
+= 16 + cinfo
.maskbits
/ 8;
6968 for (i
= 0; i
< bucketcount
; ++i
)
6970 if (cinfo
.counts
[i
] == 0)
6971 bfd_put_32 (output_bfd
, 0, contents
);
6973 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6977 cinfo
.contents
= contents
;
6979 /* Renumber dynamic symbols, populate .gnu.hash section. */
6980 elf_link_hash_traverse (elf_hash_table (info
),
6981 elf_renumber_gnu_hash_syms
, &cinfo
);
6983 contents
= s
->contents
+ 16;
6984 for (i
= 0; i
< maskwords
; ++i
)
6986 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6988 contents
+= bed
->s
->arch_size
/ 8;
6991 free (cinfo
.bitmask
);
6992 free (cinfo
.hashcodes
);
6996 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6997 BFD_ASSERT (s
!= NULL
);
6999 elf_finalize_dynstr (output_bfd
, info
);
7001 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7003 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7004 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7011 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7014 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7017 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7018 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7021 /* Finish SHF_MERGE section merging. */
7024 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7029 if (!is_elf_hash_table (info
->hash
))
7032 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7033 if ((ibfd
->flags
& DYNAMIC
) == 0
7034 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7035 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7036 == get_elf_backend_data (obfd
)->s
->elfclass
))
7037 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7038 if ((sec
->flags
& SEC_MERGE
) != 0
7039 && !bfd_is_abs_section (sec
->output_section
))
7041 struct bfd_elf_section_data
*secdata
;
7043 secdata
= elf_section_data (sec
);
7044 if (! _bfd_add_merge_section (obfd
,
7045 &elf_hash_table (info
)->merge_info
,
7046 sec
, &secdata
->sec_info
))
7048 else if (secdata
->sec_info
)
7049 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7052 if (elf_hash_table (info
)->merge_info
!= NULL
)
7053 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7054 merge_sections_remove_hook
);
7058 /* Create an entry in an ELF linker hash table. */
7060 struct bfd_hash_entry
*
7061 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7062 struct bfd_hash_table
*table
,
7065 /* Allocate the structure if it has not already been allocated by a
7069 entry
= (struct bfd_hash_entry
*)
7070 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7075 /* Call the allocation method of the superclass. */
7076 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7079 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7080 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7082 /* Set local fields. */
7085 ret
->got
= htab
->init_got_refcount
;
7086 ret
->plt
= htab
->init_plt_refcount
;
7087 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7088 - offsetof (struct elf_link_hash_entry
, size
)));
7089 /* Assume that we have been called by a non-ELF symbol reader.
7090 This flag is then reset by the code which reads an ELF input
7091 file. This ensures that a symbol created by a non-ELF symbol
7092 reader will have the flag set correctly. */
7099 /* Copy data from an indirect symbol to its direct symbol, hiding the
7100 old indirect symbol. Also used for copying flags to a weakdef. */
7103 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7104 struct elf_link_hash_entry
*dir
,
7105 struct elf_link_hash_entry
*ind
)
7107 struct elf_link_hash_table
*htab
;
7109 /* Copy down any references that we may have already seen to the
7110 symbol which just became indirect. */
7112 if (dir
->versioned
!= versioned_hidden
)
7113 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7114 dir
->ref_regular
|= ind
->ref_regular
;
7115 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7116 dir
->non_got_ref
|= ind
->non_got_ref
;
7117 dir
->needs_plt
|= ind
->needs_plt
;
7118 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7120 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7123 /* Copy over the global and procedure linkage table refcount entries.
7124 These may have been already set up by a check_relocs routine. */
7125 htab
= elf_hash_table (info
);
7126 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7128 if (dir
->got
.refcount
< 0)
7129 dir
->got
.refcount
= 0;
7130 dir
->got
.refcount
+= ind
->got
.refcount
;
7131 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7134 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7136 if (dir
->plt
.refcount
< 0)
7137 dir
->plt
.refcount
= 0;
7138 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7139 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7142 if (ind
->dynindx
!= -1)
7144 if (dir
->dynindx
!= -1)
7145 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7146 dir
->dynindx
= ind
->dynindx
;
7147 dir
->dynstr_index
= ind
->dynstr_index
;
7149 ind
->dynstr_index
= 0;
7154 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7155 struct elf_link_hash_entry
*h
,
7156 bfd_boolean force_local
)
7158 /* STT_GNU_IFUNC symbol must go through PLT. */
7159 if (h
->type
!= STT_GNU_IFUNC
)
7161 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7166 h
->forced_local
= 1;
7167 if (h
->dynindx
!= -1)
7170 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7176 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7180 _bfd_elf_link_hash_table_init
7181 (struct elf_link_hash_table
*table
,
7183 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7184 struct bfd_hash_table
*,
7186 unsigned int entsize
,
7187 enum elf_target_id target_id
)
7190 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7192 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7193 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7194 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7195 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7196 /* The first dynamic symbol is a dummy. */
7197 table
->dynsymcount
= 1;
7199 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7201 table
->root
.type
= bfd_link_elf_hash_table
;
7202 table
->hash_table_id
= target_id
;
7207 /* Create an ELF linker hash table. */
7209 struct bfd_link_hash_table
*
7210 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7212 struct elf_link_hash_table
*ret
;
7213 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7215 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7219 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7220 sizeof (struct elf_link_hash_entry
),
7226 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7231 /* Destroy an ELF linker hash table. */
7234 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7236 struct elf_link_hash_table
*htab
;
7238 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7239 if (htab
->dynstr
!= NULL
)
7240 _bfd_elf_strtab_free (htab
->dynstr
);
7241 _bfd_merge_sections_free (htab
->merge_info
);
7242 _bfd_generic_link_hash_table_free (obfd
);
7245 /* This is a hook for the ELF emulation code in the generic linker to
7246 tell the backend linker what file name to use for the DT_NEEDED
7247 entry for a dynamic object. */
7250 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7252 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7253 && bfd_get_format (abfd
) == bfd_object
)
7254 elf_dt_name (abfd
) = name
;
7258 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7261 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7262 && bfd_get_format (abfd
) == bfd_object
)
7263 lib_class
= elf_dyn_lib_class (abfd
);
7270 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7272 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7273 && bfd_get_format (abfd
) == bfd_object
)
7274 elf_dyn_lib_class (abfd
) = lib_class
;
7277 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7278 the linker ELF emulation code. */
7280 struct bfd_link_needed_list
*
7281 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7282 struct bfd_link_info
*info
)
7284 if (! is_elf_hash_table (info
->hash
))
7286 return elf_hash_table (info
)->needed
;
7289 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7290 hook for the linker ELF emulation code. */
7292 struct bfd_link_needed_list
*
7293 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7294 struct bfd_link_info
*info
)
7296 if (! is_elf_hash_table (info
->hash
))
7298 return elf_hash_table (info
)->runpath
;
7301 /* Get the name actually used for a dynamic object for a link. This
7302 is the SONAME entry if there is one. Otherwise, it is the string
7303 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7306 bfd_elf_get_dt_soname (bfd
*abfd
)
7308 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7309 && bfd_get_format (abfd
) == bfd_object
)
7310 return elf_dt_name (abfd
);
7314 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7315 the ELF linker emulation code. */
7318 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7319 struct bfd_link_needed_list
**pneeded
)
7322 bfd_byte
*dynbuf
= NULL
;
7323 unsigned int elfsec
;
7324 unsigned long shlink
;
7325 bfd_byte
*extdyn
, *extdynend
;
7327 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7331 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7332 || bfd_get_format (abfd
) != bfd_object
)
7335 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7336 if (s
== NULL
|| s
->size
== 0)
7339 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7342 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7343 if (elfsec
== SHN_BAD
)
7346 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7348 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7349 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7352 extdynend
= extdyn
+ s
->size
;
7353 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7355 Elf_Internal_Dyn dyn
;
7357 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7359 if (dyn
.d_tag
== DT_NULL
)
7362 if (dyn
.d_tag
== DT_NEEDED
)
7365 struct bfd_link_needed_list
*l
;
7366 unsigned int tagv
= dyn
.d_un
.d_val
;
7369 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7374 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7395 struct elf_symbuf_symbol
7397 unsigned long st_name
; /* Symbol name, index in string tbl */
7398 unsigned char st_info
; /* Type and binding attributes */
7399 unsigned char st_other
; /* Visibilty, and target specific */
7402 struct elf_symbuf_head
7404 struct elf_symbuf_symbol
*ssym
;
7406 unsigned int st_shndx
;
7413 Elf_Internal_Sym
*isym
;
7414 struct elf_symbuf_symbol
*ssym
;
7419 /* Sort references to symbols by ascending section number. */
7422 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7424 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7425 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7427 return s1
->st_shndx
- s2
->st_shndx
;
7431 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7433 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7434 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7435 return strcmp (s1
->name
, s2
->name
);
7438 static struct elf_symbuf_head
*
7439 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7441 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7442 struct elf_symbuf_symbol
*ssym
;
7443 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7444 size_t i
, shndx_count
, total_size
;
7446 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7450 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7451 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7452 *ind
++ = &isymbuf
[i
];
7455 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7456 elf_sort_elf_symbol
);
7459 if (indbufend
> indbuf
)
7460 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7461 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7464 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7465 + (indbufend
- indbuf
) * sizeof (*ssym
));
7466 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7467 if (ssymbuf
== NULL
)
7473 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7474 ssymbuf
->ssym
= NULL
;
7475 ssymbuf
->count
= shndx_count
;
7476 ssymbuf
->st_shndx
= 0;
7477 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7479 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7482 ssymhead
->ssym
= ssym
;
7483 ssymhead
->count
= 0;
7484 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7486 ssym
->st_name
= (*ind
)->st_name
;
7487 ssym
->st_info
= (*ind
)->st_info
;
7488 ssym
->st_other
= (*ind
)->st_other
;
7491 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7492 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7499 /* Check if 2 sections define the same set of local and global
7503 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7504 struct bfd_link_info
*info
)
7507 const struct elf_backend_data
*bed1
, *bed2
;
7508 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7509 size_t symcount1
, symcount2
;
7510 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7511 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7512 Elf_Internal_Sym
*isym
, *isymend
;
7513 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7514 size_t count1
, count2
, i
;
7515 unsigned int shndx1
, shndx2
;
7521 /* Both sections have to be in ELF. */
7522 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7523 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7526 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7529 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7530 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7531 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7534 bed1
= get_elf_backend_data (bfd1
);
7535 bed2
= get_elf_backend_data (bfd2
);
7536 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7537 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7538 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7539 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7541 if (symcount1
== 0 || symcount2
== 0)
7547 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7548 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7550 if (ssymbuf1
== NULL
)
7552 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7554 if (isymbuf1
== NULL
)
7557 if (!info
->reduce_memory_overheads
)
7558 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7559 = elf_create_symbuf (symcount1
, isymbuf1
);
7562 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7564 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7566 if (isymbuf2
== NULL
)
7569 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7570 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7571 = elf_create_symbuf (symcount2
, isymbuf2
);
7574 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7576 /* Optimized faster version. */
7578 struct elf_symbol
*symp
;
7579 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7582 hi
= ssymbuf1
->count
;
7587 mid
= (lo
+ hi
) / 2;
7588 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7590 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7594 count1
= ssymbuf1
[mid
].count
;
7601 hi
= ssymbuf2
->count
;
7606 mid
= (lo
+ hi
) / 2;
7607 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7609 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7613 count2
= ssymbuf2
[mid
].count
;
7619 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7623 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7625 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7626 if (symtable1
== NULL
|| symtable2
== NULL
)
7630 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7631 ssym
< ssymend
; ssym
++, symp
++)
7633 symp
->u
.ssym
= ssym
;
7634 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7640 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7641 ssym
< ssymend
; ssym
++, symp
++)
7643 symp
->u
.ssym
= ssym
;
7644 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7649 /* Sort symbol by name. */
7650 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7651 elf_sym_name_compare
);
7652 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7653 elf_sym_name_compare
);
7655 for (i
= 0; i
< count1
; i
++)
7656 /* Two symbols must have the same binding, type and name. */
7657 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7658 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7659 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7666 symtable1
= (struct elf_symbol
*)
7667 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7668 symtable2
= (struct elf_symbol
*)
7669 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7670 if (symtable1
== NULL
|| symtable2
== NULL
)
7673 /* Count definitions in the section. */
7675 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7676 if (isym
->st_shndx
== shndx1
)
7677 symtable1
[count1
++].u
.isym
= isym
;
7680 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7681 if (isym
->st_shndx
== shndx2
)
7682 symtable2
[count2
++].u
.isym
= isym
;
7684 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7687 for (i
= 0; i
< count1
; i
++)
7689 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7690 symtable1
[i
].u
.isym
->st_name
);
7692 for (i
= 0; i
< count2
; i
++)
7694 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7695 symtable2
[i
].u
.isym
->st_name
);
7697 /* Sort symbol by name. */
7698 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7699 elf_sym_name_compare
);
7700 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7701 elf_sym_name_compare
);
7703 for (i
= 0; i
< count1
; i
++)
7704 /* Two symbols must have the same binding, type and name. */
7705 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7706 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7707 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7725 /* Return TRUE if 2 section types are compatible. */
7728 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7729 bfd
*bbfd
, const asection
*bsec
)
7733 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7734 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7737 return elf_section_type (asec
) == elf_section_type (bsec
);
7740 /* Final phase of ELF linker. */
7742 /* A structure we use to avoid passing large numbers of arguments. */
7744 struct elf_final_link_info
7746 /* General link information. */
7747 struct bfd_link_info
*info
;
7750 /* Symbol string table. */
7751 struct elf_strtab_hash
*symstrtab
;
7752 /* .hash section. */
7754 /* symbol version section (.gnu.version). */
7755 asection
*symver_sec
;
7756 /* Buffer large enough to hold contents of any section. */
7758 /* Buffer large enough to hold external relocs of any section. */
7759 void *external_relocs
;
7760 /* Buffer large enough to hold internal relocs of any section. */
7761 Elf_Internal_Rela
*internal_relocs
;
7762 /* Buffer large enough to hold external local symbols of any input
7764 bfd_byte
*external_syms
;
7765 /* And a buffer for symbol section indices. */
7766 Elf_External_Sym_Shndx
*locsym_shndx
;
7767 /* Buffer large enough to hold internal local symbols of any input
7769 Elf_Internal_Sym
*internal_syms
;
7770 /* Array large enough to hold a symbol index for each local symbol
7771 of any input BFD. */
7773 /* Array large enough to hold a section pointer for each local
7774 symbol of any input BFD. */
7775 asection
**sections
;
7776 /* Buffer for SHT_SYMTAB_SHNDX section. */
7777 Elf_External_Sym_Shndx
*symshndxbuf
;
7778 /* Number of STT_FILE syms seen. */
7779 size_t filesym_count
;
7782 /* This struct is used to pass information to elf_link_output_extsym. */
7784 struct elf_outext_info
7787 bfd_boolean localsyms
;
7788 bfd_boolean file_sym_done
;
7789 struct elf_final_link_info
*flinfo
;
7793 /* Support for evaluating a complex relocation.
7795 Complex relocations are generalized, self-describing relocations. The
7796 implementation of them consists of two parts: complex symbols, and the
7797 relocations themselves.
7799 The relocations are use a reserved elf-wide relocation type code (R_RELC
7800 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7801 information (start bit, end bit, word width, etc) into the addend. This
7802 information is extracted from CGEN-generated operand tables within gas.
7804 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7805 internal) representing prefix-notation expressions, including but not
7806 limited to those sorts of expressions normally encoded as addends in the
7807 addend field. The symbol mangling format is:
7810 | <unary-operator> ':' <node>
7811 | <binary-operator> ':' <node> ':' <node>
7814 <literal> := 's' <digits=N> ':' <N character symbol name>
7815 | 'S' <digits=N> ':' <N character section name>
7819 <binary-operator> := as in C
7820 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7823 set_symbol_value (bfd
*bfd_with_globals
,
7824 Elf_Internal_Sym
*isymbuf
,
7829 struct elf_link_hash_entry
**sym_hashes
;
7830 struct elf_link_hash_entry
*h
;
7831 size_t extsymoff
= locsymcount
;
7833 if (symidx
< locsymcount
)
7835 Elf_Internal_Sym
*sym
;
7837 sym
= isymbuf
+ symidx
;
7838 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7840 /* It is a local symbol: move it to the
7841 "absolute" section and give it a value. */
7842 sym
->st_shndx
= SHN_ABS
;
7843 sym
->st_value
= val
;
7846 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7850 /* It is a global symbol: set its link type
7851 to "defined" and give it a value. */
7853 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7854 h
= sym_hashes
[symidx
- extsymoff
];
7855 while (h
->root
.type
== bfd_link_hash_indirect
7856 || h
->root
.type
== bfd_link_hash_warning
)
7857 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7858 h
->root
.type
= bfd_link_hash_defined
;
7859 h
->root
.u
.def
.value
= val
;
7860 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7864 resolve_symbol (const char *name
,
7866 struct elf_final_link_info
*flinfo
,
7868 Elf_Internal_Sym
*isymbuf
,
7871 Elf_Internal_Sym
*sym
;
7872 struct bfd_link_hash_entry
*global_entry
;
7873 const char *candidate
= NULL
;
7874 Elf_Internal_Shdr
*symtab_hdr
;
7877 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7879 for (i
= 0; i
< locsymcount
; ++ i
)
7883 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7886 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7887 symtab_hdr
->sh_link
,
7890 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7891 name
, candidate
, (unsigned long) sym
->st_value
);
7893 if (candidate
&& strcmp (candidate
, name
) == 0)
7895 asection
*sec
= flinfo
->sections
[i
];
7897 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7898 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7900 printf ("Found symbol with value %8.8lx\n",
7901 (unsigned long) *result
);
7907 /* Hmm, haven't found it yet. perhaps it is a global. */
7908 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7909 FALSE
, FALSE
, TRUE
);
7913 if (global_entry
->type
== bfd_link_hash_defined
7914 || global_entry
->type
== bfd_link_hash_defweak
)
7916 *result
= (global_entry
->u
.def
.value
7917 + global_entry
->u
.def
.section
->output_section
->vma
7918 + global_entry
->u
.def
.section
->output_offset
);
7920 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7921 global_entry
->root
.string
, (unsigned long) *result
);
7929 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7930 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7931 names like "foo.end" which is the end address of section "foo". */
7934 resolve_section (const char *name
,
7942 for (curr
= sections
; curr
; curr
= curr
->next
)
7943 if (strcmp (curr
->name
, name
) == 0)
7945 *result
= curr
->vma
;
7949 /* Hmm. still haven't found it. try pseudo-section names. */
7950 /* FIXME: This could be coded more efficiently... */
7951 for (curr
= sections
; curr
; curr
= curr
->next
)
7953 len
= strlen (curr
->name
);
7954 if (len
> strlen (name
))
7957 if (strncmp (curr
->name
, name
, len
) == 0)
7959 if (strncmp (".end", name
+ len
, 4) == 0)
7961 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7965 /* Insert more pseudo-section names here, if you like. */
7973 undefined_reference (const char *reftype
, const char *name
)
7975 /* xgettext:c-format */
7976 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7981 eval_symbol (bfd_vma
*result
,
7984 struct elf_final_link_info
*flinfo
,
7986 Elf_Internal_Sym
*isymbuf
,
7995 const char *sym
= *symp
;
7997 bfd_boolean symbol_is_section
= FALSE
;
8002 if (len
< 1 || len
> sizeof (symbuf
))
8004 bfd_set_error (bfd_error_invalid_operation
);
8017 *result
= strtoul (sym
, (char **) symp
, 16);
8021 symbol_is_section
= TRUE
;
8025 symlen
= strtol (sym
, (char **) symp
, 10);
8026 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8028 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8030 bfd_set_error (bfd_error_invalid_operation
);
8034 memcpy (symbuf
, sym
, symlen
);
8035 symbuf
[symlen
] = '\0';
8036 *symp
= sym
+ symlen
;
8038 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8039 the symbol as a section, or vice-versa. so we're pretty liberal in our
8040 interpretation here; section means "try section first", not "must be a
8041 section", and likewise with symbol. */
8043 if (symbol_is_section
)
8045 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8046 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8047 isymbuf
, locsymcount
))
8049 undefined_reference ("section", symbuf
);
8055 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8056 isymbuf
, locsymcount
)
8057 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8060 undefined_reference ("symbol", symbuf
);
8067 /* All that remains are operators. */
8069 #define UNARY_OP(op) \
8070 if (strncmp (sym, #op, strlen (#op)) == 0) \
8072 sym += strlen (#op); \
8076 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8077 isymbuf, locsymcount, signed_p)) \
8080 *result = op ((bfd_signed_vma) a); \
8086 #define BINARY_OP(op) \
8087 if (strncmp (sym, #op, strlen (#op)) == 0) \
8089 sym += strlen (#op); \
8093 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8094 isymbuf, locsymcount, signed_p)) \
8097 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8098 isymbuf, locsymcount, signed_p)) \
8101 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8131 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8132 bfd_set_error (bfd_error_invalid_operation
);
8138 put_value (bfd_vma size
,
8139 unsigned long chunksz
,
8144 location
+= (size
- chunksz
);
8146 for (; size
; size
-= chunksz
, location
-= chunksz
)
8151 bfd_put_8 (input_bfd
, x
, location
);
8155 bfd_put_16 (input_bfd
, x
, location
);
8159 bfd_put_32 (input_bfd
, x
, location
);
8160 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8166 bfd_put_64 (input_bfd
, x
, location
);
8167 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8180 get_value (bfd_vma size
,
8181 unsigned long chunksz
,
8188 /* Sanity checks. */
8189 BFD_ASSERT (chunksz
<= sizeof (x
)
8192 && (size
% chunksz
) == 0
8193 && input_bfd
!= NULL
8194 && location
!= NULL
);
8196 if (chunksz
== sizeof (x
))
8198 BFD_ASSERT (size
== chunksz
);
8200 /* Make sure that we do not perform an undefined shift operation.
8201 We know that size == chunksz so there will only be one iteration
8202 of the loop below. */
8206 shift
= 8 * chunksz
;
8208 for (; size
; size
-= chunksz
, location
+= chunksz
)
8213 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8216 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8219 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8223 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8234 decode_complex_addend (unsigned long *start
, /* in bits */
8235 unsigned long *oplen
, /* in bits */
8236 unsigned long *len
, /* in bits */
8237 unsigned long *wordsz
, /* in bytes */
8238 unsigned long *chunksz
, /* in bytes */
8239 unsigned long *lsb0_p
,
8240 unsigned long *signed_p
,
8241 unsigned long *trunc_p
,
8242 unsigned long encoded
)
8244 * start
= encoded
& 0x3F;
8245 * len
= (encoded
>> 6) & 0x3F;
8246 * oplen
= (encoded
>> 12) & 0x3F;
8247 * wordsz
= (encoded
>> 18) & 0xF;
8248 * chunksz
= (encoded
>> 22) & 0xF;
8249 * lsb0_p
= (encoded
>> 27) & 1;
8250 * signed_p
= (encoded
>> 28) & 1;
8251 * trunc_p
= (encoded
>> 29) & 1;
8254 bfd_reloc_status_type
8255 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8256 asection
*input_section ATTRIBUTE_UNUSED
,
8258 Elf_Internal_Rela
*rel
,
8261 bfd_vma shift
, x
, mask
;
8262 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8263 bfd_reloc_status_type r
;
8265 /* Perform this reloc, since it is complex.
8266 (this is not to say that it necessarily refers to a complex
8267 symbol; merely that it is a self-describing CGEN based reloc.
8268 i.e. the addend has the complete reloc information (bit start, end,
8269 word size, etc) encoded within it.). */
8271 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8272 &chunksz
, &lsb0_p
, &signed_p
,
8273 &trunc_p
, rel
->r_addend
);
8275 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8278 shift
= (start
+ 1) - len
;
8280 shift
= (8 * wordsz
) - (start
+ len
);
8282 x
= get_value (wordsz
, chunksz
, input_bfd
,
8283 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8286 printf ("Doing complex reloc: "
8287 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8288 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8289 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8290 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8291 oplen
, (unsigned long) x
, (unsigned long) mask
,
8292 (unsigned long) relocation
);
8297 /* Now do an overflow check. */
8298 r
= bfd_check_overflow ((signed_p
8299 ? complain_overflow_signed
8300 : complain_overflow_unsigned
),
8301 len
, 0, (8 * wordsz
),
8305 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8308 printf (" relocation: %8.8lx\n"
8309 " shifted mask: %8.8lx\n"
8310 " shifted/masked reloc: %8.8lx\n"
8311 " result: %8.8lx\n",
8312 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8313 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8315 put_value (wordsz
, chunksz
, input_bfd
, x
,
8316 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8320 /* Functions to read r_offset from external (target order) reloc
8321 entry. Faster than bfd_getl32 et al, because we let the compiler
8322 know the value is aligned. */
8325 ext32l_r_offset (const void *p
)
8332 const union aligned32
*a
8333 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8335 uint32_t aval
= ( (uint32_t) a
->c
[0]
8336 | (uint32_t) a
->c
[1] << 8
8337 | (uint32_t) a
->c
[2] << 16
8338 | (uint32_t) a
->c
[3] << 24);
8343 ext32b_r_offset (const void *p
)
8350 const union aligned32
*a
8351 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8353 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8354 | (uint32_t) a
->c
[1] << 16
8355 | (uint32_t) a
->c
[2] << 8
8356 | (uint32_t) a
->c
[3]);
8360 #ifdef BFD_HOST_64_BIT
8362 ext64l_r_offset (const void *p
)
8369 const union aligned64
*a
8370 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8372 uint64_t aval
= ( (uint64_t) a
->c
[0]
8373 | (uint64_t) a
->c
[1] << 8
8374 | (uint64_t) a
->c
[2] << 16
8375 | (uint64_t) a
->c
[3] << 24
8376 | (uint64_t) a
->c
[4] << 32
8377 | (uint64_t) a
->c
[5] << 40
8378 | (uint64_t) a
->c
[6] << 48
8379 | (uint64_t) a
->c
[7] << 56);
8384 ext64b_r_offset (const void *p
)
8391 const union aligned64
*a
8392 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8394 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8395 | (uint64_t) a
->c
[1] << 48
8396 | (uint64_t) a
->c
[2] << 40
8397 | (uint64_t) a
->c
[3] << 32
8398 | (uint64_t) a
->c
[4] << 24
8399 | (uint64_t) a
->c
[5] << 16
8400 | (uint64_t) a
->c
[6] << 8
8401 | (uint64_t) a
->c
[7]);
8406 /* When performing a relocatable link, the input relocations are
8407 preserved. But, if they reference global symbols, the indices
8408 referenced must be updated. Update all the relocations found in
8412 elf_link_adjust_relocs (bfd
*abfd
,
8414 struct bfd_elf_section_reloc_data
*reldata
,
8418 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8420 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8421 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8422 bfd_vma r_type_mask
;
8424 unsigned int count
= reldata
->count
;
8425 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8427 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8429 swap_in
= bed
->s
->swap_reloc_in
;
8430 swap_out
= bed
->s
->swap_reloc_out
;
8432 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8434 swap_in
= bed
->s
->swap_reloca_in
;
8435 swap_out
= bed
->s
->swap_reloca_out
;
8440 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8443 if (bed
->s
->arch_size
== 32)
8450 r_type_mask
= 0xffffffff;
8454 erela
= reldata
->hdr
->contents
;
8455 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8457 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8460 if (*rel_hash
== NULL
)
8463 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8465 (*swap_in
) (abfd
, erela
, irela
);
8466 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8467 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8468 | (irela
[j
].r_info
& r_type_mask
));
8469 (*swap_out
) (abfd
, irela
, erela
);
8472 if (bed
->elf_backend_update_relocs
)
8473 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8475 if (sort
&& count
!= 0)
8477 bfd_vma (*ext_r_off
) (const void *);
8480 bfd_byte
*base
, *end
, *p
, *loc
;
8481 bfd_byte
*buf
= NULL
;
8483 if (bed
->s
->arch_size
== 32)
8485 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8486 ext_r_off
= ext32l_r_offset
;
8487 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8488 ext_r_off
= ext32b_r_offset
;
8494 #ifdef BFD_HOST_64_BIT
8495 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8496 ext_r_off
= ext64l_r_offset
;
8497 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8498 ext_r_off
= ext64b_r_offset
;
8504 /* Must use a stable sort here. A modified insertion sort,
8505 since the relocs are mostly sorted already. */
8506 elt_size
= reldata
->hdr
->sh_entsize
;
8507 base
= reldata
->hdr
->contents
;
8508 end
= base
+ count
* elt_size
;
8509 if (elt_size
> sizeof (Elf64_External_Rela
))
8512 /* Ensure the first element is lowest. This acts as a sentinel,
8513 speeding the main loop below. */
8514 r_off
= (*ext_r_off
) (base
);
8515 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8517 bfd_vma r_off2
= (*ext_r_off
) (p
);
8526 /* Don't just swap *base and *loc as that changes the order
8527 of the original base[0] and base[1] if they happen to
8528 have the same r_offset. */
8529 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8530 memcpy (onebuf
, loc
, elt_size
);
8531 memmove (base
+ elt_size
, base
, loc
- base
);
8532 memcpy (base
, onebuf
, elt_size
);
8535 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8537 /* base to p is sorted, *p is next to insert. */
8538 r_off
= (*ext_r_off
) (p
);
8539 /* Search the sorted region for location to insert. */
8541 while (r_off
< (*ext_r_off
) (loc
))
8546 /* Chances are there is a run of relocs to insert here,
8547 from one of more input files. Files are not always
8548 linked in order due to the way elf_link_input_bfd is
8549 called. See pr17666. */
8550 size_t sortlen
= p
- loc
;
8551 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8552 size_t runlen
= elt_size
;
8553 size_t buf_size
= 96 * 1024;
8554 while (p
+ runlen
< end
8555 && (sortlen
<= buf_size
8556 || runlen
+ elt_size
<= buf_size
)
8557 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8561 buf
= bfd_malloc (buf_size
);
8565 if (runlen
< sortlen
)
8567 memcpy (buf
, p
, runlen
);
8568 memmove (loc
+ runlen
, loc
, sortlen
);
8569 memcpy (loc
, buf
, runlen
);
8573 memcpy (buf
, loc
, sortlen
);
8574 memmove (loc
, p
, runlen
);
8575 memcpy (loc
+ runlen
, buf
, sortlen
);
8577 p
+= runlen
- elt_size
;
8580 /* Hashes are no longer valid. */
8581 free (reldata
->hashes
);
8582 reldata
->hashes
= NULL
;
8588 struct elf_link_sort_rela
8594 enum elf_reloc_type_class type
;
8595 /* We use this as an array of size int_rels_per_ext_rel. */
8596 Elf_Internal_Rela rela
[1];
8600 elf_link_sort_cmp1 (const void *A
, const void *B
)
8602 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8603 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8604 int relativea
, relativeb
;
8606 relativea
= a
->type
== reloc_class_relative
;
8607 relativeb
= b
->type
== reloc_class_relative
;
8609 if (relativea
< relativeb
)
8611 if (relativea
> relativeb
)
8613 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8615 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8617 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8619 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8625 elf_link_sort_cmp2 (const void *A
, const void *B
)
8627 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8628 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8630 if (a
->type
< b
->type
)
8632 if (a
->type
> b
->type
)
8634 if (a
->u
.offset
< b
->u
.offset
)
8636 if (a
->u
.offset
> b
->u
.offset
)
8638 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8640 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8646 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8648 asection
*dynamic_relocs
;
8651 bfd_size_type count
, size
;
8652 size_t i
, ret
, sort_elt
, ext_size
;
8653 bfd_byte
*sort
, *s_non_relative
, *p
;
8654 struct elf_link_sort_rela
*sq
;
8655 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8656 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8657 unsigned int opb
= bfd_octets_per_byte (abfd
);
8658 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8659 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8660 struct bfd_link_order
*lo
;
8662 bfd_boolean use_rela
;
8664 /* Find a dynamic reloc section. */
8665 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8666 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8667 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8668 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8670 bfd_boolean use_rela_initialised
= FALSE
;
8672 /* This is just here to stop gcc from complaining.
8673 Its initialization checking code is not perfect. */
8676 /* Both sections are present. Examine the sizes
8677 of the indirect sections to help us choose. */
8678 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8679 if (lo
->type
== bfd_indirect_link_order
)
8681 asection
*o
= lo
->u
.indirect
.section
;
8683 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8685 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8686 /* Section size is divisible by both rel and rela sizes.
8687 It is of no help to us. */
8691 /* Section size is only divisible by rela. */
8692 if (use_rela_initialised
&& (use_rela
== FALSE
))
8694 _bfd_error_handler (_("%B: Unable to sort relocs - "
8695 "they are in more than one size"),
8697 bfd_set_error (bfd_error_invalid_operation
);
8703 use_rela_initialised
= TRUE
;
8707 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8709 /* Section size is only divisible by rel. */
8710 if (use_rela_initialised
&& (use_rela
== TRUE
))
8712 _bfd_error_handler (_("%B: Unable to sort relocs - "
8713 "they are in more than one size"),
8715 bfd_set_error (bfd_error_invalid_operation
);
8721 use_rela_initialised
= TRUE
;
8726 /* The section size is not divisible by either -
8727 something is wrong. */
8728 _bfd_error_handler (_("%B: Unable to sort relocs - "
8729 "they are of an unknown size"), abfd
);
8730 bfd_set_error (bfd_error_invalid_operation
);
8735 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8736 if (lo
->type
== bfd_indirect_link_order
)
8738 asection
*o
= lo
->u
.indirect
.section
;
8740 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8742 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8743 /* Section size is divisible by both rel and rela sizes.
8744 It is of no help to us. */
8748 /* Section size is only divisible by rela. */
8749 if (use_rela_initialised
&& (use_rela
== FALSE
))
8751 _bfd_error_handler (_("%B: Unable to sort relocs - "
8752 "they are in more than one size"),
8754 bfd_set_error (bfd_error_invalid_operation
);
8760 use_rela_initialised
= TRUE
;
8764 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8766 /* Section size is only divisible by rel. */
8767 if (use_rela_initialised
&& (use_rela
== TRUE
))
8769 _bfd_error_handler (_("%B: Unable to sort relocs - "
8770 "they are in more than one size"),
8772 bfd_set_error (bfd_error_invalid_operation
);
8778 use_rela_initialised
= TRUE
;
8783 /* The section size is not divisible by either -
8784 something is wrong. */
8785 _bfd_error_handler (_("%B: Unable to sort relocs - "
8786 "they are of an unknown size"), abfd
);
8787 bfd_set_error (bfd_error_invalid_operation
);
8792 if (! use_rela_initialised
)
8796 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8798 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8805 dynamic_relocs
= rela_dyn
;
8806 ext_size
= bed
->s
->sizeof_rela
;
8807 swap_in
= bed
->s
->swap_reloca_in
;
8808 swap_out
= bed
->s
->swap_reloca_out
;
8812 dynamic_relocs
= rel_dyn
;
8813 ext_size
= bed
->s
->sizeof_rel
;
8814 swap_in
= bed
->s
->swap_reloc_in
;
8815 swap_out
= bed
->s
->swap_reloc_out
;
8819 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8820 if (lo
->type
== bfd_indirect_link_order
)
8821 size
+= lo
->u
.indirect
.section
->size
;
8823 if (size
!= dynamic_relocs
->size
)
8826 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8827 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8829 count
= dynamic_relocs
->size
/ ext_size
;
8832 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8836 (*info
->callbacks
->warning
)
8837 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8841 if (bed
->s
->arch_size
== 32)
8842 r_sym_mask
= ~(bfd_vma
) 0xff;
8844 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8846 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8847 if (lo
->type
== bfd_indirect_link_order
)
8849 bfd_byte
*erel
, *erelend
;
8850 asection
*o
= lo
->u
.indirect
.section
;
8852 if (o
->contents
== NULL
&& o
->size
!= 0)
8854 /* This is a reloc section that is being handled as a normal
8855 section. See bfd_section_from_shdr. We can't combine
8856 relocs in this case. */
8861 erelend
= o
->contents
+ o
->size
;
8862 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8864 while (erel
< erelend
)
8866 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8868 (*swap_in
) (abfd
, erel
, s
->rela
);
8869 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8870 s
->u
.sym_mask
= r_sym_mask
;
8876 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8878 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8880 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8881 if (s
->type
!= reloc_class_relative
)
8887 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8888 for (; i
< count
; i
++, p
+= sort_elt
)
8890 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8891 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8893 sp
->u
.offset
= sq
->rela
->r_offset
;
8896 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8898 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8899 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8901 /* We have plt relocs in .rela.dyn. */
8902 sq
= (struct elf_link_sort_rela
*) sort
;
8903 for (i
= 0; i
< count
; i
++)
8904 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8906 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8908 struct bfd_link_order
**plo
;
8909 /* Put srelplt link_order last. This is so the output_offset
8910 set in the next loop is correct for DT_JMPREL. */
8911 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8912 if ((*plo
)->type
== bfd_indirect_link_order
8913 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8919 plo
= &(*plo
)->next
;
8922 dynamic_relocs
->map_tail
.link_order
= lo
;
8927 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8928 if (lo
->type
== bfd_indirect_link_order
)
8930 bfd_byte
*erel
, *erelend
;
8931 asection
*o
= lo
->u
.indirect
.section
;
8934 erelend
= o
->contents
+ o
->size
;
8935 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8936 while (erel
< erelend
)
8938 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8939 (*swap_out
) (abfd
, s
->rela
, erel
);
8946 *psec
= dynamic_relocs
;
8950 /* Add a symbol to the output symbol string table. */
8953 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8955 Elf_Internal_Sym
*elfsym
,
8956 asection
*input_sec
,
8957 struct elf_link_hash_entry
*h
)
8959 int (*output_symbol_hook
)
8960 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8961 struct elf_link_hash_entry
*);
8962 struct elf_link_hash_table
*hash_table
;
8963 const struct elf_backend_data
*bed
;
8964 bfd_size_type strtabsize
;
8966 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8968 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8969 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8970 if (output_symbol_hook
!= NULL
)
8972 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8979 || (input_sec
->flags
& SEC_EXCLUDE
))
8980 elfsym
->st_name
= (unsigned long) -1;
8983 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8984 to get the final offset for st_name. */
8986 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8988 if (elfsym
->st_name
== (unsigned long) -1)
8992 hash_table
= elf_hash_table (flinfo
->info
);
8993 strtabsize
= hash_table
->strtabsize
;
8994 if (strtabsize
<= hash_table
->strtabcount
)
8996 strtabsize
+= strtabsize
;
8997 hash_table
->strtabsize
= strtabsize
;
8998 strtabsize
*= sizeof (*hash_table
->strtab
);
9000 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9002 if (hash_table
->strtab
== NULL
)
9005 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9006 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9007 = hash_table
->strtabcount
;
9008 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9009 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9011 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9012 hash_table
->strtabcount
+= 1;
9017 /* Swap symbols out to the symbol table and flush the output symbols to
9021 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9023 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9026 const struct elf_backend_data
*bed
;
9028 Elf_Internal_Shdr
*hdr
;
9032 if (!hash_table
->strtabcount
)
9035 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9037 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9039 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9040 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9044 if (flinfo
->symshndxbuf
)
9046 amt
= sizeof (Elf_External_Sym_Shndx
);
9047 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9048 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9049 if (flinfo
->symshndxbuf
== NULL
)
9056 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9058 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9059 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9060 elfsym
->sym
.st_name
= 0;
9063 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9064 elfsym
->sym
.st_name
);
9065 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9066 ((bfd_byte
*) symbuf
9067 + (elfsym
->dest_index
9068 * bed
->s
->sizeof_sym
)),
9069 (flinfo
->symshndxbuf
9070 + elfsym
->destshndx_index
));
9073 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9074 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9075 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9076 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9077 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9079 hdr
->sh_size
+= amt
;
9087 free (hash_table
->strtab
);
9088 hash_table
->strtab
= NULL
;
9093 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9096 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9098 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9099 && sym
->st_shndx
< SHN_LORESERVE
)
9101 /* The gABI doesn't support dynamic symbols in output sections
9104 /* xgettext:c-format */
9105 (_("%B: Too many sections: %d (>= %d)"),
9106 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9107 bfd_set_error (bfd_error_nonrepresentable_section
);
9113 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9114 allowing an unsatisfied unversioned symbol in the DSO to match a
9115 versioned symbol that would normally require an explicit version.
9116 We also handle the case that a DSO references a hidden symbol
9117 which may be satisfied by a versioned symbol in another DSO. */
9120 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9121 const struct elf_backend_data
*bed
,
9122 struct elf_link_hash_entry
*h
)
9125 struct elf_link_loaded_list
*loaded
;
9127 if (!is_elf_hash_table (info
->hash
))
9130 /* Check indirect symbol. */
9131 while (h
->root
.type
== bfd_link_hash_indirect
)
9132 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9134 switch (h
->root
.type
)
9140 case bfd_link_hash_undefined
:
9141 case bfd_link_hash_undefweak
:
9142 abfd
= h
->root
.u
.undef
.abfd
;
9144 || (abfd
->flags
& DYNAMIC
) == 0
9145 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9149 case bfd_link_hash_defined
:
9150 case bfd_link_hash_defweak
:
9151 abfd
= h
->root
.u
.def
.section
->owner
;
9154 case bfd_link_hash_common
:
9155 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9158 BFD_ASSERT (abfd
!= NULL
);
9160 for (loaded
= elf_hash_table (info
)->loaded
;
9162 loaded
= loaded
->next
)
9165 Elf_Internal_Shdr
*hdr
;
9169 Elf_Internal_Shdr
*versymhdr
;
9170 Elf_Internal_Sym
*isym
;
9171 Elf_Internal_Sym
*isymend
;
9172 Elf_Internal_Sym
*isymbuf
;
9173 Elf_External_Versym
*ever
;
9174 Elf_External_Versym
*extversym
;
9176 input
= loaded
->abfd
;
9178 /* We check each DSO for a possible hidden versioned definition. */
9180 || (input
->flags
& DYNAMIC
) == 0
9181 || elf_dynversym (input
) == 0)
9184 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9186 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9187 if (elf_bad_symtab (input
))
9189 extsymcount
= symcount
;
9194 extsymcount
= symcount
- hdr
->sh_info
;
9195 extsymoff
= hdr
->sh_info
;
9198 if (extsymcount
== 0)
9201 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9203 if (isymbuf
== NULL
)
9206 /* Read in any version definitions. */
9207 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9208 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9209 if (extversym
== NULL
)
9212 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9213 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9214 != versymhdr
->sh_size
))
9222 ever
= extversym
+ extsymoff
;
9223 isymend
= isymbuf
+ extsymcount
;
9224 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9227 Elf_Internal_Versym iver
;
9228 unsigned short version_index
;
9230 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9231 || isym
->st_shndx
== SHN_UNDEF
)
9234 name
= bfd_elf_string_from_elf_section (input
,
9237 if (strcmp (name
, h
->root
.root
.string
) != 0)
9240 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9242 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9244 && h
->forced_local
))
9246 /* If we have a non-hidden versioned sym, then it should
9247 have provided a definition for the undefined sym unless
9248 it is defined in a non-shared object and forced local.
9253 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9254 if (version_index
== 1 || version_index
== 2)
9256 /* This is the base or first version. We can use it. */
9270 /* Convert ELF common symbol TYPE. */
9273 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9275 /* Commom symbol can only appear in relocatable link. */
9276 if (!bfd_link_relocatable (info
))
9278 switch (info
->elf_stt_common
)
9282 case elf_stt_common
:
9285 case no_elf_stt_common
:
9292 /* Add an external symbol to the symbol table. This is called from
9293 the hash table traversal routine. When generating a shared object,
9294 we go through the symbol table twice. The first time we output
9295 anything that might have been forced to local scope in a version
9296 script. The second time we output the symbols that are still
9300 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9302 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9303 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9304 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9306 Elf_Internal_Sym sym
;
9307 asection
*input_sec
;
9308 const struct elf_backend_data
*bed
;
9313 if (h
->root
.type
== bfd_link_hash_warning
)
9315 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9316 if (h
->root
.type
== bfd_link_hash_new
)
9320 /* Decide whether to output this symbol in this pass. */
9321 if (eoinfo
->localsyms
)
9323 if (!h
->forced_local
)
9328 if (h
->forced_local
)
9332 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9334 if (h
->root
.type
== bfd_link_hash_undefined
)
9336 /* If we have an undefined symbol reference here then it must have
9337 come from a shared library that is being linked in. (Undefined
9338 references in regular files have already been handled unless
9339 they are in unreferenced sections which are removed by garbage
9341 bfd_boolean ignore_undef
= FALSE
;
9343 /* Some symbols may be special in that the fact that they're
9344 undefined can be safely ignored - let backend determine that. */
9345 if (bed
->elf_backend_ignore_undef_symbol
)
9346 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9348 /* If we are reporting errors for this situation then do so now. */
9351 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9352 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9353 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9354 (*flinfo
->info
->callbacks
->undefined_symbol
)
9355 (flinfo
->info
, h
->root
.root
.string
,
9356 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9358 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9360 /* Strip a global symbol defined in a discarded section. */
9365 /* We should also warn if a forced local symbol is referenced from
9366 shared libraries. */
9367 if (bfd_link_executable (flinfo
->info
)
9372 && h
->ref_dynamic_nonweak
9373 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9377 struct elf_link_hash_entry
*hi
= h
;
9379 /* Check indirect symbol. */
9380 while (hi
->root
.type
== bfd_link_hash_indirect
)
9381 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9383 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9384 /* xgettext:c-format */
9385 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9386 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9387 /* xgettext:c-format */
9388 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9390 /* xgettext:c-format */
9391 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9392 def_bfd
= flinfo
->output_bfd
;
9393 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9394 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9395 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9396 h
->root
.root
.string
);
9397 bfd_set_error (bfd_error_bad_value
);
9398 eoinfo
->failed
= TRUE
;
9402 /* We don't want to output symbols that have never been mentioned by
9403 a regular file, or that we have been told to strip. However, if
9404 h->indx is set to -2, the symbol is used by a reloc and we must
9409 else if ((h
->def_dynamic
9411 || h
->root
.type
== bfd_link_hash_new
)
9415 else if (flinfo
->info
->strip
== strip_all
)
9417 else if (flinfo
->info
->strip
== strip_some
9418 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9419 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9421 else if ((h
->root
.type
== bfd_link_hash_defined
9422 || h
->root
.type
== bfd_link_hash_defweak
)
9423 && ((flinfo
->info
->strip_discarded
9424 && discarded_section (h
->root
.u
.def
.section
))
9425 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9426 && h
->root
.u
.def
.section
->owner
!= NULL
9427 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9429 else if ((h
->root
.type
== bfd_link_hash_undefined
9430 || h
->root
.type
== bfd_link_hash_undefweak
)
9431 && h
->root
.u
.undef
.abfd
!= NULL
9432 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9437 /* If we're stripping it, and it's not a dynamic symbol, there's
9438 nothing else to do. However, if it is a forced local symbol or
9439 an ifunc symbol we need to give the backend finish_dynamic_symbol
9440 function a chance to make it dynamic. */
9443 && type
!= STT_GNU_IFUNC
9444 && !h
->forced_local
)
9448 sym
.st_size
= h
->size
;
9449 sym
.st_other
= h
->other
;
9450 switch (h
->root
.type
)
9453 case bfd_link_hash_new
:
9454 case bfd_link_hash_warning
:
9458 case bfd_link_hash_undefined
:
9459 case bfd_link_hash_undefweak
:
9460 input_sec
= bfd_und_section_ptr
;
9461 sym
.st_shndx
= SHN_UNDEF
;
9464 case bfd_link_hash_defined
:
9465 case bfd_link_hash_defweak
:
9467 input_sec
= h
->root
.u
.def
.section
;
9468 if (input_sec
->output_section
!= NULL
)
9471 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9472 input_sec
->output_section
);
9473 if (sym
.st_shndx
== SHN_BAD
)
9476 /* xgettext:c-format */
9477 (_("%B: could not find output section %A for input section %A"),
9478 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9479 bfd_set_error (bfd_error_nonrepresentable_section
);
9480 eoinfo
->failed
= TRUE
;
9484 /* ELF symbols in relocatable files are section relative,
9485 but in nonrelocatable files they are virtual
9487 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9488 if (!bfd_link_relocatable (flinfo
->info
))
9490 sym
.st_value
+= input_sec
->output_section
->vma
;
9491 if (h
->type
== STT_TLS
)
9493 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9494 if (tls_sec
!= NULL
)
9495 sym
.st_value
-= tls_sec
->vma
;
9501 BFD_ASSERT (input_sec
->owner
== NULL
9502 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9503 sym
.st_shndx
= SHN_UNDEF
;
9504 input_sec
= bfd_und_section_ptr
;
9509 case bfd_link_hash_common
:
9510 input_sec
= h
->root
.u
.c
.p
->section
;
9511 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9512 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9515 case bfd_link_hash_indirect
:
9516 /* These symbols are created by symbol versioning. They point
9517 to the decorated version of the name. For example, if the
9518 symbol foo@@GNU_1.2 is the default, which should be used when
9519 foo is used with no version, then we add an indirect symbol
9520 foo which points to foo@@GNU_1.2. We ignore these symbols,
9521 since the indirected symbol is already in the hash table. */
9525 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9526 switch (h
->root
.type
)
9528 case bfd_link_hash_common
:
9529 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9531 case bfd_link_hash_defined
:
9532 case bfd_link_hash_defweak
:
9533 if (bed
->common_definition (&sym
))
9534 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9538 case bfd_link_hash_undefined
:
9539 case bfd_link_hash_undefweak
:
9545 if (h
->forced_local
)
9547 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9548 /* Turn off visibility on local symbol. */
9549 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9551 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9552 else if (h
->unique_global
&& h
->def_regular
)
9553 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9554 else if (h
->root
.type
== bfd_link_hash_undefweak
9555 || h
->root
.type
== bfd_link_hash_defweak
)
9556 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9558 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9559 sym
.st_target_internal
= h
->target_internal
;
9561 /* Give the processor backend a chance to tweak the symbol value,
9562 and also to finish up anything that needs to be done for this
9563 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9564 forced local syms when non-shared is due to a historical quirk.
9565 STT_GNU_IFUNC symbol must go through PLT. */
9566 if ((h
->type
== STT_GNU_IFUNC
9568 && !bfd_link_relocatable (flinfo
->info
))
9569 || ((h
->dynindx
!= -1
9571 && ((bfd_link_pic (flinfo
->info
)
9572 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9573 || h
->root
.type
!= bfd_link_hash_undefweak
))
9574 || !h
->forced_local
)
9575 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9577 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9578 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9580 eoinfo
->failed
= TRUE
;
9585 /* If we are marking the symbol as undefined, and there are no
9586 non-weak references to this symbol from a regular object, then
9587 mark the symbol as weak undefined; if there are non-weak
9588 references, mark the symbol as strong. We can't do this earlier,
9589 because it might not be marked as undefined until the
9590 finish_dynamic_symbol routine gets through with it. */
9591 if (sym
.st_shndx
== SHN_UNDEF
9593 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9594 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9597 type
= ELF_ST_TYPE (sym
.st_info
);
9599 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9600 if (type
== STT_GNU_IFUNC
)
9603 if (h
->ref_regular_nonweak
)
9604 bindtype
= STB_GLOBAL
;
9606 bindtype
= STB_WEAK
;
9607 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9610 /* If this is a symbol defined in a dynamic library, don't use the
9611 symbol size from the dynamic library. Relinking an executable
9612 against a new library may introduce gratuitous changes in the
9613 executable's symbols if we keep the size. */
9614 if (sym
.st_shndx
== SHN_UNDEF
9619 /* If a non-weak symbol with non-default visibility is not defined
9620 locally, it is a fatal error. */
9621 if (!bfd_link_relocatable (flinfo
->info
)
9622 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9623 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9624 && h
->root
.type
== bfd_link_hash_undefined
9629 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9630 /* xgettext:c-format */
9631 msg
= _("%B: protected symbol `%s' isn't defined");
9632 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9633 /* xgettext:c-format */
9634 msg
= _("%B: internal symbol `%s' isn't defined");
9636 /* xgettext:c-format */
9637 msg
= _("%B: hidden symbol `%s' isn't defined");
9638 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9639 bfd_set_error (bfd_error_bad_value
);
9640 eoinfo
->failed
= TRUE
;
9644 /* If this symbol should be put in the .dynsym section, then put it
9645 there now. We already know the symbol index. We also fill in
9646 the entry in the .hash section. */
9647 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9649 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9653 /* Since there is no version information in the dynamic string,
9654 if there is no version info in symbol version section, we will
9655 have a run-time problem if not linking executable, referenced
9656 by shared library, or not bound locally. */
9657 if (h
->verinfo
.verdef
== NULL
9658 && (!bfd_link_executable (flinfo
->info
)
9660 || !h
->def_regular
))
9662 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9664 if (p
&& p
[1] != '\0')
9667 /* xgettext:c-format */
9668 (_("%B: No symbol version section for versioned symbol `%s'"),
9669 flinfo
->output_bfd
, h
->root
.root
.string
);
9670 eoinfo
->failed
= TRUE
;
9675 sym
.st_name
= h
->dynstr_index
;
9676 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9677 + h
->dynindx
* bed
->s
->sizeof_sym
);
9678 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9680 eoinfo
->failed
= TRUE
;
9683 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9685 if (flinfo
->hash_sec
!= NULL
)
9687 size_t hash_entry_size
;
9688 bfd_byte
*bucketpos
;
9693 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9694 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9697 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9698 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9699 + (bucket
+ 2) * hash_entry_size
);
9700 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9701 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9703 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9704 ((bfd_byte
*) flinfo
->hash_sec
->contents
9705 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9708 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9710 Elf_Internal_Versym iversym
;
9711 Elf_External_Versym
*eversym
;
9713 if (!h
->def_regular
)
9715 if (h
->verinfo
.verdef
== NULL
9716 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9717 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9718 iversym
.vs_vers
= 0;
9720 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9724 if (h
->verinfo
.vertree
== NULL
)
9725 iversym
.vs_vers
= 1;
9727 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9728 if (flinfo
->info
->create_default_symver
)
9732 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9734 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9735 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9737 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9738 eversym
+= h
->dynindx
;
9739 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9743 /* If the symbol is undefined, and we didn't output it to .dynsym,
9744 strip it from .symtab too. Obviously we can't do this for
9745 relocatable output or when needed for --emit-relocs. */
9746 else if (input_sec
== bfd_und_section_ptr
9748 && !bfd_link_relocatable (flinfo
->info
))
9750 /* Also strip others that we couldn't earlier due to dynamic symbol
9754 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9757 /* Output a FILE symbol so that following locals are not associated
9758 with the wrong input file. We need one for forced local symbols
9759 if we've seen more than one FILE symbol or when we have exactly
9760 one FILE symbol but global symbols are present in a file other
9761 than the one with the FILE symbol. We also need one if linker
9762 defined symbols are present. In practice these conditions are
9763 always met, so just emit the FILE symbol unconditionally. */
9764 if (eoinfo
->localsyms
9765 && !eoinfo
->file_sym_done
9766 && eoinfo
->flinfo
->filesym_count
!= 0)
9768 Elf_Internal_Sym fsym
;
9770 memset (&fsym
, 0, sizeof (fsym
));
9771 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9772 fsym
.st_shndx
= SHN_ABS
;
9773 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9774 bfd_und_section_ptr
, NULL
))
9777 eoinfo
->file_sym_done
= TRUE
;
9780 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9781 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9785 eoinfo
->failed
= TRUE
;
9790 else if (h
->indx
== -2)
9796 /* Return TRUE if special handling is done for relocs in SEC against
9797 symbols defined in discarded sections. */
9800 elf_section_ignore_discarded_relocs (asection
*sec
)
9802 const struct elf_backend_data
*bed
;
9804 switch (sec
->sec_info_type
)
9806 case SEC_INFO_TYPE_STABS
:
9807 case SEC_INFO_TYPE_EH_FRAME
:
9808 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9814 bed
= get_elf_backend_data (sec
->owner
);
9815 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9816 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9822 /* Return a mask saying how ld should treat relocations in SEC against
9823 symbols defined in discarded sections. If this function returns
9824 COMPLAIN set, ld will issue a warning message. If this function
9825 returns PRETEND set, and the discarded section was link-once and the
9826 same size as the kept link-once section, ld will pretend that the
9827 symbol was actually defined in the kept section. Otherwise ld will
9828 zero the reloc (at least that is the intent, but some cooperation by
9829 the target dependent code is needed, particularly for REL targets). */
9832 _bfd_elf_default_action_discarded (asection
*sec
)
9834 if (sec
->flags
& SEC_DEBUGGING
)
9837 if (strcmp (".eh_frame", sec
->name
) == 0)
9840 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9843 return COMPLAIN
| PRETEND
;
9846 /* Find a match between a section and a member of a section group. */
9849 match_group_member (asection
*sec
, asection
*group
,
9850 struct bfd_link_info
*info
)
9852 asection
*first
= elf_next_in_group (group
);
9853 asection
*s
= first
;
9857 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9860 s
= elf_next_in_group (s
);
9868 /* Check if the kept section of a discarded section SEC can be used
9869 to replace it. Return the replacement if it is OK. Otherwise return
9873 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9877 kept
= sec
->kept_section
;
9880 if ((kept
->flags
& SEC_GROUP
) != 0)
9881 kept
= match_group_member (sec
, kept
, info
);
9883 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9884 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9886 sec
->kept_section
= kept
;
9891 /* Link an input file into the linker output file. This function
9892 handles all the sections and relocations of the input file at once.
9893 This is so that we only have to read the local symbols once, and
9894 don't have to keep them in memory. */
9897 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9899 int (*relocate_section
)
9900 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9901 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9903 Elf_Internal_Shdr
*symtab_hdr
;
9906 Elf_Internal_Sym
*isymbuf
;
9907 Elf_Internal_Sym
*isym
;
9908 Elf_Internal_Sym
*isymend
;
9910 asection
**ppsection
;
9912 const struct elf_backend_data
*bed
;
9913 struct elf_link_hash_entry
**sym_hashes
;
9914 bfd_size_type address_size
;
9915 bfd_vma r_type_mask
;
9917 bfd_boolean have_file_sym
= FALSE
;
9919 output_bfd
= flinfo
->output_bfd
;
9920 bed
= get_elf_backend_data (output_bfd
);
9921 relocate_section
= bed
->elf_backend_relocate_section
;
9923 /* If this is a dynamic object, we don't want to do anything here:
9924 we don't want the local symbols, and we don't want the section
9926 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9929 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9930 if (elf_bad_symtab (input_bfd
))
9932 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9937 locsymcount
= symtab_hdr
->sh_info
;
9938 extsymoff
= symtab_hdr
->sh_info
;
9941 /* Read the local symbols. */
9942 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9943 if (isymbuf
== NULL
&& locsymcount
!= 0)
9945 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9946 flinfo
->internal_syms
,
9947 flinfo
->external_syms
,
9948 flinfo
->locsym_shndx
);
9949 if (isymbuf
== NULL
)
9953 /* Find local symbol sections and adjust values of symbols in
9954 SEC_MERGE sections. Write out those local symbols we know are
9955 going into the output file. */
9956 isymend
= isymbuf
+ locsymcount
;
9957 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9959 isym
++, pindex
++, ppsection
++)
9963 Elf_Internal_Sym osym
;
9969 if (elf_bad_symtab (input_bfd
))
9971 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9978 if (isym
->st_shndx
== SHN_UNDEF
)
9979 isec
= bfd_und_section_ptr
;
9980 else if (isym
->st_shndx
== SHN_ABS
)
9981 isec
= bfd_abs_section_ptr
;
9982 else if (isym
->st_shndx
== SHN_COMMON
)
9983 isec
= bfd_com_section_ptr
;
9986 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9989 /* Don't attempt to output symbols with st_shnx in the
9990 reserved range other than SHN_ABS and SHN_COMMON. */
9994 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9995 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9997 _bfd_merged_section_offset (output_bfd
, &isec
,
9998 elf_section_data (isec
)->sec_info
,
10004 /* Don't output the first, undefined, symbol. In fact, don't
10005 output any undefined local symbol. */
10006 if (isec
== bfd_und_section_ptr
)
10009 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10011 /* We never output section symbols. Instead, we use the
10012 section symbol of the corresponding section in the output
10017 /* If we are stripping all symbols, we don't want to output this
10019 if (flinfo
->info
->strip
== strip_all
)
10022 /* If we are discarding all local symbols, we don't want to
10023 output this one. If we are generating a relocatable output
10024 file, then some of the local symbols may be required by
10025 relocs; we output them below as we discover that they are
10027 if (flinfo
->info
->discard
== discard_all
)
10030 /* If this symbol is defined in a section which we are
10031 discarding, we don't need to keep it. */
10032 if (isym
->st_shndx
!= SHN_UNDEF
10033 && isym
->st_shndx
< SHN_LORESERVE
10034 && bfd_section_removed_from_list (output_bfd
,
10035 isec
->output_section
))
10038 /* Get the name of the symbol. */
10039 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10044 /* See if we are discarding symbols with this name. */
10045 if ((flinfo
->info
->strip
== strip_some
10046 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10048 || (((flinfo
->info
->discard
== discard_sec_merge
10049 && (isec
->flags
& SEC_MERGE
)
10050 && !bfd_link_relocatable (flinfo
->info
))
10051 || flinfo
->info
->discard
== discard_l
)
10052 && bfd_is_local_label_name (input_bfd
, name
)))
10055 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10057 if (input_bfd
->lto_output
)
10058 /* -flto puts a temp file name here. This means builds
10059 are not reproducible. Discard the symbol. */
10061 have_file_sym
= TRUE
;
10062 flinfo
->filesym_count
+= 1;
10064 if (!have_file_sym
)
10066 /* In the absence of debug info, bfd_find_nearest_line uses
10067 FILE symbols to determine the source file for local
10068 function symbols. Provide a FILE symbol here if input
10069 files lack such, so that their symbols won't be
10070 associated with a previous input file. It's not the
10071 source file, but the best we can do. */
10072 have_file_sym
= TRUE
;
10073 flinfo
->filesym_count
+= 1;
10074 memset (&osym
, 0, sizeof (osym
));
10075 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10076 osym
.st_shndx
= SHN_ABS
;
10077 if (!elf_link_output_symstrtab (flinfo
,
10078 (input_bfd
->lto_output
? NULL
10079 : input_bfd
->filename
),
10080 &osym
, bfd_abs_section_ptr
,
10087 /* Adjust the section index for the output file. */
10088 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10089 isec
->output_section
);
10090 if (osym
.st_shndx
== SHN_BAD
)
10093 /* ELF symbols in relocatable files are section relative, but
10094 in executable files they are virtual addresses. Note that
10095 this code assumes that all ELF sections have an associated
10096 BFD section with a reasonable value for output_offset; below
10097 we assume that they also have a reasonable value for
10098 output_section. Any special sections must be set up to meet
10099 these requirements. */
10100 osym
.st_value
+= isec
->output_offset
;
10101 if (!bfd_link_relocatable (flinfo
->info
))
10103 osym
.st_value
+= isec
->output_section
->vma
;
10104 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10106 /* STT_TLS symbols are relative to PT_TLS segment base. */
10107 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10108 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10112 indx
= bfd_get_symcount (output_bfd
);
10113 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10120 if (bed
->s
->arch_size
== 32)
10122 r_type_mask
= 0xff;
10128 r_type_mask
= 0xffffffff;
10133 /* Relocate the contents of each section. */
10134 sym_hashes
= elf_sym_hashes (input_bfd
);
10135 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10137 bfd_byte
*contents
;
10139 if (! o
->linker_mark
)
10141 /* This section was omitted from the link. */
10145 if (bfd_link_relocatable (flinfo
->info
)
10146 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10148 /* Deal with the group signature symbol. */
10149 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10150 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10151 asection
*osec
= o
->output_section
;
10153 if (symndx
>= locsymcount
10154 || (elf_bad_symtab (input_bfd
)
10155 && flinfo
->sections
[symndx
] == NULL
))
10157 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10158 while (h
->root
.type
== bfd_link_hash_indirect
10159 || h
->root
.type
== bfd_link_hash_warning
)
10160 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10161 /* Arrange for symbol to be output. */
10163 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10165 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10167 /* We'll use the output section target_index. */
10168 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10169 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10173 if (flinfo
->indices
[symndx
] == -1)
10175 /* Otherwise output the local symbol now. */
10176 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10177 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10182 name
= bfd_elf_string_from_elf_section (input_bfd
,
10183 symtab_hdr
->sh_link
,
10188 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10190 if (sym
.st_shndx
== SHN_BAD
)
10193 sym
.st_value
+= o
->output_offset
;
10195 indx
= bfd_get_symcount (output_bfd
);
10196 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10201 flinfo
->indices
[symndx
] = indx
;
10205 elf_section_data (osec
)->this_hdr
.sh_info
10206 = flinfo
->indices
[symndx
];
10210 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10211 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10214 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10216 /* Section was created by _bfd_elf_link_create_dynamic_sections
10221 /* Get the contents of the section. They have been cached by a
10222 relaxation routine. Note that o is a section in an input
10223 file, so the contents field will not have been set by any of
10224 the routines which work on output files. */
10225 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10227 contents
= elf_section_data (o
)->this_hdr
.contents
;
10228 if (bed
->caches_rawsize
10230 && o
->rawsize
< o
->size
)
10232 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10233 contents
= flinfo
->contents
;
10238 contents
= flinfo
->contents
;
10239 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10243 if ((o
->flags
& SEC_RELOC
) != 0)
10245 Elf_Internal_Rela
*internal_relocs
;
10246 Elf_Internal_Rela
*rel
, *relend
;
10247 int action_discarded
;
10250 /* Get the swapped relocs. */
10252 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10253 flinfo
->internal_relocs
, FALSE
);
10254 if (internal_relocs
== NULL
10255 && o
->reloc_count
> 0)
10258 /* We need to reverse-copy input .ctors/.dtors sections if
10259 they are placed in .init_array/.finit_array for output. */
10260 if (o
->size
> address_size
10261 && ((strncmp (o
->name
, ".ctors", 6) == 0
10262 && strcmp (o
->output_section
->name
,
10263 ".init_array") == 0)
10264 || (strncmp (o
->name
, ".dtors", 6) == 0
10265 && strcmp (o
->output_section
->name
,
10266 ".fini_array") == 0))
10267 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10269 if (o
->size
!= o
->reloc_count
* address_size
)
10272 /* xgettext:c-format */
10273 (_("error: %B: size of section %A is not "
10274 "multiple of address size"),
10276 bfd_set_error (bfd_error_on_input
);
10279 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10282 action_discarded
= -1;
10283 if (!elf_section_ignore_discarded_relocs (o
))
10284 action_discarded
= (*bed
->action_discarded
) (o
);
10286 /* Run through the relocs evaluating complex reloc symbols and
10287 looking for relocs against symbols from discarded sections
10288 or section symbols from removed link-once sections.
10289 Complain about relocs against discarded sections. Zero
10290 relocs against removed link-once sections. */
10292 rel
= internal_relocs
;
10293 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10294 for ( ; rel
< relend
; rel
++)
10296 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10297 unsigned int s_type
;
10298 asection
**ps
, *sec
;
10299 struct elf_link_hash_entry
*h
= NULL
;
10300 const char *sym_name
;
10302 if (r_symndx
== STN_UNDEF
)
10305 if (r_symndx
>= locsymcount
10306 || (elf_bad_symtab (input_bfd
)
10307 && flinfo
->sections
[r_symndx
] == NULL
))
10309 h
= sym_hashes
[r_symndx
- extsymoff
];
10311 /* Badly formatted input files can contain relocs that
10312 reference non-existant symbols. Check here so that
10313 we do not seg fault. */
10318 sprintf_vma (buffer
, rel
->r_info
);
10320 /* xgettext:c-format */
10321 (_("error: %B contains a reloc (0x%s) for section %A "
10322 "that references a non-existent global symbol"),
10323 input_bfd
, o
, buffer
);
10324 bfd_set_error (bfd_error_bad_value
);
10328 while (h
->root
.type
== bfd_link_hash_indirect
10329 || h
->root
.type
== bfd_link_hash_warning
)
10330 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10334 /* If a plugin symbol is referenced from a non-IR file,
10335 mark the symbol as undefined. Note that the
10336 linker may attach linker created dynamic sections
10337 to the plugin bfd. Symbols defined in linker
10338 created sections are not plugin symbols. */
10339 if (h
->root
.non_ir_ref
10340 && (h
->root
.type
== bfd_link_hash_defined
10341 || h
->root
.type
== bfd_link_hash_defweak
)
10342 && (h
->root
.u
.def
.section
->flags
10343 & SEC_LINKER_CREATED
) == 0
10344 && h
->root
.u
.def
.section
->owner
!= NULL
10345 && (h
->root
.u
.def
.section
->owner
->flags
10346 & BFD_PLUGIN
) != 0)
10348 h
->root
.type
= bfd_link_hash_undefined
;
10349 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10353 if (h
->root
.type
== bfd_link_hash_defined
10354 || h
->root
.type
== bfd_link_hash_defweak
)
10355 ps
= &h
->root
.u
.def
.section
;
10357 sym_name
= h
->root
.root
.string
;
10361 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10363 s_type
= ELF_ST_TYPE (sym
->st_info
);
10364 ps
= &flinfo
->sections
[r_symndx
];
10365 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10369 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10370 && !bfd_link_relocatable (flinfo
->info
))
10373 bfd_vma dot
= (rel
->r_offset
10374 + o
->output_offset
+ o
->output_section
->vma
);
10376 printf ("Encountered a complex symbol!");
10377 printf (" (input_bfd %s, section %s, reloc %ld\n",
10378 input_bfd
->filename
, o
->name
,
10379 (long) (rel
- internal_relocs
));
10380 printf (" symbol: idx %8.8lx, name %s\n",
10381 r_symndx
, sym_name
);
10382 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10383 (unsigned long) rel
->r_info
,
10384 (unsigned long) rel
->r_offset
);
10386 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10387 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10390 /* Symbol evaluated OK. Update to absolute value. */
10391 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10396 if (action_discarded
!= -1 && ps
!= NULL
)
10398 /* Complain if the definition comes from a
10399 discarded section. */
10400 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10402 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10403 if (action_discarded
& COMPLAIN
)
10404 (*flinfo
->info
->callbacks
->einfo
)
10405 /* xgettext:c-format */
10406 (_("%X`%s' referenced in section `%A' of %B: "
10407 "defined in discarded section `%A' of %B\n"),
10408 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10410 /* Try to do the best we can to support buggy old
10411 versions of gcc. Pretend that the symbol is
10412 really defined in the kept linkonce section.
10413 FIXME: This is quite broken. Modifying the
10414 symbol here means we will be changing all later
10415 uses of the symbol, not just in this section. */
10416 if (action_discarded
& PRETEND
)
10420 kept
= _bfd_elf_check_kept_section (sec
,
10432 /* Relocate the section by invoking a back end routine.
10434 The back end routine is responsible for adjusting the
10435 section contents as necessary, and (if using Rela relocs
10436 and generating a relocatable output file) adjusting the
10437 reloc addend as necessary.
10439 The back end routine does not have to worry about setting
10440 the reloc address or the reloc symbol index.
10442 The back end routine is given a pointer to the swapped in
10443 internal symbols, and can access the hash table entries
10444 for the external symbols via elf_sym_hashes (input_bfd).
10446 When generating relocatable output, the back end routine
10447 must handle STB_LOCAL/STT_SECTION symbols specially. The
10448 output symbol is going to be a section symbol
10449 corresponding to the output section, which will require
10450 the addend to be adjusted. */
10452 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10453 input_bfd
, o
, contents
,
10461 || bfd_link_relocatable (flinfo
->info
)
10462 || flinfo
->info
->emitrelocations
)
10464 Elf_Internal_Rela
*irela
;
10465 Elf_Internal_Rela
*irelaend
, *irelamid
;
10466 bfd_vma last_offset
;
10467 struct elf_link_hash_entry
**rel_hash
;
10468 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10469 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10470 unsigned int next_erel
;
10471 bfd_boolean rela_normal
;
10472 struct bfd_elf_section_data
*esdi
, *esdo
;
10474 esdi
= elf_section_data (o
);
10475 esdo
= elf_section_data (o
->output_section
);
10476 rela_normal
= FALSE
;
10478 /* Adjust the reloc addresses and symbol indices. */
10480 irela
= internal_relocs
;
10481 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10482 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10483 /* We start processing the REL relocs, if any. When we reach
10484 IRELAMID in the loop, we switch to the RELA relocs. */
10486 if (esdi
->rel
.hdr
!= NULL
)
10487 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10488 * bed
->s
->int_rels_per_ext_rel
);
10489 rel_hash_list
= rel_hash
;
10490 rela_hash_list
= NULL
;
10491 last_offset
= o
->output_offset
;
10492 if (!bfd_link_relocatable (flinfo
->info
))
10493 last_offset
+= o
->output_section
->vma
;
10494 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10496 unsigned long r_symndx
;
10498 Elf_Internal_Sym sym
;
10500 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10506 if (irela
== irelamid
)
10508 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10509 rela_hash_list
= rel_hash
;
10510 rela_normal
= bed
->rela_normal
;
10513 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10516 if (irela
->r_offset
>= (bfd_vma
) -2)
10518 /* This is a reloc for a deleted entry or somesuch.
10519 Turn it into an R_*_NONE reloc, at the same
10520 offset as the last reloc. elf_eh_frame.c and
10521 bfd_elf_discard_info rely on reloc offsets
10523 irela
->r_offset
= last_offset
;
10525 irela
->r_addend
= 0;
10529 irela
->r_offset
+= o
->output_offset
;
10531 /* Relocs in an executable have to be virtual addresses. */
10532 if (!bfd_link_relocatable (flinfo
->info
))
10533 irela
->r_offset
+= o
->output_section
->vma
;
10535 last_offset
= irela
->r_offset
;
10537 r_symndx
= irela
->r_info
>> r_sym_shift
;
10538 if (r_symndx
== STN_UNDEF
)
10541 if (r_symndx
>= locsymcount
10542 || (elf_bad_symtab (input_bfd
)
10543 && flinfo
->sections
[r_symndx
] == NULL
))
10545 struct elf_link_hash_entry
*rh
;
10546 unsigned long indx
;
10548 /* This is a reloc against a global symbol. We
10549 have not yet output all the local symbols, so
10550 we do not know the symbol index of any global
10551 symbol. We set the rel_hash entry for this
10552 reloc to point to the global hash table entry
10553 for this symbol. The symbol index is then
10554 set at the end of bfd_elf_final_link. */
10555 indx
= r_symndx
- extsymoff
;
10556 rh
= elf_sym_hashes (input_bfd
)[indx
];
10557 while (rh
->root
.type
== bfd_link_hash_indirect
10558 || rh
->root
.type
== bfd_link_hash_warning
)
10559 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10561 /* Setting the index to -2 tells
10562 elf_link_output_extsym that this symbol is
10563 used by a reloc. */
10564 BFD_ASSERT (rh
->indx
< 0);
10572 /* This is a reloc against a local symbol. */
10575 sym
= isymbuf
[r_symndx
];
10576 sec
= flinfo
->sections
[r_symndx
];
10577 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10579 /* I suppose the backend ought to fill in the
10580 section of any STT_SECTION symbol against a
10581 processor specific section. */
10582 r_symndx
= STN_UNDEF
;
10583 if (bfd_is_abs_section (sec
))
10585 else if (sec
== NULL
|| sec
->owner
== NULL
)
10587 bfd_set_error (bfd_error_bad_value
);
10592 asection
*osec
= sec
->output_section
;
10594 /* If we have discarded a section, the output
10595 section will be the absolute section. In
10596 case of discarded SEC_MERGE sections, use
10597 the kept section. relocate_section should
10598 have already handled discarded linkonce
10600 if (bfd_is_abs_section (osec
)
10601 && sec
->kept_section
!= NULL
10602 && sec
->kept_section
->output_section
!= NULL
)
10604 osec
= sec
->kept_section
->output_section
;
10605 irela
->r_addend
-= osec
->vma
;
10608 if (!bfd_is_abs_section (osec
))
10610 r_symndx
= osec
->target_index
;
10611 if (r_symndx
== STN_UNDEF
)
10613 irela
->r_addend
+= osec
->vma
;
10614 osec
= _bfd_nearby_section (output_bfd
, osec
,
10616 irela
->r_addend
-= osec
->vma
;
10617 r_symndx
= osec
->target_index
;
10622 /* Adjust the addend according to where the
10623 section winds up in the output section. */
10625 irela
->r_addend
+= sec
->output_offset
;
10629 if (flinfo
->indices
[r_symndx
] == -1)
10631 unsigned long shlink
;
10636 if (flinfo
->info
->strip
== strip_all
)
10638 /* You can't do ld -r -s. */
10639 bfd_set_error (bfd_error_invalid_operation
);
10643 /* This symbol was skipped earlier, but
10644 since it is needed by a reloc, we
10645 must output it now. */
10646 shlink
= symtab_hdr
->sh_link
;
10647 name
= (bfd_elf_string_from_elf_section
10648 (input_bfd
, shlink
, sym
.st_name
));
10652 osec
= sec
->output_section
;
10654 _bfd_elf_section_from_bfd_section (output_bfd
,
10656 if (sym
.st_shndx
== SHN_BAD
)
10659 sym
.st_value
+= sec
->output_offset
;
10660 if (!bfd_link_relocatable (flinfo
->info
))
10662 sym
.st_value
+= osec
->vma
;
10663 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10665 /* STT_TLS symbols are relative to PT_TLS
10667 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10668 ->tls_sec
!= NULL
);
10669 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10674 indx
= bfd_get_symcount (output_bfd
);
10675 ret
= elf_link_output_symstrtab (flinfo
, name
,
10681 flinfo
->indices
[r_symndx
] = indx
;
10686 r_symndx
= flinfo
->indices
[r_symndx
];
10689 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10690 | (irela
->r_info
& r_type_mask
));
10693 /* Swap out the relocs. */
10694 input_rel_hdr
= esdi
->rel
.hdr
;
10695 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10697 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10702 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10703 * bed
->s
->int_rels_per_ext_rel
);
10704 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10707 input_rela_hdr
= esdi
->rela
.hdr
;
10708 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10710 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10719 /* Write out the modified section contents. */
10720 if (bed
->elf_backend_write_section
10721 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10724 /* Section written out. */
10726 else switch (o
->sec_info_type
)
10728 case SEC_INFO_TYPE_STABS
:
10729 if (! (_bfd_write_section_stabs
10731 &elf_hash_table (flinfo
->info
)->stab_info
,
10732 o
, &elf_section_data (o
)->sec_info
, contents
)))
10735 case SEC_INFO_TYPE_MERGE
:
10736 if (! _bfd_write_merged_section (output_bfd
, o
,
10737 elf_section_data (o
)->sec_info
))
10740 case SEC_INFO_TYPE_EH_FRAME
:
10742 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10747 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10749 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10757 if (! (o
->flags
& SEC_EXCLUDE
))
10759 file_ptr offset
= (file_ptr
) o
->output_offset
;
10760 bfd_size_type todo
= o
->size
;
10762 offset
*= bfd_octets_per_byte (output_bfd
);
10764 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10766 /* Reverse-copy input section to output. */
10769 todo
-= address_size
;
10770 if (! bfd_set_section_contents (output_bfd
,
10778 offset
+= address_size
;
10782 else if (! bfd_set_section_contents (output_bfd
,
10796 /* Generate a reloc when linking an ELF file. This is a reloc
10797 requested by the linker, and does not come from any input file. This
10798 is used to build constructor and destructor tables when linking
10802 elf_reloc_link_order (bfd
*output_bfd
,
10803 struct bfd_link_info
*info
,
10804 asection
*output_section
,
10805 struct bfd_link_order
*link_order
)
10807 reloc_howto_type
*howto
;
10811 struct bfd_elf_section_reloc_data
*reldata
;
10812 struct elf_link_hash_entry
**rel_hash_ptr
;
10813 Elf_Internal_Shdr
*rel_hdr
;
10814 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10815 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10818 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10820 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10823 bfd_set_error (bfd_error_bad_value
);
10827 addend
= link_order
->u
.reloc
.p
->addend
;
10830 reldata
= &esdo
->rel
;
10831 else if (esdo
->rela
.hdr
)
10832 reldata
= &esdo
->rela
;
10839 /* Figure out the symbol index. */
10840 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10841 if (link_order
->type
== bfd_section_reloc_link_order
)
10843 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10844 BFD_ASSERT (indx
!= 0);
10845 *rel_hash_ptr
= NULL
;
10849 struct elf_link_hash_entry
*h
;
10851 /* Treat a reloc against a defined symbol as though it were
10852 actually against the section. */
10853 h
= ((struct elf_link_hash_entry
*)
10854 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10855 link_order
->u
.reloc
.p
->u
.name
,
10856 FALSE
, FALSE
, TRUE
));
10858 && (h
->root
.type
== bfd_link_hash_defined
10859 || h
->root
.type
== bfd_link_hash_defweak
))
10863 section
= h
->root
.u
.def
.section
;
10864 indx
= section
->output_section
->target_index
;
10865 *rel_hash_ptr
= NULL
;
10866 /* It seems that we ought to add the symbol value to the
10867 addend here, but in practice it has already been added
10868 because it was passed to constructor_callback. */
10869 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10871 else if (h
!= NULL
)
10873 /* Setting the index to -2 tells elf_link_output_extsym that
10874 this symbol is used by a reloc. */
10881 (*info
->callbacks
->unattached_reloc
)
10882 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10887 /* If this is an inplace reloc, we must write the addend into the
10889 if (howto
->partial_inplace
&& addend
!= 0)
10891 bfd_size_type size
;
10892 bfd_reloc_status_type rstat
;
10895 const char *sym_name
;
10897 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10898 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10899 if (buf
== NULL
&& size
!= 0)
10901 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10908 case bfd_reloc_outofrange
:
10911 case bfd_reloc_overflow
:
10912 if (link_order
->type
== bfd_section_reloc_link_order
)
10913 sym_name
= bfd_section_name (output_bfd
,
10914 link_order
->u
.reloc
.p
->u
.section
);
10916 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10917 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10918 howto
->name
, addend
, NULL
, NULL
,
10923 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10925 * bfd_octets_per_byte (output_bfd
),
10932 /* The address of a reloc is relative to the section in a
10933 relocatable file, and is a virtual address in an executable
10935 offset
= link_order
->offset
;
10936 if (! bfd_link_relocatable (info
))
10937 offset
+= output_section
->vma
;
10939 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10941 irel
[i
].r_offset
= offset
;
10942 irel
[i
].r_info
= 0;
10943 irel
[i
].r_addend
= 0;
10945 if (bed
->s
->arch_size
== 32)
10946 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10948 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10950 rel_hdr
= reldata
->hdr
;
10951 erel
= rel_hdr
->contents
;
10952 if (rel_hdr
->sh_type
== SHT_REL
)
10954 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10955 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10959 irel
[0].r_addend
= addend
;
10960 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10961 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10970 /* Get the output vma of the section pointed to by the sh_link field. */
10973 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10975 Elf_Internal_Shdr
**elf_shdrp
;
10979 s
= p
->u
.indirect
.section
;
10980 elf_shdrp
= elf_elfsections (s
->owner
);
10981 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10982 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10984 The Intel C compiler generates SHT_IA_64_UNWIND with
10985 SHF_LINK_ORDER. But it doesn't set the sh_link or
10986 sh_info fields. Hence we could get the situation
10987 where elfsec is 0. */
10990 const struct elf_backend_data
*bed
10991 = get_elf_backend_data (s
->owner
);
10992 if (bed
->link_order_error_handler
)
10993 bed
->link_order_error_handler
10994 /* xgettext:c-format */
10995 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
11000 s
= elf_shdrp
[elfsec
]->bfd_section
;
11001 return s
->output_section
->vma
+ s
->output_offset
;
11006 /* Compare two sections based on the locations of the sections they are
11007 linked to. Used by elf_fixup_link_order. */
11010 compare_link_order (const void * a
, const void * b
)
11015 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11016 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11019 return apos
> bpos
;
11023 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11024 order as their linked sections. Returns false if this could not be done
11025 because an output section includes both ordered and unordered
11026 sections. Ideally we'd do this in the linker proper. */
11029 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11031 int seen_linkorder
;
11034 struct bfd_link_order
*p
;
11036 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11038 struct bfd_link_order
**sections
;
11039 asection
*s
, *other_sec
, *linkorder_sec
;
11043 linkorder_sec
= NULL
;
11045 seen_linkorder
= 0;
11046 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11048 if (p
->type
== bfd_indirect_link_order
)
11050 s
= p
->u
.indirect
.section
;
11052 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11053 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11054 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11055 && elfsec
< elf_numsections (sub
)
11056 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11057 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11071 if (seen_other
&& seen_linkorder
)
11073 if (other_sec
&& linkorder_sec
)
11075 /* xgettext:c-format */
11076 (_("%A has both ordered [`%A' in %B] "
11077 "and unordered [`%A' in %B] sections"),
11079 linkorder_sec
->owner
, other_sec
,
11083 (_("%A has both ordered and unordered sections"), o
);
11084 bfd_set_error (bfd_error_bad_value
);
11089 if (!seen_linkorder
)
11092 sections
= (struct bfd_link_order
**)
11093 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11094 if (sections
== NULL
)
11096 seen_linkorder
= 0;
11098 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11100 sections
[seen_linkorder
++] = p
;
11102 /* Sort the input sections in the order of their linked section. */
11103 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11104 compare_link_order
);
11106 /* Change the offsets of the sections. */
11108 for (n
= 0; n
< seen_linkorder
; n
++)
11110 s
= sections
[n
]->u
.indirect
.section
;
11111 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11112 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11113 sections
[n
]->offset
= offset
;
11114 offset
+= sections
[n
]->size
;
11121 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11122 Returns TRUE upon success, FALSE otherwise. */
11125 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11127 bfd_boolean ret
= FALSE
;
11129 const struct elf_backend_data
*bed
;
11131 enum bfd_architecture arch
;
11133 asymbol
**sympp
= NULL
;
11137 elf_symbol_type
*osymbuf
;
11139 implib_bfd
= info
->out_implib_bfd
;
11140 bed
= get_elf_backend_data (abfd
);
11142 if (!bfd_set_format (implib_bfd
, bfd_object
))
11145 flags
= bfd_get_file_flags (abfd
);
11146 flags
&= ~HAS_RELOC
;
11147 if (!bfd_set_start_address (implib_bfd
, 0)
11148 || !bfd_set_file_flags (implib_bfd
, flags
))
11151 /* Copy architecture of output file to import library file. */
11152 arch
= bfd_get_arch (abfd
);
11153 mach
= bfd_get_mach (abfd
);
11154 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11155 && (abfd
->target_defaulted
11156 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11159 /* Get symbol table size. */
11160 symsize
= bfd_get_symtab_upper_bound (abfd
);
11164 /* Read in the symbol table. */
11165 sympp
= (asymbol
**) xmalloc (symsize
);
11166 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11170 /* Allow the BFD backend to copy any private header data it
11171 understands from the output BFD to the import library BFD. */
11172 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11175 /* Filter symbols to appear in the import library. */
11176 if (bed
->elf_backend_filter_implib_symbols
)
11177 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11180 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11183 bfd_set_error (bfd_error_no_symbols
);
11184 _bfd_error_handler (_("%B: no symbol found for import library"),
11190 /* Make symbols absolute. */
11191 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11192 sizeof (*osymbuf
));
11193 for (src_count
= 0; src_count
< symcount
; src_count
++)
11195 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11196 sizeof (*osymbuf
));
11197 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11198 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11199 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11200 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11201 osymbuf
[src_count
].symbol
.value
;
11202 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11205 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11207 /* Allow the BFD backend to copy any private data it understands
11208 from the output BFD to the import library BFD. This is done last
11209 to permit the routine to look at the filtered symbol table. */
11210 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11213 if (!bfd_close (implib_bfd
))
11224 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11228 if (flinfo
->symstrtab
!= NULL
)
11229 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11230 if (flinfo
->contents
!= NULL
)
11231 free (flinfo
->contents
);
11232 if (flinfo
->external_relocs
!= NULL
)
11233 free (flinfo
->external_relocs
);
11234 if (flinfo
->internal_relocs
!= NULL
)
11235 free (flinfo
->internal_relocs
);
11236 if (flinfo
->external_syms
!= NULL
)
11237 free (flinfo
->external_syms
);
11238 if (flinfo
->locsym_shndx
!= NULL
)
11239 free (flinfo
->locsym_shndx
);
11240 if (flinfo
->internal_syms
!= NULL
)
11241 free (flinfo
->internal_syms
);
11242 if (flinfo
->indices
!= NULL
)
11243 free (flinfo
->indices
);
11244 if (flinfo
->sections
!= NULL
)
11245 free (flinfo
->sections
);
11246 if (flinfo
->symshndxbuf
!= NULL
)
11247 free (flinfo
->symshndxbuf
);
11248 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11250 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11251 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11252 free (esdo
->rel
.hashes
);
11253 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11254 free (esdo
->rela
.hashes
);
11258 /* Do the final step of an ELF link. */
11261 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11263 bfd_boolean dynamic
;
11264 bfd_boolean emit_relocs
;
11266 struct elf_final_link_info flinfo
;
11268 struct bfd_link_order
*p
;
11270 bfd_size_type max_contents_size
;
11271 bfd_size_type max_external_reloc_size
;
11272 bfd_size_type max_internal_reloc_count
;
11273 bfd_size_type max_sym_count
;
11274 bfd_size_type max_sym_shndx_count
;
11275 Elf_Internal_Sym elfsym
;
11277 Elf_Internal_Shdr
*symtab_hdr
;
11278 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11279 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11280 struct elf_outext_info eoinfo
;
11281 bfd_boolean merged
;
11282 size_t relativecount
= 0;
11283 asection
*reldyn
= 0;
11285 asection
*attr_section
= NULL
;
11286 bfd_vma attr_size
= 0;
11287 const char *std_attrs_section
;
11288 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11290 if (!is_elf_hash_table (htab
))
11293 if (bfd_link_pic (info
))
11294 abfd
->flags
|= DYNAMIC
;
11296 dynamic
= htab
->dynamic_sections_created
;
11297 dynobj
= htab
->dynobj
;
11299 emit_relocs
= (bfd_link_relocatable (info
)
11300 || info
->emitrelocations
);
11302 flinfo
.info
= info
;
11303 flinfo
.output_bfd
= abfd
;
11304 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11305 if (flinfo
.symstrtab
== NULL
)
11310 flinfo
.hash_sec
= NULL
;
11311 flinfo
.symver_sec
= NULL
;
11315 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11316 /* Note that dynsym_sec can be NULL (on VMS). */
11317 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11318 /* Note that it is OK if symver_sec is NULL. */
11321 flinfo
.contents
= NULL
;
11322 flinfo
.external_relocs
= NULL
;
11323 flinfo
.internal_relocs
= NULL
;
11324 flinfo
.external_syms
= NULL
;
11325 flinfo
.locsym_shndx
= NULL
;
11326 flinfo
.internal_syms
= NULL
;
11327 flinfo
.indices
= NULL
;
11328 flinfo
.sections
= NULL
;
11329 flinfo
.symshndxbuf
= NULL
;
11330 flinfo
.filesym_count
= 0;
11332 /* The object attributes have been merged. Remove the input
11333 sections from the link, and set the contents of the output
11335 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11336 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11338 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11339 || strcmp (o
->name
, ".gnu.attributes") == 0)
11341 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11343 asection
*input_section
;
11345 if (p
->type
!= bfd_indirect_link_order
)
11347 input_section
= p
->u
.indirect
.section
;
11348 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11349 elf_link_input_bfd ignores this section. */
11350 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11353 attr_size
= bfd_elf_obj_attr_size (abfd
);
11356 bfd_set_section_size (abfd
, o
, attr_size
);
11358 /* Skip this section later on. */
11359 o
->map_head
.link_order
= NULL
;
11362 o
->flags
|= SEC_EXCLUDE
;
11366 /* Count up the number of relocations we will output for each output
11367 section, so that we know the sizes of the reloc sections. We
11368 also figure out some maximum sizes. */
11369 max_contents_size
= 0;
11370 max_external_reloc_size
= 0;
11371 max_internal_reloc_count
= 0;
11373 max_sym_shndx_count
= 0;
11375 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11377 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11378 o
->reloc_count
= 0;
11380 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11382 unsigned int reloc_count
= 0;
11383 unsigned int additional_reloc_count
= 0;
11384 struct bfd_elf_section_data
*esdi
= NULL
;
11386 if (p
->type
== bfd_section_reloc_link_order
11387 || p
->type
== bfd_symbol_reloc_link_order
)
11389 else if (p
->type
== bfd_indirect_link_order
)
11393 sec
= p
->u
.indirect
.section
;
11395 /* Mark all sections which are to be included in the
11396 link. This will normally be every section. We need
11397 to do this so that we can identify any sections which
11398 the linker has decided to not include. */
11399 sec
->linker_mark
= TRUE
;
11401 if (sec
->flags
& SEC_MERGE
)
11404 if (sec
->rawsize
> max_contents_size
)
11405 max_contents_size
= sec
->rawsize
;
11406 if (sec
->size
> max_contents_size
)
11407 max_contents_size
= sec
->size
;
11409 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11410 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11414 /* We are interested in just local symbols, not all
11416 if (elf_bad_symtab (sec
->owner
))
11417 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11418 / bed
->s
->sizeof_sym
);
11420 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11422 if (sym_count
> max_sym_count
)
11423 max_sym_count
= sym_count
;
11425 if (sym_count
> max_sym_shndx_count
11426 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11427 max_sym_shndx_count
= sym_count
;
11429 if (esdo
->this_hdr
.sh_type
== SHT_REL
11430 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11431 /* Some backends use reloc_count in relocation sections
11432 to count particular types of relocs. Of course,
11433 reloc sections themselves can't have relocations. */
11435 else if (emit_relocs
)
11437 reloc_count
= sec
->reloc_count
;
11438 if (bed
->elf_backend_count_additional_relocs
)
11441 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11442 additional_reloc_count
+= c
;
11445 else if (bed
->elf_backend_count_relocs
)
11446 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11448 esdi
= elf_section_data (sec
);
11450 if ((sec
->flags
& SEC_RELOC
) != 0)
11452 size_t ext_size
= 0;
11454 if (esdi
->rel
.hdr
!= NULL
)
11455 ext_size
= esdi
->rel
.hdr
->sh_size
;
11456 if (esdi
->rela
.hdr
!= NULL
)
11457 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11459 if (ext_size
> max_external_reloc_size
)
11460 max_external_reloc_size
= ext_size
;
11461 if (sec
->reloc_count
> max_internal_reloc_count
)
11462 max_internal_reloc_count
= sec
->reloc_count
;
11467 if (reloc_count
== 0)
11470 reloc_count
+= additional_reloc_count
;
11471 o
->reloc_count
+= reloc_count
;
11473 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11477 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11478 esdo
->rel
.count
+= additional_reloc_count
;
11480 if (esdi
->rela
.hdr
)
11482 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11483 esdo
->rela
.count
+= additional_reloc_count
;
11489 esdo
->rela
.count
+= reloc_count
;
11491 esdo
->rel
.count
+= reloc_count
;
11495 if (o
->reloc_count
> 0)
11496 o
->flags
|= SEC_RELOC
;
11499 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11500 set it (this is probably a bug) and if it is set
11501 assign_section_numbers will create a reloc section. */
11502 o
->flags
&=~ SEC_RELOC
;
11505 /* If the SEC_ALLOC flag is not set, force the section VMA to
11506 zero. This is done in elf_fake_sections as well, but forcing
11507 the VMA to 0 here will ensure that relocs against these
11508 sections are handled correctly. */
11509 if ((o
->flags
& SEC_ALLOC
) == 0
11510 && ! o
->user_set_vma
)
11514 if (! bfd_link_relocatable (info
) && merged
)
11515 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11517 /* Figure out the file positions for everything but the symbol table
11518 and the relocs. We set symcount to force assign_section_numbers
11519 to create a symbol table. */
11520 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11521 BFD_ASSERT (! abfd
->output_has_begun
);
11522 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11525 /* Set sizes, and assign file positions for reloc sections. */
11526 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11528 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11529 if ((o
->flags
& SEC_RELOC
) != 0)
11532 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11536 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11540 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11541 to count upwards while actually outputting the relocations. */
11542 esdo
->rel
.count
= 0;
11543 esdo
->rela
.count
= 0;
11545 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11547 /* Cache the section contents so that they can be compressed
11548 later. Use bfd_malloc since it will be freed by
11549 bfd_compress_section_contents. */
11550 unsigned char *contents
= esdo
->this_hdr
.contents
;
11551 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11554 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11555 if (contents
== NULL
)
11557 esdo
->this_hdr
.contents
= contents
;
11561 /* We have now assigned file positions for all the sections except
11562 .symtab, .strtab, and non-loaded reloc sections. We start the
11563 .symtab section at the current file position, and write directly
11564 to it. We build the .strtab section in memory. */
11565 bfd_get_symcount (abfd
) = 0;
11566 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11567 /* sh_name is set in prep_headers. */
11568 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11569 /* sh_flags, sh_addr and sh_size all start off zero. */
11570 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11571 /* sh_link is set in assign_section_numbers. */
11572 /* sh_info is set below. */
11573 /* sh_offset is set just below. */
11574 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11576 if (max_sym_count
< 20)
11577 max_sym_count
= 20;
11578 htab
->strtabsize
= max_sym_count
;
11579 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11580 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11581 if (htab
->strtab
== NULL
)
11583 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11585 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11586 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11588 if (info
->strip
!= strip_all
|| emit_relocs
)
11590 file_ptr off
= elf_next_file_pos (abfd
);
11592 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11594 /* Note that at this point elf_next_file_pos (abfd) is
11595 incorrect. We do not yet know the size of the .symtab section.
11596 We correct next_file_pos below, after we do know the size. */
11598 /* Start writing out the symbol table. The first symbol is always a
11600 elfsym
.st_value
= 0;
11601 elfsym
.st_size
= 0;
11602 elfsym
.st_info
= 0;
11603 elfsym
.st_other
= 0;
11604 elfsym
.st_shndx
= SHN_UNDEF
;
11605 elfsym
.st_target_internal
= 0;
11606 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11607 bfd_und_section_ptr
, NULL
) != 1)
11610 /* Output a symbol for each section. We output these even if we are
11611 discarding local symbols, since they are used for relocs. These
11612 symbols have no names. We store the index of each one in the
11613 index field of the section, so that we can find it again when
11614 outputting relocs. */
11616 elfsym
.st_size
= 0;
11617 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11618 elfsym
.st_other
= 0;
11619 elfsym
.st_value
= 0;
11620 elfsym
.st_target_internal
= 0;
11621 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11623 o
= bfd_section_from_elf_index (abfd
, i
);
11626 o
->target_index
= bfd_get_symcount (abfd
);
11627 elfsym
.st_shndx
= i
;
11628 if (!bfd_link_relocatable (info
))
11629 elfsym
.st_value
= o
->vma
;
11630 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11637 /* Allocate some memory to hold information read in from the input
11639 if (max_contents_size
!= 0)
11641 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11642 if (flinfo
.contents
== NULL
)
11646 if (max_external_reloc_size
!= 0)
11648 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11649 if (flinfo
.external_relocs
== NULL
)
11653 if (max_internal_reloc_count
!= 0)
11655 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11656 amt
*= sizeof (Elf_Internal_Rela
);
11657 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11658 if (flinfo
.internal_relocs
== NULL
)
11662 if (max_sym_count
!= 0)
11664 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11665 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11666 if (flinfo
.external_syms
== NULL
)
11669 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11670 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11671 if (flinfo
.internal_syms
== NULL
)
11674 amt
= max_sym_count
* sizeof (long);
11675 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11676 if (flinfo
.indices
== NULL
)
11679 amt
= max_sym_count
* sizeof (asection
*);
11680 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11681 if (flinfo
.sections
== NULL
)
11685 if (max_sym_shndx_count
!= 0)
11687 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11688 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11689 if (flinfo
.locsym_shndx
== NULL
)
11695 bfd_vma base
, end
= 0;
11698 for (sec
= htab
->tls_sec
;
11699 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11702 bfd_size_type size
= sec
->size
;
11705 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11707 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11710 size
= ord
->offset
+ ord
->size
;
11712 end
= sec
->vma
+ size
;
11714 base
= htab
->tls_sec
->vma
;
11715 /* Only align end of TLS section if static TLS doesn't have special
11716 alignment requirements. */
11717 if (bed
->static_tls_alignment
== 1)
11718 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11719 htab
->tls_size
= end
- base
;
11722 /* Reorder SHF_LINK_ORDER sections. */
11723 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11725 if (!elf_fixup_link_order (abfd
, o
))
11729 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11732 /* Since ELF permits relocations to be against local symbols, we
11733 must have the local symbols available when we do the relocations.
11734 Since we would rather only read the local symbols once, and we
11735 would rather not keep them in memory, we handle all the
11736 relocations for a single input file at the same time.
11738 Unfortunately, there is no way to know the total number of local
11739 symbols until we have seen all of them, and the local symbol
11740 indices precede the global symbol indices. This means that when
11741 we are generating relocatable output, and we see a reloc against
11742 a global symbol, we can not know the symbol index until we have
11743 finished examining all the local symbols to see which ones we are
11744 going to output. To deal with this, we keep the relocations in
11745 memory, and don't output them until the end of the link. This is
11746 an unfortunate waste of memory, but I don't see a good way around
11747 it. Fortunately, it only happens when performing a relocatable
11748 link, which is not the common case. FIXME: If keep_memory is set
11749 we could write the relocs out and then read them again; I don't
11750 know how bad the memory loss will be. */
11752 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11753 sub
->output_has_begun
= FALSE
;
11754 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11756 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11758 if (p
->type
== bfd_indirect_link_order
11759 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11760 == bfd_target_elf_flavour
)
11761 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11763 if (! sub
->output_has_begun
)
11765 if (! elf_link_input_bfd (&flinfo
, sub
))
11767 sub
->output_has_begun
= TRUE
;
11770 else if (p
->type
== bfd_section_reloc_link_order
11771 || p
->type
== bfd_symbol_reloc_link_order
)
11773 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11778 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11780 if (p
->type
== bfd_indirect_link_order
11781 && (bfd_get_flavour (sub
)
11782 == bfd_target_elf_flavour
)
11783 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11784 != bed
->s
->elfclass
))
11786 const char *iclass
, *oclass
;
11788 switch (bed
->s
->elfclass
)
11790 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11791 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11792 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11796 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11798 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11799 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11800 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11804 bfd_set_error (bfd_error_wrong_format
);
11806 /* xgettext:c-format */
11807 (_("%B: file class %s incompatible with %s"),
11808 sub
, iclass
, oclass
);
11817 /* Free symbol buffer if needed. */
11818 if (!info
->reduce_memory_overheads
)
11820 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11821 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11822 && elf_tdata (sub
)->symbuf
)
11824 free (elf_tdata (sub
)->symbuf
);
11825 elf_tdata (sub
)->symbuf
= NULL
;
11829 /* Output any global symbols that got converted to local in a
11830 version script or due to symbol visibility. We do this in a
11831 separate step since ELF requires all local symbols to appear
11832 prior to any global symbols. FIXME: We should only do this if
11833 some global symbols were, in fact, converted to become local.
11834 FIXME: Will this work correctly with the Irix 5 linker? */
11835 eoinfo
.failed
= FALSE
;
11836 eoinfo
.flinfo
= &flinfo
;
11837 eoinfo
.localsyms
= TRUE
;
11838 eoinfo
.file_sym_done
= FALSE
;
11839 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11843 /* If backend needs to output some local symbols not present in the hash
11844 table, do it now. */
11845 if (bed
->elf_backend_output_arch_local_syms
11846 && (info
->strip
!= strip_all
|| emit_relocs
))
11848 typedef int (*out_sym_func
)
11849 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11850 struct elf_link_hash_entry
*);
11852 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11853 (abfd
, info
, &flinfo
,
11854 (out_sym_func
) elf_link_output_symstrtab
)))
11858 /* That wrote out all the local symbols. Finish up the symbol table
11859 with the global symbols. Even if we want to strip everything we
11860 can, we still need to deal with those global symbols that got
11861 converted to local in a version script. */
11863 /* The sh_info field records the index of the first non local symbol. */
11864 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11867 && htab
->dynsym
!= NULL
11868 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11870 Elf_Internal_Sym sym
;
11871 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11873 o
= htab
->dynsym
->output_section
;
11874 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11876 /* Write out the section symbols for the output sections. */
11877 if (bfd_link_pic (info
)
11878 || htab
->is_relocatable_executable
)
11884 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11886 sym
.st_target_internal
= 0;
11888 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11894 dynindx
= elf_section_data (s
)->dynindx
;
11897 indx
= elf_section_data (s
)->this_idx
;
11898 BFD_ASSERT (indx
> 0);
11899 sym
.st_shndx
= indx
;
11900 if (! check_dynsym (abfd
, &sym
))
11902 sym
.st_value
= s
->vma
;
11903 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11904 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11908 /* Write out the local dynsyms. */
11909 if (htab
->dynlocal
)
11911 struct elf_link_local_dynamic_entry
*e
;
11912 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11917 /* Copy the internal symbol and turn off visibility.
11918 Note that we saved a word of storage and overwrote
11919 the original st_name with the dynstr_index. */
11921 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11923 s
= bfd_section_from_elf_index (e
->input_bfd
,
11928 elf_section_data (s
->output_section
)->this_idx
;
11929 if (! check_dynsym (abfd
, &sym
))
11931 sym
.st_value
= (s
->output_section
->vma
11933 + e
->isym
.st_value
);
11936 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11937 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11942 /* We get the global symbols from the hash table. */
11943 eoinfo
.failed
= FALSE
;
11944 eoinfo
.localsyms
= FALSE
;
11945 eoinfo
.flinfo
= &flinfo
;
11946 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11950 /* If backend needs to output some symbols not present in the hash
11951 table, do it now. */
11952 if (bed
->elf_backend_output_arch_syms
11953 && (info
->strip
!= strip_all
|| emit_relocs
))
11955 typedef int (*out_sym_func
)
11956 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11957 struct elf_link_hash_entry
*);
11959 if (! ((*bed
->elf_backend_output_arch_syms
)
11960 (abfd
, info
, &flinfo
,
11961 (out_sym_func
) elf_link_output_symstrtab
)))
11965 /* Finalize the .strtab section. */
11966 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11968 /* Swap out the .strtab section. */
11969 if (!elf_link_swap_symbols_out (&flinfo
))
11972 /* Now we know the size of the symtab section. */
11973 if (bfd_get_symcount (abfd
) > 0)
11975 /* Finish up and write out the symbol string table (.strtab)
11977 Elf_Internal_Shdr
*symstrtab_hdr
;
11978 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11980 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11981 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11983 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11984 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11985 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11986 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11987 symtab_shndx_hdr
->sh_size
= amt
;
11989 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11992 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11993 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11997 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11998 /* sh_name was set in prep_headers. */
11999 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12000 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12001 symstrtab_hdr
->sh_addr
= 0;
12002 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12003 symstrtab_hdr
->sh_entsize
= 0;
12004 symstrtab_hdr
->sh_link
= 0;
12005 symstrtab_hdr
->sh_info
= 0;
12006 /* sh_offset is set just below. */
12007 symstrtab_hdr
->sh_addralign
= 1;
12009 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12011 elf_next_file_pos (abfd
) = off
;
12013 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12014 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12018 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12020 _bfd_error_handler (_("%B: failed to generate import library"),
12021 info
->out_implib_bfd
);
12025 /* Adjust the relocs to have the correct symbol indices. */
12026 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12028 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12030 if ((o
->flags
& SEC_RELOC
) == 0)
12033 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12034 if (esdo
->rel
.hdr
!= NULL
12035 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
12037 if (esdo
->rela
.hdr
!= NULL
12038 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
12041 /* Set the reloc_count field to 0 to prevent write_relocs from
12042 trying to swap the relocs out itself. */
12043 o
->reloc_count
= 0;
12046 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12047 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12049 /* If we are linking against a dynamic object, or generating a
12050 shared library, finish up the dynamic linking information. */
12053 bfd_byte
*dyncon
, *dynconend
;
12055 /* Fix up .dynamic entries. */
12056 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12057 BFD_ASSERT (o
!= NULL
);
12059 dyncon
= o
->contents
;
12060 dynconend
= o
->contents
+ o
->size
;
12061 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12063 Elf_Internal_Dyn dyn
;
12067 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12074 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12076 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12078 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12079 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12082 dyn
.d_un
.d_val
= relativecount
;
12089 name
= info
->init_function
;
12092 name
= info
->fini_function
;
12095 struct elf_link_hash_entry
*h
;
12097 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12099 && (h
->root
.type
== bfd_link_hash_defined
12100 || h
->root
.type
== bfd_link_hash_defweak
))
12102 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12103 o
= h
->root
.u
.def
.section
;
12104 if (o
->output_section
!= NULL
)
12105 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12106 + o
->output_offset
);
12109 /* The symbol is imported from another shared
12110 library and does not apply to this one. */
12111 dyn
.d_un
.d_ptr
= 0;
12118 case DT_PREINIT_ARRAYSZ
:
12119 name
= ".preinit_array";
12121 case DT_INIT_ARRAYSZ
:
12122 name
= ".init_array";
12124 case DT_FINI_ARRAYSZ
:
12125 name
= ".fini_array";
12127 o
= bfd_get_section_by_name (abfd
, name
);
12131 (_("could not find section %s"), name
);
12136 (_("warning: %s section has zero size"), name
);
12137 dyn
.d_un
.d_val
= o
->size
;
12140 case DT_PREINIT_ARRAY
:
12141 name
= ".preinit_array";
12143 case DT_INIT_ARRAY
:
12144 name
= ".init_array";
12146 case DT_FINI_ARRAY
:
12147 name
= ".fini_array";
12149 o
= bfd_get_section_by_name (abfd
, name
);
12156 name
= ".gnu.hash";
12165 name
= ".gnu.version_d";
12168 name
= ".gnu.version_r";
12171 name
= ".gnu.version";
12173 o
= bfd_get_linker_section (dynobj
, name
);
12178 (_("could not find section %s"), name
);
12181 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12184 (_("warning: section '%s' is being made into a note"), name
);
12185 bfd_set_error (bfd_error_nonrepresentable_section
);
12188 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12195 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12199 dyn
.d_un
.d_val
= 0;
12200 dyn
.d_un
.d_ptr
= 0;
12201 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12203 Elf_Internal_Shdr
*hdr
;
12205 hdr
= elf_elfsections (abfd
)[i
];
12206 if (hdr
->sh_type
== type
12207 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12209 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12210 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12213 if (dyn
.d_un
.d_ptr
== 0
12214 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12215 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12219 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12221 /* Don't count procedure linkage table relocs in the
12222 overall reloc count. */
12223 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12224 dyn
.d_un
.d_val
-= htab
->srelplt
->size
;
12225 /* If .rela.plt is the first .rela section, exclude
12226 it from DT_RELA. */
12227 else if (dyn
.d_un
.d_ptr
== (htab
->srelplt
->output_section
->vma
12228 + htab
->srelplt
->output_offset
))
12229 dyn
.d_un
.d_ptr
+= htab
->srelplt
->size
;
12233 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12237 /* If we have created any dynamic sections, then output them. */
12238 if (dynobj
!= NULL
)
12240 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12243 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12244 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12245 || info
->error_textrel
)
12246 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12248 bfd_byte
*dyncon
, *dynconend
;
12250 dyncon
= o
->contents
;
12251 dynconend
= o
->contents
+ o
->size
;
12252 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12254 Elf_Internal_Dyn dyn
;
12256 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12258 if (dyn
.d_tag
== DT_TEXTREL
)
12260 if (info
->error_textrel
)
12261 info
->callbacks
->einfo
12262 (_("%P%X: read-only segment has dynamic relocations.\n"));
12264 info
->callbacks
->einfo
12265 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12271 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12273 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12275 || o
->output_section
== bfd_abs_section_ptr
)
12277 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12279 /* At this point, we are only interested in sections
12280 created by _bfd_elf_link_create_dynamic_sections. */
12283 if (htab
->stab_info
.stabstr
== o
)
12285 if (htab
->eh_info
.hdr_sec
== o
)
12287 if (strcmp (o
->name
, ".dynstr") != 0)
12289 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12291 (file_ptr
) o
->output_offset
12292 * bfd_octets_per_byte (abfd
),
12298 /* The contents of the .dynstr section are actually in a
12302 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12303 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12304 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12310 if (bfd_link_relocatable (info
))
12312 bfd_boolean failed
= FALSE
;
12314 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12319 /* If we have optimized stabs strings, output them. */
12320 if (htab
->stab_info
.stabstr
!= NULL
)
12322 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12326 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12329 elf_final_link_free (abfd
, &flinfo
);
12331 elf_linker (abfd
) = TRUE
;
12335 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12336 if (contents
== NULL
)
12337 return FALSE
; /* Bail out and fail. */
12338 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12339 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12346 elf_final_link_free (abfd
, &flinfo
);
12350 /* Initialize COOKIE for input bfd ABFD. */
12353 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12354 struct bfd_link_info
*info
, bfd
*abfd
)
12356 Elf_Internal_Shdr
*symtab_hdr
;
12357 const struct elf_backend_data
*bed
;
12359 bed
= get_elf_backend_data (abfd
);
12360 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12362 cookie
->abfd
= abfd
;
12363 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12364 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12365 if (cookie
->bad_symtab
)
12367 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12368 cookie
->extsymoff
= 0;
12372 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12373 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12376 if (bed
->s
->arch_size
== 32)
12377 cookie
->r_sym_shift
= 8;
12379 cookie
->r_sym_shift
= 32;
12381 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12382 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12384 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12385 cookie
->locsymcount
, 0,
12387 if (cookie
->locsyms
== NULL
)
12389 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12392 if (info
->keep_memory
)
12393 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12398 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12401 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12403 Elf_Internal_Shdr
*symtab_hdr
;
12405 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12406 if (cookie
->locsyms
!= NULL
12407 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12408 free (cookie
->locsyms
);
12411 /* Initialize the relocation information in COOKIE for input section SEC
12412 of input bfd ABFD. */
12415 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12416 struct bfd_link_info
*info
, bfd
*abfd
,
12419 const struct elf_backend_data
*bed
;
12421 if (sec
->reloc_count
== 0)
12423 cookie
->rels
= NULL
;
12424 cookie
->relend
= NULL
;
12428 bed
= get_elf_backend_data (abfd
);
12430 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12431 info
->keep_memory
);
12432 if (cookie
->rels
== NULL
)
12434 cookie
->rel
= cookie
->rels
;
12435 cookie
->relend
= (cookie
->rels
12436 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12438 cookie
->rel
= cookie
->rels
;
12442 /* Free the memory allocated by init_reloc_cookie_rels,
12446 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12449 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12450 free (cookie
->rels
);
12453 /* Initialize the whole of COOKIE for input section SEC. */
12456 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12457 struct bfd_link_info
*info
,
12460 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12462 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12467 fini_reloc_cookie (cookie
, sec
->owner
);
12472 /* Free the memory allocated by init_reloc_cookie_for_section,
12476 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12479 fini_reloc_cookie_rels (cookie
, sec
);
12480 fini_reloc_cookie (cookie
, sec
->owner
);
12483 /* Garbage collect unused sections. */
12485 /* Default gc_mark_hook. */
12488 _bfd_elf_gc_mark_hook (asection
*sec
,
12489 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12490 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12491 struct elf_link_hash_entry
*h
,
12492 Elf_Internal_Sym
*sym
)
12496 switch (h
->root
.type
)
12498 case bfd_link_hash_defined
:
12499 case bfd_link_hash_defweak
:
12500 return h
->root
.u
.def
.section
;
12502 case bfd_link_hash_common
:
12503 return h
->root
.u
.c
.p
->section
;
12510 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12515 /* For undefined __start_<name> and __stop_<name> symbols, return the
12516 first input section matching <name>. Return NULL otherwise. */
12519 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12520 struct elf_link_hash_entry
*h
)
12523 const char *sec_name
;
12525 if (h
->root
.type
!= bfd_link_hash_undefined
12526 && h
->root
.type
!= bfd_link_hash_undefweak
)
12529 s
= h
->root
.u
.undef
.section
;
12532 if (s
== (asection
*) 0 - 1)
12538 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12539 sec_name
= h
->root
.root
.string
+ 8;
12540 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12541 sec_name
= h
->root
.root
.string
+ 7;
12543 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12547 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12549 s
= bfd_get_section_by_name (i
, sec_name
);
12552 h
->root
.u
.undef
.section
= s
;
12559 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12564 /* COOKIE->rel describes a relocation against section SEC, which is
12565 a section we've decided to keep. Return the section that contains
12566 the relocation symbol, or NULL if no section contains it. */
12569 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12570 elf_gc_mark_hook_fn gc_mark_hook
,
12571 struct elf_reloc_cookie
*cookie
,
12572 bfd_boolean
*start_stop
)
12574 unsigned long r_symndx
;
12575 struct elf_link_hash_entry
*h
;
12577 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12578 if (r_symndx
== STN_UNDEF
)
12581 if (r_symndx
>= cookie
->locsymcount
12582 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12584 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12587 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12591 while (h
->root
.type
== bfd_link_hash_indirect
12592 || h
->root
.type
== bfd_link_hash_warning
)
12593 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12595 /* If this symbol is weak and there is a non-weak definition, we
12596 keep the non-weak definition because many backends put
12597 dynamic reloc info on the non-weak definition for code
12598 handling copy relocs. */
12599 if (h
->u
.weakdef
!= NULL
)
12600 h
->u
.weakdef
->mark
= 1;
12602 if (start_stop
!= NULL
)
12604 /* To work around a glibc bug, mark all XXX input sections
12605 when there is an as yet undefined reference to __start_XXX
12606 or __stop_XXX symbols. The linker will later define such
12607 symbols for orphan input sections that have a name
12608 representable as a C identifier. */
12609 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12613 *start_stop
= !s
->gc_mark
;
12618 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12621 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12622 &cookie
->locsyms
[r_symndx
]);
12625 /* COOKIE->rel describes a relocation against section SEC, which is
12626 a section we've decided to keep. Mark the section that contains
12627 the relocation symbol. */
12630 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12632 elf_gc_mark_hook_fn gc_mark_hook
,
12633 struct elf_reloc_cookie
*cookie
)
12636 bfd_boolean start_stop
= FALSE
;
12638 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12639 while (rsec
!= NULL
)
12641 if (!rsec
->gc_mark
)
12643 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12644 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12646 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12651 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12656 /* The mark phase of garbage collection. For a given section, mark
12657 it and any sections in this section's group, and all the sections
12658 which define symbols to which it refers. */
12661 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12663 elf_gc_mark_hook_fn gc_mark_hook
)
12666 asection
*group_sec
, *eh_frame
;
12670 /* Mark all the sections in the group. */
12671 group_sec
= elf_section_data (sec
)->next_in_group
;
12672 if (group_sec
&& !group_sec
->gc_mark
)
12673 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12676 /* Look through the section relocs. */
12678 eh_frame
= elf_eh_frame_section (sec
->owner
);
12679 if ((sec
->flags
& SEC_RELOC
) != 0
12680 && sec
->reloc_count
> 0
12681 && sec
!= eh_frame
)
12683 struct elf_reloc_cookie cookie
;
12685 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12689 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12690 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12695 fini_reloc_cookie_for_section (&cookie
, sec
);
12699 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12701 struct elf_reloc_cookie cookie
;
12703 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12707 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12708 gc_mark_hook
, &cookie
))
12710 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12714 eh_frame
= elf_section_eh_frame_entry (sec
);
12715 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12716 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12722 /* Scan and mark sections in a special or debug section group. */
12725 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12727 /* Point to first section of section group. */
12729 /* Used to iterate the section group. */
12732 bfd_boolean is_special_grp
= TRUE
;
12733 bfd_boolean is_debug_grp
= TRUE
;
12735 /* First scan to see if group contains any section other than debug
12736 and special section. */
12737 ssec
= msec
= elf_next_in_group (grp
);
12740 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12741 is_debug_grp
= FALSE
;
12743 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12744 is_special_grp
= FALSE
;
12746 msec
= elf_next_in_group (msec
);
12748 while (msec
!= ssec
);
12750 /* If this is a pure debug section group or pure special section group,
12751 keep all sections in this group. */
12752 if (is_debug_grp
|| is_special_grp
)
12757 msec
= elf_next_in_group (msec
);
12759 while (msec
!= ssec
);
12763 /* Keep debug and special sections. */
12766 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12767 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12771 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12774 bfd_boolean some_kept
;
12775 bfd_boolean debug_frag_seen
;
12777 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12780 /* Ensure all linker created sections are kept,
12781 see if any other section is already marked,
12782 and note if we have any fragmented debug sections. */
12783 debug_frag_seen
= some_kept
= FALSE
;
12784 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12786 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12788 else if (isec
->gc_mark
)
12791 if (debug_frag_seen
== FALSE
12792 && (isec
->flags
& SEC_DEBUGGING
)
12793 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12794 debug_frag_seen
= TRUE
;
12797 /* If no section in this file will be kept, then we can
12798 toss out the debug and special sections. */
12802 /* Keep debug and special sections like .comment when they are
12803 not part of a group. Also keep section groups that contain
12804 just debug sections or special sections. */
12805 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12807 if ((isec
->flags
& SEC_GROUP
) != 0)
12808 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12809 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12810 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12811 && elf_next_in_group (isec
) == NULL
)
12815 if (! debug_frag_seen
)
12818 /* Look for CODE sections which are going to be discarded,
12819 and find and discard any fragmented debug sections which
12820 are associated with that code section. */
12821 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12822 if ((isec
->flags
& SEC_CODE
) != 0
12823 && isec
->gc_mark
== 0)
12828 ilen
= strlen (isec
->name
);
12830 /* Association is determined by the name of the debug section
12831 containing the name of the code section as a suffix. For
12832 example .debug_line.text.foo is a debug section associated
12834 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12838 if (dsec
->gc_mark
== 0
12839 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12842 dlen
= strlen (dsec
->name
);
12845 && strncmp (dsec
->name
+ (dlen
- ilen
),
12846 isec
->name
, ilen
) == 0)
12856 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12858 struct elf_gc_sweep_symbol_info
12860 struct bfd_link_info
*info
;
12861 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12866 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12869 && (((h
->root
.type
== bfd_link_hash_defined
12870 || h
->root
.type
== bfd_link_hash_defweak
)
12871 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12872 && h
->root
.u
.def
.section
->gc_mark
))
12873 || h
->root
.type
== bfd_link_hash_undefined
12874 || h
->root
.type
== bfd_link_hash_undefweak
))
12876 struct elf_gc_sweep_symbol_info
*inf
;
12878 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12879 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12880 h
->def_regular
= 0;
12881 h
->ref_regular
= 0;
12882 h
->ref_regular_nonweak
= 0;
12888 /* The sweep phase of garbage collection. Remove all garbage sections. */
12890 typedef bfd_boolean (*gc_sweep_hook_fn
)
12891 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12894 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12897 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12898 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12899 unsigned long section_sym_count
;
12900 struct elf_gc_sweep_symbol_info sweep_info
;
12902 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12906 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12907 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12910 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12912 /* When any section in a section group is kept, we keep all
12913 sections in the section group. If the first member of
12914 the section group is excluded, we will also exclude the
12916 if (o
->flags
& SEC_GROUP
)
12918 asection
*first
= elf_next_in_group (o
);
12919 o
->gc_mark
= first
->gc_mark
;
12925 /* Skip sweeping sections already excluded. */
12926 if (o
->flags
& SEC_EXCLUDE
)
12929 /* Since this is early in the link process, it is simple
12930 to remove a section from the output. */
12931 o
->flags
|= SEC_EXCLUDE
;
12933 if (info
->print_gc_sections
&& o
->size
!= 0)
12934 /* xgettext:c-format */
12935 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12937 /* But we also have to update some of the relocation
12938 info we collected before. */
12940 && (o
->flags
& SEC_RELOC
) != 0
12941 && o
->reloc_count
!= 0
12942 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12943 && (o
->flags
& SEC_DEBUGGING
) != 0)
12944 && !bfd_is_abs_section (o
->output_section
))
12946 Elf_Internal_Rela
*internal_relocs
;
12950 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12951 info
->keep_memory
);
12952 if (internal_relocs
== NULL
)
12955 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12957 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12958 free (internal_relocs
);
12966 /* Remove the symbols that were in the swept sections from the dynamic
12967 symbol table. GCFIXME: Anyone know how to get them out of the
12968 static symbol table as well? */
12969 sweep_info
.info
= info
;
12970 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12971 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12974 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12978 /* Propagate collected vtable information. This is called through
12979 elf_link_hash_traverse. */
12982 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12984 /* Those that are not vtables. */
12985 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12988 /* Those vtables that do not have parents, we cannot merge. */
12989 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12992 /* If we've already been done, exit. */
12993 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12996 /* Make sure the parent's table is up to date. */
12997 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12999 if (h
->vtable
->used
== NULL
)
13001 /* None of this table's entries were referenced. Re-use the
13003 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
13004 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
13009 bfd_boolean
*cu
, *pu
;
13011 /* Or the parent's entries into ours. */
13012 cu
= h
->vtable
->used
;
13014 pu
= h
->vtable
->parent
->vtable
->used
;
13017 const struct elf_backend_data
*bed
;
13018 unsigned int log_file_align
;
13020 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13021 log_file_align
= bed
->s
->log_file_align
;
13022 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
13037 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13040 bfd_vma hstart
, hend
;
13041 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13042 const struct elf_backend_data
*bed
;
13043 unsigned int log_file_align
;
13045 /* Take care of both those symbols that do not describe vtables as
13046 well as those that are not loaded. */
13047 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13050 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13051 || h
->root
.type
== bfd_link_hash_defweak
);
13053 sec
= h
->root
.u
.def
.section
;
13054 hstart
= h
->root
.u
.def
.value
;
13055 hend
= hstart
+ h
->size
;
13057 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13059 return *(bfd_boolean
*) okp
= FALSE
;
13060 bed
= get_elf_backend_data (sec
->owner
);
13061 log_file_align
= bed
->s
->log_file_align
;
13063 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13065 for (rel
= relstart
; rel
< relend
; ++rel
)
13066 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13068 /* If the entry is in use, do nothing. */
13069 if (h
->vtable
->used
13070 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13072 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13073 if (h
->vtable
->used
[entry
])
13076 /* Otherwise, kill it. */
13077 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13083 /* Mark sections containing dynamically referenced symbols. When
13084 building shared libraries, we must assume that any visible symbol is
13088 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13090 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13091 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13093 if ((h
->root
.type
== bfd_link_hash_defined
13094 || h
->root
.type
== bfd_link_hash_defweak
)
13096 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13097 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13098 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13099 && (!bfd_link_executable (info
)
13100 || info
->gc_keep_exported
13101 || info
->export_dynamic
13104 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13105 && (h
->versioned
>= versioned
13106 || !bfd_hide_sym_by_version (info
->version_info
,
13107 h
->root
.root
.string
)))))
13108 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13113 /* Keep all sections containing symbols undefined on the command-line,
13114 and the section containing the entry symbol. */
13117 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13119 struct bfd_sym_chain
*sym
;
13121 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13123 struct elf_link_hash_entry
*h
;
13125 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13126 FALSE
, FALSE
, FALSE
);
13129 && (h
->root
.type
== bfd_link_hash_defined
13130 || h
->root
.type
== bfd_link_hash_defweak
)
13131 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13132 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13133 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13138 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13139 struct bfd_link_info
*info
)
13141 bfd
*ibfd
= info
->input_bfds
;
13143 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13146 struct elf_reloc_cookie cookie
;
13148 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13151 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13154 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13156 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13157 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13159 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13160 fini_reloc_cookie_rels (&cookie
, sec
);
13167 /* Do mark and sweep of unused sections. */
13170 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13172 bfd_boolean ok
= TRUE
;
13174 elf_gc_mark_hook_fn gc_mark_hook
;
13175 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13176 struct elf_link_hash_table
*htab
;
13178 if (!bed
->can_gc_sections
13179 || !is_elf_hash_table (info
->hash
))
13181 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13185 bed
->gc_keep (info
);
13186 htab
= elf_hash_table (info
);
13188 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13189 at the .eh_frame section if we can mark the FDEs individually. */
13190 for (sub
= info
->input_bfds
;
13191 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13192 sub
= sub
->link
.next
)
13195 struct elf_reloc_cookie cookie
;
13197 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13198 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13200 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13201 if (elf_section_data (sec
)->sec_info
13202 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13203 elf_eh_frame_section (sub
) = sec
;
13204 fini_reloc_cookie_for_section (&cookie
, sec
);
13205 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13209 /* Apply transitive closure to the vtable entry usage info. */
13210 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13214 /* Kill the vtable relocations that were not used. */
13215 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13219 /* Mark dynamically referenced symbols. */
13220 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13221 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13223 /* Grovel through relocs to find out who stays ... */
13224 gc_mark_hook
= bed
->gc_mark_hook
;
13225 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13229 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13230 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13233 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13234 Also treat note sections as a root, if the section is not part
13236 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13238 && (o
->flags
& SEC_EXCLUDE
) == 0
13239 && ((o
->flags
& SEC_KEEP
) != 0
13240 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13241 && elf_next_in_group (o
) == NULL
)))
13243 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13248 /* Allow the backend to mark additional target specific sections. */
13249 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13251 /* ... and mark SEC_EXCLUDE for those that go. */
13252 return elf_gc_sweep (abfd
, info
);
13255 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13258 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13260 struct elf_link_hash_entry
*h
,
13263 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13264 struct elf_link_hash_entry
**search
, *child
;
13265 size_t extsymcount
;
13266 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13268 /* The sh_info field of the symtab header tells us where the
13269 external symbols start. We don't care about the local symbols at
13271 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13272 if (!elf_bad_symtab (abfd
))
13273 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13275 sym_hashes
= elf_sym_hashes (abfd
);
13276 sym_hashes_end
= sym_hashes
+ extsymcount
;
13278 /* Hunt down the child symbol, which is in this section at the same
13279 offset as the relocation. */
13280 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13282 if ((child
= *search
) != NULL
13283 && (child
->root
.type
== bfd_link_hash_defined
13284 || child
->root
.type
== bfd_link_hash_defweak
)
13285 && child
->root
.u
.def
.section
== sec
13286 && child
->root
.u
.def
.value
== offset
)
13290 /* xgettext:c-format */
13291 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13292 abfd
, sec
, (unsigned long) offset
);
13293 bfd_set_error (bfd_error_invalid_operation
);
13297 if (!child
->vtable
)
13299 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13300 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13301 if (!child
->vtable
)
13306 /* This *should* only be the absolute section. It could potentially
13307 be that someone has defined a non-global vtable though, which
13308 would be bad. It isn't worth paging in the local symbols to be
13309 sure though; that case should simply be handled by the assembler. */
13311 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13314 child
->vtable
->parent
= h
;
13319 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13322 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13323 asection
*sec ATTRIBUTE_UNUSED
,
13324 struct elf_link_hash_entry
*h
,
13327 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13328 unsigned int log_file_align
= bed
->s
->log_file_align
;
13332 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13333 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13338 if (addend
>= h
->vtable
->size
)
13340 size_t size
, bytes
, file_align
;
13341 bfd_boolean
*ptr
= h
->vtable
->used
;
13343 /* While the symbol is undefined, we have to be prepared to handle
13345 file_align
= 1 << log_file_align
;
13346 if (h
->root
.type
== bfd_link_hash_undefined
)
13347 size
= addend
+ file_align
;
13351 if (addend
>= size
)
13353 /* Oops! We've got a reference past the defined end of
13354 the table. This is probably a bug -- shall we warn? */
13355 size
= addend
+ file_align
;
13358 size
= (size
+ file_align
- 1) & -file_align
;
13360 /* Allocate one extra entry for use as a "done" flag for the
13361 consolidation pass. */
13362 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13366 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13372 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13373 * sizeof (bfd_boolean
));
13374 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13378 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13383 /* And arrange for that done flag to be at index -1. */
13384 h
->vtable
->used
= ptr
+ 1;
13385 h
->vtable
->size
= size
;
13388 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13393 /* Map an ELF section header flag to its corresponding string. */
13397 flagword flag_value
;
13398 } elf_flags_to_name_table
;
13400 static elf_flags_to_name_table elf_flags_to_names
[] =
13402 { "SHF_WRITE", SHF_WRITE
},
13403 { "SHF_ALLOC", SHF_ALLOC
},
13404 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13405 { "SHF_MERGE", SHF_MERGE
},
13406 { "SHF_STRINGS", SHF_STRINGS
},
13407 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13408 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13409 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13410 { "SHF_GROUP", SHF_GROUP
},
13411 { "SHF_TLS", SHF_TLS
},
13412 { "SHF_MASKOS", SHF_MASKOS
},
13413 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13416 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13418 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13419 struct flag_info
*flaginfo
,
13422 const bfd_vma sh_flags
= elf_section_flags (section
);
13424 if (!flaginfo
->flags_initialized
)
13426 bfd
*obfd
= info
->output_bfd
;
13427 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13428 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13430 int without_hex
= 0;
13432 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13435 flagword (*lookup
) (char *);
13437 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13438 if (lookup
!= NULL
)
13440 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13444 if (tf
->with
== with_flags
)
13445 with_hex
|= hexval
;
13446 else if (tf
->with
== without_flags
)
13447 without_hex
|= hexval
;
13452 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13454 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13456 if (tf
->with
== with_flags
)
13457 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13458 else if (tf
->with
== without_flags
)
13459 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13466 info
->callbacks
->einfo
13467 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13471 flaginfo
->flags_initialized
= TRUE
;
13472 flaginfo
->only_with_flags
|= with_hex
;
13473 flaginfo
->not_with_flags
|= without_hex
;
13476 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13479 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13485 struct alloc_got_off_arg
{
13487 struct bfd_link_info
*info
;
13490 /* We need a special top-level link routine to convert got reference counts
13491 to real got offsets. */
13494 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13496 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13497 bfd
*obfd
= gofarg
->info
->output_bfd
;
13498 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13500 if (h
->got
.refcount
> 0)
13502 h
->got
.offset
= gofarg
->gotoff
;
13503 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13506 h
->got
.offset
= (bfd_vma
) -1;
13511 /* And an accompanying bit to work out final got entry offsets once
13512 we're done. Should be called from final_link. */
13515 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13516 struct bfd_link_info
*info
)
13519 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13521 struct alloc_got_off_arg gofarg
;
13523 BFD_ASSERT (abfd
== info
->output_bfd
);
13525 if (! is_elf_hash_table (info
->hash
))
13528 /* The GOT offset is relative to the .got section, but the GOT header is
13529 put into the .got.plt section, if the backend uses it. */
13530 if (bed
->want_got_plt
)
13533 gotoff
= bed
->got_header_size
;
13535 /* Do the local .got entries first. */
13536 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13538 bfd_signed_vma
*local_got
;
13539 size_t j
, locsymcount
;
13540 Elf_Internal_Shdr
*symtab_hdr
;
13542 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13545 local_got
= elf_local_got_refcounts (i
);
13549 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13550 if (elf_bad_symtab (i
))
13551 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13553 locsymcount
= symtab_hdr
->sh_info
;
13555 for (j
= 0; j
< locsymcount
; ++j
)
13557 if (local_got
[j
] > 0)
13559 local_got
[j
] = gotoff
;
13560 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13563 local_got
[j
] = (bfd_vma
) -1;
13567 /* Then the global .got entries. .plt refcounts are handled by
13568 adjust_dynamic_symbol */
13569 gofarg
.gotoff
= gotoff
;
13570 gofarg
.info
= info
;
13571 elf_link_hash_traverse (elf_hash_table (info
),
13572 elf_gc_allocate_got_offsets
,
13577 /* Many folk need no more in the way of final link than this, once
13578 got entry reference counting is enabled. */
13581 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13583 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13586 /* Invoke the regular ELF backend linker to do all the work. */
13587 return bfd_elf_final_link (abfd
, info
);
13591 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13593 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13595 if (rcookie
->bad_symtab
)
13596 rcookie
->rel
= rcookie
->rels
;
13598 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13600 unsigned long r_symndx
;
13602 if (! rcookie
->bad_symtab
)
13603 if (rcookie
->rel
->r_offset
> offset
)
13605 if (rcookie
->rel
->r_offset
!= offset
)
13608 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13609 if (r_symndx
== STN_UNDEF
)
13612 if (r_symndx
>= rcookie
->locsymcount
13613 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13615 struct elf_link_hash_entry
*h
;
13617 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13619 while (h
->root
.type
== bfd_link_hash_indirect
13620 || h
->root
.type
== bfd_link_hash_warning
)
13621 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13623 if ((h
->root
.type
== bfd_link_hash_defined
13624 || h
->root
.type
== bfd_link_hash_defweak
)
13625 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13626 || h
->root
.u
.def
.section
->kept_section
!= NULL
13627 || discarded_section (h
->root
.u
.def
.section
)))
13632 /* It's not a relocation against a global symbol,
13633 but it could be a relocation against a local
13634 symbol for a discarded section. */
13636 Elf_Internal_Sym
*isym
;
13638 /* Need to: get the symbol; get the section. */
13639 isym
= &rcookie
->locsyms
[r_symndx
];
13640 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13642 && (isec
->kept_section
!= NULL
13643 || discarded_section (isec
)))
13651 /* Discard unneeded references to discarded sections.
13652 Returns -1 on error, 1 if any section's size was changed, 0 if
13653 nothing changed. This function assumes that the relocations are in
13654 sorted order, which is true for all known assemblers. */
13657 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13659 struct elf_reloc_cookie cookie
;
13664 if (info
->traditional_format
13665 || !is_elf_hash_table (info
->hash
))
13668 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13673 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13676 || i
->reloc_count
== 0
13677 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13681 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13684 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13687 if (_bfd_discard_section_stabs (abfd
, i
,
13688 elf_section_data (i
)->sec_info
,
13689 bfd_elf_reloc_symbol_deleted_p
,
13693 fini_reloc_cookie_for_section (&cookie
, i
);
13698 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13699 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13704 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13710 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13713 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13716 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13717 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13718 bfd_elf_reloc_symbol_deleted_p
,
13722 fini_reloc_cookie_for_section (&cookie
, i
);
13726 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13728 const struct elf_backend_data
*bed
;
13730 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13733 bed
= get_elf_backend_data (abfd
);
13735 if (bed
->elf_backend_discard_info
!= NULL
)
13737 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13740 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13743 fini_reloc_cookie (&cookie
, abfd
);
13747 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13748 _bfd_elf_end_eh_frame_parsing (info
);
13750 if (info
->eh_frame_hdr_type
13751 && !bfd_link_relocatable (info
)
13752 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13759 _bfd_elf_section_already_linked (bfd
*abfd
,
13761 struct bfd_link_info
*info
)
13764 const char *name
, *key
;
13765 struct bfd_section_already_linked
*l
;
13766 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13768 if (sec
->output_section
== bfd_abs_section_ptr
)
13771 flags
= sec
->flags
;
13773 /* Return if it isn't a linkonce section. A comdat group section
13774 also has SEC_LINK_ONCE set. */
13775 if ((flags
& SEC_LINK_ONCE
) == 0)
13778 /* Don't put group member sections on our list of already linked
13779 sections. They are handled as a group via their group section. */
13780 if (elf_sec_group (sec
) != NULL
)
13783 /* For a SHT_GROUP section, use the group signature as the key. */
13785 if ((flags
& SEC_GROUP
) != 0
13786 && elf_next_in_group (sec
) != NULL
13787 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13788 key
= elf_group_name (elf_next_in_group (sec
));
13791 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13792 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13793 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13796 /* Must be a user linkonce section that doesn't follow gcc's
13797 naming convention. In this case we won't be matching
13798 single member groups. */
13802 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13804 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13806 /* We may have 2 different types of sections on the list: group
13807 sections with a signature of <key> (<key> is some string),
13808 and linkonce sections named .gnu.linkonce.<type>.<key>.
13809 Match like sections. LTO plugin sections are an exception.
13810 They are always named .gnu.linkonce.t.<key> and match either
13811 type of section. */
13812 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13813 && ((flags
& SEC_GROUP
) != 0
13814 || strcmp (name
, l
->sec
->name
) == 0))
13815 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13817 /* The section has already been linked. See if we should
13818 issue a warning. */
13819 if (!_bfd_handle_already_linked (sec
, l
, info
))
13822 if (flags
& SEC_GROUP
)
13824 asection
*first
= elf_next_in_group (sec
);
13825 asection
*s
= first
;
13829 s
->output_section
= bfd_abs_section_ptr
;
13830 /* Record which group discards it. */
13831 s
->kept_section
= l
->sec
;
13832 s
= elf_next_in_group (s
);
13833 /* These lists are circular. */
13843 /* A single member comdat group section may be discarded by a
13844 linkonce section and vice versa. */
13845 if ((flags
& SEC_GROUP
) != 0)
13847 asection
*first
= elf_next_in_group (sec
);
13849 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13850 /* Check this single member group against linkonce sections. */
13851 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13852 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13853 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13855 first
->output_section
= bfd_abs_section_ptr
;
13856 first
->kept_section
= l
->sec
;
13857 sec
->output_section
= bfd_abs_section_ptr
;
13862 /* Check this linkonce section against single member groups. */
13863 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13864 if (l
->sec
->flags
& SEC_GROUP
)
13866 asection
*first
= elf_next_in_group (l
->sec
);
13869 && elf_next_in_group (first
) == first
13870 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13872 sec
->output_section
= bfd_abs_section_ptr
;
13873 sec
->kept_section
= first
;
13878 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13879 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13880 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13881 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13882 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13883 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13884 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13885 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13886 The reverse order cannot happen as there is never a bfd with only the
13887 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13888 matter as here were are looking only for cross-bfd sections. */
13890 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13891 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13892 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13893 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13895 if (abfd
!= l
->sec
->owner
)
13896 sec
->output_section
= bfd_abs_section_ptr
;
13900 /* This is the first section with this name. Record it. */
13901 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13902 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13903 return sec
->output_section
== bfd_abs_section_ptr
;
13907 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13909 return sym
->st_shndx
== SHN_COMMON
;
13913 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13919 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13921 return bfd_com_section_ptr
;
13925 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13926 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13927 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13928 bfd
*ibfd ATTRIBUTE_UNUSED
,
13929 unsigned long symndx ATTRIBUTE_UNUSED
)
13931 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13932 return bed
->s
->arch_size
/ 8;
13935 /* Routines to support the creation of dynamic relocs. */
13937 /* Returns the name of the dynamic reloc section associated with SEC. */
13939 static const char *
13940 get_dynamic_reloc_section_name (bfd
* abfd
,
13942 bfd_boolean is_rela
)
13945 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13946 const char *prefix
= is_rela
? ".rela" : ".rel";
13948 if (old_name
== NULL
)
13951 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13952 sprintf (name
, "%s%s", prefix
, old_name
);
13957 /* Returns the dynamic reloc section associated with SEC.
13958 If necessary compute the name of the dynamic reloc section based
13959 on SEC's name (looked up in ABFD's string table) and the setting
13963 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13965 bfd_boolean is_rela
)
13967 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13969 if (reloc_sec
== NULL
)
13971 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13975 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13977 if (reloc_sec
!= NULL
)
13978 elf_section_data (sec
)->sreloc
= reloc_sec
;
13985 /* Returns the dynamic reloc section associated with SEC. If the
13986 section does not exist it is created and attached to the DYNOBJ
13987 bfd and stored in the SRELOC field of SEC's elf_section_data
13990 ALIGNMENT is the alignment for the newly created section and
13991 IS_RELA defines whether the name should be .rela.<SEC's name>
13992 or .rel.<SEC's name>. The section name is looked up in the
13993 string table associated with ABFD. */
13996 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13998 unsigned int alignment
,
14000 bfd_boolean is_rela
)
14002 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14004 if (reloc_sec
== NULL
)
14006 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14011 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14013 if (reloc_sec
== NULL
)
14015 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14016 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14017 if ((sec
->flags
& SEC_ALLOC
) != 0)
14018 flags
|= SEC_ALLOC
| SEC_LOAD
;
14020 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14021 if (reloc_sec
!= NULL
)
14023 /* _bfd_elf_get_sec_type_attr chooses a section type by
14024 name. Override as it may be wrong, eg. for a user
14025 section named "auto" we'll get ".relauto" which is
14026 seen to be a .rela section. */
14027 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14028 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14033 elf_section_data (sec
)->sreloc
= reloc_sec
;
14039 /* Copy the ELF symbol type and other attributes for a linker script
14040 assignment from HSRC to HDEST. Generally this should be treated as
14041 if we found a strong non-dynamic definition for HDEST (except that
14042 ld ignores multiple definition errors). */
14044 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14045 struct bfd_link_hash_entry
*hdest
,
14046 struct bfd_link_hash_entry
*hsrc
)
14048 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14049 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14050 Elf_Internal_Sym isym
;
14052 ehdest
->type
= ehsrc
->type
;
14053 ehdest
->target_internal
= ehsrc
->target_internal
;
14055 isym
.st_other
= ehsrc
->other
;
14056 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14059 /* Append a RELA relocation REL to section S in BFD. */
14062 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14064 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14065 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14066 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14067 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14070 /* Append a REL relocation REL to section S in BFD. */
14073 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14075 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14076 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14077 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14078 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);