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 s
= bfd_get_linker_section (abfd
, ".got");
156 flags
= bed
->dynamic_sec_flags
;
158 s
= bfd_make_section_anyway_with_flags (abfd
,
159 (bed
->rela_plts_and_copies_p
160 ? ".rela.got" : ".rel.got"),
161 (bed
->dynamic_sec_flags
164 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
168 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
170 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
174 if (bed
->want_got_plt
)
176 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
178 || !bfd_set_section_alignment (abfd
, s
,
179 bed
->s
->log_file_align
))
184 /* The first bit of the global offset table is the header. */
185 s
->size
+= bed
->got_header_size
;
187 if (bed
->want_got_sym
)
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info
)->hgot
= h
;
203 /* Create a strtab to hold the dynamic symbol names. */
205 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
207 struct elf_link_hash_table
*hash_table
;
209 hash_table
= elf_hash_table (info
);
210 if (hash_table
->dynobj
== NULL
)
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
219 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
221 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
227 hash_table
->dynobj
= abfd
;
230 if (hash_table
->dynstr
== NULL
)
232 hash_table
->dynstr
= _bfd_elf_strtab_init ();
233 if (hash_table
->dynstr
== NULL
)
239 /* Create some sections which will be filled in with dynamic linking
240 information. ABFD is an input file which requires dynamic sections
241 to be created. The dynamic sections take up virtual memory space
242 when the final executable is run, so we need to create them before
243 addresses are assigned to the output sections. We work out the
244 actual contents and size of these sections later. */
247 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
251 const struct elf_backend_data
*bed
;
252 struct elf_link_hash_entry
*h
;
254 if (! is_elf_hash_table (info
->hash
))
257 if (elf_hash_table (info
)->dynamic_sections_created
)
260 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
263 abfd
= elf_hash_table (info
)->dynobj
;
264 bed
= get_elf_backend_data (abfd
);
266 flags
= bed
->dynamic_sec_flags
;
268 /* A dynamically linked executable has a .interp section, but a
269 shared library does not. */
270 if (bfd_link_executable (info
) && !info
->nointerp
)
272 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
273 flags
| SEC_READONLY
);
278 /* Create sections to hold version informations. These are removed
279 if they are not needed. */
280 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
281 flags
| SEC_READONLY
);
283 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
286 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
287 flags
| SEC_READONLY
);
289 || ! bfd_set_section_alignment (abfd
, s
, 1))
292 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
293 flags
| SEC_READONLY
);
295 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
298 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
299 flags
| SEC_READONLY
);
301 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
303 elf_hash_table (info
)->dynsym
= s
;
305 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
306 flags
| SEC_READONLY
);
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
312 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
315 /* The special symbol _DYNAMIC is always set to the start of the
316 .dynamic section. We could set _DYNAMIC in a linker script, but we
317 only want to define it if we are, in fact, creating a .dynamic
318 section. We don't want to define it if there is no .dynamic
319 section, since on some ELF platforms the start up code examines it
320 to decide how to initialize the process. */
321 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
322 elf_hash_table (info
)->hdynamic
= h
;
328 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
329 flags
| SEC_READONLY
);
331 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
333 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
336 if (info
->emit_gnu_hash
)
338 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
339 flags
| SEC_READONLY
);
341 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
343 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
344 4 32-bit words followed by variable count of 64-bit words, then
345 variable count of 32-bit words. */
346 if (bed
->s
->arch_size
== 64)
347 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
349 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
352 /* Let the backend create the rest of the sections. This lets the
353 backend set the right flags. The backend will normally create
354 the .got and .plt sections. */
355 if (bed
->elf_backend_create_dynamic_sections
== NULL
356 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
359 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
364 /* Create dynamic sections when linking against a dynamic object. */
367 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
369 flagword flags
, pltflags
;
370 struct elf_link_hash_entry
*h
;
372 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
373 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
375 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
376 .rel[a].bss sections. */
377 flags
= bed
->dynamic_sec_flags
;
380 if (bed
->plt_not_loaded
)
381 /* We do not clear SEC_ALLOC here because we still want the OS to
382 allocate space for the section; it's just that there's nothing
383 to read in from the object file. */
384 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
386 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
387 if (bed
->plt_readonly
)
388 pltflags
|= SEC_READONLY
;
390 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
392 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
396 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
398 if (bed
->want_plt_sym
)
400 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
401 "_PROCEDURE_LINKAGE_TABLE_");
402 elf_hash_table (info
)->hplt
= h
;
407 s
= bfd_make_section_anyway_with_flags (abfd
,
408 (bed
->rela_plts_and_copies_p
409 ? ".rela.plt" : ".rel.plt"),
410 flags
| SEC_READONLY
);
412 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
416 if (! _bfd_elf_create_got_section (abfd
, info
))
419 if (bed
->want_dynbss
)
421 /* The .dynbss section is a place to put symbols which are defined
422 by dynamic objects, are referenced by regular objects, and are
423 not functions. We must allocate space for them in the process
424 image and use a R_*_COPY reloc to tell the dynamic linker to
425 initialize them at run time. The linker script puts the .dynbss
426 section into the .bss section of the final image. */
427 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
428 (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_pic (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
))
458 /* Record a new dynamic symbol. We record the dynamic symbols as we
459 read the input files, since we need to have a list of all of them
460 before we can determine the final sizes of the output sections.
461 Note that we may actually call this function even though we are not
462 going to output any dynamic symbols; in some cases we know that a
463 symbol should be in the dynamic symbol table, but only if there is
467 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
468 struct elf_link_hash_entry
*h
)
470 if (h
->dynindx
== -1)
472 struct elf_strtab_hash
*dynstr
;
477 /* XXX: The ABI draft says the linker must turn hidden and
478 internal symbols into STB_LOCAL symbols when producing the
479 DSO. However, if ld.so honors st_other in the dynamic table,
480 this would not be necessary. */
481 switch (ELF_ST_VISIBILITY (h
->other
))
485 if (h
->root
.type
!= bfd_link_hash_undefined
486 && h
->root
.type
!= bfd_link_hash_undefweak
)
489 if (!elf_hash_table (info
)->is_relocatable_executable
)
497 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
498 ++elf_hash_table (info
)->dynsymcount
;
500 dynstr
= elf_hash_table (info
)->dynstr
;
503 /* Create a strtab to hold the dynamic symbol names. */
504 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
509 /* We don't put any version information in the dynamic string
511 name
= h
->root
.root
.string
;
512 p
= strchr (name
, ELF_VER_CHR
);
514 /* We know that the p points into writable memory. In fact,
515 there are only a few symbols that have read-only names, being
516 those like _GLOBAL_OFFSET_TABLE_ that are created specially
517 by the backends. Most symbols will have names pointing into
518 an ELF string table read from a file, or to objalloc memory. */
521 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
526 if (indx
== (size_t) -1)
528 h
->dynstr_index
= indx
;
534 /* Mark a symbol dynamic. */
537 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
538 struct elf_link_hash_entry
*h
,
539 Elf_Internal_Sym
*sym
)
541 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
543 /* It may be called more than once on the same H. */
544 if(h
->dynamic
|| bfd_link_relocatable (info
))
547 if ((info
->dynamic_data
548 && (h
->type
== STT_OBJECT
549 || h
->type
== STT_COMMON
551 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
552 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
554 && h
->root
.type
== bfd_link_hash_new
555 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
559 /* Record an assignment to a symbol made by a linker script. We need
560 this in case some dynamic object refers to this symbol. */
563 bfd_elf_record_link_assignment (bfd
*output_bfd
,
564 struct bfd_link_info
*info
,
569 struct elf_link_hash_entry
*h
, *hv
;
570 struct elf_link_hash_table
*htab
;
571 const struct elf_backend_data
*bed
;
573 if (!is_elf_hash_table (info
->hash
))
576 htab
= elf_hash_table (info
);
577 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
581 if (h
->versioned
== unknown
)
583 /* Set versioned if symbol version is unknown. */
584 char *version
= strrchr (name
, ELF_VER_CHR
);
587 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
588 h
->versioned
= versioned_hidden
;
590 h
->versioned
= versioned
;
594 switch (h
->root
.type
)
596 case bfd_link_hash_defined
:
597 case bfd_link_hash_defweak
:
598 case bfd_link_hash_common
:
600 case bfd_link_hash_undefweak
:
601 case bfd_link_hash_undefined
:
602 /* Since we're defining the symbol, don't let it seem to have not
603 been defined. record_dynamic_symbol and size_dynamic_sections
604 may depend on this. */
605 h
->root
.type
= bfd_link_hash_new
;
606 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
607 bfd_link_repair_undef_list (&htab
->root
);
609 case bfd_link_hash_new
:
610 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
613 case bfd_link_hash_indirect
:
614 /* We had a versioned symbol in a dynamic library. We make the
615 the versioned symbol point to this one. */
616 bed
= get_elf_backend_data (output_bfd
);
618 while (hv
->root
.type
== bfd_link_hash_indirect
619 || hv
->root
.type
== bfd_link_hash_warning
)
620 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
621 /* We don't need to update h->root.u since linker will set them
623 h
->root
.type
= bfd_link_hash_undefined
;
624 hv
->root
.type
= bfd_link_hash_indirect
;
625 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
626 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
628 case bfd_link_hash_warning
:
633 /* If this symbol is being provided by the linker script, and it is
634 currently defined by a dynamic object, but not by a regular
635 object, then mark it as undefined so that the generic linker will
636 force the correct value. */
640 h
->root
.type
= bfd_link_hash_undefined
;
642 /* If this symbol is not being provided by the linker script, and it is
643 currently defined by a dynamic object, but not by a regular object,
644 then clear out any version information because the symbol will not be
645 associated with the dynamic object any more. */
649 h
->verinfo
.verdef
= NULL
;
655 bed
= get_elf_backend_data (output_bfd
);
656 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
657 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
658 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
661 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
663 if (!bfd_link_relocatable (info
)
665 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
666 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
671 || bfd_link_dll (info
)
672 || elf_hash_table (info
)->is_relocatable_executable
)
675 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
678 /* If this is a weak defined symbol, and we know a corresponding
679 real symbol from the same dynamic object, make sure the real
680 symbol is also made into a dynamic symbol. */
681 if (h
->u
.weakdef
!= NULL
682 && h
->u
.weakdef
->dynindx
== -1)
684 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
692 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
693 success, and 2 on a failure caused by attempting to record a symbol
694 in a discarded section, eg. a discarded link-once section symbol. */
697 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
702 struct elf_link_local_dynamic_entry
*entry
;
703 struct elf_link_hash_table
*eht
;
704 struct elf_strtab_hash
*dynstr
;
707 Elf_External_Sym_Shndx eshndx
;
708 char esym
[sizeof (Elf64_External_Sym
)];
710 if (! is_elf_hash_table (info
->hash
))
713 /* See if the entry exists already. */
714 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
715 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
718 amt
= sizeof (*entry
);
719 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
723 /* Go find the symbol, so that we can find it's name. */
724 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
725 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
727 bfd_release (input_bfd
, entry
);
731 if (entry
->isym
.st_shndx
!= SHN_UNDEF
732 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
736 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
737 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
739 /* We can still bfd_release here as nothing has done another
740 bfd_alloc. We can't do this later in this function. */
741 bfd_release (input_bfd
, entry
);
746 name
= (bfd_elf_string_from_elf_section
747 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
748 entry
->isym
.st_name
));
750 dynstr
= elf_hash_table (info
)->dynstr
;
753 /* Create a strtab to hold the dynamic symbol names. */
754 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
759 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
760 if (dynstr_index
== (size_t) -1)
762 entry
->isym
.st_name
= dynstr_index
;
764 eht
= elf_hash_table (info
);
766 entry
->next
= eht
->dynlocal
;
767 eht
->dynlocal
= entry
;
768 entry
->input_bfd
= input_bfd
;
769 entry
->input_indx
= input_indx
;
772 /* Whatever binding the symbol had before, it's now local. */
774 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
776 /* The dynindx will be set at the end of size_dynamic_sections. */
781 /* Return the dynindex of a local dynamic symbol. */
784 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
788 struct elf_link_local_dynamic_entry
*e
;
790 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
791 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
796 /* This function is used to renumber the dynamic symbols, if some of
797 them are removed because they are marked as local. This is called
798 via elf_link_hash_traverse. */
801 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
804 size_t *count
= (size_t *) data
;
809 if (h
->dynindx
!= -1)
810 h
->dynindx
= ++(*count
);
816 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
817 STB_LOCAL binding. */
820 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
823 size_t *count
= (size_t *) data
;
825 if (!h
->forced_local
)
828 if (h
->dynindx
!= -1)
829 h
->dynindx
= ++(*count
);
834 /* Return true if the dynamic symbol for a given section should be
835 omitted when creating a shared library. */
837 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
838 struct bfd_link_info
*info
,
841 struct elf_link_hash_table
*htab
;
844 switch (elf_section_data (p
)->this_hdr
.sh_type
)
848 /* If sh_type is yet undecided, assume it could be
849 SHT_PROGBITS/SHT_NOBITS. */
851 htab
= elf_hash_table (info
);
852 if (p
== htab
->tls_sec
)
855 if (htab
->text_index_section
!= NULL
)
856 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
858 return (htab
->dynobj
!= NULL
859 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
860 && ip
->output_section
== p
);
862 /* There shouldn't be section relative relocations
863 against any other section. */
869 /* Assign dynsym indices. In a shared library we generate a section
870 symbol for each output section, which come first. Next come symbols
871 which have been forced to local binding. Then all of the back-end
872 allocated local dynamic syms, followed by the rest of the global
876 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
877 struct bfd_link_info
*info
,
878 unsigned long *section_sym_count
)
880 unsigned long dynsymcount
= 0;
882 if (bfd_link_pic (info
)
883 || elf_hash_table (info
)->is_relocatable_executable
)
885 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
887 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
888 if ((p
->flags
& SEC_EXCLUDE
) == 0
889 && (p
->flags
& SEC_ALLOC
) != 0
890 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
891 elf_section_data (p
)->dynindx
= ++dynsymcount
;
893 elf_section_data (p
)->dynindx
= 0;
895 *section_sym_count
= dynsymcount
;
897 elf_link_hash_traverse (elf_hash_table (info
),
898 elf_link_renumber_local_hash_table_dynsyms
,
901 if (elf_hash_table (info
)->dynlocal
)
903 struct elf_link_local_dynamic_entry
*p
;
904 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
905 p
->dynindx
= ++dynsymcount
;
907 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
909 elf_link_hash_traverse (elf_hash_table (info
),
910 elf_link_renumber_hash_table_dynsyms
,
913 /* There is an unused NULL entry at the head of the table which we
914 must account for in our count even if the table is empty since it
915 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
919 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
923 /* Merge st_other field. */
926 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
927 const Elf_Internal_Sym
*isym
, asection
*sec
,
928 bfd_boolean definition
, bfd_boolean dynamic
)
930 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
932 /* If st_other has a processor-specific meaning, specific
933 code might be needed here. */
934 if (bed
->elf_backend_merge_symbol_attribute
)
935 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
940 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
941 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
943 /* Keep the most constraining visibility. Leave the remainder
944 of the st_other field to elf_backend_merge_symbol_attribute. */
945 if (symvis
- 1 < hvis
- 1)
946 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
949 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
950 && (sec
->flags
& SEC_READONLY
) == 0)
951 h
->protected_def
= 1;
954 /* This function is called when we want to merge a new symbol with an
955 existing symbol. It handles the various cases which arise when we
956 find a definition in a dynamic object, or when there is already a
957 definition in a dynamic object. The new symbol is described by
958 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
959 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
960 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
961 of an old common symbol. We set OVERRIDE if the old symbol is
962 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
963 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
964 to change. By OK to change, we mean that we shouldn't warn if the
965 type or size does change. */
968 _bfd_elf_merge_symbol (bfd
*abfd
,
969 struct bfd_link_info
*info
,
971 Elf_Internal_Sym
*sym
,
974 struct elf_link_hash_entry
**sym_hash
,
976 bfd_boolean
*pold_weak
,
977 unsigned int *pold_alignment
,
979 bfd_boolean
*override
,
980 bfd_boolean
*type_change_ok
,
981 bfd_boolean
*size_change_ok
,
982 bfd_boolean
*matched
)
984 asection
*sec
, *oldsec
;
985 struct elf_link_hash_entry
*h
;
986 struct elf_link_hash_entry
*hi
;
987 struct elf_link_hash_entry
*flip
;
990 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
991 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
992 const struct elf_backend_data
*bed
;
999 bind
= ELF_ST_BIND (sym
->st_info
);
1001 if (! bfd_is_und_section (sec
))
1002 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1004 h
= ((struct elf_link_hash_entry
*)
1005 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1010 bed
= get_elf_backend_data (abfd
);
1012 /* NEW_VERSION is the symbol version of the new symbol. */
1013 if (h
->versioned
!= unversioned
)
1015 /* Symbol version is unknown or versioned. */
1016 new_version
= strrchr (name
, ELF_VER_CHR
);
1019 if (h
->versioned
== unknown
)
1021 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1022 h
->versioned
= versioned_hidden
;
1024 h
->versioned
= versioned
;
1027 if (new_version
[0] == '\0')
1031 h
->versioned
= unversioned
;
1036 /* For merging, we only care about real symbols. But we need to make
1037 sure that indirect symbol dynamic flags are updated. */
1039 while (h
->root
.type
== bfd_link_hash_indirect
1040 || h
->root
.type
== bfd_link_hash_warning
)
1041 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1045 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1049 /* OLD_HIDDEN is true if the existing symbol is only visible
1050 to the symbol with the same symbol version. NEW_HIDDEN is
1051 true if the new symbol is only visible to the symbol with
1052 the same symbol version. */
1053 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1054 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1055 if (!old_hidden
&& !new_hidden
)
1056 /* The new symbol matches the existing symbol if both
1061 /* OLD_VERSION is the symbol version of the existing
1065 if (h
->versioned
>= versioned
)
1066 old_version
= strrchr (h
->root
.root
.string
,
1071 /* The new symbol matches the existing symbol if they
1072 have the same symbol version. */
1073 *matched
= (old_version
== new_version
1074 || (old_version
!= NULL
1075 && new_version
!= NULL
1076 && strcmp (old_version
, new_version
) == 0));
1081 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1086 switch (h
->root
.type
)
1091 case bfd_link_hash_undefined
:
1092 case bfd_link_hash_undefweak
:
1093 oldbfd
= h
->root
.u
.undef
.abfd
;
1096 case bfd_link_hash_defined
:
1097 case bfd_link_hash_defweak
:
1098 oldbfd
= h
->root
.u
.def
.section
->owner
;
1099 oldsec
= h
->root
.u
.def
.section
;
1102 case bfd_link_hash_common
:
1103 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1104 oldsec
= h
->root
.u
.c
.p
->section
;
1106 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1109 if (poldbfd
&& *poldbfd
== NULL
)
1112 /* Differentiate strong and weak symbols. */
1113 newweak
= bind
== STB_WEAK
;
1114 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1115 || h
->root
.type
== bfd_link_hash_undefweak
);
1117 *pold_weak
= oldweak
;
1119 /* This code is for coping with dynamic objects, and is only useful
1120 if we are doing an ELF link. */
1121 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1124 /* We have to check it for every instance since the first few may be
1125 references and not all compilers emit symbol type for undefined
1127 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1129 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1130 respectively, is from a dynamic object. */
1132 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1134 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1135 syms and defined syms in dynamic libraries respectively.
1136 ref_dynamic on the other hand can be set for a symbol defined in
1137 a dynamic library, and def_dynamic may not be set; When the
1138 definition in a dynamic lib is overridden by a definition in the
1139 executable use of the symbol in the dynamic lib becomes a
1140 reference to the executable symbol. */
1143 if (bfd_is_und_section (sec
))
1145 if (bind
!= STB_WEAK
)
1147 h
->ref_dynamic_nonweak
= 1;
1148 hi
->ref_dynamic_nonweak
= 1;
1153 /* Update the existing symbol only if they match. */
1156 hi
->dynamic_def
= 1;
1160 /* If we just created the symbol, mark it as being an ELF symbol.
1161 Other than that, there is nothing to do--there is no merge issue
1162 with a newly defined symbol--so we just return. */
1164 if (h
->root
.type
== bfd_link_hash_new
)
1170 /* In cases involving weak versioned symbols, we may wind up trying
1171 to merge a symbol with itself. Catch that here, to avoid the
1172 confusion that results if we try to override a symbol with
1173 itself. The additional tests catch cases like
1174 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1175 dynamic object, which we do want to handle here. */
1177 && (newweak
|| oldweak
)
1178 && ((abfd
->flags
& DYNAMIC
) == 0
1179 || !h
->def_regular
))
1184 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1185 else if (oldsec
!= NULL
)
1187 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1188 indices used by MIPS ELF. */
1189 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1192 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1193 respectively, appear to be a definition rather than reference. */
1195 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1197 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1198 && h
->root
.type
!= bfd_link_hash_undefweak
1199 && h
->root
.type
!= bfd_link_hash_common
);
1201 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1202 respectively, appear to be a function. */
1204 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1205 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1207 oldfunc
= (h
->type
!= STT_NOTYPE
1208 && bed
->is_function_type (h
->type
));
1210 /* If creating a default indirect symbol ("foo" or "foo@") from a
1211 dynamic versioned definition ("foo@@") skip doing so if there is
1212 an existing regular definition with a different type. We don't
1213 want, for example, a "time" variable in the executable overriding
1214 a "time" function in a shared library. */
1215 if (pold_alignment
== NULL
1219 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1220 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1221 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1222 && h
->type
!= STT_NOTYPE
1223 && !(newfunc
&& oldfunc
))
1229 /* Check TLS symbols. We don't check undefined symbols introduced
1230 by "ld -u" which have no type (and oldbfd NULL), and we don't
1231 check symbols from plugins because they also have no type. */
1233 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1234 && (abfd
->flags
& BFD_PLUGIN
) == 0
1235 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1236 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1239 bfd_boolean ntdef
, tdef
;
1240 asection
*ntsec
, *tsec
;
1242 if (h
->type
== STT_TLS
)
1262 (*_bfd_error_handler
)
1263 (_("%s: TLS definition in %B section %A "
1264 "mismatches non-TLS definition in %B section %A"),
1265 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1266 else if (!tdef
&& !ntdef
)
1267 (*_bfd_error_handler
)
1268 (_("%s: TLS reference in %B "
1269 "mismatches non-TLS reference in %B"),
1270 tbfd
, ntbfd
, h
->root
.root
.string
);
1272 (*_bfd_error_handler
)
1273 (_("%s: TLS definition in %B section %A "
1274 "mismatches non-TLS reference in %B"),
1275 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1277 (*_bfd_error_handler
)
1278 (_("%s: TLS reference in %B "
1279 "mismatches non-TLS definition in %B section %A"),
1280 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1282 bfd_set_error (bfd_error_bad_value
);
1286 /* If the old symbol has non-default visibility, we ignore the new
1287 definition from a dynamic object. */
1289 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1290 && !bfd_is_und_section (sec
))
1293 /* Make sure this symbol is dynamic. */
1295 hi
->ref_dynamic
= 1;
1296 /* A protected symbol has external availability. Make sure it is
1297 recorded as dynamic.
1299 FIXME: Should we check type and size for protected symbol? */
1300 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1301 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1306 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1309 /* If the new symbol with non-default visibility comes from a
1310 relocatable file and the old definition comes from a dynamic
1311 object, we remove the old definition. */
1312 if (hi
->root
.type
== bfd_link_hash_indirect
)
1314 /* Handle the case where the old dynamic definition is
1315 default versioned. We need to copy the symbol info from
1316 the symbol with default version to the normal one if it
1317 was referenced before. */
1320 hi
->root
.type
= h
->root
.type
;
1321 h
->root
.type
= bfd_link_hash_indirect
;
1322 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1324 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1325 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1327 /* If the new symbol is hidden or internal, completely undo
1328 any dynamic link state. */
1329 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1330 h
->forced_local
= 0;
1337 /* FIXME: Should we check type and size for protected symbol? */
1347 /* If the old symbol was undefined before, then it will still be
1348 on the undefs list. If the new symbol is undefined or
1349 common, we can't make it bfd_link_hash_new here, because new
1350 undefined or common symbols will be added to the undefs list
1351 by _bfd_generic_link_add_one_symbol. Symbols may not be
1352 added twice to the undefs list. Also, if the new symbol is
1353 undefweak then we don't want to lose the strong undef. */
1354 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1356 h
->root
.type
= bfd_link_hash_undefined
;
1357 h
->root
.u
.undef
.abfd
= abfd
;
1361 h
->root
.type
= bfd_link_hash_new
;
1362 h
->root
.u
.undef
.abfd
= NULL
;
1365 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1367 /* If the new symbol is hidden or internal, completely undo
1368 any dynamic link state. */
1369 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1370 h
->forced_local
= 0;
1376 /* FIXME: Should we check type and size for protected symbol? */
1382 /* If a new weak symbol definition comes from a regular file and the
1383 old symbol comes from a dynamic library, we treat the new one as
1384 strong. Similarly, an old weak symbol definition from a regular
1385 file is treated as strong when the new symbol comes from a dynamic
1386 library. Further, an old weak symbol from a dynamic library is
1387 treated as strong if the new symbol is from a dynamic library.
1388 This reflects the way glibc's ld.so works.
1390 Do this before setting *type_change_ok or *size_change_ok so that
1391 we warn properly when dynamic library symbols are overridden. */
1393 if (newdef
&& !newdyn
&& olddyn
)
1395 if (olddef
&& newdyn
)
1398 /* Allow changes between different types of function symbol. */
1399 if (newfunc
&& oldfunc
)
1400 *type_change_ok
= TRUE
;
1402 /* It's OK to change the type if either the existing symbol or the
1403 new symbol is weak. A type change is also OK if the old symbol
1404 is undefined and the new symbol is defined. */
1409 && h
->root
.type
== bfd_link_hash_undefined
))
1410 *type_change_ok
= TRUE
;
1412 /* It's OK to change the size if either the existing symbol or the
1413 new symbol is weak, or if the old symbol is undefined. */
1416 || h
->root
.type
== bfd_link_hash_undefined
)
1417 *size_change_ok
= TRUE
;
1419 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1420 symbol, respectively, appears to be a common symbol in a dynamic
1421 object. If a symbol appears in an uninitialized section, and is
1422 not weak, and is not a function, then it may be a common symbol
1423 which was resolved when the dynamic object was created. We want
1424 to treat such symbols specially, because they raise special
1425 considerations when setting the symbol size: if the symbol
1426 appears as a common symbol in a regular object, and the size in
1427 the regular object is larger, we must make sure that we use the
1428 larger size. This problematic case can always be avoided in C,
1429 but it must be handled correctly when using Fortran shared
1432 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1433 likewise for OLDDYNCOMMON and OLDDEF.
1435 Note that this test is just a heuristic, and that it is quite
1436 possible to have an uninitialized symbol in a shared object which
1437 is really a definition, rather than a common symbol. This could
1438 lead to some minor confusion when the symbol really is a common
1439 symbol in some regular object. However, I think it will be
1445 && (sec
->flags
& SEC_ALLOC
) != 0
1446 && (sec
->flags
& SEC_LOAD
) == 0
1449 newdyncommon
= TRUE
;
1451 newdyncommon
= FALSE
;
1455 && h
->root
.type
== bfd_link_hash_defined
1457 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1458 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1461 olddyncommon
= TRUE
;
1463 olddyncommon
= FALSE
;
1465 /* We now know everything about the old and new symbols. We ask the
1466 backend to check if we can merge them. */
1467 if (bed
->merge_symbol
!= NULL
)
1469 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1474 /* If both the old and the new symbols look like common symbols in a
1475 dynamic object, set the size of the symbol to the larger of the
1480 && sym
->st_size
!= h
->size
)
1482 /* Since we think we have two common symbols, issue a multiple
1483 common warning if desired. Note that we only warn if the
1484 size is different. If the size is the same, we simply let
1485 the old symbol override the new one as normally happens with
1486 symbols defined in dynamic objects. */
1488 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1489 bfd_link_hash_common
, sym
->st_size
);
1490 if (sym
->st_size
> h
->size
)
1491 h
->size
= sym
->st_size
;
1493 *size_change_ok
= TRUE
;
1496 /* If we are looking at a dynamic object, and we have found a
1497 definition, we need to see if the symbol was already defined by
1498 some other object. If so, we want to use the existing
1499 definition, and we do not want to report a multiple symbol
1500 definition error; we do this by clobbering *PSEC to be
1501 bfd_und_section_ptr.
1503 We treat a common symbol as a definition if the symbol in the
1504 shared library is a function, since common symbols always
1505 represent variables; this can cause confusion in principle, but
1506 any such confusion would seem to indicate an erroneous program or
1507 shared library. We also permit a common symbol in a regular
1508 object to override a weak symbol in a shared object. A common
1509 symbol in executable also overrides a symbol in a shared object. */
1514 || (h
->root
.type
== bfd_link_hash_common
1517 || (!olddyn
&& bfd_link_executable (info
))))))
1521 newdyncommon
= FALSE
;
1523 *psec
= sec
= bfd_und_section_ptr
;
1524 *size_change_ok
= TRUE
;
1526 /* If we get here when the old symbol is a common symbol, then
1527 we are explicitly letting it override a weak symbol or
1528 function in a dynamic object, and we don't want to warn about
1529 a type change. If the old symbol is a defined symbol, a type
1530 change warning may still be appropriate. */
1532 if (h
->root
.type
== bfd_link_hash_common
)
1533 *type_change_ok
= TRUE
;
1536 /* Handle the special case of an old common symbol merging with a
1537 new symbol which looks like a common symbol in a shared object.
1538 We change *PSEC and *PVALUE to make the new symbol look like a
1539 common symbol, and let _bfd_generic_link_add_one_symbol do the
1543 && h
->root
.type
== bfd_link_hash_common
)
1547 newdyncommon
= FALSE
;
1548 *pvalue
= sym
->st_size
;
1549 *psec
= sec
= bed
->common_section (oldsec
);
1550 *size_change_ok
= TRUE
;
1553 /* Skip weak definitions of symbols that are already defined. */
1554 if (newdef
&& olddef
&& newweak
)
1556 /* Don't skip new non-IR weak syms. */
1557 if (!(oldbfd
!= NULL
1558 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1559 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1565 /* Merge st_other. If the symbol already has a dynamic index,
1566 but visibility says it should not be visible, turn it into a
1568 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1569 if (h
->dynindx
!= -1)
1570 switch (ELF_ST_VISIBILITY (h
->other
))
1574 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1579 /* If the old symbol is from a dynamic object, and the new symbol is
1580 a definition which is not from a dynamic object, then the new
1581 symbol overrides the old symbol. Symbols from regular files
1582 always take precedence over symbols from dynamic objects, even if
1583 they are defined after the dynamic object in the link.
1585 As above, we again permit a common symbol in a regular object to
1586 override a definition in a shared object if the shared object
1587 symbol is a function or is weak. */
1592 || (bfd_is_com_section (sec
)
1593 && (oldweak
|| oldfunc
)))
1598 /* Change the hash table entry to undefined, and let
1599 _bfd_generic_link_add_one_symbol do the right thing with the
1602 h
->root
.type
= bfd_link_hash_undefined
;
1603 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1604 *size_change_ok
= TRUE
;
1607 olddyncommon
= FALSE
;
1609 /* We again permit a type change when a common symbol may be
1610 overriding a function. */
1612 if (bfd_is_com_section (sec
))
1616 /* If a common symbol overrides a function, make sure
1617 that it isn't defined dynamically nor has type
1620 h
->type
= STT_NOTYPE
;
1622 *type_change_ok
= TRUE
;
1625 if (hi
->root
.type
== bfd_link_hash_indirect
)
1628 /* This union may have been set to be non-NULL when this symbol
1629 was seen in a dynamic object. We must force the union to be
1630 NULL, so that it is correct for a regular symbol. */
1631 h
->verinfo
.vertree
= NULL
;
1634 /* Handle the special case of a new common symbol merging with an
1635 old symbol that looks like it might be a common symbol defined in
1636 a shared object. Note that we have already handled the case in
1637 which a new common symbol should simply override the definition
1638 in the shared library. */
1641 && bfd_is_com_section (sec
)
1644 /* It would be best if we could set the hash table entry to a
1645 common symbol, but we don't know what to use for the section
1646 or the alignment. */
1647 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1648 bfd_link_hash_common
, sym
->st_size
);
1650 /* If the presumed common symbol in the dynamic object is
1651 larger, pretend that the new symbol has its size. */
1653 if (h
->size
> *pvalue
)
1656 /* We need to remember the alignment required by the symbol
1657 in the dynamic object. */
1658 BFD_ASSERT (pold_alignment
);
1659 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1662 olddyncommon
= FALSE
;
1664 h
->root
.type
= bfd_link_hash_undefined
;
1665 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1667 *size_change_ok
= TRUE
;
1668 *type_change_ok
= TRUE
;
1670 if (hi
->root
.type
== bfd_link_hash_indirect
)
1673 h
->verinfo
.vertree
= NULL
;
1678 /* Handle the case where we had a versioned symbol in a dynamic
1679 library and now find a definition in a normal object. In this
1680 case, we make the versioned symbol point to the normal one. */
1681 flip
->root
.type
= h
->root
.type
;
1682 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1683 h
->root
.type
= bfd_link_hash_indirect
;
1684 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1685 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1689 flip
->ref_dynamic
= 1;
1696 /* This function is called to create an indirect symbol from the
1697 default for the symbol with the default version if needed. The
1698 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1699 set DYNSYM if the new indirect symbol is dynamic. */
1702 _bfd_elf_add_default_symbol (bfd
*abfd
,
1703 struct bfd_link_info
*info
,
1704 struct elf_link_hash_entry
*h
,
1706 Elf_Internal_Sym
*sym
,
1710 bfd_boolean
*dynsym
)
1712 bfd_boolean type_change_ok
;
1713 bfd_boolean size_change_ok
;
1716 struct elf_link_hash_entry
*hi
;
1717 struct bfd_link_hash_entry
*bh
;
1718 const struct elf_backend_data
*bed
;
1719 bfd_boolean collect
;
1720 bfd_boolean dynamic
;
1721 bfd_boolean override
;
1723 size_t len
, shortlen
;
1725 bfd_boolean matched
;
1727 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1730 /* If this symbol has a version, and it is the default version, we
1731 create an indirect symbol from the default name to the fully
1732 decorated name. This will cause external references which do not
1733 specify a version to be bound to this version of the symbol. */
1734 p
= strchr (name
, ELF_VER_CHR
);
1735 if (h
->versioned
== unknown
)
1739 h
->versioned
= unversioned
;
1744 if (p
[1] != ELF_VER_CHR
)
1746 h
->versioned
= versioned_hidden
;
1750 h
->versioned
= versioned
;
1755 /* PR ld/19073: We may see an unversioned definition after the
1761 bed
= get_elf_backend_data (abfd
);
1762 collect
= bed
->collect
;
1763 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1765 shortlen
= p
- name
;
1766 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1767 if (shortname
== NULL
)
1769 memcpy (shortname
, name
, shortlen
);
1770 shortname
[shortlen
] = '\0';
1772 /* We are going to create a new symbol. Merge it with any existing
1773 symbol with this name. For the purposes of the merge, act as
1774 though we were defining the symbol we just defined, although we
1775 actually going to define an indirect symbol. */
1776 type_change_ok
= FALSE
;
1777 size_change_ok
= FALSE
;
1780 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1781 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1782 &type_change_ok
, &size_change_ok
, &matched
))
1788 if (hi
->def_regular
)
1790 /* If the undecorated symbol will have a version added by a
1791 script different to H, then don't indirect to/from the
1792 undecorated symbol. This isn't ideal because we may not yet
1793 have seen symbol versions, if given by a script on the
1794 command line rather than via --version-script. */
1795 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1800 = bfd_find_version_for_sym (info
->version_info
,
1801 hi
->root
.root
.string
, &hide
);
1802 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1804 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1808 if (hi
->verinfo
.vertree
!= NULL
1809 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1815 /* Add the default symbol if not performing a relocatable link. */
1816 if (! bfd_link_relocatable (info
))
1819 if (! (_bfd_generic_link_add_one_symbol
1820 (info
, abfd
, shortname
, BSF_INDIRECT
,
1821 bfd_ind_section_ptr
,
1822 0, name
, FALSE
, collect
, &bh
)))
1824 hi
= (struct elf_link_hash_entry
*) bh
;
1829 /* In this case the symbol named SHORTNAME is overriding the
1830 indirect symbol we want to add. We were planning on making
1831 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1832 is the name without a version. NAME is the fully versioned
1833 name, and it is the default version.
1835 Overriding means that we already saw a definition for the
1836 symbol SHORTNAME in a regular object, and it is overriding
1837 the symbol defined in the dynamic object.
1839 When this happens, we actually want to change NAME, the
1840 symbol we just added, to refer to SHORTNAME. This will cause
1841 references to NAME in the shared object to become references
1842 to SHORTNAME in the regular object. This is what we expect
1843 when we override a function in a shared object: that the
1844 references in the shared object will be mapped to the
1845 definition in the regular object. */
1847 while (hi
->root
.type
== bfd_link_hash_indirect
1848 || hi
->root
.type
== bfd_link_hash_warning
)
1849 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1851 h
->root
.type
= bfd_link_hash_indirect
;
1852 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1856 hi
->ref_dynamic
= 1;
1860 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1865 /* Now set HI to H, so that the following code will set the
1866 other fields correctly. */
1870 /* Check if HI is a warning symbol. */
1871 if (hi
->root
.type
== bfd_link_hash_warning
)
1872 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1874 /* If there is a duplicate definition somewhere, then HI may not
1875 point to an indirect symbol. We will have reported an error to
1876 the user in that case. */
1878 if (hi
->root
.type
== bfd_link_hash_indirect
)
1880 struct elf_link_hash_entry
*ht
;
1882 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1883 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1885 /* A reference to the SHORTNAME symbol from a dynamic library
1886 will be satisfied by the versioned symbol at runtime. In
1887 effect, we have a reference to the versioned symbol. */
1888 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1889 hi
->dynamic_def
|= ht
->dynamic_def
;
1891 /* See if the new flags lead us to realize that the symbol must
1897 if (! bfd_link_executable (info
)
1904 if (hi
->ref_regular
)
1910 /* We also need to define an indirection from the nondefault version
1914 len
= strlen (name
);
1915 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1916 if (shortname
== NULL
)
1918 memcpy (shortname
, name
, shortlen
);
1919 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1921 /* Once again, merge with any existing symbol. */
1922 type_change_ok
= FALSE
;
1923 size_change_ok
= FALSE
;
1925 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1926 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1927 &type_change_ok
, &size_change_ok
, &matched
))
1935 /* Here SHORTNAME is a versioned name, so we don't expect to see
1936 the type of override we do in the case above unless it is
1937 overridden by a versioned definition. */
1938 if (hi
->root
.type
!= bfd_link_hash_defined
1939 && hi
->root
.type
!= bfd_link_hash_defweak
)
1940 (*_bfd_error_handler
)
1941 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1947 if (! (_bfd_generic_link_add_one_symbol
1948 (info
, abfd
, shortname
, BSF_INDIRECT
,
1949 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1951 hi
= (struct elf_link_hash_entry
*) bh
;
1953 /* If there is a duplicate definition somewhere, then HI may not
1954 point to an indirect symbol. We will have reported an error
1955 to the user in that case. */
1957 if (hi
->root
.type
== bfd_link_hash_indirect
)
1959 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1960 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1961 hi
->dynamic_def
|= h
->dynamic_def
;
1963 /* See if the new flags lead us to realize that the symbol
1969 if (! bfd_link_executable (info
)
1975 if (hi
->ref_regular
)
1985 /* This routine is used to export all defined symbols into the dynamic
1986 symbol table. It is called via elf_link_hash_traverse. */
1989 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1991 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1993 /* Ignore indirect symbols. These are added by the versioning code. */
1994 if (h
->root
.type
== bfd_link_hash_indirect
)
1997 /* Ignore this if we won't export it. */
1998 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2001 if (h
->dynindx
== -1
2002 && (h
->def_regular
|| h
->ref_regular
)
2003 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2004 h
->root
.root
.string
))
2006 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2016 /* Look through the symbols which are defined in other shared
2017 libraries and referenced here. Update the list of version
2018 dependencies. This will be put into the .gnu.version_r section.
2019 This function is called via elf_link_hash_traverse. */
2022 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2025 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2026 Elf_Internal_Verneed
*t
;
2027 Elf_Internal_Vernaux
*a
;
2030 /* We only care about symbols defined in shared objects with version
2035 || h
->verinfo
.verdef
== NULL
2036 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2037 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2040 /* See if we already know about this version. */
2041 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2045 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2048 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2049 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2055 /* This is a new version. Add it to tree we are building. */
2060 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2063 rinfo
->failed
= TRUE
;
2067 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2068 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2069 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2073 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2076 rinfo
->failed
= TRUE
;
2080 /* Note that we are copying a string pointer here, and testing it
2081 above. If bfd_elf_string_from_elf_section is ever changed to
2082 discard the string data when low in memory, this will have to be
2084 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2086 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2087 a
->vna_nextptr
= t
->vn_auxptr
;
2089 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2092 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2099 /* Figure out appropriate versions for all the symbols. We may not
2100 have the version number script until we have read all of the input
2101 files, so until that point we don't know which symbols should be
2102 local. This function is called via elf_link_hash_traverse. */
2105 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2107 struct elf_info_failed
*sinfo
;
2108 struct bfd_link_info
*info
;
2109 const struct elf_backend_data
*bed
;
2110 struct elf_info_failed eif
;
2113 sinfo
= (struct elf_info_failed
*) data
;
2116 /* Fix the symbol flags. */
2119 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2122 sinfo
->failed
= TRUE
;
2126 /* We only need version numbers for symbols defined in regular
2128 if (!h
->def_regular
)
2131 bed
= get_elf_backend_data (info
->output_bfd
);
2132 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2133 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2135 struct bfd_elf_version_tree
*t
;
2138 if (*p
== ELF_VER_CHR
)
2141 /* If there is no version string, we can just return out. */
2145 /* Look for the version. If we find it, it is no longer weak. */
2146 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2148 if (strcmp (t
->name
, p
) == 0)
2152 struct bfd_elf_version_expr
*d
;
2154 len
= p
- h
->root
.root
.string
;
2155 alc
= (char *) bfd_malloc (len
);
2158 sinfo
->failed
= TRUE
;
2161 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2162 alc
[len
- 1] = '\0';
2163 if (alc
[len
- 2] == ELF_VER_CHR
)
2164 alc
[len
- 2] = '\0';
2166 h
->verinfo
.vertree
= t
;
2170 if (t
->globals
.list
!= NULL
)
2171 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2173 /* See if there is anything to force this symbol to
2175 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2177 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2180 && ! info
->export_dynamic
)
2181 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2189 /* If we are building an application, we need to create a
2190 version node for this version. */
2191 if (t
== NULL
&& bfd_link_executable (info
))
2193 struct bfd_elf_version_tree
**pp
;
2196 /* If we aren't going to export this symbol, we don't need
2197 to worry about it. */
2198 if (h
->dynindx
== -1)
2201 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2205 sinfo
->failed
= TRUE
;
2210 t
->name_indx
= (unsigned int) -1;
2214 /* Don't count anonymous version tag. */
2215 if (sinfo
->info
->version_info
!= NULL
2216 && sinfo
->info
->version_info
->vernum
== 0)
2218 for (pp
= &sinfo
->info
->version_info
;
2222 t
->vernum
= version_index
;
2226 h
->verinfo
.vertree
= t
;
2230 /* We could not find the version for a symbol when
2231 generating a shared archive. Return an error. */
2232 (*_bfd_error_handler
)
2233 (_("%B: version node not found for symbol %s"),
2234 info
->output_bfd
, h
->root
.root
.string
);
2235 bfd_set_error (bfd_error_bad_value
);
2236 sinfo
->failed
= TRUE
;
2241 /* If we don't have a version for this symbol, see if we can find
2243 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2248 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2249 h
->root
.root
.string
, &hide
);
2250 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2251 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2257 /* Read and swap the relocs from the section indicated by SHDR. This
2258 may be either a REL or a RELA section. The relocations are
2259 translated into RELA relocations and stored in INTERNAL_RELOCS,
2260 which should have already been allocated to contain enough space.
2261 The EXTERNAL_RELOCS are a buffer where the external form of the
2262 relocations should be stored.
2264 Returns FALSE if something goes wrong. */
2267 elf_link_read_relocs_from_section (bfd
*abfd
,
2269 Elf_Internal_Shdr
*shdr
,
2270 void *external_relocs
,
2271 Elf_Internal_Rela
*internal_relocs
)
2273 const struct elf_backend_data
*bed
;
2274 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2275 const bfd_byte
*erela
;
2276 const bfd_byte
*erelaend
;
2277 Elf_Internal_Rela
*irela
;
2278 Elf_Internal_Shdr
*symtab_hdr
;
2281 /* Position ourselves at the start of the section. */
2282 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2285 /* Read the relocations. */
2286 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2289 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2290 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2292 bed
= get_elf_backend_data (abfd
);
2294 /* Convert the external relocations to the internal format. */
2295 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2296 swap_in
= bed
->s
->swap_reloc_in
;
2297 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2298 swap_in
= bed
->s
->swap_reloca_in
;
2301 bfd_set_error (bfd_error_wrong_format
);
2305 erela
= (const bfd_byte
*) external_relocs
;
2306 erelaend
= erela
+ shdr
->sh_size
;
2307 irela
= internal_relocs
;
2308 while (erela
< erelaend
)
2312 (*swap_in
) (abfd
, erela
, irela
);
2313 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2314 if (bed
->s
->arch_size
== 64)
2318 if ((size_t) r_symndx
>= nsyms
)
2320 (*_bfd_error_handler
)
2321 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2322 " for offset 0x%lx in section `%A'"),
2324 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2325 bfd_set_error (bfd_error_bad_value
);
2329 else if (r_symndx
!= STN_UNDEF
)
2331 (*_bfd_error_handler
)
2332 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2333 " when the object file has no symbol table"),
2335 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2336 bfd_set_error (bfd_error_bad_value
);
2339 irela
+= bed
->s
->int_rels_per_ext_rel
;
2340 erela
+= shdr
->sh_entsize
;
2346 /* Read and swap the relocs for a section O. They may have been
2347 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2348 not NULL, they are used as buffers to read into. They are known to
2349 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2350 the return value is allocated using either malloc or bfd_alloc,
2351 according to the KEEP_MEMORY argument. If O has two relocation
2352 sections (both REL and RELA relocations), then the REL_HDR
2353 relocations will appear first in INTERNAL_RELOCS, followed by the
2354 RELA_HDR relocations. */
2357 _bfd_elf_link_read_relocs (bfd
*abfd
,
2359 void *external_relocs
,
2360 Elf_Internal_Rela
*internal_relocs
,
2361 bfd_boolean keep_memory
)
2363 void *alloc1
= NULL
;
2364 Elf_Internal_Rela
*alloc2
= NULL
;
2365 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2366 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2367 Elf_Internal_Rela
*internal_rela_relocs
;
2369 if (esdo
->relocs
!= NULL
)
2370 return esdo
->relocs
;
2372 if (o
->reloc_count
== 0)
2375 if (internal_relocs
== NULL
)
2379 size
= o
->reloc_count
;
2380 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2382 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2384 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2385 if (internal_relocs
== NULL
)
2389 if (external_relocs
== NULL
)
2391 bfd_size_type size
= 0;
2394 size
+= esdo
->rel
.hdr
->sh_size
;
2396 size
+= esdo
->rela
.hdr
->sh_size
;
2398 alloc1
= bfd_malloc (size
);
2401 external_relocs
= alloc1
;
2404 internal_rela_relocs
= internal_relocs
;
2407 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2411 external_relocs
= (((bfd_byte
*) external_relocs
)
2412 + esdo
->rel
.hdr
->sh_size
);
2413 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2414 * bed
->s
->int_rels_per_ext_rel
);
2418 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2420 internal_rela_relocs
)))
2423 /* Cache the results for next time, if we can. */
2425 esdo
->relocs
= internal_relocs
;
2430 /* Don't free alloc2, since if it was allocated we are passing it
2431 back (under the name of internal_relocs). */
2433 return internal_relocs
;
2441 bfd_release (abfd
, alloc2
);
2448 /* Compute the size of, and allocate space for, REL_HDR which is the
2449 section header for a section containing relocations for O. */
2452 _bfd_elf_link_size_reloc_section (bfd
*abfd
, struct bfd_link_info
*info
,
2453 asection
*o
, bfd_boolean rela
)
2455 struct bfd_elf_section_data
*esdo
;
2456 const struct elf_backend_data
*bed
;
2457 struct bfd_elf_section_reloc_data
*reldata
;
2458 Elf_Internal_Shdr
*rel_hdr
;
2461 esdo
= elf_section_data (o
);
2462 reldata
= rela
? &esdo
->rela
: &esdo
->rel
;
2463 rel_hdr
= reldata
->hdr
;
2465 /* That allows us to calculate the size of the section. */
2466 bed
= get_elf_backend_data (abfd
);
2467 count
= (*bed
->elf_backend_count_output_relocs
) (info
, o
, rela
);
2468 rel_hdr
->sh_size
= count
* rel_hdr
->sh_entsize
;
2470 /* The contents field must last into write_object_contents, so we
2471 allocate it with bfd_alloc rather than malloc. Also since we
2472 cannot be sure that the contents will actually be filled in,
2473 we zero the allocated space. */
2474 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2475 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2478 if (reldata
->hashes
== NULL
&& reldata
->count
)
2480 struct elf_link_hash_entry
**p
;
2482 p
= ((struct elf_link_hash_entry
**)
2483 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2487 reldata
->hashes
= p
;
2493 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2494 originated from the section given by INPUT_REL_HDR) to the
2498 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2499 asection
*input_section
,
2500 Elf_Internal_Shdr
*input_rel_hdr
,
2501 Elf_Internal_Rela
*internal_relocs
,
2502 struct elf_link_hash_entry
**rel_hash
2505 Elf_Internal_Rela
*irela
;
2506 Elf_Internal_Rela
*irelaend
;
2508 struct bfd_elf_section_reloc_data
*output_reldata
;
2509 asection
*output_section
;
2510 const struct elf_backend_data
*bed
;
2511 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2512 struct bfd_elf_section_data
*esdo
;
2514 output_section
= input_section
->output_section
;
2516 bed
= get_elf_backend_data (output_bfd
);
2517 esdo
= elf_section_data (output_section
);
2518 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2520 output_reldata
= &esdo
->rel
;
2521 swap_out
= bed
->s
->swap_reloc_out
;
2523 else if (esdo
->rela
.hdr
2524 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2526 output_reldata
= &esdo
->rela
;
2527 swap_out
= bed
->s
->swap_reloca_out
;
2531 (*_bfd_error_handler
)
2532 (_("%B: relocation size mismatch in %B section %A"),
2533 output_bfd
, input_section
->owner
, input_section
);
2534 bfd_set_error (bfd_error_wrong_format
);
2538 erel
= output_reldata
->hdr
->contents
;
2539 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2540 irela
= internal_relocs
;
2541 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2542 * bed
->s
->int_rels_per_ext_rel
);
2543 while (irela
< irelaend
)
2545 (*swap_out
) (output_bfd
, irela
, erel
);
2546 irela
+= bed
->s
->int_rels_per_ext_rel
;
2547 erel
+= input_rel_hdr
->sh_entsize
;
2550 /* Bump the counter, so that we know where to add the next set of
2552 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2558 _bfd_elf_default_count_output_relocs (struct bfd_link_info
* info ATTRIBUTE_UNUSED
,
2562 struct bfd_elf_section_data
*esdo
;
2563 struct bfd_elf_section_reloc_data
*reldata
;
2565 esdo
= elf_section_data (o
);
2566 reldata
= rela
? &esdo
->rela
: &esdo
->rel
;
2568 return reldata
->count
;
2571 /* Make weak undefined symbols in PIE dynamic. */
2574 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2575 struct elf_link_hash_entry
*h
)
2577 if (bfd_link_pie (info
)
2579 && h
->root
.type
== bfd_link_hash_undefweak
)
2580 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2585 /* Fix up the flags for a symbol. This handles various cases which
2586 can only be fixed after all the input files are seen. This is
2587 currently called by both adjust_dynamic_symbol and
2588 assign_sym_version, which is unnecessary but perhaps more robust in
2589 the face of future changes. */
2592 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2593 struct elf_info_failed
*eif
)
2595 const struct elf_backend_data
*bed
;
2597 /* If this symbol was mentioned in a non-ELF file, try to set
2598 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2599 permit a non-ELF file to correctly refer to a symbol defined in
2600 an ELF dynamic object. */
2603 while (h
->root
.type
== bfd_link_hash_indirect
)
2604 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2606 if (h
->root
.type
!= bfd_link_hash_defined
2607 && h
->root
.type
!= bfd_link_hash_defweak
)
2610 h
->ref_regular_nonweak
= 1;
2614 if (h
->root
.u
.def
.section
->owner
!= NULL
2615 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2616 == bfd_target_elf_flavour
))
2619 h
->ref_regular_nonweak
= 1;
2625 if (h
->dynindx
== -1
2629 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2638 /* Unfortunately, NON_ELF is only correct if the symbol
2639 was first seen in a non-ELF file. Fortunately, if the symbol
2640 was first seen in an ELF file, we're probably OK unless the
2641 symbol was defined in a non-ELF file. Catch that case here.
2642 FIXME: We're still in trouble if the symbol was first seen in
2643 a dynamic object, and then later in a non-ELF regular object. */
2644 if ((h
->root
.type
== bfd_link_hash_defined
2645 || h
->root
.type
== bfd_link_hash_defweak
)
2647 && (h
->root
.u
.def
.section
->owner
!= NULL
2648 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2649 != bfd_target_elf_flavour
)
2650 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2651 && !h
->def_dynamic
)))
2655 /* Backend specific symbol fixup. */
2656 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2657 if (bed
->elf_backend_fixup_symbol
2658 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2661 /* If this is a final link, and the symbol was defined as a common
2662 symbol in a regular object file, and there was no definition in
2663 any dynamic object, then the linker will have allocated space for
2664 the symbol in a common section but the DEF_REGULAR
2665 flag will not have been set. */
2666 if (h
->root
.type
== bfd_link_hash_defined
2670 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2673 /* If -Bsymbolic was used (which means to bind references to global
2674 symbols to the definition within the shared object), and this
2675 symbol was defined in a regular object, then it actually doesn't
2676 need a PLT entry. Likewise, if the symbol has non-default
2677 visibility. If the symbol has hidden or internal visibility, we
2678 will force it local. */
2680 && bfd_link_pic (eif
->info
)
2681 && is_elf_hash_table (eif
->info
->hash
)
2682 && (SYMBOLIC_BIND (eif
->info
, h
)
2683 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2686 bfd_boolean force_local
;
2688 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2689 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2690 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2693 /* If a weak undefined symbol has non-default visibility, we also
2694 hide it from the dynamic linker. */
2695 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2696 && h
->root
.type
== bfd_link_hash_undefweak
)
2697 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2699 /* 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
2827 (*_bfd_error_handler
)
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 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3694 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3696 /* As a GNU extension, any input sections which are named
3697 .gnu.warning.SYMBOL are treated as warning symbols for the given
3698 symbol. This differs from .gnu.warning sections, which generate
3699 warnings when they are included in an output file. */
3700 /* PR 12761: Also generate this warning when building shared libraries. */
3701 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3705 name
= bfd_get_section_name (abfd
, s
);
3706 if (CONST_STRNEQ (name
, ".gnu.warning."))
3711 name
+= sizeof ".gnu.warning." - 1;
3713 /* If this is a shared object, then look up the symbol
3714 in the hash table. If it is there, and it is already
3715 been defined, then we will not be using the entry
3716 from this shared object, so we don't need to warn.
3717 FIXME: If we see the definition in a regular object
3718 later on, we will warn, but we shouldn't. The only
3719 fix is to keep track of what warnings we are supposed
3720 to emit, and then handle them all at the end of the
3724 struct elf_link_hash_entry
*h
;
3726 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3728 /* FIXME: What about bfd_link_hash_common? */
3730 && (h
->root
.type
== bfd_link_hash_defined
3731 || h
->root
.type
== bfd_link_hash_defweak
))
3736 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3740 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3745 if (! (_bfd_generic_link_add_one_symbol
3746 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3747 FALSE
, bed
->collect
, NULL
)))
3750 if (bfd_link_executable (info
))
3752 /* Clobber the section size so that the warning does
3753 not get copied into the output file. */
3756 /* Also set SEC_EXCLUDE, so that symbols defined in
3757 the warning section don't get copied to the output. */
3758 s
->flags
|= SEC_EXCLUDE
;
3763 just_syms
= ((s
= abfd
->sections
) != NULL
3764 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3769 /* If we are creating a shared library, create all the dynamic
3770 sections immediately. We need to attach them to something,
3771 so we attach them to this BFD, provided it is the right
3772 format and is not from ld --just-symbols. Always create the
3773 dynamic sections for -E/--dynamic-list. FIXME: If there
3774 are no input BFD's of the same format as the output, we can't
3775 make a shared library. */
3777 && (bfd_link_pic (info
)
3778 || (!bfd_link_relocatable (info
)
3779 && (info
->export_dynamic
|| info
->dynamic
)))
3780 && is_elf_hash_table (htab
)
3781 && info
->output_bfd
->xvec
== abfd
->xvec
3782 && !htab
->dynamic_sections_created
)
3784 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3788 else if (!is_elf_hash_table (htab
))
3792 const char *soname
= NULL
;
3794 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3797 /* ld --just-symbols and dynamic objects don't mix very well.
3798 ld shouldn't allow it. */
3802 /* If this dynamic lib was specified on the command line with
3803 --as-needed in effect, then we don't want to add a DT_NEEDED
3804 tag unless the lib is actually used. Similary for libs brought
3805 in by another lib's DT_NEEDED. When --no-add-needed is used
3806 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3807 any dynamic library in DT_NEEDED tags in the dynamic lib at
3809 add_needed
= (elf_dyn_lib_class (abfd
)
3810 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3811 | DYN_NO_NEEDED
)) == 0;
3813 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3818 unsigned int elfsec
;
3819 unsigned long shlink
;
3821 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3828 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3829 if (elfsec
== SHN_BAD
)
3830 goto error_free_dyn
;
3831 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3833 for (extdyn
= dynbuf
;
3834 extdyn
< dynbuf
+ s
->size
;
3835 extdyn
+= bed
->s
->sizeof_dyn
)
3837 Elf_Internal_Dyn dyn
;
3839 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3840 if (dyn
.d_tag
== DT_SONAME
)
3842 unsigned int tagv
= dyn
.d_un
.d_val
;
3843 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3845 goto error_free_dyn
;
3847 if (dyn
.d_tag
== DT_NEEDED
)
3849 struct bfd_link_needed_list
*n
, **pn
;
3851 unsigned int tagv
= dyn
.d_un
.d_val
;
3853 amt
= sizeof (struct bfd_link_needed_list
);
3854 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3855 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3856 if (n
== NULL
|| fnm
== NULL
)
3857 goto error_free_dyn
;
3858 amt
= strlen (fnm
) + 1;
3859 anm
= (char *) bfd_alloc (abfd
, amt
);
3861 goto error_free_dyn
;
3862 memcpy (anm
, fnm
, amt
);
3866 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3870 if (dyn
.d_tag
== DT_RUNPATH
)
3872 struct bfd_link_needed_list
*n
, **pn
;
3874 unsigned int tagv
= dyn
.d_un
.d_val
;
3876 amt
= sizeof (struct bfd_link_needed_list
);
3877 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3878 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3879 if (n
== NULL
|| fnm
== NULL
)
3880 goto error_free_dyn
;
3881 amt
= strlen (fnm
) + 1;
3882 anm
= (char *) bfd_alloc (abfd
, amt
);
3884 goto error_free_dyn
;
3885 memcpy (anm
, fnm
, amt
);
3889 for (pn
= & runpath
;
3895 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3896 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3898 struct bfd_link_needed_list
*n
, **pn
;
3900 unsigned int tagv
= dyn
.d_un
.d_val
;
3902 amt
= sizeof (struct bfd_link_needed_list
);
3903 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3904 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3905 if (n
== NULL
|| fnm
== NULL
)
3906 goto error_free_dyn
;
3907 amt
= strlen (fnm
) + 1;
3908 anm
= (char *) bfd_alloc (abfd
, amt
);
3910 goto error_free_dyn
;
3911 memcpy (anm
, fnm
, amt
);
3921 if (dyn
.d_tag
== DT_AUDIT
)
3923 unsigned int tagv
= dyn
.d_un
.d_val
;
3924 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3931 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3932 frees all more recently bfd_alloc'd blocks as well. */
3938 struct bfd_link_needed_list
**pn
;
3939 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3944 /* We do not want to include any of the sections in a dynamic
3945 object in the output file. We hack by simply clobbering the
3946 list of sections in the BFD. This could be handled more
3947 cleanly by, say, a new section flag; the existing
3948 SEC_NEVER_LOAD flag is not the one we want, because that one
3949 still implies that the section takes up space in the output
3951 bfd_section_list_clear (abfd
);
3953 /* Find the name to use in a DT_NEEDED entry that refers to this
3954 object. If the object has a DT_SONAME entry, we use it.
3955 Otherwise, if the generic linker stuck something in
3956 elf_dt_name, we use that. Otherwise, we just use the file
3958 if (soname
== NULL
|| *soname
== '\0')
3960 soname
= elf_dt_name (abfd
);
3961 if (soname
== NULL
|| *soname
== '\0')
3962 soname
= bfd_get_filename (abfd
);
3965 /* Save the SONAME because sometimes the linker emulation code
3966 will need to know it. */
3967 elf_dt_name (abfd
) = soname
;
3969 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3973 /* If we have already included this dynamic object in the
3974 link, just ignore it. There is no reason to include a
3975 particular dynamic object more than once. */
3979 /* Save the DT_AUDIT entry for the linker emulation code. */
3980 elf_dt_audit (abfd
) = audit
;
3983 /* If this is a dynamic object, we always link against the .dynsym
3984 symbol table, not the .symtab symbol table. The dynamic linker
3985 will only see the .dynsym symbol table, so there is no reason to
3986 look at .symtab for a dynamic object. */
3988 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3989 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3991 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3993 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3995 /* The sh_info field of the symtab header tells us where the
3996 external symbols start. We don't care about the local symbols at
3998 if (elf_bad_symtab (abfd
))
4000 extsymcount
= symcount
;
4005 extsymcount
= symcount
- hdr
->sh_info
;
4006 extsymoff
= hdr
->sh_info
;
4009 sym_hash
= elf_sym_hashes (abfd
);
4010 if (extsymcount
!= 0)
4012 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4014 if (isymbuf
== NULL
)
4017 if (sym_hash
== NULL
)
4019 /* We store a pointer to the hash table entry for each
4022 amt
*= sizeof (struct elf_link_hash_entry
*);
4023 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4024 if (sym_hash
== NULL
)
4025 goto error_free_sym
;
4026 elf_sym_hashes (abfd
) = sym_hash
;
4032 /* Read in any version definitions. */
4033 if (!_bfd_elf_slurp_version_tables (abfd
,
4034 info
->default_imported_symver
))
4035 goto error_free_sym
;
4037 /* Read in the symbol versions, but don't bother to convert them
4038 to internal format. */
4039 if (elf_dynversym (abfd
) != 0)
4041 Elf_Internal_Shdr
*versymhdr
;
4043 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4044 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4045 if (extversym
== NULL
)
4046 goto error_free_sym
;
4047 amt
= versymhdr
->sh_size
;
4048 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4049 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4050 goto error_free_vers
;
4054 /* If we are loading an as-needed shared lib, save the symbol table
4055 state before we start adding symbols. If the lib turns out
4056 to be unneeded, restore the state. */
4057 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4062 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4064 struct bfd_hash_entry
*p
;
4065 struct elf_link_hash_entry
*h
;
4067 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4069 h
= (struct elf_link_hash_entry
*) p
;
4070 entsize
+= htab
->root
.table
.entsize
;
4071 if (h
->root
.type
== bfd_link_hash_warning
)
4072 entsize
+= htab
->root
.table
.entsize
;
4076 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4077 old_tab
= bfd_malloc (tabsize
+ entsize
);
4078 if (old_tab
== NULL
)
4079 goto error_free_vers
;
4081 /* Remember the current objalloc pointer, so that all mem for
4082 symbols added can later be reclaimed. */
4083 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4084 if (alloc_mark
== NULL
)
4085 goto error_free_vers
;
4087 /* Make a special call to the linker "notice" function to
4088 tell it that we are about to handle an as-needed lib. */
4089 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4090 goto error_free_vers
;
4092 /* Clone the symbol table. Remember some pointers into the
4093 symbol table, and dynamic symbol count. */
4094 old_ent
= (char *) old_tab
+ tabsize
;
4095 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4096 old_undefs
= htab
->root
.undefs
;
4097 old_undefs_tail
= htab
->root
.undefs_tail
;
4098 old_table
= htab
->root
.table
.table
;
4099 old_size
= htab
->root
.table
.size
;
4100 old_count
= htab
->root
.table
.count
;
4101 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4102 if (old_strtab
== NULL
)
4103 goto error_free_vers
;
4105 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4107 struct bfd_hash_entry
*p
;
4108 struct elf_link_hash_entry
*h
;
4110 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4112 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4113 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4114 h
= (struct elf_link_hash_entry
*) p
;
4115 if (h
->root
.type
== bfd_link_hash_warning
)
4117 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4118 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4125 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4126 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4128 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4132 asection
*sec
, *new_sec
;
4135 struct elf_link_hash_entry
*h
;
4136 struct elf_link_hash_entry
*hi
;
4137 bfd_boolean definition
;
4138 bfd_boolean size_change_ok
;
4139 bfd_boolean type_change_ok
;
4140 bfd_boolean new_weakdef
;
4141 bfd_boolean new_weak
;
4142 bfd_boolean old_weak
;
4143 bfd_boolean override
;
4145 bfd_boolean discarded
;
4146 unsigned int old_alignment
;
4148 bfd_boolean matched
;
4152 flags
= BSF_NO_FLAGS
;
4154 value
= isym
->st_value
;
4155 common
= bed
->common_definition (isym
);
4158 bind
= ELF_ST_BIND (isym
->st_info
);
4162 /* This should be impossible, since ELF requires that all
4163 global symbols follow all local symbols, and that sh_info
4164 point to the first global symbol. Unfortunately, Irix 5
4169 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4177 case STB_GNU_UNIQUE
:
4178 flags
= BSF_GNU_UNIQUE
;
4182 /* Leave it up to the processor backend. */
4186 if (isym
->st_shndx
== SHN_UNDEF
)
4187 sec
= bfd_und_section_ptr
;
4188 else if (isym
->st_shndx
== SHN_ABS
)
4189 sec
= bfd_abs_section_ptr
;
4190 else if (isym
->st_shndx
== SHN_COMMON
)
4192 sec
= bfd_com_section_ptr
;
4193 /* What ELF calls the size we call the value. What ELF
4194 calls the value we call the alignment. */
4195 value
= isym
->st_size
;
4199 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4201 sec
= bfd_abs_section_ptr
;
4202 else if (discarded_section (sec
))
4204 /* Symbols from discarded section are undefined. We keep
4206 sec
= bfd_und_section_ptr
;
4208 isym
->st_shndx
= SHN_UNDEF
;
4210 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4214 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4217 goto error_free_vers
;
4219 if (isym
->st_shndx
== SHN_COMMON
4220 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4222 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4226 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4228 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4230 goto error_free_vers
;
4234 else if (isym
->st_shndx
== SHN_COMMON
4235 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4236 && !bfd_link_relocatable (info
))
4238 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4242 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4243 | SEC_LINKER_CREATED
);
4244 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4246 goto error_free_vers
;
4250 else if (bed
->elf_add_symbol_hook
)
4252 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4254 goto error_free_vers
;
4256 /* The hook function sets the name to NULL if this symbol
4257 should be skipped for some reason. */
4262 /* Sanity check that all possibilities were handled. */
4265 bfd_set_error (bfd_error_bad_value
);
4266 goto error_free_vers
;
4269 /* Silently discard TLS symbols from --just-syms. There's
4270 no way to combine a static TLS block with a new TLS block
4271 for this executable. */
4272 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4273 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4276 if (bfd_is_und_section (sec
)
4277 || bfd_is_com_section (sec
))
4282 size_change_ok
= FALSE
;
4283 type_change_ok
= bed
->type_change_ok
;
4290 if (is_elf_hash_table (htab
))
4292 Elf_Internal_Versym iver
;
4293 unsigned int vernum
= 0;
4298 if (info
->default_imported_symver
)
4299 /* Use the default symbol version created earlier. */
4300 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4305 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4307 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4309 /* If this is a hidden symbol, or if it is not version
4310 1, we append the version name to the symbol name.
4311 However, we do not modify a non-hidden absolute symbol
4312 if it is not a function, because it might be the version
4313 symbol itself. FIXME: What if it isn't? */
4314 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4316 && (!bfd_is_abs_section (sec
)
4317 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4320 size_t namelen
, verlen
, newlen
;
4323 if (isym
->st_shndx
!= SHN_UNDEF
)
4325 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4327 else if (vernum
> 1)
4329 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4335 (*_bfd_error_handler
)
4336 (_("%B: %s: invalid version %u (max %d)"),
4338 elf_tdata (abfd
)->cverdefs
);
4339 bfd_set_error (bfd_error_bad_value
);
4340 goto error_free_vers
;
4345 /* We cannot simply test for the number of
4346 entries in the VERNEED section since the
4347 numbers for the needed versions do not start
4349 Elf_Internal_Verneed
*t
;
4352 for (t
= elf_tdata (abfd
)->verref
;
4356 Elf_Internal_Vernaux
*a
;
4358 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4360 if (a
->vna_other
== vernum
)
4362 verstr
= a
->vna_nodename
;
4371 (*_bfd_error_handler
)
4372 (_("%B: %s: invalid needed version %d"),
4373 abfd
, name
, vernum
);
4374 bfd_set_error (bfd_error_bad_value
);
4375 goto error_free_vers
;
4379 namelen
= strlen (name
);
4380 verlen
= strlen (verstr
);
4381 newlen
= namelen
+ verlen
+ 2;
4382 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4383 && isym
->st_shndx
!= SHN_UNDEF
)
4386 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4387 if (newname
== NULL
)
4388 goto error_free_vers
;
4389 memcpy (newname
, name
, namelen
);
4390 p
= newname
+ namelen
;
4392 /* If this is a defined non-hidden version symbol,
4393 we add another @ to the name. This indicates the
4394 default version of the symbol. */
4395 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4396 && isym
->st_shndx
!= SHN_UNDEF
)
4398 memcpy (p
, verstr
, verlen
+ 1);
4403 /* If this symbol has default visibility and the user has
4404 requested we not re-export it, then mark it as hidden. */
4405 if (!bfd_is_und_section (sec
)
4408 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4409 isym
->st_other
= (STV_HIDDEN
4410 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4412 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4413 sym_hash
, &old_bfd
, &old_weak
,
4414 &old_alignment
, &skip
, &override
,
4415 &type_change_ok
, &size_change_ok
,
4417 goto error_free_vers
;
4422 /* Override a definition only if the new symbol matches the
4424 if (override
&& matched
)
4428 while (h
->root
.type
== bfd_link_hash_indirect
4429 || h
->root
.type
== bfd_link_hash_warning
)
4430 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4432 if (elf_tdata (abfd
)->verdef
!= NULL
4435 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4438 if (! (_bfd_generic_link_add_one_symbol
4439 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4440 (struct bfd_link_hash_entry
**) sym_hash
)))
4441 goto error_free_vers
;
4443 if ((flags
& BSF_GNU_UNIQUE
)
4444 && (abfd
->flags
& DYNAMIC
) == 0
4445 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4446 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4449 /* We need to make sure that indirect symbol dynamic flags are
4452 while (h
->root
.type
== bfd_link_hash_indirect
4453 || h
->root
.type
== bfd_link_hash_warning
)
4454 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4456 /* Setting the index to -3 tells elf_link_output_extsym that
4457 this symbol is defined in a discarded section. */
4463 new_weak
= (flags
& BSF_WEAK
) != 0;
4464 new_weakdef
= FALSE
;
4468 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4469 && is_elf_hash_table (htab
)
4470 && h
->u
.weakdef
== NULL
)
4472 /* Keep a list of all weak defined non function symbols from
4473 a dynamic object, using the weakdef field. Later in this
4474 function we will set the weakdef field to the correct
4475 value. We only put non-function symbols from dynamic
4476 objects on this list, because that happens to be the only
4477 time we need to know the normal symbol corresponding to a
4478 weak symbol, and the information is time consuming to
4479 figure out. If the weakdef field is not already NULL,
4480 then this symbol was already defined by some previous
4481 dynamic object, and we will be using that previous
4482 definition anyhow. */
4484 h
->u
.weakdef
= weaks
;
4489 /* Set the alignment of a common symbol. */
4490 if ((common
|| bfd_is_com_section (sec
))
4491 && h
->root
.type
== bfd_link_hash_common
)
4496 align
= bfd_log2 (isym
->st_value
);
4499 /* The new symbol is a common symbol in a shared object.
4500 We need to get the alignment from the section. */
4501 align
= new_sec
->alignment_power
;
4503 if (align
> old_alignment
)
4504 h
->root
.u
.c
.p
->alignment_power
= align
;
4506 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4509 if (is_elf_hash_table (htab
))
4511 /* Set a flag in the hash table entry indicating the type of
4512 reference or definition we just found. A dynamic symbol
4513 is one which is referenced or defined by both a regular
4514 object and a shared object. */
4515 bfd_boolean dynsym
= FALSE
;
4517 /* Plugin symbols aren't normal. Don't set def_regular or
4518 ref_regular for them, or make them dynamic. */
4519 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4526 if (bind
!= STB_WEAK
)
4527 h
->ref_regular_nonweak
= 1;
4539 /* If the indirect symbol has been forced local, don't
4540 make the real symbol dynamic. */
4541 if ((h
== hi
|| !hi
->forced_local
)
4542 && (bfd_link_dll (info
)
4552 hi
->ref_dynamic
= 1;
4557 hi
->def_dynamic
= 1;
4560 /* If the indirect symbol has been forced local, don't
4561 make the real symbol dynamic. */
4562 if ((h
== hi
|| !hi
->forced_local
)
4565 || (h
->u
.weakdef
!= NULL
4567 && h
->u
.weakdef
->dynindx
!= -1)))
4571 /* Check to see if we need to add an indirect symbol for
4572 the default name. */
4574 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4575 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4576 sec
, value
, &old_bfd
, &dynsym
))
4577 goto error_free_vers
;
4579 /* Check the alignment when a common symbol is involved. This
4580 can change when a common symbol is overridden by a normal
4581 definition or a common symbol is ignored due to the old
4582 normal definition. We need to make sure the maximum
4583 alignment is maintained. */
4584 if ((old_alignment
|| common
)
4585 && h
->root
.type
!= bfd_link_hash_common
)
4587 unsigned int common_align
;
4588 unsigned int normal_align
;
4589 unsigned int symbol_align
;
4593 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4594 || h
->root
.type
== bfd_link_hash_defweak
);
4596 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4597 if (h
->root
.u
.def
.section
->owner
!= NULL
4598 && (h
->root
.u
.def
.section
->owner
->flags
4599 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4601 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4602 if (normal_align
> symbol_align
)
4603 normal_align
= symbol_align
;
4606 normal_align
= symbol_align
;
4610 common_align
= old_alignment
;
4611 common_bfd
= old_bfd
;
4616 common_align
= bfd_log2 (isym
->st_value
);
4618 normal_bfd
= old_bfd
;
4621 if (normal_align
< common_align
)
4623 /* PR binutils/2735 */
4624 if (normal_bfd
== NULL
)
4625 (*_bfd_error_handler
)
4626 (_("Warning: alignment %u of common symbol `%s' in %B is"
4627 " greater than the alignment (%u) of its section %A"),
4628 common_bfd
, h
->root
.u
.def
.section
,
4629 1 << common_align
, name
, 1 << normal_align
);
4631 (*_bfd_error_handler
)
4632 (_("Warning: alignment %u of symbol `%s' in %B"
4633 " is smaller than %u in %B"),
4634 normal_bfd
, common_bfd
,
4635 1 << normal_align
, name
, 1 << common_align
);
4639 /* Remember the symbol size if it isn't undefined. */
4640 if (isym
->st_size
!= 0
4641 && isym
->st_shndx
!= SHN_UNDEF
4642 && (definition
|| h
->size
== 0))
4645 && h
->size
!= isym
->st_size
4646 && ! size_change_ok
)
4647 (*_bfd_error_handler
)
4648 (_("Warning: size of symbol `%s' changed"
4649 " from %lu in %B to %lu in %B"),
4651 name
, (unsigned long) h
->size
,
4652 (unsigned long) isym
->st_size
);
4654 h
->size
= isym
->st_size
;
4657 /* If this is a common symbol, then we always want H->SIZE
4658 to be the size of the common symbol. The code just above
4659 won't fix the size if a common symbol becomes larger. We
4660 don't warn about a size change here, because that is
4661 covered by --warn-common. Allow changes between different
4663 if (h
->root
.type
== bfd_link_hash_common
)
4664 h
->size
= h
->root
.u
.c
.size
;
4666 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4667 && ((definition
&& !new_weak
)
4668 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4669 || h
->type
== STT_NOTYPE
))
4671 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4673 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4675 if (type
== STT_GNU_IFUNC
4676 && (abfd
->flags
& DYNAMIC
) != 0)
4679 if (h
->type
!= type
)
4681 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4682 (*_bfd_error_handler
)
4683 (_("Warning: type of symbol `%s' changed"
4684 " from %d to %d in %B"),
4685 abfd
, name
, h
->type
, type
);
4691 /* Merge st_other field. */
4692 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4694 /* We don't want to make debug symbol dynamic. */
4696 && (sec
->flags
& SEC_DEBUGGING
)
4697 && !bfd_link_relocatable (info
))
4700 /* Nor should we make plugin symbols dynamic. */
4701 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4706 h
->target_internal
= isym
->st_target_internal
;
4707 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4710 if (definition
&& !dynamic
)
4712 char *p
= strchr (name
, ELF_VER_CHR
);
4713 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4715 /* Queue non-default versions so that .symver x, x@FOO
4716 aliases can be checked. */
4719 amt
= ((isymend
- isym
+ 1)
4720 * sizeof (struct elf_link_hash_entry
*));
4722 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4724 goto error_free_vers
;
4726 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4730 if (dynsym
&& h
->dynindx
== -1)
4732 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4733 goto error_free_vers
;
4734 if (h
->u
.weakdef
!= NULL
4736 && h
->u
.weakdef
->dynindx
== -1)
4738 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4739 goto error_free_vers
;
4742 else if (h
->dynindx
!= -1)
4743 /* If the symbol already has a dynamic index, but
4744 visibility says it should not be visible, turn it into
4746 switch (ELF_ST_VISIBILITY (h
->other
))
4750 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4755 /* Don't add DT_NEEDED for references from the dummy bfd nor
4756 for unmatched symbol. */
4761 && h
->ref_regular_nonweak
4763 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4764 || (h
->ref_dynamic_nonweak
4765 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4766 && !on_needed_list (elf_dt_name (abfd
),
4767 htab
->needed
, NULL
))))
4770 const char *soname
= elf_dt_name (abfd
);
4772 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4773 h
->root
.root
.string
);
4775 /* A symbol from a library loaded via DT_NEEDED of some
4776 other library is referenced by a regular object.
4777 Add a DT_NEEDED entry for it. Issue an error if
4778 --no-add-needed is used and the reference was not
4781 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4783 (*_bfd_error_handler
)
4784 (_("%B: undefined reference to symbol '%s'"),
4786 bfd_set_error (bfd_error_missing_dso
);
4787 goto error_free_vers
;
4790 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4791 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4794 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4796 goto error_free_vers
;
4798 BFD_ASSERT (ret
== 0);
4803 if (extversym
!= NULL
)
4809 if (isymbuf
!= NULL
)
4815 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4819 /* Restore the symbol table. */
4820 old_ent
= (char *) old_tab
+ tabsize
;
4821 memset (elf_sym_hashes (abfd
), 0,
4822 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4823 htab
->root
.table
.table
= old_table
;
4824 htab
->root
.table
.size
= old_size
;
4825 htab
->root
.table
.count
= old_count
;
4826 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4827 htab
->root
.undefs
= old_undefs
;
4828 htab
->root
.undefs_tail
= old_undefs_tail
;
4829 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4832 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4834 struct bfd_hash_entry
*p
;
4835 struct elf_link_hash_entry
*h
;
4837 unsigned int alignment_power
;
4839 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4841 h
= (struct elf_link_hash_entry
*) p
;
4842 if (h
->root
.type
== bfd_link_hash_warning
)
4843 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4845 /* Preserve the maximum alignment and size for common
4846 symbols even if this dynamic lib isn't on DT_NEEDED
4847 since it can still be loaded at run time by another
4849 if (h
->root
.type
== bfd_link_hash_common
)
4851 size
= h
->root
.u
.c
.size
;
4852 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4857 alignment_power
= 0;
4859 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4860 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4861 h
= (struct elf_link_hash_entry
*) p
;
4862 if (h
->root
.type
== bfd_link_hash_warning
)
4864 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4865 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4866 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4868 if (h
->root
.type
== bfd_link_hash_common
)
4870 if (size
> h
->root
.u
.c
.size
)
4871 h
->root
.u
.c
.size
= size
;
4872 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4873 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4878 /* Make a special call to the linker "notice" function to
4879 tell it that symbols added for crefs may need to be removed. */
4880 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4881 goto error_free_vers
;
4884 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4886 if (nondeflt_vers
!= NULL
)
4887 free (nondeflt_vers
);
4891 if (old_tab
!= NULL
)
4893 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4894 goto error_free_vers
;
4899 /* Now that all the symbols from this input file are created, if
4900 not performing a relocatable link, handle .symver foo, foo@BAR
4901 such that any relocs against foo become foo@BAR. */
4902 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4906 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4908 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4909 char *shortname
, *p
;
4911 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4913 || (h
->root
.type
!= bfd_link_hash_defined
4914 && h
->root
.type
!= bfd_link_hash_defweak
))
4917 amt
= p
- h
->root
.root
.string
;
4918 shortname
= (char *) bfd_malloc (amt
+ 1);
4920 goto error_free_vers
;
4921 memcpy (shortname
, h
->root
.root
.string
, amt
);
4922 shortname
[amt
] = '\0';
4924 hi
= (struct elf_link_hash_entry
*)
4925 bfd_link_hash_lookup (&htab
->root
, shortname
,
4926 FALSE
, FALSE
, FALSE
);
4928 && hi
->root
.type
== h
->root
.type
4929 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4930 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4932 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4933 hi
->root
.type
= bfd_link_hash_indirect
;
4934 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4935 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4936 sym_hash
= elf_sym_hashes (abfd
);
4938 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4939 if (sym_hash
[symidx
] == hi
)
4941 sym_hash
[symidx
] = h
;
4947 free (nondeflt_vers
);
4948 nondeflt_vers
= NULL
;
4951 /* Now set the weakdefs field correctly for all the weak defined
4952 symbols we found. The only way to do this is to search all the
4953 symbols. Since we only need the information for non functions in
4954 dynamic objects, that's the only time we actually put anything on
4955 the list WEAKS. We need this information so that if a regular
4956 object refers to a symbol defined weakly in a dynamic object, the
4957 real symbol in the dynamic object is also put in the dynamic
4958 symbols; we also must arrange for both symbols to point to the
4959 same memory location. We could handle the general case of symbol
4960 aliasing, but a general symbol alias can only be generated in
4961 assembler code, handling it correctly would be very time
4962 consuming, and other ELF linkers don't handle general aliasing
4966 struct elf_link_hash_entry
**hpp
;
4967 struct elf_link_hash_entry
**hppend
;
4968 struct elf_link_hash_entry
**sorted_sym_hash
;
4969 struct elf_link_hash_entry
*h
;
4972 /* Since we have to search the whole symbol list for each weak
4973 defined symbol, search time for N weak defined symbols will be
4974 O(N^2). Binary search will cut it down to O(NlogN). */
4976 amt
*= sizeof (struct elf_link_hash_entry
*);
4977 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4978 if (sorted_sym_hash
== NULL
)
4980 sym_hash
= sorted_sym_hash
;
4981 hpp
= elf_sym_hashes (abfd
);
4982 hppend
= hpp
+ extsymcount
;
4984 for (; hpp
< hppend
; hpp
++)
4988 && h
->root
.type
== bfd_link_hash_defined
4989 && !bed
->is_function_type (h
->type
))
4997 qsort (sorted_sym_hash
, sym_count
,
4998 sizeof (struct elf_link_hash_entry
*),
5001 while (weaks
!= NULL
)
5003 struct elf_link_hash_entry
*hlook
;
5006 size_t i
, j
, idx
= 0;
5009 weaks
= hlook
->u
.weakdef
;
5010 hlook
->u
.weakdef
= NULL
;
5012 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5013 || hlook
->root
.type
== bfd_link_hash_defweak
5014 || hlook
->root
.type
== bfd_link_hash_common
5015 || hlook
->root
.type
== bfd_link_hash_indirect
);
5016 slook
= hlook
->root
.u
.def
.section
;
5017 vlook
= hlook
->root
.u
.def
.value
;
5023 bfd_signed_vma vdiff
;
5025 h
= sorted_sym_hash
[idx
];
5026 vdiff
= vlook
- h
->root
.u
.def
.value
;
5033 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5043 /* We didn't find a value/section match. */
5047 /* With multiple aliases, or when the weak symbol is already
5048 strongly defined, we have multiple matching symbols and
5049 the binary search above may land on any of them. Step
5050 one past the matching symbol(s). */
5053 h
= sorted_sym_hash
[idx
];
5054 if (h
->root
.u
.def
.section
!= slook
5055 || h
->root
.u
.def
.value
!= vlook
)
5059 /* Now look back over the aliases. Since we sorted by size
5060 as well as value and section, we'll choose the one with
5061 the largest size. */
5064 h
= sorted_sym_hash
[idx
];
5066 /* Stop if value or section doesn't match. */
5067 if (h
->root
.u
.def
.section
!= slook
5068 || h
->root
.u
.def
.value
!= vlook
)
5070 else if (h
!= hlook
)
5072 hlook
->u
.weakdef
= h
;
5074 /* If the weak definition is in the list of dynamic
5075 symbols, make sure the real definition is put
5077 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5079 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5082 free (sorted_sym_hash
);
5087 /* If the real definition is in the list of dynamic
5088 symbols, make sure the weak definition is put
5089 there as well. If we don't do this, then the
5090 dynamic loader might not merge the entries for the
5091 real definition and the weak definition. */
5092 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5094 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5095 goto err_free_sym_hash
;
5102 free (sorted_sym_hash
);
5105 if (bed
->check_directives
5106 && !(*bed
->check_directives
) (abfd
, info
))
5109 if (!info
->check_relocs_after_open_input
5110 && !_bfd_elf_link_check_relocs (abfd
, info
))
5113 /* If this is a non-traditional link, try to optimize the handling
5114 of the .stab/.stabstr sections. */
5116 && ! info
->traditional_format
5117 && is_elf_hash_table (htab
)
5118 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5122 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5123 if (stabstr
!= NULL
)
5125 bfd_size_type string_offset
= 0;
5128 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5129 if (CONST_STRNEQ (stab
->name
, ".stab")
5130 && (!stab
->name
[5] ||
5131 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5132 && (stab
->flags
& SEC_MERGE
) == 0
5133 && !bfd_is_abs_section (stab
->output_section
))
5135 struct bfd_elf_section_data
*secdata
;
5137 secdata
= elf_section_data (stab
);
5138 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5139 stabstr
, &secdata
->sec_info
,
5142 if (secdata
->sec_info
)
5143 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5148 if (is_elf_hash_table (htab
) && add_needed
)
5150 /* Add this bfd to the loaded list. */
5151 struct elf_link_loaded_list
*n
;
5153 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5157 n
->next
= htab
->loaded
;
5164 if (old_tab
!= NULL
)
5166 if (old_strtab
!= NULL
)
5168 if (nondeflt_vers
!= NULL
)
5169 free (nondeflt_vers
);
5170 if (extversym
!= NULL
)
5173 if (isymbuf
!= NULL
)
5179 /* Return the linker hash table entry of a symbol that might be
5180 satisfied by an archive symbol. Return -1 on error. */
5182 struct elf_link_hash_entry
*
5183 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5184 struct bfd_link_info
*info
,
5187 struct elf_link_hash_entry
*h
;
5191 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5195 /* If this is a default version (the name contains @@), look up the
5196 symbol again with only one `@' as well as without the version.
5197 The effect is that references to the symbol with and without the
5198 version will be matched by the default symbol in the archive. */
5200 p
= strchr (name
, ELF_VER_CHR
);
5201 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5204 /* First check with only one `@'. */
5205 len
= strlen (name
);
5206 copy
= (char *) bfd_alloc (abfd
, len
);
5208 return (struct elf_link_hash_entry
*) 0 - 1;
5210 first
= p
- name
+ 1;
5211 memcpy (copy
, name
, first
);
5212 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5214 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5217 /* We also need to check references to the symbol without the
5219 copy
[first
- 1] = '\0';
5220 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5221 FALSE
, FALSE
, TRUE
);
5224 bfd_release (abfd
, copy
);
5228 /* Add symbols from an ELF archive file to the linker hash table. We
5229 don't use _bfd_generic_link_add_archive_symbols because we need to
5230 handle versioned symbols.
5232 Fortunately, ELF archive handling is simpler than that done by
5233 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5234 oddities. In ELF, if we find a symbol in the archive map, and the
5235 symbol is currently undefined, we know that we must pull in that
5238 Unfortunately, we do have to make multiple passes over the symbol
5239 table until nothing further is resolved. */
5242 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5245 unsigned char *included
= NULL
;
5249 const struct elf_backend_data
*bed
;
5250 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5251 (bfd
*, struct bfd_link_info
*, const char *);
5253 if (! bfd_has_map (abfd
))
5255 /* An empty archive is a special case. */
5256 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5258 bfd_set_error (bfd_error_no_armap
);
5262 /* Keep track of all symbols we know to be already defined, and all
5263 files we know to be already included. This is to speed up the
5264 second and subsequent passes. */
5265 c
= bfd_ardata (abfd
)->symdef_count
;
5269 amt
*= sizeof (*included
);
5270 included
= (unsigned char *) bfd_zmalloc (amt
);
5271 if (included
== NULL
)
5274 symdefs
= bfd_ardata (abfd
)->symdefs
;
5275 bed
= get_elf_backend_data (abfd
);
5276 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5289 symdefend
= symdef
+ c
;
5290 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5292 struct elf_link_hash_entry
*h
;
5294 struct bfd_link_hash_entry
*undefs_tail
;
5299 if (symdef
->file_offset
== last
)
5305 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5306 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5312 if (h
->root
.type
== bfd_link_hash_common
)
5314 /* We currently have a common symbol. The archive map contains
5315 a reference to this symbol, so we may want to include it. We
5316 only want to include it however, if this archive element
5317 contains a definition of the symbol, not just another common
5320 Unfortunately some archivers (including GNU ar) will put
5321 declarations of common symbols into their archive maps, as
5322 well as real definitions, so we cannot just go by the archive
5323 map alone. Instead we must read in the element's symbol
5324 table and check that to see what kind of symbol definition
5326 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5329 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5331 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5332 /* Symbol must be defined. Don't check it again. */
5337 /* We need to include this archive member. */
5338 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5339 if (element
== NULL
)
5342 if (! bfd_check_format (element
, bfd_object
))
5345 undefs_tail
= info
->hash
->undefs_tail
;
5347 if (!(*info
->callbacks
5348 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5350 if (!bfd_link_add_symbols (element
, info
))
5353 /* If there are any new undefined symbols, we need to make
5354 another pass through the archive in order to see whether
5355 they can be defined. FIXME: This isn't perfect, because
5356 common symbols wind up on undefs_tail and because an
5357 undefined symbol which is defined later on in this pass
5358 does not require another pass. This isn't a bug, but it
5359 does make the code less efficient than it could be. */
5360 if (undefs_tail
!= info
->hash
->undefs_tail
)
5363 /* Look backward to mark all symbols from this object file
5364 which we have already seen in this pass. */
5368 included
[mark
] = TRUE
;
5373 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5375 /* We mark subsequent symbols from this object file as we go
5376 on through the loop. */
5377 last
= symdef
->file_offset
;
5387 if (included
!= NULL
)
5392 /* Given an ELF BFD, add symbols to the global hash table as
5396 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5398 switch (bfd_get_format (abfd
))
5401 return elf_link_add_object_symbols (abfd
, info
);
5403 return elf_link_add_archive_symbols (abfd
, info
);
5405 bfd_set_error (bfd_error_wrong_format
);
5410 struct hash_codes_info
5412 unsigned long *hashcodes
;
5416 /* This function will be called though elf_link_hash_traverse to store
5417 all hash value of the exported symbols in an array. */
5420 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5422 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5427 /* Ignore indirect symbols. These are added by the versioning code. */
5428 if (h
->dynindx
== -1)
5431 name
= h
->root
.root
.string
;
5432 if (h
->versioned
>= versioned
)
5434 char *p
= strchr (name
, ELF_VER_CHR
);
5437 alc
= (char *) bfd_malloc (p
- name
+ 1);
5443 memcpy (alc
, name
, p
- name
);
5444 alc
[p
- name
] = '\0';
5449 /* Compute the hash value. */
5450 ha
= bfd_elf_hash (name
);
5452 /* Store the found hash value in the array given as the argument. */
5453 *(inf
->hashcodes
)++ = ha
;
5455 /* And store it in the struct so that we can put it in the hash table
5457 h
->u
.elf_hash_value
= ha
;
5465 struct collect_gnu_hash_codes
5468 const struct elf_backend_data
*bed
;
5469 unsigned long int nsyms
;
5470 unsigned long int maskbits
;
5471 unsigned long int *hashcodes
;
5472 unsigned long int *hashval
;
5473 unsigned long int *indx
;
5474 unsigned long int *counts
;
5477 long int min_dynindx
;
5478 unsigned long int bucketcount
;
5479 unsigned long int symindx
;
5480 long int local_indx
;
5481 long int shift1
, shift2
;
5482 unsigned long int mask
;
5486 /* This function will be called though elf_link_hash_traverse to store
5487 all hash value of the exported symbols in an array. */
5490 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5492 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5497 /* Ignore indirect symbols. These are added by the versioning code. */
5498 if (h
->dynindx
== -1)
5501 /* Ignore also local symbols and undefined symbols. */
5502 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5505 name
= h
->root
.root
.string
;
5506 if (h
->versioned
>= versioned
)
5508 char *p
= strchr (name
, ELF_VER_CHR
);
5511 alc
= (char *) bfd_malloc (p
- name
+ 1);
5517 memcpy (alc
, name
, p
- name
);
5518 alc
[p
- name
] = '\0';
5523 /* Compute the hash value. */
5524 ha
= bfd_elf_gnu_hash (name
);
5526 /* Store the found hash value in the array for compute_bucket_count,
5527 and also for .dynsym reordering purposes. */
5528 s
->hashcodes
[s
->nsyms
] = ha
;
5529 s
->hashval
[h
->dynindx
] = ha
;
5531 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5532 s
->min_dynindx
= h
->dynindx
;
5540 /* This function will be called though elf_link_hash_traverse to do
5541 final dynaminc symbol renumbering. */
5544 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5546 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5547 unsigned long int bucket
;
5548 unsigned long int val
;
5550 /* Ignore indirect symbols. */
5551 if (h
->dynindx
== -1)
5554 /* Ignore also local symbols and undefined symbols. */
5555 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5557 if (h
->dynindx
>= s
->min_dynindx
)
5558 h
->dynindx
= s
->local_indx
++;
5562 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5563 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5564 & ((s
->maskbits
>> s
->shift1
) - 1);
5565 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5567 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5568 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5569 if (s
->counts
[bucket
] == 1)
5570 /* Last element terminates the chain. */
5572 bfd_put_32 (s
->output_bfd
, val
,
5573 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5574 --s
->counts
[bucket
];
5575 h
->dynindx
= s
->indx
[bucket
]++;
5579 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5582 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5584 return !(h
->forced_local
5585 || h
->root
.type
== bfd_link_hash_undefined
5586 || h
->root
.type
== bfd_link_hash_undefweak
5587 || ((h
->root
.type
== bfd_link_hash_defined
5588 || h
->root
.type
== bfd_link_hash_defweak
)
5589 && h
->root
.u
.def
.section
->output_section
== NULL
));
5592 /* Array used to determine the number of hash table buckets to use
5593 based on the number of symbols there are. If there are fewer than
5594 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5595 fewer than 37 we use 17 buckets, and so forth. We never use more
5596 than 32771 buckets. */
5598 static const size_t elf_buckets
[] =
5600 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5604 /* Compute bucket count for hashing table. We do not use a static set
5605 of possible tables sizes anymore. Instead we determine for all
5606 possible reasonable sizes of the table the outcome (i.e., the
5607 number of collisions etc) and choose the best solution. The
5608 weighting functions are not too simple to allow the table to grow
5609 without bounds. Instead one of the weighting factors is the size.
5610 Therefore the result is always a good payoff between few collisions
5611 (= short chain lengths) and table size. */
5613 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5614 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5615 unsigned long int nsyms
,
5618 size_t best_size
= 0;
5619 unsigned long int i
;
5621 /* We have a problem here. The following code to optimize the table
5622 size requires an integer type with more the 32 bits. If
5623 BFD_HOST_U_64_BIT is set we know about such a type. */
5624 #ifdef BFD_HOST_U_64_BIT
5629 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5630 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5631 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5632 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5633 unsigned long int *counts
;
5635 unsigned int no_improvement_count
= 0;
5637 /* Possible optimization parameters: if we have NSYMS symbols we say
5638 that the hashing table must at least have NSYMS/4 and at most
5640 minsize
= nsyms
/ 4;
5643 best_size
= maxsize
= nsyms
* 2;
5648 if ((best_size
& 31) == 0)
5652 /* Create array where we count the collisions in. We must use bfd_malloc
5653 since the size could be large. */
5655 amt
*= sizeof (unsigned long int);
5656 counts
= (unsigned long int *) bfd_malloc (amt
);
5660 /* Compute the "optimal" size for the hash table. The criteria is a
5661 minimal chain length. The minor criteria is (of course) the size
5663 for (i
= minsize
; i
< maxsize
; ++i
)
5665 /* Walk through the array of hashcodes and count the collisions. */
5666 BFD_HOST_U_64_BIT max
;
5667 unsigned long int j
;
5668 unsigned long int fact
;
5670 if (gnu_hash
&& (i
& 31) == 0)
5673 memset (counts
, '\0', i
* sizeof (unsigned long int));
5675 /* Determine how often each hash bucket is used. */
5676 for (j
= 0; j
< nsyms
; ++j
)
5677 ++counts
[hashcodes
[j
] % i
];
5679 /* For the weight function we need some information about the
5680 pagesize on the target. This is information need not be 100%
5681 accurate. Since this information is not available (so far) we
5682 define it here to a reasonable default value. If it is crucial
5683 to have a better value some day simply define this value. */
5684 # ifndef BFD_TARGET_PAGESIZE
5685 # define BFD_TARGET_PAGESIZE (4096)
5688 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5690 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5693 /* Variant 1: optimize for short chains. We add the squares
5694 of all the chain lengths (which favors many small chain
5695 over a few long chains). */
5696 for (j
= 0; j
< i
; ++j
)
5697 max
+= counts
[j
] * counts
[j
];
5699 /* This adds penalties for the overall size of the table. */
5700 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5703 /* Variant 2: Optimize a lot more for small table. Here we
5704 also add squares of the size but we also add penalties for
5705 empty slots (the +1 term). */
5706 for (j
= 0; j
< i
; ++j
)
5707 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5709 /* The overall size of the table is considered, but not as
5710 strong as in variant 1, where it is squared. */
5711 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5715 /* Compare with current best results. */
5716 if (max
< best_chlen
)
5720 no_improvement_count
= 0;
5722 /* PR 11843: Avoid futile long searches for the best bucket size
5723 when there are a large number of symbols. */
5724 else if (++no_improvement_count
== 100)
5731 #endif /* defined (BFD_HOST_U_64_BIT) */
5733 /* This is the fallback solution if no 64bit type is available or if we
5734 are not supposed to spend much time on optimizations. We select the
5735 bucket count using a fixed set of numbers. */
5736 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5738 best_size
= elf_buckets
[i
];
5739 if (nsyms
< elf_buckets
[i
+ 1])
5742 if (gnu_hash
&& best_size
< 2)
5749 /* Size any SHT_GROUP section for ld -r. */
5752 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5756 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5757 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5758 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5763 /* Set a default stack segment size. The value in INFO wins. If it
5764 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5765 undefined it is initialized. */
5768 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5769 struct bfd_link_info
*info
,
5770 const char *legacy_symbol
,
5771 bfd_vma default_size
)
5773 struct elf_link_hash_entry
*h
= NULL
;
5775 /* Look for legacy symbol. */
5777 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5778 FALSE
, FALSE
, FALSE
);
5779 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5780 || h
->root
.type
== bfd_link_hash_defweak
)
5782 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5784 /* The symbol has no type if specified on the command line. */
5785 h
->type
= STT_OBJECT
;
5786 if (info
->stacksize
)
5787 (*_bfd_error_handler
) (_("%B: stack size specified and %s set"),
5788 output_bfd
, legacy_symbol
);
5789 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5790 (*_bfd_error_handler
) (_("%B: %s not absolute"),
5791 output_bfd
, legacy_symbol
);
5793 info
->stacksize
= h
->root
.u
.def
.value
;
5796 if (!info
->stacksize
)
5797 /* If the user didn't set a size, or explicitly inhibit the
5798 size, set it now. */
5799 info
->stacksize
= default_size
;
5801 /* Provide the legacy symbol, if it is referenced. */
5802 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5803 || h
->root
.type
== bfd_link_hash_undefweak
))
5805 struct bfd_link_hash_entry
*bh
= NULL
;
5807 if (!(_bfd_generic_link_add_one_symbol
5808 (info
, output_bfd
, legacy_symbol
,
5809 BSF_GLOBAL
, bfd_abs_section_ptr
,
5810 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5811 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5814 h
= (struct elf_link_hash_entry
*) bh
;
5816 h
->type
= STT_OBJECT
;
5822 /* Set up the sizes and contents of the ELF dynamic sections. This is
5823 called by the ELF linker emulation before_allocation routine. We
5824 must set the sizes of the sections before the linker sets the
5825 addresses of the various sections. */
5828 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5831 const char *filter_shlib
,
5833 const char *depaudit
,
5834 const char * const *auxiliary_filters
,
5835 struct bfd_link_info
*info
,
5836 asection
**sinterpptr
)
5840 const struct elf_backend_data
*bed
;
5841 struct elf_info_failed asvinfo
;
5845 soname_indx
= (size_t) -1;
5847 if (!is_elf_hash_table (info
->hash
))
5850 bed
= get_elf_backend_data (output_bfd
);
5852 /* Any syms created from now on start with -1 in
5853 got.refcount/offset and plt.refcount/offset. */
5854 elf_hash_table (info
)->init_got_refcount
5855 = elf_hash_table (info
)->init_got_offset
;
5856 elf_hash_table (info
)->init_plt_refcount
5857 = elf_hash_table (info
)->init_plt_offset
;
5859 if (bfd_link_relocatable (info
)
5860 && !_bfd_elf_size_group_sections (info
))
5863 /* The backend may have to create some sections regardless of whether
5864 we're dynamic or not. */
5865 if (bed
->elf_backend_always_size_sections
5866 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5869 /* Determine any GNU_STACK segment requirements, after the backend
5870 has had a chance to set a default segment size. */
5871 if (info
->execstack
)
5872 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5873 else if (info
->noexecstack
)
5874 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5878 asection
*notesec
= NULL
;
5881 for (inputobj
= info
->input_bfds
;
5883 inputobj
= inputobj
->link
.next
)
5888 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5890 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5893 if (s
->flags
& SEC_CODE
)
5897 else if (bed
->default_execstack
)
5900 if (notesec
|| info
->stacksize
> 0)
5901 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5902 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5903 && notesec
->output_section
!= bfd_abs_section_ptr
)
5904 notesec
->output_section
->flags
|= SEC_CODE
;
5907 dynobj
= elf_hash_table (info
)->dynobj
;
5909 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5911 struct elf_info_failed eif
;
5912 struct elf_link_hash_entry
*h
;
5914 struct bfd_elf_version_tree
*t
;
5915 struct bfd_elf_version_expr
*d
;
5917 bfd_boolean all_defined
;
5919 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5920 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5924 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5926 if (soname_indx
== (size_t) -1
5927 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5933 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5935 info
->flags
|= DF_SYMBOLIC
;
5943 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5945 if (indx
== (size_t) -1)
5948 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5949 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5953 if (filter_shlib
!= NULL
)
5957 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5958 filter_shlib
, TRUE
);
5959 if (indx
== (size_t) -1
5960 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5964 if (auxiliary_filters
!= NULL
)
5966 const char * const *p
;
5968 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5972 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5974 if (indx
== (size_t) -1
5975 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5984 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5986 if (indx
== (size_t) -1
5987 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5991 if (depaudit
!= NULL
)
5995 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5997 if (indx
== (size_t) -1
5998 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6005 /* If we are supposed to export all symbols into the dynamic symbol
6006 table (this is not the normal case), then do so. */
6007 if (info
->export_dynamic
6008 || (bfd_link_executable (info
) && info
->dynamic
))
6010 elf_link_hash_traverse (elf_hash_table (info
),
6011 _bfd_elf_export_symbol
,
6017 /* Make all global versions with definition. */
6018 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6019 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6020 if (!d
->symver
&& d
->literal
)
6022 const char *verstr
, *name
;
6023 size_t namelen
, verlen
, newlen
;
6024 char *newname
, *p
, leading_char
;
6025 struct elf_link_hash_entry
*newh
;
6027 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6029 namelen
= strlen (name
) + (leading_char
!= '\0');
6031 verlen
= strlen (verstr
);
6032 newlen
= namelen
+ verlen
+ 3;
6034 newname
= (char *) bfd_malloc (newlen
);
6035 if (newname
== NULL
)
6037 newname
[0] = leading_char
;
6038 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6040 /* Check the hidden versioned definition. */
6041 p
= newname
+ namelen
;
6043 memcpy (p
, verstr
, verlen
+ 1);
6044 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6045 newname
, FALSE
, FALSE
,
6048 || (newh
->root
.type
!= bfd_link_hash_defined
6049 && newh
->root
.type
!= bfd_link_hash_defweak
))
6051 /* Check the default versioned definition. */
6053 memcpy (p
, verstr
, verlen
+ 1);
6054 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6055 newname
, FALSE
, FALSE
,
6060 /* Mark this version if there is a definition and it is
6061 not defined in a shared object. */
6063 && !newh
->def_dynamic
6064 && (newh
->root
.type
== bfd_link_hash_defined
6065 || newh
->root
.type
== bfd_link_hash_defweak
))
6069 /* Attach all the symbols to their version information. */
6070 asvinfo
.info
= info
;
6071 asvinfo
.failed
= FALSE
;
6073 elf_link_hash_traverse (elf_hash_table (info
),
6074 _bfd_elf_link_assign_sym_version
,
6079 if (!info
->allow_undefined_version
)
6081 /* Check if all global versions have a definition. */
6083 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6084 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6085 if (d
->literal
&& !d
->symver
&& !d
->script
)
6087 (*_bfd_error_handler
)
6088 (_("%s: undefined version: %s"),
6089 d
->pattern
, t
->name
);
6090 all_defined
= FALSE
;
6095 bfd_set_error (bfd_error_bad_value
);
6100 /* Find all symbols which were defined in a dynamic object and make
6101 the backend pick a reasonable value for them. */
6102 elf_link_hash_traverse (elf_hash_table (info
),
6103 _bfd_elf_adjust_dynamic_symbol
,
6108 /* Add some entries to the .dynamic section. We fill in some of the
6109 values later, in bfd_elf_final_link, but we must add the entries
6110 now so that we know the final size of the .dynamic section. */
6112 /* If there are initialization and/or finalization functions to
6113 call then add the corresponding DT_INIT/DT_FINI entries. */
6114 h
= (info
->init_function
6115 ? elf_link_hash_lookup (elf_hash_table (info
),
6116 info
->init_function
, FALSE
,
6123 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6126 h
= (info
->fini_function
6127 ? elf_link_hash_lookup (elf_hash_table (info
),
6128 info
->fini_function
, FALSE
,
6135 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6139 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6140 if (s
!= NULL
&& s
->linker_has_input
)
6142 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6143 if (! bfd_link_executable (info
))
6148 for (sub
= info
->input_bfds
; sub
!= NULL
;
6149 sub
= sub
->link
.next
)
6150 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6151 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6152 if (elf_section_data (o
)->this_hdr
.sh_type
6153 == SHT_PREINIT_ARRAY
)
6155 (*_bfd_error_handler
)
6156 (_("%B: .preinit_array section is not allowed in DSO"),
6161 bfd_set_error (bfd_error_nonrepresentable_section
);
6165 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6166 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6169 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6170 if (s
!= NULL
&& s
->linker_has_input
)
6172 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6173 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6176 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6177 if (s
!= NULL
&& s
->linker_has_input
)
6179 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6180 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6184 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6185 /* If .dynstr is excluded from the link, we don't want any of
6186 these tags. Strictly, we should be checking each section
6187 individually; This quick check covers for the case where
6188 someone does a /DISCARD/ : { *(*) }. */
6189 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6191 bfd_size_type strsize
;
6193 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6194 if ((info
->emit_hash
6195 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6196 || (info
->emit_gnu_hash
6197 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6198 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6199 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6200 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6201 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6202 bed
->s
->sizeof_sym
))
6207 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6210 /* The backend must work out the sizes of all the other dynamic
6213 && bed
->elf_backend_size_dynamic_sections
!= NULL
6214 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6217 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6219 unsigned long section_sym_count
;
6220 struct bfd_elf_version_tree
*verdefs
;
6223 /* Set up the version definition section. */
6224 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6225 BFD_ASSERT (s
!= NULL
);
6227 /* We may have created additional version definitions if we are
6228 just linking a regular application. */
6229 verdefs
= info
->version_info
;
6231 /* Skip anonymous version tag. */
6232 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6233 verdefs
= verdefs
->next
;
6235 if (verdefs
== NULL
&& !info
->create_default_symver
)
6236 s
->flags
|= SEC_EXCLUDE
;
6241 struct bfd_elf_version_tree
*t
;
6243 Elf_Internal_Verdef def
;
6244 Elf_Internal_Verdaux defaux
;
6245 struct bfd_link_hash_entry
*bh
;
6246 struct elf_link_hash_entry
*h
;
6252 /* Make space for the base version. */
6253 size
+= sizeof (Elf_External_Verdef
);
6254 size
+= sizeof (Elf_External_Verdaux
);
6257 /* Make space for the default version. */
6258 if (info
->create_default_symver
)
6260 size
+= sizeof (Elf_External_Verdef
);
6264 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6266 struct bfd_elf_version_deps
*n
;
6268 /* Don't emit base version twice. */
6272 size
+= sizeof (Elf_External_Verdef
);
6273 size
+= sizeof (Elf_External_Verdaux
);
6276 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6277 size
+= sizeof (Elf_External_Verdaux
);
6281 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6282 if (s
->contents
== NULL
&& s
->size
!= 0)
6285 /* Fill in the version definition section. */
6289 def
.vd_version
= VER_DEF_CURRENT
;
6290 def
.vd_flags
= VER_FLG_BASE
;
6293 if (info
->create_default_symver
)
6295 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6296 def
.vd_next
= sizeof (Elf_External_Verdef
);
6300 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6301 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6302 + sizeof (Elf_External_Verdaux
));
6305 if (soname_indx
!= (size_t) -1)
6307 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6309 def
.vd_hash
= bfd_elf_hash (soname
);
6310 defaux
.vda_name
= soname_indx
;
6317 name
= lbasename (output_bfd
->filename
);
6318 def
.vd_hash
= bfd_elf_hash (name
);
6319 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6321 if (indx
== (size_t) -1)
6323 defaux
.vda_name
= indx
;
6325 defaux
.vda_next
= 0;
6327 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6328 (Elf_External_Verdef
*) p
);
6329 p
+= sizeof (Elf_External_Verdef
);
6330 if (info
->create_default_symver
)
6332 /* Add a symbol representing this version. */
6334 if (! (_bfd_generic_link_add_one_symbol
6335 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6337 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6339 h
= (struct elf_link_hash_entry
*) bh
;
6342 h
->type
= STT_OBJECT
;
6343 h
->verinfo
.vertree
= NULL
;
6345 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6348 /* Create a duplicate of the base version with the same
6349 aux block, but different flags. */
6352 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6354 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6355 + sizeof (Elf_External_Verdaux
));
6358 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6359 (Elf_External_Verdef
*) p
);
6360 p
+= sizeof (Elf_External_Verdef
);
6362 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6363 (Elf_External_Verdaux
*) p
);
6364 p
+= sizeof (Elf_External_Verdaux
);
6366 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6369 struct bfd_elf_version_deps
*n
;
6371 /* Don't emit the base version twice. */
6376 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6379 /* Add a symbol representing this version. */
6381 if (! (_bfd_generic_link_add_one_symbol
6382 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6384 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6386 h
= (struct elf_link_hash_entry
*) bh
;
6389 h
->type
= STT_OBJECT
;
6390 h
->verinfo
.vertree
= t
;
6392 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6395 def
.vd_version
= VER_DEF_CURRENT
;
6397 if (t
->globals
.list
== NULL
6398 && t
->locals
.list
== NULL
6400 def
.vd_flags
|= VER_FLG_WEAK
;
6401 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6402 def
.vd_cnt
= cdeps
+ 1;
6403 def
.vd_hash
= bfd_elf_hash (t
->name
);
6404 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6407 /* If a basever node is next, it *must* be the last node in
6408 the chain, otherwise Verdef construction breaks. */
6409 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6410 BFD_ASSERT (t
->next
->next
== NULL
);
6412 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6413 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6414 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6416 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6417 (Elf_External_Verdef
*) p
);
6418 p
+= sizeof (Elf_External_Verdef
);
6420 defaux
.vda_name
= h
->dynstr_index
;
6421 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6423 defaux
.vda_next
= 0;
6424 if (t
->deps
!= NULL
)
6425 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6426 t
->name_indx
= defaux
.vda_name
;
6428 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6429 (Elf_External_Verdaux
*) p
);
6430 p
+= sizeof (Elf_External_Verdaux
);
6432 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6434 if (n
->version_needed
== NULL
)
6436 /* This can happen if there was an error in the
6438 defaux
.vda_name
= 0;
6442 defaux
.vda_name
= n
->version_needed
->name_indx
;
6443 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6446 if (n
->next
== NULL
)
6447 defaux
.vda_next
= 0;
6449 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6451 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6452 (Elf_External_Verdaux
*) p
);
6453 p
+= sizeof (Elf_External_Verdaux
);
6457 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6458 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6461 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6464 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6466 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6469 else if (info
->flags
& DF_BIND_NOW
)
6471 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6477 if (bfd_link_executable (info
))
6478 info
->flags_1
&= ~ (DF_1_INITFIRST
6481 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6485 /* Work out the size of the version reference section. */
6487 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6488 BFD_ASSERT (s
!= NULL
);
6490 struct elf_find_verdep_info sinfo
;
6493 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6494 if (sinfo
.vers
== 0)
6496 sinfo
.failed
= FALSE
;
6498 elf_link_hash_traverse (elf_hash_table (info
),
6499 _bfd_elf_link_find_version_dependencies
,
6504 if (elf_tdata (output_bfd
)->verref
== NULL
)
6505 s
->flags
|= SEC_EXCLUDE
;
6508 Elf_Internal_Verneed
*t
;
6513 /* Build the version dependency section. */
6516 for (t
= elf_tdata (output_bfd
)->verref
;
6520 Elf_Internal_Vernaux
*a
;
6522 size
+= sizeof (Elf_External_Verneed
);
6524 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6525 size
+= sizeof (Elf_External_Vernaux
);
6529 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6530 if (s
->contents
== NULL
)
6534 for (t
= elf_tdata (output_bfd
)->verref
;
6539 Elf_Internal_Vernaux
*a
;
6543 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6546 t
->vn_version
= VER_NEED_CURRENT
;
6548 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6549 elf_dt_name (t
->vn_bfd
) != NULL
6550 ? elf_dt_name (t
->vn_bfd
)
6551 : lbasename (t
->vn_bfd
->filename
),
6553 if (indx
== (size_t) -1)
6556 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6557 if (t
->vn_nextref
== NULL
)
6560 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6561 + caux
* sizeof (Elf_External_Vernaux
));
6563 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6564 (Elf_External_Verneed
*) p
);
6565 p
+= sizeof (Elf_External_Verneed
);
6567 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6569 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6570 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6571 a
->vna_nodename
, FALSE
);
6572 if (indx
== (size_t) -1)
6575 if (a
->vna_nextptr
== NULL
)
6578 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6580 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6581 (Elf_External_Vernaux
*) p
);
6582 p
+= sizeof (Elf_External_Vernaux
);
6586 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6587 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6590 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6594 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6595 && elf_tdata (output_bfd
)->cverdefs
== 0)
6596 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6597 §ion_sym_count
) == 0)
6599 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6600 s
->flags
|= SEC_EXCLUDE
;
6606 /* Find the first non-excluded output section. We'll use its
6607 section symbol for some emitted relocs. */
6609 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6613 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6614 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6615 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6617 elf_hash_table (info
)->text_index_section
= s
;
6622 /* Find two non-excluded output sections, one for code, one for data.
6623 We'll use their section symbols for some emitted relocs. */
6625 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6629 /* Data first, since setting text_index_section changes
6630 _bfd_elf_link_omit_section_dynsym. */
6631 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6632 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6633 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6635 elf_hash_table (info
)->data_index_section
= s
;
6639 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6640 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6641 == (SEC_ALLOC
| SEC_READONLY
))
6642 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6644 elf_hash_table (info
)->text_index_section
= s
;
6648 if (elf_hash_table (info
)->text_index_section
== NULL
)
6649 elf_hash_table (info
)->text_index_section
6650 = elf_hash_table (info
)->data_index_section
;
6654 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6656 const struct elf_backend_data
*bed
;
6658 if (!is_elf_hash_table (info
->hash
))
6661 bed
= get_elf_backend_data (output_bfd
);
6662 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6664 if (elf_hash_table (info
)->dynamic_sections_created
)
6668 bfd_size_type dynsymcount
;
6669 unsigned long section_sym_count
;
6670 unsigned int dtagcount
;
6672 dynobj
= elf_hash_table (info
)->dynobj
;
6674 /* Assign dynsym indicies. In a shared library we generate a
6675 section symbol for each output section, which come first.
6676 Next come all of the back-end allocated local dynamic syms,
6677 followed by the rest of the global symbols. */
6679 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6680 §ion_sym_count
);
6682 /* Work out the size of the symbol version section. */
6683 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6684 BFD_ASSERT (s
!= NULL
);
6685 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6687 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6688 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6689 if (s
->contents
== NULL
)
6692 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6696 /* Set the size of the .dynsym and .hash sections. We counted
6697 the number of dynamic symbols in elf_link_add_object_symbols.
6698 We will build the contents of .dynsym and .hash when we build
6699 the final symbol table, because until then we do not know the
6700 correct value to give the symbols. We built the .dynstr
6701 section as we went along in elf_link_add_object_symbols. */
6702 s
= elf_hash_table (info
)->dynsym
;
6703 BFD_ASSERT (s
!= NULL
);
6704 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6706 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6707 if (s
->contents
== NULL
)
6710 /* The first entry in .dynsym is a dummy symbol. Clear all the
6711 section syms, in case we don't output them all. */
6712 ++section_sym_count
;
6713 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6715 elf_hash_table (info
)->bucketcount
= 0;
6717 /* Compute the size of the hashing table. As a side effect this
6718 computes the hash values for all the names we export. */
6719 if (info
->emit_hash
)
6721 unsigned long int *hashcodes
;
6722 struct hash_codes_info hashinf
;
6724 unsigned long int nsyms
;
6726 size_t hash_entry_size
;
6728 /* Compute the hash values for all exported symbols. At the same
6729 time store the values in an array so that we could use them for
6731 amt
= dynsymcount
* sizeof (unsigned long int);
6732 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6733 if (hashcodes
== NULL
)
6735 hashinf
.hashcodes
= hashcodes
;
6736 hashinf
.error
= FALSE
;
6738 /* Put all hash values in HASHCODES. */
6739 elf_link_hash_traverse (elf_hash_table (info
),
6740 elf_collect_hash_codes
, &hashinf
);
6747 nsyms
= hashinf
.hashcodes
- hashcodes
;
6749 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6752 if (bucketcount
== 0)
6755 elf_hash_table (info
)->bucketcount
= bucketcount
;
6757 s
= bfd_get_linker_section (dynobj
, ".hash");
6758 BFD_ASSERT (s
!= NULL
);
6759 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6760 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6761 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6762 if (s
->contents
== NULL
)
6765 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6766 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6767 s
->contents
+ hash_entry_size
);
6770 if (info
->emit_gnu_hash
)
6773 unsigned char *contents
;
6774 struct collect_gnu_hash_codes cinfo
;
6778 memset (&cinfo
, 0, sizeof (cinfo
));
6780 /* Compute the hash values for all exported symbols. At the same
6781 time store the values in an array so that we could use them for
6783 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6784 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6785 if (cinfo
.hashcodes
== NULL
)
6788 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6789 cinfo
.min_dynindx
= -1;
6790 cinfo
.output_bfd
= output_bfd
;
6793 /* Put all hash values in HASHCODES. */
6794 elf_link_hash_traverse (elf_hash_table (info
),
6795 elf_collect_gnu_hash_codes
, &cinfo
);
6798 free (cinfo
.hashcodes
);
6803 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6805 if (bucketcount
== 0)
6807 free (cinfo
.hashcodes
);
6811 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6812 BFD_ASSERT (s
!= NULL
);
6814 if (cinfo
.nsyms
== 0)
6816 /* Empty .gnu.hash section is special. */
6817 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6818 free (cinfo
.hashcodes
);
6819 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6820 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6821 if (contents
== NULL
)
6823 s
->contents
= contents
;
6824 /* 1 empty bucket. */
6825 bfd_put_32 (output_bfd
, 1, contents
);
6826 /* SYMIDX above the special symbol 0. */
6827 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6828 /* Just one word for bitmask. */
6829 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6830 /* Only hash fn bloom filter. */
6831 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6832 /* No hashes are valid - empty bitmask. */
6833 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6834 /* No hashes in the only bucket. */
6835 bfd_put_32 (output_bfd
, 0,
6836 contents
+ 16 + bed
->s
->arch_size
/ 8);
6840 unsigned long int maskwords
, maskbitslog2
, x
;
6841 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6845 while ((x
>>= 1) != 0)
6847 if (maskbitslog2
< 3)
6849 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6850 maskbitslog2
= maskbitslog2
+ 3;
6852 maskbitslog2
= maskbitslog2
+ 2;
6853 if (bed
->s
->arch_size
== 64)
6855 if (maskbitslog2
== 5)
6861 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6862 cinfo
.shift2
= maskbitslog2
;
6863 cinfo
.maskbits
= 1 << maskbitslog2
;
6864 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6865 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6866 amt
+= maskwords
* sizeof (bfd_vma
);
6867 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6868 if (cinfo
.bitmask
== NULL
)
6870 free (cinfo
.hashcodes
);
6874 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6875 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6876 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6877 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6879 /* Determine how often each hash bucket is used. */
6880 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6881 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6882 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6884 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6885 if (cinfo
.counts
[i
] != 0)
6887 cinfo
.indx
[i
] = cnt
;
6888 cnt
+= cinfo
.counts
[i
];
6890 BFD_ASSERT (cnt
== dynsymcount
);
6891 cinfo
.bucketcount
= bucketcount
;
6892 cinfo
.local_indx
= cinfo
.min_dynindx
;
6894 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6895 s
->size
+= cinfo
.maskbits
/ 8;
6896 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6897 if (contents
== NULL
)
6899 free (cinfo
.bitmask
);
6900 free (cinfo
.hashcodes
);
6904 s
->contents
= contents
;
6905 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6906 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6907 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6908 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6909 contents
+= 16 + cinfo
.maskbits
/ 8;
6911 for (i
= 0; i
< bucketcount
; ++i
)
6913 if (cinfo
.counts
[i
] == 0)
6914 bfd_put_32 (output_bfd
, 0, contents
);
6916 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6920 cinfo
.contents
= contents
;
6922 /* Renumber dynamic symbols, populate .gnu.hash section. */
6923 elf_link_hash_traverse (elf_hash_table (info
),
6924 elf_renumber_gnu_hash_syms
, &cinfo
);
6926 contents
= s
->contents
+ 16;
6927 for (i
= 0; i
< maskwords
; ++i
)
6929 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6931 contents
+= bed
->s
->arch_size
/ 8;
6934 free (cinfo
.bitmask
);
6935 free (cinfo
.hashcodes
);
6939 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6940 BFD_ASSERT (s
!= NULL
);
6942 elf_finalize_dynstr (output_bfd
, info
);
6944 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6946 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6947 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6954 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6957 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6960 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6961 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6964 /* Finish SHF_MERGE section merging. */
6967 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6972 if (!is_elf_hash_table (info
->hash
))
6975 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6976 if ((ibfd
->flags
& DYNAMIC
) == 0
6977 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6978 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6979 == get_elf_backend_data (obfd
)->s
->elfclass
))
6980 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6981 if ((sec
->flags
& SEC_MERGE
) != 0
6982 && !bfd_is_abs_section (sec
->output_section
))
6984 struct bfd_elf_section_data
*secdata
;
6986 secdata
= elf_section_data (sec
);
6987 if (! _bfd_add_merge_section (obfd
,
6988 &elf_hash_table (info
)->merge_info
,
6989 sec
, &secdata
->sec_info
))
6991 else if (secdata
->sec_info
)
6992 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6995 if (elf_hash_table (info
)->merge_info
!= NULL
)
6996 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
6997 merge_sections_remove_hook
);
7001 /* Create an entry in an ELF linker hash table. */
7003 struct bfd_hash_entry
*
7004 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7005 struct bfd_hash_table
*table
,
7008 /* Allocate the structure if it has not already been allocated by a
7012 entry
= (struct bfd_hash_entry
*)
7013 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7018 /* Call the allocation method of the superclass. */
7019 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7022 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7023 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7025 /* Set local fields. */
7028 ret
->got
= htab
->init_got_refcount
;
7029 ret
->plt
= htab
->init_plt_refcount
;
7030 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7031 - offsetof (struct elf_link_hash_entry
, size
)));
7032 /* Assume that we have been called by a non-ELF symbol reader.
7033 This flag is then reset by the code which reads an ELF input
7034 file. This ensures that a symbol created by a non-ELF symbol
7035 reader will have the flag set correctly. */
7042 /* Copy data from an indirect symbol to its direct symbol, hiding the
7043 old indirect symbol. Also used for copying flags to a weakdef. */
7046 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7047 struct elf_link_hash_entry
*dir
,
7048 struct elf_link_hash_entry
*ind
)
7050 struct elf_link_hash_table
*htab
;
7052 /* Copy down any references that we may have already seen to the
7053 symbol which just became indirect if DIR isn't a hidden versioned
7056 if (dir
->versioned
!= versioned_hidden
)
7058 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7059 dir
->ref_regular
|= ind
->ref_regular
;
7060 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7061 dir
->non_got_ref
|= ind
->non_got_ref
;
7062 dir
->needs_plt
|= ind
->needs_plt
;
7063 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7066 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7069 /* Copy over the global and procedure linkage table refcount entries.
7070 These may have been already set up by a check_relocs routine. */
7071 htab
= elf_hash_table (info
);
7072 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7074 if (dir
->got
.refcount
< 0)
7075 dir
->got
.refcount
= 0;
7076 dir
->got
.refcount
+= ind
->got
.refcount
;
7077 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7080 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7082 if (dir
->plt
.refcount
< 0)
7083 dir
->plt
.refcount
= 0;
7084 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7085 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7088 if (ind
->dynindx
!= -1)
7090 if (dir
->dynindx
!= -1)
7091 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7092 dir
->dynindx
= ind
->dynindx
;
7093 dir
->dynstr_index
= ind
->dynstr_index
;
7095 ind
->dynstr_index
= 0;
7100 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7101 struct elf_link_hash_entry
*h
,
7102 bfd_boolean force_local
)
7104 /* STT_GNU_IFUNC symbol must go through PLT. */
7105 if (h
->type
!= STT_GNU_IFUNC
)
7107 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7112 h
->forced_local
= 1;
7113 if (h
->dynindx
!= -1)
7116 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7122 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7126 _bfd_elf_link_hash_table_init
7127 (struct elf_link_hash_table
*table
,
7129 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7130 struct bfd_hash_table
*,
7132 unsigned int entsize
,
7133 enum elf_target_id target_id
)
7136 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7138 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7139 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7140 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7141 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7142 /* The first dynamic symbol is a dummy. */
7143 table
->dynsymcount
= 1;
7145 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7147 table
->root
.type
= bfd_link_elf_hash_table
;
7148 table
->hash_table_id
= target_id
;
7153 /* Create an ELF linker hash table. */
7155 struct bfd_link_hash_table
*
7156 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7158 struct elf_link_hash_table
*ret
;
7159 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7161 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7165 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7166 sizeof (struct elf_link_hash_entry
),
7172 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7177 /* Destroy an ELF linker hash table. */
7180 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7182 struct elf_link_hash_table
*htab
;
7184 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7185 if (htab
->dynstr
!= NULL
)
7186 _bfd_elf_strtab_free (htab
->dynstr
);
7187 _bfd_merge_sections_free (htab
->merge_info
);
7188 _bfd_generic_link_hash_table_free (obfd
);
7191 /* This is a hook for the ELF emulation code in the generic linker to
7192 tell the backend linker what file name to use for the DT_NEEDED
7193 entry for a dynamic object. */
7196 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7198 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7199 && bfd_get_format (abfd
) == bfd_object
)
7200 elf_dt_name (abfd
) = name
;
7204 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7207 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7208 && bfd_get_format (abfd
) == bfd_object
)
7209 lib_class
= elf_dyn_lib_class (abfd
);
7216 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7218 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7219 && bfd_get_format (abfd
) == bfd_object
)
7220 elf_dyn_lib_class (abfd
) = lib_class
;
7223 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7224 the linker ELF emulation code. */
7226 struct bfd_link_needed_list
*
7227 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7228 struct bfd_link_info
*info
)
7230 if (! is_elf_hash_table (info
->hash
))
7232 return elf_hash_table (info
)->needed
;
7235 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7236 hook for the linker ELF emulation code. */
7238 struct bfd_link_needed_list
*
7239 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7240 struct bfd_link_info
*info
)
7242 if (! is_elf_hash_table (info
->hash
))
7244 return elf_hash_table (info
)->runpath
;
7247 /* Get the name actually used for a dynamic object for a link. This
7248 is the SONAME entry if there is one. Otherwise, it is the string
7249 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7252 bfd_elf_get_dt_soname (bfd
*abfd
)
7254 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7255 && bfd_get_format (abfd
) == bfd_object
)
7256 return elf_dt_name (abfd
);
7260 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7261 the ELF linker emulation code. */
7264 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7265 struct bfd_link_needed_list
**pneeded
)
7268 bfd_byte
*dynbuf
= NULL
;
7269 unsigned int elfsec
;
7270 unsigned long shlink
;
7271 bfd_byte
*extdyn
, *extdynend
;
7273 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7277 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7278 || bfd_get_format (abfd
) != bfd_object
)
7281 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7282 if (s
== NULL
|| s
->size
== 0)
7285 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7288 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7289 if (elfsec
== SHN_BAD
)
7292 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7294 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7295 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7298 extdynend
= extdyn
+ s
->size
;
7299 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7301 Elf_Internal_Dyn dyn
;
7303 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7305 if (dyn
.d_tag
== DT_NULL
)
7308 if (dyn
.d_tag
== DT_NEEDED
)
7311 struct bfd_link_needed_list
*l
;
7312 unsigned int tagv
= dyn
.d_un
.d_val
;
7315 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7320 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7341 struct elf_symbuf_symbol
7343 unsigned long st_name
; /* Symbol name, index in string tbl */
7344 unsigned char st_info
; /* Type and binding attributes */
7345 unsigned char st_other
; /* Visibilty, and target specific */
7348 struct elf_symbuf_head
7350 struct elf_symbuf_symbol
*ssym
;
7352 unsigned int st_shndx
;
7359 Elf_Internal_Sym
*isym
;
7360 struct elf_symbuf_symbol
*ssym
;
7365 /* Sort references to symbols by ascending section number. */
7368 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7370 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7371 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7373 return s1
->st_shndx
- s2
->st_shndx
;
7377 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7379 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7380 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7381 return strcmp (s1
->name
, s2
->name
);
7384 static struct elf_symbuf_head
*
7385 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7387 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7388 struct elf_symbuf_symbol
*ssym
;
7389 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7390 size_t i
, shndx_count
, total_size
;
7392 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7396 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7397 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7398 *ind
++ = &isymbuf
[i
];
7401 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7402 elf_sort_elf_symbol
);
7405 if (indbufend
> indbuf
)
7406 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7407 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7410 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7411 + (indbufend
- indbuf
) * sizeof (*ssym
));
7412 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7413 if (ssymbuf
== NULL
)
7419 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7420 ssymbuf
->ssym
= NULL
;
7421 ssymbuf
->count
= shndx_count
;
7422 ssymbuf
->st_shndx
= 0;
7423 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7425 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7428 ssymhead
->ssym
= ssym
;
7429 ssymhead
->count
= 0;
7430 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7432 ssym
->st_name
= (*ind
)->st_name
;
7433 ssym
->st_info
= (*ind
)->st_info
;
7434 ssym
->st_other
= (*ind
)->st_other
;
7437 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7438 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7445 /* Check if 2 sections define the same set of local and global
7449 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7450 struct bfd_link_info
*info
)
7453 const struct elf_backend_data
*bed1
, *bed2
;
7454 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7455 size_t symcount1
, symcount2
;
7456 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7457 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7458 Elf_Internal_Sym
*isym
, *isymend
;
7459 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7460 size_t count1
, count2
, i
;
7461 unsigned int shndx1
, shndx2
;
7467 /* Both sections have to be in ELF. */
7468 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7469 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7472 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7475 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7476 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7477 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7480 bed1
= get_elf_backend_data (bfd1
);
7481 bed2
= get_elf_backend_data (bfd2
);
7482 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7483 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7484 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7485 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7487 if (symcount1
== 0 || symcount2
== 0)
7493 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7494 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7496 if (ssymbuf1
== NULL
)
7498 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7500 if (isymbuf1
== NULL
)
7503 if (!info
->reduce_memory_overheads
)
7504 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7505 = elf_create_symbuf (symcount1
, isymbuf1
);
7508 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7510 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7512 if (isymbuf2
== NULL
)
7515 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7516 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7517 = elf_create_symbuf (symcount2
, isymbuf2
);
7520 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7522 /* Optimized faster version. */
7524 struct elf_symbol
*symp
;
7525 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7528 hi
= ssymbuf1
->count
;
7533 mid
= (lo
+ hi
) / 2;
7534 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7536 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7540 count1
= ssymbuf1
[mid
].count
;
7547 hi
= ssymbuf2
->count
;
7552 mid
= (lo
+ hi
) / 2;
7553 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7555 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7559 count2
= ssymbuf2
[mid
].count
;
7565 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7569 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7571 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7572 if (symtable1
== NULL
|| symtable2
== NULL
)
7576 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7577 ssym
< ssymend
; ssym
++, symp
++)
7579 symp
->u
.ssym
= ssym
;
7580 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7586 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7587 ssym
< ssymend
; ssym
++, symp
++)
7589 symp
->u
.ssym
= ssym
;
7590 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7595 /* Sort symbol by name. */
7596 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7597 elf_sym_name_compare
);
7598 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7599 elf_sym_name_compare
);
7601 for (i
= 0; i
< count1
; i
++)
7602 /* Two symbols must have the same binding, type and name. */
7603 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7604 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7605 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7612 symtable1
= (struct elf_symbol
*)
7613 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7614 symtable2
= (struct elf_symbol
*)
7615 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7616 if (symtable1
== NULL
|| symtable2
== NULL
)
7619 /* Count definitions in the section. */
7621 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7622 if (isym
->st_shndx
== shndx1
)
7623 symtable1
[count1
++].u
.isym
= isym
;
7626 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7627 if (isym
->st_shndx
== shndx2
)
7628 symtable2
[count2
++].u
.isym
= isym
;
7630 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7633 for (i
= 0; i
< count1
; i
++)
7635 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7636 symtable1
[i
].u
.isym
->st_name
);
7638 for (i
= 0; i
< count2
; i
++)
7640 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7641 symtable2
[i
].u
.isym
->st_name
);
7643 /* Sort symbol by name. */
7644 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7645 elf_sym_name_compare
);
7646 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7647 elf_sym_name_compare
);
7649 for (i
= 0; i
< count1
; i
++)
7650 /* Two symbols must have the same binding, type and name. */
7651 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7652 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7653 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7671 /* Return TRUE if 2 section types are compatible. */
7674 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7675 bfd
*bbfd
, const asection
*bsec
)
7679 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7680 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7683 return elf_section_type (asec
) == elf_section_type (bsec
);
7686 /* Final phase of ELF linker. */
7688 /* A structure we use to avoid passing large numbers of arguments. */
7690 struct elf_final_link_info
7692 /* General link information. */
7693 struct bfd_link_info
*info
;
7696 /* Symbol string table. */
7697 struct elf_strtab_hash
*symstrtab
;
7698 /* .hash section. */
7700 /* symbol version section (.gnu.version). */
7701 asection
*symver_sec
;
7702 /* Buffer large enough to hold contents of any section. */
7704 /* Buffer large enough to hold external relocs of any section. */
7705 void *external_relocs
;
7706 /* Buffer large enough to hold internal relocs of any section. */
7707 Elf_Internal_Rela
*internal_relocs
;
7708 /* Buffer large enough to hold external local symbols of any input
7710 bfd_byte
*external_syms
;
7711 /* And a buffer for symbol section indices. */
7712 Elf_External_Sym_Shndx
*locsym_shndx
;
7713 /* Buffer large enough to hold internal local symbols of any input
7715 Elf_Internal_Sym
*internal_syms
;
7716 /* Array large enough to hold a symbol index for each local symbol
7717 of any input BFD. */
7719 /* Array large enough to hold a section pointer for each local
7720 symbol of any input BFD. */
7721 asection
**sections
;
7722 /* Buffer for SHT_SYMTAB_SHNDX section. */
7723 Elf_External_Sym_Shndx
*symshndxbuf
;
7724 /* Number of STT_FILE syms seen. */
7725 size_t filesym_count
;
7728 /* This struct is used to pass information to elf_link_output_extsym. */
7730 struct elf_outext_info
7733 bfd_boolean localsyms
;
7734 bfd_boolean file_sym_done
;
7735 struct elf_final_link_info
*flinfo
;
7739 /* Support for evaluating a complex relocation.
7741 Complex relocations are generalized, self-describing relocations. The
7742 implementation of them consists of two parts: complex symbols, and the
7743 relocations themselves.
7745 The relocations are use a reserved elf-wide relocation type code (R_RELC
7746 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7747 information (start bit, end bit, word width, etc) into the addend. This
7748 information is extracted from CGEN-generated operand tables within gas.
7750 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7751 internal) representing prefix-notation expressions, including but not
7752 limited to those sorts of expressions normally encoded as addends in the
7753 addend field. The symbol mangling format is:
7756 | <unary-operator> ':' <node>
7757 | <binary-operator> ':' <node> ':' <node>
7760 <literal> := 's' <digits=N> ':' <N character symbol name>
7761 | 'S' <digits=N> ':' <N character section name>
7765 <binary-operator> := as in C
7766 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7769 set_symbol_value (bfd
*bfd_with_globals
,
7770 Elf_Internal_Sym
*isymbuf
,
7775 struct elf_link_hash_entry
**sym_hashes
;
7776 struct elf_link_hash_entry
*h
;
7777 size_t extsymoff
= locsymcount
;
7779 if (symidx
< locsymcount
)
7781 Elf_Internal_Sym
*sym
;
7783 sym
= isymbuf
+ symidx
;
7784 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7786 /* It is a local symbol: move it to the
7787 "absolute" section and give it a value. */
7788 sym
->st_shndx
= SHN_ABS
;
7789 sym
->st_value
= val
;
7792 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7796 /* It is a global symbol: set its link type
7797 to "defined" and give it a value. */
7799 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7800 h
= sym_hashes
[symidx
- extsymoff
];
7801 while (h
->root
.type
== bfd_link_hash_indirect
7802 || h
->root
.type
== bfd_link_hash_warning
)
7803 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7804 h
->root
.type
= bfd_link_hash_defined
;
7805 h
->root
.u
.def
.value
= val
;
7806 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7810 resolve_symbol (const char *name
,
7812 struct elf_final_link_info
*flinfo
,
7814 Elf_Internal_Sym
*isymbuf
,
7817 Elf_Internal_Sym
*sym
;
7818 struct bfd_link_hash_entry
*global_entry
;
7819 const char *candidate
= NULL
;
7820 Elf_Internal_Shdr
*symtab_hdr
;
7823 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7825 for (i
= 0; i
< locsymcount
; ++ i
)
7829 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7832 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7833 symtab_hdr
->sh_link
,
7836 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7837 name
, candidate
, (unsigned long) sym
->st_value
);
7839 if (candidate
&& strcmp (candidate
, name
) == 0)
7841 asection
*sec
= flinfo
->sections
[i
];
7843 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7844 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7846 printf ("Found symbol with value %8.8lx\n",
7847 (unsigned long) *result
);
7853 /* Hmm, haven't found it yet. perhaps it is a global. */
7854 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7855 FALSE
, FALSE
, TRUE
);
7859 if (global_entry
->type
== bfd_link_hash_defined
7860 || global_entry
->type
== bfd_link_hash_defweak
)
7862 *result
= (global_entry
->u
.def
.value
7863 + global_entry
->u
.def
.section
->output_section
->vma
7864 + global_entry
->u
.def
.section
->output_offset
);
7866 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7867 global_entry
->root
.string
, (unsigned long) *result
);
7875 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7876 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7877 names like "foo.end" which is the end address of section "foo". */
7880 resolve_section (const char *name
,
7888 for (curr
= sections
; curr
; curr
= curr
->next
)
7889 if (strcmp (curr
->name
, name
) == 0)
7891 *result
= curr
->vma
;
7895 /* Hmm. still haven't found it. try pseudo-section names. */
7896 /* FIXME: This could be coded more efficiently... */
7897 for (curr
= sections
; curr
; curr
= curr
->next
)
7899 len
= strlen (curr
->name
);
7900 if (len
> strlen (name
))
7903 if (strncmp (curr
->name
, name
, len
) == 0)
7905 if (strncmp (".end", name
+ len
, 4) == 0)
7907 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7911 /* Insert more pseudo-section names here, if you like. */
7919 undefined_reference (const char *reftype
, const char *name
)
7921 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7926 eval_symbol (bfd_vma
*result
,
7929 struct elf_final_link_info
*flinfo
,
7931 Elf_Internal_Sym
*isymbuf
,
7940 const char *sym
= *symp
;
7942 bfd_boolean symbol_is_section
= FALSE
;
7947 if (len
< 1 || len
> sizeof (symbuf
))
7949 bfd_set_error (bfd_error_invalid_operation
);
7962 *result
= strtoul (sym
, (char **) symp
, 16);
7966 symbol_is_section
= TRUE
;
7969 symlen
= strtol (sym
, (char **) symp
, 10);
7970 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7972 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7974 bfd_set_error (bfd_error_invalid_operation
);
7978 memcpy (symbuf
, sym
, symlen
);
7979 symbuf
[symlen
] = '\0';
7980 *symp
= sym
+ symlen
;
7982 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7983 the symbol as a section, or vice-versa. so we're pretty liberal in our
7984 interpretation here; section means "try section first", not "must be a
7985 section", and likewise with symbol. */
7987 if (symbol_is_section
)
7989 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
7990 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7991 isymbuf
, locsymcount
))
7993 undefined_reference ("section", symbuf
);
7999 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8000 isymbuf
, locsymcount
)
8001 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8004 undefined_reference ("symbol", symbuf
);
8011 /* All that remains are operators. */
8013 #define UNARY_OP(op) \
8014 if (strncmp (sym, #op, strlen (#op)) == 0) \
8016 sym += strlen (#op); \
8020 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8021 isymbuf, locsymcount, signed_p)) \
8024 *result = op ((bfd_signed_vma) a); \
8030 #define BINARY_OP(op) \
8031 if (strncmp (sym, #op, strlen (#op)) == 0) \
8033 sym += strlen (#op); \
8037 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8038 isymbuf, locsymcount, signed_p)) \
8041 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8042 isymbuf, locsymcount, signed_p)) \
8045 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8075 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8076 bfd_set_error (bfd_error_invalid_operation
);
8082 put_value (bfd_vma size
,
8083 unsigned long chunksz
,
8088 location
+= (size
- chunksz
);
8090 for (; size
; size
-= chunksz
, location
-= chunksz
)
8095 bfd_put_8 (input_bfd
, x
, location
);
8099 bfd_put_16 (input_bfd
, x
, location
);
8103 bfd_put_32 (input_bfd
, x
, location
);
8104 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8110 bfd_put_64 (input_bfd
, x
, location
);
8111 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8124 get_value (bfd_vma size
,
8125 unsigned long chunksz
,
8132 /* Sanity checks. */
8133 BFD_ASSERT (chunksz
<= sizeof (x
)
8136 && (size
% chunksz
) == 0
8137 && input_bfd
!= NULL
8138 && location
!= NULL
);
8140 if (chunksz
== sizeof (x
))
8142 BFD_ASSERT (size
== chunksz
);
8144 /* Make sure that we do not perform an undefined shift operation.
8145 We know that size == chunksz so there will only be one iteration
8146 of the loop below. */
8150 shift
= 8 * chunksz
;
8152 for (; size
; size
-= chunksz
, location
+= chunksz
)
8157 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8160 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8163 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8167 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8178 decode_complex_addend (unsigned long *start
, /* in bits */
8179 unsigned long *oplen
, /* in bits */
8180 unsigned long *len
, /* in bits */
8181 unsigned long *wordsz
, /* in bytes */
8182 unsigned long *chunksz
, /* in bytes */
8183 unsigned long *lsb0_p
,
8184 unsigned long *signed_p
,
8185 unsigned long *trunc_p
,
8186 unsigned long encoded
)
8188 * start
= encoded
& 0x3F;
8189 * len
= (encoded
>> 6) & 0x3F;
8190 * oplen
= (encoded
>> 12) & 0x3F;
8191 * wordsz
= (encoded
>> 18) & 0xF;
8192 * chunksz
= (encoded
>> 22) & 0xF;
8193 * lsb0_p
= (encoded
>> 27) & 1;
8194 * signed_p
= (encoded
>> 28) & 1;
8195 * trunc_p
= (encoded
>> 29) & 1;
8198 bfd_reloc_status_type
8199 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8200 asection
*input_section ATTRIBUTE_UNUSED
,
8202 Elf_Internal_Rela
*rel
,
8205 bfd_vma shift
, x
, mask
;
8206 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8207 bfd_reloc_status_type r
;
8209 /* Perform this reloc, since it is complex.
8210 (this is not to say that it necessarily refers to a complex
8211 symbol; merely that it is a self-describing CGEN based reloc.
8212 i.e. the addend has the complete reloc information (bit start, end,
8213 word size, etc) encoded within it.). */
8215 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8216 &chunksz
, &lsb0_p
, &signed_p
,
8217 &trunc_p
, rel
->r_addend
);
8219 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8222 shift
= (start
+ 1) - len
;
8224 shift
= (8 * wordsz
) - (start
+ len
);
8226 x
= get_value (wordsz
, chunksz
, input_bfd
,
8227 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8230 printf ("Doing complex reloc: "
8231 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8232 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8233 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8234 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8235 oplen
, (unsigned long) x
, (unsigned long) mask
,
8236 (unsigned long) relocation
);
8241 /* Now do an overflow check. */
8242 r
= bfd_check_overflow ((signed_p
8243 ? complain_overflow_signed
8244 : complain_overflow_unsigned
),
8245 len
, 0, (8 * wordsz
),
8249 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8252 printf (" relocation: %8.8lx\n"
8253 " shifted mask: %8.8lx\n"
8254 " shifted/masked reloc: %8.8lx\n"
8255 " result: %8.8lx\n",
8256 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8257 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8259 put_value (wordsz
, chunksz
, input_bfd
, x
,
8260 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8264 /* Functions to read r_offset from external (target order) reloc
8265 entry. Faster than bfd_getl32 et al, because we let the compiler
8266 know the value is aligned. */
8269 ext32l_r_offset (const void *p
)
8276 const union aligned32
*a
8277 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8279 uint32_t aval
= ( (uint32_t) a
->c
[0]
8280 | (uint32_t) a
->c
[1] << 8
8281 | (uint32_t) a
->c
[2] << 16
8282 | (uint32_t) a
->c
[3] << 24);
8287 ext32b_r_offset (const void *p
)
8294 const union aligned32
*a
8295 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8297 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8298 | (uint32_t) a
->c
[1] << 16
8299 | (uint32_t) a
->c
[2] << 8
8300 | (uint32_t) a
->c
[3]);
8304 #ifdef BFD_HOST_64_BIT
8306 ext64l_r_offset (const void *p
)
8313 const union aligned64
*a
8314 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8316 uint64_t aval
= ( (uint64_t) a
->c
[0]
8317 | (uint64_t) a
->c
[1] << 8
8318 | (uint64_t) a
->c
[2] << 16
8319 | (uint64_t) a
->c
[3] << 24
8320 | (uint64_t) a
->c
[4] << 32
8321 | (uint64_t) a
->c
[5] << 40
8322 | (uint64_t) a
->c
[6] << 48
8323 | (uint64_t) a
->c
[7] << 56);
8328 ext64b_r_offset (const void *p
)
8335 const union aligned64
*a
8336 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8338 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8339 | (uint64_t) a
->c
[1] << 48
8340 | (uint64_t) a
->c
[2] << 40
8341 | (uint64_t) a
->c
[3] << 32
8342 | (uint64_t) a
->c
[4] << 24
8343 | (uint64_t) a
->c
[5] << 16
8344 | (uint64_t) a
->c
[6] << 8
8345 | (uint64_t) a
->c
[7]);
8350 /* When performing a relocatable link, the input relocations are
8351 preserved. But, if they reference global symbols, the indices
8352 referenced must be updated. Update all the relocations found in
8356 elf_link_adjust_relocs (bfd
*abfd
,
8357 struct bfd_elf_section_reloc_data
*reldata
,
8361 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8363 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8364 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8365 bfd_vma r_type_mask
;
8367 unsigned int count
= reldata
->count
;
8368 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8370 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8372 swap_in
= bed
->s
->swap_reloc_in
;
8373 swap_out
= bed
->s
->swap_reloc_out
;
8375 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8377 swap_in
= bed
->s
->swap_reloca_in
;
8378 swap_out
= bed
->s
->swap_reloca_out
;
8383 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8386 if (bed
->s
->arch_size
== 32)
8393 r_type_mask
= 0xffffffff;
8397 erela
= reldata
->hdr
->contents
;
8398 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8400 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8403 if (*rel_hash
== NULL
)
8406 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8408 (*swap_in
) (abfd
, erela
, irela
);
8409 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8410 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8411 | (irela
[j
].r_info
& r_type_mask
));
8412 (*swap_out
) (abfd
, irela
, erela
);
8415 if (sort
&& count
!= 0)
8417 bfd_vma (*ext_r_off
) (const void *);
8420 bfd_byte
*base
, *end
, *p
, *loc
;
8421 bfd_byte
*buf
= NULL
;
8423 if (bed
->s
->arch_size
== 32)
8425 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8426 ext_r_off
= ext32l_r_offset
;
8427 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8428 ext_r_off
= ext32b_r_offset
;
8434 #ifdef BFD_HOST_64_BIT
8435 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8436 ext_r_off
= ext64l_r_offset
;
8437 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8438 ext_r_off
= ext64b_r_offset
;
8444 /* Must use a stable sort here. A modified insertion sort,
8445 since the relocs are mostly sorted already. */
8446 elt_size
= reldata
->hdr
->sh_entsize
;
8447 base
= reldata
->hdr
->contents
;
8448 end
= base
+ count
* elt_size
;
8449 if (elt_size
> sizeof (Elf64_External_Rela
))
8452 /* Ensure the first element is lowest. This acts as a sentinel,
8453 speeding the main loop below. */
8454 r_off
= (*ext_r_off
) (base
);
8455 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8457 bfd_vma r_off2
= (*ext_r_off
) (p
);
8466 /* Don't just swap *base and *loc as that changes the order
8467 of the original base[0] and base[1] if they happen to
8468 have the same r_offset. */
8469 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8470 memcpy (onebuf
, loc
, elt_size
);
8471 memmove (base
+ elt_size
, base
, loc
- base
);
8472 memcpy (base
, onebuf
, elt_size
);
8475 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8477 /* base to p is sorted, *p is next to insert. */
8478 r_off
= (*ext_r_off
) (p
);
8479 /* Search the sorted region for location to insert. */
8481 while (r_off
< (*ext_r_off
) (loc
))
8486 /* Chances are there is a run of relocs to insert here,
8487 from one of more input files. Files are not always
8488 linked in order due to the way elf_link_input_bfd is
8489 called. See pr17666. */
8490 size_t sortlen
= p
- loc
;
8491 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8492 size_t runlen
= elt_size
;
8493 size_t buf_size
= 96 * 1024;
8494 while (p
+ runlen
< end
8495 && (sortlen
<= buf_size
8496 || runlen
+ elt_size
<= buf_size
)
8497 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8501 buf
= bfd_malloc (buf_size
);
8505 if (runlen
< sortlen
)
8507 memcpy (buf
, p
, runlen
);
8508 memmove (loc
+ runlen
, loc
, sortlen
);
8509 memcpy (loc
, buf
, runlen
);
8513 memcpy (buf
, loc
, sortlen
);
8514 memmove (loc
, p
, runlen
);
8515 memcpy (loc
+ runlen
, buf
, sortlen
);
8517 p
+= runlen
- elt_size
;
8520 /* Hashes are no longer valid. */
8521 free (reldata
->hashes
);
8522 reldata
->hashes
= NULL
;
8528 struct elf_link_sort_rela
8534 enum elf_reloc_type_class type
;
8535 /* We use this as an array of size int_rels_per_ext_rel. */
8536 Elf_Internal_Rela rela
[1];
8540 elf_link_sort_cmp1 (const void *A
, const void *B
)
8542 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8543 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8544 int relativea
, relativeb
;
8546 relativea
= a
->type
== reloc_class_relative
;
8547 relativeb
= b
->type
== reloc_class_relative
;
8549 if (relativea
< relativeb
)
8551 if (relativea
> relativeb
)
8553 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8555 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8557 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8559 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8565 elf_link_sort_cmp2 (const void *A
, const void *B
)
8567 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8568 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8570 if (a
->type
< b
->type
)
8572 if (a
->type
> b
->type
)
8574 if (a
->u
.offset
< b
->u
.offset
)
8576 if (a
->u
.offset
> b
->u
.offset
)
8578 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8580 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8586 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8588 asection
*dynamic_relocs
;
8591 bfd_size_type count
, size
;
8592 size_t i
, ret
, sort_elt
, ext_size
;
8593 bfd_byte
*sort
, *s_non_relative
, *p
;
8594 struct elf_link_sort_rela
*sq
;
8595 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8596 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8597 unsigned int opb
= bfd_octets_per_byte (abfd
);
8598 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8599 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8600 struct bfd_link_order
*lo
;
8602 bfd_boolean use_rela
;
8604 /* Find a dynamic reloc section. */
8605 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8606 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8607 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8608 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8610 bfd_boolean use_rela_initialised
= FALSE
;
8612 /* This is just here to stop gcc from complaining.
8613 Its initialization checking code is not perfect. */
8616 /* Both sections are present. Examine the sizes
8617 of the indirect sections to help us choose. */
8618 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8619 if (lo
->type
== bfd_indirect_link_order
)
8621 asection
*o
= lo
->u
.indirect
.section
;
8623 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8625 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8626 /* Section size is divisible by both rel and rela sizes.
8627 It is of no help to us. */
8631 /* Section size is only divisible by rela. */
8632 if (use_rela_initialised
&& (use_rela
== FALSE
))
8634 _bfd_error_handler (_("%B: Unable to sort relocs - "
8635 "they are in more than one size"),
8637 bfd_set_error (bfd_error_invalid_operation
);
8643 use_rela_initialised
= TRUE
;
8647 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8649 /* Section size is only divisible by rel. */
8650 if (use_rela_initialised
&& (use_rela
== TRUE
))
8652 _bfd_error_handler (_("%B: Unable to sort relocs - "
8653 "they are in more than one size"),
8655 bfd_set_error (bfd_error_invalid_operation
);
8661 use_rela_initialised
= TRUE
;
8666 /* The section size is not divisible by either -
8667 something is wrong. */
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are of an unknown size"), abfd
);
8670 bfd_set_error (bfd_error_invalid_operation
);
8675 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8676 if (lo
->type
== bfd_indirect_link_order
)
8678 asection
*o
= lo
->u
.indirect
.section
;
8680 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8682 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8683 /* Section size is divisible by both rel and rela sizes.
8684 It is of no help to us. */
8688 /* Section size is only divisible by rela. */
8689 if (use_rela_initialised
&& (use_rela
== FALSE
))
8691 _bfd_error_handler (_("%B: Unable to sort relocs - "
8692 "they are in more than one size"),
8694 bfd_set_error (bfd_error_invalid_operation
);
8700 use_rela_initialised
= TRUE
;
8704 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8706 /* Section size is only divisible by rel. */
8707 if (use_rela_initialised
&& (use_rela
== TRUE
))
8709 _bfd_error_handler (_("%B: Unable to sort relocs - "
8710 "they are in more than one size"),
8712 bfd_set_error (bfd_error_invalid_operation
);
8718 use_rela_initialised
= TRUE
;
8723 /* The section size is not divisible by either -
8724 something is wrong. */
8725 _bfd_error_handler (_("%B: Unable to sort relocs - "
8726 "they are of an unknown size"), abfd
);
8727 bfd_set_error (bfd_error_invalid_operation
);
8732 if (! use_rela_initialised
)
8736 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8738 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8745 dynamic_relocs
= rela_dyn
;
8746 ext_size
= bed
->s
->sizeof_rela
;
8747 swap_in
= bed
->s
->swap_reloca_in
;
8748 swap_out
= bed
->s
->swap_reloca_out
;
8752 dynamic_relocs
= rel_dyn
;
8753 ext_size
= bed
->s
->sizeof_rel
;
8754 swap_in
= bed
->s
->swap_reloc_in
;
8755 swap_out
= bed
->s
->swap_reloc_out
;
8759 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8760 if (lo
->type
== bfd_indirect_link_order
)
8761 size
+= lo
->u
.indirect
.section
->size
;
8763 if (size
!= dynamic_relocs
->size
)
8766 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8767 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8769 count
= dynamic_relocs
->size
/ ext_size
;
8772 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8776 (*info
->callbacks
->warning
)
8777 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8781 if (bed
->s
->arch_size
== 32)
8782 r_sym_mask
= ~(bfd_vma
) 0xff;
8784 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8786 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8787 if (lo
->type
== bfd_indirect_link_order
)
8789 bfd_byte
*erel
, *erelend
;
8790 asection
*o
= lo
->u
.indirect
.section
;
8792 if (o
->contents
== NULL
&& o
->size
!= 0)
8794 /* This is a reloc section that is being handled as a normal
8795 section. See bfd_section_from_shdr. We can't combine
8796 relocs in this case. */
8801 erelend
= o
->contents
+ o
->size
;
8802 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8804 while (erel
< erelend
)
8806 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8808 (*swap_in
) (abfd
, erel
, s
->rela
);
8809 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8810 s
->u
.sym_mask
= r_sym_mask
;
8816 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8818 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8820 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8821 if (s
->type
!= reloc_class_relative
)
8827 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8828 for (; i
< count
; i
++, p
+= sort_elt
)
8830 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8831 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8833 sp
->u
.offset
= sq
->rela
->r_offset
;
8836 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8838 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8839 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8841 /* We have plt relocs in .rela.dyn. */
8842 sq
= (struct elf_link_sort_rela
*) sort
;
8843 for (i
= 0; i
< count
; i
++)
8844 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8846 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8848 struct bfd_link_order
**plo
;
8849 /* Put srelplt link_order last. This is so the output_offset
8850 set in the next loop is correct for DT_JMPREL. */
8851 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8852 if ((*plo
)->type
== bfd_indirect_link_order
8853 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8859 plo
= &(*plo
)->next
;
8862 dynamic_relocs
->map_tail
.link_order
= lo
;
8867 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8868 if (lo
->type
== bfd_indirect_link_order
)
8870 bfd_byte
*erel
, *erelend
;
8871 asection
*o
= lo
->u
.indirect
.section
;
8874 erelend
= o
->contents
+ o
->size
;
8875 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8876 while (erel
< erelend
)
8878 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8879 (*swap_out
) (abfd
, s
->rela
, erel
);
8886 *psec
= dynamic_relocs
;
8890 /* Add a symbol to the output symbol string table. */
8893 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8895 Elf_Internal_Sym
*elfsym
,
8896 asection
*input_sec
,
8897 struct elf_link_hash_entry
*h
)
8899 int (*output_symbol_hook
)
8900 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8901 struct elf_link_hash_entry
*);
8902 struct elf_link_hash_table
*hash_table
;
8903 const struct elf_backend_data
*bed
;
8904 bfd_size_type strtabsize
;
8906 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8908 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8909 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8910 if (output_symbol_hook
!= NULL
)
8912 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8919 || (input_sec
->flags
& SEC_EXCLUDE
))
8920 elfsym
->st_name
= (unsigned long) -1;
8923 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8924 to get the final offset for st_name. */
8926 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8928 if (elfsym
->st_name
== (unsigned long) -1)
8932 hash_table
= elf_hash_table (flinfo
->info
);
8933 strtabsize
= hash_table
->strtabsize
;
8934 if (strtabsize
<= hash_table
->strtabcount
)
8936 strtabsize
+= strtabsize
;
8937 hash_table
->strtabsize
= strtabsize
;
8938 strtabsize
*= sizeof (*hash_table
->strtab
);
8940 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8942 if (hash_table
->strtab
== NULL
)
8945 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8946 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8947 = hash_table
->strtabcount
;
8948 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8949 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8951 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8952 hash_table
->strtabcount
+= 1;
8957 /* Swap symbols out to the symbol table and flush the output symbols to
8961 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8963 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8966 const struct elf_backend_data
*bed
;
8968 Elf_Internal_Shdr
*hdr
;
8972 if (!hash_table
->strtabcount
)
8975 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8977 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8979 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8980 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8984 if (flinfo
->symshndxbuf
)
8986 amt
= sizeof (Elf_External_Sym_Shndx
);
8987 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
8988 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
8989 if (flinfo
->symshndxbuf
== NULL
)
8996 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
8998 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
8999 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9000 elfsym
->sym
.st_name
= 0;
9003 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9004 elfsym
->sym
.st_name
);
9005 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9006 ((bfd_byte
*) symbuf
9007 + (elfsym
->dest_index
9008 * bed
->s
->sizeof_sym
)),
9009 (flinfo
->symshndxbuf
9010 + elfsym
->destshndx_index
));
9013 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9014 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9015 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9016 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9017 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9019 hdr
->sh_size
+= amt
;
9027 free (hash_table
->strtab
);
9028 hash_table
->strtab
= NULL
;
9033 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9036 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9038 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9039 && sym
->st_shndx
< SHN_LORESERVE
)
9041 /* The gABI doesn't support dynamic symbols in output sections
9043 (*_bfd_error_handler
)
9044 (_("%B: Too many sections: %d (>= %d)"),
9045 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9046 bfd_set_error (bfd_error_nonrepresentable_section
);
9052 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9053 allowing an unsatisfied unversioned symbol in the DSO to match a
9054 versioned symbol that would normally require an explicit version.
9055 We also handle the case that a DSO references a hidden symbol
9056 which may be satisfied by a versioned symbol in another DSO. */
9059 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9060 const struct elf_backend_data
*bed
,
9061 struct elf_link_hash_entry
*h
)
9064 struct elf_link_loaded_list
*loaded
;
9066 if (!is_elf_hash_table (info
->hash
))
9069 /* Check indirect symbol. */
9070 while (h
->root
.type
== bfd_link_hash_indirect
)
9071 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9073 switch (h
->root
.type
)
9079 case bfd_link_hash_undefined
:
9080 case bfd_link_hash_undefweak
:
9081 abfd
= h
->root
.u
.undef
.abfd
;
9083 || (abfd
->flags
& DYNAMIC
) == 0
9084 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9088 case bfd_link_hash_defined
:
9089 case bfd_link_hash_defweak
:
9090 abfd
= h
->root
.u
.def
.section
->owner
;
9093 case bfd_link_hash_common
:
9094 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9097 BFD_ASSERT (abfd
!= NULL
);
9099 for (loaded
= elf_hash_table (info
)->loaded
;
9101 loaded
= loaded
->next
)
9104 Elf_Internal_Shdr
*hdr
;
9108 Elf_Internal_Shdr
*versymhdr
;
9109 Elf_Internal_Sym
*isym
;
9110 Elf_Internal_Sym
*isymend
;
9111 Elf_Internal_Sym
*isymbuf
;
9112 Elf_External_Versym
*ever
;
9113 Elf_External_Versym
*extversym
;
9115 input
= loaded
->abfd
;
9117 /* We check each DSO for a possible hidden versioned definition. */
9119 || (input
->flags
& DYNAMIC
) == 0
9120 || elf_dynversym (input
) == 0)
9123 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9125 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9126 if (elf_bad_symtab (input
))
9128 extsymcount
= symcount
;
9133 extsymcount
= symcount
- hdr
->sh_info
;
9134 extsymoff
= hdr
->sh_info
;
9137 if (extsymcount
== 0)
9140 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9142 if (isymbuf
== NULL
)
9145 /* Read in any version definitions. */
9146 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9147 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9148 if (extversym
== NULL
)
9151 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9152 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9153 != versymhdr
->sh_size
))
9161 ever
= extversym
+ extsymoff
;
9162 isymend
= isymbuf
+ extsymcount
;
9163 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9166 Elf_Internal_Versym iver
;
9167 unsigned short version_index
;
9169 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9170 || isym
->st_shndx
== SHN_UNDEF
)
9173 name
= bfd_elf_string_from_elf_section (input
,
9176 if (strcmp (name
, h
->root
.root
.string
) != 0)
9179 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9181 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9183 && h
->forced_local
))
9185 /* If we have a non-hidden versioned sym, then it should
9186 have provided a definition for the undefined sym unless
9187 it is defined in a non-shared object and forced local.
9192 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9193 if (version_index
== 1 || version_index
== 2)
9195 /* This is the base or first version. We can use it. */
9209 /* Convert ELF common symbol TYPE. */
9212 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9214 /* Commom symbol can only appear in relocatable link. */
9215 if (!bfd_link_relocatable (info
))
9217 switch (info
->elf_stt_common
)
9221 case elf_stt_common
:
9224 case no_elf_stt_common
:
9231 /* Add an external symbol to the symbol table. This is called from
9232 the hash table traversal routine. When generating a shared object,
9233 we go through the symbol table twice. The first time we output
9234 anything that might have been forced to local scope in a version
9235 script. The second time we output the symbols that are still
9239 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9241 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9242 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9243 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9245 Elf_Internal_Sym sym
;
9246 asection
*input_sec
;
9247 const struct elf_backend_data
*bed
;
9251 /* A symbol is bound locally if it is forced local or it is locally
9252 defined, hidden versioned, not referenced by shared library and
9253 not exported when linking executable. */
9254 bfd_boolean local_bind
= (h
->forced_local
9255 || (bfd_link_executable (flinfo
->info
)
9256 && !flinfo
->info
->export_dynamic
9260 && h
->versioned
== versioned_hidden
));
9262 if (h
->root
.type
== bfd_link_hash_warning
)
9264 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9265 if (h
->root
.type
== bfd_link_hash_new
)
9269 /* Decide whether to output this symbol in this pass. */
9270 if (eoinfo
->localsyms
)
9281 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9283 if (h
->root
.type
== bfd_link_hash_undefined
)
9285 /* If we have an undefined symbol reference here then it must have
9286 come from a shared library that is being linked in. (Undefined
9287 references in regular files have already been handled unless
9288 they are in unreferenced sections which are removed by garbage
9290 bfd_boolean ignore_undef
= FALSE
;
9292 /* Some symbols may be special in that the fact that they're
9293 undefined can be safely ignored - let backend determine that. */
9294 if (bed
->elf_backend_ignore_undef_symbol
)
9295 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9297 /* If we are reporting errors for this situation then do so now. */
9300 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9301 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9302 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9303 (*flinfo
->info
->callbacks
->undefined_symbol
)
9304 (flinfo
->info
, h
->root
.root
.string
,
9305 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9307 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9309 /* Strip a global symbol defined in a discarded section. */
9314 /* We should also warn if a forced local symbol is referenced from
9315 shared libraries. */
9316 if (bfd_link_executable (flinfo
->info
)
9321 && h
->ref_dynamic_nonweak
9322 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9326 struct elf_link_hash_entry
*hi
= h
;
9328 /* Check indirect symbol. */
9329 while (hi
->root
.type
== bfd_link_hash_indirect
)
9330 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9332 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9333 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9334 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9335 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9337 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9338 def_bfd
= flinfo
->output_bfd
;
9339 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9340 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9341 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, def_bfd
,
9342 h
->root
.root
.string
);
9343 bfd_set_error (bfd_error_bad_value
);
9344 eoinfo
->failed
= TRUE
;
9348 /* We don't want to output symbols that have never been mentioned by
9349 a regular file, or that we have been told to strip. However, if
9350 h->indx is set to -2, the symbol is used by a reloc and we must
9355 else if ((h
->def_dynamic
9357 || h
->root
.type
== bfd_link_hash_new
)
9361 else if (flinfo
->info
->strip
== strip_all
)
9363 else if (flinfo
->info
->strip
== strip_some
9364 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9365 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9367 else if ((h
->root
.type
== bfd_link_hash_defined
9368 || h
->root
.type
== bfd_link_hash_defweak
)
9369 && ((flinfo
->info
->strip_discarded
9370 && discarded_section (h
->root
.u
.def
.section
))
9371 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9372 && h
->root
.u
.def
.section
->owner
!= NULL
9373 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9375 else if ((h
->root
.type
== bfd_link_hash_undefined
9376 || h
->root
.type
== bfd_link_hash_undefweak
)
9377 && h
->root
.u
.undef
.abfd
!= NULL
9378 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9383 /* If we're stripping it, and it's not a dynamic symbol, there's
9384 nothing else to do. However, if it is a forced local symbol or
9385 an ifunc symbol we need to give the backend finish_dynamic_symbol
9386 function a chance to make it dynamic. */
9389 && type
!= STT_GNU_IFUNC
9390 && !h
->forced_local
)
9394 sym
.st_size
= h
->size
;
9395 sym
.st_other
= h
->other
;
9396 switch (h
->root
.type
)
9399 case bfd_link_hash_new
:
9400 case bfd_link_hash_warning
:
9404 case bfd_link_hash_undefined
:
9405 case bfd_link_hash_undefweak
:
9406 input_sec
= bfd_und_section_ptr
;
9407 sym
.st_shndx
= SHN_UNDEF
;
9410 case bfd_link_hash_defined
:
9411 case bfd_link_hash_defweak
:
9413 input_sec
= h
->root
.u
.def
.section
;
9414 if (input_sec
->output_section
!= NULL
)
9417 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9418 input_sec
->output_section
);
9419 if (sym
.st_shndx
== SHN_BAD
)
9421 (*_bfd_error_handler
)
9422 (_("%B: could not find output section %A for input section %A"),
9423 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9424 bfd_set_error (bfd_error_nonrepresentable_section
);
9425 eoinfo
->failed
= TRUE
;
9429 /* ELF symbols in relocatable files are section relative,
9430 but in nonrelocatable files they are virtual
9432 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9433 if (!bfd_link_relocatable (flinfo
->info
))
9435 sym
.st_value
+= input_sec
->output_section
->vma
;
9436 if (h
->type
== STT_TLS
)
9438 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9439 if (tls_sec
!= NULL
)
9440 sym
.st_value
-= tls_sec
->vma
;
9446 BFD_ASSERT (input_sec
->owner
== NULL
9447 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9448 sym
.st_shndx
= SHN_UNDEF
;
9449 input_sec
= bfd_und_section_ptr
;
9454 case bfd_link_hash_common
:
9455 input_sec
= h
->root
.u
.c
.p
->section
;
9456 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9457 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9460 case bfd_link_hash_indirect
:
9461 /* These symbols are created by symbol versioning. They point
9462 to the decorated version of the name. For example, if the
9463 symbol foo@@GNU_1.2 is the default, which should be used when
9464 foo is used with no version, then we add an indirect symbol
9465 foo which points to foo@@GNU_1.2. We ignore these symbols,
9466 since the indirected symbol is already in the hash table. */
9470 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9471 switch (h
->root
.type
)
9473 case bfd_link_hash_common
:
9474 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9476 case bfd_link_hash_defined
:
9477 case bfd_link_hash_defweak
:
9478 if (bed
->common_definition (&sym
))
9479 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9483 case bfd_link_hash_undefined
:
9484 case bfd_link_hash_undefweak
:
9492 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9493 /* Turn off visibility on local symbol. */
9494 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9496 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9497 else if (h
->unique_global
&& h
->def_regular
)
9498 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9499 else if (h
->root
.type
== bfd_link_hash_undefweak
9500 || h
->root
.type
== bfd_link_hash_defweak
)
9501 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9503 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9504 sym
.st_target_internal
= h
->target_internal
;
9506 /* Give the processor backend a chance to tweak the symbol value,
9507 and also to finish up anything that needs to be done for this
9508 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9509 forced local syms when non-shared is due to a historical quirk.
9510 STT_GNU_IFUNC symbol must go through PLT. */
9511 if ((h
->type
== STT_GNU_IFUNC
9513 && !bfd_link_relocatable (flinfo
->info
))
9514 || ((h
->dynindx
!= -1
9516 && ((bfd_link_pic (flinfo
->info
)
9517 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9518 || h
->root
.type
!= bfd_link_hash_undefweak
))
9519 || !h
->forced_local
)
9520 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9522 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9523 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9525 eoinfo
->failed
= TRUE
;
9530 /* If we are marking the symbol as undefined, and there are no
9531 non-weak references to this symbol from a regular object, then
9532 mark the symbol as weak undefined; if there are non-weak
9533 references, mark the symbol as strong. We can't do this earlier,
9534 because it might not be marked as undefined until the
9535 finish_dynamic_symbol routine gets through with it. */
9536 if (sym
.st_shndx
== SHN_UNDEF
9538 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9539 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9542 type
= ELF_ST_TYPE (sym
.st_info
);
9544 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9545 if (type
== STT_GNU_IFUNC
)
9548 if (h
->ref_regular_nonweak
)
9549 bindtype
= STB_GLOBAL
;
9551 bindtype
= STB_WEAK
;
9552 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9555 /* If this is a symbol defined in a dynamic library, don't use the
9556 symbol size from the dynamic library. Relinking an executable
9557 against a new library may introduce gratuitous changes in the
9558 executable's symbols if we keep the size. */
9559 if (sym
.st_shndx
== SHN_UNDEF
9564 /* If a non-weak symbol with non-default visibility is not defined
9565 locally, it is a fatal error. */
9566 if (!bfd_link_relocatable (flinfo
->info
)
9567 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9568 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9569 && h
->root
.type
== bfd_link_hash_undefined
9574 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9575 msg
= _("%B: protected symbol `%s' isn't defined");
9576 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9577 msg
= _("%B: internal symbol `%s' isn't defined");
9579 msg
= _("%B: hidden symbol `%s' isn't defined");
9580 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9581 bfd_set_error (bfd_error_bad_value
);
9582 eoinfo
->failed
= TRUE
;
9586 /* If this symbol should be put in the .dynsym section, then put it
9587 there now. We already know the symbol index. We also fill in
9588 the entry in the .hash section. */
9589 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9591 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9595 /* Since there is no version information in the dynamic string,
9596 if there is no version info in symbol version section, we will
9597 have a run-time problem if not linking executable, referenced
9598 by shared library, not locally defined, or not bound locally.
9600 if (h
->verinfo
.verdef
== NULL
9602 && (!bfd_link_executable (flinfo
->info
)
9604 || !h
->def_regular
))
9606 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9608 if (p
&& p
[1] != '\0')
9610 (*_bfd_error_handler
)
9611 (_("%B: No symbol version section for versioned symbol `%s'"),
9612 flinfo
->output_bfd
, h
->root
.root
.string
);
9613 eoinfo
->failed
= TRUE
;
9618 sym
.st_name
= h
->dynstr_index
;
9619 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9620 + h
->dynindx
* bed
->s
->sizeof_sym
);
9621 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9623 eoinfo
->failed
= TRUE
;
9626 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9628 if (flinfo
->hash_sec
!= NULL
)
9630 size_t hash_entry_size
;
9631 bfd_byte
*bucketpos
;
9636 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9637 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9640 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9641 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9642 + (bucket
+ 2) * hash_entry_size
);
9643 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9644 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9646 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9647 ((bfd_byte
*) flinfo
->hash_sec
->contents
9648 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9651 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9653 Elf_Internal_Versym iversym
;
9654 Elf_External_Versym
*eversym
;
9656 if (!h
->def_regular
)
9658 if (h
->verinfo
.verdef
== NULL
9659 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9660 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9661 iversym
.vs_vers
= 0;
9663 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9667 if (h
->verinfo
.vertree
== NULL
)
9668 iversym
.vs_vers
= 1;
9670 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9671 if (flinfo
->info
->create_default_symver
)
9675 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9677 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9678 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9680 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9681 eversym
+= h
->dynindx
;
9682 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9686 /* If the symbol is undefined, and we didn't output it to .dynsym,
9687 strip it from .symtab too. Obviously we can't do this for
9688 relocatable output or when needed for --emit-relocs. */
9689 else if (input_sec
== bfd_und_section_ptr
9691 && !bfd_link_relocatable (flinfo
->info
))
9693 /* Also strip others that we couldn't earlier due to dynamic symbol
9697 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9700 /* Output a FILE symbol so that following locals are not associated
9701 with the wrong input file. We need one for forced local symbols
9702 if we've seen more than one FILE symbol or when we have exactly
9703 one FILE symbol but global symbols are present in a file other
9704 than the one with the FILE symbol. We also need one if linker
9705 defined symbols are present. In practice these conditions are
9706 always met, so just emit the FILE symbol unconditionally. */
9707 if (eoinfo
->localsyms
9708 && !eoinfo
->file_sym_done
9709 && eoinfo
->flinfo
->filesym_count
!= 0)
9711 Elf_Internal_Sym fsym
;
9713 memset (&fsym
, 0, sizeof (fsym
));
9714 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9715 fsym
.st_shndx
= SHN_ABS
;
9716 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9717 bfd_und_section_ptr
, NULL
))
9720 eoinfo
->file_sym_done
= TRUE
;
9723 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9724 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9728 eoinfo
->failed
= TRUE
;
9733 else if (h
->indx
== -2)
9739 /* Return TRUE if special handling is done for relocs in SEC against
9740 symbols defined in discarded sections. */
9743 elf_section_ignore_discarded_relocs (asection
*sec
)
9745 const struct elf_backend_data
*bed
;
9747 switch (sec
->sec_info_type
)
9749 case SEC_INFO_TYPE_STABS
:
9750 case SEC_INFO_TYPE_EH_FRAME
:
9751 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9757 bed
= get_elf_backend_data (sec
->owner
);
9758 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9759 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9765 /* Return a mask saying how ld should treat relocations in SEC against
9766 symbols defined in discarded sections. If this function returns
9767 COMPLAIN set, ld will issue a warning message. If this function
9768 returns PRETEND set, and the discarded section was link-once and the
9769 same size as the kept link-once section, ld will pretend that the
9770 symbol was actually defined in the kept section. Otherwise ld will
9771 zero the reloc (at least that is the intent, but some cooperation by
9772 the target dependent code is needed, particularly for REL targets). */
9775 _bfd_elf_default_action_discarded (asection
*sec
)
9777 if (sec
->flags
& SEC_DEBUGGING
)
9780 if (strcmp (".eh_frame", sec
->name
) == 0)
9783 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9786 return COMPLAIN
| PRETEND
;
9789 /* Find a match between a section and a member of a section group. */
9792 match_group_member (asection
*sec
, asection
*group
,
9793 struct bfd_link_info
*info
)
9795 asection
*first
= elf_next_in_group (group
);
9796 asection
*s
= first
;
9800 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9803 s
= elf_next_in_group (s
);
9811 /* Check if the kept section of a discarded section SEC can be used
9812 to replace it. Return the replacement if it is OK. Otherwise return
9816 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9820 kept
= sec
->kept_section
;
9823 if ((kept
->flags
& SEC_GROUP
) != 0)
9824 kept
= match_group_member (sec
, kept
, info
);
9826 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9827 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9829 sec
->kept_section
= kept
;
9834 /* Link an input file into the linker output file. This function
9835 handles all the sections and relocations of the input file at once.
9836 This is so that we only have to read the local symbols once, and
9837 don't have to keep them in memory. */
9840 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9842 int (*relocate_section
)
9843 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9844 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9846 Elf_Internal_Shdr
*symtab_hdr
;
9849 Elf_Internal_Sym
*isymbuf
;
9850 Elf_Internal_Sym
*isym
;
9851 Elf_Internal_Sym
*isymend
;
9853 asection
**ppsection
;
9855 const struct elf_backend_data
*bed
;
9856 struct elf_link_hash_entry
**sym_hashes
;
9857 bfd_size_type address_size
;
9858 bfd_vma r_type_mask
;
9860 bfd_boolean have_file_sym
= FALSE
;
9862 output_bfd
= flinfo
->output_bfd
;
9863 bed
= get_elf_backend_data (output_bfd
);
9864 relocate_section
= bed
->elf_backend_relocate_section
;
9866 /* If this is a dynamic object, we don't want to do anything here:
9867 we don't want the local symbols, and we don't want the section
9869 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9872 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9873 if (elf_bad_symtab (input_bfd
))
9875 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9880 locsymcount
= symtab_hdr
->sh_info
;
9881 extsymoff
= symtab_hdr
->sh_info
;
9884 /* Read the local symbols. */
9885 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9886 if (isymbuf
== NULL
&& locsymcount
!= 0)
9888 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9889 flinfo
->internal_syms
,
9890 flinfo
->external_syms
,
9891 flinfo
->locsym_shndx
);
9892 if (isymbuf
== NULL
)
9896 /* Find local symbol sections and adjust values of symbols in
9897 SEC_MERGE sections. Write out those local symbols we know are
9898 going into the output file. */
9899 isymend
= isymbuf
+ locsymcount
;
9900 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9902 isym
++, pindex
++, ppsection
++)
9906 Elf_Internal_Sym osym
;
9912 if (elf_bad_symtab (input_bfd
))
9914 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9921 if (isym
->st_shndx
== SHN_UNDEF
)
9922 isec
= bfd_und_section_ptr
;
9923 else if (isym
->st_shndx
== SHN_ABS
)
9924 isec
= bfd_abs_section_ptr
;
9925 else if (isym
->st_shndx
== SHN_COMMON
)
9926 isec
= bfd_com_section_ptr
;
9929 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9932 /* Don't attempt to output symbols with st_shnx in the
9933 reserved range other than SHN_ABS and SHN_COMMON. */
9937 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9938 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9940 _bfd_merged_section_offset (output_bfd
, &isec
,
9941 elf_section_data (isec
)->sec_info
,
9947 /* Don't output the first, undefined, symbol. In fact, don't
9948 output any undefined local symbol. */
9949 if (isec
== bfd_und_section_ptr
)
9952 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9954 /* We never output section symbols. Instead, we use the
9955 section symbol of the corresponding section in the output
9960 /* If we are stripping all symbols, we don't want to output this
9962 if (flinfo
->info
->strip
== strip_all
)
9965 /* If we are discarding all local symbols, we don't want to
9966 output this one. If we are generating a relocatable output
9967 file, then some of the local symbols may be required by
9968 relocs; we output them below as we discover that they are
9970 if (flinfo
->info
->discard
== discard_all
)
9973 /* If this symbol is defined in a section which we are
9974 discarding, we don't need to keep it. */
9975 if (isym
->st_shndx
!= SHN_UNDEF
9976 && isym
->st_shndx
< SHN_LORESERVE
9977 && bfd_section_removed_from_list (output_bfd
,
9978 isec
->output_section
))
9981 /* Get the name of the symbol. */
9982 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9987 /* See if we are discarding symbols with this name. */
9988 if ((flinfo
->info
->strip
== strip_some
9989 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9991 || (((flinfo
->info
->discard
== discard_sec_merge
9992 && (isec
->flags
& SEC_MERGE
)
9993 && !bfd_link_relocatable (flinfo
->info
))
9994 || flinfo
->info
->discard
== discard_l
)
9995 && bfd_is_local_label_name (input_bfd
, name
)))
9998 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10000 if (input_bfd
->lto_output
)
10001 /* -flto puts a temp file name here. This means builds
10002 are not reproducible. Discard the symbol. */
10004 have_file_sym
= TRUE
;
10005 flinfo
->filesym_count
+= 1;
10007 if (!have_file_sym
)
10009 /* In the absence of debug info, bfd_find_nearest_line uses
10010 FILE symbols to determine the source file for local
10011 function symbols. Provide a FILE symbol here if input
10012 files lack such, so that their symbols won't be
10013 associated with a previous input file. It's not the
10014 source file, but the best we can do. */
10015 have_file_sym
= TRUE
;
10016 flinfo
->filesym_count
+= 1;
10017 memset (&osym
, 0, sizeof (osym
));
10018 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10019 osym
.st_shndx
= SHN_ABS
;
10020 if (!elf_link_output_symstrtab (flinfo
,
10021 (input_bfd
->lto_output
? NULL
10022 : input_bfd
->filename
),
10023 &osym
, bfd_abs_section_ptr
,
10030 /* Adjust the section index for the output file. */
10031 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10032 isec
->output_section
);
10033 if (osym
.st_shndx
== SHN_BAD
)
10036 /* ELF symbols in relocatable files are section relative, but
10037 in executable files they are virtual addresses. Note that
10038 this code assumes that all ELF sections have an associated
10039 BFD section with a reasonable value for output_offset; below
10040 we assume that they also have a reasonable value for
10041 output_section. Any special sections must be set up to meet
10042 these requirements. */
10043 osym
.st_value
+= isec
->output_offset
;
10044 if (!bfd_link_relocatable (flinfo
->info
))
10046 osym
.st_value
+= isec
->output_section
->vma
;
10047 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10049 /* STT_TLS symbols are relative to PT_TLS segment base. */
10050 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10051 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10055 indx
= bfd_get_symcount (output_bfd
);
10056 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10063 if (bed
->s
->arch_size
== 32)
10065 r_type_mask
= 0xff;
10071 r_type_mask
= 0xffffffff;
10076 /* Relocate the contents of each section. */
10077 sym_hashes
= elf_sym_hashes (input_bfd
);
10078 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10080 bfd_byte
*contents
;
10082 if (! o
->linker_mark
)
10084 /* This section was omitted from the link. */
10088 if (bfd_link_relocatable (flinfo
->info
)
10089 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10091 /* Deal with the group signature symbol. */
10092 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10093 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10094 asection
*osec
= o
->output_section
;
10096 if (symndx
>= locsymcount
10097 || (elf_bad_symtab (input_bfd
)
10098 && flinfo
->sections
[symndx
] == NULL
))
10100 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10101 while (h
->root
.type
== bfd_link_hash_indirect
10102 || h
->root
.type
== bfd_link_hash_warning
)
10103 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10104 /* Arrange for symbol to be output. */
10106 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10108 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10110 /* We'll use the output section target_index. */
10111 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10112 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10116 if (flinfo
->indices
[symndx
] == -1)
10118 /* Otherwise output the local symbol now. */
10119 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10120 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10125 name
= bfd_elf_string_from_elf_section (input_bfd
,
10126 symtab_hdr
->sh_link
,
10131 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10133 if (sym
.st_shndx
== SHN_BAD
)
10136 sym
.st_value
+= o
->output_offset
;
10138 indx
= bfd_get_symcount (output_bfd
);
10139 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10144 flinfo
->indices
[symndx
] = indx
;
10148 elf_section_data (osec
)->this_hdr
.sh_info
10149 = flinfo
->indices
[symndx
];
10153 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10154 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10157 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10159 /* Section was created by _bfd_elf_link_create_dynamic_sections
10164 /* Get the contents of the section. They have been cached by a
10165 relaxation routine. Note that o is a section in an input
10166 file, so the contents field will not have been set by any of
10167 the routines which work on output files. */
10168 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10170 contents
= elf_section_data (o
)->this_hdr
.contents
;
10171 if (bed
->caches_rawsize
10173 && o
->rawsize
< o
->size
)
10175 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10176 contents
= flinfo
->contents
;
10181 contents
= flinfo
->contents
;
10182 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10186 if ((o
->flags
& SEC_RELOC
) != 0)
10188 Elf_Internal_Rela
*internal_relocs
;
10189 Elf_Internal_Rela
*rel
, *relend
;
10190 int action_discarded
;
10193 /* Get the swapped relocs. */
10195 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10196 flinfo
->internal_relocs
, FALSE
);
10197 if (internal_relocs
== NULL
10198 && o
->reloc_count
> 0)
10201 /* We need to reverse-copy input .ctors/.dtors sections if
10202 they are placed in .init_array/.finit_array for output. */
10203 if (o
->size
> address_size
10204 && ((strncmp (o
->name
, ".ctors", 6) == 0
10205 && strcmp (o
->output_section
->name
,
10206 ".init_array") == 0)
10207 || (strncmp (o
->name
, ".dtors", 6) == 0
10208 && strcmp (o
->output_section
->name
,
10209 ".fini_array") == 0))
10210 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10212 if (o
->size
!= o
->reloc_count
* address_size
)
10214 (*_bfd_error_handler
)
10215 (_("error: %B: size of section %A is not "
10216 "multiple of address size"),
10218 bfd_set_error (bfd_error_on_input
);
10221 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10224 action_discarded
= -1;
10225 if (!elf_section_ignore_discarded_relocs (o
))
10226 action_discarded
= (*bed
->action_discarded
) (o
);
10228 /* Run through the relocs evaluating complex reloc symbols and
10229 looking for relocs against symbols from discarded sections
10230 or section symbols from removed link-once sections.
10231 Complain about relocs against discarded sections. Zero
10232 relocs against removed link-once sections. */
10234 rel
= internal_relocs
;
10235 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10236 for ( ; rel
< relend
; rel
++)
10238 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10239 unsigned int s_type
;
10240 asection
**ps
, *sec
;
10241 struct elf_link_hash_entry
*h
= NULL
;
10242 const char *sym_name
;
10244 if (r_symndx
== STN_UNDEF
)
10247 if (r_symndx
>= locsymcount
10248 || (elf_bad_symtab (input_bfd
)
10249 && flinfo
->sections
[r_symndx
] == NULL
))
10251 h
= sym_hashes
[r_symndx
- extsymoff
];
10253 /* Badly formatted input files can contain relocs that
10254 reference non-existant symbols. Check here so that
10255 we do not seg fault. */
10260 sprintf_vma (buffer
, rel
->r_info
);
10261 (*_bfd_error_handler
)
10262 (_("error: %B contains a reloc (0x%s) for section %A "
10263 "that references a non-existent global symbol"),
10264 input_bfd
, o
, buffer
);
10265 bfd_set_error (bfd_error_bad_value
);
10269 while (h
->root
.type
== bfd_link_hash_indirect
10270 || h
->root
.type
== bfd_link_hash_warning
)
10271 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10275 /* If a plugin symbol is referenced from a non-IR file,
10276 mark the symbol as undefined. Note that the
10277 linker may attach linker created dynamic sections
10278 to the plugin bfd. Symbols defined in linker
10279 created sections are not plugin symbols. */
10280 if (h
->root
.non_ir_ref
10281 && (h
->root
.type
== bfd_link_hash_defined
10282 || h
->root
.type
== bfd_link_hash_defweak
)
10283 && (h
->root
.u
.def
.section
->flags
10284 & SEC_LINKER_CREATED
) == 0
10285 && h
->root
.u
.def
.section
->owner
!= NULL
10286 && (h
->root
.u
.def
.section
->owner
->flags
10287 & BFD_PLUGIN
) != 0)
10289 h
->root
.type
= bfd_link_hash_undefined
;
10290 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10294 if (h
->root
.type
== bfd_link_hash_defined
10295 || h
->root
.type
== bfd_link_hash_defweak
)
10296 ps
= &h
->root
.u
.def
.section
;
10298 sym_name
= h
->root
.root
.string
;
10302 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10304 s_type
= ELF_ST_TYPE (sym
->st_info
);
10305 ps
= &flinfo
->sections
[r_symndx
];
10306 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10310 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10311 && !bfd_link_relocatable (flinfo
->info
))
10314 bfd_vma dot
= (rel
->r_offset
10315 + o
->output_offset
+ o
->output_section
->vma
);
10317 printf ("Encountered a complex symbol!");
10318 printf (" (input_bfd %s, section %s, reloc %ld\n",
10319 input_bfd
->filename
, o
->name
,
10320 (long) (rel
- internal_relocs
));
10321 printf (" symbol: idx %8.8lx, name %s\n",
10322 r_symndx
, sym_name
);
10323 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10324 (unsigned long) rel
->r_info
,
10325 (unsigned long) rel
->r_offset
);
10327 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10328 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10331 /* Symbol evaluated OK. Update to absolute value. */
10332 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10337 if (action_discarded
!= -1 && ps
!= NULL
)
10339 /* Complain if the definition comes from a
10340 discarded section. */
10341 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10343 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10344 if (action_discarded
& COMPLAIN
)
10345 (*flinfo
->info
->callbacks
->einfo
)
10346 (_("%X`%s' referenced in section `%A' of %B: "
10347 "defined in discarded section `%A' of %B\n"),
10348 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10350 /* Try to do the best we can to support buggy old
10351 versions of gcc. Pretend that the symbol is
10352 really defined in the kept linkonce section.
10353 FIXME: This is quite broken. Modifying the
10354 symbol here means we will be changing all later
10355 uses of the symbol, not just in this section. */
10356 if (action_discarded
& PRETEND
)
10360 kept
= _bfd_elf_check_kept_section (sec
,
10372 /* Relocate the section by invoking a back end routine.
10374 The back end routine is responsible for adjusting the
10375 section contents as necessary, and (if using Rela relocs
10376 and generating a relocatable output file) adjusting the
10377 reloc addend as necessary.
10379 The back end routine does not have to worry about setting
10380 the reloc address or the reloc symbol index.
10382 The back end routine is given a pointer to the swapped in
10383 internal symbols, and can access the hash table entries
10384 for the external symbols via elf_sym_hashes (input_bfd).
10386 When generating relocatable output, the back end routine
10387 must handle STB_LOCAL/STT_SECTION symbols specially. The
10388 output symbol is going to be a section symbol
10389 corresponding to the output section, which will require
10390 the addend to be adjusted. */
10392 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10393 input_bfd
, o
, contents
,
10401 || bfd_link_relocatable (flinfo
->info
)
10402 || flinfo
->info
->emitrelocations
)
10404 Elf_Internal_Rela
*irela
;
10405 Elf_Internal_Rela
*irelaend
, *irelamid
;
10406 bfd_vma last_offset
;
10407 struct elf_link_hash_entry
**rel_hash
;
10408 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10409 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10410 unsigned int next_erel
;
10411 bfd_boolean rela_normal
;
10412 struct bfd_elf_section_data
*esdi
, *esdo
;
10414 esdi
= elf_section_data (o
);
10415 esdo
= elf_section_data (o
->output_section
);
10416 rela_normal
= FALSE
;
10418 /* Adjust the reloc addresses and symbol indices. */
10420 irela
= internal_relocs
;
10421 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10422 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10423 /* We start processing the REL relocs, if any. When we reach
10424 IRELAMID in the loop, we switch to the RELA relocs. */
10426 if (esdi
->rel
.hdr
!= NULL
)
10427 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10428 * bed
->s
->int_rels_per_ext_rel
);
10429 rel_hash_list
= rel_hash
;
10430 rela_hash_list
= NULL
;
10431 last_offset
= o
->output_offset
;
10432 if (!bfd_link_relocatable (flinfo
->info
))
10433 last_offset
+= o
->output_section
->vma
;
10434 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10436 unsigned long r_symndx
;
10438 Elf_Internal_Sym sym
;
10440 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10446 if (irela
== irelamid
)
10448 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10449 rela_hash_list
= rel_hash
;
10450 rela_normal
= bed
->rela_normal
;
10453 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10456 if (irela
->r_offset
>= (bfd_vma
) -2)
10458 /* This is a reloc for a deleted entry or somesuch.
10459 Turn it into an R_*_NONE reloc, at the same
10460 offset as the last reloc. elf_eh_frame.c and
10461 bfd_elf_discard_info rely on reloc offsets
10463 irela
->r_offset
= last_offset
;
10465 irela
->r_addend
= 0;
10469 irela
->r_offset
+= o
->output_offset
;
10471 /* Relocs in an executable have to be virtual addresses. */
10472 if (!bfd_link_relocatable (flinfo
->info
))
10473 irela
->r_offset
+= o
->output_section
->vma
;
10475 last_offset
= irela
->r_offset
;
10477 r_symndx
= irela
->r_info
>> r_sym_shift
;
10478 if (r_symndx
== STN_UNDEF
)
10481 if (r_symndx
>= locsymcount
10482 || (elf_bad_symtab (input_bfd
)
10483 && flinfo
->sections
[r_symndx
] == NULL
))
10485 struct elf_link_hash_entry
*rh
;
10486 unsigned long indx
;
10488 /* This is a reloc against a global symbol. We
10489 have not yet output all the local symbols, so
10490 we do not know the symbol index of any global
10491 symbol. We set the rel_hash entry for this
10492 reloc to point to the global hash table entry
10493 for this symbol. The symbol index is then
10494 set at the end of bfd_elf_final_link. */
10495 indx
= r_symndx
- extsymoff
;
10496 rh
= elf_sym_hashes (input_bfd
)[indx
];
10497 while (rh
->root
.type
== bfd_link_hash_indirect
10498 || rh
->root
.type
== bfd_link_hash_warning
)
10499 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10501 /* Setting the index to -2 tells
10502 elf_link_output_extsym that this symbol is
10503 used by a reloc. */
10504 BFD_ASSERT (rh
->indx
< 0);
10512 /* This is a reloc against a local symbol. */
10515 sym
= isymbuf
[r_symndx
];
10516 sec
= flinfo
->sections
[r_symndx
];
10517 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10519 /* I suppose the backend ought to fill in the
10520 section of any STT_SECTION symbol against a
10521 processor specific section. */
10522 r_symndx
= STN_UNDEF
;
10523 if (bfd_is_abs_section (sec
))
10525 else if (sec
== NULL
|| sec
->owner
== NULL
)
10527 bfd_set_error (bfd_error_bad_value
);
10532 asection
*osec
= sec
->output_section
;
10534 /* If we have discarded a section, the output
10535 section will be the absolute section. In
10536 case of discarded SEC_MERGE sections, use
10537 the kept section. relocate_section should
10538 have already handled discarded linkonce
10540 if (bfd_is_abs_section (osec
)
10541 && sec
->kept_section
!= NULL
10542 && sec
->kept_section
->output_section
!= NULL
)
10544 osec
= sec
->kept_section
->output_section
;
10545 irela
->r_addend
-= osec
->vma
;
10548 if (!bfd_is_abs_section (osec
))
10550 r_symndx
= osec
->target_index
;
10551 if (r_symndx
== STN_UNDEF
)
10553 irela
->r_addend
+= osec
->vma
;
10554 osec
= _bfd_nearby_section (output_bfd
, osec
,
10556 irela
->r_addend
-= osec
->vma
;
10557 r_symndx
= osec
->target_index
;
10562 /* Adjust the addend according to where the
10563 section winds up in the output section. */
10565 irela
->r_addend
+= sec
->output_offset
;
10569 if (flinfo
->indices
[r_symndx
] == -1)
10571 unsigned long shlink
;
10576 if (flinfo
->info
->strip
== strip_all
)
10578 /* You can't do ld -r -s. */
10579 bfd_set_error (bfd_error_invalid_operation
);
10583 /* This symbol was skipped earlier, but
10584 since it is needed by a reloc, we
10585 must output it now. */
10586 shlink
= symtab_hdr
->sh_link
;
10587 name
= (bfd_elf_string_from_elf_section
10588 (input_bfd
, shlink
, sym
.st_name
));
10592 osec
= sec
->output_section
;
10594 _bfd_elf_section_from_bfd_section (output_bfd
,
10596 if (sym
.st_shndx
== SHN_BAD
)
10599 sym
.st_value
+= sec
->output_offset
;
10600 if (!bfd_link_relocatable (flinfo
->info
))
10602 sym
.st_value
+= osec
->vma
;
10603 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10605 /* STT_TLS symbols are relative to PT_TLS
10607 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10608 ->tls_sec
!= NULL
);
10609 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10614 indx
= bfd_get_symcount (output_bfd
);
10615 ret
= elf_link_output_symstrtab (flinfo
, name
,
10621 flinfo
->indices
[r_symndx
] = indx
;
10626 r_symndx
= flinfo
->indices
[r_symndx
];
10629 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10630 | (irela
->r_info
& r_type_mask
));
10633 /* Swap out the relocs. */
10634 input_rel_hdr
= esdi
->rel
.hdr
;
10635 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10637 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10642 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10643 * bed
->s
->int_rels_per_ext_rel
);
10644 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10647 input_rela_hdr
= esdi
->rela
.hdr
;
10648 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10650 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10659 /* Write out the modified section contents. */
10660 if (bed
->elf_backend_write_section
10661 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10664 /* Section written out. */
10666 else switch (o
->sec_info_type
)
10668 case SEC_INFO_TYPE_STABS
:
10669 if (! (_bfd_write_section_stabs
10671 &elf_hash_table (flinfo
->info
)->stab_info
,
10672 o
, &elf_section_data (o
)->sec_info
, contents
)))
10675 case SEC_INFO_TYPE_MERGE
:
10676 if (! _bfd_write_merged_section (output_bfd
, o
,
10677 elf_section_data (o
)->sec_info
))
10680 case SEC_INFO_TYPE_EH_FRAME
:
10682 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10687 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10689 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10697 if (! (o
->flags
& SEC_EXCLUDE
))
10699 file_ptr offset
= (file_ptr
) o
->output_offset
;
10700 bfd_size_type todo
= o
->size
;
10702 offset
*= bfd_octets_per_byte (output_bfd
);
10704 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10706 /* Reverse-copy input section to output. */
10709 todo
-= address_size
;
10710 if (! bfd_set_section_contents (output_bfd
,
10718 offset
+= address_size
;
10722 else if (! bfd_set_section_contents (output_bfd
,
10736 /* Generate a reloc when linking an ELF file. This is a reloc
10737 requested by the linker, and does not come from any input file. This
10738 is used to build constructor and destructor tables when linking
10742 elf_reloc_link_order (bfd
*output_bfd
,
10743 struct bfd_link_info
*info
,
10744 asection
*output_section
,
10745 struct bfd_link_order
*link_order
)
10747 reloc_howto_type
*howto
;
10751 struct bfd_elf_section_reloc_data
*reldata
;
10752 struct elf_link_hash_entry
**rel_hash_ptr
;
10753 Elf_Internal_Shdr
*rel_hdr
;
10754 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10755 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10758 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10760 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10763 bfd_set_error (bfd_error_bad_value
);
10767 addend
= link_order
->u
.reloc
.p
->addend
;
10770 reldata
= &esdo
->rel
;
10771 else if (esdo
->rela
.hdr
)
10772 reldata
= &esdo
->rela
;
10779 /* Figure out the symbol index. */
10780 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10781 if (link_order
->type
== bfd_section_reloc_link_order
)
10783 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10784 BFD_ASSERT (indx
!= 0);
10785 *rel_hash_ptr
= NULL
;
10789 struct elf_link_hash_entry
*h
;
10791 /* Treat a reloc against a defined symbol as though it were
10792 actually against the section. */
10793 h
= ((struct elf_link_hash_entry
*)
10794 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10795 link_order
->u
.reloc
.p
->u
.name
,
10796 FALSE
, FALSE
, TRUE
));
10798 && (h
->root
.type
== bfd_link_hash_defined
10799 || h
->root
.type
== bfd_link_hash_defweak
))
10803 section
= h
->root
.u
.def
.section
;
10804 indx
= section
->output_section
->target_index
;
10805 *rel_hash_ptr
= NULL
;
10806 /* It seems that we ought to add the symbol value to the
10807 addend here, but in practice it has already been added
10808 because it was passed to constructor_callback. */
10809 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10811 else if (h
!= NULL
)
10813 /* Setting the index to -2 tells elf_link_output_extsym that
10814 this symbol is used by a reloc. */
10821 (*info
->callbacks
->unattached_reloc
)
10822 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10827 /* If this is an inplace reloc, we must write the addend into the
10829 if (howto
->partial_inplace
&& addend
!= 0)
10831 bfd_size_type size
;
10832 bfd_reloc_status_type rstat
;
10835 const char *sym_name
;
10837 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10838 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10839 if (buf
== NULL
&& size
!= 0)
10841 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10848 case bfd_reloc_outofrange
:
10851 case bfd_reloc_overflow
:
10852 if (link_order
->type
== bfd_section_reloc_link_order
)
10853 sym_name
= bfd_section_name (output_bfd
,
10854 link_order
->u
.reloc
.p
->u
.section
);
10856 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10857 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10858 howto
->name
, addend
, NULL
, NULL
,
10863 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10865 * bfd_octets_per_byte (output_bfd
),
10872 /* The address of a reloc is relative to the section in a
10873 relocatable file, and is a virtual address in an executable
10875 offset
= link_order
->offset
;
10876 if (! bfd_link_relocatable (info
))
10877 offset
+= output_section
->vma
;
10879 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10881 irel
[i
].r_offset
= offset
;
10882 irel
[i
].r_info
= 0;
10883 irel
[i
].r_addend
= 0;
10885 if (bed
->s
->arch_size
== 32)
10886 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10888 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10890 rel_hdr
= reldata
->hdr
;
10891 erel
= rel_hdr
->contents
;
10892 if (rel_hdr
->sh_type
== SHT_REL
)
10894 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10895 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10899 irel
[0].r_addend
= addend
;
10900 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10901 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10910 /* Get the output vma of the section pointed to by the sh_link field. */
10913 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10915 Elf_Internal_Shdr
**elf_shdrp
;
10919 s
= p
->u
.indirect
.section
;
10920 elf_shdrp
= elf_elfsections (s
->owner
);
10921 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10922 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10924 The Intel C compiler generates SHT_IA_64_UNWIND with
10925 SHF_LINK_ORDER. But it doesn't set the sh_link or
10926 sh_info fields. Hence we could get the situation
10927 where elfsec is 0. */
10930 const struct elf_backend_data
*bed
10931 = get_elf_backend_data (s
->owner
);
10932 if (bed
->link_order_error_handler
)
10933 bed
->link_order_error_handler
10934 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10939 s
= elf_shdrp
[elfsec
]->bfd_section
;
10940 return s
->output_section
->vma
+ s
->output_offset
;
10945 /* Compare two sections based on the locations of the sections they are
10946 linked to. Used by elf_fixup_link_order. */
10949 compare_link_order (const void * a
, const void * b
)
10954 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10955 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10958 return apos
> bpos
;
10962 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10963 order as their linked sections. Returns false if this could not be done
10964 because an output section includes both ordered and unordered
10965 sections. Ideally we'd do this in the linker proper. */
10968 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10970 int seen_linkorder
;
10973 struct bfd_link_order
*p
;
10975 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10977 struct bfd_link_order
**sections
;
10978 asection
*s
, *other_sec
, *linkorder_sec
;
10982 linkorder_sec
= NULL
;
10984 seen_linkorder
= 0;
10985 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10987 if (p
->type
== bfd_indirect_link_order
)
10989 s
= p
->u
.indirect
.section
;
10991 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10992 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
10993 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
10994 && elfsec
< elf_numsections (sub
)
10995 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
10996 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11010 if (seen_other
&& seen_linkorder
)
11012 if (other_sec
&& linkorder_sec
)
11013 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
11015 linkorder_sec
->owner
, other_sec
,
11018 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
11020 bfd_set_error (bfd_error_bad_value
);
11025 if (!seen_linkorder
)
11028 sections
= (struct bfd_link_order
**)
11029 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11030 if (sections
== NULL
)
11032 seen_linkorder
= 0;
11034 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11036 sections
[seen_linkorder
++] = p
;
11038 /* Sort the input sections in the order of their linked section. */
11039 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11040 compare_link_order
);
11042 /* Change the offsets of the sections. */
11044 for (n
= 0; n
< seen_linkorder
; n
++)
11046 s
= sections
[n
]->u
.indirect
.section
;
11047 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11048 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11049 sections
[n
]->offset
= offset
;
11050 offset
+= sections
[n
]->size
;
11057 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11058 Returns TRUE upon success, FALSE otherwise. */
11061 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11063 bfd_boolean ret
= FALSE
;
11065 const struct elf_backend_data
*bed
;
11067 enum bfd_architecture arch
;
11069 asymbol
**sympp
= NULL
;
11073 elf_symbol_type
*osymbuf
;
11075 implib_bfd
= info
->out_implib_bfd
;
11076 bed
= get_elf_backend_data (abfd
);
11078 if (!bfd_set_format (implib_bfd
, bfd_object
))
11081 flags
= bfd_get_file_flags (abfd
);
11082 flags
&= ~HAS_RELOC
;
11083 if (!bfd_set_start_address (implib_bfd
, 0)
11084 || !bfd_set_file_flags (implib_bfd
, flags
))
11087 /* Copy architecture of output file to import library file. */
11088 arch
= bfd_get_arch (abfd
);
11089 mach
= bfd_get_mach (abfd
);
11090 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11091 && (abfd
->target_defaulted
11092 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11095 /* Get symbol table size. */
11096 symsize
= bfd_get_symtab_upper_bound (abfd
);
11100 /* Read in the symbol table. */
11101 sympp
= (asymbol
**) xmalloc (symsize
);
11102 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11106 /* Allow the BFD backend to copy any private header data it
11107 understands from the output BFD to the import library BFD. */
11108 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11111 /* Filter symbols to appear in the import library. */
11112 if (bed
->elf_backend_filter_implib_symbols
)
11113 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11116 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11119 bfd_set_error (bfd_error_no_symbols
);
11120 (*_bfd_error_handler
) (_("%B: no symbol found for import library"),
11126 /* Make symbols absolute. */
11127 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11128 sizeof (*osymbuf
));
11129 for (src_count
= 0; src_count
< symcount
; src_count
++)
11131 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11132 sizeof (*osymbuf
));
11133 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11134 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11135 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11136 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11137 osymbuf
[src_count
].symbol
.value
;
11138 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11141 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11143 /* Allow the BFD backend to copy any private data it understands
11144 from the output BFD to the import library BFD. This is done last
11145 to permit the routine to look at the filtered symbol table. */
11146 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11149 if (!bfd_close (implib_bfd
))
11160 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11164 if (flinfo
->symstrtab
!= NULL
)
11165 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11166 if (flinfo
->contents
!= NULL
)
11167 free (flinfo
->contents
);
11168 if (flinfo
->external_relocs
!= NULL
)
11169 free (flinfo
->external_relocs
);
11170 if (flinfo
->internal_relocs
!= NULL
)
11171 free (flinfo
->internal_relocs
);
11172 if (flinfo
->external_syms
!= NULL
)
11173 free (flinfo
->external_syms
);
11174 if (flinfo
->locsym_shndx
!= NULL
)
11175 free (flinfo
->locsym_shndx
);
11176 if (flinfo
->internal_syms
!= NULL
)
11177 free (flinfo
->internal_syms
);
11178 if (flinfo
->indices
!= NULL
)
11179 free (flinfo
->indices
);
11180 if (flinfo
->sections
!= NULL
)
11181 free (flinfo
->sections
);
11182 if (flinfo
->symshndxbuf
!= NULL
)
11183 free (flinfo
->symshndxbuf
);
11184 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11186 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11187 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11188 free (esdo
->rel
.hashes
);
11189 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11190 free (esdo
->rela
.hashes
);
11194 /* Do the final step of an ELF link. */
11197 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11199 bfd_boolean dynamic
;
11200 bfd_boolean emit_relocs
;
11202 struct elf_final_link_info flinfo
;
11204 struct bfd_link_order
*p
;
11206 bfd_size_type max_contents_size
;
11207 bfd_size_type max_external_reloc_size
;
11208 bfd_size_type max_internal_reloc_count
;
11209 bfd_size_type max_sym_count
;
11210 bfd_size_type max_sym_shndx_count
;
11211 Elf_Internal_Sym elfsym
;
11213 Elf_Internal_Shdr
*symtab_hdr
;
11214 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11216 struct elf_outext_info eoinfo
;
11217 bfd_boolean merged
;
11218 size_t relativecount
= 0;
11219 asection
*reldyn
= 0;
11221 asection
*attr_section
= NULL
;
11222 bfd_vma attr_size
= 0;
11223 const char *std_attrs_section
;
11225 if (! is_elf_hash_table (info
->hash
))
11228 if (bfd_link_pic (info
))
11229 abfd
->flags
|= DYNAMIC
;
11231 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
11232 dynobj
= elf_hash_table (info
)->dynobj
;
11234 emit_relocs
= (bfd_link_relocatable (info
)
11235 || info
->emitrelocations
);
11237 flinfo
.info
= info
;
11238 flinfo
.output_bfd
= abfd
;
11239 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11240 if (flinfo
.symstrtab
== NULL
)
11245 flinfo
.hash_sec
= NULL
;
11246 flinfo
.symver_sec
= NULL
;
11250 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11251 /* Note that dynsym_sec can be NULL (on VMS). */
11252 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11253 /* Note that it is OK if symver_sec is NULL. */
11256 flinfo
.contents
= NULL
;
11257 flinfo
.external_relocs
= NULL
;
11258 flinfo
.internal_relocs
= NULL
;
11259 flinfo
.external_syms
= NULL
;
11260 flinfo
.locsym_shndx
= NULL
;
11261 flinfo
.internal_syms
= NULL
;
11262 flinfo
.indices
= NULL
;
11263 flinfo
.sections
= NULL
;
11264 flinfo
.symshndxbuf
= NULL
;
11265 flinfo
.filesym_count
= 0;
11267 /* The object attributes have been merged. Remove the input
11268 sections from the link, and set the contents of the output
11270 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11271 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11273 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11274 || strcmp (o
->name
, ".gnu.attributes") == 0)
11276 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11278 asection
*input_section
;
11280 if (p
->type
!= bfd_indirect_link_order
)
11282 input_section
= p
->u
.indirect
.section
;
11283 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11284 elf_link_input_bfd ignores this section. */
11285 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11288 attr_size
= bfd_elf_obj_attr_size (abfd
);
11291 bfd_set_section_size (abfd
, o
, attr_size
);
11293 /* Skip this section later on. */
11294 o
->map_head
.link_order
= NULL
;
11297 o
->flags
|= SEC_EXCLUDE
;
11301 /* Count up the number of relocations we will output for each output
11302 section, so that we know the sizes of the reloc sections. We
11303 also figure out some maximum sizes. */
11304 max_contents_size
= 0;
11305 max_external_reloc_size
= 0;
11306 max_internal_reloc_count
= 0;
11308 max_sym_shndx_count
= 0;
11310 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11312 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11313 unsigned int additional_reloc_count
= 0;
11314 o
->reloc_count
= 0;
11316 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11318 unsigned int reloc_count
= 0;
11319 struct bfd_elf_section_data
*esdi
= NULL
;
11321 if (p
->type
== bfd_section_reloc_link_order
11322 || p
->type
== bfd_symbol_reloc_link_order
)
11324 else if (p
->type
== bfd_indirect_link_order
)
11328 sec
= p
->u
.indirect
.section
;
11329 esdi
= elf_section_data (sec
);
11331 /* Mark all sections which are to be included in the
11332 link. This will normally be every section. We need
11333 to do this so that we can identify any sections which
11334 the linker has decided to not include. */
11335 sec
->linker_mark
= TRUE
;
11337 if (sec
->flags
& SEC_MERGE
)
11340 if (esdo
->this_hdr
.sh_type
== SHT_REL
11341 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11342 /* Some backends use reloc_count in relocation sections
11343 to count particular types of relocs. Of course,
11344 reloc sections themselves can't have relocations. */
11346 else if (emit_relocs
)
11348 reloc_count
= sec
->reloc_count
;
11349 if (bed
->elf_backend_count_additional_relocs
)
11352 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11353 additional_reloc_count
+= c
;
11356 else if (bed
->elf_backend_count_relocs
)
11357 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11359 if (sec
->rawsize
> max_contents_size
)
11360 max_contents_size
= sec
->rawsize
;
11361 if (sec
->size
> max_contents_size
)
11362 max_contents_size
= sec
->size
;
11364 /* We are interested in just local symbols, not all
11366 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11367 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11371 if (elf_bad_symtab (sec
->owner
))
11372 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11373 / bed
->s
->sizeof_sym
);
11375 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11377 if (sym_count
> max_sym_count
)
11378 max_sym_count
= sym_count
;
11380 if (sym_count
> max_sym_shndx_count
11381 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11382 max_sym_shndx_count
= sym_count
;
11384 if ((sec
->flags
& SEC_RELOC
) != 0)
11386 size_t ext_size
= 0;
11388 if (esdi
->rel
.hdr
!= NULL
)
11389 ext_size
= esdi
->rel
.hdr
->sh_size
;
11390 if (esdi
->rela
.hdr
!= NULL
)
11391 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11393 if (ext_size
> max_external_reloc_size
)
11394 max_external_reloc_size
= ext_size
;
11395 if (sec
->reloc_count
> max_internal_reloc_count
)
11396 max_internal_reloc_count
= sec
->reloc_count
;
11401 if (reloc_count
== 0)
11404 o
->reloc_count
+= reloc_count
;
11406 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11409 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11410 if (esdi
->rela
.hdr
)
11411 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11416 esdo
->rela
.count
+= reloc_count
;
11418 esdo
->rel
.count
+= reloc_count
;
11422 if (o
->reloc_count
> 0 || additional_reloc_count
> 0)
11423 o
->flags
|= SEC_RELOC
;
11426 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11427 set it (this is probably a bug) and if it is set
11428 assign_section_numbers will create a reloc section. */
11429 o
->flags
&=~ SEC_RELOC
;
11432 /* If the SEC_ALLOC flag is not set, force the section VMA to
11433 zero. This is done in elf_fake_sections as well, but forcing
11434 the VMA to 0 here will ensure that relocs against these
11435 sections are handled correctly. */
11436 if ((o
->flags
& SEC_ALLOC
) == 0
11437 && ! o
->user_set_vma
)
11441 if (! bfd_link_relocatable (info
) && merged
)
11442 elf_link_hash_traverse (elf_hash_table (info
),
11443 _bfd_elf_link_sec_merge_syms
, abfd
);
11445 /* Figure out the file positions for everything but the symbol table
11446 and the relocs. We set symcount to force assign_section_numbers
11447 to create a symbol table. */
11448 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11449 BFD_ASSERT (! abfd
->output_has_begun
);
11450 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11453 /* Set sizes, and assign file positions for reloc sections. */
11454 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11456 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11457 if ((o
->flags
& SEC_RELOC
) != 0)
11460 && !(_bfd_elf_link_size_reloc_section (abfd
, info
, o
, FALSE
)))
11464 && !(_bfd_elf_link_size_reloc_section (abfd
, info
, o
, TRUE
)))
11468 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11469 to count upwards while actually outputting the relocations. */
11470 esdo
->rel
.count
= 0;
11471 esdo
->rela
.count
= 0;
11473 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11475 /* Cache the section contents so that they can be compressed
11476 later. Use bfd_malloc since it will be freed by
11477 bfd_compress_section_contents. */
11478 unsigned char *contents
= esdo
->this_hdr
.contents
;
11479 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11482 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11483 if (contents
== NULL
)
11485 esdo
->this_hdr
.contents
= contents
;
11489 /* We have now assigned file positions for all the sections except
11490 .symtab, .strtab, and non-loaded reloc sections. We start the
11491 .symtab section at the current file position, and write directly
11492 to it. We build the .strtab section in memory. */
11493 bfd_get_symcount (abfd
) = 0;
11494 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11495 /* sh_name is set in prep_headers. */
11496 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11497 /* sh_flags, sh_addr and sh_size all start off zero. */
11498 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11499 /* sh_link is set in assign_section_numbers. */
11500 /* sh_info is set below. */
11501 /* sh_offset is set just below. */
11502 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11504 if (max_sym_count
< 20)
11505 max_sym_count
= 20;
11506 elf_hash_table (info
)->strtabsize
= max_sym_count
;
11507 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11508 elf_hash_table (info
)->strtab
11509 = (struct elf_sym_strtab
*) bfd_malloc (amt
);
11510 if (elf_hash_table (info
)->strtab
== NULL
)
11512 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11514 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11515 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11517 if (info
->strip
!= strip_all
|| emit_relocs
)
11519 file_ptr off
= elf_next_file_pos (abfd
);
11521 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11523 /* Note that at this point elf_next_file_pos (abfd) is
11524 incorrect. We do not yet know the size of the .symtab section.
11525 We correct next_file_pos below, after we do know the size. */
11527 /* Start writing out the symbol table. The first symbol is always a
11529 elfsym
.st_value
= 0;
11530 elfsym
.st_size
= 0;
11531 elfsym
.st_info
= 0;
11532 elfsym
.st_other
= 0;
11533 elfsym
.st_shndx
= SHN_UNDEF
;
11534 elfsym
.st_target_internal
= 0;
11535 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11536 bfd_und_section_ptr
, NULL
) != 1)
11539 /* Output a symbol for each section. We output these even if we are
11540 discarding local symbols, since they are used for relocs. These
11541 symbols have no names. We store the index of each one in the
11542 index field of the section, so that we can find it again when
11543 outputting relocs. */
11545 elfsym
.st_size
= 0;
11546 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11547 elfsym
.st_other
= 0;
11548 elfsym
.st_value
= 0;
11549 elfsym
.st_target_internal
= 0;
11550 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11552 o
= bfd_section_from_elf_index (abfd
, i
);
11555 o
->target_index
= bfd_get_symcount (abfd
);
11556 elfsym
.st_shndx
= i
;
11557 if (!bfd_link_relocatable (info
))
11558 elfsym
.st_value
= o
->vma
;
11559 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11566 /* Allocate some memory to hold information read in from the input
11568 if (max_contents_size
!= 0)
11570 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11571 if (flinfo
.contents
== NULL
)
11575 if (max_external_reloc_size
!= 0)
11577 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11578 if (flinfo
.external_relocs
== NULL
)
11582 if (max_internal_reloc_count
!= 0)
11584 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11585 amt
*= sizeof (Elf_Internal_Rela
);
11586 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11587 if (flinfo
.internal_relocs
== NULL
)
11591 if (max_sym_count
!= 0)
11593 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11594 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11595 if (flinfo
.external_syms
== NULL
)
11598 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11599 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11600 if (flinfo
.internal_syms
== NULL
)
11603 amt
= max_sym_count
* sizeof (long);
11604 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11605 if (flinfo
.indices
== NULL
)
11608 amt
= max_sym_count
* sizeof (asection
*);
11609 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11610 if (flinfo
.sections
== NULL
)
11614 if (max_sym_shndx_count
!= 0)
11616 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11617 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11618 if (flinfo
.locsym_shndx
== NULL
)
11622 if (elf_hash_table (info
)->tls_sec
)
11624 bfd_vma base
, end
= 0;
11627 for (sec
= elf_hash_table (info
)->tls_sec
;
11628 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11631 bfd_size_type size
= sec
->size
;
11634 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11636 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11639 size
= ord
->offset
+ ord
->size
;
11641 end
= sec
->vma
+ size
;
11643 base
= elf_hash_table (info
)->tls_sec
->vma
;
11644 /* Only align end of TLS section if static TLS doesn't have special
11645 alignment requirements. */
11646 if (bed
->static_tls_alignment
== 1)
11647 end
= align_power (end
,
11648 elf_hash_table (info
)->tls_sec
->alignment_power
);
11649 elf_hash_table (info
)->tls_size
= end
- base
;
11652 /* Reorder SHF_LINK_ORDER sections. */
11653 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11655 if (!elf_fixup_link_order (abfd
, o
))
11659 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11662 /* Since ELF permits relocations to be against local symbols, we
11663 must have the local symbols available when we do the relocations.
11664 Since we would rather only read the local symbols once, and we
11665 would rather not keep them in memory, we handle all the
11666 relocations for a single input file at the same time.
11668 Unfortunately, there is no way to know the total number of local
11669 symbols until we have seen all of them, and the local symbol
11670 indices precede the global symbol indices. This means that when
11671 we are generating relocatable output, and we see a reloc against
11672 a global symbol, we can not know the symbol index until we have
11673 finished examining all the local symbols to see which ones we are
11674 going to output. To deal with this, we keep the relocations in
11675 memory, and don't output them until the end of the link. This is
11676 an unfortunate waste of memory, but I don't see a good way around
11677 it. Fortunately, it only happens when performing a relocatable
11678 link, which is not the common case. FIXME: If keep_memory is set
11679 we could write the relocs out and then read them again; I don't
11680 know how bad the memory loss will be. */
11682 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11683 sub
->output_has_begun
= FALSE
;
11684 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11686 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11688 if (p
->type
== bfd_indirect_link_order
11689 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11690 == bfd_target_elf_flavour
)
11691 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11693 if (! sub
->output_has_begun
)
11695 if (! elf_link_input_bfd (&flinfo
, sub
))
11697 sub
->output_has_begun
= TRUE
;
11700 else if (p
->type
== bfd_section_reloc_link_order
11701 || p
->type
== bfd_symbol_reloc_link_order
)
11703 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11708 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11710 if (p
->type
== bfd_indirect_link_order
11711 && (bfd_get_flavour (sub
)
11712 == bfd_target_elf_flavour
)
11713 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11714 != bed
->s
->elfclass
))
11716 const char *iclass
, *oclass
;
11718 switch (bed
->s
->elfclass
)
11720 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11721 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11722 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11726 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11728 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11729 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11730 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11734 bfd_set_error (bfd_error_wrong_format
);
11735 (*_bfd_error_handler
)
11736 (_("%B: file class %s incompatible with %s"),
11737 sub
, iclass
, oclass
);
11746 /* Free symbol buffer if needed. */
11747 if (!info
->reduce_memory_overheads
)
11749 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11750 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11751 && elf_tdata (sub
)->symbuf
)
11753 free (elf_tdata (sub
)->symbuf
);
11754 elf_tdata (sub
)->symbuf
= NULL
;
11758 /* Output any global symbols that got converted to local in a
11759 version script or due to symbol visibility. We do this in a
11760 separate step since ELF requires all local symbols to appear
11761 prior to any global symbols. FIXME: We should only do this if
11762 some global symbols were, in fact, converted to become local.
11763 FIXME: Will this work correctly with the Irix 5 linker? */
11764 eoinfo
.failed
= FALSE
;
11765 eoinfo
.flinfo
= &flinfo
;
11766 eoinfo
.localsyms
= TRUE
;
11767 eoinfo
.file_sym_done
= FALSE
;
11768 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11772 /* If backend needs to output some local symbols not present in the hash
11773 table, do it now. */
11774 if (bed
->elf_backend_output_arch_local_syms
11775 && (info
->strip
!= strip_all
|| emit_relocs
))
11777 typedef int (*out_sym_func
)
11778 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11779 struct elf_link_hash_entry
*);
11781 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11782 (abfd
, info
, &flinfo
,
11783 (out_sym_func
) elf_link_output_symstrtab
)))
11787 /* That wrote out all the local symbols. Finish up the symbol table
11788 with the global symbols. Even if we want to strip everything we
11789 can, we still need to deal with those global symbols that got
11790 converted to local in a version script. */
11792 /* The sh_info field records the index of the first non local symbol. */
11793 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11796 && elf_hash_table (info
)->dynsym
!= NULL
11797 && (elf_hash_table (info
)->dynsym
->output_section
11798 != bfd_abs_section_ptr
))
11800 Elf_Internal_Sym sym
;
11801 bfd_byte
*dynsym
= elf_hash_table (info
)->dynsym
->contents
;
11803 o
= elf_hash_table (info
)->dynsym
->output_section
;
11804 elf_section_data (o
)->this_hdr
.sh_info
11805 = elf_hash_table (info
)->local_dynsymcount
+ 1;
11807 /* Write out the section symbols for the output sections. */
11808 if (bfd_link_pic (info
)
11809 || elf_hash_table (info
)->is_relocatable_executable
)
11815 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11817 sym
.st_target_internal
= 0;
11819 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11825 dynindx
= elf_section_data (s
)->dynindx
;
11828 indx
= elf_section_data (s
)->this_idx
;
11829 BFD_ASSERT (indx
> 0);
11830 sym
.st_shndx
= indx
;
11831 if (! check_dynsym (abfd
, &sym
))
11833 sym
.st_value
= s
->vma
;
11834 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11835 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11839 /* Write out the local dynsyms. */
11840 if (elf_hash_table (info
)->dynlocal
)
11842 struct elf_link_local_dynamic_entry
*e
;
11843 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
11848 /* Copy the internal symbol and turn off visibility.
11849 Note that we saved a word of storage and overwrote
11850 the original st_name with the dynstr_index. */
11852 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11854 s
= bfd_section_from_elf_index (e
->input_bfd
,
11859 elf_section_data (s
->output_section
)->this_idx
;
11860 if (! check_dynsym (abfd
, &sym
))
11862 sym
.st_value
= (s
->output_section
->vma
11864 + e
->isym
.st_value
);
11867 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11868 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11873 /* We get the global symbols from the hash table. */
11874 eoinfo
.failed
= FALSE
;
11875 eoinfo
.localsyms
= FALSE
;
11876 eoinfo
.flinfo
= &flinfo
;
11877 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11881 /* If backend needs to output some symbols not present in the hash
11882 table, do it now. */
11883 if (bed
->elf_backend_output_arch_syms
11884 && (info
->strip
!= strip_all
|| emit_relocs
))
11886 typedef int (*out_sym_func
)
11887 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11888 struct elf_link_hash_entry
*);
11890 if (! ((*bed
->elf_backend_output_arch_syms
)
11891 (abfd
, info
, &flinfo
,
11892 (out_sym_func
) elf_link_output_symstrtab
)))
11896 /* Finalize the .strtab section. */
11897 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11899 /* Swap out the .strtab section. */
11900 if (!elf_link_swap_symbols_out (&flinfo
))
11903 /* Now we know the size of the symtab section. */
11904 if (bfd_get_symcount (abfd
) > 0)
11906 /* Finish up and write out the symbol string table (.strtab)
11908 Elf_Internal_Shdr
*symstrtab_hdr
;
11909 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11911 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11912 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11914 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11915 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11916 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11917 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11918 symtab_shndx_hdr
->sh_size
= amt
;
11920 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11923 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11924 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11928 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11929 /* sh_name was set in prep_headers. */
11930 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11931 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11932 symstrtab_hdr
->sh_addr
= 0;
11933 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11934 symstrtab_hdr
->sh_entsize
= 0;
11935 symstrtab_hdr
->sh_link
= 0;
11936 symstrtab_hdr
->sh_info
= 0;
11937 /* sh_offset is set just below. */
11938 symstrtab_hdr
->sh_addralign
= 1;
11940 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11942 elf_next_file_pos (abfd
) = off
;
11944 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11945 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11949 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11951 (*_bfd_error_handler
) (_("%B: failed to generate import library"),
11952 info
->out_implib_bfd
);
11956 /* Adjust the relocs to have the correct symbol indices. */
11957 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11959 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11961 if ((o
->flags
& SEC_RELOC
) == 0)
11964 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11965 if (esdo
->rel
.hdr
!= NULL
11966 && !elf_link_adjust_relocs (abfd
, &esdo
->rel
, sort
))
11968 if (esdo
->rela
.hdr
!= NULL
11969 && !elf_link_adjust_relocs (abfd
, &esdo
->rela
, sort
))
11972 /* Set the reloc_count field to 0 to prevent write_relocs from
11973 trying to swap the relocs out itself. */
11974 o
->reloc_count
= 0;
11977 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11978 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
11980 /* If we are linking against a dynamic object, or generating a
11981 shared library, finish up the dynamic linking information. */
11984 bfd_byte
*dyncon
, *dynconend
;
11986 /* Fix up .dynamic entries. */
11987 o
= bfd_get_linker_section (dynobj
, ".dynamic");
11988 BFD_ASSERT (o
!= NULL
);
11990 dyncon
= o
->contents
;
11991 dynconend
= o
->contents
+ o
->size
;
11992 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11994 Elf_Internal_Dyn dyn
;
11998 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12005 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12007 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12009 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12010 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12013 dyn
.d_un
.d_val
= relativecount
;
12020 name
= info
->init_function
;
12023 name
= info
->fini_function
;
12026 struct elf_link_hash_entry
*h
;
12028 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
12029 FALSE
, FALSE
, TRUE
);
12031 && (h
->root
.type
== bfd_link_hash_defined
12032 || h
->root
.type
== bfd_link_hash_defweak
))
12034 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12035 o
= h
->root
.u
.def
.section
;
12036 if (o
->output_section
!= NULL
)
12037 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12038 + o
->output_offset
);
12041 /* The symbol is imported from another shared
12042 library and does not apply to this one. */
12043 dyn
.d_un
.d_ptr
= 0;
12050 case DT_PREINIT_ARRAYSZ
:
12051 name
= ".preinit_array";
12053 case DT_INIT_ARRAYSZ
:
12054 name
= ".init_array";
12056 case DT_FINI_ARRAYSZ
:
12057 name
= ".fini_array";
12059 o
= bfd_get_section_by_name (abfd
, name
);
12062 (*_bfd_error_handler
)
12063 (_("could not find section %s"), name
);
12067 (*_bfd_error_handler
)
12068 (_("warning: %s section has zero size"), name
);
12069 dyn
.d_un
.d_val
= o
->size
;
12072 case DT_PREINIT_ARRAY
:
12073 name
= ".preinit_array";
12075 case DT_INIT_ARRAY
:
12076 name
= ".init_array";
12078 case DT_FINI_ARRAY
:
12079 name
= ".fini_array";
12081 o
= bfd_get_section_by_name (abfd
, name
);
12088 name
= ".gnu.hash";
12097 name
= ".gnu.version_d";
12100 name
= ".gnu.version_r";
12103 name
= ".gnu.version";
12105 o
= bfd_get_linker_section (dynobj
, name
);
12109 (*_bfd_error_handler
)
12110 (_("could not find section %s"), name
);
12113 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12115 (*_bfd_error_handler
)
12116 (_("warning: section '%s' is being made into a note"), name
);
12117 bfd_set_error (bfd_error_nonrepresentable_section
);
12120 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12127 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12131 dyn
.d_un
.d_val
= 0;
12132 dyn
.d_un
.d_ptr
= 0;
12133 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12135 Elf_Internal_Shdr
*hdr
;
12137 hdr
= elf_elfsections (abfd
)[i
];
12138 if (hdr
->sh_type
== type
12139 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12141 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12142 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12145 if (dyn
.d_un
.d_ptr
== 0
12146 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12147 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12153 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12157 /* If we have created any dynamic sections, then output them. */
12158 if (dynobj
!= NULL
)
12160 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12163 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12164 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12165 || info
->error_textrel
)
12166 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12168 bfd_byte
*dyncon
, *dynconend
;
12170 dyncon
= o
->contents
;
12171 dynconend
= o
->contents
+ o
->size
;
12172 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12174 Elf_Internal_Dyn dyn
;
12176 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12178 if (dyn
.d_tag
== DT_TEXTREL
)
12180 if (info
->error_textrel
)
12181 info
->callbacks
->einfo
12182 (_("%P%X: read-only segment has dynamic relocations.\n"));
12184 info
->callbacks
->einfo
12185 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12191 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12193 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12195 || o
->output_section
== bfd_abs_section_ptr
)
12197 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12199 /* At this point, we are only interested in sections
12200 created by _bfd_elf_link_create_dynamic_sections. */
12203 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
12205 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
12207 if (strcmp (o
->name
, ".dynstr") != 0)
12209 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12211 (file_ptr
) o
->output_offset
12212 * bfd_octets_per_byte (abfd
),
12218 /* The contents of the .dynstr section are actually in a
12222 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12223 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12224 || ! _bfd_elf_strtab_emit (abfd
,
12225 elf_hash_table (info
)->dynstr
))
12231 if (bfd_link_relocatable (info
))
12233 bfd_boolean failed
= FALSE
;
12235 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12240 /* If we have optimized stabs strings, output them. */
12241 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
12243 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
12247 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12250 elf_final_link_free (abfd
, &flinfo
);
12252 elf_linker (abfd
) = TRUE
;
12256 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12257 if (contents
== NULL
)
12258 return FALSE
; /* Bail out and fail. */
12259 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12260 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12267 elf_final_link_free (abfd
, &flinfo
);
12271 /* Initialize COOKIE for input bfd ABFD. */
12274 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12275 struct bfd_link_info
*info
, bfd
*abfd
)
12277 Elf_Internal_Shdr
*symtab_hdr
;
12278 const struct elf_backend_data
*bed
;
12280 bed
= get_elf_backend_data (abfd
);
12281 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12283 cookie
->abfd
= abfd
;
12284 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12285 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12286 if (cookie
->bad_symtab
)
12288 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12289 cookie
->extsymoff
= 0;
12293 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12294 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12297 if (bed
->s
->arch_size
== 32)
12298 cookie
->r_sym_shift
= 8;
12300 cookie
->r_sym_shift
= 32;
12302 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12303 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12305 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12306 cookie
->locsymcount
, 0,
12308 if (cookie
->locsyms
== NULL
)
12310 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12313 if (info
->keep_memory
)
12314 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12319 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12322 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12324 Elf_Internal_Shdr
*symtab_hdr
;
12326 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12327 if (cookie
->locsyms
!= NULL
12328 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12329 free (cookie
->locsyms
);
12332 /* Initialize the relocation information in COOKIE for input section SEC
12333 of input bfd ABFD. */
12336 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12337 struct bfd_link_info
*info
, bfd
*abfd
,
12340 const struct elf_backend_data
*bed
;
12342 if (sec
->reloc_count
== 0)
12344 cookie
->rels
= NULL
;
12345 cookie
->relend
= NULL
;
12349 bed
= get_elf_backend_data (abfd
);
12351 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12352 info
->keep_memory
);
12353 if (cookie
->rels
== NULL
)
12355 cookie
->rel
= cookie
->rels
;
12356 cookie
->relend
= (cookie
->rels
12357 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12359 cookie
->rel
= cookie
->rels
;
12363 /* Free the memory allocated by init_reloc_cookie_rels,
12367 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12370 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12371 free (cookie
->rels
);
12374 /* Initialize the whole of COOKIE for input section SEC. */
12377 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12378 struct bfd_link_info
*info
,
12381 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12383 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12388 fini_reloc_cookie (cookie
, sec
->owner
);
12393 /* Free the memory allocated by init_reloc_cookie_for_section,
12397 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12400 fini_reloc_cookie_rels (cookie
, sec
);
12401 fini_reloc_cookie (cookie
, sec
->owner
);
12404 /* Garbage collect unused sections. */
12406 /* Default gc_mark_hook. */
12409 _bfd_elf_gc_mark_hook (asection
*sec
,
12410 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12411 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12412 struct elf_link_hash_entry
*h
,
12413 Elf_Internal_Sym
*sym
)
12417 switch (h
->root
.type
)
12419 case bfd_link_hash_defined
:
12420 case bfd_link_hash_defweak
:
12421 return h
->root
.u
.def
.section
;
12423 case bfd_link_hash_common
:
12424 return h
->root
.u
.c
.p
->section
;
12431 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12436 /* For undefined __start_<name> and __stop_<name> symbols, return the
12437 first input section matching <name>. Return NULL otherwise. */
12440 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12441 struct elf_link_hash_entry
*h
)
12444 const char *sec_name
;
12446 if (h
->root
.type
!= bfd_link_hash_undefined
12447 && h
->root
.type
!= bfd_link_hash_undefweak
)
12450 s
= h
->root
.u
.undef
.section
;
12453 if (s
== (asection
*) 0 - 1)
12459 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12460 sec_name
= h
->root
.root
.string
+ 8;
12461 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12462 sec_name
= h
->root
.root
.string
+ 7;
12464 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12468 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12470 s
= bfd_get_section_by_name (i
, sec_name
);
12473 h
->root
.u
.undef
.section
= s
;
12480 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12485 /* COOKIE->rel describes a relocation against section SEC, which is
12486 a section we've decided to keep. Return the section that contains
12487 the relocation symbol, or NULL if no section contains it. */
12490 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12491 elf_gc_mark_hook_fn gc_mark_hook
,
12492 struct elf_reloc_cookie
*cookie
,
12493 bfd_boolean
*start_stop
)
12495 unsigned long r_symndx
;
12496 struct elf_link_hash_entry
*h
;
12498 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12499 if (r_symndx
== STN_UNDEF
)
12502 if (r_symndx
>= cookie
->locsymcount
12503 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12505 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12508 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12512 while (h
->root
.type
== bfd_link_hash_indirect
12513 || h
->root
.type
== bfd_link_hash_warning
)
12514 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12516 /* If this symbol is weak and there is a non-weak definition, we
12517 keep the non-weak definition because many backends put
12518 dynamic reloc info on the non-weak definition for code
12519 handling copy relocs. */
12520 if (h
->u
.weakdef
!= NULL
)
12521 h
->u
.weakdef
->mark
= 1;
12523 if (start_stop
!= NULL
)
12525 /* To work around a glibc bug, mark all XXX input sections
12526 when there is an as yet undefined reference to __start_XXX
12527 or __stop_XXX symbols. The linker will later define such
12528 symbols for orphan input sections that have a name
12529 representable as a C identifier. */
12530 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12534 *start_stop
= !s
->gc_mark
;
12539 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12542 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12543 &cookie
->locsyms
[r_symndx
]);
12546 /* COOKIE->rel describes a relocation against section SEC, which is
12547 a section we've decided to keep. Mark the section that contains
12548 the relocation symbol. */
12551 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12553 elf_gc_mark_hook_fn gc_mark_hook
,
12554 struct elf_reloc_cookie
*cookie
)
12557 bfd_boolean start_stop
= FALSE
;
12559 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12560 while (rsec
!= NULL
)
12562 if (!rsec
->gc_mark
)
12564 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12565 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12567 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12572 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12577 /* The mark phase of garbage collection. For a given section, mark
12578 it and any sections in this section's group, and all the sections
12579 which define symbols to which it refers. */
12582 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12584 elf_gc_mark_hook_fn gc_mark_hook
)
12587 asection
*group_sec
, *eh_frame
;
12591 /* Mark all the sections in the group. */
12592 group_sec
= elf_section_data (sec
)->next_in_group
;
12593 if (group_sec
&& !group_sec
->gc_mark
)
12594 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12597 /* Look through the section relocs. */
12599 eh_frame
= elf_eh_frame_section (sec
->owner
);
12600 if ((sec
->flags
& SEC_RELOC
) != 0
12601 && sec
->reloc_count
> 0
12602 && sec
!= eh_frame
)
12604 struct elf_reloc_cookie cookie
;
12606 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12610 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12611 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12616 fini_reloc_cookie_for_section (&cookie
, sec
);
12620 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12622 struct elf_reloc_cookie cookie
;
12624 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12628 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12629 gc_mark_hook
, &cookie
))
12631 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12635 eh_frame
= elf_section_eh_frame_entry (sec
);
12636 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12637 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12643 /* Scan and mark sections in a special or debug section group. */
12646 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12648 /* Point to first section of section group. */
12650 /* Used to iterate the section group. */
12653 bfd_boolean is_special_grp
= TRUE
;
12654 bfd_boolean is_debug_grp
= TRUE
;
12656 /* First scan to see if group contains any section other than debug
12657 and special section. */
12658 ssec
= msec
= elf_next_in_group (grp
);
12661 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12662 is_debug_grp
= FALSE
;
12664 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12665 is_special_grp
= FALSE
;
12667 msec
= elf_next_in_group (msec
);
12669 while (msec
!= ssec
);
12671 /* If this is a pure debug section group or pure special section group,
12672 keep all sections in this group. */
12673 if (is_debug_grp
|| is_special_grp
)
12678 msec
= elf_next_in_group (msec
);
12680 while (msec
!= ssec
);
12684 /* Keep debug and special sections. */
12687 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12688 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12692 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12695 bfd_boolean some_kept
;
12696 bfd_boolean debug_frag_seen
;
12698 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12701 /* Ensure all linker created sections are kept,
12702 see if any other section is already marked,
12703 and note if we have any fragmented debug sections. */
12704 debug_frag_seen
= some_kept
= FALSE
;
12705 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12707 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12709 else if (isec
->gc_mark
)
12712 if (debug_frag_seen
== FALSE
12713 && (isec
->flags
& SEC_DEBUGGING
)
12714 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12715 debug_frag_seen
= TRUE
;
12718 /* If no section in this file will be kept, then we can
12719 toss out the debug and special sections. */
12723 /* Keep debug and special sections like .comment when they are
12724 not part of a group. Also keep section groups that contain
12725 just debug sections or special sections. */
12726 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12728 if ((isec
->flags
& SEC_GROUP
) != 0)
12729 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12730 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12731 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12732 && elf_next_in_group (isec
) == NULL
)
12736 if (! debug_frag_seen
)
12739 /* Look for CODE sections which are going to be discarded,
12740 and find and discard any fragmented debug sections which
12741 are associated with that code section. */
12742 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12743 if ((isec
->flags
& SEC_CODE
) != 0
12744 && isec
->gc_mark
== 0)
12749 ilen
= strlen (isec
->name
);
12751 /* Association is determined by the name of the debug section
12752 containing the name of the code section as a suffix. For
12753 example .debug_line.text.foo is a debug section associated
12755 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12759 if (dsec
->gc_mark
== 0
12760 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12763 dlen
= strlen (dsec
->name
);
12766 && strncmp (dsec
->name
+ (dlen
- ilen
),
12767 isec
->name
, ilen
) == 0)
12777 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12779 struct elf_gc_sweep_symbol_info
12781 struct bfd_link_info
*info
;
12782 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12787 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12790 && (((h
->root
.type
== bfd_link_hash_defined
12791 || h
->root
.type
== bfd_link_hash_defweak
)
12792 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12793 && h
->root
.u
.def
.section
->gc_mark
))
12794 || h
->root
.type
== bfd_link_hash_undefined
12795 || h
->root
.type
== bfd_link_hash_undefweak
))
12797 struct elf_gc_sweep_symbol_info
*inf
;
12799 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12800 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12801 h
->def_regular
= 0;
12802 h
->ref_regular
= 0;
12803 h
->ref_regular_nonweak
= 0;
12809 /* The sweep phase of garbage collection. Remove all garbage sections. */
12811 typedef bfd_boolean (*gc_sweep_hook_fn
)
12812 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12815 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12818 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12819 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12820 unsigned long section_sym_count
;
12821 struct elf_gc_sweep_symbol_info sweep_info
;
12823 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12827 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12828 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12831 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12833 /* When any section in a section group is kept, we keep all
12834 sections in the section group. If the first member of
12835 the section group is excluded, we will also exclude the
12837 if (o
->flags
& SEC_GROUP
)
12839 asection
*first
= elf_next_in_group (o
);
12840 o
->gc_mark
= first
->gc_mark
;
12846 /* Skip sweeping sections already excluded. */
12847 if (o
->flags
& SEC_EXCLUDE
)
12850 /* Since this is early in the link process, it is simple
12851 to remove a section from the output. */
12852 o
->flags
|= SEC_EXCLUDE
;
12854 if (info
->print_gc_sections
&& o
->size
!= 0)
12855 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12857 /* But we also have to update some of the relocation
12858 info we collected before. */
12860 && (o
->flags
& SEC_RELOC
) != 0
12861 && o
->reloc_count
!= 0
12862 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12863 && (o
->flags
& SEC_DEBUGGING
) != 0)
12864 && !bfd_is_abs_section (o
->output_section
))
12866 Elf_Internal_Rela
*internal_relocs
;
12870 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12871 info
->keep_memory
);
12872 if (internal_relocs
== NULL
)
12875 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12877 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12878 free (internal_relocs
);
12886 /* Remove the symbols that were in the swept sections from the dynamic
12887 symbol table. GCFIXME: Anyone know how to get them out of the
12888 static symbol table as well? */
12889 sweep_info
.info
= info
;
12890 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12891 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12894 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12898 /* Propagate collected vtable information. This is called through
12899 elf_link_hash_traverse. */
12902 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12904 /* Those that are not vtables. */
12905 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12908 /* Those vtables that do not have parents, we cannot merge. */
12909 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12912 /* If we've already been done, exit. */
12913 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12916 /* Make sure the parent's table is up to date. */
12917 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12919 if (h
->vtable
->used
== NULL
)
12921 /* None of this table's entries were referenced. Re-use the
12923 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12924 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12929 bfd_boolean
*cu
, *pu
;
12931 /* Or the parent's entries into ours. */
12932 cu
= h
->vtable
->used
;
12934 pu
= h
->vtable
->parent
->vtable
->used
;
12937 const struct elf_backend_data
*bed
;
12938 unsigned int log_file_align
;
12940 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12941 log_file_align
= bed
->s
->log_file_align
;
12942 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12957 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12960 bfd_vma hstart
, hend
;
12961 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12962 const struct elf_backend_data
*bed
;
12963 unsigned int log_file_align
;
12965 /* Take care of both those symbols that do not describe vtables as
12966 well as those that are not loaded. */
12967 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12970 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
12971 || h
->root
.type
== bfd_link_hash_defweak
);
12973 sec
= h
->root
.u
.def
.section
;
12974 hstart
= h
->root
.u
.def
.value
;
12975 hend
= hstart
+ h
->size
;
12977 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
12979 return *(bfd_boolean
*) okp
= FALSE
;
12980 bed
= get_elf_backend_data (sec
->owner
);
12981 log_file_align
= bed
->s
->log_file_align
;
12983 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
12985 for (rel
= relstart
; rel
< relend
; ++rel
)
12986 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
12988 /* If the entry is in use, do nothing. */
12989 if (h
->vtable
->used
12990 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
12992 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
12993 if (h
->vtable
->used
[entry
])
12996 /* Otherwise, kill it. */
12997 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13003 /* Mark sections containing dynamically referenced symbols. When
13004 building shared libraries, we must assume that any visible symbol is
13008 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13010 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13011 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13013 if ((h
->root
.type
== bfd_link_hash_defined
13014 || h
->root
.type
== bfd_link_hash_defweak
)
13016 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13017 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13018 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13019 && (!bfd_link_executable (info
)
13020 || info
->export_dynamic
13023 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13024 && (h
->versioned
>= versioned
13025 || !bfd_hide_sym_by_version (info
->version_info
,
13026 h
->root
.root
.string
)))))
13027 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13032 /* Keep all sections containing symbols undefined on the command-line,
13033 and the section containing the entry symbol. */
13036 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13038 struct bfd_sym_chain
*sym
;
13040 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13042 struct elf_link_hash_entry
*h
;
13044 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13045 FALSE
, FALSE
, FALSE
);
13048 && (h
->root
.type
== bfd_link_hash_defined
13049 || h
->root
.type
== bfd_link_hash_defweak
)
13050 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
13051 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13056 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13057 struct bfd_link_info
*info
)
13059 bfd
*ibfd
= info
->input_bfds
;
13061 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13064 struct elf_reloc_cookie cookie
;
13066 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13069 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13072 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13074 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13075 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13077 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13078 fini_reloc_cookie_rels (&cookie
, sec
);
13085 /* Do mark and sweep of unused sections. */
13088 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13090 bfd_boolean ok
= TRUE
;
13092 elf_gc_mark_hook_fn gc_mark_hook
;
13093 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13094 struct elf_link_hash_table
*htab
;
13096 if (!bed
->can_gc_sections
13097 || !is_elf_hash_table (info
->hash
))
13099 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
13103 bed
->gc_keep (info
);
13104 htab
= elf_hash_table (info
);
13106 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13107 at the .eh_frame section if we can mark the FDEs individually. */
13108 for (sub
= info
->input_bfds
;
13109 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13110 sub
= sub
->link
.next
)
13113 struct elf_reloc_cookie cookie
;
13115 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13116 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13118 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13119 if (elf_section_data (sec
)->sec_info
13120 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13121 elf_eh_frame_section (sub
) = sec
;
13122 fini_reloc_cookie_for_section (&cookie
, sec
);
13123 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13127 /* Apply transitive closure to the vtable entry usage info. */
13128 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13132 /* Kill the vtable relocations that were not used. */
13133 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13137 /* Mark dynamically referenced symbols. */
13138 if (htab
->dynamic_sections_created
)
13139 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13141 /* Grovel through relocs to find out who stays ... */
13142 gc_mark_hook
= bed
->gc_mark_hook
;
13143 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13147 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13148 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13151 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13152 Also treat note sections as a root, if the section is not part
13154 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13156 && (o
->flags
& SEC_EXCLUDE
) == 0
13157 && ((o
->flags
& SEC_KEEP
) != 0
13158 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13159 && elf_next_in_group (o
) == NULL
)))
13161 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13166 /* Allow the backend to mark additional target specific sections. */
13167 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13169 /* ... and mark SEC_EXCLUDE for those that go. */
13170 return elf_gc_sweep (abfd
, info
);
13173 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13176 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13178 struct elf_link_hash_entry
*h
,
13181 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13182 struct elf_link_hash_entry
**search
, *child
;
13183 size_t extsymcount
;
13184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13186 /* The sh_info field of the symtab header tells us where the
13187 external symbols start. We don't care about the local symbols at
13189 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13190 if (!elf_bad_symtab (abfd
))
13191 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13193 sym_hashes
= elf_sym_hashes (abfd
);
13194 sym_hashes_end
= sym_hashes
+ extsymcount
;
13196 /* Hunt down the child symbol, which is in this section at the same
13197 offset as the relocation. */
13198 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13200 if ((child
= *search
) != NULL
13201 && (child
->root
.type
== bfd_link_hash_defined
13202 || child
->root
.type
== bfd_link_hash_defweak
)
13203 && child
->root
.u
.def
.section
== sec
13204 && child
->root
.u
.def
.value
== offset
)
13208 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
13209 abfd
, sec
, (unsigned long) offset
);
13210 bfd_set_error (bfd_error_invalid_operation
);
13214 if (!child
->vtable
)
13216 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13217 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13218 if (!child
->vtable
)
13223 /* This *should* only be the absolute section. It could potentially
13224 be that someone has defined a non-global vtable though, which
13225 would be bad. It isn't worth paging in the local symbols to be
13226 sure though; that case should simply be handled by the assembler. */
13228 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13231 child
->vtable
->parent
= h
;
13236 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13239 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13240 asection
*sec ATTRIBUTE_UNUSED
,
13241 struct elf_link_hash_entry
*h
,
13244 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13245 unsigned int log_file_align
= bed
->s
->log_file_align
;
13249 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13250 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13255 if (addend
>= h
->vtable
->size
)
13257 size_t size
, bytes
, file_align
;
13258 bfd_boolean
*ptr
= h
->vtable
->used
;
13260 /* While the symbol is undefined, we have to be prepared to handle
13262 file_align
= 1 << log_file_align
;
13263 if (h
->root
.type
== bfd_link_hash_undefined
)
13264 size
= addend
+ file_align
;
13268 if (addend
>= size
)
13270 /* Oops! We've got a reference past the defined end of
13271 the table. This is probably a bug -- shall we warn? */
13272 size
= addend
+ file_align
;
13275 size
= (size
+ file_align
- 1) & -file_align
;
13277 /* Allocate one extra entry for use as a "done" flag for the
13278 consolidation pass. */
13279 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13283 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13289 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13290 * sizeof (bfd_boolean
));
13291 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13295 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13300 /* And arrange for that done flag to be at index -1. */
13301 h
->vtable
->used
= ptr
+ 1;
13302 h
->vtable
->size
= size
;
13305 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13310 /* Map an ELF section header flag to its corresponding string. */
13314 flagword flag_value
;
13315 } elf_flags_to_name_table
;
13317 static elf_flags_to_name_table elf_flags_to_names
[] =
13319 { "SHF_WRITE", SHF_WRITE
},
13320 { "SHF_ALLOC", SHF_ALLOC
},
13321 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13322 { "SHF_MERGE", SHF_MERGE
},
13323 { "SHF_STRINGS", SHF_STRINGS
},
13324 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13325 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13326 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13327 { "SHF_GROUP", SHF_GROUP
},
13328 { "SHF_TLS", SHF_TLS
},
13329 { "SHF_MASKOS", SHF_MASKOS
},
13330 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13333 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13335 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13336 struct flag_info
*flaginfo
,
13339 const bfd_vma sh_flags
= elf_section_flags (section
);
13341 if (!flaginfo
->flags_initialized
)
13343 bfd
*obfd
= info
->output_bfd
;
13344 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13345 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13347 int without_hex
= 0;
13349 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13352 flagword (*lookup
) (char *);
13354 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13355 if (lookup
!= NULL
)
13357 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13361 if (tf
->with
== with_flags
)
13362 with_hex
|= hexval
;
13363 else if (tf
->with
== without_flags
)
13364 without_hex
|= hexval
;
13369 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13371 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13373 if (tf
->with
== with_flags
)
13374 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13375 else if (tf
->with
== without_flags
)
13376 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13383 info
->callbacks
->einfo
13384 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13388 flaginfo
->flags_initialized
= TRUE
;
13389 flaginfo
->only_with_flags
|= with_hex
;
13390 flaginfo
->not_with_flags
|= without_hex
;
13393 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13396 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13402 struct alloc_got_off_arg
{
13404 struct bfd_link_info
*info
;
13407 /* We need a special top-level link routine to convert got reference counts
13408 to real got offsets. */
13411 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13413 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13414 bfd
*obfd
= gofarg
->info
->output_bfd
;
13415 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13417 if (h
->got
.refcount
> 0)
13419 h
->got
.offset
= gofarg
->gotoff
;
13420 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13423 h
->got
.offset
= (bfd_vma
) -1;
13428 /* And an accompanying bit to work out final got entry offsets once
13429 we're done. Should be called from final_link. */
13432 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13433 struct bfd_link_info
*info
)
13436 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13438 struct alloc_got_off_arg gofarg
;
13440 BFD_ASSERT (abfd
== info
->output_bfd
);
13442 if (! is_elf_hash_table (info
->hash
))
13445 /* The GOT offset is relative to the .got section, but the GOT header is
13446 put into the .got.plt section, if the backend uses it. */
13447 if (bed
->want_got_plt
)
13450 gotoff
= bed
->got_header_size
;
13452 /* Do the local .got entries first. */
13453 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13455 bfd_signed_vma
*local_got
;
13456 size_t j
, locsymcount
;
13457 Elf_Internal_Shdr
*symtab_hdr
;
13459 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13462 local_got
= elf_local_got_refcounts (i
);
13466 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13467 if (elf_bad_symtab (i
))
13468 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13470 locsymcount
= symtab_hdr
->sh_info
;
13472 for (j
= 0; j
< locsymcount
; ++j
)
13474 if (local_got
[j
] > 0)
13476 local_got
[j
] = gotoff
;
13477 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13480 local_got
[j
] = (bfd_vma
) -1;
13484 /* Then the global .got entries. .plt refcounts are handled by
13485 adjust_dynamic_symbol */
13486 gofarg
.gotoff
= gotoff
;
13487 gofarg
.info
= info
;
13488 elf_link_hash_traverse (elf_hash_table (info
),
13489 elf_gc_allocate_got_offsets
,
13494 /* Many folk need no more in the way of final link than this, once
13495 got entry reference counting is enabled. */
13498 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13500 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13503 /* Invoke the regular ELF backend linker to do all the work. */
13504 return bfd_elf_final_link (abfd
, info
);
13508 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13510 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13512 if (rcookie
->bad_symtab
)
13513 rcookie
->rel
= rcookie
->rels
;
13515 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13517 unsigned long r_symndx
;
13519 if (! rcookie
->bad_symtab
)
13520 if (rcookie
->rel
->r_offset
> offset
)
13522 if (rcookie
->rel
->r_offset
!= offset
)
13525 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13526 if (r_symndx
== STN_UNDEF
)
13529 if (r_symndx
>= rcookie
->locsymcount
13530 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13532 struct elf_link_hash_entry
*h
;
13534 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13536 while (h
->root
.type
== bfd_link_hash_indirect
13537 || h
->root
.type
== bfd_link_hash_warning
)
13538 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13540 if ((h
->root
.type
== bfd_link_hash_defined
13541 || h
->root
.type
== bfd_link_hash_defweak
)
13542 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13543 || h
->root
.u
.def
.section
->kept_section
!= NULL
13544 || discarded_section (h
->root
.u
.def
.section
)))
13549 /* It's not a relocation against a global symbol,
13550 but it could be a relocation against a local
13551 symbol for a discarded section. */
13553 Elf_Internal_Sym
*isym
;
13555 /* Need to: get the symbol; get the section. */
13556 isym
= &rcookie
->locsyms
[r_symndx
];
13557 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13559 && (isec
->kept_section
!= NULL
13560 || discarded_section (isec
)))
13568 /* Discard unneeded references to discarded sections.
13569 Returns -1 on error, 1 if any section's size was changed, 0 if
13570 nothing changed. This function assumes that the relocations are in
13571 sorted order, which is true for all known assemblers. */
13574 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13576 struct elf_reloc_cookie cookie
;
13581 if (info
->traditional_format
13582 || !is_elf_hash_table (info
->hash
))
13585 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13590 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13593 || i
->reloc_count
== 0
13594 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13598 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13601 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13604 if (_bfd_discard_section_stabs (abfd
, i
,
13605 elf_section_data (i
)->sec_info
,
13606 bfd_elf_reloc_symbol_deleted_p
,
13610 fini_reloc_cookie_for_section (&cookie
, i
);
13615 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13616 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13621 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13627 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13630 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13633 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13634 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13635 bfd_elf_reloc_symbol_deleted_p
,
13639 fini_reloc_cookie_for_section (&cookie
, i
);
13643 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13645 const struct elf_backend_data
*bed
;
13647 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13650 bed
= get_elf_backend_data (abfd
);
13652 if (bed
->elf_backend_discard_info
!= NULL
)
13654 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13657 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13660 fini_reloc_cookie (&cookie
, abfd
);
13664 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13665 _bfd_elf_end_eh_frame_parsing (info
);
13667 if (info
->eh_frame_hdr_type
13668 && !bfd_link_relocatable (info
)
13669 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13676 _bfd_elf_section_already_linked (bfd
*abfd
,
13678 struct bfd_link_info
*info
)
13681 const char *name
, *key
;
13682 struct bfd_section_already_linked
*l
;
13683 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13685 if (sec
->output_section
== bfd_abs_section_ptr
)
13688 flags
= sec
->flags
;
13690 /* Return if it isn't a linkonce section. A comdat group section
13691 also has SEC_LINK_ONCE set. */
13692 if ((flags
& SEC_LINK_ONCE
) == 0)
13695 /* Don't put group member sections on our list of already linked
13696 sections. They are handled as a group via their group section. */
13697 if (elf_sec_group (sec
) != NULL
)
13700 /* For a SHT_GROUP section, use the group signature as the key. */
13702 if ((flags
& SEC_GROUP
) != 0
13703 && elf_next_in_group (sec
) != NULL
13704 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13705 key
= elf_group_name (elf_next_in_group (sec
));
13708 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13709 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13710 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13713 /* Must be a user linkonce section that doesn't follow gcc's
13714 naming convention. In this case we won't be matching
13715 single member groups. */
13719 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13721 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13723 /* We may have 2 different types of sections on the list: group
13724 sections with a signature of <key> (<key> is some string),
13725 and linkonce sections named .gnu.linkonce.<type>.<key>.
13726 Match like sections. LTO plugin sections are an exception.
13727 They are always named .gnu.linkonce.t.<key> and match either
13728 type of section. */
13729 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13730 && ((flags
& SEC_GROUP
) != 0
13731 || strcmp (name
, l
->sec
->name
) == 0))
13732 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13734 /* The section has already been linked. See if we should
13735 issue a warning. */
13736 if (!_bfd_handle_already_linked (sec
, l
, info
))
13739 if (flags
& SEC_GROUP
)
13741 asection
*first
= elf_next_in_group (sec
);
13742 asection
*s
= first
;
13746 s
->output_section
= bfd_abs_section_ptr
;
13747 /* Record which group discards it. */
13748 s
->kept_section
= l
->sec
;
13749 s
= elf_next_in_group (s
);
13750 /* These lists are circular. */
13760 /* A single member comdat group section may be discarded by a
13761 linkonce section and vice versa. */
13762 if ((flags
& SEC_GROUP
) != 0)
13764 asection
*first
= elf_next_in_group (sec
);
13766 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13767 /* Check this single member group against linkonce sections. */
13768 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13769 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13770 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13772 first
->output_section
= bfd_abs_section_ptr
;
13773 first
->kept_section
= l
->sec
;
13774 sec
->output_section
= bfd_abs_section_ptr
;
13779 /* Check this linkonce section against single member groups. */
13780 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13781 if (l
->sec
->flags
& SEC_GROUP
)
13783 asection
*first
= elf_next_in_group (l
->sec
);
13786 && elf_next_in_group (first
) == first
13787 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13789 sec
->output_section
= bfd_abs_section_ptr
;
13790 sec
->kept_section
= first
;
13795 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13796 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13797 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13798 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13799 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13800 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13801 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13802 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13803 The reverse order cannot happen as there is never a bfd with only the
13804 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13805 matter as here were are looking only for cross-bfd sections. */
13807 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13808 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13809 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13810 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13812 if (abfd
!= l
->sec
->owner
)
13813 sec
->output_section
= bfd_abs_section_ptr
;
13817 /* This is the first section with this name. Record it. */
13818 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13819 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13820 return sec
->output_section
== bfd_abs_section_ptr
;
13824 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13826 return sym
->st_shndx
== SHN_COMMON
;
13830 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13836 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13838 return bfd_com_section_ptr
;
13842 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13843 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13844 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13845 bfd
*ibfd ATTRIBUTE_UNUSED
,
13846 unsigned long symndx ATTRIBUTE_UNUSED
)
13848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13849 return bed
->s
->arch_size
/ 8;
13852 /* Routines to support the creation of dynamic relocs. */
13854 /* Returns the name of the dynamic reloc section associated with SEC. */
13856 static const char *
13857 get_dynamic_reloc_section_name (bfd
* abfd
,
13859 bfd_boolean is_rela
)
13862 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13863 const char *prefix
= is_rela
? ".rela" : ".rel";
13865 if (old_name
== NULL
)
13868 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13869 sprintf (name
, "%s%s", prefix
, old_name
);
13874 /* Returns the dynamic reloc section associated with SEC.
13875 If necessary compute the name of the dynamic reloc section based
13876 on SEC's name (looked up in ABFD's string table) and the setting
13880 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13882 bfd_boolean is_rela
)
13884 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13886 if (reloc_sec
== NULL
)
13888 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13892 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13894 if (reloc_sec
!= NULL
)
13895 elf_section_data (sec
)->sreloc
= reloc_sec
;
13902 /* Returns the dynamic reloc section associated with SEC. If the
13903 section does not exist it is created and attached to the DYNOBJ
13904 bfd and stored in the SRELOC field of SEC's elf_section_data
13907 ALIGNMENT is the alignment for the newly created section and
13908 IS_RELA defines whether the name should be .rela.<SEC's name>
13909 or .rel.<SEC's name>. The section name is looked up in the
13910 string table associated with ABFD. */
13913 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13915 unsigned int alignment
,
13917 bfd_boolean is_rela
)
13919 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13921 if (reloc_sec
== NULL
)
13923 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13928 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13930 if (reloc_sec
== NULL
)
13932 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13933 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13934 if ((sec
->flags
& SEC_ALLOC
) != 0)
13935 flags
|= SEC_ALLOC
| SEC_LOAD
;
13937 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13938 if (reloc_sec
!= NULL
)
13940 /* _bfd_elf_get_sec_type_attr chooses a section type by
13941 name. Override as it may be wrong, eg. for a user
13942 section named "auto" we'll get ".relauto" which is
13943 seen to be a .rela section. */
13944 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13945 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13950 elf_section_data (sec
)->sreloc
= reloc_sec
;
13956 /* Copy the ELF symbol type and other attributes for a linker script
13957 assignment from HSRC to HDEST. Generally this should be treated as
13958 if we found a strong non-dynamic definition for HDEST (except that
13959 ld ignores multiple definition errors). */
13961 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
13962 struct bfd_link_hash_entry
*hdest
,
13963 struct bfd_link_hash_entry
*hsrc
)
13965 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
13966 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
13967 Elf_Internal_Sym isym
;
13969 ehdest
->type
= ehsrc
->type
;
13970 ehdest
->target_internal
= ehsrc
->target_internal
;
13972 isym
.st_other
= ehsrc
->other
;
13973 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
13976 /* Append a RELA relocation REL to section S in BFD. */
13979 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13981 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13982 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
13983 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
13984 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
13987 /* Append a REL relocation REL to section S in BFD. */
13990 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13993 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
13994 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
13995 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);