1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 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
));
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
442 if (! bfd_link_pic (info
))
444 s
= bfd_make_section_anyway_with_flags (abfd
,
445 (bed
->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags
| SEC_READONLY
);
449 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
467 struct elf_link_hash_entry
*h
)
469 if (h
->dynindx
== -1)
471 struct elf_strtab_hash
*dynstr
;
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h
->other
))
484 if (h
->root
.type
!= bfd_link_hash_undefined
485 && h
->root
.type
!= bfd_link_hash_undefweak
)
488 if (!elf_hash_table (info
)->is_relocatable_executable
)
496 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
497 ++elf_hash_table (info
)->dynsymcount
;
499 dynstr
= elf_hash_table (info
)->dynstr
;
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
508 /* We don't put any version information in the dynamic string
510 name
= h
->root
.root
.string
;
511 p
= strchr (name
, ELF_VER_CHR
);
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
520 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
525 if (indx
== (size_t) -1)
527 h
->dynstr_index
= indx
;
533 /* Mark a symbol dynamic. */
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
537 struct elf_link_hash_entry
*h
,
538 Elf_Internal_Sym
*sym
)
540 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
542 /* It may be called more than once on the same H. */
543 if(h
->dynamic
|| bfd_link_relocatable (info
))
546 if ((info
->dynamic_data
547 && (h
->type
== STT_OBJECT
548 || h
->type
== STT_COMMON
550 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
551 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
553 && h
->root
.type
== bfd_link_hash_new
554 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
562 bfd_elf_record_link_assignment (bfd
*output_bfd
,
563 struct bfd_link_info
*info
,
568 struct elf_link_hash_entry
*h
, *hv
;
569 struct elf_link_hash_table
*htab
;
570 const struct elf_backend_data
*bed
;
572 if (!is_elf_hash_table (info
->hash
))
575 htab
= elf_hash_table (info
);
576 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
580 if (h
->versioned
== unknown
)
582 /* Set versioned if symbol version is unknown. */
583 char *version
= strrchr (name
, ELF_VER_CHR
);
586 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
587 h
->versioned
= versioned_hidden
;
589 h
->versioned
= versioned
;
593 switch (h
->root
.type
)
595 case bfd_link_hash_defined
:
596 case bfd_link_hash_defweak
:
597 case bfd_link_hash_common
:
599 case bfd_link_hash_undefweak
:
600 case bfd_link_hash_undefined
:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
604 h
->root
.type
= bfd_link_hash_new
;
605 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
606 bfd_link_repair_undef_list (&htab
->root
);
608 case bfd_link_hash_new
:
609 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
612 case bfd_link_hash_indirect
:
613 /* We had a versioned symbol in a dynamic library. We make the
614 the versioned symbol point to this one. */
615 bed
= get_elf_backend_data (output_bfd
);
617 while (hv
->root
.type
== bfd_link_hash_indirect
618 || hv
->root
.type
== bfd_link_hash_warning
)
619 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
620 /* We don't need to update h->root.u since linker will set them
622 h
->root
.type
= bfd_link_hash_undefined
;
623 hv
->root
.type
= bfd_link_hash_indirect
;
624 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
625 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
627 case bfd_link_hash_warning
:
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
639 h
->root
.type
= bfd_link_hash_undefined
;
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
648 h
->verinfo
.verdef
= NULL
;
654 bed
= get_elf_backend_data (output_bfd
);
655 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
656 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
657 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
662 if (!bfd_link_relocatable (info
)
664 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
670 || bfd_link_dll (info
)
671 || elf_hash_table (info
)->is_relocatable_executable
)
674 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
680 if (h
->u
.weakdef
!= NULL
681 && h
->u
.weakdef
->dynindx
== -1)
683 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
691 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
701 struct elf_link_local_dynamic_entry
*entry
;
702 struct elf_link_hash_table
*eht
;
703 struct elf_strtab_hash
*dynstr
;
706 Elf_External_Sym_Shndx eshndx
;
707 char esym
[sizeof (Elf64_External_Sym
)];
709 if (! is_elf_hash_table (info
->hash
))
712 /* See if the entry exists already. */
713 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
714 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
717 amt
= sizeof (*entry
);
718 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
724 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
726 bfd_release (input_bfd
, entry
);
730 if (entry
->isym
.st_shndx
!= SHN_UNDEF
731 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
735 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
736 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd
, entry
);
745 name
= (bfd_elf_string_from_elf_section
746 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
747 entry
->isym
.st_name
));
749 dynstr
= elf_hash_table (info
)->dynstr
;
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
758 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
759 if (dynstr_index
== (size_t) -1)
761 entry
->isym
.st_name
= dynstr_index
;
763 eht
= elf_hash_table (info
);
765 entry
->next
= eht
->dynlocal
;
766 eht
->dynlocal
= entry
;
767 entry
->input_bfd
= input_bfd
;
768 entry
->input_indx
= input_indx
;
771 /* Whatever binding the symbol had before, it's now local. */
773 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
775 /* The dynindx will be set at the end of size_dynamic_sections. */
780 /* Return the dynindex of a local dynamic symbol. */
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
787 struct elf_link_local_dynamic_entry
*e
;
789 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
790 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
795 /* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
803 size_t *count
= (size_t *) data
;
808 if (h
->dynindx
!= -1)
809 h
->dynindx
= ++(*count
);
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
822 size_t *count
= (size_t *) data
;
824 if (!h
->forced_local
)
827 if (h
->dynindx
!= -1)
828 h
->dynindx
= ++(*count
);
833 /* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
836 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
837 struct bfd_link_info
*info
,
840 struct elf_link_hash_table
*htab
;
843 switch (elf_section_data (p
)->this_hdr
.sh_type
)
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
850 htab
= elf_hash_table (info
);
851 if (p
== htab
->tls_sec
)
854 if (htab
->text_index_section
!= NULL
)
855 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
857 return (htab
->dynobj
!= NULL
858 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
859 && ip
->output_section
== p
);
861 /* There shouldn't be section relative relocations
862 against any other section. */
868 /* Assign dynsym indices. In a shared library we generate a section
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
875 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
876 struct bfd_link_info
*info
,
877 unsigned long *section_sym_count
)
879 unsigned long dynsymcount
= 0;
881 if (bfd_link_pic (info
)
882 || elf_hash_table (info
)->is_relocatable_executable
)
884 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
886 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
887 if ((p
->flags
& SEC_EXCLUDE
) == 0
888 && (p
->flags
& SEC_ALLOC
) != 0
889 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
890 elf_section_data (p
)->dynindx
= ++dynsymcount
;
892 elf_section_data (p
)->dynindx
= 0;
894 *section_sym_count
= dynsymcount
;
896 elf_link_hash_traverse (elf_hash_table (info
),
897 elf_link_renumber_local_hash_table_dynsyms
,
900 if (elf_hash_table (info
)->dynlocal
)
902 struct elf_link_local_dynamic_entry
*p
;
903 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
904 p
->dynindx
= ++dynsymcount
;
906 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
908 elf_link_hash_traverse (elf_hash_table (info
),
909 elf_link_renumber_hash_table_dynsyms
,
912 /* There is an unused NULL entry at the head of the table which we
913 must account for in our count even if the table is empty since it
914 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
918 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
922 /* Merge st_other field. */
925 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
926 const Elf_Internal_Sym
*isym
, asection
*sec
,
927 bfd_boolean definition
, bfd_boolean dynamic
)
929 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
931 /* If st_other has a processor-specific meaning, specific
932 code might be needed here. */
933 if (bed
->elf_backend_merge_symbol_attribute
)
934 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
939 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
940 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
942 /* Keep the most constraining visibility. Leave the remainder
943 of the st_other field to elf_backend_merge_symbol_attribute. */
944 if (symvis
- 1 < hvis
- 1)
945 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
948 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
949 && (sec
->flags
& SEC_READONLY
) == 0)
950 h
->protected_def
= 1;
953 /* This function is called when we want to merge a new symbol with an
954 existing symbol. It handles the various cases which arise when we
955 find a definition in a dynamic object, or when there is already a
956 definition in a dynamic object. The new symbol is described by
957 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
958 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
959 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
960 of an old common symbol. We set OVERRIDE if the old symbol is
961 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
962 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
963 to change. By OK to change, we mean that we shouldn't warn if the
964 type or size does change. */
967 _bfd_elf_merge_symbol (bfd
*abfd
,
968 struct bfd_link_info
*info
,
970 Elf_Internal_Sym
*sym
,
973 struct elf_link_hash_entry
**sym_hash
,
975 bfd_boolean
*pold_weak
,
976 unsigned int *pold_alignment
,
978 bfd_boolean
*override
,
979 bfd_boolean
*type_change_ok
,
980 bfd_boolean
*size_change_ok
,
981 bfd_boolean
*matched
)
983 asection
*sec
, *oldsec
;
984 struct elf_link_hash_entry
*h
;
985 struct elf_link_hash_entry
*hi
;
986 struct elf_link_hash_entry
*flip
;
989 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
990 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
991 const struct elf_backend_data
*bed
;
998 bind
= ELF_ST_BIND (sym
->st_info
);
1000 if (! bfd_is_und_section (sec
))
1001 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1003 h
= ((struct elf_link_hash_entry
*)
1004 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1009 bed
= get_elf_backend_data (abfd
);
1011 /* NEW_VERSION is the symbol version of the new symbol. */
1012 if (h
->versioned
!= unversioned
)
1014 /* Symbol version is unknown or versioned. */
1015 new_version
= strrchr (name
, ELF_VER_CHR
);
1018 if (h
->versioned
== unknown
)
1020 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1021 h
->versioned
= versioned_hidden
;
1023 h
->versioned
= versioned
;
1026 if (new_version
[0] == '\0')
1030 h
->versioned
= unversioned
;
1035 /* For merging, we only care about real symbols. But we need to make
1036 sure that indirect symbol dynamic flags are updated. */
1038 while (h
->root
.type
== bfd_link_hash_indirect
1039 || h
->root
.type
== bfd_link_hash_warning
)
1040 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1044 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1048 /* OLD_HIDDEN is true if the existing symbol is only visible
1049 to the symbol with the same symbol version. NEW_HIDDEN is
1050 true if the new symbol is only visible to the symbol with
1051 the same symbol version. */
1052 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1053 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1054 if (!old_hidden
&& !new_hidden
)
1055 /* The new symbol matches the existing symbol if both
1060 /* OLD_VERSION is the symbol version of the existing
1064 if (h
->versioned
>= versioned
)
1065 old_version
= strrchr (h
->root
.root
.string
,
1070 /* The new symbol matches the existing symbol if they
1071 have the same symbol version. */
1072 *matched
= (old_version
== new_version
1073 || (old_version
!= NULL
1074 && new_version
!= NULL
1075 && strcmp (old_version
, new_version
) == 0));
1080 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1085 switch (h
->root
.type
)
1090 case bfd_link_hash_undefined
:
1091 case bfd_link_hash_undefweak
:
1092 oldbfd
= h
->root
.u
.undef
.abfd
;
1095 case bfd_link_hash_defined
:
1096 case bfd_link_hash_defweak
:
1097 oldbfd
= h
->root
.u
.def
.section
->owner
;
1098 oldsec
= h
->root
.u
.def
.section
;
1101 case bfd_link_hash_common
:
1102 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1103 oldsec
= h
->root
.u
.c
.p
->section
;
1105 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1108 if (poldbfd
&& *poldbfd
== NULL
)
1111 /* Differentiate strong and weak symbols. */
1112 newweak
= bind
== STB_WEAK
;
1113 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1114 || h
->root
.type
== bfd_link_hash_undefweak
);
1116 *pold_weak
= oldweak
;
1118 /* This code is for coping with dynamic objects, and is only useful
1119 if we are doing an ELF link. */
1120 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1123 /* We have to check it for every instance since the first few may be
1124 references and not all compilers emit symbol type for undefined
1126 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1128 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1129 respectively, is from a dynamic object. */
1131 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1133 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1134 syms and defined syms in dynamic libraries respectively.
1135 ref_dynamic on the other hand can be set for a symbol defined in
1136 a dynamic library, and def_dynamic may not be set; When the
1137 definition in a dynamic lib is overridden by a definition in the
1138 executable use of the symbol in the dynamic lib becomes a
1139 reference to the executable symbol. */
1142 if (bfd_is_und_section (sec
))
1144 if (bind
!= STB_WEAK
)
1146 h
->ref_dynamic_nonweak
= 1;
1147 hi
->ref_dynamic_nonweak
= 1;
1152 /* Update the existing symbol only if they match. */
1155 hi
->dynamic_def
= 1;
1159 /* If we just created the symbol, mark it as being an ELF symbol.
1160 Other than that, there is nothing to do--there is no merge issue
1161 with a newly defined symbol--so we just return. */
1163 if (h
->root
.type
== bfd_link_hash_new
)
1169 /* In cases involving weak versioned symbols, we may wind up trying
1170 to merge a symbol with itself. Catch that here, to avoid the
1171 confusion that results if we try to override a symbol with
1172 itself. The additional tests catch cases like
1173 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1174 dynamic object, which we do want to handle here. */
1176 && (newweak
|| oldweak
)
1177 && ((abfd
->flags
& DYNAMIC
) == 0
1178 || !h
->def_regular
))
1183 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1184 else if (oldsec
!= NULL
)
1186 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1187 indices used by MIPS ELF. */
1188 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1191 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1192 respectively, appear to be a definition rather than reference. */
1194 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1196 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1197 && h
->root
.type
!= bfd_link_hash_undefweak
1198 && h
->root
.type
!= bfd_link_hash_common
);
1200 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1201 respectively, appear to be a function. */
1203 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1204 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1206 oldfunc
= (h
->type
!= STT_NOTYPE
1207 && bed
->is_function_type (h
->type
));
1209 /* If creating a default indirect symbol ("foo" or "foo@") from a
1210 dynamic versioned definition ("foo@@") skip doing so if there is
1211 an existing regular definition with a different type. We don't
1212 want, for example, a "time" variable in the executable overriding
1213 a "time" function in a shared library. */
1214 if (pold_alignment
== NULL
1218 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1219 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1220 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1221 && h
->type
!= STT_NOTYPE
1222 && !(newfunc
&& oldfunc
))
1228 /* Check TLS symbols. We don't check undefined symbols introduced
1229 by "ld -u" which have no type (and oldbfd NULL), and we don't
1230 check symbols from plugins because they also have no type. */
1232 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1233 && (abfd
->flags
& BFD_PLUGIN
) == 0
1234 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1235 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1238 bfd_boolean ntdef
, tdef
;
1239 asection
*ntsec
, *tsec
;
1241 if (h
->type
== STT_TLS
)
1262 /* xgettext:c-format */
1263 (_("%s: TLS definition in %B section %A "
1264 "mismatches non-TLS definition in %B section %A"),
1265 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1266 else if (!tdef
&& !ntdef
)
1268 /* xgettext:c-format */
1269 (_("%s: TLS reference in %B "
1270 "mismatches non-TLS reference in %B"),
1271 tbfd
, ntbfd
, h
->root
.root
.string
);
1274 /* xgettext:c-format */
1275 (_("%s: TLS definition in %B section %A "
1276 "mismatches non-TLS reference in %B"),
1277 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1280 /* xgettext:c-format */
1281 (_("%s: TLS reference in %B "
1282 "mismatches non-TLS definition in %B section %A"),
1283 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1285 bfd_set_error (bfd_error_bad_value
);
1289 /* If the old symbol has non-default visibility, we ignore the new
1290 definition from a dynamic object. */
1292 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1293 && !bfd_is_und_section (sec
))
1296 /* Make sure this symbol is dynamic. */
1298 hi
->ref_dynamic
= 1;
1299 /* A protected symbol has external availability. Make sure it is
1300 recorded as dynamic.
1302 FIXME: Should we check type and size for protected symbol? */
1303 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1304 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1309 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1312 /* If the new symbol with non-default visibility comes from a
1313 relocatable file and the old definition comes from a dynamic
1314 object, we remove the old definition. */
1315 if (hi
->root
.type
== bfd_link_hash_indirect
)
1317 /* Handle the case where the old dynamic definition is
1318 default versioned. We need to copy the symbol info from
1319 the symbol with default version to the normal one if it
1320 was referenced before. */
1323 hi
->root
.type
= h
->root
.type
;
1324 h
->root
.type
= bfd_link_hash_indirect
;
1325 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1327 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1328 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1330 /* If the new symbol is hidden or internal, completely undo
1331 any dynamic link state. */
1332 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1333 h
->forced_local
= 0;
1340 /* FIXME: Should we check type and size for protected symbol? */
1350 /* If the old symbol was undefined before, then it will still be
1351 on the undefs list. If the new symbol is undefined or
1352 common, we can't make it bfd_link_hash_new here, because new
1353 undefined or common symbols will be added to the undefs list
1354 by _bfd_generic_link_add_one_symbol. Symbols may not be
1355 added twice to the undefs list. Also, if the new symbol is
1356 undefweak then we don't want to lose the strong undef. */
1357 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1359 h
->root
.type
= bfd_link_hash_undefined
;
1360 h
->root
.u
.undef
.abfd
= abfd
;
1364 h
->root
.type
= bfd_link_hash_new
;
1365 h
->root
.u
.undef
.abfd
= NULL
;
1368 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1370 /* If the new symbol is hidden or internal, completely undo
1371 any dynamic link state. */
1372 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1373 h
->forced_local
= 0;
1379 /* FIXME: Should we check type and size for protected symbol? */
1385 /* If a new weak symbol definition comes from a regular file and the
1386 old symbol comes from a dynamic library, we treat the new one as
1387 strong. Similarly, an old weak symbol definition from a regular
1388 file is treated as strong when the new symbol comes from a dynamic
1389 library. Further, an old weak symbol from a dynamic library is
1390 treated as strong if the new symbol is from a dynamic library.
1391 This reflects the way glibc's ld.so works.
1393 Do this before setting *type_change_ok or *size_change_ok so that
1394 we warn properly when dynamic library symbols are overridden. */
1396 if (newdef
&& !newdyn
&& olddyn
)
1398 if (olddef
&& newdyn
)
1401 /* Allow changes between different types of function symbol. */
1402 if (newfunc
&& oldfunc
)
1403 *type_change_ok
= TRUE
;
1405 /* It's OK to change the type if either the existing symbol or the
1406 new symbol is weak. A type change is also OK if the old symbol
1407 is undefined and the new symbol is defined. */
1412 && h
->root
.type
== bfd_link_hash_undefined
))
1413 *type_change_ok
= TRUE
;
1415 /* It's OK to change the size if either the existing symbol or the
1416 new symbol is weak, or if the old symbol is undefined. */
1419 || h
->root
.type
== bfd_link_hash_undefined
)
1420 *size_change_ok
= TRUE
;
1422 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1423 symbol, respectively, appears to be a common symbol in a dynamic
1424 object. If a symbol appears in an uninitialized section, and is
1425 not weak, and is not a function, then it may be a common symbol
1426 which was resolved when the dynamic object was created. We want
1427 to treat such symbols specially, because they raise special
1428 considerations when setting the symbol size: if the symbol
1429 appears as a common symbol in a regular object, and the size in
1430 the regular object is larger, we must make sure that we use the
1431 larger size. This problematic case can always be avoided in C,
1432 but it must be handled correctly when using Fortran shared
1435 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1436 likewise for OLDDYNCOMMON and OLDDEF.
1438 Note that this test is just a heuristic, and that it is quite
1439 possible to have an uninitialized symbol in a shared object which
1440 is really a definition, rather than a common symbol. This could
1441 lead to some minor confusion when the symbol really is a common
1442 symbol in some regular object. However, I think it will be
1448 && (sec
->flags
& SEC_ALLOC
) != 0
1449 && (sec
->flags
& SEC_LOAD
) == 0
1452 newdyncommon
= TRUE
;
1454 newdyncommon
= FALSE
;
1458 && h
->root
.type
== bfd_link_hash_defined
1460 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1461 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1464 olddyncommon
= TRUE
;
1466 olddyncommon
= FALSE
;
1468 /* We now know everything about the old and new symbols. We ask the
1469 backend to check if we can merge them. */
1470 if (bed
->merge_symbol
!= NULL
)
1472 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1477 /* If both the old and the new symbols look like common symbols in a
1478 dynamic object, set the size of the symbol to the larger of the
1483 && sym
->st_size
!= h
->size
)
1485 /* Since we think we have two common symbols, issue a multiple
1486 common warning if desired. Note that we only warn if the
1487 size is different. If the size is the same, we simply let
1488 the old symbol override the new one as normally happens with
1489 symbols defined in dynamic objects. */
1491 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1492 bfd_link_hash_common
, sym
->st_size
);
1493 if (sym
->st_size
> h
->size
)
1494 h
->size
= sym
->st_size
;
1496 *size_change_ok
= TRUE
;
1499 /* If we are looking at a dynamic object, and we have found a
1500 definition, we need to see if the symbol was already defined by
1501 some other object. If so, we want to use the existing
1502 definition, and we do not want to report a multiple symbol
1503 definition error; we do this by clobbering *PSEC to be
1504 bfd_und_section_ptr.
1506 We treat a common symbol as a definition if the symbol in the
1507 shared library is a function, since common symbols always
1508 represent variables; this can cause confusion in principle, but
1509 any such confusion would seem to indicate an erroneous program or
1510 shared library. We also permit a common symbol in a regular
1511 object to override a weak symbol in a shared object. A common
1512 symbol in executable also overrides a symbol in a shared object. */
1517 || (h
->root
.type
== bfd_link_hash_common
1520 || (!olddyn
&& bfd_link_executable (info
))))))
1524 newdyncommon
= FALSE
;
1526 *psec
= sec
= bfd_und_section_ptr
;
1527 *size_change_ok
= TRUE
;
1529 /* If we get here when the old symbol is a common symbol, then
1530 we are explicitly letting it override a weak symbol or
1531 function in a dynamic object, and we don't want to warn about
1532 a type change. If the old symbol is a defined symbol, a type
1533 change warning may still be appropriate. */
1535 if (h
->root
.type
== bfd_link_hash_common
)
1536 *type_change_ok
= TRUE
;
1539 /* Handle the special case of an old common symbol merging with a
1540 new symbol which looks like a common symbol in a shared object.
1541 We change *PSEC and *PVALUE to make the new symbol look like a
1542 common symbol, and let _bfd_generic_link_add_one_symbol do the
1546 && h
->root
.type
== bfd_link_hash_common
)
1550 newdyncommon
= FALSE
;
1551 *pvalue
= sym
->st_size
;
1552 *psec
= sec
= bed
->common_section (oldsec
);
1553 *size_change_ok
= TRUE
;
1556 /* Skip weak definitions of symbols that are already defined. */
1557 if (newdef
&& olddef
&& newweak
)
1559 /* Don't skip new non-IR weak syms. */
1560 if (!(oldbfd
!= NULL
1561 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1562 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1568 /* Merge st_other. If the symbol already has a dynamic index,
1569 but visibility says it should not be visible, turn it into a
1571 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1572 if (h
->dynindx
!= -1)
1573 switch (ELF_ST_VISIBILITY (h
->other
))
1577 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1582 /* If the old symbol is from a dynamic object, and the new symbol is
1583 a definition which is not from a dynamic object, then the new
1584 symbol overrides the old symbol. Symbols from regular files
1585 always take precedence over symbols from dynamic objects, even if
1586 they are defined after the dynamic object in the link.
1588 As above, we again permit a common symbol in a regular object to
1589 override a definition in a shared object if the shared object
1590 symbol is a function or is weak. */
1595 || (bfd_is_com_section (sec
)
1596 && (oldweak
|| oldfunc
)))
1601 /* Change the hash table entry to undefined, and let
1602 _bfd_generic_link_add_one_symbol do the right thing with the
1605 h
->root
.type
= bfd_link_hash_undefined
;
1606 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1607 *size_change_ok
= TRUE
;
1610 olddyncommon
= FALSE
;
1612 /* We again permit a type change when a common symbol may be
1613 overriding a function. */
1615 if (bfd_is_com_section (sec
))
1619 /* If a common symbol overrides a function, make sure
1620 that it isn't defined dynamically nor has type
1623 h
->type
= STT_NOTYPE
;
1625 *type_change_ok
= TRUE
;
1628 if (hi
->root
.type
== bfd_link_hash_indirect
)
1631 /* This union may have been set to be non-NULL when this symbol
1632 was seen in a dynamic object. We must force the union to be
1633 NULL, so that it is correct for a regular symbol. */
1634 h
->verinfo
.vertree
= NULL
;
1637 /* Handle the special case of a new common symbol merging with an
1638 old symbol that looks like it might be a common symbol defined in
1639 a shared object. Note that we have already handled the case in
1640 which a new common symbol should simply override the definition
1641 in the shared library. */
1644 && bfd_is_com_section (sec
)
1647 /* It would be best if we could set the hash table entry to a
1648 common symbol, but we don't know what to use for the section
1649 or the alignment. */
1650 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1651 bfd_link_hash_common
, sym
->st_size
);
1653 /* If the presumed common symbol in the dynamic object is
1654 larger, pretend that the new symbol has its size. */
1656 if (h
->size
> *pvalue
)
1659 /* We need to remember the alignment required by the symbol
1660 in the dynamic object. */
1661 BFD_ASSERT (pold_alignment
);
1662 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1665 olddyncommon
= FALSE
;
1667 h
->root
.type
= bfd_link_hash_undefined
;
1668 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1670 *size_change_ok
= TRUE
;
1671 *type_change_ok
= TRUE
;
1673 if (hi
->root
.type
== bfd_link_hash_indirect
)
1676 h
->verinfo
.vertree
= NULL
;
1681 /* Handle the case where we had a versioned symbol in a dynamic
1682 library and now find a definition in a normal object. In this
1683 case, we make the versioned symbol point to the normal one. */
1684 flip
->root
.type
= h
->root
.type
;
1685 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1686 h
->root
.type
= bfd_link_hash_indirect
;
1687 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1688 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1692 flip
->ref_dynamic
= 1;
1699 /* This function is called to create an indirect symbol from the
1700 default for the symbol with the default version if needed. The
1701 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1702 set DYNSYM if the new indirect symbol is dynamic. */
1705 _bfd_elf_add_default_symbol (bfd
*abfd
,
1706 struct bfd_link_info
*info
,
1707 struct elf_link_hash_entry
*h
,
1709 Elf_Internal_Sym
*sym
,
1713 bfd_boolean
*dynsym
)
1715 bfd_boolean type_change_ok
;
1716 bfd_boolean size_change_ok
;
1719 struct elf_link_hash_entry
*hi
;
1720 struct bfd_link_hash_entry
*bh
;
1721 const struct elf_backend_data
*bed
;
1722 bfd_boolean collect
;
1723 bfd_boolean dynamic
;
1724 bfd_boolean override
;
1726 size_t len
, shortlen
;
1728 bfd_boolean matched
;
1730 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1733 /* If this symbol has a version, and it is the default version, we
1734 create an indirect symbol from the default name to the fully
1735 decorated name. This will cause external references which do not
1736 specify a version to be bound to this version of the symbol. */
1737 p
= strchr (name
, ELF_VER_CHR
);
1738 if (h
->versioned
== unknown
)
1742 h
->versioned
= unversioned
;
1747 if (p
[1] != ELF_VER_CHR
)
1749 h
->versioned
= versioned_hidden
;
1753 h
->versioned
= versioned
;
1758 /* PR ld/19073: We may see an unversioned definition after the
1764 bed
= get_elf_backend_data (abfd
);
1765 collect
= bed
->collect
;
1766 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1768 shortlen
= p
- name
;
1769 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1770 if (shortname
== NULL
)
1772 memcpy (shortname
, name
, shortlen
);
1773 shortname
[shortlen
] = '\0';
1775 /* We are going to create a new symbol. Merge it with any existing
1776 symbol with this name. For the purposes of the merge, act as
1777 though we were defining the symbol we just defined, although we
1778 actually going to define an indirect symbol. */
1779 type_change_ok
= FALSE
;
1780 size_change_ok
= FALSE
;
1783 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1784 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1785 &type_change_ok
, &size_change_ok
, &matched
))
1791 if (hi
->def_regular
)
1793 /* If the undecorated symbol will have a version added by a
1794 script different to H, then don't indirect to/from the
1795 undecorated symbol. This isn't ideal because we may not yet
1796 have seen symbol versions, if given by a script on the
1797 command line rather than via --version-script. */
1798 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1803 = bfd_find_version_for_sym (info
->version_info
,
1804 hi
->root
.root
.string
, &hide
);
1805 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1807 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1811 if (hi
->verinfo
.vertree
!= NULL
1812 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1818 /* Add the default symbol if not performing a relocatable link. */
1819 if (! bfd_link_relocatable (info
))
1822 if (! (_bfd_generic_link_add_one_symbol
1823 (info
, abfd
, shortname
, BSF_INDIRECT
,
1824 bfd_ind_section_ptr
,
1825 0, name
, FALSE
, collect
, &bh
)))
1827 hi
= (struct elf_link_hash_entry
*) bh
;
1832 /* In this case the symbol named SHORTNAME is overriding the
1833 indirect symbol we want to add. We were planning on making
1834 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1835 is the name without a version. NAME is the fully versioned
1836 name, and it is the default version.
1838 Overriding means that we already saw a definition for the
1839 symbol SHORTNAME in a regular object, and it is overriding
1840 the symbol defined in the dynamic object.
1842 When this happens, we actually want to change NAME, the
1843 symbol we just added, to refer to SHORTNAME. This will cause
1844 references to NAME in the shared object to become references
1845 to SHORTNAME in the regular object. This is what we expect
1846 when we override a function in a shared object: that the
1847 references in the shared object will be mapped to the
1848 definition in the regular object. */
1850 while (hi
->root
.type
== bfd_link_hash_indirect
1851 || hi
->root
.type
== bfd_link_hash_warning
)
1852 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1854 h
->root
.type
= bfd_link_hash_indirect
;
1855 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1859 hi
->ref_dynamic
= 1;
1863 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1868 /* Now set HI to H, so that the following code will set the
1869 other fields correctly. */
1873 /* Check if HI is a warning symbol. */
1874 if (hi
->root
.type
== bfd_link_hash_warning
)
1875 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1877 /* If there is a duplicate definition somewhere, then HI may not
1878 point to an indirect symbol. We will have reported an error to
1879 the user in that case. */
1881 if (hi
->root
.type
== bfd_link_hash_indirect
)
1883 struct elf_link_hash_entry
*ht
;
1885 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1886 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1888 /* A reference to the SHORTNAME symbol from a dynamic library
1889 will be satisfied by the versioned symbol at runtime. In
1890 effect, we have a reference to the versioned symbol. */
1891 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1892 hi
->dynamic_def
|= ht
->dynamic_def
;
1894 /* See if the new flags lead us to realize that the symbol must
1900 if (! bfd_link_executable (info
)
1907 if (hi
->ref_regular
)
1913 /* We also need to define an indirection from the nondefault version
1917 len
= strlen (name
);
1918 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1919 if (shortname
== NULL
)
1921 memcpy (shortname
, name
, shortlen
);
1922 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1924 /* Once again, merge with any existing symbol. */
1925 type_change_ok
= FALSE
;
1926 size_change_ok
= FALSE
;
1928 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1929 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1930 &type_change_ok
, &size_change_ok
, &matched
))
1938 /* Here SHORTNAME is a versioned name, so we don't expect to see
1939 the type of override we do in the case above unless it is
1940 overridden by a versioned definition. */
1941 if (hi
->root
.type
!= bfd_link_hash_defined
1942 && hi
->root
.type
!= bfd_link_hash_defweak
)
1944 /* xgettext:c-format */
1945 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1951 if (! (_bfd_generic_link_add_one_symbol
1952 (info
, abfd
, shortname
, BSF_INDIRECT
,
1953 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1955 hi
= (struct elf_link_hash_entry
*) bh
;
1957 /* If there is a duplicate definition somewhere, then HI may not
1958 point to an indirect symbol. We will have reported an error
1959 to the user in that case. */
1961 if (hi
->root
.type
== bfd_link_hash_indirect
)
1963 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1964 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1965 hi
->dynamic_def
|= h
->dynamic_def
;
1967 /* See if the new flags lead us to realize that the symbol
1973 if (! bfd_link_executable (info
)
1979 if (hi
->ref_regular
)
1989 /* This routine is used to export all defined symbols into the dynamic
1990 symbol table. It is called via elf_link_hash_traverse. */
1993 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1995 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1997 /* Ignore indirect symbols. These are added by the versioning code. */
1998 if (h
->root
.type
== bfd_link_hash_indirect
)
2001 /* Ignore this if we won't export it. */
2002 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2005 if (h
->dynindx
== -1
2006 && (h
->def_regular
|| h
->ref_regular
)
2007 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2008 h
->root
.root
.string
))
2010 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2020 /* Look through the symbols which are defined in other shared
2021 libraries and referenced here. Update the list of version
2022 dependencies. This will be put into the .gnu.version_r section.
2023 This function is called via elf_link_hash_traverse. */
2026 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2029 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2030 Elf_Internal_Verneed
*t
;
2031 Elf_Internal_Vernaux
*a
;
2034 /* We only care about symbols defined in shared objects with version
2039 || h
->verinfo
.verdef
== NULL
2040 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2041 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2044 /* See if we already know about this version. */
2045 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2049 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2052 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2053 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2059 /* This is a new version. Add it to tree we are building. */
2064 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2067 rinfo
->failed
= TRUE
;
2071 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2072 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2073 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2077 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2080 rinfo
->failed
= TRUE
;
2084 /* Note that we are copying a string pointer here, and testing it
2085 above. If bfd_elf_string_from_elf_section is ever changed to
2086 discard the string data when low in memory, this will have to be
2088 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2090 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2091 a
->vna_nextptr
= t
->vn_auxptr
;
2093 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2096 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2103 /* Figure out appropriate versions for all the symbols. We may not
2104 have the version number script until we have read all of the input
2105 files, so until that point we don't know which symbols should be
2106 local. This function is called via elf_link_hash_traverse. */
2109 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2111 struct elf_info_failed
*sinfo
;
2112 struct bfd_link_info
*info
;
2113 const struct elf_backend_data
*bed
;
2114 struct elf_info_failed eif
;
2117 sinfo
= (struct elf_info_failed
*) data
;
2120 /* Fix the symbol flags. */
2123 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2126 sinfo
->failed
= TRUE
;
2130 /* We only need version numbers for symbols defined in regular
2132 if (!h
->def_regular
)
2135 bed
= get_elf_backend_data (info
->output_bfd
);
2136 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2137 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2139 struct bfd_elf_version_tree
*t
;
2142 if (*p
== ELF_VER_CHR
)
2145 /* If there is no version string, we can just return out. */
2149 /* Look for the version. If we find it, it is no longer weak. */
2150 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2152 if (strcmp (t
->name
, p
) == 0)
2156 struct bfd_elf_version_expr
*d
;
2158 len
= p
- h
->root
.root
.string
;
2159 alc
= (char *) bfd_malloc (len
);
2162 sinfo
->failed
= TRUE
;
2165 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2166 alc
[len
- 1] = '\0';
2167 if (alc
[len
- 2] == ELF_VER_CHR
)
2168 alc
[len
- 2] = '\0';
2170 h
->verinfo
.vertree
= t
;
2174 if (t
->globals
.list
!= NULL
)
2175 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2177 /* See if there is anything to force this symbol to
2179 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2181 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2184 && ! info
->export_dynamic
)
2185 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2193 /* If we are building an application, we need to create a
2194 version node for this version. */
2195 if (t
== NULL
&& bfd_link_executable (info
))
2197 struct bfd_elf_version_tree
**pp
;
2200 /* If we aren't going to export this symbol, we don't need
2201 to worry about it. */
2202 if (h
->dynindx
== -1)
2205 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2209 sinfo
->failed
= TRUE
;
2214 t
->name_indx
= (unsigned int) -1;
2218 /* Don't count anonymous version tag. */
2219 if (sinfo
->info
->version_info
!= NULL
2220 && sinfo
->info
->version_info
->vernum
== 0)
2222 for (pp
= &sinfo
->info
->version_info
;
2226 t
->vernum
= version_index
;
2230 h
->verinfo
.vertree
= t
;
2234 /* We could not find the version for a symbol when
2235 generating a shared archive. Return an error. */
2237 /* xgettext:c-format */
2238 (_("%B: version node not found for symbol %s"),
2239 info
->output_bfd
, h
->root
.root
.string
);
2240 bfd_set_error (bfd_error_bad_value
);
2241 sinfo
->failed
= TRUE
;
2246 /* If we don't have a version for this symbol, see if we can find
2248 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2253 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2254 h
->root
.root
.string
, &hide
);
2255 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2256 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2262 /* Read and swap the relocs from the section indicated by SHDR. This
2263 may be either a REL or a RELA section. The relocations are
2264 translated into RELA relocations and stored in INTERNAL_RELOCS,
2265 which should have already been allocated to contain enough space.
2266 The EXTERNAL_RELOCS are a buffer where the external form of the
2267 relocations should be stored.
2269 Returns FALSE if something goes wrong. */
2272 elf_link_read_relocs_from_section (bfd
*abfd
,
2274 Elf_Internal_Shdr
*shdr
,
2275 void *external_relocs
,
2276 Elf_Internal_Rela
*internal_relocs
)
2278 const struct elf_backend_data
*bed
;
2279 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2280 const bfd_byte
*erela
;
2281 const bfd_byte
*erelaend
;
2282 Elf_Internal_Rela
*irela
;
2283 Elf_Internal_Shdr
*symtab_hdr
;
2286 /* Position ourselves at the start of the section. */
2287 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2290 /* Read the relocations. */
2291 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2294 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2295 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2297 bed
= get_elf_backend_data (abfd
);
2299 /* Convert the external relocations to the internal format. */
2300 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2301 swap_in
= bed
->s
->swap_reloc_in
;
2302 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2303 swap_in
= bed
->s
->swap_reloca_in
;
2306 bfd_set_error (bfd_error_wrong_format
);
2310 erela
= (const bfd_byte
*) external_relocs
;
2311 erelaend
= erela
+ shdr
->sh_size
;
2312 irela
= internal_relocs
;
2313 while (erela
< erelaend
)
2317 (*swap_in
) (abfd
, erela
, irela
);
2318 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2319 if (bed
->s
->arch_size
== 64)
2323 if ((size_t) r_symndx
>= nsyms
)
2326 /* xgettext:c-format */
2327 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2328 " for offset 0x%lx in section `%A'"),
2330 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2331 bfd_set_error (bfd_error_bad_value
);
2335 else if (r_symndx
!= STN_UNDEF
)
2338 /* xgettext:c-format */
2339 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2340 " when the object file has no symbol table"),
2342 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2343 bfd_set_error (bfd_error_bad_value
);
2346 irela
+= bed
->s
->int_rels_per_ext_rel
;
2347 erela
+= shdr
->sh_entsize
;
2353 /* Read and swap the relocs for a section O. They may have been
2354 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2355 not NULL, they are used as buffers to read into. They are known to
2356 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2357 the return value is allocated using either malloc or bfd_alloc,
2358 according to the KEEP_MEMORY argument. If O has two relocation
2359 sections (both REL and RELA relocations), then the REL_HDR
2360 relocations will appear first in INTERNAL_RELOCS, followed by the
2361 RELA_HDR relocations. */
2364 _bfd_elf_link_read_relocs (bfd
*abfd
,
2366 void *external_relocs
,
2367 Elf_Internal_Rela
*internal_relocs
,
2368 bfd_boolean keep_memory
)
2370 void *alloc1
= NULL
;
2371 Elf_Internal_Rela
*alloc2
= NULL
;
2372 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2373 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2374 Elf_Internal_Rela
*internal_rela_relocs
;
2376 if (esdo
->relocs
!= NULL
)
2377 return esdo
->relocs
;
2379 if (o
->reloc_count
== 0)
2382 if (internal_relocs
== NULL
)
2386 size
= o
->reloc_count
;
2387 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2389 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2391 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2392 if (internal_relocs
== NULL
)
2396 if (external_relocs
== NULL
)
2398 bfd_size_type size
= 0;
2401 size
+= esdo
->rel
.hdr
->sh_size
;
2403 size
+= esdo
->rela
.hdr
->sh_size
;
2405 alloc1
= bfd_malloc (size
);
2408 external_relocs
= alloc1
;
2411 internal_rela_relocs
= internal_relocs
;
2414 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2418 external_relocs
= (((bfd_byte
*) external_relocs
)
2419 + esdo
->rel
.hdr
->sh_size
);
2420 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2421 * bed
->s
->int_rels_per_ext_rel
);
2425 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2427 internal_rela_relocs
)))
2430 /* Cache the results for next time, if we can. */
2432 esdo
->relocs
= internal_relocs
;
2437 /* Don't free alloc2, since if it was allocated we are passing it
2438 back (under the name of internal_relocs). */
2440 return internal_relocs
;
2448 bfd_release (abfd
, alloc2
);
2455 /* Compute the size of, and allocate space for, REL_HDR which is the
2456 section header for a section containing relocations for O. */
2459 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2460 struct bfd_elf_section_reloc_data
*reldata
)
2462 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2464 /* That allows us to calculate the size of the section. */
2465 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2467 /* The contents field must last into write_object_contents, so we
2468 allocate it with bfd_alloc rather than malloc. Also since we
2469 cannot be sure that the contents will actually be filled in,
2470 we zero the allocated space. */
2471 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2472 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2475 if (reldata
->hashes
== NULL
&& reldata
->count
)
2477 struct elf_link_hash_entry
**p
;
2479 p
= ((struct elf_link_hash_entry
**)
2480 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2484 reldata
->hashes
= p
;
2490 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2491 originated from the section given by INPUT_REL_HDR) to the
2495 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2496 asection
*input_section
,
2497 Elf_Internal_Shdr
*input_rel_hdr
,
2498 Elf_Internal_Rela
*internal_relocs
,
2499 struct elf_link_hash_entry
**rel_hash
2502 Elf_Internal_Rela
*irela
;
2503 Elf_Internal_Rela
*irelaend
;
2505 struct bfd_elf_section_reloc_data
*output_reldata
;
2506 asection
*output_section
;
2507 const struct elf_backend_data
*bed
;
2508 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2509 struct bfd_elf_section_data
*esdo
;
2511 output_section
= input_section
->output_section
;
2513 bed
= get_elf_backend_data (output_bfd
);
2514 esdo
= elf_section_data (output_section
);
2515 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2517 output_reldata
= &esdo
->rel
;
2518 swap_out
= bed
->s
->swap_reloc_out
;
2520 else if (esdo
->rela
.hdr
2521 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2523 output_reldata
= &esdo
->rela
;
2524 swap_out
= bed
->s
->swap_reloca_out
;
2529 /* xgettext:c-format */
2530 (_("%B: relocation size mismatch in %B section %A"),
2531 output_bfd
, input_section
->owner
, input_section
);
2532 bfd_set_error (bfd_error_wrong_format
);
2536 erel
= output_reldata
->hdr
->contents
;
2537 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2538 irela
= internal_relocs
;
2539 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2540 * bed
->s
->int_rels_per_ext_rel
);
2541 while (irela
< irelaend
)
2543 (*swap_out
) (output_bfd
, irela
, erel
);
2544 irela
+= bed
->s
->int_rels_per_ext_rel
;
2545 erel
+= input_rel_hdr
->sh_entsize
;
2548 /* Bump the counter, so that we know where to add the next set of
2550 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2555 /* Make weak undefined symbols in PIE dynamic. */
2558 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2559 struct elf_link_hash_entry
*h
)
2561 if (bfd_link_pie (info
)
2563 && h
->root
.type
== bfd_link_hash_undefweak
)
2564 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2569 /* Fix up the flags for a symbol. This handles various cases which
2570 can only be fixed after all the input files are seen. This is
2571 currently called by both adjust_dynamic_symbol and
2572 assign_sym_version, which is unnecessary but perhaps more robust in
2573 the face of future changes. */
2576 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2577 struct elf_info_failed
*eif
)
2579 const struct elf_backend_data
*bed
;
2581 /* If this symbol was mentioned in a non-ELF file, try to set
2582 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2583 permit a non-ELF file to correctly refer to a symbol defined in
2584 an ELF dynamic object. */
2587 while (h
->root
.type
== bfd_link_hash_indirect
)
2588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2590 if (h
->root
.type
!= bfd_link_hash_defined
2591 && h
->root
.type
!= bfd_link_hash_defweak
)
2594 h
->ref_regular_nonweak
= 1;
2598 if (h
->root
.u
.def
.section
->owner
!= NULL
2599 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2600 == bfd_target_elf_flavour
))
2603 h
->ref_regular_nonweak
= 1;
2609 if (h
->dynindx
== -1
2613 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2622 /* Unfortunately, NON_ELF is only correct if the symbol
2623 was first seen in a non-ELF file. Fortunately, if the symbol
2624 was first seen in an ELF file, we're probably OK unless the
2625 symbol was defined in a non-ELF file. Catch that case here.
2626 FIXME: We're still in trouble if the symbol was first seen in
2627 a dynamic object, and then later in a non-ELF regular object. */
2628 if ((h
->root
.type
== bfd_link_hash_defined
2629 || h
->root
.type
== bfd_link_hash_defweak
)
2631 && (h
->root
.u
.def
.section
->owner
!= NULL
2632 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2633 != bfd_target_elf_flavour
)
2634 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2635 && !h
->def_dynamic
)))
2639 /* Backend specific symbol fixup. */
2640 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2641 if (bed
->elf_backend_fixup_symbol
2642 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2645 /* If this is a final link, and the symbol was defined as a common
2646 symbol in a regular object file, and there was no definition in
2647 any dynamic object, then the linker will have allocated space for
2648 the symbol in a common section but the DEF_REGULAR
2649 flag will not have been set. */
2650 if (h
->root
.type
== bfd_link_hash_defined
2654 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2657 /* If a weak undefined symbol has non-default visibility, we also
2658 hide it from the dynamic linker. */
2659 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2660 && h
->root
.type
== bfd_link_hash_undefweak
)
2661 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2663 /* A hidden versioned symbol in executable should be forced local if
2664 it is is locally defined, not referenced by shared library and not
2666 else if (bfd_link_executable (eif
->info
)
2667 && h
->versioned
== versioned_hidden
2668 && !eif
->info
->export_dynamic
2672 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2674 /* If -Bsymbolic was used (which means to bind references to global
2675 symbols to the definition within the shared object), and this
2676 symbol was defined in a regular object, then it actually doesn't
2677 need a PLT entry. Likewise, if the symbol has non-default
2678 visibility. If the symbol has hidden or internal visibility, we
2679 will force it local. */
2680 else if (h
->needs_plt
2681 && bfd_link_pic (eif
->info
)
2682 && is_elf_hash_table (eif
->info
->hash
)
2683 && (SYMBOLIC_BIND (eif
->info
, h
)
2684 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2687 bfd_boolean force_local
;
2689 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2690 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2691 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2694 /* If this is a weak defined symbol in a dynamic object, and we know
2695 the real definition in the dynamic object, copy interesting flags
2696 over to the real definition. */
2697 if (h
->u
.weakdef
!= NULL
)
2699 /* If the real definition is defined by a regular object file,
2700 don't do anything special. See the longer description in
2701 _bfd_elf_adjust_dynamic_symbol, below. */
2702 if (h
->u
.weakdef
->def_regular
)
2703 h
->u
.weakdef
= NULL
;
2706 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2708 while (h
->root
.type
== bfd_link_hash_indirect
)
2709 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2711 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2712 || h
->root
.type
== bfd_link_hash_defweak
);
2713 BFD_ASSERT (weakdef
->def_dynamic
);
2714 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2715 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2716 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2723 /* Make the backend pick a good value for a dynamic symbol. This is
2724 called via elf_link_hash_traverse, and also calls itself
2728 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2730 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2732 const struct elf_backend_data
*bed
;
2734 if (! is_elf_hash_table (eif
->info
->hash
))
2737 /* Ignore indirect symbols. These are added by the versioning code. */
2738 if (h
->root
.type
== bfd_link_hash_indirect
)
2741 /* Fix the symbol flags. */
2742 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2745 /* If this symbol does not require a PLT entry, and it is not
2746 defined by a dynamic object, or is not referenced by a regular
2747 object, ignore it. We do have to handle a weak defined symbol,
2748 even if no regular object refers to it, if we decided to add it
2749 to the dynamic symbol table. FIXME: Do we normally need to worry
2750 about symbols which are defined by one dynamic object and
2751 referenced by another one? */
2753 && h
->type
!= STT_GNU_IFUNC
2757 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2759 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2763 /* If we've already adjusted this symbol, don't do it again. This
2764 can happen via a recursive call. */
2765 if (h
->dynamic_adjusted
)
2768 /* Don't look at this symbol again. Note that we must set this
2769 after checking the above conditions, because we may look at a
2770 symbol once, decide not to do anything, and then get called
2771 recursively later after REF_REGULAR is set below. */
2772 h
->dynamic_adjusted
= 1;
2774 /* If this is a weak definition, and we know a real definition, and
2775 the real symbol is not itself defined by a regular object file,
2776 then get a good value for the real definition. We handle the
2777 real symbol first, for the convenience of the backend routine.
2779 Note that there is a confusing case here. If the real definition
2780 is defined by a regular object file, we don't get the real symbol
2781 from the dynamic object, but we do get the weak symbol. If the
2782 processor backend uses a COPY reloc, then if some routine in the
2783 dynamic object changes the real symbol, we will not see that
2784 change in the corresponding weak symbol. This is the way other
2785 ELF linkers work as well, and seems to be a result of the shared
2788 I will clarify this issue. Most SVR4 shared libraries define the
2789 variable _timezone and define timezone as a weak synonym. The
2790 tzset call changes _timezone. If you write
2791 extern int timezone;
2793 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2794 you might expect that, since timezone is a synonym for _timezone,
2795 the same number will print both times. However, if the processor
2796 backend uses a COPY reloc, then actually timezone will be copied
2797 into your process image, and, since you define _timezone
2798 yourself, _timezone will not. Thus timezone and _timezone will
2799 wind up at different memory locations. The tzset call will set
2800 _timezone, leaving timezone unchanged. */
2802 if (h
->u
.weakdef
!= NULL
)
2804 /* If we get to this point, there is an implicit reference to
2805 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2806 h
->u
.weakdef
->ref_regular
= 1;
2808 /* Ensure that the backend adjust_dynamic_symbol function sees
2809 H->U.WEAKDEF before H by recursively calling ourselves. */
2810 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2814 /* If a symbol has no type and no size and does not require a PLT
2815 entry, then we are probably about to do the wrong thing here: we
2816 are probably going to create a COPY reloc for an empty object.
2817 This case can arise when a shared object is built with assembly
2818 code, and the assembly code fails to set the symbol type. */
2820 && h
->type
== STT_NOTYPE
2823 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2824 h
->root
.root
.string
);
2826 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2827 bed
= get_elf_backend_data (dynobj
);
2829 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2838 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2842 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2843 struct elf_link_hash_entry
*h
,
2846 unsigned int power_of_two
;
2848 asection
*sec
= h
->root
.u
.def
.section
;
2850 /* The section aligment of definition is the maximum alignment
2851 requirement of symbols defined in the section. Since we don't
2852 know the symbol alignment requirement, we start with the
2853 maximum alignment and check low bits of the symbol address
2854 for the minimum alignment. */
2855 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2856 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2857 while ((h
->root
.u
.def
.value
& mask
) != 0)
2863 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2866 /* Adjust the section alignment if needed. */
2867 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2872 /* We make sure that the symbol will be aligned properly. */
2873 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2875 /* Define the symbol as being at this point in DYNBSS. */
2876 h
->root
.u
.def
.section
= dynbss
;
2877 h
->root
.u
.def
.value
= dynbss
->size
;
2879 /* Increment the size of DYNBSS to make room for the symbol. */
2880 dynbss
->size
+= h
->size
;
2882 /* No error if extern_protected_data is true. */
2883 if (h
->protected_def
2884 && (!info
->extern_protected_data
2885 || (info
->extern_protected_data
< 0
2886 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2887 info
->callbacks
->einfo
2888 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2889 h
->root
.root
.string
);
2894 /* Adjust all external symbols pointing into SEC_MERGE sections
2895 to reflect the object merging within the sections. */
2898 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2902 if ((h
->root
.type
== bfd_link_hash_defined
2903 || h
->root
.type
== bfd_link_hash_defweak
)
2904 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2905 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2907 bfd
*output_bfd
= (bfd
*) data
;
2909 h
->root
.u
.def
.value
=
2910 _bfd_merged_section_offset (output_bfd
,
2911 &h
->root
.u
.def
.section
,
2912 elf_section_data (sec
)->sec_info
,
2913 h
->root
.u
.def
.value
);
2919 /* Returns false if the symbol referred to by H should be considered
2920 to resolve local to the current module, and true if it should be
2921 considered to bind dynamically. */
2924 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2925 struct bfd_link_info
*info
,
2926 bfd_boolean not_local_protected
)
2928 bfd_boolean binding_stays_local_p
;
2929 const struct elf_backend_data
*bed
;
2930 struct elf_link_hash_table
*hash_table
;
2935 while (h
->root
.type
== bfd_link_hash_indirect
2936 || h
->root
.type
== bfd_link_hash_warning
)
2937 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2939 /* If it was forced local, then clearly it's not dynamic. */
2940 if (h
->dynindx
== -1)
2942 if (h
->forced_local
)
2945 /* Identify the cases where name binding rules say that a
2946 visible symbol resolves locally. */
2947 binding_stays_local_p
= (bfd_link_executable (info
)
2948 || SYMBOLIC_BIND (info
, h
));
2950 switch (ELF_ST_VISIBILITY (h
->other
))
2957 hash_table
= elf_hash_table (info
);
2958 if (!is_elf_hash_table (hash_table
))
2961 bed
= get_elf_backend_data (hash_table
->dynobj
);
2963 /* Proper resolution for function pointer equality may require
2964 that these symbols perhaps be resolved dynamically, even though
2965 we should be resolving them to the current module. */
2966 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2967 binding_stays_local_p
= TRUE
;
2974 /* If it isn't defined locally, then clearly it's dynamic. */
2975 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2978 /* Otherwise, the symbol is dynamic if binding rules don't tell
2979 us that it remains local. */
2980 return !binding_stays_local_p
;
2983 /* Return true if the symbol referred to by H should be considered
2984 to resolve local to the current module, and false otherwise. Differs
2985 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2986 undefined symbols. The two functions are virtually identical except
2987 for the place where forced_local and dynindx == -1 are tested. If
2988 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2989 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2990 the symbol is local only for defined symbols.
2991 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2992 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2993 treatment of undefined weak symbols. For those that do not make
2994 undefined weak symbols dynamic, both functions may return false. */
2997 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2998 struct bfd_link_info
*info
,
2999 bfd_boolean local_protected
)
3001 const struct elf_backend_data
*bed
;
3002 struct elf_link_hash_table
*hash_table
;
3004 /* If it's a local sym, of course we resolve locally. */
3008 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3009 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3010 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3013 /* Common symbols that become definitions don't get the DEF_REGULAR
3014 flag set, so test it first, and don't bail out. */
3015 if (ELF_COMMON_DEF_P (h
))
3017 /* If we don't have a definition in a regular file, then we can't
3018 resolve locally. The sym is either undefined or dynamic. */
3019 else if (!h
->def_regular
)
3022 /* Forced local symbols resolve locally. */
3023 if (h
->forced_local
)
3026 /* As do non-dynamic symbols. */
3027 if (h
->dynindx
== -1)
3030 /* At this point, we know the symbol is defined and dynamic. In an
3031 executable it must resolve locally, likewise when building symbolic
3032 shared libraries. */
3033 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3036 /* Now deal with defined dynamic symbols in shared libraries. Ones
3037 with default visibility might not resolve locally. */
3038 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3041 hash_table
= elf_hash_table (info
);
3042 if (!is_elf_hash_table (hash_table
))
3045 bed
= get_elf_backend_data (hash_table
->dynobj
);
3047 /* If extern_protected_data is false, STV_PROTECTED non-function
3048 symbols are local. */
3049 if ((!info
->extern_protected_data
3050 || (info
->extern_protected_data
< 0
3051 && !bed
->extern_protected_data
))
3052 && !bed
->is_function_type (h
->type
))
3055 /* Function pointer equality tests may require that STV_PROTECTED
3056 symbols be treated as dynamic symbols. If the address of a
3057 function not defined in an executable is set to that function's
3058 plt entry in the executable, then the address of the function in
3059 a shared library must also be the plt entry in the executable. */
3060 return local_protected
;
3063 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3064 aligned. Returns the first TLS output section. */
3066 struct bfd_section
*
3067 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3069 struct bfd_section
*sec
, *tls
;
3070 unsigned int align
= 0;
3072 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3073 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3077 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3078 if (sec
->alignment_power
> align
)
3079 align
= sec
->alignment_power
;
3081 elf_hash_table (info
)->tls_sec
= tls
;
3083 /* Ensure the alignment of the first section is the largest alignment,
3084 so that the tls segment starts aligned. */
3086 tls
->alignment_power
= align
;
3091 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3093 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3094 Elf_Internal_Sym
*sym
)
3096 const struct elf_backend_data
*bed
;
3098 /* Local symbols do not count, but target specific ones might. */
3099 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3100 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3103 bed
= get_elf_backend_data (abfd
);
3104 /* Function symbols do not count. */
3105 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3108 /* If the section is undefined, then so is the symbol. */
3109 if (sym
->st_shndx
== SHN_UNDEF
)
3112 /* If the symbol is defined in the common section, then
3113 it is a common definition and so does not count. */
3114 if (bed
->common_definition (sym
))
3117 /* If the symbol is in a target specific section then we
3118 must rely upon the backend to tell us what it is. */
3119 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3120 /* FIXME - this function is not coded yet:
3122 return _bfd_is_global_symbol_definition (abfd, sym);
3124 Instead for now assume that the definition is not global,
3125 Even if this is wrong, at least the linker will behave
3126 in the same way that it used to do. */
3132 /* Search the symbol table of the archive element of the archive ABFD
3133 whose archive map contains a mention of SYMDEF, and determine if
3134 the symbol is defined in this element. */
3136 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3138 Elf_Internal_Shdr
* hdr
;
3142 Elf_Internal_Sym
*isymbuf
;
3143 Elf_Internal_Sym
*isym
;
3144 Elf_Internal_Sym
*isymend
;
3147 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3151 if (! bfd_check_format (abfd
, bfd_object
))
3154 /* Select the appropriate symbol table. If we don't know if the
3155 object file is an IR object, give linker LTO plugin a chance to
3156 get the correct symbol table. */
3157 if (abfd
->plugin_format
== bfd_plugin_yes
3158 #if BFD_SUPPORTS_PLUGINS
3159 || (abfd
->plugin_format
== bfd_plugin_unknown
3160 && bfd_link_plugin_object_p (abfd
))
3164 /* Use the IR symbol table if the object has been claimed by
3166 abfd
= abfd
->plugin_dummy_bfd
;
3167 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3169 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3170 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3172 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3174 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3176 /* The sh_info field of the symtab header tells us where the
3177 external symbols start. We don't care about the local symbols. */
3178 if (elf_bad_symtab (abfd
))
3180 extsymcount
= symcount
;
3185 extsymcount
= symcount
- hdr
->sh_info
;
3186 extsymoff
= hdr
->sh_info
;
3189 if (extsymcount
== 0)
3192 /* Read in the symbol table. */
3193 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3195 if (isymbuf
== NULL
)
3198 /* Scan the symbol table looking for SYMDEF. */
3200 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3204 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3209 if (strcmp (name
, symdef
->name
) == 0)
3211 result
= is_global_data_symbol_definition (abfd
, isym
);
3221 /* Add an entry to the .dynamic table. */
3224 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3228 struct elf_link_hash_table
*hash_table
;
3229 const struct elf_backend_data
*bed
;
3231 bfd_size_type newsize
;
3232 bfd_byte
*newcontents
;
3233 Elf_Internal_Dyn dyn
;
3235 hash_table
= elf_hash_table (info
);
3236 if (! is_elf_hash_table (hash_table
))
3239 bed
= get_elf_backend_data (hash_table
->dynobj
);
3240 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3241 BFD_ASSERT (s
!= NULL
);
3243 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3244 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3245 if (newcontents
== NULL
)
3249 dyn
.d_un
.d_val
= val
;
3250 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3253 s
->contents
= newcontents
;
3258 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3259 otherwise just check whether one already exists. Returns -1 on error,
3260 1 if a DT_NEEDED tag already exists, and 0 on success. */
3263 elf_add_dt_needed_tag (bfd
*abfd
,
3264 struct bfd_link_info
*info
,
3268 struct elf_link_hash_table
*hash_table
;
3271 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3274 hash_table
= elf_hash_table (info
);
3275 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3276 if (strindex
== (size_t) -1)
3279 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3282 const struct elf_backend_data
*bed
;
3285 bed
= get_elf_backend_data (hash_table
->dynobj
);
3286 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3288 for (extdyn
= sdyn
->contents
;
3289 extdyn
< sdyn
->contents
+ sdyn
->size
;
3290 extdyn
+= bed
->s
->sizeof_dyn
)
3292 Elf_Internal_Dyn dyn
;
3294 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3295 if (dyn
.d_tag
== DT_NEEDED
3296 && dyn
.d_un
.d_val
== strindex
)
3298 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3306 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3309 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3313 /* We were just checking for existence of the tag. */
3314 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3319 /* Return true if SONAME is on the needed list between NEEDED and STOP
3320 (or the end of list if STOP is NULL), and needed by a library that
3324 on_needed_list (const char *soname
,
3325 struct bfd_link_needed_list
*needed
,
3326 struct bfd_link_needed_list
*stop
)
3328 struct bfd_link_needed_list
*look
;
3329 for (look
= needed
; look
!= stop
; look
= look
->next
)
3330 if (strcmp (soname
, look
->name
) == 0
3331 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3332 /* If needed by a library that itself is not directly
3333 needed, recursively check whether that library is
3334 indirectly needed. Since we add DT_NEEDED entries to
3335 the end of the list, library dependencies appear after
3336 the library. Therefore search prior to the current
3337 LOOK, preventing possible infinite recursion. */
3338 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3344 /* Sort symbol by value, section, and size. */
3346 elf_sort_symbol (const void *arg1
, const void *arg2
)
3348 const struct elf_link_hash_entry
*h1
;
3349 const struct elf_link_hash_entry
*h2
;
3350 bfd_signed_vma vdiff
;
3352 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3353 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3354 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3356 return vdiff
> 0 ? 1 : -1;
3359 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3361 return sdiff
> 0 ? 1 : -1;
3363 vdiff
= h1
->size
- h2
->size
;
3364 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3367 /* This function is used to adjust offsets into .dynstr for
3368 dynamic symbols. This is called via elf_link_hash_traverse. */
3371 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3373 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3375 if (h
->dynindx
!= -1)
3376 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3380 /* Assign string offsets in .dynstr, update all structures referencing
3384 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3386 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3387 struct elf_link_local_dynamic_entry
*entry
;
3388 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3389 bfd
*dynobj
= hash_table
->dynobj
;
3392 const struct elf_backend_data
*bed
;
3395 _bfd_elf_strtab_finalize (dynstr
);
3396 size
= _bfd_elf_strtab_size (dynstr
);
3398 bed
= get_elf_backend_data (dynobj
);
3399 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3400 BFD_ASSERT (sdyn
!= NULL
);
3402 /* Update all .dynamic entries referencing .dynstr strings. */
3403 for (extdyn
= sdyn
->contents
;
3404 extdyn
< sdyn
->contents
+ sdyn
->size
;
3405 extdyn
+= bed
->s
->sizeof_dyn
)
3407 Elf_Internal_Dyn dyn
;
3409 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3413 dyn
.d_un
.d_val
= size
;
3423 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3428 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3431 /* Now update local dynamic symbols. */
3432 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3433 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3434 entry
->isym
.st_name
);
3436 /* And the rest of dynamic symbols. */
3437 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3439 /* Adjust version definitions. */
3440 if (elf_tdata (output_bfd
)->cverdefs
)
3445 Elf_Internal_Verdef def
;
3446 Elf_Internal_Verdaux defaux
;
3448 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3452 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3454 p
+= sizeof (Elf_External_Verdef
);
3455 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3457 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3459 _bfd_elf_swap_verdaux_in (output_bfd
,
3460 (Elf_External_Verdaux
*) p
, &defaux
);
3461 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3463 _bfd_elf_swap_verdaux_out (output_bfd
,
3464 &defaux
, (Elf_External_Verdaux
*) p
);
3465 p
+= sizeof (Elf_External_Verdaux
);
3468 while (def
.vd_next
);
3471 /* Adjust version references. */
3472 if (elf_tdata (output_bfd
)->verref
)
3477 Elf_Internal_Verneed need
;
3478 Elf_Internal_Vernaux needaux
;
3480 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3484 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3486 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3487 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3488 (Elf_External_Verneed
*) p
);
3489 p
+= sizeof (Elf_External_Verneed
);
3490 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3492 _bfd_elf_swap_vernaux_in (output_bfd
,
3493 (Elf_External_Vernaux
*) p
, &needaux
);
3494 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3496 _bfd_elf_swap_vernaux_out (output_bfd
,
3498 (Elf_External_Vernaux
*) p
);
3499 p
+= sizeof (Elf_External_Vernaux
);
3502 while (need
.vn_next
);
3508 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3509 The default is to only match when the INPUT and OUTPUT are exactly
3513 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3514 const bfd_target
*output
)
3516 return input
== output
;
3519 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3520 This version is used when different targets for the same architecture
3521 are virtually identical. */
3524 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3525 const bfd_target
*output
)
3527 const struct elf_backend_data
*obed
, *ibed
;
3529 if (input
== output
)
3532 ibed
= xvec_get_elf_backend_data (input
);
3533 obed
= xvec_get_elf_backend_data (output
);
3535 if (ibed
->arch
!= obed
->arch
)
3538 /* If both backends are using this function, deem them compatible. */
3539 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3542 /* Make a special call to the linker "notice" function to tell it that
3543 we are about to handle an as-needed lib, or have finished
3544 processing the lib. */
3547 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3548 struct bfd_link_info
*info
,
3549 enum notice_asneeded_action act
)
3551 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3554 /* Check relocations an ELF object file. */
3557 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3559 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3560 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3562 /* If this object is the same format as the output object, and it is
3563 not a shared library, then let the backend look through the
3566 This is required to build global offset table entries and to
3567 arrange for dynamic relocs. It is not required for the
3568 particular common case of linking non PIC code, even when linking
3569 against shared libraries, but unfortunately there is no way of
3570 knowing whether an object file has been compiled PIC or not.
3571 Looking through the relocs is not particularly time consuming.
3572 The problem is that we must either (1) keep the relocs in memory,
3573 which causes the linker to require additional runtime memory or
3574 (2) read the relocs twice from the input file, which wastes time.
3575 This would be a good case for using mmap.
3577 I have no idea how to handle linking PIC code into a file of a
3578 different format. It probably can't be done. */
3579 if ((abfd
->flags
& DYNAMIC
) == 0
3580 && is_elf_hash_table (htab
)
3581 && bed
->check_relocs
!= NULL
3582 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3583 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3587 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3589 Elf_Internal_Rela
*internal_relocs
;
3592 /* Don't check relocations in excluded sections. */
3593 if ((o
->flags
& SEC_RELOC
) == 0
3594 || (o
->flags
& SEC_EXCLUDE
) != 0
3595 || o
->reloc_count
== 0
3596 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3597 && (o
->flags
& SEC_DEBUGGING
) != 0)
3598 || bfd_is_abs_section (o
->output_section
))
3601 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3603 if (internal_relocs
== NULL
)
3606 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3608 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3609 free (internal_relocs
);
3619 /* Add symbols from an ELF object file to the linker hash table. */
3622 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3624 Elf_Internal_Ehdr
*ehdr
;
3625 Elf_Internal_Shdr
*hdr
;
3629 struct elf_link_hash_entry
**sym_hash
;
3630 bfd_boolean dynamic
;
3631 Elf_External_Versym
*extversym
= NULL
;
3632 Elf_External_Versym
*ever
;
3633 struct elf_link_hash_entry
*weaks
;
3634 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3635 size_t nondeflt_vers_cnt
= 0;
3636 Elf_Internal_Sym
*isymbuf
= NULL
;
3637 Elf_Internal_Sym
*isym
;
3638 Elf_Internal_Sym
*isymend
;
3639 const struct elf_backend_data
*bed
;
3640 bfd_boolean add_needed
;
3641 struct elf_link_hash_table
*htab
;
3643 void *alloc_mark
= NULL
;
3644 struct bfd_hash_entry
**old_table
= NULL
;
3645 unsigned int old_size
= 0;
3646 unsigned int old_count
= 0;
3647 void *old_tab
= NULL
;
3649 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3650 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3651 void *old_strtab
= NULL
;
3654 bfd_boolean just_syms
;
3656 htab
= elf_hash_table (info
);
3657 bed
= get_elf_backend_data (abfd
);
3659 if ((abfd
->flags
& DYNAMIC
) == 0)
3665 /* You can't use -r against a dynamic object. Also, there's no
3666 hope of using a dynamic object which does not exactly match
3667 the format of the output file. */
3668 if (bfd_link_relocatable (info
)
3669 || !is_elf_hash_table (htab
)
3670 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3672 if (bfd_link_relocatable (info
))
3673 bfd_set_error (bfd_error_invalid_operation
);
3675 bfd_set_error (bfd_error_wrong_format
);
3680 ehdr
= elf_elfheader (abfd
);
3681 if (info
->warn_alternate_em
3682 && bed
->elf_machine_code
!= ehdr
->e_machine
3683 && ((bed
->elf_machine_alt1
!= 0
3684 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3685 || (bed
->elf_machine_alt2
!= 0
3686 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3687 info
->callbacks
->einfo
3688 /* xgettext:c-format */
3689 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3690 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3692 /* As a GNU extension, any input sections which are named
3693 .gnu.warning.SYMBOL are treated as warning symbols for the given
3694 symbol. This differs from .gnu.warning sections, which generate
3695 warnings when they are included in an output file. */
3696 /* PR 12761: Also generate this warning when building shared libraries. */
3697 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3701 name
= bfd_get_section_name (abfd
, s
);
3702 if (CONST_STRNEQ (name
, ".gnu.warning."))
3707 name
+= sizeof ".gnu.warning." - 1;
3709 /* If this is a shared object, then look up the symbol
3710 in the hash table. If it is there, and it is already
3711 been defined, then we will not be using the entry
3712 from this shared object, so we don't need to warn.
3713 FIXME: If we see the definition in a regular object
3714 later on, we will warn, but we shouldn't. The only
3715 fix is to keep track of what warnings we are supposed
3716 to emit, and then handle them all at the end of the
3720 struct elf_link_hash_entry
*h
;
3722 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3724 /* FIXME: What about bfd_link_hash_common? */
3726 && (h
->root
.type
== bfd_link_hash_defined
3727 || h
->root
.type
== bfd_link_hash_defweak
))
3732 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3736 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3741 if (! (_bfd_generic_link_add_one_symbol
3742 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3743 FALSE
, bed
->collect
, NULL
)))
3746 if (bfd_link_executable (info
))
3748 /* Clobber the section size so that the warning does
3749 not get copied into the output file. */
3752 /* Also set SEC_EXCLUDE, so that symbols defined in
3753 the warning section don't get copied to the output. */
3754 s
->flags
|= SEC_EXCLUDE
;
3759 just_syms
= ((s
= abfd
->sections
) != NULL
3760 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3765 /* If we are creating a shared library, create all the dynamic
3766 sections immediately. We need to attach them to something,
3767 so we attach them to this BFD, provided it is the right
3768 format and is not from ld --just-symbols. Always create the
3769 dynamic sections for -E/--dynamic-list. FIXME: If there
3770 are no input BFD's of the same format as the output, we can't
3771 make a shared library. */
3773 && (bfd_link_pic (info
)
3774 || (!bfd_link_relocatable (info
)
3775 && (info
->export_dynamic
|| info
->dynamic
)))
3776 && is_elf_hash_table (htab
)
3777 && info
->output_bfd
->xvec
== abfd
->xvec
3778 && !htab
->dynamic_sections_created
)
3780 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3784 else if (!is_elf_hash_table (htab
))
3788 const char *soname
= NULL
;
3790 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3793 /* ld --just-symbols and dynamic objects don't mix very well.
3794 ld shouldn't allow it. */
3798 /* If this dynamic lib was specified on the command line with
3799 --as-needed in effect, then we don't want to add a DT_NEEDED
3800 tag unless the lib is actually used. Similary for libs brought
3801 in by another lib's DT_NEEDED. When --no-add-needed is used
3802 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3803 any dynamic library in DT_NEEDED tags in the dynamic lib at
3805 add_needed
= (elf_dyn_lib_class (abfd
)
3806 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3807 | DYN_NO_NEEDED
)) == 0;
3809 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3814 unsigned int elfsec
;
3815 unsigned long shlink
;
3817 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3824 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3825 if (elfsec
== SHN_BAD
)
3826 goto error_free_dyn
;
3827 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3829 for (extdyn
= dynbuf
;
3830 extdyn
< dynbuf
+ s
->size
;
3831 extdyn
+= bed
->s
->sizeof_dyn
)
3833 Elf_Internal_Dyn dyn
;
3835 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3836 if (dyn
.d_tag
== DT_SONAME
)
3838 unsigned int tagv
= dyn
.d_un
.d_val
;
3839 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3841 goto error_free_dyn
;
3843 if (dyn
.d_tag
== DT_NEEDED
)
3845 struct bfd_link_needed_list
*n
, **pn
;
3847 unsigned int tagv
= dyn
.d_un
.d_val
;
3849 amt
= sizeof (struct bfd_link_needed_list
);
3850 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3851 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3852 if (n
== NULL
|| fnm
== NULL
)
3853 goto error_free_dyn
;
3854 amt
= strlen (fnm
) + 1;
3855 anm
= (char *) bfd_alloc (abfd
, amt
);
3857 goto error_free_dyn
;
3858 memcpy (anm
, fnm
, amt
);
3862 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3866 if (dyn
.d_tag
== DT_RUNPATH
)
3868 struct bfd_link_needed_list
*n
, **pn
;
3870 unsigned int tagv
= dyn
.d_un
.d_val
;
3872 amt
= sizeof (struct bfd_link_needed_list
);
3873 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3874 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3875 if (n
== NULL
|| fnm
== NULL
)
3876 goto error_free_dyn
;
3877 amt
= strlen (fnm
) + 1;
3878 anm
= (char *) bfd_alloc (abfd
, amt
);
3880 goto error_free_dyn
;
3881 memcpy (anm
, fnm
, amt
);
3885 for (pn
= & runpath
;
3891 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3892 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3894 struct bfd_link_needed_list
*n
, **pn
;
3896 unsigned int tagv
= dyn
.d_un
.d_val
;
3898 amt
= sizeof (struct bfd_link_needed_list
);
3899 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3900 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3901 if (n
== NULL
|| fnm
== NULL
)
3902 goto error_free_dyn
;
3903 amt
= strlen (fnm
) + 1;
3904 anm
= (char *) bfd_alloc (abfd
, amt
);
3906 goto error_free_dyn
;
3907 memcpy (anm
, fnm
, amt
);
3917 if (dyn
.d_tag
== DT_AUDIT
)
3919 unsigned int tagv
= dyn
.d_un
.d_val
;
3920 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3927 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3928 frees all more recently bfd_alloc'd blocks as well. */
3934 struct bfd_link_needed_list
**pn
;
3935 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3940 /* We do not want to include any of the sections in a dynamic
3941 object in the output file. We hack by simply clobbering the
3942 list of sections in the BFD. This could be handled more
3943 cleanly by, say, a new section flag; the existing
3944 SEC_NEVER_LOAD flag is not the one we want, because that one
3945 still implies that the section takes up space in the output
3947 bfd_section_list_clear (abfd
);
3949 /* Find the name to use in a DT_NEEDED entry that refers to this
3950 object. If the object has a DT_SONAME entry, we use it.
3951 Otherwise, if the generic linker stuck something in
3952 elf_dt_name, we use that. Otherwise, we just use the file
3954 if (soname
== NULL
|| *soname
== '\0')
3956 soname
= elf_dt_name (abfd
);
3957 if (soname
== NULL
|| *soname
== '\0')
3958 soname
= bfd_get_filename (abfd
);
3961 /* Save the SONAME because sometimes the linker emulation code
3962 will need to know it. */
3963 elf_dt_name (abfd
) = soname
;
3965 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3969 /* If we have already included this dynamic object in the
3970 link, just ignore it. There is no reason to include a
3971 particular dynamic object more than once. */
3975 /* Save the DT_AUDIT entry for the linker emulation code. */
3976 elf_dt_audit (abfd
) = audit
;
3979 /* If this is a dynamic object, we always link against the .dynsym
3980 symbol table, not the .symtab symbol table. The dynamic linker
3981 will only see the .dynsym symbol table, so there is no reason to
3982 look at .symtab for a dynamic object. */
3984 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3985 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3987 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3989 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3991 /* The sh_info field of the symtab header tells us where the
3992 external symbols start. We don't care about the local symbols at
3994 if (elf_bad_symtab (abfd
))
3996 extsymcount
= symcount
;
4001 extsymcount
= symcount
- hdr
->sh_info
;
4002 extsymoff
= hdr
->sh_info
;
4005 sym_hash
= elf_sym_hashes (abfd
);
4006 if (extsymcount
!= 0)
4008 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4010 if (isymbuf
== NULL
)
4013 if (sym_hash
== NULL
)
4015 /* We store a pointer to the hash table entry for each
4018 amt
*= sizeof (struct elf_link_hash_entry
*);
4019 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4020 if (sym_hash
== NULL
)
4021 goto error_free_sym
;
4022 elf_sym_hashes (abfd
) = sym_hash
;
4028 /* Read in any version definitions. */
4029 if (!_bfd_elf_slurp_version_tables (abfd
,
4030 info
->default_imported_symver
))
4031 goto error_free_sym
;
4033 /* Read in the symbol versions, but don't bother to convert them
4034 to internal format. */
4035 if (elf_dynversym (abfd
) != 0)
4037 Elf_Internal_Shdr
*versymhdr
;
4039 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4040 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4041 if (extversym
== NULL
)
4042 goto error_free_sym
;
4043 amt
= versymhdr
->sh_size
;
4044 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4045 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4046 goto error_free_vers
;
4050 /* If we are loading an as-needed shared lib, save the symbol table
4051 state before we start adding symbols. If the lib turns out
4052 to be unneeded, restore the state. */
4053 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4058 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4060 struct bfd_hash_entry
*p
;
4061 struct elf_link_hash_entry
*h
;
4063 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4065 h
= (struct elf_link_hash_entry
*) p
;
4066 entsize
+= htab
->root
.table
.entsize
;
4067 if (h
->root
.type
== bfd_link_hash_warning
)
4068 entsize
+= htab
->root
.table
.entsize
;
4072 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4073 old_tab
= bfd_malloc (tabsize
+ entsize
);
4074 if (old_tab
== NULL
)
4075 goto error_free_vers
;
4077 /* Remember the current objalloc pointer, so that all mem for
4078 symbols added can later be reclaimed. */
4079 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4080 if (alloc_mark
== NULL
)
4081 goto error_free_vers
;
4083 /* Make a special call to the linker "notice" function to
4084 tell it that we are about to handle an as-needed lib. */
4085 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4086 goto error_free_vers
;
4088 /* Clone the symbol table. Remember some pointers into the
4089 symbol table, and dynamic symbol count. */
4090 old_ent
= (char *) old_tab
+ tabsize
;
4091 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4092 old_undefs
= htab
->root
.undefs
;
4093 old_undefs_tail
= htab
->root
.undefs_tail
;
4094 old_table
= htab
->root
.table
.table
;
4095 old_size
= htab
->root
.table
.size
;
4096 old_count
= htab
->root
.table
.count
;
4097 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4098 if (old_strtab
== NULL
)
4099 goto error_free_vers
;
4101 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4103 struct bfd_hash_entry
*p
;
4104 struct elf_link_hash_entry
*h
;
4106 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4108 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4109 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4110 h
= (struct elf_link_hash_entry
*) p
;
4111 if (h
->root
.type
== bfd_link_hash_warning
)
4113 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4114 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4121 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4122 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4124 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4128 asection
*sec
, *new_sec
;
4131 struct elf_link_hash_entry
*h
;
4132 struct elf_link_hash_entry
*hi
;
4133 bfd_boolean definition
;
4134 bfd_boolean size_change_ok
;
4135 bfd_boolean type_change_ok
;
4136 bfd_boolean new_weakdef
;
4137 bfd_boolean new_weak
;
4138 bfd_boolean old_weak
;
4139 bfd_boolean override
;
4141 bfd_boolean discarded
;
4142 unsigned int old_alignment
;
4144 bfd_boolean matched
;
4148 flags
= BSF_NO_FLAGS
;
4150 value
= isym
->st_value
;
4151 common
= bed
->common_definition (isym
);
4154 bind
= ELF_ST_BIND (isym
->st_info
);
4158 /* This should be impossible, since ELF requires that all
4159 global symbols follow all local symbols, and that sh_info
4160 point to the first global symbol. Unfortunately, Irix 5
4165 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4173 case STB_GNU_UNIQUE
:
4174 flags
= BSF_GNU_UNIQUE
;
4178 /* Leave it up to the processor backend. */
4182 if (isym
->st_shndx
== SHN_UNDEF
)
4183 sec
= bfd_und_section_ptr
;
4184 else if (isym
->st_shndx
== SHN_ABS
)
4185 sec
= bfd_abs_section_ptr
;
4186 else if (isym
->st_shndx
== SHN_COMMON
)
4188 sec
= bfd_com_section_ptr
;
4189 /* What ELF calls the size we call the value. What ELF
4190 calls the value we call the alignment. */
4191 value
= isym
->st_size
;
4195 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4197 sec
= bfd_abs_section_ptr
;
4198 else if (discarded_section (sec
))
4200 /* Symbols from discarded section are undefined. We keep
4202 sec
= bfd_und_section_ptr
;
4204 isym
->st_shndx
= SHN_UNDEF
;
4206 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4210 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4213 goto error_free_vers
;
4215 if (isym
->st_shndx
== SHN_COMMON
4216 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4218 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4222 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4224 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4226 goto error_free_vers
;
4230 else if (isym
->st_shndx
== SHN_COMMON
4231 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4232 && !bfd_link_relocatable (info
))
4234 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4238 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4239 | SEC_LINKER_CREATED
);
4240 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4242 goto error_free_vers
;
4246 else if (bed
->elf_add_symbol_hook
)
4248 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4250 goto error_free_vers
;
4252 /* The hook function sets the name to NULL if this symbol
4253 should be skipped for some reason. */
4258 /* Sanity check that all possibilities were handled. */
4261 bfd_set_error (bfd_error_bad_value
);
4262 goto error_free_vers
;
4265 /* Silently discard TLS symbols from --just-syms. There's
4266 no way to combine a static TLS block with a new TLS block
4267 for this executable. */
4268 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4269 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4272 if (bfd_is_und_section (sec
)
4273 || bfd_is_com_section (sec
))
4278 size_change_ok
= FALSE
;
4279 type_change_ok
= bed
->type_change_ok
;
4286 if (is_elf_hash_table (htab
))
4288 Elf_Internal_Versym iver
;
4289 unsigned int vernum
= 0;
4294 if (info
->default_imported_symver
)
4295 /* Use the default symbol version created earlier. */
4296 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4301 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4303 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4305 /* If this is a hidden symbol, or if it is not version
4306 1, we append the version name to the symbol name.
4307 However, we do not modify a non-hidden absolute symbol
4308 if it is not a function, because it might be the version
4309 symbol itself. FIXME: What if it isn't? */
4310 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4312 && (!bfd_is_abs_section (sec
)
4313 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4316 size_t namelen
, verlen
, newlen
;
4319 if (isym
->st_shndx
!= SHN_UNDEF
)
4321 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4323 else if (vernum
> 1)
4325 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4332 /* xgettext:c-format */
4333 (_("%B: %s: invalid version %u (max %d)"),
4335 elf_tdata (abfd
)->cverdefs
);
4336 bfd_set_error (bfd_error_bad_value
);
4337 goto error_free_vers
;
4342 /* We cannot simply test for the number of
4343 entries in the VERNEED section since the
4344 numbers for the needed versions do not start
4346 Elf_Internal_Verneed
*t
;
4349 for (t
= elf_tdata (abfd
)->verref
;
4353 Elf_Internal_Vernaux
*a
;
4355 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4357 if (a
->vna_other
== vernum
)
4359 verstr
= a
->vna_nodename
;
4369 /* xgettext:c-format */
4370 (_("%B: %s: invalid needed version %d"),
4371 abfd
, name
, vernum
);
4372 bfd_set_error (bfd_error_bad_value
);
4373 goto error_free_vers
;
4377 namelen
= strlen (name
);
4378 verlen
= strlen (verstr
);
4379 newlen
= namelen
+ verlen
+ 2;
4380 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4381 && isym
->st_shndx
!= SHN_UNDEF
)
4384 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4385 if (newname
== NULL
)
4386 goto error_free_vers
;
4387 memcpy (newname
, name
, namelen
);
4388 p
= newname
+ namelen
;
4390 /* If this is a defined non-hidden version symbol,
4391 we add another @ to the name. This indicates the
4392 default version of the symbol. */
4393 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4394 && isym
->st_shndx
!= SHN_UNDEF
)
4396 memcpy (p
, verstr
, verlen
+ 1);
4401 /* If this symbol has default visibility and the user has
4402 requested we not re-export it, then mark it as hidden. */
4403 if (!bfd_is_und_section (sec
)
4406 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4407 isym
->st_other
= (STV_HIDDEN
4408 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4410 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4411 sym_hash
, &old_bfd
, &old_weak
,
4412 &old_alignment
, &skip
, &override
,
4413 &type_change_ok
, &size_change_ok
,
4415 goto error_free_vers
;
4420 /* Override a definition only if the new symbol matches the
4422 if (override
&& matched
)
4426 while (h
->root
.type
== bfd_link_hash_indirect
4427 || h
->root
.type
== bfd_link_hash_warning
)
4428 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4430 if (elf_tdata (abfd
)->verdef
!= NULL
4433 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4436 if (! (_bfd_generic_link_add_one_symbol
4437 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4438 (struct bfd_link_hash_entry
**) sym_hash
)))
4439 goto error_free_vers
;
4441 if ((flags
& BSF_GNU_UNIQUE
)
4442 && (abfd
->flags
& DYNAMIC
) == 0
4443 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4444 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4447 /* We need to make sure that indirect symbol dynamic flags are
4450 while (h
->root
.type
== bfd_link_hash_indirect
4451 || h
->root
.type
== bfd_link_hash_warning
)
4452 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4454 /* Setting the index to -3 tells elf_link_output_extsym that
4455 this symbol is defined in a discarded section. */
4461 new_weak
= (flags
& BSF_WEAK
) != 0;
4462 new_weakdef
= FALSE
;
4466 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4467 && is_elf_hash_table (htab
)
4468 && h
->u
.weakdef
== NULL
)
4470 /* Keep a list of all weak defined non function symbols from
4471 a dynamic object, using the weakdef field. Later in this
4472 function we will set the weakdef field to the correct
4473 value. We only put non-function symbols from dynamic
4474 objects on this list, because that happens to be the only
4475 time we need to know the normal symbol corresponding to a
4476 weak symbol, and the information is time consuming to
4477 figure out. If the weakdef field is not already NULL,
4478 then this symbol was already defined by some previous
4479 dynamic object, and we will be using that previous
4480 definition anyhow. */
4482 h
->u
.weakdef
= weaks
;
4487 /* Set the alignment of a common symbol. */
4488 if ((common
|| bfd_is_com_section (sec
))
4489 && h
->root
.type
== bfd_link_hash_common
)
4494 align
= bfd_log2 (isym
->st_value
);
4497 /* The new symbol is a common symbol in a shared object.
4498 We need to get the alignment from the section. */
4499 align
= new_sec
->alignment_power
;
4501 if (align
> old_alignment
)
4502 h
->root
.u
.c
.p
->alignment_power
= align
;
4504 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4507 if (is_elf_hash_table (htab
))
4509 /* Set a flag in the hash table entry indicating the type of
4510 reference or definition we just found. A dynamic symbol
4511 is one which is referenced or defined by both a regular
4512 object and a shared object. */
4513 bfd_boolean dynsym
= FALSE
;
4515 /* Plugin symbols aren't normal. Don't set def_regular or
4516 ref_regular for them, or make them dynamic. */
4517 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4524 if (bind
!= STB_WEAK
)
4525 h
->ref_regular_nonweak
= 1;
4537 /* If the indirect symbol has been forced local, don't
4538 make the real symbol dynamic. */
4539 if ((h
== hi
|| !hi
->forced_local
)
4540 && (bfd_link_dll (info
)
4550 hi
->ref_dynamic
= 1;
4555 hi
->def_dynamic
= 1;
4558 /* If the indirect symbol has been forced local, don't
4559 make the real symbol dynamic. */
4560 if ((h
== hi
|| !hi
->forced_local
)
4563 || (h
->u
.weakdef
!= NULL
4565 && h
->u
.weakdef
->dynindx
!= -1)))
4569 /* Check to see if we need to add an indirect symbol for
4570 the default name. */
4572 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4573 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4574 sec
, value
, &old_bfd
, &dynsym
))
4575 goto error_free_vers
;
4577 /* Check the alignment when a common symbol is involved. This
4578 can change when a common symbol is overridden by a normal
4579 definition or a common symbol is ignored due to the old
4580 normal definition. We need to make sure the maximum
4581 alignment is maintained. */
4582 if ((old_alignment
|| common
)
4583 && h
->root
.type
!= bfd_link_hash_common
)
4585 unsigned int common_align
;
4586 unsigned int normal_align
;
4587 unsigned int symbol_align
;
4591 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4592 || h
->root
.type
== bfd_link_hash_defweak
);
4594 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4595 if (h
->root
.u
.def
.section
->owner
!= NULL
4596 && (h
->root
.u
.def
.section
->owner
->flags
4597 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4599 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4600 if (normal_align
> symbol_align
)
4601 normal_align
= symbol_align
;
4604 normal_align
= symbol_align
;
4608 common_align
= old_alignment
;
4609 common_bfd
= old_bfd
;
4614 common_align
= bfd_log2 (isym
->st_value
);
4616 normal_bfd
= old_bfd
;
4619 if (normal_align
< common_align
)
4621 /* PR binutils/2735 */
4622 if (normal_bfd
== NULL
)
4624 /* xgettext:c-format */
4625 (_("Warning: alignment %u of common symbol `%s' in %B is"
4626 " greater than the alignment (%u) of its section %A"),
4627 common_bfd
, h
->root
.u
.def
.section
,
4628 1 << common_align
, name
, 1 << normal_align
);
4631 /* xgettext:c-format */
4632 (_("Warning: alignment %u of symbol `%s' in %B"
4633 " is smaller than %u in %B"),
4634 normal_bfd
, common_bfd
,
4635 1 << normal_align
, name
, 1 << common_align
);
4639 /* Remember the symbol size if it isn't undefined. */
4640 if (isym
->st_size
!= 0
4641 && isym
->st_shndx
!= SHN_UNDEF
4642 && (definition
|| h
->size
== 0))
4645 && h
->size
!= isym
->st_size
4646 && ! size_change_ok
)
4648 /* xgettext:c-format */
4649 (_("Warning: size of symbol `%s' changed"
4650 " from %lu in %B to %lu in %B"),
4652 name
, (unsigned long) h
->size
,
4653 (unsigned long) isym
->st_size
);
4655 h
->size
= isym
->st_size
;
4658 /* If this is a common symbol, then we always want H->SIZE
4659 to be the size of the common symbol. The code just above
4660 won't fix the size if a common symbol becomes larger. We
4661 don't warn about a size change here, because that is
4662 covered by --warn-common. Allow changes between different
4664 if (h
->root
.type
== bfd_link_hash_common
)
4665 h
->size
= h
->root
.u
.c
.size
;
4667 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4668 && ((definition
&& !new_weak
)
4669 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4670 || h
->type
== STT_NOTYPE
))
4672 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4674 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4676 if (type
== STT_GNU_IFUNC
4677 && (abfd
->flags
& DYNAMIC
) != 0)
4680 if (h
->type
!= type
)
4682 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4683 /* xgettext:c-format */
4685 (_("Warning: type of symbol `%s' changed"
4686 " from %d to %d in %B"),
4687 abfd
, name
, h
->type
, type
);
4693 /* Merge st_other field. */
4694 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4696 /* We don't want to make debug symbol dynamic. */
4698 && (sec
->flags
& SEC_DEBUGGING
)
4699 && !bfd_link_relocatable (info
))
4702 /* Nor should we make plugin symbols dynamic. */
4703 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4708 h
->target_internal
= isym
->st_target_internal
;
4709 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4712 if (definition
&& !dynamic
)
4714 char *p
= strchr (name
, ELF_VER_CHR
);
4715 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4717 /* Queue non-default versions so that .symver x, x@FOO
4718 aliases can be checked. */
4721 amt
= ((isymend
- isym
+ 1)
4722 * sizeof (struct elf_link_hash_entry
*));
4724 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4726 goto error_free_vers
;
4728 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4732 if (dynsym
&& h
->dynindx
== -1)
4734 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4735 goto error_free_vers
;
4736 if (h
->u
.weakdef
!= NULL
4738 && h
->u
.weakdef
->dynindx
== -1)
4740 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4741 goto error_free_vers
;
4744 else if (h
->dynindx
!= -1)
4745 /* If the symbol already has a dynamic index, but
4746 visibility says it should not be visible, turn it into
4748 switch (ELF_ST_VISIBILITY (h
->other
))
4752 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4757 /* Don't add DT_NEEDED for references from the dummy bfd nor
4758 for unmatched symbol. */
4763 && h
->ref_regular_nonweak
4765 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4766 || (h
->ref_dynamic_nonweak
4767 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4768 && !on_needed_list (elf_dt_name (abfd
),
4769 htab
->needed
, NULL
))))
4772 const char *soname
= elf_dt_name (abfd
);
4774 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4775 h
->root
.root
.string
);
4777 /* A symbol from a library loaded via DT_NEEDED of some
4778 other library is referenced by a regular object.
4779 Add a DT_NEEDED entry for it. Issue an error if
4780 --no-add-needed is used and the reference was not
4783 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4786 /* xgettext:c-format */
4787 (_("%B: undefined reference to symbol '%s'"),
4789 bfd_set_error (bfd_error_missing_dso
);
4790 goto error_free_vers
;
4793 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4794 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4797 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4799 goto error_free_vers
;
4801 BFD_ASSERT (ret
== 0);
4806 if (extversym
!= NULL
)
4812 if (isymbuf
!= NULL
)
4818 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4822 /* Restore the symbol table. */
4823 old_ent
= (char *) old_tab
+ tabsize
;
4824 memset (elf_sym_hashes (abfd
), 0,
4825 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4826 htab
->root
.table
.table
= old_table
;
4827 htab
->root
.table
.size
= old_size
;
4828 htab
->root
.table
.count
= old_count
;
4829 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4830 htab
->root
.undefs
= old_undefs
;
4831 htab
->root
.undefs_tail
= old_undefs_tail
;
4832 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4835 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4837 struct bfd_hash_entry
*p
;
4838 struct elf_link_hash_entry
*h
;
4840 unsigned int alignment_power
;
4842 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4844 h
= (struct elf_link_hash_entry
*) p
;
4845 if (h
->root
.type
== bfd_link_hash_warning
)
4846 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4848 /* Preserve the maximum alignment and size for common
4849 symbols even if this dynamic lib isn't on DT_NEEDED
4850 since it can still be loaded at run time by another
4852 if (h
->root
.type
== bfd_link_hash_common
)
4854 size
= h
->root
.u
.c
.size
;
4855 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4860 alignment_power
= 0;
4862 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4863 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4864 h
= (struct elf_link_hash_entry
*) p
;
4865 if (h
->root
.type
== bfd_link_hash_warning
)
4867 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4868 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4869 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4871 if (h
->root
.type
== bfd_link_hash_common
)
4873 if (size
> h
->root
.u
.c
.size
)
4874 h
->root
.u
.c
.size
= size
;
4875 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4876 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4881 /* Make a special call to the linker "notice" function to
4882 tell it that symbols added for crefs may need to be removed. */
4883 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4884 goto error_free_vers
;
4887 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4889 if (nondeflt_vers
!= NULL
)
4890 free (nondeflt_vers
);
4894 if (old_tab
!= NULL
)
4896 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4897 goto error_free_vers
;
4902 /* Now that all the symbols from this input file are created, if
4903 not performing a relocatable link, handle .symver foo, foo@BAR
4904 such that any relocs against foo become foo@BAR. */
4905 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4909 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4911 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4912 char *shortname
, *p
;
4914 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4916 || (h
->root
.type
!= bfd_link_hash_defined
4917 && h
->root
.type
!= bfd_link_hash_defweak
))
4920 amt
= p
- h
->root
.root
.string
;
4921 shortname
= (char *) bfd_malloc (amt
+ 1);
4923 goto error_free_vers
;
4924 memcpy (shortname
, h
->root
.root
.string
, amt
);
4925 shortname
[amt
] = '\0';
4927 hi
= (struct elf_link_hash_entry
*)
4928 bfd_link_hash_lookup (&htab
->root
, shortname
,
4929 FALSE
, FALSE
, FALSE
);
4931 && hi
->root
.type
== h
->root
.type
4932 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4933 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4935 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4936 hi
->root
.type
= bfd_link_hash_indirect
;
4937 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4938 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4939 sym_hash
= elf_sym_hashes (abfd
);
4941 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4942 if (sym_hash
[symidx
] == hi
)
4944 sym_hash
[symidx
] = h
;
4950 free (nondeflt_vers
);
4951 nondeflt_vers
= NULL
;
4954 /* Now set the weakdefs field correctly for all the weak defined
4955 symbols we found. The only way to do this is to search all the
4956 symbols. Since we only need the information for non functions in
4957 dynamic objects, that's the only time we actually put anything on
4958 the list WEAKS. We need this information so that if a regular
4959 object refers to a symbol defined weakly in a dynamic object, the
4960 real symbol in the dynamic object is also put in the dynamic
4961 symbols; we also must arrange for both symbols to point to the
4962 same memory location. We could handle the general case of symbol
4963 aliasing, but a general symbol alias can only be generated in
4964 assembler code, handling it correctly would be very time
4965 consuming, and other ELF linkers don't handle general aliasing
4969 struct elf_link_hash_entry
**hpp
;
4970 struct elf_link_hash_entry
**hppend
;
4971 struct elf_link_hash_entry
**sorted_sym_hash
;
4972 struct elf_link_hash_entry
*h
;
4975 /* Since we have to search the whole symbol list for each weak
4976 defined symbol, search time for N weak defined symbols will be
4977 O(N^2). Binary search will cut it down to O(NlogN). */
4979 amt
*= sizeof (struct elf_link_hash_entry
*);
4980 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4981 if (sorted_sym_hash
== NULL
)
4983 sym_hash
= sorted_sym_hash
;
4984 hpp
= elf_sym_hashes (abfd
);
4985 hppend
= hpp
+ extsymcount
;
4987 for (; hpp
< hppend
; hpp
++)
4991 && h
->root
.type
== bfd_link_hash_defined
4992 && !bed
->is_function_type (h
->type
))
5000 qsort (sorted_sym_hash
, sym_count
,
5001 sizeof (struct elf_link_hash_entry
*),
5004 while (weaks
!= NULL
)
5006 struct elf_link_hash_entry
*hlook
;
5009 size_t i
, j
, idx
= 0;
5012 weaks
= hlook
->u
.weakdef
;
5013 hlook
->u
.weakdef
= NULL
;
5015 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5016 || hlook
->root
.type
== bfd_link_hash_defweak
5017 || hlook
->root
.type
== bfd_link_hash_common
5018 || hlook
->root
.type
== bfd_link_hash_indirect
);
5019 slook
= hlook
->root
.u
.def
.section
;
5020 vlook
= hlook
->root
.u
.def
.value
;
5026 bfd_signed_vma vdiff
;
5028 h
= sorted_sym_hash
[idx
];
5029 vdiff
= vlook
- h
->root
.u
.def
.value
;
5036 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5046 /* We didn't find a value/section match. */
5050 /* With multiple aliases, or when the weak symbol is already
5051 strongly defined, we have multiple matching symbols and
5052 the binary search above may land on any of them. Step
5053 one past the matching symbol(s). */
5056 h
= sorted_sym_hash
[idx
];
5057 if (h
->root
.u
.def
.section
!= slook
5058 || h
->root
.u
.def
.value
!= vlook
)
5062 /* Now look back over the aliases. Since we sorted by size
5063 as well as value and section, we'll choose the one with
5064 the largest size. */
5067 h
= sorted_sym_hash
[idx
];
5069 /* Stop if value or section doesn't match. */
5070 if (h
->root
.u
.def
.section
!= slook
5071 || h
->root
.u
.def
.value
!= vlook
)
5073 else if (h
!= hlook
)
5075 hlook
->u
.weakdef
= h
;
5077 /* If the weak definition is in the list of dynamic
5078 symbols, make sure the real definition is put
5080 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5082 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5085 free (sorted_sym_hash
);
5090 /* If the real definition is in the list of dynamic
5091 symbols, make sure the weak definition is put
5092 there as well. If we don't do this, then the
5093 dynamic loader might not merge the entries for the
5094 real definition and the weak definition. */
5095 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5097 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5098 goto err_free_sym_hash
;
5105 free (sorted_sym_hash
);
5108 if (bed
->check_directives
5109 && !(*bed
->check_directives
) (abfd
, info
))
5112 if (!info
->check_relocs_after_open_input
5113 && !_bfd_elf_link_check_relocs (abfd
, info
))
5116 /* If this is a non-traditional link, try to optimize the handling
5117 of the .stab/.stabstr sections. */
5119 && ! info
->traditional_format
5120 && is_elf_hash_table (htab
)
5121 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5125 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5126 if (stabstr
!= NULL
)
5128 bfd_size_type string_offset
= 0;
5131 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5132 if (CONST_STRNEQ (stab
->name
, ".stab")
5133 && (!stab
->name
[5] ||
5134 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5135 && (stab
->flags
& SEC_MERGE
) == 0
5136 && !bfd_is_abs_section (stab
->output_section
))
5138 struct bfd_elf_section_data
*secdata
;
5140 secdata
= elf_section_data (stab
);
5141 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5142 stabstr
, &secdata
->sec_info
,
5145 if (secdata
->sec_info
)
5146 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5151 if (is_elf_hash_table (htab
) && add_needed
)
5153 /* Add this bfd to the loaded list. */
5154 struct elf_link_loaded_list
*n
;
5156 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5160 n
->next
= htab
->loaded
;
5167 if (old_tab
!= NULL
)
5169 if (old_strtab
!= NULL
)
5171 if (nondeflt_vers
!= NULL
)
5172 free (nondeflt_vers
);
5173 if (extversym
!= NULL
)
5176 if (isymbuf
!= NULL
)
5182 /* Return the linker hash table entry of a symbol that might be
5183 satisfied by an archive symbol. Return -1 on error. */
5185 struct elf_link_hash_entry
*
5186 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5187 struct bfd_link_info
*info
,
5190 struct elf_link_hash_entry
*h
;
5194 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5198 /* If this is a default version (the name contains @@), look up the
5199 symbol again with only one `@' as well as without the version.
5200 The effect is that references to the symbol with and without the
5201 version will be matched by the default symbol in the archive. */
5203 p
= strchr (name
, ELF_VER_CHR
);
5204 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5207 /* First check with only one `@'. */
5208 len
= strlen (name
);
5209 copy
= (char *) bfd_alloc (abfd
, len
);
5211 return (struct elf_link_hash_entry
*) 0 - 1;
5213 first
= p
- name
+ 1;
5214 memcpy (copy
, name
, first
);
5215 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5217 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5220 /* We also need to check references to the symbol without the
5222 copy
[first
- 1] = '\0';
5223 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5224 FALSE
, FALSE
, TRUE
);
5227 bfd_release (abfd
, copy
);
5231 /* Add symbols from an ELF archive file to the linker hash table. We
5232 don't use _bfd_generic_link_add_archive_symbols because we need to
5233 handle versioned symbols.
5235 Fortunately, ELF archive handling is simpler than that done by
5236 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5237 oddities. In ELF, if we find a symbol in the archive map, and the
5238 symbol is currently undefined, we know that we must pull in that
5241 Unfortunately, we do have to make multiple passes over the symbol
5242 table until nothing further is resolved. */
5245 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5248 unsigned char *included
= NULL
;
5252 const struct elf_backend_data
*bed
;
5253 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5254 (bfd
*, struct bfd_link_info
*, const char *);
5256 if (! bfd_has_map (abfd
))
5258 /* An empty archive is a special case. */
5259 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5261 bfd_set_error (bfd_error_no_armap
);
5265 /* Keep track of all symbols we know to be already defined, and all
5266 files we know to be already included. This is to speed up the
5267 second and subsequent passes. */
5268 c
= bfd_ardata (abfd
)->symdef_count
;
5272 amt
*= sizeof (*included
);
5273 included
= (unsigned char *) bfd_zmalloc (amt
);
5274 if (included
== NULL
)
5277 symdefs
= bfd_ardata (abfd
)->symdefs
;
5278 bed
= get_elf_backend_data (abfd
);
5279 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5292 symdefend
= symdef
+ c
;
5293 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5295 struct elf_link_hash_entry
*h
;
5297 struct bfd_link_hash_entry
*undefs_tail
;
5302 if (symdef
->file_offset
== last
)
5308 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5309 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5315 if (h
->root
.type
== bfd_link_hash_common
)
5317 /* We currently have a common symbol. The archive map contains
5318 a reference to this symbol, so we may want to include it. We
5319 only want to include it however, if this archive element
5320 contains a definition of the symbol, not just another common
5323 Unfortunately some archivers (including GNU ar) will put
5324 declarations of common symbols into their archive maps, as
5325 well as real definitions, so we cannot just go by the archive
5326 map alone. Instead we must read in the element's symbol
5327 table and check that to see what kind of symbol definition
5329 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5332 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5334 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5335 /* Symbol must be defined. Don't check it again. */
5340 /* We need to include this archive member. */
5341 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5342 if (element
== NULL
)
5345 if (! bfd_check_format (element
, bfd_object
))
5348 undefs_tail
= info
->hash
->undefs_tail
;
5350 if (!(*info
->callbacks
5351 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5353 if (!bfd_link_add_symbols (element
, info
))
5356 /* If there are any new undefined symbols, we need to make
5357 another pass through the archive in order to see whether
5358 they can be defined. FIXME: This isn't perfect, because
5359 common symbols wind up on undefs_tail and because an
5360 undefined symbol which is defined later on in this pass
5361 does not require another pass. This isn't a bug, but it
5362 does make the code less efficient than it could be. */
5363 if (undefs_tail
!= info
->hash
->undefs_tail
)
5366 /* Look backward to mark all symbols from this object file
5367 which we have already seen in this pass. */
5371 included
[mark
] = TRUE
;
5376 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5378 /* We mark subsequent symbols from this object file as we go
5379 on through the loop. */
5380 last
= symdef
->file_offset
;
5390 if (included
!= NULL
)
5395 /* Given an ELF BFD, add symbols to the global hash table as
5399 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5401 switch (bfd_get_format (abfd
))
5404 return elf_link_add_object_symbols (abfd
, info
);
5406 return elf_link_add_archive_symbols (abfd
, info
);
5408 bfd_set_error (bfd_error_wrong_format
);
5413 struct hash_codes_info
5415 unsigned long *hashcodes
;
5419 /* This function will be called though elf_link_hash_traverse to store
5420 all hash value of the exported symbols in an array. */
5423 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5425 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5430 /* Ignore indirect symbols. These are added by the versioning code. */
5431 if (h
->dynindx
== -1)
5434 name
= h
->root
.root
.string
;
5435 if (h
->versioned
>= versioned
)
5437 char *p
= strchr (name
, ELF_VER_CHR
);
5440 alc
= (char *) bfd_malloc (p
- name
+ 1);
5446 memcpy (alc
, name
, p
- name
);
5447 alc
[p
- name
] = '\0';
5452 /* Compute the hash value. */
5453 ha
= bfd_elf_hash (name
);
5455 /* Store the found hash value in the array given as the argument. */
5456 *(inf
->hashcodes
)++ = ha
;
5458 /* And store it in the struct so that we can put it in the hash table
5460 h
->u
.elf_hash_value
= ha
;
5468 struct collect_gnu_hash_codes
5471 const struct elf_backend_data
*bed
;
5472 unsigned long int nsyms
;
5473 unsigned long int maskbits
;
5474 unsigned long int *hashcodes
;
5475 unsigned long int *hashval
;
5476 unsigned long int *indx
;
5477 unsigned long int *counts
;
5480 long int min_dynindx
;
5481 unsigned long int bucketcount
;
5482 unsigned long int symindx
;
5483 long int local_indx
;
5484 long int shift1
, shift2
;
5485 unsigned long int mask
;
5489 /* This function will be called though elf_link_hash_traverse to store
5490 all hash value of the exported symbols in an array. */
5493 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5495 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5500 /* Ignore indirect symbols. These are added by the versioning code. */
5501 if (h
->dynindx
== -1)
5504 /* Ignore also local symbols and undefined symbols. */
5505 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5508 name
= h
->root
.root
.string
;
5509 if (h
->versioned
>= versioned
)
5511 char *p
= strchr (name
, ELF_VER_CHR
);
5514 alc
= (char *) bfd_malloc (p
- name
+ 1);
5520 memcpy (alc
, name
, p
- name
);
5521 alc
[p
- name
] = '\0';
5526 /* Compute the hash value. */
5527 ha
= bfd_elf_gnu_hash (name
);
5529 /* Store the found hash value in the array for compute_bucket_count,
5530 and also for .dynsym reordering purposes. */
5531 s
->hashcodes
[s
->nsyms
] = ha
;
5532 s
->hashval
[h
->dynindx
] = ha
;
5534 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5535 s
->min_dynindx
= h
->dynindx
;
5543 /* This function will be called though elf_link_hash_traverse to do
5544 final dynaminc symbol renumbering. */
5547 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5549 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5550 unsigned long int bucket
;
5551 unsigned long int val
;
5553 /* Ignore indirect symbols. */
5554 if (h
->dynindx
== -1)
5557 /* Ignore also local symbols and undefined symbols. */
5558 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5560 if (h
->dynindx
>= s
->min_dynindx
)
5561 h
->dynindx
= s
->local_indx
++;
5565 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5566 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5567 & ((s
->maskbits
>> s
->shift1
) - 1);
5568 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5570 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5571 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5572 if (s
->counts
[bucket
] == 1)
5573 /* Last element terminates the chain. */
5575 bfd_put_32 (s
->output_bfd
, val
,
5576 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5577 --s
->counts
[bucket
];
5578 h
->dynindx
= s
->indx
[bucket
]++;
5582 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5585 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5587 return !(h
->forced_local
5588 || h
->root
.type
== bfd_link_hash_undefined
5589 || h
->root
.type
== bfd_link_hash_undefweak
5590 || ((h
->root
.type
== bfd_link_hash_defined
5591 || h
->root
.type
== bfd_link_hash_defweak
)
5592 && h
->root
.u
.def
.section
->output_section
== NULL
));
5595 /* Array used to determine the number of hash table buckets to use
5596 based on the number of symbols there are. If there are fewer than
5597 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5598 fewer than 37 we use 17 buckets, and so forth. We never use more
5599 than 32771 buckets. */
5601 static const size_t elf_buckets
[] =
5603 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5607 /* Compute bucket count for hashing table. We do not use a static set
5608 of possible tables sizes anymore. Instead we determine for all
5609 possible reasonable sizes of the table the outcome (i.e., the
5610 number of collisions etc) and choose the best solution. The
5611 weighting functions are not too simple to allow the table to grow
5612 without bounds. Instead one of the weighting factors is the size.
5613 Therefore the result is always a good payoff between few collisions
5614 (= short chain lengths) and table size. */
5616 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5617 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5618 unsigned long int nsyms
,
5621 size_t best_size
= 0;
5622 unsigned long int i
;
5624 /* We have a problem here. The following code to optimize the table
5625 size requires an integer type with more the 32 bits. If
5626 BFD_HOST_U_64_BIT is set we know about such a type. */
5627 #ifdef BFD_HOST_U_64_BIT
5632 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5633 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5634 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5635 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5636 unsigned long int *counts
;
5638 unsigned int no_improvement_count
= 0;
5640 /* Possible optimization parameters: if we have NSYMS symbols we say
5641 that the hashing table must at least have NSYMS/4 and at most
5643 minsize
= nsyms
/ 4;
5646 best_size
= maxsize
= nsyms
* 2;
5651 if ((best_size
& 31) == 0)
5655 /* Create array where we count the collisions in. We must use bfd_malloc
5656 since the size could be large. */
5658 amt
*= sizeof (unsigned long int);
5659 counts
= (unsigned long int *) bfd_malloc (amt
);
5663 /* Compute the "optimal" size for the hash table. The criteria is a
5664 minimal chain length. The minor criteria is (of course) the size
5666 for (i
= minsize
; i
< maxsize
; ++i
)
5668 /* Walk through the array of hashcodes and count the collisions. */
5669 BFD_HOST_U_64_BIT max
;
5670 unsigned long int j
;
5671 unsigned long int fact
;
5673 if (gnu_hash
&& (i
& 31) == 0)
5676 memset (counts
, '\0', i
* sizeof (unsigned long int));
5678 /* Determine how often each hash bucket is used. */
5679 for (j
= 0; j
< nsyms
; ++j
)
5680 ++counts
[hashcodes
[j
] % i
];
5682 /* For the weight function we need some information about the
5683 pagesize on the target. This is information need not be 100%
5684 accurate. Since this information is not available (so far) we
5685 define it here to a reasonable default value. If it is crucial
5686 to have a better value some day simply define this value. */
5687 # ifndef BFD_TARGET_PAGESIZE
5688 # define BFD_TARGET_PAGESIZE (4096)
5691 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5693 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5696 /* Variant 1: optimize for short chains. We add the squares
5697 of all the chain lengths (which favors many small chain
5698 over a few long chains). */
5699 for (j
= 0; j
< i
; ++j
)
5700 max
+= counts
[j
] * counts
[j
];
5702 /* This adds penalties for the overall size of the table. */
5703 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5706 /* Variant 2: Optimize a lot more for small table. Here we
5707 also add squares of the size but we also add penalties for
5708 empty slots (the +1 term). */
5709 for (j
= 0; j
< i
; ++j
)
5710 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5712 /* The overall size of the table is considered, but not as
5713 strong as in variant 1, where it is squared. */
5714 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5718 /* Compare with current best results. */
5719 if (max
< best_chlen
)
5723 no_improvement_count
= 0;
5725 /* PR 11843: Avoid futile long searches for the best bucket size
5726 when there are a large number of symbols. */
5727 else if (++no_improvement_count
== 100)
5734 #endif /* defined (BFD_HOST_U_64_BIT) */
5736 /* This is the fallback solution if no 64bit type is available or if we
5737 are not supposed to spend much time on optimizations. We select the
5738 bucket count using a fixed set of numbers. */
5739 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5741 best_size
= elf_buckets
[i
];
5742 if (nsyms
< elf_buckets
[i
+ 1])
5745 if (gnu_hash
&& best_size
< 2)
5752 /* Size any SHT_GROUP section for ld -r. */
5755 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5759 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5760 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5761 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5766 /* Set a default stack segment size. The value in INFO wins. If it
5767 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5768 undefined it is initialized. */
5771 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5772 struct bfd_link_info
*info
,
5773 const char *legacy_symbol
,
5774 bfd_vma default_size
)
5776 struct elf_link_hash_entry
*h
= NULL
;
5778 /* Look for legacy symbol. */
5780 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5781 FALSE
, FALSE
, FALSE
);
5782 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5783 || h
->root
.type
== bfd_link_hash_defweak
)
5785 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5787 /* The symbol has no type if specified on the command line. */
5788 h
->type
= STT_OBJECT
;
5789 if (info
->stacksize
)
5790 /* xgettext:c-format */
5791 _bfd_error_handler (_("%B: stack size specified and %s set"),
5792 output_bfd
, legacy_symbol
);
5793 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5794 /* xgettext:c-format */
5795 _bfd_error_handler (_("%B: %s not absolute"),
5796 output_bfd
, legacy_symbol
);
5798 info
->stacksize
= h
->root
.u
.def
.value
;
5801 if (!info
->stacksize
)
5802 /* If the user didn't set a size, or explicitly inhibit the
5803 size, set it now. */
5804 info
->stacksize
= default_size
;
5806 /* Provide the legacy symbol, if it is referenced. */
5807 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5808 || h
->root
.type
== bfd_link_hash_undefweak
))
5810 struct bfd_link_hash_entry
*bh
= NULL
;
5812 if (!(_bfd_generic_link_add_one_symbol
5813 (info
, output_bfd
, legacy_symbol
,
5814 BSF_GLOBAL
, bfd_abs_section_ptr
,
5815 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5816 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5819 h
= (struct elf_link_hash_entry
*) bh
;
5821 h
->type
= STT_OBJECT
;
5827 /* Set up the sizes and contents of the ELF dynamic sections. This is
5828 called by the ELF linker emulation before_allocation routine. We
5829 must set the sizes of the sections before the linker sets the
5830 addresses of the various sections. */
5833 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5836 const char *filter_shlib
,
5838 const char *depaudit
,
5839 const char * const *auxiliary_filters
,
5840 struct bfd_link_info
*info
,
5841 asection
**sinterpptr
)
5845 const struct elf_backend_data
*bed
;
5846 struct elf_info_failed asvinfo
;
5850 soname_indx
= (size_t) -1;
5852 if (!is_elf_hash_table (info
->hash
))
5855 bed
= get_elf_backend_data (output_bfd
);
5857 /* Any syms created from now on start with -1 in
5858 got.refcount/offset and plt.refcount/offset. */
5859 elf_hash_table (info
)->init_got_refcount
5860 = elf_hash_table (info
)->init_got_offset
;
5861 elf_hash_table (info
)->init_plt_refcount
5862 = elf_hash_table (info
)->init_plt_offset
;
5864 if (bfd_link_relocatable (info
)
5865 && !_bfd_elf_size_group_sections (info
))
5868 /* The backend may have to create some sections regardless of whether
5869 we're dynamic or not. */
5870 if (bed
->elf_backend_always_size_sections
5871 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5874 /* Determine any GNU_STACK segment requirements, after the backend
5875 has had a chance to set a default segment size. */
5876 if (info
->execstack
)
5877 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5878 else if (info
->noexecstack
)
5879 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5883 asection
*notesec
= NULL
;
5886 for (inputobj
= info
->input_bfds
;
5888 inputobj
= inputobj
->link
.next
)
5893 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5895 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5898 if (s
->flags
& SEC_CODE
)
5902 else if (bed
->default_execstack
)
5905 if (notesec
|| info
->stacksize
> 0)
5906 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5907 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5908 && notesec
->output_section
!= bfd_abs_section_ptr
)
5909 notesec
->output_section
->flags
|= SEC_CODE
;
5912 dynobj
= elf_hash_table (info
)->dynobj
;
5914 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5916 struct elf_info_failed eif
;
5917 struct elf_link_hash_entry
*h
;
5919 struct bfd_elf_version_tree
*t
;
5920 struct bfd_elf_version_expr
*d
;
5922 bfd_boolean all_defined
;
5924 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5925 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5929 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5931 if (soname_indx
== (size_t) -1
5932 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5938 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5940 info
->flags
|= DF_SYMBOLIC
;
5948 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5950 if (indx
== (size_t) -1)
5953 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5954 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5958 if (filter_shlib
!= NULL
)
5962 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5963 filter_shlib
, TRUE
);
5964 if (indx
== (size_t) -1
5965 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5969 if (auxiliary_filters
!= NULL
)
5971 const char * const *p
;
5973 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5977 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5979 if (indx
== (size_t) -1
5980 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5989 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5991 if (indx
== (size_t) -1
5992 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5996 if (depaudit
!= NULL
)
6000 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6002 if (indx
== (size_t) -1
6003 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6010 /* If we are supposed to export all symbols into the dynamic symbol
6011 table (this is not the normal case), then do so. */
6012 if (info
->export_dynamic
6013 || (bfd_link_executable (info
) && info
->dynamic
))
6015 elf_link_hash_traverse (elf_hash_table (info
),
6016 _bfd_elf_export_symbol
,
6022 /* Make all global versions with definition. */
6023 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6024 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6025 if (!d
->symver
&& d
->literal
)
6027 const char *verstr
, *name
;
6028 size_t namelen
, verlen
, newlen
;
6029 char *newname
, *p
, leading_char
;
6030 struct elf_link_hash_entry
*newh
;
6032 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6034 namelen
= strlen (name
) + (leading_char
!= '\0');
6036 verlen
= strlen (verstr
);
6037 newlen
= namelen
+ verlen
+ 3;
6039 newname
= (char *) bfd_malloc (newlen
);
6040 if (newname
== NULL
)
6042 newname
[0] = leading_char
;
6043 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6045 /* Check the hidden versioned definition. */
6046 p
= newname
+ namelen
;
6048 memcpy (p
, verstr
, verlen
+ 1);
6049 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6050 newname
, FALSE
, FALSE
,
6053 || (newh
->root
.type
!= bfd_link_hash_defined
6054 && newh
->root
.type
!= bfd_link_hash_defweak
))
6056 /* Check the default versioned definition. */
6058 memcpy (p
, verstr
, verlen
+ 1);
6059 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6060 newname
, FALSE
, FALSE
,
6065 /* Mark this version if there is a definition and it is
6066 not defined in a shared object. */
6068 && !newh
->def_dynamic
6069 && (newh
->root
.type
== bfd_link_hash_defined
6070 || newh
->root
.type
== bfd_link_hash_defweak
))
6074 /* Attach all the symbols to their version information. */
6075 asvinfo
.info
= info
;
6076 asvinfo
.failed
= FALSE
;
6078 elf_link_hash_traverse (elf_hash_table (info
),
6079 _bfd_elf_link_assign_sym_version
,
6084 if (!info
->allow_undefined_version
)
6086 /* Check if all global versions have a definition. */
6088 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6089 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6090 if (d
->literal
&& !d
->symver
&& !d
->script
)
6093 (_("%s: undefined version: %s"),
6094 d
->pattern
, t
->name
);
6095 all_defined
= FALSE
;
6100 bfd_set_error (bfd_error_bad_value
);
6105 /* Find all symbols which were defined in a dynamic object and make
6106 the backend pick a reasonable value for them. */
6107 elf_link_hash_traverse (elf_hash_table (info
),
6108 _bfd_elf_adjust_dynamic_symbol
,
6113 /* Add some entries to the .dynamic section. We fill in some of the
6114 values later, in bfd_elf_final_link, but we must add the entries
6115 now so that we know the final size of the .dynamic section. */
6117 /* If there are initialization and/or finalization functions to
6118 call then add the corresponding DT_INIT/DT_FINI entries. */
6119 h
= (info
->init_function
6120 ? elf_link_hash_lookup (elf_hash_table (info
),
6121 info
->init_function
, FALSE
,
6128 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6131 h
= (info
->fini_function
6132 ? elf_link_hash_lookup (elf_hash_table (info
),
6133 info
->fini_function
, FALSE
,
6140 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6144 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6145 if (s
!= NULL
&& s
->linker_has_input
)
6147 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6148 if (! bfd_link_executable (info
))
6153 for (sub
= info
->input_bfds
; sub
!= NULL
;
6154 sub
= sub
->link
.next
)
6155 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6156 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6157 if (elf_section_data (o
)->this_hdr
.sh_type
6158 == SHT_PREINIT_ARRAY
)
6161 (_("%B: .preinit_array section is not allowed in DSO"),
6166 bfd_set_error (bfd_error_nonrepresentable_section
);
6170 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6171 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6174 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6175 if (s
!= NULL
&& s
->linker_has_input
)
6177 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6178 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6181 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6182 if (s
!= NULL
&& s
->linker_has_input
)
6184 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6185 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6189 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6190 /* If .dynstr is excluded from the link, we don't want any of
6191 these tags. Strictly, we should be checking each section
6192 individually; This quick check covers for the case where
6193 someone does a /DISCARD/ : { *(*) }. */
6194 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6196 bfd_size_type strsize
;
6198 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6199 if ((info
->emit_hash
6200 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6201 || (info
->emit_gnu_hash
6202 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6203 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6204 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6205 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6206 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6207 bed
->s
->sizeof_sym
))
6212 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6215 /* The backend must work out the sizes of all the other dynamic
6218 && bed
->elf_backend_size_dynamic_sections
!= NULL
6219 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6222 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6224 unsigned long section_sym_count
;
6225 struct bfd_elf_version_tree
*verdefs
;
6228 /* Set up the version definition section. */
6229 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6230 BFD_ASSERT (s
!= NULL
);
6232 /* We may have created additional version definitions if we are
6233 just linking a regular application. */
6234 verdefs
= info
->version_info
;
6236 /* Skip anonymous version tag. */
6237 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6238 verdefs
= verdefs
->next
;
6240 if (verdefs
== NULL
&& !info
->create_default_symver
)
6241 s
->flags
|= SEC_EXCLUDE
;
6246 struct bfd_elf_version_tree
*t
;
6248 Elf_Internal_Verdef def
;
6249 Elf_Internal_Verdaux defaux
;
6250 struct bfd_link_hash_entry
*bh
;
6251 struct elf_link_hash_entry
*h
;
6257 /* Make space for the base version. */
6258 size
+= sizeof (Elf_External_Verdef
);
6259 size
+= sizeof (Elf_External_Verdaux
);
6262 /* Make space for the default version. */
6263 if (info
->create_default_symver
)
6265 size
+= sizeof (Elf_External_Verdef
);
6269 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6271 struct bfd_elf_version_deps
*n
;
6273 /* Don't emit base version twice. */
6277 size
+= sizeof (Elf_External_Verdef
);
6278 size
+= sizeof (Elf_External_Verdaux
);
6281 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6282 size
+= sizeof (Elf_External_Verdaux
);
6286 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6287 if (s
->contents
== NULL
&& s
->size
!= 0)
6290 /* Fill in the version definition section. */
6294 def
.vd_version
= VER_DEF_CURRENT
;
6295 def
.vd_flags
= VER_FLG_BASE
;
6298 if (info
->create_default_symver
)
6300 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6301 def
.vd_next
= sizeof (Elf_External_Verdef
);
6305 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6306 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6307 + sizeof (Elf_External_Verdaux
));
6310 if (soname_indx
!= (size_t) -1)
6312 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6314 def
.vd_hash
= bfd_elf_hash (soname
);
6315 defaux
.vda_name
= soname_indx
;
6322 name
= lbasename (output_bfd
->filename
);
6323 def
.vd_hash
= bfd_elf_hash (name
);
6324 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6326 if (indx
== (size_t) -1)
6328 defaux
.vda_name
= indx
;
6330 defaux
.vda_next
= 0;
6332 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6333 (Elf_External_Verdef
*) p
);
6334 p
+= sizeof (Elf_External_Verdef
);
6335 if (info
->create_default_symver
)
6337 /* Add a symbol representing this version. */
6339 if (! (_bfd_generic_link_add_one_symbol
6340 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6342 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6344 h
= (struct elf_link_hash_entry
*) bh
;
6347 h
->type
= STT_OBJECT
;
6348 h
->verinfo
.vertree
= NULL
;
6350 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6353 /* Create a duplicate of the base version with the same
6354 aux block, but different flags. */
6357 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6359 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6360 + sizeof (Elf_External_Verdaux
));
6363 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6364 (Elf_External_Verdef
*) p
);
6365 p
+= sizeof (Elf_External_Verdef
);
6367 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6368 (Elf_External_Verdaux
*) p
);
6369 p
+= sizeof (Elf_External_Verdaux
);
6371 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6374 struct bfd_elf_version_deps
*n
;
6376 /* Don't emit the base version twice. */
6381 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6384 /* Add a symbol representing this version. */
6386 if (! (_bfd_generic_link_add_one_symbol
6387 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6389 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6391 h
= (struct elf_link_hash_entry
*) bh
;
6394 h
->type
= STT_OBJECT
;
6395 h
->verinfo
.vertree
= t
;
6397 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6400 def
.vd_version
= VER_DEF_CURRENT
;
6402 if (t
->globals
.list
== NULL
6403 && t
->locals
.list
== NULL
6405 def
.vd_flags
|= VER_FLG_WEAK
;
6406 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6407 def
.vd_cnt
= cdeps
+ 1;
6408 def
.vd_hash
= bfd_elf_hash (t
->name
);
6409 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6412 /* If a basever node is next, it *must* be the last node in
6413 the chain, otherwise Verdef construction breaks. */
6414 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6415 BFD_ASSERT (t
->next
->next
== NULL
);
6417 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6418 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6419 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6421 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6422 (Elf_External_Verdef
*) p
);
6423 p
+= sizeof (Elf_External_Verdef
);
6425 defaux
.vda_name
= h
->dynstr_index
;
6426 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6428 defaux
.vda_next
= 0;
6429 if (t
->deps
!= NULL
)
6430 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6431 t
->name_indx
= defaux
.vda_name
;
6433 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6434 (Elf_External_Verdaux
*) p
);
6435 p
+= sizeof (Elf_External_Verdaux
);
6437 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6439 if (n
->version_needed
== NULL
)
6441 /* This can happen if there was an error in the
6443 defaux
.vda_name
= 0;
6447 defaux
.vda_name
= n
->version_needed
->name_indx
;
6448 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6451 if (n
->next
== NULL
)
6452 defaux
.vda_next
= 0;
6454 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6456 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6457 (Elf_External_Verdaux
*) p
);
6458 p
+= sizeof (Elf_External_Verdaux
);
6462 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6463 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6466 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6469 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6471 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6474 else if (info
->flags
& DF_BIND_NOW
)
6476 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6482 if (bfd_link_executable (info
))
6483 info
->flags_1
&= ~ (DF_1_INITFIRST
6486 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6490 /* Work out the size of the version reference section. */
6492 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6493 BFD_ASSERT (s
!= NULL
);
6495 struct elf_find_verdep_info sinfo
;
6498 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6499 if (sinfo
.vers
== 0)
6501 sinfo
.failed
= FALSE
;
6503 elf_link_hash_traverse (elf_hash_table (info
),
6504 _bfd_elf_link_find_version_dependencies
,
6509 if (elf_tdata (output_bfd
)->verref
== NULL
)
6510 s
->flags
|= SEC_EXCLUDE
;
6513 Elf_Internal_Verneed
*t
;
6518 /* Build the version dependency section. */
6521 for (t
= elf_tdata (output_bfd
)->verref
;
6525 Elf_Internal_Vernaux
*a
;
6527 size
+= sizeof (Elf_External_Verneed
);
6529 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6530 size
+= sizeof (Elf_External_Vernaux
);
6534 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6535 if (s
->contents
== NULL
)
6539 for (t
= elf_tdata (output_bfd
)->verref
;
6544 Elf_Internal_Vernaux
*a
;
6548 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6551 t
->vn_version
= VER_NEED_CURRENT
;
6553 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6554 elf_dt_name (t
->vn_bfd
) != NULL
6555 ? elf_dt_name (t
->vn_bfd
)
6556 : lbasename (t
->vn_bfd
->filename
),
6558 if (indx
== (size_t) -1)
6561 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6562 if (t
->vn_nextref
== NULL
)
6565 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6566 + caux
* sizeof (Elf_External_Vernaux
));
6568 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6569 (Elf_External_Verneed
*) p
);
6570 p
+= sizeof (Elf_External_Verneed
);
6572 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6574 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6575 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6576 a
->vna_nodename
, FALSE
);
6577 if (indx
== (size_t) -1)
6580 if (a
->vna_nextptr
== NULL
)
6583 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6585 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6586 (Elf_External_Vernaux
*) p
);
6587 p
+= sizeof (Elf_External_Vernaux
);
6591 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6592 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6595 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6599 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6600 && elf_tdata (output_bfd
)->cverdefs
== 0)
6601 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6602 §ion_sym_count
) == 0)
6604 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6605 s
->flags
|= SEC_EXCLUDE
;
6611 /* Find the first non-excluded output section. We'll use its
6612 section symbol for some emitted relocs. */
6614 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6618 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6619 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6620 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6622 elf_hash_table (info
)->text_index_section
= s
;
6627 /* Find two non-excluded output sections, one for code, one for data.
6628 We'll use their section symbols for some emitted relocs. */
6630 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6634 /* Data first, since setting text_index_section changes
6635 _bfd_elf_link_omit_section_dynsym. */
6636 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6637 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6638 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6640 elf_hash_table (info
)->data_index_section
= s
;
6644 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6645 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6646 == (SEC_ALLOC
| SEC_READONLY
))
6647 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6649 elf_hash_table (info
)->text_index_section
= s
;
6653 if (elf_hash_table (info
)->text_index_section
== NULL
)
6654 elf_hash_table (info
)->text_index_section
6655 = elf_hash_table (info
)->data_index_section
;
6659 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6661 const struct elf_backend_data
*bed
;
6663 if (!is_elf_hash_table (info
->hash
))
6666 bed
= get_elf_backend_data (output_bfd
);
6667 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6669 if (elf_hash_table (info
)->dynamic_sections_created
)
6673 bfd_size_type dynsymcount
;
6674 unsigned long section_sym_count
;
6675 unsigned int dtagcount
;
6677 dynobj
= elf_hash_table (info
)->dynobj
;
6679 /* Assign dynsym indicies. In a shared library we generate a
6680 section symbol for each output section, which come first.
6681 Next come all of the back-end allocated local dynamic syms,
6682 followed by the rest of the global symbols. */
6684 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6685 §ion_sym_count
);
6687 /* Work out the size of the symbol version section. */
6688 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6689 BFD_ASSERT (s
!= NULL
);
6690 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6692 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6693 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6694 if (s
->contents
== NULL
)
6697 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6701 /* Set the size of the .dynsym and .hash sections. We counted
6702 the number of dynamic symbols in elf_link_add_object_symbols.
6703 We will build the contents of .dynsym and .hash when we build
6704 the final symbol table, because until then we do not know the
6705 correct value to give the symbols. We built the .dynstr
6706 section as we went along in elf_link_add_object_symbols. */
6707 s
= elf_hash_table (info
)->dynsym
;
6708 BFD_ASSERT (s
!= NULL
);
6709 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6711 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6712 if (s
->contents
== NULL
)
6715 /* The first entry in .dynsym is a dummy symbol. Clear all the
6716 section syms, in case we don't output them all. */
6717 ++section_sym_count
;
6718 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6720 elf_hash_table (info
)->bucketcount
= 0;
6722 /* Compute the size of the hashing table. As a side effect this
6723 computes the hash values for all the names we export. */
6724 if (info
->emit_hash
)
6726 unsigned long int *hashcodes
;
6727 struct hash_codes_info hashinf
;
6729 unsigned long int nsyms
;
6731 size_t hash_entry_size
;
6733 /* Compute the hash values for all exported symbols. At the same
6734 time store the values in an array so that we could use them for
6736 amt
= dynsymcount
* sizeof (unsigned long int);
6737 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6738 if (hashcodes
== NULL
)
6740 hashinf
.hashcodes
= hashcodes
;
6741 hashinf
.error
= FALSE
;
6743 /* Put all hash values in HASHCODES. */
6744 elf_link_hash_traverse (elf_hash_table (info
),
6745 elf_collect_hash_codes
, &hashinf
);
6752 nsyms
= hashinf
.hashcodes
- hashcodes
;
6754 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6757 if (bucketcount
== 0)
6760 elf_hash_table (info
)->bucketcount
= bucketcount
;
6762 s
= bfd_get_linker_section (dynobj
, ".hash");
6763 BFD_ASSERT (s
!= NULL
);
6764 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6765 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6766 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6767 if (s
->contents
== NULL
)
6770 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6771 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6772 s
->contents
+ hash_entry_size
);
6775 if (info
->emit_gnu_hash
)
6778 unsigned char *contents
;
6779 struct collect_gnu_hash_codes cinfo
;
6783 memset (&cinfo
, 0, sizeof (cinfo
));
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
* 2 * sizeof (unsigned long int);
6789 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6790 if (cinfo
.hashcodes
== NULL
)
6793 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6794 cinfo
.min_dynindx
= -1;
6795 cinfo
.output_bfd
= output_bfd
;
6798 /* Put all hash values in HASHCODES. */
6799 elf_link_hash_traverse (elf_hash_table (info
),
6800 elf_collect_gnu_hash_codes
, &cinfo
);
6803 free (cinfo
.hashcodes
);
6808 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6810 if (bucketcount
== 0)
6812 free (cinfo
.hashcodes
);
6816 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6817 BFD_ASSERT (s
!= NULL
);
6819 if (cinfo
.nsyms
== 0)
6821 /* Empty .gnu.hash section is special. */
6822 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6823 free (cinfo
.hashcodes
);
6824 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6825 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6826 if (contents
== NULL
)
6828 s
->contents
= contents
;
6829 /* 1 empty bucket. */
6830 bfd_put_32 (output_bfd
, 1, contents
);
6831 /* SYMIDX above the special symbol 0. */
6832 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6833 /* Just one word for bitmask. */
6834 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6835 /* Only hash fn bloom filter. */
6836 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6837 /* No hashes are valid - empty bitmask. */
6838 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6839 /* No hashes in the only bucket. */
6840 bfd_put_32 (output_bfd
, 0,
6841 contents
+ 16 + bed
->s
->arch_size
/ 8);
6845 unsigned long int maskwords
, maskbitslog2
, x
;
6846 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6850 while ((x
>>= 1) != 0)
6852 if (maskbitslog2
< 3)
6854 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6855 maskbitslog2
= maskbitslog2
+ 3;
6857 maskbitslog2
= maskbitslog2
+ 2;
6858 if (bed
->s
->arch_size
== 64)
6860 if (maskbitslog2
== 5)
6866 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6867 cinfo
.shift2
= maskbitslog2
;
6868 cinfo
.maskbits
= 1 << maskbitslog2
;
6869 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6870 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6871 amt
+= maskwords
* sizeof (bfd_vma
);
6872 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6873 if (cinfo
.bitmask
== NULL
)
6875 free (cinfo
.hashcodes
);
6879 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6880 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6881 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6882 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6884 /* Determine how often each hash bucket is used. */
6885 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6886 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6887 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6889 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6890 if (cinfo
.counts
[i
] != 0)
6892 cinfo
.indx
[i
] = cnt
;
6893 cnt
+= cinfo
.counts
[i
];
6895 BFD_ASSERT (cnt
== dynsymcount
);
6896 cinfo
.bucketcount
= bucketcount
;
6897 cinfo
.local_indx
= cinfo
.min_dynindx
;
6899 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6900 s
->size
+= cinfo
.maskbits
/ 8;
6901 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6902 if (contents
== NULL
)
6904 free (cinfo
.bitmask
);
6905 free (cinfo
.hashcodes
);
6909 s
->contents
= contents
;
6910 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6911 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6912 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6913 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6914 contents
+= 16 + cinfo
.maskbits
/ 8;
6916 for (i
= 0; i
< bucketcount
; ++i
)
6918 if (cinfo
.counts
[i
] == 0)
6919 bfd_put_32 (output_bfd
, 0, contents
);
6921 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6925 cinfo
.contents
= contents
;
6927 /* Renumber dynamic symbols, populate .gnu.hash section. */
6928 elf_link_hash_traverse (elf_hash_table (info
),
6929 elf_renumber_gnu_hash_syms
, &cinfo
);
6931 contents
= s
->contents
+ 16;
6932 for (i
= 0; i
< maskwords
; ++i
)
6934 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6936 contents
+= bed
->s
->arch_size
/ 8;
6939 free (cinfo
.bitmask
);
6940 free (cinfo
.hashcodes
);
6944 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6945 BFD_ASSERT (s
!= NULL
);
6947 elf_finalize_dynstr (output_bfd
, info
);
6949 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6951 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6952 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6959 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6962 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6965 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6966 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6969 /* Finish SHF_MERGE section merging. */
6972 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6977 if (!is_elf_hash_table (info
->hash
))
6980 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6981 if ((ibfd
->flags
& DYNAMIC
) == 0
6982 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6983 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6984 == get_elf_backend_data (obfd
)->s
->elfclass
))
6985 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6986 if ((sec
->flags
& SEC_MERGE
) != 0
6987 && !bfd_is_abs_section (sec
->output_section
))
6989 struct bfd_elf_section_data
*secdata
;
6991 secdata
= elf_section_data (sec
);
6992 if (! _bfd_add_merge_section (obfd
,
6993 &elf_hash_table (info
)->merge_info
,
6994 sec
, &secdata
->sec_info
))
6996 else if (secdata
->sec_info
)
6997 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7000 if (elf_hash_table (info
)->merge_info
!= NULL
)
7001 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7002 merge_sections_remove_hook
);
7006 /* Create an entry in an ELF linker hash table. */
7008 struct bfd_hash_entry
*
7009 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7010 struct bfd_hash_table
*table
,
7013 /* Allocate the structure if it has not already been allocated by a
7017 entry
= (struct bfd_hash_entry
*)
7018 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7023 /* Call the allocation method of the superclass. */
7024 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7027 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7028 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7030 /* Set local fields. */
7033 ret
->got
= htab
->init_got_refcount
;
7034 ret
->plt
= htab
->init_plt_refcount
;
7035 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7036 - offsetof (struct elf_link_hash_entry
, size
)));
7037 /* Assume that we have been called by a non-ELF symbol reader.
7038 This flag is then reset by the code which reads an ELF input
7039 file. This ensures that a symbol created by a non-ELF symbol
7040 reader will have the flag set correctly. */
7047 /* Copy data from an indirect symbol to its direct symbol, hiding the
7048 old indirect symbol. Also used for copying flags to a weakdef. */
7051 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7052 struct elf_link_hash_entry
*dir
,
7053 struct elf_link_hash_entry
*ind
)
7055 struct elf_link_hash_table
*htab
;
7057 /* Copy down any references that we may have already seen to the
7058 symbol which just became indirect if DIR isn't a hidden versioned
7061 if (dir
->versioned
!= versioned_hidden
)
7063 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7064 dir
->ref_regular
|= ind
->ref_regular
;
7065 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7066 dir
->non_got_ref
|= ind
->non_got_ref
;
7067 dir
->needs_plt
|= ind
->needs_plt
;
7068 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7071 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7074 /* Copy over the global and procedure linkage table refcount entries.
7075 These may have been already set up by a check_relocs routine. */
7076 htab
= elf_hash_table (info
);
7077 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7079 if (dir
->got
.refcount
< 0)
7080 dir
->got
.refcount
= 0;
7081 dir
->got
.refcount
+= ind
->got
.refcount
;
7082 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7085 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7087 if (dir
->plt
.refcount
< 0)
7088 dir
->plt
.refcount
= 0;
7089 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7090 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7093 if (ind
->dynindx
!= -1)
7095 if (dir
->dynindx
!= -1)
7096 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7097 dir
->dynindx
= ind
->dynindx
;
7098 dir
->dynstr_index
= ind
->dynstr_index
;
7100 ind
->dynstr_index
= 0;
7105 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7106 struct elf_link_hash_entry
*h
,
7107 bfd_boolean force_local
)
7109 /* STT_GNU_IFUNC symbol must go through PLT. */
7110 if (h
->type
!= STT_GNU_IFUNC
)
7112 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7117 h
->forced_local
= 1;
7118 if (h
->dynindx
!= -1)
7121 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7127 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7131 _bfd_elf_link_hash_table_init
7132 (struct elf_link_hash_table
*table
,
7134 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7135 struct bfd_hash_table
*,
7137 unsigned int entsize
,
7138 enum elf_target_id target_id
)
7141 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7143 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7144 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7145 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7146 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7147 /* The first dynamic symbol is a dummy. */
7148 table
->dynsymcount
= 1;
7150 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7152 table
->root
.type
= bfd_link_elf_hash_table
;
7153 table
->hash_table_id
= target_id
;
7158 /* Create an ELF linker hash table. */
7160 struct bfd_link_hash_table
*
7161 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7163 struct elf_link_hash_table
*ret
;
7164 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7166 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7170 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7171 sizeof (struct elf_link_hash_entry
),
7177 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7182 /* Destroy an ELF linker hash table. */
7185 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7187 struct elf_link_hash_table
*htab
;
7189 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7190 if (htab
->dynstr
!= NULL
)
7191 _bfd_elf_strtab_free (htab
->dynstr
);
7192 _bfd_merge_sections_free (htab
->merge_info
);
7193 _bfd_generic_link_hash_table_free (obfd
);
7196 /* This is a hook for the ELF emulation code in the generic linker to
7197 tell the backend linker what file name to use for the DT_NEEDED
7198 entry for a dynamic object. */
7201 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7203 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7204 && bfd_get_format (abfd
) == bfd_object
)
7205 elf_dt_name (abfd
) = name
;
7209 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7212 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7213 && bfd_get_format (abfd
) == bfd_object
)
7214 lib_class
= elf_dyn_lib_class (abfd
);
7221 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7223 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7224 && bfd_get_format (abfd
) == bfd_object
)
7225 elf_dyn_lib_class (abfd
) = lib_class
;
7228 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7229 the linker ELF emulation code. */
7231 struct bfd_link_needed_list
*
7232 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7233 struct bfd_link_info
*info
)
7235 if (! is_elf_hash_table (info
->hash
))
7237 return elf_hash_table (info
)->needed
;
7240 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7241 hook for the linker ELF emulation code. */
7243 struct bfd_link_needed_list
*
7244 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7245 struct bfd_link_info
*info
)
7247 if (! is_elf_hash_table (info
->hash
))
7249 return elf_hash_table (info
)->runpath
;
7252 /* Get the name actually used for a dynamic object for a link. This
7253 is the SONAME entry if there is one. Otherwise, it is the string
7254 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7257 bfd_elf_get_dt_soname (bfd
*abfd
)
7259 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7260 && bfd_get_format (abfd
) == bfd_object
)
7261 return elf_dt_name (abfd
);
7265 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7266 the ELF linker emulation code. */
7269 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7270 struct bfd_link_needed_list
**pneeded
)
7273 bfd_byte
*dynbuf
= NULL
;
7274 unsigned int elfsec
;
7275 unsigned long shlink
;
7276 bfd_byte
*extdyn
, *extdynend
;
7278 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7282 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7283 || bfd_get_format (abfd
) != bfd_object
)
7286 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7287 if (s
== NULL
|| s
->size
== 0)
7290 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7293 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7294 if (elfsec
== SHN_BAD
)
7297 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7299 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7300 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7303 extdynend
= extdyn
+ s
->size
;
7304 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7306 Elf_Internal_Dyn dyn
;
7308 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7310 if (dyn
.d_tag
== DT_NULL
)
7313 if (dyn
.d_tag
== DT_NEEDED
)
7316 struct bfd_link_needed_list
*l
;
7317 unsigned int tagv
= dyn
.d_un
.d_val
;
7320 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7325 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7346 struct elf_symbuf_symbol
7348 unsigned long st_name
; /* Symbol name, index in string tbl */
7349 unsigned char st_info
; /* Type and binding attributes */
7350 unsigned char st_other
; /* Visibilty, and target specific */
7353 struct elf_symbuf_head
7355 struct elf_symbuf_symbol
*ssym
;
7357 unsigned int st_shndx
;
7364 Elf_Internal_Sym
*isym
;
7365 struct elf_symbuf_symbol
*ssym
;
7370 /* Sort references to symbols by ascending section number. */
7373 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7375 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7376 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7378 return s1
->st_shndx
- s2
->st_shndx
;
7382 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7384 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7385 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7386 return strcmp (s1
->name
, s2
->name
);
7389 static struct elf_symbuf_head
*
7390 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7392 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7393 struct elf_symbuf_symbol
*ssym
;
7394 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7395 size_t i
, shndx_count
, total_size
;
7397 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7401 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7402 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7403 *ind
++ = &isymbuf
[i
];
7406 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7407 elf_sort_elf_symbol
);
7410 if (indbufend
> indbuf
)
7411 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7412 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7415 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7416 + (indbufend
- indbuf
) * sizeof (*ssym
));
7417 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7418 if (ssymbuf
== NULL
)
7424 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7425 ssymbuf
->ssym
= NULL
;
7426 ssymbuf
->count
= shndx_count
;
7427 ssymbuf
->st_shndx
= 0;
7428 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7430 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7433 ssymhead
->ssym
= ssym
;
7434 ssymhead
->count
= 0;
7435 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7437 ssym
->st_name
= (*ind
)->st_name
;
7438 ssym
->st_info
= (*ind
)->st_info
;
7439 ssym
->st_other
= (*ind
)->st_other
;
7442 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7443 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7450 /* Check if 2 sections define the same set of local and global
7454 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7455 struct bfd_link_info
*info
)
7458 const struct elf_backend_data
*bed1
, *bed2
;
7459 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7460 size_t symcount1
, symcount2
;
7461 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7462 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7463 Elf_Internal_Sym
*isym
, *isymend
;
7464 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7465 size_t count1
, count2
, i
;
7466 unsigned int shndx1
, shndx2
;
7472 /* Both sections have to be in ELF. */
7473 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7474 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7477 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7480 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7481 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7482 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7485 bed1
= get_elf_backend_data (bfd1
);
7486 bed2
= get_elf_backend_data (bfd2
);
7487 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7488 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7489 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7490 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7492 if (symcount1
== 0 || symcount2
== 0)
7498 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7499 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7501 if (ssymbuf1
== NULL
)
7503 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7505 if (isymbuf1
== NULL
)
7508 if (!info
->reduce_memory_overheads
)
7509 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7510 = elf_create_symbuf (symcount1
, isymbuf1
);
7513 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7515 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7517 if (isymbuf2
== NULL
)
7520 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7521 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7522 = elf_create_symbuf (symcount2
, isymbuf2
);
7525 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7527 /* Optimized faster version. */
7529 struct elf_symbol
*symp
;
7530 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7533 hi
= ssymbuf1
->count
;
7538 mid
= (lo
+ hi
) / 2;
7539 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7541 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7545 count1
= ssymbuf1
[mid
].count
;
7552 hi
= ssymbuf2
->count
;
7557 mid
= (lo
+ hi
) / 2;
7558 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7560 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7564 count2
= ssymbuf2
[mid
].count
;
7570 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7574 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7576 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7577 if (symtable1
== NULL
|| symtable2
== NULL
)
7581 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7582 ssym
< ssymend
; ssym
++, symp
++)
7584 symp
->u
.ssym
= ssym
;
7585 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7591 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7592 ssym
< ssymend
; ssym
++, symp
++)
7594 symp
->u
.ssym
= ssym
;
7595 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7600 /* Sort symbol by name. */
7601 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7602 elf_sym_name_compare
);
7603 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7604 elf_sym_name_compare
);
7606 for (i
= 0; i
< count1
; i
++)
7607 /* Two symbols must have the same binding, type and name. */
7608 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7609 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7610 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7617 symtable1
= (struct elf_symbol
*)
7618 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7619 symtable2
= (struct elf_symbol
*)
7620 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7621 if (symtable1
== NULL
|| symtable2
== NULL
)
7624 /* Count definitions in the section. */
7626 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7627 if (isym
->st_shndx
== shndx1
)
7628 symtable1
[count1
++].u
.isym
= isym
;
7631 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7632 if (isym
->st_shndx
== shndx2
)
7633 symtable2
[count2
++].u
.isym
= isym
;
7635 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7638 for (i
= 0; i
< count1
; i
++)
7640 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7641 symtable1
[i
].u
.isym
->st_name
);
7643 for (i
= 0; i
< count2
; i
++)
7645 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7646 symtable2
[i
].u
.isym
->st_name
);
7648 /* Sort symbol by name. */
7649 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7650 elf_sym_name_compare
);
7651 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7652 elf_sym_name_compare
);
7654 for (i
= 0; i
< count1
; i
++)
7655 /* Two symbols must have the same binding, type and name. */
7656 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7657 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7658 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7676 /* Return TRUE if 2 section types are compatible. */
7679 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7680 bfd
*bbfd
, const asection
*bsec
)
7684 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7685 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7688 return elf_section_type (asec
) == elf_section_type (bsec
);
7691 /* Final phase of ELF linker. */
7693 /* A structure we use to avoid passing large numbers of arguments. */
7695 struct elf_final_link_info
7697 /* General link information. */
7698 struct bfd_link_info
*info
;
7701 /* Symbol string table. */
7702 struct elf_strtab_hash
*symstrtab
;
7703 /* .hash section. */
7705 /* symbol version section (.gnu.version). */
7706 asection
*symver_sec
;
7707 /* Buffer large enough to hold contents of any section. */
7709 /* Buffer large enough to hold external relocs of any section. */
7710 void *external_relocs
;
7711 /* Buffer large enough to hold internal relocs of any section. */
7712 Elf_Internal_Rela
*internal_relocs
;
7713 /* Buffer large enough to hold external local symbols of any input
7715 bfd_byte
*external_syms
;
7716 /* And a buffer for symbol section indices. */
7717 Elf_External_Sym_Shndx
*locsym_shndx
;
7718 /* Buffer large enough to hold internal local symbols of any input
7720 Elf_Internal_Sym
*internal_syms
;
7721 /* Array large enough to hold a symbol index for each local symbol
7722 of any input BFD. */
7724 /* Array large enough to hold a section pointer for each local
7725 symbol of any input BFD. */
7726 asection
**sections
;
7727 /* Buffer for SHT_SYMTAB_SHNDX section. */
7728 Elf_External_Sym_Shndx
*symshndxbuf
;
7729 /* Number of STT_FILE syms seen. */
7730 size_t filesym_count
;
7733 /* This struct is used to pass information to elf_link_output_extsym. */
7735 struct elf_outext_info
7738 bfd_boolean localsyms
;
7739 bfd_boolean file_sym_done
;
7740 struct elf_final_link_info
*flinfo
;
7744 /* Support for evaluating a complex relocation.
7746 Complex relocations are generalized, self-describing relocations. The
7747 implementation of them consists of two parts: complex symbols, and the
7748 relocations themselves.
7750 The relocations are use a reserved elf-wide relocation type code (R_RELC
7751 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7752 information (start bit, end bit, word width, etc) into the addend. This
7753 information is extracted from CGEN-generated operand tables within gas.
7755 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7756 internal) representing prefix-notation expressions, including but not
7757 limited to those sorts of expressions normally encoded as addends in the
7758 addend field. The symbol mangling format is:
7761 | <unary-operator> ':' <node>
7762 | <binary-operator> ':' <node> ':' <node>
7765 <literal> := 's' <digits=N> ':' <N character symbol name>
7766 | 'S' <digits=N> ':' <N character section name>
7770 <binary-operator> := as in C
7771 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7774 set_symbol_value (bfd
*bfd_with_globals
,
7775 Elf_Internal_Sym
*isymbuf
,
7780 struct elf_link_hash_entry
**sym_hashes
;
7781 struct elf_link_hash_entry
*h
;
7782 size_t extsymoff
= locsymcount
;
7784 if (symidx
< locsymcount
)
7786 Elf_Internal_Sym
*sym
;
7788 sym
= isymbuf
+ symidx
;
7789 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7791 /* It is a local symbol: move it to the
7792 "absolute" section and give it a value. */
7793 sym
->st_shndx
= SHN_ABS
;
7794 sym
->st_value
= val
;
7797 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7801 /* It is a global symbol: set its link type
7802 to "defined" and give it a value. */
7804 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7805 h
= sym_hashes
[symidx
- extsymoff
];
7806 while (h
->root
.type
== bfd_link_hash_indirect
7807 || h
->root
.type
== bfd_link_hash_warning
)
7808 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7809 h
->root
.type
= bfd_link_hash_defined
;
7810 h
->root
.u
.def
.value
= val
;
7811 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7815 resolve_symbol (const char *name
,
7817 struct elf_final_link_info
*flinfo
,
7819 Elf_Internal_Sym
*isymbuf
,
7822 Elf_Internal_Sym
*sym
;
7823 struct bfd_link_hash_entry
*global_entry
;
7824 const char *candidate
= NULL
;
7825 Elf_Internal_Shdr
*symtab_hdr
;
7828 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7830 for (i
= 0; i
< locsymcount
; ++ i
)
7834 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7837 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7838 symtab_hdr
->sh_link
,
7841 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7842 name
, candidate
, (unsigned long) sym
->st_value
);
7844 if (candidate
&& strcmp (candidate
, name
) == 0)
7846 asection
*sec
= flinfo
->sections
[i
];
7848 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7849 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7851 printf ("Found symbol with value %8.8lx\n",
7852 (unsigned long) *result
);
7858 /* Hmm, haven't found it yet. perhaps it is a global. */
7859 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7860 FALSE
, FALSE
, TRUE
);
7864 if (global_entry
->type
== bfd_link_hash_defined
7865 || global_entry
->type
== bfd_link_hash_defweak
)
7867 *result
= (global_entry
->u
.def
.value
7868 + global_entry
->u
.def
.section
->output_section
->vma
7869 + global_entry
->u
.def
.section
->output_offset
);
7871 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7872 global_entry
->root
.string
, (unsigned long) *result
);
7880 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7881 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7882 names like "foo.end" which is the end address of section "foo". */
7885 resolve_section (const char *name
,
7893 for (curr
= sections
; curr
; curr
= curr
->next
)
7894 if (strcmp (curr
->name
, name
) == 0)
7896 *result
= curr
->vma
;
7900 /* Hmm. still haven't found it. try pseudo-section names. */
7901 /* FIXME: This could be coded more efficiently... */
7902 for (curr
= sections
; curr
; curr
= curr
->next
)
7904 len
= strlen (curr
->name
);
7905 if (len
> strlen (name
))
7908 if (strncmp (curr
->name
, name
, len
) == 0)
7910 if (strncmp (".end", name
+ len
, 4) == 0)
7912 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7916 /* Insert more pseudo-section names here, if you like. */
7924 undefined_reference (const char *reftype
, const char *name
)
7926 /* xgettext:c-format */
7927 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7932 eval_symbol (bfd_vma
*result
,
7935 struct elf_final_link_info
*flinfo
,
7937 Elf_Internal_Sym
*isymbuf
,
7946 const char *sym
= *symp
;
7948 bfd_boolean symbol_is_section
= FALSE
;
7953 if (len
< 1 || len
> sizeof (symbuf
))
7955 bfd_set_error (bfd_error_invalid_operation
);
7968 *result
= strtoul (sym
, (char **) symp
, 16);
7972 symbol_is_section
= TRUE
;
7976 symlen
= strtol (sym
, (char **) symp
, 10);
7977 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7979 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7981 bfd_set_error (bfd_error_invalid_operation
);
7985 memcpy (symbuf
, sym
, symlen
);
7986 symbuf
[symlen
] = '\0';
7987 *symp
= sym
+ symlen
;
7989 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7990 the symbol as a section, or vice-versa. so we're pretty liberal in our
7991 interpretation here; section means "try section first", not "must be a
7992 section", and likewise with symbol. */
7994 if (symbol_is_section
)
7996 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
7997 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7998 isymbuf
, locsymcount
))
8000 undefined_reference ("section", symbuf
);
8006 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8007 isymbuf
, locsymcount
)
8008 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8011 undefined_reference ("symbol", symbuf
);
8018 /* All that remains are operators. */
8020 #define UNARY_OP(op) \
8021 if (strncmp (sym, #op, strlen (#op)) == 0) \
8023 sym += strlen (#op); \
8027 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8028 isymbuf, locsymcount, signed_p)) \
8031 *result = op ((bfd_signed_vma) a); \
8037 #define BINARY_OP(op) \
8038 if (strncmp (sym, #op, strlen (#op)) == 0) \
8040 sym += strlen (#op); \
8044 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8045 isymbuf, locsymcount, signed_p)) \
8048 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8049 isymbuf, locsymcount, signed_p)) \
8052 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8082 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8083 bfd_set_error (bfd_error_invalid_operation
);
8089 put_value (bfd_vma size
,
8090 unsigned long chunksz
,
8095 location
+= (size
- chunksz
);
8097 for (; size
; size
-= chunksz
, location
-= chunksz
)
8102 bfd_put_8 (input_bfd
, x
, location
);
8106 bfd_put_16 (input_bfd
, x
, location
);
8110 bfd_put_32 (input_bfd
, x
, location
);
8111 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8117 bfd_put_64 (input_bfd
, x
, location
);
8118 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8131 get_value (bfd_vma size
,
8132 unsigned long chunksz
,
8139 /* Sanity checks. */
8140 BFD_ASSERT (chunksz
<= sizeof (x
)
8143 && (size
% chunksz
) == 0
8144 && input_bfd
!= NULL
8145 && location
!= NULL
);
8147 if (chunksz
== sizeof (x
))
8149 BFD_ASSERT (size
== chunksz
);
8151 /* Make sure that we do not perform an undefined shift operation.
8152 We know that size == chunksz so there will only be one iteration
8153 of the loop below. */
8157 shift
= 8 * chunksz
;
8159 for (; size
; size
-= chunksz
, location
+= chunksz
)
8164 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8167 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8170 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8174 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8185 decode_complex_addend (unsigned long *start
, /* in bits */
8186 unsigned long *oplen
, /* in bits */
8187 unsigned long *len
, /* in bits */
8188 unsigned long *wordsz
, /* in bytes */
8189 unsigned long *chunksz
, /* in bytes */
8190 unsigned long *lsb0_p
,
8191 unsigned long *signed_p
,
8192 unsigned long *trunc_p
,
8193 unsigned long encoded
)
8195 * start
= encoded
& 0x3F;
8196 * len
= (encoded
>> 6) & 0x3F;
8197 * oplen
= (encoded
>> 12) & 0x3F;
8198 * wordsz
= (encoded
>> 18) & 0xF;
8199 * chunksz
= (encoded
>> 22) & 0xF;
8200 * lsb0_p
= (encoded
>> 27) & 1;
8201 * signed_p
= (encoded
>> 28) & 1;
8202 * trunc_p
= (encoded
>> 29) & 1;
8205 bfd_reloc_status_type
8206 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8207 asection
*input_section ATTRIBUTE_UNUSED
,
8209 Elf_Internal_Rela
*rel
,
8212 bfd_vma shift
, x
, mask
;
8213 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8214 bfd_reloc_status_type r
;
8216 /* Perform this reloc, since it is complex.
8217 (this is not to say that it necessarily refers to a complex
8218 symbol; merely that it is a self-describing CGEN based reloc.
8219 i.e. the addend has the complete reloc information (bit start, end,
8220 word size, etc) encoded within it.). */
8222 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8223 &chunksz
, &lsb0_p
, &signed_p
,
8224 &trunc_p
, rel
->r_addend
);
8226 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8229 shift
= (start
+ 1) - len
;
8231 shift
= (8 * wordsz
) - (start
+ len
);
8233 x
= get_value (wordsz
, chunksz
, input_bfd
,
8234 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8237 printf ("Doing complex reloc: "
8238 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8239 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8240 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8241 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8242 oplen
, (unsigned long) x
, (unsigned long) mask
,
8243 (unsigned long) relocation
);
8248 /* Now do an overflow check. */
8249 r
= bfd_check_overflow ((signed_p
8250 ? complain_overflow_signed
8251 : complain_overflow_unsigned
),
8252 len
, 0, (8 * wordsz
),
8256 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8259 printf (" relocation: %8.8lx\n"
8260 " shifted mask: %8.8lx\n"
8261 " shifted/masked reloc: %8.8lx\n"
8262 " result: %8.8lx\n",
8263 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8264 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8266 put_value (wordsz
, chunksz
, input_bfd
, x
,
8267 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8271 /* Functions to read r_offset from external (target order) reloc
8272 entry. Faster than bfd_getl32 et al, because we let the compiler
8273 know the value is aligned. */
8276 ext32l_r_offset (const void *p
)
8283 const union aligned32
*a
8284 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8286 uint32_t aval
= ( (uint32_t) a
->c
[0]
8287 | (uint32_t) a
->c
[1] << 8
8288 | (uint32_t) a
->c
[2] << 16
8289 | (uint32_t) a
->c
[3] << 24);
8294 ext32b_r_offset (const void *p
)
8301 const union aligned32
*a
8302 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8304 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8305 | (uint32_t) a
->c
[1] << 16
8306 | (uint32_t) a
->c
[2] << 8
8307 | (uint32_t) a
->c
[3]);
8311 #ifdef BFD_HOST_64_BIT
8313 ext64l_r_offset (const void *p
)
8320 const union aligned64
*a
8321 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8323 uint64_t aval
= ( (uint64_t) a
->c
[0]
8324 | (uint64_t) a
->c
[1] << 8
8325 | (uint64_t) a
->c
[2] << 16
8326 | (uint64_t) a
->c
[3] << 24
8327 | (uint64_t) a
->c
[4] << 32
8328 | (uint64_t) a
->c
[5] << 40
8329 | (uint64_t) a
->c
[6] << 48
8330 | (uint64_t) a
->c
[7] << 56);
8335 ext64b_r_offset (const void *p
)
8342 const union aligned64
*a
8343 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8345 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8346 | (uint64_t) a
->c
[1] << 48
8347 | (uint64_t) a
->c
[2] << 40
8348 | (uint64_t) a
->c
[3] << 32
8349 | (uint64_t) a
->c
[4] << 24
8350 | (uint64_t) a
->c
[5] << 16
8351 | (uint64_t) a
->c
[6] << 8
8352 | (uint64_t) a
->c
[7]);
8357 /* When performing a relocatable link, the input relocations are
8358 preserved. But, if they reference global symbols, the indices
8359 referenced must be updated. Update all the relocations found in
8363 elf_link_adjust_relocs (bfd
*abfd
,
8365 struct bfd_elf_section_reloc_data
*reldata
,
8369 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8371 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8372 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8373 bfd_vma r_type_mask
;
8375 unsigned int count
= reldata
->count
;
8376 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8378 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8380 swap_in
= bed
->s
->swap_reloc_in
;
8381 swap_out
= bed
->s
->swap_reloc_out
;
8383 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8385 swap_in
= bed
->s
->swap_reloca_in
;
8386 swap_out
= bed
->s
->swap_reloca_out
;
8391 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8394 if (bed
->s
->arch_size
== 32)
8401 r_type_mask
= 0xffffffff;
8405 erela
= reldata
->hdr
->contents
;
8406 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8408 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8411 if (*rel_hash
== NULL
)
8414 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8416 (*swap_in
) (abfd
, erela
, irela
);
8417 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8418 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8419 | (irela
[j
].r_info
& r_type_mask
));
8420 (*swap_out
) (abfd
, irela
, erela
);
8423 if (bed
->elf_backend_update_relocs
)
8424 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8426 if (sort
&& count
!= 0)
8428 bfd_vma (*ext_r_off
) (const void *);
8431 bfd_byte
*base
, *end
, *p
, *loc
;
8432 bfd_byte
*buf
= NULL
;
8434 if (bed
->s
->arch_size
== 32)
8436 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8437 ext_r_off
= ext32l_r_offset
;
8438 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8439 ext_r_off
= ext32b_r_offset
;
8445 #ifdef BFD_HOST_64_BIT
8446 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8447 ext_r_off
= ext64l_r_offset
;
8448 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8449 ext_r_off
= ext64b_r_offset
;
8455 /* Must use a stable sort here. A modified insertion sort,
8456 since the relocs are mostly sorted already. */
8457 elt_size
= reldata
->hdr
->sh_entsize
;
8458 base
= reldata
->hdr
->contents
;
8459 end
= base
+ count
* elt_size
;
8460 if (elt_size
> sizeof (Elf64_External_Rela
))
8463 /* Ensure the first element is lowest. This acts as a sentinel,
8464 speeding the main loop below. */
8465 r_off
= (*ext_r_off
) (base
);
8466 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8468 bfd_vma r_off2
= (*ext_r_off
) (p
);
8477 /* Don't just swap *base and *loc as that changes the order
8478 of the original base[0] and base[1] if they happen to
8479 have the same r_offset. */
8480 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8481 memcpy (onebuf
, loc
, elt_size
);
8482 memmove (base
+ elt_size
, base
, loc
- base
);
8483 memcpy (base
, onebuf
, elt_size
);
8486 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8488 /* base to p is sorted, *p is next to insert. */
8489 r_off
= (*ext_r_off
) (p
);
8490 /* Search the sorted region for location to insert. */
8492 while (r_off
< (*ext_r_off
) (loc
))
8497 /* Chances are there is a run of relocs to insert here,
8498 from one of more input files. Files are not always
8499 linked in order due to the way elf_link_input_bfd is
8500 called. See pr17666. */
8501 size_t sortlen
= p
- loc
;
8502 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8503 size_t runlen
= elt_size
;
8504 size_t buf_size
= 96 * 1024;
8505 while (p
+ runlen
< end
8506 && (sortlen
<= buf_size
8507 || runlen
+ elt_size
<= buf_size
)
8508 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8512 buf
= bfd_malloc (buf_size
);
8516 if (runlen
< sortlen
)
8518 memcpy (buf
, p
, runlen
);
8519 memmove (loc
+ runlen
, loc
, sortlen
);
8520 memcpy (loc
, buf
, runlen
);
8524 memcpy (buf
, loc
, sortlen
);
8525 memmove (loc
, p
, runlen
);
8526 memcpy (loc
+ runlen
, buf
, sortlen
);
8528 p
+= runlen
- elt_size
;
8531 /* Hashes are no longer valid. */
8532 free (reldata
->hashes
);
8533 reldata
->hashes
= NULL
;
8539 struct elf_link_sort_rela
8545 enum elf_reloc_type_class type
;
8546 /* We use this as an array of size int_rels_per_ext_rel. */
8547 Elf_Internal_Rela rela
[1];
8551 elf_link_sort_cmp1 (const void *A
, const void *B
)
8553 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8554 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8555 int relativea
, relativeb
;
8557 relativea
= a
->type
== reloc_class_relative
;
8558 relativeb
= b
->type
== reloc_class_relative
;
8560 if (relativea
< relativeb
)
8562 if (relativea
> relativeb
)
8564 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8566 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8568 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8570 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8576 elf_link_sort_cmp2 (const void *A
, const void *B
)
8578 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8579 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8581 if (a
->type
< b
->type
)
8583 if (a
->type
> b
->type
)
8585 if (a
->u
.offset
< b
->u
.offset
)
8587 if (a
->u
.offset
> b
->u
.offset
)
8589 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8591 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8597 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8599 asection
*dynamic_relocs
;
8602 bfd_size_type count
, size
;
8603 size_t i
, ret
, sort_elt
, ext_size
;
8604 bfd_byte
*sort
, *s_non_relative
, *p
;
8605 struct elf_link_sort_rela
*sq
;
8606 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8607 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8608 unsigned int opb
= bfd_octets_per_byte (abfd
);
8609 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8610 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8611 struct bfd_link_order
*lo
;
8613 bfd_boolean use_rela
;
8615 /* Find a dynamic reloc section. */
8616 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8617 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8618 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8619 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8621 bfd_boolean use_rela_initialised
= FALSE
;
8623 /* This is just here to stop gcc from complaining.
8624 Its initialization checking code is not perfect. */
8627 /* Both sections are present. Examine the sizes
8628 of the indirect sections to help us choose. */
8629 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8630 if (lo
->type
== bfd_indirect_link_order
)
8632 asection
*o
= lo
->u
.indirect
.section
;
8634 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8636 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8637 /* Section size is divisible by both rel and rela sizes.
8638 It is of no help to us. */
8642 /* Section size is only divisible by rela. */
8643 if (use_rela_initialised
&& (use_rela
== FALSE
))
8645 _bfd_error_handler (_("%B: Unable to sort relocs - "
8646 "they are in more than one size"),
8648 bfd_set_error (bfd_error_invalid_operation
);
8654 use_rela_initialised
= TRUE
;
8658 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8660 /* Section size is only divisible by rel. */
8661 if (use_rela_initialised
&& (use_rela
== TRUE
))
8663 _bfd_error_handler (_("%B: Unable to sort relocs - "
8664 "they are in more than one size"),
8666 bfd_set_error (bfd_error_invalid_operation
);
8672 use_rela_initialised
= TRUE
;
8677 /* The section size is not divisible by either -
8678 something is wrong. */
8679 _bfd_error_handler (_("%B: Unable to sort relocs - "
8680 "they are of an unknown size"), abfd
);
8681 bfd_set_error (bfd_error_invalid_operation
);
8686 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8687 if (lo
->type
== bfd_indirect_link_order
)
8689 asection
*o
= lo
->u
.indirect
.section
;
8691 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8693 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8694 /* Section size is divisible by both rel and rela sizes.
8695 It is of no help to us. */
8699 /* Section size is only divisible by rela. */
8700 if (use_rela_initialised
&& (use_rela
== FALSE
))
8702 _bfd_error_handler (_("%B: Unable to sort relocs - "
8703 "they are in more than one size"),
8705 bfd_set_error (bfd_error_invalid_operation
);
8711 use_rela_initialised
= TRUE
;
8715 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8717 /* Section size is only divisible by rel. */
8718 if (use_rela_initialised
&& (use_rela
== TRUE
))
8720 _bfd_error_handler (_("%B: Unable to sort relocs - "
8721 "they are in more than one size"),
8723 bfd_set_error (bfd_error_invalid_operation
);
8729 use_rela_initialised
= TRUE
;
8734 /* The section size is not divisible by either -
8735 something is wrong. */
8736 _bfd_error_handler (_("%B: Unable to sort relocs - "
8737 "they are of an unknown size"), abfd
);
8738 bfd_set_error (bfd_error_invalid_operation
);
8743 if (! use_rela_initialised
)
8747 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8749 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8756 dynamic_relocs
= rela_dyn
;
8757 ext_size
= bed
->s
->sizeof_rela
;
8758 swap_in
= bed
->s
->swap_reloca_in
;
8759 swap_out
= bed
->s
->swap_reloca_out
;
8763 dynamic_relocs
= rel_dyn
;
8764 ext_size
= bed
->s
->sizeof_rel
;
8765 swap_in
= bed
->s
->swap_reloc_in
;
8766 swap_out
= bed
->s
->swap_reloc_out
;
8770 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8771 if (lo
->type
== bfd_indirect_link_order
)
8772 size
+= lo
->u
.indirect
.section
->size
;
8774 if (size
!= dynamic_relocs
->size
)
8777 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8778 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8780 count
= dynamic_relocs
->size
/ ext_size
;
8783 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8787 (*info
->callbacks
->warning
)
8788 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8792 if (bed
->s
->arch_size
== 32)
8793 r_sym_mask
= ~(bfd_vma
) 0xff;
8795 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8797 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8798 if (lo
->type
== bfd_indirect_link_order
)
8800 bfd_byte
*erel
, *erelend
;
8801 asection
*o
= lo
->u
.indirect
.section
;
8803 if (o
->contents
== NULL
&& o
->size
!= 0)
8805 /* This is a reloc section that is being handled as a normal
8806 section. See bfd_section_from_shdr. We can't combine
8807 relocs in this case. */
8812 erelend
= o
->contents
+ o
->size
;
8813 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8815 while (erel
< erelend
)
8817 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8819 (*swap_in
) (abfd
, erel
, s
->rela
);
8820 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8821 s
->u
.sym_mask
= r_sym_mask
;
8827 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8829 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8831 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8832 if (s
->type
!= reloc_class_relative
)
8838 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8839 for (; i
< count
; i
++, p
+= sort_elt
)
8841 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8842 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8844 sp
->u
.offset
= sq
->rela
->r_offset
;
8847 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8849 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8850 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8852 /* We have plt relocs in .rela.dyn. */
8853 sq
= (struct elf_link_sort_rela
*) sort
;
8854 for (i
= 0; i
< count
; i
++)
8855 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8857 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8859 struct bfd_link_order
**plo
;
8860 /* Put srelplt link_order last. This is so the output_offset
8861 set in the next loop is correct for DT_JMPREL. */
8862 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8863 if ((*plo
)->type
== bfd_indirect_link_order
8864 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8870 plo
= &(*plo
)->next
;
8873 dynamic_relocs
->map_tail
.link_order
= lo
;
8878 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8879 if (lo
->type
== bfd_indirect_link_order
)
8881 bfd_byte
*erel
, *erelend
;
8882 asection
*o
= lo
->u
.indirect
.section
;
8885 erelend
= o
->contents
+ o
->size
;
8886 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8887 while (erel
< erelend
)
8889 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8890 (*swap_out
) (abfd
, s
->rela
, erel
);
8897 *psec
= dynamic_relocs
;
8901 /* Add a symbol to the output symbol string table. */
8904 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8906 Elf_Internal_Sym
*elfsym
,
8907 asection
*input_sec
,
8908 struct elf_link_hash_entry
*h
)
8910 int (*output_symbol_hook
)
8911 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8912 struct elf_link_hash_entry
*);
8913 struct elf_link_hash_table
*hash_table
;
8914 const struct elf_backend_data
*bed
;
8915 bfd_size_type strtabsize
;
8917 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8919 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8920 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8921 if (output_symbol_hook
!= NULL
)
8923 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8930 || (input_sec
->flags
& SEC_EXCLUDE
))
8931 elfsym
->st_name
= (unsigned long) -1;
8934 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8935 to get the final offset for st_name. */
8937 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8939 if (elfsym
->st_name
== (unsigned long) -1)
8943 hash_table
= elf_hash_table (flinfo
->info
);
8944 strtabsize
= hash_table
->strtabsize
;
8945 if (strtabsize
<= hash_table
->strtabcount
)
8947 strtabsize
+= strtabsize
;
8948 hash_table
->strtabsize
= strtabsize
;
8949 strtabsize
*= sizeof (*hash_table
->strtab
);
8951 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8953 if (hash_table
->strtab
== NULL
)
8956 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8957 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8958 = hash_table
->strtabcount
;
8959 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8960 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8962 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8963 hash_table
->strtabcount
+= 1;
8968 /* Swap symbols out to the symbol table and flush the output symbols to
8972 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8974 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8977 const struct elf_backend_data
*bed
;
8979 Elf_Internal_Shdr
*hdr
;
8983 if (!hash_table
->strtabcount
)
8986 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8988 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8990 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8991 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8995 if (flinfo
->symshndxbuf
)
8997 amt
= sizeof (Elf_External_Sym_Shndx
);
8998 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
8999 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9000 if (flinfo
->symshndxbuf
== NULL
)
9007 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9009 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9010 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9011 elfsym
->sym
.st_name
= 0;
9014 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9015 elfsym
->sym
.st_name
);
9016 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9017 ((bfd_byte
*) symbuf
9018 + (elfsym
->dest_index
9019 * bed
->s
->sizeof_sym
)),
9020 (flinfo
->symshndxbuf
9021 + elfsym
->destshndx_index
));
9024 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9025 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9026 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9027 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9028 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9030 hdr
->sh_size
+= amt
;
9038 free (hash_table
->strtab
);
9039 hash_table
->strtab
= NULL
;
9044 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9047 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9049 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9050 && sym
->st_shndx
< SHN_LORESERVE
)
9052 /* The gABI doesn't support dynamic symbols in output sections
9055 /* xgettext:c-format */
9056 (_("%B: Too many sections: %d (>= %d)"),
9057 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9058 bfd_set_error (bfd_error_nonrepresentable_section
);
9064 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9065 allowing an unsatisfied unversioned symbol in the DSO to match a
9066 versioned symbol that would normally require an explicit version.
9067 We also handle the case that a DSO references a hidden symbol
9068 which may be satisfied by a versioned symbol in another DSO. */
9071 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9072 const struct elf_backend_data
*bed
,
9073 struct elf_link_hash_entry
*h
)
9076 struct elf_link_loaded_list
*loaded
;
9078 if (!is_elf_hash_table (info
->hash
))
9081 /* Check indirect symbol. */
9082 while (h
->root
.type
== bfd_link_hash_indirect
)
9083 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9085 switch (h
->root
.type
)
9091 case bfd_link_hash_undefined
:
9092 case bfd_link_hash_undefweak
:
9093 abfd
= h
->root
.u
.undef
.abfd
;
9095 || (abfd
->flags
& DYNAMIC
) == 0
9096 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9100 case bfd_link_hash_defined
:
9101 case bfd_link_hash_defweak
:
9102 abfd
= h
->root
.u
.def
.section
->owner
;
9105 case bfd_link_hash_common
:
9106 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9109 BFD_ASSERT (abfd
!= NULL
);
9111 for (loaded
= elf_hash_table (info
)->loaded
;
9113 loaded
= loaded
->next
)
9116 Elf_Internal_Shdr
*hdr
;
9120 Elf_Internal_Shdr
*versymhdr
;
9121 Elf_Internal_Sym
*isym
;
9122 Elf_Internal_Sym
*isymend
;
9123 Elf_Internal_Sym
*isymbuf
;
9124 Elf_External_Versym
*ever
;
9125 Elf_External_Versym
*extversym
;
9127 input
= loaded
->abfd
;
9129 /* We check each DSO for a possible hidden versioned definition. */
9131 || (input
->flags
& DYNAMIC
) == 0
9132 || elf_dynversym (input
) == 0)
9135 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9137 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9138 if (elf_bad_symtab (input
))
9140 extsymcount
= symcount
;
9145 extsymcount
= symcount
- hdr
->sh_info
;
9146 extsymoff
= hdr
->sh_info
;
9149 if (extsymcount
== 0)
9152 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9154 if (isymbuf
== NULL
)
9157 /* Read in any version definitions. */
9158 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9159 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9160 if (extversym
== NULL
)
9163 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9164 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9165 != versymhdr
->sh_size
))
9173 ever
= extversym
+ extsymoff
;
9174 isymend
= isymbuf
+ extsymcount
;
9175 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9178 Elf_Internal_Versym iver
;
9179 unsigned short version_index
;
9181 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9182 || isym
->st_shndx
== SHN_UNDEF
)
9185 name
= bfd_elf_string_from_elf_section (input
,
9188 if (strcmp (name
, h
->root
.root
.string
) != 0)
9191 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9193 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9195 && h
->forced_local
))
9197 /* If we have a non-hidden versioned sym, then it should
9198 have provided a definition for the undefined sym unless
9199 it is defined in a non-shared object and forced local.
9204 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9205 if (version_index
== 1 || version_index
== 2)
9207 /* This is the base or first version. We can use it. */
9221 /* Convert ELF common symbol TYPE. */
9224 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9226 /* Commom symbol can only appear in relocatable link. */
9227 if (!bfd_link_relocatable (info
))
9229 switch (info
->elf_stt_common
)
9233 case elf_stt_common
:
9236 case no_elf_stt_common
:
9243 /* Add an external symbol to the symbol table. This is called from
9244 the hash table traversal routine. When generating a shared object,
9245 we go through the symbol table twice. The first time we output
9246 anything that might have been forced to local scope in a version
9247 script. The second time we output the symbols that are still
9251 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9253 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9254 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9255 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9257 Elf_Internal_Sym sym
;
9258 asection
*input_sec
;
9259 const struct elf_backend_data
*bed
;
9264 if (h
->root
.type
== bfd_link_hash_warning
)
9266 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9267 if (h
->root
.type
== bfd_link_hash_new
)
9271 /* Decide whether to output this symbol in this pass. */
9272 if (eoinfo
->localsyms
)
9274 if (!h
->forced_local
)
9279 if (h
->forced_local
)
9283 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9285 if (h
->root
.type
== bfd_link_hash_undefined
)
9287 /* If we have an undefined symbol reference here then it must have
9288 come from a shared library that is being linked in. (Undefined
9289 references in regular files have already been handled unless
9290 they are in unreferenced sections which are removed by garbage
9292 bfd_boolean ignore_undef
= FALSE
;
9294 /* Some symbols may be special in that the fact that they're
9295 undefined can be safely ignored - let backend determine that. */
9296 if (bed
->elf_backend_ignore_undef_symbol
)
9297 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9299 /* If we are reporting errors for this situation then do so now. */
9302 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9303 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9304 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9305 (*flinfo
->info
->callbacks
->undefined_symbol
)
9306 (flinfo
->info
, h
->root
.root
.string
,
9307 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9309 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9311 /* Strip a global symbol defined in a discarded section. */
9316 /* We should also warn if a forced local symbol is referenced from
9317 shared libraries. */
9318 if (bfd_link_executable (flinfo
->info
)
9323 && h
->ref_dynamic_nonweak
9324 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9328 struct elf_link_hash_entry
*hi
= h
;
9330 /* Check indirect symbol. */
9331 while (hi
->root
.type
== bfd_link_hash_indirect
)
9332 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9334 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9335 /* xgettext:c-format */
9336 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9337 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9338 /* xgettext:c-format */
9339 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9341 /* xgettext:c-format */
9342 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9343 def_bfd
= flinfo
->output_bfd
;
9344 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9345 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9346 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9347 h
->root
.root
.string
);
9348 bfd_set_error (bfd_error_bad_value
);
9349 eoinfo
->failed
= TRUE
;
9353 /* We don't want to output symbols that have never been mentioned by
9354 a regular file, or that we have been told to strip. However, if
9355 h->indx is set to -2, the symbol is used by a reloc and we must
9360 else if ((h
->def_dynamic
9362 || h
->root
.type
== bfd_link_hash_new
)
9366 else if (flinfo
->info
->strip
== strip_all
)
9368 else if (flinfo
->info
->strip
== strip_some
9369 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9370 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9372 else if ((h
->root
.type
== bfd_link_hash_defined
9373 || h
->root
.type
== bfd_link_hash_defweak
)
9374 && ((flinfo
->info
->strip_discarded
9375 && discarded_section (h
->root
.u
.def
.section
))
9376 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9377 && h
->root
.u
.def
.section
->owner
!= NULL
9378 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9380 else if ((h
->root
.type
== bfd_link_hash_undefined
9381 || h
->root
.type
== bfd_link_hash_undefweak
)
9382 && h
->root
.u
.undef
.abfd
!= NULL
9383 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9388 /* If we're stripping it, and it's not a dynamic symbol, there's
9389 nothing else to do. However, if it is a forced local symbol or
9390 an ifunc symbol we need to give the backend finish_dynamic_symbol
9391 function a chance to make it dynamic. */
9394 && type
!= STT_GNU_IFUNC
9395 && !h
->forced_local
)
9399 sym
.st_size
= h
->size
;
9400 sym
.st_other
= h
->other
;
9401 switch (h
->root
.type
)
9404 case bfd_link_hash_new
:
9405 case bfd_link_hash_warning
:
9409 case bfd_link_hash_undefined
:
9410 case bfd_link_hash_undefweak
:
9411 input_sec
= bfd_und_section_ptr
;
9412 sym
.st_shndx
= SHN_UNDEF
;
9415 case bfd_link_hash_defined
:
9416 case bfd_link_hash_defweak
:
9418 input_sec
= h
->root
.u
.def
.section
;
9419 if (input_sec
->output_section
!= NULL
)
9422 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9423 input_sec
->output_section
);
9424 if (sym
.st_shndx
== SHN_BAD
)
9427 /* xgettext:c-format */
9428 (_("%B: could not find output section %A for input section %A"),
9429 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9430 bfd_set_error (bfd_error_nonrepresentable_section
);
9431 eoinfo
->failed
= TRUE
;
9435 /* ELF symbols in relocatable files are section relative,
9436 but in nonrelocatable files they are virtual
9438 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9439 if (!bfd_link_relocatable (flinfo
->info
))
9441 sym
.st_value
+= input_sec
->output_section
->vma
;
9442 if (h
->type
== STT_TLS
)
9444 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9445 if (tls_sec
!= NULL
)
9446 sym
.st_value
-= tls_sec
->vma
;
9452 BFD_ASSERT (input_sec
->owner
== NULL
9453 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9454 sym
.st_shndx
= SHN_UNDEF
;
9455 input_sec
= bfd_und_section_ptr
;
9460 case bfd_link_hash_common
:
9461 input_sec
= h
->root
.u
.c
.p
->section
;
9462 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9463 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9466 case bfd_link_hash_indirect
:
9467 /* These symbols are created by symbol versioning. They point
9468 to the decorated version of the name. For example, if the
9469 symbol foo@@GNU_1.2 is the default, which should be used when
9470 foo is used with no version, then we add an indirect symbol
9471 foo which points to foo@@GNU_1.2. We ignore these symbols,
9472 since the indirected symbol is already in the hash table. */
9476 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9477 switch (h
->root
.type
)
9479 case bfd_link_hash_common
:
9480 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9482 case bfd_link_hash_defined
:
9483 case bfd_link_hash_defweak
:
9484 if (bed
->common_definition (&sym
))
9485 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9489 case bfd_link_hash_undefined
:
9490 case bfd_link_hash_undefweak
:
9496 if (h
->forced_local
)
9498 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9499 /* Turn off visibility on local symbol. */
9500 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9502 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9503 else if (h
->unique_global
&& h
->def_regular
)
9504 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9505 else if (h
->root
.type
== bfd_link_hash_undefweak
9506 || h
->root
.type
== bfd_link_hash_defweak
)
9507 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9509 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9510 sym
.st_target_internal
= h
->target_internal
;
9512 /* Give the processor backend a chance to tweak the symbol value,
9513 and also to finish up anything that needs to be done for this
9514 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9515 forced local syms when non-shared is due to a historical quirk.
9516 STT_GNU_IFUNC symbol must go through PLT. */
9517 if ((h
->type
== STT_GNU_IFUNC
9519 && !bfd_link_relocatable (flinfo
->info
))
9520 || ((h
->dynindx
!= -1
9522 && ((bfd_link_pic (flinfo
->info
)
9523 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9524 || h
->root
.type
!= bfd_link_hash_undefweak
))
9525 || !h
->forced_local
)
9526 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9528 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9529 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9531 eoinfo
->failed
= TRUE
;
9536 /* If we are marking the symbol as undefined, and there are no
9537 non-weak references to this symbol from a regular object, then
9538 mark the symbol as weak undefined; if there are non-weak
9539 references, mark the symbol as strong. We can't do this earlier,
9540 because it might not be marked as undefined until the
9541 finish_dynamic_symbol routine gets through with it. */
9542 if (sym
.st_shndx
== SHN_UNDEF
9544 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9545 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9548 type
= ELF_ST_TYPE (sym
.st_info
);
9550 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9551 if (type
== STT_GNU_IFUNC
)
9554 if (h
->ref_regular_nonweak
)
9555 bindtype
= STB_GLOBAL
;
9557 bindtype
= STB_WEAK
;
9558 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9561 /* If this is a symbol defined in a dynamic library, don't use the
9562 symbol size from the dynamic library. Relinking an executable
9563 against a new library may introduce gratuitous changes in the
9564 executable's symbols if we keep the size. */
9565 if (sym
.st_shndx
== SHN_UNDEF
9570 /* If a non-weak symbol with non-default visibility is not defined
9571 locally, it is a fatal error. */
9572 if (!bfd_link_relocatable (flinfo
->info
)
9573 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9574 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9575 && h
->root
.type
== bfd_link_hash_undefined
9580 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9581 /* xgettext:c-format */
9582 msg
= _("%B: protected symbol `%s' isn't defined");
9583 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9584 /* xgettext:c-format */
9585 msg
= _("%B: internal symbol `%s' isn't defined");
9587 /* xgettext:c-format */
9588 msg
= _("%B: hidden symbol `%s' isn't defined");
9589 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9590 bfd_set_error (bfd_error_bad_value
);
9591 eoinfo
->failed
= TRUE
;
9595 /* If this symbol should be put in the .dynsym section, then put it
9596 there now. We already know the symbol index. We also fill in
9597 the entry in the .hash section. */
9598 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9600 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9604 /* Since there is no version information in the dynamic string,
9605 if there is no version info in symbol version section, we will
9606 have a run-time problem if not linking executable, referenced
9607 by shared library, or not bound locally. */
9608 if (h
->verinfo
.verdef
== NULL
9609 && (!bfd_link_executable (flinfo
->info
)
9611 || !h
->def_regular
))
9613 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9615 if (p
&& p
[1] != '\0')
9618 /* xgettext:c-format */
9619 (_("%B: No symbol version section for versioned symbol `%s'"),
9620 flinfo
->output_bfd
, h
->root
.root
.string
);
9621 eoinfo
->failed
= TRUE
;
9626 sym
.st_name
= h
->dynstr_index
;
9627 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9628 + h
->dynindx
* bed
->s
->sizeof_sym
);
9629 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9631 eoinfo
->failed
= TRUE
;
9634 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9636 if (flinfo
->hash_sec
!= NULL
)
9638 size_t hash_entry_size
;
9639 bfd_byte
*bucketpos
;
9644 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9645 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9648 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9649 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9650 + (bucket
+ 2) * hash_entry_size
);
9651 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9652 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9654 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9655 ((bfd_byte
*) flinfo
->hash_sec
->contents
9656 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9659 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9661 Elf_Internal_Versym iversym
;
9662 Elf_External_Versym
*eversym
;
9664 if (!h
->def_regular
)
9666 if (h
->verinfo
.verdef
== NULL
9667 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9668 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9669 iversym
.vs_vers
= 0;
9671 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9675 if (h
->verinfo
.vertree
== NULL
)
9676 iversym
.vs_vers
= 1;
9678 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9679 if (flinfo
->info
->create_default_symver
)
9683 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9685 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9686 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9688 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9689 eversym
+= h
->dynindx
;
9690 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9694 /* If the symbol is undefined, and we didn't output it to .dynsym,
9695 strip it from .symtab too. Obviously we can't do this for
9696 relocatable output or when needed for --emit-relocs. */
9697 else if (input_sec
== bfd_und_section_ptr
9699 && !bfd_link_relocatable (flinfo
->info
))
9701 /* Also strip others that we couldn't earlier due to dynamic symbol
9705 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9708 /* Output a FILE symbol so that following locals are not associated
9709 with the wrong input file. We need one for forced local symbols
9710 if we've seen more than one FILE symbol or when we have exactly
9711 one FILE symbol but global symbols are present in a file other
9712 than the one with the FILE symbol. We also need one if linker
9713 defined symbols are present. In practice these conditions are
9714 always met, so just emit the FILE symbol unconditionally. */
9715 if (eoinfo
->localsyms
9716 && !eoinfo
->file_sym_done
9717 && eoinfo
->flinfo
->filesym_count
!= 0)
9719 Elf_Internal_Sym fsym
;
9721 memset (&fsym
, 0, sizeof (fsym
));
9722 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9723 fsym
.st_shndx
= SHN_ABS
;
9724 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9725 bfd_und_section_ptr
, NULL
))
9728 eoinfo
->file_sym_done
= TRUE
;
9731 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9732 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9736 eoinfo
->failed
= TRUE
;
9741 else if (h
->indx
== -2)
9747 /* Return TRUE if special handling is done for relocs in SEC against
9748 symbols defined in discarded sections. */
9751 elf_section_ignore_discarded_relocs (asection
*sec
)
9753 const struct elf_backend_data
*bed
;
9755 switch (sec
->sec_info_type
)
9757 case SEC_INFO_TYPE_STABS
:
9758 case SEC_INFO_TYPE_EH_FRAME
:
9759 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9765 bed
= get_elf_backend_data (sec
->owner
);
9766 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9767 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9773 /* Return a mask saying how ld should treat relocations in SEC against
9774 symbols defined in discarded sections. If this function returns
9775 COMPLAIN set, ld will issue a warning message. If this function
9776 returns PRETEND set, and the discarded section was link-once and the
9777 same size as the kept link-once section, ld will pretend that the
9778 symbol was actually defined in the kept section. Otherwise ld will
9779 zero the reloc (at least that is the intent, but some cooperation by
9780 the target dependent code is needed, particularly for REL targets). */
9783 _bfd_elf_default_action_discarded (asection
*sec
)
9785 if (sec
->flags
& SEC_DEBUGGING
)
9788 if (strcmp (".eh_frame", sec
->name
) == 0)
9791 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9794 return COMPLAIN
| PRETEND
;
9797 /* Find a match between a section and a member of a section group. */
9800 match_group_member (asection
*sec
, asection
*group
,
9801 struct bfd_link_info
*info
)
9803 asection
*first
= elf_next_in_group (group
);
9804 asection
*s
= first
;
9808 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9811 s
= elf_next_in_group (s
);
9819 /* Check if the kept section of a discarded section SEC can be used
9820 to replace it. Return the replacement if it is OK. Otherwise return
9824 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9828 kept
= sec
->kept_section
;
9831 if ((kept
->flags
& SEC_GROUP
) != 0)
9832 kept
= match_group_member (sec
, kept
, info
);
9834 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9835 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9837 sec
->kept_section
= kept
;
9842 /* Link an input file into the linker output file. This function
9843 handles all the sections and relocations of the input file at once.
9844 This is so that we only have to read the local symbols once, and
9845 don't have to keep them in memory. */
9848 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9850 int (*relocate_section
)
9851 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9852 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9854 Elf_Internal_Shdr
*symtab_hdr
;
9857 Elf_Internal_Sym
*isymbuf
;
9858 Elf_Internal_Sym
*isym
;
9859 Elf_Internal_Sym
*isymend
;
9861 asection
**ppsection
;
9863 const struct elf_backend_data
*bed
;
9864 struct elf_link_hash_entry
**sym_hashes
;
9865 bfd_size_type address_size
;
9866 bfd_vma r_type_mask
;
9868 bfd_boolean have_file_sym
= FALSE
;
9870 output_bfd
= flinfo
->output_bfd
;
9871 bed
= get_elf_backend_data (output_bfd
);
9872 relocate_section
= bed
->elf_backend_relocate_section
;
9874 /* If this is a dynamic object, we don't want to do anything here:
9875 we don't want the local symbols, and we don't want the section
9877 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9880 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9881 if (elf_bad_symtab (input_bfd
))
9883 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9888 locsymcount
= symtab_hdr
->sh_info
;
9889 extsymoff
= symtab_hdr
->sh_info
;
9892 /* Read the local symbols. */
9893 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9894 if (isymbuf
== NULL
&& locsymcount
!= 0)
9896 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9897 flinfo
->internal_syms
,
9898 flinfo
->external_syms
,
9899 flinfo
->locsym_shndx
);
9900 if (isymbuf
== NULL
)
9904 /* Find local symbol sections and adjust values of symbols in
9905 SEC_MERGE sections. Write out those local symbols we know are
9906 going into the output file. */
9907 isymend
= isymbuf
+ locsymcount
;
9908 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9910 isym
++, pindex
++, ppsection
++)
9914 Elf_Internal_Sym osym
;
9920 if (elf_bad_symtab (input_bfd
))
9922 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9929 if (isym
->st_shndx
== SHN_UNDEF
)
9930 isec
= bfd_und_section_ptr
;
9931 else if (isym
->st_shndx
== SHN_ABS
)
9932 isec
= bfd_abs_section_ptr
;
9933 else if (isym
->st_shndx
== SHN_COMMON
)
9934 isec
= bfd_com_section_ptr
;
9937 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9940 /* Don't attempt to output symbols with st_shnx in the
9941 reserved range other than SHN_ABS and SHN_COMMON. */
9945 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9946 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9948 _bfd_merged_section_offset (output_bfd
, &isec
,
9949 elf_section_data (isec
)->sec_info
,
9955 /* Don't output the first, undefined, symbol. In fact, don't
9956 output any undefined local symbol. */
9957 if (isec
== bfd_und_section_ptr
)
9960 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9962 /* We never output section symbols. Instead, we use the
9963 section symbol of the corresponding section in the output
9968 /* If we are stripping all symbols, we don't want to output this
9970 if (flinfo
->info
->strip
== strip_all
)
9973 /* If we are discarding all local symbols, we don't want to
9974 output this one. If we are generating a relocatable output
9975 file, then some of the local symbols may be required by
9976 relocs; we output them below as we discover that they are
9978 if (flinfo
->info
->discard
== discard_all
)
9981 /* If this symbol is defined in a section which we are
9982 discarding, we don't need to keep it. */
9983 if (isym
->st_shndx
!= SHN_UNDEF
9984 && isym
->st_shndx
< SHN_LORESERVE
9985 && bfd_section_removed_from_list (output_bfd
,
9986 isec
->output_section
))
9989 /* Get the name of the symbol. */
9990 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9995 /* See if we are discarding symbols with this name. */
9996 if ((flinfo
->info
->strip
== strip_some
9997 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9999 || (((flinfo
->info
->discard
== discard_sec_merge
10000 && (isec
->flags
& SEC_MERGE
)
10001 && !bfd_link_relocatable (flinfo
->info
))
10002 || flinfo
->info
->discard
== discard_l
)
10003 && bfd_is_local_label_name (input_bfd
, name
)))
10006 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10008 if (input_bfd
->lto_output
)
10009 /* -flto puts a temp file name here. This means builds
10010 are not reproducible. Discard the symbol. */
10012 have_file_sym
= TRUE
;
10013 flinfo
->filesym_count
+= 1;
10015 if (!have_file_sym
)
10017 /* In the absence of debug info, bfd_find_nearest_line uses
10018 FILE symbols to determine the source file for local
10019 function symbols. Provide a FILE symbol here if input
10020 files lack such, so that their symbols won't be
10021 associated with a previous input file. It's not the
10022 source file, but the best we can do. */
10023 have_file_sym
= TRUE
;
10024 flinfo
->filesym_count
+= 1;
10025 memset (&osym
, 0, sizeof (osym
));
10026 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10027 osym
.st_shndx
= SHN_ABS
;
10028 if (!elf_link_output_symstrtab (flinfo
,
10029 (input_bfd
->lto_output
? NULL
10030 : input_bfd
->filename
),
10031 &osym
, bfd_abs_section_ptr
,
10038 /* Adjust the section index for the output file. */
10039 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10040 isec
->output_section
);
10041 if (osym
.st_shndx
== SHN_BAD
)
10044 /* ELF symbols in relocatable files are section relative, but
10045 in executable files they are virtual addresses. Note that
10046 this code assumes that all ELF sections have an associated
10047 BFD section with a reasonable value for output_offset; below
10048 we assume that they also have a reasonable value for
10049 output_section. Any special sections must be set up to meet
10050 these requirements. */
10051 osym
.st_value
+= isec
->output_offset
;
10052 if (!bfd_link_relocatable (flinfo
->info
))
10054 osym
.st_value
+= isec
->output_section
->vma
;
10055 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10057 /* STT_TLS symbols are relative to PT_TLS segment base. */
10058 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10059 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10063 indx
= bfd_get_symcount (output_bfd
);
10064 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10071 if (bed
->s
->arch_size
== 32)
10073 r_type_mask
= 0xff;
10079 r_type_mask
= 0xffffffff;
10084 /* Relocate the contents of each section. */
10085 sym_hashes
= elf_sym_hashes (input_bfd
);
10086 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10088 bfd_byte
*contents
;
10090 if (! o
->linker_mark
)
10092 /* This section was omitted from the link. */
10096 if (bfd_link_relocatable (flinfo
->info
)
10097 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10099 /* Deal with the group signature symbol. */
10100 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10101 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10102 asection
*osec
= o
->output_section
;
10104 if (symndx
>= locsymcount
10105 || (elf_bad_symtab (input_bfd
)
10106 && flinfo
->sections
[symndx
] == NULL
))
10108 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10109 while (h
->root
.type
== bfd_link_hash_indirect
10110 || h
->root
.type
== bfd_link_hash_warning
)
10111 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10112 /* Arrange for symbol to be output. */
10114 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10116 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10118 /* We'll use the output section target_index. */
10119 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10120 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10124 if (flinfo
->indices
[symndx
] == -1)
10126 /* Otherwise output the local symbol now. */
10127 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10128 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10133 name
= bfd_elf_string_from_elf_section (input_bfd
,
10134 symtab_hdr
->sh_link
,
10139 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10141 if (sym
.st_shndx
== SHN_BAD
)
10144 sym
.st_value
+= o
->output_offset
;
10146 indx
= bfd_get_symcount (output_bfd
);
10147 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10152 flinfo
->indices
[symndx
] = indx
;
10156 elf_section_data (osec
)->this_hdr
.sh_info
10157 = flinfo
->indices
[symndx
];
10161 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10162 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10165 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10167 /* Section was created by _bfd_elf_link_create_dynamic_sections
10172 /* Get the contents of the section. They have been cached by a
10173 relaxation routine. Note that o is a section in an input
10174 file, so the contents field will not have been set by any of
10175 the routines which work on output files. */
10176 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10178 contents
= elf_section_data (o
)->this_hdr
.contents
;
10179 if (bed
->caches_rawsize
10181 && o
->rawsize
< o
->size
)
10183 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10184 contents
= flinfo
->contents
;
10189 contents
= flinfo
->contents
;
10190 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10194 if ((o
->flags
& SEC_RELOC
) != 0)
10196 Elf_Internal_Rela
*internal_relocs
;
10197 Elf_Internal_Rela
*rel
, *relend
;
10198 int action_discarded
;
10201 /* Get the swapped relocs. */
10203 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10204 flinfo
->internal_relocs
, FALSE
);
10205 if (internal_relocs
== NULL
10206 && o
->reloc_count
> 0)
10209 /* We need to reverse-copy input .ctors/.dtors sections if
10210 they are placed in .init_array/.finit_array for output. */
10211 if (o
->size
> address_size
10212 && ((strncmp (o
->name
, ".ctors", 6) == 0
10213 && strcmp (o
->output_section
->name
,
10214 ".init_array") == 0)
10215 || (strncmp (o
->name
, ".dtors", 6) == 0
10216 && strcmp (o
->output_section
->name
,
10217 ".fini_array") == 0))
10218 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10220 if (o
->size
!= o
->reloc_count
* address_size
)
10223 /* xgettext:c-format */
10224 (_("error: %B: size of section %A is not "
10225 "multiple of address size"),
10227 bfd_set_error (bfd_error_on_input
);
10230 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10233 action_discarded
= -1;
10234 if (!elf_section_ignore_discarded_relocs (o
))
10235 action_discarded
= (*bed
->action_discarded
) (o
);
10237 /* Run through the relocs evaluating complex reloc symbols and
10238 looking for relocs against symbols from discarded sections
10239 or section symbols from removed link-once sections.
10240 Complain about relocs against discarded sections. Zero
10241 relocs against removed link-once sections. */
10243 rel
= internal_relocs
;
10244 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10245 for ( ; rel
< relend
; rel
++)
10247 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10248 unsigned int s_type
;
10249 asection
**ps
, *sec
;
10250 struct elf_link_hash_entry
*h
= NULL
;
10251 const char *sym_name
;
10253 if (r_symndx
== STN_UNDEF
)
10256 if (r_symndx
>= locsymcount
10257 || (elf_bad_symtab (input_bfd
)
10258 && flinfo
->sections
[r_symndx
] == NULL
))
10260 h
= sym_hashes
[r_symndx
- extsymoff
];
10262 /* Badly formatted input files can contain relocs that
10263 reference non-existant symbols. Check here so that
10264 we do not seg fault. */
10269 sprintf_vma (buffer
, rel
->r_info
);
10271 /* xgettext:c-format */
10272 (_("error: %B contains a reloc (0x%s) for section %A "
10273 "that references a non-existent global symbol"),
10274 input_bfd
, o
, buffer
);
10275 bfd_set_error (bfd_error_bad_value
);
10279 while (h
->root
.type
== bfd_link_hash_indirect
10280 || h
->root
.type
== bfd_link_hash_warning
)
10281 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10285 /* If a plugin symbol is referenced from a non-IR file,
10286 mark the symbol as undefined. Note that the
10287 linker may attach linker created dynamic sections
10288 to the plugin bfd. Symbols defined in linker
10289 created sections are not plugin symbols. */
10290 if (h
->root
.non_ir_ref
10291 && (h
->root
.type
== bfd_link_hash_defined
10292 || h
->root
.type
== bfd_link_hash_defweak
)
10293 && (h
->root
.u
.def
.section
->flags
10294 & SEC_LINKER_CREATED
) == 0
10295 && h
->root
.u
.def
.section
->owner
!= NULL
10296 && (h
->root
.u
.def
.section
->owner
->flags
10297 & BFD_PLUGIN
) != 0)
10299 h
->root
.type
= bfd_link_hash_undefined
;
10300 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10304 if (h
->root
.type
== bfd_link_hash_defined
10305 || h
->root
.type
== bfd_link_hash_defweak
)
10306 ps
= &h
->root
.u
.def
.section
;
10308 sym_name
= h
->root
.root
.string
;
10312 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10314 s_type
= ELF_ST_TYPE (sym
->st_info
);
10315 ps
= &flinfo
->sections
[r_symndx
];
10316 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10320 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10321 && !bfd_link_relocatable (flinfo
->info
))
10324 bfd_vma dot
= (rel
->r_offset
10325 + o
->output_offset
+ o
->output_section
->vma
);
10327 printf ("Encountered a complex symbol!");
10328 printf (" (input_bfd %s, section %s, reloc %ld\n",
10329 input_bfd
->filename
, o
->name
,
10330 (long) (rel
- internal_relocs
));
10331 printf (" symbol: idx %8.8lx, name %s\n",
10332 r_symndx
, sym_name
);
10333 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10334 (unsigned long) rel
->r_info
,
10335 (unsigned long) rel
->r_offset
);
10337 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10338 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10341 /* Symbol evaluated OK. Update to absolute value. */
10342 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10347 if (action_discarded
!= -1 && ps
!= NULL
)
10349 /* Complain if the definition comes from a
10350 discarded section. */
10351 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10353 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10354 if (action_discarded
& COMPLAIN
)
10355 (*flinfo
->info
->callbacks
->einfo
)
10356 /* xgettext:c-format */
10357 (_("%X`%s' referenced in section `%A' of %B: "
10358 "defined in discarded section `%A' of %B\n"),
10359 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10361 /* Try to do the best we can to support buggy old
10362 versions of gcc. Pretend that the symbol is
10363 really defined in the kept linkonce section.
10364 FIXME: This is quite broken. Modifying the
10365 symbol here means we will be changing all later
10366 uses of the symbol, not just in this section. */
10367 if (action_discarded
& PRETEND
)
10371 kept
= _bfd_elf_check_kept_section (sec
,
10383 /* Relocate the section by invoking a back end routine.
10385 The back end routine is responsible for adjusting the
10386 section contents as necessary, and (if using Rela relocs
10387 and generating a relocatable output file) adjusting the
10388 reloc addend as necessary.
10390 The back end routine does not have to worry about setting
10391 the reloc address or the reloc symbol index.
10393 The back end routine is given a pointer to the swapped in
10394 internal symbols, and can access the hash table entries
10395 for the external symbols via elf_sym_hashes (input_bfd).
10397 When generating relocatable output, the back end routine
10398 must handle STB_LOCAL/STT_SECTION symbols specially. The
10399 output symbol is going to be a section symbol
10400 corresponding to the output section, which will require
10401 the addend to be adjusted. */
10403 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10404 input_bfd
, o
, contents
,
10412 || bfd_link_relocatable (flinfo
->info
)
10413 || flinfo
->info
->emitrelocations
)
10415 Elf_Internal_Rela
*irela
;
10416 Elf_Internal_Rela
*irelaend
, *irelamid
;
10417 bfd_vma last_offset
;
10418 struct elf_link_hash_entry
**rel_hash
;
10419 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10420 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10421 unsigned int next_erel
;
10422 bfd_boolean rela_normal
;
10423 struct bfd_elf_section_data
*esdi
, *esdo
;
10425 esdi
= elf_section_data (o
);
10426 esdo
= elf_section_data (o
->output_section
);
10427 rela_normal
= FALSE
;
10429 /* Adjust the reloc addresses and symbol indices. */
10431 irela
= internal_relocs
;
10432 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10433 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10434 /* We start processing the REL relocs, if any. When we reach
10435 IRELAMID in the loop, we switch to the RELA relocs. */
10437 if (esdi
->rel
.hdr
!= NULL
)
10438 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10439 * bed
->s
->int_rels_per_ext_rel
);
10440 rel_hash_list
= rel_hash
;
10441 rela_hash_list
= NULL
;
10442 last_offset
= o
->output_offset
;
10443 if (!bfd_link_relocatable (flinfo
->info
))
10444 last_offset
+= o
->output_section
->vma
;
10445 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10447 unsigned long r_symndx
;
10449 Elf_Internal_Sym sym
;
10451 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10457 if (irela
== irelamid
)
10459 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10460 rela_hash_list
= rel_hash
;
10461 rela_normal
= bed
->rela_normal
;
10464 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10467 if (irela
->r_offset
>= (bfd_vma
) -2)
10469 /* This is a reloc for a deleted entry or somesuch.
10470 Turn it into an R_*_NONE reloc, at the same
10471 offset as the last reloc. elf_eh_frame.c and
10472 bfd_elf_discard_info rely on reloc offsets
10474 irela
->r_offset
= last_offset
;
10476 irela
->r_addend
= 0;
10480 irela
->r_offset
+= o
->output_offset
;
10482 /* Relocs in an executable have to be virtual addresses. */
10483 if (!bfd_link_relocatable (flinfo
->info
))
10484 irela
->r_offset
+= o
->output_section
->vma
;
10486 last_offset
= irela
->r_offset
;
10488 r_symndx
= irela
->r_info
>> r_sym_shift
;
10489 if (r_symndx
== STN_UNDEF
)
10492 if (r_symndx
>= locsymcount
10493 || (elf_bad_symtab (input_bfd
)
10494 && flinfo
->sections
[r_symndx
] == NULL
))
10496 struct elf_link_hash_entry
*rh
;
10497 unsigned long indx
;
10499 /* This is a reloc against a global symbol. We
10500 have not yet output all the local symbols, so
10501 we do not know the symbol index of any global
10502 symbol. We set the rel_hash entry for this
10503 reloc to point to the global hash table entry
10504 for this symbol. The symbol index is then
10505 set at the end of bfd_elf_final_link. */
10506 indx
= r_symndx
- extsymoff
;
10507 rh
= elf_sym_hashes (input_bfd
)[indx
];
10508 while (rh
->root
.type
== bfd_link_hash_indirect
10509 || rh
->root
.type
== bfd_link_hash_warning
)
10510 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10512 /* Setting the index to -2 tells
10513 elf_link_output_extsym that this symbol is
10514 used by a reloc. */
10515 BFD_ASSERT (rh
->indx
< 0);
10523 /* This is a reloc against a local symbol. */
10526 sym
= isymbuf
[r_symndx
];
10527 sec
= flinfo
->sections
[r_symndx
];
10528 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10530 /* I suppose the backend ought to fill in the
10531 section of any STT_SECTION symbol against a
10532 processor specific section. */
10533 r_symndx
= STN_UNDEF
;
10534 if (bfd_is_abs_section (sec
))
10536 else if (sec
== NULL
|| sec
->owner
== NULL
)
10538 bfd_set_error (bfd_error_bad_value
);
10543 asection
*osec
= sec
->output_section
;
10545 /* If we have discarded a section, the output
10546 section will be the absolute section. In
10547 case of discarded SEC_MERGE sections, use
10548 the kept section. relocate_section should
10549 have already handled discarded linkonce
10551 if (bfd_is_abs_section (osec
)
10552 && sec
->kept_section
!= NULL
10553 && sec
->kept_section
->output_section
!= NULL
)
10555 osec
= sec
->kept_section
->output_section
;
10556 irela
->r_addend
-= osec
->vma
;
10559 if (!bfd_is_abs_section (osec
))
10561 r_symndx
= osec
->target_index
;
10562 if (r_symndx
== STN_UNDEF
)
10564 irela
->r_addend
+= osec
->vma
;
10565 osec
= _bfd_nearby_section (output_bfd
, osec
,
10567 irela
->r_addend
-= osec
->vma
;
10568 r_symndx
= osec
->target_index
;
10573 /* Adjust the addend according to where the
10574 section winds up in the output section. */
10576 irela
->r_addend
+= sec
->output_offset
;
10580 if (flinfo
->indices
[r_symndx
] == -1)
10582 unsigned long shlink
;
10587 if (flinfo
->info
->strip
== strip_all
)
10589 /* You can't do ld -r -s. */
10590 bfd_set_error (bfd_error_invalid_operation
);
10594 /* This symbol was skipped earlier, but
10595 since it is needed by a reloc, we
10596 must output it now. */
10597 shlink
= symtab_hdr
->sh_link
;
10598 name
= (bfd_elf_string_from_elf_section
10599 (input_bfd
, shlink
, sym
.st_name
));
10603 osec
= sec
->output_section
;
10605 _bfd_elf_section_from_bfd_section (output_bfd
,
10607 if (sym
.st_shndx
== SHN_BAD
)
10610 sym
.st_value
+= sec
->output_offset
;
10611 if (!bfd_link_relocatable (flinfo
->info
))
10613 sym
.st_value
+= osec
->vma
;
10614 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10616 /* STT_TLS symbols are relative to PT_TLS
10618 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10619 ->tls_sec
!= NULL
);
10620 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10625 indx
= bfd_get_symcount (output_bfd
);
10626 ret
= elf_link_output_symstrtab (flinfo
, name
,
10632 flinfo
->indices
[r_symndx
] = indx
;
10637 r_symndx
= flinfo
->indices
[r_symndx
];
10640 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10641 | (irela
->r_info
& r_type_mask
));
10644 /* Swap out the relocs. */
10645 input_rel_hdr
= esdi
->rel
.hdr
;
10646 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10648 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10653 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10654 * bed
->s
->int_rels_per_ext_rel
);
10655 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10658 input_rela_hdr
= esdi
->rela
.hdr
;
10659 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10661 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10670 /* Write out the modified section contents. */
10671 if (bed
->elf_backend_write_section
10672 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10675 /* Section written out. */
10677 else switch (o
->sec_info_type
)
10679 case SEC_INFO_TYPE_STABS
:
10680 if (! (_bfd_write_section_stabs
10682 &elf_hash_table (flinfo
->info
)->stab_info
,
10683 o
, &elf_section_data (o
)->sec_info
, contents
)))
10686 case SEC_INFO_TYPE_MERGE
:
10687 if (! _bfd_write_merged_section (output_bfd
, o
,
10688 elf_section_data (o
)->sec_info
))
10691 case SEC_INFO_TYPE_EH_FRAME
:
10693 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10698 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10700 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10708 if (! (o
->flags
& SEC_EXCLUDE
))
10710 file_ptr offset
= (file_ptr
) o
->output_offset
;
10711 bfd_size_type todo
= o
->size
;
10713 offset
*= bfd_octets_per_byte (output_bfd
);
10715 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10717 /* Reverse-copy input section to output. */
10720 todo
-= address_size
;
10721 if (! bfd_set_section_contents (output_bfd
,
10729 offset
+= address_size
;
10733 else if (! bfd_set_section_contents (output_bfd
,
10747 /* Generate a reloc when linking an ELF file. This is a reloc
10748 requested by the linker, and does not come from any input file. This
10749 is used to build constructor and destructor tables when linking
10753 elf_reloc_link_order (bfd
*output_bfd
,
10754 struct bfd_link_info
*info
,
10755 asection
*output_section
,
10756 struct bfd_link_order
*link_order
)
10758 reloc_howto_type
*howto
;
10762 struct bfd_elf_section_reloc_data
*reldata
;
10763 struct elf_link_hash_entry
**rel_hash_ptr
;
10764 Elf_Internal_Shdr
*rel_hdr
;
10765 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10766 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10769 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10771 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10774 bfd_set_error (bfd_error_bad_value
);
10778 addend
= link_order
->u
.reloc
.p
->addend
;
10781 reldata
= &esdo
->rel
;
10782 else if (esdo
->rela
.hdr
)
10783 reldata
= &esdo
->rela
;
10790 /* Figure out the symbol index. */
10791 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10792 if (link_order
->type
== bfd_section_reloc_link_order
)
10794 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10795 BFD_ASSERT (indx
!= 0);
10796 *rel_hash_ptr
= NULL
;
10800 struct elf_link_hash_entry
*h
;
10802 /* Treat a reloc against a defined symbol as though it were
10803 actually against the section. */
10804 h
= ((struct elf_link_hash_entry
*)
10805 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10806 link_order
->u
.reloc
.p
->u
.name
,
10807 FALSE
, FALSE
, TRUE
));
10809 && (h
->root
.type
== bfd_link_hash_defined
10810 || h
->root
.type
== bfd_link_hash_defweak
))
10814 section
= h
->root
.u
.def
.section
;
10815 indx
= section
->output_section
->target_index
;
10816 *rel_hash_ptr
= NULL
;
10817 /* It seems that we ought to add the symbol value to the
10818 addend here, but in practice it has already been added
10819 because it was passed to constructor_callback. */
10820 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10822 else if (h
!= NULL
)
10824 /* Setting the index to -2 tells elf_link_output_extsym that
10825 this symbol is used by a reloc. */
10832 (*info
->callbacks
->unattached_reloc
)
10833 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10838 /* If this is an inplace reloc, we must write the addend into the
10840 if (howto
->partial_inplace
&& addend
!= 0)
10842 bfd_size_type size
;
10843 bfd_reloc_status_type rstat
;
10846 const char *sym_name
;
10848 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10849 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10850 if (buf
== NULL
&& size
!= 0)
10852 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10859 case bfd_reloc_outofrange
:
10862 case bfd_reloc_overflow
:
10863 if (link_order
->type
== bfd_section_reloc_link_order
)
10864 sym_name
= bfd_section_name (output_bfd
,
10865 link_order
->u
.reloc
.p
->u
.section
);
10867 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10868 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10869 howto
->name
, addend
, NULL
, NULL
,
10874 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10876 * bfd_octets_per_byte (output_bfd
),
10883 /* The address of a reloc is relative to the section in a
10884 relocatable file, and is a virtual address in an executable
10886 offset
= link_order
->offset
;
10887 if (! bfd_link_relocatable (info
))
10888 offset
+= output_section
->vma
;
10890 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10892 irel
[i
].r_offset
= offset
;
10893 irel
[i
].r_info
= 0;
10894 irel
[i
].r_addend
= 0;
10896 if (bed
->s
->arch_size
== 32)
10897 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10899 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10901 rel_hdr
= reldata
->hdr
;
10902 erel
= rel_hdr
->contents
;
10903 if (rel_hdr
->sh_type
== SHT_REL
)
10905 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10906 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10910 irel
[0].r_addend
= addend
;
10911 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10912 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10921 /* Get the output vma of the section pointed to by the sh_link field. */
10924 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10926 Elf_Internal_Shdr
**elf_shdrp
;
10930 s
= p
->u
.indirect
.section
;
10931 elf_shdrp
= elf_elfsections (s
->owner
);
10932 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10933 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10935 The Intel C compiler generates SHT_IA_64_UNWIND with
10936 SHF_LINK_ORDER. But it doesn't set the sh_link or
10937 sh_info fields. Hence we could get the situation
10938 where elfsec is 0. */
10941 const struct elf_backend_data
*bed
10942 = get_elf_backend_data (s
->owner
);
10943 if (bed
->link_order_error_handler
)
10944 bed
->link_order_error_handler
10945 /* xgettext:c-format */
10946 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10951 s
= elf_shdrp
[elfsec
]->bfd_section
;
10952 return s
->output_section
->vma
+ s
->output_offset
;
10957 /* Compare two sections based on the locations of the sections they are
10958 linked to. Used by elf_fixup_link_order. */
10961 compare_link_order (const void * a
, const void * b
)
10966 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10967 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10970 return apos
> bpos
;
10974 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10975 order as their linked sections. Returns false if this could not be done
10976 because an output section includes both ordered and unordered
10977 sections. Ideally we'd do this in the linker proper. */
10980 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10982 int seen_linkorder
;
10985 struct bfd_link_order
*p
;
10987 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10989 struct bfd_link_order
**sections
;
10990 asection
*s
, *other_sec
, *linkorder_sec
;
10994 linkorder_sec
= NULL
;
10996 seen_linkorder
= 0;
10997 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10999 if (p
->type
== bfd_indirect_link_order
)
11001 s
= p
->u
.indirect
.section
;
11003 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11004 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11005 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11006 && elfsec
< elf_numsections (sub
)
11007 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11008 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11022 if (seen_other
&& seen_linkorder
)
11024 if (other_sec
&& linkorder_sec
)
11026 /* xgettext:c-format */
11027 (_("%A has both ordered [`%A' in %B] "
11028 "and unordered [`%A' in %B] sections"),
11030 linkorder_sec
->owner
, other_sec
,
11034 (_("%A has both ordered and unordered sections"), o
);
11035 bfd_set_error (bfd_error_bad_value
);
11040 if (!seen_linkorder
)
11043 sections
= (struct bfd_link_order
**)
11044 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11045 if (sections
== NULL
)
11047 seen_linkorder
= 0;
11049 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11051 sections
[seen_linkorder
++] = p
;
11053 /* Sort the input sections in the order of their linked section. */
11054 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11055 compare_link_order
);
11057 /* Change the offsets of the sections. */
11059 for (n
= 0; n
< seen_linkorder
; n
++)
11061 s
= sections
[n
]->u
.indirect
.section
;
11062 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11063 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11064 sections
[n
]->offset
= offset
;
11065 offset
+= sections
[n
]->size
;
11072 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11073 Returns TRUE upon success, FALSE otherwise. */
11076 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11078 bfd_boolean ret
= FALSE
;
11080 const struct elf_backend_data
*bed
;
11082 enum bfd_architecture arch
;
11084 asymbol
**sympp
= NULL
;
11088 elf_symbol_type
*osymbuf
;
11090 implib_bfd
= info
->out_implib_bfd
;
11091 bed
= get_elf_backend_data (abfd
);
11093 if (!bfd_set_format (implib_bfd
, bfd_object
))
11096 flags
= bfd_get_file_flags (abfd
);
11097 flags
&= ~HAS_RELOC
;
11098 if (!bfd_set_start_address (implib_bfd
, 0)
11099 || !bfd_set_file_flags (implib_bfd
, flags
))
11102 /* Copy architecture of output file to import library file. */
11103 arch
= bfd_get_arch (abfd
);
11104 mach
= bfd_get_mach (abfd
);
11105 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11106 && (abfd
->target_defaulted
11107 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11110 /* Get symbol table size. */
11111 symsize
= bfd_get_symtab_upper_bound (abfd
);
11115 /* Read in the symbol table. */
11116 sympp
= (asymbol
**) xmalloc (symsize
);
11117 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11121 /* Allow the BFD backend to copy any private header data it
11122 understands from the output BFD to the import library BFD. */
11123 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11126 /* Filter symbols to appear in the import library. */
11127 if (bed
->elf_backend_filter_implib_symbols
)
11128 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11131 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11134 bfd_set_error (bfd_error_no_symbols
);
11135 _bfd_error_handler (_("%B: no symbol found for import library"),
11141 /* Make symbols absolute. */
11142 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11143 sizeof (*osymbuf
));
11144 for (src_count
= 0; src_count
< symcount
; src_count
++)
11146 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11147 sizeof (*osymbuf
));
11148 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11149 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11150 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11151 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11152 osymbuf
[src_count
].symbol
.value
;
11153 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11156 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11158 /* Allow the BFD backend to copy any private data it understands
11159 from the output BFD to the import library BFD. This is done last
11160 to permit the routine to look at the filtered symbol table. */
11161 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11164 if (!bfd_close (implib_bfd
))
11175 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11179 if (flinfo
->symstrtab
!= NULL
)
11180 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11181 if (flinfo
->contents
!= NULL
)
11182 free (flinfo
->contents
);
11183 if (flinfo
->external_relocs
!= NULL
)
11184 free (flinfo
->external_relocs
);
11185 if (flinfo
->internal_relocs
!= NULL
)
11186 free (flinfo
->internal_relocs
);
11187 if (flinfo
->external_syms
!= NULL
)
11188 free (flinfo
->external_syms
);
11189 if (flinfo
->locsym_shndx
!= NULL
)
11190 free (flinfo
->locsym_shndx
);
11191 if (flinfo
->internal_syms
!= NULL
)
11192 free (flinfo
->internal_syms
);
11193 if (flinfo
->indices
!= NULL
)
11194 free (flinfo
->indices
);
11195 if (flinfo
->sections
!= NULL
)
11196 free (flinfo
->sections
);
11197 if (flinfo
->symshndxbuf
!= NULL
)
11198 free (flinfo
->symshndxbuf
);
11199 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11201 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11202 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11203 free (esdo
->rel
.hashes
);
11204 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11205 free (esdo
->rela
.hashes
);
11209 /* Do the final step of an ELF link. */
11212 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11214 bfd_boolean dynamic
;
11215 bfd_boolean emit_relocs
;
11217 struct elf_final_link_info flinfo
;
11219 struct bfd_link_order
*p
;
11221 bfd_size_type max_contents_size
;
11222 bfd_size_type max_external_reloc_size
;
11223 bfd_size_type max_internal_reloc_count
;
11224 bfd_size_type max_sym_count
;
11225 bfd_size_type max_sym_shndx_count
;
11226 Elf_Internal_Sym elfsym
;
11228 Elf_Internal_Shdr
*symtab_hdr
;
11229 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11231 struct elf_outext_info eoinfo
;
11232 bfd_boolean merged
;
11233 size_t relativecount
= 0;
11234 asection
*reldyn
= 0;
11236 asection
*attr_section
= NULL
;
11237 bfd_vma attr_size
= 0;
11238 const char *std_attrs_section
;
11239 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11241 if (!is_elf_hash_table (htab
))
11244 if (bfd_link_pic (info
))
11245 abfd
->flags
|= DYNAMIC
;
11247 dynamic
= htab
->dynamic_sections_created
;
11248 dynobj
= htab
->dynobj
;
11250 emit_relocs
= (bfd_link_relocatable (info
)
11251 || info
->emitrelocations
);
11253 flinfo
.info
= info
;
11254 flinfo
.output_bfd
= abfd
;
11255 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11256 if (flinfo
.symstrtab
== NULL
)
11261 flinfo
.hash_sec
= NULL
;
11262 flinfo
.symver_sec
= NULL
;
11266 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11267 /* Note that dynsym_sec can be NULL (on VMS). */
11268 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11269 /* Note that it is OK if symver_sec is NULL. */
11272 flinfo
.contents
= NULL
;
11273 flinfo
.external_relocs
= NULL
;
11274 flinfo
.internal_relocs
= NULL
;
11275 flinfo
.external_syms
= NULL
;
11276 flinfo
.locsym_shndx
= NULL
;
11277 flinfo
.internal_syms
= NULL
;
11278 flinfo
.indices
= NULL
;
11279 flinfo
.sections
= NULL
;
11280 flinfo
.symshndxbuf
= NULL
;
11281 flinfo
.filesym_count
= 0;
11283 /* The object attributes have been merged. Remove the input
11284 sections from the link, and set the contents of the output
11286 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11287 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11289 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11290 || strcmp (o
->name
, ".gnu.attributes") == 0)
11292 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11294 asection
*input_section
;
11296 if (p
->type
!= bfd_indirect_link_order
)
11298 input_section
= p
->u
.indirect
.section
;
11299 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11300 elf_link_input_bfd ignores this section. */
11301 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11304 attr_size
= bfd_elf_obj_attr_size (abfd
);
11307 bfd_set_section_size (abfd
, o
, attr_size
);
11309 /* Skip this section later on. */
11310 o
->map_head
.link_order
= NULL
;
11313 o
->flags
|= SEC_EXCLUDE
;
11317 /* Count up the number of relocations we will output for each output
11318 section, so that we know the sizes of the reloc sections. We
11319 also figure out some maximum sizes. */
11320 max_contents_size
= 0;
11321 max_external_reloc_size
= 0;
11322 max_internal_reloc_count
= 0;
11324 max_sym_shndx_count
= 0;
11326 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11328 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11329 o
->reloc_count
= 0;
11331 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11333 unsigned int reloc_count
= 0;
11334 unsigned int additional_reloc_count
= 0;
11335 struct bfd_elf_section_data
*esdi
= NULL
;
11337 if (p
->type
== bfd_section_reloc_link_order
11338 || p
->type
== bfd_symbol_reloc_link_order
)
11340 else if (p
->type
== bfd_indirect_link_order
)
11344 sec
= p
->u
.indirect
.section
;
11345 /* See PR 20908 for a reproducer. */
11346 if (bfd_get_flavour (sec
->owner
) != bfd_target_elf_flavour
)
11348 _bfd_error_handler (_("%B: not in ELF format"), sec
->owner
);
11351 esdi
= elf_section_data (sec
);
11353 /* Mark all sections which are to be included in the
11354 link. This will normally be every section. We need
11355 to do this so that we can identify any sections which
11356 the linker has decided to not include. */
11357 sec
->linker_mark
= TRUE
;
11359 if (sec
->flags
& SEC_MERGE
)
11362 if (esdo
->this_hdr
.sh_type
== SHT_REL
11363 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11364 /* Some backends use reloc_count in relocation sections
11365 to count particular types of relocs. Of course,
11366 reloc sections themselves can't have relocations. */
11368 else if (emit_relocs
)
11370 reloc_count
= sec
->reloc_count
;
11371 if (bed
->elf_backend_count_additional_relocs
)
11374 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11375 additional_reloc_count
+= c
;
11378 else if (bed
->elf_backend_count_relocs
)
11379 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11381 if (sec
->rawsize
> max_contents_size
)
11382 max_contents_size
= sec
->rawsize
;
11383 if (sec
->size
> max_contents_size
)
11384 max_contents_size
= sec
->size
;
11386 /* We are interested in just local symbols, not all
11388 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11389 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11393 if (elf_bad_symtab (sec
->owner
))
11394 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11395 / bed
->s
->sizeof_sym
);
11397 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11399 if (sym_count
> max_sym_count
)
11400 max_sym_count
= sym_count
;
11402 if (sym_count
> max_sym_shndx_count
11403 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11404 max_sym_shndx_count
= sym_count
;
11406 if ((sec
->flags
& SEC_RELOC
) != 0)
11408 size_t ext_size
= 0;
11410 if (esdi
->rel
.hdr
!= NULL
)
11411 ext_size
= esdi
->rel
.hdr
->sh_size
;
11412 if (esdi
->rela
.hdr
!= NULL
)
11413 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11415 if (ext_size
> max_external_reloc_size
)
11416 max_external_reloc_size
= ext_size
;
11417 if (sec
->reloc_count
> max_internal_reloc_count
)
11418 max_internal_reloc_count
= sec
->reloc_count
;
11423 if (reloc_count
== 0)
11426 reloc_count
+= additional_reloc_count
;
11427 o
->reloc_count
+= reloc_count
;
11429 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11433 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11434 esdo
->rel
.count
+= additional_reloc_count
;
11436 if (esdi
->rela
.hdr
)
11438 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11439 esdo
->rela
.count
+= additional_reloc_count
;
11445 esdo
->rela
.count
+= reloc_count
;
11447 esdo
->rel
.count
+= reloc_count
;
11451 if (o
->reloc_count
> 0)
11452 o
->flags
|= SEC_RELOC
;
11455 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11456 set it (this is probably a bug) and if it is set
11457 assign_section_numbers will create a reloc section. */
11458 o
->flags
&=~ SEC_RELOC
;
11461 /* If the SEC_ALLOC flag is not set, force the section VMA to
11462 zero. This is done in elf_fake_sections as well, but forcing
11463 the VMA to 0 here will ensure that relocs against these
11464 sections are handled correctly. */
11465 if ((o
->flags
& SEC_ALLOC
) == 0
11466 && ! o
->user_set_vma
)
11470 if (! bfd_link_relocatable (info
) && merged
)
11471 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11473 /* Figure out the file positions for everything but the symbol table
11474 and the relocs. We set symcount to force assign_section_numbers
11475 to create a symbol table. */
11476 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11477 BFD_ASSERT (! abfd
->output_has_begun
);
11478 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11481 /* Set sizes, and assign file positions for reloc sections. */
11482 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11484 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11485 if ((o
->flags
& SEC_RELOC
) != 0)
11488 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11492 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11496 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11497 to count upwards while actually outputting the relocations. */
11498 esdo
->rel
.count
= 0;
11499 esdo
->rela
.count
= 0;
11501 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11503 /* Cache the section contents so that they can be compressed
11504 later. Use bfd_malloc since it will be freed by
11505 bfd_compress_section_contents. */
11506 unsigned char *contents
= esdo
->this_hdr
.contents
;
11507 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11510 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11511 if (contents
== NULL
)
11513 esdo
->this_hdr
.contents
= contents
;
11517 /* We have now assigned file positions for all the sections except
11518 .symtab, .strtab, and non-loaded reloc sections. We start the
11519 .symtab section at the current file position, and write directly
11520 to it. We build the .strtab section in memory. */
11521 bfd_get_symcount (abfd
) = 0;
11522 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11523 /* sh_name is set in prep_headers. */
11524 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11525 /* sh_flags, sh_addr and sh_size all start off zero. */
11526 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11527 /* sh_link is set in assign_section_numbers. */
11528 /* sh_info is set below. */
11529 /* sh_offset is set just below. */
11530 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11532 if (max_sym_count
< 20)
11533 max_sym_count
= 20;
11534 htab
->strtabsize
= max_sym_count
;
11535 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11536 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11537 if (htab
->strtab
== NULL
)
11539 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11541 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11542 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11544 if (info
->strip
!= strip_all
|| emit_relocs
)
11546 file_ptr off
= elf_next_file_pos (abfd
);
11548 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11550 /* Note that at this point elf_next_file_pos (abfd) is
11551 incorrect. We do not yet know the size of the .symtab section.
11552 We correct next_file_pos below, after we do know the size. */
11554 /* Start writing out the symbol table. The first symbol is always a
11556 elfsym
.st_value
= 0;
11557 elfsym
.st_size
= 0;
11558 elfsym
.st_info
= 0;
11559 elfsym
.st_other
= 0;
11560 elfsym
.st_shndx
= SHN_UNDEF
;
11561 elfsym
.st_target_internal
= 0;
11562 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11563 bfd_und_section_ptr
, NULL
) != 1)
11566 /* Output a symbol for each section. We output these even if we are
11567 discarding local symbols, since they are used for relocs. These
11568 symbols have no names. We store the index of each one in the
11569 index field of the section, so that we can find it again when
11570 outputting relocs. */
11572 elfsym
.st_size
= 0;
11573 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11574 elfsym
.st_other
= 0;
11575 elfsym
.st_value
= 0;
11576 elfsym
.st_target_internal
= 0;
11577 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11579 o
= bfd_section_from_elf_index (abfd
, i
);
11582 o
->target_index
= bfd_get_symcount (abfd
);
11583 elfsym
.st_shndx
= i
;
11584 if (!bfd_link_relocatable (info
))
11585 elfsym
.st_value
= o
->vma
;
11586 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11593 /* Allocate some memory to hold information read in from the input
11595 if (max_contents_size
!= 0)
11597 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11598 if (flinfo
.contents
== NULL
)
11602 if (max_external_reloc_size
!= 0)
11604 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11605 if (flinfo
.external_relocs
== NULL
)
11609 if (max_internal_reloc_count
!= 0)
11611 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11612 amt
*= sizeof (Elf_Internal_Rela
);
11613 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11614 if (flinfo
.internal_relocs
== NULL
)
11618 if (max_sym_count
!= 0)
11620 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11621 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11622 if (flinfo
.external_syms
== NULL
)
11625 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11626 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11627 if (flinfo
.internal_syms
== NULL
)
11630 amt
= max_sym_count
* sizeof (long);
11631 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11632 if (flinfo
.indices
== NULL
)
11635 amt
= max_sym_count
* sizeof (asection
*);
11636 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11637 if (flinfo
.sections
== NULL
)
11641 if (max_sym_shndx_count
!= 0)
11643 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11644 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11645 if (flinfo
.locsym_shndx
== NULL
)
11651 bfd_vma base
, end
= 0;
11654 for (sec
= htab
->tls_sec
;
11655 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11658 bfd_size_type size
= sec
->size
;
11661 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11663 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11666 size
= ord
->offset
+ ord
->size
;
11668 end
= sec
->vma
+ size
;
11670 base
= htab
->tls_sec
->vma
;
11671 /* Only align end of TLS section if static TLS doesn't have special
11672 alignment requirements. */
11673 if (bed
->static_tls_alignment
== 1)
11674 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11675 htab
->tls_size
= end
- base
;
11678 /* Reorder SHF_LINK_ORDER sections. */
11679 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11681 if (!elf_fixup_link_order (abfd
, o
))
11685 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11688 /* Since ELF permits relocations to be against local symbols, we
11689 must have the local symbols available when we do the relocations.
11690 Since we would rather only read the local symbols once, and we
11691 would rather not keep them in memory, we handle all the
11692 relocations for a single input file at the same time.
11694 Unfortunately, there is no way to know the total number of local
11695 symbols until we have seen all of them, and the local symbol
11696 indices precede the global symbol indices. This means that when
11697 we are generating relocatable output, and we see a reloc against
11698 a global symbol, we can not know the symbol index until we have
11699 finished examining all the local symbols to see which ones we are
11700 going to output. To deal with this, we keep the relocations in
11701 memory, and don't output them until the end of the link. This is
11702 an unfortunate waste of memory, but I don't see a good way around
11703 it. Fortunately, it only happens when performing a relocatable
11704 link, which is not the common case. FIXME: If keep_memory is set
11705 we could write the relocs out and then read them again; I don't
11706 know how bad the memory loss will be. */
11708 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11709 sub
->output_has_begun
= FALSE
;
11710 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11712 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11714 if (p
->type
== bfd_indirect_link_order
11715 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11716 == bfd_target_elf_flavour
)
11717 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11719 if (! sub
->output_has_begun
)
11721 if (! elf_link_input_bfd (&flinfo
, sub
))
11723 sub
->output_has_begun
= TRUE
;
11726 else if (p
->type
== bfd_section_reloc_link_order
11727 || p
->type
== bfd_symbol_reloc_link_order
)
11729 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11734 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11736 if (p
->type
== bfd_indirect_link_order
11737 && (bfd_get_flavour (sub
)
11738 == bfd_target_elf_flavour
)
11739 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11740 != bed
->s
->elfclass
))
11742 const char *iclass
, *oclass
;
11744 switch (bed
->s
->elfclass
)
11746 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11747 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11748 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11752 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11754 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11755 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11756 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11760 bfd_set_error (bfd_error_wrong_format
);
11762 /* xgettext:c-format */
11763 (_("%B: file class %s incompatible with %s"),
11764 sub
, iclass
, oclass
);
11773 /* Free symbol buffer if needed. */
11774 if (!info
->reduce_memory_overheads
)
11776 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11777 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11778 && elf_tdata (sub
)->symbuf
)
11780 free (elf_tdata (sub
)->symbuf
);
11781 elf_tdata (sub
)->symbuf
= NULL
;
11785 /* Output any global symbols that got converted to local in a
11786 version script or due to symbol visibility. We do this in a
11787 separate step since ELF requires all local symbols to appear
11788 prior to any global symbols. FIXME: We should only do this if
11789 some global symbols were, in fact, converted to become local.
11790 FIXME: Will this work correctly with the Irix 5 linker? */
11791 eoinfo
.failed
= FALSE
;
11792 eoinfo
.flinfo
= &flinfo
;
11793 eoinfo
.localsyms
= TRUE
;
11794 eoinfo
.file_sym_done
= FALSE
;
11795 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11799 /* If backend needs to output some local symbols not present in the hash
11800 table, do it now. */
11801 if (bed
->elf_backend_output_arch_local_syms
11802 && (info
->strip
!= strip_all
|| emit_relocs
))
11804 typedef int (*out_sym_func
)
11805 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11806 struct elf_link_hash_entry
*);
11808 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11809 (abfd
, info
, &flinfo
,
11810 (out_sym_func
) elf_link_output_symstrtab
)))
11814 /* That wrote out all the local symbols. Finish up the symbol table
11815 with the global symbols. Even if we want to strip everything we
11816 can, we still need to deal with those global symbols that got
11817 converted to local in a version script. */
11819 /* The sh_info field records the index of the first non local symbol. */
11820 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11823 && htab
->dynsym
!= NULL
11824 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11826 Elf_Internal_Sym sym
;
11827 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11829 o
= htab
->dynsym
->output_section
;
11830 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11832 /* Write out the section symbols for the output sections. */
11833 if (bfd_link_pic (info
)
11834 || htab
->is_relocatable_executable
)
11840 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11842 sym
.st_target_internal
= 0;
11844 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11850 dynindx
= elf_section_data (s
)->dynindx
;
11853 indx
= elf_section_data (s
)->this_idx
;
11854 BFD_ASSERT (indx
> 0);
11855 sym
.st_shndx
= indx
;
11856 if (! check_dynsym (abfd
, &sym
))
11858 sym
.st_value
= s
->vma
;
11859 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11860 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11864 /* Write out the local dynsyms. */
11865 if (htab
->dynlocal
)
11867 struct elf_link_local_dynamic_entry
*e
;
11868 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11873 /* Copy the internal symbol and turn off visibility.
11874 Note that we saved a word of storage and overwrote
11875 the original st_name with the dynstr_index. */
11877 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11879 s
= bfd_section_from_elf_index (e
->input_bfd
,
11884 elf_section_data (s
->output_section
)->this_idx
;
11885 if (! check_dynsym (abfd
, &sym
))
11887 sym
.st_value
= (s
->output_section
->vma
11889 + e
->isym
.st_value
);
11892 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11893 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11898 /* We get the global symbols from the hash table. */
11899 eoinfo
.failed
= FALSE
;
11900 eoinfo
.localsyms
= FALSE
;
11901 eoinfo
.flinfo
= &flinfo
;
11902 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11906 /* If backend needs to output some symbols not present in the hash
11907 table, do it now. */
11908 if (bed
->elf_backend_output_arch_syms
11909 && (info
->strip
!= strip_all
|| emit_relocs
))
11911 typedef int (*out_sym_func
)
11912 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11913 struct elf_link_hash_entry
*);
11915 if (! ((*bed
->elf_backend_output_arch_syms
)
11916 (abfd
, info
, &flinfo
,
11917 (out_sym_func
) elf_link_output_symstrtab
)))
11921 /* Finalize the .strtab section. */
11922 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11924 /* Swap out the .strtab section. */
11925 if (!elf_link_swap_symbols_out (&flinfo
))
11928 /* Now we know the size of the symtab section. */
11929 if (bfd_get_symcount (abfd
) > 0)
11931 /* Finish up and write out the symbol string table (.strtab)
11933 Elf_Internal_Shdr
*symstrtab_hdr
;
11934 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11936 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11937 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11939 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11940 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11941 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11942 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11943 symtab_shndx_hdr
->sh_size
= amt
;
11945 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11948 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11949 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11953 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11954 /* sh_name was set in prep_headers. */
11955 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11956 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11957 symstrtab_hdr
->sh_addr
= 0;
11958 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11959 symstrtab_hdr
->sh_entsize
= 0;
11960 symstrtab_hdr
->sh_link
= 0;
11961 symstrtab_hdr
->sh_info
= 0;
11962 /* sh_offset is set just below. */
11963 symstrtab_hdr
->sh_addralign
= 1;
11965 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11967 elf_next_file_pos (abfd
) = off
;
11969 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11970 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11974 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11976 _bfd_error_handler (_("%B: failed to generate import library"),
11977 info
->out_implib_bfd
);
11981 /* Adjust the relocs to have the correct symbol indices. */
11982 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11984 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11986 if ((o
->flags
& SEC_RELOC
) == 0)
11989 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11990 if (esdo
->rel
.hdr
!= NULL
11991 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
11993 if (esdo
->rela
.hdr
!= NULL
11994 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
11997 /* Set the reloc_count field to 0 to prevent write_relocs from
11998 trying to swap the relocs out itself. */
11999 o
->reloc_count
= 0;
12002 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12003 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12005 /* If we are linking against a dynamic object, or generating a
12006 shared library, finish up the dynamic linking information. */
12009 bfd_byte
*dyncon
, *dynconend
;
12011 /* Fix up .dynamic entries. */
12012 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12013 BFD_ASSERT (o
!= NULL
);
12015 dyncon
= o
->contents
;
12016 dynconend
= o
->contents
+ o
->size
;
12017 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12019 Elf_Internal_Dyn dyn
;
12023 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12030 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12032 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12034 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12035 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12038 dyn
.d_un
.d_val
= relativecount
;
12045 name
= info
->init_function
;
12048 name
= info
->fini_function
;
12051 struct elf_link_hash_entry
*h
;
12053 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12055 && (h
->root
.type
== bfd_link_hash_defined
12056 || h
->root
.type
== bfd_link_hash_defweak
))
12058 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12059 o
= h
->root
.u
.def
.section
;
12060 if (o
->output_section
!= NULL
)
12061 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12062 + o
->output_offset
);
12065 /* The symbol is imported from another shared
12066 library and does not apply to this one. */
12067 dyn
.d_un
.d_ptr
= 0;
12074 case DT_PREINIT_ARRAYSZ
:
12075 name
= ".preinit_array";
12077 case DT_INIT_ARRAYSZ
:
12078 name
= ".init_array";
12080 case DT_FINI_ARRAYSZ
:
12081 name
= ".fini_array";
12083 o
= bfd_get_section_by_name (abfd
, name
);
12087 (_("could not find section %s"), name
);
12092 (_("warning: %s section has zero size"), name
);
12093 dyn
.d_un
.d_val
= o
->size
;
12096 case DT_PREINIT_ARRAY
:
12097 name
= ".preinit_array";
12099 case DT_INIT_ARRAY
:
12100 name
= ".init_array";
12102 case DT_FINI_ARRAY
:
12103 name
= ".fini_array";
12105 o
= bfd_get_section_by_name (abfd
, name
);
12112 name
= ".gnu.hash";
12121 name
= ".gnu.version_d";
12124 name
= ".gnu.version_r";
12127 name
= ".gnu.version";
12129 o
= bfd_get_linker_section (dynobj
, name
);
12134 (_("could not find section %s"), name
);
12137 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12140 (_("warning: section '%s' is being made into a note"), name
);
12141 bfd_set_error (bfd_error_nonrepresentable_section
);
12144 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12151 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12155 dyn
.d_un
.d_val
= 0;
12156 dyn
.d_un
.d_ptr
= 0;
12157 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12159 Elf_Internal_Shdr
*hdr
;
12161 hdr
= elf_elfsections (abfd
)[i
];
12162 if (hdr
->sh_type
== type
12163 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12165 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12166 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12169 if (dyn
.d_un
.d_ptr
== 0
12170 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12171 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12175 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12177 /* Don't count procedure linkage table relocs in the
12178 overall reloc count. */
12179 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12180 dyn
.d_un
.d_val
-= htab
->srelplt
->size
;
12181 /* If .rela.plt is the first .rela section, exclude
12182 it from DT_RELA. */
12183 else if (dyn
.d_un
.d_ptr
== (htab
->srelplt
->output_section
->vma
12184 + htab
->srelplt
->output_offset
))
12185 dyn
.d_un
.d_ptr
+= htab
->srelplt
->size
;
12189 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12193 /* If we have created any dynamic sections, then output them. */
12194 if (dynobj
!= NULL
)
12196 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12199 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12200 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12201 || info
->error_textrel
)
12202 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12204 bfd_byte
*dyncon
, *dynconend
;
12206 dyncon
= o
->contents
;
12207 dynconend
= o
->contents
+ o
->size
;
12208 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12210 Elf_Internal_Dyn dyn
;
12212 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12214 if (dyn
.d_tag
== DT_TEXTREL
)
12216 if (info
->error_textrel
)
12217 info
->callbacks
->einfo
12218 (_("%P%X: read-only segment has dynamic relocations.\n"));
12220 info
->callbacks
->einfo
12221 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12227 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12229 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12231 || o
->output_section
== bfd_abs_section_ptr
)
12233 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12235 /* At this point, we are only interested in sections
12236 created by _bfd_elf_link_create_dynamic_sections. */
12239 if (htab
->stab_info
.stabstr
== o
)
12241 if (htab
->eh_info
.hdr_sec
== o
)
12243 if (strcmp (o
->name
, ".dynstr") != 0)
12245 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12247 (file_ptr
) o
->output_offset
12248 * bfd_octets_per_byte (abfd
),
12254 /* The contents of the .dynstr section are actually in a
12258 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12259 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12260 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12266 if (bfd_link_relocatable (info
))
12268 bfd_boolean failed
= FALSE
;
12270 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12275 /* If we have optimized stabs strings, output them. */
12276 if (htab
->stab_info
.stabstr
!= NULL
)
12278 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12282 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12285 elf_final_link_free (abfd
, &flinfo
);
12287 elf_linker (abfd
) = TRUE
;
12291 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12292 if (contents
== NULL
)
12293 return FALSE
; /* Bail out and fail. */
12294 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12295 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12302 elf_final_link_free (abfd
, &flinfo
);
12306 /* Initialize COOKIE for input bfd ABFD. */
12309 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12310 struct bfd_link_info
*info
, bfd
*abfd
)
12312 Elf_Internal_Shdr
*symtab_hdr
;
12313 const struct elf_backend_data
*bed
;
12315 bed
= get_elf_backend_data (abfd
);
12316 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12318 cookie
->abfd
= abfd
;
12319 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12320 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12321 if (cookie
->bad_symtab
)
12323 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12324 cookie
->extsymoff
= 0;
12328 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12329 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12332 if (bed
->s
->arch_size
== 32)
12333 cookie
->r_sym_shift
= 8;
12335 cookie
->r_sym_shift
= 32;
12337 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12338 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12340 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12341 cookie
->locsymcount
, 0,
12343 if (cookie
->locsyms
== NULL
)
12345 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12348 if (info
->keep_memory
)
12349 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12354 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12357 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12359 Elf_Internal_Shdr
*symtab_hdr
;
12361 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12362 if (cookie
->locsyms
!= NULL
12363 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12364 free (cookie
->locsyms
);
12367 /* Initialize the relocation information in COOKIE for input section SEC
12368 of input bfd ABFD. */
12371 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12372 struct bfd_link_info
*info
, bfd
*abfd
,
12375 const struct elf_backend_data
*bed
;
12377 if (sec
->reloc_count
== 0)
12379 cookie
->rels
= NULL
;
12380 cookie
->relend
= NULL
;
12384 bed
= get_elf_backend_data (abfd
);
12386 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12387 info
->keep_memory
);
12388 if (cookie
->rels
== NULL
)
12390 cookie
->rel
= cookie
->rels
;
12391 cookie
->relend
= (cookie
->rels
12392 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12394 cookie
->rel
= cookie
->rels
;
12398 /* Free the memory allocated by init_reloc_cookie_rels,
12402 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12405 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12406 free (cookie
->rels
);
12409 /* Initialize the whole of COOKIE for input section SEC. */
12412 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12413 struct bfd_link_info
*info
,
12416 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12418 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12423 fini_reloc_cookie (cookie
, sec
->owner
);
12428 /* Free the memory allocated by init_reloc_cookie_for_section,
12432 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12435 fini_reloc_cookie_rels (cookie
, sec
);
12436 fini_reloc_cookie (cookie
, sec
->owner
);
12439 /* Garbage collect unused sections. */
12441 /* Default gc_mark_hook. */
12444 _bfd_elf_gc_mark_hook (asection
*sec
,
12445 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12446 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12447 struct elf_link_hash_entry
*h
,
12448 Elf_Internal_Sym
*sym
)
12452 switch (h
->root
.type
)
12454 case bfd_link_hash_defined
:
12455 case bfd_link_hash_defweak
:
12456 return h
->root
.u
.def
.section
;
12458 case bfd_link_hash_common
:
12459 return h
->root
.u
.c
.p
->section
;
12466 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12471 /* For undefined __start_<name> and __stop_<name> symbols, return the
12472 first input section matching <name>. Return NULL otherwise. */
12475 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12476 struct elf_link_hash_entry
*h
)
12479 const char *sec_name
;
12481 if (h
->root
.type
!= bfd_link_hash_undefined
12482 && h
->root
.type
!= bfd_link_hash_undefweak
)
12485 s
= h
->root
.u
.undef
.section
;
12488 if (s
== (asection
*) 0 - 1)
12494 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12495 sec_name
= h
->root
.root
.string
+ 8;
12496 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12497 sec_name
= h
->root
.root
.string
+ 7;
12499 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12503 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12505 s
= bfd_get_section_by_name (i
, sec_name
);
12508 h
->root
.u
.undef
.section
= s
;
12515 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12520 /* COOKIE->rel describes a relocation against section SEC, which is
12521 a section we've decided to keep. Return the section that contains
12522 the relocation symbol, or NULL if no section contains it. */
12525 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12526 elf_gc_mark_hook_fn gc_mark_hook
,
12527 struct elf_reloc_cookie
*cookie
,
12528 bfd_boolean
*start_stop
)
12530 unsigned long r_symndx
;
12531 struct elf_link_hash_entry
*h
;
12533 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12534 if (r_symndx
== STN_UNDEF
)
12537 if (r_symndx
>= cookie
->locsymcount
12538 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12540 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12543 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12547 while (h
->root
.type
== bfd_link_hash_indirect
12548 || h
->root
.type
== bfd_link_hash_warning
)
12549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12551 /* If this symbol is weak and there is a non-weak definition, we
12552 keep the non-weak definition because many backends put
12553 dynamic reloc info on the non-weak definition for code
12554 handling copy relocs. */
12555 if (h
->u
.weakdef
!= NULL
)
12556 h
->u
.weakdef
->mark
= 1;
12558 if (start_stop
!= NULL
)
12560 /* To work around a glibc bug, mark all XXX input sections
12561 when there is an as yet undefined reference to __start_XXX
12562 or __stop_XXX symbols. The linker will later define such
12563 symbols for orphan input sections that have a name
12564 representable as a C identifier. */
12565 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12569 *start_stop
= !s
->gc_mark
;
12574 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12577 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12578 &cookie
->locsyms
[r_symndx
]);
12581 /* COOKIE->rel describes a relocation against section SEC, which is
12582 a section we've decided to keep. Mark the section that contains
12583 the relocation symbol. */
12586 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12588 elf_gc_mark_hook_fn gc_mark_hook
,
12589 struct elf_reloc_cookie
*cookie
)
12592 bfd_boolean start_stop
= FALSE
;
12594 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12595 while (rsec
!= NULL
)
12597 if (!rsec
->gc_mark
)
12599 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12600 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12602 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12607 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12612 /* The mark phase of garbage collection. For a given section, mark
12613 it and any sections in this section's group, and all the sections
12614 which define symbols to which it refers. */
12617 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12619 elf_gc_mark_hook_fn gc_mark_hook
)
12622 asection
*group_sec
, *eh_frame
;
12626 /* Mark all the sections in the group. */
12627 group_sec
= elf_section_data (sec
)->next_in_group
;
12628 if (group_sec
&& !group_sec
->gc_mark
)
12629 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12632 /* Look through the section relocs. */
12634 eh_frame
= elf_eh_frame_section (sec
->owner
);
12635 if ((sec
->flags
& SEC_RELOC
) != 0
12636 && sec
->reloc_count
> 0
12637 && sec
!= eh_frame
)
12639 struct elf_reloc_cookie cookie
;
12641 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12645 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12646 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12651 fini_reloc_cookie_for_section (&cookie
, sec
);
12655 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12657 struct elf_reloc_cookie cookie
;
12659 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12663 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12664 gc_mark_hook
, &cookie
))
12666 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12670 eh_frame
= elf_section_eh_frame_entry (sec
);
12671 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12672 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12678 /* Scan and mark sections in a special or debug section group. */
12681 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12683 /* Point to first section of section group. */
12685 /* Used to iterate the section group. */
12688 bfd_boolean is_special_grp
= TRUE
;
12689 bfd_boolean is_debug_grp
= TRUE
;
12691 /* First scan to see if group contains any section other than debug
12692 and special section. */
12693 ssec
= msec
= elf_next_in_group (grp
);
12696 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12697 is_debug_grp
= FALSE
;
12699 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12700 is_special_grp
= FALSE
;
12702 msec
= elf_next_in_group (msec
);
12704 while (msec
!= ssec
);
12706 /* If this is a pure debug section group or pure special section group,
12707 keep all sections in this group. */
12708 if (is_debug_grp
|| is_special_grp
)
12713 msec
= elf_next_in_group (msec
);
12715 while (msec
!= ssec
);
12719 /* Keep debug and special sections. */
12722 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12723 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12727 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12730 bfd_boolean some_kept
;
12731 bfd_boolean debug_frag_seen
;
12733 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12736 /* Ensure all linker created sections are kept,
12737 see if any other section is already marked,
12738 and note if we have any fragmented debug sections. */
12739 debug_frag_seen
= some_kept
= FALSE
;
12740 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12742 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12744 else if (isec
->gc_mark
)
12747 if (debug_frag_seen
== FALSE
12748 && (isec
->flags
& SEC_DEBUGGING
)
12749 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12750 debug_frag_seen
= TRUE
;
12753 /* If no section in this file will be kept, then we can
12754 toss out the debug and special sections. */
12758 /* Keep debug and special sections like .comment when they are
12759 not part of a group. Also keep section groups that contain
12760 just debug sections or special sections. */
12761 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12763 if ((isec
->flags
& SEC_GROUP
) != 0)
12764 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12765 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12766 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12767 && elf_next_in_group (isec
) == NULL
)
12771 if (! debug_frag_seen
)
12774 /* Look for CODE sections which are going to be discarded,
12775 and find and discard any fragmented debug sections which
12776 are associated with that code section. */
12777 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12778 if ((isec
->flags
& SEC_CODE
) != 0
12779 && isec
->gc_mark
== 0)
12784 ilen
= strlen (isec
->name
);
12786 /* Association is determined by the name of the debug section
12787 containing the name of the code section as a suffix. For
12788 example .debug_line.text.foo is a debug section associated
12790 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12794 if (dsec
->gc_mark
== 0
12795 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12798 dlen
= strlen (dsec
->name
);
12801 && strncmp (dsec
->name
+ (dlen
- ilen
),
12802 isec
->name
, ilen
) == 0)
12812 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12814 struct elf_gc_sweep_symbol_info
12816 struct bfd_link_info
*info
;
12817 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12822 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12825 && (((h
->root
.type
== bfd_link_hash_defined
12826 || h
->root
.type
== bfd_link_hash_defweak
)
12827 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12828 && h
->root
.u
.def
.section
->gc_mark
))
12829 || h
->root
.type
== bfd_link_hash_undefined
12830 || h
->root
.type
== bfd_link_hash_undefweak
))
12832 struct elf_gc_sweep_symbol_info
*inf
;
12834 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12835 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12836 h
->def_regular
= 0;
12837 h
->ref_regular
= 0;
12838 h
->ref_regular_nonweak
= 0;
12844 /* The sweep phase of garbage collection. Remove all garbage sections. */
12846 typedef bfd_boolean (*gc_sweep_hook_fn
)
12847 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12850 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12853 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12854 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12855 unsigned long section_sym_count
;
12856 struct elf_gc_sweep_symbol_info sweep_info
;
12858 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12862 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12863 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12866 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12868 /* When any section in a section group is kept, we keep all
12869 sections in the section group. If the first member of
12870 the section group is excluded, we will also exclude the
12872 if (o
->flags
& SEC_GROUP
)
12874 asection
*first
= elf_next_in_group (o
);
12875 o
->gc_mark
= first
->gc_mark
;
12881 /* Skip sweeping sections already excluded. */
12882 if (o
->flags
& SEC_EXCLUDE
)
12885 /* Since this is early in the link process, it is simple
12886 to remove a section from the output. */
12887 o
->flags
|= SEC_EXCLUDE
;
12889 if (info
->print_gc_sections
&& o
->size
!= 0)
12890 /* xgettext:c-format */
12891 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12893 /* But we also have to update some of the relocation
12894 info we collected before. */
12896 && (o
->flags
& SEC_RELOC
) != 0
12897 && o
->reloc_count
!= 0
12898 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12899 && (o
->flags
& SEC_DEBUGGING
) != 0)
12900 && !bfd_is_abs_section (o
->output_section
))
12902 Elf_Internal_Rela
*internal_relocs
;
12906 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12907 info
->keep_memory
);
12908 if (internal_relocs
== NULL
)
12911 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12913 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12914 free (internal_relocs
);
12922 /* Remove the symbols that were in the swept sections from the dynamic
12923 symbol table. GCFIXME: Anyone know how to get them out of the
12924 static symbol table as well? */
12925 sweep_info
.info
= info
;
12926 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12927 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12930 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12934 /* Propagate collected vtable information. This is called through
12935 elf_link_hash_traverse. */
12938 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12940 /* Those that are not vtables. */
12941 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12944 /* Those vtables that do not have parents, we cannot merge. */
12945 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12948 /* If we've already been done, exit. */
12949 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12952 /* Make sure the parent's table is up to date. */
12953 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12955 if (h
->vtable
->used
== NULL
)
12957 /* None of this table's entries were referenced. Re-use the
12959 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12960 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12965 bfd_boolean
*cu
, *pu
;
12967 /* Or the parent's entries into ours. */
12968 cu
= h
->vtable
->used
;
12970 pu
= h
->vtable
->parent
->vtable
->used
;
12973 const struct elf_backend_data
*bed
;
12974 unsigned int log_file_align
;
12976 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12977 log_file_align
= bed
->s
->log_file_align
;
12978 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12993 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12996 bfd_vma hstart
, hend
;
12997 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12998 const struct elf_backend_data
*bed
;
12999 unsigned int log_file_align
;
13001 /* Take care of both those symbols that do not describe vtables as
13002 well as those that are not loaded. */
13003 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13006 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13007 || h
->root
.type
== bfd_link_hash_defweak
);
13009 sec
= h
->root
.u
.def
.section
;
13010 hstart
= h
->root
.u
.def
.value
;
13011 hend
= hstart
+ h
->size
;
13013 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13015 return *(bfd_boolean
*) okp
= FALSE
;
13016 bed
= get_elf_backend_data (sec
->owner
);
13017 log_file_align
= bed
->s
->log_file_align
;
13019 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13021 for (rel
= relstart
; rel
< relend
; ++rel
)
13022 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13024 /* If the entry is in use, do nothing. */
13025 if (h
->vtable
->used
13026 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13028 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13029 if (h
->vtable
->used
[entry
])
13032 /* Otherwise, kill it. */
13033 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13039 /* Mark sections containing dynamically referenced symbols. When
13040 building shared libraries, we must assume that any visible symbol is
13044 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13046 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13047 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13049 if ((h
->root
.type
== bfd_link_hash_defined
13050 || h
->root
.type
== bfd_link_hash_defweak
)
13052 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13053 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13054 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13055 && (!bfd_link_executable (info
)
13056 || info
->export_dynamic
13059 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13060 && (h
->versioned
>= versioned
13061 || !bfd_hide_sym_by_version (info
->version_info
,
13062 h
->root
.root
.string
)))))
13063 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13068 /* Keep all sections containing symbols undefined on the command-line,
13069 and the section containing the entry symbol. */
13072 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13074 struct bfd_sym_chain
*sym
;
13076 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13078 struct elf_link_hash_entry
*h
;
13080 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13081 FALSE
, FALSE
, FALSE
);
13084 && (h
->root
.type
== bfd_link_hash_defined
13085 || h
->root
.type
== bfd_link_hash_defweak
)
13086 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13087 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13088 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13093 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13094 struct bfd_link_info
*info
)
13096 bfd
*ibfd
= info
->input_bfds
;
13098 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13101 struct elf_reloc_cookie cookie
;
13103 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13106 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13109 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13111 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13112 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13114 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13115 fini_reloc_cookie_rels (&cookie
, sec
);
13122 /* Do mark and sweep of unused sections. */
13125 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13127 bfd_boolean ok
= TRUE
;
13129 elf_gc_mark_hook_fn gc_mark_hook
;
13130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13131 struct elf_link_hash_table
*htab
;
13133 if (!bed
->can_gc_sections
13134 || !is_elf_hash_table (info
->hash
))
13136 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13140 bed
->gc_keep (info
);
13141 htab
= elf_hash_table (info
);
13143 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13144 at the .eh_frame section if we can mark the FDEs individually. */
13145 for (sub
= info
->input_bfds
;
13146 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13147 sub
= sub
->link
.next
)
13150 struct elf_reloc_cookie cookie
;
13152 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13153 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13155 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13156 if (elf_section_data (sec
)->sec_info
13157 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13158 elf_eh_frame_section (sub
) = sec
;
13159 fini_reloc_cookie_for_section (&cookie
, sec
);
13160 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13164 /* Apply transitive closure to the vtable entry usage info. */
13165 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13169 /* Kill the vtable relocations that were not used. */
13170 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13174 /* Mark dynamically referenced symbols. */
13175 if (htab
->dynamic_sections_created
)
13176 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13178 /* Grovel through relocs to find out who stays ... */
13179 gc_mark_hook
= bed
->gc_mark_hook
;
13180 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13184 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13185 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13188 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13189 Also treat note sections as a root, if the section is not part
13191 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13193 && (o
->flags
& SEC_EXCLUDE
) == 0
13194 && ((o
->flags
& SEC_KEEP
) != 0
13195 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13196 && elf_next_in_group (o
) == NULL
)))
13198 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13203 /* Allow the backend to mark additional target specific sections. */
13204 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13206 /* ... and mark SEC_EXCLUDE for those that go. */
13207 return elf_gc_sweep (abfd
, info
);
13210 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13213 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13215 struct elf_link_hash_entry
*h
,
13218 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13219 struct elf_link_hash_entry
**search
, *child
;
13220 size_t extsymcount
;
13221 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13223 /* The sh_info field of the symtab header tells us where the
13224 external symbols start. We don't care about the local symbols at
13226 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13227 if (!elf_bad_symtab (abfd
))
13228 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13230 sym_hashes
= elf_sym_hashes (abfd
);
13231 sym_hashes_end
= sym_hashes
+ extsymcount
;
13233 /* Hunt down the child symbol, which is in this section at the same
13234 offset as the relocation. */
13235 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13237 if ((child
= *search
) != NULL
13238 && (child
->root
.type
== bfd_link_hash_defined
13239 || child
->root
.type
== bfd_link_hash_defweak
)
13240 && child
->root
.u
.def
.section
== sec
13241 && child
->root
.u
.def
.value
== offset
)
13245 /* xgettext:c-format */
13246 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13247 abfd
, sec
, (unsigned long) offset
);
13248 bfd_set_error (bfd_error_invalid_operation
);
13252 if (!child
->vtable
)
13254 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13255 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13256 if (!child
->vtable
)
13261 /* This *should* only be the absolute section. It could potentially
13262 be that someone has defined a non-global vtable though, which
13263 would be bad. It isn't worth paging in the local symbols to be
13264 sure though; that case should simply be handled by the assembler. */
13266 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13269 child
->vtable
->parent
= h
;
13274 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13277 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13278 asection
*sec ATTRIBUTE_UNUSED
,
13279 struct elf_link_hash_entry
*h
,
13282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13283 unsigned int log_file_align
= bed
->s
->log_file_align
;
13287 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13288 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13293 if (addend
>= h
->vtable
->size
)
13295 size_t size
, bytes
, file_align
;
13296 bfd_boolean
*ptr
= h
->vtable
->used
;
13298 /* While the symbol is undefined, we have to be prepared to handle
13300 file_align
= 1 << log_file_align
;
13301 if (h
->root
.type
== bfd_link_hash_undefined
)
13302 size
= addend
+ file_align
;
13306 if (addend
>= size
)
13308 /* Oops! We've got a reference past the defined end of
13309 the table. This is probably a bug -- shall we warn? */
13310 size
= addend
+ file_align
;
13313 size
= (size
+ file_align
- 1) & -file_align
;
13315 /* Allocate one extra entry for use as a "done" flag for the
13316 consolidation pass. */
13317 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13321 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13327 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13328 * sizeof (bfd_boolean
));
13329 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13333 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13338 /* And arrange for that done flag to be at index -1. */
13339 h
->vtable
->used
= ptr
+ 1;
13340 h
->vtable
->size
= size
;
13343 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13348 /* Map an ELF section header flag to its corresponding string. */
13352 flagword flag_value
;
13353 } elf_flags_to_name_table
;
13355 static elf_flags_to_name_table elf_flags_to_names
[] =
13357 { "SHF_WRITE", SHF_WRITE
},
13358 { "SHF_ALLOC", SHF_ALLOC
},
13359 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13360 { "SHF_MERGE", SHF_MERGE
},
13361 { "SHF_STRINGS", SHF_STRINGS
},
13362 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13363 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13364 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13365 { "SHF_GROUP", SHF_GROUP
},
13366 { "SHF_TLS", SHF_TLS
},
13367 { "SHF_MASKOS", SHF_MASKOS
},
13368 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13371 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13373 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13374 struct flag_info
*flaginfo
,
13377 const bfd_vma sh_flags
= elf_section_flags (section
);
13379 if (!flaginfo
->flags_initialized
)
13381 bfd
*obfd
= info
->output_bfd
;
13382 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13383 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13385 int without_hex
= 0;
13387 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13390 flagword (*lookup
) (char *);
13392 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13393 if (lookup
!= NULL
)
13395 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13399 if (tf
->with
== with_flags
)
13400 with_hex
|= hexval
;
13401 else if (tf
->with
== without_flags
)
13402 without_hex
|= hexval
;
13407 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13409 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13411 if (tf
->with
== with_flags
)
13412 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13413 else if (tf
->with
== without_flags
)
13414 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13421 info
->callbacks
->einfo
13422 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13426 flaginfo
->flags_initialized
= TRUE
;
13427 flaginfo
->only_with_flags
|= with_hex
;
13428 flaginfo
->not_with_flags
|= without_hex
;
13431 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13434 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13440 struct alloc_got_off_arg
{
13442 struct bfd_link_info
*info
;
13445 /* We need a special top-level link routine to convert got reference counts
13446 to real got offsets. */
13449 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13451 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13452 bfd
*obfd
= gofarg
->info
->output_bfd
;
13453 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13455 if (h
->got
.refcount
> 0)
13457 h
->got
.offset
= gofarg
->gotoff
;
13458 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13461 h
->got
.offset
= (bfd_vma
) -1;
13466 /* And an accompanying bit to work out final got entry offsets once
13467 we're done. Should be called from final_link. */
13470 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13471 struct bfd_link_info
*info
)
13474 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13476 struct alloc_got_off_arg gofarg
;
13478 BFD_ASSERT (abfd
== info
->output_bfd
);
13480 if (! is_elf_hash_table (info
->hash
))
13483 /* The GOT offset is relative to the .got section, but the GOT header is
13484 put into the .got.plt section, if the backend uses it. */
13485 if (bed
->want_got_plt
)
13488 gotoff
= bed
->got_header_size
;
13490 /* Do the local .got entries first. */
13491 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13493 bfd_signed_vma
*local_got
;
13494 size_t j
, locsymcount
;
13495 Elf_Internal_Shdr
*symtab_hdr
;
13497 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13500 local_got
= elf_local_got_refcounts (i
);
13504 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13505 if (elf_bad_symtab (i
))
13506 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13508 locsymcount
= symtab_hdr
->sh_info
;
13510 for (j
= 0; j
< locsymcount
; ++j
)
13512 if (local_got
[j
] > 0)
13514 local_got
[j
] = gotoff
;
13515 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13518 local_got
[j
] = (bfd_vma
) -1;
13522 /* Then the global .got entries. .plt refcounts are handled by
13523 adjust_dynamic_symbol */
13524 gofarg
.gotoff
= gotoff
;
13525 gofarg
.info
= info
;
13526 elf_link_hash_traverse (elf_hash_table (info
),
13527 elf_gc_allocate_got_offsets
,
13532 /* Many folk need no more in the way of final link than this, once
13533 got entry reference counting is enabled. */
13536 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13538 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13541 /* Invoke the regular ELF backend linker to do all the work. */
13542 return bfd_elf_final_link (abfd
, info
);
13546 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13548 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13550 if (rcookie
->bad_symtab
)
13551 rcookie
->rel
= rcookie
->rels
;
13553 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13555 unsigned long r_symndx
;
13557 if (! rcookie
->bad_symtab
)
13558 if (rcookie
->rel
->r_offset
> offset
)
13560 if (rcookie
->rel
->r_offset
!= offset
)
13563 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13564 if (r_symndx
== STN_UNDEF
)
13567 if (r_symndx
>= rcookie
->locsymcount
13568 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13570 struct elf_link_hash_entry
*h
;
13572 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13574 while (h
->root
.type
== bfd_link_hash_indirect
13575 || h
->root
.type
== bfd_link_hash_warning
)
13576 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13578 if ((h
->root
.type
== bfd_link_hash_defined
13579 || h
->root
.type
== bfd_link_hash_defweak
)
13580 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13581 || h
->root
.u
.def
.section
->kept_section
!= NULL
13582 || discarded_section (h
->root
.u
.def
.section
)))
13587 /* It's not a relocation against a global symbol,
13588 but it could be a relocation against a local
13589 symbol for a discarded section. */
13591 Elf_Internal_Sym
*isym
;
13593 /* Need to: get the symbol; get the section. */
13594 isym
= &rcookie
->locsyms
[r_symndx
];
13595 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13597 && (isec
->kept_section
!= NULL
13598 || discarded_section (isec
)))
13606 /* Discard unneeded references to discarded sections.
13607 Returns -1 on error, 1 if any section's size was changed, 0 if
13608 nothing changed. This function assumes that the relocations are in
13609 sorted order, which is true for all known assemblers. */
13612 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13614 struct elf_reloc_cookie cookie
;
13619 if (info
->traditional_format
13620 || !is_elf_hash_table (info
->hash
))
13623 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13628 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13631 || i
->reloc_count
== 0
13632 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13636 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13639 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13642 if (_bfd_discard_section_stabs (abfd
, i
,
13643 elf_section_data (i
)->sec_info
,
13644 bfd_elf_reloc_symbol_deleted_p
,
13648 fini_reloc_cookie_for_section (&cookie
, i
);
13653 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13654 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13659 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13665 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13668 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13671 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13672 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13673 bfd_elf_reloc_symbol_deleted_p
,
13677 fini_reloc_cookie_for_section (&cookie
, i
);
13681 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13683 const struct elf_backend_data
*bed
;
13685 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13688 bed
= get_elf_backend_data (abfd
);
13690 if (bed
->elf_backend_discard_info
!= NULL
)
13692 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13695 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13698 fini_reloc_cookie (&cookie
, abfd
);
13702 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13703 _bfd_elf_end_eh_frame_parsing (info
);
13705 if (info
->eh_frame_hdr_type
13706 && !bfd_link_relocatable (info
)
13707 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13714 _bfd_elf_section_already_linked (bfd
*abfd
,
13716 struct bfd_link_info
*info
)
13719 const char *name
, *key
;
13720 struct bfd_section_already_linked
*l
;
13721 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13723 if (sec
->output_section
== bfd_abs_section_ptr
)
13726 flags
= sec
->flags
;
13728 /* Return if it isn't a linkonce section. A comdat group section
13729 also has SEC_LINK_ONCE set. */
13730 if ((flags
& SEC_LINK_ONCE
) == 0)
13733 /* Don't put group member sections on our list of already linked
13734 sections. They are handled as a group via their group section. */
13735 if (elf_sec_group (sec
) != NULL
)
13738 /* For a SHT_GROUP section, use the group signature as the key. */
13740 if ((flags
& SEC_GROUP
) != 0
13741 && elf_next_in_group (sec
) != NULL
13742 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13743 key
= elf_group_name (elf_next_in_group (sec
));
13746 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13747 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13748 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13751 /* Must be a user linkonce section that doesn't follow gcc's
13752 naming convention. In this case we won't be matching
13753 single member groups. */
13757 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13759 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13761 /* We may have 2 different types of sections on the list: group
13762 sections with a signature of <key> (<key> is some string),
13763 and linkonce sections named .gnu.linkonce.<type>.<key>.
13764 Match like sections. LTO plugin sections are an exception.
13765 They are always named .gnu.linkonce.t.<key> and match either
13766 type of section. */
13767 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13768 && ((flags
& SEC_GROUP
) != 0
13769 || strcmp (name
, l
->sec
->name
) == 0))
13770 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13772 /* The section has already been linked. See if we should
13773 issue a warning. */
13774 if (!_bfd_handle_already_linked (sec
, l
, info
))
13777 if (flags
& SEC_GROUP
)
13779 asection
*first
= elf_next_in_group (sec
);
13780 asection
*s
= first
;
13784 s
->output_section
= bfd_abs_section_ptr
;
13785 /* Record which group discards it. */
13786 s
->kept_section
= l
->sec
;
13787 s
= elf_next_in_group (s
);
13788 /* These lists are circular. */
13798 /* A single member comdat group section may be discarded by a
13799 linkonce section and vice versa. */
13800 if ((flags
& SEC_GROUP
) != 0)
13802 asection
*first
= elf_next_in_group (sec
);
13804 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13805 /* Check this single member group against linkonce sections. */
13806 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13807 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13808 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13810 first
->output_section
= bfd_abs_section_ptr
;
13811 first
->kept_section
= l
->sec
;
13812 sec
->output_section
= bfd_abs_section_ptr
;
13817 /* Check this linkonce section against single member groups. */
13818 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13819 if (l
->sec
->flags
& SEC_GROUP
)
13821 asection
*first
= elf_next_in_group (l
->sec
);
13824 && elf_next_in_group (first
) == first
13825 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13827 sec
->output_section
= bfd_abs_section_ptr
;
13828 sec
->kept_section
= first
;
13833 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13834 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13835 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13836 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13837 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13838 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13839 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13840 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13841 The reverse order cannot happen as there is never a bfd with only the
13842 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13843 matter as here were are looking only for cross-bfd sections. */
13845 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13846 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13847 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13848 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13850 if (abfd
!= l
->sec
->owner
)
13851 sec
->output_section
= bfd_abs_section_ptr
;
13855 /* This is the first section with this name. Record it. */
13856 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13857 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13858 return sec
->output_section
== bfd_abs_section_ptr
;
13862 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13864 return sym
->st_shndx
== SHN_COMMON
;
13868 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13874 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13876 return bfd_com_section_ptr
;
13880 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13881 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13882 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13883 bfd
*ibfd ATTRIBUTE_UNUSED
,
13884 unsigned long symndx ATTRIBUTE_UNUSED
)
13886 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13887 return bed
->s
->arch_size
/ 8;
13890 /* Routines to support the creation of dynamic relocs. */
13892 /* Returns the name of the dynamic reloc section associated with SEC. */
13894 static const char *
13895 get_dynamic_reloc_section_name (bfd
* abfd
,
13897 bfd_boolean is_rela
)
13900 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13901 const char *prefix
= is_rela
? ".rela" : ".rel";
13903 if (old_name
== NULL
)
13906 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13907 sprintf (name
, "%s%s", prefix
, old_name
);
13912 /* Returns the dynamic reloc section associated with SEC.
13913 If necessary compute the name of the dynamic reloc section based
13914 on SEC's name (looked up in ABFD's string table) and the setting
13918 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13920 bfd_boolean is_rela
)
13922 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13924 if (reloc_sec
== NULL
)
13926 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13930 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13932 if (reloc_sec
!= NULL
)
13933 elf_section_data (sec
)->sreloc
= reloc_sec
;
13940 /* Returns the dynamic reloc section associated with SEC. If the
13941 section does not exist it is created and attached to the DYNOBJ
13942 bfd and stored in the SRELOC field of SEC's elf_section_data
13945 ALIGNMENT is the alignment for the newly created section and
13946 IS_RELA defines whether the name should be .rela.<SEC's name>
13947 or .rel.<SEC's name>. The section name is looked up in the
13948 string table associated with ABFD. */
13951 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13953 unsigned int alignment
,
13955 bfd_boolean is_rela
)
13957 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13959 if (reloc_sec
== NULL
)
13961 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13966 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13968 if (reloc_sec
== NULL
)
13970 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13971 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13972 if ((sec
->flags
& SEC_ALLOC
) != 0)
13973 flags
|= SEC_ALLOC
| SEC_LOAD
;
13975 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13976 if (reloc_sec
!= NULL
)
13978 /* _bfd_elf_get_sec_type_attr chooses a section type by
13979 name. Override as it may be wrong, eg. for a user
13980 section named "auto" we'll get ".relauto" which is
13981 seen to be a .rela section. */
13982 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13983 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13988 elf_section_data (sec
)->sreloc
= reloc_sec
;
13994 /* Copy the ELF symbol type and other attributes for a linker script
13995 assignment from HSRC to HDEST. Generally this should be treated as
13996 if we found a strong non-dynamic definition for HDEST (except that
13997 ld ignores multiple definition errors). */
13999 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14000 struct bfd_link_hash_entry
*hdest
,
14001 struct bfd_link_hash_entry
*hsrc
)
14003 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14004 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14005 Elf_Internal_Sym isym
;
14007 ehdest
->type
= ehsrc
->type
;
14008 ehdest
->target_internal
= ehsrc
->target_internal
;
14010 isym
.st_other
= ehsrc
->other
;
14011 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14014 /* Append a RELA relocation REL to section S in BFD. */
14017 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14019 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14020 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14021 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14022 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14025 /* Append a REL relocation REL to section S in BFD. */
14028 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14030 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14031 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14032 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14033 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);