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 -Bsymbolic was used (which means to bind references to global
2658 symbols to the definition within the shared object), and this
2659 symbol was defined in a regular object, then it actually doesn't
2660 need a PLT entry. Likewise, if the symbol has non-default
2661 visibility. If the symbol has hidden or internal visibility, we
2662 will force it local. */
2664 && bfd_link_pic (eif
->info
)
2665 && is_elf_hash_table (eif
->info
->hash
)
2666 && (SYMBOLIC_BIND (eif
->info
, h
)
2667 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2670 bfd_boolean force_local
;
2672 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2673 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2674 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2677 /* If a weak undefined symbol has non-default visibility, we also
2678 hide it from the dynamic linker. */
2679 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2680 && h
->root
.type
== bfd_link_hash_undefweak
)
2681 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2683 /* If this is a weak defined symbol in a dynamic object, and we know
2684 the real definition in the dynamic object, copy interesting flags
2685 over to the real definition. */
2686 if (h
->u
.weakdef
!= NULL
)
2688 /* If the real definition is defined by a regular object file,
2689 don't do anything special. See the longer description in
2690 _bfd_elf_adjust_dynamic_symbol, below. */
2691 if (h
->u
.weakdef
->def_regular
)
2692 h
->u
.weakdef
= NULL
;
2695 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2697 while (h
->root
.type
== bfd_link_hash_indirect
)
2698 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2700 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2701 || h
->root
.type
== bfd_link_hash_defweak
);
2702 BFD_ASSERT (weakdef
->def_dynamic
);
2703 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2704 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2705 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2712 /* Make the backend pick a good value for a dynamic symbol. This is
2713 called via elf_link_hash_traverse, and also calls itself
2717 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2719 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2721 const struct elf_backend_data
*bed
;
2723 if (! is_elf_hash_table (eif
->info
->hash
))
2726 /* Ignore indirect symbols. These are added by the versioning code. */
2727 if (h
->root
.type
== bfd_link_hash_indirect
)
2730 /* Fix the symbol flags. */
2731 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2734 /* If this symbol does not require a PLT entry, and it is not
2735 defined by a dynamic object, or is not referenced by a regular
2736 object, ignore it. We do have to handle a weak defined symbol,
2737 even if no regular object refers to it, if we decided to add it
2738 to the dynamic symbol table. FIXME: Do we normally need to worry
2739 about symbols which are defined by one dynamic object and
2740 referenced by another one? */
2742 && h
->type
!= STT_GNU_IFUNC
2746 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2748 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2752 /* If we've already adjusted this symbol, don't do it again. This
2753 can happen via a recursive call. */
2754 if (h
->dynamic_adjusted
)
2757 /* Don't look at this symbol again. Note that we must set this
2758 after checking the above conditions, because we may look at a
2759 symbol once, decide not to do anything, and then get called
2760 recursively later after REF_REGULAR is set below. */
2761 h
->dynamic_adjusted
= 1;
2763 /* If this is a weak definition, and we know a real definition, and
2764 the real symbol is not itself defined by a regular object file,
2765 then get a good value for the real definition. We handle the
2766 real symbol first, for the convenience of the backend routine.
2768 Note that there is a confusing case here. If the real definition
2769 is defined by a regular object file, we don't get the real symbol
2770 from the dynamic object, but we do get the weak symbol. If the
2771 processor backend uses a COPY reloc, then if some routine in the
2772 dynamic object changes the real symbol, we will not see that
2773 change in the corresponding weak symbol. This is the way other
2774 ELF linkers work as well, and seems to be a result of the shared
2777 I will clarify this issue. Most SVR4 shared libraries define the
2778 variable _timezone and define timezone as a weak synonym. The
2779 tzset call changes _timezone. If you write
2780 extern int timezone;
2782 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2783 you might expect that, since timezone is a synonym for _timezone,
2784 the same number will print both times. However, if the processor
2785 backend uses a COPY reloc, then actually timezone will be copied
2786 into your process image, and, since you define _timezone
2787 yourself, _timezone will not. Thus timezone and _timezone will
2788 wind up at different memory locations. The tzset call will set
2789 _timezone, leaving timezone unchanged. */
2791 if (h
->u
.weakdef
!= NULL
)
2793 /* If we get to this point, there is an implicit reference to
2794 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2795 h
->u
.weakdef
->ref_regular
= 1;
2797 /* Ensure that the backend adjust_dynamic_symbol function sees
2798 H->U.WEAKDEF before H by recursively calling ourselves. */
2799 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2803 /* If a symbol has no type and no size and does not require a PLT
2804 entry, then we are probably about to do the wrong thing here: we
2805 are probably going to create a COPY reloc for an empty object.
2806 This case can arise when a shared object is built with assembly
2807 code, and the assembly code fails to set the symbol type. */
2809 && h
->type
== STT_NOTYPE
2812 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2813 h
->root
.root
.string
);
2815 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2816 bed
= get_elf_backend_data (dynobj
);
2818 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2827 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2831 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2832 struct elf_link_hash_entry
*h
,
2835 unsigned int power_of_two
;
2837 asection
*sec
= h
->root
.u
.def
.section
;
2839 /* The section aligment of definition is the maximum alignment
2840 requirement of symbols defined in the section. Since we don't
2841 know the symbol alignment requirement, we start with the
2842 maximum alignment and check low bits of the symbol address
2843 for the minimum alignment. */
2844 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2845 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2846 while ((h
->root
.u
.def
.value
& mask
) != 0)
2852 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2855 /* Adjust the section alignment if needed. */
2856 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2861 /* We make sure that the symbol will be aligned properly. */
2862 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2864 /* Define the symbol as being at this point in DYNBSS. */
2865 h
->root
.u
.def
.section
= dynbss
;
2866 h
->root
.u
.def
.value
= dynbss
->size
;
2868 /* Increment the size of DYNBSS to make room for the symbol. */
2869 dynbss
->size
+= h
->size
;
2871 /* No error if extern_protected_data is true. */
2872 if (h
->protected_def
2873 && (!info
->extern_protected_data
2874 || (info
->extern_protected_data
< 0
2875 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2876 info
->callbacks
->einfo
2877 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2878 h
->root
.root
.string
);
2883 /* Adjust all external symbols pointing into SEC_MERGE sections
2884 to reflect the object merging within the sections. */
2887 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2891 if ((h
->root
.type
== bfd_link_hash_defined
2892 || h
->root
.type
== bfd_link_hash_defweak
)
2893 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2894 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2896 bfd
*output_bfd
= (bfd
*) data
;
2898 h
->root
.u
.def
.value
=
2899 _bfd_merged_section_offset (output_bfd
,
2900 &h
->root
.u
.def
.section
,
2901 elf_section_data (sec
)->sec_info
,
2902 h
->root
.u
.def
.value
);
2908 /* Returns false if the symbol referred to by H should be considered
2909 to resolve local to the current module, and true if it should be
2910 considered to bind dynamically. */
2913 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2914 struct bfd_link_info
*info
,
2915 bfd_boolean not_local_protected
)
2917 bfd_boolean binding_stays_local_p
;
2918 const struct elf_backend_data
*bed
;
2919 struct elf_link_hash_table
*hash_table
;
2924 while (h
->root
.type
== bfd_link_hash_indirect
2925 || h
->root
.type
== bfd_link_hash_warning
)
2926 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2928 /* If it was forced local, then clearly it's not dynamic. */
2929 if (h
->dynindx
== -1)
2931 if (h
->forced_local
)
2934 /* Identify the cases where name binding rules say that a
2935 visible symbol resolves locally. */
2936 binding_stays_local_p
= (bfd_link_executable (info
)
2937 || SYMBOLIC_BIND (info
, h
));
2939 switch (ELF_ST_VISIBILITY (h
->other
))
2946 hash_table
= elf_hash_table (info
);
2947 if (!is_elf_hash_table (hash_table
))
2950 bed
= get_elf_backend_data (hash_table
->dynobj
);
2952 /* Proper resolution for function pointer equality may require
2953 that these symbols perhaps be resolved dynamically, even though
2954 we should be resolving them to the current module. */
2955 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2956 binding_stays_local_p
= TRUE
;
2963 /* If it isn't defined locally, then clearly it's dynamic. */
2964 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2967 /* Otherwise, the symbol is dynamic if binding rules don't tell
2968 us that it remains local. */
2969 return !binding_stays_local_p
;
2972 /* Return true if the symbol referred to by H should be considered
2973 to resolve local to the current module, and false otherwise. Differs
2974 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2975 undefined symbols. The two functions are virtually identical except
2976 for the place where forced_local and dynindx == -1 are tested. If
2977 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2978 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2979 the symbol is local only for defined symbols.
2980 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2981 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2982 treatment of undefined weak symbols. For those that do not make
2983 undefined weak symbols dynamic, both functions may return false. */
2986 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2987 struct bfd_link_info
*info
,
2988 bfd_boolean local_protected
)
2990 const struct elf_backend_data
*bed
;
2991 struct elf_link_hash_table
*hash_table
;
2993 /* If it's a local sym, of course we resolve locally. */
2997 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2998 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2999 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3002 /* Common symbols that become definitions don't get the DEF_REGULAR
3003 flag set, so test it first, and don't bail out. */
3004 if (ELF_COMMON_DEF_P (h
))
3006 /* If we don't have a definition in a regular file, then we can't
3007 resolve locally. The sym is either undefined or dynamic. */
3008 else if (!h
->def_regular
)
3011 /* Forced local symbols resolve locally. */
3012 if (h
->forced_local
)
3015 /* As do non-dynamic symbols. */
3016 if (h
->dynindx
== -1)
3019 /* At this point, we know the symbol is defined and dynamic. In an
3020 executable it must resolve locally, likewise when building symbolic
3021 shared libraries. */
3022 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3025 /* Now deal with defined dynamic symbols in shared libraries. Ones
3026 with default visibility might not resolve locally. */
3027 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3030 hash_table
= elf_hash_table (info
);
3031 if (!is_elf_hash_table (hash_table
))
3034 bed
= get_elf_backend_data (hash_table
->dynobj
);
3036 /* If extern_protected_data is false, STV_PROTECTED non-function
3037 symbols are local. */
3038 if ((!info
->extern_protected_data
3039 || (info
->extern_protected_data
< 0
3040 && !bed
->extern_protected_data
))
3041 && !bed
->is_function_type (h
->type
))
3044 /* Function pointer equality tests may require that STV_PROTECTED
3045 symbols be treated as dynamic symbols. If the address of a
3046 function not defined in an executable is set to that function's
3047 plt entry in the executable, then the address of the function in
3048 a shared library must also be the plt entry in the executable. */
3049 return local_protected
;
3052 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3053 aligned. Returns the first TLS output section. */
3055 struct bfd_section
*
3056 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3058 struct bfd_section
*sec
, *tls
;
3059 unsigned int align
= 0;
3061 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3062 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3066 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3067 if (sec
->alignment_power
> align
)
3068 align
= sec
->alignment_power
;
3070 elf_hash_table (info
)->tls_sec
= tls
;
3072 /* Ensure the alignment of the first section is the largest alignment,
3073 so that the tls segment starts aligned. */
3075 tls
->alignment_power
= align
;
3080 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3082 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3083 Elf_Internal_Sym
*sym
)
3085 const struct elf_backend_data
*bed
;
3087 /* Local symbols do not count, but target specific ones might. */
3088 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3089 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3092 bed
= get_elf_backend_data (abfd
);
3093 /* Function symbols do not count. */
3094 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3097 /* If the section is undefined, then so is the symbol. */
3098 if (sym
->st_shndx
== SHN_UNDEF
)
3101 /* If the symbol is defined in the common section, then
3102 it is a common definition and so does not count. */
3103 if (bed
->common_definition (sym
))
3106 /* If the symbol is in a target specific section then we
3107 must rely upon the backend to tell us what it is. */
3108 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3109 /* FIXME - this function is not coded yet:
3111 return _bfd_is_global_symbol_definition (abfd, sym);
3113 Instead for now assume that the definition is not global,
3114 Even if this is wrong, at least the linker will behave
3115 in the same way that it used to do. */
3121 /* Search the symbol table of the archive element of the archive ABFD
3122 whose archive map contains a mention of SYMDEF, and determine if
3123 the symbol is defined in this element. */
3125 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3127 Elf_Internal_Shdr
* hdr
;
3131 Elf_Internal_Sym
*isymbuf
;
3132 Elf_Internal_Sym
*isym
;
3133 Elf_Internal_Sym
*isymend
;
3136 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3140 if (! bfd_check_format (abfd
, bfd_object
))
3143 /* Select the appropriate symbol table. If we don't know if the
3144 object file is an IR object, give linker LTO plugin a chance to
3145 get the correct symbol table. */
3146 if (abfd
->plugin_format
== bfd_plugin_yes
3147 #if BFD_SUPPORTS_PLUGINS
3148 || (abfd
->plugin_format
== bfd_plugin_unknown
3149 && bfd_link_plugin_object_p (abfd
))
3153 /* Use the IR symbol table if the object has been claimed by
3155 abfd
= abfd
->plugin_dummy_bfd
;
3156 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3158 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3159 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3161 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3163 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3165 /* The sh_info field of the symtab header tells us where the
3166 external symbols start. We don't care about the local symbols. */
3167 if (elf_bad_symtab (abfd
))
3169 extsymcount
= symcount
;
3174 extsymcount
= symcount
- hdr
->sh_info
;
3175 extsymoff
= hdr
->sh_info
;
3178 if (extsymcount
== 0)
3181 /* Read in the symbol table. */
3182 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3184 if (isymbuf
== NULL
)
3187 /* Scan the symbol table looking for SYMDEF. */
3189 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3193 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3198 if (strcmp (name
, symdef
->name
) == 0)
3200 result
= is_global_data_symbol_definition (abfd
, isym
);
3210 /* Add an entry to the .dynamic table. */
3213 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3217 struct elf_link_hash_table
*hash_table
;
3218 const struct elf_backend_data
*bed
;
3220 bfd_size_type newsize
;
3221 bfd_byte
*newcontents
;
3222 Elf_Internal_Dyn dyn
;
3224 hash_table
= elf_hash_table (info
);
3225 if (! is_elf_hash_table (hash_table
))
3228 bed
= get_elf_backend_data (hash_table
->dynobj
);
3229 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3230 BFD_ASSERT (s
!= NULL
);
3232 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3233 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3234 if (newcontents
== NULL
)
3238 dyn
.d_un
.d_val
= val
;
3239 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3242 s
->contents
= newcontents
;
3247 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3248 otherwise just check whether one already exists. Returns -1 on error,
3249 1 if a DT_NEEDED tag already exists, and 0 on success. */
3252 elf_add_dt_needed_tag (bfd
*abfd
,
3253 struct bfd_link_info
*info
,
3257 struct elf_link_hash_table
*hash_table
;
3260 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3263 hash_table
= elf_hash_table (info
);
3264 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3265 if (strindex
== (size_t) -1)
3268 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3271 const struct elf_backend_data
*bed
;
3274 bed
= get_elf_backend_data (hash_table
->dynobj
);
3275 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3277 for (extdyn
= sdyn
->contents
;
3278 extdyn
< sdyn
->contents
+ sdyn
->size
;
3279 extdyn
+= bed
->s
->sizeof_dyn
)
3281 Elf_Internal_Dyn dyn
;
3283 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3284 if (dyn
.d_tag
== DT_NEEDED
3285 && dyn
.d_un
.d_val
== strindex
)
3287 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3295 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3298 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3302 /* We were just checking for existence of the tag. */
3303 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3308 /* Return true if SONAME is on the needed list between NEEDED and STOP
3309 (or the end of list if STOP is NULL), and needed by a library that
3313 on_needed_list (const char *soname
,
3314 struct bfd_link_needed_list
*needed
,
3315 struct bfd_link_needed_list
*stop
)
3317 struct bfd_link_needed_list
*look
;
3318 for (look
= needed
; look
!= stop
; look
= look
->next
)
3319 if (strcmp (soname
, look
->name
) == 0
3320 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3321 /* If needed by a library that itself is not directly
3322 needed, recursively check whether that library is
3323 indirectly needed. Since we add DT_NEEDED entries to
3324 the end of the list, library dependencies appear after
3325 the library. Therefore search prior to the current
3326 LOOK, preventing possible infinite recursion. */
3327 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3333 /* Sort symbol by value, section, and size. */
3335 elf_sort_symbol (const void *arg1
, const void *arg2
)
3337 const struct elf_link_hash_entry
*h1
;
3338 const struct elf_link_hash_entry
*h2
;
3339 bfd_signed_vma vdiff
;
3341 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3342 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3343 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3345 return vdiff
> 0 ? 1 : -1;
3348 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3350 return sdiff
> 0 ? 1 : -1;
3352 vdiff
= h1
->size
- h2
->size
;
3353 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3356 /* This function is used to adjust offsets into .dynstr for
3357 dynamic symbols. This is called via elf_link_hash_traverse. */
3360 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3362 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3364 if (h
->dynindx
!= -1)
3365 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3369 /* Assign string offsets in .dynstr, update all structures referencing
3373 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3375 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3376 struct elf_link_local_dynamic_entry
*entry
;
3377 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3378 bfd
*dynobj
= hash_table
->dynobj
;
3381 const struct elf_backend_data
*bed
;
3384 _bfd_elf_strtab_finalize (dynstr
);
3385 size
= _bfd_elf_strtab_size (dynstr
);
3387 bed
= get_elf_backend_data (dynobj
);
3388 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3389 BFD_ASSERT (sdyn
!= NULL
);
3391 /* Update all .dynamic entries referencing .dynstr strings. */
3392 for (extdyn
= sdyn
->contents
;
3393 extdyn
< sdyn
->contents
+ sdyn
->size
;
3394 extdyn
+= bed
->s
->sizeof_dyn
)
3396 Elf_Internal_Dyn dyn
;
3398 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3402 dyn
.d_un
.d_val
= size
;
3412 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3417 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3420 /* Now update local dynamic symbols. */
3421 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3422 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3423 entry
->isym
.st_name
);
3425 /* And the rest of dynamic symbols. */
3426 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3428 /* Adjust version definitions. */
3429 if (elf_tdata (output_bfd
)->cverdefs
)
3434 Elf_Internal_Verdef def
;
3435 Elf_Internal_Verdaux defaux
;
3437 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3441 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3443 p
+= sizeof (Elf_External_Verdef
);
3444 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3446 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3448 _bfd_elf_swap_verdaux_in (output_bfd
,
3449 (Elf_External_Verdaux
*) p
, &defaux
);
3450 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3452 _bfd_elf_swap_verdaux_out (output_bfd
,
3453 &defaux
, (Elf_External_Verdaux
*) p
);
3454 p
+= sizeof (Elf_External_Verdaux
);
3457 while (def
.vd_next
);
3460 /* Adjust version references. */
3461 if (elf_tdata (output_bfd
)->verref
)
3466 Elf_Internal_Verneed need
;
3467 Elf_Internal_Vernaux needaux
;
3469 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3473 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3475 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3476 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3477 (Elf_External_Verneed
*) p
);
3478 p
+= sizeof (Elf_External_Verneed
);
3479 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3481 _bfd_elf_swap_vernaux_in (output_bfd
,
3482 (Elf_External_Vernaux
*) p
, &needaux
);
3483 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3485 _bfd_elf_swap_vernaux_out (output_bfd
,
3487 (Elf_External_Vernaux
*) p
);
3488 p
+= sizeof (Elf_External_Vernaux
);
3491 while (need
.vn_next
);
3497 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3498 The default is to only match when the INPUT and OUTPUT are exactly
3502 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3503 const bfd_target
*output
)
3505 return input
== output
;
3508 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3509 This version is used when different targets for the same architecture
3510 are virtually identical. */
3513 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3514 const bfd_target
*output
)
3516 const struct elf_backend_data
*obed
, *ibed
;
3518 if (input
== output
)
3521 ibed
= xvec_get_elf_backend_data (input
);
3522 obed
= xvec_get_elf_backend_data (output
);
3524 if (ibed
->arch
!= obed
->arch
)
3527 /* If both backends are using this function, deem them compatible. */
3528 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3531 /* Make a special call to the linker "notice" function to tell it that
3532 we are about to handle an as-needed lib, or have finished
3533 processing the lib. */
3536 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3537 struct bfd_link_info
*info
,
3538 enum notice_asneeded_action act
)
3540 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3543 /* Check relocations an ELF object file. */
3546 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3549 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3551 /* If this object is the same format as the output object, and it is
3552 not a shared library, then let the backend look through the
3555 This is required to build global offset table entries and to
3556 arrange for dynamic relocs. It is not required for the
3557 particular common case of linking non PIC code, even when linking
3558 against shared libraries, but unfortunately there is no way of
3559 knowing whether an object file has been compiled PIC or not.
3560 Looking through the relocs is not particularly time consuming.
3561 The problem is that we must either (1) keep the relocs in memory,
3562 which causes the linker to require additional runtime memory or
3563 (2) read the relocs twice from the input file, which wastes time.
3564 This would be a good case for using mmap.
3566 I have no idea how to handle linking PIC code into a file of a
3567 different format. It probably can't be done. */
3568 if ((abfd
->flags
& DYNAMIC
) == 0
3569 && is_elf_hash_table (htab
)
3570 && bed
->check_relocs
!= NULL
3571 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3572 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3576 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3578 Elf_Internal_Rela
*internal_relocs
;
3581 /* Don't check relocations in excluded sections. */
3582 if ((o
->flags
& SEC_RELOC
) == 0
3583 || (o
->flags
& SEC_EXCLUDE
) != 0
3584 || o
->reloc_count
== 0
3585 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3586 && (o
->flags
& SEC_DEBUGGING
) != 0)
3587 || bfd_is_abs_section (o
->output_section
))
3590 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3592 if (internal_relocs
== NULL
)
3595 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3597 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3598 free (internal_relocs
);
3608 /* Add symbols from an ELF object file to the linker hash table. */
3611 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3613 Elf_Internal_Ehdr
*ehdr
;
3614 Elf_Internal_Shdr
*hdr
;
3618 struct elf_link_hash_entry
**sym_hash
;
3619 bfd_boolean dynamic
;
3620 Elf_External_Versym
*extversym
= NULL
;
3621 Elf_External_Versym
*ever
;
3622 struct elf_link_hash_entry
*weaks
;
3623 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3624 size_t nondeflt_vers_cnt
= 0;
3625 Elf_Internal_Sym
*isymbuf
= NULL
;
3626 Elf_Internal_Sym
*isym
;
3627 Elf_Internal_Sym
*isymend
;
3628 const struct elf_backend_data
*bed
;
3629 bfd_boolean add_needed
;
3630 struct elf_link_hash_table
*htab
;
3632 void *alloc_mark
= NULL
;
3633 struct bfd_hash_entry
**old_table
= NULL
;
3634 unsigned int old_size
= 0;
3635 unsigned int old_count
= 0;
3636 void *old_tab
= NULL
;
3638 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3639 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3640 void *old_strtab
= NULL
;
3643 bfd_boolean just_syms
;
3645 htab
= elf_hash_table (info
);
3646 bed
= get_elf_backend_data (abfd
);
3648 if ((abfd
->flags
& DYNAMIC
) == 0)
3654 /* You can't use -r against a dynamic object. Also, there's no
3655 hope of using a dynamic object which does not exactly match
3656 the format of the output file. */
3657 if (bfd_link_relocatable (info
)
3658 || !is_elf_hash_table (htab
)
3659 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3661 if (bfd_link_relocatable (info
))
3662 bfd_set_error (bfd_error_invalid_operation
);
3664 bfd_set_error (bfd_error_wrong_format
);
3669 ehdr
= elf_elfheader (abfd
);
3670 if (info
->warn_alternate_em
3671 && bed
->elf_machine_code
!= ehdr
->e_machine
3672 && ((bed
->elf_machine_alt1
!= 0
3673 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3674 || (bed
->elf_machine_alt2
!= 0
3675 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3676 info
->callbacks
->einfo
3677 /* xgettext:c-format */
3678 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3679 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3681 /* As a GNU extension, any input sections which are named
3682 .gnu.warning.SYMBOL are treated as warning symbols for the given
3683 symbol. This differs from .gnu.warning sections, which generate
3684 warnings when they are included in an output file. */
3685 /* PR 12761: Also generate this warning when building shared libraries. */
3686 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3690 name
= bfd_get_section_name (abfd
, s
);
3691 if (CONST_STRNEQ (name
, ".gnu.warning."))
3696 name
+= sizeof ".gnu.warning." - 1;
3698 /* If this is a shared object, then look up the symbol
3699 in the hash table. If it is there, and it is already
3700 been defined, then we will not be using the entry
3701 from this shared object, so we don't need to warn.
3702 FIXME: If we see the definition in a regular object
3703 later on, we will warn, but we shouldn't. The only
3704 fix is to keep track of what warnings we are supposed
3705 to emit, and then handle them all at the end of the
3709 struct elf_link_hash_entry
*h
;
3711 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3713 /* FIXME: What about bfd_link_hash_common? */
3715 && (h
->root
.type
== bfd_link_hash_defined
3716 || h
->root
.type
== bfd_link_hash_defweak
))
3721 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3725 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3730 if (! (_bfd_generic_link_add_one_symbol
3731 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3732 FALSE
, bed
->collect
, NULL
)))
3735 if (bfd_link_executable (info
))
3737 /* Clobber the section size so that the warning does
3738 not get copied into the output file. */
3741 /* Also set SEC_EXCLUDE, so that symbols defined in
3742 the warning section don't get copied to the output. */
3743 s
->flags
|= SEC_EXCLUDE
;
3748 just_syms
= ((s
= abfd
->sections
) != NULL
3749 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3754 /* If we are creating a shared library, create all the dynamic
3755 sections immediately. We need to attach them to something,
3756 so we attach them to this BFD, provided it is the right
3757 format and is not from ld --just-symbols. Always create the
3758 dynamic sections for -E/--dynamic-list. FIXME: If there
3759 are no input BFD's of the same format as the output, we can't
3760 make a shared library. */
3762 && (bfd_link_pic (info
)
3763 || (!bfd_link_relocatable (info
)
3764 && (info
->export_dynamic
|| info
->dynamic
)))
3765 && is_elf_hash_table (htab
)
3766 && info
->output_bfd
->xvec
== abfd
->xvec
3767 && !htab
->dynamic_sections_created
)
3769 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3773 else if (!is_elf_hash_table (htab
))
3777 const char *soname
= NULL
;
3779 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3782 /* ld --just-symbols and dynamic objects don't mix very well.
3783 ld shouldn't allow it. */
3787 /* If this dynamic lib was specified on the command line with
3788 --as-needed in effect, then we don't want to add a DT_NEEDED
3789 tag unless the lib is actually used. Similary for libs brought
3790 in by another lib's DT_NEEDED. When --no-add-needed is used
3791 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3792 any dynamic library in DT_NEEDED tags in the dynamic lib at
3794 add_needed
= (elf_dyn_lib_class (abfd
)
3795 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3796 | DYN_NO_NEEDED
)) == 0;
3798 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3803 unsigned int elfsec
;
3804 unsigned long shlink
;
3806 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3813 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3814 if (elfsec
== SHN_BAD
)
3815 goto error_free_dyn
;
3816 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3818 for (extdyn
= dynbuf
;
3819 extdyn
< dynbuf
+ s
->size
;
3820 extdyn
+= bed
->s
->sizeof_dyn
)
3822 Elf_Internal_Dyn dyn
;
3824 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3825 if (dyn
.d_tag
== DT_SONAME
)
3827 unsigned int tagv
= dyn
.d_un
.d_val
;
3828 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3830 goto error_free_dyn
;
3832 if (dyn
.d_tag
== DT_NEEDED
)
3834 struct bfd_link_needed_list
*n
, **pn
;
3836 unsigned int tagv
= dyn
.d_un
.d_val
;
3838 amt
= sizeof (struct bfd_link_needed_list
);
3839 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3840 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3841 if (n
== NULL
|| fnm
== NULL
)
3842 goto error_free_dyn
;
3843 amt
= strlen (fnm
) + 1;
3844 anm
= (char *) bfd_alloc (abfd
, amt
);
3846 goto error_free_dyn
;
3847 memcpy (anm
, fnm
, amt
);
3851 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3855 if (dyn
.d_tag
== DT_RUNPATH
)
3857 struct bfd_link_needed_list
*n
, **pn
;
3859 unsigned int tagv
= dyn
.d_un
.d_val
;
3861 amt
= sizeof (struct bfd_link_needed_list
);
3862 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3863 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3864 if (n
== NULL
|| fnm
== NULL
)
3865 goto error_free_dyn
;
3866 amt
= strlen (fnm
) + 1;
3867 anm
= (char *) bfd_alloc (abfd
, amt
);
3869 goto error_free_dyn
;
3870 memcpy (anm
, fnm
, amt
);
3874 for (pn
= & runpath
;
3880 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3881 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3883 struct bfd_link_needed_list
*n
, **pn
;
3885 unsigned int tagv
= dyn
.d_un
.d_val
;
3887 amt
= sizeof (struct bfd_link_needed_list
);
3888 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3889 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3890 if (n
== NULL
|| fnm
== NULL
)
3891 goto error_free_dyn
;
3892 amt
= strlen (fnm
) + 1;
3893 anm
= (char *) bfd_alloc (abfd
, amt
);
3895 goto error_free_dyn
;
3896 memcpy (anm
, fnm
, amt
);
3906 if (dyn
.d_tag
== DT_AUDIT
)
3908 unsigned int tagv
= dyn
.d_un
.d_val
;
3909 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3916 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3917 frees all more recently bfd_alloc'd blocks as well. */
3923 struct bfd_link_needed_list
**pn
;
3924 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3929 /* We do not want to include any of the sections in a dynamic
3930 object in the output file. We hack by simply clobbering the
3931 list of sections in the BFD. This could be handled more
3932 cleanly by, say, a new section flag; the existing
3933 SEC_NEVER_LOAD flag is not the one we want, because that one
3934 still implies that the section takes up space in the output
3936 bfd_section_list_clear (abfd
);
3938 /* Find the name to use in a DT_NEEDED entry that refers to this
3939 object. If the object has a DT_SONAME entry, we use it.
3940 Otherwise, if the generic linker stuck something in
3941 elf_dt_name, we use that. Otherwise, we just use the file
3943 if (soname
== NULL
|| *soname
== '\0')
3945 soname
= elf_dt_name (abfd
);
3946 if (soname
== NULL
|| *soname
== '\0')
3947 soname
= bfd_get_filename (abfd
);
3950 /* Save the SONAME because sometimes the linker emulation code
3951 will need to know it. */
3952 elf_dt_name (abfd
) = soname
;
3954 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3958 /* If we have already included this dynamic object in the
3959 link, just ignore it. There is no reason to include a
3960 particular dynamic object more than once. */
3964 /* Save the DT_AUDIT entry for the linker emulation code. */
3965 elf_dt_audit (abfd
) = audit
;
3968 /* If this is a dynamic object, we always link against the .dynsym
3969 symbol table, not the .symtab symbol table. The dynamic linker
3970 will only see the .dynsym symbol table, so there is no reason to
3971 look at .symtab for a dynamic object. */
3973 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3974 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3976 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3978 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3980 /* The sh_info field of the symtab header tells us where the
3981 external symbols start. We don't care about the local symbols at
3983 if (elf_bad_symtab (abfd
))
3985 extsymcount
= symcount
;
3990 extsymcount
= symcount
- hdr
->sh_info
;
3991 extsymoff
= hdr
->sh_info
;
3994 sym_hash
= elf_sym_hashes (abfd
);
3995 if (extsymcount
!= 0)
3997 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3999 if (isymbuf
== NULL
)
4002 if (sym_hash
== NULL
)
4004 /* We store a pointer to the hash table entry for each
4007 amt
*= sizeof (struct elf_link_hash_entry
*);
4008 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4009 if (sym_hash
== NULL
)
4010 goto error_free_sym
;
4011 elf_sym_hashes (abfd
) = sym_hash
;
4017 /* Read in any version definitions. */
4018 if (!_bfd_elf_slurp_version_tables (abfd
,
4019 info
->default_imported_symver
))
4020 goto error_free_sym
;
4022 /* Read in the symbol versions, but don't bother to convert them
4023 to internal format. */
4024 if (elf_dynversym (abfd
) != 0)
4026 Elf_Internal_Shdr
*versymhdr
;
4028 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4029 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4030 if (extversym
== NULL
)
4031 goto error_free_sym
;
4032 amt
= versymhdr
->sh_size
;
4033 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4034 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4035 goto error_free_vers
;
4039 /* If we are loading an as-needed shared lib, save the symbol table
4040 state before we start adding symbols. If the lib turns out
4041 to be unneeded, restore the state. */
4042 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4047 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4049 struct bfd_hash_entry
*p
;
4050 struct elf_link_hash_entry
*h
;
4052 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4054 h
= (struct elf_link_hash_entry
*) p
;
4055 entsize
+= htab
->root
.table
.entsize
;
4056 if (h
->root
.type
== bfd_link_hash_warning
)
4057 entsize
+= htab
->root
.table
.entsize
;
4061 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4062 old_tab
= bfd_malloc (tabsize
+ entsize
);
4063 if (old_tab
== NULL
)
4064 goto error_free_vers
;
4066 /* Remember the current objalloc pointer, so that all mem for
4067 symbols added can later be reclaimed. */
4068 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4069 if (alloc_mark
== NULL
)
4070 goto error_free_vers
;
4072 /* Make a special call to the linker "notice" function to
4073 tell it that we are about to handle an as-needed lib. */
4074 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4075 goto error_free_vers
;
4077 /* Clone the symbol table. Remember some pointers into the
4078 symbol table, and dynamic symbol count. */
4079 old_ent
= (char *) old_tab
+ tabsize
;
4080 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4081 old_undefs
= htab
->root
.undefs
;
4082 old_undefs_tail
= htab
->root
.undefs_tail
;
4083 old_table
= htab
->root
.table
.table
;
4084 old_size
= htab
->root
.table
.size
;
4085 old_count
= htab
->root
.table
.count
;
4086 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4087 if (old_strtab
== NULL
)
4088 goto error_free_vers
;
4090 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4092 struct bfd_hash_entry
*p
;
4093 struct elf_link_hash_entry
*h
;
4095 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4097 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4098 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4099 h
= (struct elf_link_hash_entry
*) p
;
4100 if (h
->root
.type
== bfd_link_hash_warning
)
4102 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4103 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4110 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4111 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4113 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4117 asection
*sec
, *new_sec
;
4120 struct elf_link_hash_entry
*h
;
4121 struct elf_link_hash_entry
*hi
;
4122 bfd_boolean definition
;
4123 bfd_boolean size_change_ok
;
4124 bfd_boolean type_change_ok
;
4125 bfd_boolean new_weakdef
;
4126 bfd_boolean new_weak
;
4127 bfd_boolean old_weak
;
4128 bfd_boolean override
;
4130 bfd_boolean discarded
;
4131 unsigned int old_alignment
;
4133 bfd_boolean matched
;
4137 flags
= BSF_NO_FLAGS
;
4139 value
= isym
->st_value
;
4140 common
= bed
->common_definition (isym
);
4143 bind
= ELF_ST_BIND (isym
->st_info
);
4147 /* This should be impossible, since ELF requires that all
4148 global symbols follow all local symbols, and that sh_info
4149 point to the first global symbol. Unfortunately, Irix 5
4154 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4162 case STB_GNU_UNIQUE
:
4163 flags
= BSF_GNU_UNIQUE
;
4167 /* Leave it up to the processor backend. */
4171 if (isym
->st_shndx
== SHN_UNDEF
)
4172 sec
= bfd_und_section_ptr
;
4173 else if (isym
->st_shndx
== SHN_ABS
)
4174 sec
= bfd_abs_section_ptr
;
4175 else if (isym
->st_shndx
== SHN_COMMON
)
4177 sec
= bfd_com_section_ptr
;
4178 /* What ELF calls the size we call the value. What ELF
4179 calls the value we call the alignment. */
4180 value
= isym
->st_size
;
4184 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4186 sec
= bfd_abs_section_ptr
;
4187 else if (discarded_section (sec
))
4189 /* Symbols from discarded section are undefined. We keep
4191 sec
= bfd_und_section_ptr
;
4193 isym
->st_shndx
= SHN_UNDEF
;
4195 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4199 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4202 goto error_free_vers
;
4204 if (isym
->st_shndx
== SHN_COMMON
4205 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4207 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4211 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4213 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4215 goto error_free_vers
;
4219 else if (isym
->st_shndx
== SHN_COMMON
4220 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4221 && !bfd_link_relocatable (info
))
4223 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4227 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4228 | SEC_LINKER_CREATED
);
4229 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4231 goto error_free_vers
;
4235 else if (bed
->elf_add_symbol_hook
)
4237 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4239 goto error_free_vers
;
4241 /* The hook function sets the name to NULL if this symbol
4242 should be skipped for some reason. */
4247 /* Sanity check that all possibilities were handled. */
4250 bfd_set_error (bfd_error_bad_value
);
4251 goto error_free_vers
;
4254 /* Silently discard TLS symbols from --just-syms. There's
4255 no way to combine a static TLS block with a new TLS block
4256 for this executable. */
4257 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4258 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4261 if (bfd_is_und_section (sec
)
4262 || bfd_is_com_section (sec
))
4267 size_change_ok
= FALSE
;
4268 type_change_ok
= bed
->type_change_ok
;
4275 if (is_elf_hash_table (htab
))
4277 Elf_Internal_Versym iver
;
4278 unsigned int vernum
= 0;
4283 if (info
->default_imported_symver
)
4284 /* Use the default symbol version created earlier. */
4285 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4290 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4292 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4294 /* If this is a hidden symbol, or if it is not version
4295 1, we append the version name to the symbol name.
4296 However, we do not modify a non-hidden absolute symbol
4297 if it is not a function, because it might be the version
4298 symbol itself. FIXME: What if it isn't? */
4299 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4301 && (!bfd_is_abs_section (sec
)
4302 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4305 size_t namelen
, verlen
, newlen
;
4308 if (isym
->st_shndx
!= SHN_UNDEF
)
4310 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4312 else if (vernum
> 1)
4314 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4321 /* xgettext:c-format */
4322 (_("%B: %s: invalid version %u (max %d)"),
4324 elf_tdata (abfd
)->cverdefs
);
4325 bfd_set_error (bfd_error_bad_value
);
4326 goto error_free_vers
;
4331 /* We cannot simply test for the number of
4332 entries in the VERNEED section since the
4333 numbers for the needed versions do not start
4335 Elf_Internal_Verneed
*t
;
4338 for (t
= elf_tdata (abfd
)->verref
;
4342 Elf_Internal_Vernaux
*a
;
4344 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4346 if (a
->vna_other
== vernum
)
4348 verstr
= a
->vna_nodename
;
4358 /* xgettext:c-format */
4359 (_("%B: %s: invalid needed version %d"),
4360 abfd
, name
, vernum
);
4361 bfd_set_error (bfd_error_bad_value
);
4362 goto error_free_vers
;
4366 namelen
= strlen (name
);
4367 verlen
= strlen (verstr
);
4368 newlen
= namelen
+ verlen
+ 2;
4369 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4370 && isym
->st_shndx
!= SHN_UNDEF
)
4373 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4374 if (newname
== NULL
)
4375 goto error_free_vers
;
4376 memcpy (newname
, name
, namelen
);
4377 p
= newname
+ namelen
;
4379 /* If this is a defined non-hidden version symbol,
4380 we add another @ to the name. This indicates the
4381 default version of the symbol. */
4382 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4383 && isym
->st_shndx
!= SHN_UNDEF
)
4385 memcpy (p
, verstr
, verlen
+ 1);
4390 /* If this symbol has default visibility and the user has
4391 requested we not re-export it, then mark it as hidden. */
4392 if (!bfd_is_und_section (sec
)
4395 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4396 isym
->st_other
= (STV_HIDDEN
4397 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4399 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4400 sym_hash
, &old_bfd
, &old_weak
,
4401 &old_alignment
, &skip
, &override
,
4402 &type_change_ok
, &size_change_ok
,
4404 goto error_free_vers
;
4409 /* Override a definition only if the new symbol matches the
4411 if (override
&& matched
)
4415 while (h
->root
.type
== bfd_link_hash_indirect
4416 || h
->root
.type
== bfd_link_hash_warning
)
4417 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4419 if (elf_tdata (abfd
)->verdef
!= NULL
4422 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4425 if (! (_bfd_generic_link_add_one_symbol
4426 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4427 (struct bfd_link_hash_entry
**) sym_hash
)))
4428 goto error_free_vers
;
4430 if ((flags
& BSF_GNU_UNIQUE
)
4431 && (abfd
->flags
& DYNAMIC
) == 0
4432 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4433 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4436 /* We need to make sure that indirect symbol dynamic flags are
4439 while (h
->root
.type
== bfd_link_hash_indirect
4440 || h
->root
.type
== bfd_link_hash_warning
)
4441 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4443 /* Setting the index to -3 tells elf_link_output_extsym that
4444 this symbol is defined in a discarded section. */
4450 new_weak
= (flags
& BSF_WEAK
) != 0;
4451 new_weakdef
= FALSE
;
4455 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4456 && is_elf_hash_table (htab
)
4457 && h
->u
.weakdef
== NULL
)
4459 /* Keep a list of all weak defined non function symbols from
4460 a dynamic object, using the weakdef field. Later in this
4461 function we will set the weakdef field to the correct
4462 value. We only put non-function symbols from dynamic
4463 objects on this list, because that happens to be the only
4464 time we need to know the normal symbol corresponding to a
4465 weak symbol, and the information is time consuming to
4466 figure out. If the weakdef field is not already NULL,
4467 then this symbol was already defined by some previous
4468 dynamic object, and we will be using that previous
4469 definition anyhow. */
4471 h
->u
.weakdef
= weaks
;
4476 /* Set the alignment of a common symbol. */
4477 if ((common
|| bfd_is_com_section (sec
))
4478 && h
->root
.type
== bfd_link_hash_common
)
4483 align
= bfd_log2 (isym
->st_value
);
4486 /* The new symbol is a common symbol in a shared object.
4487 We need to get the alignment from the section. */
4488 align
= new_sec
->alignment_power
;
4490 if (align
> old_alignment
)
4491 h
->root
.u
.c
.p
->alignment_power
= align
;
4493 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4496 if (is_elf_hash_table (htab
))
4498 /* Set a flag in the hash table entry indicating the type of
4499 reference or definition we just found. A dynamic symbol
4500 is one which is referenced or defined by both a regular
4501 object and a shared object. */
4502 bfd_boolean dynsym
= FALSE
;
4504 /* Plugin symbols aren't normal. Don't set def_regular or
4505 ref_regular for them, or make them dynamic. */
4506 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4513 if (bind
!= STB_WEAK
)
4514 h
->ref_regular_nonweak
= 1;
4526 /* If the indirect symbol has been forced local, don't
4527 make the real symbol dynamic. */
4528 if ((h
== hi
|| !hi
->forced_local
)
4529 && (bfd_link_dll (info
)
4539 hi
->ref_dynamic
= 1;
4544 hi
->def_dynamic
= 1;
4547 /* If the indirect symbol has been forced local, don't
4548 make the real symbol dynamic. */
4549 if ((h
== hi
|| !hi
->forced_local
)
4552 || (h
->u
.weakdef
!= NULL
4554 && h
->u
.weakdef
->dynindx
!= -1)))
4558 /* Check to see if we need to add an indirect symbol for
4559 the default name. */
4561 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4562 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4563 sec
, value
, &old_bfd
, &dynsym
))
4564 goto error_free_vers
;
4566 /* Check the alignment when a common symbol is involved. This
4567 can change when a common symbol is overridden by a normal
4568 definition or a common symbol is ignored due to the old
4569 normal definition. We need to make sure the maximum
4570 alignment is maintained. */
4571 if ((old_alignment
|| common
)
4572 && h
->root
.type
!= bfd_link_hash_common
)
4574 unsigned int common_align
;
4575 unsigned int normal_align
;
4576 unsigned int symbol_align
;
4580 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4581 || h
->root
.type
== bfd_link_hash_defweak
);
4583 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4584 if (h
->root
.u
.def
.section
->owner
!= NULL
4585 && (h
->root
.u
.def
.section
->owner
->flags
4586 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4588 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4589 if (normal_align
> symbol_align
)
4590 normal_align
= symbol_align
;
4593 normal_align
= symbol_align
;
4597 common_align
= old_alignment
;
4598 common_bfd
= old_bfd
;
4603 common_align
= bfd_log2 (isym
->st_value
);
4605 normal_bfd
= old_bfd
;
4608 if (normal_align
< common_align
)
4610 /* PR binutils/2735 */
4611 if (normal_bfd
== NULL
)
4613 /* xgettext:c-format */
4614 (_("Warning: alignment %u of common symbol `%s' in %B is"
4615 " greater than the alignment (%u) of its section %A"),
4616 common_bfd
, h
->root
.u
.def
.section
,
4617 1 << common_align
, name
, 1 << normal_align
);
4620 /* xgettext:c-format */
4621 (_("Warning: alignment %u of symbol `%s' in %B"
4622 " is smaller than %u in %B"),
4623 normal_bfd
, common_bfd
,
4624 1 << normal_align
, name
, 1 << common_align
);
4628 /* Remember the symbol size if it isn't undefined. */
4629 if (isym
->st_size
!= 0
4630 && isym
->st_shndx
!= SHN_UNDEF
4631 && (definition
|| h
->size
== 0))
4634 && h
->size
!= isym
->st_size
4635 && ! size_change_ok
)
4637 /* xgettext:c-format */
4638 (_("Warning: size of symbol `%s' changed"
4639 " from %lu in %B to %lu in %B"),
4641 name
, (unsigned long) h
->size
,
4642 (unsigned long) isym
->st_size
);
4644 h
->size
= isym
->st_size
;
4647 /* If this is a common symbol, then we always want H->SIZE
4648 to be the size of the common symbol. The code just above
4649 won't fix the size if a common symbol becomes larger. We
4650 don't warn about a size change here, because that is
4651 covered by --warn-common. Allow changes between different
4653 if (h
->root
.type
== bfd_link_hash_common
)
4654 h
->size
= h
->root
.u
.c
.size
;
4656 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4657 && ((definition
&& !new_weak
)
4658 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4659 || h
->type
== STT_NOTYPE
))
4661 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4663 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4665 if (type
== STT_GNU_IFUNC
4666 && (abfd
->flags
& DYNAMIC
) != 0)
4669 if (h
->type
!= type
)
4671 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4672 /* xgettext:c-format */
4674 (_("Warning: type of symbol `%s' changed"
4675 " from %d to %d in %B"),
4676 abfd
, name
, h
->type
, type
);
4682 /* Merge st_other field. */
4683 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4685 /* We don't want to make debug symbol dynamic. */
4687 && (sec
->flags
& SEC_DEBUGGING
)
4688 && !bfd_link_relocatable (info
))
4691 /* Nor should we make plugin symbols dynamic. */
4692 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4697 h
->target_internal
= isym
->st_target_internal
;
4698 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4701 if (definition
&& !dynamic
)
4703 char *p
= strchr (name
, ELF_VER_CHR
);
4704 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4706 /* Queue non-default versions so that .symver x, x@FOO
4707 aliases can be checked. */
4710 amt
= ((isymend
- isym
+ 1)
4711 * sizeof (struct elf_link_hash_entry
*));
4713 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4715 goto error_free_vers
;
4717 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4721 if (dynsym
&& h
->dynindx
== -1)
4723 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4724 goto error_free_vers
;
4725 if (h
->u
.weakdef
!= NULL
4727 && h
->u
.weakdef
->dynindx
== -1)
4729 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4730 goto error_free_vers
;
4733 else if (h
->dynindx
!= -1)
4734 /* If the symbol already has a dynamic index, but
4735 visibility says it should not be visible, turn it into
4737 switch (ELF_ST_VISIBILITY (h
->other
))
4741 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4746 /* Don't add DT_NEEDED for references from the dummy bfd nor
4747 for unmatched symbol. */
4752 && h
->ref_regular_nonweak
4754 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4755 || (h
->ref_dynamic_nonweak
4756 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4757 && !on_needed_list (elf_dt_name (abfd
),
4758 htab
->needed
, NULL
))))
4761 const char *soname
= elf_dt_name (abfd
);
4763 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4764 h
->root
.root
.string
);
4766 /* A symbol from a library loaded via DT_NEEDED of some
4767 other library is referenced by a regular object.
4768 Add a DT_NEEDED entry for it. Issue an error if
4769 --no-add-needed is used and the reference was not
4772 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4775 /* xgettext:c-format */
4776 (_("%B: undefined reference to symbol '%s'"),
4778 bfd_set_error (bfd_error_missing_dso
);
4779 goto error_free_vers
;
4782 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4783 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4786 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4788 goto error_free_vers
;
4790 BFD_ASSERT (ret
== 0);
4795 if (extversym
!= NULL
)
4801 if (isymbuf
!= NULL
)
4807 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4811 /* Restore the symbol table. */
4812 old_ent
= (char *) old_tab
+ tabsize
;
4813 memset (elf_sym_hashes (abfd
), 0,
4814 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4815 htab
->root
.table
.table
= old_table
;
4816 htab
->root
.table
.size
= old_size
;
4817 htab
->root
.table
.count
= old_count
;
4818 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4819 htab
->root
.undefs
= old_undefs
;
4820 htab
->root
.undefs_tail
= old_undefs_tail
;
4821 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4824 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4826 struct bfd_hash_entry
*p
;
4827 struct elf_link_hash_entry
*h
;
4829 unsigned int alignment_power
;
4831 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4833 h
= (struct elf_link_hash_entry
*) p
;
4834 if (h
->root
.type
== bfd_link_hash_warning
)
4835 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4837 /* Preserve the maximum alignment and size for common
4838 symbols even if this dynamic lib isn't on DT_NEEDED
4839 since it can still be loaded at run time by another
4841 if (h
->root
.type
== bfd_link_hash_common
)
4843 size
= h
->root
.u
.c
.size
;
4844 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4849 alignment_power
= 0;
4851 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4852 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4853 h
= (struct elf_link_hash_entry
*) p
;
4854 if (h
->root
.type
== bfd_link_hash_warning
)
4856 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4857 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4858 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4860 if (h
->root
.type
== bfd_link_hash_common
)
4862 if (size
> h
->root
.u
.c
.size
)
4863 h
->root
.u
.c
.size
= size
;
4864 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4865 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4870 /* Make a special call to the linker "notice" function to
4871 tell it that symbols added for crefs may need to be removed. */
4872 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4873 goto error_free_vers
;
4876 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4878 if (nondeflt_vers
!= NULL
)
4879 free (nondeflt_vers
);
4883 if (old_tab
!= NULL
)
4885 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4886 goto error_free_vers
;
4891 /* Now that all the symbols from this input file are created, if
4892 not performing a relocatable link, handle .symver foo, foo@BAR
4893 such that any relocs against foo become foo@BAR. */
4894 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4898 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4900 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4901 char *shortname
, *p
;
4903 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4905 || (h
->root
.type
!= bfd_link_hash_defined
4906 && h
->root
.type
!= bfd_link_hash_defweak
))
4909 amt
= p
- h
->root
.root
.string
;
4910 shortname
= (char *) bfd_malloc (amt
+ 1);
4912 goto error_free_vers
;
4913 memcpy (shortname
, h
->root
.root
.string
, amt
);
4914 shortname
[amt
] = '\0';
4916 hi
= (struct elf_link_hash_entry
*)
4917 bfd_link_hash_lookup (&htab
->root
, shortname
,
4918 FALSE
, FALSE
, FALSE
);
4920 && hi
->root
.type
== h
->root
.type
4921 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4922 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4924 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4925 hi
->root
.type
= bfd_link_hash_indirect
;
4926 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4927 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4928 sym_hash
= elf_sym_hashes (abfd
);
4930 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4931 if (sym_hash
[symidx
] == hi
)
4933 sym_hash
[symidx
] = h
;
4939 free (nondeflt_vers
);
4940 nondeflt_vers
= NULL
;
4943 /* Now set the weakdefs field correctly for all the weak defined
4944 symbols we found. The only way to do this is to search all the
4945 symbols. Since we only need the information for non functions in
4946 dynamic objects, that's the only time we actually put anything on
4947 the list WEAKS. We need this information so that if a regular
4948 object refers to a symbol defined weakly in a dynamic object, the
4949 real symbol in the dynamic object is also put in the dynamic
4950 symbols; we also must arrange for both symbols to point to the
4951 same memory location. We could handle the general case of symbol
4952 aliasing, but a general symbol alias can only be generated in
4953 assembler code, handling it correctly would be very time
4954 consuming, and other ELF linkers don't handle general aliasing
4958 struct elf_link_hash_entry
**hpp
;
4959 struct elf_link_hash_entry
**hppend
;
4960 struct elf_link_hash_entry
**sorted_sym_hash
;
4961 struct elf_link_hash_entry
*h
;
4964 /* Since we have to search the whole symbol list for each weak
4965 defined symbol, search time for N weak defined symbols will be
4966 O(N^2). Binary search will cut it down to O(NlogN). */
4968 amt
*= sizeof (struct elf_link_hash_entry
*);
4969 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4970 if (sorted_sym_hash
== NULL
)
4972 sym_hash
= sorted_sym_hash
;
4973 hpp
= elf_sym_hashes (abfd
);
4974 hppend
= hpp
+ extsymcount
;
4976 for (; hpp
< hppend
; hpp
++)
4980 && h
->root
.type
== bfd_link_hash_defined
4981 && !bed
->is_function_type (h
->type
))
4989 qsort (sorted_sym_hash
, sym_count
,
4990 sizeof (struct elf_link_hash_entry
*),
4993 while (weaks
!= NULL
)
4995 struct elf_link_hash_entry
*hlook
;
4998 size_t i
, j
, idx
= 0;
5001 weaks
= hlook
->u
.weakdef
;
5002 hlook
->u
.weakdef
= NULL
;
5004 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5005 || hlook
->root
.type
== bfd_link_hash_defweak
5006 || hlook
->root
.type
== bfd_link_hash_common
5007 || hlook
->root
.type
== bfd_link_hash_indirect
);
5008 slook
= hlook
->root
.u
.def
.section
;
5009 vlook
= hlook
->root
.u
.def
.value
;
5015 bfd_signed_vma vdiff
;
5017 h
= sorted_sym_hash
[idx
];
5018 vdiff
= vlook
- h
->root
.u
.def
.value
;
5025 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5035 /* We didn't find a value/section match. */
5039 /* With multiple aliases, or when the weak symbol is already
5040 strongly defined, we have multiple matching symbols and
5041 the binary search above may land on any of them. Step
5042 one past the matching symbol(s). */
5045 h
= sorted_sym_hash
[idx
];
5046 if (h
->root
.u
.def
.section
!= slook
5047 || h
->root
.u
.def
.value
!= vlook
)
5051 /* Now look back over the aliases. Since we sorted by size
5052 as well as value and section, we'll choose the one with
5053 the largest size. */
5056 h
= sorted_sym_hash
[idx
];
5058 /* Stop if value or section doesn't match. */
5059 if (h
->root
.u
.def
.section
!= slook
5060 || h
->root
.u
.def
.value
!= vlook
)
5062 else if (h
!= hlook
)
5064 hlook
->u
.weakdef
= h
;
5066 /* If the weak definition is in the list of dynamic
5067 symbols, make sure the real definition is put
5069 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5071 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5074 free (sorted_sym_hash
);
5079 /* If the real definition is in the list of dynamic
5080 symbols, make sure the weak definition is put
5081 there as well. If we don't do this, then the
5082 dynamic loader might not merge the entries for the
5083 real definition and the weak definition. */
5084 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5086 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5087 goto err_free_sym_hash
;
5094 free (sorted_sym_hash
);
5097 if (bed
->check_directives
5098 && !(*bed
->check_directives
) (abfd
, info
))
5101 if (!info
->check_relocs_after_open_input
5102 && !_bfd_elf_link_check_relocs (abfd
, info
))
5105 /* If this is a non-traditional link, try to optimize the handling
5106 of the .stab/.stabstr sections. */
5108 && ! info
->traditional_format
5109 && is_elf_hash_table (htab
)
5110 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5114 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5115 if (stabstr
!= NULL
)
5117 bfd_size_type string_offset
= 0;
5120 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5121 if (CONST_STRNEQ (stab
->name
, ".stab")
5122 && (!stab
->name
[5] ||
5123 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5124 && (stab
->flags
& SEC_MERGE
) == 0
5125 && !bfd_is_abs_section (stab
->output_section
))
5127 struct bfd_elf_section_data
*secdata
;
5129 secdata
= elf_section_data (stab
);
5130 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5131 stabstr
, &secdata
->sec_info
,
5134 if (secdata
->sec_info
)
5135 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5140 if (is_elf_hash_table (htab
) && add_needed
)
5142 /* Add this bfd to the loaded list. */
5143 struct elf_link_loaded_list
*n
;
5145 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5149 n
->next
= htab
->loaded
;
5156 if (old_tab
!= NULL
)
5158 if (old_strtab
!= NULL
)
5160 if (nondeflt_vers
!= NULL
)
5161 free (nondeflt_vers
);
5162 if (extversym
!= NULL
)
5165 if (isymbuf
!= NULL
)
5171 /* Return the linker hash table entry of a symbol that might be
5172 satisfied by an archive symbol. Return -1 on error. */
5174 struct elf_link_hash_entry
*
5175 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5176 struct bfd_link_info
*info
,
5179 struct elf_link_hash_entry
*h
;
5183 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5187 /* If this is a default version (the name contains @@), look up the
5188 symbol again with only one `@' as well as without the version.
5189 The effect is that references to the symbol with and without the
5190 version will be matched by the default symbol in the archive. */
5192 p
= strchr (name
, ELF_VER_CHR
);
5193 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5196 /* First check with only one `@'. */
5197 len
= strlen (name
);
5198 copy
= (char *) bfd_alloc (abfd
, len
);
5200 return (struct elf_link_hash_entry
*) 0 - 1;
5202 first
= p
- name
+ 1;
5203 memcpy (copy
, name
, first
);
5204 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5206 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5209 /* We also need to check references to the symbol without the
5211 copy
[first
- 1] = '\0';
5212 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5213 FALSE
, FALSE
, TRUE
);
5216 bfd_release (abfd
, copy
);
5220 /* Add symbols from an ELF archive file to the linker hash table. We
5221 don't use _bfd_generic_link_add_archive_symbols because we need to
5222 handle versioned symbols.
5224 Fortunately, ELF archive handling is simpler than that done by
5225 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5226 oddities. In ELF, if we find a symbol in the archive map, and the
5227 symbol is currently undefined, we know that we must pull in that
5230 Unfortunately, we do have to make multiple passes over the symbol
5231 table until nothing further is resolved. */
5234 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5237 unsigned char *included
= NULL
;
5241 const struct elf_backend_data
*bed
;
5242 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5243 (bfd
*, struct bfd_link_info
*, const char *);
5245 if (! bfd_has_map (abfd
))
5247 /* An empty archive is a special case. */
5248 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5250 bfd_set_error (bfd_error_no_armap
);
5254 /* Keep track of all symbols we know to be already defined, and all
5255 files we know to be already included. This is to speed up the
5256 second and subsequent passes. */
5257 c
= bfd_ardata (abfd
)->symdef_count
;
5261 amt
*= sizeof (*included
);
5262 included
= (unsigned char *) bfd_zmalloc (amt
);
5263 if (included
== NULL
)
5266 symdefs
= bfd_ardata (abfd
)->symdefs
;
5267 bed
= get_elf_backend_data (abfd
);
5268 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5281 symdefend
= symdef
+ c
;
5282 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5284 struct elf_link_hash_entry
*h
;
5286 struct bfd_link_hash_entry
*undefs_tail
;
5291 if (symdef
->file_offset
== last
)
5297 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5298 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5304 if (h
->root
.type
== bfd_link_hash_common
)
5306 /* We currently have a common symbol. The archive map contains
5307 a reference to this symbol, so we may want to include it. We
5308 only want to include it however, if this archive element
5309 contains a definition of the symbol, not just another common
5312 Unfortunately some archivers (including GNU ar) will put
5313 declarations of common symbols into their archive maps, as
5314 well as real definitions, so we cannot just go by the archive
5315 map alone. Instead we must read in the element's symbol
5316 table and check that to see what kind of symbol definition
5318 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5321 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5323 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5324 /* Symbol must be defined. Don't check it again. */
5329 /* We need to include this archive member. */
5330 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5331 if (element
== NULL
)
5334 if (! bfd_check_format (element
, bfd_object
))
5337 undefs_tail
= info
->hash
->undefs_tail
;
5339 if (!(*info
->callbacks
5340 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5342 if (!bfd_link_add_symbols (element
, info
))
5345 /* If there are any new undefined symbols, we need to make
5346 another pass through the archive in order to see whether
5347 they can be defined. FIXME: This isn't perfect, because
5348 common symbols wind up on undefs_tail and because an
5349 undefined symbol which is defined later on in this pass
5350 does not require another pass. This isn't a bug, but it
5351 does make the code less efficient than it could be. */
5352 if (undefs_tail
!= info
->hash
->undefs_tail
)
5355 /* Look backward to mark all symbols from this object file
5356 which we have already seen in this pass. */
5360 included
[mark
] = TRUE
;
5365 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5367 /* We mark subsequent symbols from this object file as we go
5368 on through the loop. */
5369 last
= symdef
->file_offset
;
5379 if (included
!= NULL
)
5384 /* Given an ELF BFD, add symbols to the global hash table as
5388 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5390 switch (bfd_get_format (abfd
))
5393 return elf_link_add_object_symbols (abfd
, info
);
5395 return elf_link_add_archive_symbols (abfd
, info
);
5397 bfd_set_error (bfd_error_wrong_format
);
5402 struct hash_codes_info
5404 unsigned long *hashcodes
;
5408 /* This function will be called though elf_link_hash_traverse to store
5409 all hash value of the exported symbols in an array. */
5412 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5414 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5419 /* Ignore indirect symbols. These are added by the versioning code. */
5420 if (h
->dynindx
== -1)
5423 name
= h
->root
.root
.string
;
5424 if (h
->versioned
>= versioned
)
5426 char *p
= strchr (name
, ELF_VER_CHR
);
5429 alc
= (char *) bfd_malloc (p
- name
+ 1);
5435 memcpy (alc
, name
, p
- name
);
5436 alc
[p
- name
] = '\0';
5441 /* Compute the hash value. */
5442 ha
= bfd_elf_hash (name
);
5444 /* Store the found hash value in the array given as the argument. */
5445 *(inf
->hashcodes
)++ = ha
;
5447 /* And store it in the struct so that we can put it in the hash table
5449 h
->u
.elf_hash_value
= ha
;
5457 struct collect_gnu_hash_codes
5460 const struct elf_backend_data
*bed
;
5461 unsigned long int nsyms
;
5462 unsigned long int maskbits
;
5463 unsigned long int *hashcodes
;
5464 unsigned long int *hashval
;
5465 unsigned long int *indx
;
5466 unsigned long int *counts
;
5469 long int min_dynindx
;
5470 unsigned long int bucketcount
;
5471 unsigned long int symindx
;
5472 long int local_indx
;
5473 long int shift1
, shift2
;
5474 unsigned long int mask
;
5478 /* This function will be called though elf_link_hash_traverse to store
5479 all hash value of the exported symbols in an array. */
5482 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5484 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5489 /* Ignore indirect symbols. These are added by the versioning code. */
5490 if (h
->dynindx
== -1)
5493 /* Ignore also local symbols and undefined symbols. */
5494 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5497 name
= h
->root
.root
.string
;
5498 if (h
->versioned
>= versioned
)
5500 char *p
= strchr (name
, ELF_VER_CHR
);
5503 alc
= (char *) bfd_malloc (p
- name
+ 1);
5509 memcpy (alc
, name
, p
- name
);
5510 alc
[p
- name
] = '\0';
5515 /* Compute the hash value. */
5516 ha
= bfd_elf_gnu_hash (name
);
5518 /* Store the found hash value in the array for compute_bucket_count,
5519 and also for .dynsym reordering purposes. */
5520 s
->hashcodes
[s
->nsyms
] = ha
;
5521 s
->hashval
[h
->dynindx
] = ha
;
5523 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5524 s
->min_dynindx
= h
->dynindx
;
5532 /* This function will be called though elf_link_hash_traverse to do
5533 final dynaminc symbol renumbering. */
5536 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5538 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5539 unsigned long int bucket
;
5540 unsigned long int val
;
5542 /* Ignore indirect symbols. */
5543 if (h
->dynindx
== -1)
5546 /* Ignore also local symbols and undefined symbols. */
5547 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5549 if (h
->dynindx
>= s
->min_dynindx
)
5550 h
->dynindx
= s
->local_indx
++;
5554 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5555 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5556 & ((s
->maskbits
>> s
->shift1
) - 1);
5557 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5559 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5560 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5561 if (s
->counts
[bucket
] == 1)
5562 /* Last element terminates the chain. */
5564 bfd_put_32 (s
->output_bfd
, val
,
5565 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5566 --s
->counts
[bucket
];
5567 h
->dynindx
= s
->indx
[bucket
]++;
5571 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5574 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5576 return !(h
->forced_local
5577 || h
->root
.type
== bfd_link_hash_undefined
5578 || h
->root
.type
== bfd_link_hash_undefweak
5579 || ((h
->root
.type
== bfd_link_hash_defined
5580 || h
->root
.type
== bfd_link_hash_defweak
)
5581 && h
->root
.u
.def
.section
->output_section
== NULL
));
5584 /* Array used to determine the number of hash table buckets to use
5585 based on the number of symbols there are. If there are fewer than
5586 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5587 fewer than 37 we use 17 buckets, and so forth. We never use more
5588 than 32771 buckets. */
5590 static const size_t elf_buckets
[] =
5592 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5596 /* Compute bucket count for hashing table. We do not use a static set
5597 of possible tables sizes anymore. Instead we determine for all
5598 possible reasonable sizes of the table the outcome (i.e., the
5599 number of collisions etc) and choose the best solution. The
5600 weighting functions are not too simple to allow the table to grow
5601 without bounds. Instead one of the weighting factors is the size.
5602 Therefore the result is always a good payoff between few collisions
5603 (= short chain lengths) and table size. */
5605 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5606 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5607 unsigned long int nsyms
,
5610 size_t best_size
= 0;
5611 unsigned long int i
;
5613 /* We have a problem here. The following code to optimize the table
5614 size requires an integer type with more the 32 bits. If
5615 BFD_HOST_U_64_BIT is set we know about such a type. */
5616 #ifdef BFD_HOST_U_64_BIT
5621 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5622 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5623 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5624 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5625 unsigned long int *counts
;
5627 unsigned int no_improvement_count
= 0;
5629 /* Possible optimization parameters: if we have NSYMS symbols we say
5630 that the hashing table must at least have NSYMS/4 and at most
5632 minsize
= nsyms
/ 4;
5635 best_size
= maxsize
= nsyms
* 2;
5640 if ((best_size
& 31) == 0)
5644 /* Create array where we count the collisions in. We must use bfd_malloc
5645 since the size could be large. */
5647 amt
*= sizeof (unsigned long int);
5648 counts
= (unsigned long int *) bfd_malloc (amt
);
5652 /* Compute the "optimal" size for the hash table. The criteria is a
5653 minimal chain length. The minor criteria is (of course) the size
5655 for (i
= minsize
; i
< maxsize
; ++i
)
5657 /* Walk through the array of hashcodes and count the collisions. */
5658 BFD_HOST_U_64_BIT max
;
5659 unsigned long int j
;
5660 unsigned long int fact
;
5662 if (gnu_hash
&& (i
& 31) == 0)
5665 memset (counts
, '\0', i
* sizeof (unsigned long int));
5667 /* Determine how often each hash bucket is used. */
5668 for (j
= 0; j
< nsyms
; ++j
)
5669 ++counts
[hashcodes
[j
] % i
];
5671 /* For the weight function we need some information about the
5672 pagesize on the target. This is information need not be 100%
5673 accurate. Since this information is not available (so far) we
5674 define it here to a reasonable default value. If it is crucial
5675 to have a better value some day simply define this value. */
5676 # ifndef BFD_TARGET_PAGESIZE
5677 # define BFD_TARGET_PAGESIZE (4096)
5680 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5682 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5685 /* Variant 1: optimize for short chains. We add the squares
5686 of all the chain lengths (which favors many small chain
5687 over a few long chains). */
5688 for (j
= 0; j
< i
; ++j
)
5689 max
+= counts
[j
] * counts
[j
];
5691 /* This adds penalties for the overall size of the table. */
5692 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5695 /* Variant 2: Optimize a lot more for small table. Here we
5696 also add squares of the size but we also add penalties for
5697 empty slots (the +1 term). */
5698 for (j
= 0; j
< i
; ++j
)
5699 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5701 /* The overall size of the table is considered, but not as
5702 strong as in variant 1, where it is squared. */
5703 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5707 /* Compare with current best results. */
5708 if (max
< best_chlen
)
5712 no_improvement_count
= 0;
5714 /* PR 11843: Avoid futile long searches for the best bucket size
5715 when there are a large number of symbols. */
5716 else if (++no_improvement_count
== 100)
5723 #endif /* defined (BFD_HOST_U_64_BIT) */
5725 /* This is the fallback solution if no 64bit type is available or if we
5726 are not supposed to spend much time on optimizations. We select the
5727 bucket count using a fixed set of numbers. */
5728 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5730 best_size
= elf_buckets
[i
];
5731 if (nsyms
< elf_buckets
[i
+ 1])
5734 if (gnu_hash
&& best_size
< 2)
5741 /* Size any SHT_GROUP section for ld -r. */
5744 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5748 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5749 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5750 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5755 /* Set a default stack segment size. The value in INFO wins. If it
5756 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5757 undefined it is initialized. */
5760 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5761 struct bfd_link_info
*info
,
5762 const char *legacy_symbol
,
5763 bfd_vma default_size
)
5765 struct elf_link_hash_entry
*h
= NULL
;
5767 /* Look for legacy symbol. */
5769 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5770 FALSE
, FALSE
, FALSE
);
5771 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5772 || h
->root
.type
== bfd_link_hash_defweak
)
5774 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5776 /* The symbol has no type if specified on the command line. */
5777 h
->type
= STT_OBJECT
;
5778 if (info
->stacksize
)
5779 /* xgettext:c-format */
5780 _bfd_error_handler (_("%B: stack size specified and %s set"),
5781 output_bfd
, legacy_symbol
);
5782 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5783 /* xgettext:c-format */
5784 _bfd_error_handler (_("%B: %s not absolute"),
5785 output_bfd
, legacy_symbol
);
5787 info
->stacksize
= h
->root
.u
.def
.value
;
5790 if (!info
->stacksize
)
5791 /* If the user didn't set a size, or explicitly inhibit the
5792 size, set it now. */
5793 info
->stacksize
= default_size
;
5795 /* Provide the legacy symbol, if it is referenced. */
5796 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5797 || h
->root
.type
== bfd_link_hash_undefweak
))
5799 struct bfd_link_hash_entry
*bh
= NULL
;
5801 if (!(_bfd_generic_link_add_one_symbol
5802 (info
, output_bfd
, legacy_symbol
,
5803 BSF_GLOBAL
, bfd_abs_section_ptr
,
5804 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5805 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5808 h
= (struct elf_link_hash_entry
*) bh
;
5810 h
->type
= STT_OBJECT
;
5816 /* Set up the sizes and contents of the ELF dynamic sections. This is
5817 called by the ELF linker emulation before_allocation routine. We
5818 must set the sizes of the sections before the linker sets the
5819 addresses of the various sections. */
5822 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5825 const char *filter_shlib
,
5827 const char *depaudit
,
5828 const char * const *auxiliary_filters
,
5829 struct bfd_link_info
*info
,
5830 asection
**sinterpptr
)
5834 const struct elf_backend_data
*bed
;
5835 struct elf_info_failed asvinfo
;
5839 soname_indx
= (size_t) -1;
5841 if (!is_elf_hash_table (info
->hash
))
5844 bed
= get_elf_backend_data (output_bfd
);
5846 /* Any syms created from now on start with -1 in
5847 got.refcount/offset and plt.refcount/offset. */
5848 elf_hash_table (info
)->init_got_refcount
5849 = elf_hash_table (info
)->init_got_offset
;
5850 elf_hash_table (info
)->init_plt_refcount
5851 = elf_hash_table (info
)->init_plt_offset
;
5853 if (bfd_link_relocatable (info
)
5854 && !_bfd_elf_size_group_sections (info
))
5857 /* The backend may have to create some sections regardless of whether
5858 we're dynamic or not. */
5859 if (bed
->elf_backend_always_size_sections
5860 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5863 /* Determine any GNU_STACK segment requirements, after the backend
5864 has had a chance to set a default segment size. */
5865 if (info
->execstack
)
5866 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5867 else if (info
->noexecstack
)
5868 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5872 asection
*notesec
= NULL
;
5875 for (inputobj
= info
->input_bfds
;
5877 inputobj
= inputobj
->link
.next
)
5882 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5884 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5887 if (s
->flags
& SEC_CODE
)
5891 else if (bed
->default_execstack
)
5894 if (notesec
|| info
->stacksize
> 0)
5895 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5896 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5897 && notesec
->output_section
!= bfd_abs_section_ptr
)
5898 notesec
->output_section
->flags
|= SEC_CODE
;
5901 dynobj
= elf_hash_table (info
)->dynobj
;
5903 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5905 struct elf_info_failed eif
;
5906 struct elf_link_hash_entry
*h
;
5908 struct bfd_elf_version_tree
*t
;
5909 struct bfd_elf_version_expr
*d
;
5911 bfd_boolean all_defined
;
5913 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5914 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5918 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5920 if (soname_indx
== (size_t) -1
5921 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5927 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5929 info
->flags
|= DF_SYMBOLIC
;
5937 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5939 if (indx
== (size_t) -1)
5942 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5943 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5947 if (filter_shlib
!= NULL
)
5951 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5952 filter_shlib
, TRUE
);
5953 if (indx
== (size_t) -1
5954 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5958 if (auxiliary_filters
!= NULL
)
5960 const char * const *p
;
5962 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5966 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5968 if (indx
== (size_t) -1
5969 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5978 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5980 if (indx
== (size_t) -1
5981 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5985 if (depaudit
!= NULL
)
5989 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5991 if (indx
== (size_t) -1
5992 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
5999 /* If we are supposed to export all symbols into the dynamic symbol
6000 table (this is not the normal case), then do so. */
6001 if (info
->export_dynamic
6002 || (bfd_link_executable (info
) && info
->dynamic
))
6004 elf_link_hash_traverse (elf_hash_table (info
),
6005 _bfd_elf_export_symbol
,
6011 /* Make all global versions with definition. */
6012 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6013 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6014 if (!d
->symver
&& d
->literal
)
6016 const char *verstr
, *name
;
6017 size_t namelen
, verlen
, newlen
;
6018 char *newname
, *p
, leading_char
;
6019 struct elf_link_hash_entry
*newh
;
6021 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6023 namelen
= strlen (name
) + (leading_char
!= '\0');
6025 verlen
= strlen (verstr
);
6026 newlen
= namelen
+ verlen
+ 3;
6028 newname
= (char *) bfd_malloc (newlen
);
6029 if (newname
== NULL
)
6031 newname
[0] = leading_char
;
6032 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6034 /* Check the hidden versioned definition. */
6035 p
= newname
+ namelen
;
6037 memcpy (p
, verstr
, verlen
+ 1);
6038 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6039 newname
, FALSE
, FALSE
,
6042 || (newh
->root
.type
!= bfd_link_hash_defined
6043 && newh
->root
.type
!= bfd_link_hash_defweak
))
6045 /* Check the default versioned definition. */
6047 memcpy (p
, verstr
, verlen
+ 1);
6048 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6049 newname
, FALSE
, FALSE
,
6054 /* Mark this version if there is a definition and it is
6055 not defined in a shared object. */
6057 && !newh
->def_dynamic
6058 && (newh
->root
.type
== bfd_link_hash_defined
6059 || newh
->root
.type
== bfd_link_hash_defweak
))
6063 /* Attach all the symbols to their version information. */
6064 asvinfo
.info
= info
;
6065 asvinfo
.failed
= FALSE
;
6067 elf_link_hash_traverse (elf_hash_table (info
),
6068 _bfd_elf_link_assign_sym_version
,
6073 if (!info
->allow_undefined_version
)
6075 /* Check if all global versions have a definition. */
6077 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6078 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6079 if (d
->literal
&& !d
->symver
&& !d
->script
)
6082 (_("%s: undefined version: %s"),
6083 d
->pattern
, t
->name
);
6084 all_defined
= FALSE
;
6089 bfd_set_error (bfd_error_bad_value
);
6094 /* Find all symbols which were defined in a dynamic object and make
6095 the backend pick a reasonable value for them. */
6096 elf_link_hash_traverse (elf_hash_table (info
),
6097 _bfd_elf_adjust_dynamic_symbol
,
6102 /* Add some entries to the .dynamic section. We fill in some of the
6103 values later, in bfd_elf_final_link, but we must add the entries
6104 now so that we know the final size of the .dynamic section. */
6106 /* If there are initialization and/or finalization functions to
6107 call then add the corresponding DT_INIT/DT_FINI entries. */
6108 h
= (info
->init_function
6109 ? elf_link_hash_lookup (elf_hash_table (info
),
6110 info
->init_function
, FALSE
,
6117 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6120 h
= (info
->fini_function
6121 ? elf_link_hash_lookup (elf_hash_table (info
),
6122 info
->fini_function
, FALSE
,
6129 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6133 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6134 if (s
!= NULL
&& s
->linker_has_input
)
6136 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6137 if (! bfd_link_executable (info
))
6142 for (sub
= info
->input_bfds
; sub
!= NULL
;
6143 sub
= sub
->link
.next
)
6144 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6145 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6146 if (elf_section_data (o
)->this_hdr
.sh_type
6147 == SHT_PREINIT_ARRAY
)
6150 (_("%B: .preinit_array section is not allowed in DSO"),
6155 bfd_set_error (bfd_error_nonrepresentable_section
);
6159 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6160 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6163 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6164 if (s
!= NULL
&& s
->linker_has_input
)
6166 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6167 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6170 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6171 if (s
!= NULL
&& s
->linker_has_input
)
6173 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6174 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6178 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6179 /* If .dynstr is excluded from the link, we don't want any of
6180 these tags. Strictly, we should be checking each section
6181 individually; This quick check covers for the case where
6182 someone does a /DISCARD/ : { *(*) }. */
6183 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6185 bfd_size_type strsize
;
6187 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6188 if ((info
->emit_hash
6189 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6190 || (info
->emit_gnu_hash
6191 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6192 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6193 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6194 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6195 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6196 bed
->s
->sizeof_sym
))
6201 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6204 /* The backend must work out the sizes of all the other dynamic
6207 && bed
->elf_backend_size_dynamic_sections
!= NULL
6208 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6211 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6213 unsigned long section_sym_count
;
6214 struct bfd_elf_version_tree
*verdefs
;
6217 /* Set up the version definition section. */
6218 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6219 BFD_ASSERT (s
!= NULL
);
6221 /* We may have created additional version definitions if we are
6222 just linking a regular application. */
6223 verdefs
= info
->version_info
;
6225 /* Skip anonymous version tag. */
6226 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6227 verdefs
= verdefs
->next
;
6229 if (verdefs
== NULL
&& !info
->create_default_symver
)
6230 s
->flags
|= SEC_EXCLUDE
;
6235 struct bfd_elf_version_tree
*t
;
6237 Elf_Internal_Verdef def
;
6238 Elf_Internal_Verdaux defaux
;
6239 struct bfd_link_hash_entry
*bh
;
6240 struct elf_link_hash_entry
*h
;
6246 /* Make space for the base version. */
6247 size
+= sizeof (Elf_External_Verdef
);
6248 size
+= sizeof (Elf_External_Verdaux
);
6251 /* Make space for the default version. */
6252 if (info
->create_default_symver
)
6254 size
+= sizeof (Elf_External_Verdef
);
6258 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6260 struct bfd_elf_version_deps
*n
;
6262 /* Don't emit base version twice. */
6266 size
+= sizeof (Elf_External_Verdef
);
6267 size
+= sizeof (Elf_External_Verdaux
);
6270 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6271 size
+= sizeof (Elf_External_Verdaux
);
6275 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6276 if (s
->contents
== NULL
&& s
->size
!= 0)
6279 /* Fill in the version definition section. */
6283 def
.vd_version
= VER_DEF_CURRENT
;
6284 def
.vd_flags
= VER_FLG_BASE
;
6287 if (info
->create_default_symver
)
6289 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6290 def
.vd_next
= sizeof (Elf_External_Verdef
);
6294 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6295 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6296 + sizeof (Elf_External_Verdaux
));
6299 if (soname_indx
!= (size_t) -1)
6301 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6303 def
.vd_hash
= bfd_elf_hash (soname
);
6304 defaux
.vda_name
= soname_indx
;
6311 name
= lbasename (output_bfd
->filename
);
6312 def
.vd_hash
= bfd_elf_hash (name
);
6313 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6315 if (indx
== (size_t) -1)
6317 defaux
.vda_name
= indx
;
6319 defaux
.vda_next
= 0;
6321 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6322 (Elf_External_Verdef
*) p
);
6323 p
+= sizeof (Elf_External_Verdef
);
6324 if (info
->create_default_symver
)
6326 /* Add a symbol representing this version. */
6328 if (! (_bfd_generic_link_add_one_symbol
6329 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6331 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6333 h
= (struct elf_link_hash_entry
*) bh
;
6336 h
->type
= STT_OBJECT
;
6337 h
->verinfo
.vertree
= NULL
;
6339 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6342 /* Create a duplicate of the base version with the same
6343 aux block, but different flags. */
6346 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6348 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6349 + sizeof (Elf_External_Verdaux
));
6352 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6353 (Elf_External_Verdef
*) p
);
6354 p
+= sizeof (Elf_External_Verdef
);
6356 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6357 (Elf_External_Verdaux
*) p
);
6358 p
+= sizeof (Elf_External_Verdaux
);
6360 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6363 struct bfd_elf_version_deps
*n
;
6365 /* Don't emit the base version twice. */
6370 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6373 /* Add a symbol representing this version. */
6375 if (! (_bfd_generic_link_add_one_symbol
6376 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6378 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6380 h
= (struct elf_link_hash_entry
*) bh
;
6383 h
->type
= STT_OBJECT
;
6384 h
->verinfo
.vertree
= t
;
6386 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6389 def
.vd_version
= VER_DEF_CURRENT
;
6391 if (t
->globals
.list
== NULL
6392 && t
->locals
.list
== NULL
6394 def
.vd_flags
|= VER_FLG_WEAK
;
6395 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6396 def
.vd_cnt
= cdeps
+ 1;
6397 def
.vd_hash
= bfd_elf_hash (t
->name
);
6398 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6401 /* If a basever node is next, it *must* be the last node in
6402 the chain, otherwise Verdef construction breaks. */
6403 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6404 BFD_ASSERT (t
->next
->next
== NULL
);
6406 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6407 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6408 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6410 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6411 (Elf_External_Verdef
*) p
);
6412 p
+= sizeof (Elf_External_Verdef
);
6414 defaux
.vda_name
= h
->dynstr_index
;
6415 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6417 defaux
.vda_next
= 0;
6418 if (t
->deps
!= NULL
)
6419 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6420 t
->name_indx
= defaux
.vda_name
;
6422 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6423 (Elf_External_Verdaux
*) p
);
6424 p
+= sizeof (Elf_External_Verdaux
);
6426 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6428 if (n
->version_needed
== NULL
)
6430 /* This can happen if there was an error in the
6432 defaux
.vda_name
= 0;
6436 defaux
.vda_name
= n
->version_needed
->name_indx
;
6437 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6440 if (n
->next
== NULL
)
6441 defaux
.vda_next
= 0;
6443 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6445 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6446 (Elf_External_Verdaux
*) p
);
6447 p
+= sizeof (Elf_External_Verdaux
);
6451 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6452 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6455 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6458 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6460 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6463 else if (info
->flags
& DF_BIND_NOW
)
6465 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6471 if (bfd_link_executable (info
))
6472 info
->flags_1
&= ~ (DF_1_INITFIRST
6475 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6479 /* Work out the size of the version reference section. */
6481 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6482 BFD_ASSERT (s
!= NULL
);
6484 struct elf_find_verdep_info sinfo
;
6487 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6488 if (sinfo
.vers
== 0)
6490 sinfo
.failed
= FALSE
;
6492 elf_link_hash_traverse (elf_hash_table (info
),
6493 _bfd_elf_link_find_version_dependencies
,
6498 if (elf_tdata (output_bfd
)->verref
== NULL
)
6499 s
->flags
|= SEC_EXCLUDE
;
6502 Elf_Internal_Verneed
*t
;
6507 /* Build the version dependency section. */
6510 for (t
= elf_tdata (output_bfd
)->verref
;
6514 Elf_Internal_Vernaux
*a
;
6516 size
+= sizeof (Elf_External_Verneed
);
6518 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6519 size
+= sizeof (Elf_External_Vernaux
);
6523 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6524 if (s
->contents
== NULL
)
6528 for (t
= elf_tdata (output_bfd
)->verref
;
6533 Elf_Internal_Vernaux
*a
;
6537 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6540 t
->vn_version
= VER_NEED_CURRENT
;
6542 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6543 elf_dt_name (t
->vn_bfd
) != NULL
6544 ? elf_dt_name (t
->vn_bfd
)
6545 : lbasename (t
->vn_bfd
->filename
),
6547 if (indx
== (size_t) -1)
6550 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6551 if (t
->vn_nextref
== NULL
)
6554 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6555 + caux
* sizeof (Elf_External_Vernaux
));
6557 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6558 (Elf_External_Verneed
*) p
);
6559 p
+= sizeof (Elf_External_Verneed
);
6561 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6563 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6564 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6565 a
->vna_nodename
, FALSE
);
6566 if (indx
== (size_t) -1)
6569 if (a
->vna_nextptr
== NULL
)
6572 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6574 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6575 (Elf_External_Vernaux
*) p
);
6576 p
+= sizeof (Elf_External_Vernaux
);
6580 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6581 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6584 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6588 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6589 && elf_tdata (output_bfd
)->cverdefs
== 0)
6590 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6591 §ion_sym_count
) == 0)
6593 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6594 s
->flags
|= SEC_EXCLUDE
;
6600 /* Find the first non-excluded output section. We'll use its
6601 section symbol for some emitted relocs. */
6603 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6607 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6608 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6609 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6611 elf_hash_table (info
)->text_index_section
= s
;
6616 /* Find two non-excluded output sections, one for code, one for data.
6617 We'll use their section symbols for some emitted relocs. */
6619 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6623 /* Data first, since setting text_index_section changes
6624 _bfd_elf_link_omit_section_dynsym. */
6625 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6626 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6627 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6629 elf_hash_table (info
)->data_index_section
= s
;
6633 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6634 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6635 == (SEC_ALLOC
| SEC_READONLY
))
6636 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6638 elf_hash_table (info
)->text_index_section
= s
;
6642 if (elf_hash_table (info
)->text_index_section
== NULL
)
6643 elf_hash_table (info
)->text_index_section
6644 = elf_hash_table (info
)->data_index_section
;
6648 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6650 const struct elf_backend_data
*bed
;
6652 if (!is_elf_hash_table (info
->hash
))
6655 bed
= get_elf_backend_data (output_bfd
);
6656 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6658 if (elf_hash_table (info
)->dynamic_sections_created
)
6662 bfd_size_type dynsymcount
;
6663 unsigned long section_sym_count
;
6664 unsigned int dtagcount
;
6666 dynobj
= elf_hash_table (info
)->dynobj
;
6668 /* Assign dynsym indicies. In a shared library we generate a
6669 section symbol for each output section, which come first.
6670 Next come all of the back-end allocated local dynamic syms,
6671 followed by the rest of the global symbols. */
6673 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6674 §ion_sym_count
);
6676 /* Work out the size of the symbol version section. */
6677 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6678 BFD_ASSERT (s
!= NULL
);
6679 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6681 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6682 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6683 if (s
->contents
== NULL
)
6686 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6690 /* Set the size of the .dynsym and .hash sections. We counted
6691 the number of dynamic symbols in elf_link_add_object_symbols.
6692 We will build the contents of .dynsym and .hash when we build
6693 the final symbol table, because until then we do not know the
6694 correct value to give the symbols. We built the .dynstr
6695 section as we went along in elf_link_add_object_symbols. */
6696 s
= elf_hash_table (info
)->dynsym
;
6697 BFD_ASSERT (s
!= NULL
);
6698 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6700 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6701 if (s
->contents
== NULL
)
6704 /* The first entry in .dynsym is a dummy symbol. Clear all the
6705 section syms, in case we don't output them all. */
6706 ++section_sym_count
;
6707 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6709 elf_hash_table (info
)->bucketcount
= 0;
6711 /* Compute the size of the hashing table. As a side effect this
6712 computes the hash values for all the names we export. */
6713 if (info
->emit_hash
)
6715 unsigned long int *hashcodes
;
6716 struct hash_codes_info hashinf
;
6718 unsigned long int nsyms
;
6720 size_t hash_entry_size
;
6722 /* Compute the hash values for all exported symbols. At the same
6723 time store the values in an array so that we could use them for
6725 amt
= dynsymcount
* sizeof (unsigned long int);
6726 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6727 if (hashcodes
== NULL
)
6729 hashinf
.hashcodes
= hashcodes
;
6730 hashinf
.error
= FALSE
;
6732 /* Put all hash values in HASHCODES. */
6733 elf_link_hash_traverse (elf_hash_table (info
),
6734 elf_collect_hash_codes
, &hashinf
);
6741 nsyms
= hashinf
.hashcodes
- hashcodes
;
6743 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6746 if (bucketcount
== 0)
6749 elf_hash_table (info
)->bucketcount
= bucketcount
;
6751 s
= bfd_get_linker_section (dynobj
, ".hash");
6752 BFD_ASSERT (s
!= NULL
);
6753 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6754 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6755 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6756 if (s
->contents
== NULL
)
6759 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6760 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6761 s
->contents
+ hash_entry_size
);
6764 if (info
->emit_gnu_hash
)
6767 unsigned char *contents
;
6768 struct collect_gnu_hash_codes cinfo
;
6772 memset (&cinfo
, 0, sizeof (cinfo
));
6774 /* Compute the hash values for all exported symbols. At the same
6775 time store the values in an array so that we could use them for
6777 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6778 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6779 if (cinfo
.hashcodes
== NULL
)
6782 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6783 cinfo
.min_dynindx
= -1;
6784 cinfo
.output_bfd
= output_bfd
;
6787 /* Put all hash values in HASHCODES. */
6788 elf_link_hash_traverse (elf_hash_table (info
),
6789 elf_collect_gnu_hash_codes
, &cinfo
);
6792 free (cinfo
.hashcodes
);
6797 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6799 if (bucketcount
== 0)
6801 free (cinfo
.hashcodes
);
6805 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6806 BFD_ASSERT (s
!= NULL
);
6808 if (cinfo
.nsyms
== 0)
6810 /* Empty .gnu.hash section is special. */
6811 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6812 free (cinfo
.hashcodes
);
6813 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6814 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6815 if (contents
== NULL
)
6817 s
->contents
= contents
;
6818 /* 1 empty bucket. */
6819 bfd_put_32 (output_bfd
, 1, contents
);
6820 /* SYMIDX above the special symbol 0. */
6821 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6822 /* Just one word for bitmask. */
6823 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6824 /* Only hash fn bloom filter. */
6825 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6826 /* No hashes are valid - empty bitmask. */
6827 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6828 /* No hashes in the only bucket. */
6829 bfd_put_32 (output_bfd
, 0,
6830 contents
+ 16 + bed
->s
->arch_size
/ 8);
6834 unsigned long int maskwords
, maskbitslog2
, x
;
6835 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6839 while ((x
>>= 1) != 0)
6841 if (maskbitslog2
< 3)
6843 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6844 maskbitslog2
= maskbitslog2
+ 3;
6846 maskbitslog2
= maskbitslog2
+ 2;
6847 if (bed
->s
->arch_size
== 64)
6849 if (maskbitslog2
== 5)
6855 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6856 cinfo
.shift2
= maskbitslog2
;
6857 cinfo
.maskbits
= 1 << maskbitslog2
;
6858 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6859 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6860 amt
+= maskwords
* sizeof (bfd_vma
);
6861 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6862 if (cinfo
.bitmask
== NULL
)
6864 free (cinfo
.hashcodes
);
6868 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6869 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6870 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6871 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6873 /* Determine how often each hash bucket is used. */
6874 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6875 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6876 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6878 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6879 if (cinfo
.counts
[i
] != 0)
6881 cinfo
.indx
[i
] = cnt
;
6882 cnt
+= cinfo
.counts
[i
];
6884 BFD_ASSERT (cnt
== dynsymcount
);
6885 cinfo
.bucketcount
= bucketcount
;
6886 cinfo
.local_indx
= cinfo
.min_dynindx
;
6888 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6889 s
->size
+= cinfo
.maskbits
/ 8;
6890 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6891 if (contents
== NULL
)
6893 free (cinfo
.bitmask
);
6894 free (cinfo
.hashcodes
);
6898 s
->contents
= contents
;
6899 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6900 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6901 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6902 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6903 contents
+= 16 + cinfo
.maskbits
/ 8;
6905 for (i
= 0; i
< bucketcount
; ++i
)
6907 if (cinfo
.counts
[i
] == 0)
6908 bfd_put_32 (output_bfd
, 0, contents
);
6910 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6914 cinfo
.contents
= contents
;
6916 /* Renumber dynamic symbols, populate .gnu.hash section. */
6917 elf_link_hash_traverse (elf_hash_table (info
),
6918 elf_renumber_gnu_hash_syms
, &cinfo
);
6920 contents
= s
->contents
+ 16;
6921 for (i
= 0; i
< maskwords
; ++i
)
6923 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6925 contents
+= bed
->s
->arch_size
/ 8;
6928 free (cinfo
.bitmask
);
6929 free (cinfo
.hashcodes
);
6933 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6934 BFD_ASSERT (s
!= NULL
);
6936 elf_finalize_dynstr (output_bfd
, info
);
6938 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6940 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6941 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6948 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6951 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6954 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6955 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6958 /* Finish SHF_MERGE section merging. */
6961 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6966 if (!is_elf_hash_table (info
->hash
))
6969 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6970 if ((ibfd
->flags
& DYNAMIC
) == 0
6971 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6972 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6973 == get_elf_backend_data (obfd
)->s
->elfclass
))
6974 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6975 if ((sec
->flags
& SEC_MERGE
) != 0
6976 && !bfd_is_abs_section (sec
->output_section
))
6978 struct bfd_elf_section_data
*secdata
;
6980 secdata
= elf_section_data (sec
);
6981 if (! _bfd_add_merge_section (obfd
,
6982 &elf_hash_table (info
)->merge_info
,
6983 sec
, &secdata
->sec_info
))
6985 else if (secdata
->sec_info
)
6986 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6989 if (elf_hash_table (info
)->merge_info
!= NULL
)
6990 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
6991 merge_sections_remove_hook
);
6995 /* Create an entry in an ELF linker hash table. */
6997 struct bfd_hash_entry
*
6998 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6999 struct bfd_hash_table
*table
,
7002 /* Allocate the structure if it has not already been allocated by a
7006 entry
= (struct bfd_hash_entry
*)
7007 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7012 /* Call the allocation method of the superclass. */
7013 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7016 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7017 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7019 /* Set local fields. */
7022 ret
->got
= htab
->init_got_refcount
;
7023 ret
->plt
= htab
->init_plt_refcount
;
7024 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7025 - offsetof (struct elf_link_hash_entry
, size
)));
7026 /* Assume that we have been called by a non-ELF symbol reader.
7027 This flag is then reset by the code which reads an ELF input
7028 file. This ensures that a symbol created by a non-ELF symbol
7029 reader will have the flag set correctly. */
7036 /* Copy data from an indirect symbol to its direct symbol, hiding the
7037 old indirect symbol. Also used for copying flags to a weakdef. */
7040 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7041 struct elf_link_hash_entry
*dir
,
7042 struct elf_link_hash_entry
*ind
)
7044 struct elf_link_hash_table
*htab
;
7046 /* Copy down any references that we may have already seen to the
7047 symbol which just became indirect if DIR isn't a hidden versioned
7050 if (dir
->versioned
!= versioned_hidden
)
7052 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7053 dir
->ref_regular
|= ind
->ref_regular
;
7054 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7055 dir
->non_got_ref
|= ind
->non_got_ref
;
7056 dir
->needs_plt
|= ind
->needs_plt
;
7057 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7060 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7063 /* Copy over the global and procedure linkage table refcount entries.
7064 These may have been already set up by a check_relocs routine. */
7065 htab
= elf_hash_table (info
);
7066 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7068 if (dir
->got
.refcount
< 0)
7069 dir
->got
.refcount
= 0;
7070 dir
->got
.refcount
+= ind
->got
.refcount
;
7071 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7074 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7076 if (dir
->plt
.refcount
< 0)
7077 dir
->plt
.refcount
= 0;
7078 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7079 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7082 if (ind
->dynindx
!= -1)
7084 if (dir
->dynindx
!= -1)
7085 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7086 dir
->dynindx
= ind
->dynindx
;
7087 dir
->dynstr_index
= ind
->dynstr_index
;
7089 ind
->dynstr_index
= 0;
7094 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7095 struct elf_link_hash_entry
*h
,
7096 bfd_boolean force_local
)
7098 /* STT_GNU_IFUNC symbol must go through PLT. */
7099 if (h
->type
!= STT_GNU_IFUNC
)
7101 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7106 h
->forced_local
= 1;
7107 if (h
->dynindx
!= -1)
7110 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7116 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7120 _bfd_elf_link_hash_table_init
7121 (struct elf_link_hash_table
*table
,
7123 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7124 struct bfd_hash_table
*,
7126 unsigned int entsize
,
7127 enum elf_target_id target_id
)
7130 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7132 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7133 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7134 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7135 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7136 /* The first dynamic symbol is a dummy. */
7137 table
->dynsymcount
= 1;
7139 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7141 table
->root
.type
= bfd_link_elf_hash_table
;
7142 table
->hash_table_id
= target_id
;
7147 /* Create an ELF linker hash table. */
7149 struct bfd_link_hash_table
*
7150 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7152 struct elf_link_hash_table
*ret
;
7153 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7155 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7159 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7160 sizeof (struct elf_link_hash_entry
),
7166 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7171 /* Destroy an ELF linker hash table. */
7174 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7176 struct elf_link_hash_table
*htab
;
7178 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7179 if (htab
->dynstr
!= NULL
)
7180 _bfd_elf_strtab_free (htab
->dynstr
);
7181 _bfd_merge_sections_free (htab
->merge_info
);
7182 _bfd_generic_link_hash_table_free (obfd
);
7185 /* This is a hook for the ELF emulation code in the generic linker to
7186 tell the backend linker what file name to use for the DT_NEEDED
7187 entry for a dynamic object. */
7190 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7192 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd
) == bfd_object
)
7194 elf_dt_name (abfd
) = name
;
7198 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7201 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7202 && bfd_get_format (abfd
) == bfd_object
)
7203 lib_class
= elf_dyn_lib_class (abfd
);
7210 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7212 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7213 && bfd_get_format (abfd
) == bfd_object
)
7214 elf_dyn_lib_class (abfd
) = lib_class
;
7217 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7218 the linker ELF emulation code. */
7220 struct bfd_link_needed_list
*
7221 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7222 struct bfd_link_info
*info
)
7224 if (! is_elf_hash_table (info
->hash
))
7226 return elf_hash_table (info
)->needed
;
7229 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7230 hook for the linker ELF emulation code. */
7232 struct bfd_link_needed_list
*
7233 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7234 struct bfd_link_info
*info
)
7236 if (! is_elf_hash_table (info
->hash
))
7238 return elf_hash_table (info
)->runpath
;
7241 /* Get the name actually used for a dynamic object for a link. This
7242 is the SONAME entry if there is one. Otherwise, it is the string
7243 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7246 bfd_elf_get_dt_soname (bfd
*abfd
)
7248 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7249 && bfd_get_format (abfd
) == bfd_object
)
7250 return elf_dt_name (abfd
);
7254 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7255 the ELF linker emulation code. */
7258 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7259 struct bfd_link_needed_list
**pneeded
)
7262 bfd_byte
*dynbuf
= NULL
;
7263 unsigned int elfsec
;
7264 unsigned long shlink
;
7265 bfd_byte
*extdyn
, *extdynend
;
7267 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7271 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7272 || bfd_get_format (abfd
) != bfd_object
)
7275 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7276 if (s
== NULL
|| s
->size
== 0)
7279 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7282 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7283 if (elfsec
== SHN_BAD
)
7286 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7288 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7289 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7292 extdynend
= extdyn
+ s
->size
;
7293 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7295 Elf_Internal_Dyn dyn
;
7297 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7299 if (dyn
.d_tag
== DT_NULL
)
7302 if (dyn
.d_tag
== DT_NEEDED
)
7305 struct bfd_link_needed_list
*l
;
7306 unsigned int tagv
= dyn
.d_un
.d_val
;
7309 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7314 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7335 struct elf_symbuf_symbol
7337 unsigned long st_name
; /* Symbol name, index in string tbl */
7338 unsigned char st_info
; /* Type and binding attributes */
7339 unsigned char st_other
; /* Visibilty, and target specific */
7342 struct elf_symbuf_head
7344 struct elf_symbuf_symbol
*ssym
;
7346 unsigned int st_shndx
;
7353 Elf_Internal_Sym
*isym
;
7354 struct elf_symbuf_symbol
*ssym
;
7359 /* Sort references to symbols by ascending section number. */
7362 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7364 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7365 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7367 return s1
->st_shndx
- s2
->st_shndx
;
7371 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7373 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7374 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7375 return strcmp (s1
->name
, s2
->name
);
7378 static struct elf_symbuf_head
*
7379 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7381 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7382 struct elf_symbuf_symbol
*ssym
;
7383 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7384 size_t i
, shndx_count
, total_size
;
7386 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7390 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7391 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7392 *ind
++ = &isymbuf
[i
];
7395 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7396 elf_sort_elf_symbol
);
7399 if (indbufend
> indbuf
)
7400 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7401 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7404 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7405 + (indbufend
- indbuf
) * sizeof (*ssym
));
7406 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7407 if (ssymbuf
== NULL
)
7413 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7414 ssymbuf
->ssym
= NULL
;
7415 ssymbuf
->count
= shndx_count
;
7416 ssymbuf
->st_shndx
= 0;
7417 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7419 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7422 ssymhead
->ssym
= ssym
;
7423 ssymhead
->count
= 0;
7424 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7426 ssym
->st_name
= (*ind
)->st_name
;
7427 ssym
->st_info
= (*ind
)->st_info
;
7428 ssym
->st_other
= (*ind
)->st_other
;
7431 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7432 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7439 /* Check if 2 sections define the same set of local and global
7443 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7444 struct bfd_link_info
*info
)
7447 const struct elf_backend_data
*bed1
, *bed2
;
7448 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7449 size_t symcount1
, symcount2
;
7450 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7451 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7452 Elf_Internal_Sym
*isym
, *isymend
;
7453 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7454 size_t count1
, count2
, i
;
7455 unsigned int shndx1
, shndx2
;
7461 /* Both sections have to be in ELF. */
7462 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7463 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7466 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7469 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7470 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7471 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7474 bed1
= get_elf_backend_data (bfd1
);
7475 bed2
= get_elf_backend_data (bfd2
);
7476 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7477 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7478 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7479 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7481 if (symcount1
== 0 || symcount2
== 0)
7487 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7488 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7490 if (ssymbuf1
== NULL
)
7492 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7494 if (isymbuf1
== NULL
)
7497 if (!info
->reduce_memory_overheads
)
7498 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7499 = elf_create_symbuf (symcount1
, isymbuf1
);
7502 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7504 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7506 if (isymbuf2
== NULL
)
7509 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7510 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7511 = elf_create_symbuf (symcount2
, isymbuf2
);
7514 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7516 /* Optimized faster version. */
7518 struct elf_symbol
*symp
;
7519 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7522 hi
= ssymbuf1
->count
;
7527 mid
= (lo
+ hi
) / 2;
7528 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7530 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7534 count1
= ssymbuf1
[mid
].count
;
7541 hi
= ssymbuf2
->count
;
7546 mid
= (lo
+ hi
) / 2;
7547 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7549 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7553 count2
= ssymbuf2
[mid
].count
;
7559 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7563 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7565 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7566 if (symtable1
== NULL
|| symtable2
== NULL
)
7570 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7571 ssym
< ssymend
; ssym
++, symp
++)
7573 symp
->u
.ssym
= ssym
;
7574 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7580 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7581 ssym
< ssymend
; ssym
++, symp
++)
7583 symp
->u
.ssym
= ssym
;
7584 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7589 /* Sort symbol by name. */
7590 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7591 elf_sym_name_compare
);
7592 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7593 elf_sym_name_compare
);
7595 for (i
= 0; i
< count1
; i
++)
7596 /* Two symbols must have the same binding, type and name. */
7597 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7598 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7599 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7606 symtable1
= (struct elf_symbol
*)
7607 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7608 symtable2
= (struct elf_symbol
*)
7609 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7610 if (symtable1
== NULL
|| symtable2
== NULL
)
7613 /* Count definitions in the section. */
7615 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7616 if (isym
->st_shndx
== shndx1
)
7617 symtable1
[count1
++].u
.isym
= isym
;
7620 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7621 if (isym
->st_shndx
== shndx2
)
7622 symtable2
[count2
++].u
.isym
= isym
;
7624 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7627 for (i
= 0; i
< count1
; i
++)
7629 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7630 symtable1
[i
].u
.isym
->st_name
);
7632 for (i
= 0; i
< count2
; i
++)
7634 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7635 symtable2
[i
].u
.isym
->st_name
);
7637 /* Sort symbol by name. */
7638 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7639 elf_sym_name_compare
);
7640 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7641 elf_sym_name_compare
);
7643 for (i
= 0; i
< count1
; i
++)
7644 /* Two symbols must have the same binding, type and name. */
7645 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7646 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7647 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7665 /* Return TRUE if 2 section types are compatible. */
7668 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7669 bfd
*bbfd
, const asection
*bsec
)
7673 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7674 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7677 return elf_section_type (asec
) == elf_section_type (bsec
);
7680 /* Final phase of ELF linker. */
7682 /* A structure we use to avoid passing large numbers of arguments. */
7684 struct elf_final_link_info
7686 /* General link information. */
7687 struct bfd_link_info
*info
;
7690 /* Symbol string table. */
7691 struct elf_strtab_hash
*symstrtab
;
7692 /* .hash section. */
7694 /* symbol version section (.gnu.version). */
7695 asection
*symver_sec
;
7696 /* Buffer large enough to hold contents of any section. */
7698 /* Buffer large enough to hold external relocs of any section. */
7699 void *external_relocs
;
7700 /* Buffer large enough to hold internal relocs of any section. */
7701 Elf_Internal_Rela
*internal_relocs
;
7702 /* Buffer large enough to hold external local symbols of any input
7704 bfd_byte
*external_syms
;
7705 /* And a buffer for symbol section indices. */
7706 Elf_External_Sym_Shndx
*locsym_shndx
;
7707 /* Buffer large enough to hold internal local symbols of any input
7709 Elf_Internal_Sym
*internal_syms
;
7710 /* Array large enough to hold a symbol index for each local symbol
7711 of any input BFD. */
7713 /* Array large enough to hold a section pointer for each local
7714 symbol of any input BFD. */
7715 asection
**sections
;
7716 /* Buffer for SHT_SYMTAB_SHNDX section. */
7717 Elf_External_Sym_Shndx
*symshndxbuf
;
7718 /* Number of STT_FILE syms seen. */
7719 size_t filesym_count
;
7722 /* This struct is used to pass information to elf_link_output_extsym. */
7724 struct elf_outext_info
7727 bfd_boolean localsyms
;
7728 bfd_boolean file_sym_done
;
7729 struct elf_final_link_info
*flinfo
;
7733 /* Support for evaluating a complex relocation.
7735 Complex relocations are generalized, self-describing relocations. The
7736 implementation of them consists of two parts: complex symbols, and the
7737 relocations themselves.
7739 The relocations are use a reserved elf-wide relocation type code (R_RELC
7740 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7741 information (start bit, end bit, word width, etc) into the addend. This
7742 information is extracted from CGEN-generated operand tables within gas.
7744 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7745 internal) representing prefix-notation expressions, including but not
7746 limited to those sorts of expressions normally encoded as addends in the
7747 addend field. The symbol mangling format is:
7750 | <unary-operator> ':' <node>
7751 | <binary-operator> ':' <node> ':' <node>
7754 <literal> := 's' <digits=N> ':' <N character symbol name>
7755 | 'S' <digits=N> ':' <N character section name>
7759 <binary-operator> := as in C
7760 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7763 set_symbol_value (bfd
*bfd_with_globals
,
7764 Elf_Internal_Sym
*isymbuf
,
7769 struct elf_link_hash_entry
**sym_hashes
;
7770 struct elf_link_hash_entry
*h
;
7771 size_t extsymoff
= locsymcount
;
7773 if (symidx
< locsymcount
)
7775 Elf_Internal_Sym
*sym
;
7777 sym
= isymbuf
+ symidx
;
7778 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7780 /* It is a local symbol: move it to the
7781 "absolute" section and give it a value. */
7782 sym
->st_shndx
= SHN_ABS
;
7783 sym
->st_value
= val
;
7786 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7790 /* It is a global symbol: set its link type
7791 to "defined" and give it a value. */
7793 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7794 h
= sym_hashes
[symidx
- extsymoff
];
7795 while (h
->root
.type
== bfd_link_hash_indirect
7796 || h
->root
.type
== bfd_link_hash_warning
)
7797 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7798 h
->root
.type
= bfd_link_hash_defined
;
7799 h
->root
.u
.def
.value
= val
;
7800 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7804 resolve_symbol (const char *name
,
7806 struct elf_final_link_info
*flinfo
,
7808 Elf_Internal_Sym
*isymbuf
,
7811 Elf_Internal_Sym
*sym
;
7812 struct bfd_link_hash_entry
*global_entry
;
7813 const char *candidate
= NULL
;
7814 Elf_Internal_Shdr
*symtab_hdr
;
7817 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7819 for (i
= 0; i
< locsymcount
; ++ i
)
7823 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7826 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7827 symtab_hdr
->sh_link
,
7830 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7831 name
, candidate
, (unsigned long) sym
->st_value
);
7833 if (candidate
&& strcmp (candidate
, name
) == 0)
7835 asection
*sec
= flinfo
->sections
[i
];
7837 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7838 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7840 printf ("Found symbol with value %8.8lx\n",
7841 (unsigned long) *result
);
7847 /* Hmm, haven't found it yet. perhaps it is a global. */
7848 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7849 FALSE
, FALSE
, TRUE
);
7853 if (global_entry
->type
== bfd_link_hash_defined
7854 || global_entry
->type
== bfd_link_hash_defweak
)
7856 *result
= (global_entry
->u
.def
.value
7857 + global_entry
->u
.def
.section
->output_section
->vma
7858 + global_entry
->u
.def
.section
->output_offset
);
7860 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7861 global_entry
->root
.string
, (unsigned long) *result
);
7869 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7870 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7871 names like "foo.end" which is the end address of section "foo". */
7874 resolve_section (const char *name
,
7882 for (curr
= sections
; curr
; curr
= curr
->next
)
7883 if (strcmp (curr
->name
, name
) == 0)
7885 *result
= curr
->vma
;
7889 /* Hmm. still haven't found it. try pseudo-section names. */
7890 /* FIXME: This could be coded more efficiently... */
7891 for (curr
= sections
; curr
; curr
= curr
->next
)
7893 len
= strlen (curr
->name
);
7894 if (len
> strlen (name
))
7897 if (strncmp (curr
->name
, name
, len
) == 0)
7899 if (strncmp (".end", name
+ len
, 4) == 0)
7901 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7905 /* Insert more pseudo-section names here, if you like. */
7913 undefined_reference (const char *reftype
, const char *name
)
7915 /* xgettext:c-format */
7916 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7921 eval_symbol (bfd_vma
*result
,
7924 struct elf_final_link_info
*flinfo
,
7926 Elf_Internal_Sym
*isymbuf
,
7935 const char *sym
= *symp
;
7937 bfd_boolean symbol_is_section
= FALSE
;
7942 if (len
< 1 || len
> sizeof (symbuf
))
7944 bfd_set_error (bfd_error_invalid_operation
);
7957 *result
= strtoul (sym
, (char **) symp
, 16);
7961 symbol_is_section
= TRUE
;
7965 symlen
= strtol (sym
, (char **) symp
, 10);
7966 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7968 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7970 bfd_set_error (bfd_error_invalid_operation
);
7974 memcpy (symbuf
, sym
, symlen
);
7975 symbuf
[symlen
] = '\0';
7976 *symp
= sym
+ symlen
;
7978 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7979 the symbol as a section, or vice-versa. so we're pretty liberal in our
7980 interpretation here; section means "try section first", not "must be a
7981 section", and likewise with symbol. */
7983 if (symbol_is_section
)
7985 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
7986 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7987 isymbuf
, locsymcount
))
7989 undefined_reference ("section", symbuf
);
7995 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7996 isymbuf
, locsymcount
)
7997 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8000 undefined_reference ("symbol", symbuf
);
8007 /* All that remains are operators. */
8009 #define UNARY_OP(op) \
8010 if (strncmp (sym, #op, strlen (#op)) == 0) \
8012 sym += strlen (#op); \
8016 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8017 isymbuf, locsymcount, signed_p)) \
8020 *result = op ((bfd_signed_vma) a); \
8026 #define BINARY_OP(op) \
8027 if (strncmp (sym, #op, strlen (#op)) == 0) \
8029 sym += strlen (#op); \
8033 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8034 isymbuf, locsymcount, signed_p)) \
8037 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8038 isymbuf, locsymcount, signed_p)) \
8041 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8071 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8072 bfd_set_error (bfd_error_invalid_operation
);
8078 put_value (bfd_vma size
,
8079 unsigned long chunksz
,
8084 location
+= (size
- chunksz
);
8086 for (; size
; size
-= chunksz
, location
-= chunksz
)
8091 bfd_put_8 (input_bfd
, x
, location
);
8095 bfd_put_16 (input_bfd
, x
, location
);
8099 bfd_put_32 (input_bfd
, x
, location
);
8100 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8106 bfd_put_64 (input_bfd
, x
, location
);
8107 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8120 get_value (bfd_vma size
,
8121 unsigned long chunksz
,
8128 /* Sanity checks. */
8129 BFD_ASSERT (chunksz
<= sizeof (x
)
8132 && (size
% chunksz
) == 0
8133 && input_bfd
!= NULL
8134 && location
!= NULL
);
8136 if (chunksz
== sizeof (x
))
8138 BFD_ASSERT (size
== chunksz
);
8140 /* Make sure that we do not perform an undefined shift operation.
8141 We know that size == chunksz so there will only be one iteration
8142 of the loop below. */
8146 shift
= 8 * chunksz
;
8148 for (; size
; size
-= chunksz
, location
+= chunksz
)
8153 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8156 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8159 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8163 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8174 decode_complex_addend (unsigned long *start
, /* in bits */
8175 unsigned long *oplen
, /* in bits */
8176 unsigned long *len
, /* in bits */
8177 unsigned long *wordsz
, /* in bytes */
8178 unsigned long *chunksz
, /* in bytes */
8179 unsigned long *lsb0_p
,
8180 unsigned long *signed_p
,
8181 unsigned long *trunc_p
,
8182 unsigned long encoded
)
8184 * start
= encoded
& 0x3F;
8185 * len
= (encoded
>> 6) & 0x3F;
8186 * oplen
= (encoded
>> 12) & 0x3F;
8187 * wordsz
= (encoded
>> 18) & 0xF;
8188 * chunksz
= (encoded
>> 22) & 0xF;
8189 * lsb0_p
= (encoded
>> 27) & 1;
8190 * signed_p
= (encoded
>> 28) & 1;
8191 * trunc_p
= (encoded
>> 29) & 1;
8194 bfd_reloc_status_type
8195 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8196 asection
*input_section ATTRIBUTE_UNUSED
,
8198 Elf_Internal_Rela
*rel
,
8201 bfd_vma shift
, x
, mask
;
8202 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8203 bfd_reloc_status_type r
;
8205 /* Perform this reloc, since it is complex.
8206 (this is not to say that it necessarily refers to a complex
8207 symbol; merely that it is a self-describing CGEN based reloc.
8208 i.e. the addend has the complete reloc information (bit start, end,
8209 word size, etc) encoded within it.). */
8211 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8212 &chunksz
, &lsb0_p
, &signed_p
,
8213 &trunc_p
, rel
->r_addend
);
8215 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8218 shift
= (start
+ 1) - len
;
8220 shift
= (8 * wordsz
) - (start
+ len
);
8222 x
= get_value (wordsz
, chunksz
, input_bfd
,
8223 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8226 printf ("Doing complex reloc: "
8227 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8228 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8229 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8230 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8231 oplen
, (unsigned long) x
, (unsigned long) mask
,
8232 (unsigned long) relocation
);
8237 /* Now do an overflow check. */
8238 r
= bfd_check_overflow ((signed_p
8239 ? complain_overflow_signed
8240 : complain_overflow_unsigned
),
8241 len
, 0, (8 * wordsz
),
8245 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8248 printf (" relocation: %8.8lx\n"
8249 " shifted mask: %8.8lx\n"
8250 " shifted/masked reloc: %8.8lx\n"
8251 " result: %8.8lx\n",
8252 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8253 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8255 put_value (wordsz
, chunksz
, input_bfd
, x
,
8256 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8260 /* Functions to read r_offset from external (target order) reloc
8261 entry. Faster than bfd_getl32 et al, because we let the compiler
8262 know the value is aligned. */
8265 ext32l_r_offset (const void *p
)
8272 const union aligned32
*a
8273 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8275 uint32_t aval
= ( (uint32_t) a
->c
[0]
8276 | (uint32_t) a
->c
[1] << 8
8277 | (uint32_t) a
->c
[2] << 16
8278 | (uint32_t) a
->c
[3] << 24);
8283 ext32b_r_offset (const void *p
)
8290 const union aligned32
*a
8291 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8293 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8294 | (uint32_t) a
->c
[1] << 16
8295 | (uint32_t) a
->c
[2] << 8
8296 | (uint32_t) a
->c
[3]);
8300 #ifdef BFD_HOST_64_BIT
8302 ext64l_r_offset (const void *p
)
8309 const union aligned64
*a
8310 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8312 uint64_t aval
= ( (uint64_t) a
->c
[0]
8313 | (uint64_t) a
->c
[1] << 8
8314 | (uint64_t) a
->c
[2] << 16
8315 | (uint64_t) a
->c
[3] << 24
8316 | (uint64_t) a
->c
[4] << 32
8317 | (uint64_t) a
->c
[5] << 40
8318 | (uint64_t) a
->c
[6] << 48
8319 | (uint64_t) a
->c
[7] << 56);
8324 ext64b_r_offset (const void *p
)
8331 const union aligned64
*a
8332 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8334 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8335 | (uint64_t) a
->c
[1] << 48
8336 | (uint64_t) a
->c
[2] << 40
8337 | (uint64_t) a
->c
[3] << 32
8338 | (uint64_t) a
->c
[4] << 24
8339 | (uint64_t) a
->c
[5] << 16
8340 | (uint64_t) a
->c
[6] << 8
8341 | (uint64_t) a
->c
[7]);
8346 /* When performing a relocatable link, the input relocations are
8347 preserved. But, if they reference global symbols, the indices
8348 referenced must be updated. Update all the relocations found in
8352 elf_link_adjust_relocs (bfd
*abfd
,
8354 struct bfd_elf_section_reloc_data
*reldata
,
8358 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8360 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8361 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8362 bfd_vma r_type_mask
;
8364 unsigned int count
= reldata
->count
;
8365 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8367 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8369 swap_in
= bed
->s
->swap_reloc_in
;
8370 swap_out
= bed
->s
->swap_reloc_out
;
8372 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8374 swap_in
= bed
->s
->swap_reloca_in
;
8375 swap_out
= bed
->s
->swap_reloca_out
;
8380 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8383 if (bed
->s
->arch_size
== 32)
8390 r_type_mask
= 0xffffffff;
8394 erela
= reldata
->hdr
->contents
;
8395 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8397 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8400 if (*rel_hash
== NULL
)
8403 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8405 (*swap_in
) (abfd
, erela
, irela
);
8406 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8407 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8408 | (irela
[j
].r_info
& r_type_mask
));
8409 (*swap_out
) (abfd
, irela
, erela
);
8412 if (bed
->elf_backend_update_relocs
)
8413 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8415 if (sort
&& count
!= 0)
8417 bfd_vma (*ext_r_off
) (const void *);
8420 bfd_byte
*base
, *end
, *p
, *loc
;
8421 bfd_byte
*buf
= NULL
;
8423 if (bed
->s
->arch_size
== 32)
8425 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8426 ext_r_off
= ext32l_r_offset
;
8427 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8428 ext_r_off
= ext32b_r_offset
;
8434 #ifdef BFD_HOST_64_BIT
8435 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8436 ext_r_off
= ext64l_r_offset
;
8437 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8438 ext_r_off
= ext64b_r_offset
;
8444 /* Must use a stable sort here. A modified insertion sort,
8445 since the relocs are mostly sorted already. */
8446 elt_size
= reldata
->hdr
->sh_entsize
;
8447 base
= reldata
->hdr
->contents
;
8448 end
= base
+ count
* elt_size
;
8449 if (elt_size
> sizeof (Elf64_External_Rela
))
8452 /* Ensure the first element is lowest. This acts as a sentinel,
8453 speeding the main loop below. */
8454 r_off
= (*ext_r_off
) (base
);
8455 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8457 bfd_vma r_off2
= (*ext_r_off
) (p
);
8466 /* Don't just swap *base and *loc as that changes the order
8467 of the original base[0] and base[1] if they happen to
8468 have the same r_offset. */
8469 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8470 memcpy (onebuf
, loc
, elt_size
);
8471 memmove (base
+ elt_size
, base
, loc
- base
);
8472 memcpy (base
, onebuf
, elt_size
);
8475 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8477 /* base to p is sorted, *p is next to insert. */
8478 r_off
= (*ext_r_off
) (p
);
8479 /* Search the sorted region for location to insert. */
8481 while (r_off
< (*ext_r_off
) (loc
))
8486 /* Chances are there is a run of relocs to insert here,
8487 from one of more input files. Files are not always
8488 linked in order due to the way elf_link_input_bfd is
8489 called. See pr17666. */
8490 size_t sortlen
= p
- loc
;
8491 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8492 size_t runlen
= elt_size
;
8493 size_t buf_size
= 96 * 1024;
8494 while (p
+ runlen
< end
8495 && (sortlen
<= buf_size
8496 || runlen
+ elt_size
<= buf_size
)
8497 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8501 buf
= bfd_malloc (buf_size
);
8505 if (runlen
< sortlen
)
8507 memcpy (buf
, p
, runlen
);
8508 memmove (loc
+ runlen
, loc
, sortlen
);
8509 memcpy (loc
, buf
, runlen
);
8513 memcpy (buf
, loc
, sortlen
);
8514 memmove (loc
, p
, runlen
);
8515 memcpy (loc
+ runlen
, buf
, sortlen
);
8517 p
+= runlen
- elt_size
;
8520 /* Hashes are no longer valid. */
8521 free (reldata
->hashes
);
8522 reldata
->hashes
= NULL
;
8528 struct elf_link_sort_rela
8534 enum elf_reloc_type_class type
;
8535 /* We use this as an array of size int_rels_per_ext_rel. */
8536 Elf_Internal_Rela rela
[1];
8540 elf_link_sort_cmp1 (const void *A
, const void *B
)
8542 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8543 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8544 int relativea
, relativeb
;
8546 relativea
= a
->type
== reloc_class_relative
;
8547 relativeb
= b
->type
== reloc_class_relative
;
8549 if (relativea
< relativeb
)
8551 if (relativea
> relativeb
)
8553 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8555 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8557 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8559 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8565 elf_link_sort_cmp2 (const void *A
, const void *B
)
8567 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8568 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8570 if (a
->type
< b
->type
)
8572 if (a
->type
> b
->type
)
8574 if (a
->u
.offset
< b
->u
.offset
)
8576 if (a
->u
.offset
> b
->u
.offset
)
8578 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8580 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8586 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8588 asection
*dynamic_relocs
;
8591 bfd_size_type count
, size
;
8592 size_t i
, ret
, sort_elt
, ext_size
;
8593 bfd_byte
*sort
, *s_non_relative
, *p
;
8594 struct elf_link_sort_rela
*sq
;
8595 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8596 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8597 unsigned int opb
= bfd_octets_per_byte (abfd
);
8598 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8599 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8600 struct bfd_link_order
*lo
;
8602 bfd_boolean use_rela
;
8604 /* Find a dynamic reloc section. */
8605 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8606 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8607 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8608 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8610 bfd_boolean use_rela_initialised
= FALSE
;
8612 /* This is just here to stop gcc from complaining.
8613 Its initialization checking code is not perfect. */
8616 /* Both sections are present. Examine the sizes
8617 of the indirect sections to help us choose. */
8618 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8619 if (lo
->type
== bfd_indirect_link_order
)
8621 asection
*o
= lo
->u
.indirect
.section
;
8623 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8625 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8626 /* Section size is divisible by both rel and rela sizes.
8627 It is of no help to us. */
8631 /* Section size is only divisible by rela. */
8632 if (use_rela_initialised
&& (use_rela
== FALSE
))
8634 _bfd_error_handler (_("%B: Unable to sort relocs - "
8635 "they are in more than one size"),
8637 bfd_set_error (bfd_error_invalid_operation
);
8643 use_rela_initialised
= TRUE
;
8647 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8649 /* Section size is only divisible by rel. */
8650 if (use_rela_initialised
&& (use_rela
== TRUE
))
8652 _bfd_error_handler (_("%B: Unable to sort relocs - "
8653 "they are in more than one size"),
8655 bfd_set_error (bfd_error_invalid_operation
);
8661 use_rela_initialised
= TRUE
;
8666 /* The section size is not divisible by either -
8667 something is wrong. */
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are of an unknown size"), abfd
);
8670 bfd_set_error (bfd_error_invalid_operation
);
8675 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8676 if (lo
->type
== bfd_indirect_link_order
)
8678 asection
*o
= lo
->u
.indirect
.section
;
8680 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8682 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8683 /* Section size is divisible by both rel and rela sizes.
8684 It is of no help to us. */
8688 /* Section size is only divisible by rela. */
8689 if (use_rela_initialised
&& (use_rela
== FALSE
))
8691 _bfd_error_handler (_("%B: Unable to sort relocs - "
8692 "they are in more than one size"),
8694 bfd_set_error (bfd_error_invalid_operation
);
8700 use_rela_initialised
= TRUE
;
8704 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8706 /* Section size is only divisible by rel. */
8707 if (use_rela_initialised
&& (use_rela
== TRUE
))
8709 _bfd_error_handler (_("%B: Unable to sort relocs - "
8710 "they are in more than one size"),
8712 bfd_set_error (bfd_error_invalid_operation
);
8718 use_rela_initialised
= TRUE
;
8723 /* The section size is not divisible by either -
8724 something is wrong. */
8725 _bfd_error_handler (_("%B: Unable to sort relocs - "
8726 "they are of an unknown size"), abfd
);
8727 bfd_set_error (bfd_error_invalid_operation
);
8732 if (! use_rela_initialised
)
8736 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8738 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8745 dynamic_relocs
= rela_dyn
;
8746 ext_size
= bed
->s
->sizeof_rela
;
8747 swap_in
= bed
->s
->swap_reloca_in
;
8748 swap_out
= bed
->s
->swap_reloca_out
;
8752 dynamic_relocs
= rel_dyn
;
8753 ext_size
= bed
->s
->sizeof_rel
;
8754 swap_in
= bed
->s
->swap_reloc_in
;
8755 swap_out
= bed
->s
->swap_reloc_out
;
8759 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8760 if (lo
->type
== bfd_indirect_link_order
)
8761 size
+= lo
->u
.indirect
.section
->size
;
8763 if (size
!= dynamic_relocs
->size
)
8766 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8767 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8769 count
= dynamic_relocs
->size
/ ext_size
;
8772 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8776 (*info
->callbacks
->warning
)
8777 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8781 if (bed
->s
->arch_size
== 32)
8782 r_sym_mask
= ~(bfd_vma
) 0xff;
8784 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8786 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8787 if (lo
->type
== bfd_indirect_link_order
)
8789 bfd_byte
*erel
, *erelend
;
8790 asection
*o
= lo
->u
.indirect
.section
;
8792 if (o
->contents
== NULL
&& o
->size
!= 0)
8794 /* This is a reloc section that is being handled as a normal
8795 section. See bfd_section_from_shdr. We can't combine
8796 relocs in this case. */
8801 erelend
= o
->contents
+ o
->size
;
8802 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8804 while (erel
< erelend
)
8806 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8808 (*swap_in
) (abfd
, erel
, s
->rela
);
8809 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8810 s
->u
.sym_mask
= r_sym_mask
;
8816 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8818 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8820 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8821 if (s
->type
!= reloc_class_relative
)
8827 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8828 for (; i
< count
; i
++, p
+= sort_elt
)
8830 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8831 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8833 sp
->u
.offset
= sq
->rela
->r_offset
;
8836 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8838 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8839 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8841 /* We have plt relocs in .rela.dyn. */
8842 sq
= (struct elf_link_sort_rela
*) sort
;
8843 for (i
= 0; i
< count
; i
++)
8844 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8846 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8848 struct bfd_link_order
**plo
;
8849 /* Put srelplt link_order last. This is so the output_offset
8850 set in the next loop is correct for DT_JMPREL. */
8851 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8852 if ((*plo
)->type
== bfd_indirect_link_order
8853 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8859 plo
= &(*plo
)->next
;
8862 dynamic_relocs
->map_tail
.link_order
= lo
;
8867 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8868 if (lo
->type
== bfd_indirect_link_order
)
8870 bfd_byte
*erel
, *erelend
;
8871 asection
*o
= lo
->u
.indirect
.section
;
8874 erelend
= o
->contents
+ o
->size
;
8875 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8876 while (erel
< erelend
)
8878 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8879 (*swap_out
) (abfd
, s
->rela
, erel
);
8886 *psec
= dynamic_relocs
;
8890 /* Add a symbol to the output symbol string table. */
8893 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8895 Elf_Internal_Sym
*elfsym
,
8896 asection
*input_sec
,
8897 struct elf_link_hash_entry
*h
)
8899 int (*output_symbol_hook
)
8900 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8901 struct elf_link_hash_entry
*);
8902 struct elf_link_hash_table
*hash_table
;
8903 const struct elf_backend_data
*bed
;
8904 bfd_size_type strtabsize
;
8906 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8908 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8909 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8910 if (output_symbol_hook
!= NULL
)
8912 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8919 || (input_sec
->flags
& SEC_EXCLUDE
))
8920 elfsym
->st_name
= (unsigned long) -1;
8923 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8924 to get the final offset for st_name. */
8926 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8928 if (elfsym
->st_name
== (unsigned long) -1)
8932 hash_table
= elf_hash_table (flinfo
->info
);
8933 strtabsize
= hash_table
->strtabsize
;
8934 if (strtabsize
<= hash_table
->strtabcount
)
8936 strtabsize
+= strtabsize
;
8937 hash_table
->strtabsize
= strtabsize
;
8938 strtabsize
*= sizeof (*hash_table
->strtab
);
8940 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8942 if (hash_table
->strtab
== NULL
)
8945 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8946 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8947 = hash_table
->strtabcount
;
8948 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8949 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8951 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8952 hash_table
->strtabcount
+= 1;
8957 /* Swap symbols out to the symbol table and flush the output symbols to
8961 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8963 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8966 const struct elf_backend_data
*bed
;
8968 Elf_Internal_Shdr
*hdr
;
8972 if (!hash_table
->strtabcount
)
8975 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8977 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8979 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8980 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8984 if (flinfo
->symshndxbuf
)
8986 amt
= sizeof (Elf_External_Sym_Shndx
);
8987 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
8988 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
8989 if (flinfo
->symshndxbuf
== NULL
)
8996 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
8998 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
8999 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9000 elfsym
->sym
.st_name
= 0;
9003 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9004 elfsym
->sym
.st_name
);
9005 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9006 ((bfd_byte
*) symbuf
9007 + (elfsym
->dest_index
9008 * bed
->s
->sizeof_sym
)),
9009 (flinfo
->symshndxbuf
9010 + elfsym
->destshndx_index
));
9013 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9014 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9015 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9016 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9017 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9019 hdr
->sh_size
+= amt
;
9027 free (hash_table
->strtab
);
9028 hash_table
->strtab
= NULL
;
9033 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9036 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9038 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9039 && sym
->st_shndx
< SHN_LORESERVE
)
9041 /* The gABI doesn't support dynamic symbols in output sections
9044 /* xgettext:c-format */
9045 (_("%B: Too many sections: %d (>= %d)"),
9046 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9047 bfd_set_error (bfd_error_nonrepresentable_section
);
9053 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9054 allowing an unsatisfied unversioned symbol in the DSO to match a
9055 versioned symbol that would normally require an explicit version.
9056 We also handle the case that a DSO references a hidden symbol
9057 which may be satisfied by a versioned symbol in another DSO. */
9060 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9061 const struct elf_backend_data
*bed
,
9062 struct elf_link_hash_entry
*h
)
9065 struct elf_link_loaded_list
*loaded
;
9067 if (!is_elf_hash_table (info
->hash
))
9070 /* Check indirect symbol. */
9071 while (h
->root
.type
== bfd_link_hash_indirect
)
9072 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9074 switch (h
->root
.type
)
9080 case bfd_link_hash_undefined
:
9081 case bfd_link_hash_undefweak
:
9082 abfd
= h
->root
.u
.undef
.abfd
;
9084 || (abfd
->flags
& DYNAMIC
) == 0
9085 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9089 case bfd_link_hash_defined
:
9090 case bfd_link_hash_defweak
:
9091 abfd
= h
->root
.u
.def
.section
->owner
;
9094 case bfd_link_hash_common
:
9095 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9098 BFD_ASSERT (abfd
!= NULL
);
9100 for (loaded
= elf_hash_table (info
)->loaded
;
9102 loaded
= loaded
->next
)
9105 Elf_Internal_Shdr
*hdr
;
9109 Elf_Internal_Shdr
*versymhdr
;
9110 Elf_Internal_Sym
*isym
;
9111 Elf_Internal_Sym
*isymend
;
9112 Elf_Internal_Sym
*isymbuf
;
9113 Elf_External_Versym
*ever
;
9114 Elf_External_Versym
*extversym
;
9116 input
= loaded
->abfd
;
9118 /* We check each DSO for a possible hidden versioned definition. */
9120 || (input
->flags
& DYNAMIC
) == 0
9121 || elf_dynversym (input
) == 0)
9124 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9126 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9127 if (elf_bad_symtab (input
))
9129 extsymcount
= symcount
;
9134 extsymcount
= symcount
- hdr
->sh_info
;
9135 extsymoff
= hdr
->sh_info
;
9138 if (extsymcount
== 0)
9141 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9143 if (isymbuf
== NULL
)
9146 /* Read in any version definitions. */
9147 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9148 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9149 if (extversym
== NULL
)
9152 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9153 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9154 != versymhdr
->sh_size
))
9162 ever
= extversym
+ extsymoff
;
9163 isymend
= isymbuf
+ extsymcount
;
9164 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9167 Elf_Internal_Versym iver
;
9168 unsigned short version_index
;
9170 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9171 || isym
->st_shndx
== SHN_UNDEF
)
9174 name
= bfd_elf_string_from_elf_section (input
,
9177 if (strcmp (name
, h
->root
.root
.string
) != 0)
9180 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9182 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9184 && h
->forced_local
))
9186 /* If we have a non-hidden versioned sym, then it should
9187 have provided a definition for the undefined sym unless
9188 it is defined in a non-shared object and forced local.
9193 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9194 if (version_index
== 1 || version_index
== 2)
9196 /* This is the base or first version. We can use it. */
9210 /* Convert ELF common symbol TYPE. */
9213 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9215 /* Commom symbol can only appear in relocatable link. */
9216 if (!bfd_link_relocatable (info
))
9218 switch (info
->elf_stt_common
)
9222 case elf_stt_common
:
9225 case no_elf_stt_common
:
9232 /* Add an external symbol to the symbol table. This is called from
9233 the hash table traversal routine. When generating a shared object,
9234 we go through the symbol table twice. The first time we output
9235 anything that might have been forced to local scope in a version
9236 script. The second time we output the symbols that are still
9240 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9242 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9243 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9244 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9246 Elf_Internal_Sym sym
;
9247 asection
*input_sec
;
9248 const struct elf_backend_data
*bed
;
9252 /* A symbol is bound locally if it is forced local or it is locally
9253 defined, hidden versioned, not referenced by shared library and
9254 not exported when linking executable. */
9255 bfd_boolean local_bind
= (h
->forced_local
9256 || (bfd_link_executable (flinfo
->info
)
9257 && !flinfo
->info
->export_dynamic
9261 && h
->versioned
== versioned_hidden
));
9263 if (h
->root
.type
== bfd_link_hash_warning
)
9265 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9266 if (h
->root
.type
== bfd_link_hash_new
)
9270 /* Decide whether to output this symbol in this pass. */
9271 if (eoinfo
->localsyms
)
9282 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9284 if (h
->root
.type
== bfd_link_hash_undefined
)
9286 /* If we have an undefined symbol reference here then it must have
9287 come from a shared library that is being linked in. (Undefined
9288 references in regular files have already been handled unless
9289 they are in unreferenced sections which are removed by garbage
9291 bfd_boolean ignore_undef
= FALSE
;
9293 /* Some symbols may be special in that the fact that they're
9294 undefined can be safely ignored - let backend determine that. */
9295 if (bed
->elf_backend_ignore_undef_symbol
)
9296 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9298 /* If we are reporting errors for this situation then do so now. */
9301 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9302 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9303 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9304 (*flinfo
->info
->callbacks
->undefined_symbol
)
9305 (flinfo
->info
, h
->root
.root
.string
,
9306 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9308 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9310 /* Strip a global symbol defined in a discarded section. */
9315 /* We should also warn if a forced local symbol is referenced from
9316 shared libraries. */
9317 if (bfd_link_executable (flinfo
->info
)
9322 && h
->ref_dynamic_nonweak
9323 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9327 struct elf_link_hash_entry
*hi
= h
;
9329 /* Check indirect symbol. */
9330 while (hi
->root
.type
== bfd_link_hash_indirect
)
9331 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9333 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9334 /* xgettext:c-format */
9335 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9336 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9337 /* xgettext:c-format */
9338 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9340 /* xgettext:c-format */
9341 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9342 def_bfd
= flinfo
->output_bfd
;
9343 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9344 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9345 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9346 h
->root
.root
.string
);
9347 bfd_set_error (bfd_error_bad_value
);
9348 eoinfo
->failed
= TRUE
;
9352 /* We don't want to output symbols that have never been mentioned by
9353 a regular file, or that we have been told to strip. However, if
9354 h->indx is set to -2, the symbol is used by a reloc and we must
9359 else if ((h
->def_dynamic
9361 || h
->root
.type
== bfd_link_hash_new
)
9365 else if (flinfo
->info
->strip
== strip_all
)
9367 else if (flinfo
->info
->strip
== strip_some
9368 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9369 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9371 else if ((h
->root
.type
== bfd_link_hash_defined
9372 || h
->root
.type
== bfd_link_hash_defweak
)
9373 && ((flinfo
->info
->strip_discarded
9374 && discarded_section (h
->root
.u
.def
.section
))
9375 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9376 && h
->root
.u
.def
.section
->owner
!= NULL
9377 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9379 else if ((h
->root
.type
== bfd_link_hash_undefined
9380 || h
->root
.type
== bfd_link_hash_undefweak
)
9381 && h
->root
.u
.undef
.abfd
!= NULL
9382 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9387 /* If we're stripping it, and it's not a dynamic symbol, there's
9388 nothing else to do. However, if it is a forced local symbol or
9389 an ifunc symbol we need to give the backend finish_dynamic_symbol
9390 function a chance to make it dynamic. */
9393 && type
!= STT_GNU_IFUNC
9394 && !h
->forced_local
)
9398 sym
.st_size
= h
->size
;
9399 sym
.st_other
= h
->other
;
9400 switch (h
->root
.type
)
9403 case bfd_link_hash_new
:
9404 case bfd_link_hash_warning
:
9408 case bfd_link_hash_undefined
:
9409 case bfd_link_hash_undefweak
:
9410 input_sec
= bfd_und_section_ptr
;
9411 sym
.st_shndx
= SHN_UNDEF
;
9414 case bfd_link_hash_defined
:
9415 case bfd_link_hash_defweak
:
9417 input_sec
= h
->root
.u
.def
.section
;
9418 if (input_sec
->output_section
!= NULL
)
9421 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9422 input_sec
->output_section
);
9423 if (sym
.st_shndx
== SHN_BAD
)
9426 /* xgettext:c-format */
9427 (_("%B: could not find output section %A for input section %A"),
9428 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9429 bfd_set_error (bfd_error_nonrepresentable_section
);
9430 eoinfo
->failed
= TRUE
;
9434 /* ELF symbols in relocatable files are section relative,
9435 but in nonrelocatable files they are virtual
9437 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9438 if (!bfd_link_relocatable (flinfo
->info
))
9440 sym
.st_value
+= input_sec
->output_section
->vma
;
9441 if (h
->type
== STT_TLS
)
9443 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9444 if (tls_sec
!= NULL
)
9445 sym
.st_value
-= tls_sec
->vma
;
9451 BFD_ASSERT (input_sec
->owner
== NULL
9452 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9453 sym
.st_shndx
= SHN_UNDEF
;
9454 input_sec
= bfd_und_section_ptr
;
9459 case bfd_link_hash_common
:
9460 input_sec
= h
->root
.u
.c
.p
->section
;
9461 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9462 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9465 case bfd_link_hash_indirect
:
9466 /* These symbols are created by symbol versioning. They point
9467 to the decorated version of the name. For example, if the
9468 symbol foo@@GNU_1.2 is the default, which should be used when
9469 foo is used with no version, then we add an indirect symbol
9470 foo which points to foo@@GNU_1.2. We ignore these symbols,
9471 since the indirected symbol is already in the hash table. */
9475 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9476 switch (h
->root
.type
)
9478 case bfd_link_hash_common
:
9479 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9481 case bfd_link_hash_defined
:
9482 case bfd_link_hash_defweak
:
9483 if (bed
->common_definition (&sym
))
9484 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9488 case bfd_link_hash_undefined
:
9489 case bfd_link_hash_undefweak
:
9497 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9498 /* Turn off visibility on local symbol. */
9499 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9501 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9502 else if (h
->unique_global
&& h
->def_regular
)
9503 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9504 else if (h
->root
.type
== bfd_link_hash_undefweak
9505 || h
->root
.type
== bfd_link_hash_defweak
)
9506 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9508 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9509 sym
.st_target_internal
= h
->target_internal
;
9511 /* Give the processor backend a chance to tweak the symbol value,
9512 and also to finish up anything that needs to be done for this
9513 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9514 forced local syms when non-shared is due to a historical quirk.
9515 STT_GNU_IFUNC symbol must go through PLT. */
9516 if ((h
->type
== STT_GNU_IFUNC
9518 && !bfd_link_relocatable (flinfo
->info
))
9519 || ((h
->dynindx
!= -1
9521 && ((bfd_link_pic (flinfo
->info
)
9522 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9523 || h
->root
.type
!= bfd_link_hash_undefweak
))
9524 || !h
->forced_local
)
9525 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9527 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9528 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9530 eoinfo
->failed
= TRUE
;
9535 /* If we are marking the symbol as undefined, and there are no
9536 non-weak references to this symbol from a regular object, then
9537 mark the symbol as weak undefined; if there are non-weak
9538 references, mark the symbol as strong. We can't do this earlier,
9539 because it might not be marked as undefined until the
9540 finish_dynamic_symbol routine gets through with it. */
9541 if (sym
.st_shndx
== SHN_UNDEF
9543 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9544 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9547 type
= ELF_ST_TYPE (sym
.st_info
);
9549 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9550 if (type
== STT_GNU_IFUNC
)
9553 if (h
->ref_regular_nonweak
)
9554 bindtype
= STB_GLOBAL
;
9556 bindtype
= STB_WEAK
;
9557 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9560 /* If this is a symbol defined in a dynamic library, don't use the
9561 symbol size from the dynamic library. Relinking an executable
9562 against a new library may introduce gratuitous changes in the
9563 executable's symbols if we keep the size. */
9564 if (sym
.st_shndx
== SHN_UNDEF
9569 /* If a non-weak symbol with non-default visibility is not defined
9570 locally, it is a fatal error. */
9571 if (!bfd_link_relocatable (flinfo
->info
)
9572 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9573 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9574 && h
->root
.type
== bfd_link_hash_undefined
9579 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9580 /* xgettext:c-format */
9581 msg
= _("%B: protected symbol `%s' isn't defined");
9582 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9583 /* xgettext:c-format */
9584 msg
= _("%B: internal symbol `%s' isn't defined");
9586 /* xgettext:c-format */
9587 msg
= _("%B: hidden symbol `%s' isn't defined");
9588 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9589 bfd_set_error (bfd_error_bad_value
);
9590 eoinfo
->failed
= TRUE
;
9594 /* If this symbol should be put in the .dynsym section, then put it
9595 there now. We already know the symbol index. We also fill in
9596 the entry in the .hash section. */
9597 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9599 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9603 /* Since there is no version information in the dynamic string,
9604 if there is no version info in symbol version section, we will
9605 have a run-time problem if not linking executable, referenced
9606 by shared library, not locally defined, or not bound locally.
9608 if (h
->verinfo
.verdef
== NULL
9610 && (!bfd_link_executable (flinfo
->info
)
9612 || !h
->def_regular
))
9614 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9616 if (p
&& p
[1] != '\0')
9619 /* xgettext:c-format */
9620 (_("%B: No symbol version section for versioned symbol `%s'"),
9621 flinfo
->output_bfd
, h
->root
.root
.string
);
9622 eoinfo
->failed
= TRUE
;
9627 sym
.st_name
= h
->dynstr_index
;
9628 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9629 + h
->dynindx
* bed
->s
->sizeof_sym
);
9630 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9632 eoinfo
->failed
= TRUE
;
9635 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9637 if (flinfo
->hash_sec
!= NULL
)
9639 size_t hash_entry_size
;
9640 bfd_byte
*bucketpos
;
9645 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9646 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9649 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9650 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9651 + (bucket
+ 2) * hash_entry_size
);
9652 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9653 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9655 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9656 ((bfd_byte
*) flinfo
->hash_sec
->contents
9657 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9660 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9662 Elf_Internal_Versym iversym
;
9663 Elf_External_Versym
*eversym
;
9665 if (!h
->def_regular
)
9667 if (h
->verinfo
.verdef
== NULL
9668 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9669 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9670 iversym
.vs_vers
= 0;
9672 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9676 if (h
->verinfo
.vertree
== NULL
)
9677 iversym
.vs_vers
= 1;
9679 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9680 if (flinfo
->info
->create_default_symver
)
9684 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9686 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9687 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9689 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9690 eversym
+= h
->dynindx
;
9691 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9695 /* If the symbol is undefined, and we didn't output it to .dynsym,
9696 strip it from .symtab too. Obviously we can't do this for
9697 relocatable output or when needed for --emit-relocs. */
9698 else if (input_sec
== bfd_und_section_ptr
9700 && !bfd_link_relocatable (flinfo
->info
))
9702 /* Also strip others that we couldn't earlier due to dynamic symbol
9706 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9709 /* Output a FILE symbol so that following locals are not associated
9710 with the wrong input file. We need one for forced local symbols
9711 if we've seen more than one FILE symbol or when we have exactly
9712 one FILE symbol but global symbols are present in a file other
9713 than the one with the FILE symbol. We also need one if linker
9714 defined symbols are present. In practice these conditions are
9715 always met, so just emit the FILE symbol unconditionally. */
9716 if (eoinfo
->localsyms
9717 && !eoinfo
->file_sym_done
9718 && eoinfo
->flinfo
->filesym_count
!= 0)
9720 Elf_Internal_Sym fsym
;
9722 memset (&fsym
, 0, sizeof (fsym
));
9723 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9724 fsym
.st_shndx
= SHN_ABS
;
9725 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9726 bfd_und_section_ptr
, NULL
))
9729 eoinfo
->file_sym_done
= TRUE
;
9732 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9733 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9737 eoinfo
->failed
= TRUE
;
9742 else if (h
->indx
== -2)
9748 /* Return TRUE if special handling is done for relocs in SEC against
9749 symbols defined in discarded sections. */
9752 elf_section_ignore_discarded_relocs (asection
*sec
)
9754 const struct elf_backend_data
*bed
;
9756 switch (sec
->sec_info_type
)
9758 case SEC_INFO_TYPE_STABS
:
9759 case SEC_INFO_TYPE_EH_FRAME
:
9760 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9766 bed
= get_elf_backend_data (sec
->owner
);
9767 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9768 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9774 /* Return a mask saying how ld should treat relocations in SEC against
9775 symbols defined in discarded sections. If this function returns
9776 COMPLAIN set, ld will issue a warning message. If this function
9777 returns PRETEND set, and the discarded section was link-once and the
9778 same size as the kept link-once section, ld will pretend that the
9779 symbol was actually defined in the kept section. Otherwise ld will
9780 zero the reloc (at least that is the intent, but some cooperation by
9781 the target dependent code is needed, particularly for REL targets). */
9784 _bfd_elf_default_action_discarded (asection
*sec
)
9786 if (sec
->flags
& SEC_DEBUGGING
)
9789 if (strcmp (".eh_frame", sec
->name
) == 0)
9792 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9795 return COMPLAIN
| PRETEND
;
9798 /* Find a match between a section and a member of a section group. */
9801 match_group_member (asection
*sec
, asection
*group
,
9802 struct bfd_link_info
*info
)
9804 asection
*first
= elf_next_in_group (group
);
9805 asection
*s
= first
;
9809 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9812 s
= elf_next_in_group (s
);
9820 /* Check if the kept section of a discarded section SEC can be used
9821 to replace it. Return the replacement if it is OK. Otherwise return
9825 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9829 kept
= sec
->kept_section
;
9832 if ((kept
->flags
& SEC_GROUP
) != 0)
9833 kept
= match_group_member (sec
, kept
, info
);
9835 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9836 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9838 sec
->kept_section
= kept
;
9843 /* Link an input file into the linker output file. This function
9844 handles all the sections and relocations of the input file at once.
9845 This is so that we only have to read the local symbols once, and
9846 don't have to keep them in memory. */
9849 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9851 int (*relocate_section
)
9852 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9853 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9855 Elf_Internal_Shdr
*symtab_hdr
;
9858 Elf_Internal_Sym
*isymbuf
;
9859 Elf_Internal_Sym
*isym
;
9860 Elf_Internal_Sym
*isymend
;
9862 asection
**ppsection
;
9864 const struct elf_backend_data
*bed
;
9865 struct elf_link_hash_entry
**sym_hashes
;
9866 bfd_size_type address_size
;
9867 bfd_vma r_type_mask
;
9869 bfd_boolean have_file_sym
= FALSE
;
9871 output_bfd
= flinfo
->output_bfd
;
9872 bed
= get_elf_backend_data (output_bfd
);
9873 relocate_section
= bed
->elf_backend_relocate_section
;
9875 /* If this is a dynamic object, we don't want to do anything here:
9876 we don't want the local symbols, and we don't want the section
9878 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9881 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9882 if (elf_bad_symtab (input_bfd
))
9884 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9889 locsymcount
= symtab_hdr
->sh_info
;
9890 extsymoff
= symtab_hdr
->sh_info
;
9893 /* Read the local symbols. */
9894 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9895 if (isymbuf
== NULL
&& locsymcount
!= 0)
9897 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9898 flinfo
->internal_syms
,
9899 flinfo
->external_syms
,
9900 flinfo
->locsym_shndx
);
9901 if (isymbuf
== NULL
)
9905 /* Find local symbol sections and adjust values of symbols in
9906 SEC_MERGE sections. Write out those local symbols we know are
9907 going into the output file. */
9908 isymend
= isymbuf
+ locsymcount
;
9909 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9911 isym
++, pindex
++, ppsection
++)
9915 Elf_Internal_Sym osym
;
9921 if (elf_bad_symtab (input_bfd
))
9923 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9930 if (isym
->st_shndx
== SHN_UNDEF
)
9931 isec
= bfd_und_section_ptr
;
9932 else if (isym
->st_shndx
== SHN_ABS
)
9933 isec
= bfd_abs_section_ptr
;
9934 else if (isym
->st_shndx
== SHN_COMMON
)
9935 isec
= bfd_com_section_ptr
;
9938 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9941 /* Don't attempt to output symbols with st_shnx in the
9942 reserved range other than SHN_ABS and SHN_COMMON. */
9946 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9947 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9949 _bfd_merged_section_offset (output_bfd
, &isec
,
9950 elf_section_data (isec
)->sec_info
,
9956 /* Don't output the first, undefined, symbol. In fact, don't
9957 output any undefined local symbol. */
9958 if (isec
== bfd_und_section_ptr
)
9961 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9963 /* We never output section symbols. Instead, we use the
9964 section symbol of the corresponding section in the output
9969 /* If we are stripping all symbols, we don't want to output this
9971 if (flinfo
->info
->strip
== strip_all
)
9974 /* If we are discarding all local symbols, we don't want to
9975 output this one. If we are generating a relocatable output
9976 file, then some of the local symbols may be required by
9977 relocs; we output them below as we discover that they are
9979 if (flinfo
->info
->discard
== discard_all
)
9982 /* If this symbol is defined in a section which we are
9983 discarding, we don't need to keep it. */
9984 if (isym
->st_shndx
!= SHN_UNDEF
9985 && isym
->st_shndx
< SHN_LORESERVE
9986 && bfd_section_removed_from_list (output_bfd
,
9987 isec
->output_section
))
9990 /* Get the name of the symbol. */
9991 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9996 /* See if we are discarding symbols with this name. */
9997 if ((flinfo
->info
->strip
== strip_some
9998 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10000 || (((flinfo
->info
->discard
== discard_sec_merge
10001 && (isec
->flags
& SEC_MERGE
)
10002 && !bfd_link_relocatable (flinfo
->info
))
10003 || flinfo
->info
->discard
== discard_l
)
10004 && bfd_is_local_label_name (input_bfd
, name
)))
10007 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10009 if (input_bfd
->lto_output
)
10010 /* -flto puts a temp file name here. This means builds
10011 are not reproducible. Discard the symbol. */
10013 have_file_sym
= TRUE
;
10014 flinfo
->filesym_count
+= 1;
10016 if (!have_file_sym
)
10018 /* In the absence of debug info, bfd_find_nearest_line uses
10019 FILE symbols to determine the source file for local
10020 function symbols. Provide a FILE symbol here if input
10021 files lack such, so that their symbols won't be
10022 associated with a previous input file. It's not the
10023 source file, but the best we can do. */
10024 have_file_sym
= TRUE
;
10025 flinfo
->filesym_count
+= 1;
10026 memset (&osym
, 0, sizeof (osym
));
10027 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10028 osym
.st_shndx
= SHN_ABS
;
10029 if (!elf_link_output_symstrtab (flinfo
,
10030 (input_bfd
->lto_output
? NULL
10031 : input_bfd
->filename
),
10032 &osym
, bfd_abs_section_ptr
,
10039 /* Adjust the section index for the output file. */
10040 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10041 isec
->output_section
);
10042 if (osym
.st_shndx
== SHN_BAD
)
10045 /* ELF symbols in relocatable files are section relative, but
10046 in executable files they are virtual addresses. Note that
10047 this code assumes that all ELF sections have an associated
10048 BFD section with a reasonable value for output_offset; below
10049 we assume that they also have a reasonable value for
10050 output_section. Any special sections must be set up to meet
10051 these requirements. */
10052 osym
.st_value
+= isec
->output_offset
;
10053 if (!bfd_link_relocatable (flinfo
->info
))
10055 osym
.st_value
+= isec
->output_section
->vma
;
10056 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10058 /* STT_TLS symbols are relative to PT_TLS segment base. */
10059 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10060 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10064 indx
= bfd_get_symcount (output_bfd
);
10065 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10072 if (bed
->s
->arch_size
== 32)
10074 r_type_mask
= 0xff;
10080 r_type_mask
= 0xffffffff;
10085 /* Relocate the contents of each section. */
10086 sym_hashes
= elf_sym_hashes (input_bfd
);
10087 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10089 bfd_byte
*contents
;
10091 if (! o
->linker_mark
)
10093 /* This section was omitted from the link. */
10097 if (bfd_link_relocatable (flinfo
->info
)
10098 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10100 /* Deal with the group signature symbol. */
10101 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10102 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10103 asection
*osec
= o
->output_section
;
10105 if (symndx
>= locsymcount
10106 || (elf_bad_symtab (input_bfd
)
10107 && flinfo
->sections
[symndx
] == NULL
))
10109 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10110 while (h
->root
.type
== bfd_link_hash_indirect
10111 || h
->root
.type
== bfd_link_hash_warning
)
10112 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10113 /* Arrange for symbol to be output. */
10115 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10117 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10119 /* We'll use the output section target_index. */
10120 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10121 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10125 if (flinfo
->indices
[symndx
] == -1)
10127 /* Otherwise output the local symbol now. */
10128 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10129 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10134 name
= bfd_elf_string_from_elf_section (input_bfd
,
10135 symtab_hdr
->sh_link
,
10140 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10142 if (sym
.st_shndx
== SHN_BAD
)
10145 sym
.st_value
+= o
->output_offset
;
10147 indx
= bfd_get_symcount (output_bfd
);
10148 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10153 flinfo
->indices
[symndx
] = indx
;
10157 elf_section_data (osec
)->this_hdr
.sh_info
10158 = flinfo
->indices
[symndx
];
10162 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10163 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10166 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10168 /* Section was created by _bfd_elf_link_create_dynamic_sections
10173 /* Get the contents of the section. They have been cached by a
10174 relaxation routine. Note that o is a section in an input
10175 file, so the contents field will not have been set by any of
10176 the routines which work on output files. */
10177 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10179 contents
= elf_section_data (o
)->this_hdr
.contents
;
10180 if (bed
->caches_rawsize
10182 && o
->rawsize
< o
->size
)
10184 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10185 contents
= flinfo
->contents
;
10190 contents
= flinfo
->contents
;
10191 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10195 if ((o
->flags
& SEC_RELOC
) != 0)
10197 Elf_Internal_Rela
*internal_relocs
;
10198 Elf_Internal_Rela
*rel
, *relend
;
10199 int action_discarded
;
10202 /* Get the swapped relocs. */
10204 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10205 flinfo
->internal_relocs
, FALSE
);
10206 if (internal_relocs
== NULL
10207 && o
->reloc_count
> 0)
10210 /* We need to reverse-copy input .ctors/.dtors sections if
10211 they are placed in .init_array/.finit_array for output. */
10212 if (o
->size
> address_size
10213 && ((strncmp (o
->name
, ".ctors", 6) == 0
10214 && strcmp (o
->output_section
->name
,
10215 ".init_array") == 0)
10216 || (strncmp (o
->name
, ".dtors", 6) == 0
10217 && strcmp (o
->output_section
->name
,
10218 ".fini_array") == 0))
10219 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10221 if (o
->size
!= o
->reloc_count
* address_size
)
10224 /* xgettext:c-format */
10225 (_("error: %B: size of section %A is not "
10226 "multiple of address size"),
10228 bfd_set_error (bfd_error_on_input
);
10231 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10234 action_discarded
= -1;
10235 if (!elf_section_ignore_discarded_relocs (o
))
10236 action_discarded
= (*bed
->action_discarded
) (o
);
10238 /* Run through the relocs evaluating complex reloc symbols and
10239 looking for relocs against symbols from discarded sections
10240 or section symbols from removed link-once sections.
10241 Complain about relocs against discarded sections. Zero
10242 relocs against removed link-once sections. */
10244 rel
= internal_relocs
;
10245 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10246 for ( ; rel
< relend
; rel
++)
10248 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10249 unsigned int s_type
;
10250 asection
**ps
, *sec
;
10251 struct elf_link_hash_entry
*h
= NULL
;
10252 const char *sym_name
;
10254 if (r_symndx
== STN_UNDEF
)
10257 if (r_symndx
>= locsymcount
10258 || (elf_bad_symtab (input_bfd
)
10259 && flinfo
->sections
[r_symndx
] == NULL
))
10261 h
= sym_hashes
[r_symndx
- extsymoff
];
10263 /* Badly formatted input files can contain relocs that
10264 reference non-existant symbols. Check here so that
10265 we do not seg fault. */
10270 sprintf_vma (buffer
, rel
->r_info
);
10272 /* xgettext:c-format */
10273 (_("error: %B contains a reloc (0x%s) for section %A "
10274 "that references a non-existent global symbol"),
10275 input_bfd
, o
, buffer
);
10276 bfd_set_error (bfd_error_bad_value
);
10280 while (h
->root
.type
== bfd_link_hash_indirect
10281 || h
->root
.type
== bfd_link_hash_warning
)
10282 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10286 /* If a plugin symbol is referenced from a non-IR file,
10287 mark the symbol as undefined. Note that the
10288 linker may attach linker created dynamic sections
10289 to the plugin bfd. Symbols defined in linker
10290 created sections are not plugin symbols. */
10291 if (h
->root
.non_ir_ref
10292 && (h
->root
.type
== bfd_link_hash_defined
10293 || h
->root
.type
== bfd_link_hash_defweak
)
10294 && (h
->root
.u
.def
.section
->flags
10295 & SEC_LINKER_CREATED
) == 0
10296 && h
->root
.u
.def
.section
->owner
!= NULL
10297 && (h
->root
.u
.def
.section
->owner
->flags
10298 & BFD_PLUGIN
) != 0)
10300 h
->root
.type
= bfd_link_hash_undefined
;
10301 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10305 if (h
->root
.type
== bfd_link_hash_defined
10306 || h
->root
.type
== bfd_link_hash_defweak
)
10307 ps
= &h
->root
.u
.def
.section
;
10309 sym_name
= h
->root
.root
.string
;
10313 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10315 s_type
= ELF_ST_TYPE (sym
->st_info
);
10316 ps
= &flinfo
->sections
[r_symndx
];
10317 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10321 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10322 && !bfd_link_relocatable (flinfo
->info
))
10325 bfd_vma dot
= (rel
->r_offset
10326 + o
->output_offset
+ o
->output_section
->vma
);
10328 printf ("Encountered a complex symbol!");
10329 printf (" (input_bfd %s, section %s, reloc %ld\n",
10330 input_bfd
->filename
, o
->name
,
10331 (long) (rel
- internal_relocs
));
10332 printf (" symbol: idx %8.8lx, name %s\n",
10333 r_symndx
, sym_name
);
10334 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10335 (unsigned long) rel
->r_info
,
10336 (unsigned long) rel
->r_offset
);
10338 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10339 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10342 /* Symbol evaluated OK. Update to absolute value. */
10343 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10348 if (action_discarded
!= -1 && ps
!= NULL
)
10350 /* Complain if the definition comes from a
10351 discarded section. */
10352 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10354 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10355 if (action_discarded
& COMPLAIN
)
10356 (*flinfo
->info
->callbacks
->einfo
)
10357 /* xgettext:c-format */
10358 (_("%X`%s' referenced in section `%A' of %B: "
10359 "defined in discarded section `%A' of %B\n"),
10360 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10362 /* Try to do the best we can to support buggy old
10363 versions of gcc. Pretend that the symbol is
10364 really defined in the kept linkonce section.
10365 FIXME: This is quite broken. Modifying the
10366 symbol here means we will be changing all later
10367 uses of the symbol, not just in this section. */
10368 if (action_discarded
& PRETEND
)
10372 kept
= _bfd_elf_check_kept_section (sec
,
10384 /* Relocate the section by invoking a back end routine.
10386 The back end routine is responsible for adjusting the
10387 section contents as necessary, and (if using Rela relocs
10388 and generating a relocatable output file) adjusting the
10389 reloc addend as necessary.
10391 The back end routine does not have to worry about setting
10392 the reloc address or the reloc symbol index.
10394 The back end routine is given a pointer to the swapped in
10395 internal symbols, and can access the hash table entries
10396 for the external symbols via elf_sym_hashes (input_bfd).
10398 When generating relocatable output, the back end routine
10399 must handle STB_LOCAL/STT_SECTION symbols specially. The
10400 output symbol is going to be a section symbol
10401 corresponding to the output section, which will require
10402 the addend to be adjusted. */
10404 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10405 input_bfd
, o
, contents
,
10413 || bfd_link_relocatable (flinfo
->info
)
10414 || flinfo
->info
->emitrelocations
)
10416 Elf_Internal_Rela
*irela
;
10417 Elf_Internal_Rela
*irelaend
, *irelamid
;
10418 bfd_vma last_offset
;
10419 struct elf_link_hash_entry
**rel_hash
;
10420 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10421 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10422 unsigned int next_erel
;
10423 bfd_boolean rela_normal
;
10424 struct bfd_elf_section_data
*esdi
, *esdo
;
10426 esdi
= elf_section_data (o
);
10427 esdo
= elf_section_data (o
->output_section
);
10428 rela_normal
= FALSE
;
10430 /* Adjust the reloc addresses and symbol indices. */
10432 irela
= internal_relocs
;
10433 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10434 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10435 /* We start processing the REL relocs, if any. When we reach
10436 IRELAMID in the loop, we switch to the RELA relocs. */
10438 if (esdi
->rel
.hdr
!= NULL
)
10439 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10440 * bed
->s
->int_rels_per_ext_rel
);
10441 rel_hash_list
= rel_hash
;
10442 rela_hash_list
= NULL
;
10443 last_offset
= o
->output_offset
;
10444 if (!bfd_link_relocatable (flinfo
->info
))
10445 last_offset
+= o
->output_section
->vma
;
10446 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10448 unsigned long r_symndx
;
10450 Elf_Internal_Sym sym
;
10452 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10458 if (irela
== irelamid
)
10460 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10461 rela_hash_list
= rel_hash
;
10462 rela_normal
= bed
->rela_normal
;
10465 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10468 if (irela
->r_offset
>= (bfd_vma
) -2)
10470 /* This is a reloc for a deleted entry or somesuch.
10471 Turn it into an R_*_NONE reloc, at the same
10472 offset as the last reloc. elf_eh_frame.c and
10473 bfd_elf_discard_info rely on reloc offsets
10475 irela
->r_offset
= last_offset
;
10477 irela
->r_addend
= 0;
10481 irela
->r_offset
+= o
->output_offset
;
10483 /* Relocs in an executable have to be virtual addresses. */
10484 if (!bfd_link_relocatable (flinfo
->info
))
10485 irela
->r_offset
+= o
->output_section
->vma
;
10487 last_offset
= irela
->r_offset
;
10489 r_symndx
= irela
->r_info
>> r_sym_shift
;
10490 if (r_symndx
== STN_UNDEF
)
10493 if (r_symndx
>= locsymcount
10494 || (elf_bad_symtab (input_bfd
)
10495 && flinfo
->sections
[r_symndx
] == NULL
))
10497 struct elf_link_hash_entry
*rh
;
10498 unsigned long indx
;
10500 /* This is a reloc against a global symbol. We
10501 have not yet output all the local symbols, so
10502 we do not know the symbol index of any global
10503 symbol. We set the rel_hash entry for this
10504 reloc to point to the global hash table entry
10505 for this symbol. The symbol index is then
10506 set at the end of bfd_elf_final_link. */
10507 indx
= r_symndx
- extsymoff
;
10508 rh
= elf_sym_hashes (input_bfd
)[indx
];
10509 while (rh
->root
.type
== bfd_link_hash_indirect
10510 || rh
->root
.type
== bfd_link_hash_warning
)
10511 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10513 /* Setting the index to -2 tells
10514 elf_link_output_extsym that this symbol is
10515 used by a reloc. */
10516 BFD_ASSERT (rh
->indx
< 0);
10524 /* This is a reloc against a local symbol. */
10527 sym
= isymbuf
[r_symndx
];
10528 sec
= flinfo
->sections
[r_symndx
];
10529 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10531 /* I suppose the backend ought to fill in the
10532 section of any STT_SECTION symbol against a
10533 processor specific section. */
10534 r_symndx
= STN_UNDEF
;
10535 if (bfd_is_abs_section (sec
))
10537 else if (sec
== NULL
|| sec
->owner
== NULL
)
10539 bfd_set_error (bfd_error_bad_value
);
10544 asection
*osec
= sec
->output_section
;
10546 /* If we have discarded a section, the output
10547 section will be the absolute section. In
10548 case of discarded SEC_MERGE sections, use
10549 the kept section. relocate_section should
10550 have already handled discarded linkonce
10552 if (bfd_is_abs_section (osec
)
10553 && sec
->kept_section
!= NULL
10554 && sec
->kept_section
->output_section
!= NULL
)
10556 osec
= sec
->kept_section
->output_section
;
10557 irela
->r_addend
-= osec
->vma
;
10560 if (!bfd_is_abs_section (osec
))
10562 r_symndx
= osec
->target_index
;
10563 if (r_symndx
== STN_UNDEF
)
10565 irela
->r_addend
+= osec
->vma
;
10566 osec
= _bfd_nearby_section (output_bfd
, osec
,
10568 irela
->r_addend
-= osec
->vma
;
10569 r_symndx
= osec
->target_index
;
10574 /* Adjust the addend according to where the
10575 section winds up in the output section. */
10577 irela
->r_addend
+= sec
->output_offset
;
10581 if (flinfo
->indices
[r_symndx
] == -1)
10583 unsigned long shlink
;
10588 if (flinfo
->info
->strip
== strip_all
)
10590 /* You can't do ld -r -s. */
10591 bfd_set_error (bfd_error_invalid_operation
);
10595 /* This symbol was skipped earlier, but
10596 since it is needed by a reloc, we
10597 must output it now. */
10598 shlink
= symtab_hdr
->sh_link
;
10599 name
= (bfd_elf_string_from_elf_section
10600 (input_bfd
, shlink
, sym
.st_name
));
10604 osec
= sec
->output_section
;
10606 _bfd_elf_section_from_bfd_section (output_bfd
,
10608 if (sym
.st_shndx
== SHN_BAD
)
10611 sym
.st_value
+= sec
->output_offset
;
10612 if (!bfd_link_relocatable (flinfo
->info
))
10614 sym
.st_value
+= osec
->vma
;
10615 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10617 /* STT_TLS symbols are relative to PT_TLS
10619 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10620 ->tls_sec
!= NULL
);
10621 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10626 indx
= bfd_get_symcount (output_bfd
);
10627 ret
= elf_link_output_symstrtab (flinfo
, name
,
10633 flinfo
->indices
[r_symndx
] = indx
;
10638 r_symndx
= flinfo
->indices
[r_symndx
];
10641 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10642 | (irela
->r_info
& r_type_mask
));
10645 /* Swap out the relocs. */
10646 input_rel_hdr
= esdi
->rel
.hdr
;
10647 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10649 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10654 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10655 * bed
->s
->int_rels_per_ext_rel
);
10656 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10659 input_rela_hdr
= esdi
->rela
.hdr
;
10660 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10662 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10671 /* Write out the modified section contents. */
10672 if (bed
->elf_backend_write_section
10673 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10676 /* Section written out. */
10678 else switch (o
->sec_info_type
)
10680 case SEC_INFO_TYPE_STABS
:
10681 if (! (_bfd_write_section_stabs
10683 &elf_hash_table (flinfo
->info
)->stab_info
,
10684 o
, &elf_section_data (o
)->sec_info
, contents
)))
10687 case SEC_INFO_TYPE_MERGE
:
10688 if (! _bfd_write_merged_section (output_bfd
, o
,
10689 elf_section_data (o
)->sec_info
))
10692 case SEC_INFO_TYPE_EH_FRAME
:
10694 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10699 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10701 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10709 if (! (o
->flags
& SEC_EXCLUDE
))
10711 file_ptr offset
= (file_ptr
) o
->output_offset
;
10712 bfd_size_type todo
= o
->size
;
10714 offset
*= bfd_octets_per_byte (output_bfd
);
10716 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10718 /* Reverse-copy input section to output. */
10721 todo
-= address_size
;
10722 if (! bfd_set_section_contents (output_bfd
,
10730 offset
+= address_size
;
10734 else if (! bfd_set_section_contents (output_bfd
,
10748 /* Generate a reloc when linking an ELF file. This is a reloc
10749 requested by the linker, and does not come from any input file. This
10750 is used to build constructor and destructor tables when linking
10754 elf_reloc_link_order (bfd
*output_bfd
,
10755 struct bfd_link_info
*info
,
10756 asection
*output_section
,
10757 struct bfd_link_order
*link_order
)
10759 reloc_howto_type
*howto
;
10763 struct bfd_elf_section_reloc_data
*reldata
;
10764 struct elf_link_hash_entry
**rel_hash_ptr
;
10765 Elf_Internal_Shdr
*rel_hdr
;
10766 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10767 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10770 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10772 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10775 bfd_set_error (bfd_error_bad_value
);
10779 addend
= link_order
->u
.reloc
.p
->addend
;
10782 reldata
= &esdo
->rel
;
10783 else if (esdo
->rela
.hdr
)
10784 reldata
= &esdo
->rela
;
10791 /* Figure out the symbol index. */
10792 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10793 if (link_order
->type
== bfd_section_reloc_link_order
)
10795 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10796 BFD_ASSERT (indx
!= 0);
10797 *rel_hash_ptr
= NULL
;
10801 struct elf_link_hash_entry
*h
;
10803 /* Treat a reloc against a defined symbol as though it were
10804 actually against the section. */
10805 h
= ((struct elf_link_hash_entry
*)
10806 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10807 link_order
->u
.reloc
.p
->u
.name
,
10808 FALSE
, FALSE
, TRUE
));
10810 && (h
->root
.type
== bfd_link_hash_defined
10811 || h
->root
.type
== bfd_link_hash_defweak
))
10815 section
= h
->root
.u
.def
.section
;
10816 indx
= section
->output_section
->target_index
;
10817 *rel_hash_ptr
= NULL
;
10818 /* It seems that we ought to add the symbol value to the
10819 addend here, but in practice it has already been added
10820 because it was passed to constructor_callback. */
10821 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10823 else if (h
!= NULL
)
10825 /* Setting the index to -2 tells elf_link_output_extsym that
10826 this symbol is used by a reloc. */
10833 (*info
->callbacks
->unattached_reloc
)
10834 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10839 /* If this is an inplace reloc, we must write the addend into the
10841 if (howto
->partial_inplace
&& addend
!= 0)
10843 bfd_size_type size
;
10844 bfd_reloc_status_type rstat
;
10847 const char *sym_name
;
10849 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10850 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10851 if (buf
== NULL
&& size
!= 0)
10853 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10860 case bfd_reloc_outofrange
:
10863 case bfd_reloc_overflow
:
10864 if (link_order
->type
== bfd_section_reloc_link_order
)
10865 sym_name
= bfd_section_name (output_bfd
,
10866 link_order
->u
.reloc
.p
->u
.section
);
10868 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10869 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10870 howto
->name
, addend
, NULL
, NULL
,
10875 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10877 * bfd_octets_per_byte (output_bfd
),
10884 /* The address of a reloc is relative to the section in a
10885 relocatable file, and is a virtual address in an executable
10887 offset
= link_order
->offset
;
10888 if (! bfd_link_relocatable (info
))
10889 offset
+= output_section
->vma
;
10891 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10893 irel
[i
].r_offset
= offset
;
10894 irel
[i
].r_info
= 0;
10895 irel
[i
].r_addend
= 0;
10897 if (bed
->s
->arch_size
== 32)
10898 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10900 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10902 rel_hdr
= reldata
->hdr
;
10903 erel
= rel_hdr
->contents
;
10904 if (rel_hdr
->sh_type
== SHT_REL
)
10906 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10907 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10911 irel
[0].r_addend
= addend
;
10912 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10913 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10922 /* Get the output vma of the section pointed to by the sh_link field. */
10925 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10927 Elf_Internal_Shdr
**elf_shdrp
;
10931 s
= p
->u
.indirect
.section
;
10932 elf_shdrp
= elf_elfsections (s
->owner
);
10933 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10934 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10936 The Intel C compiler generates SHT_IA_64_UNWIND with
10937 SHF_LINK_ORDER. But it doesn't set the sh_link or
10938 sh_info fields. Hence we could get the situation
10939 where elfsec is 0. */
10942 const struct elf_backend_data
*bed
10943 = get_elf_backend_data (s
->owner
);
10944 if (bed
->link_order_error_handler
)
10945 bed
->link_order_error_handler
10946 /* xgettext:c-format */
10947 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10952 s
= elf_shdrp
[elfsec
]->bfd_section
;
10953 return s
->output_section
->vma
+ s
->output_offset
;
10958 /* Compare two sections based on the locations of the sections they are
10959 linked to. Used by elf_fixup_link_order. */
10962 compare_link_order (const void * a
, const void * b
)
10967 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10968 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10971 return apos
> bpos
;
10975 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10976 order as their linked sections. Returns false if this could not be done
10977 because an output section includes both ordered and unordered
10978 sections. Ideally we'd do this in the linker proper. */
10981 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10983 int seen_linkorder
;
10986 struct bfd_link_order
*p
;
10988 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10990 struct bfd_link_order
**sections
;
10991 asection
*s
, *other_sec
, *linkorder_sec
;
10995 linkorder_sec
= NULL
;
10997 seen_linkorder
= 0;
10998 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11000 if (p
->type
== bfd_indirect_link_order
)
11002 s
= p
->u
.indirect
.section
;
11004 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11005 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11006 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11007 && elfsec
< elf_numsections (sub
)
11008 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11009 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11023 if (seen_other
&& seen_linkorder
)
11025 if (other_sec
&& linkorder_sec
)
11027 /* xgettext:c-format */
11028 (_("%A has both ordered [`%A' in %B] "
11029 "and unordered [`%A' in %B] sections"),
11031 linkorder_sec
->owner
, other_sec
,
11035 (_("%A has both ordered and unordered sections"), o
);
11036 bfd_set_error (bfd_error_bad_value
);
11041 if (!seen_linkorder
)
11044 sections
= (struct bfd_link_order
**)
11045 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11046 if (sections
== NULL
)
11048 seen_linkorder
= 0;
11050 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11052 sections
[seen_linkorder
++] = p
;
11054 /* Sort the input sections in the order of their linked section. */
11055 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11056 compare_link_order
);
11058 /* Change the offsets of the sections. */
11060 for (n
= 0; n
< seen_linkorder
; n
++)
11062 s
= sections
[n
]->u
.indirect
.section
;
11063 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11064 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11065 sections
[n
]->offset
= offset
;
11066 offset
+= sections
[n
]->size
;
11073 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11074 Returns TRUE upon success, FALSE otherwise. */
11077 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11079 bfd_boolean ret
= FALSE
;
11081 const struct elf_backend_data
*bed
;
11083 enum bfd_architecture arch
;
11085 asymbol
**sympp
= NULL
;
11089 elf_symbol_type
*osymbuf
;
11091 implib_bfd
= info
->out_implib_bfd
;
11092 bed
= get_elf_backend_data (abfd
);
11094 if (!bfd_set_format (implib_bfd
, bfd_object
))
11097 flags
= bfd_get_file_flags (abfd
);
11098 flags
&= ~HAS_RELOC
;
11099 if (!bfd_set_start_address (implib_bfd
, 0)
11100 || !bfd_set_file_flags (implib_bfd
, flags
))
11103 /* Copy architecture of output file to import library file. */
11104 arch
= bfd_get_arch (abfd
);
11105 mach
= bfd_get_mach (abfd
);
11106 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11107 && (abfd
->target_defaulted
11108 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11111 /* Get symbol table size. */
11112 symsize
= bfd_get_symtab_upper_bound (abfd
);
11116 /* Read in the symbol table. */
11117 sympp
= (asymbol
**) xmalloc (symsize
);
11118 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11122 /* Allow the BFD backend to copy any private header data it
11123 understands from the output BFD to the import library BFD. */
11124 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11127 /* Filter symbols to appear in the import library. */
11128 if (bed
->elf_backend_filter_implib_symbols
)
11129 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11132 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11135 bfd_set_error (bfd_error_no_symbols
);
11136 _bfd_error_handler (_("%B: no symbol found for import library"),
11142 /* Make symbols absolute. */
11143 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11144 sizeof (*osymbuf
));
11145 for (src_count
= 0; src_count
< symcount
; src_count
++)
11147 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11148 sizeof (*osymbuf
));
11149 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11150 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11151 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11152 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11153 osymbuf
[src_count
].symbol
.value
;
11154 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11157 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11159 /* Allow the BFD backend to copy any private data it understands
11160 from the output BFD to the import library BFD. This is done last
11161 to permit the routine to look at the filtered symbol table. */
11162 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11165 if (!bfd_close (implib_bfd
))
11176 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11180 if (flinfo
->symstrtab
!= NULL
)
11181 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11182 if (flinfo
->contents
!= NULL
)
11183 free (flinfo
->contents
);
11184 if (flinfo
->external_relocs
!= NULL
)
11185 free (flinfo
->external_relocs
);
11186 if (flinfo
->internal_relocs
!= NULL
)
11187 free (flinfo
->internal_relocs
);
11188 if (flinfo
->external_syms
!= NULL
)
11189 free (flinfo
->external_syms
);
11190 if (flinfo
->locsym_shndx
!= NULL
)
11191 free (flinfo
->locsym_shndx
);
11192 if (flinfo
->internal_syms
!= NULL
)
11193 free (flinfo
->internal_syms
);
11194 if (flinfo
->indices
!= NULL
)
11195 free (flinfo
->indices
);
11196 if (flinfo
->sections
!= NULL
)
11197 free (flinfo
->sections
);
11198 if (flinfo
->symshndxbuf
!= NULL
)
11199 free (flinfo
->symshndxbuf
);
11200 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11202 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11203 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11204 free (esdo
->rel
.hashes
);
11205 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11206 free (esdo
->rela
.hashes
);
11210 /* Do the final step of an ELF link. */
11213 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11215 bfd_boolean dynamic
;
11216 bfd_boolean emit_relocs
;
11218 struct elf_final_link_info flinfo
;
11220 struct bfd_link_order
*p
;
11222 bfd_size_type max_contents_size
;
11223 bfd_size_type max_external_reloc_size
;
11224 bfd_size_type max_internal_reloc_count
;
11225 bfd_size_type max_sym_count
;
11226 bfd_size_type max_sym_shndx_count
;
11227 Elf_Internal_Sym elfsym
;
11229 Elf_Internal_Shdr
*symtab_hdr
;
11230 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11231 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11232 struct elf_outext_info eoinfo
;
11233 bfd_boolean merged
;
11234 size_t relativecount
= 0;
11235 asection
*reldyn
= 0;
11237 asection
*attr_section
= NULL
;
11238 bfd_vma attr_size
= 0;
11239 const char *std_attrs_section
;
11240 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11242 if (!is_elf_hash_table (htab
))
11245 if (bfd_link_pic (info
))
11246 abfd
->flags
|= DYNAMIC
;
11248 dynamic
= htab
->dynamic_sections_created
;
11249 dynobj
= htab
->dynobj
;
11251 emit_relocs
= (bfd_link_relocatable (info
)
11252 || info
->emitrelocations
);
11254 flinfo
.info
= info
;
11255 flinfo
.output_bfd
= abfd
;
11256 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11257 if (flinfo
.symstrtab
== NULL
)
11262 flinfo
.hash_sec
= NULL
;
11263 flinfo
.symver_sec
= NULL
;
11267 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11268 /* Note that dynsym_sec can be NULL (on VMS). */
11269 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11270 /* Note that it is OK if symver_sec is NULL. */
11273 flinfo
.contents
= NULL
;
11274 flinfo
.external_relocs
= NULL
;
11275 flinfo
.internal_relocs
= NULL
;
11276 flinfo
.external_syms
= NULL
;
11277 flinfo
.locsym_shndx
= NULL
;
11278 flinfo
.internal_syms
= NULL
;
11279 flinfo
.indices
= NULL
;
11280 flinfo
.sections
= NULL
;
11281 flinfo
.symshndxbuf
= NULL
;
11282 flinfo
.filesym_count
= 0;
11284 /* The object attributes have been merged. Remove the input
11285 sections from the link, and set the contents of the output
11287 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11288 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11290 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11291 || strcmp (o
->name
, ".gnu.attributes") == 0)
11293 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11295 asection
*input_section
;
11297 if (p
->type
!= bfd_indirect_link_order
)
11299 input_section
= p
->u
.indirect
.section
;
11300 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11301 elf_link_input_bfd ignores this section. */
11302 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11305 attr_size
= bfd_elf_obj_attr_size (abfd
);
11308 bfd_set_section_size (abfd
, o
, attr_size
);
11310 /* Skip this section later on. */
11311 o
->map_head
.link_order
= NULL
;
11314 o
->flags
|= SEC_EXCLUDE
;
11318 /* Count up the number of relocations we will output for each output
11319 section, so that we know the sizes of the reloc sections. We
11320 also figure out some maximum sizes. */
11321 max_contents_size
= 0;
11322 max_external_reloc_size
= 0;
11323 max_internal_reloc_count
= 0;
11325 max_sym_shndx_count
= 0;
11327 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11329 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11330 o
->reloc_count
= 0;
11332 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11334 unsigned int reloc_count
= 0;
11335 unsigned int additional_reloc_count
= 0;
11336 struct bfd_elf_section_data
*esdi
= NULL
;
11338 if (p
->type
== bfd_section_reloc_link_order
11339 || p
->type
== bfd_symbol_reloc_link_order
)
11341 else if (p
->type
== bfd_indirect_link_order
)
11345 sec
= p
->u
.indirect
.section
;
11346 esdi
= elf_section_data (sec
);
11348 /* Mark all sections which are to be included in the
11349 link. This will normally be every section. We need
11350 to do this so that we can identify any sections which
11351 the linker has decided to not include. */
11352 sec
->linker_mark
= TRUE
;
11354 if (sec
->flags
& SEC_MERGE
)
11357 if (esdo
->this_hdr
.sh_type
== SHT_REL
11358 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11359 /* Some backends use reloc_count in relocation sections
11360 to count particular types of relocs. Of course,
11361 reloc sections themselves can't have relocations. */
11363 else if (emit_relocs
)
11365 reloc_count
= sec
->reloc_count
;
11366 if (bed
->elf_backend_count_additional_relocs
)
11369 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11370 additional_reloc_count
+= c
;
11373 else if (bed
->elf_backend_count_relocs
)
11374 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11376 if (sec
->rawsize
> max_contents_size
)
11377 max_contents_size
= sec
->rawsize
;
11378 if (sec
->size
> max_contents_size
)
11379 max_contents_size
= sec
->size
;
11381 /* We are interested in just local symbols, not all
11383 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11384 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11388 if (elf_bad_symtab (sec
->owner
))
11389 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11390 / bed
->s
->sizeof_sym
);
11392 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11394 if (sym_count
> max_sym_count
)
11395 max_sym_count
= sym_count
;
11397 if (sym_count
> max_sym_shndx_count
11398 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11399 max_sym_shndx_count
= sym_count
;
11401 if ((sec
->flags
& SEC_RELOC
) != 0)
11403 size_t ext_size
= 0;
11405 if (esdi
->rel
.hdr
!= NULL
)
11406 ext_size
= esdi
->rel
.hdr
->sh_size
;
11407 if (esdi
->rela
.hdr
!= NULL
)
11408 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11410 if (ext_size
> max_external_reloc_size
)
11411 max_external_reloc_size
= ext_size
;
11412 if (sec
->reloc_count
> max_internal_reloc_count
)
11413 max_internal_reloc_count
= sec
->reloc_count
;
11418 if (reloc_count
== 0)
11421 reloc_count
+= additional_reloc_count
;
11422 o
->reloc_count
+= reloc_count
;
11424 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11428 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11429 esdo
->rel
.count
+= additional_reloc_count
;
11431 if (esdi
->rela
.hdr
)
11433 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11434 esdo
->rela
.count
+= additional_reloc_count
;
11440 esdo
->rela
.count
+= reloc_count
;
11442 esdo
->rel
.count
+= reloc_count
;
11446 if (o
->reloc_count
> 0)
11447 o
->flags
|= SEC_RELOC
;
11450 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11451 set it (this is probably a bug) and if it is set
11452 assign_section_numbers will create a reloc section. */
11453 o
->flags
&=~ SEC_RELOC
;
11456 /* If the SEC_ALLOC flag is not set, force the section VMA to
11457 zero. This is done in elf_fake_sections as well, but forcing
11458 the VMA to 0 here will ensure that relocs against these
11459 sections are handled correctly. */
11460 if ((o
->flags
& SEC_ALLOC
) == 0
11461 && ! o
->user_set_vma
)
11465 if (! bfd_link_relocatable (info
) && merged
)
11466 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11468 /* Figure out the file positions for everything but the symbol table
11469 and the relocs. We set symcount to force assign_section_numbers
11470 to create a symbol table. */
11471 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11472 BFD_ASSERT (! abfd
->output_has_begun
);
11473 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11476 /* Set sizes, and assign file positions for reloc sections. */
11477 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11479 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11480 if ((o
->flags
& SEC_RELOC
) != 0)
11483 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11487 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11491 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11492 to count upwards while actually outputting the relocations. */
11493 esdo
->rel
.count
= 0;
11494 esdo
->rela
.count
= 0;
11496 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11498 /* Cache the section contents so that they can be compressed
11499 later. Use bfd_malloc since it will be freed by
11500 bfd_compress_section_contents. */
11501 unsigned char *contents
= esdo
->this_hdr
.contents
;
11502 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11505 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11506 if (contents
== NULL
)
11508 esdo
->this_hdr
.contents
= contents
;
11512 /* We have now assigned file positions for all the sections except
11513 .symtab, .strtab, and non-loaded reloc sections. We start the
11514 .symtab section at the current file position, and write directly
11515 to it. We build the .strtab section in memory. */
11516 bfd_get_symcount (abfd
) = 0;
11517 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11518 /* sh_name is set in prep_headers. */
11519 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11520 /* sh_flags, sh_addr and sh_size all start off zero. */
11521 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11522 /* sh_link is set in assign_section_numbers. */
11523 /* sh_info is set below. */
11524 /* sh_offset is set just below. */
11525 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11527 if (max_sym_count
< 20)
11528 max_sym_count
= 20;
11529 htab
->strtabsize
= max_sym_count
;
11530 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11531 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11532 if (htab
->strtab
== NULL
)
11534 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11536 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11537 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11539 if (info
->strip
!= strip_all
|| emit_relocs
)
11541 file_ptr off
= elf_next_file_pos (abfd
);
11543 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11545 /* Note that at this point elf_next_file_pos (abfd) is
11546 incorrect. We do not yet know the size of the .symtab section.
11547 We correct next_file_pos below, after we do know the size. */
11549 /* Start writing out the symbol table. The first symbol is always a
11551 elfsym
.st_value
= 0;
11552 elfsym
.st_size
= 0;
11553 elfsym
.st_info
= 0;
11554 elfsym
.st_other
= 0;
11555 elfsym
.st_shndx
= SHN_UNDEF
;
11556 elfsym
.st_target_internal
= 0;
11557 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11558 bfd_und_section_ptr
, NULL
) != 1)
11561 /* Output a symbol for each section. We output these even if we are
11562 discarding local symbols, since they are used for relocs. These
11563 symbols have no names. We store the index of each one in the
11564 index field of the section, so that we can find it again when
11565 outputting relocs. */
11567 elfsym
.st_size
= 0;
11568 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11569 elfsym
.st_other
= 0;
11570 elfsym
.st_value
= 0;
11571 elfsym
.st_target_internal
= 0;
11572 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11574 o
= bfd_section_from_elf_index (abfd
, i
);
11577 o
->target_index
= bfd_get_symcount (abfd
);
11578 elfsym
.st_shndx
= i
;
11579 if (!bfd_link_relocatable (info
))
11580 elfsym
.st_value
= o
->vma
;
11581 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11588 /* Allocate some memory to hold information read in from the input
11590 if (max_contents_size
!= 0)
11592 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11593 if (flinfo
.contents
== NULL
)
11597 if (max_external_reloc_size
!= 0)
11599 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11600 if (flinfo
.external_relocs
== NULL
)
11604 if (max_internal_reloc_count
!= 0)
11606 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11607 amt
*= sizeof (Elf_Internal_Rela
);
11608 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11609 if (flinfo
.internal_relocs
== NULL
)
11613 if (max_sym_count
!= 0)
11615 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11616 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11617 if (flinfo
.external_syms
== NULL
)
11620 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11621 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11622 if (flinfo
.internal_syms
== NULL
)
11625 amt
= max_sym_count
* sizeof (long);
11626 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11627 if (flinfo
.indices
== NULL
)
11630 amt
= max_sym_count
* sizeof (asection
*);
11631 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11632 if (flinfo
.sections
== NULL
)
11636 if (max_sym_shndx_count
!= 0)
11638 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11639 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11640 if (flinfo
.locsym_shndx
== NULL
)
11646 bfd_vma base
, end
= 0;
11649 for (sec
= htab
->tls_sec
;
11650 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11653 bfd_size_type size
= sec
->size
;
11656 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11658 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11661 size
= ord
->offset
+ ord
->size
;
11663 end
= sec
->vma
+ size
;
11665 base
= htab
->tls_sec
->vma
;
11666 /* Only align end of TLS section if static TLS doesn't have special
11667 alignment requirements. */
11668 if (bed
->static_tls_alignment
== 1)
11669 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11670 htab
->tls_size
= end
- base
;
11673 /* Reorder SHF_LINK_ORDER sections. */
11674 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11676 if (!elf_fixup_link_order (abfd
, o
))
11680 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11683 /* Since ELF permits relocations to be against local symbols, we
11684 must have the local symbols available when we do the relocations.
11685 Since we would rather only read the local symbols once, and we
11686 would rather not keep them in memory, we handle all the
11687 relocations for a single input file at the same time.
11689 Unfortunately, there is no way to know the total number of local
11690 symbols until we have seen all of them, and the local symbol
11691 indices precede the global symbol indices. This means that when
11692 we are generating relocatable output, and we see a reloc against
11693 a global symbol, we can not know the symbol index until we have
11694 finished examining all the local symbols to see which ones we are
11695 going to output. To deal with this, we keep the relocations in
11696 memory, and don't output them until the end of the link. This is
11697 an unfortunate waste of memory, but I don't see a good way around
11698 it. Fortunately, it only happens when performing a relocatable
11699 link, which is not the common case. FIXME: If keep_memory is set
11700 we could write the relocs out and then read them again; I don't
11701 know how bad the memory loss will be. */
11703 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11704 sub
->output_has_begun
= FALSE
;
11705 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11707 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11709 if (p
->type
== bfd_indirect_link_order
11710 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11711 == bfd_target_elf_flavour
)
11712 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11714 if (! sub
->output_has_begun
)
11716 if (! elf_link_input_bfd (&flinfo
, sub
))
11718 sub
->output_has_begun
= TRUE
;
11721 else if (p
->type
== bfd_section_reloc_link_order
11722 || p
->type
== bfd_symbol_reloc_link_order
)
11724 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11729 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11731 if (p
->type
== bfd_indirect_link_order
11732 && (bfd_get_flavour (sub
)
11733 == bfd_target_elf_flavour
)
11734 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11735 != bed
->s
->elfclass
))
11737 const char *iclass
, *oclass
;
11739 switch (bed
->s
->elfclass
)
11741 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11742 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11743 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11747 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11749 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11750 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11751 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11755 bfd_set_error (bfd_error_wrong_format
);
11757 /* xgettext:c-format */
11758 (_("%B: file class %s incompatible with %s"),
11759 sub
, iclass
, oclass
);
11768 /* Free symbol buffer if needed. */
11769 if (!info
->reduce_memory_overheads
)
11771 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11772 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11773 && elf_tdata (sub
)->symbuf
)
11775 free (elf_tdata (sub
)->symbuf
);
11776 elf_tdata (sub
)->symbuf
= NULL
;
11780 /* Output any global symbols that got converted to local in a
11781 version script or due to symbol visibility. We do this in a
11782 separate step since ELF requires all local symbols to appear
11783 prior to any global symbols. FIXME: We should only do this if
11784 some global symbols were, in fact, converted to become local.
11785 FIXME: Will this work correctly with the Irix 5 linker? */
11786 eoinfo
.failed
= FALSE
;
11787 eoinfo
.flinfo
= &flinfo
;
11788 eoinfo
.localsyms
= TRUE
;
11789 eoinfo
.file_sym_done
= FALSE
;
11790 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11794 /* If backend needs to output some local symbols not present in the hash
11795 table, do it now. */
11796 if (bed
->elf_backend_output_arch_local_syms
11797 && (info
->strip
!= strip_all
|| emit_relocs
))
11799 typedef int (*out_sym_func
)
11800 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11801 struct elf_link_hash_entry
*);
11803 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11804 (abfd
, info
, &flinfo
,
11805 (out_sym_func
) elf_link_output_symstrtab
)))
11809 /* That wrote out all the local symbols. Finish up the symbol table
11810 with the global symbols. Even if we want to strip everything we
11811 can, we still need to deal with those global symbols that got
11812 converted to local in a version script. */
11814 /* The sh_info field records the index of the first non local symbol. */
11815 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11818 && htab
->dynsym
!= NULL
11819 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11821 Elf_Internal_Sym sym
;
11822 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11824 o
= htab
->dynsym
->output_section
;
11825 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11827 /* Write out the section symbols for the output sections. */
11828 if (bfd_link_pic (info
)
11829 || htab
->is_relocatable_executable
)
11835 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11837 sym
.st_target_internal
= 0;
11839 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11845 dynindx
= elf_section_data (s
)->dynindx
;
11848 indx
= elf_section_data (s
)->this_idx
;
11849 BFD_ASSERT (indx
> 0);
11850 sym
.st_shndx
= indx
;
11851 if (! check_dynsym (abfd
, &sym
))
11853 sym
.st_value
= s
->vma
;
11854 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11855 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11859 /* Write out the local dynsyms. */
11860 if (htab
->dynlocal
)
11862 struct elf_link_local_dynamic_entry
*e
;
11863 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11868 /* Copy the internal symbol and turn off visibility.
11869 Note that we saved a word of storage and overwrote
11870 the original st_name with the dynstr_index. */
11872 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11874 s
= bfd_section_from_elf_index (e
->input_bfd
,
11879 elf_section_data (s
->output_section
)->this_idx
;
11880 if (! check_dynsym (abfd
, &sym
))
11882 sym
.st_value
= (s
->output_section
->vma
11884 + e
->isym
.st_value
);
11887 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11888 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11893 /* We get the global symbols from the hash table. */
11894 eoinfo
.failed
= FALSE
;
11895 eoinfo
.localsyms
= FALSE
;
11896 eoinfo
.flinfo
= &flinfo
;
11897 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11901 /* If backend needs to output some symbols not present in the hash
11902 table, do it now. */
11903 if (bed
->elf_backend_output_arch_syms
11904 && (info
->strip
!= strip_all
|| emit_relocs
))
11906 typedef int (*out_sym_func
)
11907 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11908 struct elf_link_hash_entry
*);
11910 if (! ((*bed
->elf_backend_output_arch_syms
)
11911 (abfd
, info
, &flinfo
,
11912 (out_sym_func
) elf_link_output_symstrtab
)))
11916 /* Finalize the .strtab section. */
11917 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11919 /* Swap out the .strtab section. */
11920 if (!elf_link_swap_symbols_out (&flinfo
))
11923 /* Now we know the size of the symtab section. */
11924 if (bfd_get_symcount (abfd
) > 0)
11926 /* Finish up and write out the symbol string table (.strtab)
11928 Elf_Internal_Shdr
*symstrtab_hdr
;
11929 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11931 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11932 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11934 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11935 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11936 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11937 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11938 symtab_shndx_hdr
->sh_size
= amt
;
11940 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11943 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11944 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11948 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11949 /* sh_name was set in prep_headers. */
11950 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11951 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11952 symstrtab_hdr
->sh_addr
= 0;
11953 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11954 symstrtab_hdr
->sh_entsize
= 0;
11955 symstrtab_hdr
->sh_link
= 0;
11956 symstrtab_hdr
->sh_info
= 0;
11957 /* sh_offset is set just below. */
11958 symstrtab_hdr
->sh_addralign
= 1;
11960 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11962 elf_next_file_pos (abfd
) = off
;
11964 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11965 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11969 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11971 _bfd_error_handler (_("%B: failed to generate import library"),
11972 info
->out_implib_bfd
);
11976 /* Adjust the relocs to have the correct symbol indices. */
11977 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11979 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11981 if ((o
->flags
& SEC_RELOC
) == 0)
11984 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11985 if (esdo
->rel
.hdr
!= NULL
11986 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
11988 if (esdo
->rela
.hdr
!= NULL
11989 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
11992 /* Set the reloc_count field to 0 to prevent write_relocs from
11993 trying to swap the relocs out itself. */
11994 o
->reloc_count
= 0;
11997 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11998 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12000 /* If we are linking against a dynamic object, or generating a
12001 shared library, finish up the dynamic linking information. */
12004 bfd_byte
*dyncon
, *dynconend
;
12006 /* Fix up .dynamic entries. */
12007 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12008 BFD_ASSERT (o
!= NULL
);
12010 dyncon
= o
->contents
;
12011 dynconend
= o
->contents
+ o
->size
;
12012 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12014 Elf_Internal_Dyn dyn
;
12018 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12025 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12027 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12029 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12030 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12033 dyn
.d_un
.d_val
= relativecount
;
12040 name
= info
->init_function
;
12043 name
= info
->fini_function
;
12046 struct elf_link_hash_entry
*h
;
12048 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12050 && (h
->root
.type
== bfd_link_hash_defined
12051 || h
->root
.type
== bfd_link_hash_defweak
))
12053 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12054 o
= h
->root
.u
.def
.section
;
12055 if (o
->output_section
!= NULL
)
12056 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12057 + o
->output_offset
);
12060 /* The symbol is imported from another shared
12061 library and does not apply to this one. */
12062 dyn
.d_un
.d_ptr
= 0;
12069 case DT_PREINIT_ARRAYSZ
:
12070 name
= ".preinit_array";
12072 case DT_INIT_ARRAYSZ
:
12073 name
= ".init_array";
12075 case DT_FINI_ARRAYSZ
:
12076 name
= ".fini_array";
12078 o
= bfd_get_section_by_name (abfd
, name
);
12082 (_("could not find section %s"), name
);
12087 (_("warning: %s section has zero size"), name
);
12088 dyn
.d_un
.d_val
= o
->size
;
12091 case DT_PREINIT_ARRAY
:
12092 name
= ".preinit_array";
12094 case DT_INIT_ARRAY
:
12095 name
= ".init_array";
12097 case DT_FINI_ARRAY
:
12098 name
= ".fini_array";
12100 o
= bfd_get_section_by_name (abfd
, name
);
12107 name
= ".gnu.hash";
12116 name
= ".gnu.version_d";
12119 name
= ".gnu.version_r";
12122 name
= ".gnu.version";
12124 o
= bfd_get_linker_section (dynobj
, name
);
12129 (_("could not find section %s"), name
);
12132 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12135 (_("warning: section '%s' is being made into a note"), name
);
12136 bfd_set_error (bfd_error_nonrepresentable_section
);
12139 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12146 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12150 dyn
.d_un
.d_val
= 0;
12151 dyn
.d_un
.d_ptr
= 0;
12152 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12154 Elf_Internal_Shdr
*hdr
;
12156 hdr
= elf_elfsections (abfd
)[i
];
12157 if (hdr
->sh_type
== type
12158 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12160 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12161 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12164 if (dyn
.d_un
.d_ptr
== 0
12165 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12166 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12170 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12172 /* Don't count procedure linkage table relocs in the
12173 overall reloc count. */
12174 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12175 dyn
.d_un
.d_val
-= htab
->srelplt
->size
;
12176 /* If .rela.plt is the first .rela section, exclude
12177 it from DT_RELA. */
12178 else if (dyn
.d_un
.d_ptr
== (htab
->srelplt
->output_section
->vma
12179 + htab
->srelplt
->output_offset
))
12180 dyn
.d_un
.d_ptr
+= htab
->srelplt
->size
;
12184 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12188 /* If we have created any dynamic sections, then output them. */
12189 if (dynobj
!= NULL
)
12191 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12194 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12195 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12196 || info
->error_textrel
)
12197 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12199 bfd_byte
*dyncon
, *dynconend
;
12201 dyncon
= o
->contents
;
12202 dynconend
= o
->contents
+ o
->size
;
12203 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12205 Elf_Internal_Dyn dyn
;
12207 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12209 if (dyn
.d_tag
== DT_TEXTREL
)
12211 if (info
->error_textrel
)
12212 info
->callbacks
->einfo
12213 (_("%P%X: read-only segment has dynamic relocations.\n"));
12215 info
->callbacks
->einfo
12216 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12222 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12224 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12226 || o
->output_section
== bfd_abs_section_ptr
)
12228 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12230 /* At this point, we are only interested in sections
12231 created by _bfd_elf_link_create_dynamic_sections. */
12234 if (htab
->stab_info
.stabstr
== o
)
12236 if (htab
->eh_info
.hdr_sec
== o
)
12238 if (strcmp (o
->name
, ".dynstr") != 0)
12240 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12242 (file_ptr
) o
->output_offset
12243 * bfd_octets_per_byte (abfd
),
12249 /* The contents of the .dynstr section are actually in a
12253 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12254 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12255 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12261 if (bfd_link_relocatable (info
))
12263 bfd_boolean failed
= FALSE
;
12265 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12270 /* If we have optimized stabs strings, output them. */
12271 if (htab
->stab_info
.stabstr
!= NULL
)
12273 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12277 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12280 elf_final_link_free (abfd
, &flinfo
);
12282 elf_linker (abfd
) = TRUE
;
12286 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12287 if (contents
== NULL
)
12288 return FALSE
; /* Bail out and fail. */
12289 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12290 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12297 elf_final_link_free (abfd
, &flinfo
);
12301 /* Initialize COOKIE for input bfd ABFD. */
12304 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12305 struct bfd_link_info
*info
, bfd
*abfd
)
12307 Elf_Internal_Shdr
*symtab_hdr
;
12308 const struct elf_backend_data
*bed
;
12310 bed
= get_elf_backend_data (abfd
);
12311 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12313 cookie
->abfd
= abfd
;
12314 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12315 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12316 if (cookie
->bad_symtab
)
12318 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12319 cookie
->extsymoff
= 0;
12323 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12324 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12327 if (bed
->s
->arch_size
== 32)
12328 cookie
->r_sym_shift
= 8;
12330 cookie
->r_sym_shift
= 32;
12332 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12333 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12335 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12336 cookie
->locsymcount
, 0,
12338 if (cookie
->locsyms
== NULL
)
12340 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12343 if (info
->keep_memory
)
12344 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12349 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12352 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12354 Elf_Internal_Shdr
*symtab_hdr
;
12356 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12357 if (cookie
->locsyms
!= NULL
12358 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12359 free (cookie
->locsyms
);
12362 /* Initialize the relocation information in COOKIE for input section SEC
12363 of input bfd ABFD. */
12366 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12367 struct bfd_link_info
*info
, bfd
*abfd
,
12370 const struct elf_backend_data
*bed
;
12372 if (sec
->reloc_count
== 0)
12374 cookie
->rels
= NULL
;
12375 cookie
->relend
= NULL
;
12379 bed
= get_elf_backend_data (abfd
);
12381 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12382 info
->keep_memory
);
12383 if (cookie
->rels
== NULL
)
12385 cookie
->rel
= cookie
->rels
;
12386 cookie
->relend
= (cookie
->rels
12387 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12389 cookie
->rel
= cookie
->rels
;
12393 /* Free the memory allocated by init_reloc_cookie_rels,
12397 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12400 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12401 free (cookie
->rels
);
12404 /* Initialize the whole of COOKIE for input section SEC. */
12407 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12408 struct bfd_link_info
*info
,
12411 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12413 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12418 fini_reloc_cookie (cookie
, sec
->owner
);
12423 /* Free the memory allocated by init_reloc_cookie_for_section,
12427 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12430 fini_reloc_cookie_rels (cookie
, sec
);
12431 fini_reloc_cookie (cookie
, sec
->owner
);
12434 /* Garbage collect unused sections. */
12436 /* Default gc_mark_hook. */
12439 _bfd_elf_gc_mark_hook (asection
*sec
,
12440 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12441 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12442 struct elf_link_hash_entry
*h
,
12443 Elf_Internal_Sym
*sym
)
12447 switch (h
->root
.type
)
12449 case bfd_link_hash_defined
:
12450 case bfd_link_hash_defweak
:
12451 return h
->root
.u
.def
.section
;
12453 case bfd_link_hash_common
:
12454 return h
->root
.u
.c
.p
->section
;
12461 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12466 /* For undefined __start_<name> and __stop_<name> symbols, return the
12467 first input section matching <name>. Return NULL otherwise. */
12470 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12471 struct elf_link_hash_entry
*h
)
12474 const char *sec_name
;
12476 if (h
->root
.type
!= bfd_link_hash_undefined
12477 && h
->root
.type
!= bfd_link_hash_undefweak
)
12480 s
= h
->root
.u
.undef
.section
;
12483 if (s
== (asection
*) 0 - 1)
12489 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12490 sec_name
= h
->root
.root
.string
+ 8;
12491 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12492 sec_name
= h
->root
.root
.string
+ 7;
12494 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12498 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12500 s
= bfd_get_section_by_name (i
, sec_name
);
12503 h
->root
.u
.undef
.section
= s
;
12510 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12515 /* COOKIE->rel describes a relocation against section SEC, which is
12516 a section we've decided to keep. Return the section that contains
12517 the relocation symbol, or NULL if no section contains it. */
12520 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12521 elf_gc_mark_hook_fn gc_mark_hook
,
12522 struct elf_reloc_cookie
*cookie
,
12523 bfd_boolean
*start_stop
)
12525 unsigned long r_symndx
;
12526 struct elf_link_hash_entry
*h
;
12528 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12529 if (r_symndx
== STN_UNDEF
)
12532 if (r_symndx
>= cookie
->locsymcount
12533 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12535 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12538 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12542 while (h
->root
.type
== bfd_link_hash_indirect
12543 || h
->root
.type
== bfd_link_hash_warning
)
12544 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12546 /* If this symbol is weak and there is a non-weak definition, we
12547 keep the non-weak definition because many backends put
12548 dynamic reloc info on the non-weak definition for code
12549 handling copy relocs. */
12550 if (h
->u
.weakdef
!= NULL
)
12551 h
->u
.weakdef
->mark
= 1;
12553 if (start_stop
!= NULL
)
12555 /* To work around a glibc bug, mark all XXX input sections
12556 when there is an as yet undefined reference to __start_XXX
12557 or __stop_XXX symbols. The linker will later define such
12558 symbols for orphan input sections that have a name
12559 representable as a C identifier. */
12560 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12564 *start_stop
= !s
->gc_mark
;
12569 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12572 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12573 &cookie
->locsyms
[r_symndx
]);
12576 /* COOKIE->rel describes a relocation against section SEC, which is
12577 a section we've decided to keep. Mark the section that contains
12578 the relocation symbol. */
12581 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12583 elf_gc_mark_hook_fn gc_mark_hook
,
12584 struct elf_reloc_cookie
*cookie
)
12587 bfd_boolean start_stop
= FALSE
;
12589 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12590 while (rsec
!= NULL
)
12592 if (!rsec
->gc_mark
)
12594 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12595 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12597 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12602 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12607 /* The mark phase of garbage collection. For a given section, mark
12608 it and any sections in this section's group, and all the sections
12609 which define symbols to which it refers. */
12612 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12614 elf_gc_mark_hook_fn gc_mark_hook
)
12617 asection
*group_sec
, *eh_frame
;
12621 /* Mark all the sections in the group. */
12622 group_sec
= elf_section_data (sec
)->next_in_group
;
12623 if (group_sec
&& !group_sec
->gc_mark
)
12624 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12627 /* Look through the section relocs. */
12629 eh_frame
= elf_eh_frame_section (sec
->owner
);
12630 if ((sec
->flags
& SEC_RELOC
) != 0
12631 && sec
->reloc_count
> 0
12632 && sec
!= eh_frame
)
12634 struct elf_reloc_cookie cookie
;
12636 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12640 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12641 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12646 fini_reloc_cookie_for_section (&cookie
, sec
);
12650 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12652 struct elf_reloc_cookie cookie
;
12654 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12658 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12659 gc_mark_hook
, &cookie
))
12661 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12665 eh_frame
= elf_section_eh_frame_entry (sec
);
12666 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12667 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12673 /* Scan and mark sections in a special or debug section group. */
12676 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12678 /* Point to first section of section group. */
12680 /* Used to iterate the section group. */
12683 bfd_boolean is_special_grp
= TRUE
;
12684 bfd_boolean is_debug_grp
= TRUE
;
12686 /* First scan to see if group contains any section other than debug
12687 and special section. */
12688 ssec
= msec
= elf_next_in_group (grp
);
12691 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12692 is_debug_grp
= FALSE
;
12694 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12695 is_special_grp
= FALSE
;
12697 msec
= elf_next_in_group (msec
);
12699 while (msec
!= ssec
);
12701 /* If this is a pure debug section group or pure special section group,
12702 keep all sections in this group. */
12703 if (is_debug_grp
|| is_special_grp
)
12708 msec
= elf_next_in_group (msec
);
12710 while (msec
!= ssec
);
12714 /* Keep debug and special sections. */
12717 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12718 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12722 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12725 bfd_boolean some_kept
;
12726 bfd_boolean debug_frag_seen
;
12728 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12731 /* Ensure all linker created sections are kept,
12732 see if any other section is already marked,
12733 and note if we have any fragmented debug sections. */
12734 debug_frag_seen
= some_kept
= FALSE
;
12735 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12737 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12739 else if (isec
->gc_mark
)
12742 if (debug_frag_seen
== FALSE
12743 && (isec
->flags
& SEC_DEBUGGING
)
12744 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12745 debug_frag_seen
= TRUE
;
12748 /* If no section in this file will be kept, then we can
12749 toss out the debug and special sections. */
12753 /* Keep debug and special sections like .comment when they are
12754 not part of a group. Also keep section groups that contain
12755 just debug sections or special sections. */
12756 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12758 if ((isec
->flags
& SEC_GROUP
) != 0)
12759 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12760 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12761 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12762 && elf_next_in_group (isec
) == NULL
)
12766 if (! debug_frag_seen
)
12769 /* Look for CODE sections which are going to be discarded,
12770 and find and discard any fragmented debug sections which
12771 are associated with that code section. */
12772 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12773 if ((isec
->flags
& SEC_CODE
) != 0
12774 && isec
->gc_mark
== 0)
12779 ilen
= strlen (isec
->name
);
12781 /* Association is determined by the name of the debug section
12782 containing the name of the code section as a suffix. For
12783 example .debug_line.text.foo is a debug section associated
12785 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12789 if (dsec
->gc_mark
== 0
12790 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12793 dlen
= strlen (dsec
->name
);
12796 && strncmp (dsec
->name
+ (dlen
- ilen
),
12797 isec
->name
, ilen
) == 0)
12807 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12809 struct elf_gc_sweep_symbol_info
12811 struct bfd_link_info
*info
;
12812 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12817 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12820 && (((h
->root
.type
== bfd_link_hash_defined
12821 || h
->root
.type
== bfd_link_hash_defweak
)
12822 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12823 && h
->root
.u
.def
.section
->gc_mark
))
12824 || h
->root
.type
== bfd_link_hash_undefined
12825 || h
->root
.type
== bfd_link_hash_undefweak
))
12827 struct elf_gc_sweep_symbol_info
*inf
;
12829 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12830 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12831 h
->def_regular
= 0;
12832 h
->ref_regular
= 0;
12833 h
->ref_regular_nonweak
= 0;
12839 /* The sweep phase of garbage collection. Remove all garbage sections. */
12841 typedef bfd_boolean (*gc_sweep_hook_fn
)
12842 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12845 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12849 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12850 unsigned long section_sym_count
;
12851 struct elf_gc_sweep_symbol_info sweep_info
;
12853 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12857 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12858 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12861 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12863 /* When any section in a section group is kept, we keep all
12864 sections in the section group. If the first member of
12865 the section group is excluded, we will also exclude the
12867 if (o
->flags
& SEC_GROUP
)
12869 asection
*first
= elf_next_in_group (o
);
12870 o
->gc_mark
= first
->gc_mark
;
12876 /* Skip sweeping sections already excluded. */
12877 if (o
->flags
& SEC_EXCLUDE
)
12880 /* Since this is early in the link process, it is simple
12881 to remove a section from the output. */
12882 o
->flags
|= SEC_EXCLUDE
;
12884 if (info
->print_gc_sections
&& o
->size
!= 0)
12885 /* xgettext:c-format */
12886 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12888 /* But we also have to update some of the relocation
12889 info we collected before. */
12891 && (o
->flags
& SEC_RELOC
) != 0
12892 && o
->reloc_count
!= 0
12893 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12894 && (o
->flags
& SEC_DEBUGGING
) != 0)
12895 && !bfd_is_abs_section (o
->output_section
))
12897 Elf_Internal_Rela
*internal_relocs
;
12901 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12902 info
->keep_memory
);
12903 if (internal_relocs
== NULL
)
12906 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12908 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12909 free (internal_relocs
);
12917 /* Remove the symbols that were in the swept sections from the dynamic
12918 symbol table. GCFIXME: Anyone know how to get them out of the
12919 static symbol table as well? */
12920 sweep_info
.info
= info
;
12921 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12922 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12925 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12929 /* Propagate collected vtable information. This is called through
12930 elf_link_hash_traverse. */
12933 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12935 /* Those that are not vtables. */
12936 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12939 /* Those vtables that do not have parents, we cannot merge. */
12940 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12943 /* If we've already been done, exit. */
12944 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12947 /* Make sure the parent's table is up to date. */
12948 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12950 if (h
->vtable
->used
== NULL
)
12952 /* None of this table's entries were referenced. Re-use the
12954 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12955 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12960 bfd_boolean
*cu
, *pu
;
12962 /* Or the parent's entries into ours. */
12963 cu
= h
->vtable
->used
;
12965 pu
= h
->vtable
->parent
->vtable
->used
;
12968 const struct elf_backend_data
*bed
;
12969 unsigned int log_file_align
;
12971 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12972 log_file_align
= bed
->s
->log_file_align
;
12973 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12988 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12991 bfd_vma hstart
, hend
;
12992 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12993 const struct elf_backend_data
*bed
;
12994 unsigned int log_file_align
;
12996 /* Take care of both those symbols that do not describe vtables as
12997 well as those that are not loaded. */
12998 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13001 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13002 || h
->root
.type
== bfd_link_hash_defweak
);
13004 sec
= h
->root
.u
.def
.section
;
13005 hstart
= h
->root
.u
.def
.value
;
13006 hend
= hstart
+ h
->size
;
13008 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13010 return *(bfd_boolean
*) okp
= FALSE
;
13011 bed
= get_elf_backend_data (sec
->owner
);
13012 log_file_align
= bed
->s
->log_file_align
;
13014 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13016 for (rel
= relstart
; rel
< relend
; ++rel
)
13017 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13019 /* If the entry is in use, do nothing. */
13020 if (h
->vtable
->used
13021 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13023 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13024 if (h
->vtable
->used
[entry
])
13027 /* Otherwise, kill it. */
13028 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13034 /* Mark sections containing dynamically referenced symbols. When
13035 building shared libraries, we must assume that any visible symbol is
13039 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13041 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13042 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13044 if ((h
->root
.type
== bfd_link_hash_defined
13045 || h
->root
.type
== bfd_link_hash_defweak
)
13047 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13048 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13049 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13050 && (!bfd_link_executable (info
)
13051 || info
->export_dynamic
13054 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13055 && (h
->versioned
>= versioned
13056 || !bfd_hide_sym_by_version (info
->version_info
,
13057 h
->root
.root
.string
)))))
13058 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13063 /* Keep all sections containing symbols undefined on the command-line,
13064 and the section containing the entry symbol. */
13067 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13069 struct bfd_sym_chain
*sym
;
13071 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13073 struct elf_link_hash_entry
*h
;
13075 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13076 FALSE
, FALSE
, FALSE
);
13079 && (h
->root
.type
== bfd_link_hash_defined
13080 || h
->root
.type
== bfd_link_hash_defweak
)
13081 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13082 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13083 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13088 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13089 struct bfd_link_info
*info
)
13091 bfd
*ibfd
= info
->input_bfds
;
13093 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13096 struct elf_reloc_cookie cookie
;
13098 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13101 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13104 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13106 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13107 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13109 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13110 fini_reloc_cookie_rels (&cookie
, sec
);
13117 /* Do mark and sweep of unused sections. */
13120 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13122 bfd_boolean ok
= TRUE
;
13124 elf_gc_mark_hook_fn gc_mark_hook
;
13125 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13126 struct elf_link_hash_table
*htab
;
13128 if (!bed
->can_gc_sections
13129 || !is_elf_hash_table (info
->hash
))
13131 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13135 bed
->gc_keep (info
);
13136 htab
= elf_hash_table (info
);
13138 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13139 at the .eh_frame section if we can mark the FDEs individually. */
13140 for (sub
= info
->input_bfds
;
13141 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13142 sub
= sub
->link
.next
)
13145 struct elf_reloc_cookie cookie
;
13147 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13148 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13150 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13151 if (elf_section_data (sec
)->sec_info
13152 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13153 elf_eh_frame_section (sub
) = sec
;
13154 fini_reloc_cookie_for_section (&cookie
, sec
);
13155 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13159 /* Apply transitive closure to the vtable entry usage info. */
13160 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13164 /* Kill the vtable relocations that were not used. */
13165 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13169 /* Mark dynamically referenced symbols. */
13170 if (htab
->dynamic_sections_created
)
13171 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13173 /* Grovel through relocs to find out who stays ... */
13174 gc_mark_hook
= bed
->gc_mark_hook
;
13175 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13179 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13180 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13183 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13184 Also treat note sections as a root, if the section is not part
13186 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13188 && (o
->flags
& SEC_EXCLUDE
) == 0
13189 && ((o
->flags
& SEC_KEEP
) != 0
13190 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13191 && elf_next_in_group (o
) == NULL
)))
13193 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13198 /* Allow the backend to mark additional target specific sections. */
13199 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13201 /* ... and mark SEC_EXCLUDE for those that go. */
13202 return elf_gc_sweep (abfd
, info
);
13205 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13208 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13210 struct elf_link_hash_entry
*h
,
13213 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13214 struct elf_link_hash_entry
**search
, *child
;
13215 size_t extsymcount
;
13216 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13218 /* The sh_info field of the symtab header tells us where the
13219 external symbols start. We don't care about the local symbols at
13221 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13222 if (!elf_bad_symtab (abfd
))
13223 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13225 sym_hashes
= elf_sym_hashes (abfd
);
13226 sym_hashes_end
= sym_hashes
+ extsymcount
;
13228 /* Hunt down the child symbol, which is in this section at the same
13229 offset as the relocation. */
13230 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13232 if ((child
= *search
) != NULL
13233 && (child
->root
.type
== bfd_link_hash_defined
13234 || child
->root
.type
== bfd_link_hash_defweak
)
13235 && child
->root
.u
.def
.section
== sec
13236 && child
->root
.u
.def
.value
== offset
)
13240 /* xgettext:c-format */
13241 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13242 abfd
, sec
, (unsigned long) offset
);
13243 bfd_set_error (bfd_error_invalid_operation
);
13247 if (!child
->vtable
)
13249 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13250 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13251 if (!child
->vtable
)
13256 /* This *should* only be the absolute section. It could potentially
13257 be that someone has defined a non-global vtable though, which
13258 would be bad. It isn't worth paging in the local symbols to be
13259 sure though; that case should simply be handled by the assembler. */
13261 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13264 child
->vtable
->parent
= h
;
13269 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13272 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13273 asection
*sec ATTRIBUTE_UNUSED
,
13274 struct elf_link_hash_entry
*h
,
13277 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13278 unsigned int log_file_align
= bed
->s
->log_file_align
;
13282 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13283 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13288 if (addend
>= h
->vtable
->size
)
13290 size_t size
, bytes
, file_align
;
13291 bfd_boolean
*ptr
= h
->vtable
->used
;
13293 /* While the symbol is undefined, we have to be prepared to handle
13295 file_align
= 1 << log_file_align
;
13296 if (h
->root
.type
== bfd_link_hash_undefined
)
13297 size
= addend
+ file_align
;
13301 if (addend
>= size
)
13303 /* Oops! We've got a reference past the defined end of
13304 the table. This is probably a bug -- shall we warn? */
13305 size
= addend
+ file_align
;
13308 size
= (size
+ file_align
- 1) & -file_align
;
13310 /* Allocate one extra entry for use as a "done" flag for the
13311 consolidation pass. */
13312 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13316 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13322 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13323 * sizeof (bfd_boolean
));
13324 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13328 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13333 /* And arrange for that done flag to be at index -1. */
13334 h
->vtable
->used
= ptr
+ 1;
13335 h
->vtable
->size
= size
;
13338 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13343 /* Map an ELF section header flag to its corresponding string. */
13347 flagword flag_value
;
13348 } elf_flags_to_name_table
;
13350 static elf_flags_to_name_table elf_flags_to_names
[] =
13352 { "SHF_WRITE", SHF_WRITE
},
13353 { "SHF_ALLOC", SHF_ALLOC
},
13354 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13355 { "SHF_MERGE", SHF_MERGE
},
13356 { "SHF_STRINGS", SHF_STRINGS
},
13357 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13358 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13359 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13360 { "SHF_GROUP", SHF_GROUP
},
13361 { "SHF_TLS", SHF_TLS
},
13362 { "SHF_MASKOS", SHF_MASKOS
},
13363 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13366 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13368 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13369 struct flag_info
*flaginfo
,
13372 const bfd_vma sh_flags
= elf_section_flags (section
);
13374 if (!flaginfo
->flags_initialized
)
13376 bfd
*obfd
= info
->output_bfd
;
13377 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13378 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13380 int without_hex
= 0;
13382 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13385 flagword (*lookup
) (char *);
13387 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13388 if (lookup
!= NULL
)
13390 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13394 if (tf
->with
== with_flags
)
13395 with_hex
|= hexval
;
13396 else if (tf
->with
== without_flags
)
13397 without_hex
|= hexval
;
13402 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13404 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13406 if (tf
->with
== with_flags
)
13407 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13408 else if (tf
->with
== without_flags
)
13409 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13416 info
->callbacks
->einfo
13417 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13421 flaginfo
->flags_initialized
= TRUE
;
13422 flaginfo
->only_with_flags
|= with_hex
;
13423 flaginfo
->not_with_flags
|= without_hex
;
13426 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13429 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13435 struct alloc_got_off_arg
{
13437 struct bfd_link_info
*info
;
13440 /* We need a special top-level link routine to convert got reference counts
13441 to real got offsets. */
13444 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13446 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13447 bfd
*obfd
= gofarg
->info
->output_bfd
;
13448 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13450 if (h
->got
.refcount
> 0)
13452 h
->got
.offset
= gofarg
->gotoff
;
13453 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13456 h
->got
.offset
= (bfd_vma
) -1;
13461 /* And an accompanying bit to work out final got entry offsets once
13462 we're done. Should be called from final_link. */
13465 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13466 struct bfd_link_info
*info
)
13469 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13471 struct alloc_got_off_arg gofarg
;
13473 BFD_ASSERT (abfd
== info
->output_bfd
);
13475 if (! is_elf_hash_table (info
->hash
))
13478 /* The GOT offset is relative to the .got section, but the GOT header is
13479 put into the .got.plt section, if the backend uses it. */
13480 if (bed
->want_got_plt
)
13483 gotoff
= bed
->got_header_size
;
13485 /* Do the local .got entries first. */
13486 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13488 bfd_signed_vma
*local_got
;
13489 size_t j
, locsymcount
;
13490 Elf_Internal_Shdr
*symtab_hdr
;
13492 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13495 local_got
= elf_local_got_refcounts (i
);
13499 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13500 if (elf_bad_symtab (i
))
13501 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13503 locsymcount
= symtab_hdr
->sh_info
;
13505 for (j
= 0; j
< locsymcount
; ++j
)
13507 if (local_got
[j
] > 0)
13509 local_got
[j
] = gotoff
;
13510 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13513 local_got
[j
] = (bfd_vma
) -1;
13517 /* Then the global .got entries. .plt refcounts are handled by
13518 adjust_dynamic_symbol */
13519 gofarg
.gotoff
= gotoff
;
13520 gofarg
.info
= info
;
13521 elf_link_hash_traverse (elf_hash_table (info
),
13522 elf_gc_allocate_got_offsets
,
13527 /* Many folk need no more in the way of final link than this, once
13528 got entry reference counting is enabled. */
13531 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13533 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13536 /* Invoke the regular ELF backend linker to do all the work. */
13537 return bfd_elf_final_link (abfd
, info
);
13541 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13543 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13545 if (rcookie
->bad_symtab
)
13546 rcookie
->rel
= rcookie
->rels
;
13548 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13550 unsigned long r_symndx
;
13552 if (! rcookie
->bad_symtab
)
13553 if (rcookie
->rel
->r_offset
> offset
)
13555 if (rcookie
->rel
->r_offset
!= offset
)
13558 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13559 if (r_symndx
== STN_UNDEF
)
13562 if (r_symndx
>= rcookie
->locsymcount
13563 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13565 struct elf_link_hash_entry
*h
;
13567 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13569 while (h
->root
.type
== bfd_link_hash_indirect
13570 || h
->root
.type
== bfd_link_hash_warning
)
13571 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13573 if ((h
->root
.type
== bfd_link_hash_defined
13574 || h
->root
.type
== bfd_link_hash_defweak
)
13575 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13576 || h
->root
.u
.def
.section
->kept_section
!= NULL
13577 || discarded_section (h
->root
.u
.def
.section
)))
13582 /* It's not a relocation against a global symbol,
13583 but it could be a relocation against a local
13584 symbol for a discarded section. */
13586 Elf_Internal_Sym
*isym
;
13588 /* Need to: get the symbol; get the section. */
13589 isym
= &rcookie
->locsyms
[r_symndx
];
13590 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13592 && (isec
->kept_section
!= NULL
13593 || discarded_section (isec
)))
13601 /* Discard unneeded references to discarded sections.
13602 Returns -1 on error, 1 if any section's size was changed, 0 if
13603 nothing changed. This function assumes that the relocations are in
13604 sorted order, which is true for all known assemblers. */
13607 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13609 struct elf_reloc_cookie cookie
;
13614 if (info
->traditional_format
13615 || !is_elf_hash_table (info
->hash
))
13618 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13623 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13626 || i
->reloc_count
== 0
13627 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13631 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13634 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13637 if (_bfd_discard_section_stabs (abfd
, i
,
13638 elf_section_data (i
)->sec_info
,
13639 bfd_elf_reloc_symbol_deleted_p
,
13643 fini_reloc_cookie_for_section (&cookie
, i
);
13648 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13649 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13654 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13660 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13663 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13666 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13667 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13668 bfd_elf_reloc_symbol_deleted_p
,
13672 fini_reloc_cookie_for_section (&cookie
, i
);
13676 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13678 const struct elf_backend_data
*bed
;
13680 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13683 bed
= get_elf_backend_data (abfd
);
13685 if (bed
->elf_backend_discard_info
!= NULL
)
13687 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13690 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13693 fini_reloc_cookie (&cookie
, abfd
);
13697 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13698 _bfd_elf_end_eh_frame_parsing (info
);
13700 if (info
->eh_frame_hdr_type
13701 && !bfd_link_relocatable (info
)
13702 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13709 _bfd_elf_section_already_linked (bfd
*abfd
,
13711 struct bfd_link_info
*info
)
13714 const char *name
, *key
;
13715 struct bfd_section_already_linked
*l
;
13716 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13718 if (sec
->output_section
== bfd_abs_section_ptr
)
13721 flags
= sec
->flags
;
13723 /* Return if it isn't a linkonce section. A comdat group section
13724 also has SEC_LINK_ONCE set. */
13725 if ((flags
& SEC_LINK_ONCE
) == 0)
13728 /* Don't put group member sections on our list of already linked
13729 sections. They are handled as a group via their group section. */
13730 if (elf_sec_group (sec
) != NULL
)
13733 /* For a SHT_GROUP section, use the group signature as the key. */
13735 if ((flags
& SEC_GROUP
) != 0
13736 && elf_next_in_group (sec
) != NULL
13737 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13738 key
= elf_group_name (elf_next_in_group (sec
));
13741 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13742 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13743 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13746 /* Must be a user linkonce section that doesn't follow gcc's
13747 naming convention. In this case we won't be matching
13748 single member groups. */
13752 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13754 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13756 /* We may have 2 different types of sections on the list: group
13757 sections with a signature of <key> (<key> is some string),
13758 and linkonce sections named .gnu.linkonce.<type>.<key>.
13759 Match like sections. LTO plugin sections are an exception.
13760 They are always named .gnu.linkonce.t.<key> and match either
13761 type of section. */
13762 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13763 && ((flags
& SEC_GROUP
) != 0
13764 || strcmp (name
, l
->sec
->name
) == 0))
13765 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13767 /* The section has already been linked. See if we should
13768 issue a warning. */
13769 if (!_bfd_handle_already_linked (sec
, l
, info
))
13772 if (flags
& SEC_GROUP
)
13774 asection
*first
= elf_next_in_group (sec
);
13775 asection
*s
= first
;
13779 s
->output_section
= bfd_abs_section_ptr
;
13780 /* Record which group discards it. */
13781 s
->kept_section
= l
->sec
;
13782 s
= elf_next_in_group (s
);
13783 /* These lists are circular. */
13793 /* A single member comdat group section may be discarded by a
13794 linkonce section and vice versa. */
13795 if ((flags
& SEC_GROUP
) != 0)
13797 asection
*first
= elf_next_in_group (sec
);
13799 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13800 /* Check this single member group against linkonce sections. */
13801 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13802 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13803 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13805 first
->output_section
= bfd_abs_section_ptr
;
13806 first
->kept_section
= l
->sec
;
13807 sec
->output_section
= bfd_abs_section_ptr
;
13812 /* Check this linkonce section against single member groups. */
13813 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13814 if (l
->sec
->flags
& SEC_GROUP
)
13816 asection
*first
= elf_next_in_group (l
->sec
);
13819 && elf_next_in_group (first
) == first
13820 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13822 sec
->output_section
= bfd_abs_section_ptr
;
13823 sec
->kept_section
= first
;
13828 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13829 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13830 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13831 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13832 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13833 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13834 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13835 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13836 The reverse order cannot happen as there is never a bfd with only the
13837 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13838 matter as here were are looking only for cross-bfd sections. */
13840 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13841 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13842 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13843 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13845 if (abfd
!= l
->sec
->owner
)
13846 sec
->output_section
= bfd_abs_section_ptr
;
13850 /* This is the first section with this name. Record it. */
13851 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13852 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13853 return sec
->output_section
== bfd_abs_section_ptr
;
13857 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13859 return sym
->st_shndx
== SHN_COMMON
;
13863 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13869 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13871 return bfd_com_section_ptr
;
13875 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13876 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13877 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13878 bfd
*ibfd ATTRIBUTE_UNUSED
,
13879 unsigned long symndx ATTRIBUTE_UNUSED
)
13881 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13882 return bed
->s
->arch_size
/ 8;
13885 /* Routines to support the creation of dynamic relocs. */
13887 /* Returns the name of the dynamic reloc section associated with SEC. */
13889 static const char *
13890 get_dynamic_reloc_section_name (bfd
* abfd
,
13892 bfd_boolean is_rela
)
13895 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13896 const char *prefix
= is_rela
? ".rela" : ".rel";
13898 if (old_name
== NULL
)
13901 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13902 sprintf (name
, "%s%s", prefix
, old_name
);
13907 /* Returns the dynamic reloc section associated with SEC.
13908 If necessary compute the name of the dynamic reloc section based
13909 on SEC's name (looked up in ABFD's string table) and the setting
13913 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13915 bfd_boolean is_rela
)
13917 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13919 if (reloc_sec
== NULL
)
13921 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13925 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13927 if (reloc_sec
!= NULL
)
13928 elf_section_data (sec
)->sreloc
= reloc_sec
;
13935 /* Returns the dynamic reloc section associated with SEC. If the
13936 section does not exist it is created and attached to the DYNOBJ
13937 bfd and stored in the SRELOC field of SEC's elf_section_data
13940 ALIGNMENT is the alignment for the newly created section and
13941 IS_RELA defines whether the name should be .rela.<SEC's name>
13942 or .rel.<SEC's name>. The section name is looked up in the
13943 string table associated with ABFD. */
13946 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13948 unsigned int alignment
,
13950 bfd_boolean is_rela
)
13952 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13954 if (reloc_sec
== NULL
)
13956 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13961 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13963 if (reloc_sec
== NULL
)
13965 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13966 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13967 if ((sec
->flags
& SEC_ALLOC
) != 0)
13968 flags
|= SEC_ALLOC
| SEC_LOAD
;
13970 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13971 if (reloc_sec
!= NULL
)
13973 /* _bfd_elf_get_sec_type_attr chooses a section type by
13974 name. Override as it may be wrong, eg. for a user
13975 section named "auto" we'll get ".relauto" which is
13976 seen to be a .rela section. */
13977 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13978 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13983 elf_section_data (sec
)->sreloc
= reloc_sec
;
13989 /* Copy the ELF symbol type and other attributes for a linker script
13990 assignment from HSRC to HDEST. Generally this should be treated as
13991 if we found a strong non-dynamic definition for HDEST (except that
13992 ld ignores multiple definition errors). */
13994 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
13995 struct bfd_link_hash_entry
*hdest
,
13996 struct bfd_link_hash_entry
*hsrc
)
13998 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
13999 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14000 Elf_Internal_Sym isym
;
14002 ehdest
->type
= ehsrc
->type
;
14003 ehdest
->target_internal
= ehsrc
->target_internal
;
14005 isym
.st_other
= ehsrc
->other
;
14006 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14009 /* Append a RELA relocation REL to section S in BFD. */
14012 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14014 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14015 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14016 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14017 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14020 /* Append a REL relocation REL to section S in BFD. */
14023 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14025 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14026 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14027 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14028 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);