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
));
432 /* The .rel[a].bss section holds copy relocs. This section is not
433 normally needed. We need to create it here, though, so that the
434 linker will map it to an output section. We can't just create it
435 only if we need it, because we will not know whether we need it
436 until we have seen all the input files, and the first time the
437 main linker code calls BFD after examining all the input files
438 (size_dynamic_sections) the input sections have already been
439 mapped to the output sections. If the section turns out not to
440 be needed, we can discard it later. We will never need this
441 section when generating a shared object, since they do not use
443 if (bfd_link_executable (info
))
445 s
= bfd_make_section_anyway_with_flags (abfd
,
446 (bed
->rela_plts_and_copies_p
447 ? ".rela.bss" : ".rel.bss"),
448 flags
| SEC_READONLY
);
450 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
459 /* Record a new dynamic symbol. We record the dynamic symbols as we
460 read the input files, since we need to have a list of all of them
461 before we can determine the final sizes of the output sections.
462 Note that we may actually call this function even though we are not
463 going to output any dynamic symbols; in some cases we know that a
464 symbol should be in the dynamic symbol table, but only if there is
468 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
469 struct elf_link_hash_entry
*h
)
471 if (h
->dynindx
== -1)
473 struct elf_strtab_hash
*dynstr
;
478 /* XXX: The ABI draft says the linker must turn hidden and
479 internal symbols into STB_LOCAL symbols when producing the
480 DSO. However, if ld.so honors st_other in the dynamic table,
481 this would not be necessary. */
482 switch (ELF_ST_VISIBILITY (h
->other
))
486 if (h
->root
.type
!= bfd_link_hash_undefined
487 && h
->root
.type
!= bfd_link_hash_undefweak
)
490 if (!elf_hash_table (info
)->is_relocatable_executable
)
498 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
499 ++elf_hash_table (info
)->dynsymcount
;
501 dynstr
= elf_hash_table (info
)->dynstr
;
504 /* Create a strtab to hold the dynamic symbol names. */
505 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
510 /* We don't put any version information in the dynamic string
512 name
= h
->root
.root
.string
;
513 p
= strchr (name
, ELF_VER_CHR
);
515 /* We know that the p points into writable memory. In fact,
516 there are only a few symbols that have read-only names, being
517 those like _GLOBAL_OFFSET_TABLE_ that are created specially
518 by the backends. Most symbols will have names pointing into
519 an ELF string table read from a file, or to objalloc memory. */
522 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
527 if (indx
== (size_t) -1)
529 h
->dynstr_index
= indx
;
535 /* Mark a symbol dynamic. */
538 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
539 struct elf_link_hash_entry
*h
,
540 Elf_Internal_Sym
*sym
)
542 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
544 /* It may be called more than once on the same H. */
545 if(h
->dynamic
|| bfd_link_relocatable (info
))
548 if ((info
->dynamic_data
549 && (h
->type
== STT_OBJECT
550 || h
->type
== STT_COMMON
552 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
553 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
555 && h
->root
.type
== bfd_link_hash_new
556 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
560 /* Record an assignment to a symbol made by a linker script. We need
561 this in case some dynamic object refers to this symbol. */
564 bfd_elf_record_link_assignment (bfd
*output_bfd
,
565 struct bfd_link_info
*info
,
570 struct elf_link_hash_entry
*h
, *hv
;
571 struct elf_link_hash_table
*htab
;
572 const struct elf_backend_data
*bed
;
574 if (!is_elf_hash_table (info
->hash
))
577 htab
= elf_hash_table (info
);
578 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
582 if (h
->root
.type
== bfd_link_hash_warning
)
583 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
585 if (h
->versioned
== unknown
)
587 /* Set versioned if symbol version is unknown. */
588 char *version
= strrchr (name
, ELF_VER_CHR
);
591 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
592 h
->versioned
= versioned_hidden
;
594 h
->versioned
= versioned
;
598 switch (h
->root
.type
)
600 case bfd_link_hash_defined
:
601 case bfd_link_hash_defweak
:
602 case bfd_link_hash_common
:
604 case bfd_link_hash_undefweak
:
605 case bfd_link_hash_undefined
:
606 /* Since we're defining the symbol, don't let it seem to have not
607 been defined. record_dynamic_symbol and size_dynamic_sections
608 may depend on this. */
609 h
->root
.type
= bfd_link_hash_new
;
610 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
611 bfd_link_repair_undef_list (&htab
->root
);
613 case bfd_link_hash_new
:
614 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
617 case bfd_link_hash_indirect
:
618 /* We had a versioned symbol in a dynamic library. We make the
619 the versioned symbol point to this one. */
620 bed
= get_elf_backend_data (output_bfd
);
622 while (hv
->root
.type
== bfd_link_hash_indirect
623 || hv
->root
.type
== bfd_link_hash_warning
)
624 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
625 /* We don't need to update h->root.u since linker will set them
627 h
->root
.type
= bfd_link_hash_undefined
;
628 hv
->root
.type
= bfd_link_hash_indirect
;
629 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
630 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
637 /* If this symbol is being provided by the linker script, and it is
638 currently defined by a dynamic object, but not by a regular
639 object, then mark it as undefined so that the generic linker will
640 force the correct value. */
644 h
->root
.type
= bfd_link_hash_undefined
;
646 /* If this symbol is not being provided by the linker script, and it is
647 currently defined by a dynamic object, but not by a regular object,
648 then clear out any version information because the symbol will not be
649 associated with the dynamic object any more. */
653 h
->verinfo
.verdef
= NULL
;
659 bed
= get_elf_backend_data (output_bfd
);
660 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
661 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
662 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
665 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
667 if (!bfd_link_relocatable (info
)
669 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
670 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
675 || bfd_link_dll (info
)
676 || elf_hash_table (info
)->is_relocatable_executable
)
679 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
682 /* If this is a weak defined symbol, and we know a corresponding
683 real symbol from the same dynamic object, make sure the real
684 symbol is also made into a dynamic symbol. */
685 if (h
->u
.weakdef
!= NULL
686 && h
->u
.weakdef
->dynindx
== -1)
688 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
696 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
697 success, and 2 on a failure caused by attempting to record a symbol
698 in a discarded section, eg. a discarded link-once section symbol. */
701 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
706 struct elf_link_local_dynamic_entry
*entry
;
707 struct elf_link_hash_table
*eht
;
708 struct elf_strtab_hash
*dynstr
;
711 Elf_External_Sym_Shndx eshndx
;
712 char esym
[sizeof (Elf64_External_Sym
)];
714 if (! is_elf_hash_table (info
->hash
))
717 /* See if the entry exists already. */
718 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
719 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
722 amt
= sizeof (*entry
);
723 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
727 /* Go find the symbol, so that we can find it's name. */
728 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
729 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
731 bfd_release (input_bfd
, entry
);
735 if (entry
->isym
.st_shndx
!= SHN_UNDEF
736 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
740 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
741 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
743 /* We can still bfd_release here as nothing has done another
744 bfd_alloc. We can't do this later in this function. */
745 bfd_release (input_bfd
, entry
);
750 name
= (bfd_elf_string_from_elf_section
751 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
752 entry
->isym
.st_name
));
754 dynstr
= elf_hash_table (info
)->dynstr
;
757 /* Create a strtab to hold the dynamic symbol names. */
758 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
763 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
764 if (dynstr_index
== (size_t) -1)
766 entry
->isym
.st_name
= dynstr_index
;
768 eht
= elf_hash_table (info
);
770 entry
->next
= eht
->dynlocal
;
771 eht
->dynlocal
= entry
;
772 entry
->input_bfd
= input_bfd
;
773 entry
->input_indx
= input_indx
;
776 /* Whatever binding the symbol had before, it's now local. */
778 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
780 /* The dynindx will be set at the end of size_dynamic_sections. */
785 /* Return the dynindex of a local dynamic symbol. */
788 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
792 struct elf_link_local_dynamic_entry
*e
;
794 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
795 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
800 /* This function is used to renumber the dynamic symbols, if some of
801 them are removed because they are marked as local. This is called
802 via elf_link_hash_traverse. */
805 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
808 size_t *count
= (size_t *) data
;
813 if (h
->dynindx
!= -1)
814 h
->dynindx
= ++(*count
);
820 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
821 STB_LOCAL binding. */
824 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
827 size_t *count
= (size_t *) data
;
829 if (!h
->forced_local
)
832 if (h
->dynindx
!= -1)
833 h
->dynindx
= ++(*count
);
838 /* Return true if the dynamic symbol for a given section should be
839 omitted when creating a shared library. */
841 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
842 struct bfd_link_info
*info
,
845 struct elf_link_hash_table
*htab
;
848 switch (elf_section_data (p
)->this_hdr
.sh_type
)
852 /* If sh_type is yet undecided, assume it could be
853 SHT_PROGBITS/SHT_NOBITS. */
855 htab
= elf_hash_table (info
);
856 if (p
== htab
->tls_sec
)
859 if (htab
->text_index_section
!= NULL
)
860 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
862 return (htab
->dynobj
!= NULL
863 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
864 && ip
->output_section
== p
);
866 /* There shouldn't be section relative relocations
867 against any other section. */
873 /* Assign dynsym indices. In a shared library we generate a section
874 symbol for each output section, which come first. Next come symbols
875 which have been forced to local binding. Then all of the back-end
876 allocated local dynamic syms, followed by the rest of the global
880 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
881 struct bfd_link_info
*info
,
882 unsigned long *section_sym_count
)
884 unsigned long dynsymcount
= 0;
886 if (bfd_link_pic (info
)
887 || elf_hash_table (info
)->is_relocatable_executable
)
889 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
891 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
892 if ((p
->flags
& SEC_EXCLUDE
) == 0
893 && (p
->flags
& SEC_ALLOC
) != 0
894 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
895 elf_section_data (p
)->dynindx
= ++dynsymcount
;
897 elf_section_data (p
)->dynindx
= 0;
899 *section_sym_count
= dynsymcount
;
901 elf_link_hash_traverse (elf_hash_table (info
),
902 elf_link_renumber_local_hash_table_dynsyms
,
905 if (elf_hash_table (info
)->dynlocal
)
907 struct elf_link_local_dynamic_entry
*p
;
908 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
909 p
->dynindx
= ++dynsymcount
;
911 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
913 elf_link_hash_traverse (elf_hash_table (info
),
914 elf_link_renumber_hash_table_dynsyms
,
917 /* There is an unused NULL entry at the head of the table which we
918 must account for in our count even if the table is empty since it
919 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
923 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
927 /* Merge st_other field. */
930 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
931 const Elf_Internal_Sym
*isym
, asection
*sec
,
932 bfd_boolean definition
, bfd_boolean dynamic
)
934 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
936 /* If st_other has a processor-specific meaning, specific
937 code might be needed here. */
938 if (bed
->elf_backend_merge_symbol_attribute
)
939 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
944 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
945 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
947 /* Keep the most constraining visibility. Leave the remainder
948 of the st_other field to elf_backend_merge_symbol_attribute. */
949 if (symvis
- 1 < hvis
- 1)
950 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
953 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
954 && (sec
->flags
& SEC_READONLY
) == 0)
955 h
->protected_def
= 1;
958 /* This function is called when we want to merge a new symbol with an
959 existing symbol. It handles the various cases which arise when we
960 find a definition in a dynamic object, or when there is already a
961 definition in a dynamic object. The new symbol is described by
962 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
963 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
964 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
965 of an old common symbol. We set OVERRIDE if the old symbol is
966 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
967 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
968 to change. By OK to change, we mean that we shouldn't warn if the
969 type or size does change. */
972 _bfd_elf_merge_symbol (bfd
*abfd
,
973 struct bfd_link_info
*info
,
975 Elf_Internal_Sym
*sym
,
978 struct elf_link_hash_entry
**sym_hash
,
980 bfd_boolean
*pold_weak
,
981 unsigned int *pold_alignment
,
983 bfd_boolean
*override
,
984 bfd_boolean
*type_change_ok
,
985 bfd_boolean
*size_change_ok
,
986 bfd_boolean
*matched
)
988 asection
*sec
, *oldsec
;
989 struct elf_link_hash_entry
*h
;
990 struct elf_link_hash_entry
*hi
;
991 struct elf_link_hash_entry
*flip
;
994 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
995 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
996 const struct elf_backend_data
*bed
;
1003 bind
= ELF_ST_BIND (sym
->st_info
);
1005 if (! bfd_is_und_section (sec
))
1006 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1008 h
= ((struct elf_link_hash_entry
*)
1009 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1014 bed
= get_elf_backend_data (abfd
);
1016 /* NEW_VERSION is the symbol version of the new symbol. */
1017 if (h
->versioned
!= unversioned
)
1019 /* Symbol version is unknown or versioned. */
1020 new_version
= strrchr (name
, ELF_VER_CHR
);
1023 if (h
->versioned
== unknown
)
1025 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1026 h
->versioned
= versioned_hidden
;
1028 h
->versioned
= versioned
;
1031 if (new_version
[0] == '\0')
1035 h
->versioned
= unversioned
;
1040 /* For merging, we only care about real symbols. But we need to make
1041 sure that indirect symbol dynamic flags are updated. */
1043 while (h
->root
.type
== bfd_link_hash_indirect
1044 || h
->root
.type
== bfd_link_hash_warning
)
1045 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1049 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1053 /* OLD_HIDDEN is true if the existing symbol is only visible
1054 to the symbol with the same symbol version. NEW_HIDDEN is
1055 true if the new symbol is only visible to the symbol with
1056 the same symbol version. */
1057 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1058 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1059 if (!old_hidden
&& !new_hidden
)
1060 /* The new symbol matches the existing symbol if both
1065 /* OLD_VERSION is the symbol version of the existing
1069 if (h
->versioned
>= versioned
)
1070 old_version
= strrchr (h
->root
.root
.string
,
1075 /* The new symbol matches the existing symbol if they
1076 have the same symbol version. */
1077 *matched
= (old_version
== new_version
1078 || (old_version
!= NULL
1079 && new_version
!= NULL
1080 && strcmp (old_version
, new_version
) == 0));
1085 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1090 switch (h
->root
.type
)
1095 case bfd_link_hash_undefined
:
1096 case bfd_link_hash_undefweak
:
1097 oldbfd
= h
->root
.u
.undef
.abfd
;
1100 case bfd_link_hash_defined
:
1101 case bfd_link_hash_defweak
:
1102 oldbfd
= h
->root
.u
.def
.section
->owner
;
1103 oldsec
= h
->root
.u
.def
.section
;
1106 case bfd_link_hash_common
:
1107 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1108 oldsec
= h
->root
.u
.c
.p
->section
;
1110 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1113 if (poldbfd
&& *poldbfd
== NULL
)
1116 /* Differentiate strong and weak symbols. */
1117 newweak
= bind
== STB_WEAK
;
1118 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1119 || h
->root
.type
== bfd_link_hash_undefweak
);
1121 *pold_weak
= oldweak
;
1123 /* This code is for coping with dynamic objects, and is only useful
1124 if we are doing an ELF link. */
1125 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1128 /* We have to check it for every instance since the first few may be
1129 references and not all compilers emit symbol type for undefined
1131 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1133 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1134 respectively, is from a dynamic object. */
1136 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1138 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1139 syms and defined syms in dynamic libraries respectively.
1140 ref_dynamic on the other hand can be set for a symbol defined in
1141 a dynamic library, and def_dynamic may not be set; When the
1142 definition in a dynamic lib is overridden by a definition in the
1143 executable use of the symbol in the dynamic lib becomes a
1144 reference to the executable symbol. */
1147 if (bfd_is_und_section (sec
))
1149 if (bind
!= STB_WEAK
)
1151 h
->ref_dynamic_nonweak
= 1;
1152 hi
->ref_dynamic_nonweak
= 1;
1157 /* Update the existing symbol only if they match. */
1160 hi
->dynamic_def
= 1;
1164 /* If we just created the symbol, mark it as being an ELF symbol.
1165 Other than that, there is nothing to do--there is no merge issue
1166 with a newly defined symbol--so we just return. */
1168 if (h
->root
.type
== bfd_link_hash_new
)
1174 /* In cases involving weak versioned symbols, we may wind up trying
1175 to merge a symbol with itself. Catch that here, to avoid the
1176 confusion that results if we try to override a symbol with
1177 itself. The additional tests catch cases like
1178 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1179 dynamic object, which we do want to handle here. */
1181 && (newweak
|| oldweak
)
1182 && ((abfd
->flags
& DYNAMIC
) == 0
1183 || !h
->def_regular
))
1188 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1189 else if (oldsec
!= NULL
)
1191 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1192 indices used by MIPS ELF. */
1193 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1196 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1197 respectively, appear to be a definition rather than reference. */
1199 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1201 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1202 && h
->root
.type
!= bfd_link_hash_undefweak
1203 && h
->root
.type
!= bfd_link_hash_common
);
1205 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1206 respectively, appear to be a function. */
1208 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1209 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1211 oldfunc
= (h
->type
!= STT_NOTYPE
1212 && bed
->is_function_type (h
->type
));
1214 /* If creating a default indirect symbol ("foo" or "foo@") from a
1215 dynamic versioned definition ("foo@@") skip doing so if there is
1216 an existing regular definition with a different type. We don't
1217 want, for example, a "time" variable in the executable overriding
1218 a "time" function in a shared library. */
1219 if (pold_alignment
== NULL
1223 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1224 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1225 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1226 && h
->type
!= STT_NOTYPE
1227 && !(newfunc
&& oldfunc
))
1233 /* Check TLS symbols. We don't check undefined symbols introduced
1234 by "ld -u" which have no type (and oldbfd NULL), and we don't
1235 check symbols from plugins because they also have no type. */
1237 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1238 && (abfd
->flags
& BFD_PLUGIN
) == 0
1239 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1240 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1243 bfd_boolean ntdef
, tdef
;
1244 asection
*ntsec
, *tsec
;
1246 if (h
->type
== STT_TLS
)
1267 /* xgettext:c-format */
1268 (_("%s: TLS definition in %B section %A "
1269 "mismatches non-TLS definition in %B section %A"),
1270 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1271 else if (!tdef
&& !ntdef
)
1273 /* xgettext:c-format */
1274 (_("%s: TLS reference in %B "
1275 "mismatches non-TLS reference in %B"),
1276 tbfd
, ntbfd
, h
->root
.root
.string
);
1279 /* xgettext:c-format */
1280 (_("%s: TLS definition in %B section %A "
1281 "mismatches non-TLS reference in %B"),
1282 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1285 /* xgettext:c-format */
1286 (_("%s: TLS reference in %B "
1287 "mismatches non-TLS definition in %B section %A"),
1288 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1290 bfd_set_error (bfd_error_bad_value
);
1294 /* If the old symbol has non-default visibility, we ignore the new
1295 definition from a dynamic object. */
1297 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1298 && !bfd_is_und_section (sec
))
1301 /* Make sure this symbol is dynamic. */
1303 hi
->ref_dynamic
= 1;
1304 /* A protected symbol has external availability. Make sure it is
1305 recorded as dynamic.
1307 FIXME: Should we check type and size for protected symbol? */
1308 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1309 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1314 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1317 /* If the new symbol with non-default visibility comes from a
1318 relocatable file and the old definition comes from a dynamic
1319 object, we remove the old definition. */
1320 if (hi
->root
.type
== bfd_link_hash_indirect
)
1322 /* Handle the case where the old dynamic definition is
1323 default versioned. We need to copy the symbol info from
1324 the symbol with default version to the normal one if it
1325 was referenced before. */
1328 hi
->root
.type
= h
->root
.type
;
1329 h
->root
.type
= bfd_link_hash_indirect
;
1330 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1332 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1333 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1335 /* If the new symbol is hidden or internal, completely undo
1336 any dynamic link state. */
1337 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1338 h
->forced_local
= 0;
1345 /* FIXME: Should we check type and size for protected symbol? */
1355 /* If the old symbol was undefined before, then it will still be
1356 on the undefs list. If the new symbol is undefined or
1357 common, we can't make it bfd_link_hash_new here, because new
1358 undefined or common symbols will be added to the undefs list
1359 by _bfd_generic_link_add_one_symbol. Symbols may not be
1360 added twice to the undefs list. Also, if the new symbol is
1361 undefweak then we don't want to lose the strong undef. */
1362 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1364 h
->root
.type
= bfd_link_hash_undefined
;
1365 h
->root
.u
.undef
.abfd
= abfd
;
1369 h
->root
.type
= bfd_link_hash_new
;
1370 h
->root
.u
.undef
.abfd
= NULL
;
1373 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1375 /* If the new symbol is hidden or internal, completely undo
1376 any dynamic link state. */
1377 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1378 h
->forced_local
= 0;
1384 /* FIXME: Should we check type and size for protected symbol? */
1390 /* If a new weak symbol definition comes from a regular file and the
1391 old symbol comes from a dynamic library, we treat the new one as
1392 strong. Similarly, an old weak symbol definition from a regular
1393 file is treated as strong when the new symbol comes from a dynamic
1394 library. Further, an old weak symbol from a dynamic library is
1395 treated as strong if the new symbol is from a dynamic library.
1396 This reflects the way glibc's ld.so works.
1398 Do this before setting *type_change_ok or *size_change_ok so that
1399 we warn properly when dynamic library symbols are overridden. */
1401 if (newdef
&& !newdyn
&& olddyn
)
1403 if (olddef
&& newdyn
)
1406 /* Allow changes between different types of function symbol. */
1407 if (newfunc
&& oldfunc
)
1408 *type_change_ok
= TRUE
;
1410 /* It's OK to change the type if either the existing symbol or the
1411 new symbol is weak. A type change is also OK if the old symbol
1412 is undefined and the new symbol is defined. */
1417 && h
->root
.type
== bfd_link_hash_undefined
))
1418 *type_change_ok
= TRUE
;
1420 /* It's OK to change the size if either the existing symbol or the
1421 new symbol is weak, or if the old symbol is undefined. */
1424 || h
->root
.type
== bfd_link_hash_undefined
)
1425 *size_change_ok
= TRUE
;
1427 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1428 symbol, respectively, appears to be a common symbol in a dynamic
1429 object. If a symbol appears in an uninitialized section, and is
1430 not weak, and is not a function, then it may be a common symbol
1431 which was resolved when the dynamic object was created. We want
1432 to treat such symbols specially, because they raise special
1433 considerations when setting the symbol size: if the symbol
1434 appears as a common symbol in a regular object, and the size in
1435 the regular object is larger, we must make sure that we use the
1436 larger size. This problematic case can always be avoided in C,
1437 but it must be handled correctly when using Fortran shared
1440 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1441 likewise for OLDDYNCOMMON and OLDDEF.
1443 Note that this test is just a heuristic, and that it is quite
1444 possible to have an uninitialized symbol in a shared object which
1445 is really a definition, rather than a common symbol. This could
1446 lead to some minor confusion when the symbol really is a common
1447 symbol in some regular object. However, I think it will be
1453 && (sec
->flags
& SEC_ALLOC
) != 0
1454 && (sec
->flags
& SEC_LOAD
) == 0
1457 newdyncommon
= TRUE
;
1459 newdyncommon
= FALSE
;
1463 && h
->root
.type
== bfd_link_hash_defined
1465 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1466 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1469 olddyncommon
= TRUE
;
1471 olddyncommon
= FALSE
;
1473 /* We now know everything about the old and new symbols. We ask the
1474 backend to check if we can merge them. */
1475 if (bed
->merge_symbol
!= NULL
)
1477 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1482 /* If both the old and the new symbols look like common symbols in a
1483 dynamic object, set the size of the symbol to the larger of the
1488 && sym
->st_size
!= h
->size
)
1490 /* Since we think we have two common symbols, issue a multiple
1491 common warning if desired. Note that we only warn if the
1492 size is different. If the size is the same, we simply let
1493 the old symbol override the new one as normally happens with
1494 symbols defined in dynamic objects. */
1496 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1497 bfd_link_hash_common
, sym
->st_size
);
1498 if (sym
->st_size
> h
->size
)
1499 h
->size
= sym
->st_size
;
1501 *size_change_ok
= TRUE
;
1504 /* If we are looking at a dynamic object, and we have found a
1505 definition, we need to see if the symbol was already defined by
1506 some other object. If so, we want to use the existing
1507 definition, and we do not want to report a multiple symbol
1508 definition error; we do this by clobbering *PSEC to be
1509 bfd_und_section_ptr.
1511 We treat a common symbol as a definition if the symbol in the
1512 shared library is a function, since common symbols always
1513 represent variables; this can cause confusion in principle, but
1514 any such confusion would seem to indicate an erroneous program or
1515 shared library. We also permit a common symbol in a regular
1516 object to override a weak symbol in a shared object. A common
1517 symbol in executable also overrides a symbol in a shared object. */
1522 || (h
->root
.type
== bfd_link_hash_common
1525 || (!olddyn
&& bfd_link_executable (info
))))))
1529 newdyncommon
= FALSE
;
1531 *psec
= sec
= bfd_und_section_ptr
;
1532 *size_change_ok
= TRUE
;
1534 /* If we get here when the old symbol is a common symbol, then
1535 we are explicitly letting it override a weak symbol or
1536 function in a dynamic object, and we don't want to warn about
1537 a type change. If the old symbol is a defined symbol, a type
1538 change warning may still be appropriate. */
1540 if (h
->root
.type
== bfd_link_hash_common
)
1541 *type_change_ok
= TRUE
;
1544 /* Handle the special case of an old common symbol merging with a
1545 new symbol which looks like a common symbol in a shared object.
1546 We change *PSEC and *PVALUE to make the new symbol look like a
1547 common symbol, and let _bfd_generic_link_add_one_symbol do the
1551 && h
->root
.type
== bfd_link_hash_common
)
1555 newdyncommon
= FALSE
;
1556 *pvalue
= sym
->st_size
;
1557 *psec
= sec
= bed
->common_section (oldsec
);
1558 *size_change_ok
= TRUE
;
1561 /* Skip weak definitions of symbols that are already defined. */
1562 if (newdef
&& olddef
&& newweak
)
1564 /* Don't skip new non-IR weak syms. */
1565 if (!(oldbfd
!= NULL
1566 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1567 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1573 /* Merge st_other. If the symbol already has a dynamic index,
1574 but visibility says it should not be visible, turn it into a
1576 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1577 if (h
->dynindx
!= -1)
1578 switch (ELF_ST_VISIBILITY (h
->other
))
1582 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1587 /* If the old symbol is from a dynamic object, and the new symbol is
1588 a definition which is not from a dynamic object, then the new
1589 symbol overrides the old symbol. Symbols from regular files
1590 always take precedence over symbols from dynamic objects, even if
1591 they are defined after the dynamic object in the link.
1593 As above, we again permit a common symbol in a regular object to
1594 override a definition in a shared object if the shared object
1595 symbol is a function or is weak. */
1600 || (bfd_is_com_section (sec
)
1601 && (oldweak
|| oldfunc
)))
1606 /* Change the hash table entry to undefined, and let
1607 _bfd_generic_link_add_one_symbol do the right thing with the
1610 h
->root
.type
= bfd_link_hash_undefined
;
1611 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1612 *size_change_ok
= TRUE
;
1615 olddyncommon
= FALSE
;
1617 /* We again permit a type change when a common symbol may be
1618 overriding a function. */
1620 if (bfd_is_com_section (sec
))
1624 /* If a common symbol overrides a function, make sure
1625 that it isn't defined dynamically nor has type
1628 h
->type
= STT_NOTYPE
;
1630 *type_change_ok
= TRUE
;
1633 if (hi
->root
.type
== bfd_link_hash_indirect
)
1636 /* This union may have been set to be non-NULL when this symbol
1637 was seen in a dynamic object. We must force the union to be
1638 NULL, so that it is correct for a regular symbol. */
1639 h
->verinfo
.vertree
= NULL
;
1642 /* Handle the special case of a new common symbol merging with an
1643 old symbol that looks like it might be a common symbol defined in
1644 a shared object. Note that we have already handled the case in
1645 which a new common symbol should simply override the definition
1646 in the shared library. */
1649 && bfd_is_com_section (sec
)
1652 /* It would be best if we could set the hash table entry to a
1653 common symbol, but we don't know what to use for the section
1654 or the alignment. */
1655 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1656 bfd_link_hash_common
, sym
->st_size
);
1658 /* If the presumed common symbol in the dynamic object is
1659 larger, pretend that the new symbol has its size. */
1661 if (h
->size
> *pvalue
)
1664 /* We need to remember the alignment required by the symbol
1665 in the dynamic object. */
1666 BFD_ASSERT (pold_alignment
);
1667 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1670 olddyncommon
= FALSE
;
1672 h
->root
.type
= bfd_link_hash_undefined
;
1673 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1675 *size_change_ok
= TRUE
;
1676 *type_change_ok
= TRUE
;
1678 if (hi
->root
.type
== bfd_link_hash_indirect
)
1681 h
->verinfo
.vertree
= NULL
;
1686 /* Handle the case where we had a versioned symbol in a dynamic
1687 library and now find a definition in a normal object. In this
1688 case, we make the versioned symbol point to the normal one. */
1689 flip
->root
.type
= h
->root
.type
;
1690 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1691 h
->root
.type
= bfd_link_hash_indirect
;
1692 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1693 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1697 flip
->ref_dynamic
= 1;
1704 /* This function is called to create an indirect symbol from the
1705 default for the symbol with the default version if needed. The
1706 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1707 set DYNSYM if the new indirect symbol is dynamic. */
1710 _bfd_elf_add_default_symbol (bfd
*abfd
,
1711 struct bfd_link_info
*info
,
1712 struct elf_link_hash_entry
*h
,
1714 Elf_Internal_Sym
*sym
,
1718 bfd_boolean
*dynsym
)
1720 bfd_boolean type_change_ok
;
1721 bfd_boolean size_change_ok
;
1724 struct elf_link_hash_entry
*hi
;
1725 struct bfd_link_hash_entry
*bh
;
1726 const struct elf_backend_data
*bed
;
1727 bfd_boolean collect
;
1728 bfd_boolean dynamic
;
1729 bfd_boolean override
;
1731 size_t len
, shortlen
;
1733 bfd_boolean matched
;
1735 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1738 /* If this symbol has a version, and it is the default version, we
1739 create an indirect symbol from the default name to the fully
1740 decorated name. This will cause external references which do not
1741 specify a version to be bound to this version of the symbol. */
1742 p
= strchr (name
, ELF_VER_CHR
);
1743 if (h
->versioned
== unknown
)
1747 h
->versioned
= unversioned
;
1752 if (p
[1] != ELF_VER_CHR
)
1754 h
->versioned
= versioned_hidden
;
1758 h
->versioned
= versioned
;
1763 /* PR ld/19073: We may see an unversioned definition after the
1769 bed
= get_elf_backend_data (abfd
);
1770 collect
= bed
->collect
;
1771 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1773 shortlen
= p
- name
;
1774 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1775 if (shortname
== NULL
)
1777 memcpy (shortname
, name
, shortlen
);
1778 shortname
[shortlen
] = '\0';
1780 /* We are going to create a new symbol. Merge it with any existing
1781 symbol with this name. For the purposes of the merge, act as
1782 though we were defining the symbol we just defined, although we
1783 actually going to define an indirect symbol. */
1784 type_change_ok
= FALSE
;
1785 size_change_ok
= FALSE
;
1788 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1789 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1790 &type_change_ok
, &size_change_ok
, &matched
))
1796 if (hi
->def_regular
)
1798 /* If the undecorated symbol will have a version added by a
1799 script different to H, then don't indirect to/from the
1800 undecorated symbol. This isn't ideal because we may not yet
1801 have seen symbol versions, if given by a script on the
1802 command line rather than via --version-script. */
1803 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1808 = bfd_find_version_for_sym (info
->version_info
,
1809 hi
->root
.root
.string
, &hide
);
1810 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1812 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1816 if (hi
->verinfo
.vertree
!= NULL
1817 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1823 /* Add the default symbol if not performing a relocatable link. */
1824 if (! bfd_link_relocatable (info
))
1827 if (! (_bfd_generic_link_add_one_symbol
1828 (info
, abfd
, shortname
, BSF_INDIRECT
,
1829 bfd_ind_section_ptr
,
1830 0, name
, FALSE
, collect
, &bh
)))
1832 hi
= (struct elf_link_hash_entry
*) bh
;
1837 /* In this case the symbol named SHORTNAME is overriding the
1838 indirect symbol we want to add. We were planning on making
1839 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1840 is the name without a version. NAME is the fully versioned
1841 name, and it is the default version.
1843 Overriding means that we already saw a definition for the
1844 symbol SHORTNAME in a regular object, and it is overriding
1845 the symbol defined in the dynamic object.
1847 When this happens, we actually want to change NAME, the
1848 symbol we just added, to refer to SHORTNAME. This will cause
1849 references to NAME in the shared object to become references
1850 to SHORTNAME in the regular object. This is what we expect
1851 when we override a function in a shared object: that the
1852 references in the shared object will be mapped to the
1853 definition in the regular object. */
1855 while (hi
->root
.type
== bfd_link_hash_indirect
1856 || hi
->root
.type
== bfd_link_hash_warning
)
1857 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1859 h
->root
.type
= bfd_link_hash_indirect
;
1860 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1864 hi
->ref_dynamic
= 1;
1868 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1873 /* Now set HI to H, so that the following code will set the
1874 other fields correctly. */
1878 /* Check if HI is a warning symbol. */
1879 if (hi
->root
.type
== bfd_link_hash_warning
)
1880 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1882 /* If there is a duplicate definition somewhere, then HI may not
1883 point to an indirect symbol. We will have reported an error to
1884 the user in that case. */
1886 if (hi
->root
.type
== bfd_link_hash_indirect
)
1888 struct elf_link_hash_entry
*ht
;
1890 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1891 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1893 /* A reference to the SHORTNAME symbol from a dynamic library
1894 will be satisfied by the versioned symbol at runtime. In
1895 effect, we have a reference to the versioned symbol. */
1896 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1897 hi
->dynamic_def
|= ht
->dynamic_def
;
1899 /* See if the new flags lead us to realize that the symbol must
1905 if (! bfd_link_executable (info
)
1912 if (hi
->ref_regular
)
1918 /* We also need to define an indirection from the nondefault version
1922 len
= strlen (name
);
1923 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1924 if (shortname
== NULL
)
1926 memcpy (shortname
, name
, shortlen
);
1927 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1929 /* Once again, merge with any existing symbol. */
1930 type_change_ok
= FALSE
;
1931 size_change_ok
= FALSE
;
1933 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1934 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1935 &type_change_ok
, &size_change_ok
, &matched
))
1943 /* Here SHORTNAME is a versioned name, so we don't expect to see
1944 the type of override we do in the case above unless it is
1945 overridden by a versioned definition. */
1946 if (hi
->root
.type
!= bfd_link_hash_defined
1947 && hi
->root
.type
!= bfd_link_hash_defweak
)
1949 /* xgettext:c-format */
1950 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1956 if (! (_bfd_generic_link_add_one_symbol
1957 (info
, abfd
, shortname
, BSF_INDIRECT
,
1958 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1960 hi
= (struct elf_link_hash_entry
*) bh
;
1962 /* If there is a duplicate definition somewhere, then HI may not
1963 point to an indirect symbol. We will have reported an error
1964 to the user in that case. */
1966 if (hi
->root
.type
== bfd_link_hash_indirect
)
1968 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1969 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1970 hi
->dynamic_def
|= h
->dynamic_def
;
1972 /* See if the new flags lead us to realize that the symbol
1978 if (! bfd_link_executable (info
)
1984 if (hi
->ref_regular
)
1994 /* This routine is used to export all defined symbols into the dynamic
1995 symbol table. It is called via elf_link_hash_traverse. */
1998 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2000 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2002 /* Ignore indirect symbols. These are added by the versioning code. */
2003 if (h
->root
.type
== bfd_link_hash_indirect
)
2006 /* Ignore this if we won't export it. */
2007 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2010 if (h
->dynindx
== -1
2011 && (h
->def_regular
|| h
->ref_regular
)
2012 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2013 h
->root
.root
.string
))
2015 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2025 /* Look through the symbols which are defined in other shared
2026 libraries and referenced here. Update the list of version
2027 dependencies. This will be put into the .gnu.version_r section.
2028 This function is called via elf_link_hash_traverse. */
2031 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2034 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2035 Elf_Internal_Verneed
*t
;
2036 Elf_Internal_Vernaux
*a
;
2039 /* We only care about symbols defined in shared objects with version
2044 || h
->verinfo
.verdef
== NULL
2045 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2046 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2049 /* See if we already know about this version. */
2050 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2054 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2057 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2058 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2064 /* This is a new version. Add it to tree we are building. */
2069 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2072 rinfo
->failed
= TRUE
;
2076 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2077 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2078 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2082 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2085 rinfo
->failed
= TRUE
;
2089 /* Note that we are copying a string pointer here, and testing it
2090 above. If bfd_elf_string_from_elf_section is ever changed to
2091 discard the string data when low in memory, this will have to be
2093 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2095 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2096 a
->vna_nextptr
= t
->vn_auxptr
;
2098 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2101 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2108 /* Figure out appropriate versions for all the symbols. We may not
2109 have the version number script until we have read all of the input
2110 files, so until that point we don't know which symbols should be
2111 local. This function is called via elf_link_hash_traverse. */
2114 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2116 struct elf_info_failed
*sinfo
;
2117 struct bfd_link_info
*info
;
2118 const struct elf_backend_data
*bed
;
2119 struct elf_info_failed eif
;
2122 sinfo
= (struct elf_info_failed
*) data
;
2125 /* Fix the symbol flags. */
2128 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2131 sinfo
->failed
= TRUE
;
2135 /* We only need version numbers for symbols defined in regular
2137 if (!h
->def_regular
)
2140 bed
= get_elf_backend_data (info
->output_bfd
);
2141 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2142 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2144 struct bfd_elf_version_tree
*t
;
2147 if (*p
== ELF_VER_CHR
)
2150 /* If there is no version string, we can just return out. */
2154 /* Look for the version. If we find it, it is no longer weak. */
2155 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2157 if (strcmp (t
->name
, p
) == 0)
2161 struct bfd_elf_version_expr
*d
;
2163 len
= p
- h
->root
.root
.string
;
2164 alc
= (char *) bfd_malloc (len
);
2167 sinfo
->failed
= TRUE
;
2170 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2171 alc
[len
- 1] = '\0';
2172 if (alc
[len
- 2] == ELF_VER_CHR
)
2173 alc
[len
- 2] = '\0';
2175 h
->verinfo
.vertree
= t
;
2179 if (t
->globals
.list
!= NULL
)
2180 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2182 /* See if there is anything to force this symbol to
2184 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2186 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2189 && ! info
->export_dynamic
)
2190 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2198 /* If we are building an application, we need to create a
2199 version node for this version. */
2200 if (t
== NULL
&& bfd_link_executable (info
))
2202 struct bfd_elf_version_tree
**pp
;
2205 /* If we aren't going to export this symbol, we don't need
2206 to worry about it. */
2207 if (h
->dynindx
== -1)
2210 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2214 sinfo
->failed
= TRUE
;
2219 t
->name_indx
= (unsigned int) -1;
2223 /* Don't count anonymous version tag. */
2224 if (sinfo
->info
->version_info
!= NULL
2225 && sinfo
->info
->version_info
->vernum
== 0)
2227 for (pp
= &sinfo
->info
->version_info
;
2231 t
->vernum
= version_index
;
2235 h
->verinfo
.vertree
= t
;
2239 /* We could not find the version for a symbol when
2240 generating a shared archive. Return an error. */
2242 /* xgettext:c-format */
2243 (_("%B: version node not found for symbol %s"),
2244 info
->output_bfd
, h
->root
.root
.string
);
2245 bfd_set_error (bfd_error_bad_value
);
2246 sinfo
->failed
= TRUE
;
2251 /* If we don't have a version for this symbol, see if we can find
2253 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2258 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2259 h
->root
.root
.string
, &hide
);
2260 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2261 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2267 /* Read and swap the relocs from the section indicated by SHDR. This
2268 may be either a REL or a RELA section. The relocations are
2269 translated into RELA relocations and stored in INTERNAL_RELOCS,
2270 which should have already been allocated to contain enough space.
2271 The EXTERNAL_RELOCS are a buffer where the external form of the
2272 relocations should be stored.
2274 Returns FALSE if something goes wrong. */
2277 elf_link_read_relocs_from_section (bfd
*abfd
,
2279 Elf_Internal_Shdr
*shdr
,
2280 void *external_relocs
,
2281 Elf_Internal_Rela
*internal_relocs
)
2283 const struct elf_backend_data
*bed
;
2284 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2285 const bfd_byte
*erela
;
2286 const bfd_byte
*erelaend
;
2287 Elf_Internal_Rela
*irela
;
2288 Elf_Internal_Shdr
*symtab_hdr
;
2291 /* Position ourselves at the start of the section. */
2292 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2295 /* Read the relocations. */
2296 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2299 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2300 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2302 bed
= get_elf_backend_data (abfd
);
2304 /* Convert the external relocations to the internal format. */
2305 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2306 swap_in
= bed
->s
->swap_reloc_in
;
2307 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2308 swap_in
= bed
->s
->swap_reloca_in
;
2311 bfd_set_error (bfd_error_wrong_format
);
2315 erela
= (const bfd_byte
*) external_relocs
;
2316 erelaend
= erela
+ shdr
->sh_size
;
2317 irela
= internal_relocs
;
2318 while (erela
< erelaend
)
2322 (*swap_in
) (abfd
, erela
, irela
);
2323 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2324 if (bed
->s
->arch_size
== 64)
2328 if ((size_t) r_symndx
>= nsyms
)
2331 /* xgettext:c-format */
2332 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2333 " for offset 0x%lx in section `%A'"),
2335 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2336 bfd_set_error (bfd_error_bad_value
);
2340 else if (r_symndx
!= STN_UNDEF
)
2343 /* xgettext:c-format */
2344 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2345 " when the object file has no symbol table"),
2347 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2348 bfd_set_error (bfd_error_bad_value
);
2351 irela
+= bed
->s
->int_rels_per_ext_rel
;
2352 erela
+= shdr
->sh_entsize
;
2358 /* Read and swap the relocs for a section O. They may have been
2359 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2360 not NULL, they are used as buffers to read into. They are known to
2361 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2362 the return value is allocated using either malloc or bfd_alloc,
2363 according to the KEEP_MEMORY argument. If O has two relocation
2364 sections (both REL and RELA relocations), then the REL_HDR
2365 relocations will appear first in INTERNAL_RELOCS, followed by the
2366 RELA_HDR relocations. */
2369 _bfd_elf_link_read_relocs (bfd
*abfd
,
2371 void *external_relocs
,
2372 Elf_Internal_Rela
*internal_relocs
,
2373 bfd_boolean keep_memory
)
2375 void *alloc1
= NULL
;
2376 Elf_Internal_Rela
*alloc2
= NULL
;
2377 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2378 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2379 Elf_Internal_Rela
*internal_rela_relocs
;
2381 if (esdo
->relocs
!= NULL
)
2382 return esdo
->relocs
;
2384 if (o
->reloc_count
== 0)
2387 if (internal_relocs
== NULL
)
2391 size
= o
->reloc_count
;
2392 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2394 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2396 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2397 if (internal_relocs
== NULL
)
2401 if (external_relocs
== NULL
)
2403 bfd_size_type size
= 0;
2406 size
+= esdo
->rel
.hdr
->sh_size
;
2408 size
+= esdo
->rela
.hdr
->sh_size
;
2410 alloc1
= bfd_malloc (size
);
2413 external_relocs
= alloc1
;
2416 internal_rela_relocs
= internal_relocs
;
2419 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2423 external_relocs
= (((bfd_byte
*) external_relocs
)
2424 + esdo
->rel
.hdr
->sh_size
);
2425 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2426 * bed
->s
->int_rels_per_ext_rel
);
2430 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2432 internal_rela_relocs
)))
2435 /* Cache the results for next time, if we can. */
2437 esdo
->relocs
= internal_relocs
;
2442 /* Don't free alloc2, since if it was allocated we are passing it
2443 back (under the name of internal_relocs). */
2445 return internal_relocs
;
2453 bfd_release (abfd
, alloc2
);
2460 /* Compute the size of, and allocate space for, REL_HDR which is the
2461 section header for a section containing relocations for O. */
2464 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2465 struct bfd_elf_section_reloc_data
*reldata
)
2467 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2469 /* That allows us to calculate the size of the section. */
2470 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2472 /* The contents field must last into write_object_contents, so we
2473 allocate it with bfd_alloc rather than malloc. Also since we
2474 cannot be sure that the contents will actually be filled in,
2475 we zero the allocated space. */
2476 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2477 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2480 if (reldata
->hashes
== NULL
&& reldata
->count
)
2482 struct elf_link_hash_entry
**p
;
2484 p
= ((struct elf_link_hash_entry
**)
2485 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2489 reldata
->hashes
= p
;
2495 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2496 originated from the section given by INPUT_REL_HDR) to the
2500 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2501 asection
*input_section
,
2502 Elf_Internal_Shdr
*input_rel_hdr
,
2503 Elf_Internal_Rela
*internal_relocs
,
2504 struct elf_link_hash_entry
**rel_hash
2507 Elf_Internal_Rela
*irela
;
2508 Elf_Internal_Rela
*irelaend
;
2510 struct bfd_elf_section_reloc_data
*output_reldata
;
2511 asection
*output_section
;
2512 const struct elf_backend_data
*bed
;
2513 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2514 struct bfd_elf_section_data
*esdo
;
2516 output_section
= input_section
->output_section
;
2518 bed
= get_elf_backend_data (output_bfd
);
2519 esdo
= elf_section_data (output_section
);
2520 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2522 output_reldata
= &esdo
->rel
;
2523 swap_out
= bed
->s
->swap_reloc_out
;
2525 else if (esdo
->rela
.hdr
2526 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2528 output_reldata
= &esdo
->rela
;
2529 swap_out
= bed
->s
->swap_reloca_out
;
2534 /* xgettext:c-format */
2535 (_("%B: relocation size mismatch in %B section %A"),
2536 output_bfd
, input_section
->owner
, input_section
);
2537 bfd_set_error (bfd_error_wrong_format
);
2541 erel
= output_reldata
->hdr
->contents
;
2542 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2543 irela
= internal_relocs
;
2544 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2545 * bed
->s
->int_rels_per_ext_rel
);
2546 while (irela
< irelaend
)
2548 (*swap_out
) (output_bfd
, irela
, erel
);
2549 irela
+= bed
->s
->int_rels_per_ext_rel
;
2550 erel
+= input_rel_hdr
->sh_entsize
;
2553 /* Bump the counter, so that we know where to add the next set of
2555 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2560 /* Make weak undefined symbols in PIE dynamic. */
2563 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2564 struct elf_link_hash_entry
*h
)
2566 if (bfd_link_pie (info
)
2568 && h
->root
.type
== bfd_link_hash_undefweak
)
2569 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2574 /* Fix up the flags for a symbol. This handles various cases which
2575 can only be fixed after all the input files are seen. This is
2576 currently called by both adjust_dynamic_symbol and
2577 assign_sym_version, which is unnecessary but perhaps more robust in
2578 the face of future changes. */
2581 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2582 struct elf_info_failed
*eif
)
2584 const struct elf_backend_data
*bed
;
2586 /* If this symbol was mentioned in a non-ELF file, try to set
2587 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2588 permit a non-ELF file to correctly refer to a symbol defined in
2589 an ELF dynamic object. */
2592 while (h
->root
.type
== bfd_link_hash_indirect
)
2593 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2595 if (h
->root
.type
!= bfd_link_hash_defined
2596 && h
->root
.type
!= bfd_link_hash_defweak
)
2599 h
->ref_regular_nonweak
= 1;
2603 if (h
->root
.u
.def
.section
->owner
!= NULL
2604 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2605 == bfd_target_elf_flavour
))
2608 h
->ref_regular_nonweak
= 1;
2614 if (h
->dynindx
== -1
2618 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2627 /* Unfortunately, NON_ELF is only correct if the symbol
2628 was first seen in a non-ELF file. Fortunately, if the symbol
2629 was first seen in an ELF file, we're probably OK unless the
2630 symbol was defined in a non-ELF file. Catch that case here.
2631 FIXME: We're still in trouble if the symbol was first seen in
2632 a dynamic object, and then later in a non-ELF regular object. */
2633 if ((h
->root
.type
== bfd_link_hash_defined
2634 || h
->root
.type
== bfd_link_hash_defweak
)
2636 && (h
->root
.u
.def
.section
->owner
!= NULL
2637 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2638 != bfd_target_elf_flavour
)
2639 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2640 && !h
->def_dynamic
)))
2644 /* Backend specific symbol fixup. */
2645 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2646 if (bed
->elf_backend_fixup_symbol
2647 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2650 /* If this is a final link, and the symbol was defined as a common
2651 symbol in a regular object file, and there was no definition in
2652 any dynamic object, then the linker will have allocated space for
2653 the symbol in a common section but the DEF_REGULAR
2654 flag will not have been set. */
2655 if (h
->root
.type
== bfd_link_hash_defined
2659 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2662 /* If a weak undefined symbol has non-default visibility, we also
2663 hide it from the dynamic linker. */
2664 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2665 && h
->root
.type
== bfd_link_hash_undefweak
)
2666 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2668 /* A hidden versioned symbol in executable should be forced local if
2669 it is is locally defined, not referenced by shared library and not
2671 else if (bfd_link_executable (eif
->info
)
2672 && h
->versioned
== versioned_hidden
2673 && !eif
->info
->export_dynamic
2677 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2679 /* If -Bsymbolic was used (which means to bind references to global
2680 symbols to the definition within the shared object), and this
2681 symbol was defined in a regular object, then it actually doesn't
2682 need a PLT entry. Likewise, if the symbol has non-default
2683 visibility. If the symbol has hidden or internal visibility, we
2684 will force it local. */
2685 else if (h
->needs_plt
2686 && bfd_link_pic (eif
->info
)
2687 && is_elf_hash_table (eif
->info
->hash
)
2688 && (SYMBOLIC_BIND (eif
->info
, h
)
2689 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2692 bfd_boolean force_local
;
2694 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2695 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2696 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2699 /* If this is a weak defined symbol in a dynamic object, and we know
2700 the real definition in the dynamic object, copy interesting flags
2701 over to the real definition. */
2702 if (h
->u
.weakdef
!= NULL
)
2704 /* If the real definition is defined by a regular object file,
2705 don't do anything special. See the longer description in
2706 _bfd_elf_adjust_dynamic_symbol, below. */
2707 if (h
->u
.weakdef
->def_regular
)
2708 h
->u
.weakdef
= NULL
;
2711 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2713 while (h
->root
.type
== bfd_link_hash_indirect
)
2714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2716 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2717 || h
->root
.type
== bfd_link_hash_defweak
);
2718 BFD_ASSERT (weakdef
->def_dynamic
);
2719 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2720 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2721 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2728 /* Make the backend pick a good value for a dynamic symbol. This is
2729 called via elf_link_hash_traverse, and also calls itself
2733 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2735 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2737 const struct elf_backend_data
*bed
;
2739 if (! is_elf_hash_table (eif
->info
->hash
))
2742 /* Ignore indirect symbols. These are added by the versioning code. */
2743 if (h
->root
.type
== bfd_link_hash_indirect
)
2746 /* Fix the symbol flags. */
2747 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2750 /* If this symbol does not require a PLT entry, and it is not
2751 defined by a dynamic object, or is not referenced by a regular
2752 object, ignore it. We do have to handle a weak defined symbol,
2753 even if no regular object refers to it, if we decided to add it
2754 to the dynamic symbol table. FIXME: Do we normally need to worry
2755 about symbols which are defined by one dynamic object and
2756 referenced by another one? */
2758 && h
->type
!= STT_GNU_IFUNC
2762 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2764 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2768 /* If we've already adjusted this symbol, don't do it again. This
2769 can happen via a recursive call. */
2770 if (h
->dynamic_adjusted
)
2773 /* Don't look at this symbol again. Note that we must set this
2774 after checking the above conditions, because we may look at a
2775 symbol once, decide not to do anything, and then get called
2776 recursively later after REF_REGULAR is set below. */
2777 h
->dynamic_adjusted
= 1;
2779 /* If this is a weak definition, and we know a real definition, and
2780 the real symbol is not itself defined by a regular object file,
2781 then get a good value for the real definition. We handle the
2782 real symbol first, for the convenience of the backend routine.
2784 Note that there is a confusing case here. If the real definition
2785 is defined by a regular object file, we don't get the real symbol
2786 from the dynamic object, but we do get the weak symbol. If the
2787 processor backend uses a COPY reloc, then if some routine in the
2788 dynamic object changes the real symbol, we will not see that
2789 change in the corresponding weak symbol. This is the way other
2790 ELF linkers work as well, and seems to be a result of the shared
2793 I will clarify this issue. Most SVR4 shared libraries define the
2794 variable _timezone and define timezone as a weak synonym. The
2795 tzset call changes _timezone. If you write
2796 extern int timezone;
2798 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2799 you might expect that, since timezone is a synonym for _timezone,
2800 the same number will print both times. However, if the processor
2801 backend uses a COPY reloc, then actually timezone will be copied
2802 into your process image, and, since you define _timezone
2803 yourself, _timezone will not. Thus timezone and _timezone will
2804 wind up at different memory locations. The tzset call will set
2805 _timezone, leaving timezone unchanged. */
2807 if (h
->u
.weakdef
!= NULL
)
2809 /* If we get to this point, there is an implicit reference to
2810 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2811 h
->u
.weakdef
->ref_regular
= 1;
2813 /* Ensure that the backend adjust_dynamic_symbol function sees
2814 H->U.WEAKDEF before H by recursively calling ourselves. */
2815 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2819 /* If a symbol has no type and no size and does not require a PLT
2820 entry, then we are probably about to do the wrong thing here: we
2821 are probably going to create a COPY reloc for an empty object.
2822 This case can arise when a shared object is built with assembly
2823 code, and the assembly code fails to set the symbol type. */
2825 && h
->type
== STT_NOTYPE
2828 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2829 h
->root
.root
.string
);
2831 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2832 bed
= get_elf_backend_data (dynobj
);
2834 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2843 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2847 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2848 struct elf_link_hash_entry
*h
,
2851 unsigned int power_of_two
;
2853 asection
*sec
= h
->root
.u
.def
.section
;
2855 /* The section aligment of definition is the maximum alignment
2856 requirement of symbols defined in the section. Since we don't
2857 know the symbol alignment requirement, we start with the
2858 maximum alignment and check low bits of the symbol address
2859 for the minimum alignment. */
2860 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2861 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2862 while ((h
->root
.u
.def
.value
& mask
) != 0)
2868 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2871 /* Adjust the section alignment if needed. */
2872 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2877 /* We make sure that the symbol will be aligned properly. */
2878 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2880 /* Define the symbol as being at this point in DYNBSS. */
2881 h
->root
.u
.def
.section
= dynbss
;
2882 h
->root
.u
.def
.value
= dynbss
->size
;
2884 /* Increment the size of DYNBSS to make room for the symbol. */
2885 dynbss
->size
+= h
->size
;
2887 /* No error if extern_protected_data is true. */
2888 if (h
->protected_def
2889 && (!info
->extern_protected_data
2890 || (info
->extern_protected_data
< 0
2891 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2892 info
->callbacks
->einfo
2893 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2894 h
->root
.root
.string
);
2899 /* Adjust all external symbols pointing into SEC_MERGE sections
2900 to reflect the object merging within the sections. */
2903 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2907 if ((h
->root
.type
== bfd_link_hash_defined
2908 || h
->root
.type
== bfd_link_hash_defweak
)
2909 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2910 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2912 bfd
*output_bfd
= (bfd
*) data
;
2914 h
->root
.u
.def
.value
=
2915 _bfd_merged_section_offset (output_bfd
,
2916 &h
->root
.u
.def
.section
,
2917 elf_section_data (sec
)->sec_info
,
2918 h
->root
.u
.def
.value
);
2924 /* Returns false if the symbol referred to by H should be considered
2925 to resolve local to the current module, and true if it should be
2926 considered to bind dynamically. */
2929 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2930 struct bfd_link_info
*info
,
2931 bfd_boolean not_local_protected
)
2933 bfd_boolean binding_stays_local_p
;
2934 const struct elf_backend_data
*bed
;
2935 struct elf_link_hash_table
*hash_table
;
2940 while (h
->root
.type
== bfd_link_hash_indirect
2941 || h
->root
.type
== bfd_link_hash_warning
)
2942 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2944 /* If it was forced local, then clearly it's not dynamic. */
2945 if (h
->dynindx
== -1)
2947 if (h
->forced_local
)
2950 /* Identify the cases where name binding rules say that a
2951 visible symbol resolves locally. */
2952 binding_stays_local_p
= (bfd_link_executable (info
)
2953 || SYMBOLIC_BIND (info
, h
));
2955 switch (ELF_ST_VISIBILITY (h
->other
))
2962 hash_table
= elf_hash_table (info
);
2963 if (!is_elf_hash_table (hash_table
))
2966 bed
= get_elf_backend_data (hash_table
->dynobj
);
2968 /* Proper resolution for function pointer equality may require
2969 that these symbols perhaps be resolved dynamically, even though
2970 we should be resolving them to the current module. */
2971 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2972 binding_stays_local_p
= TRUE
;
2979 /* If it isn't defined locally, then clearly it's dynamic. */
2980 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2983 /* Otherwise, the symbol is dynamic if binding rules don't tell
2984 us that it remains local. */
2985 return !binding_stays_local_p
;
2988 /* Return true if the symbol referred to by H should be considered
2989 to resolve local to the current module, and false otherwise. Differs
2990 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2991 undefined symbols. The two functions are virtually identical except
2992 for the place where forced_local and dynindx == -1 are tested. If
2993 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2994 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2995 the symbol is local only for defined symbols.
2996 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2997 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2998 treatment of undefined weak symbols. For those that do not make
2999 undefined weak symbols dynamic, both functions may return false. */
3002 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3003 struct bfd_link_info
*info
,
3004 bfd_boolean local_protected
)
3006 const struct elf_backend_data
*bed
;
3007 struct elf_link_hash_table
*hash_table
;
3009 /* If it's a local sym, of course we resolve locally. */
3013 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3014 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3015 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3018 /* Common symbols that become definitions don't get the DEF_REGULAR
3019 flag set, so test it first, and don't bail out. */
3020 if (ELF_COMMON_DEF_P (h
))
3022 /* If we don't have a definition in a regular file, then we can't
3023 resolve locally. The sym is either undefined or dynamic. */
3024 else if (!h
->def_regular
)
3027 /* Forced local symbols resolve locally. */
3028 if (h
->forced_local
)
3031 /* As do non-dynamic symbols. */
3032 if (h
->dynindx
== -1)
3035 /* At this point, we know the symbol is defined and dynamic. In an
3036 executable it must resolve locally, likewise when building symbolic
3037 shared libraries. */
3038 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3041 /* Now deal with defined dynamic symbols in shared libraries. Ones
3042 with default visibility might not resolve locally. */
3043 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3046 hash_table
= elf_hash_table (info
);
3047 if (!is_elf_hash_table (hash_table
))
3050 bed
= get_elf_backend_data (hash_table
->dynobj
);
3052 /* If extern_protected_data is false, STV_PROTECTED non-function
3053 symbols are local. */
3054 if ((!info
->extern_protected_data
3055 || (info
->extern_protected_data
< 0
3056 && !bed
->extern_protected_data
))
3057 && !bed
->is_function_type (h
->type
))
3060 /* Function pointer equality tests may require that STV_PROTECTED
3061 symbols be treated as dynamic symbols. If the address of a
3062 function not defined in an executable is set to that function's
3063 plt entry in the executable, then the address of the function in
3064 a shared library must also be the plt entry in the executable. */
3065 return local_protected
;
3068 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3069 aligned. Returns the first TLS output section. */
3071 struct bfd_section
*
3072 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3074 struct bfd_section
*sec
, *tls
;
3075 unsigned int align
= 0;
3077 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3078 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3082 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3083 if (sec
->alignment_power
> align
)
3084 align
= sec
->alignment_power
;
3086 elf_hash_table (info
)->tls_sec
= tls
;
3088 /* Ensure the alignment of the first section is the largest alignment,
3089 so that the tls segment starts aligned. */
3091 tls
->alignment_power
= align
;
3096 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3098 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3099 Elf_Internal_Sym
*sym
)
3101 const struct elf_backend_data
*bed
;
3103 /* Local symbols do not count, but target specific ones might. */
3104 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3105 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3108 bed
= get_elf_backend_data (abfd
);
3109 /* Function symbols do not count. */
3110 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3113 /* If the section is undefined, then so is the symbol. */
3114 if (sym
->st_shndx
== SHN_UNDEF
)
3117 /* If the symbol is defined in the common section, then
3118 it is a common definition and so does not count. */
3119 if (bed
->common_definition (sym
))
3122 /* If the symbol is in a target specific section then we
3123 must rely upon the backend to tell us what it is. */
3124 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3125 /* FIXME - this function is not coded yet:
3127 return _bfd_is_global_symbol_definition (abfd, sym);
3129 Instead for now assume that the definition is not global,
3130 Even if this is wrong, at least the linker will behave
3131 in the same way that it used to do. */
3137 /* Search the symbol table of the archive element of the archive ABFD
3138 whose archive map contains a mention of SYMDEF, and determine if
3139 the symbol is defined in this element. */
3141 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3143 Elf_Internal_Shdr
* hdr
;
3147 Elf_Internal_Sym
*isymbuf
;
3148 Elf_Internal_Sym
*isym
;
3149 Elf_Internal_Sym
*isymend
;
3152 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3156 if (! bfd_check_format (abfd
, bfd_object
))
3159 /* Select the appropriate symbol table. If we don't know if the
3160 object file is an IR object, give linker LTO plugin a chance to
3161 get the correct symbol table. */
3162 if (abfd
->plugin_format
== bfd_plugin_yes
3163 #if BFD_SUPPORTS_PLUGINS
3164 || (abfd
->plugin_format
== bfd_plugin_unknown
3165 && bfd_link_plugin_object_p (abfd
))
3169 /* Use the IR symbol table if the object has been claimed by
3171 abfd
= abfd
->plugin_dummy_bfd
;
3172 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3174 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3175 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3177 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3179 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3181 /* The sh_info field of the symtab header tells us where the
3182 external symbols start. We don't care about the local symbols. */
3183 if (elf_bad_symtab (abfd
))
3185 extsymcount
= symcount
;
3190 extsymcount
= symcount
- hdr
->sh_info
;
3191 extsymoff
= hdr
->sh_info
;
3194 if (extsymcount
== 0)
3197 /* Read in the symbol table. */
3198 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3200 if (isymbuf
== NULL
)
3203 /* Scan the symbol table looking for SYMDEF. */
3205 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3209 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3214 if (strcmp (name
, symdef
->name
) == 0)
3216 result
= is_global_data_symbol_definition (abfd
, isym
);
3226 /* Add an entry to the .dynamic table. */
3229 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3233 struct elf_link_hash_table
*hash_table
;
3234 const struct elf_backend_data
*bed
;
3236 bfd_size_type newsize
;
3237 bfd_byte
*newcontents
;
3238 Elf_Internal_Dyn dyn
;
3240 hash_table
= elf_hash_table (info
);
3241 if (! is_elf_hash_table (hash_table
))
3244 bed
= get_elf_backend_data (hash_table
->dynobj
);
3245 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3246 BFD_ASSERT (s
!= NULL
);
3248 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3249 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3250 if (newcontents
== NULL
)
3254 dyn
.d_un
.d_val
= val
;
3255 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3258 s
->contents
= newcontents
;
3263 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3264 otherwise just check whether one already exists. Returns -1 on error,
3265 1 if a DT_NEEDED tag already exists, and 0 on success. */
3268 elf_add_dt_needed_tag (bfd
*abfd
,
3269 struct bfd_link_info
*info
,
3273 struct elf_link_hash_table
*hash_table
;
3276 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3279 hash_table
= elf_hash_table (info
);
3280 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3281 if (strindex
== (size_t) -1)
3284 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3287 const struct elf_backend_data
*bed
;
3290 bed
= get_elf_backend_data (hash_table
->dynobj
);
3291 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3293 for (extdyn
= sdyn
->contents
;
3294 extdyn
< sdyn
->contents
+ sdyn
->size
;
3295 extdyn
+= bed
->s
->sizeof_dyn
)
3297 Elf_Internal_Dyn dyn
;
3299 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3300 if (dyn
.d_tag
== DT_NEEDED
3301 && dyn
.d_un
.d_val
== strindex
)
3303 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3311 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3314 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3318 /* We were just checking for existence of the tag. */
3319 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3324 /* Return true if SONAME is on the needed list between NEEDED and STOP
3325 (or the end of list if STOP is NULL), and needed by a library that
3329 on_needed_list (const char *soname
,
3330 struct bfd_link_needed_list
*needed
,
3331 struct bfd_link_needed_list
*stop
)
3333 struct bfd_link_needed_list
*look
;
3334 for (look
= needed
; look
!= stop
; look
= look
->next
)
3335 if (strcmp (soname
, look
->name
) == 0
3336 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3337 /* If needed by a library that itself is not directly
3338 needed, recursively check whether that library is
3339 indirectly needed. Since we add DT_NEEDED entries to
3340 the end of the list, library dependencies appear after
3341 the library. Therefore search prior to the current
3342 LOOK, preventing possible infinite recursion. */
3343 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3349 /* Sort symbol by value, section, and size. */
3351 elf_sort_symbol (const void *arg1
, const void *arg2
)
3353 const struct elf_link_hash_entry
*h1
;
3354 const struct elf_link_hash_entry
*h2
;
3355 bfd_signed_vma vdiff
;
3357 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3358 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3359 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3361 return vdiff
> 0 ? 1 : -1;
3364 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3366 return sdiff
> 0 ? 1 : -1;
3368 vdiff
= h1
->size
- h2
->size
;
3369 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3372 /* This function is used to adjust offsets into .dynstr for
3373 dynamic symbols. This is called via elf_link_hash_traverse. */
3376 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3378 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3380 if (h
->dynindx
!= -1)
3381 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3385 /* Assign string offsets in .dynstr, update all structures referencing
3389 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3391 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3392 struct elf_link_local_dynamic_entry
*entry
;
3393 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3394 bfd
*dynobj
= hash_table
->dynobj
;
3397 const struct elf_backend_data
*bed
;
3400 _bfd_elf_strtab_finalize (dynstr
);
3401 size
= _bfd_elf_strtab_size (dynstr
);
3403 bed
= get_elf_backend_data (dynobj
);
3404 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3405 BFD_ASSERT (sdyn
!= NULL
);
3407 /* Update all .dynamic entries referencing .dynstr strings. */
3408 for (extdyn
= sdyn
->contents
;
3409 extdyn
< sdyn
->contents
+ sdyn
->size
;
3410 extdyn
+= bed
->s
->sizeof_dyn
)
3412 Elf_Internal_Dyn dyn
;
3414 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3418 dyn
.d_un
.d_val
= size
;
3428 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3433 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3436 /* Now update local dynamic symbols. */
3437 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3438 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3439 entry
->isym
.st_name
);
3441 /* And the rest of dynamic symbols. */
3442 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3444 /* Adjust version definitions. */
3445 if (elf_tdata (output_bfd
)->cverdefs
)
3450 Elf_Internal_Verdef def
;
3451 Elf_Internal_Verdaux defaux
;
3453 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3457 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3459 p
+= sizeof (Elf_External_Verdef
);
3460 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3462 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3464 _bfd_elf_swap_verdaux_in (output_bfd
,
3465 (Elf_External_Verdaux
*) p
, &defaux
);
3466 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3468 _bfd_elf_swap_verdaux_out (output_bfd
,
3469 &defaux
, (Elf_External_Verdaux
*) p
);
3470 p
+= sizeof (Elf_External_Verdaux
);
3473 while (def
.vd_next
);
3476 /* Adjust version references. */
3477 if (elf_tdata (output_bfd
)->verref
)
3482 Elf_Internal_Verneed need
;
3483 Elf_Internal_Vernaux needaux
;
3485 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3489 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3491 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3492 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3493 (Elf_External_Verneed
*) p
);
3494 p
+= sizeof (Elf_External_Verneed
);
3495 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3497 _bfd_elf_swap_vernaux_in (output_bfd
,
3498 (Elf_External_Vernaux
*) p
, &needaux
);
3499 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3501 _bfd_elf_swap_vernaux_out (output_bfd
,
3503 (Elf_External_Vernaux
*) p
);
3504 p
+= sizeof (Elf_External_Vernaux
);
3507 while (need
.vn_next
);
3513 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3514 The default is to only match when the INPUT and OUTPUT are exactly
3518 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3519 const bfd_target
*output
)
3521 return input
== output
;
3524 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3525 This version is used when different targets for the same architecture
3526 are virtually identical. */
3529 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3530 const bfd_target
*output
)
3532 const struct elf_backend_data
*obed
, *ibed
;
3534 if (input
== output
)
3537 ibed
= xvec_get_elf_backend_data (input
);
3538 obed
= xvec_get_elf_backend_data (output
);
3540 if (ibed
->arch
!= obed
->arch
)
3543 /* If both backends are using this function, deem them compatible. */
3544 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3547 /* Make a special call to the linker "notice" function to tell it that
3548 we are about to handle an as-needed lib, or have finished
3549 processing the lib. */
3552 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3553 struct bfd_link_info
*info
,
3554 enum notice_asneeded_action act
)
3556 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3559 /* Check relocations an ELF object file. */
3562 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3564 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3565 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3567 /* If this object is the same format as the output object, and it is
3568 not a shared library, then let the backend look through the
3571 This is required to build global offset table entries and to
3572 arrange for dynamic relocs. It is not required for the
3573 particular common case of linking non PIC code, even when linking
3574 against shared libraries, but unfortunately there is no way of
3575 knowing whether an object file has been compiled PIC or not.
3576 Looking through the relocs is not particularly time consuming.
3577 The problem is that we must either (1) keep the relocs in memory,
3578 which causes the linker to require additional runtime memory or
3579 (2) read the relocs twice from the input file, which wastes time.
3580 This would be a good case for using mmap.
3582 I have no idea how to handle linking PIC code into a file of a
3583 different format. It probably can't be done. */
3584 if ((abfd
->flags
& DYNAMIC
) == 0
3585 && is_elf_hash_table (htab
)
3586 && bed
->check_relocs
!= NULL
3587 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3588 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3592 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3594 Elf_Internal_Rela
*internal_relocs
;
3597 /* Don't check relocations in excluded sections. */
3598 if ((o
->flags
& SEC_RELOC
) == 0
3599 || (o
->flags
& SEC_EXCLUDE
) != 0
3600 || o
->reloc_count
== 0
3601 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3602 && (o
->flags
& SEC_DEBUGGING
) != 0)
3603 || bfd_is_abs_section (o
->output_section
))
3606 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3608 if (internal_relocs
== NULL
)
3611 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3613 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3614 free (internal_relocs
);
3624 /* Add symbols from an ELF object file to the linker hash table. */
3627 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3629 Elf_Internal_Ehdr
*ehdr
;
3630 Elf_Internal_Shdr
*hdr
;
3634 struct elf_link_hash_entry
**sym_hash
;
3635 bfd_boolean dynamic
;
3636 Elf_External_Versym
*extversym
= NULL
;
3637 Elf_External_Versym
*ever
;
3638 struct elf_link_hash_entry
*weaks
;
3639 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3640 size_t nondeflt_vers_cnt
= 0;
3641 Elf_Internal_Sym
*isymbuf
= NULL
;
3642 Elf_Internal_Sym
*isym
;
3643 Elf_Internal_Sym
*isymend
;
3644 const struct elf_backend_data
*bed
;
3645 bfd_boolean add_needed
;
3646 struct elf_link_hash_table
*htab
;
3648 void *alloc_mark
= NULL
;
3649 struct bfd_hash_entry
**old_table
= NULL
;
3650 unsigned int old_size
= 0;
3651 unsigned int old_count
= 0;
3652 void *old_tab
= NULL
;
3654 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3655 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3656 void *old_strtab
= NULL
;
3659 bfd_boolean just_syms
;
3661 htab
= elf_hash_table (info
);
3662 bed
= get_elf_backend_data (abfd
);
3664 if ((abfd
->flags
& DYNAMIC
) == 0)
3670 /* You can't use -r against a dynamic object. Also, there's no
3671 hope of using a dynamic object which does not exactly match
3672 the format of the output file. */
3673 if (bfd_link_relocatable (info
)
3674 || !is_elf_hash_table (htab
)
3675 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3677 if (bfd_link_relocatable (info
))
3678 bfd_set_error (bfd_error_invalid_operation
);
3680 bfd_set_error (bfd_error_wrong_format
);
3685 ehdr
= elf_elfheader (abfd
);
3686 if (info
->warn_alternate_em
3687 && bed
->elf_machine_code
!= ehdr
->e_machine
3688 && ((bed
->elf_machine_alt1
!= 0
3689 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3690 || (bed
->elf_machine_alt2
!= 0
3691 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3692 info
->callbacks
->einfo
3693 /* xgettext:c-format */
3694 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3695 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3697 /* As a GNU extension, any input sections which are named
3698 .gnu.warning.SYMBOL are treated as warning symbols for the given
3699 symbol. This differs from .gnu.warning sections, which generate
3700 warnings when they are included in an output file. */
3701 /* PR 12761: Also generate this warning when building shared libraries. */
3702 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3706 name
= bfd_get_section_name (abfd
, s
);
3707 if (CONST_STRNEQ (name
, ".gnu.warning."))
3712 name
+= sizeof ".gnu.warning." - 1;
3714 /* If this is a shared object, then look up the symbol
3715 in the hash table. If it is there, and it is already
3716 been defined, then we will not be using the entry
3717 from this shared object, so we don't need to warn.
3718 FIXME: If we see the definition in a regular object
3719 later on, we will warn, but we shouldn't. The only
3720 fix is to keep track of what warnings we are supposed
3721 to emit, and then handle them all at the end of the
3725 struct elf_link_hash_entry
*h
;
3727 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3729 /* FIXME: What about bfd_link_hash_common? */
3731 && (h
->root
.type
== bfd_link_hash_defined
3732 || h
->root
.type
== bfd_link_hash_defweak
))
3737 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3741 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3746 if (! (_bfd_generic_link_add_one_symbol
3747 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3748 FALSE
, bed
->collect
, NULL
)))
3751 if (bfd_link_executable (info
))
3753 /* Clobber the section size so that the warning does
3754 not get copied into the output file. */
3757 /* Also set SEC_EXCLUDE, so that symbols defined in
3758 the warning section don't get copied to the output. */
3759 s
->flags
|= SEC_EXCLUDE
;
3764 just_syms
= ((s
= abfd
->sections
) != NULL
3765 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3770 /* If we are creating a shared library, create all the dynamic
3771 sections immediately. We need to attach them to something,
3772 so we attach them to this BFD, provided it is the right
3773 format and is not from ld --just-symbols. Always create the
3774 dynamic sections for -E/--dynamic-list. FIXME: If there
3775 are no input BFD's of the same format as the output, we can't
3776 make a shared library. */
3778 && (bfd_link_pic (info
)
3779 || (!bfd_link_relocatable (info
)
3780 && (info
->export_dynamic
|| info
->dynamic
)))
3781 && is_elf_hash_table (htab
)
3782 && info
->output_bfd
->xvec
== abfd
->xvec
3783 && !htab
->dynamic_sections_created
)
3785 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3789 else if (!is_elf_hash_table (htab
))
3793 const char *soname
= NULL
;
3795 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3798 /* ld --just-symbols and dynamic objects don't mix very well.
3799 ld shouldn't allow it. */
3803 /* If this dynamic lib was specified on the command line with
3804 --as-needed in effect, then we don't want to add a DT_NEEDED
3805 tag unless the lib is actually used. Similary for libs brought
3806 in by another lib's DT_NEEDED. When --no-add-needed is used
3807 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3808 any dynamic library in DT_NEEDED tags in the dynamic lib at
3810 add_needed
= (elf_dyn_lib_class (abfd
)
3811 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3812 | DYN_NO_NEEDED
)) == 0;
3814 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3819 unsigned int elfsec
;
3820 unsigned long shlink
;
3822 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3829 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3830 if (elfsec
== SHN_BAD
)
3831 goto error_free_dyn
;
3832 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3834 for (extdyn
= dynbuf
;
3835 extdyn
< dynbuf
+ s
->size
;
3836 extdyn
+= bed
->s
->sizeof_dyn
)
3838 Elf_Internal_Dyn dyn
;
3840 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3841 if (dyn
.d_tag
== DT_SONAME
)
3843 unsigned int tagv
= dyn
.d_un
.d_val
;
3844 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3846 goto error_free_dyn
;
3848 if (dyn
.d_tag
== DT_NEEDED
)
3850 struct bfd_link_needed_list
*n
, **pn
;
3852 unsigned int tagv
= dyn
.d_un
.d_val
;
3854 amt
= sizeof (struct bfd_link_needed_list
);
3855 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3856 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3857 if (n
== NULL
|| fnm
== NULL
)
3858 goto error_free_dyn
;
3859 amt
= strlen (fnm
) + 1;
3860 anm
= (char *) bfd_alloc (abfd
, amt
);
3862 goto error_free_dyn
;
3863 memcpy (anm
, fnm
, amt
);
3867 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3871 if (dyn
.d_tag
== DT_RUNPATH
)
3873 struct bfd_link_needed_list
*n
, **pn
;
3875 unsigned int tagv
= dyn
.d_un
.d_val
;
3877 amt
= sizeof (struct bfd_link_needed_list
);
3878 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3879 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3880 if (n
== NULL
|| fnm
== NULL
)
3881 goto error_free_dyn
;
3882 amt
= strlen (fnm
) + 1;
3883 anm
= (char *) bfd_alloc (abfd
, amt
);
3885 goto error_free_dyn
;
3886 memcpy (anm
, fnm
, amt
);
3890 for (pn
= & runpath
;
3896 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3897 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3899 struct bfd_link_needed_list
*n
, **pn
;
3901 unsigned int tagv
= dyn
.d_un
.d_val
;
3903 amt
= sizeof (struct bfd_link_needed_list
);
3904 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3905 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3906 if (n
== NULL
|| fnm
== NULL
)
3907 goto error_free_dyn
;
3908 amt
= strlen (fnm
) + 1;
3909 anm
= (char *) bfd_alloc (abfd
, amt
);
3911 goto error_free_dyn
;
3912 memcpy (anm
, fnm
, amt
);
3922 if (dyn
.d_tag
== DT_AUDIT
)
3924 unsigned int tagv
= dyn
.d_un
.d_val
;
3925 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3932 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3933 frees all more recently bfd_alloc'd blocks as well. */
3939 struct bfd_link_needed_list
**pn
;
3940 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3945 /* We do not want to include any of the sections in a dynamic
3946 object in the output file. We hack by simply clobbering the
3947 list of sections in the BFD. This could be handled more
3948 cleanly by, say, a new section flag; the existing
3949 SEC_NEVER_LOAD flag is not the one we want, because that one
3950 still implies that the section takes up space in the output
3952 bfd_section_list_clear (abfd
);
3954 /* Find the name to use in a DT_NEEDED entry that refers to this
3955 object. If the object has a DT_SONAME entry, we use it.
3956 Otherwise, if the generic linker stuck something in
3957 elf_dt_name, we use that. Otherwise, we just use the file
3959 if (soname
== NULL
|| *soname
== '\0')
3961 soname
= elf_dt_name (abfd
);
3962 if (soname
== NULL
|| *soname
== '\0')
3963 soname
= bfd_get_filename (abfd
);
3966 /* Save the SONAME because sometimes the linker emulation code
3967 will need to know it. */
3968 elf_dt_name (abfd
) = soname
;
3970 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3974 /* If we have already included this dynamic object in the
3975 link, just ignore it. There is no reason to include a
3976 particular dynamic object more than once. */
3980 /* Save the DT_AUDIT entry for the linker emulation code. */
3981 elf_dt_audit (abfd
) = audit
;
3984 /* If this is a dynamic object, we always link against the .dynsym
3985 symbol table, not the .symtab symbol table. The dynamic linker
3986 will only see the .dynsym symbol table, so there is no reason to
3987 look at .symtab for a dynamic object. */
3989 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3990 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3992 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3994 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3996 /* The sh_info field of the symtab header tells us where the
3997 external symbols start. We don't care about the local symbols at
3999 if (elf_bad_symtab (abfd
))
4001 extsymcount
= symcount
;
4006 extsymcount
= symcount
- hdr
->sh_info
;
4007 extsymoff
= hdr
->sh_info
;
4010 sym_hash
= elf_sym_hashes (abfd
);
4011 if (extsymcount
!= 0)
4013 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4015 if (isymbuf
== NULL
)
4018 if (sym_hash
== NULL
)
4020 /* We store a pointer to the hash table entry for each
4023 amt
*= sizeof (struct elf_link_hash_entry
*);
4024 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4025 if (sym_hash
== NULL
)
4026 goto error_free_sym
;
4027 elf_sym_hashes (abfd
) = sym_hash
;
4033 /* Read in any version definitions. */
4034 if (!_bfd_elf_slurp_version_tables (abfd
,
4035 info
->default_imported_symver
))
4036 goto error_free_sym
;
4038 /* Read in the symbol versions, but don't bother to convert them
4039 to internal format. */
4040 if (elf_dynversym (abfd
) != 0)
4042 Elf_Internal_Shdr
*versymhdr
;
4044 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4045 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4046 if (extversym
== NULL
)
4047 goto error_free_sym
;
4048 amt
= versymhdr
->sh_size
;
4049 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4050 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4051 goto error_free_vers
;
4055 /* If we are loading an as-needed shared lib, save the symbol table
4056 state before we start adding symbols. If the lib turns out
4057 to be unneeded, restore the state. */
4058 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4063 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4065 struct bfd_hash_entry
*p
;
4066 struct elf_link_hash_entry
*h
;
4068 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4070 h
= (struct elf_link_hash_entry
*) p
;
4071 entsize
+= htab
->root
.table
.entsize
;
4072 if (h
->root
.type
== bfd_link_hash_warning
)
4073 entsize
+= htab
->root
.table
.entsize
;
4077 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4078 old_tab
= bfd_malloc (tabsize
+ entsize
);
4079 if (old_tab
== NULL
)
4080 goto error_free_vers
;
4082 /* Remember the current objalloc pointer, so that all mem for
4083 symbols added can later be reclaimed. */
4084 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4085 if (alloc_mark
== NULL
)
4086 goto error_free_vers
;
4088 /* Make a special call to the linker "notice" function to
4089 tell it that we are about to handle an as-needed lib. */
4090 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4091 goto error_free_vers
;
4093 /* Clone the symbol table. Remember some pointers into the
4094 symbol table, and dynamic symbol count. */
4095 old_ent
= (char *) old_tab
+ tabsize
;
4096 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4097 old_undefs
= htab
->root
.undefs
;
4098 old_undefs_tail
= htab
->root
.undefs_tail
;
4099 old_table
= htab
->root
.table
.table
;
4100 old_size
= htab
->root
.table
.size
;
4101 old_count
= htab
->root
.table
.count
;
4102 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4103 if (old_strtab
== NULL
)
4104 goto error_free_vers
;
4106 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4108 struct bfd_hash_entry
*p
;
4109 struct elf_link_hash_entry
*h
;
4111 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4113 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4114 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4115 h
= (struct elf_link_hash_entry
*) p
;
4116 if (h
->root
.type
== bfd_link_hash_warning
)
4118 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4119 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4126 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4127 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4129 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4133 asection
*sec
, *new_sec
;
4136 struct elf_link_hash_entry
*h
;
4137 struct elf_link_hash_entry
*hi
;
4138 bfd_boolean definition
;
4139 bfd_boolean size_change_ok
;
4140 bfd_boolean type_change_ok
;
4141 bfd_boolean new_weakdef
;
4142 bfd_boolean new_weak
;
4143 bfd_boolean old_weak
;
4144 bfd_boolean override
;
4146 bfd_boolean discarded
;
4147 unsigned int old_alignment
;
4149 bfd_boolean matched
;
4153 flags
= BSF_NO_FLAGS
;
4155 value
= isym
->st_value
;
4156 common
= bed
->common_definition (isym
);
4159 bind
= ELF_ST_BIND (isym
->st_info
);
4163 /* This should be impossible, since ELF requires that all
4164 global symbols follow all local symbols, and that sh_info
4165 point to the first global symbol. Unfortunately, Irix 5
4170 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4178 case STB_GNU_UNIQUE
:
4179 flags
= BSF_GNU_UNIQUE
;
4183 /* Leave it up to the processor backend. */
4187 if (isym
->st_shndx
== SHN_UNDEF
)
4188 sec
= bfd_und_section_ptr
;
4189 else if (isym
->st_shndx
== SHN_ABS
)
4190 sec
= bfd_abs_section_ptr
;
4191 else if (isym
->st_shndx
== SHN_COMMON
)
4193 sec
= bfd_com_section_ptr
;
4194 /* What ELF calls the size we call the value. What ELF
4195 calls the value we call the alignment. */
4196 value
= isym
->st_size
;
4200 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4202 sec
= bfd_abs_section_ptr
;
4203 else if (discarded_section (sec
))
4205 /* Symbols from discarded section are undefined. We keep
4207 sec
= bfd_und_section_ptr
;
4209 isym
->st_shndx
= SHN_UNDEF
;
4211 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4215 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4218 goto error_free_vers
;
4220 if (isym
->st_shndx
== SHN_COMMON
4221 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4223 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4227 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4229 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4231 goto error_free_vers
;
4235 else if (isym
->st_shndx
== SHN_COMMON
4236 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4237 && !bfd_link_relocatable (info
))
4239 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4243 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4244 | SEC_LINKER_CREATED
);
4245 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4247 goto error_free_vers
;
4251 else if (bed
->elf_add_symbol_hook
)
4253 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4255 goto error_free_vers
;
4257 /* The hook function sets the name to NULL if this symbol
4258 should be skipped for some reason. */
4263 /* Sanity check that all possibilities were handled. */
4266 bfd_set_error (bfd_error_bad_value
);
4267 goto error_free_vers
;
4270 /* Silently discard TLS symbols from --just-syms. There's
4271 no way to combine a static TLS block with a new TLS block
4272 for this executable. */
4273 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4274 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4277 if (bfd_is_und_section (sec
)
4278 || bfd_is_com_section (sec
))
4283 size_change_ok
= FALSE
;
4284 type_change_ok
= bed
->type_change_ok
;
4291 if (is_elf_hash_table (htab
))
4293 Elf_Internal_Versym iver
;
4294 unsigned int vernum
= 0;
4299 if (info
->default_imported_symver
)
4300 /* Use the default symbol version created earlier. */
4301 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4306 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4308 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4310 /* If this is a hidden symbol, or if it is not version
4311 1, we append the version name to the symbol name.
4312 However, we do not modify a non-hidden absolute symbol
4313 if it is not a function, because it might be the version
4314 symbol itself. FIXME: What if it isn't? */
4315 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4317 && (!bfd_is_abs_section (sec
)
4318 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4321 size_t namelen
, verlen
, newlen
;
4324 if (isym
->st_shndx
!= SHN_UNDEF
)
4326 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4328 else if (vernum
> 1)
4330 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4337 /* xgettext:c-format */
4338 (_("%B: %s: invalid version %u (max %d)"),
4340 elf_tdata (abfd
)->cverdefs
);
4341 bfd_set_error (bfd_error_bad_value
);
4342 goto error_free_vers
;
4347 /* We cannot simply test for the number of
4348 entries in the VERNEED section since the
4349 numbers for the needed versions do not start
4351 Elf_Internal_Verneed
*t
;
4354 for (t
= elf_tdata (abfd
)->verref
;
4358 Elf_Internal_Vernaux
*a
;
4360 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4362 if (a
->vna_other
== vernum
)
4364 verstr
= a
->vna_nodename
;
4374 /* xgettext:c-format */
4375 (_("%B: %s: invalid needed version %d"),
4376 abfd
, name
, vernum
);
4377 bfd_set_error (bfd_error_bad_value
);
4378 goto error_free_vers
;
4382 namelen
= strlen (name
);
4383 verlen
= strlen (verstr
);
4384 newlen
= namelen
+ verlen
+ 2;
4385 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4386 && isym
->st_shndx
!= SHN_UNDEF
)
4389 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4390 if (newname
== NULL
)
4391 goto error_free_vers
;
4392 memcpy (newname
, name
, namelen
);
4393 p
= newname
+ namelen
;
4395 /* If this is a defined non-hidden version symbol,
4396 we add another @ to the name. This indicates the
4397 default version of the symbol. */
4398 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4399 && isym
->st_shndx
!= SHN_UNDEF
)
4401 memcpy (p
, verstr
, verlen
+ 1);
4406 /* If this symbol has default visibility and the user has
4407 requested we not re-export it, then mark it as hidden. */
4408 if (!bfd_is_und_section (sec
)
4411 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4412 isym
->st_other
= (STV_HIDDEN
4413 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4415 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4416 sym_hash
, &old_bfd
, &old_weak
,
4417 &old_alignment
, &skip
, &override
,
4418 &type_change_ok
, &size_change_ok
,
4420 goto error_free_vers
;
4425 /* Override a definition only if the new symbol matches the
4427 if (override
&& matched
)
4431 while (h
->root
.type
== bfd_link_hash_indirect
4432 || h
->root
.type
== bfd_link_hash_warning
)
4433 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4435 if (elf_tdata (abfd
)->verdef
!= NULL
4438 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4441 if (! (_bfd_generic_link_add_one_symbol
4442 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4443 (struct bfd_link_hash_entry
**) sym_hash
)))
4444 goto error_free_vers
;
4446 if ((flags
& BSF_GNU_UNIQUE
)
4447 && (abfd
->flags
& DYNAMIC
) == 0
4448 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4449 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4452 /* We need to make sure that indirect symbol dynamic flags are
4455 while (h
->root
.type
== bfd_link_hash_indirect
4456 || h
->root
.type
== bfd_link_hash_warning
)
4457 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4459 /* Setting the index to -3 tells elf_link_output_extsym that
4460 this symbol is defined in a discarded section. */
4466 new_weak
= (flags
& BSF_WEAK
) != 0;
4467 new_weakdef
= FALSE
;
4471 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4472 && is_elf_hash_table (htab
)
4473 && h
->u
.weakdef
== NULL
)
4475 /* Keep a list of all weak defined non function symbols from
4476 a dynamic object, using the weakdef field. Later in this
4477 function we will set the weakdef field to the correct
4478 value. We only put non-function symbols from dynamic
4479 objects on this list, because that happens to be the only
4480 time we need to know the normal symbol corresponding to a
4481 weak symbol, and the information is time consuming to
4482 figure out. If the weakdef field is not already NULL,
4483 then this symbol was already defined by some previous
4484 dynamic object, and we will be using that previous
4485 definition anyhow. */
4487 h
->u
.weakdef
= weaks
;
4492 /* Set the alignment of a common symbol. */
4493 if ((common
|| bfd_is_com_section (sec
))
4494 && h
->root
.type
== bfd_link_hash_common
)
4499 align
= bfd_log2 (isym
->st_value
);
4502 /* The new symbol is a common symbol in a shared object.
4503 We need to get the alignment from the section. */
4504 align
= new_sec
->alignment_power
;
4506 if (align
> old_alignment
)
4507 h
->root
.u
.c
.p
->alignment_power
= align
;
4509 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4512 if (is_elf_hash_table (htab
))
4514 /* Set a flag in the hash table entry indicating the type of
4515 reference or definition we just found. A dynamic symbol
4516 is one which is referenced or defined by both a regular
4517 object and a shared object. */
4518 bfd_boolean dynsym
= FALSE
;
4520 /* Plugin symbols aren't normal. Don't set def_regular or
4521 ref_regular for them, or make them dynamic. */
4522 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4529 if (bind
!= STB_WEAK
)
4530 h
->ref_regular_nonweak
= 1;
4542 /* If the indirect symbol has been forced local, don't
4543 make the real symbol dynamic. */
4544 if ((h
== hi
|| !hi
->forced_local
)
4545 && (bfd_link_dll (info
)
4555 hi
->ref_dynamic
= 1;
4560 hi
->def_dynamic
= 1;
4563 /* If the indirect symbol has been forced local, don't
4564 make the real symbol dynamic. */
4565 if ((h
== hi
|| !hi
->forced_local
)
4568 || (h
->u
.weakdef
!= NULL
4570 && h
->u
.weakdef
->dynindx
!= -1)))
4574 /* Check to see if we need to add an indirect symbol for
4575 the default name. */
4577 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4578 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4579 sec
, value
, &old_bfd
, &dynsym
))
4580 goto error_free_vers
;
4582 /* Check the alignment when a common symbol is involved. This
4583 can change when a common symbol is overridden by a normal
4584 definition or a common symbol is ignored due to the old
4585 normal definition. We need to make sure the maximum
4586 alignment is maintained. */
4587 if ((old_alignment
|| common
)
4588 && h
->root
.type
!= bfd_link_hash_common
)
4590 unsigned int common_align
;
4591 unsigned int normal_align
;
4592 unsigned int symbol_align
;
4596 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4597 || h
->root
.type
== bfd_link_hash_defweak
);
4599 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4600 if (h
->root
.u
.def
.section
->owner
!= NULL
4601 && (h
->root
.u
.def
.section
->owner
->flags
4602 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4604 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4605 if (normal_align
> symbol_align
)
4606 normal_align
= symbol_align
;
4609 normal_align
= symbol_align
;
4613 common_align
= old_alignment
;
4614 common_bfd
= old_bfd
;
4619 common_align
= bfd_log2 (isym
->st_value
);
4621 normal_bfd
= old_bfd
;
4624 if (normal_align
< common_align
)
4626 /* PR binutils/2735 */
4627 if (normal_bfd
== NULL
)
4629 /* xgettext:c-format */
4630 (_("Warning: alignment %u of common symbol `%s' in %B is"
4631 " greater than the alignment (%u) of its section %A"),
4632 common_bfd
, h
->root
.u
.def
.section
,
4633 1 << common_align
, name
, 1 << normal_align
);
4636 /* xgettext:c-format */
4637 (_("Warning: alignment %u of symbol `%s' in %B"
4638 " is smaller than %u in %B"),
4639 normal_bfd
, common_bfd
,
4640 1 << normal_align
, name
, 1 << common_align
);
4644 /* Remember the symbol size if it isn't undefined. */
4645 if (isym
->st_size
!= 0
4646 && isym
->st_shndx
!= SHN_UNDEF
4647 && (definition
|| h
->size
== 0))
4650 && h
->size
!= isym
->st_size
4651 && ! size_change_ok
)
4653 /* xgettext:c-format */
4654 (_("Warning: size of symbol `%s' changed"
4655 " from %lu in %B to %lu in %B"),
4657 name
, (unsigned long) h
->size
,
4658 (unsigned long) isym
->st_size
);
4660 h
->size
= isym
->st_size
;
4663 /* If this is a common symbol, then we always want H->SIZE
4664 to be the size of the common symbol. The code just above
4665 won't fix the size if a common symbol becomes larger. We
4666 don't warn about a size change here, because that is
4667 covered by --warn-common. Allow changes between different
4669 if (h
->root
.type
== bfd_link_hash_common
)
4670 h
->size
= h
->root
.u
.c
.size
;
4672 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4673 && ((definition
&& !new_weak
)
4674 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4675 || h
->type
== STT_NOTYPE
))
4677 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4679 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4681 if (type
== STT_GNU_IFUNC
4682 && (abfd
->flags
& DYNAMIC
) != 0)
4685 if (h
->type
!= type
)
4687 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4688 /* xgettext:c-format */
4690 (_("Warning: type of symbol `%s' changed"
4691 " from %d to %d in %B"),
4692 abfd
, name
, h
->type
, type
);
4698 /* Merge st_other field. */
4699 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4701 /* We don't want to make debug symbol dynamic. */
4703 && (sec
->flags
& SEC_DEBUGGING
)
4704 && !bfd_link_relocatable (info
))
4707 /* Nor should we make plugin symbols dynamic. */
4708 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4713 h
->target_internal
= isym
->st_target_internal
;
4714 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4717 if (definition
&& !dynamic
)
4719 char *p
= strchr (name
, ELF_VER_CHR
);
4720 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4722 /* Queue non-default versions so that .symver x, x@FOO
4723 aliases can be checked. */
4726 amt
= ((isymend
- isym
+ 1)
4727 * sizeof (struct elf_link_hash_entry
*));
4729 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4731 goto error_free_vers
;
4733 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4737 if (dynsym
&& h
->dynindx
== -1)
4739 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4740 goto error_free_vers
;
4741 if (h
->u
.weakdef
!= NULL
4743 && h
->u
.weakdef
->dynindx
== -1)
4745 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4746 goto error_free_vers
;
4749 else if (h
->dynindx
!= -1)
4750 /* If the symbol already has a dynamic index, but
4751 visibility says it should not be visible, turn it into
4753 switch (ELF_ST_VISIBILITY (h
->other
))
4757 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4762 /* Don't add DT_NEEDED for references from the dummy bfd nor
4763 for unmatched symbol. */
4768 && h
->ref_regular_nonweak
4770 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4771 || (h
->ref_dynamic_nonweak
4772 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4773 && !on_needed_list (elf_dt_name (abfd
),
4774 htab
->needed
, NULL
))))
4777 const char *soname
= elf_dt_name (abfd
);
4779 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4780 h
->root
.root
.string
);
4782 /* A symbol from a library loaded via DT_NEEDED of some
4783 other library is referenced by a regular object.
4784 Add a DT_NEEDED entry for it. Issue an error if
4785 --no-add-needed is used and the reference was not
4788 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4791 /* xgettext:c-format */
4792 (_("%B: undefined reference to symbol '%s'"),
4794 bfd_set_error (bfd_error_missing_dso
);
4795 goto error_free_vers
;
4798 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4799 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4802 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4804 goto error_free_vers
;
4806 BFD_ASSERT (ret
== 0);
4811 if (extversym
!= NULL
)
4817 if (isymbuf
!= NULL
)
4823 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4827 /* Restore the symbol table. */
4828 old_ent
= (char *) old_tab
+ tabsize
;
4829 memset (elf_sym_hashes (abfd
), 0,
4830 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4831 htab
->root
.table
.table
= old_table
;
4832 htab
->root
.table
.size
= old_size
;
4833 htab
->root
.table
.count
= old_count
;
4834 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4835 htab
->root
.undefs
= old_undefs
;
4836 htab
->root
.undefs_tail
= old_undefs_tail
;
4837 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4840 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4842 struct bfd_hash_entry
*p
;
4843 struct elf_link_hash_entry
*h
;
4845 unsigned int alignment_power
;
4847 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4849 h
= (struct elf_link_hash_entry
*) p
;
4850 if (h
->root
.type
== bfd_link_hash_warning
)
4851 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4853 /* Preserve the maximum alignment and size for common
4854 symbols even if this dynamic lib isn't on DT_NEEDED
4855 since it can still be loaded at run time by another
4857 if (h
->root
.type
== bfd_link_hash_common
)
4859 size
= h
->root
.u
.c
.size
;
4860 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4865 alignment_power
= 0;
4867 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4868 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4869 h
= (struct elf_link_hash_entry
*) p
;
4870 if (h
->root
.type
== bfd_link_hash_warning
)
4872 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4873 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4874 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4876 if (h
->root
.type
== bfd_link_hash_common
)
4878 if (size
> h
->root
.u
.c
.size
)
4879 h
->root
.u
.c
.size
= size
;
4880 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4881 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4886 /* Make a special call to the linker "notice" function to
4887 tell it that symbols added for crefs may need to be removed. */
4888 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4889 goto error_free_vers
;
4892 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4894 if (nondeflt_vers
!= NULL
)
4895 free (nondeflt_vers
);
4899 if (old_tab
!= NULL
)
4901 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4902 goto error_free_vers
;
4907 /* Now that all the symbols from this input file are created, if
4908 not performing a relocatable link, handle .symver foo, foo@BAR
4909 such that any relocs against foo become foo@BAR. */
4910 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4914 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4916 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4917 char *shortname
, *p
;
4919 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4921 || (h
->root
.type
!= bfd_link_hash_defined
4922 && h
->root
.type
!= bfd_link_hash_defweak
))
4925 amt
= p
- h
->root
.root
.string
;
4926 shortname
= (char *) bfd_malloc (amt
+ 1);
4928 goto error_free_vers
;
4929 memcpy (shortname
, h
->root
.root
.string
, amt
);
4930 shortname
[amt
] = '\0';
4932 hi
= (struct elf_link_hash_entry
*)
4933 bfd_link_hash_lookup (&htab
->root
, shortname
,
4934 FALSE
, FALSE
, FALSE
);
4936 && hi
->root
.type
== h
->root
.type
4937 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4938 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4940 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4941 hi
->root
.type
= bfd_link_hash_indirect
;
4942 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4943 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4944 sym_hash
= elf_sym_hashes (abfd
);
4946 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4947 if (sym_hash
[symidx
] == hi
)
4949 sym_hash
[symidx
] = h
;
4955 free (nondeflt_vers
);
4956 nondeflt_vers
= NULL
;
4959 /* Now set the weakdefs field correctly for all the weak defined
4960 symbols we found. The only way to do this is to search all the
4961 symbols. Since we only need the information for non functions in
4962 dynamic objects, that's the only time we actually put anything on
4963 the list WEAKS. We need this information so that if a regular
4964 object refers to a symbol defined weakly in a dynamic object, the
4965 real symbol in the dynamic object is also put in the dynamic
4966 symbols; we also must arrange for both symbols to point to the
4967 same memory location. We could handle the general case of symbol
4968 aliasing, but a general symbol alias can only be generated in
4969 assembler code, handling it correctly would be very time
4970 consuming, and other ELF linkers don't handle general aliasing
4974 struct elf_link_hash_entry
**hpp
;
4975 struct elf_link_hash_entry
**hppend
;
4976 struct elf_link_hash_entry
**sorted_sym_hash
;
4977 struct elf_link_hash_entry
*h
;
4980 /* Since we have to search the whole symbol list for each weak
4981 defined symbol, search time for N weak defined symbols will be
4982 O(N^2). Binary search will cut it down to O(NlogN). */
4984 amt
*= sizeof (struct elf_link_hash_entry
*);
4985 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4986 if (sorted_sym_hash
== NULL
)
4988 sym_hash
= sorted_sym_hash
;
4989 hpp
= elf_sym_hashes (abfd
);
4990 hppend
= hpp
+ extsymcount
;
4992 for (; hpp
< hppend
; hpp
++)
4996 && h
->root
.type
== bfd_link_hash_defined
4997 && !bed
->is_function_type (h
->type
))
5005 qsort (sorted_sym_hash
, sym_count
,
5006 sizeof (struct elf_link_hash_entry
*),
5009 while (weaks
!= NULL
)
5011 struct elf_link_hash_entry
*hlook
;
5014 size_t i
, j
, idx
= 0;
5017 weaks
= hlook
->u
.weakdef
;
5018 hlook
->u
.weakdef
= NULL
;
5020 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5021 || hlook
->root
.type
== bfd_link_hash_defweak
5022 || hlook
->root
.type
== bfd_link_hash_common
5023 || hlook
->root
.type
== bfd_link_hash_indirect
);
5024 slook
= hlook
->root
.u
.def
.section
;
5025 vlook
= hlook
->root
.u
.def
.value
;
5031 bfd_signed_vma vdiff
;
5033 h
= sorted_sym_hash
[idx
];
5034 vdiff
= vlook
- h
->root
.u
.def
.value
;
5041 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5051 /* We didn't find a value/section match. */
5055 /* With multiple aliases, or when the weak symbol is already
5056 strongly defined, we have multiple matching symbols and
5057 the binary search above may land on any of them. Step
5058 one past the matching symbol(s). */
5061 h
= sorted_sym_hash
[idx
];
5062 if (h
->root
.u
.def
.section
!= slook
5063 || h
->root
.u
.def
.value
!= vlook
)
5067 /* Now look back over the aliases. Since we sorted by size
5068 as well as value and section, we'll choose the one with
5069 the largest size. */
5072 h
= sorted_sym_hash
[idx
];
5074 /* Stop if value or section doesn't match. */
5075 if (h
->root
.u
.def
.section
!= slook
5076 || h
->root
.u
.def
.value
!= vlook
)
5078 else if (h
!= hlook
)
5080 hlook
->u
.weakdef
= h
;
5082 /* If the weak definition is in the list of dynamic
5083 symbols, make sure the real definition is put
5085 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5087 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5090 free (sorted_sym_hash
);
5095 /* If the real definition is in the list of dynamic
5096 symbols, make sure the weak definition is put
5097 there as well. If we don't do this, then the
5098 dynamic loader might not merge the entries for the
5099 real definition and the weak definition. */
5100 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5102 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5103 goto err_free_sym_hash
;
5110 free (sorted_sym_hash
);
5113 if (bed
->check_directives
5114 && !(*bed
->check_directives
) (abfd
, info
))
5117 if (!info
->check_relocs_after_open_input
5118 && !_bfd_elf_link_check_relocs (abfd
, info
))
5121 /* If this is a non-traditional link, try to optimize the handling
5122 of the .stab/.stabstr sections. */
5124 && ! info
->traditional_format
5125 && is_elf_hash_table (htab
)
5126 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5130 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5131 if (stabstr
!= NULL
)
5133 bfd_size_type string_offset
= 0;
5136 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5137 if (CONST_STRNEQ (stab
->name
, ".stab")
5138 && (!stab
->name
[5] ||
5139 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5140 && (stab
->flags
& SEC_MERGE
) == 0
5141 && !bfd_is_abs_section (stab
->output_section
))
5143 struct bfd_elf_section_data
*secdata
;
5145 secdata
= elf_section_data (stab
);
5146 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5147 stabstr
, &secdata
->sec_info
,
5150 if (secdata
->sec_info
)
5151 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5156 if (is_elf_hash_table (htab
) && add_needed
)
5158 /* Add this bfd to the loaded list. */
5159 struct elf_link_loaded_list
*n
;
5161 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5165 n
->next
= htab
->loaded
;
5172 if (old_tab
!= NULL
)
5174 if (old_strtab
!= NULL
)
5176 if (nondeflt_vers
!= NULL
)
5177 free (nondeflt_vers
);
5178 if (extversym
!= NULL
)
5181 if (isymbuf
!= NULL
)
5187 /* Return the linker hash table entry of a symbol that might be
5188 satisfied by an archive symbol. Return -1 on error. */
5190 struct elf_link_hash_entry
*
5191 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5192 struct bfd_link_info
*info
,
5195 struct elf_link_hash_entry
*h
;
5199 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5203 /* If this is a default version (the name contains @@), look up the
5204 symbol again with only one `@' as well as without the version.
5205 The effect is that references to the symbol with and without the
5206 version will be matched by the default symbol in the archive. */
5208 p
= strchr (name
, ELF_VER_CHR
);
5209 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5212 /* First check with only one `@'. */
5213 len
= strlen (name
);
5214 copy
= (char *) bfd_alloc (abfd
, len
);
5216 return (struct elf_link_hash_entry
*) 0 - 1;
5218 first
= p
- name
+ 1;
5219 memcpy (copy
, name
, first
);
5220 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5222 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5225 /* We also need to check references to the symbol without the
5227 copy
[first
- 1] = '\0';
5228 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5229 FALSE
, FALSE
, TRUE
);
5232 bfd_release (abfd
, copy
);
5236 /* Add symbols from an ELF archive file to the linker hash table. We
5237 don't use _bfd_generic_link_add_archive_symbols because we need to
5238 handle versioned symbols.
5240 Fortunately, ELF archive handling is simpler than that done by
5241 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5242 oddities. In ELF, if we find a symbol in the archive map, and the
5243 symbol is currently undefined, we know that we must pull in that
5246 Unfortunately, we do have to make multiple passes over the symbol
5247 table until nothing further is resolved. */
5250 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5253 unsigned char *included
= NULL
;
5257 const struct elf_backend_data
*bed
;
5258 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5259 (bfd
*, struct bfd_link_info
*, const char *);
5261 if (! bfd_has_map (abfd
))
5263 /* An empty archive is a special case. */
5264 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5266 bfd_set_error (bfd_error_no_armap
);
5270 /* Keep track of all symbols we know to be already defined, and all
5271 files we know to be already included. This is to speed up the
5272 second and subsequent passes. */
5273 c
= bfd_ardata (abfd
)->symdef_count
;
5277 amt
*= sizeof (*included
);
5278 included
= (unsigned char *) bfd_zmalloc (amt
);
5279 if (included
== NULL
)
5282 symdefs
= bfd_ardata (abfd
)->symdefs
;
5283 bed
= get_elf_backend_data (abfd
);
5284 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5297 symdefend
= symdef
+ c
;
5298 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5300 struct elf_link_hash_entry
*h
;
5302 struct bfd_link_hash_entry
*undefs_tail
;
5307 if (symdef
->file_offset
== last
)
5313 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5314 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5320 if (h
->root
.type
== bfd_link_hash_common
)
5322 /* We currently have a common symbol. The archive map contains
5323 a reference to this symbol, so we may want to include it. We
5324 only want to include it however, if this archive element
5325 contains a definition of the symbol, not just another common
5328 Unfortunately some archivers (including GNU ar) will put
5329 declarations of common symbols into their archive maps, as
5330 well as real definitions, so we cannot just go by the archive
5331 map alone. Instead we must read in the element's symbol
5332 table and check that to see what kind of symbol definition
5334 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5337 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5339 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5340 /* Symbol must be defined. Don't check it again. */
5345 /* We need to include this archive member. */
5346 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5347 if (element
== NULL
)
5350 if (! bfd_check_format (element
, bfd_object
))
5353 undefs_tail
= info
->hash
->undefs_tail
;
5355 if (!(*info
->callbacks
5356 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5358 if (!bfd_link_add_symbols (element
, info
))
5361 /* If there are any new undefined symbols, we need to make
5362 another pass through the archive in order to see whether
5363 they can be defined. FIXME: This isn't perfect, because
5364 common symbols wind up on undefs_tail and because an
5365 undefined symbol which is defined later on in this pass
5366 does not require another pass. This isn't a bug, but it
5367 does make the code less efficient than it could be. */
5368 if (undefs_tail
!= info
->hash
->undefs_tail
)
5371 /* Look backward to mark all symbols from this object file
5372 which we have already seen in this pass. */
5376 included
[mark
] = TRUE
;
5381 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5383 /* We mark subsequent symbols from this object file as we go
5384 on through the loop. */
5385 last
= symdef
->file_offset
;
5395 if (included
!= NULL
)
5400 /* Given an ELF BFD, add symbols to the global hash table as
5404 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5406 switch (bfd_get_format (abfd
))
5409 return elf_link_add_object_symbols (abfd
, info
);
5411 return elf_link_add_archive_symbols (abfd
, info
);
5413 bfd_set_error (bfd_error_wrong_format
);
5418 struct hash_codes_info
5420 unsigned long *hashcodes
;
5424 /* This function will be called though elf_link_hash_traverse to store
5425 all hash value of the exported symbols in an array. */
5428 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5430 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5435 /* Ignore indirect symbols. These are added by the versioning code. */
5436 if (h
->dynindx
== -1)
5439 name
= h
->root
.root
.string
;
5440 if (h
->versioned
>= versioned
)
5442 char *p
= strchr (name
, ELF_VER_CHR
);
5445 alc
= (char *) bfd_malloc (p
- name
+ 1);
5451 memcpy (alc
, name
, p
- name
);
5452 alc
[p
- name
] = '\0';
5457 /* Compute the hash value. */
5458 ha
= bfd_elf_hash (name
);
5460 /* Store the found hash value in the array given as the argument. */
5461 *(inf
->hashcodes
)++ = ha
;
5463 /* And store it in the struct so that we can put it in the hash table
5465 h
->u
.elf_hash_value
= ha
;
5473 struct collect_gnu_hash_codes
5476 const struct elf_backend_data
*bed
;
5477 unsigned long int nsyms
;
5478 unsigned long int maskbits
;
5479 unsigned long int *hashcodes
;
5480 unsigned long int *hashval
;
5481 unsigned long int *indx
;
5482 unsigned long int *counts
;
5485 long int min_dynindx
;
5486 unsigned long int bucketcount
;
5487 unsigned long int symindx
;
5488 long int local_indx
;
5489 long int shift1
, shift2
;
5490 unsigned long int mask
;
5494 /* This function will be called though elf_link_hash_traverse to store
5495 all hash value of the exported symbols in an array. */
5498 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5500 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5505 /* Ignore indirect symbols. These are added by the versioning code. */
5506 if (h
->dynindx
== -1)
5509 /* Ignore also local symbols and undefined symbols. */
5510 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5513 name
= h
->root
.root
.string
;
5514 if (h
->versioned
>= versioned
)
5516 char *p
= strchr (name
, ELF_VER_CHR
);
5519 alc
= (char *) bfd_malloc (p
- name
+ 1);
5525 memcpy (alc
, name
, p
- name
);
5526 alc
[p
- name
] = '\0';
5531 /* Compute the hash value. */
5532 ha
= bfd_elf_gnu_hash (name
);
5534 /* Store the found hash value in the array for compute_bucket_count,
5535 and also for .dynsym reordering purposes. */
5536 s
->hashcodes
[s
->nsyms
] = ha
;
5537 s
->hashval
[h
->dynindx
] = ha
;
5539 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5540 s
->min_dynindx
= h
->dynindx
;
5548 /* This function will be called though elf_link_hash_traverse to do
5549 final dynaminc symbol renumbering. */
5552 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5554 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5555 unsigned long int bucket
;
5556 unsigned long int val
;
5558 /* Ignore indirect symbols. */
5559 if (h
->dynindx
== -1)
5562 /* Ignore also local symbols and undefined symbols. */
5563 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5565 if (h
->dynindx
>= s
->min_dynindx
)
5566 h
->dynindx
= s
->local_indx
++;
5570 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5571 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5572 & ((s
->maskbits
>> s
->shift1
) - 1);
5573 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5575 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5576 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5577 if (s
->counts
[bucket
] == 1)
5578 /* Last element terminates the chain. */
5580 bfd_put_32 (s
->output_bfd
, val
,
5581 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5582 --s
->counts
[bucket
];
5583 h
->dynindx
= s
->indx
[bucket
]++;
5587 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5590 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5592 return !(h
->forced_local
5593 || h
->root
.type
== bfd_link_hash_undefined
5594 || h
->root
.type
== bfd_link_hash_undefweak
5595 || ((h
->root
.type
== bfd_link_hash_defined
5596 || h
->root
.type
== bfd_link_hash_defweak
)
5597 && h
->root
.u
.def
.section
->output_section
== NULL
));
5600 /* Array used to determine the number of hash table buckets to use
5601 based on the number of symbols there are. If there are fewer than
5602 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5603 fewer than 37 we use 17 buckets, and so forth. We never use more
5604 than 32771 buckets. */
5606 static const size_t elf_buckets
[] =
5608 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5612 /* Compute bucket count for hashing table. We do not use a static set
5613 of possible tables sizes anymore. Instead we determine for all
5614 possible reasonable sizes of the table the outcome (i.e., the
5615 number of collisions etc) and choose the best solution. The
5616 weighting functions are not too simple to allow the table to grow
5617 without bounds. Instead one of the weighting factors is the size.
5618 Therefore the result is always a good payoff between few collisions
5619 (= short chain lengths) and table size. */
5621 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5622 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5623 unsigned long int nsyms
,
5626 size_t best_size
= 0;
5627 unsigned long int i
;
5629 /* We have a problem here. The following code to optimize the table
5630 size requires an integer type with more the 32 bits. If
5631 BFD_HOST_U_64_BIT is set we know about such a type. */
5632 #ifdef BFD_HOST_U_64_BIT
5637 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5638 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5639 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5640 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5641 unsigned long int *counts
;
5643 unsigned int no_improvement_count
= 0;
5645 /* Possible optimization parameters: if we have NSYMS symbols we say
5646 that the hashing table must at least have NSYMS/4 and at most
5648 minsize
= nsyms
/ 4;
5651 best_size
= maxsize
= nsyms
* 2;
5656 if ((best_size
& 31) == 0)
5660 /* Create array where we count the collisions in. We must use bfd_malloc
5661 since the size could be large. */
5663 amt
*= sizeof (unsigned long int);
5664 counts
= (unsigned long int *) bfd_malloc (amt
);
5668 /* Compute the "optimal" size for the hash table. The criteria is a
5669 minimal chain length. The minor criteria is (of course) the size
5671 for (i
= minsize
; i
< maxsize
; ++i
)
5673 /* Walk through the array of hashcodes and count the collisions. */
5674 BFD_HOST_U_64_BIT max
;
5675 unsigned long int j
;
5676 unsigned long int fact
;
5678 if (gnu_hash
&& (i
& 31) == 0)
5681 memset (counts
, '\0', i
* sizeof (unsigned long int));
5683 /* Determine how often each hash bucket is used. */
5684 for (j
= 0; j
< nsyms
; ++j
)
5685 ++counts
[hashcodes
[j
] % i
];
5687 /* For the weight function we need some information about the
5688 pagesize on the target. This is information need not be 100%
5689 accurate. Since this information is not available (so far) we
5690 define it here to a reasonable default value. If it is crucial
5691 to have a better value some day simply define this value. */
5692 # ifndef BFD_TARGET_PAGESIZE
5693 # define BFD_TARGET_PAGESIZE (4096)
5696 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5698 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5701 /* Variant 1: optimize for short chains. We add the squares
5702 of all the chain lengths (which favors many small chain
5703 over a few long chains). */
5704 for (j
= 0; j
< i
; ++j
)
5705 max
+= counts
[j
] * counts
[j
];
5707 /* This adds penalties for the overall size of the table. */
5708 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5711 /* Variant 2: Optimize a lot more for small table. Here we
5712 also add squares of the size but we also add penalties for
5713 empty slots (the +1 term). */
5714 for (j
= 0; j
< i
; ++j
)
5715 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5717 /* The overall size of the table is considered, but not as
5718 strong as in variant 1, where it is squared. */
5719 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5723 /* Compare with current best results. */
5724 if (max
< best_chlen
)
5728 no_improvement_count
= 0;
5730 /* PR 11843: Avoid futile long searches for the best bucket size
5731 when there are a large number of symbols. */
5732 else if (++no_improvement_count
== 100)
5739 #endif /* defined (BFD_HOST_U_64_BIT) */
5741 /* This is the fallback solution if no 64bit type is available or if we
5742 are not supposed to spend much time on optimizations. We select the
5743 bucket count using a fixed set of numbers. */
5744 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5746 best_size
= elf_buckets
[i
];
5747 if (nsyms
< elf_buckets
[i
+ 1])
5750 if (gnu_hash
&& best_size
< 2)
5757 /* Size any SHT_GROUP section for ld -r. */
5760 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5764 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5765 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5766 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5771 /* Set a default stack segment size. The value in INFO wins. If it
5772 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5773 undefined it is initialized. */
5776 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5777 struct bfd_link_info
*info
,
5778 const char *legacy_symbol
,
5779 bfd_vma default_size
)
5781 struct elf_link_hash_entry
*h
= NULL
;
5783 /* Look for legacy symbol. */
5785 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5786 FALSE
, FALSE
, FALSE
);
5787 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5788 || h
->root
.type
== bfd_link_hash_defweak
)
5790 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5792 /* The symbol has no type if specified on the command line. */
5793 h
->type
= STT_OBJECT
;
5794 if (info
->stacksize
)
5795 /* xgettext:c-format */
5796 _bfd_error_handler (_("%B: stack size specified and %s set"),
5797 output_bfd
, legacy_symbol
);
5798 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5799 /* xgettext:c-format */
5800 _bfd_error_handler (_("%B: %s not absolute"),
5801 output_bfd
, legacy_symbol
);
5803 info
->stacksize
= h
->root
.u
.def
.value
;
5806 if (!info
->stacksize
)
5807 /* If the user didn't set a size, or explicitly inhibit the
5808 size, set it now. */
5809 info
->stacksize
= default_size
;
5811 /* Provide the legacy symbol, if it is referenced. */
5812 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5813 || h
->root
.type
== bfd_link_hash_undefweak
))
5815 struct bfd_link_hash_entry
*bh
= NULL
;
5817 if (!(_bfd_generic_link_add_one_symbol
5818 (info
, output_bfd
, legacy_symbol
,
5819 BSF_GLOBAL
, bfd_abs_section_ptr
,
5820 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5821 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5824 h
= (struct elf_link_hash_entry
*) bh
;
5826 h
->type
= STT_OBJECT
;
5832 /* Set up the sizes and contents of the ELF dynamic sections. This is
5833 called by the ELF linker emulation before_allocation routine. We
5834 must set the sizes of the sections before the linker sets the
5835 addresses of the various sections. */
5838 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5841 const char *filter_shlib
,
5843 const char *depaudit
,
5844 const char * const *auxiliary_filters
,
5845 struct bfd_link_info
*info
,
5846 asection
**sinterpptr
)
5850 const struct elf_backend_data
*bed
;
5851 struct elf_info_failed asvinfo
;
5855 soname_indx
= (size_t) -1;
5857 if (!is_elf_hash_table (info
->hash
))
5860 bed
= get_elf_backend_data (output_bfd
);
5862 /* Any syms created from now on start with -1 in
5863 got.refcount/offset and plt.refcount/offset. */
5864 elf_hash_table (info
)->init_got_refcount
5865 = elf_hash_table (info
)->init_got_offset
;
5866 elf_hash_table (info
)->init_plt_refcount
5867 = elf_hash_table (info
)->init_plt_offset
;
5869 if (bfd_link_relocatable (info
)
5870 && !_bfd_elf_size_group_sections (info
))
5873 /* The backend may have to create some sections regardless of whether
5874 we're dynamic or not. */
5875 if (bed
->elf_backend_always_size_sections
5876 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5879 /* Determine any GNU_STACK segment requirements, after the backend
5880 has had a chance to set a default segment size. */
5881 if (info
->execstack
)
5882 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5883 else if (info
->noexecstack
)
5884 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5888 asection
*notesec
= NULL
;
5891 for (inputobj
= info
->input_bfds
;
5893 inputobj
= inputobj
->link
.next
)
5898 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5900 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5903 if (s
->flags
& SEC_CODE
)
5907 else if (bed
->default_execstack
)
5910 if (notesec
|| info
->stacksize
> 0)
5911 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5912 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5913 && notesec
->output_section
!= bfd_abs_section_ptr
)
5914 notesec
->output_section
->flags
|= SEC_CODE
;
5917 dynobj
= elf_hash_table (info
)->dynobj
;
5919 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5921 struct elf_info_failed eif
;
5922 struct elf_link_hash_entry
*h
;
5924 struct bfd_elf_version_tree
*t
;
5925 struct bfd_elf_version_expr
*d
;
5927 bfd_boolean all_defined
;
5929 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5930 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5934 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5936 if (soname_indx
== (size_t) -1
5937 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5943 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5945 info
->flags
|= DF_SYMBOLIC
;
5953 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5955 if (indx
== (size_t) -1)
5958 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5959 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5963 if (filter_shlib
!= NULL
)
5967 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5968 filter_shlib
, TRUE
);
5969 if (indx
== (size_t) -1
5970 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5974 if (auxiliary_filters
!= NULL
)
5976 const char * const *p
;
5978 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5982 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5984 if (indx
== (size_t) -1
5985 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5994 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5996 if (indx
== (size_t) -1
5997 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6001 if (depaudit
!= NULL
)
6005 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6007 if (indx
== (size_t) -1
6008 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6015 /* If we are supposed to export all symbols into the dynamic symbol
6016 table (this is not the normal case), then do so. */
6017 if (info
->export_dynamic
6018 || (bfd_link_executable (info
) && info
->dynamic
))
6020 elf_link_hash_traverse (elf_hash_table (info
),
6021 _bfd_elf_export_symbol
,
6027 /* Make all global versions with definition. */
6028 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6029 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6030 if (!d
->symver
&& d
->literal
)
6032 const char *verstr
, *name
;
6033 size_t namelen
, verlen
, newlen
;
6034 char *newname
, *p
, leading_char
;
6035 struct elf_link_hash_entry
*newh
;
6037 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6039 namelen
= strlen (name
) + (leading_char
!= '\0');
6041 verlen
= strlen (verstr
);
6042 newlen
= namelen
+ verlen
+ 3;
6044 newname
= (char *) bfd_malloc (newlen
);
6045 if (newname
== NULL
)
6047 newname
[0] = leading_char
;
6048 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6050 /* Check the hidden versioned definition. */
6051 p
= newname
+ namelen
;
6053 memcpy (p
, verstr
, verlen
+ 1);
6054 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6055 newname
, FALSE
, FALSE
,
6058 || (newh
->root
.type
!= bfd_link_hash_defined
6059 && newh
->root
.type
!= bfd_link_hash_defweak
))
6061 /* Check the default versioned definition. */
6063 memcpy (p
, verstr
, verlen
+ 1);
6064 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6065 newname
, FALSE
, FALSE
,
6070 /* Mark this version if there is a definition and it is
6071 not defined in a shared object. */
6073 && !newh
->def_dynamic
6074 && (newh
->root
.type
== bfd_link_hash_defined
6075 || newh
->root
.type
== bfd_link_hash_defweak
))
6079 /* Attach all the symbols to their version information. */
6080 asvinfo
.info
= info
;
6081 asvinfo
.failed
= FALSE
;
6083 elf_link_hash_traverse (elf_hash_table (info
),
6084 _bfd_elf_link_assign_sym_version
,
6089 if (!info
->allow_undefined_version
)
6091 /* Check if all global versions have a definition. */
6093 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6094 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6095 if (d
->literal
&& !d
->symver
&& !d
->script
)
6098 (_("%s: undefined version: %s"),
6099 d
->pattern
, t
->name
);
6100 all_defined
= FALSE
;
6105 bfd_set_error (bfd_error_bad_value
);
6110 /* Find all symbols which were defined in a dynamic object and make
6111 the backend pick a reasonable value for them. */
6112 elf_link_hash_traverse (elf_hash_table (info
),
6113 _bfd_elf_adjust_dynamic_symbol
,
6118 /* Add some entries to the .dynamic section. We fill in some of the
6119 values later, in bfd_elf_final_link, but we must add the entries
6120 now so that we know the final size of the .dynamic section. */
6122 /* If there are initialization and/or finalization functions to
6123 call then add the corresponding DT_INIT/DT_FINI entries. */
6124 h
= (info
->init_function
6125 ? elf_link_hash_lookup (elf_hash_table (info
),
6126 info
->init_function
, FALSE
,
6133 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6136 h
= (info
->fini_function
6137 ? elf_link_hash_lookup (elf_hash_table (info
),
6138 info
->fini_function
, FALSE
,
6145 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6149 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6150 if (s
!= NULL
&& s
->linker_has_input
)
6152 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6153 if (! bfd_link_executable (info
))
6158 for (sub
= info
->input_bfds
; sub
!= NULL
;
6159 sub
= sub
->link
.next
)
6160 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6161 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6162 if (elf_section_data (o
)->this_hdr
.sh_type
6163 == SHT_PREINIT_ARRAY
)
6166 (_("%B: .preinit_array section is not allowed in DSO"),
6171 bfd_set_error (bfd_error_nonrepresentable_section
);
6175 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6176 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6179 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6180 if (s
!= NULL
&& s
->linker_has_input
)
6182 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6183 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6186 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6187 if (s
!= NULL
&& s
->linker_has_input
)
6189 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6190 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6194 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6195 /* If .dynstr is excluded from the link, we don't want any of
6196 these tags. Strictly, we should be checking each section
6197 individually; This quick check covers for the case where
6198 someone does a /DISCARD/ : { *(*) }. */
6199 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6201 bfd_size_type strsize
;
6203 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6204 if ((info
->emit_hash
6205 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6206 || (info
->emit_gnu_hash
6207 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6208 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6209 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6210 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6211 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6212 bed
->s
->sizeof_sym
))
6217 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6220 /* The backend must work out the sizes of all the other dynamic
6223 && bed
->elf_backend_size_dynamic_sections
!= NULL
6224 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6227 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6229 unsigned long section_sym_count
;
6230 struct bfd_elf_version_tree
*verdefs
;
6233 /* Set up the version definition section. */
6234 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6235 BFD_ASSERT (s
!= NULL
);
6237 /* We may have created additional version definitions if we are
6238 just linking a regular application. */
6239 verdefs
= info
->version_info
;
6241 /* Skip anonymous version tag. */
6242 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6243 verdefs
= verdefs
->next
;
6245 if (verdefs
== NULL
&& !info
->create_default_symver
)
6246 s
->flags
|= SEC_EXCLUDE
;
6251 struct bfd_elf_version_tree
*t
;
6253 Elf_Internal_Verdef def
;
6254 Elf_Internal_Verdaux defaux
;
6255 struct bfd_link_hash_entry
*bh
;
6256 struct elf_link_hash_entry
*h
;
6262 /* Make space for the base version. */
6263 size
+= sizeof (Elf_External_Verdef
);
6264 size
+= sizeof (Elf_External_Verdaux
);
6267 /* Make space for the default version. */
6268 if (info
->create_default_symver
)
6270 size
+= sizeof (Elf_External_Verdef
);
6274 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6276 struct bfd_elf_version_deps
*n
;
6278 /* Don't emit base version twice. */
6282 size
+= sizeof (Elf_External_Verdef
);
6283 size
+= sizeof (Elf_External_Verdaux
);
6286 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6287 size
+= sizeof (Elf_External_Verdaux
);
6291 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6292 if (s
->contents
== NULL
&& s
->size
!= 0)
6295 /* Fill in the version definition section. */
6299 def
.vd_version
= VER_DEF_CURRENT
;
6300 def
.vd_flags
= VER_FLG_BASE
;
6303 if (info
->create_default_symver
)
6305 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6306 def
.vd_next
= sizeof (Elf_External_Verdef
);
6310 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6311 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6312 + sizeof (Elf_External_Verdaux
));
6315 if (soname_indx
!= (size_t) -1)
6317 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6319 def
.vd_hash
= bfd_elf_hash (soname
);
6320 defaux
.vda_name
= soname_indx
;
6327 name
= lbasename (output_bfd
->filename
);
6328 def
.vd_hash
= bfd_elf_hash (name
);
6329 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6331 if (indx
== (size_t) -1)
6333 defaux
.vda_name
= indx
;
6335 defaux
.vda_next
= 0;
6337 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6338 (Elf_External_Verdef
*) p
);
6339 p
+= sizeof (Elf_External_Verdef
);
6340 if (info
->create_default_symver
)
6342 /* Add a symbol representing this version. */
6344 if (! (_bfd_generic_link_add_one_symbol
6345 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6347 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6349 h
= (struct elf_link_hash_entry
*) bh
;
6352 h
->type
= STT_OBJECT
;
6353 h
->verinfo
.vertree
= NULL
;
6355 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6358 /* Create a duplicate of the base version with the same
6359 aux block, but different flags. */
6362 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6364 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6365 + sizeof (Elf_External_Verdaux
));
6368 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6369 (Elf_External_Verdef
*) p
);
6370 p
+= sizeof (Elf_External_Verdef
);
6372 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6373 (Elf_External_Verdaux
*) p
);
6374 p
+= sizeof (Elf_External_Verdaux
);
6376 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6379 struct bfd_elf_version_deps
*n
;
6381 /* Don't emit the base version twice. */
6386 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6389 /* Add a symbol representing this version. */
6391 if (! (_bfd_generic_link_add_one_symbol
6392 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6394 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6396 h
= (struct elf_link_hash_entry
*) bh
;
6399 h
->type
= STT_OBJECT
;
6400 h
->verinfo
.vertree
= t
;
6402 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6405 def
.vd_version
= VER_DEF_CURRENT
;
6407 if (t
->globals
.list
== NULL
6408 && t
->locals
.list
== NULL
6410 def
.vd_flags
|= VER_FLG_WEAK
;
6411 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6412 def
.vd_cnt
= cdeps
+ 1;
6413 def
.vd_hash
= bfd_elf_hash (t
->name
);
6414 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6417 /* If a basever node is next, it *must* be the last node in
6418 the chain, otherwise Verdef construction breaks. */
6419 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6420 BFD_ASSERT (t
->next
->next
== NULL
);
6422 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6423 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6424 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6426 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6427 (Elf_External_Verdef
*) p
);
6428 p
+= sizeof (Elf_External_Verdef
);
6430 defaux
.vda_name
= h
->dynstr_index
;
6431 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6433 defaux
.vda_next
= 0;
6434 if (t
->deps
!= NULL
)
6435 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6436 t
->name_indx
= defaux
.vda_name
;
6438 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6439 (Elf_External_Verdaux
*) p
);
6440 p
+= sizeof (Elf_External_Verdaux
);
6442 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6444 if (n
->version_needed
== NULL
)
6446 /* This can happen if there was an error in the
6448 defaux
.vda_name
= 0;
6452 defaux
.vda_name
= n
->version_needed
->name_indx
;
6453 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6456 if (n
->next
== NULL
)
6457 defaux
.vda_next
= 0;
6459 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6461 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6462 (Elf_External_Verdaux
*) p
);
6463 p
+= sizeof (Elf_External_Verdaux
);
6467 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6468 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6471 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6474 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6476 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6479 else if (info
->flags
& DF_BIND_NOW
)
6481 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6487 if (bfd_link_executable (info
))
6488 info
->flags_1
&= ~ (DF_1_INITFIRST
6491 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6495 /* Work out the size of the version reference section. */
6497 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6498 BFD_ASSERT (s
!= NULL
);
6500 struct elf_find_verdep_info sinfo
;
6503 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6504 if (sinfo
.vers
== 0)
6506 sinfo
.failed
= FALSE
;
6508 elf_link_hash_traverse (elf_hash_table (info
),
6509 _bfd_elf_link_find_version_dependencies
,
6514 if (elf_tdata (output_bfd
)->verref
== NULL
)
6515 s
->flags
|= SEC_EXCLUDE
;
6518 Elf_Internal_Verneed
*t
;
6523 /* Build the version dependency section. */
6526 for (t
= elf_tdata (output_bfd
)->verref
;
6530 Elf_Internal_Vernaux
*a
;
6532 size
+= sizeof (Elf_External_Verneed
);
6534 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6535 size
+= sizeof (Elf_External_Vernaux
);
6539 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6540 if (s
->contents
== NULL
)
6544 for (t
= elf_tdata (output_bfd
)->verref
;
6549 Elf_Internal_Vernaux
*a
;
6553 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6556 t
->vn_version
= VER_NEED_CURRENT
;
6558 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6559 elf_dt_name (t
->vn_bfd
) != NULL
6560 ? elf_dt_name (t
->vn_bfd
)
6561 : lbasename (t
->vn_bfd
->filename
),
6563 if (indx
== (size_t) -1)
6566 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6567 if (t
->vn_nextref
== NULL
)
6570 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6571 + caux
* sizeof (Elf_External_Vernaux
));
6573 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6574 (Elf_External_Verneed
*) p
);
6575 p
+= sizeof (Elf_External_Verneed
);
6577 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6579 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6580 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6581 a
->vna_nodename
, FALSE
);
6582 if (indx
== (size_t) -1)
6585 if (a
->vna_nextptr
== NULL
)
6588 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6590 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6591 (Elf_External_Vernaux
*) p
);
6592 p
+= sizeof (Elf_External_Vernaux
);
6596 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6597 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6600 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6604 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6605 && elf_tdata (output_bfd
)->cverdefs
== 0)
6606 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6607 §ion_sym_count
) == 0)
6609 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6610 s
->flags
|= SEC_EXCLUDE
;
6616 /* Find the first non-excluded output section. We'll use its
6617 section symbol for some emitted relocs. */
6619 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6623 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6624 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6625 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6627 elf_hash_table (info
)->text_index_section
= s
;
6632 /* Find two non-excluded output sections, one for code, one for data.
6633 We'll use their section symbols for some emitted relocs. */
6635 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6639 /* Data first, since setting text_index_section changes
6640 _bfd_elf_link_omit_section_dynsym. */
6641 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6642 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6643 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6645 elf_hash_table (info
)->data_index_section
= s
;
6649 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6650 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6651 == (SEC_ALLOC
| SEC_READONLY
))
6652 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6654 elf_hash_table (info
)->text_index_section
= s
;
6658 if (elf_hash_table (info
)->text_index_section
== NULL
)
6659 elf_hash_table (info
)->text_index_section
6660 = elf_hash_table (info
)->data_index_section
;
6664 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6666 const struct elf_backend_data
*bed
;
6668 if (!is_elf_hash_table (info
->hash
))
6671 bed
= get_elf_backend_data (output_bfd
);
6672 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6674 if (elf_hash_table (info
)->dynamic_sections_created
)
6678 bfd_size_type dynsymcount
;
6679 unsigned long section_sym_count
;
6680 unsigned int dtagcount
;
6682 dynobj
= elf_hash_table (info
)->dynobj
;
6684 /* Assign dynsym indicies. In a shared library we generate a
6685 section symbol for each output section, which come first.
6686 Next come all of the back-end allocated local dynamic syms,
6687 followed by the rest of the global symbols. */
6689 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6690 §ion_sym_count
);
6692 /* Work out the size of the symbol version section. */
6693 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6694 BFD_ASSERT (s
!= NULL
);
6695 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6697 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6698 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6699 if (s
->contents
== NULL
)
6702 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6706 /* Set the size of the .dynsym and .hash sections. We counted
6707 the number of dynamic symbols in elf_link_add_object_symbols.
6708 We will build the contents of .dynsym and .hash when we build
6709 the final symbol table, because until then we do not know the
6710 correct value to give the symbols. We built the .dynstr
6711 section as we went along in elf_link_add_object_symbols. */
6712 s
= elf_hash_table (info
)->dynsym
;
6713 BFD_ASSERT (s
!= NULL
);
6714 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6716 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6717 if (s
->contents
== NULL
)
6720 /* The first entry in .dynsym is a dummy symbol. Clear all the
6721 section syms, in case we don't output them all. */
6722 ++section_sym_count
;
6723 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6725 elf_hash_table (info
)->bucketcount
= 0;
6727 /* Compute the size of the hashing table. As a side effect this
6728 computes the hash values for all the names we export. */
6729 if (info
->emit_hash
)
6731 unsigned long int *hashcodes
;
6732 struct hash_codes_info hashinf
;
6734 unsigned long int nsyms
;
6736 size_t hash_entry_size
;
6738 /* Compute the hash values for all exported symbols. At the same
6739 time store the values in an array so that we could use them for
6741 amt
= dynsymcount
* sizeof (unsigned long int);
6742 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6743 if (hashcodes
== NULL
)
6745 hashinf
.hashcodes
= hashcodes
;
6746 hashinf
.error
= FALSE
;
6748 /* Put all hash values in HASHCODES. */
6749 elf_link_hash_traverse (elf_hash_table (info
),
6750 elf_collect_hash_codes
, &hashinf
);
6757 nsyms
= hashinf
.hashcodes
- hashcodes
;
6759 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6762 if (bucketcount
== 0)
6765 elf_hash_table (info
)->bucketcount
= bucketcount
;
6767 s
= bfd_get_linker_section (dynobj
, ".hash");
6768 BFD_ASSERT (s
!= NULL
);
6769 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6770 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6771 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6772 if (s
->contents
== NULL
)
6775 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6776 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6777 s
->contents
+ hash_entry_size
);
6780 if (info
->emit_gnu_hash
)
6783 unsigned char *contents
;
6784 struct collect_gnu_hash_codes cinfo
;
6788 memset (&cinfo
, 0, sizeof (cinfo
));
6790 /* Compute the hash values for all exported symbols. At the same
6791 time store the values in an array so that we could use them for
6793 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6794 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6795 if (cinfo
.hashcodes
== NULL
)
6798 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6799 cinfo
.min_dynindx
= -1;
6800 cinfo
.output_bfd
= output_bfd
;
6803 /* Put all hash values in HASHCODES. */
6804 elf_link_hash_traverse (elf_hash_table (info
),
6805 elf_collect_gnu_hash_codes
, &cinfo
);
6808 free (cinfo
.hashcodes
);
6813 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6815 if (bucketcount
== 0)
6817 free (cinfo
.hashcodes
);
6821 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6822 BFD_ASSERT (s
!= NULL
);
6824 if (cinfo
.nsyms
== 0)
6826 /* Empty .gnu.hash section is special. */
6827 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6828 free (cinfo
.hashcodes
);
6829 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6830 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6831 if (contents
== NULL
)
6833 s
->contents
= contents
;
6834 /* 1 empty bucket. */
6835 bfd_put_32 (output_bfd
, 1, contents
);
6836 /* SYMIDX above the special symbol 0. */
6837 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6838 /* Just one word for bitmask. */
6839 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6840 /* Only hash fn bloom filter. */
6841 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6842 /* No hashes are valid - empty bitmask. */
6843 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6844 /* No hashes in the only bucket. */
6845 bfd_put_32 (output_bfd
, 0,
6846 contents
+ 16 + bed
->s
->arch_size
/ 8);
6850 unsigned long int maskwords
, maskbitslog2
, x
;
6851 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6855 while ((x
>>= 1) != 0)
6857 if (maskbitslog2
< 3)
6859 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6860 maskbitslog2
= maskbitslog2
+ 3;
6862 maskbitslog2
= maskbitslog2
+ 2;
6863 if (bed
->s
->arch_size
== 64)
6865 if (maskbitslog2
== 5)
6871 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6872 cinfo
.shift2
= maskbitslog2
;
6873 cinfo
.maskbits
= 1 << maskbitslog2
;
6874 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6875 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6876 amt
+= maskwords
* sizeof (bfd_vma
);
6877 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6878 if (cinfo
.bitmask
== NULL
)
6880 free (cinfo
.hashcodes
);
6884 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6885 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6886 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6887 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6889 /* Determine how often each hash bucket is used. */
6890 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6891 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6892 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6894 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6895 if (cinfo
.counts
[i
] != 0)
6897 cinfo
.indx
[i
] = cnt
;
6898 cnt
+= cinfo
.counts
[i
];
6900 BFD_ASSERT (cnt
== dynsymcount
);
6901 cinfo
.bucketcount
= bucketcount
;
6902 cinfo
.local_indx
= cinfo
.min_dynindx
;
6904 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6905 s
->size
+= cinfo
.maskbits
/ 8;
6906 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6907 if (contents
== NULL
)
6909 free (cinfo
.bitmask
);
6910 free (cinfo
.hashcodes
);
6914 s
->contents
= contents
;
6915 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6916 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6917 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6918 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6919 contents
+= 16 + cinfo
.maskbits
/ 8;
6921 for (i
= 0; i
< bucketcount
; ++i
)
6923 if (cinfo
.counts
[i
] == 0)
6924 bfd_put_32 (output_bfd
, 0, contents
);
6926 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6930 cinfo
.contents
= contents
;
6932 /* Renumber dynamic symbols, populate .gnu.hash section. */
6933 elf_link_hash_traverse (elf_hash_table (info
),
6934 elf_renumber_gnu_hash_syms
, &cinfo
);
6936 contents
= s
->contents
+ 16;
6937 for (i
= 0; i
< maskwords
; ++i
)
6939 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6941 contents
+= bed
->s
->arch_size
/ 8;
6944 free (cinfo
.bitmask
);
6945 free (cinfo
.hashcodes
);
6949 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6950 BFD_ASSERT (s
!= NULL
);
6952 elf_finalize_dynstr (output_bfd
, info
);
6954 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6956 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6957 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6964 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6967 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6970 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6971 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6974 /* Finish SHF_MERGE section merging. */
6977 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6982 if (!is_elf_hash_table (info
->hash
))
6985 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6986 if ((ibfd
->flags
& DYNAMIC
) == 0
6987 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6988 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6989 == get_elf_backend_data (obfd
)->s
->elfclass
))
6990 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6991 if ((sec
->flags
& SEC_MERGE
) != 0
6992 && !bfd_is_abs_section (sec
->output_section
))
6994 struct bfd_elf_section_data
*secdata
;
6996 secdata
= elf_section_data (sec
);
6997 if (! _bfd_add_merge_section (obfd
,
6998 &elf_hash_table (info
)->merge_info
,
6999 sec
, &secdata
->sec_info
))
7001 else if (secdata
->sec_info
)
7002 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7005 if (elf_hash_table (info
)->merge_info
!= NULL
)
7006 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7007 merge_sections_remove_hook
);
7011 /* Create an entry in an ELF linker hash table. */
7013 struct bfd_hash_entry
*
7014 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7015 struct bfd_hash_table
*table
,
7018 /* Allocate the structure if it has not already been allocated by a
7022 entry
= (struct bfd_hash_entry
*)
7023 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7028 /* Call the allocation method of the superclass. */
7029 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7032 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7033 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7035 /* Set local fields. */
7038 ret
->got
= htab
->init_got_refcount
;
7039 ret
->plt
= htab
->init_plt_refcount
;
7040 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7041 - offsetof (struct elf_link_hash_entry
, size
)));
7042 /* Assume that we have been called by a non-ELF symbol reader.
7043 This flag is then reset by the code which reads an ELF input
7044 file. This ensures that a symbol created by a non-ELF symbol
7045 reader will have the flag set correctly. */
7052 /* Copy data from an indirect symbol to its direct symbol, hiding the
7053 old indirect symbol. Also used for copying flags to a weakdef. */
7056 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7057 struct elf_link_hash_entry
*dir
,
7058 struct elf_link_hash_entry
*ind
)
7060 struct elf_link_hash_table
*htab
;
7062 /* Copy down any references that we may have already seen to the
7063 symbol which just became indirect if DIR isn't a hidden versioned
7066 if (dir
->versioned
!= versioned_hidden
)
7068 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7069 dir
->ref_regular
|= ind
->ref_regular
;
7070 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7071 dir
->non_got_ref
|= ind
->non_got_ref
;
7072 dir
->needs_plt
|= ind
->needs_plt
;
7073 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7076 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7079 /* Copy over the global and procedure linkage table refcount entries.
7080 These may have been already set up by a check_relocs routine. */
7081 htab
= elf_hash_table (info
);
7082 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7084 if (dir
->got
.refcount
< 0)
7085 dir
->got
.refcount
= 0;
7086 dir
->got
.refcount
+= ind
->got
.refcount
;
7087 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7090 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7092 if (dir
->plt
.refcount
< 0)
7093 dir
->plt
.refcount
= 0;
7094 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7095 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7098 if (ind
->dynindx
!= -1)
7100 if (dir
->dynindx
!= -1)
7101 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7102 dir
->dynindx
= ind
->dynindx
;
7103 dir
->dynstr_index
= ind
->dynstr_index
;
7105 ind
->dynstr_index
= 0;
7110 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7111 struct elf_link_hash_entry
*h
,
7112 bfd_boolean force_local
)
7114 /* STT_GNU_IFUNC symbol must go through PLT. */
7115 if (h
->type
!= STT_GNU_IFUNC
)
7117 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7122 h
->forced_local
= 1;
7123 if (h
->dynindx
!= -1)
7126 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7132 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7136 _bfd_elf_link_hash_table_init
7137 (struct elf_link_hash_table
*table
,
7139 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7140 struct bfd_hash_table
*,
7142 unsigned int entsize
,
7143 enum elf_target_id target_id
)
7146 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7148 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7149 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7150 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7151 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7152 /* The first dynamic symbol is a dummy. */
7153 table
->dynsymcount
= 1;
7155 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7157 table
->root
.type
= bfd_link_elf_hash_table
;
7158 table
->hash_table_id
= target_id
;
7163 /* Create an ELF linker hash table. */
7165 struct bfd_link_hash_table
*
7166 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7168 struct elf_link_hash_table
*ret
;
7169 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7171 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7175 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7176 sizeof (struct elf_link_hash_entry
),
7182 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7187 /* Destroy an ELF linker hash table. */
7190 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7192 struct elf_link_hash_table
*htab
;
7194 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7195 if (htab
->dynstr
!= NULL
)
7196 _bfd_elf_strtab_free (htab
->dynstr
);
7197 _bfd_merge_sections_free (htab
->merge_info
);
7198 _bfd_generic_link_hash_table_free (obfd
);
7201 /* This is a hook for the ELF emulation code in the generic linker to
7202 tell the backend linker what file name to use for the DT_NEEDED
7203 entry for a dynamic object. */
7206 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7208 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7209 && bfd_get_format (abfd
) == bfd_object
)
7210 elf_dt_name (abfd
) = name
;
7214 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7217 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7218 && bfd_get_format (abfd
) == bfd_object
)
7219 lib_class
= elf_dyn_lib_class (abfd
);
7226 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7228 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7229 && bfd_get_format (abfd
) == bfd_object
)
7230 elf_dyn_lib_class (abfd
) = lib_class
;
7233 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7234 the linker ELF emulation code. */
7236 struct bfd_link_needed_list
*
7237 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7238 struct bfd_link_info
*info
)
7240 if (! is_elf_hash_table (info
->hash
))
7242 return elf_hash_table (info
)->needed
;
7245 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7246 hook for the linker ELF emulation code. */
7248 struct bfd_link_needed_list
*
7249 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7250 struct bfd_link_info
*info
)
7252 if (! is_elf_hash_table (info
->hash
))
7254 return elf_hash_table (info
)->runpath
;
7257 /* Get the name actually used for a dynamic object for a link. This
7258 is the SONAME entry if there is one. Otherwise, it is the string
7259 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7262 bfd_elf_get_dt_soname (bfd
*abfd
)
7264 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7265 && bfd_get_format (abfd
) == bfd_object
)
7266 return elf_dt_name (abfd
);
7270 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7271 the ELF linker emulation code. */
7274 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7275 struct bfd_link_needed_list
**pneeded
)
7278 bfd_byte
*dynbuf
= NULL
;
7279 unsigned int elfsec
;
7280 unsigned long shlink
;
7281 bfd_byte
*extdyn
, *extdynend
;
7283 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7287 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7288 || bfd_get_format (abfd
) != bfd_object
)
7291 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7292 if (s
== NULL
|| s
->size
== 0)
7295 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7298 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7299 if (elfsec
== SHN_BAD
)
7302 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7304 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7305 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7308 extdynend
= extdyn
+ s
->size
;
7309 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7311 Elf_Internal_Dyn dyn
;
7313 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7315 if (dyn
.d_tag
== DT_NULL
)
7318 if (dyn
.d_tag
== DT_NEEDED
)
7321 struct bfd_link_needed_list
*l
;
7322 unsigned int tagv
= dyn
.d_un
.d_val
;
7325 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7330 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7351 struct elf_symbuf_symbol
7353 unsigned long st_name
; /* Symbol name, index in string tbl */
7354 unsigned char st_info
; /* Type and binding attributes */
7355 unsigned char st_other
; /* Visibilty, and target specific */
7358 struct elf_symbuf_head
7360 struct elf_symbuf_symbol
*ssym
;
7362 unsigned int st_shndx
;
7369 Elf_Internal_Sym
*isym
;
7370 struct elf_symbuf_symbol
*ssym
;
7375 /* Sort references to symbols by ascending section number. */
7378 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7380 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7381 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7383 return s1
->st_shndx
- s2
->st_shndx
;
7387 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7389 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7390 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7391 return strcmp (s1
->name
, s2
->name
);
7394 static struct elf_symbuf_head
*
7395 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7397 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7398 struct elf_symbuf_symbol
*ssym
;
7399 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7400 size_t i
, shndx_count
, total_size
;
7402 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7406 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7407 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7408 *ind
++ = &isymbuf
[i
];
7411 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7412 elf_sort_elf_symbol
);
7415 if (indbufend
> indbuf
)
7416 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7417 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7420 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7421 + (indbufend
- indbuf
) * sizeof (*ssym
));
7422 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7423 if (ssymbuf
== NULL
)
7429 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7430 ssymbuf
->ssym
= NULL
;
7431 ssymbuf
->count
= shndx_count
;
7432 ssymbuf
->st_shndx
= 0;
7433 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7435 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7438 ssymhead
->ssym
= ssym
;
7439 ssymhead
->count
= 0;
7440 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7442 ssym
->st_name
= (*ind
)->st_name
;
7443 ssym
->st_info
= (*ind
)->st_info
;
7444 ssym
->st_other
= (*ind
)->st_other
;
7447 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7448 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7455 /* Check if 2 sections define the same set of local and global
7459 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7460 struct bfd_link_info
*info
)
7463 const struct elf_backend_data
*bed1
, *bed2
;
7464 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7465 size_t symcount1
, symcount2
;
7466 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7467 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7468 Elf_Internal_Sym
*isym
, *isymend
;
7469 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7470 size_t count1
, count2
, i
;
7471 unsigned int shndx1
, shndx2
;
7477 /* Both sections have to be in ELF. */
7478 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7479 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7482 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7485 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7486 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7487 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7490 bed1
= get_elf_backend_data (bfd1
);
7491 bed2
= get_elf_backend_data (bfd2
);
7492 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7493 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7494 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7495 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7497 if (symcount1
== 0 || symcount2
== 0)
7503 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7504 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7506 if (ssymbuf1
== NULL
)
7508 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7510 if (isymbuf1
== NULL
)
7513 if (!info
->reduce_memory_overheads
)
7514 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7515 = elf_create_symbuf (symcount1
, isymbuf1
);
7518 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7520 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7522 if (isymbuf2
== NULL
)
7525 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7526 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7527 = elf_create_symbuf (symcount2
, isymbuf2
);
7530 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7532 /* Optimized faster version. */
7534 struct elf_symbol
*symp
;
7535 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7538 hi
= ssymbuf1
->count
;
7543 mid
= (lo
+ hi
) / 2;
7544 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7546 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7550 count1
= ssymbuf1
[mid
].count
;
7557 hi
= ssymbuf2
->count
;
7562 mid
= (lo
+ hi
) / 2;
7563 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7565 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7569 count2
= ssymbuf2
[mid
].count
;
7575 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7579 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7581 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7582 if (symtable1
== NULL
|| symtable2
== NULL
)
7586 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7587 ssym
< ssymend
; ssym
++, symp
++)
7589 symp
->u
.ssym
= ssym
;
7590 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7596 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7597 ssym
< ssymend
; ssym
++, symp
++)
7599 symp
->u
.ssym
= ssym
;
7600 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7605 /* Sort symbol by name. */
7606 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7607 elf_sym_name_compare
);
7608 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7609 elf_sym_name_compare
);
7611 for (i
= 0; i
< count1
; i
++)
7612 /* Two symbols must have the same binding, type and name. */
7613 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7614 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7615 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7622 symtable1
= (struct elf_symbol
*)
7623 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7624 symtable2
= (struct elf_symbol
*)
7625 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7626 if (symtable1
== NULL
|| symtable2
== NULL
)
7629 /* Count definitions in the section. */
7631 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7632 if (isym
->st_shndx
== shndx1
)
7633 symtable1
[count1
++].u
.isym
= isym
;
7636 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7637 if (isym
->st_shndx
== shndx2
)
7638 symtable2
[count2
++].u
.isym
= isym
;
7640 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7643 for (i
= 0; i
< count1
; i
++)
7645 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7646 symtable1
[i
].u
.isym
->st_name
);
7648 for (i
= 0; i
< count2
; i
++)
7650 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7651 symtable2
[i
].u
.isym
->st_name
);
7653 /* Sort symbol by name. */
7654 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7655 elf_sym_name_compare
);
7656 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7657 elf_sym_name_compare
);
7659 for (i
= 0; i
< count1
; i
++)
7660 /* Two symbols must have the same binding, type and name. */
7661 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7662 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7663 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7681 /* Return TRUE if 2 section types are compatible. */
7684 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7685 bfd
*bbfd
, const asection
*bsec
)
7689 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7690 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7693 return elf_section_type (asec
) == elf_section_type (bsec
);
7696 /* Final phase of ELF linker. */
7698 /* A structure we use to avoid passing large numbers of arguments. */
7700 struct elf_final_link_info
7702 /* General link information. */
7703 struct bfd_link_info
*info
;
7706 /* Symbol string table. */
7707 struct elf_strtab_hash
*symstrtab
;
7708 /* .hash section. */
7710 /* symbol version section (.gnu.version). */
7711 asection
*symver_sec
;
7712 /* Buffer large enough to hold contents of any section. */
7714 /* Buffer large enough to hold external relocs of any section. */
7715 void *external_relocs
;
7716 /* Buffer large enough to hold internal relocs of any section. */
7717 Elf_Internal_Rela
*internal_relocs
;
7718 /* Buffer large enough to hold external local symbols of any input
7720 bfd_byte
*external_syms
;
7721 /* And a buffer for symbol section indices. */
7722 Elf_External_Sym_Shndx
*locsym_shndx
;
7723 /* Buffer large enough to hold internal local symbols of any input
7725 Elf_Internal_Sym
*internal_syms
;
7726 /* Array large enough to hold a symbol index for each local symbol
7727 of any input BFD. */
7729 /* Array large enough to hold a section pointer for each local
7730 symbol of any input BFD. */
7731 asection
**sections
;
7732 /* Buffer for SHT_SYMTAB_SHNDX section. */
7733 Elf_External_Sym_Shndx
*symshndxbuf
;
7734 /* Number of STT_FILE syms seen. */
7735 size_t filesym_count
;
7738 /* This struct is used to pass information to elf_link_output_extsym. */
7740 struct elf_outext_info
7743 bfd_boolean localsyms
;
7744 bfd_boolean file_sym_done
;
7745 struct elf_final_link_info
*flinfo
;
7749 /* Support for evaluating a complex relocation.
7751 Complex relocations are generalized, self-describing relocations. The
7752 implementation of them consists of two parts: complex symbols, and the
7753 relocations themselves.
7755 The relocations are use a reserved elf-wide relocation type code (R_RELC
7756 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7757 information (start bit, end bit, word width, etc) into the addend. This
7758 information is extracted from CGEN-generated operand tables within gas.
7760 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7761 internal) representing prefix-notation expressions, including but not
7762 limited to those sorts of expressions normally encoded as addends in the
7763 addend field. The symbol mangling format is:
7766 | <unary-operator> ':' <node>
7767 | <binary-operator> ':' <node> ':' <node>
7770 <literal> := 's' <digits=N> ':' <N character symbol name>
7771 | 'S' <digits=N> ':' <N character section name>
7775 <binary-operator> := as in C
7776 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7779 set_symbol_value (bfd
*bfd_with_globals
,
7780 Elf_Internal_Sym
*isymbuf
,
7785 struct elf_link_hash_entry
**sym_hashes
;
7786 struct elf_link_hash_entry
*h
;
7787 size_t extsymoff
= locsymcount
;
7789 if (symidx
< locsymcount
)
7791 Elf_Internal_Sym
*sym
;
7793 sym
= isymbuf
+ symidx
;
7794 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7796 /* It is a local symbol: move it to the
7797 "absolute" section and give it a value. */
7798 sym
->st_shndx
= SHN_ABS
;
7799 sym
->st_value
= val
;
7802 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7806 /* It is a global symbol: set its link type
7807 to "defined" and give it a value. */
7809 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7810 h
= sym_hashes
[symidx
- extsymoff
];
7811 while (h
->root
.type
== bfd_link_hash_indirect
7812 || h
->root
.type
== bfd_link_hash_warning
)
7813 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7814 h
->root
.type
= bfd_link_hash_defined
;
7815 h
->root
.u
.def
.value
= val
;
7816 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7820 resolve_symbol (const char *name
,
7822 struct elf_final_link_info
*flinfo
,
7824 Elf_Internal_Sym
*isymbuf
,
7827 Elf_Internal_Sym
*sym
;
7828 struct bfd_link_hash_entry
*global_entry
;
7829 const char *candidate
= NULL
;
7830 Elf_Internal_Shdr
*symtab_hdr
;
7833 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7835 for (i
= 0; i
< locsymcount
; ++ i
)
7839 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7842 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7843 symtab_hdr
->sh_link
,
7846 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7847 name
, candidate
, (unsigned long) sym
->st_value
);
7849 if (candidate
&& strcmp (candidate
, name
) == 0)
7851 asection
*sec
= flinfo
->sections
[i
];
7853 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7854 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7856 printf ("Found symbol with value %8.8lx\n",
7857 (unsigned long) *result
);
7863 /* Hmm, haven't found it yet. perhaps it is a global. */
7864 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7865 FALSE
, FALSE
, TRUE
);
7869 if (global_entry
->type
== bfd_link_hash_defined
7870 || global_entry
->type
== bfd_link_hash_defweak
)
7872 *result
= (global_entry
->u
.def
.value
7873 + global_entry
->u
.def
.section
->output_section
->vma
7874 + global_entry
->u
.def
.section
->output_offset
);
7876 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7877 global_entry
->root
.string
, (unsigned long) *result
);
7885 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7886 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7887 names like "foo.end" which is the end address of section "foo". */
7890 resolve_section (const char *name
,
7898 for (curr
= sections
; curr
; curr
= curr
->next
)
7899 if (strcmp (curr
->name
, name
) == 0)
7901 *result
= curr
->vma
;
7905 /* Hmm. still haven't found it. try pseudo-section names. */
7906 /* FIXME: This could be coded more efficiently... */
7907 for (curr
= sections
; curr
; curr
= curr
->next
)
7909 len
= strlen (curr
->name
);
7910 if (len
> strlen (name
))
7913 if (strncmp (curr
->name
, name
, len
) == 0)
7915 if (strncmp (".end", name
+ len
, 4) == 0)
7917 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7921 /* Insert more pseudo-section names here, if you like. */
7929 undefined_reference (const char *reftype
, const char *name
)
7931 /* xgettext:c-format */
7932 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7937 eval_symbol (bfd_vma
*result
,
7940 struct elf_final_link_info
*flinfo
,
7942 Elf_Internal_Sym
*isymbuf
,
7951 const char *sym
= *symp
;
7953 bfd_boolean symbol_is_section
= FALSE
;
7958 if (len
< 1 || len
> sizeof (symbuf
))
7960 bfd_set_error (bfd_error_invalid_operation
);
7973 *result
= strtoul (sym
, (char **) symp
, 16);
7977 symbol_is_section
= TRUE
;
7981 symlen
= strtol (sym
, (char **) symp
, 10);
7982 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7984 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7986 bfd_set_error (bfd_error_invalid_operation
);
7990 memcpy (symbuf
, sym
, symlen
);
7991 symbuf
[symlen
] = '\0';
7992 *symp
= sym
+ symlen
;
7994 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7995 the symbol as a section, or vice-versa. so we're pretty liberal in our
7996 interpretation here; section means "try section first", not "must be a
7997 section", and likewise with symbol. */
7999 if (symbol_is_section
)
8001 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8002 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8003 isymbuf
, locsymcount
))
8005 undefined_reference ("section", symbuf
);
8011 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8012 isymbuf
, locsymcount
)
8013 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8016 undefined_reference ("symbol", symbuf
);
8023 /* All that remains are operators. */
8025 #define UNARY_OP(op) \
8026 if (strncmp (sym, #op, strlen (#op)) == 0) \
8028 sym += strlen (#op); \
8032 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8033 isymbuf, locsymcount, signed_p)) \
8036 *result = op ((bfd_signed_vma) a); \
8042 #define BINARY_OP(op) \
8043 if (strncmp (sym, #op, strlen (#op)) == 0) \
8045 sym += strlen (#op); \
8049 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8050 isymbuf, locsymcount, signed_p)) \
8053 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8054 isymbuf, locsymcount, signed_p)) \
8057 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8087 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8088 bfd_set_error (bfd_error_invalid_operation
);
8094 put_value (bfd_vma size
,
8095 unsigned long chunksz
,
8100 location
+= (size
- chunksz
);
8102 for (; size
; size
-= chunksz
, location
-= chunksz
)
8107 bfd_put_8 (input_bfd
, x
, location
);
8111 bfd_put_16 (input_bfd
, x
, location
);
8115 bfd_put_32 (input_bfd
, x
, location
);
8116 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8122 bfd_put_64 (input_bfd
, x
, location
);
8123 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8136 get_value (bfd_vma size
,
8137 unsigned long chunksz
,
8144 /* Sanity checks. */
8145 BFD_ASSERT (chunksz
<= sizeof (x
)
8148 && (size
% chunksz
) == 0
8149 && input_bfd
!= NULL
8150 && location
!= NULL
);
8152 if (chunksz
== sizeof (x
))
8154 BFD_ASSERT (size
== chunksz
);
8156 /* Make sure that we do not perform an undefined shift operation.
8157 We know that size == chunksz so there will only be one iteration
8158 of the loop below. */
8162 shift
= 8 * chunksz
;
8164 for (; size
; size
-= chunksz
, location
+= chunksz
)
8169 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8172 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8175 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8179 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8190 decode_complex_addend (unsigned long *start
, /* in bits */
8191 unsigned long *oplen
, /* in bits */
8192 unsigned long *len
, /* in bits */
8193 unsigned long *wordsz
, /* in bytes */
8194 unsigned long *chunksz
, /* in bytes */
8195 unsigned long *lsb0_p
,
8196 unsigned long *signed_p
,
8197 unsigned long *trunc_p
,
8198 unsigned long encoded
)
8200 * start
= encoded
& 0x3F;
8201 * len
= (encoded
>> 6) & 0x3F;
8202 * oplen
= (encoded
>> 12) & 0x3F;
8203 * wordsz
= (encoded
>> 18) & 0xF;
8204 * chunksz
= (encoded
>> 22) & 0xF;
8205 * lsb0_p
= (encoded
>> 27) & 1;
8206 * signed_p
= (encoded
>> 28) & 1;
8207 * trunc_p
= (encoded
>> 29) & 1;
8210 bfd_reloc_status_type
8211 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8212 asection
*input_section ATTRIBUTE_UNUSED
,
8214 Elf_Internal_Rela
*rel
,
8217 bfd_vma shift
, x
, mask
;
8218 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8219 bfd_reloc_status_type r
;
8221 /* Perform this reloc, since it is complex.
8222 (this is not to say that it necessarily refers to a complex
8223 symbol; merely that it is a self-describing CGEN based reloc.
8224 i.e. the addend has the complete reloc information (bit start, end,
8225 word size, etc) encoded within it.). */
8227 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8228 &chunksz
, &lsb0_p
, &signed_p
,
8229 &trunc_p
, rel
->r_addend
);
8231 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8234 shift
= (start
+ 1) - len
;
8236 shift
= (8 * wordsz
) - (start
+ len
);
8238 x
= get_value (wordsz
, chunksz
, input_bfd
,
8239 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8242 printf ("Doing complex reloc: "
8243 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8244 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8245 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8246 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8247 oplen
, (unsigned long) x
, (unsigned long) mask
,
8248 (unsigned long) relocation
);
8253 /* Now do an overflow check. */
8254 r
= bfd_check_overflow ((signed_p
8255 ? complain_overflow_signed
8256 : complain_overflow_unsigned
),
8257 len
, 0, (8 * wordsz
),
8261 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8264 printf (" relocation: %8.8lx\n"
8265 " shifted mask: %8.8lx\n"
8266 " shifted/masked reloc: %8.8lx\n"
8267 " result: %8.8lx\n",
8268 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8269 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8271 put_value (wordsz
, chunksz
, input_bfd
, x
,
8272 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8276 /* Functions to read r_offset from external (target order) reloc
8277 entry. Faster than bfd_getl32 et al, because we let the compiler
8278 know the value is aligned. */
8281 ext32l_r_offset (const void *p
)
8288 const union aligned32
*a
8289 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8291 uint32_t aval
= ( (uint32_t) a
->c
[0]
8292 | (uint32_t) a
->c
[1] << 8
8293 | (uint32_t) a
->c
[2] << 16
8294 | (uint32_t) a
->c
[3] << 24);
8299 ext32b_r_offset (const void *p
)
8306 const union aligned32
*a
8307 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8309 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8310 | (uint32_t) a
->c
[1] << 16
8311 | (uint32_t) a
->c
[2] << 8
8312 | (uint32_t) a
->c
[3]);
8316 #ifdef BFD_HOST_64_BIT
8318 ext64l_r_offset (const void *p
)
8325 const union aligned64
*a
8326 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8328 uint64_t aval
= ( (uint64_t) a
->c
[0]
8329 | (uint64_t) a
->c
[1] << 8
8330 | (uint64_t) a
->c
[2] << 16
8331 | (uint64_t) a
->c
[3] << 24
8332 | (uint64_t) a
->c
[4] << 32
8333 | (uint64_t) a
->c
[5] << 40
8334 | (uint64_t) a
->c
[6] << 48
8335 | (uint64_t) a
->c
[7] << 56);
8340 ext64b_r_offset (const void *p
)
8347 const union aligned64
*a
8348 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8350 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8351 | (uint64_t) a
->c
[1] << 48
8352 | (uint64_t) a
->c
[2] << 40
8353 | (uint64_t) a
->c
[3] << 32
8354 | (uint64_t) a
->c
[4] << 24
8355 | (uint64_t) a
->c
[5] << 16
8356 | (uint64_t) a
->c
[6] << 8
8357 | (uint64_t) a
->c
[7]);
8362 /* When performing a relocatable link, the input relocations are
8363 preserved. But, if they reference global symbols, the indices
8364 referenced must be updated. Update all the relocations found in
8368 elf_link_adjust_relocs (bfd
*abfd
,
8370 struct bfd_elf_section_reloc_data
*reldata
,
8374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8376 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8377 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8378 bfd_vma r_type_mask
;
8380 unsigned int count
= reldata
->count
;
8381 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8383 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8385 swap_in
= bed
->s
->swap_reloc_in
;
8386 swap_out
= bed
->s
->swap_reloc_out
;
8388 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8390 swap_in
= bed
->s
->swap_reloca_in
;
8391 swap_out
= bed
->s
->swap_reloca_out
;
8396 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8399 if (bed
->s
->arch_size
== 32)
8406 r_type_mask
= 0xffffffff;
8410 erela
= reldata
->hdr
->contents
;
8411 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8413 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8416 if (*rel_hash
== NULL
)
8419 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8421 (*swap_in
) (abfd
, erela
, irela
);
8422 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8423 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8424 | (irela
[j
].r_info
& r_type_mask
));
8425 (*swap_out
) (abfd
, irela
, erela
);
8428 if (bed
->elf_backend_update_relocs
)
8429 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8431 if (sort
&& count
!= 0)
8433 bfd_vma (*ext_r_off
) (const void *);
8436 bfd_byte
*base
, *end
, *p
, *loc
;
8437 bfd_byte
*buf
= NULL
;
8439 if (bed
->s
->arch_size
== 32)
8441 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8442 ext_r_off
= ext32l_r_offset
;
8443 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8444 ext_r_off
= ext32b_r_offset
;
8450 #ifdef BFD_HOST_64_BIT
8451 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8452 ext_r_off
= ext64l_r_offset
;
8453 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8454 ext_r_off
= ext64b_r_offset
;
8460 /* Must use a stable sort here. A modified insertion sort,
8461 since the relocs are mostly sorted already. */
8462 elt_size
= reldata
->hdr
->sh_entsize
;
8463 base
= reldata
->hdr
->contents
;
8464 end
= base
+ count
* elt_size
;
8465 if (elt_size
> sizeof (Elf64_External_Rela
))
8468 /* Ensure the first element is lowest. This acts as a sentinel,
8469 speeding the main loop below. */
8470 r_off
= (*ext_r_off
) (base
);
8471 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8473 bfd_vma r_off2
= (*ext_r_off
) (p
);
8482 /* Don't just swap *base and *loc as that changes the order
8483 of the original base[0] and base[1] if they happen to
8484 have the same r_offset. */
8485 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8486 memcpy (onebuf
, loc
, elt_size
);
8487 memmove (base
+ elt_size
, base
, loc
- base
);
8488 memcpy (base
, onebuf
, elt_size
);
8491 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8493 /* base to p is sorted, *p is next to insert. */
8494 r_off
= (*ext_r_off
) (p
);
8495 /* Search the sorted region for location to insert. */
8497 while (r_off
< (*ext_r_off
) (loc
))
8502 /* Chances are there is a run of relocs to insert here,
8503 from one of more input files. Files are not always
8504 linked in order due to the way elf_link_input_bfd is
8505 called. See pr17666. */
8506 size_t sortlen
= p
- loc
;
8507 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8508 size_t runlen
= elt_size
;
8509 size_t buf_size
= 96 * 1024;
8510 while (p
+ runlen
< end
8511 && (sortlen
<= buf_size
8512 || runlen
+ elt_size
<= buf_size
)
8513 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8517 buf
= bfd_malloc (buf_size
);
8521 if (runlen
< sortlen
)
8523 memcpy (buf
, p
, runlen
);
8524 memmove (loc
+ runlen
, loc
, sortlen
);
8525 memcpy (loc
, buf
, runlen
);
8529 memcpy (buf
, loc
, sortlen
);
8530 memmove (loc
, p
, runlen
);
8531 memcpy (loc
+ runlen
, buf
, sortlen
);
8533 p
+= runlen
- elt_size
;
8536 /* Hashes are no longer valid. */
8537 free (reldata
->hashes
);
8538 reldata
->hashes
= NULL
;
8544 struct elf_link_sort_rela
8550 enum elf_reloc_type_class type
;
8551 /* We use this as an array of size int_rels_per_ext_rel. */
8552 Elf_Internal_Rela rela
[1];
8556 elf_link_sort_cmp1 (const void *A
, const void *B
)
8558 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8559 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8560 int relativea
, relativeb
;
8562 relativea
= a
->type
== reloc_class_relative
;
8563 relativeb
= b
->type
== reloc_class_relative
;
8565 if (relativea
< relativeb
)
8567 if (relativea
> relativeb
)
8569 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8571 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8573 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8575 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8581 elf_link_sort_cmp2 (const void *A
, const void *B
)
8583 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8584 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8586 if (a
->type
< b
->type
)
8588 if (a
->type
> b
->type
)
8590 if (a
->u
.offset
< b
->u
.offset
)
8592 if (a
->u
.offset
> b
->u
.offset
)
8594 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8596 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8602 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8604 asection
*dynamic_relocs
;
8607 bfd_size_type count
, size
;
8608 size_t i
, ret
, sort_elt
, ext_size
;
8609 bfd_byte
*sort
, *s_non_relative
, *p
;
8610 struct elf_link_sort_rela
*sq
;
8611 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8612 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8613 unsigned int opb
= bfd_octets_per_byte (abfd
);
8614 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8615 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8616 struct bfd_link_order
*lo
;
8618 bfd_boolean use_rela
;
8620 /* Find a dynamic reloc section. */
8621 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8622 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8623 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8624 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8626 bfd_boolean use_rela_initialised
= FALSE
;
8628 /* This is just here to stop gcc from complaining.
8629 Its initialization checking code is not perfect. */
8632 /* Both sections are present. Examine the sizes
8633 of the indirect sections to help us choose. */
8634 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8635 if (lo
->type
== bfd_indirect_link_order
)
8637 asection
*o
= lo
->u
.indirect
.section
;
8639 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8641 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8642 /* Section size is divisible by both rel and rela sizes.
8643 It is of no help to us. */
8647 /* Section size is only divisible by rela. */
8648 if (use_rela_initialised
&& (use_rela
== FALSE
))
8650 _bfd_error_handler (_("%B: Unable to sort relocs - "
8651 "they are in more than one size"),
8653 bfd_set_error (bfd_error_invalid_operation
);
8659 use_rela_initialised
= TRUE
;
8663 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8665 /* Section size is only divisible by rel. */
8666 if (use_rela_initialised
&& (use_rela
== TRUE
))
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are in more than one size"),
8671 bfd_set_error (bfd_error_invalid_operation
);
8677 use_rela_initialised
= TRUE
;
8682 /* The section size is not divisible by either -
8683 something is wrong. */
8684 _bfd_error_handler (_("%B: Unable to sort relocs - "
8685 "they are of an unknown size"), abfd
);
8686 bfd_set_error (bfd_error_invalid_operation
);
8691 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8692 if (lo
->type
== bfd_indirect_link_order
)
8694 asection
*o
= lo
->u
.indirect
.section
;
8696 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8698 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8699 /* Section size is divisible by both rel and rela sizes.
8700 It is of no help to us. */
8704 /* Section size is only divisible by rela. */
8705 if (use_rela_initialised
&& (use_rela
== FALSE
))
8707 _bfd_error_handler (_("%B: Unable to sort relocs - "
8708 "they are in more than one size"),
8710 bfd_set_error (bfd_error_invalid_operation
);
8716 use_rela_initialised
= TRUE
;
8720 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8722 /* Section size is only divisible by rel. */
8723 if (use_rela_initialised
&& (use_rela
== TRUE
))
8725 _bfd_error_handler (_("%B: Unable to sort relocs - "
8726 "they are in more than one size"),
8728 bfd_set_error (bfd_error_invalid_operation
);
8734 use_rela_initialised
= TRUE
;
8739 /* The section size is not divisible by either -
8740 something is wrong. */
8741 _bfd_error_handler (_("%B: Unable to sort relocs - "
8742 "they are of an unknown size"), abfd
);
8743 bfd_set_error (bfd_error_invalid_operation
);
8748 if (! use_rela_initialised
)
8752 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8754 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8761 dynamic_relocs
= rela_dyn
;
8762 ext_size
= bed
->s
->sizeof_rela
;
8763 swap_in
= bed
->s
->swap_reloca_in
;
8764 swap_out
= bed
->s
->swap_reloca_out
;
8768 dynamic_relocs
= rel_dyn
;
8769 ext_size
= bed
->s
->sizeof_rel
;
8770 swap_in
= bed
->s
->swap_reloc_in
;
8771 swap_out
= bed
->s
->swap_reloc_out
;
8775 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8776 if (lo
->type
== bfd_indirect_link_order
)
8777 size
+= lo
->u
.indirect
.section
->size
;
8779 if (size
!= dynamic_relocs
->size
)
8782 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8783 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8785 count
= dynamic_relocs
->size
/ ext_size
;
8788 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8792 (*info
->callbacks
->warning
)
8793 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8797 if (bed
->s
->arch_size
== 32)
8798 r_sym_mask
= ~(bfd_vma
) 0xff;
8800 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8802 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8803 if (lo
->type
== bfd_indirect_link_order
)
8805 bfd_byte
*erel
, *erelend
;
8806 asection
*o
= lo
->u
.indirect
.section
;
8808 if (o
->contents
== NULL
&& o
->size
!= 0)
8810 /* This is a reloc section that is being handled as a normal
8811 section. See bfd_section_from_shdr. We can't combine
8812 relocs in this case. */
8817 erelend
= o
->contents
+ o
->size
;
8818 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8820 while (erel
< erelend
)
8822 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8824 (*swap_in
) (abfd
, erel
, s
->rela
);
8825 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8826 s
->u
.sym_mask
= r_sym_mask
;
8832 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8834 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8836 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8837 if (s
->type
!= reloc_class_relative
)
8843 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8844 for (; i
< count
; i
++, p
+= sort_elt
)
8846 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8847 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8849 sp
->u
.offset
= sq
->rela
->r_offset
;
8852 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8854 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8855 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8857 /* We have plt relocs in .rela.dyn. */
8858 sq
= (struct elf_link_sort_rela
*) sort
;
8859 for (i
= 0; i
< count
; i
++)
8860 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8862 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8864 struct bfd_link_order
**plo
;
8865 /* Put srelplt link_order last. This is so the output_offset
8866 set in the next loop is correct for DT_JMPREL. */
8867 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8868 if ((*plo
)->type
== bfd_indirect_link_order
8869 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8875 plo
= &(*plo
)->next
;
8878 dynamic_relocs
->map_tail
.link_order
= lo
;
8883 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8884 if (lo
->type
== bfd_indirect_link_order
)
8886 bfd_byte
*erel
, *erelend
;
8887 asection
*o
= lo
->u
.indirect
.section
;
8890 erelend
= o
->contents
+ o
->size
;
8891 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8892 while (erel
< erelend
)
8894 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8895 (*swap_out
) (abfd
, s
->rela
, erel
);
8902 *psec
= dynamic_relocs
;
8906 /* Add a symbol to the output symbol string table. */
8909 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8911 Elf_Internal_Sym
*elfsym
,
8912 asection
*input_sec
,
8913 struct elf_link_hash_entry
*h
)
8915 int (*output_symbol_hook
)
8916 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8917 struct elf_link_hash_entry
*);
8918 struct elf_link_hash_table
*hash_table
;
8919 const struct elf_backend_data
*bed
;
8920 bfd_size_type strtabsize
;
8922 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8924 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8925 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8926 if (output_symbol_hook
!= NULL
)
8928 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8935 || (input_sec
->flags
& SEC_EXCLUDE
))
8936 elfsym
->st_name
= (unsigned long) -1;
8939 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8940 to get the final offset for st_name. */
8942 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8944 if (elfsym
->st_name
== (unsigned long) -1)
8948 hash_table
= elf_hash_table (flinfo
->info
);
8949 strtabsize
= hash_table
->strtabsize
;
8950 if (strtabsize
<= hash_table
->strtabcount
)
8952 strtabsize
+= strtabsize
;
8953 hash_table
->strtabsize
= strtabsize
;
8954 strtabsize
*= sizeof (*hash_table
->strtab
);
8956 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8958 if (hash_table
->strtab
== NULL
)
8961 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8962 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8963 = hash_table
->strtabcount
;
8964 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8965 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8967 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8968 hash_table
->strtabcount
+= 1;
8973 /* Swap symbols out to the symbol table and flush the output symbols to
8977 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8979 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8982 const struct elf_backend_data
*bed
;
8984 Elf_Internal_Shdr
*hdr
;
8988 if (!hash_table
->strtabcount
)
8991 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8993 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8995 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8996 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9000 if (flinfo
->symshndxbuf
)
9002 amt
= sizeof (Elf_External_Sym_Shndx
);
9003 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9004 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9005 if (flinfo
->symshndxbuf
== NULL
)
9012 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9014 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9015 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9016 elfsym
->sym
.st_name
= 0;
9019 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9020 elfsym
->sym
.st_name
);
9021 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9022 ((bfd_byte
*) symbuf
9023 + (elfsym
->dest_index
9024 * bed
->s
->sizeof_sym
)),
9025 (flinfo
->symshndxbuf
9026 + elfsym
->destshndx_index
));
9029 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9030 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9031 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9032 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9033 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9035 hdr
->sh_size
+= amt
;
9043 free (hash_table
->strtab
);
9044 hash_table
->strtab
= NULL
;
9049 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9052 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9054 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9055 && sym
->st_shndx
< SHN_LORESERVE
)
9057 /* The gABI doesn't support dynamic symbols in output sections
9060 /* xgettext:c-format */
9061 (_("%B: Too many sections: %d (>= %d)"),
9062 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9063 bfd_set_error (bfd_error_nonrepresentable_section
);
9069 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9070 allowing an unsatisfied unversioned symbol in the DSO to match a
9071 versioned symbol that would normally require an explicit version.
9072 We also handle the case that a DSO references a hidden symbol
9073 which may be satisfied by a versioned symbol in another DSO. */
9076 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9077 const struct elf_backend_data
*bed
,
9078 struct elf_link_hash_entry
*h
)
9081 struct elf_link_loaded_list
*loaded
;
9083 if (!is_elf_hash_table (info
->hash
))
9086 /* Check indirect symbol. */
9087 while (h
->root
.type
== bfd_link_hash_indirect
)
9088 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9090 switch (h
->root
.type
)
9096 case bfd_link_hash_undefined
:
9097 case bfd_link_hash_undefweak
:
9098 abfd
= h
->root
.u
.undef
.abfd
;
9100 || (abfd
->flags
& DYNAMIC
) == 0
9101 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9105 case bfd_link_hash_defined
:
9106 case bfd_link_hash_defweak
:
9107 abfd
= h
->root
.u
.def
.section
->owner
;
9110 case bfd_link_hash_common
:
9111 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9114 BFD_ASSERT (abfd
!= NULL
);
9116 for (loaded
= elf_hash_table (info
)->loaded
;
9118 loaded
= loaded
->next
)
9121 Elf_Internal_Shdr
*hdr
;
9125 Elf_Internal_Shdr
*versymhdr
;
9126 Elf_Internal_Sym
*isym
;
9127 Elf_Internal_Sym
*isymend
;
9128 Elf_Internal_Sym
*isymbuf
;
9129 Elf_External_Versym
*ever
;
9130 Elf_External_Versym
*extversym
;
9132 input
= loaded
->abfd
;
9134 /* We check each DSO for a possible hidden versioned definition. */
9136 || (input
->flags
& DYNAMIC
) == 0
9137 || elf_dynversym (input
) == 0)
9140 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9142 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9143 if (elf_bad_symtab (input
))
9145 extsymcount
= symcount
;
9150 extsymcount
= symcount
- hdr
->sh_info
;
9151 extsymoff
= hdr
->sh_info
;
9154 if (extsymcount
== 0)
9157 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9159 if (isymbuf
== NULL
)
9162 /* Read in any version definitions. */
9163 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9164 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9165 if (extversym
== NULL
)
9168 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9169 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9170 != versymhdr
->sh_size
))
9178 ever
= extversym
+ extsymoff
;
9179 isymend
= isymbuf
+ extsymcount
;
9180 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9183 Elf_Internal_Versym iver
;
9184 unsigned short version_index
;
9186 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9187 || isym
->st_shndx
== SHN_UNDEF
)
9190 name
= bfd_elf_string_from_elf_section (input
,
9193 if (strcmp (name
, h
->root
.root
.string
) != 0)
9196 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9198 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9200 && h
->forced_local
))
9202 /* If we have a non-hidden versioned sym, then it should
9203 have provided a definition for the undefined sym unless
9204 it is defined in a non-shared object and forced local.
9209 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9210 if (version_index
== 1 || version_index
== 2)
9212 /* This is the base or first version. We can use it. */
9226 /* Convert ELF common symbol TYPE. */
9229 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9231 /* Commom symbol can only appear in relocatable link. */
9232 if (!bfd_link_relocatable (info
))
9234 switch (info
->elf_stt_common
)
9238 case elf_stt_common
:
9241 case no_elf_stt_common
:
9248 /* Add an external symbol to the symbol table. This is called from
9249 the hash table traversal routine. When generating a shared object,
9250 we go through the symbol table twice. The first time we output
9251 anything that might have been forced to local scope in a version
9252 script. The second time we output the symbols that are still
9256 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9258 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9259 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9260 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9262 Elf_Internal_Sym sym
;
9263 asection
*input_sec
;
9264 const struct elf_backend_data
*bed
;
9269 if (h
->root
.type
== bfd_link_hash_warning
)
9271 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9272 if (h
->root
.type
== bfd_link_hash_new
)
9276 /* Decide whether to output this symbol in this pass. */
9277 if (eoinfo
->localsyms
)
9279 if (!h
->forced_local
)
9284 if (h
->forced_local
)
9288 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9290 if (h
->root
.type
== bfd_link_hash_undefined
)
9292 /* If we have an undefined symbol reference here then it must have
9293 come from a shared library that is being linked in. (Undefined
9294 references in regular files have already been handled unless
9295 they are in unreferenced sections which are removed by garbage
9297 bfd_boolean ignore_undef
= FALSE
;
9299 /* Some symbols may be special in that the fact that they're
9300 undefined can be safely ignored - let backend determine that. */
9301 if (bed
->elf_backend_ignore_undef_symbol
)
9302 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9304 /* If we are reporting errors for this situation then do so now. */
9307 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9308 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9309 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9310 (*flinfo
->info
->callbacks
->undefined_symbol
)
9311 (flinfo
->info
, h
->root
.root
.string
,
9312 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9314 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9316 /* Strip a global symbol defined in a discarded section. */
9321 /* We should also warn if a forced local symbol is referenced from
9322 shared libraries. */
9323 if (bfd_link_executable (flinfo
->info
)
9328 && h
->ref_dynamic_nonweak
9329 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9333 struct elf_link_hash_entry
*hi
= h
;
9335 /* Check indirect symbol. */
9336 while (hi
->root
.type
== bfd_link_hash_indirect
)
9337 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9339 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9340 /* xgettext:c-format */
9341 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9342 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9343 /* xgettext:c-format */
9344 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9346 /* xgettext:c-format */
9347 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9348 def_bfd
= flinfo
->output_bfd
;
9349 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9350 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9351 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9352 h
->root
.root
.string
);
9353 bfd_set_error (bfd_error_bad_value
);
9354 eoinfo
->failed
= TRUE
;
9358 /* We don't want to output symbols that have never been mentioned by
9359 a regular file, or that we have been told to strip. However, if
9360 h->indx is set to -2, the symbol is used by a reloc and we must
9365 else if ((h
->def_dynamic
9367 || h
->root
.type
== bfd_link_hash_new
)
9371 else if (flinfo
->info
->strip
== strip_all
)
9373 else if (flinfo
->info
->strip
== strip_some
9374 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9375 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9377 else if ((h
->root
.type
== bfd_link_hash_defined
9378 || h
->root
.type
== bfd_link_hash_defweak
)
9379 && ((flinfo
->info
->strip_discarded
9380 && discarded_section (h
->root
.u
.def
.section
))
9381 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9382 && h
->root
.u
.def
.section
->owner
!= NULL
9383 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9385 else if ((h
->root
.type
== bfd_link_hash_undefined
9386 || h
->root
.type
== bfd_link_hash_undefweak
)
9387 && h
->root
.u
.undef
.abfd
!= NULL
9388 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9393 /* If we're stripping it, and it's not a dynamic symbol, there's
9394 nothing else to do. However, if it is a forced local symbol or
9395 an ifunc symbol we need to give the backend finish_dynamic_symbol
9396 function a chance to make it dynamic. */
9399 && type
!= STT_GNU_IFUNC
9400 && !h
->forced_local
)
9404 sym
.st_size
= h
->size
;
9405 sym
.st_other
= h
->other
;
9406 switch (h
->root
.type
)
9409 case bfd_link_hash_new
:
9410 case bfd_link_hash_warning
:
9414 case bfd_link_hash_undefined
:
9415 case bfd_link_hash_undefweak
:
9416 input_sec
= bfd_und_section_ptr
;
9417 sym
.st_shndx
= SHN_UNDEF
;
9420 case bfd_link_hash_defined
:
9421 case bfd_link_hash_defweak
:
9423 input_sec
= h
->root
.u
.def
.section
;
9424 if (input_sec
->output_section
!= NULL
)
9427 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9428 input_sec
->output_section
);
9429 if (sym
.st_shndx
== SHN_BAD
)
9432 /* xgettext:c-format */
9433 (_("%B: could not find output section %A for input section %A"),
9434 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9435 bfd_set_error (bfd_error_nonrepresentable_section
);
9436 eoinfo
->failed
= TRUE
;
9440 /* ELF symbols in relocatable files are section relative,
9441 but in nonrelocatable files they are virtual
9443 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9444 if (!bfd_link_relocatable (flinfo
->info
))
9446 sym
.st_value
+= input_sec
->output_section
->vma
;
9447 if (h
->type
== STT_TLS
)
9449 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9450 if (tls_sec
!= NULL
)
9451 sym
.st_value
-= tls_sec
->vma
;
9457 BFD_ASSERT (input_sec
->owner
== NULL
9458 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9459 sym
.st_shndx
= SHN_UNDEF
;
9460 input_sec
= bfd_und_section_ptr
;
9465 case bfd_link_hash_common
:
9466 input_sec
= h
->root
.u
.c
.p
->section
;
9467 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9468 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9471 case bfd_link_hash_indirect
:
9472 /* These symbols are created by symbol versioning. They point
9473 to the decorated version of the name. For example, if the
9474 symbol foo@@GNU_1.2 is the default, which should be used when
9475 foo is used with no version, then we add an indirect symbol
9476 foo which points to foo@@GNU_1.2. We ignore these symbols,
9477 since the indirected symbol is already in the hash table. */
9481 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9482 switch (h
->root
.type
)
9484 case bfd_link_hash_common
:
9485 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9487 case bfd_link_hash_defined
:
9488 case bfd_link_hash_defweak
:
9489 if (bed
->common_definition (&sym
))
9490 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9494 case bfd_link_hash_undefined
:
9495 case bfd_link_hash_undefweak
:
9501 if (h
->forced_local
)
9503 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9504 /* Turn off visibility on local symbol. */
9505 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9507 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9508 else if (h
->unique_global
&& h
->def_regular
)
9509 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9510 else if (h
->root
.type
== bfd_link_hash_undefweak
9511 || h
->root
.type
== bfd_link_hash_defweak
)
9512 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9514 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9515 sym
.st_target_internal
= h
->target_internal
;
9517 /* Give the processor backend a chance to tweak the symbol value,
9518 and also to finish up anything that needs to be done for this
9519 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9520 forced local syms when non-shared is due to a historical quirk.
9521 STT_GNU_IFUNC symbol must go through PLT. */
9522 if ((h
->type
== STT_GNU_IFUNC
9524 && !bfd_link_relocatable (flinfo
->info
))
9525 || ((h
->dynindx
!= -1
9527 && ((bfd_link_pic (flinfo
->info
)
9528 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9529 || h
->root
.type
!= bfd_link_hash_undefweak
))
9530 || !h
->forced_local
)
9531 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9533 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9534 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9536 eoinfo
->failed
= TRUE
;
9541 /* If we are marking the symbol as undefined, and there are no
9542 non-weak references to this symbol from a regular object, then
9543 mark the symbol as weak undefined; if there are non-weak
9544 references, mark the symbol as strong. We can't do this earlier,
9545 because it might not be marked as undefined until the
9546 finish_dynamic_symbol routine gets through with it. */
9547 if (sym
.st_shndx
== SHN_UNDEF
9549 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9550 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9553 type
= ELF_ST_TYPE (sym
.st_info
);
9555 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9556 if (type
== STT_GNU_IFUNC
)
9559 if (h
->ref_regular_nonweak
)
9560 bindtype
= STB_GLOBAL
;
9562 bindtype
= STB_WEAK
;
9563 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9566 /* If this is a symbol defined in a dynamic library, don't use the
9567 symbol size from the dynamic library. Relinking an executable
9568 against a new library may introduce gratuitous changes in the
9569 executable's symbols if we keep the size. */
9570 if (sym
.st_shndx
== SHN_UNDEF
9575 /* If a non-weak symbol with non-default visibility is not defined
9576 locally, it is a fatal error. */
9577 if (!bfd_link_relocatable (flinfo
->info
)
9578 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9579 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9580 && h
->root
.type
== bfd_link_hash_undefined
9585 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9586 /* xgettext:c-format */
9587 msg
= _("%B: protected symbol `%s' isn't defined");
9588 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9589 /* xgettext:c-format */
9590 msg
= _("%B: internal symbol `%s' isn't defined");
9592 /* xgettext:c-format */
9593 msg
= _("%B: hidden symbol `%s' isn't defined");
9594 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9595 bfd_set_error (bfd_error_bad_value
);
9596 eoinfo
->failed
= TRUE
;
9600 /* If this symbol should be put in the .dynsym section, then put it
9601 there now. We already know the symbol index. We also fill in
9602 the entry in the .hash section. */
9603 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9605 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9609 /* Since there is no version information in the dynamic string,
9610 if there is no version info in symbol version section, we will
9611 have a run-time problem if not linking executable, referenced
9612 by shared library, or not bound locally. */
9613 if (h
->verinfo
.verdef
== NULL
9614 && (!bfd_link_executable (flinfo
->info
)
9616 || !h
->def_regular
))
9618 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9620 if (p
&& p
[1] != '\0')
9623 /* xgettext:c-format */
9624 (_("%B: No symbol version section for versioned symbol `%s'"),
9625 flinfo
->output_bfd
, h
->root
.root
.string
);
9626 eoinfo
->failed
= TRUE
;
9631 sym
.st_name
= h
->dynstr_index
;
9632 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9633 + h
->dynindx
* bed
->s
->sizeof_sym
);
9634 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9636 eoinfo
->failed
= TRUE
;
9639 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9641 if (flinfo
->hash_sec
!= NULL
)
9643 size_t hash_entry_size
;
9644 bfd_byte
*bucketpos
;
9649 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9650 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9653 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9654 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9655 + (bucket
+ 2) * hash_entry_size
);
9656 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9657 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9659 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9660 ((bfd_byte
*) flinfo
->hash_sec
->contents
9661 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9664 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9666 Elf_Internal_Versym iversym
;
9667 Elf_External_Versym
*eversym
;
9669 if (!h
->def_regular
)
9671 if (h
->verinfo
.verdef
== NULL
9672 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9673 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9674 iversym
.vs_vers
= 0;
9676 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9680 if (h
->verinfo
.vertree
== NULL
)
9681 iversym
.vs_vers
= 1;
9683 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9684 if (flinfo
->info
->create_default_symver
)
9688 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9690 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9691 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9693 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9694 eversym
+= h
->dynindx
;
9695 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9699 /* If the symbol is undefined, and we didn't output it to .dynsym,
9700 strip it from .symtab too. Obviously we can't do this for
9701 relocatable output or when needed for --emit-relocs. */
9702 else if (input_sec
== bfd_und_section_ptr
9704 && !bfd_link_relocatable (flinfo
->info
))
9706 /* Also strip others that we couldn't earlier due to dynamic symbol
9710 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9713 /* Output a FILE symbol so that following locals are not associated
9714 with the wrong input file. We need one for forced local symbols
9715 if we've seen more than one FILE symbol or when we have exactly
9716 one FILE symbol but global symbols are present in a file other
9717 than the one with the FILE symbol. We also need one if linker
9718 defined symbols are present. In practice these conditions are
9719 always met, so just emit the FILE symbol unconditionally. */
9720 if (eoinfo
->localsyms
9721 && !eoinfo
->file_sym_done
9722 && eoinfo
->flinfo
->filesym_count
!= 0)
9724 Elf_Internal_Sym fsym
;
9726 memset (&fsym
, 0, sizeof (fsym
));
9727 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9728 fsym
.st_shndx
= SHN_ABS
;
9729 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9730 bfd_und_section_ptr
, NULL
))
9733 eoinfo
->file_sym_done
= TRUE
;
9736 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9737 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9741 eoinfo
->failed
= TRUE
;
9746 else if (h
->indx
== -2)
9752 /* Return TRUE if special handling is done for relocs in SEC against
9753 symbols defined in discarded sections. */
9756 elf_section_ignore_discarded_relocs (asection
*sec
)
9758 const struct elf_backend_data
*bed
;
9760 switch (sec
->sec_info_type
)
9762 case SEC_INFO_TYPE_STABS
:
9763 case SEC_INFO_TYPE_EH_FRAME
:
9764 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9770 bed
= get_elf_backend_data (sec
->owner
);
9771 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9772 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9778 /* Return a mask saying how ld should treat relocations in SEC against
9779 symbols defined in discarded sections. If this function returns
9780 COMPLAIN set, ld will issue a warning message. If this function
9781 returns PRETEND set, and the discarded section was link-once and the
9782 same size as the kept link-once section, ld will pretend that the
9783 symbol was actually defined in the kept section. Otherwise ld will
9784 zero the reloc (at least that is the intent, but some cooperation by
9785 the target dependent code is needed, particularly for REL targets). */
9788 _bfd_elf_default_action_discarded (asection
*sec
)
9790 if (sec
->flags
& SEC_DEBUGGING
)
9793 if (strcmp (".eh_frame", sec
->name
) == 0)
9796 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9799 return COMPLAIN
| PRETEND
;
9802 /* Find a match between a section and a member of a section group. */
9805 match_group_member (asection
*sec
, asection
*group
,
9806 struct bfd_link_info
*info
)
9808 asection
*first
= elf_next_in_group (group
);
9809 asection
*s
= first
;
9813 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9816 s
= elf_next_in_group (s
);
9824 /* Check if the kept section of a discarded section SEC can be used
9825 to replace it. Return the replacement if it is OK. Otherwise return
9829 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9833 kept
= sec
->kept_section
;
9836 if ((kept
->flags
& SEC_GROUP
) != 0)
9837 kept
= match_group_member (sec
, kept
, info
);
9839 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9840 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9842 sec
->kept_section
= kept
;
9847 /* Link an input file into the linker output file. This function
9848 handles all the sections and relocations of the input file at once.
9849 This is so that we only have to read the local symbols once, and
9850 don't have to keep them in memory. */
9853 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9855 int (*relocate_section
)
9856 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9857 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9859 Elf_Internal_Shdr
*symtab_hdr
;
9862 Elf_Internal_Sym
*isymbuf
;
9863 Elf_Internal_Sym
*isym
;
9864 Elf_Internal_Sym
*isymend
;
9866 asection
**ppsection
;
9868 const struct elf_backend_data
*bed
;
9869 struct elf_link_hash_entry
**sym_hashes
;
9870 bfd_size_type address_size
;
9871 bfd_vma r_type_mask
;
9873 bfd_boolean have_file_sym
= FALSE
;
9875 output_bfd
= flinfo
->output_bfd
;
9876 bed
= get_elf_backend_data (output_bfd
);
9877 relocate_section
= bed
->elf_backend_relocate_section
;
9879 /* If this is a dynamic object, we don't want to do anything here:
9880 we don't want the local symbols, and we don't want the section
9882 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9885 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9886 if (elf_bad_symtab (input_bfd
))
9888 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9893 locsymcount
= symtab_hdr
->sh_info
;
9894 extsymoff
= symtab_hdr
->sh_info
;
9897 /* Read the local symbols. */
9898 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9899 if (isymbuf
== NULL
&& locsymcount
!= 0)
9901 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9902 flinfo
->internal_syms
,
9903 flinfo
->external_syms
,
9904 flinfo
->locsym_shndx
);
9905 if (isymbuf
== NULL
)
9909 /* Find local symbol sections and adjust values of symbols in
9910 SEC_MERGE sections. Write out those local symbols we know are
9911 going into the output file. */
9912 isymend
= isymbuf
+ locsymcount
;
9913 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9915 isym
++, pindex
++, ppsection
++)
9919 Elf_Internal_Sym osym
;
9925 if (elf_bad_symtab (input_bfd
))
9927 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9934 if (isym
->st_shndx
== SHN_UNDEF
)
9935 isec
= bfd_und_section_ptr
;
9936 else if (isym
->st_shndx
== SHN_ABS
)
9937 isec
= bfd_abs_section_ptr
;
9938 else if (isym
->st_shndx
== SHN_COMMON
)
9939 isec
= bfd_com_section_ptr
;
9942 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9945 /* Don't attempt to output symbols with st_shnx in the
9946 reserved range other than SHN_ABS and SHN_COMMON. */
9950 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9951 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9953 _bfd_merged_section_offset (output_bfd
, &isec
,
9954 elf_section_data (isec
)->sec_info
,
9960 /* Don't output the first, undefined, symbol. In fact, don't
9961 output any undefined local symbol. */
9962 if (isec
== bfd_und_section_ptr
)
9965 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9967 /* We never output section symbols. Instead, we use the
9968 section symbol of the corresponding section in the output
9973 /* If we are stripping all symbols, we don't want to output this
9975 if (flinfo
->info
->strip
== strip_all
)
9978 /* If we are discarding all local symbols, we don't want to
9979 output this one. If we are generating a relocatable output
9980 file, then some of the local symbols may be required by
9981 relocs; we output them below as we discover that they are
9983 if (flinfo
->info
->discard
== discard_all
)
9986 /* If this symbol is defined in a section which we are
9987 discarding, we don't need to keep it. */
9988 if (isym
->st_shndx
!= SHN_UNDEF
9989 && isym
->st_shndx
< SHN_LORESERVE
9990 && bfd_section_removed_from_list (output_bfd
,
9991 isec
->output_section
))
9994 /* Get the name of the symbol. */
9995 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10000 /* See if we are discarding symbols with this name. */
10001 if ((flinfo
->info
->strip
== strip_some
10002 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10004 || (((flinfo
->info
->discard
== discard_sec_merge
10005 && (isec
->flags
& SEC_MERGE
)
10006 && !bfd_link_relocatable (flinfo
->info
))
10007 || flinfo
->info
->discard
== discard_l
)
10008 && bfd_is_local_label_name (input_bfd
, name
)))
10011 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10013 if (input_bfd
->lto_output
)
10014 /* -flto puts a temp file name here. This means builds
10015 are not reproducible. Discard the symbol. */
10017 have_file_sym
= TRUE
;
10018 flinfo
->filesym_count
+= 1;
10020 if (!have_file_sym
)
10022 /* In the absence of debug info, bfd_find_nearest_line uses
10023 FILE symbols to determine the source file for local
10024 function symbols. Provide a FILE symbol here if input
10025 files lack such, so that their symbols won't be
10026 associated with a previous input file. It's not the
10027 source file, but the best we can do. */
10028 have_file_sym
= TRUE
;
10029 flinfo
->filesym_count
+= 1;
10030 memset (&osym
, 0, sizeof (osym
));
10031 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10032 osym
.st_shndx
= SHN_ABS
;
10033 if (!elf_link_output_symstrtab (flinfo
,
10034 (input_bfd
->lto_output
? NULL
10035 : input_bfd
->filename
),
10036 &osym
, bfd_abs_section_ptr
,
10043 /* Adjust the section index for the output file. */
10044 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10045 isec
->output_section
);
10046 if (osym
.st_shndx
== SHN_BAD
)
10049 /* ELF symbols in relocatable files are section relative, but
10050 in executable files they are virtual addresses. Note that
10051 this code assumes that all ELF sections have an associated
10052 BFD section with a reasonable value for output_offset; below
10053 we assume that they also have a reasonable value for
10054 output_section. Any special sections must be set up to meet
10055 these requirements. */
10056 osym
.st_value
+= isec
->output_offset
;
10057 if (!bfd_link_relocatable (flinfo
->info
))
10059 osym
.st_value
+= isec
->output_section
->vma
;
10060 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10062 /* STT_TLS symbols are relative to PT_TLS segment base. */
10063 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10064 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10068 indx
= bfd_get_symcount (output_bfd
);
10069 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10076 if (bed
->s
->arch_size
== 32)
10078 r_type_mask
= 0xff;
10084 r_type_mask
= 0xffffffff;
10089 /* Relocate the contents of each section. */
10090 sym_hashes
= elf_sym_hashes (input_bfd
);
10091 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10093 bfd_byte
*contents
;
10095 if (! o
->linker_mark
)
10097 /* This section was omitted from the link. */
10101 if (bfd_link_relocatable (flinfo
->info
)
10102 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10104 /* Deal with the group signature symbol. */
10105 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10106 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10107 asection
*osec
= o
->output_section
;
10109 if (symndx
>= locsymcount
10110 || (elf_bad_symtab (input_bfd
)
10111 && flinfo
->sections
[symndx
] == NULL
))
10113 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10114 while (h
->root
.type
== bfd_link_hash_indirect
10115 || h
->root
.type
== bfd_link_hash_warning
)
10116 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10117 /* Arrange for symbol to be output. */
10119 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10121 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10123 /* We'll use the output section target_index. */
10124 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10125 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10129 if (flinfo
->indices
[symndx
] == -1)
10131 /* Otherwise output the local symbol now. */
10132 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10133 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10138 name
= bfd_elf_string_from_elf_section (input_bfd
,
10139 symtab_hdr
->sh_link
,
10144 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10146 if (sym
.st_shndx
== SHN_BAD
)
10149 sym
.st_value
+= o
->output_offset
;
10151 indx
= bfd_get_symcount (output_bfd
);
10152 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10157 flinfo
->indices
[symndx
] = indx
;
10161 elf_section_data (osec
)->this_hdr
.sh_info
10162 = flinfo
->indices
[symndx
];
10166 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10167 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10170 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10172 /* Section was created by _bfd_elf_link_create_dynamic_sections
10177 /* Get the contents of the section. They have been cached by a
10178 relaxation routine. Note that o is a section in an input
10179 file, so the contents field will not have been set by any of
10180 the routines which work on output files. */
10181 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10183 contents
= elf_section_data (o
)->this_hdr
.contents
;
10184 if (bed
->caches_rawsize
10186 && o
->rawsize
< o
->size
)
10188 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10189 contents
= flinfo
->contents
;
10194 contents
= flinfo
->contents
;
10195 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10199 if ((o
->flags
& SEC_RELOC
) != 0)
10201 Elf_Internal_Rela
*internal_relocs
;
10202 Elf_Internal_Rela
*rel
, *relend
;
10203 int action_discarded
;
10206 /* Get the swapped relocs. */
10208 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10209 flinfo
->internal_relocs
, FALSE
);
10210 if (internal_relocs
== NULL
10211 && o
->reloc_count
> 0)
10214 /* We need to reverse-copy input .ctors/.dtors sections if
10215 they are placed in .init_array/.finit_array for output. */
10216 if (o
->size
> address_size
10217 && ((strncmp (o
->name
, ".ctors", 6) == 0
10218 && strcmp (o
->output_section
->name
,
10219 ".init_array") == 0)
10220 || (strncmp (o
->name
, ".dtors", 6) == 0
10221 && strcmp (o
->output_section
->name
,
10222 ".fini_array") == 0))
10223 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10225 if (o
->size
!= o
->reloc_count
* address_size
)
10228 /* xgettext:c-format */
10229 (_("error: %B: size of section %A is not "
10230 "multiple of address size"),
10232 bfd_set_error (bfd_error_on_input
);
10235 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10238 action_discarded
= -1;
10239 if (!elf_section_ignore_discarded_relocs (o
))
10240 action_discarded
= (*bed
->action_discarded
) (o
);
10242 /* Run through the relocs evaluating complex reloc symbols and
10243 looking for relocs against symbols from discarded sections
10244 or section symbols from removed link-once sections.
10245 Complain about relocs against discarded sections. Zero
10246 relocs against removed link-once sections. */
10248 rel
= internal_relocs
;
10249 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10250 for ( ; rel
< relend
; rel
++)
10252 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10253 unsigned int s_type
;
10254 asection
**ps
, *sec
;
10255 struct elf_link_hash_entry
*h
= NULL
;
10256 const char *sym_name
;
10258 if (r_symndx
== STN_UNDEF
)
10261 if (r_symndx
>= locsymcount
10262 || (elf_bad_symtab (input_bfd
)
10263 && flinfo
->sections
[r_symndx
] == NULL
))
10265 h
= sym_hashes
[r_symndx
- extsymoff
];
10267 /* Badly formatted input files can contain relocs that
10268 reference non-existant symbols. Check here so that
10269 we do not seg fault. */
10274 sprintf_vma (buffer
, rel
->r_info
);
10276 /* xgettext:c-format */
10277 (_("error: %B contains a reloc (0x%s) for section %A "
10278 "that references a non-existent global symbol"),
10279 input_bfd
, o
, buffer
);
10280 bfd_set_error (bfd_error_bad_value
);
10284 while (h
->root
.type
== bfd_link_hash_indirect
10285 || h
->root
.type
== bfd_link_hash_warning
)
10286 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10290 /* If a plugin symbol is referenced from a non-IR file,
10291 mark the symbol as undefined. Note that the
10292 linker may attach linker created dynamic sections
10293 to the plugin bfd. Symbols defined in linker
10294 created sections are not plugin symbols. */
10295 if (h
->root
.non_ir_ref
10296 && (h
->root
.type
== bfd_link_hash_defined
10297 || h
->root
.type
== bfd_link_hash_defweak
)
10298 && (h
->root
.u
.def
.section
->flags
10299 & SEC_LINKER_CREATED
) == 0
10300 && h
->root
.u
.def
.section
->owner
!= NULL
10301 && (h
->root
.u
.def
.section
->owner
->flags
10302 & BFD_PLUGIN
) != 0)
10304 h
->root
.type
= bfd_link_hash_undefined
;
10305 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10309 if (h
->root
.type
== bfd_link_hash_defined
10310 || h
->root
.type
== bfd_link_hash_defweak
)
10311 ps
= &h
->root
.u
.def
.section
;
10313 sym_name
= h
->root
.root
.string
;
10317 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10319 s_type
= ELF_ST_TYPE (sym
->st_info
);
10320 ps
= &flinfo
->sections
[r_symndx
];
10321 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10325 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10326 && !bfd_link_relocatable (flinfo
->info
))
10329 bfd_vma dot
= (rel
->r_offset
10330 + o
->output_offset
+ o
->output_section
->vma
);
10332 printf ("Encountered a complex symbol!");
10333 printf (" (input_bfd %s, section %s, reloc %ld\n",
10334 input_bfd
->filename
, o
->name
,
10335 (long) (rel
- internal_relocs
));
10336 printf (" symbol: idx %8.8lx, name %s\n",
10337 r_symndx
, sym_name
);
10338 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10339 (unsigned long) rel
->r_info
,
10340 (unsigned long) rel
->r_offset
);
10342 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10343 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10346 /* Symbol evaluated OK. Update to absolute value. */
10347 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10352 if (action_discarded
!= -1 && ps
!= NULL
)
10354 /* Complain if the definition comes from a
10355 discarded section. */
10356 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10358 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10359 if (action_discarded
& COMPLAIN
)
10360 (*flinfo
->info
->callbacks
->einfo
)
10361 /* xgettext:c-format */
10362 (_("%X`%s' referenced in section `%A' of %B: "
10363 "defined in discarded section `%A' of %B\n"),
10364 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10366 /* Try to do the best we can to support buggy old
10367 versions of gcc. Pretend that the symbol is
10368 really defined in the kept linkonce section.
10369 FIXME: This is quite broken. Modifying the
10370 symbol here means we will be changing all later
10371 uses of the symbol, not just in this section. */
10372 if (action_discarded
& PRETEND
)
10376 kept
= _bfd_elf_check_kept_section (sec
,
10388 /* Relocate the section by invoking a back end routine.
10390 The back end routine is responsible for adjusting the
10391 section contents as necessary, and (if using Rela relocs
10392 and generating a relocatable output file) adjusting the
10393 reloc addend as necessary.
10395 The back end routine does not have to worry about setting
10396 the reloc address or the reloc symbol index.
10398 The back end routine is given a pointer to the swapped in
10399 internal symbols, and can access the hash table entries
10400 for the external symbols via elf_sym_hashes (input_bfd).
10402 When generating relocatable output, the back end routine
10403 must handle STB_LOCAL/STT_SECTION symbols specially. The
10404 output symbol is going to be a section symbol
10405 corresponding to the output section, which will require
10406 the addend to be adjusted. */
10408 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10409 input_bfd
, o
, contents
,
10417 || bfd_link_relocatable (flinfo
->info
)
10418 || flinfo
->info
->emitrelocations
)
10420 Elf_Internal_Rela
*irela
;
10421 Elf_Internal_Rela
*irelaend
, *irelamid
;
10422 bfd_vma last_offset
;
10423 struct elf_link_hash_entry
**rel_hash
;
10424 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10425 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10426 unsigned int next_erel
;
10427 bfd_boolean rela_normal
;
10428 struct bfd_elf_section_data
*esdi
, *esdo
;
10430 esdi
= elf_section_data (o
);
10431 esdo
= elf_section_data (o
->output_section
);
10432 rela_normal
= FALSE
;
10434 /* Adjust the reloc addresses and symbol indices. */
10436 irela
= internal_relocs
;
10437 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10438 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10439 /* We start processing the REL relocs, if any. When we reach
10440 IRELAMID in the loop, we switch to the RELA relocs. */
10442 if (esdi
->rel
.hdr
!= NULL
)
10443 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10444 * bed
->s
->int_rels_per_ext_rel
);
10445 rel_hash_list
= rel_hash
;
10446 rela_hash_list
= NULL
;
10447 last_offset
= o
->output_offset
;
10448 if (!bfd_link_relocatable (flinfo
->info
))
10449 last_offset
+= o
->output_section
->vma
;
10450 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10452 unsigned long r_symndx
;
10454 Elf_Internal_Sym sym
;
10456 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10462 if (irela
== irelamid
)
10464 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10465 rela_hash_list
= rel_hash
;
10466 rela_normal
= bed
->rela_normal
;
10469 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10472 if (irela
->r_offset
>= (bfd_vma
) -2)
10474 /* This is a reloc for a deleted entry or somesuch.
10475 Turn it into an R_*_NONE reloc, at the same
10476 offset as the last reloc. elf_eh_frame.c and
10477 bfd_elf_discard_info rely on reloc offsets
10479 irela
->r_offset
= last_offset
;
10481 irela
->r_addend
= 0;
10485 irela
->r_offset
+= o
->output_offset
;
10487 /* Relocs in an executable have to be virtual addresses. */
10488 if (!bfd_link_relocatable (flinfo
->info
))
10489 irela
->r_offset
+= o
->output_section
->vma
;
10491 last_offset
= irela
->r_offset
;
10493 r_symndx
= irela
->r_info
>> r_sym_shift
;
10494 if (r_symndx
== STN_UNDEF
)
10497 if (r_symndx
>= locsymcount
10498 || (elf_bad_symtab (input_bfd
)
10499 && flinfo
->sections
[r_symndx
] == NULL
))
10501 struct elf_link_hash_entry
*rh
;
10502 unsigned long indx
;
10504 /* This is a reloc against a global symbol. We
10505 have not yet output all the local symbols, so
10506 we do not know the symbol index of any global
10507 symbol. We set the rel_hash entry for this
10508 reloc to point to the global hash table entry
10509 for this symbol. The symbol index is then
10510 set at the end of bfd_elf_final_link. */
10511 indx
= r_symndx
- extsymoff
;
10512 rh
= elf_sym_hashes (input_bfd
)[indx
];
10513 while (rh
->root
.type
== bfd_link_hash_indirect
10514 || rh
->root
.type
== bfd_link_hash_warning
)
10515 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10517 /* Setting the index to -2 tells
10518 elf_link_output_extsym that this symbol is
10519 used by a reloc. */
10520 BFD_ASSERT (rh
->indx
< 0);
10528 /* This is a reloc against a local symbol. */
10531 sym
= isymbuf
[r_symndx
];
10532 sec
= flinfo
->sections
[r_symndx
];
10533 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10535 /* I suppose the backend ought to fill in the
10536 section of any STT_SECTION symbol against a
10537 processor specific section. */
10538 r_symndx
= STN_UNDEF
;
10539 if (bfd_is_abs_section (sec
))
10541 else if (sec
== NULL
|| sec
->owner
== NULL
)
10543 bfd_set_error (bfd_error_bad_value
);
10548 asection
*osec
= sec
->output_section
;
10550 /* If we have discarded a section, the output
10551 section will be the absolute section. In
10552 case of discarded SEC_MERGE sections, use
10553 the kept section. relocate_section should
10554 have already handled discarded linkonce
10556 if (bfd_is_abs_section (osec
)
10557 && sec
->kept_section
!= NULL
10558 && sec
->kept_section
->output_section
!= NULL
)
10560 osec
= sec
->kept_section
->output_section
;
10561 irela
->r_addend
-= osec
->vma
;
10564 if (!bfd_is_abs_section (osec
))
10566 r_symndx
= osec
->target_index
;
10567 if (r_symndx
== STN_UNDEF
)
10569 irela
->r_addend
+= osec
->vma
;
10570 osec
= _bfd_nearby_section (output_bfd
, osec
,
10572 irela
->r_addend
-= osec
->vma
;
10573 r_symndx
= osec
->target_index
;
10578 /* Adjust the addend according to where the
10579 section winds up in the output section. */
10581 irela
->r_addend
+= sec
->output_offset
;
10585 if (flinfo
->indices
[r_symndx
] == -1)
10587 unsigned long shlink
;
10592 if (flinfo
->info
->strip
== strip_all
)
10594 /* You can't do ld -r -s. */
10595 bfd_set_error (bfd_error_invalid_operation
);
10599 /* This symbol was skipped earlier, but
10600 since it is needed by a reloc, we
10601 must output it now. */
10602 shlink
= symtab_hdr
->sh_link
;
10603 name
= (bfd_elf_string_from_elf_section
10604 (input_bfd
, shlink
, sym
.st_name
));
10608 osec
= sec
->output_section
;
10610 _bfd_elf_section_from_bfd_section (output_bfd
,
10612 if (sym
.st_shndx
== SHN_BAD
)
10615 sym
.st_value
+= sec
->output_offset
;
10616 if (!bfd_link_relocatable (flinfo
->info
))
10618 sym
.st_value
+= osec
->vma
;
10619 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10621 /* STT_TLS symbols are relative to PT_TLS
10623 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10624 ->tls_sec
!= NULL
);
10625 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10630 indx
= bfd_get_symcount (output_bfd
);
10631 ret
= elf_link_output_symstrtab (flinfo
, name
,
10637 flinfo
->indices
[r_symndx
] = indx
;
10642 r_symndx
= flinfo
->indices
[r_symndx
];
10645 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10646 | (irela
->r_info
& r_type_mask
));
10649 /* Swap out the relocs. */
10650 input_rel_hdr
= esdi
->rel
.hdr
;
10651 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10653 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10658 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10659 * bed
->s
->int_rels_per_ext_rel
);
10660 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10663 input_rela_hdr
= esdi
->rela
.hdr
;
10664 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10666 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10675 /* Write out the modified section contents. */
10676 if (bed
->elf_backend_write_section
10677 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10680 /* Section written out. */
10682 else switch (o
->sec_info_type
)
10684 case SEC_INFO_TYPE_STABS
:
10685 if (! (_bfd_write_section_stabs
10687 &elf_hash_table (flinfo
->info
)->stab_info
,
10688 o
, &elf_section_data (o
)->sec_info
, contents
)))
10691 case SEC_INFO_TYPE_MERGE
:
10692 if (! _bfd_write_merged_section (output_bfd
, o
,
10693 elf_section_data (o
)->sec_info
))
10696 case SEC_INFO_TYPE_EH_FRAME
:
10698 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10703 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10705 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10713 if (! (o
->flags
& SEC_EXCLUDE
))
10715 file_ptr offset
= (file_ptr
) o
->output_offset
;
10716 bfd_size_type todo
= o
->size
;
10718 offset
*= bfd_octets_per_byte (output_bfd
);
10720 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10722 /* Reverse-copy input section to output. */
10725 todo
-= address_size
;
10726 if (! bfd_set_section_contents (output_bfd
,
10734 offset
+= address_size
;
10738 else if (! bfd_set_section_contents (output_bfd
,
10752 /* Generate a reloc when linking an ELF file. This is a reloc
10753 requested by the linker, and does not come from any input file. This
10754 is used to build constructor and destructor tables when linking
10758 elf_reloc_link_order (bfd
*output_bfd
,
10759 struct bfd_link_info
*info
,
10760 asection
*output_section
,
10761 struct bfd_link_order
*link_order
)
10763 reloc_howto_type
*howto
;
10767 struct bfd_elf_section_reloc_data
*reldata
;
10768 struct elf_link_hash_entry
**rel_hash_ptr
;
10769 Elf_Internal_Shdr
*rel_hdr
;
10770 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10771 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10774 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10776 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10779 bfd_set_error (bfd_error_bad_value
);
10783 addend
= link_order
->u
.reloc
.p
->addend
;
10786 reldata
= &esdo
->rel
;
10787 else if (esdo
->rela
.hdr
)
10788 reldata
= &esdo
->rela
;
10795 /* Figure out the symbol index. */
10796 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10797 if (link_order
->type
== bfd_section_reloc_link_order
)
10799 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10800 BFD_ASSERT (indx
!= 0);
10801 *rel_hash_ptr
= NULL
;
10805 struct elf_link_hash_entry
*h
;
10807 /* Treat a reloc against a defined symbol as though it were
10808 actually against the section. */
10809 h
= ((struct elf_link_hash_entry
*)
10810 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10811 link_order
->u
.reloc
.p
->u
.name
,
10812 FALSE
, FALSE
, TRUE
));
10814 && (h
->root
.type
== bfd_link_hash_defined
10815 || h
->root
.type
== bfd_link_hash_defweak
))
10819 section
= h
->root
.u
.def
.section
;
10820 indx
= section
->output_section
->target_index
;
10821 *rel_hash_ptr
= NULL
;
10822 /* It seems that we ought to add the symbol value to the
10823 addend here, but in practice it has already been added
10824 because it was passed to constructor_callback. */
10825 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10827 else if (h
!= NULL
)
10829 /* Setting the index to -2 tells elf_link_output_extsym that
10830 this symbol is used by a reloc. */
10837 (*info
->callbacks
->unattached_reloc
)
10838 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10843 /* If this is an inplace reloc, we must write the addend into the
10845 if (howto
->partial_inplace
&& addend
!= 0)
10847 bfd_size_type size
;
10848 bfd_reloc_status_type rstat
;
10851 const char *sym_name
;
10853 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10854 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10855 if (buf
== NULL
&& size
!= 0)
10857 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10864 case bfd_reloc_outofrange
:
10867 case bfd_reloc_overflow
:
10868 if (link_order
->type
== bfd_section_reloc_link_order
)
10869 sym_name
= bfd_section_name (output_bfd
,
10870 link_order
->u
.reloc
.p
->u
.section
);
10872 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10873 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10874 howto
->name
, addend
, NULL
, NULL
,
10879 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10881 * bfd_octets_per_byte (output_bfd
),
10888 /* The address of a reloc is relative to the section in a
10889 relocatable file, and is a virtual address in an executable
10891 offset
= link_order
->offset
;
10892 if (! bfd_link_relocatable (info
))
10893 offset
+= output_section
->vma
;
10895 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10897 irel
[i
].r_offset
= offset
;
10898 irel
[i
].r_info
= 0;
10899 irel
[i
].r_addend
= 0;
10901 if (bed
->s
->arch_size
== 32)
10902 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10904 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10906 rel_hdr
= reldata
->hdr
;
10907 erel
= rel_hdr
->contents
;
10908 if (rel_hdr
->sh_type
== SHT_REL
)
10910 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10911 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10915 irel
[0].r_addend
= addend
;
10916 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10917 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10926 /* Get the output vma of the section pointed to by the sh_link field. */
10929 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10931 Elf_Internal_Shdr
**elf_shdrp
;
10935 s
= p
->u
.indirect
.section
;
10936 elf_shdrp
= elf_elfsections (s
->owner
);
10937 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10938 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10940 The Intel C compiler generates SHT_IA_64_UNWIND with
10941 SHF_LINK_ORDER. But it doesn't set the sh_link or
10942 sh_info fields. Hence we could get the situation
10943 where elfsec is 0. */
10946 const struct elf_backend_data
*bed
10947 = get_elf_backend_data (s
->owner
);
10948 if (bed
->link_order_error_handler
)
10949 bed
->link_order_error_handler
10950 /* xgettext:c-format */
10951 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10956 s
= elf_shdrp
[elfsec
]->bfd_section
;
10957 return s
->output_section
->vma
+ s
->output_offset
;
10962 /* Compare two sections based on the locations of the sections they are
10963 linked to. Used by elf_fixup_link_order. */
10966 compare_link_order (const void * a
, const void * b
)
10971 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10972 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10975 return apos
> bpos
;
10979 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10980 order as their linked sections. Returns false if this could not be done
10981 because an output section includes both ordered and unordered
10982 sections. Ideally we'd do this in the linker proper. */
10985 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10987 int seen_linkorder
;
10990 struct bfd_link_order
*p
;
10992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10994 struct bfd_link_order
**sections
;
10995 asection
*s
, *other_sec
, *linkorder_sec
;
10999 linkorder_sec
= NULL
;
11001 seen_linkorder
= 0;
11002 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11004 if (p
->type
== bfd_indirect_link_order
)
11006 s
= p
->u
.indirect
.section
;
11008 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11009 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11010 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11011 && elfsec
< elf_numsections (sub
)
11012 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11013 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11027 if (seen_other
&& seen_linkorder
)
11029 if (other_sec
&& linkorder_sec
)
11031 /* xgettext:c-format */
11032 (_("%A has both ordered [`%A' in %B] "
11033 "and unordered [`%A' in %B] sections"),
11035 linkorder_sec
->owner
, other_sec
,
11039 (_("%A has both ordered and unordered sections"), o
);
11040 bfd_set_error (bfd_error_bad_value
);
11045 if (!seen_linkorder
)
11048 sections
= (struct bfd_link_order
**)
11049 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11050 if (sections
== NULL
)
11052 seen_linkorder
= 0;
11054 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11056 sections
[seen_linkorder
++] = p
;
11058 /* Sort the input sections in the order of their linked section. */
11059 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11060 compare_link_order
);
11062 /* Change the offsets of the sections. */
11064 for (n
= 0; n
< seen_linkorder
; n
++)
11066 s
= sections
[n
]->u
.indirect
.section
;
11067 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11068 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11069 sections
[n
]->offset
= offset
;
11070 offset
+= sections
[n
]->size
;
11077 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11078 Returns TRUE upon success, FALSE otherwise. */
11081 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11083 bfd_boolean ret
= FALSE
;
11085 const struct elf_backend_data
*bed
;
11087 enum bfd_architecture arch
;
11089 asymbol
**sympp
= NULL
;
11093 elf_symbol_type
*osymbuf
;
11095 implib_bfd
= info
->out_implib_bfd
;
11096 bed
= get_elf_backend_data (abfd
);
11098 if (!bfd_set_format (implib_bfd
, bfd_object
))
11101 flags
= bfd_get_file_flags (abfd
);
11102 flags
&= ~HAS_RELOC
;
11103 if (!bfd_set_start_address (implib_bfd
, 0)
11104 || !bfd_set_file_flags (implib_bfd
, flags
))
11107 /* Copy architecture of output file to import library file. */
11108 arch
= bfd_get_arch (abfd
);
11109 mach
= bfd_get_mach (abfd
);
11110 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11111 && (abfd
->target_defaulted
11112 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11115 /* Get symbol table size. */
11116 symsize
= bfd_get_symtab_upper_bound (abfd
);
11120 /* Read in the symbol table. */
11121 sympp
= (asymbol
**) xmalloc (symsize
);
11122 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11126 /* Allow the BFD backend to copy any private header data it
11127 understands from the output BFD to the import library BFD. */
11128 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11131 /* Filter symbols to appear in the import library. */
11132 if (bed
->elf_backend_filter_implib_symbols
)
11133 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11136 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11139 bfd_set_error (bfd_error_no_symbols
);
11140 _bfd_error_handler (_("%B: no symbol found for import library"),
11146 /* Make symbols absolute. */
11147 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11148 sizeof (*osymbuf
));
11149 for (src_count
= 0; src_count
< symcount
; src_count
++)
11151 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11152 sizeof (*osymbuf
));
11153 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11154 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11155 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11156 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11157 osymbuf
[src_count
].symbol
.value
;
11158 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11161 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11163 /* Allow the BFD backend to copy any private data it understands
11164 from the output BFD to the import library BFD. This is done last
11165 to permit the routine to look at the filtered symbol table. */
11166 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11169 if (!bfd_close (implib_bfd
))
11180 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11184 if (flinfo
->symstrtab
!= NULL
)
11185 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11186 if (flinfo
->contents
!= NULL
)
11187 free (flinfo
->contents
);
11188 if (flinfo
->external_relocs
!= NULL
)
11189 free (flinfo
->external_relocs
);
11190 if (flinfo
->internal_relocs
!= NULL
)
11191 free (flinfo
->internal_relocs
);
11192 if (flinfo
->external_syms
!= NULL
)
11193 free (flinfo
->external_syms
);
11194 if (flinfo
->locsym_shndx
!= NULL
)
11195 free (flinfo
->locsym_shndx
);
11196 if (flinfo
->internal_syms
!= NULL
)
11197 free (flinfo
->internal_syms
);
11198 if (flinfo
->indices
!= NULL
)
11199 free (flinfo
->indices
);
11200 if (flinfo
->sections
!= NULL
)
11201 free (flinfo
->sections
);
11202 if (flinfo
->symshndxbuf
!= NULL
)
11203 free (flinfo
->symshndxbuf
);
11204 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11206 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11207 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11208 free (esdo
->rel
.hashes
);
11209 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11210 free (esdo
->rela
.hashes
);
11214 /* Do the final step of an ELF link. */
11217 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11219 bfd_boolean dynamic
;
11220 bfd_boolean emit_relocs
;
11222 struct elf_final_link_info flinfo
;
11224 struct bfd_link_order
*p
;
11226 bfd_size_type max_contents_size
;
11227 bfd_size_type max_external_reloc_size
;
11228 bfd_size_type max_internal_reloc_count
;
11229 bfd_size_type max_sym_count
;
11230 bfd_size_type max_sym_shndx_count
;
11231 Elf_Internal_Sym elfsym
;
11233 Elf_Internal_Shdr
*symtab_hdr
;
11234 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11235 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11236 struct elf_outext_info eoinfo
;
11237 bfd_boolean merged
;
11238 size_t relativecount
= 0;
11239 asection
*reldyn
= 0;
11241 asection
*attr_section
= NULL
;
11242 bfd_vma attr_size
= 0;
11243 const char *std_attrs_section
;
11244 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11246 if (!is_elf_hash_table (htab
))
11249 if (bfd_link_pic (info
))
11250 abfd
->flags
|= DYNAMIC
;
11252 dynamic
= htab
->dynamic_sections_created
;
11253 dynobj
= htab
->dynobj
;
11255 emit_relocs
= (bfd_link_relocatable (info
)
11256 || info
->emitrelocations
);
11258 flinfo
.info
= info
;
11259 flinfo
.output_bfd
= abfd
;
11260 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11261 if (flinfo
.symstrtab
== NULL
)
11266 flinfo
.hash_sec
= NULL
;
11267 flinfo
.symver_sec
= NULL
;
11271 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11272 /* Note that dynsym_sec can be NULL (on VMS). */
11273 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11274 /* Note that it is OK if symver_sec is NULL. */
11277 flinfo
.contents
= NULL
;
11278 flinfo
.external_relocs
= NULL
;
11279 flinfo
.internal_relocs
= NULL
;
11280 flinfo
.external_syms
= NULL
;
11281 flinfo
.locsym_shndx
= NULL
;
11282 flinfo
.internal_syms
= NULL
;
11283 flinfo
.indices
= NULL
;
11284 flinfo
.sections
= NULL
;
11285 flinfo
.symshndxbuf
= NULL
;
11286 flinfo
.filesym_count
= 0;
11288 /* The object attributes have been merged. Remove the input
11289 sections from the link, and set the contents of the output
11291 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11292 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11294 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11295 || strcmp (o
->name
, ".gnu.attributes") == 0)
11297 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11299 asection
*input_section
;
11301 if (p
->type
!= bfd_indirect_link_order
)
11303 input_section
= p
->u
.indirect
.section
;
11304 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11305 elf_link_input_bfd ignores this section. */
11306 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11309 attr_size
= bfd_elf_obj_attr_size (abfd
);
11312 bfd_set_section_size (abfd
, o
, attr_size
);
11314 /* Skip this section later on. */
11315 o
->map_head
.link_order
= NULL
;
11318 o
->flags
|= SEC_EXCLUDE
;
11322 /* Count up the number of relocations we will output for each output
11323 section, so that we know the sizes of the reloc sections. We
11324 also figure out some maximum sizes. */
11325 max_contents_size
= 0;
11326 max_external_reloc_size
= 0;
11327 max_internal_reloc_count
= 0;
11329 max_sym_shndx_count
= 0;
11331 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11333 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11334 o
->reloc_count
= 0;
11336 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11338 unsigned int reloc_count
= 0;
11339 unsigned int additional_reloc_count
= 0;
11340 struct bfd_elf_section_data
*esdi
= NULL
;
11342 if (p
->type
== bfd_section_reloc_link_order
11343 || p
->type
== bfd_symbol_reloc_link_order
)
11345 else if (p
->type
== bfd_indirect_link_order
)
11349 sec
= p
->u
.indirect
.section
;
11351 /* Mark all sections which are to be included in the
11352 link. This will normally be every section. We need
11353 to do this so that we can identify any sections which
11354 the linker has decided to not include. */
11355 sec
->linker_mark
= TRUE
;
11357 if (sec
->flags
& SEC_MERGE
)
11360 if (sec
->rawsize
> max_contents_size
)
11361 max_contents_size
= sec
->rawsize
;
11362 if (sec
->size
> max_contents_size
)
11363 max_contents_size
= sec
->size
;
11365 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11366 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11370 /* We are interested in just local symbols, not all
11372 if (elf_bad_symtab (sec
->owner
))
11373 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11374 / bed
->s
->sizeof_sym
);
11376 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11378 if (sym_count
> max_sym_count
)
11379 max_sym_count
= sym_count
;
11381 if (sym_count
> max_sym_shndx_count
11382 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11383 max_sym_shndx_count
= sym_count
;
11385 if (esdo
->this_hdr
.sh_type
== SHT_REL
11386 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11387 /* Some backends use reloc_count in relocation sections
11388 to count particular types of relocs. Of course,
11389 reloc sections themselves can't have relocations. */
11391 else if (emit_relocs
)
11393 reloc_count
= sec
->reloc_count
;
11394 if (bed
->elf_backend_count_additional_relocs
)
11397 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11398 additional_reloc_count
+= c
;
11401 else if (bed
->elf_backend_count_relocs
)
11402 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11404 esdi
= elf_section_data (sec
);
11406 if ((sec
->flags
& SEC_RELOC
) != 0)
11408 size_t ext_size
= 0;
11410 if (esdi
->rel
.hdr
!= NULL
)
11411 ext_size
= esdi
->rel
.hdr
->sh_size
;
11412 if (esdi
->rela
.hdr
!= NULL
)
11413 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11415 if (ext_size
> max_external_reloc_size
)
11416 max_external_reloc_size
= ext_size
;
11417 if (sec
->reloc_count
> max_internal_reloc_count
)
11418 max_internal_reloc_count
= sec
->reloc_count
;
11423 if (reloc_count
== 0)
11426 reloc_count
+= additional_reloc_count
;
11427 o
->reloc_count
+= reloc_count
;
11429 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11433 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11434 esdo
->rel
.count
+= additional_reloc_count
;
11436 if (esdi
->rela
.hdr
)
11438 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11439 esdo
->rela
.count
+= additional_reloc_count
;
11445 esdo
->rela
.count
+= reloc_count
;
11447 esdo
->rel
.count
+= reloc_count
;
11451 if (o
->reloc_count
> 0)
11452 o
->flags
|= SEC_RELOC
;
11455 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11456 set it (this is probably a bug) and if it is set
11457 assign_section_numbers will create a reloc section. */
11458 o
->flags
&=~ SEC_RELOC
;
11461 /* If the SEC_ALLOC flag is not set, force the section VMA to
11462 zero. This is done in elf_fake_sections as well, but forcing
11463 the VMA to 0 here will ensure that relocs against these
11464 sections are handled correctly. */
11465 if ((o
->flags
& SEC_ALLOC
) == 0
11466 && ! o
->user_set_vma
)
11470 if (! bfd_link_relocatable (info
) && merged
)
11471 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11473 /* Figure out the file positions for everything but the symbol table
11474 and the relocs. We set symcount to force assign_section_numbers
11475 to create a symbol table. */
11476 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11477 BFD_ASSERT (! abfd
->output_has_begun
);
11478 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11481 /* Set sizes, and assign file positions for reloc sections. */
11482 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11484 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11485 if ((o
->flags
& SEC_RELOC
) != 0)
11488 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11492 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11496 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11497 to count upwards while actually outputting the relocations. */
11498 esdo
->rel
.count
= 0;
11499 esdo
->rela
.count
= 0;
11501 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11503 /* Cache the section contents so that they can be compressed
11504 later. Use bfd_malloc since it will be freed by
11505 bfd_compress_section_contents. */
11506 unsigned char *contents
= esdo
->this_hdr
.contents
;
11507 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11510 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11511 if (contents
== NULL
)
11513 esdo
->this_hdr
.contents
= contents
;
11517 /* We have now assigned file positions for all the sections except
11518 .symtab, .strtab, and non-loaded reloc sections. We start the
11519 .symtab section at the current file position, and write directly
11520 to it. We build the .strtab section in memory. */
11521 bfd_get_symcount (abfd
) = 0;
11522 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11523 /* sh_name is set in prep_headers. */
11524 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11525 /* sh_flags, sh_addr and sh_size all start off zero. */
11526 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11527 /* sh_link is set in assign_section_numbers. */
11528 /* sh_info is set below. */
11529 /* sh_offset is set just below. */
11530 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11532 if (max_sym_count
< 20)
11533 max_sym_count
= 20;
11534 htab
->strtabsize
= max_sym_count
;
11535 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11536 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11537 if (htab
->strtab
== NULL
)
11539 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11541 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11542 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11544 if (info
->strip
!= strip_all
|| emit_relocs
)
11546 file_ptr off
= elf_next_file_pos (abfd
);
11548 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11550 /* Note that at this point elf_next_file_pos (abfd) is
11551 incorrect. We do not yet know the size of the .symtab section.
11552 We correct next_file_pos below, after we do know the size. */
11554 /* Start writing out the symbol table. The first symbol is always a
11556 elfsym
.st_value
= 0;
11557 elfsym
.st_size
= 0;
11558 elfsym
.st_info
= 0;
11559 elfsym
.st_other
= 0;
11560 elfsym
.st_shndx
= SHN_UNDEF
;
11561 elfsym
.st_target_internal
= 0;
11562 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11563 bfd_und_section_ptr
, NULL
) != 1)
11566 /* Output a symbol for each section. We output these even if we are
11567 discarding local symbols, since they are used for relocs. These
11568 symbols have no names. We store the index of each one in the
11569 index field of the section, so that we can find it again when
11570 outputting relocs. */
11572 elfsym
.st_size
= 0;
11573 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11574 elfsym
.st_other
= 0;
11575 elfsym
.st_value
= 0;
11576 elfsym
.st_target_internal
= 0;
11577 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11579 o
= bfd_section_from_elf_index (abfd
, i
);
11582 o
->target_index
= bfd_get_symcount (abfd
);
11583 elfsym
.st_shndx
= i
;
11584 if (!bfd_link_relocatable (info
))
11585 elfsym
.st_value
= o
->vma
;
11586 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11593 /* Allocate some memory to hold information read in from the input
11595 if (max_contents_size
!= 0)
11597 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11598 if (flinfo
.contents
== NULL
)
11602 if (max_external_reloc_size
!= 0)
11604 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11605 if (flinfo
.external_relocs
== NULL
)
11609 if (max_internal_reloc_count
!= 0)
11611 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11612 amt
*= sizeof (Elf_Internal_Rela
);
11613 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11614 if (flinfo
.internal_relocs
== NULL
)
11618 if (max_sym_count
!= 0)
11620 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11621 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11622 if (flinfo
.external_syms
== NULL
)
11625 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11626 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11627 if (flinfo
.internal_syms
== NULL
)
11630 amt
= max_sym_count
* sizeof (long);
11631 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11632 if (flinfo
.indices
== NULL
)
11635 amt
= max_sym_count
* sizeof (asection
*);
11636 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11637 if (flinfo
.sections
== NULL
)
11641 if (max_sym_shndx_count
!= 0)
11643 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11644 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11645 if (flinfo
.locsym_shndx
== NULL
)
11651 bfd_vma base
, end
= 0;
11654 for (sec
= htab
->tls_sec
;
11655 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11658 bfd_size_type size
= sec
->size
;
11661 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11663 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11666 size
= ord
->offset
+ ord
->size
;
11668 end
= sec
->vma
+ size
;
11670 base
= htab
->tls_sec
->vma
;
11671 /* Only align end of TLS section if static TLS doesn't have special
11672 alignment requirements. */
11673 if (bed
->static_tls_alignment
== 1)
11674 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11675 htab
->tls_size
= end
- base
;
11678 /* Reorder SHF_LINK_ORDER sections. */
11679 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11681 if (!elf_fixup_link_order (abfd
, o
))
11685 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11688 /* Since ELF permits relocations to be against local symbols, we
11689 must have the local symbols available when we do the relocations.
11690 Since we would rather only read the local symbols once, and we
11691 would rather not keep them in memory, we handle all the
11692 relocations for a single input file at the same time.
11694 Unfortunately, there is no way to know the total number of local
11695 symbols until we have seen all of them, and the local symbol
11696 indices precede the global symbol indices. This means that when
11697 we are generating relocatable output, and we see a reloc against
11698 a global symbol, we can not know the symbol index until we have
11699 finished examining all the local symbols to see which ones we are
11700 going to output. To deal with this, we keep the relocations in
11701 memory, and don't output them until the end of the link. This is
11702 an unfortunate waste of memory, but I don't see a good way around
11703 it. Fortunately, it only happens when performing a relocatable
11704 link, which is not the common case. FIXME: If keep_memory is set
11705 we could write the relocs out and then read them again; I don't
11706 know how bad the memory loss will be. */
11708 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11709 sub
->output_has_begun
= FALSE
;
11710 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11712 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11714 if (p
->type
== bfd_indirect_link_order
11715 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11716 == bfd_target_elf_flavour
)
11717 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11719 if (! sub
->output_has_begun
)
11721 if (! elf_link_input_bfd (&flinfo
, sub
))
11723 sub
->output_has_begun
= TRUE
;
11726 else if (p
->type
== bfd_section_reloc_link_order
11727 || p
->type
== bfd_symbol_reloc_link_order
)
11729 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11734 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11736 if (p
->type
== bfd_indirect_link_order
11737 && (bfd_get_flavour (sub
)
11738 == bfd_target_elf_flavour
)
11739 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11740 != bed
->s
->elfclass
))
11742 const char *iclass
, *oclass
;
11744 switch (bed
->s
->elfclass
)
11746 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11747 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11748 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11752 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11754 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11755 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11756 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11760 bfd_set_error (bfd_error_wrong_format
);
11762 /* xgettext:c-format */
11763 (_("%B: file class %s incompatible with %s"),
11764 sub
, iclass
, oclass
);
11773 /* Free symbol buffer if needed. */
11774 if (!info
->reduce_memory_overheads
)
11776 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11777 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11778 && elf_tdata (sub
)->symbuf
)
11780 free (elf_tdata (sub
)->symbuf
);
11781 elf_tdata (sub
)->symbuf
= NULL
;
11785 /* Output any global symbols that got converted to local in a
11786 version script or due to symbol visibility. We do this in a
11787 separate step since ELF requires all local symbols to appear
11788 prior to any global symbols. FIXME: We should only do this if
11789 some global symbols were, in fact, converted to become local.
11790 FIXME: Will this work correctly with the Irix 5 linker? */
11791 eoinfo
.failed
= FALSE
;
11792 eoinfo
.flinfo
= &flinfo
;
11793 eoinfo
.localsyms
= TRUE
;
11794 eoinfo
.file_sym_done
= FALSE
;
11795 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11799 /* If backend needs to output some local symbols not present in the hash
11800 table, do it now. */
11801 if (bed
->elf_backend_output_arch_local_syms
11802 && (info
->strip
!= strip_all
|| emit_relocs
))
11804 typedef int (*out_sym_func
)
11805 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11806 struct elf_link_hash_entry
*);
11808 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11809 (abfd
, info
, &flinfo
,
11810 (out_sym_func
) elf_link_output_symstrtab
)))
11814 /* That wrote out all the local symbols. Finish up the symbol table
11815 with the global symbols. Even if we want to strip everything we
11816 can, we still need to deal with those global symbols that got
11817 converted to local in a version script. */
11819 /* The sh_info field records the index of the first non local symbol. */
11820 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11823 && htab
->dynsym
!= NULL
11824 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11826 Elf_Internal_Sym sym
;
11827 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11829 o
= htab
->dynsym
->output_section
;
11830 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11832 /* Write out the section symbols for the output sections. */
11833 if (bfd_link_pic (info
)
11834 || htab
->is_relocatable_executable
)
11840 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11842 sym
.st_target_internal
= 0;
11844 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11850 dynindx
= elf_section_data (s
)->dynindx
;
11853 indx
= elf_section_data (s
)->this_idx
;
11854 BFD_ASSERT (indx
> 0);
11855 sym
.st_shndx
= indx
;
11856 if (! check_dynsym (abfd
, &sym
))
11858 sym
.st_value
= s
->vma
;
11859 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11860 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11864 /* Write out the local dynsyms. */
11865 if (htab
->dynlocal
)
11867 struct elf_link_local_dynamic_entry
*e
;
11868 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11873 /* Copy the internal symbol and turn off visibility.
11874 Note that we saved a word of storage and overwrote
11875 the original st_name with the dynstr_index. */
11877 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11879 s
= bfd_section_from_elf_index (e
->input_bfd
,
11884 elf_section_data (s
->output_section
)->this_idx
;
11885 if (! check_dynsym (abfd
, &sym
))
11887 sym
.st_value
= (s
->output_section
->vma
11889 + e
->isym
.st_value
);
11892 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11893 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11898 /* We get the global symbols from the hash table. */
11899 eoinfo
.failed
= FALSE
;
11900 eoinfo
.localsyms
= FALSE
;
11901 eoinfo
.flinfo
= &flinfo
;
11902 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11906 /* If backend needs to output some symbols not present in the hash
11907 table, do it now. */
11908 if (bed
->elf_backend_output_arch_syms
11909 && (info
->strip
!= strip_all
|| emit_relocs
))
11911 typedef int (*out_sym_func
)
11912 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11913 struct elf_link_hash_entry
*);
11915 if (! ((*bed
->elf_backend_output_arch_syms
)
11916 (abfd
, info
, &flinfo
,
11917 (out_sym_func
) elf_link_output_symstrtab
)))
11921 /* Finalize the .strtab section. */
11922 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11924 /* Swap out the .strtab section. */
11925 if (!elf_link_swap_symbols_out (&flinfo
))
11928 /* Now we know the size of the symtab section. */
11929 if (bfd_get_symcount (abfd
) > 0)
11931 /* Finish up and write out the symbol string table (.strtab)
11933 Elf_Internal_Shdr
*symstrtab_hdr
;
11934 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11936 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11937 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11939 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11940 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11941 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11942 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11943 symtab_shndx_hdr
->sh_size
= amt
;
11945 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11948 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11949 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11953 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11954 /* sh_name was set in prep_headers. */
11955 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11956 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11957 symstrtab_hdr
->sh_addr
= 0;
11958 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11959 symstrtab_hdr
->sh_entsize
= 0;
11960 symstrtab_hdr
->sh_link
= 0;
11961 symstrtab_hdr
->sh_info
= 0;
11962 /* sh_offset is set just below. */
11963 symstrtab_hdr
->sh_addralign
= 1;
11965 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11967 elf_next_file_pos (abfd
) = off
;
11969 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11970 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11974 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11976 _bfd_error_handler (_("%B: failed to generate import library"),
11977 info
->out_implib_bfd
);
11981 /* Adjust the relocs to have the correct symbol indices. */
11982 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11984 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11986 if ((o
->flags
& SEC_RELOC
) == 0)
11989 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11990 if (esdo
->rel
.hdr
!= NULL
11991 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
11993 if (esdo
->rela
.hdr
!= NULL
11994 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
11997 /* Set the reloc_count field to 0 to prevent write_relocs from
11998 trying to swap the relocs out itself. */
11999 o
->reloc_count
= 0;
12002 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12003 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12005 /* If we are linking against a dynamic object, or generating a
12006 shared library, finish up the dynamic linking information. */
12009 bfd_byte
*dyncon
, *dynconend
;
12011 /* Fix up .dynamic entries. */
12012 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12013 BFD_ASSERT (o
!= NULL
);
12015 dyncon
= o
->contents
;
12016 dynconend
= o
->contents
+ o
->size
;
12017 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12019 Elf_Internal_Dyn dyn
;
12023 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12030 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12032 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12034 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12035 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12038 dyn
.d_un
.d_val
= relativecount
;
12045 name
= info
->init_function
;
12048 name
= info
->fini_function
;
12051 struct elf_link_hash_entry
*h
;
12053 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12055 && (h
->root
.type
== bfd_link_hash_defined
12056 || h
->root
.type
== bfd_link_hash_defweak
))
12058 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12059 o
= h
->root
.u
.def
.section
;
12060 if (o
->output_section
!= NULL
)
12061 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12062 + o
->output_offset
);
12065 /* The symbol is imported from another shared
12066 library and does not apply to this one. */
12067 dyn
.d_un
.d_ptr
= 0;
12074 case DT_PREINIT_ARRAYSZ
:
12075 name
= ".preinit_array";
12077 case DT_INIT_ARRAYSZ
:
12078 name
= ".init_array";
12080 case DT_FINI_ARRAYSZ
:
12081 name
= ".fini_array";
12083 o
= bfd_get_section_by_name (abfd
, name
);
12087 (_("could not find section %s"), name
);
12092 (_("warning: %s section has zero size"), name
);
12093 dyn
.d_un
.d_val
= o
->size
;
12096 case DT_PREINIT_ARRAY
:
12097 name
= ".preinit_array";
12099 case DT_INIT_ARRAY
:
12100 name
= ".init_array";
12102 case DT_FINI_ARRAY
:
12103 name
= ".fini_array";
12105 o
= bfd_get_section_by_name (abfd
, name
);
12112 name
= ".gnu.hash";
12121 name
= ".gnu.version_d";
12124 name
= ".gnu.version_r";
12127 name
= ".gnu.version";
12129 o
= bfd_get_linker_section (dynobj
, name
);
12134 (_("could not find section %s"), name
);
12137 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12140 (_("warning: section '%s' is being made into a note"), name
);
12141 bfd_set_error (bfd_error_nonrepresentable_section
);
12144 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12151 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12155 dyn
.d_un
.d_val
= 0;
12156 dyn
.d_un
.d_ptr
= 0;
12157 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12159 Elf_Internal_Shdr
*hdr
;
12161 hdr
= elf_elfsections (abfd
)[i
];
12162 if (hdr
->sh_type
== type
12163 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12165 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12166 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12169 if (dyn
.d_un
.d_ptr
== 0
12170 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12171 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12175 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12177 /* Don't count procedure linkage table relocs in the
12178 overall reloc count. */
12179 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12180 dyn
.d_un
.d_val
-= htab
->srelplt
->size
;
12181 /* If .rela.plt is the first .rela section, exclude
12182 it from DT_RELA. */
12183 else if (dyn
.d_un
.d_ptr
== (htab
->srelplt
->output_section
->vma
12184 + htab
->srelplt
->output_offset
))
12185 dyn
.d_un
.d_ptr
+= htab
->srelplt
->size
;
12189 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12193 /* If we have created any dynamic sections, then output them. */
12194 if (dynobj
!= NULL
)
12196 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12199 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12200 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12201 || info
->error_textrel
)
12202 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12204 bfd_byte
*dyncon
, *dynconend
;
12206 dyncon
= o
->contents
;
12207 dynconend
= o
->contents
+ o
->size
;
12208 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12210 Elf_Internal_Dyn dyn
;
12212 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12214 if (dyn
.d_tag
== DT_TEXTREL
)
12216 if (info
->error_textrel
)
12217 info
->callbacks
->einfo
12218 (_("%P%X: read-only segment has dynamic relocations.\n"));
12220 info
->callbacks
->einfo
12221 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12227 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12229 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12231 || o
->output_section
== bfd_abs_section_ptr
)
12233 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12235 /* At this point, we are only interested in sections
12236 created by _bfd_elf_link_create_dynamic_sections. */
12239 if (htab
->stab_info
.stabstr
== o
)
12241 if (htab
->eh_info
.hdr_sec
== o
)
12243 if (strcmp (o
->name
, ".dynstr") != 0)
12245 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12247 (file_ptr
) o
->output_offset
12248 * bfd_octets_per_byte (abfd
),
12254 /* The contents of the .dynstr section are actually in a
12258 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12259 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12260 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12266 if (bfd_link_relocatable (info
))
12268 bfd_boolean failed
= FALSE
;
12270 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12275 /* If we have optimized stabs strings, output them. */
12276 if (htab
->stab_info
.stabstr
!= NULL
)
12278 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12282 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12285 elf_final_link_free (abfd
, &flinfo
);
12287 elf_linker (abfd
) = TRUE
;
12291 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12292 if (contents
== NULL
)
12293 return FALSE
; /* Bail out and fail. */
12294 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12295 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12302 elf_final_link_free (abfd
, &flinfo
);
12306 /* Initialize COOKIE for input bfd ABFD. */
12309 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12310 struct bfd_link_info
*info
, bfd
*abfd
)
12312 Elf_Internal_Shdr
*symtab_hdr
;
12313 const struct elf_backend_data
*bed
;
12315 bed
= get_elf_backend_data (abfd
);
12316 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12318 cookie
->abfd
= abfd
;
12319 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12320 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12321 if (cookie
->bad_symtab
)
12323 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12324 cookie
->extsymoff
= 0;
12328 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12329 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12332 if (bed
->s
->arch_size
== 32)
12333 cookie
->r_sym_shift
= 8;
12335 cookie
->r_sym_shift
= 32;
12337 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12338 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12340 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12341 cookie
->locsymcount
, 0,
12343 if (cookie
->locsyms
== NULL
)
12345 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12348 if (info
->keep_memory
)
12349 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12354 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12357 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12359 Elf_Internal_Shdr
*symtab_hdr
;
12361 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12362 if (cookie
->locsyms
!= NULL
12363 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12364 free (cookie
->locsyms
);
12367 /* Initialize the relocation information in COOKIE for input section SEC
12368 of input bfd ABFD. */
12371 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12372 struct bfd_link_info
*info
, bfd
*abfd
,
12375 const struct elf_backend_data
*bed
;
12377 if (sec
->reloc_count
== 0)
12379 cookie
->rels
= NULL
;
12380 cookie
->relend
= NULL
;
12384 bed
= get_elf_backend_data (abfd
);
12386 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12387 info
->keep_memory
);
12388 if (cookie
->rels
== NULL
)
12390 cookie
->rel
= cookie
->rels
;
12391 cookie
->relend
= (cookie
->rels
12392 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12394 cookie
->rel
= cookie
->rels
;
12398 /* Free the memory allocated by init_reloc_cookie_rels,
12402 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12405 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12406 free (cookie
->rels
);
12409 /* Initialize the whole of COOKIE for input section SEC. */
12412 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12413 struct bfd_link_info
*info
,
12416 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12418 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12423 fini_reloc_cookie (cookie
, sec
->owner
);
12428 /* Free the memory allocated by init_reloc_cookie_for_section,
12432 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12435 fini_reloc_cookie_rels (cookie
, sec
);
12436 fini_reloc_cookie (cookie
, sec
->owner
);
12439 /* Garbage collect unused sections. */
12441 /* Default gc_mark_hook. */
12444 _bfd_elf_gc_mark_hook (asection
*sec
,
12445 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12446 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12447 struct elf_link_hash_entry
*h
,
12448 Elf_Internal_Sym
*sym
)
12452 switch (h
->root
.type
)
12454 case bfd_link_hash_defined
:
12455 case bfd_link_hash_defweak
:
12456 return h
->root
.u
.def
.section
;
12458 case bfd_link_hash_common
:
12459 return h
->root
.u
.c
.p
->section
;
12466 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12471 /* For undefined __start_<name> and __stop_<name> symbols, return the
12472 first input section matching <name>. Return NULL otherwise. */
12475 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12476 struct elf_link_hash_entry
*h
)
12479 const char *sec_name
;
12481 if (h
->root
.type
!= bfd_link_hash_undefined
12482 && h
->root
.type
!= bfd_link_hash_undefweak
)
12485 s
= h
->root
.u
.undef
.section
;
12488 if (s
== (asection
*) 0 - 1)
12494 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12495 sec_name
= h
->root
.root
.string
+ 8;
12496 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12497 sec_name
= h
->root
.root
.string
+ 7;
12499 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12503 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12505 s
= bfd_get_section_by_name (i
, sec_name
);
12508 h
->root
.u
.undef
.section
= s
;
12515 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12520 /* COOKIE->rel describes a relocation against section SEC, which is
12521 a section we've decided to keep. Return the section that contains
12522 the relocation symbol, or NULL if no section contains it. */
12525 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12526 elf_gc_mark_hook_fn gc_mark_hook
,
12527 struct elf_reloc_cookie
*cookie
,
12528 bfd_boolean
*start_stop
)
12530 unsigned long r_symndx
;
12531 struct elf_link_hash_entry
*h
;
12533 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12534 if (r_symndx
== STN_UNDEF
)
12537 if (r_symndx
>= cookie
->locsymcount
12538 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12540 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12543 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12547 while (h
->root
.type
== bfd_link_hash_indirect
12548 || h
->root
.type
== bfd_link_hash_warning
)
12549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12551 /* If this symbol is weak and there is a non-weak definition, we
12552 keep the non-weak definition because many backends put
12553 dynamic reloc info on the non-weak definition for code
12554 handling copy relocs. */
12555 if (h
->u
.weakdef
!= NULL
)
12556 h
->u
.weakdef
->mark
= 1;
12558 if (start_stop
!= NULL
)
12560 /* To work around a glibc bug, mark all XXX input sections
12561 when there is an as yet undefined reference to __start_XXX
12562 or __stop_XXX symbols. The linker will later define such
12563 symbols for orphan input sections that have a name
12564 representable as a C identifier. */
12565 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12569 *start_stop
= !s
->gc_mark
;
12574 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12577 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12578 &cookie
->locsyms
[r_symndx
]);
12581 /* COOKIE->rel describes a relocation against section SEC, which is
12582 a section we've decided to keep. Mark the section that contains
12583 the relocation symbol. */
12586 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12588 elf_gc_mark_hook_fn gc_mark_hook
,
12589 struct elf_reloc_cookie
*cookie
)
12592 bfd_boolean start_stop
= FALSE
;
12594 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12595 while (rsec
!= NULL
)
12597 if (!rsec
->gc_mark
)
12599 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12600 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12602 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12607 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12612 /* The mark phase of garbage collection. For a given section, mark
12613 it and any sections in this section's group, and all the sections
12614 which define symbols to which it refers. */
12617 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12619 elf_gc_mark_hook_fn gc_mark_hook
)
12622 asection
*group_sec
, *eh_frame
;
12626 /* Mark all the sections in the group. */
12627 group_sec
= elf_section_data (sec
)->next_in_group
;
12628 if (group_sec
&& !group_sec
->gc_mark
)
12629 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12632 /* Look through the section relocs. */
12634 eh_frame
= elf_eh_frame_section (sec
->owner
);
12635 if ((sec
->flags
& SEC_RELOC
) != 0
12636 && sec
->reloc_count
> 0
12637 && sec
!= eh_frame
)
12639 struct elf_reloc_cookie cookie
;
12641 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12645 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12646 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12651 fini_reloc_cookie_for_section (&cookie
, sec
);
12655 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12657 struct elf_reloc_cookie cookie
;
12659 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12663 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12664 gc_mark_hook
, &cookie
))
12666 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12670 eh_frame
= elf_section_eh_frame_entry (sec
);
12671 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12672 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12678 /* Scan and mark sections in a special or debug section group. */
12681 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12683 /* Point to first section of section group. */
12685 /* Used to iterate the section group. */
12688 bfd_boolean is_special_grp
= TRUE
;
12689 bfd_boolean is_debug_grp
= TRUE
;
12691 /* First scan to see if group contains any section other than debug
12692 and special section. */
12693 ssec
= msec
= elf_next_in_group (grp
);
12696 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12697 is_debug_grp
= FALSE
;
12699 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12700 is_special_grp
= FALSE
;
12702 msec
= elf_next_in_group (msec
);
12704 while (msec
!= ssec
);
12706 /* If this is a pure debug section group or pure special section group,
12707 keep all sections in this group. */
12708 if (is_debug_grp
|| is_special_grp
)
12713 msec
= elf_next_in_group (msec
);
12715 while (msec
!= ssec
);
12719 /* Keep debug and special sections. */
12722 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12723 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12727 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12730 bfd_boolean some_kept
;
12731 bfd_boolean debug_frag_seen
;
12733 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12736 /* Ensure all linker created sections are kept,
12737 see if any other section is already marked,
12738 and note if we have any fragmented debug sections. */
12739 debug_frag_seen
= some_kept
= FALSE
;
12740 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12742 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12744 else if (isec
->gc_mark
)
12747 if (debug_frag_seen
== FALSE
12748 && (isec
->flags
& SEC_DEBUGGING
)
12749 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12750 debug_frag_seen
= TRUE
;
12753 /* If no section in this file will be kept, then we can
12754 toss out the debug and special sections. */
12758 /* Keep debug and special sections like .comment when they are
12759 not part of a group. Also keep section groups that contain
12760 just debug sections or special sections. */
12761 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12763 if ((isec
->flags
& SEC_GROUP
) != 0)
12764 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12765 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12766 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12767 && elf_next_in_group (isec
) == NULL
)
12771 if (! debug_frag_seen
)
12774 /* Look for CODE sections which are going to be discarded,
12775 and find and discard any fragmented debug sections which
12776 are associated with that code section. */
12777 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12778 if ((isec
->flags
& SEC_CODE
) != 0
12779 && isec
->gc_mark
== 0)
12784 ilen
= strlen (isec
->name
);
12786 /* Association is determined by the name of the debug section
12787 containing the name of the code section as a suffix. For
12788 example .debug_line.text.foo is a debug section associated
12790 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12794 if (dsec
->gc_mark
== 0
12795 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12798 dlen
= strlen (dsec
->name
);
12801 && strncmp (dsec
->name
+ (dlen
- ilen
),
12802 isec
->name
, ilen
) == 0)
12812 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12814 struct elf_gc_sweep_symbol_info
12816 struct bfd_link_info
*info
;
12817 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12822 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12825 && (((h
->root
.type
== bfd_link_hash_defined
12826 || h
->root
.type
== bfd_link_hash_defweak
)
12827 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12828 && h
->root
.u
.def
.section
->gc_mark
))
12829 || h
->root
.type
== bfd_link_hash_undefined
12830 || h
->root
.type
== bfd_link_hash_undefweak
))
12832 struct elf_gc_sweep_symbol_info
*inf
;
12834 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12835 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12836 h
->def_regular
= 0;
12837 h
->ref_regular
= 0;
12838 h
->ref_regular_nonweak
= 0;
12844 /* The sweep phase of garbage collection. Remove all garbage sections. */
12846 typedef bfd_boolean (*gc_sweep_hook_fn
)
12847 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12850 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12853 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12854 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12855 unsigned long section_sym_count
;
12856 struct elf_gc_sweep_symbol_info sweep_info
;
12858 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12862 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12863 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12866 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12868 /* When any section in a section group is kept, we keep all
12869 sections in the section group. If the first member of
12870 the section group is excluded, we will also exclude the
12872 if (o
->flags
& SEC_GROUP
)
12874 asection
*first
= elf_next_in_group (o
);
12875 o
->gc_mark
= first
->gc_mark
;
12881 /* Skip sweeping sections already excluded. */
12882 if (o
->flags
& SEC_EXCLUDE
)
12885 /* Since this is early in the link process, it is simple
12886 to remove a section from the output. */
12887 o
->flags
|= SEC_EXCLUDE
;
12889 if (info
->print_gc_sections
&& o
->size
!= 0)
12890 /* xgettext:c-format */
12891 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12893 /* But we also have to update some of the relocation
12894 info we collected before. */
12896 && (o
->flags
& SEC_RELOC
) != 0
12897 && o
->reloc_count
!= 0
12898 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12899 && (o
->flags
& SEC_DEBUGGING
) != 0)
12900 && !bfd_is_abs_section (o
->output_section
))
12902 Elf_Internal_Rela
*internal_relocs
;
12906 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12907 info
->keep_memory
);
12908 if (internal_relocs
== NULL
)
12911 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12913 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12914 free (internal_relocs
);
12922 /* Remove the symbols that were in the swept sections from the dynamic
12923 symbol table. GCFIXME: Anyone know how to get them out of the
12924 static symbol table as well? */
12925 sweep_info
.info
= info
;
12926 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12927 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12930 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12934 /* Propagate collected vtable information. This is called through
12935 elf_link_hash_traverse. */
12938 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12940 /* Those that are not vtables. */
12941 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12944 /* Those vtables that do not have parents, we cannot merge. */
12945 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12948 /* If we've already been done, exit. */
12949 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12952 /* Make sure the parent's table is up to date. */
12953 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12955 if (h
->vtable
->used
== NULL
)
12957 /* None of this table's entries were referenced. Re-use the
12959 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12960 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12965 bfd_boolean
*cu
, *pu
;
12967 /* Or the parent's entries into ours. */
12968 cu
= h
->vtable
->used
;
12970 pu
= h
->vtable
->parent
->vtable
->used
;
12973 const struct elf_backend_data
*bed
;
12974 unsigned int log_file_align
;
12976 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12977 log_file_align
= bed
->s
->log_file_align
;
12978 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12993 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12996 bfd_vma hstart
, hend
;
12997 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12998 const struct elf_backend_data
*bed
;
12999 unsigned int log_file_align
;
13001 /* Take care of both those symbols that do not describe vtables as
13002 well as those that are not loaded. */
13003 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13006 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13007 || h
->root
.type
== bfd_link_hash_defweak
);
13009 sec
= h
->root
.u
.def
.section
;
13010 hstart
= h
->root
.u
.def
.value
;
13011 hend
= hstart
+ h
->size
;
13013 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13015 return *(bfd_boolean
*) okp
= FALSE
;
13016 bed
= get_elf_backend_data (sec
->owner
);
13017 log_file_align
= bed
->s
->log_file_align
;
13019 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13021 for (rel
= relstart
; rel
< relend
; ++rel
)
13022 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13024 /* If the entry is in use, do nothing. */
13025 if (h
->vtable
->used
13026 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13028 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13029 if (h
->vtable
->used
[entry
])
13032 /* Otherwise, kill it. */
13033 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13039 /* Mark sections containing dynamically referenced symbols. When
13040 building shared libraries, we must assume that any visible symbol is
13044 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13046 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13047 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13049 if ((h
->root
.type
== bfd_link_hash_defined
13050 || h
->root
.type
== bfd_link_hash_defweak
)
13052 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13053 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13054 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13055 && (!bfd_link_executable (info
)
13056 || info
->gc_keep_exported
13057 || info
->export_dynamic
13060 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13061 && (h
->versioned
>= versioned
13062 || !bfd_hide_sym_by_version (info
->version_info
,
13063 h
->root
.root
.string
)))))
13064 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13069 /* Keep all sections containing symbols undefined on the command-line,
13070 and the section containing the entry symbol. */
13073 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13075 struct bfd_sym_chain
*sym
;
13077 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13079 struct elf_link_hash_entry
*h
;
13081 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13082 FALSE
, FALSE
, FALSE
);
13085 && (h
->root
.type
== bfd_link_hash_defined
13086 || h
->root
.type
== bfd_link_hash_defweak
)
13087 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13088 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13089 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13094 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13095 struct bfd_link_info
*info
)
13097 bfd
*ibfd
= info
->input_bfds
;
13099 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13102 struct elf_reloc_cookie cookie
;
13104 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13107 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13110 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13112 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13113 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13115 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13116 fini_reloc_cookie_rels (&cookie
, sec
);
13123 /* Do mark and sweep of unused sections. */
13126 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13128 bfd_boolean ok
= TRUE
;
13130 elf_gc_mark_hook_fn gc_mark_hook
;
13131 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13132 struct elf_link_hash_table
*htab
;
13134 if (!bed
->can_gc_sections
13135 || !is_elf_hash_table (info
->hash
))
13137 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13141 bed
->gc_keep (info
);
13142 htab
= elf_hash_table (info
);
13144 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13145 at the .eh_frame section if we can mark the FDEs individually. */
13146 for (sub
= info
->input_bfds
;
13147 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13148 sub
= sub
->link
.next
)
13151 struct elf_reloc_cookie cookie
;
13153 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13154 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13156 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13157 if (elf_section_data (sec
)->sec_info
13158 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13159 elf_eh_frame_section (sub
) = sec
;
13160 fini_reloc_cookie_for_section (&cookie
, sec
);
13161 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13165 /* Apply transitive closure to the vtable entry usage info. */
13166 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13170 /* Kill the vtable relocations that were not used. */
13171 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13175 /* Mark dynamically referenced symbols. */
13176 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13177 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13179 /* Grovel through relocs to find out who stays ... */
13180 gc_mark_hook
= bed
->gc_mark_hook
;
13181 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13185 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13186 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13189 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13190 Also treat note sections as a root, if the section is not part
13192 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13194 && (o
->flags
& SEC_EXCLUDE
) == 0
13195 && ((o
->flags
& SEC_KEEP
) != 0
13196 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13197 && elf_next_in_group (o
) == NULL
)))
13199 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13204 /* Allow the backend to mark additional target specific sections. */
13205 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13207 /* ... and mark SEC_EXCLUDE for those that go. */
13208 return elf_gc_sweep (abfd
, info
);
13211 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13214 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13216 struct elf_link_hash_entry
*h
,
13219 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13220 struct elf_link_hash_entry
**search
, *child
;
13221 size_t extsymcount
;
13222 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13224 /* The sh_info field of the symtab header tells us where the
13225 external symbols start. We don't care about the local symbols at
13227 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13228 if (!elf_bad_symtab (abfd
))
13229 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13231 sym_hashes
= elf_sym_hashes (abfd
);
13232 sym_hashes_end
= sym_hashes
+ extsymcount
;
13234 /* Hunt down the child symbol, which is in this section at the same
13235 offset as the relocation. */
13236 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13238 if ((child
= *search
) != NULL
13239 && (child
->root
.type
== bfd_link_hash_defined
13240 || child
->root
.type
== bfd_link_hash_defweak
)
13241 && child
->root
.u
.def
.section
== sec
13242 && child
->root
.u
.def
.value
== offset
)
13246 /* xgettext:c-format */
13247 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13248 abfd
, sec
, (unsigned long) offset
);
13249 bfd_set_error (bfd_error_invalid_operation
);
13253 if (!child
->vtable
)
13255 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13256 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13257 if (!child
->vtable
)
13262 /* This *should* only be the absolute section. It could potentially
13263 be that someone has defined a non-global vtable though, which
13264 would be bad. It isn't worth paging in the local symbols to be
13265 sure though; that case should simply be handled by the assembler. */
13267 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13270 child
->vtable
->parent
= h
;
13275 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13278 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13279 asection
*sec ATTRIBUTE_UNUSED
,
13280 struct elf_link_hash_entry
*h
,
13283 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13284 unsigned int log_file_align
= bed
->s
->log_file_align
;
13288 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13289 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13294 if (addend
>= h
->vtable
->size
)
13296 size_t size
, bytes
, file_align
;
13297 bfd_boolean
*ptr
= h
->vtable
->used
;
13299 /* While the symbol is undefined, we have to be prepared to handle
13301 file_align
= 1 << log_file_align
;
13302 if (h
->root
.type
== bfd_link_hash_undefined
)
13303 size
= addend
+ file_align
;
13307 if (addend
>= size
)
13309 /* Oops! We've got a reference past the defined end of
13310 the table. This is probably a bug -- shall we warn? */
13311 size
= addend
+ file_align
;
13314 size
= (size
+ file_align
- 1) & -file_align
;
13316 /* Allocate one extra entry for use as a "done" flag for the
13317 consolidation pass. */
13318 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13322 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13328 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13329 * sizeof (bfd_boolean
));
13330 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13334 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13339 /* And arrange for that done flag to be at index -1. */
13340 h
->vtable
->used
= ptr
+ 1;
13341 h
->vtable
->size
= size
;
13344 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13349 /* Map an ELF section header flag to its corresponding string. */
13353 flagword flag_value
;
13354 } elf_flags_to_name_table
;
13356 static elf_flags_to_name_table elf_flags_to_names
[] =
13358 { "SHF_WRITE", SHF_WRITE
},
13359 { "SHF_ALLOC", SHF_ALLOC
},
13360 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13361 { "SHF_MERGE", SHF_MERGE
},
13362 { "SHF_STRINGS", SHF_STRINGS
},
13363 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13364 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13365 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13366 { "SHF_GROUP", SHF_GROUP
},
13367 { "SHF_TLS", SHF_TLS
},
13368 { "SHF_MASKOS", SHF_MASKOS
},
13369 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13372 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13374 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13375 struct flag_info
*flaginfo
,
13378 const bfd_vma sh_flags
= elf_section_flags (section
);
13380 if (!flaginfo
->flags_initialized
)
13382 bfd
*obfd
= info
->output_bfd
;
13383 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13384 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13386 int without_hex
= 0;
13388 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13391 flagword (*lookup
) (char *);
13393 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13394 if (lookup
!= NULL
)
13396 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13400 if (tf
->with
== with_flags
)
13401 with_hex
|= hexval
;
13402 else if (tf
->with
== without_flags
)
13403 without_hex
|= hexval
;
13408 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13410 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13412 if (tf
->with
== with_flags
)
13413 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13414 else if (tf
->with
== without_flags
)
13415 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13422 info
->callbacks
->einfo
13423 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13427 flaginfo
->flags_initialized
= TRUE
;
13428 flaginfo
->only_with_flags
|= with_hex
;
13429 flaginfo
->not_with_flags
|= without_hex
;
13432 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13435 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13441 struct alloc_got_off_arg
{
13443 struct bfd_link_info
*info
;
13446 /* We need a special top-level link routine to convert got reference counts
13447 to real got offsets. */
13450 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13452 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13453 bfd
*obfd
= gofarg
->info
->output_bfd
;
13454 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13456 if (h
->got
.refcount
> 0)
13458 h
->got
.offset
= gofarg
->gotoff
;
13459 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13462 h
->got
.offset
= (bfd_vma
) -1;
13467 /* And an accompanying bit to work out final got entry offsets once
13468 we're done. Should be called from final_link. */
13471 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13472 struct bfd_link_info
*info
)
13475 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13477 struct alloc_got_off_arg gofarg
;
13479 BFD_ASSERT (abfd
== info
->output_bfd
);
13481 if (! is_elf_hash_table (info
->hash
))
13484 /* The GOT offset is relative to the .got section, but the GOT header is
13485 put into the .got.plt section, if the backend uses it. */
13486 if (bed
->want_got_plt
)
13489 gotoff
= bed
->got_header_size
;
13491 /* Do the local .got entries first. */
13492 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13494 bfd_signed_vma
*local_got
;
13495 size_t j
, locsymcount
;
13496 Elf_Internal_Shdr
*symtab_hdr
;
13498 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13501 local_got
= elf_local_got_refcounts (i
);
13505 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13506 if (elf_bad_symtab (i
))
13507 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13509 locsymcount
= symtab_hdr
->sh_info
;
13511 for (j
= 0; j
< locsymcount
; ++j
)
13513 if (local_got
[j
] > 0)
13515 local_got
[j
] = gotoff
;
13516 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13519 local_got
[j
] = (bfd_vma
) -1;
13523 /* Then the global .got entries. .plt refcounts are handled by
13524 adjust_dynamic_symbol */
13525 gofarg
.gotoff
= gotoff
;
13526 gofarg
.info
= info
;
13527 elf_link_hash_traverse (elf_hash_table (info
),
13528 elf_gc_allocate_got_offsets
,
13533 /* Many folk need no more in the way of final link than this, once
13534 got entry reference counting is enabled. */
13537 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13539 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13542 /* Invoke the regular ELF backend linker to do all the work. */
13543 return bfd_elf_final_link (abfd
, info
);
13547 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13549 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13551 if (rcookie
->bad_symtab
)
13552 rcookie
->rel
= rcookie
->rels
;
13554 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13556 unsigned long r_symndx
;
13558 if (! rcookie
->bad_symtab
)
13559 if (rcookie
->rel
->r_offset
> offset
)
13561 if (rcookie
->rel
->r_offset
!= offset
)
13564 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13565 if (r_symndx
== STN_UNDEF
)
13568 if (r_symndx
>= rcookie
->locsymcount
13569 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13571 struct elf_link_hash_entry
*h
;
13573 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13575 while (h
->root
.type
== bfd_link_hash_indirect
13576 || h
->root
.type
== bfd_link_hash_warning
)
13577 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13579 if ((h
->root
.type
== bfd_link_hash_defined
13580 || h
->root
.type
== bfd_link_hash_defweak
)
13581 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13582 || h
->root
.u
.def
.section
->kept_section
!= NULL
13583 || discarded_section (h
->root
.u
.def
.section
)))
13588 /* It's not a relocation against a global symbol,
13589 but it could be a relocation against a local
13590 symbol for a discarded section. */
13592 Elf_Internal_Sym
*isym
;
13594 /* Need to: get the symbol; get the section. */
13595 isym
= &rcookie
->locsyms
[r_symndx
];
13596 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13598 && (isec
->kept_section
!= NULL
13599 || discarded_section (isec
)))
13607 /* Discard unneeded references to discarded sections.
13608 Returns -1 on error, 1 if any section's size was changed, 0 if
13609 nothing changed. This function assumes that the relocations are in
13610 sorted order, which is true for all known assemblers. */
13613 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13615 struct elf_reloc_cookie cookie
;
13620 if (info
->traditional_format
13621 || !is_elf_hash_table (info
->hash
))
13624 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13629 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13632 || i
->reloc_count
== 0
13633 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13637 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13640 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13643 if (_bfd_discard_section_stabs (abfd
, i
,
13644 elf_section_data (i
)->sec_info
,
13645 bfd_elf_reloc_symbol_deleted_p
,
13649 fini_reloc_cookie_for_section (&cookie
, i
);
13654 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13655 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13660 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13666 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13669 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13672 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13673 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13674 bfd_elf_reloc_symbol_deleted_p
,
13678 fini_reloc_cookie_for_section (&cookie
, i
);
13682 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13684 const struct elf_backend_data
*bed
;
13686 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13689 bed
= get_elf_backend_data (abfd
);
13691 if (bed
->elf_backend_discard_info
!= NULL
)
13693 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13696 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13699 fini_reloc_cookie (&cookie
, abfd
);
13703 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13704 _bfd_elf_end_eh_frame_parsing (info
);
13706 if (info
->eh_frame_hdr_type
13707 && !bfd_link_relocatable (info
)
13708 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13715 _bfd_elf_section_already_linked (bfd
*abfd
,
13717 struct bfd_link_info
*info
)
13720 const char *name
, *key
;
13721 struct bfd_section_already_linked
*l
;
13722 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13724 if (sec
->output_section
== bfd_abs_section_ptr
)
13727 flags
= sec
->flags
;
13729 /* Return if it isn't a linkonce section. A comdat group section
13730 also has SEC_LINK_ONCE set. */
13731 if ((flags
& SEC_LINK_ONCE
) == 0)
13734 /* Don't put group member sections on our list of already linked
13735 sections. They are handled as a group via their group section. */
13736 if (elf_sec_group (sec
) != NULL
)
13739 /* For a SHT_GROUP section, use the group signature as the key. */
13741 if ((flags
& SEC_GROUP
) != 0
13742 && elf_next_in_group (sec
) != NULL
13743 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13744 key
= elf_group_name (elf_next_in_group (sec
));
13747 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13748 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13749 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13752 /* Must be a user linkonce section that doesn't follow gcc's
13753 naming convention. In this case we won't be matching
13754 single member groups. */
13758 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13760 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13762 /* We may have 2 different types of sections on the list: group
13763 sections with a signature of <key> (<key> is some string),
13764 and linkonce sections named .gnu.linkonce.<type>.<key>.
13765 Match like sections. LTO plugin sections are an exception.
13766 They are always named .gnu.linkonce.t.<key> and match either
13767 type of section. */
13768 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13769 && ((flags
& SEC_GROUP
) != 0
13770 || strcmp (name
, l
->sec
->name
) == 0))
13771 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13773 /* The section has already been linked. See if we should
13774 issue a warning. */
13775 if (!_bfd_handle_already_linked (sec
, l
, info
))
13778 if (flags
& SEC_GROUP
)
13780 asection
*first
= elf_next_in_group (sec
);
13781 asection
*s
= first
;
13785 s
->output_section
= bfd_abs_section_ptr
;
13786 /* Record which group discards it. */
13787 s
->kept_section
= l
->sec
;
13788 s
= elf_next_in_group (s
);
13789 /* These lists are circular. */
13799 /* A single member comdat group section may be discarded by a
13800 linkonce section and vice versa. */
13801 if ((flags
& SEC_GROUP
) != 0)
13803 asection
*first
= elf_next_in_group (sec
);
13805 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13806 /* Check this single member group against linkonce sections. */
13807 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13808 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13809 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13811 first
->output_section
= bfd_abs_section_ptr
;
13812 first
->kept_section
= l
->sec
;
13813 sec
->output_section
= bfd_abs_section_ptr
;
13818 /* Check this linkonce section against single member groups. */
13819 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13820 if (l
->sec
->flags
& SEC_GROUP
)
13822 asection
*first
= elf_next_in_group (l
->sec
);
13825 && elf_next_in_group (first
) == first
13826 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13828 sec
->output_section
= bfd_abs_section_ptr
;
13829 sec
->kept_section
= first
;
13834 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13835 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13836 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13837 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13838 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13839 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13840 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13841 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13842 The reverse order cannot happen as there is never a bfd with only the
13843 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13844 matter as here were are looking only for cross-bfd sections. */
13846 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13847 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13848 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13849 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13851 if (abfd
!= l
->sec
->owner
)
13852 sec
->output_section
= bfd_abs_section_ptr
;
13856 /* This is the first section with this name. Record it. */
13857 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13858 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13859 return sec
->output_section
== bfd_abs_section_ptr
;
13863 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13865 return sym
->st_shndx
== SHN_COMMON
;
13869 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13875 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13877 return bfd_com_section_ptr
;
13881 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13882 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13883 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13884 bfd
*ibfd ATTRIBUTE_UNUSED
,
13885 unsigned long symndx ATTRIBUTE_UNUSED
)
13887 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13888 return bed
->s
->arch_size
/ 8;
13891 /* Routines to support the creation of dynamic relocs. */
13893 /* Returns the name of the dynamic reloc section associated with SEC. */
13895 static const char *
13896 get_dynamic_reloc_section_name (bfd
* abfd
,
13898 bfd_boolean is_rela
)
13901 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13902 const char *prefix
= is_rela
? ".rela" : ".rel";
13904 if (old_name
== NULL
)
13907 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13908 sprintf (name
, "%s%s", prefix
, old_name
);
13913 /* Returns the dynamic reloc section associated with SEC.
13914 If necessary compute the name of the dynamic reloc section based
13915 on SEC's name (looked up in ABFD's string table) and the setting
13919 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13921 bfd_boolean is_rela
)
13923 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13925 if (reloc_sec
== NULL
)
13927 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13931 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13933 if (reloc_sec
!= NULL
)
13934 elf_section_data (sec
)->sreloc
= reloc_sec
;
13941 /* Returns the dynamic reloc section associated with SEC. If the
13942 section does not exist it is created and attached to the DYNOBJ
13943 bfd and stored in the SRELOC field of SEC's elf_section_data
13946 ALIGNMENT is the alignment for the newly created section and
13947 IS_RELA defines whether the name should be .rela.<SEC's name>
13948 or .rel.<SEC's name>. The section name is looked up in the
13949 string table associated with ABFD. */
13952 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13954 unsigned int alignment
,
13956 bfd_boolean is_rela
)
13958 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13960 if (reloc_sec
== NULL
)
13962 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13967 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13969 if (reloc_sec
== NULL
)
13971 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13972 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13973 if ((sec
->flags
& SEC_ALLOC
) != 0)
13974 flags
|= SEC_ALLOC
| SEC_LOAD
;
13976 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13977 if (reloc_sec
!= NULL
)
13979 /* _bfd_elf_get_sec_type_attr chooses a section type by
13980 name. Override as it may be wrong, eg. for a user
13981 section named "auto" we'll get ".relauto" which is
13982 seen to be a .rela section. */
13983 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13984 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13989 elf_section_data (sec
)->sreloc
= reloc_sec
;
13995 /* Copy the ELF symbol type and other attributes for a linker script
13996 assignment from HSRC to HDEST. Generally this should be treated as
13997 if we found a strong non-dynamic definition for HDEST (except that
13998 ld ignores multiple definition errors). */
14000 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14001 struct bfd_link_hash_entry
*hdest
,
14002 struct bfd_link_hash_entry
*hsrc
)
14004 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14005 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14006 Elf_Internal_Sym isym
;
14008 ehdest
->type
= ehsrc
->type
;
14009 ehdest
->target_internal
= ehsrc
->target_internal
;
14011 isym
.st_other
= ehsrc
->other
;
14012 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14015 /* Append a RELA relocation REL to section S in BFD. */
14018 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14020 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14021 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14022 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14023 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14026 /* Append a REL relocation REL to section S in BFD. */
14029 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14031 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14032 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14033 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14034 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);