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
,
2453 struct bfd_elf_section_reloc_data
*reldata
)
2455 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2457 /* That allows us to calculate the size of the section. */
2458 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2460 /* The contents field must last into write_object_contents, so we
2461 allocate it with bfd_alloc rather than malloc. Also since we
2462 cannot be sure that the contents will actually be filled in,
2463 we zero the allocated space. */
2464 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2465 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2468 if (reldata
->hashes
== NULL
&& reldata
->count
)
2470 struct elf_link_hash_entry
**p
;
2472 p
= ((struct elf_link_hash_entry
**)
2473 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2477 reldata
->hashes
= p
;
2483 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2484 originated from the section given by INPUT_REL_HDR) to the
2488 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2489 asection
*input_section
,
2490 Elf_Internal_Shdr
*input_rel_hdr
,
2491 Elf_Internal_Rela
*internal_relocs
,
2492 struct elf_link_hash_entry
**rel_hash
2495 Elf_Internal_Rela
*irela
;
2496 Elf_Internal_Rela
*irelaend
;
2498 struct bfd_elf_section_reloc_data
*output_reldata
;
2499 asection
*output_section
;
2500 const struct elf_backend_data
*bed
;
2501 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2502 struct bfd_elf_section_data
*esdo
;
2504 output_section
= input_section
->output_section
;
2506 bed
= get_elf_backend_data (output_bfd
);
2507 esdo
= elf_section_data (output_section
);
2508 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2510 output_reldata
= &esdo
->rel
;
2511 swap_out
= bed
->s
->swap_reloc_out
;
2513 else if (esdo
->rela
.hdr
2514 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2516 output_reldata
= &esdo
->rela
;
2517 swap_out
= bed
->s
->swap_reloca_out
;
2521 (*_bfd_error_handler
)
2522 (_("%B: relocation size mismatch in %B section %A"),
2523 output_bfd
, input_section
->owner
, input_section
);
2524 bfd_set_error (bfd_error_wrong_format
);
2528 erel
= output_reldata
->hdr
->contents
;
2529 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2530 irela
= internal_relocs
;
2531 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2532 * bed
->s
->int_rels_per_ext_rel
);
2533 while (irela
< irelaend
)
2535 (*swap_out
) (output_bfd
, irela
, erel
);
2536 irela
+= bed
->s
->int_rels_per_ext_rel
;
2537 erel
+= input_rel_hdr
->sh_entsize
;
2540 /* Bump the counter, so that we know where to add the next set of
2542 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2547 /* Make weak undefined symbols in PIE dynamic. */
2550 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2551 struct elf_link_hash_entry
*h
)
2553 if (bfd_link_pie (info
)
2555 && h
->root
.type
== bfd_link_hash_undefweak
)
2556 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2561 /* Fix up the flags for a symbol. This handles various cases which
2562 can only be fixed after all the input files are seen. This is
2563 currently called by both adjust_dynamic_symbol and
2564 assign_sym_version, which is unnecessary but perhaps more robust in
2565 the face of future changes. */
2568 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2569 struct elf_info_failed
*eif
)
2571 const struct elf_backend_data
*bed
;
2573 /* If this symbol was mentioned in a non-ELF file, try to set
2574 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2575 permit a non-ELF file to correctly refer to a symbol defined in
2576 an ELF dynamic object. */
2579 while (h
->root
.type
== bfd_link_hash_indirect
)
2580 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2582 if (h
->root
.type
!= bfd_link_hash_defined
2583 && h
->root
.type
!= bfd_link_hash_defweak
)
2586 h
->ref_regular_nonweak
= 1;
2590 if (h
->root
.u
.def
.section
->owner
!= NULL
2591 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2592 == bfd_target_elf_flavour
))
2595 h
->ref_regular_nonweak
= 1;
2601 if (h
->dynindx
== -1
2605 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2614 /* Unfortunately, NON_ELF is only correct if the symbol
2615 was first seen in a non-ELF file. Fortunately, if the symbol
2616 was first seen in an ELF file, we're probably OK unless the
2617 symbol was defined in a non-ELF file. Catch that case here.
2618 FIXME: We're still in trouble if the symbol was first seen in
2619 a dynamic object, and then later in a non-ELF regular object. */
2620 if ((h
->root
.type
== bfd_link_hash_defined
2621 || h
->root
.type
== bfd_link_hash_defweak
)
2623 && (h
->root
.u
.def
.section
->owner
!= NULL
2624 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2625 != bfd_target_elf_flavour
)
2626 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2627 && !h
->def_dynamic
)))
2631 /* Backend specific symbol fixup. */
2632 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2633 if (bed
->elf_backend_fixup_symbol
2634 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2637 /* If this is a final link, and the symbol was defined as a common
2638 symbol in a regular object file, and there was no definition in
2639 any dynamic object, then the linker will have allocated space for
2640 the symbol in a common section but the DEF_REGULAR
2641 flag will not have been set. */
2642 if (h
->root
.type
== bfd_link_hash_defined
2646 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2649 /* If -Bsymbolic was used (which means to bind references to global
2650 symbols to the definition within the shared object), and this
2651 symbol was defined in a regular object, then it actually doesn't
2652 need a PLT entry. Likewise, if the symbol has non-default
2653 visibility. If the symbol has hidden or internal visibility, we
2654 will force it local. */
2656 && bfd_link_pic (eif
->info
)
2657 && is_elf_hash_table (eif
->info
->hash
)
2658 && (SYMBOLIC_BIND (eif
->info
, h
)
2659 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2662 bfd_boolean force_local
;
2664 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2665 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2666 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2669 /* If a weak undefined symbol has non-default visibility, we also
2670 hide it from the dynamic linker. */
2671 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2672 && h
->root
.type
== bfd_link_hash_undefweak
)
2673 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2675 /* If this is a weak defined symbol in a dynamic object, and we know
2676 the real definition in the dynamic object, copy interesting flags
2677 over to the real definition. */
2678 if (h
->u
.weakdef
!= NULL
)
2680 /* If the real definition is defined by a regular object file,
2681 don't do anything special. See the longer description in
2682 _bfd_elf_adjust_dynamic_symbol, below. */
2683 if (h
->u
.weakdef
->def_regular
)
2684 h
->u
.weakdef
= NULL
;
2687 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2689 while (h
->root
.type
== bfd_link_hash_indirect
)
2690 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2692 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2693 || h
->root
.type
== bfd_link_hash_defweak
);
2694 BFD_ASSERT (weakdef
->def_dynamic
);
2695 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2696 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2697 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2704 /* Make the backend pick a good value for a dynamic symbol. This is
2705 called via elf_link_hash_traverse, and also calls itself
2709 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2711 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2713 const struct elf_backend_data
*bed
;
2715 if (! is_elf_hash_table (eif
->info
->hash
))
2718 /* Ignore indirect symbols. These are added by the versioning code. */
2719 if (h
->root
.type
== bfd_link_hash_indirect
)
2722 /* Fix the symbol flags. */
2723 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2726 /* If this symbol does not require a PLT entry, and it is not
2727 defined by a dynamic object, or is not referenced by a regular
2728 object, ignore it. We do have to handle a weak defined symbol,
2729 even if no regular object refers to it, if we decided to add it
2730 to the dynamic symbol table. FIXME: Do we normally need to worry
2731 about symbols which are defined by one dynamic object and
2732 referenced by another one? */
2734 && h
->type
!= STT_GNU_IFUNC
2738 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2740 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2744 /* If we've already adjusted this symbol, don't do it again. This
2745 can happen via a recursive call. */
2746 if (h
->dynamic_adjusted
)
2749 /* Don't look at this symbol again. Note that we must set this
2750 after checking the above conditions, because we may look at a
2751 symbol once, decide not to do anything, and then get called
2752 recursively later after REF_REGULAR is set below. */
2753 h
->dynamic_adjusted
= 1;
2755 /* If this is a weak definition, and we know a real definition, and
2756 the real symbol is not itself defined by a regular object file,
2757 then get a good value for the real definition. We handle the
2758 real symbol first, for the convenience of the backend routine.
2760 Note that there is a confusing case here. If the real definition
2761 is defined by a regular object file, we don't get the real symbol
2762 from the dynamic object, but we do get the weak symbol. If the
2763 processor backend uses a COPY reloc, then if some routine in the
2764 dynamic object changes the real symbol, we will not see that
2765 change in the corresponding weak symbol. This is the way other
2766 ELF linkers work as well, and seems to be a result of the shared
2769 I will clarify this issue. Most SVR4 shared libraries define the
2770 variable _timezone and define timezone as a weak synonym. The
2771 tzset call changes _timezone. If you write
2772 extern int timezone;
2774 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2775 you might expect that, since timezone is a synonym for _timezone,
2776 the same number will print both times. However, if the processor
2777 backend uses a COPY reloc, then actually timezone will be copied
2778 into your process image, and, since you define _timezone
2779 yourself, _timezone will not. Thus timezone and _timezone will
2780 wind up at different memory locations. The tzset call will set
2781 _timezone, leaving timezone unchanged. */
2783 if (h
->u
.weakdef
!= NULL
)
2785 /* If we get to this point, there is an implicit reference to
2786 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2787 h
->u
.weakdef
->ref_regular
= 1;
2789 /* Ensure that the backend adjust_dynamic_symbol function sees
2790 H->U.WEAKDEF before H by recursively calling ourselves. */
2791 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2795 /* If a symbol has no type and no size and does not require a PLT
2796 entry, then we are probably about to do the wrong thing here: we
2797 are probably going to create a COPY reloc for an empty object.
2798 This case can arise when a shared object is built with assembly
2799 code, and the assembly code fails to set the symbol type. */
2801 && h
->type
== STT_NOTYPE
2803 (*_bfd_error_handler
)
2804 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2805 h
->root
.root
.string
);
2807 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2808 bed
= get_elf_backend_data (dynobj
);
2810 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2819 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2823 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2824 struct elf_link_hash_entry
*h
,
2827 unsigned int power_of_two
;
2829 asection
*sec
= h
->root
.u
.def
.section
;
2831 /* The section aligment of definition is the maximum alignment
2832 requirement of symbols defined in the section. Since we don't
2833 know the symbol alignment requirement, we start with the
2834 maximum alignment and check low bits of the symbol address
2835 for the minimum alignment. */
2836 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2837 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2838 while ((h
->root
.u
.def
.value
& mask
) != 0)
2844 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2847 /* Adjust the section alignment if needed. */
2848 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2853 /* We make sure that the symbol will be aligned properly. */
2854 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2856 /* Define the symbol as being at this point in DYNBSS. */
2857 h
->root
.u
.def
.section
= dynbss
;
2858 h
->root
.u
.def
.value
= dynbss
->size
;
2860 /* Increment the size of DYNBSS to make room for the symbol. */
2861 dynbss
->size
+= h
->size
;
2863 /* No error if extern_protected_data is true. */
2864 if (h
->protected_def
2865 && (!info
->extern_protected_data
2866 || (info
->extern_protected_data
< 0
2867 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2868 info
->callbacks
->einfo
2869 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2870 h
->root
.root
.string
);
2875 /* Adjust all external symbols pointing into SEC_MERGE sections
2876 to reflect the object merging within the sections. */
2879 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2883 if ((h
->root
.type
== bfd_link_hash_defined
2884 || h
->root
.type
== bfd_link_hash_defweak
)
2885 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2886 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2888 bfd
*output_bfd
= (bfd
*) data
;
2890 h
->root
.u
.def
.value
=
2891 _bfd_merged_section_offset (output_bfd
,
2892 &h
->root
.u
.def
.section
,
2893 elf_section_data (sec
)->sec_info
,
2894 h
->root
.u
.def
.value
);
2900 /* Returns false if the symbol referred to by H should be considered
2901 to resolve local to the current module, and true if it should be
2902 considered to bind dynamically. */
2905 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2906 struct bfd_link_info
*info
,
2907 bfd_boolean not_local_protected
)
2909 bfd_boolean binding_stays_local_p
;
2910 const struct elf_backend_data
*bed
;
2911 struct elf_link_hash_table
*hash_table
;
2916 while (h
->root
.type
== bfd_link_hash_indirect
2917 || h
->root
.type
== bfd_link_hash_warning
)
2918 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2920 /* If it was forced local, then clearly it's not dynamic. */
2921 if (h
->dynindx
== -1)
2923 if (h
->forced_local
)
2926 /* Identify the cases where name binding rules say that a
2927 visible symbol resolves locally. */
2928 binding_stays_local_p
= (bfd_link_executable (info
)
2929 || SYMBOLIC_BIND (info
, h
));
2931 switch (ELF_ST_VISIBILITY (h
->other
))
2938 hash_table
= elf_hash_table (info
);
2939 if (!is_elf_hash_table (hash_table
))
2942 bed
= get_elf_backend_data (hash_table
->dynobj
);
2944 /* Proper resolution for function pointer equality may require
2945 that these symbols perhaps be resolved dynamically, even though
2946 we should be resolving them to the current module. */
2947 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2948 binding_stays_local_p
= TRUE
;
2955 /* If it isn't defined locally, then clearly it's dynamic. */
2956 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2959 /* Otherwise, the symbol is dynamic if binding rules don't tell
2960 us that it remains local. */
2961 return !binding_stays_local_p
;
2964 /* Return true if the symbol referred to by H should be considered
2965 to resolve local to the current module, and false otherwise. Differs
2966 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2967 undefined symbols. The two functions are virtually identical except
2968 for the place where forced_local and dynindx == -1 are tested. If
2969 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2970 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2971 the symbol is local only for defined symbols.
2972 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2973 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2974 treatment of undefined weak symbols. For those that do not make
2975 undefined weak symbols dynamic, both functions may return false. */
2978 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2979 struct bfd_link_info
*info
,
2980 bfd_boolean local_protected
)
2982 const struct elf_backend_data
*bed
;
2983 struct elf_link_hash_table
*hash_table
;
2985 /* If it's a local sym, of course we resolve locally. */
2989 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2990 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2991 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2994 /* Common symbols that become definitions don't get the DEF_REGULAR
2995 flag set, so test it first, and don't bail out. */
2996 if (ELF_COMMON_DEF_P (h
))
2998 /* If we don't have a definition in a regular file, then we can't
2999 resolve locally. The sym is either undefined or dynamic. */
3000 else if (!h
->def_regular
)
3003 /* Forced local symbols resolve locally. */
3004 if (h
->forced_local
)
3007 /* As do non-dynamic symbols. */
3008 if (h
->dynindx
== -1)
3011 /* At this point, we know the symbol is defined and dynamic. In an
3012 executable it must resolve locally, likewise when building symbolic
3013 shared libraries. */
3014 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3017 /* Now deal with defined dynamic symbols in shared libraries. Ones
3018 with default visibility might not resolve locally. */
3019 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3022 hash_table
= elf_hash_table (info
);
3023 if (!is_elf_hash_table (hash_table
))
3026 bed
= get_elf_backend_data (hash_table
->dynobj
);
3028 /* If extern_protected_data is false, STV_PROTECTED non-function
3029 symbols are local. */
3030 if ((!info
->extern_protected_data
3031 || (info
->extern_protected_data
< 0
3032 && !bed
->extern_protected_data
))
3033 && !bed
->is_function_type (h
->type
))
3036 /* Function pointer equality tests may require that STV_PROTECTED
3037 symbols be treated as dynamic symbols. If the address of a
3038 function not defined in an executable is set to that function's
3039 plt entry in the executable, then the address of the function in
3040 a shared library must also be the plt entry in the executable. */
3041 return local_protected
;
3044 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3045 aligned. Returns the first TLS output section. */
3047 struct bfd_section
*
3048 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3050 struct bfd_section
*sec
, *tls
;
3051 unsigned int align
= 0;
3053 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3054 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3058 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3059 if (sec
->alignment_power
> align
)
3060 align
= sec
->alignment_power
;
3062 elf_hash_table (info
)->tls_sec
= tls
;
3064 /* Ensure the alignment of the first section is the largest alignment,
3065 so that the tls segment starts aligned. */
3067 tls
->alignment_power
= align
;
3072 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3074 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3075 Elf_Internal_Sym
*sym
)
3077 const struct elf_backend_data
*bed
;
3079 /* Local symbols do not count, but target specific ones might. */
3080 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3081 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3084 bed
= get_elf_backend_data (abfd
);
3085 /* Function symbols do not count. */
3086 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3089 /* If the section is undefined, then so is the symbol. */
3090 if (sym
->st_shndx
== SHN_UNDEF
)
3093 /* If the symbol is defined in the common section, then
3094 it is a common definition and so does not count. */
3095 if (bed
->common_definition (sym
))
3098 /* If the symbol is in a target specific section then we
3099 must rely upon the backend to tell us what it is. */
3100 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3101 /* FIXME - this function is not coded yet:
3103 return _bfd_is_global_symbol_definition (abfd, sym);
3105 Instead for now assume that the definition is not global,
3106 Even if this is wrong, at least the linker will behave
3107 in the same way that it used to do. */
3113 /* Search the symbol table of the archive element of the archive ABFD
3114 whose archive map contains a mention of SYMDEF, and determine if
3115 the symbol is defined in this element. */
3117 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3119 Elf_Internal_Shdr
* hdr
;
3123 Elf_Internal_Sym
*isymbuf
;
3124 Elf_Internal_Sym
*isym
;
3125 Elf_Internal_Sym
*isymend
;
3128 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3132 if (! bfd_check_format (abfd
, bfd_object
))
3135 /* Select the appropriate symbol table. If we don't know if the
3136 object file is an IR object, give linker LTO plugin a chance to
3137 get the correct symbol table. */
3138 if (abfd
->plugin_format
== bfd_plugin_yes
3139 #if BFD_SUPPORTS_PLUGINS
3140 || (abfd
->plugin_format
== bfd_plugin_unknown
3141 && bfd_link_plugin_object_p (abfd
))
3145 /* Use the IR symbol table if the object has been claimed by
3147 abfd
= abfd
->plugin_dummy_bfd
;
3148 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3150 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3151 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3153 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3155 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3157 /* The sh_info field of the symtab header tells us where the
3158 external symbols start. We don't care about the local symbols. */
3159 if (elf_bad_symtab (abfd
))
3161 extsymcount
= symcount
;
3166 extsymcount
= symcount
- hdr
->sh_info
;
3167 extsymoff
= hdr
->sh_info
;
3170 if (extsymcount
== 0)
3173 /* Read in the symbol table. */
3174 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3176 if (isymbuf
== NULL
)
3179 /* Scan the symbol table looking for SYMDEF. */
3181 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3185 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3190 if (strcmp (name
, symdef
->name
) == 0)
3192 result
= is_global_data_symbol_definition (abfd
, isym
);
3202 /* Add an entry to the .dynamic table. */
3205 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3209 struct elf_link_hash_table
*hash_table
;
3210 const struct elf_backend_data
*bed
;
3212 bfd_size_type newsize
;
3213 bfd_byte
*newcontents
;
3214 Elf_Internal_Dyn dyn
;
3216 hash_table
= elf_hash_table (info
);
3217 if (! is_elf_hash_table (hash_table
))
3220 bed
= get_elf_backend_data (hash_table
->dynobj
);
3221 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3222 BFD_ASSERT (s
!= NULL
);
3224 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3225 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3226 if (newcontents
== NULL
)
3230 dyn
.d_un
.d_val
= val
;
3231 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3234 s
->contents
= newcontents
;
3239 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3240 otherwise just check whether one already exists. Returns -1 on error,
3241 1 if a DT_NEEDED tag already exists, and 0 on success. */
3244 elf_add_dt_needed_tag (bfd
*abfd
,
3245 struct bfd_link_info
*info
,
3249 struct elf_link_hash_table
*hash_table
;
3252 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3255 hash_table
= elf_hash_table (info
);
3256 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3257 if (strindex
== (size_t) -1)
3260 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3263 const struct elf_backend_data
*bed
;
3266 bed
= get_elf_backend_data (hash_table
->dynobj
);
3267 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3269 for (extdyn
= sdyn
->contents
;
3270 extdyn
< sdyn
->contents
+ sdyn
->size
;
3271 extdyn
+= bed
->s
->sizeof_dyn
)
3273 Elf_Internal_Dyn dyn
;
3275 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3276 if (dyn
.d_tag
== DT_NEEDED
3277 && dyn
.d_un
.d_val
== strindex
)
3279 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3287 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3290 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3294 /* We were just checking for existence of the tag. */
3295 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3300 /* Return true if SONAME is on the needed list between NEEDED and STOP
3301 (or the end of list if STOP is NULL), and needed by a library that
3305 on_needed_list (const char *soname
,
3306 struct bfd_link_needed_list
*needed
,
3307 struct bfd_link_needed_list
*stop
)
3309 struct bfd_link_needed_list
*look
;
3310 for (look
= needed
; look
!= stop
; look
= look
->next
)
3311 if (strcmp (soname
, look
->name
) == 0
3312 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3313 /* If needed by a library that itself is not directly
3314 needed, recursively check whether that library is
3315 indirectly needed. Since we add DT_NEEDED entries to
3316 the end of the list, library dependencies appear after
3317 the library. Therefore search prior to the current
3318 LOOK, preventing possible infinite recursion. */
3319 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3325 /* Sort symbol by value, section, and size. */
3327 elf_sort_symbol (const void *arg1
, const void *arg2
)
3329 const struct elf_link_hash_entry
*h1
;
3330 const struct elf_link_hash_entry
*h2
;
3331 bfd_signed_vma vdiff
;
3333 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3334 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3335 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3337 return vdiff
> 0 ? 1 : -1;
3340 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3342 return sdiff
> 0 ? 1 : -1;
3344 vdiff
= h1
->size
- h2
->size
;
3345 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3348 /* This function is used to adjust offsets into .dynstr for
3349 dynamic symbols. This is called via elf_link_hash_traverse. */
3352 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3354 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3356 if (h
->dynindx
!= -1)
3357 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3361 /* Assign string offsets in .dynstr, update all structures referencing
3365 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3367 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3368 struct elf_link_local_dynamic_entry
*entry
;
3369 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3370 bfd
*dynobj
= hash_table
->dynobj
;
3373 const struct elf_backend_data
*bed
;
3376 _bfd_elf_strtab_finalize (dynstr
);
3377 size
= _bfd_elf_strtab_size (dynstr
);
3379 bed
= get_elf_backend_data (dynobj
);
3380 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3381 BFD_ASSERT (sdyn
!= NULL
);
3383 /* Update all .dynamic entries referencing .dynstr strings. */
3384 for (extdyn
= sdyn
->contents
;
3385 extdyn
< sdyn
->contents
+ sdyn
->size
;
3386 extdyn
+= bed
->s
->sizeof_dyn
)
3388 Elf_Internal_Dyn dyn
;
3390 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3394 dyn
.d_un
.d_val
= size
;
3404 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3409 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3412 /* Now update local dynamic symbols. */
3413 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3414 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3415 entry
->isym
.st_name
);
3417 /* And the rest of dynamic symbols. */
3418 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3420 /* Adjust version definitions. */
3421 if (elf_tdata (output_bfd
)->cverdefs
)
3426 Elf_Internal_Verdef def
;
3427 Elf_Internal_Verdaux defaux
;
3429 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3433 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3435 p
+= sizeof (Elf_External_Verdef
);
3436 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3438 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3440 _bfd_elf_swap_verdaux_in (output_bfd
,
3441 (Elf_External_Verdaux
*) p
, &defaux
);
3442 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3444 _bfd_elf_swap_verdaux_out (output_bfd
,
3445 &defaux
, (Elf_External_Verdaux
*) p
);
3446 p
+= sizeof (Elf_External_Verdaux
);
3449 while (def
.vd_next
);
3452 /* Adjust version references. */
3453 if (elf_tdata (output_bfd
)->verref
)
3458 Elf_Internal_Verneed need
;
3459 Elf_Internal_Vernaux needaux
;
3461 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3465 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3467 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3468 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3469 (Elf_External_Verneed
*) p
);
3470 p
+= sizeof (Elf_External_Verneed
);
3471 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3473 _bfd_elf_swap_vernaux_in (output_bfd
,
3474 (Elf_External_Vernaux
*) p
, &needaux
);
3475 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3477 _bfd_elf_swap_vernaux_out (output_bfd
,
3479 (Elf_External_Vernaux
*) p
);
3480 p
+= sizeof (Elf_External_Vernaux
);
3483 while (need
.vn_next
);
3489 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3490 The default is to only match when the INPUT and OUTPUT are exactly
3494 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3495 const bfd_target
*output
)
3497 return input
== output
;
3500 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3501 This version is used when different targets for the same architecture
3502 are virtually identical. */
3505 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3506 const bfd_target
*output
)
3508 const struct elf_backend_data
*obed
, *ibed
;
3510 if (input
== output
)
3513 ibed
= xvec_get_elf_backend_data (input
);
3514 obed
= xvec_get_elf_backend_data (output
);
3516 if (ibed
->arch
!= obed
->arch
)
3519 /* If both backends are using this function, deem them compatible. */
3520 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3523 /* Make a special call to the linker "notice" function to tell it that
3524 we are about to handle an as-needed lib, or have finished
3525 processing the lib. */
3528 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3529 struct bfd_link_info
*info
,
3530 enum notice_asneeded_action act
)
3532 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3535 /* Check relocations an ELF object file. */
3538 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3540 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3541 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3543 /* If this object is the same format as the output object, and it is
3544 not a shared library, then let the backend look through the
3547 This is required to build global offset table entries and to
3548 arrange for dynamic relocs. It is not required for the
3549 particular common case of linking non PIC code, even when linking
3550 against shared libraries, but unfortunately there is no way of
3551 knowing whether an object file has been compiled PIC or not.
3552 Looking through the relocs is not particularly time consuming.
3553 The problem is that we must either (1) keep the relocs in memory,
3554 which causes the linker to require additional runtime memory or
3555 (2) read the relocs twice from the input file, which wastes time.
3556 This would be a good case for using mmap.
3558 I have no idea how to handle linking PIC code into a file of a
3559 different format. It probably can't be done. */
3560 if ((abfd
->flags
& DYNAMIC
) == 0
3561 && is_elf_hash_table (htab
)
3562 && bed
->check_relocs
!= NULL
3563 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3564 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3568 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3570 Elf_Internal_Rela
*internal_relocs
;
3573 /* Don't check relocations in excluded sections. */
3574 if ((o
->flags
& SEC_RELOC
) == 0
3575 || (o
->flags
& SEC_EXCLUDE
) != 0
3576 || o
->reloc_count
== 0
3577 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3578 && (o
->flags
& SEC_DEBUGGING
) != 0)
3579 || bfd_is_abs_section (o
->output_section
))
3582 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3584 if (internal_relocs
== NULL
)
3587 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3589 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3590 free (internal_relocs
);
3600 /* Add symbols from an ELF object file to the linker hash table. */
3603 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3605 Elf_Internal_Ehdr
*ehdr
;
3606 Elf_Internal_Shdr
*hdr
;
3610 struct elf_link_hash_entry
**sym_hash
;
3611 bfd_boolean dynamic
;
3612 Elf_External_Versym
*extversym
= NULL
;
3613 Elf_External_Versym
*ever
;
3614 struct elf_link_hash_entry
*weaks
;
3615 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3616 size_t nondeflt_vers_cnt
= 0;
3617 Elf_Internal_Sym
*isymbuf
= NULL
;
3618 Elf_Internal_Sym
*isym
;
3619 Elf_Internal_Sym
*isymend
;
3620 const struct elf_backend_data
*bed
;
3621 bfd_boolean add_needed
;
3622 struct elf_link_hash_table
*htab
;
3624 void *alloc_mark
= NULL
;
3625 struct bfd_hash_entry
**old_table
= NULL
;
3626 unsigned int old_size
= 0;
3627 unsigned int old_count
= 0;
3628 void *old_tab
= NULL
;
3630 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3631 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3632 void *old_strtab
= NULL
;
3635 bfd_boolean just_syms
;
3637 htab
= elf_hash_table (info
);
3638 bed
= get_elf_backend_data (abfd
);
3640 if ((abfd
->flags
& DYNAMIC
) == 0)
3646 /* You can't use -r against a dynamic object. Also, there's no
3647 hope of using a dynamic object which does not exactly match
3648 the format of the output file. */
3649 if (bfd_link_relocatable (info
)
3650 || !is_elf_hash_table (htab
)
3651 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3653 if (bfd_link_relocatable (info
))
3654 bfd_set_error (bfd_error_invalid_operation
);
3656 bfd_set_error (bfd_error_wrong_format
);
3661 ehdr
= elf_elfheader (abfd
);
3662 if (info
->warn_alternate_em
3663 && bed
->elf_machine_code
!= ehdr
->e_machine
3664 && ((bed
->elf_machine_alt1
!= 0
3665 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3666 || (bed
->elf_machine_alt2
!= 0
3667 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3668 info
->callbacks
->einfo
3669 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3670 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3672 /* As a GNU extension, any input sections which are named
3673 .gnu.warning.SYMBOL are treated as warning symbols for the given
3674 symbol. This differs from .gnu.warning sections, which generate
3675 warnings when they are included in an output file. */
3676 /* PR 12761: Also generate this warning when building shared libraries. */
3677 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3681 name
= bfd_get_section_name (abfd
, s
);
3682 if (CONST_STRNEQ (name
, ".gnu.warning."))
3687 name
+= sizeof ".gnu.warning." - 1;
3689 /* If this is a shared object, then look up the symbol
3690 in the hash table. If it is there, and it is already
3691 been defined, then we will not be using the entry
3692 from this shared object, so we don't need to warn.
3693 FIXME: If we see the definition in a regular object
3694 later on, we will warn, but we shouldn't. The only
3695 fix is to keep track of what warnings we are supposed
3696 to emit, and then handle them all at the end of the
3700 struct elf_link_hash_entry
*h
;
3702 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3704 /* FIXME: What about bfd_link_hash_common? */
3706 && (h
->root
.type
== bfd_link_hash_defined
3707 || h
->root
.type
== bfd_link_hash_defweak
))
3712 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3716 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3721 if (! (_bfd_generic_link_add_one_symbol
3722 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3723 FALSE
, bed
->collect
, NULL
)))
3726 if (bfd_link_executable (info
))
3728 /* Clobber the section size so that the warning does
3729 not get copied into the output file. */
3732 /* Also set SEC_EXCLUDE, so that symbols defined in
3733 the warning section don't get copied to the output. */
3734 s
->flags
|= SEC_EXCLUDE
;
3739 just_syms
= ((s
= abfd
->sections
) != NULL
3740 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3745 /* If we are creating a shared library, create all the dynamic
3746 sections immediately. We need to attach them to something,
3747 so we attach them to this BFD, provided it is the right
3748 format and is not from ld --just-symbols. Always create the
3749 dynamic sections for -E/--dynamic-list. FIXME: If there
3750 are no input BFD's of the same format as the output, we can't
3751 make a shared library. */
3753 && (bfd_link_pic (info
)
3754 || (!bfd_link_relocatable (info
)
3755 && (info
->export_dynamic
|| info
->dynamic
)))
3756 && is_elf_hash_table (htab
)
3757 && info
->output_bfd
->xvec
== abfd
->xvec
3758 && !htab
->dynamic_sections_created
)
3760 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3764 else if (!is_elf_hash_table (htab
))
3768 const char *soname
= NULL
;
3770 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3773 /* ld --just-symbols and dynamic objects don't mix very well.
3774 ld shouldn't allow it. */
3778 /* If this dynamic lib was specified on the command line with
3779 --as-needed in effect, then we don't want to add a DT_NEEDED
3780 tag unless the lib is actually used. Similary for libs brought
3781 in by another lib's DT_NEEDED. When --no-add-needed is used
3782 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3783 any dynamic library in DT_NEEDED tags in the dynamic lib at
3785 add_needed
= (elf_dyn_lib_class (abfd
)
3786 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3787 | DYN_NO_NEEDED
)) == 0;
3789 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3794 unsigned int elfsec
;
3795 unsigned long shlink
;
3797 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3804 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3805 if (elfsec
== SHN_BAD
)
3806 goto error_free_dyn
;
3807 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3809 for (extdyn
= dynbuf
;
3810 extdyn
< dynbuf
+ s
->size
;
3811 extdyn
+= bed
->s
->sizeof_dyn
)
3813 Elf_Internal_Dyn dyn
;
3815 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3816 if (dyn
.d_tag
== DT_SONAME
)
3818 unsigned int tagv
= dyn
.d_un
.d_val
;
3819 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3821 goto error_free_dyn
;
3823 if (dyn
.d_tag
== DT_NEEDED
)
3825 struct bfd_link_needed_list
*n
, **pn
;
3827 unsigned int tagv
= dyn
.d_un
.d_val
;
3829 amt
= sizeof (struct bfd_link_needed_list
);
3830 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3831 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3832 if (n
== NULL
|| fnm
== NULL
)
3833 goto error_free_dyn
;
3834 amt
= strlen (fnm
) + 1;
3835 anm
= (char *) bfd_alloc (abfd
, amt
);
3837 goto error_free_dyn
;
3838 memcpy (anm
, fnm
, amt
);
3842 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3846 if (dyn
.d_tag
== DT_RUNPATH
)
3848 struct bfd_link_needed_list
*n
, **pn
;
3850 unsigned int tagv
= dyn
.d_un
.d_val
;
3852 amt
= sizeof (struct bfd_link_needed_list
);
3853 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3854 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3855 if (n
== NULL
|| fnm
== NULL
)
3856 goto error_free_dyn
;
3857 amt
= strlen (fnm
) + 1;
3858 anm
= (char *) bfd_alloc (abfd
, amt
);
3860 goto error_free_dyn
;
3861 memcpy (anm
, fnm
, amt
);
3865 for (pn
= & runpath
;
3871 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3872 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3874 struct bfd_link_needed_list
*n
, **pn
;
3876 unsigned int tagv
= dyn
.d_un
.d_val
;
3878 amt
= sizeof (struct bfd_link_needed_list
);
3879 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3880 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3881 if (n
== NULL
|| fnm
== NULL
)
3882 goto error_free_dyn
;
3883 amt
= strlen (fnm
) + 1;
3884 anm
= (char *) bfd_alloc (abfd
, amt
);
3886 goto error_free_dyn
;
3887 memcpy (anm
, fnm
, amt
);
3897 if (dyn
.d_tag
== DT_AUDIT
)
3899 unsigned int tagv
= dyn
.d_un
.d_val
;
3900 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3907 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3908 frees all more recently bfd_alloc'd blocks as well. */
3914 struct bfd_link_needed_list
**pn
;
3915 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3920 /* We do not want to include any of the sections in a dynamic
3921 object in the output file. We hack by simply clobbering the
3922 list of sections in the BFD. This could be handled more
3923 cleanly by, say, a new section flag; the existing
3924 SEC_NEVER_LOAD flag is not the one we want, because that one
3925 still implies that the section takes up space in the output
3927 bfd_section_list_clear (abfd
);
3929 /* Find the name to use in a DT_NEEDED entry that refers to this
3930 object. If the object has a DT_SONAME entry, we use it.
3931 Otherwise, if the generic linker stuck something in
3932 elf_dt_name, we use that. Otherwise, we just use the file
3934 if (soname
== NULL
|| *soname
== '\0')
3936 soname
= elf_dt_name (abfd
);
3937 if (soname
== NULL
|| *soname
== '\0')
3938 soname
= bfd_get_filename (abfd
);
3941 /* Save the SONAME because sometimes the linker emulation code
3942 will need to know it. */
3943 elf_dt_name (abfd
) = soname
;
3945 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3949 /* If we have already included this dynamic object in the
3950 link, just ignore it. There is no reason to include a
3951 particular dynamic object more than once. */
3955 /* Save the DT_AUDIT entry for the linker emulation code. */
3956 elf_dt_audit (abfd
) = audit
;
3959 /* If this is a dynamic object, we always link against the .dynsym
3960 symbol table, not the .symtab symbol table. The dynamic linker
3961 will only see the .dynsym symbol table, so there is no reason to
3962 look at .symtab for a dynamic object. */
3964 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3965 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3967 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3969 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3971 /* The sh_info field of the symtab header tells us where the
3972 external symbols start. We don't care about the local symbols at
3974 if (elf_bad_symtab (abfd
))
3976 extsymcount
= symcount
;
3981 extsymcount
= symcount
- hdr
->sh_info
;
3982 extsymoff
= hdr
->sh_info
;
3985 sym_hash
= elf_sym_hashes (abfd
);
3986 if (extsymcount
!= 0)
3988 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3990 if (isymbuf
== NULL
)
3993 if (sym_hash
== NULL
)
3995 /* We store a pointer to the hash table entry for each
3998 amt
*= sizeof (struct elf_link_hash_entry
*);
3999 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4000 if (sym_hash
== NULL
)
4001 goto error_free_sym
;
4002 elf_sym_hashes (abfd
) = sym_hash
;
4008 /* Read in any version definitions. */
4009 if (!_bfd_elf_slurp_version_tables (abfd
,
4010 info
->default_imported_symver
))
4011 goto error_free_sym
;
4013 /* Read in the symbol versions, but don't bother to convert them
4014 to internal format. */
4015 if (elf_dynversym (abfd
) != 0)
4017 Elf_Internal_Shdr
*versymhdr
;
4019 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4020 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4021 if (extversym
== NULL
)
4022 goto error_free_sym
;
4023 amt
= versymhdr
->sh_size
;
4024 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4025 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4026 goto error_free_vers
;
4030 /* If we are loading an as-needed shared lib, save the symbol table
4031 state before we start adding symbols. If the lib turns out
4032 to be unneeded, restore the state. */
4033 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4038 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4040 struct bfd_hash_entry
*p
;
4041 struct elf_link_hash_entry
*h
;
4043 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4045 h
= (struct elf_link_hash_entry
*) p
;
4046 entsize
+= htab
->root
.table
.entsize
;
4047 if (h
->root
.type
== bfd_link_hash_warning
)
4048 entsize
+= htab
->root
.table
.entsize
;
4052 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4053 old_tab
= bfd_malloc (tabsize
+ entsize
);
4054 if (old_tab
== NULL
)
4055 goto error_free_vers
;
4057 /* Remember the current objalloc pointer, so that all mem for
4058 symbols added can later be reclaimed. */
4059 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4060 if (alloc_mark
== NULL
)
4061 goto error_free_vers
;
4063 /* Make a special call to the linker "notice" function to
4064 tell it that we are about to handle an as-needed lib. */
4065 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4066 goto error_free_vers
;
4068 /* Clone the symbol table. Remember some pointers into the
4069 symbol table, and dynamic symbol count. */
4070 old_ent
= (char *) old_tab
+ tabsize
;
4071 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4072 old_undefs
= htab
->root
.undefs
;
4073 old_undefs_tail
= htab
->root
.undefs_tail
;
4074 old_table
= htab
->root
.table
.table
;
4075 old_size
= htab
->root
.table
.size
;
4076 old_count
= htab
->root
.table
.count
;
4077 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4078 if (old_strtab
== NULL
)
4079 goto error_free_vers
;
4081 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4083 struct bfd_hash_entry
*p
;
4084 struct elf_link_hash_entry
*h
;
4086 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4088 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4089 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4090 h
= (struct elf_link_hash_entry
*) p
;
4091 if (h
->root
.type
== bfd_link_hash_warning
)
4093 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4094 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4101 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4102 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4104 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4108 asection
*sec
, *new_sec
;
4111 struct elf_link_hash_entry
*h
;
4112 struct elf_link_hash_entry
*hi
;
4113 bfd_boolean definition
;
4114 bfd_boolean size_change_ok
;
4115 bfd_boolean type_change_ok
;
4116 bfd_boolean new_weakdef
;
4117 bfd_boolean new_weak
;
4118 bfd_boolean old_weak
;
4119 bfd_boolean override
;
4121 bfd_boolean discarded
;
4122 unsigned int old_alignment
;
4124 bfd_boolean matched
;
4128 flags
= BSF_NO_FLAGS
;
4130 value
= isym
->st_value
;
4131 common
= bed
->common_definition (isym
);
4134 bind
= ELF_ST_BIND (isym
->st_info
);
4138 /* This should be impossible, since ELF requires that all
4139 global symbols follow all local symbols, and that sh_info
4140 point to the first global symbol. Unfortunately, Irix 5
4145 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4153 case STB_GNU_UNIQUE
:
4154 flags
= BSF_GNU_UNIQUE
;
4158 /* Leave it up to the processor backend. */
4162 if (isym
->st_shndx
== SHN_UNDEF
)
4163 sec
= bfd_und_section_ptr
;
4164 else if (isym
->st_shndx
== SHN_ABS
)
4165 sec
= bfd_abs_section_ptr
;
4166 else if (isym
->st_shndx
== SHN_COMMON
)
4168 sec
= bfd_com_section_ptr
;
4169 /* What ELF calls the size we call the value. What ELF
4170 calls the value we call the alignment. */
4171 value
= isym
->st_size
;
4175 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4177 sec
= bfd_abs_section_ptr
;
4178 else if (discarded_section (sec
))
4180 /* Symbols from discarded section are undefined. We keep
4182 sec
= bfd_und_section_ptr
;
4184 isym
->st_shndx
= SHN_UNDEF
;
4186 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4190 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4193 goto error_free_vers
;
4195 if (isym
->st_shndx
== SHN_COMMON
4196 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4198 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4202 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4204 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4206 goto error_free_vers
;
4210 else if (isym
->st_shndx
== SHN_COMMON
4211 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4212 && !bfd_link_relocatable (info
))
4214 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4218 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4219 | SEC_LINKER_CREATED
);
4220 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4222 goto error_free_vers
;
4226 else if (bed
->elf_add_symbol_hook
)
4228 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4230 goto error_free_vers
;
4232 /* The hook function sets the name to NULL if this symbol
4233 should be skipped for some reason. */
4238 /* Sanity check that all possibilities were handled. */
4241 bfd_set_error (bfd_error_bad_value
);
4242 goto error_free_vers
;
4245 /* Silently discard TLS symbols from --just-syms. There's
4246 no way to combine a static TLS block with a new TLS block
4247 for this executable. */
4248 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4249 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4252 if (bfd_is_und_section (sec
)
4253 || bfd_is_com_section (sec
))
4258 size_change_ok
= FALSE
;
4259 type_change_ok
= bed
->type_change_ok
;
4266 if (is_elf_hash_table (htab
))
4268 Elf_Internal_Versym iver
;
4269 unsigned int vernum
= 0;
4274 if (info
->default_imported_symver
)
4275 /* Use the default symbol version created earlier. */
4276 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4281 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4283 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4285 /* If this is a hidden symbol, or if it is not version
4286 1, we append the version name to the symbol name.
4287 However, we do not modify a non-hidden absolute symbol
4288 if it is not a function, because it might be the version
4289 symbol itself. FIXME: What if it isn't? */
4290 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4292 && (!bfd_is_abs_section (sec
)
4293 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4296 size_t namelen
, verlen
, newlen
;
4299 if (isym
->st_shndx
!= SHN_UNDEF
)
4301 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4303 else if (vernum
> 1)
4305 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4311 (*_bfd_error_handler
)
4312 (_("%B: %s: invalid version %u (max %d)"),
4314 elf_tdata (abfd
)->cverdefs
);
4315 bfd_set_error (bfd_error_bad_value
);
4316 goto error_free_vers
;
4321 /* We cannot simply test for the number of
4322 entries in the VERNEED section since the
4323 numbers for the needed versions do not start
4325 Elf_Internal_Verneed
*t
;
4328 for (t
= elf_tdata (abfd
)->verref
;
4332 Elf_Internal_Vernaux
*a
;
4334 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4336 if (a
->vna_other
== vernum
)
4338 verstr
= a
->vna_nodename
;
4347 (*_bfd_error_handler
)
4348 (_("%B: %s: invalid needed version %d"),
4349 abfd
, name
, vernum
);
4350 bfd_set_error (bfd_error_bad_value
);
4351 goto error_free_vers
;
4355 namelen
= strlen (name
);
4356 verlen
= strlen (verstr
);
4357 newlen
= namelen
+ verlen
+ 2;
4358 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4359 && isym
->st_shndx
!= SHN_UNDEF
)
4362 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4363 if (newname
== NULL
)
4364 goto error_free_vers
;
4365 memcpy (newname
, name
, namelen
);
4366 p
= newname
+ namelen
;
4368 /* If this is a defined non-hidden version symbol,
4369 we add another @ to the name. This indicates the
4370 default version of the symbol. */
4371 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4372 && isym
->st_shndx
!= SHN_UNDEF
)
4374 memcpy (p
, verstr
, verlen
+ 1);
4379 /* If this symbol has default visibility and the user has
4380 requested we not re-export it, then mark it as hidden. */
4381 if (!bfd_is_und_section (sec
)
4384 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4385 isym
->st_other
= (STV_HIDDEN
4386 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4388 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4389 sym_hash
, &old_bfd
, &old_weak
,
4390 &old_alignment
, &skip
, &override
,
4391 &type_change_ok
, &size_change_ok
,
4393 goto error_free_vers
;
4398 /* Override a definition only if the new symbol matches the
4400 if (override
&& matched
)
4404 while (h
->root
.type
== bfd_link_hash_indirect
4405 || h
->root
.type
== bfd_link_hash_warning
)
4406 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4408 if (elf_tdata (abfd
)->verdef
!= NULL
4411 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4414 if (! (_bfd_generic_link_add_one_symbol
4415 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4416 (struct bfd_link_hash_entry
**) sym_hash
)))
4417 goto error_free_vers
;
4419 if ((flags
& BSF_GNU_UNIQUE
)
4420 && (abfd
->flags
& DYNAMIC
) == 0
4421 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4422 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4425 /* We need to make sure that indirect symbol dynamic flags are
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 /* Setting the index to -3 tells elf_link_output_extsym that
4433 this symbol is defined in a discarded section. */
4439 new_weak
= (flags
& BSF_WEAK
) != 0;
4440 new_weakdef
= FALSE
;
4444 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4445 && is_elf_hash_table (htab
)
4446 && h
->u
.weakdef
== NULL
)
4448 /* Keep a list of all weak defined non function symbols from
4449 a dynamic object, using the weakdef field. Later in this
4450 function we will set the weakdef field to the correct
4451 value. We only put non-function symbols from dynamic
4452 objects on this list, because that happens to be the only
4453 time we need to know the normal symbol corresponding to a
4454 weak symbol, and the information is time consuming to
4455 figure out. If the weakdef field is not already NULL,
4456 then this symbol was already defined by some previous
4457 dynamic object, and we will be using that previous
4458 definition anyhow. */
4460 h
->u
.weakdef
= weaks
;
4465 /* Set the alignment of a common symbol. */
4466 if ((common
|| bfd_is_com_section (sec
))
4467 && h
->root
.type
== bfd_link_hash_common
)
4472 align
= bfd_log2 (isym
->st_value
);
4475 /* The new symbol is a common symbol in a shared object.
4476 We need to get the alignment from the section. */
4477 align
= new_sec
->alignment_power
;
4479 if (align
> old_alignment
)
4480 h
->root
.u
.c
.p
->alignment_power
= align
;
4482 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4485 if (is_elf_hash_table (htab
))
4487 /* Set a flag in the hash table entry indicating the type of
4488 reference or definition we just found. A dynamic symbol
4489 is one which is referenced or defined by both a regular
4490 object and a shared object. */
4491 bfd_boolean dynsym
= FALSE
;
4493 /* Plugin symbols aren't normal. Don't set def_regular or
4494 ref_regular for them, or make them dynamic. */
4495 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4502 if (bind
!= STB_WEAK
)
4503 h
->ref_regular_nonweak
= 1;
4515 /* If the indirect symbol has been forced local, don't
4516 make the real symbol dynamic. */
4517 if ((h
== hi
|| !hi
->forced_local
)
4518 && (bfd_link_dll (info
)
4528 hi
->ref_dynamic
= 1;
4533 hi
->def_dynamic
= 1;
4536 /* If the indirect symbol has been forced local, don't
4537 make the real symbol dynamic. */
4538 if ((h
== hi
|| !hi
->forced_local
)
4541 || (h
->u
.weakdef
!= NULL
4543 && h
->u
.weakdef
->dynindx
!= -1)))
4547 /* Check to see if we need to add an indirect symbol for
4548 the default name. */
4550 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4551 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4552 sec
, value
, &old_bfd
, &dynsym
))
4553 goto error_free_vers
;
4555 /* Check the alignment when a common symbol is involved. This
4556 can change when a common symbol is overridden by a normal
4557 definition or a common symbol is ignored due to the old
4558 normal definition. We need to make sure the maximum
4559 alignment is maintained. */
4560 if ((old_alignment
|| common
)
4561 && h
->root
.type
!= bfd_link_hash_common
)
4563 unsigned int common_align
;
4564 unsigned int normal_align
;
4565 unsigned int symbol_align
;
4569 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4570 || h
->root
.type
== bfd_link_hash_defweak
);
4572 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4573 if (h
->root
.u
.def
.section
->owner
!= NULL
4574 && (h
->root
.u
.def
.section
->owner
->flags
4575 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4577 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4578 if (normal_align
> symbol_align
)
4579 normal_align
= symbol_align
;
4582 normal_align
= symbol_align
;
4586 common_align
= old_alignment
;
4587 common_bfd
= old_bfd
;
4592 common_align
= bfd_log2 (isym
->st_value
);
4594 normal_bfd
= old_bfd
;
4597 if (normal_align
< common_align
)
4599 /* PR binutils/2735 */
4600 if (normal_bfd
== NULL
)
4601 (*_bfd_error_handler
)
4602 (_("Warning: alignment %u of common symbol `%s' in %B is"
4603 " greater than the alignment (%u) of its section %A"),
4604 common_bfd
, h
->root
.u
.def
.section
,
4605 1 << common_align
, name
, 1 << normal_align
);
4607 (*_bfd_error_handler
)
4608 (_("Warning: alignment %u of symbol `%s' in %B"
4609 " is smaller than %u in %B"),
4610 normal_bfd
, common_bfd
,
4611 1 << normal_align
, name
, 1 << common_align
);
4615 /* Remember the symbol size if it isn't undefined. */
4616 if (isym
->st_size
!= 0
4617 && isym
->st_shndx
!= SHN_UNDEF
4618 && (definition
|| h
->size
== 0))
4621 && h
->size
!= isym
->st_size
4622 && ! size_change_ok
)
4623 (*_bfd_error_handler
)
4624 (_("Warning: size of symbol `%s' changed"
4625 " from %lu in %B to %lu in %B"),
4627 name
, (unsigned long) h
->size
,
4628 (unsigned long) isym
->st_size
);
4630 h
->size
= isym
->st_size
;
4633 /* If this is a common symbol, then we always want H->SIZE
4634 to be the size of the common symbol. The code just above
4635 won't fix the size if a common symbol becomes larger. We
4636 don't warn about a size change here, because that is
4637 covered by --warn-common. Allow changes between different
4639 if (h
->root
.type
== bfd_link_hash_common
)
4640 h
->size
= h
->root
.u
.c
.size
;
4642 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4643 && ((definition
&& !new_weak
)
4644 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4645 || h
->type
== STT_NOTYPE
))
4647 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4649 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4651 if (type
== STT_GNU_IFUNC
4652 && (abfd
->flags
& DYNAMIC
) != 0)
4655 if (h
->type
!= type
)
4657 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4658 (*_bfd_error_handler
)
4659 (_("Warning: type of symbol `%s' changed"
4660 " from %d to %d in %B"),
4661 abfd
, name
, h
->type
, type
);
4667 /* Merge st_other field. */
4668 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4670 /* We don't want to make debug symbol dynamic. */
4672 && (sec
->flags
& SEC_DEBUGGING
)
4673 && !bfd_link_relocatable (info
))
4676 /* Nor should we make plugin symbols dynamic. */
4677 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4682 h
->target_internal
= isym
->st_target_internal
;
4683 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4686 if (definition
&& !dynamic
)
4688 char *p
= strchr (name
, ELF_VER_CHR
);
4689 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4691 /* Queue non-default versions so that .symver x, x@FOO
4692 aliases can be checked. */
4695 amt
= ((isymend
- isym
+ 1)
4696 * sizeof (struct elf_link_hash_entry
*));
4698 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4700 goto error_free_vers
;
4702 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4706 if (dynsym
&& h
->dynindx
== -1)
4708 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4709 goto error_free_vers
;
4710 if (h
->u
.weakdef
!= NULL
4712 && h
->u
.weakdef
->dynindx
== -1)
4714 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4715 goto error_free_vers
;
4718 else if (h
->dynindx
!= -1)
4719 /* If the symbol already has a dynamic index, but
4720 visibility says it should not be visible, turn it into
4722 switch (ELF_ST_VISIBILITY (h
->other
))
4726 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4731 /* Don't add DT_NEEDED for references from the dummy bfd nor
4732 for unmatched symbol. */
4737 && h
->ref_regular_nonweak
4739 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4740 || (h
->ref_dynamic_nonweak
4741 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4742 && !on_needed_list (elf_dt_name (abfd
),
4743 htab
->needed
, NULL
))))
4746 const char *soname
= elf_dt_name (abfd
);
4748 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4749 h
->root
.root
.string
);
4751 /* A symbol from a library loaded via DT_NEEDED of some
4752 other library is referenced by a regular object.
4753 Add a DT_NEEDED entry for it. Issue an error if
4754 --no-add-needed is used and the reference was not
4757 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4759 (*_bfd_error_handler
)
4760 (_("%B: undefined reference to symbol '%s'"),
4762 bfd_set_error (bfd_error_missing_dso
);
4763 goto error_free_vers
;
4766 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4767 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4770 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4772 goto error_free_vers
;
4774 BFD_ASSERT (ret
== 0);
4779 if (extversym
!= NULL
)
4785 if (isymbuf
!= NULL
)
4791 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4795 /* Restore the symbol table. */
4796 old_ent
= (char *) old_tab
+ tabsize
;
4797 memset (elf_sym_hashes (abfd
), 0,
4798 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4799 htab
->root
.table
.table
= old_table
;
4800 htab
->root
.table
.size
= old_size
;
4801 htab
->root
.table
.count
= old_count
;
4802 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4803 htab
->root
.undefs
= old_undefs
;
4804 htab
->root
.undefs_tail
= old_undefs_tail
;
4805 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4808 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4810 struct bfd_hash_entry
*p
;
4811 struct elf_link_hash_entry
*h
;
4813 unsigned int alignment_power
;
4815 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4817 h
= (struct elf_link_hash_entry
*) p
;
4818 if (h
->root
.type
== bfd_link_hash_warning
)
4819 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4821 /* Preserve the maximum alignment and size for common
4822 symbols even if this dynamic lib isn't on DT_NEEDED
4823 since it can still be loaded at run time by another
4825 if (h
->root
.type
== bfd_link_hash_common
)
4827 size
= h
->root
.u
.c
.size
;
4828 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4833 alignment_power
= 0;
4835 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4836 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4837 h
= (struct elf_link_hash_entry
*) p
;
4838 if (h
->root
.type
== bfd_link_hash_warning
)
4840 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4841 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4842 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4844 if (h
->root
.type
== bfd_link_hash_common
)
4846 if (size
> h
->root
.u
.c
.size
)
4847 h
->root
.u
.c
.size
= size
;
4848 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4849 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4854 /* Make a special call to the linker "notice" function to
4855 tell it that symbols added for crefs may need to be removed. */
4856 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4857 goto error_free_vers
;
4860 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4862 if (nondeflt_vers
!= NULL
)
4863 free (nondeflt_vers
);
4867 if (old_tab
!= NULL
)
4869 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4870 goto error_free_vers
;
4875 /* Now that all the symbols from this input file are created, if
4876 not performing a relocatable link, handle .symver foo, foo@BAR
4877 such that any relocs against foo become foo@BAR. */
4878 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4882 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4884 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4885 char *shortname
, *p
;
4887 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4889 || (h
->root
.type
!= bfd_link_hash_defined
4890 && h
->root
.type
!= bfd_link_hash_defweak
))
4893 amt
= p
- h
->root
.root
.string
;
4894 shortname
= (char *) bfd_malloc (amt
+ 1);
4896 goto error_free_vers
;
4897 memcpy (shortname
, h
->root
.root
.string
, amt
);
4898 shortname
[amt
] = '\0';
4900 hi
= (struct elf_link_hash_entry
*)
4901 bfd_link_hash_lookup (&htab
->root
, shortname
,
4902 FALSE
, FALSE
, FALSE
);
4904 && hi
->root
.type
== h
->root
.type
4905 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4906 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4908 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4909 hi
->root
.type
= bfd_link_hash_indirect
;
4910 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4911 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4912 sym_hash
= elf_sym_hashes (abfd
);
4914 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4915 if (sym_hash
[symidx
] == hi
)
4917 sym_hash
[symidx
] = h
;
4923 free (nondeflt_vers
);
4924 nondeflt_vers
= NULL
;
4927 /* Now set the weakdefs field correctly for all the weak defined
4928 symbols we found. The only way to do this is to search all the
4929 symbols. Since we only need the information for non functions in
4930 dynamic objects, that's the only time we actually put anything on
4931 the list WEAKS. We need this information so that if a regular
4932 object refers to a symbol defined weakly in a dynamic object, the
4933 real symbol in the dynamic object is also put in the dynamic
4934 symbols; we also must arrange for both symbols to point to the
4935 same memory location. We could handle the general case of symbol
4936 aliasing, but a general symbol alias can only be generated in
4937 assembler code, handling it correctly would be very time
4938 consuming, and other ELF linkers don't handle general aliasing
4942 struct elf_link_hash_entry
**hpp
;
4943 struct elf_link_hash_entry
**hppend
;
4944 struct elf_link_hash_entry
**sorted_sym_hash
;
4945 struct elf_link_hash_entry
*h
;
4948 /* Since we have to search the whole symbol list for each weak
4949 defined symbol, search time for N weak defined symbols will be
4950 O(N^2). Binary search will cut it down to O(NlogN). */
4952 amt
*= sizeof (struct elf_link_hash_entry
*);
4953 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4954 if (sorted_sym_hash
== NULL
)
4956 sym_hash
= sorted_sym_hash
;
4957 hpp
= elf_sym_hashes (abfd
);
4958 hppend
= hpp
+ extsymcount
;
4960 for (; hpp
< hppend
; hpp
++)
4964 && h
->root
.type
== bfd_link_hash_defined
4965 && !bed
->is_function_type (h
->type
))
4973 qsort (sorted_sym_hash
, sym_count
,
4974 sizeof (struct elf_link_hash_entry
*),
4977 while (weaks
!= NULL
)
4979 struct elf_link_hash_entry
*hlook
;
4982 size_t i
, j
, idx
= 0;
4985 weaks
= hlook
->u
.weakdef
;
4986 hlook
->u
.weakdef
= NULL
;
4988 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4989 || hlook
->root
.type
== bfd_link_hash_defweak
4990 || hlook
->root
.type
== bfd_link_hash_common
4991 || hlook
->root
.type
== bfd_link_hash_indirect
);
4992 slook
= hlook
->root
.u
.def
.section
;
4993 vlook
= hlook
->root
.u
.def
.value
;
4999 bfd_signed_vma vdiff
;
5001 h
= sorted_sym_hash
[idx
];
5002 vdiff
= vlook
- h
->root
.u
.def
.value
;
5009 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5019 /* We didn't find a value/section match. */
5023 /* With multiple aliases, or when the weak symbol is already
5024 strongly defined, we have multiple matching symbols and
5025 the binary search above may land on any of them. Step
5026 one past the matching symbol(s). */
5029 h
= sorted_sym_hash
[idx
];
5030 if (h
->root
.u
.def
.section
!= slook
5031 || h
->root
.u
.def
.value
!= vlook
)
5035 /* Now look back over the aliases. Since we sorted by size
5036 as well as value and section, we'll choose the one with
5037 the largest size. */
5040 h
= sorted_sym_hash
[idx
];
5042 /* Stop if value or section doesn't match. */
5043 if (h
->root
.u
.def
.section
!= slook
5044 || h
->root
.u
.def
.value
!= vlook
)
5046 else if (h
!= hlook
)
5048 hlook
->u
.weakdef
= h
;
5050 /* If the weak definition is in the list of dynamic
5051 symbols, make sure the real definition is put
5053 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5055 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5058 free (sorted_sym_hash
);
5063 /* If the real definition is in the list of dynamic
5064 symbols, make sure the weak definition is put
5065 there as well. If we don't do this, then the
5066 dynamic loader might not merge the entries for the
5067 real definition and the weak definition. */
5068 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5070 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5071 goto err_free_sym_hash
;
5078 free (sorted_sym_hash
);
5081 if (bed
->check_directives
5082 && !(*bed
->check_directives
) (abfd
, info
))
5085 if (!info
->check_relocs_after_open_input
5086 && !_bfd_elf_link_check_relocs (abfd
, info
))
5089 /* If this is a non-traditional link, try to optimize the handling
5090 of the .stab/.stabstr sections. */
5092 && ! info
->traditional_format
5093 && is_elf_hash_table (htab
)
5094 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5098 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5099 if (stabstr
!= NULL
)
5101 bfd_size_type string_offset
= 0;
5104 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5105 if (CONST_STRNEQ (stab
->name
, ".stab")
5106 && (!stab
->name
[5] ||
5107 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5108 && (stab
->flags
& SEC_MERGE
) == 0
5109 && !bfd_is_abs_section (stab
->output_section
))
5111 struct bfd_elf_section_data
*secdata
;
5113 secdata
= elf_section_data (stab
);
5114 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5115 stabstr
, &secdata
->sec_info
,
5118 if (secdata
->sec_info
)
5119 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5124 if (is_elf_hash_table (htab
) && add_needed
)
5126 /* Add this bfd to the loaded list. */
5127 struct elf_link_loaded_list
*n
;
5129 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5133 n
->next
= htab
->loaded
;
5140 if (old_tab
!= NULL
)
5142 if (old_strtab
!= NULL
)
5144 if (nondeflt_vers
!= NULL
)
5145 free (nondeflt_vers
);
5146 if (extversym
!= NULL
)
5149 if (isymbuf
!= NULL
)
5155 /* Return the linker hash table entry of a symbol that might be
5156 satisfied by an archive symbol. Return -1 on error. */
5158 struct elf_link_hash_entry
*
5159 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5160 struct bfd_link_info
*info
,
5163 struct elf_link_hash_entry
*h
;
5167 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5171 /* If this is a default version (the name contains @@), look up the
5172 symbol again with only one `@' as well as without the version.
5173 The effect is that references to the symbol with and without the
5174 version will be matched by the default symbol in the archive. */
5176 p
= strchr (name
, ELF_VER_CHR
);
5177 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5180 /* First check with only one `@'. */
5181 len
= strlen (name
);
5182 copy
= (char *) bfd_alloc (abfd
, len
);
5184 return (struct elf_link_hash_entry
*) 0 - 1;
5186 first
= p
- name
+ 1;
5187 memcpy (copy
, name
, first
);
5188 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5190 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5193 /* We also need to check references to the symbol without the
5195 copy
[first
- 1] = '\0';
5196 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5197 FALSE
, FALSE
, TRUE
);
5200 bfd_release (abfd
, copy
);
5204 /* Add symbols from an ELF archive file to the linker hash table. We
5205 don't use _bfd_generic_link_add_archive_symbols because we need to
5206 handle versioned symbols.
5208 Fortunately, ELF archive handling is simpler than that done by
5209 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5210 oddities. In ELF, if we find a symbol in the archive map, and the
5211 symbol is currently undefined, we know that we must pull in that
5214 Unfortunately, we do have to make multiple passes over the symbol
5215 table until nothing further is resolved. */
5218 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5221 unsigned char *included
= NULL
;
5225 const struct elf_backend_data
*bed
;
5226 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5227 (bfd
*, struct bfd_link_info
*, const char *);
5229 if (! bfd_has_map (abfd
))
5231 /* An empty archive is a special case. */
5232 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5234 bfd_set_error (bfd_error_no_armap
);
5238 /* Keep track of all symbols we know to be already defined, and all
5239 files we know to be already included. This is to speed up the
5240 second and subsequent passes. */
5241 c
= bfd_ardata (abfd
)->symdef_count
;
5245 amt
*= sizeof (*included
);
5246 included
= (unsigned char *) bfd_zmalloc (amt
);
5247 if (included
== NULL
)
5250 symdefs
= bfd_ardata (abfd
)->symdefs
;
5251 bed
= get_elf_backend_data (abfd
);
5252 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5265 symdefend
= symdef
+ c
;
5266 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5268 struct elf_link_hash_entry
*h
;
5270 struct bfd_link_hash_entry
*undefs_tail
;
5275 if (symdef
->file_offset
== last
)
5281 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5282 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5288 if (h
->root
.type
== bfd_link_hash_common
)
5290 /* We currently have a common symbol. The archive map contains
5291 a reference to this symbol, so we may want to include it. We
5292 only want to include it however, if this archive element
5293 contains a definition of the symbol, not just another common
5296 Unfortunately some archivers (including GNU ar) will put
5297 declarations of common symbols into their archive maps, as
5298 well as real definitions, so we cannot just go by the archive
5299 map alone. Instead we must read in the element's symbol
5300 table and check that to see what kind of symbol definition
5302 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5305 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5307 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5308 /* Symbol must be defined. Don't check it again. */
5313 /* We need to include this archive member. */
5314 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5315 if (element
== NULL
)
5318 if (! bfd_check_format (element
, bfd_object
))
5321 undefs_tail
= info
->hash
->undefs_tail
;
5323 if (!(*info
->callbacks
5324 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5326 if (!bfd_link_add_symbols (element
, info
))
5329 /* If there are any new undefined symbols, we need to make
5330 another pass through the archive in order to see whether
5331 they can be defined. FIXME: This isn't perfect, because
5332 common symbols wind up on undefs_tail and because an
5333 undefined symbol which is defined later on in this pass
5334 does not require another pass. This isn't a bug, but it
5335 does make the code less efficient than it could be. */
5336 if (undefs_tail
!= info
->hash
->undefs_tail
)
5339 /* Look backward to mark all symbols from this object file
5340 which we have already seen in this pass. */
5344 included
[mark
] = TRUE
;
5349 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5351 /* We mark subsequent symbols from this object file as we go
5352 on through the loop. */
5353 last
= symdef
->file_offset
;
5363 if (included
!= NULL
)
5368 /* Given an ELF BFD, add symbols to the global hash table as
5372 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5374 switch (bfd_get_format (abfd
))
5377 return elf_link_add_object_symbols (abfd
, info
);
5379 return elf_link_add_archive_symbols (abfd
, info
);
5381 bfd_set_error (bfd_error_wrong_format
);
5386 struct hash_codes_info
5388 unsigned long *hashcodes
;
5392 /* This function will be called though elf_link_hash_traverse to store
5393 all hash value of the exported symbols in an array. */
5396 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5398 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5403 /* Ignore indirect symbols. These are added by the versioning code. */
5404 if (h
->dynindx
== -1)
5407 name
= h
->root
.root
.string
;
5408 if (h
->versioned
>= versioned
)
5410 char *p
= strchr (name
, ELF_VER_CHR
);
5413 alc
= (char *) bfd_malloc (p
- name
+ 1);
5419 memcpy (alc
, name
, p
- name
);
5420 alc
[p
- name
] = '\0';
5425 /* Compute the hash value. */
5426 ha
= bfd_elf_hash (name
);
5428 /* Store the found hash value in the array given as the argument. */
5429 *(inf
->hashcodes
)++ = ha
;
5431 /* And store it in the struct so that we can put it in the hash table
5433 h
->u
.elf_hash_value
= ha
;
5441 struct collect_gnu_hash_codes
5444 const struct elf_backend_data
*bed
;
5445 unsigned long int nsyms
;
5446 unsigned long int maskbits
;
5447 unsigned long int *hashcodes
;
5448 unsigned long int *hashval
;
5449 unsigned long int *indx
;
5450 unsigned long int *counts
;
5453 long int min_dynindx
;
5454 unsigned long int bucketcount
;
5455 unsigned long int symindx
;
5456 long int local_indx
;
5457 long int shift1
, shift2
;
5458 unsigned long int mask
;
5462 /* This function will be called though elf_link_hash_traverse to store
5463 all hash value of the exported symbols in an array. */
5466 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5468 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5473 /* Ignore indirect symbols. These are added by the versioning code. */
5474 if (h
->dynindx
== -1)
5477 /* Ignore also local symbols and undefined symbols. */
5478 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5481 name
= h
->root
.root
.string
;
5482 if (h
->versioned
>= versioned
)
5484 char *p
= strchr (name
, ELF_VER_CHR
);
5487 alc
= (char *) bfd_malloc (p
- name
+ 1);
5493 memcpy (alc
, name
, p
- name
);
5494 alc
[p
- name
] = '\0';
5499 /* Compute the hash value. */
5500 ha
= bfd_elf_gnu_hash (name
);
5502 /* Store the found hash value in the array for compute_bucket_count,
5503 and also for .dynsym reordering purposes. */
5504 s
->hashcodes
[s
->nsyms
] = ha
;
5505 s
->hashval
[h
->dynindx
] = ha
;
5507 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5508 s
->min_dynindx
= h
->dynindx
;
5516 /* This function will be called though elf_link_hash_traverse to do
5517 final dynaminc symbol renumbering. */
5520 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5522 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5523 unsigned long int bucket
;
5524 unsigned long int val
;
5526 /* Ignore indirect symbols. */
5527 if (h
->dynindx
== -1)
5530 /* Ignore also local symbols and undefined symbols. */
5531 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5533 if (h
->dynindx
>= s
->min_dynindx
)
5534 h
->dynindx
= s
->local_indx
++;
5538 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5539 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5540 & ((s
->maskbits
>> s
->shift1
) - 1);
5541 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5543 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5544 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5545 if (s
->counts
[bucket
] == 1)
5546 /* Last element terminates the chain. */
5548 bfd_put_32 (s
->output_bfd
, val
,
5549 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5550 --s
->counts
[bucket
];
5551 h
->dynindx
= s
->indx
[bucket
]++;
5555 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5558 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5560 return !(h
->forced_local
5561 || h
->root
.type
== bfd_link_hash_undefined
5562 || h
->root
.type
== bfd_link_hash_undefweak
5563 || ((h
->root
.type
== bfd_link_hash_defined
5564 || h
->root
.type
== bfd_link_hash_defweak
)
5565 && h
->root
.u
.def
.section
->output_section
== NULL
));
5568 /* Array used to determine the number of hash table buckets to use
5569 based on the number of symbols there are. If there are fewer than
5570 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5571 fewer than 37 we use 17 buckets, and so forth. We never use more
5572 than 32771 buckets. */
5574 static const size_t elf_buckets
[] =
5576 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5580 /* Compute bucket count for hashing table. We do not use a static set
5581 of possible tables sizes anymore. Instead we determine for all
5582 possible reasonable sizes of the table the outcome (i.e., the
5583 number of collisions etc) and choose the best solution. The
5584 weighting functions are not too simple to allow the table to grow
5585 without bounds. Instead one of the weighting factors is the size.
5586 Therefore the result is always a good payoff between few collisions
5587 (= short chain lengths) and table size. */
5589 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5590 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5591 unsigned long int nsyms
,
5594 size_t best_size
= 0;
5595 unsigned long int i
;
5597 /* We have a problem here. The following code to optimize the table
5598 size requires an integer type with more the 32 bits. If
5599 BFD_HOST_U_64_BIT is set we know about such a type. */
5600 #ifdef BFD_HOST_U_64_BIT
5605 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5606 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5607 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5608 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5609 unsigned long int *counts
;
5611 unsigned int no_improvement_count
= 0;
5613 /* Possible optimization parameters: if we have NSYMS symbols we say
5614 that the hashing table must at least have NSYMS/4 and at most
5616 minsize
= nsyms
/ 4;
5619 best_size
= maxsize
= nsyms
* 2;
5624 if ((best_size
& 31) == 0)
5628 /* Create array where we count the collisions in. We must use bfd_malloc
5629 since the size could be large. */
5631 amt
*= sizeof (unsigned long int);
5632 counts
= (unsigned long int *) bfd_malloc (amt
);
5636 /* Compute the "optimal" size for the hash table. The criteria is a
5637 minimal chain length. The minor criteria is (of course) the size
5639 for (i
= minsize
; i
< maxsize
; ++i
)
5641 /* Walk through the array of hashcodes and count the collisions. */
5642 BFD_HOST_U_64_BIT max
;
5643 unsigned long int j
;
5644 unsigned long int fact
;
5646 if (gnu_hash
&& (i
& 31) == 0)
5649 memset (counts
, '\0', i
* sizeof (unsigned long int));
5651 /* Determine how often each hash bucket is used. */
5652 for (j
= 0; j
< nsyms
; ++j
)
5653 ++counts
[hashcodes
[j
] % i
];
5655 /* For the weight function we need some information about the
5656 pagesize on the target. This is information need not be 100%
5657 accurate. Since this information is not available (so far) we
5658 define it here to a reasonable default value. If it is crucial
5659 to have a better value some day simply define this value. */
5660 # ifndef BFD_TARGET_PAGESIZE
5661 # define BFD_TARGET_PAGESIZE (4096)
5664 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5666 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5669 /* Variant 1: optimize for short chains. We add the squares
5670 of all the chain lengths (which favors many small chain
5671 over a few long chains). */
5672 for (j
= 0; j
< i
; ++j
)
5673 max
+= counts
[j
] * counts
[j
];
5675 /* This adds penalties for the overall size of the table. */
5676 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5679 /* Variant 2: Optimize a lot more for small table. Here we
5680 also add squares of the size but we also add penalties for
5681 empty slots (the +1 term). */
5682 for (j
= 0; j
< i
; ++j
)
5683 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5685 /* The overall size of the table is considered, but not as
5686 strong as in variant 1, where it is squared. */
5687 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5691 /* Compare with current best results. */
5692 if (max
< best_chlen
)
5696 no_improvement_count
= 0;
5698 /* PR 11843: Avoid futile long searches for the best bucket size
5699 when there are a large number of symbols. */
5700 else if (++no_improvement_count
== 100)
5707 #endif /* defined (BFD_HOST_U_64_BIT) */
5709 /* This is the fallback solution if no 64bit type is available or if we
5710 are not supposed to spend much time on optimizations. We select the
5711 bucket count using a fixed set of numbers. */
5712 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5714 best_size
= elf_buckets
[i
];
5715 if (nsyms
< elf_buckets
[i
+ 1])
5718 if (gnu_hash
&& best_size
< 2)
5725 /* Size any SHT_GROUP section for ld -r. */
5728 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5732 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5733 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5734 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5739 /* Set a default stack segment size. The value in INFO wins. If it
5740 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5741 undefined it is initialized. */
5744 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5745 struct bfd_link_info
*info
,
5746 const char *legacy_symbol
,
5747 bfd_vma default_size
)
5749 struct elf_link_hash_entry
*h
= NULL
;
5751 /* Look for legacy symbol. */
5753 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5754 FALSE
, FALSE
, FALSE
);
5755 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5756 || h
->root
.type
== bfd_link_hash_defweak
)
5758 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5760 /* The symbol has no type if specified on the command line. */
5761 h
->type
= STT_OBJECT
;
5762 if (info
->stacksize
)
5763 (*_bfd_error_handler
) (_("%B: stack size specified and %s set"),
5764 output_bfd
, legacy_symbol
);
5765 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5766 (*_bfd_error_handler
) (_("%B: %s not absolute"),
5767 output_bfd
, legacy_symbol
);
5769 info
->stacksize
= h
->root
.u
.def
.value
;
5772 if (!info
->stacksize
)
5773 /* If the user didn't set a size, or explicitly inhibit the
5774 size, set it now. */
5775 info
->stacksize
= default_size
;
5777 /* Provide the legacy symbol, if it is referenced. */
5778 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5779 || h
->root
.type
== bfd_link_hash_undefweak
))
5781 struct bfd_link_hash_entry
*bh
= NULL
;
5783 if (!(_bfd_generic_link_add_one_symbol
5784 (info
, output_bfd
, legacy_symbol
,
5785 BSF_GLOBAL
, bfd_abs_section_ptr
,
5786 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5787 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5790 h
= (struct elf_link_hash_entry
*) bh
;
5792 h
->type
= STT_OBJECT
;
5798 /* Set up the sizes and contents of the ELF dynamic sections. This is
5799 called by the ELF linker emulation before_allocation routine. We
5800 must set the sizes of the sections before the linker sets the
5801 addresses of the various sections. */
5804 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5807 const char *filter_shlib
,
5809 const char *depaudit
,
5810 const char * const *auxiliary_filters
,
5811 struct bfd_link_info
*info
,
5812 asection
**sinterpptr
)
5816 const struct elf_backend_data
*bed
;
5817 struct elf_info_failed asvinfo
;
5821 soname_indx
= (size_t) -1;
5823 if (!is_elf_hash_table (info
->hash
))
5826 bed
= get_elf_backend_data (output_bfd
);
5828 /* Any syms created from now on start with -1 in
5829 got.refcount/offset and plt.refcount/offset. */
5830 elf_hash_table (info
)->init_got_refcount
5831 = elf_hash_table (info
)->init_got_offset
;
5832 elf_hash_table (info
)->init_plt_refcount
5833 = elf_hash_table (info
)->init_plt_offset
;
5835 if (bfd_link_relocatable (info
)
5836 && !_bfd_elf_size_group_sections (info
))
5839 /* The backend may have to create some sections regardless of whether
5840 we're dynamic or not. */
5841 if (bed
->elf_backend_always_size_sections
5842 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5845 /* Determine any GNU_STACK segment requirements, after the backend
5846 has had a chance to set a default segment size. */
5847 if (info
->execstack
)
5848 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5849 else if (info
->noexecstack
)
5850 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5854 asection
*notesec
= NULL
;
5857 for (inputobj
= info
->input_bfds
;
5859 inputobj
= inputobj
->link
.next
)
5864 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5866 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5869 if (s
->flags
& SEC_CODE
)
5873 else if (bed
->default_execstack
)
5876 if (notesec
|| info
->stacksize
> 0)
5877 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5878 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5879 && notesec
->output_section
!= bfd_abs_section_ptr
)
5880 notesec
->output_section
->flags
|= SEC_CODE
;
5883 dynobj
= elf_hash_table (info
)->dynobj
;
5885 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5887 struct elf_info_failed eif
;
5888 struct elf_link_hash_entry
*h
;
5890 struct bfd_elf_version_tree
*t
;
5891 struct bfd_elf_version_expr
*d
;
5893 bfd_boolean all_defined
;
5895 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5896 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5900 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5902 if (soname_indx
== (size_t) -1
5903 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5909 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5911 info
->flags
|= DF_SYMBOLIC
;
5919 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5921 if (indx
== (size_t) -1)
5924 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5925 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5929 if (filter_shlib
!= NULL
)
5933 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5934 filter_shlib
, TRUE
);
5935 if (indx
== (size_t) -1
5936 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5940 if (auxiliary_filters
!= NULL
)
5942 const char * const *p
;
5944 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5948 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5950 if (indx
== (size_t) -1
5951 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5960 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5962 if (indx
== (size_t) -1
5963 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5967 if (depaudit
!= NULL
)
5971 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5973 if (indx
== (size_t) -1
5974 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
5981 /* If we are supposed to export all symbols into the dynamic symbol
5982 table (this is not the normal case), then do so. */
5983 if (info
->export_dynamic
5984 || (bfd_link_executable (info
) && info
->dynamic
))
5986 elf_link_hash_traverse (elf_hash_table (info
),
5987 _bfd_elf_export_symbol
,
5993 /* Make all global versions with definition. */
5994 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5995 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5996 if (!d
->symver
&& d
->literal
)
5998 const char *verstr
, *name
;
5999 size_t namelen
, verlen
, newlen
;
6000 char *newname
, *p
, leading_char
;
6001 struct elf_link_hash_entry
*newh
;
6003 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6005 namelen
= strlen (name
) + (leading_char
!= '\0');
6007 verlen
= strlen (verstr
);
6008 newlen
= namelen
+ verlen
+ 3;
6010 newname
= (char *) bfd_malloc (newlen
);
6011 if (newname
== NULL
)
6013 newname
[0] = leading_char
;
6014 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6016 /* Check the hidden versioned definition. */
6017 p
= newname
+ namelen
;
6019 memcpy (p
, verstr
, verlen
+ 1);
6020 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6021 newname
, FALSE
, FALSE
,
6024 || (newh
->root
.type
!= bfd_link_hash_defined
6025 && newh
->root
.type
!= bfd_link_hash_defweak
))
6027 /* Check the default versioned definition. */
6029 memcpy (p
, verstr
, verlen
+ 1);
6030 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6031 newname
, FALSE
, FALSE
,
6036 /* Mark this version if there is a definition and it is
6037 not defined in a shared object. */
6039 && !newh
->def_dynamic
6040 && (newh
->root
.type
== bfd_link_hash_defined
6041 || newh
->root
.type
== bfd_link_hash_defweak
))
6045 /* Attach all the symbols to their version information. */
6046 asvinfo
.info
= info
;
6047 asvinfo
.failed
= FALSE
;
6049 elf_link_hash_traverse (elf_hash_table (info
),
6050 _bfd_elf_link_assign_sym_version
,
6055 if (!info
->allow_undefined_version
)
6057 /* Check if all global versions have a definition. */
6059 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6060 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6061 if (d
->literal
&& !d
->symver
&& !d
->script
)
6063 (*_bfd_error_handler
)
6064 (_("%s: undefined version: %s"),
6065 d
->pattern
, t
->name
);
6066 all_defined
= FALSE
;
6071 bfd_set_error (bfd_error_bad_value
);
6076 /* Find all symbols which were defined in a dynamic object and make
6077 the backend pick a reasonable value for them. */
6078 elf_link_hash_traverse (elf_hash_table (info
),
6079 _bfd_elf_adjust_dynamic_symbol
,
6084 /* Add some entries to the .dynamic section. We fill in some of the
6085 values later, in bfd_elf_final_link, but we must add the entries
6086 now so that we know the final size of the .dynamic section. */
6088 /* If there are initialization and/or finalization functions to
6089 call then add the corresponding DT_INIT/DT_FINI entries. */
6090 h
= (info
->init_function
6091 ? elf_link_hash_lookup (elf_hash_table (info
),
6092 info
->init_function
, FALSE
,
6099 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6102 h
= (info
->fini_function
6103 ? elf_link_hash_lookup (elf_hash_table (info
),
6104 info
->fini_function
, FALSE
,
6111 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6115 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6116 if (s
!= NULL
&& s
->linker_has_input
)
6118 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6119 if (! bfd_link_executable (info
))
6124 for (sub
= info
->input_bfds
; sub
!= NULL
;
6125 sub
= sub
->link
.next
)
6126 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6127 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6128 if (elf_section_data (o
)->this_hdr
.sh_type
6129 == SHT_PREINIT_ARRAY
)
6131 (*_bfd_error_handler
)
6132 (_("%B: .preinit_array section is not allowed in DSO"),
6137 bfd_set_error (bfd_error_nonrepresentable_section
);
6141 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6142 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6145 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6146 if (s
!= NULL
&& s
->linker_has_input
)
6148 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6149 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6152 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6153 if (s
!= NULL
&& s
->linker_has_input
)
6155 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6156 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6160 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6161 /* If .dynstr is excluded from the link, we don't want any of
6162 these tags. Strictly, we should be checking each section
6163 individually; This quick check covers for the case where
6164 someone does a /DISCARD/ : { *(*) }. */
6165 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6167 bfd_size_type strsize
;
6169 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6170 if ((info
->emit_hash
6171 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6172 || (info
->emit_gnu_hash
6173 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6174 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6175 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6176 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6177 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6178 bed
->s
->sizeof_sym
))
6183 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6186 /* The backend must work out the sizes of all the other dynamic
6189 && bed
->elf_backend_size_dynamic_sections
!= NULL
6190 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6193 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6195 unsigned long section_sym_count
;
6196 struct bfd_elf_version_tree
*verdefs
;
6199 /* Set up the version definition section. */
6200 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6201 BFD_ASSERT (s
!= NULL
);
6203 /* We may have created additional version definitions if we are
6204 just linking a regular application. */
6205 verdefs
= info
->version_info
;
6207 /* Skip anonymous version tag. */
6208 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6209 verdefs
= verdefs
->next
;
6211 if (verdefs
== NULL
&& !info
->create_default_symver
)
6212 s
->flags
|= SEC_EXCLUDE
;
6217 struct bfd_elf_version_tree
*t
;
6219 Elf_Internal_Verdef def
;
6220 Elf_Internal_Verdaux defaux
;
6221 struct bfd_link_hash_entry
*bh
;
6222 struct elf_link_hash_entry
*h
;
6228 /* Make space for the base version. */
6229 size
+= sizeof (Elf_External_Verdef
);
6230 size
+= sizeof (Elf_External_Verdaux
);
6233 /* Make space for the default version. */
6234 if (info
->create_default_symver
)
6236 size
+= sizeof (Elf_External_Verdef
);
6240 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6242 struct bfd_elf_version_deps
*n
;
6244 /* Don't emit base version twice. */
6248 size
+= sizeof (Elf_External_Verdef
);
6249 size
+= sizeof (Elf_External_Verdaux
);
6252 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6253 size
+= sizeof (Elf_External_Verdaux
);
6257 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6258 if (s
->contents
== NULL
&& s
->size
!= 0)
6261 /* Fill in the version definition section. */
6265 def
.vd_version
= VER_DEF_CURRENT
;
6266 def
.vd_flags
= VER_FLG_BASE
;
6269 if (info
->create_default_symver
)
6271 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6272 def
.vd_next
= sizeof (Elf_External_Verdef
);
6276 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6277 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6278 + sizeof (Elf_External_Verdaux
));
6281 if (soname_indx
!= (size_t) -1)
6283 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6285 def
.vd_hash
= bfd_elf_hash (soname
);
6286 defaux
.vda_name
= soname_indx
;
6293 name
= lbasename (output_bfd
->filename
);
6294 def
.vd_hash
= bfd_elf_hash (name
);
6295 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6297 if (indx
== (size_t) -1)
6299 defaux
.vda_name
= indx
;
6301 defaux
.vda_next
= 0;
6303 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6304 (Elf_External_Verdef
*) p
);
6305 p
+= sizeof (Elf_External_Verdef
);
6306 if (info
->create_default_symver
)
6308 /* Add a symbol representing this version. */
6310 if (! (_bfd_generic_link_add_one_symbol
6311 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6313 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6315 h
= (struct elf_link_hash_entry
*) bh
;
6318 h
->type
= STT_OBJECT
;
6319 h
->verinfo
.vertree
= NULL
;
6321 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6324 /* Create a duplicate of the base version with the same
6325 aux block, but different flags. */
6328 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6330 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6331 + sizeof (Elf_External_Verdaux
));
6334 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6335 (Elf_External_Verdef
*) p
);
6336 p
+= sizeof (Elf_External_Verdef
);
6338 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6339 (Elf_External_Verdaux
*) p
);
6340 p
+= sizeof (Elf_External_Verdaux
);
6342 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6345 struct bfd_elf_version_deps
*n
;
6347 /* Don't emit the base version twice. */
6352 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6355 /* Add a symbol representing this version. */
6357 if (! (_bfd_generic_link_add_one_symbol
6358 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6360 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6362 h
= (struct elf_link_hash_entry
*) bh
;
6365 h
->type
= STT_OBJECT
;
6366 h
->verinfo
.vertree
= t
;
6368 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6371 def
.vd_version
= VER_DEF_CURRENT
;
6373 if (t
->globals
.list
== NULL
6374 && t
->locals
.list
== NULL
6376 def
.vd_flags
|= VER_FLG_WEAK
;
6377 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6378 def
.vd_cnt
= cdeps
+ 1;
6379 def
.vd_hash
= bfd_elf_hash (t
->name
);
6380 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6383 /* If a basever node is next, it *must* be the last node in
6384 the chain, otherwise Verdef construction breaks. */
6385 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6386 BFD_ASSERT (t
->next
->next
== NULL
);
6388 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6389 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6390 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6392 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6393 (Elf_External_Verdef
*) p
);
6394 p
+= sizeof (Elf_External_Verdef
);
6396 defaux
.vda_name
= h
->dynstr_index
;
6397 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6399 defaux
.vda_next
= 0;
6400 if (t
->deps
!= NULL
)
6401 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6402 t
->name_indx
= defaux
.vda_name
;
6404 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6405 (Elf_External_Verdaux
*) p
);
6406 p
+= sizeof (Elf_External_Verdaux
);
6408 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6410 if (n
->version_needed
== NULL
)
6412 /* This can happen if there was an error in the
6414 defaux
.vda_name
= 0;
6418 defaux
.vda_name
= n
->version_needed
->name_indx
;
6419 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6422 if (n
->next
== NULL
)
6423 defaux
.vda_next
= 0;
6425 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6427 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6428 (Elf_External_Verdaux
*) p
);
6429 p
+= sizeof (Elf_External_Verdaux
);
6433 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6434 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6437 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6440 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6442 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6445 else if (info
->flags
& DF_BIND_NOW
)
6447 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6453 if (bfd_link_executable (info
))
6454 info
->flags_1
&= ~ (DF_1_INITFIRST
6457 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6461 /* Work out the size of the version reference section. */
6463 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6464 BFD_ASSERT (s
!= NULL
);
6466 struct elf_find_verdep_info sinfo
;
6469 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6470 if (sinfo
.vers
== 0)
6472 sinfo
.failed
= FALSE
;
6474 elf_link_hash_traverse (elf_hash_table (info
),
6475 _bfd_elf_link_find_version_dependencies
,
6480 if (elf_tdata (output_bfd
)->verref
== NULL
)
6481 s
->flags
|= SEC_EXCLUDE
;
6484 Elf_Internal_Verneed
*t
;
6489 /* Build the version dependency section. */
6492 for (t
= elf_tdata (output_bfd
)->verref
;
6496 Elf_Internal_Vernaux
*a
;
6498 size
+= sizeof (Elf_External_Verneed
);
6500 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6501 size
+= sizeof (Elf_External_Vernaux
);
6505 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6506 if (s
->contents
== NULL
)
6510 for (t
= elf_tdata (output_bfd
)->verref
;
6515 Elf_Internal_Vernaux
*a
;
6519 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6522 t
->vn_version
= VER_NEED_CURRENT
;
6524 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6525 elf_dt_name (t
->vn_bfd
) != NULL
6526 ? elf_dt_name (t
->vn_bfd
)
6527 : lbasename (t
->vn_bfd
->filename
),
6529 if (indx
== (size_t) -1)
6532 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6533 if (t
->vn_nextref
== NULL
)
6536 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6537 + caux
* sizeof (Elf_External_Vernaux
));
6539 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6540 (Elf_External_Verneed
*) p
);
6541 p
+= sizeof (Elf_External_Verneed
);
6543 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6545 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6546 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6547 a
->vna_nodename
, FALSE
);
6548 if (indx
== (size_t) -1)
6551 if (a
->vna_nextptr
== NULL
)
6554 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6556 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6557 (Elf_External_Vernaux
*) p
);
6558 p
+= sizeof (Elf_External_Vernaux
);
6562 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6563 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6566 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6570 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6571 && elf_tdata (output_bfd
)->cverdefs
== 0)
6572 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6573 §ion_sym_count
) == 0)
6575 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6576 s
->flags
|= SEC_EXCLUDE
;
6582 /* Find the first non-excluded output section. We'll use its
6583 section symbol for some emitted relocs. */
6585 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6589 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6590 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6591 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6593 elf_hash_table (info
)->text_index_section
= s
;
6598 /* Find two non-excluded output sections, one for code, one for data.
6599 We'll use their section symbols for some emitted relocs. */
6601 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6605 /* Data first, since setting text_index_section changes
6606 _bfd_elf_link_omit_section_dynsym. */
6607 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6608 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6609 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6611 elf_hash_table (info
)->data_index_section
= s
;
6615 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6616 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6617 == (SEC_ALLOC
| SEC_READONLY
))
6618 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6620 elf_hash_table (info
)->text_index_section
= s
;
6624 if (elf_hash_table (info
)->text_index_section
== NULL
)
6625 elf_hash_table (info
)->text_index_section
6626 = elf_hash_table (info
)->data_index_section
;
6630 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6632 const struct elf_backend_data
*bed
;
6634 if (!is_elf_hash_table (info
->hash
))
6637 bed
= get_elf_backend_data (output_bfd
);
6638 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6640 if (elf_hash_table (info
)->dynamic_sections_created
)
6644 bfd_size_type dynsymcount
;
6645 unsigned long section_sym_count
;
6646 unsigned int dtagcount
;
6648 dynobj
= elf_hash_table (info
)->dynobj
;
6650 /* Assign dynsym indicies. In a shared library we generate a
6651 section symbol for each output section, which come first.
6652 Next come all of the back-end allocated local dynamic syms,
6653 followed by the rest of the global symbols. */
6655 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6656 §ion_sym_count
);
6658 /* Work out the size of the symbol version section. */
6659 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6660 BFD_ASSERT (s
!= NULL
);
6661 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6663 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6664 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6665 if (s
->contents
== NULL
)
6668 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6672 /* Set the size of the .dynsym and .hash sections. We counted
6673 the number of dynamic symbols in elf_link_add_object_symbols.
6674 We will build the contents of .dynsym and .hash when we build
6675 the final symbol table, because until then we do not know the
6676 correct value to give the symbols. We built the .dynstr
6677 section as we went along in elf_link_add_object_symbols. */
6678 s
= elf_hash_table (info
)->dynsym
;
6679 BFD_ASSERT (s
!= NULL
);
6680 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6682 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6683 if (s
->contents
== NULL
)
6686 /* The first entry in .dynsym is a dummy symbol. Clear all the
6687 section syms, in case we don't output them all. */
6688 ++section_sym_count
;
6689 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6691 elf_hash_table (info
)->bucketcount
= 0;
6693 /* Compute the size of the hashing table. As a side effect this
6694 computes the hash values for all the names we export. */
6695 if (info
->emit_hash
)
6697 unsigned long int *hashcodes
;
6698 struct hash_codes_info hashinf
;
6700 unsigned long int nsyms
;
6702 size_t hash_entry_size
;
6704 /* Compute the hash values for all exported symbols. At the same
6705 time store the values in an array so that we could use them for
6707 amt
= dynsymcount
* sizeof (unsigned long int);
6708 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6709 if (hashcodes
== NULL
)
6711 hashinf
.hashcodes
= hashcodes
;
6712 hashinf
.error
= FALSE
;
6714 /* Put all hash values in HASHCODES. */
6715 elf_link_hash_traverse (elf_hash_table (info
),
6716 elf_collect_hash_codes
, &hashinf
);
6723 nsyms
= hashinf
.hashcodes
- hashcodes
;
6725 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6728 if (bucketcount
== 0)
6731 elf_hash_table (info
)->bucketcount
= bucketcount
;
6733 s
= bfd_get_linker_section (dynobj
, ".hash");
6734 BFD_ASSERT (s
!= NULL
);
6735 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6736 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6737 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6738 if (s
->contents
== NULL
)
6741 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6742 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6743 s
->contents
+ hash_entry_size
);
6746 if (info
->emit_gnu_hash
)
6749 unsigned char *contents
;
6750 struct collect_gnu_hash_codes cinfo
;
6754 memset (&cinfo
, 0, sizeof (cinfo
));
6756 /* Compute the hash values for all exported symbols. At the same
6757 time store the values in an array so that we could use them for
6759 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6760 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6761 if (cinfo
.hashcodes
== NULL
)
6764 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6765 cinfo
.min_dynindx
= -1;
6766 cinfo
.output_bfd
= output_bfd
;
6769 /* Put all hash values in HASHCODES. */
6770 elf_link_hash_traverse (elf_hash_table (info
),
6771 elf_collect_gnu_hash_codes
, &cinfo
);
6774 free (cinfo
.hashcodes
);
6779 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6781 if (bucketcount
== 0)
6783 free (cinfo
.hashcodes
);
6787 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6788 BFD_ASSERT (s
!= NULL
);
6790 if (cinfo
.nsyms
== 0)
6792 /* Empty .gnu.hash section is special. */
6793 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6794 free (cinfo
.hashcodes
);
6795 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6796 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6797 if (contents
== NULL
)
6799 s
->contents
= contents
;
6800 /* 1 empty bucket. */
6801 bfd_put_32 (output_bfd
, 1, contents
);
6802 /* SYMIDX above the special symbol 0. */
6803 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6804 /* Just one word for bitmask. */
6805 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6806 /* Only hash fn bloom filter. */
6807 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6808 /* No hashes are valid - empty bitmask. */
6809 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6810 /* No hashes in the only bucket. */
6811 bfd_put_32 (output_bfd
, 0,
6812 contents
+ 16 + bed
->s
->arch_size
/ 8);
6816 unsigned long int maskwords
, maskbitslog2
, x
;
6817 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6821 while ((x
>>= 1) != 0)
6823 if (maskbitslog2
< 3)
6825 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6826 maskbitslog2
= maskbitslog2
+ 3;
6828 maskbitslog2
= maskbitslog2
+ 2;
6829 if (bed
->s
->arch_size
== 64)
6831 if (maskbitslog2
== 5)
6837 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6838 cinfo
.shift2
= maskbitslog2
;
6839 cinfo
.maskbits
= 1 << maskbitslog2
;
6840 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6841 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6842 amt
+= maskwords
* sizeof (bfd_vma
);
6843 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6844 if (cinfo
.bitmask
== NULL
)
6846 free (cinfo
.hashcodes
);
6850 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6851 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6852 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6853 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6855 /* Determine how often each hash bucket is used. */
6856 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6857 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6858 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6860 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6861 if (cinfo
.counts
[i
] != 0)
6863 cinfo
.indx
[i
] = cnt
;
6864 cnt
+= cinfo
.counts
[i
];
6866 BFD_ASSERT (cnt
== dynsymcount
);
6867 cinfo
.bucketcount
= bucketcount
;
6868 cinfo
.local_indx
= cinfo
.min_dynindx
;
6870 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6871 s
->size
+= cinfo
.maskbits
/ 8;
6872 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6873 if (contents
== NULL
)
6875 free (cinfo
.bitmask
);
6876 free (cinfo
.hashcodes
);
6880 s
->contents
= contents
;
6881 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6882 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6883 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6884 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6885 contents
+= 16 + cinfo
.maskbits
/ 8;
6887 for (i
= 0; i
< bucketcount
; ++i
)
6889 if (cinfo
.counts
[i
] == 0)
6890 bfd_put_32 (output_bfd
, 0, contents
);
6892 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6896 cinfo
.contents
= contents
;
6898 /* Renumber dynamic symbols, populate .gnu.hash section. */
6899 elf_link_hash_traverse (elf_hash_table (info
),
6900 elf_renumber_gnu_hash_syms
, &cinfo
);
6902 contents
= s
->contents
+ 16;
6903 for (i
= 0; i
< maskwords
; ++i
)
6905 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6907 contents
+= bed
->s
->arch_size
/ 8;
6910 free (cinfo
.bitmask
);
6911 free (cinfo
.hashcodes
);
6915 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6916 BFD_ASSERT (s
!= NULL
);
6918 elf_finalize_dynstr (output_bfd
, info
);
6920 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6922 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6923 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6930 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6933 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6936 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6937 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6940 /* Finish SHF_MERGE section merging. */
6943 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6948 if (!is_elf_hash_table (info
->hash
))
6951 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6952 if ((ibfd
->flags
& DYNAMIC
) == 0
6953 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6954 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6955 == get_elf_backend_data (obfd
)->s
->elfclass
))
6956 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6957 if ((sec
->flags
& SEC_MERGE
) != 0
6958 && !bfd_is_abs_section (sec
->output_section
))
6960 struct bfd_elf_section_data
*secdata
;
6962 secdata
= elf_section_data (sec
);
6963 if (! _bfd_add_merge_section (obfd
,
6964 &elf_hash_table (info
)->merge_info
,
6965 sec
, &secdata
->sec_info
))
6967 else if (secdata
->sec_info
)
6968 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6971 if (elf_hash_table (info
)->merge_info
!= NULL
)
6972 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
6973 merge_sections_remove_hook
);
6977 /* Create an entry in an ELF linker hash table. */
6979 struct bfd_hash_entry
*
6980 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6981 struct bfd_hash_table
*table
,
6984 /* Allocate the structure if it has not already been allocated by a
6988 entry
= (struct bfd_hash_entry
*)
6989 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6994 /* Call the allocation method of the superclass. */
6995 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6998 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6999 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7001 /* Set local fields. */
7004 ret
->got
= htab
->init_got_refcount
;
7005 ret
->plt
= htab
->init_plt_refcount
;
7006 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7007 - offsetof (struct elf_link_hash_entry
, size
)));
7008 /* Assume that we have been called by a non-ELF symbol reader.
7009 This flag is then reset by the code which reads an ELF input
7010 file. This ensures that a symbol created by a non-ELF symbol
7011 reader will have the flag set correctly. */
7018 /* Copy data from an indirect symbol to its direct symbol, hiding the
7019 old indirect symbol. Also used for copying flags to a weakdef. */
7022 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7023 struct elf_link_hash_entry
*dir
,
7024 struct elf_link_hash_entry
*ind
)
7026 struct elf_link_hash_table
*htab
;
7028 /* Copy down any references that we may have already seen to the
7029 symbol which just became indirect if DIR isn't a hidden versioned
7032 if (dir
->versioned
!= versioned_hidden
)
7034 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7035 dir
->ref_regular
|= ind
->ref_regular
;
7036 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7037 dir
->non_got_ref
|= ind
->non_got_ref
;
7038 dir
->needs_plt
|= ind
->needs_plt
;
7039 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7042 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7045 /* Copy over the global and procedure linkage table refcount entries.
7046 These may have been already set up by a check_relocs routine. */
7047 htab
= elf_hash_table (info
);
7048 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7050 if (dir
->got
.refcount
< 0)
7051 dir
->got
.refcount
= 0;
7052 dir
->got
.refcount
+= ind
->got
.refcount
;
7053 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7056 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7058 if (dir
->plt
.refcount
< 0)
7059 dir
->plt
.refcount
= 0;
7060 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7061 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7064 if (ind
->dynindx
!= -1)
7066 if (dir
->dynindx
!= -1)
7067 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7068 dir
->dynindx
= ind
->dynindx
;
7069 dir
->dynstr_index
= ind
->dynstr_index
;
7071 ind
->dynstr_index
= 0;
7076 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7077 struct elf_link_hash_entry
*h
,
7078 bfd_boolean force_local
)
7080 /* STT_GNU_IFUNC symbol must go through PLT. */
7081 if (h
->type
!= STT_GNU_IFUNC
)
7083 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7088 h
->forced_local
= 1;
7089 if (h
->dynindx
!= -1)
7092 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7098 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7102 _bfd_elf_link_hash_table_init
7103 (struct elf_link_hash_table
*table
,
7105 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7106 struct bfd_hash_table
*,
7108 unsigned int entsize
,
7109 enum elf_target_id target_id
)
7112 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7114 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7115 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7116 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7117 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7118 /* The first dynamic symbol is a dummy. */
7119 table
->dynsymcount
= 1;
7121 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7123 table
->root
.type
= bfd_link_elf_hash_table
;
7124 table
->hash_table_id
= target_id
;
7129 /* Create an ELF linker hash table. */
7131 struct bfd_link_hash_table
*
7132 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7134 struct elf_link_hash_table
*ret
;
7135 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7137 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7141 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7142 sizeof (struct elf_link_hash_entry
),
7148 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7153 /* Destroy an ELF linker hash table. */
7156 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7158 struct elf_link_hash_table
*htab
;
7160 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7161 if (htab
->dynstr
!= NULL
)
7162 _bfd_elf_strtab_free (htab
->dynstr
);
7163 _bfd_merge_sections_free (htab
->merge_info
);
7164 _bfd_generic_link_hash_table_free (obfd
);
7167 /* This is a hook for the ELF emulation code in the generic linker to
7168 tell the backend linker what file name to use for the DT_NEEDED
7169 entry for a dynamic object. */
7172 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7174 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7175 && bfd_get_format (abfd
) == bfd_object
)
7176 elf_dt_name (abfd
) = name
;
7180 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7183 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7184 && bfd_get_format (abfd
) == bfd_object
)
7185 lib_class
= elf_dyn_lib_class (abfd
);
7192 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7194 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7195 && bfd_get_format (abfd
) == bfd_object
)
7196 elf_dyn_lib_class (abfd
) = lib_class
;
7199 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7200 the linker ELF emulation code. */
7202 struct bfd_link_needed_list
*
7203 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7204 struct bfd_link_info
*info
)
7206 if (! is_elf_hash_table (info
->hash
))
7208 return elf_hash_table (info
)->needed
;
7211 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7212 hook for the linker ELF emulation code. */
7214 struct bfd_link_needed_list
*
7215 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7216 struct bfd_link_info
*info
)
7218 if (! is_elf_hash_table (info
->hash
))
7220 return elf_hash_table (info
)->runpath
;
7223 /* Get the name actually used for a dynamic object for a link. This
7224 is the SONAME entry if there is one. Otherwise, it is the string
7225 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7228 bfd_elf_get_dt_soname (bfd
*abfd
)
7230 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7231 && bfd_get_format (abfd
) == bfd_object
)
7232 return elf_dt_name (abfd
);
7236 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7237 the ELF linker emulation code. */
7240 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7241 struct bfd_link_needed_list
**pneeded
)
7244 bfd_byte
*dynbuf
= NULL
;
7245 unsigned int elfsec
;
7246 unsigned long shlink
;
7247 bfd_byte
*extdyn
, *extdynend
;
7249 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7253 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7254 || bfd_get_format (abfd
) != bfd_object
)
7257 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7258 if (s
== NULL
|| s
->size
== 0)
7261 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7264 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7265 if (elfsec
== SHN_BAD
)
7268 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7270 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7271 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7274 extdynend
= extdyn
+ s
->size
;
7275 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7277 Elf_Internal_Dyn dyn
;
7279 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7281 if (dyn
.d_tag
== DT_NULL
)
7284 if (dyn
.d_tag
== DT_NEEDED
)
7287 struct bfd_link_needed_list
*l
;
7288 unsigned int tagv
= dyn
.d_un
.d_val
;
7291 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7296 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7317 struct elf_symbuf_symbol
7319 unsigned long st_name
; /* Symbol name, index in string tbl */
7320 unsigned char st_info
; /* Type and binding attributes */
7321 unsigned char st_other
; /* Visibilty, and target specific */
7324 struct elf_symbuf_head
7326 struct elf_symbuf_symbol
*ssym
;
7328 unsigned int st_shndx
;
7335 Elf_Internal_Sym
*isym
;
7336 struct elf_symbuf_symbol
*ssym
;
7341 /* Sort references to symbols by ascending section number. */
7344 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7346 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7347 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7349 return s1
->st_shndx
- s2
->st_shndx
;
7353 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7355 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7356 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7357 return strcmp (s1
->name
, s2
->name
);
7360 static struct elf_symbuf_head
*
7361 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7363 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7364 struct elf_symbuf_symbol
*ssym
;
7365 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7366 size_t i
, shndx_count
, total_size
;
7368 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7372 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7373 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7374 *ind
++ = &isymbuf
[i
];
7377 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7378 elf_sort_elf_symbol
);
7381 if (indbufend
> indbuf
)
7382 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7383 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7386 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7387 + (indbufend
- indbuf
) * sizeof (*ssym
));
7388 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7389 if (ssymbuf
== NULL
)
7395 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7396 ssymbuf
->ssym
= NULL
;
7397 ssymbuf
->count
= shndx_count
;
7398 ssymbuf
->st_shndx
= 0;
7399 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7401 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7404 ssymhead
->ssym
= ssym
;
7405 ssymhead
->count
= 0;
7406 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7408 ssym
->st_name
= (*ind
)->st_name
;
7409 ssym
->st_info
= (*ind
)->st_info
;
7410 ssym
->st_other
= (*ind
)->st_other
;
7413 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7414 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7421 /* Check if 2 sections define the same set of local and global
7425 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7426 struct bfd_link_info
*info
)
7429 const struct elf_backend_data
*bed1
, *bed2
;
7430 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7431 size_t symcount1
, symcount2
;
7432 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7433 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7434 Elf_Internal_Sym
*isym
, *isymend
;
7435 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7436 size_t count1
, count2
, i
;
7437 unsigned int shndx1
, shndx2
;
7443 /* Both sections have to be in ELF. */
7444 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7445 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7448 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7451 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7452 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7453 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7456 bed1
= get_elf_backend_data (bfd1
);
7457 bed2
= get_elf_backend_data (bfd2
);
7458 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7459 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7460 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7461 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7463 if (symcount1
== 0 || symcount2
== 0)
7469 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7470 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7472 if (ssymbuf1
== NULL
)
7474 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7476 if (isymbuf1
== NULL
)
7479 if (!info
->reduce_memory_overheads
)
7480 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7481 = elf_create_symbuf (symcount1
, isymbuf1
);
7484 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7486 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7488 if (isymbuf2
== NULL
)
7491 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7492 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7493 = elf_create_symbuf (symcount2
, isymbuf2
);
7496 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7498 /* Optimized faster version. */
7500 struct elf_symbol
*symp
;
7501 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7504 hi
= ssymbuf1
->count
;
7509 mid
= (lo
+ hi
) / 2;
7510 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7512 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7516 count1
= ssymbuf1
[mid
].count
;
7523 hi
= ssymbuf2
->count
;
7528 mid
= (lo
+ hi
) / 2;
7529 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7531 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7535 count2
= ssymbuf2
[mid
].count
;
7541 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7545 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7547 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7548 if (symtable1
== NULL
|| symtable2
== NULL
)
7552 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7553 ssym
< ssymend
; ssym
++, symp
++)
7555 symp
->u
.ssym
= ssym
;
7556 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7562 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7563 ssym
< ssymend
; ssym
++, symp
++)
7565 symp
->u
.ssym
= ssym
;
7566 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7571 /* Sort symbol by name. */
7572 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7573 elf_sym_name_compare
);
7574 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7575 elf_sym_name_compare
);
7577 for (i
= 0; i
< count1
; i
++)
7578 /* Two symbols must have the same binding, type and name. */
7579 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7580 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7581 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7588 symtable1
= (struct elf_symbol
*)
7589 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7590 symtable2
= (struct elf_symbol
*)
7591 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7592 if (symtable1
== NULL
|| symtable2
== NULL
)
7595 /* Count definitions in the section. */
7597 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7598 if (isym
->st_shndx
== shndx1
)
7599 symtable1
[count1
++].u
.isym
= isym
;
7602 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7603 if (isym
->st_shndx
== shndx2
)
7604 symtable2
[count2
++].u
.isym
= isym
;
7606 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7609 for (i
= 0; i
< count1
; i
++)
7611 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7612 symtable1
[i
].u
.isym
->st_name
);
7614 for (i
= 0; i
< count2
; i
++)
7616 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7617 symtable2
[i
].u
.isym
->st_name
);
7619 /* Sort symbol by name. */
7620 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7621 elf_sym_name_compare
);
7622 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7623 elf_sym_name_compare
);
7625 for (i
= 0; i
< count1
; i
++)
7626 /* Two symbols must have the same binding, type and name. */
7627 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7628 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7629 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7647 /* Return TRUE if 2 section types are compatible. */
7650 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7651 bfd
*bbfd
, const asection
*bsec
)
7655 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7656 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7659 return elf_section_type (asec
) == elf_section_type (bsec
);
7662 /* Final phase of ELF linker. */
7664 /* A structure we use to avoid passing large numbers of arguments. */
7666 struct elf_final_link_info
7668 /* General link information. */
7669 struct bfd_link_info
*info
;
7672 /* Symbol string table. */
7673 struct elf_strtab_hash
*symstrtab
;
7674 /* .hash section. */
7676 /* symbol version section (.gnu.version). */
7677 asection
*symver_sec
;
7678 /* Buffer large enough to hold contents of any section. */
7680 /* Buffer large enough to hold external relocs of any section. */
7681 void *external_relocs
;
7682 /* Buffer large enough to hold internal relocs of any section. */
7683 Elf_Internal_Rela
*internal_relocs
;
7684 /* Buffer large enough to hold external local symbols of any input
7686 bfd_byte
*external_syms
;
7687 /* And a buffer for symbol section indices. */
7688 Elf_External_Sym_Shndx
*locsym_shndx
;
7689 /* Buffer large enough to hold internal local symbols of any input
7691 Elf_Internal_Sym
*internal_syms
;
7692 /* Array large enough to hold a symbol index for each local symbol
7693 of any input BFD. */
7695 /* Array large enough to hold a section pointer for each local
7696 symbol of any input BFD. */
7697 asection
**sections
;
7698 /* Buffer for SHT_SYMTAB_SHNDX section. */
7699 Elf_External_Sym_Shndx
*symshndxbuf
;
7700 /* Number of STT_FILE syms seen. */
7701 size_t filesym_count
;
7704 /* This struct is used to pass information to elf_link_output_extsym. */
7706 struct elf_outext_info
7709 bfd_boolean localsyms
;
7710 bfd_boolean file_sym_done
;
7711 struct elf_final_link_info
*flinfo
;
7715 /* Support for evaluating a complex relocation.
7717 Complex relocations are generalized, self-describing relocations. The
7718 implementation of them consists of two parts: complex symbols, and the
7719 relocations themselves.
7721 The relocations are use a reserved elf-wide relocation type code (R_RELC
7722 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7723 information (start bit, end bit, word width, etc) into the addend. This
7724 information is extracted from CGEN-generated operand tables within gas.
7726 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7727 internal) representing prefix-notation expressions, including but not
7728 limited to those sorts of expressions normally encoded as addends in the
7729 addend field. The symbol mangling format is:
7732 | <unary-operator> ':' <node>
7733 | <binary-operator> ':' <node> ':' <node>
7736 <literal> := 's' <digits=N> ':' <N character symbol name>
7737 | 'S' <digits=N> ':' <N character section name>
7741 <binary-operator> := as in C
7742 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7745 set_symbol_value (bfd
*bfd_with_globals
,
7746 Elf_Internal_Sym
*isymbuf
,
7751 struct elf_link_hash_entry
**sym_hashes
;
7752 struct elf_link_hash_entry
*h
;
7753 size_t extsymoff
= locsymcount
;
7755 if (symidx
< locsymcount
)
7757 Elf_Internal_Sym
*sym
;
7759 sym
= isymbuf
+ symidx
;
7760 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7762 /* It is a local symbol: move it to the
7763 "absolute" section and give it a value. */
7764 sym
->st_shndx
= SHN_ABS
;
7765 sym
->st_value
= val
;
7768 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7772 /* It is a global symbol: set its link type
7773 to "defined" and give it a value. */
7775 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7776 h
= sym_hashes
[symidx
- extsymoff
];
7777 while (h
->root
.type
== bfd_link_hash_indirect
7778 || h
->root
.type
== bfd_link_hash_warning
)
7779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7780 h
->root
.type
= bfd_link_hash_defined
;
7781 h
->root
.u
.def
.value
= val
;
7782 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7786 resolve_symbol (const char *name
,
7788 struct elf_final_link_info
*flinfo
,
7790 Elf_Internal_Sym
*isymbuf
,
7793 Elf_Internal_Sym
*sym
;
7794 struct bfd_link_hash_entry
*global_entry
;
7795 const char *candidate
= NULL
;
7796 Elf_Internal_Shdr
*symtab_hdr
;
7799 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7801 for (i
= 0; i
< locsymcount
; ++ i
)
7805 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7808 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7809 symtab_hdr
->sh_link
,
7812 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7813 name
, candidate
, (unsigned long) sym
->st_value
);
7815 if (candidate
&& strcmp (candidate
, name
) == 0)
7817 asection
*sec
= flinfo
->sections
[i
];
7819 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7820 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7822 printf ("Found symbol with value %8.8lx\n",
7823 (unsigned long) *result
);
7829 /* Hmm, haven't found it yet. perhaps it is a global. */
7830 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7831 FALSE
, FALSE
, TRUE
);
7835 if (global_entry
->type
== bfd_link_hash_defined
7836 || global_entry
->type
== bfd_link_hash_defweak
)
7838 *result
= (global_entry
->u
.def
.value
7839 + global_entry
->u
.def
.section
->output_section
->vma
7840 + global_entry
->u
.def
.section
->output_offset
);
7842 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7843 global_entry
->root
.string
, (unsigned long) *result
);
7851 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7852 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7853 names like "foo.end" which is the end address of section "foo". */
7856 resolve_section (const char *name
,
7864 for (curr
= sections
; curr
; curr
= curr
->next
)
7865 if (strcmp (curr
->name
, name
) == 0)
7867 *result
= curr
->vma
;
7871 /* Hmm. still haven't found it. try pseudo-section names. */
7872 /* FIXME: This could be coded more efficiently... */
7873 for (curr
= sections
; curr
; curr
= curr
->next
)
7875 len
= strlen (curr
->name
);
7876 if (len
> strlen (name
))
7879 if (strncmp (curr
->name
, name
, len
) == 0)
7881 if (strncmp (".end", name
+ len
, 4) == 0)
7883 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7887 /* Insert more pseudo-section names here, if you like. */
7895 undefined_reference (const char *reftype
, const char *name
)
7897 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7902 eval_symbol (bfd_vma
*result
,
7905 struct elf_final_link_info
*flinfo
,
7907 Elf_Internal_Sym
*isymbuf
,
7916 const char *sym
= *symp
;
7918 bfd_boolean symbol_is_section
= FALSE
;
7923 if (len
< 1 || len
> sizeof (symbuf
))
7925 bfd_set_error (bfd_error_invalid_operation
);
7938 *result
= strtoul (sym
, (char **) symp
, 16);
7942 symbol_is_section
= TRUE
;
7945 symlen
= strtol (sym
, (char **) symp
, 10);
7946 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7948 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7950 bfd_set_error (bfd_error_invalid_operation
);
7954 memcpy (symbuf
, sym
, symlen
);
7955 symbuf
[symlen
] = '\0';
7956 *symp
= sym
+ symlen
;
7958 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7959 the symbol as a section, or vice-versa. so we're pretty liberal in our
7960 interpretation here; section means "try section first", not "must be a
7961 section", and likewise with symbol. */
7963 if (symbol_is_section
)
7965 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
7966 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7967 isymbuf
, locsymcount
))
7969 undefined_reference ("section", symbuf
);
7975 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7976 isymbuf
, locsymcount
)
7977 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
7980 undefined_reference ("symbol", symbuf
);
7987 /* All that remains are operators. */
7989 #define UNARY_OP(op) \
7990 if (strncmp (sym, #op, strlen (#op)) == 0) \
7992 sym += strlen (#op); \
7996 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7997 isymbuf, locsymcount, signed_p)) \
8000 *result = op ((bfd_signed_vma) a); \
8006 #define BINARY_OP(op) \
8007 if (strncmp (sym, #op, strlen (#op)) == 0) \
8009 sym += strlen (#op); \
8013 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8014 isymbuf, locsymcount, signed_p)) \
8017 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8018 isymbuf, locsymcount, signed_p)) \
8021 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8051 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8052 bfd_set_error (bfd_error_invalid_operation
);
8058 put_value (bfd_vma size
,
8059 unsigned long chunksz
,
8064 location
+= (size
- chunksz
);
8066 for (; size
; size
-= chunksz
, location
-= chunksz
)
8071 bfd_put_8 (input_bfd
, x
, location
);
8075 bfd_put_16 (input_bfd
, x
, location
);
8079 bfd_put_32 (input_bfd
, x
, location
);
8080 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8086 bfd_put_64 (input_bfd
, x
, location
);
8087 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8100 get_value (bfd_vma size
,
8101 unsigned long chunksz
,
8108 /* Sanity checks. */
8109 BFD_ASSERT (chunksz
<= sizeof (x
)
8112 && (size
% chunksz
) == 0
8113 && input_bfd
!= NULL
8114 && location
!= NULL
);
8116 if (chunksz
== sizeof (x
))
8118 BFD_ASSERT (size
== chunksz
);
8120 /* Make sure that we do not perform an undefined shift operation.
8121 We know that size == chunksz so there will only be one iteration
8122 of the loop below. */
8126 shift
= 8 * chunksz
;
8128 for (; size
; size
-= chunksz
, location
+= chunksz
)
8133 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8136 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8139 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8143 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8154 decode_complex_addend (unsigned long *start
, /* in bits */
8155 unsigned long *oplen
, /* in bits */
8156 unsigned long *len
, /* in bits */
8157 unsigned long *wordsz
, /* in bytes */
8158 unsigned long *chunksz
, /* in bytes */
8159 unsigned long *lsb0_p
,
8160 unsigned long *signed_p
,
8161 unsigned long *trunc_p
,
8162 unsigned long encoded
)
8164 * start
= encoded
& 0x3F;
8165 * len
= (encoded
>> 6) & 0x3F;
8166 * oplen
= (encoded
>> 12) & 0x3F;
8167 * wordsz
= (encoded
>> 18) & 0xF;
8168 * chunksz
= (encoded
>> 22) & 0xF;
8169 * lsb0_p
= (encoded
>> 27) & 1;
8170 * signed_p
= (encoded
>> 28) & 1;
8171 * trunc_p
= (encoded
>> 29) & 1;
8174 bfd_reloc_status_type
8175 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8176 asection
*input_section ATTRIBUTE_UNUSED
,
8178 Elf_Internal_Rela
*rel
,
8181 bfd_vma shift
, x
, mask
;
8182 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8183 bfd_reloc_status_type r
;
8185 /* Perform this reloc, since it is complex.
8186 (this is not to say that it necessarily refers to a complex
8187 symbol; merely that it is a self-describing CGEN based reloc.
8188 i.e. the addend has the complete reloc information (bit start, end,
8189 word size, etc) encoded within it.). */
8191 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8192 &chunksz
, &lsb0_p
, &signed_p
,
8193 &trunc_p
, rel
->r_addend
);
8195 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8198 shift
= (start
+ 1) - len
;
8200 shift
= (8 * wordsz
) - (start
+ len
);
8202 x
= get_value (wordsz
, chunksz
, input_bfd
,
8203 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8206 printf ("Doing complex reloc: "
8207 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8208 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8209 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8210 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8211 oplen
, (unsigned long) x
, (unsigned long) mask
,
8212 (unsigned long) relocation
);
8217 /* Now do an overflow check. */
8218 r
= bfd_check_overflow ((signed_p
8219 ? complain_overflow_signed
8220 : complain_overflow_unsigned
),
8221 len
, 0, (8 * wordsz
),
8225 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8228 printf (" relocation: %8.8lx\n"
8229 " shifted mask: %8.8lx\n"
8230 " shifted/masked reloc: %8.8lx\n"
8231 " result: %8.8lx\n",
8232 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8233 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8235 put_value (wordsz
, chunksz
, input_bfd
, x
,
8236 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8240 /* Functions to read r_offset from external (target order) reloc
8241 entry. Faster than bfd_getl32 et al, because we let the compiler
8242 know the value is aligned. */
8245 ext32l_r_offset (const void *p
)
8252 const union aligned32
*a
8253 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8255 uint32_t aval
= ( (uint32_t) a
->c
[0]
8256 | (uint32_t) a
->c
[1] << 8
8257 | (uint32_t) a
->c
[2] << 16
8258 | (uint32_t) a
->c
[3] << 24);
8263 ext32b_r_offset (const void *p
)
8270 const union aligned32
*a
8271 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8273 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8274 | (uint32_t) a
->c
[1] << 16
8275 | (uint32_t) a
->c
[2] << 8
8276 | (uint32_t) a
->c
[3]);
8280 #ifdef BFD_HOST_64_BIT
8282 ext64l_r_offset (const void *p
)
8289 const union aligned64
*a
8290 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8292 uint64_t aval
= ( (uint64_t) a
->c
[0]
8293 | (uint64_t) a
->c
[1] << 8
8294 | (uint64_t) a
->c
[2] << 16
8295 | (uint64_t) a
->c
[3] << 24
8296 | (uint64_t) a
->c
[4] << 32
8297 | (uint64_t) a
->c
[5] << 40
8298 | (uint64_t) a
->c
[6] << 48
8299 | (uint64_t) a
->c
[7] << 56);
8304 ext64b_r_offset (const void *p
)
8311 const union aligned64
*a
8312 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8314 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8315 | (uint64_t) a
->c
[1] << 48
8316 | (uint64_t) a
->c
[2] << 40
8317 | (uint64_t) a
->c
[3] << 32
8318 | (uint64_t) a
->c
[4] << 24
8319 | (uint64_t) a
->c
[5] << 16
8320 | (uint64_t) a
->c
[6] << 8
8321 | (uint64_t) a
->c
[7]);
8326 /* When performing a relocatable link, the input relocations are
8327 preserved. But, if they reference global symbols, the indices
8328 referenced must be updated. Update all the relocations found in
8332 elf_link_adjust_relocs (bfd
*abfd
,
8334 struct bfd_elf_section_reloc_data
*reldata
,
8338 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8340 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8341 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8342 bfd_vma r_type_mask
;
8344 unsigned int count
= reldata
->count
;
8345 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8347 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8349 swap_in
= bed
->s
->swap_reloc_in
;
8350 swap_out
= bed
->s
->swap_reloc_out
;
8352 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8354 swap_in
= bed
->s
->swap_reloca_in
;
8355 swap_out
= bed
->s
->swap_reloca_out
;
8360 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8363 if (bed
->s
->arch_size
== 32)
8370 r_type_mask
= 0xffffffff;
8374 erela
= reldata
->hdr
->contents
;
8375 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8377 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8380 if (*rel_hash
== NULL
)
8383 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8385 (*swap_in
) (abfd
, erela
, irela
);
8386 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8387 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8388 | (irela
[j
].r_info
& r_type_mask
));
8389 (*swap_out
) (abfd
, irela
, erela
);
8392 if (bed
->elf_backend_update_relocs
)
8393 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8395 if (sort
&& count
!= 0)
8397 bfd_vma (*ext_r_off
) (const void *);
8400 bfd_byte
*base
, *end
, *p
, *loc
;
8401 bfd_byte
*buf
= NULL
;
8403 if (bed
->s
->arch_size
== 32)
8405 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8406 ext_r_off
= ext32l_r_offset
;
8407 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8408 ext_r_off
= ext32b_r_offset
;
8414 #ifdef BFD_HOST_64_BIT
8415 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8416 ext_r_off
= ext64l_r_offset
;
8417 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8418 ext_r_off
= ext64b_r_offset
;
8424 /* Must use a stable sort here. A modified insertion sort,
8425 since the relocs are mostly sorted already. */
8426 elt_size
= reldata
->hdr
->sh_entsize
;
8427 base
= reldata
->hdr
->contents
;
8428 end
= base
+ count
* elt_size
;
8429 if (elt_size
> sizeof (Elf64_External_Rela
))
8432 /* Ensure the first element is lowest. This acts as a sentinel,
8433 speeding the main loop below. */
8434 r_off
= (*ext_r_off
) (base
);
8435 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8437 bfd_vma r_off2
= (*ext_r_off
) (p
);
8446 /* Don't just swap *base and *loc as that changes the order
8447 of the original base[0] and base[1] if they happen to
8448 have the same r_offset. */
8449 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8450 memcpy (onebuf
, loc
, elt_size
);
8451 memmove (base
+ elt_size
, base
, loc
- base
);
8452 memcpy (base
, onebuf
, elt_size
);
8455 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8457 /* base to p is sorted, *p is next to insert. */
8458 r_off
= (*ext_r_off
) (p
);
8459 /* Search the sorted region for location to insert. */
8461 while (r_off
< (*ext_r_off
) (loc
))
8466 /* Chances are there is a run of relocs to insert here,
8467 from one of more input files. Files are not always
8468 linked in order due to the way elf_link_input_bfd is
8469 called. See pr17666. */
8470 size_t sortlen
= p
- loc
;
8471 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8472 size_t runlen
= elt_size
;
8473 size_t buf_size
= 96 * 1024;
8474 while (p
+ runlen
< end
8475 && (sortlen
<= buf_size
8476 || runlen
+ elt_size
<= buf_size
)
8477 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8481 buf
= bfd_malloc (buf_size
);
8485 if (runlen
< sortlen
)
8487 memcpy (buf
, p
, runlen
);
8488 memmove (loc
+ runlen
, loc
, sortlen
);
8489 memcpy (loc
, buf
, runlen
);
8493 memcpy (buf
, loc
, sortlen
);
8494 memmove (loc
, p
, runlen
);
8495 memcpy (loc
+ runlen
, buf
, sortlen
);
8497 p
+= runlen
- elt_size
;
8500 /* Hashes are no longer valid. */
8501 free (reldata
->hashes
);
8502 reldata
->hashes
= NULL
;
8508 struct elf_link_sort_rela
8514 enum elf_reloc_type_class type
;
8515 /* We use this as an array of size int_rels_per_ext_rel. */
8516 Elf_Internal_Rela rela
[1];
8520 elf_link_sort_cmp1 (const void *A
, const void *B
)
8522 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8523 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8524 int relativea
, relativeb
;
8526 relativea
= a
->type
== reloc_class_relative
;
8527 relativeb
= b
->type
== reloc_class_relative
;
8529 if (relativea
< relativeb
)
8531 if (relativea
> relativeb
)
8533 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8535 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8537 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8539 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8545 elf_link_sort_cmp2 (const void *A
, const void *B
)
8547 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8548 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8550 if (a
->type
< b
->type
)
8552 if (a
->type
> b
->type
)
8554 if (a
->u
.offset
< b
->u
.offset
)
8556 if (a
->u
.offset
> b
->u
.offset
)
8558 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8560 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8566 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8568 asection
*dynamic_relocs
;
8571 bfd_size_type count
, size
;
8572 size_t i
, ret
, sort_elt
, ext_size
;
8573 bfd_byte
*sort
, *s_non_relative
, *p
;
8574 struct elf_link_sort_rela
*sq
;
8575 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8576 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8577 unsigned int opb
= bfd_octets_per_byte (abfd
);
8578 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8579 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8580 struct bfd_link_order
*lo
;
8582 bfd_boolean use_rela
;
8584 /* Find a dynamic reloc section. */
8585 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8586 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8587 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8588 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8590 bfd_boolean use_rela_initialised
= FALSE
;
8592 /* This is just here to stop gcc from complaining.
8593 Its initialization checking code is not perfect. */
8596 /* Both sections are present. Examine the sizes
8597 of the indirect sections to help us choose. */
8598 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8599 if (lo
->type
== bfd_indirect_link_order
)
8601 asection
*o
= lo
->u
.indirect
.section
;
8603 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8605 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8606 /* Section size is divisible by both rel and rela sizes.
8607 It is of no help to us. */
8611 /* Section size is only divisible by rela. */
8612 if (use_rela_initialised
&& (use_rela
== FALSE
))
8614 _bfd_error_handler (_("%B: Unable to sort relocs - "
8615 "they are in more than one size"),
8617 bfd_set_error (bfd_error_invalid_operation
);
8623 use_rela_initialised
= TRUE
;
8627 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8629 /* Section size is only divisible by rel. */
8630 if (use_rela_initialised
&& (use_rela
== TRUE
))
8632 _bfd_error_handler (_("%B: Unable to sort relocs - "
8633 "they are in more than one size"),
8635 bfd_set_error (bfd_error_invalid_operation
);
8641 use_rela_initialised
= TRUE
;
8646 /* The section size is not divisible by either -
8647 something is wrong. */
8648 _bfd_error_handler (_("%B: Unable to sort relocs - "
8649 "they are of an unknown size"), abfd
);
8650 bfd_set_error (bfd_error_invalid_operation
);
8655 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8656 if (lo
->type
== bfd_indirect_link_order
)
8658 asection
*o
= lo
->u
.indirect
.section
;
8660 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8662 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8663 /* Section size is divisible by both rel and rela sizes.
8664 It is of no help to us. */
8668 /* Section size is only divisible by rela. */
8669 if (use_rela_initialised
&& (use_rela
== FALSE
))
8671 _bfd_error_handler (_("%B: Unable to sort relocs - "
8672 "they are in more than one size"),
8674 bfd_set_error (bfd_error_invalid_operation
);
8680 use_rela_initialised
= TRUE
;
8684 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8686 /* Section size is only divisible by rel. */
8687 if (use_rela_initialised
&& (use_rela
== TRUE
))
8689 _bfd_error_handler (_("%B: Unable to sort relocs - "
8690 "they are in more than one size"),
8692 bfd_set_error (bfd_error_invalid_operation
);
8698 use_rela_initialised
= TRUE
;
8703 /* The section size is not divisible by either -
8704 something is wrong. */
8705 _bfd_error_handler (_("%B: Unable to sort relocs - "
8706 "they are of an unknown size"), abfd
);
8707 bfd_set_error (bfd_error_invalid_operation
);
8712 if (! use_rela_initialised
)
8716 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8718 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8725 dynamic_relocs
= rela_dyn
;
8726 ext_size
= bed
->s
->sizeof_rela
;
8727 swap_in
= bed
->s
->swap_reloca_in
;
8728 swap_out
= bed
->s
->swap_reloca_out
;
8732 dynamic_relocs
= rel_dyn
;
8733 ext_size
= bed
->s
->sizeof_rel
;
8734 swap_in
= bed
->s
->swap_reloc_in
;
8735 swap_out
= bed
->s
->swap_reloc_out
;
8739 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8740 if (lo
->type
== bfd_indirect_link_order
)
8741 size
+= lo
->u
.indirect
.section
->size
;
8743 if (size
!= dynamic_relocs
->size
)
8746 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8747 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8749 count
= dynamic_relocs
->size
/ ext_size
;
8752 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8756 (*info
->callbacks
->warning
)
8757 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8761 if (bed
->s
->arch_size
== 32)
8762 r_sym_mask
= ~(bfd_vma
) 0xff;
8764 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8766 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8767 if (lo
->type
== bfd_indirect_link_order
)
8769 bfd_byte
*erel
, *erelend
;
8770 asection
*o
= lo
->u
.indirect
.section
;
8772 if (o
->contents
== NULL
&& o
->size
!= 0)
8774 /* This is a reloc section that is being handled as a normal
8775 section. See bfd_section_from_shdr. We can't combine
8776 relocs in this case. */
8781 erelend
= o
->contents
+ o
->size
;
8782 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8784 while (erel
< erelend
)
8786 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8788 (*swap_in
) (abfd
, erel
, s
->rela
);
8789 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8790 s
->u
.sym_mask
= r_sym_mask
;
8796 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8798 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8800 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8801 if (s
->type
!= reloc_class_relative
)
8807 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8808 for (; i
< count
; i
++, p
+= sort_elt
)
8810 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8811 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8813 sp
->u
.offset
= sq
->rela
->r_offset
;
8816 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8818 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8819 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8821 /* We have plt relocs in .rela.dyn. */
8822 sq
= (struct elf_link_sort_rela
*) sort
;
8823 for (i
= 0; i
< count
; i
++)
8824 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8826 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8828 struct bfd_link_order
**plo
;
8829 /* Put srelplt link_order last. This is so the output_offset
8830 set in the next loop is correct for DT_JMPREL. */
8831 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8832 if ((*plo
)->type
== bfd_indirect_link_order
8833 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8839 plo
= &(*plo
)->next
;
8842 dynamic_relocs
->map_tail
.link_order
= lo
;
8847 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8848 if (lo
->type
== bfd_indirect_link_order
)
8850 bfd_byte
*erel
, *erelend
;
8851 asection
*o
= lo
->u
.indirect
.section
;
8854 erelend
= o
->contents
+ o
->size
;
8855 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8856 while (erel
< erelend
)
8858 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8859 (*swap_out
) (abfd
, s
->rela
, erel
);
8866 *psec
= dynamic_relocs
;
8870 /* Add a symbol to the output symbol string table. */
8873 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8875 Elf_Internal_Sym
*elfsym
,
8876 asection
*input_sec
,
8877 struct elf_link_hash_entry
*h
)
8879 int (*output_symbol_hook
)
8880 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8881 struct elf_link_hash_entry
*);
8882 struct elf_link_hash_table
*hash_table
;
8883 const struct elf_backend_data
*bed
;
8884 bfd_size_type strtabsize
;
8886 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8888 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8889 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8890 if (output_symbol_hook
!= NULL
)
8892 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8899 || (input_sec
->flags
& SEC_EXCLUDE
))
8900 elfsym
->st_name
= (unsigned long) -1;
8903 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8904 to get the final offset for st_name. */
8906 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8908 if (elfsym
->st_name
== (unsigned long) -1)
8912 hash_table
= elf_hash_table (flinfo
->info
);
8913 strtabsize
= hash_table
->strtabsize
;
8914 if (strtabsize
<= hash_table
->strtabcount
)
8916 strtabsize
+= strtabsize
;
8917 hash_table
->strtabsize
= strtabsize
;
8918 strtabsize
*= sizeof (*hash_table
->strtab
);
8920 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8922 if (hash_table
->strtab
== NULL
)
8925 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8926 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8927 = hash_table
->strtabcount
;
8928 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8929 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8931 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8932 hash_table
->strtabcount
+= 1;
8937 /* Swap symbols out to the symbol table and flush the output symbols to
8941 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8943 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8946 const struct elf_backend_data
*bed
;
8948 Elf_Internal_Shdr
*hdr
;
8952 if (!hash_table
->strtabcount
)
8955 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8957 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8959 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8960 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8964 if (flinfo
->symshndxbuf
)
8966 amt
= sizeof (Elf_External_Sym_Shndx
);
8967 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
8968 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
8969 if (flinfo
->symshndxbuf
== NULL
)
8976 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
8978 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
8979 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8980 elfsym
->sym
.st_name
= 0;
8983 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
8984 elfsym
->sym
.st_name
);
8985 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
8986 ((bfd_byte
*) symbuf
8987 + (elfsym
->dest_index
8988 * bed
->s
->sizeof_sym
)),
8989 (flinfo
->symshndxbuf
8990 + elfsym
->destshndx_index
));
8993 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
8994 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8995 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
8996 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
8997 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
8999 hdr
->sh_size
+= amt
;
9007 free (hash_table
->strtab
);
9008 hash_table
->strtab
= NULL
;
9013 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9016 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9018 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9019 && sym
->st_shndx
< SHN_LORESERVE
)
9021 /* The gABI doesn't support dynamic symbols in output sections
9023 (*_bfd_error_handler
)
9024 (_("%B: Too many sections: %d (>= %d)"),
9025 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9026 bfd_set_error (bfd_error_nonrepresentable_section
);
9032 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9033 allowing an unsatisfied unversioned symbol in the DSO to match a
9034 versioned symbol that would normally require an explicit version.
9035 We also handle the case that a DSO references a hidden symbol
9036 which may be satisfied by a versioned symbol in another DSO. */
9039 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9040 const struct elf_backend_data
*bed
,
9041 struct elf_link_hash_entry
*h
)
9044 struct elf_link_loaded_list
*loaded
;
9046 if (!is_elf_hash_table (info
->hash
))
9049 /* Check indirect symbol. */
9050 while (h
->root
.type
== bfd_link_hash_indirect
)
9051 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9053 switch (h
->root
.type
)
9059 case bfd_link_hash_undefined
:
9060 case bfd_link_hash_undefweak
:
9061 abfd
= h
->root
.u
.undef
.abfd
;
9063 || (abfd
->flags
& DYNAMIC
) == 0
9064 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9068 case bfd_link_hash_defined
:
9069 case bfd_link_hash_defweak
:
9070 abfd
= h
->root
.u
.def
.section
->owner
;
9073 case bfd_link_hash_common
:
9074 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9077 BFD_ASSERT (abfd
!= NULL
);
9079 for (loaded
= elf_hash_table (info
)->loaded
;
9081 loaded
= loaded
->next
)
9084 Elf_Internal_Shdr
*hdr
;
9088 Elf_Internal_Shdr
*versymhdr
;
9089 Elf_Internal_Sym
*isym
;
9090 Elf_Internal_Sym
*isymend
;
9091 Elf_Internal_Sym
*isymbuf
;
9092 Elf_External_Versym
*ever
;
9093 Elf_External_Versym
*extversym
;
9095 input
= loaded
->abfd
;
9097 /* We check each DSO for a possible hidden versioned definition. */
9099 || (input
->flags
& DYNAMIC
) == 0
9100 || elf_dynversym (input
) == 0)
9103 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9105 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9106 if (elf_bad_symtab (input
))
9108 extsymcount
= symcount
;
9113 extsymcount
= symcount
- hdr
->sh_info
;
9114 extsymoff
= hdr
->sh_info
;
9117 if (extsymcount
== 0)
9120 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9122 if (isymbuf
== NULL
)
9125 /* Read in any version definitions. */
9126 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9127 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9128 if (extversym
== NULL
)
9131 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9132 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9133 != versymhdr
->sh_size
))
9141 ever
= extversym
+ extsymoff
;
9142 isymend
= isymbuf
+ extsymcount
;
9143 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9146 Elf_Internal_Versym iver
;
9147 unsigned short version_index
;
9149 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9150 || isym
->st_shndx
== SHN_UNDEF
)
9153 name
= bfd_elf_string_from_elf_section (input
,
9156 if (strcmp (name
, h
->root
.root
.string
) != 0)
9159 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9161 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9163 && h
->forced_local
))
9165 /* If we have a non-hidden versioned sym, then it should
9166 have provided a definition for the undefined sym unless
9167 it is defined in a non-shared object and forced local.
9172 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9173 if (version_index
== 1 || version_index
== 2)
9175 /* This is the base or first version. We can use it. */
9189 /* Convert ELF common symbol TYPE. */
9192 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9194 /* Commom symbol can only appear in relocatable link. */
9195 if (!bfd_link_relocatable (info
))
9197 switch (info
->elf_stt_common
)
9201 case elf_stt_common
:
9204 case no_elf_stt_common
:
9211 /* Add an external symbol to the symbol table. This is called from
9212 the hash table traversal routine. When generating a shared object,
9213 we go through the symbol table twice. The first time we output
9214 anything that might have been forced to local scope in a version
9215 script. The second time we output the symbols that are still
9219 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9221 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9222 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9223 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9225 Elf_Internal_Sym sym
;
9226 asection
*input_sec
;
9227 const struct elf_backend_data
*bed
;
9231 /* A symbol is bound locally if it is forced local or it is locally
9232 defined, hidden versioned, not referenced by shared library and
9233 not exported when linking executable. */
9234 bfd_boolean local_bind
= (h
->forced_local
9235 || (bfd_link_executable (flinfo
->info
)
9236 && !flinfo
->info
->export_dynamic
9240 && h
->versioned
== versioned_hidden
));
9242 if (h
->root
.type
== bfd_link_hash_warning
)
9244 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9245 if (h
->root
.type
== bfd_link_hash_new
)
9249 /* Decide whether to output this symbol in this pass. */
9250 if (eoinfo
->localsyms
)
9261 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9263 if (h
->root
.type
== bfd_link_hash_undefined
)
9265 /* If we have an undefined symbol reference here then it must have
9266 come from a shared library that is being linked in. (Undefined
9267 references in regular files have already been handled unless
9268 they are in unreferenced sections which are removed by garbage
9270 bfd_boolean ignore_undef
= FALSE
;
9272 /* Some symbols may be special in that the fact that they're
9273 undefined can be safely ignored - let backend determine that. */
9274 if (bed
->elf_backend_ignore_undef_symbol
)
9275 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9277 /* If we are reporting errors for this situation then do so now. */
9280 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9281 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9282 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9283 (*flinfo
->info
->callbacks
->undefined_symbol
)
9284 (flinfo
->info
, h
->root
.root
.string
,
9285 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9287 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9289 /* Strip a global symbol defined in a discarded section. */
9294 /* We should also warn if a forced local symbol is referenced from
9295 shared libraries. */
9296 if (bfd_link_executable (flinfo
->info
)
9301 && h
->ref_dynamic_nonweak
9302 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9306 struct elf_link_hash_entry
*hi
= h
;
9308 /* Check indirect symbol. */
9309 while (hi
->root
.type
== bfd_link_hash_indirect
)
9310 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9312 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9313 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9314 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9315 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9317 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9318 def_bfd
= flinfo
->output_bfd
;
9319 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9320 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9321 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, def_bfd
,
9322 h
->root
.root
.string
);
9323 bfd_set_error (bfd_error_bad_value
);
9324 eoinfo
->failed
= TRUE
;
9328 /* We don't want to output symbols that have never been mentioned by
9329 a regular file, or that we have been told to strip. However, if
9330 h->indx is set to -2, the symbol is used by a reloc and we must
9335 else if ((h
->def_dynamic
9337 || h
->root
.type
== bfd_link_hash_new
)
9341 else if (flinfo
->info
->strip
== strip_all
)
9343 else if (flinfo
->info
->strip
== strip_some
9344 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9345 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9347 else if ((h
->root
.type
== bfd_link_hash_defined
9348 || h
->root
.type
== bfd_link_hash_defweak
)
9349 && ((flinfo
->info
->strip_discarded
9350 && discarded_section (h
->root
.u
.def
.section
))
9351 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9352 && h
->root
.u
.def
.section
->owner
!= NULL
9353 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9355 else if ((h
->root
.type
== bfd_link_hash_undefined
9356 || h
->root
.type
== bfd_link_hash_undefweak
)
9357 && h
->root
.u
.undef
.abfd
!= NULL
9358 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9363 /* If we're stripping it, and it's not a dynamic symbol, there's
9364 nothing else to do. However, if it is a forced local symbol or
9365 an ifunc symbol we need to give the backend finish_dynamic_symbol
9366 function a chance to make it dynamic. */
9369 && type
!= STT_GNU_IFUNC
9370 && !h
->forced_local
)
9374 sym
.st_size
= h
->size
;
9375 sym
.st_other
= h
->other
;
9376 switch (h
->root
.type
)
9379 case bfd_link_hash_new
:
9380 case bfd_link_hash_warning
:
9384 case bfd_link_hash_undefined
:
9385 case bfd_link_hash_undefweak
:
9386 input_sec
= bfd_und_section_ptr
;
9387 sym
.st_shndx
= SHN_UNDEF
;
9390 case bfd_link_hash_defined
:
9391 case bfd_link_hash_defweak
:
9393 input_sec
= h
->root
.u
.def
.section
;
9394 if (input_sec
->output_section
!= NULL
)
9397 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9398 input_sec
->output_section
);
9399 if (sym
.st_shndx
== SHN_BAD
)
9401 (*_bfd_error_handler
)
9402 (_("%B: could not find output section %A for input section %A"),
9403 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9404 bfd_set_error (bfd_error_nonrepresentable_section
);
9405 eoinfo
->failed
= TRUE
;
9409 /* ELF symbols in relocatable files are section relative,
9410 but in nonrelocatable files they are virtual
9412 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9413 if (!bfd_link_relocatable (flinfo
->info
))
9415 sym
.st_value
+= input_sec
->output_section
->vma
;
9416 if (h
->type
== STT_TLS
)
9418 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9419 if (tls_sec
!= NULL
)
9420 sym
.st_value
-= tls_sec
->vma
;
9426 BFD_ASSERT (input_sec
->owner
== NULL
9427 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9428 sym
.st_shndx
= SHN_UNDEF
;
9429 input_sec
= bfd_und_section_ptr
;
9434 case bfd_link_hash_common
:
9435 input_sec
= h
->root
.u
.c
.p
->section
;
9436 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9437 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9440 case bfd_link_hash_indirect
:
9441 /* These symbols are created by symbol versioning. They point
9442 to the decorated version of the name. For example, if the
9443 symbol foo@@GNU_1.2 is the default, which should be used when
9444 foo is used with no version, then we add an indirect symbol
9445 foo which points to foo@@GNU_1.2. We ignore these symbols,
9446 since the indirected symbol is already in the hash table. */
9450 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9451 switch (h
->root
.type
)
9453 case bfd_link_hash_common
:
9454 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9456 case bfd_link_hash_defined
:
9457 case bfd_link_hash_defweak
:
9458 if (bed
->common_definition (&sym
))
9459 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9463 case bfd_link_hash_undefined
:
9464 case bfd_link_hash_undefweak
:
9472 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9473 /* Turn off visibility on local symbol. */
9474 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9476 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9477 else if (h
->unique_global
&& h
->def_regular
)
9478 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9479 else if (h
->root
.type
== bfd_link_hash_undefweak
9480 || h
->root
.type
== bfd_link_hash_defweak
)
9481 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9483 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9484 sym
.st_target_internal
= h
->target_internal
;
9486 /* Give the processor backend a chance to tweak the symbol value,
9487 and also to finish up anything that needs to be done for this
9488 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9489 forced local syms when non-shared is due to a historical quirk.
9490 STT_GNU_IFUNC symbol must go through PLT. */
9491 if ((h
->type
== STT_GNU_IFUNC
9493 && !bfd_link_relocatable (flinfo
->info
))
9494 || ((h
->dynindx
!= -1
9496 && ((bfd_link_pic (flinfo
->info
)
9497 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9498 || h
->root
.type
!= bfd_link_hash_undefweak
))
9499 || !h
->forced_local
)
9500 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9502 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9503 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9505 eoinfo
->failed
= TRUE
;
9510 /* If we are marking the symbol as undefined, and there are no
9511 non-weak references to this symbol from a regular object, then
9512 mark the symbol as weak undefined; if there are non-weak
9513 references, mark the symbol as strong. We can't do this earlier,
9514 because it might not be marked as undefined until the
9515 finish_dynamic_symbol routine gets through with it. */
9516 if (sym
.st_shndx
== SHN_UNDEF
9518 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9519 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9522 type
= ELF_ST_TYPE (sym
.st_info
);
9524 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9525 if (type
== STT_GNU_IFUNC
)
9528 if (h
->ref_regular_nonweak
)
9529 bindtype
= STB_GLOBAL
;
9531 bindtype
= STB_WEAK
;
9532 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9535 /* If this is a symbol defined in a dynamic library, don't use the
9536 symbol size from the dynamic library. Relinking an executable
9537 against a new library may introduce gratuitous changes in the
9538 executable's symbols if we keep the size. */
9539 if (sym
.st_shndx
== SHN_UNDEF
9544 /* If a non-weak symbol with non-default visibility is not defined
9545 locally, it is a fatal error. */
9546 if (!bfd_link_relocatable (flinfo
->info
)
9547 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9548 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9549 && h
->root
.type
== bfd_link_hash_undefined
9554 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9555 msg
= _("%B: protected symbol `%s' isn't defined");
9556 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9557 msg
= _("%B: internal symbol `%s' isn't defined");
9559 msg
= _("%B: hidden symbol `%s' isn't defined");
9560 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9561 bfd_set_error (bfd_error_bad_value
);
9562 eoinfo
->failed
= TRUE
;
9566 /* If this symbol should be put in the .dynsym section, then put it
9567 there now. We already know the symbol index. We also fill in
9568 the entry in the .hash section. */
9569 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9571 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9575 /* Since there is no version information in the dynamic string,
9576 if there is no version info in symbol version section, we will
9577 have a run-time problem if not linking executable, referenced
9578 by shared library, not locally defined, or not bound locally.
9580 if (h
->verinfo
.verdef
== NULL
9582 && (!bfd_link_executable (flinfo
->info
)
9584 || !h
->def_regular
))
9586 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9588 if (p
&& p
[1] != '\0')
9590 (*_bfd_error_handler
)
9591 (_("%B: No symbol version section for versioned symbol `%s'"),
9592 flinfo
->output_bfd
, h
->root
.root
.string
);
9593 eoinfo
->failed
= TRUE
;
9598 sym
.st_name
= h
->dynstr_index
;
9599 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9600 + h
->dynindx
* bed
->s
->sizeof_sym
);
9601 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9603 eoinfo
->failed
= TRUE
;
9606 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9608 if (flinfo
->hash_sec
!= NULL
)
9610 size_t hash_entry_size
;
9611 bfd_byte
*bucketpos
;
9616 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9617 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9620 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9621 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9622 + (bucket
+ 2) * hash_entry_size
);
9623 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9624 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9626 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9627 ((bfd_byte
*) flinfo
->hash_sec
->contents
9628 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9631 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9633 Elf_Internal_Versym iversym
;
9634 Elf_External_Versym
*eversym
;
9636 if (!h
->def_regular
)
9638 if (h
->verinfo
.verdef
== NULL
9639 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9640 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9641 iversym
.vs_vers
= 0;
9643 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9647 if (h
->verinfo
.vertree
== NULL
)
9648 iversym
.vs_vers
= 1;
9650 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9651 if (flinfo
->info
->create_default_symver
)
9655 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9657 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9658 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9660 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9661 eversym
+= h
->dynindx
;
9662 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9666 /* If the symbol is undefined, and we didn't output it to .dynsym,
9667 strip it from .symtab too. Obviously we can't do this for
9668 relocatable output or when needed for --emit-relocs. */
9669 else if (input_sec
== bfd_und_section_ptr
9671 && !bfd_link_relocatable (flinfo
->info
))
9673 /* Also strip others that we couldn't earlier due to dynamic symbol
9677 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9680 /* Output a FILE symbol so that following locals are not associated
9681 with the wrong input file. We need one for forced local symbols
9682 if we've seen more than one FILE symbol or when we have exactly
9683 one FILE symbol but global symbols are present in a file other
9684 than the one with the FILE symbol. We also need one if linker
9685 defined symbols are present. In practice these conditions are
9686 always met, so just emit the FILE symbol unconditionally. */
9687 if (eoinfo
->localsyms
9688 && !eoinfo
->file_sym_done
9689 && eoinfo
->flinfo
->filesym_count
!= 0)
9691 Elf_Internal_Sym fsym
;
9693 memset (&fsym
, 0, sizeof (fsym
));
9694 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9695 fsym
.st_shndx
= SHN_ABS
;
9696 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9697 bfd_und_section_ptr
, NULL
))
9700 eoinfo
->file_sym_done
= TRUE
;
9703 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9704 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9708 eoinfo
->failed
= TRUE
;
9713 else if (h
->indx
== -2)
9719 /* Return TRUE if special handling is done for relocs in SEC against
9720 symbols defined in discarded sections. */
9723 elf_section_ignore_discarded_relocs (asection
*sec
)
9725 const struct elf_backend_data
*bed
;
9727 switch (sec
->sec_info_type
)
9729 case SEC_INFO_TYPE_STABS
:
9730 case SEC_INFO_TYPE_EH_FRAME
:
9731 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9737 bed
= get_elf_backend_data (sec
->owner
);
9738 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9739 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9745 /* Return a mask saying how ld should treat relocations in SEC against
9746 symbols defined in discarded sections. If this function returns
9747 COMPLAIN set, ld will issue a warning message. If this function
9748 returns PRETEND set, and the discarded section was link-once and the
9749 same size as the kept link-once section, ld will pretend that the
9750 symbol was actually defined in the kept section. Otherwise ld will
9751 zero the reloc (at least that is the intent, but some cooperation by
9752 the target dependent code is needed, particularly for REL targets). */
9755 _bfd_elf_default_action_discarded (asection
*sec
)
9757 if (sec
->flags
& SEC_DEBUGGING
)
9760 if (strcmp (".eh_frame", sec
->name
) == 0)
9763 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9766 return COMPLAIN
| PRETEND
;
9769 /* Find a match between a section and a member of a section group. */
9772 match_group_member (asection
*sec
, asection
*group
,
9773 struct bfd_link_info
*info
)
9775 asection
*first
= elf_next_in_group (group
);
9776 asection
*s
= first
;
9780 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9783 s
= elf_next_in_group (s
);
9791 /* Check if the kept section of a discarded section SEC can be used
9792 to replace it. Return the replacement if it is OK. Otherwise return
9796 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9800 kept
= sec
->kept_section
;
9803 if ((kept
->flags
& SEC_GROUP
) != 0)
9804 kept
= match_group_member (sec
, kept
, info
);
9806 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9807 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9809 sec
->kept_section
= kept
;
9814 /* Link an input file into the linker output file. This function
9815 handles all the sections and relocations of the input file at once.
9816 This is so that we only have to read the local symbols once, and
9817 don't have to keep them in memory. */
9820 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9822 int (*relocate_section
)
9823 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9824 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9826 Elf_Internal_Shdr
*symtab_hdr
;
9829 Elf_Internal_Sym
*isymbuf
;
9830 Elf_Internal_Sym
*isym
;
9831 Elf_Internal_Sym
*isymend
;
9833 asection
**ppsection
;
9835 const struct elf_backend_data
*bed
;
9836 struct elf_link_hash_entry
**sym_hashes
;
9837 bfd_size_type address_size
;
9838 bfd_vma r_type_mask
;
9840 bfd_boolean have_file_sym
= FALSE
;
9842 output_bfd
= flinfo
->output_bfd
;
9843 bed
= get_elf_backend_data (output_bfd
);
9844 relocate_section
= bed
->elf_backend_relocate_section
;
9846 /* If this is a dynamic object, we don't want to do anything here:
9847 we don't want the local symbols, and we don't want the section
9849 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9852 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9853 if (elf_bad_symtab (input_bfd
))
9855 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9860 locsymcount
= symtab_hdr
->sh_info
;
9861 extsymoff
= symtab_hdr
->sh_info
;
9864 /* Read the local symbols. */
9865 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9866 if (isymbuf
== NULL
&& locsymcount
!= 0)
9868 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9869 flinfo
->internal_syms
,
9870 flinfo
->external_syms
,
9871 flinfo
->locsym_shndx
);
9872 if (isymbuf
== NULL
)
9876 /* Find local symbol sections and adjust values of symbols in
9877 SEC_MERGE sections. Write out those local symbols we know are
9878 going into the output file. */
9879 isymend
= isymbuf
+ locsymcount
;
9880 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9882 isym
++, pindex
++, ppsection
++)
9886 Elf_Internal_Sym osym
;
9892 if (elf_bad_symtab (input_bfd
))
9894 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9901 if (isym
->st_shndx
== SHN_UNDEF
)
9902 isec
= bfd_und_section_ptr
;
9903 else if (isym
->st_shndx
== SHN_ABS
)
9904 isec
= bfd_abs_section_ptr
;
9905 else if (isym
->st_shndx
== SHN_COMMON
)
9906 isec
= bfd_com_section_ptr
;
9909 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9912 /* Don't attempt to output symbols with st_shnx in the
9913 reserved range other than SHN_ABS and SHN_COMMON. */
9917 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9918 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9920 _bfd_merged_section_offset (output_bfd
, &isec
,
9921 elf_section_data (isec
)->sec_info
,
9927 /* Don't output the first, undefined, symbol. In fact, don't
9928 output any undefined local symbol. */
9929 if (isec
== bfd_und_section_ptr
)
9932 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9934 /* We never output section symbols. Instead, we use the
9935 section symbol of the corresponding section in the output
9940 /* If we are stripping all symbols, we don't want to output this
9942 if (flinfo
->info
->strip
== strip_all
)
9945 /* If we are discarding all local symbols, we don't want to
9946 output this one. If we are generating a relocatable output
9947 file, then some of the local symbols may be required by
9948 relocs; we output them below as we discover that they are
9950 if (flinfo
->info
->discard
== discard_all
)
9953 /* If this symbol is defined in a section which we are
9954 discarding, we don't need to keep it. */
9955 if (isym
->st_shndx
!= SHN_UNDEF
9956 && isym
->st_shndx
< SHN_LORESERVE
9957 && bfd_section_removed_from_list (output_bfd
,
9958 isec
->output_section
))
9961 /* Get the name of the symbol. */
9962 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9967 /* See if we are discarding symbols with this name. */
9968 if ((flinfo
->info
->strip
== strip_some
9969 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9971 || (((flinfo
->info
->discard
== discard_sec_merge
9972 && (isec
->flags
& SEC_MERGE
)
9973 && !bfd_link_relocatable (flinfo
->info
))
9974 || flinfo
->info
->discard
== discard_l
)
9975 && bfd_is_local_label_name (input_bfd
, name
)))
9978 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
9980 if (input_bfd
->lto_output
)
9981 /* -flto puts a temp file name here. This means builds
9982 are not reproducible. Discard the symbol. */
9984 have_file_sym
= TRUE
;
9985 flinfo
->filesym_count
+= 1;
9989 /* In the absence of debug info, bfd_find_nearest_line uses
9990 FILE symbols to determine the source file for local
9991 function symbols. Provide a FILE symbol here if input
9992 files lack such, so that their symbols won't be
9993 associated with a previous input file. It's not the
9994 source file, but the best we can do. */
9995 have_file_sym
= TRUE
;
9996 flinfo
->filesym_count
+= 1;
9997 memset (&osym
, 0, sizeof (osym
));
9998 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9999 osym
.st_shndx
= SHN_ABS
;
10000 if (!elf_link_output_symstrtab (flinfo
,
10001 (input_bfd
->lto_output
? NULL
10002 : input_bfd
->filename
),
10003 &osym
, bfd_abs_section_ptr
,
10010 /* Adjust the section index for the output file. */
10011 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10012 isec
->output_section
);
10013 if (osym
.st_shndx
== SHN_BAD
)
10016 /* ELF symbols in relocatable files are section relative, but
10017 in executable files they are virtual addresses. Note that
10018 this code assumes that all ELF sections have an associated
10019 BFD section with a reasonable value for output_offset; below
10020 we assume that they also have a reasonable value for
10021 output_section. Any special sections must be set up to meet
10022 these requirements. */
10023 osym
.st_value
+= isec
->output_offset
;
10024 if (!bfd_link_relocatable (flinfo
->info
))
10026 osym
.st_value
+= isec
->output_section
->vma
;
10027 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10029 /* STT_TLS symbols are relative to PT_TLS segment base. */
10030 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10031 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10035 indx
= bfd_get_symcount (output_bfd
);
10036 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10043 if (bed
->s
->arch_size
== 32)
10045 r_type_mask
= 0xff;
10051 r_type_mask
= 0xffffffff;
10056 /* Relocate the contents of each section. */
10057 sym_hashes
= elf_sym_hashes (input_bfd
);
10058 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10060 bfd_byte
*contents
;
10062 if (! o
->linker_mark
)
10064 /* This section was omitted from the link. */
10068 if (bfd_link_relocatable (flinfo
->info
)
10069 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10071 /* Deal with the group signature symbol. */
10072 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10073 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10074 asection
*osec
= o
->output_section
;
10076 if (symndx
>= locsymcount
10077 || (elf_bad_symtab (input_bfd
)
10078 && flinfo
->sections
[symndx
] == NULL
))
10080 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10081 while (h
->root
.type
== bfd_link_hash_indirect
10082 || h
->root
.type
== bfd_link_hash_warning
)
10083 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10084 /* Arrange for symbol to be output. */
10086 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10088 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10090 /* We'll use the output section target_index. */
10091 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10092 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10096 if (flinfo
->indices
[symndx
] == -1)
10098 /* Otherwise output the local symbol now. */
10099 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10100 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10105 name
= bfd_elf_string_from_elf_section (input_bfd
,
10106 symtab_hdr
->sh_link
,
10111 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10113 if (sym
.st_shndx
== SHN_BAD
)
10116 sym
.st_value
+= o
->output_offset
;
10118 indx
= bfd_get_symcount (output_bfd
);
10119 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10124 flinfo
->indices
[symndx
] = indx
;
10128 elf_section_data (osec
)->this_hdr
.sh_info
10129 = flinfo
->indices
[symndx
];
10133 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10134 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10137 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10139 /* Section was created by _bfd_elf_link_create_dynamic_sections
10144 /* Get the contents of the section. They have been cached by a
10145 relaxation routine. Note that o is a section in an input
10146 file, so the contents field will not have been set by any of
10147 the routines which work on output files. */
10148 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10150 contents
= elf_section_data (o
)->this_hdr
.contents
;
10151 if (bed
->caches_rawsize
10153 && o
->rawsize
< o
->size
)
10155 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10156 contents
= flinfo
->contents
;
10161 contents
= flinfo
->contents
;
10162 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10166 if ((o
->flags
& SEC_RELOC
) != 0)
10168 Elf_Internal_Rela
*internal_relocs
;
10169 Elf_Internal_Rela
*rel
, *relend
;
10170 int action_discarded
;
10173 /* Get the swapped relocs. */
10175 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10176 flinfo
->internal_relocs
, FALSE
);
10177 if (internal_relocs
== NULL
10178 && o
->reloc_count
> 0)
10181 /* We need to reverse-copy input .ctors/.dtors sections if
10182 they are placed in .init_array/.finit_array for output. */
10183 if (o
->size
> address_size
10184 && ((strncmp (o
->name
, ".ctors", 6) == 0
10185 && strcmp (o
->output_section
->name
,
10186 ".init_array") == 0)
10187 || (strncmp (o
->name
, ".dtors", 6) == 0
10188 && strcmp (o
->output_section
->name
,
10189 ".fini_array") == 0))
10190 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10192 if (o
->size
!= o
->reloc_count
* address_size
)
10194 (*_bfd_error_handler
)
10195 (_("error: %B: size of section %A is not "
10196 "multiple of address size"),
10198 bfd_set_error (bfd_error_on_input
);
10201 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10204 action_discarded
= -1;
10205 if (!elf_section_ignore_discarded_relocs (o
))
10206 action_discarded
= (*bed
->action_discarded
) (o
);
10208 /* Run through the relocs evaluating complex reloc symbols and
10209 looking for relocs against symbols from discarded sections
10210 or section symbols from removed link-once sections.
10211 Complain about relocs against discarded sections. Zero
10212 relocs against removed link-once sections. */
10214 rel
= internal_relocs
;
10215 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10216 for ( ; rel
< relend
; rel
++)
10218 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10219 unsigned int s_type
;
10220 asection
**ps
, *sec
;
10221 struct elf_link_hash_entry
*h
= NULL
;
10222 const char *sym_name
;
10224 if (r_symndx
== STN_UNDEF
)
10227 if (r_symndx
>= locsymcount
10228 || (elf_bad_symtab (input_bfd
)
10229 && flinfo
->sections
[r_symndx
] == NULL
))
10231 h
= sym_hashes
[r_symndx
- extsymoff
];
10233 /* Badly formatted input files can contain relocs that
10234 reference non-existant symbols. Check here so that
10235 we do not seg fault. */
10240 sprintf_vma (buffer
, rel
->r_info
);
10241 (*_bfd_error_handler
)
10242 (_("error: %B contains a reloc (0x%s) for section %A "
10243 "that references a non-existent global symbol"),
10244 input_bfd
, o
, buffer
);
10245 bfd_set_error (bfd_error_bad_value
);
10249 while (h
->root
.type
== bfd_link_hash_indirect
10250 || h
->root
.type
== bfd_link_hash_warning
)
10251 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10255 /* If a plugin symbol is referenced from a non-IR file,
10256 mark the symbol as undefined. Note that the
10257 linker may attach linker created dynamic sections
10258 to the plugin bfd. Symbols defined in linker
10259 created sections are not plugin symbols. */
10260 if (h
->root
.non_ir_ref
10261 && (h
->root
.type
== bfd_link_hash_defined
10262 || h
->root
.type
== bfd_link_hash_defweak
)
10263 && (h
->root
.u
.def
.section
->flags
10264 & SEC_LINKER_CREATED
) == 0
10265 && h
->root
.u
.def
.section
->owner
!= NULL
10266 && (h
->root
.u
.def
.section
->owner
->flags
10267 & BFD_PLUGIN
) != 0)
10269 h
->root
.type
= bfd_link_hash_undefined
;
10270 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10274 if (h
->root
.type
== bfd_link_hash_defined
10275 || h
->root
.type
== bfd_link_hash_defweak
)
10276 ps
= &h
->root
.u
.def
.section
;
10278 sym_name
= h
->root
.root
.string
;
10282 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10284 s_type
= ELF_ST_TYPE (sym
->st_info
);
10285 ps
= &flinfo
->sections
[r_symndx
];
10286 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10290 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10291 && !bfd_link_relocatable (flinfo
->info
))
10294 bfd_vma dot
= (rel
->r_offset
10295 + o
->output_offset
+ o
->output_section
->vma
);
10297 printf ("Encountered a complex symbol!");
10298 printf (" (input_bfd %s, section %s, reloc %ld\n",
10299 input_bfd
->filename
, o
->name
,
10300 (long) (rel
- internal_relocs
));
10301 printf (" symbol: idx %8.8lx, name %s\n",
10302 r_symndx
, sym_name
);
10303 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10304 (unsigned long) rel
->r_info
,
10305 (unsigned long) rel
->r_offset
);
10307 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10308 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10311 /* Symbol evaluated OK. Update to absolute value. */
10312 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10317 if (action_discarded
!= -1 && ps
!= NULL
)
10319 /* Complain if the definition comes from a
10320 discarded section. */
10321 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10323 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10324 if (action_discarded
& COMPLAIN
)
10325 (*flinfo
->info
->callbacks
->einfo
)
10326 (_("%X`%s' referenced in section `%A' of %B: "
10327 "defined in discarded section `%A' of %B\n"),
10328 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10330 /* Try to do the best we can to support buggy old
10331 versions of gcc. Pretend that the symbol is
10332 really defined in the kept linkonce section.
10333 FIXME: This is quite broken. Modifying the
10334 symbol here means we will be changing all later
10335 uses of the symbol, not just in this section. */
10336 if (action_discarded
& PRETEND
)
10340 kept
= _bfd_elf_check_kept_section (sec
,
10352 /* Relocate the section by invoking a back end routine.
10354 The back end routine is responsible for adjusting the
10355 section contents as necessary, and (if using Rela relocs
10356 and generating a relocatable output file) adjusting the
10357 reloc addend as necessary.
10359 The back end routine does not have to worry about setting
10360 the reloc address or the reloc symbol index.
10362 The back end routine is given a pointer to the swapped in
10363 internal symbols, and can access the hash table entries
10364 for the external symbols via elf_sym_hashes (input_bfd).
10366 When generating relocatable output, the back end routine
10367 must handle STB_LOCAL/STT_SECTION symbols specially. The
10368 output symbol is going to be a section symbol
10369 corresponding to the output section, which will require
10370 the addend to be adjusted. */
10372 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10373 input_bfd
, o
, contents
,
10381 || bfd_link_relocatable (flinfo
->info
)
10382 || flinfo
->info
->emitrelocations
)
10384 Elf_Internal_Rela
*irela
;
10385 Elf_Internal_Rela
*irelaend
, *irelamid
;
10386 bfd_vma last_offset
;
10387 struct elf_link_hash_entry
**rel_hash
;
10388 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10389 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10390 unsigned int next_erel
;
10391 bfd_boolean rela_normal
;
10392 struct bfd_elf_section_data
*esdi
, *esdo
;
10394 esdi
= elf_section_data (o
);
10395 esdo
= elf_section_data (o
->output_section
);
10396 rela_normal
= FALSE
;
10398 /* Adjust the reloc addresses and symbol indices. */
10400 irela
= internal_relocs
;
10401 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10402 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10403 /* We start processing the REL relocs, if any. When we reach
10404 IRELAMID in the loop, we switch to the RELA relocs. */
10406 if (esdi
->rel
.hdr
!= NULL
)
10407 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10408 * bed
->s
->int_rels_per_ext_rel
);
10409 rel_hash_list
= rel_hash
;
10410 rela_hash_list
= NULL
;
10411 last_offset
= o
->output_offset
;
10412 if (!bfd_link_relocatable (flinfo
->info
))
10413 last_offset
+= o
->output_section
->vma
;
10414 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10416 unsigned long r_symndx
;
10418 Elf_Internal_Sym sym
;
10420 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10426 if (irela
== irelamid
)
10428 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10429 rela_hash_list
= rel_hash
;
10430 rela_normal
= bed
->rela_normal
;
10433 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10436 if (irela
->r_offset
>= (bfd_vma
) -2)
10438 /* This is a reloc for a deleted entry or somesuch.
10439 Turn it into an R_*_NONE reloc, at the same
10440 offset as the last reloc. elf_eh_frame.c and
10441 bfd_elf_discard_info rely on reloc offsets
10443 irela
->r_offset
= last_offset
;
10445 irela
->r_addend
= 0;
10449 irela
->r_offset
+= o
->output_offset
;
10451 /* Relocs in an executable have to be virtual addresses. */
10452 if (!bfd_link_relocatable (flinfo
->info
))
10453 irela
->r_offset
+= o
->output_section
->vma
;
10455 last_offset
= irela
->r_offset
;
10457 r_symndx
= irela
->r_info
>> r_sym_shift
;
10458 if (r_symndx
== STN_UNDEF
)
10461 if (r_symndx
>= locsymcount
10462 || (elf_bad_symtab (input_bfd
)
10463 && flinfo
->sections
[r_symndx
] == NULL
))
10465 struct elf_link_hash_entry
*rh
;
10466 unsigned long indx
;
10468 /* This is a reloc against a global symbol. We
10469 have not yet output all the local symbols, so
10470 we do not know the symbol index of any global
10471 symbol. We set the rel_hash entry for this
10472 reloc to point to the global hash table entry
10473 for this symbol. The symbol index is then
10474 set at the end of bfd_elf_final_link. */
10475 indx
= r_symndx
- extsymoff
;
10476 rh
= elf_sym_hashes (input_bfd
)[indx
];
10477 while (rh
->root
.type
== bfd_link_hash_indirect
10478 || rh
->root
.type
== bfd_link_hash_warning
)
10479 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10481 /* Setting the index to -2 tells
10482 elf_link_output_extsym that this symbol is
10483 used by a reloc. */
10484 BFD_ASSERT (rh
->indx
< 0);
10492 /* This is a reloc against a local symbol. */
10495 sym
= isymbuf
[r_symndx
];
10496 sec
= flinfo
->sections
[r_symndx
];
10497 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10499 /* I suppose the backend ought to fill in the
10500 section of any STT_SECTION symbol against a
10501 processor specific section. */
10502 r_symndx
= STN_UNDEF
;
10503 if (bfd_is_abs_section (sec
))
10505 else if (sec
== NULL
|| sec
->owner
== NULL
)
10507 bfd_set_error (bfd_error_bad_value
);
10512 asection
*osec
= sec
->output_section
;
10514 /* If we have discarded a section, the output
10515 section will be the absolute section. In
10516 case of discarded SEC_MERGE sections, use
10517 the kept section. relocate_section should
10518 have already handled discarded linkonce
10520 if (bfd_is_abs_section (osec
)
10521 && sec
->kept_section
!= NULL
10522 && sec
->kept_section
->output_section
!= NULL
)
10524 osec
= sec
->kept_section
->output_section
;
10525 irela
->r_addend
-= osec
->vma
;
10528 if (!bfd_is_abs_section (osec
))
10530 r_symndx
= osec
->target_index
;
10531 if (r_symndx
== STN_UNDEF
)
10533 irela
->r_addend
+= osec
->vma
;
10534 osec
= _bfd_nearby_section (output_bfd
, osec
,
10536 irela
->r_addend
-= osec
->vma
;
10537 r_symndx
= osec
->target_index
;
10542 /* Adjust the addend according to where the
10543 section winds up in the output section. */
10545 irela
->r_addend
+= sec
->output_offset
;
10549 if (flinfo
->indices
[r_symndx
] == -1)
10551 unsigned long shlink
;
10556 if (flinfo
->info
->strip
== strip_all
)
10558 /* You can't do ld -r -s. */
10559 bfd_set_error (bfd_error_invalid_operation
);
10563 /* This symbol was skipped earlier, but
10564 since it is needed by a reloc, we
10565 must output it now. */
10566 shlink
= symtab_hdr
->sh_link
;
10567 name
= (bfd_elf_string_from_elf_section
10568 (input_bfd
, shlink
, sym
.st_name
));
10572 osec
= sec
->output_section
;
10574 _bfd_elf_section_from_bfd_section (output_bfd
,
10576 if (sym
.st_shndx
== SHN_BAD
)
10579 sym
.st_value
+= sec
->output_offset
;
10580 if (!bfd_link_relocatable (flinfo
->info
))
10582 sym
.st_value
+= osec
->vma
;
10583 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10585 /* STT_TLS symbols are relative to PT_TLS
10587 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10588 ->tls_sec
!= NULL
);
10589 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10594 indx
= bfd_get_symcount (output_bfd
);
10595 ret
= elf_link_output_symstrtab (flinfo
, name
,
10601 flinfo
->indices
[r_symndx
] = indx
;
10606 r_symndx
= flinfo
->indices
[r_symndx
];
10609 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10610 | (irela
->r_info
& r_type_mask
));
10613 /* Swap out the relocs. */
10614 input_rel_hdr
= esdi
->rel
.hdr
;
10615 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10617 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10622 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10623 * bed
->s
->int_rels_per_ext_rel
);
10624 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10627 input_rela_hdr
= esdi
->rela
.hdr
;
10628 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10630 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10639 /* Write out the modified section contents. */
10640 if (bed
->elf_backend_write_section
10641 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10644 /* Section written out. */
10646 else switch (o
->sec_info_type
)
10648 case SEC_INFO_TYPE_STABS
:
10649 if (! (_bfd_write_section_stabs
10651 &elf_hash_table (flinfo
->info
)->stab_info
,
10652 o
, &elf_section_data (o
)->sec_info
, contents
)))
10655 case SEC_INFO_TYPE_MERGE
:
10656 if (! _bfd_write_merged_section (output_bfd
, o
,
10657 elf_section_data (o
)->sec_info
))
10660 case SEC_INFO_TYPE_EH_FRAME
:
10662 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10667 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10669 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10677 if (! (o
->flags
& SEC_EXCLUDE
))
10679 file_ptr offset
= (file_ptr
) o
->output_offset
;
10680 bfd_size_type todo
= o
->size
;
10682 offset
*= bfd_octets_per_byte (output_bfd
);
10684 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10686 /* Reverse-copy input section to output. */
10689 todo
-= address_size
;
10690 if (! bfd_set_section_contents (output_bfd
,
10698 offset
+= address_size
;
10702 else if (! bfd_set_section_contents (output_bfd
,
10716 /* Generate a reloc when linking an ELF file. This is a reloc
10717 requested by the linker, and does not come from any input file. This
10718 is used to build constructor and destructor tables when linking
10722 elf_reloc_link_order (bfd
*output_bfd
,
10723 struct bfd_link_info
*info
,
10724 asection
*output_section
,
10725 struct bfd_link_order
*link_order
)
10727 reloc_howto_type
*howto
;
10731 struct bfd_elf_section_reloc_data
*reldata
;
10732 struct elf_link_hash_entry
**rel_hash_ptr
;
10733 Elf_Internal_Shdr
*rel_hdr
;
10734 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10735 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10738 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10740 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10743 bfd_set_error (bfd_error_bad_value
);
10747 addend
= link_order
->u
.reloc
.p
->addend
;
10750 reldata
= &esdo
->rel
;
10751 else if (esdo
->rela
.hdr
)
10752 reldata
= &esdo
->rela
;
10759 /* Figure out the symbol index. */
10760 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10761 if (link_order
->type
== bfd_section_reloc_link_order
)
10763 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10764 BFD_ASSERT (indx
!= 0);
10765 *rel_hash_ptr
= NULL
;
10769 struct elf_link_hash_entry
*h
;
10771 /* Treat a reloc against a defined symbol as though it were
10772 actually against the section. */
10773 h
= ((struct elf_link_hash_entry
*)
10774 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10775 link_order
->u
.reloc
.p
->u
.name
,
10776 FALSE
, FALSE
, TRUE
));
10778 && (h
->root
.type
== bfd_link_hash_defined
10779 || h
->root
.type
== bfd_link_hash_defweak
))
10783 section
= h
->root
.u
.def
.section
;
10784 indx
= section
->output_section
->target_index
;
10785 *rel_hash_ptr
= NULL
;
10786 /* It seems that we ought to add the symbol value to the
10787 addend here, but in practice it has already been added
10788 because it was passed to constructor_callback. */
10789 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10791 else if (h
!= NULL
)
10793 /* Setting the index to -2 tells elf_link_output_extsym that
10794 this symbol is used by a reloc. */
10801 (*info
->callbacks
->unattached_reloc
)
10802 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10807 /* If this is an inplace reloc, we must write the addend into the
10809 if (howto
->partial_inplace
&& addend
!= 0)
10811 bfd_size_type size
;
10812 bfd_reloc_status_type rstat
;
10815 const char *sym_name
;
10817 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10818 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10819 if (buf
== NULL
&& size
!= 0)
10821 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10828 case bfd_reloc_outofrange
:
10831 case bfd_reloc_overflow
:
10832 if (link_order
->type
== bfd_section_reloc_link_order
)
10833 sym_name
= bfd_section_name (output_bfd
,
10834 link_order
->u
.reloc
.p
->u
.section
);
10836 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10837 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10838 howto
->name
, addend
, NULL
, NULL
,
10843 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10845 * bfd_octets_per_byte (output_bfd
),
10852 /* The address of a reloc is relative to the section in a
10853 relocatable file, and is a virtual address in an executable
10855 offset
= link_order
->offset
;
10856 if (! bfd_link_relocatable (info
))
10857 offset
+= output_section
->vma
;
10859 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10861 irel
[i
].r_offset
= offset
;
10862 irel
[i
].r_info
= 0;
10863 irel
[i
].r_addend
= 0;
10865 if (bed
->s
->arch_size
== 32)
10866 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10868 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10870 rel_hdr
= reldata
->hdr
;
10871 erel
= rel_hdr
->contents
;
10872 if (rel_hdr
->sh_type
== SHT_REL
)
10874 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10875 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10879 irel
[0].r_addend
= addend
;
10880 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10881 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10890 /* Get the output vma of the section pointed to by the sh_link field. */
10893 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10895 Elf_Internal_Shdr
**elf_shdrp
;
10899 s
= p
->u
.indirect
.section
;
10900 elf_shdrp
= elf_elfsections (s
->owner
);
10901 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10902 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10904 The Intel C compiler generates SHT_IA_64_UNWIND with
10905 SHF_LINK_ORDER. But it doesn't set the sh_link or
10906 sh_info fields. Hence we could get the situation
10907 where elfsec is 0. */
10910 const struct elf_backend_data
*bed
10911 = get_elf_backend_data (s
->owner
);
10912 if (bed
->link_order_error_handler
)
10913 bed
->link_order_error_handler
10914 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10919 s
= elf_shdrp
[elfsec
]->bfd_section
;
10920 return s
->output_section
->vma
+ s
->output_offset
;
10925 /* Compare two sections based on the locations of the sections they are
10926 linked to. Used by elf_fixup_link_order. */
10929 compare_link_order (const void * a
, const void * b
)
10934 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10935 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10938 return apos
> bpos
;
10942 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10943 order as their linked sections. Returns false if this could not be done
10944 because an output section includes both ordered and unordered
10945 sections. Ideally we'd do this in the linker proper. */
10948 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10950 int seen_linkorder
;
10953 struct bfd_link_order
*p
;
10955 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10957 struct bfd_link_order
**sections
;
10958 asection
*s
, *other_sec
, *linkorder_sec
;
10962 linkorder_sec
= NULL
;
10964 seen_linkorder
= 0;
10965 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10967 if (p
->type
== bfd_indirect_link_order
)
10969 s
= p
->u
.indirect
.section
;
10971 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10972 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
10973 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
10974 && elfsec
< elf_numsections (sub
)
10975 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
10976 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
10990 if (seen_other
&& seen_linkorder
)
10992 if (other_sec
&& linkorder_sec
)
10993 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10995 linkorder_sec
->owner
, other_sec
,
10998 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
11000 bfd_set_error (bfd_error_bad_value
);
11005 if (!seen_linkorder
)
11008 sections
= (struct bfd_link_order
**)
11009 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11010 if (sections
== NULL
)
11012 seen_linkorder
= 0;
11014 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11016 sections
[seen_linkorder
++] = p
;
11018 /* Sort the input sections in the order of their linked section. */
11019 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11020 compare_link_order
);
11022 /* Change the offsets of the sections. */
11024 for (n
= 0; n
< seen_linkorder
; n
++)
11026 s
= sections
[n
]->u
.indirect
.section
;
11027 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11028 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11029 sections
[n
]->offset
= offset
;
11030 offset
+= sections
[n
]->size
;
11037 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11038 Returns TRUE upon success, FALSE otherwise. */
11041 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11043 bfd_boolean ret
= FALSE
;
11045 const struct elf_backend_data
*bed
;
11047 enum bfd_architecture arch
;
11049 asymbol
**sympp
= NULL
;
11053 elf_symbol_type
*osymbuf
;
11055 implib_bfd
= info
->out_implib_bfd
;
11056 bed
= get_elf_backend_data (abfd
);
11058 if (!bfd_set_format (implib_bfd
, bfd_object
))
11061 flags
= bfd_get_file_flags (abfd
);
11062 flags
&= ~HAS_RELOC
;
11063 if (!bfd_set_start_address (implib_bfd
, 0)
11064 || !bfd_set_file_flags (implib_bfd
, flags
))
11067 /* Copy architecture of output file to import library file. */
11068 arch
= bfd_get_arch (abfd
);
11069 mach
= bfd_get_mach (abfd
);
11070 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11071 && (abfd
->target_defaulted
11072 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11075 /* Get symbol table size. */
11076 symsize
= bfd_get_symtab_upper_bound (abfd
);
11080 /* Read in the symbol table. */
11081 sympp
= (asymbol
**) xmalloc (symsize
);
11082 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11086 /* Allow the BFD backend to copy any private header data it
11087 understands from the output BFD to the import library BFD. */
11088 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11091 /* Filter symbols to appear in the import library. */
11092 if (bed
->elf_backend_filter_implib_symbols
)
11093 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11096 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11099 bfd_set_error (bfd_error_no_symbols
);
11100 (*_bfd_error_handler
) (_("%B: no symbol found for import library"),
11106 /* Make symbols absolute. */
11107 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11108 sizeof (*osymbuf
));
11109 for (src_count
= 0; src_count
< symcount
; src_count
++)
11111 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11112 sizeof (*osymbuf
));
11113 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11114 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11115 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11116 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11117 osymbuf
[src_count
].symbol
.value
;
11118 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11121 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11123 /* Allow the BFD backend to copy any private data it understands
11124 from the output BFD to the import library BFD. This is done last
11125 to permit the routine to look at the filtered symbol table. */
11126 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11129 if (!bfd_close (implib_bfd
))
11140 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11144 if (flinfo
->symstrtab
!= NULL
)
11145 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11146 if (flinfo
->contents
!= NULL
)
11147 free (flinfo
->contents
);
11148 if (flinfo
->external_relocs
!= NULL
)
11149 free (flinfo
->external_relocs
);
11150 if (flinfo
->internal_relocs
!= NULL
)
11151 free (flinfo
->internal_relocs
);
11152 if (flinfo
->external_syms
!= NULL
)
11153 free (flinfo
->external_syms
);
11154 if (flinfo
->locsym_shndx
!= NULL
)
11155 free (flinfo
->locsym_shndx
);
11156 if (flinfo
->internal_syms
!= NULL
)
11157 free (flinfo
->internal_syms
);
11158 if (flinfo
->indices
!= NULL
)
11159 free (flinfo
->indices
);
11160 if (flinfo
->sections
!= NULL
)
11161 free (flinfo
->sections
);
11162 if (flinfo
->symshndxbuf
!= NULL
)
11163 free (flinfo
->symshndxbuf
);
11164 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11166 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11167 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11168 free (esdo
->rel
.hashes
);
11169 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11170 free (esdo
->rela
.hashes
);
11174 /* Do the final step of an ELF link. */
11177 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11179 bfd_boolean dynamic
;
11180 bfd_boolean emit_relocs
;
11182 struct elf_final_link_info flinfo
;
11184 struct bfd_link_order
*p
;
11186 bfd_size_type max_contents_size
;
11187 bfd_size_type max_external_reloc_size
;
11188 bfd_size_type max_internal_reloc_count
;
11189 bfd_size_type max_sym_count
;
11190 bfd_size_type max_sym_shndx_count
;
11191 Elf_Internal_Sym elfsym
;
11193 Elf_Internal_Shdr
*symtab_hdr
;
11194 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11195 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11196 struct elf_outext_info eoinfo
;
11197 bfd_boolean merged
;
11198 size_t relativecount
= 0;
11199 asection
*reldyn
= 0;
11201 asection
*attr_section
= NULL
;
11202 bfd_vma attr_size
= 0;
11203 const char *std_attrs_section
;
11205 if (! is_elf_hash_table (info
->hash
))
11208 if (bfd_link_pic (info
))
11209 abfd
->flags
|= DYNAMIC
;
11211 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
11212 dynobj
= elf_hash_table (info
)->dynobj
;
11214 emit_relocs
= (bfd_link_relocatable (info
)
11215 || info
->emitrelocations
);
11217 flinfo
.info
= info
;
11218 flinfo
.output_bfd
= abfd
;
11219 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11220 if (flinfo
.symstrtab
== NULL
)
11225 flinfo
.hash_sec
= NULL
;
11226 flinfo
.symver_sec
= NULL
;
11230 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11231 /* Note that dynsym_sec can be NULL (on VMS). */
11232 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11233 /* Note that it is OK if symver_sec is NULL. */
11236 flinfo
.contents
= NULL
;
11237 flinfo
.external_relocs
= NULL
;
11238 flinfo
.internal_relocs
= NULL
;
11239 flinfo
.external_syms
= NULL
;
11240 flinfo
.locsym_shndx
= NULL
;
11241 flinfo
.internal_syms
= NULL
;
11242 flinfo
.indices
= NULL
;
11243 flinfo
.sections
= NULL
;
11244 flinfo
.symshndxbuf
= NULL
;
11245 flinfo
.filesym_count
= 0;
11247 /* The object attributes have been merged. Remove the input
11248 sections from the link, and set the contents of the output
11250 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11251 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11253 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11254 || strcmp (o
->name
, ".gnu.attributes") == 0)
11256 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11258 asection
*input_section
;
11260 if (p
->type
!= bfd_indirect_link_order
)
11262 input_section
= p
->u
.indirect
.section
;
11263 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11264 elf_link_input_bfd ignores this section. */
11265 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11268 attr_size
= bfd_elf_obj_attr_size (abfd
);
11271 bfd_set_section_size (abfd
, o
, attr_size
);
11273 /* Skip this section later on. */
11274 o
->map_head
.link_order
= NULL
;
11277 o
->flags
|= SEC_EXCLUDE
;
11281 /* Count up the number of relocations we will output for each output
11282 section, so that we know the sizes of the reloc sections. We
11283 also figure out some maximum sizes. */
11284 max_contents_size
= 0;
11285 max_external_reloc_size
= 0;
11286 max_internal_reloc_count
= 0;
11288 max_sym_shndx_count
= 0;
11290 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11292 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11293 o
->reloc_count
= 0;
11295 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11297 unsigned int reloc_count
= 0;
11298 unsigned int additional_reloc_count
= 0;
11299 struct bfd_elf_section_data
*esdi
= NULL
;
11301 if (p
->type
== bfd_section_reloc_link_order
11302 || p
->type
== bfd_symbol_reloc_link_order
)
11304 else if (p
->type
== bfd_indirect_link_order
)
11308 sec
= p
->u
.indirect
.section
;
11309 esdi
= elf_section_data (sec
);
11311 /* Mark all sections which are to be included in the
11312 link. This will normally be every section. We need
11313 to do this so that we can identify any sections which
11314 the linker has decided to not include. */
11315 sec
->linker_mark
= TRUE
;
11317 if (sec
->flags
& SEC_MERGE
)
11320 if (esdo
->this_hdr
.sh_type
== SHT_REL
11321 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11322 /* Some backends use reloc_count in relocation sections
11323 to count particular types of relocs. Of course,
11324 reloc sections themselves can't have relocations. */
11326 else if (emit_relocs
)
11328 reloc_count
= sec
->reloc_count
;
11329 if (bed
->elf_backend_count_additional_relocs
)
11332 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11333 additional_reloc_count
+= c
;
11336 else if (bed
->elf_backend_count_relocs
)
11337 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11339 if (sec
->rawsize
> max_contents_size
)
11340 max_contents_size
= sec
->rawsize
;
11341 if (sec
->size
> max_contents_size
)
11342 max_contents_size
= sec
->size
;
11344 /* We are interested in just local symbols, not all
11346 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11347 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11351 if (elf_bad_symtab (sec
->owner
))
11352 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11353 / bed
->s
->sizeof_sym
);
11355 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11357 if (sym_count
> max_sym_count
)
11358 max_sym_count
= sym_count
;
11360 if (sym_count
> max_sym_shndx_count
11361 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11362 max_sym_shndx_count
= sym_count
;
11364 if ((sec
->flags
& SEC_RELOC
) != 0)
11366 size_t ext_size
= 0;
11368 if (esdi
->rel
.hdr
!= NULL
)
11369 ext_size
= esdi
->rel
.hdr
->sh_size
;
11370 if (esdi
->rela
.hdr
!= NULL
)
11371 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11373 if (ext_size
> max_external_reloc_size
)
11374 max_external_reloc_size
= ext_size
;
11375 if (sec
->reloc_count
> max_internal_reloc_count
)
11376 max_internal_reloc_count
= sec
->reloc_count
;
11381 if (reloc_count
== 0)
11384 reloc_count
+= additional_reloc_count
;
11385 o
->reloc_count
+= reloc_count
;
11387 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11391 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11392 esdo
->rel
.count
+= additional_reloc_count
;
11394 if (esdi
->rela
.hdr
)
11396 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11397 esdo
->rela
.count
+= additional_reloc_count
;
11403 esdo
->rela
.count
+= reloc_count
;
11405 esdo
->rel
.count
+= reloc_count
;
11409 if (o
->reloc_count
> 0)
11410 o
->flags
|= SEC_RELOC
;
11413 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11414 set it (this is probably a bug) and if it is set
11415 assign_section_numbers will create a reloc section. */
11416 o
->flags
&=~ SEC_RELOC
;
11419 /* If the SEC_ALLOC flag is not set, force the section VMA to
11420 zero. This is done in elf_fake_sections as well, but forcing
11421 the VMA to 0 here will ensure that relocs against these
11422 sections are handled correctly. */
11423 if ((o
->flags
& SEC_ALLOC
) == 0
11424 && ! o
->user_set_vma
)
11428 if (! bfd_link_relocatable (info
) && merged
)
11429 elf_link_hash_traverse (elf_hash_table (info
),
11430 _bfd_elf_link_sec_merge_syms
, abfd
);
11432 /* Figure out the file positions for everything but the symbol table
11433 and the relocs. We set symcount to force assign_section_numbers
11434 to create a symbol table. */
11435 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11436 BFD_ASSERT (! abfd
->output_has_begun
);
11437 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11440 /* Set sizes, and assign file positions for reloc sections. */
11441 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11443 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11444 if ((o
->flags
& SEC_RELOC
) != 0)
11447 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11451 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11455 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11456 to count upwards while actually outputting the relocations. */
11457 esdo
->rel
.count
= 0;
11458 esdo
->rela
.count
= 0;
11460 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11462 /* Cache the section contents so that they can be compressed
11463 later. Use bfd_malloc since it will be freed by
11464 bfd_compress_section_contents. */
11465 unsigned char *contents
= esdo
->this_hdr
.contents
;
11466 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11469 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11470 if (contents
== NULL
)
11472 esdo
->this_hdr
.contents
= contents
;
11476 /* We have now assigned file positions for all the sections except
11477 .symtab, .strtab, and non-loaded reloc sections. We start the
11478 .symtab section at the current file position, and write directly
11479 to it. We build the .strtab section in memory. */
11480 bfd_get_symcount (abfd
) = 0;
11481 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11482 /* sh_name is set in prep_headers. */
11483 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11484 /* sh_flags, sh_addr and sh_size all start off zero. */
11485 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11486 /* sh_link is set in assign_section_numbers. */
11487 /* sh_info is set below. */
11488 /* sh_offset is set just below. */
11489 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11491 if (max_sym_count
< 20)
11492 max_sym_count
= 20;
11493 elf_hash_table (info
)->strtabsize
= max_sym_count
;
11494 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11495 elf_hash_table (info
)->strtab
11496 = (struct elf_sym_strtab
*) bfd_malloc (amt
);
11497 if (elf_hash_table (info
)->strtab
== NULL
)
11499 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11501 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11502 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11504 if (info
->strip
!= strip_all
|| emit_relocs
)
11506 file_ptr off
= elf_next_file_pos (abfd
);
11508 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11510 /* Note that at this point elf_next_file_pos (abfd) is
11511 incorrect. We do not yet know the size of the .symtab section.
11512 We correct next_file_pos below, after we do know the size. */
11514 /* Start writing out the symbol table. The first symbol is always a
11516 elfsym
.st_value
= 0;
11517 elfsym
.st_size
= 0;
11518 elfsym
.st_info
= 0;
11519 elfsym
.st_other
= 0;
11520 elfsym
.st_shndx
= SHN_UNDEF
;
11521 elfsym
.st_target_internal
= 0;
11522 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11523 bfd_und_section_ptr
, NULL
) != 1)
11526 /* Output a symbol for each section. We output these even if we are
11527 discarding local symbols, since they are used for relocs. These
11528 symbols have no names. We store the index of each one in the
11529 index field of the section, so that we can find it again when
11530 outputting relocs. */
11532 elfsym
.st_size
= 0;
11533 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11534 elfsym
.st_other
= 0;
11535 elfsym
.st_value
= 0;
11536 elfsym
.st_target_internal
= 0;
11537 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11539 o
= bfd_section_from_elf_index (abfd
, i
);
11542 o
->target_index
= bfd_get_symcount (abfd
);
11543 elfsym
.st_shndx
= i
;
11544 if (!bfd_link_relocatable (info
))
11545 elfsym
.st_value
= o
->vma
;
11546 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11553 /* Allocate some memory to hold information read in from the input
11555 if (max_contents_size
!= 0)
11557 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11558 if (flinfo
.contents
== NULL
)
11562 if (max_external_reloc_size
!= 0)
11564 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11565 if (flinfo
.external_relocs
== NULL
)
11569 if (max_internal_reloc_count
!= 0)
11571 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11572 amt
*= sizeof (Elf_Internal_Rela
);
11573 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11574 if (flinfo
.internal_relocs
== NULL
)
11578 if (max_sym_count
!= 0)
11580 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11581 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11582 if (flinfo
.external_syms
== NULL
)
11585 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11586 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11587 if (flinfo
.internal_syms
== NULL
)
11590 amt
= max_sym_count
* sizeof (long);
11591 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11592 if (flinfo
.indices
== NULL
)
11595 amt
= max_sym_count
* sizeof (asection
*);
11596 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11597 if (flinfo
.sections
== NULL
)
11601 if (max_sym_shndx_count
!= 0)
11603 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11604 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11605 if (flinfo
.locsym_shndx
== NULL
)
11609 if (elf_hash_table (info
)->tls_sec
)
11611 bfd_vma base
, end
= 0;
11614 for (sec
= elf_hash_table (info
)->tls_sec
;
11615 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11618 bfd_size_type size
= sec
->size
;
11621 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11623 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11626 size
= ord
->offset
+ ord
->size
;
11628 end
= sec
->vma
+ size
;
11630 base
= elf_hash_table (info
)->tls_sec
->vma
;
11631 /* Only align end of TLS section if static TLS doesn't have special
11632 alignment requirements. */
11633 if (bed
->static_tls_alignment
== 1)
11634 end
= align_power (end
,
11635 elf_hash_table (info
)->tls_sec
->alignment_power
);
11636 elf_hash_table (info
)->tls_size
= end
- base
;
11639 /* Reorder SHF_LINK_ORDER sections. */
11640 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11642 if (!elf_fixup_link_order (abfd
, o
))
11646 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11649 /* Since ELF permits relocations to be against local symbols, we
11650 must have the local symbols available when we do the relocations.
11651 Since we would rather only read the local symbols once, and we
11652 would rather not keep them in memory, we handle all the
11653 relocations for a single input file at the same time.
11655 Unfortunately, there is no way to know the total number of local
11656 symbols until we have seen all of them, and the local symbol
11657 indices precede the global symbol indices. This means that when
11658 we are generating relocatable output, and we see a reloc against
11659 a global symbol, we can not know the symbol index until we have
11660 finished examining all the local symbols to see which ones we are
11661 going to output. To deal with this, we keep the relocations in
11662 memory, and don't output them until the end of the link. This is
11663 an unfortunate waste of memory, but I don't see a good way around
11664 it. Fortunately, it only happens when performing a relocatable
11665 link, which is not the common case. FIXME: If keep_memory is set
11666 we could write the relocs out and then read them again; I don't
11667 know how bad the memory loss will be. */
11669 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11670 sub
->output_has_begun
= FALSE
;
11671 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11673 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11675 if (p
->type
== bfd_indirect_link_order
11676 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11677 == bfd_target_elf_flavour
)
11678 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11680 if (! sub
->output_has_begun
)
11682 if (! elf_link_input_bfd (&flinfo
, sub
))
11684 sub
->output_has_begun
= TRUE
;
11687 else if (p
->type
== bfd_section_reloc_link_order
11688 || p
->type
== bfd_symbol_reloc_link_order
)
11690 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11695 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11697 if (p
->type
== bfd_indirect_link_order
11698 && (bfd_get_flavour (sub
)
11699 == bfd_target_elf_flavour
)
11700 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11701 != bed
->s
->elfclass
))
11703 const char *iclass
, *oclass
;
11705 switch (bed
->s
->elfclass
)
11707 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11708 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11709 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11713 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11715 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11716 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11717 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11721 bfd_set_error (bfd_error_wrong_format
);
11722 (*_bfd_error_handler
)
11723 (_("%B: file class %s incompatible with %s"),
11724 sub
, iclass
, oclass
);
11733 /* Free symbol buffer if needed. */
11734 if (!info
->reduce_memory_overheads
)
11736 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11737 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11738 && elf_tdata (sub
)->symbuf
)
11740 free (elf_tdata (sub
)->symbuf
);
11741 elf_tdata (sub
)->symbuf
= NULL
;
11745 /* Output any global symbols that got converted to local in a
11746 version script or due to symbol visibility. We do this in a
11747 separate step since ELF requires all local symbols to appear
11748 prior to any global symbols. FIXME: We should only do this if
11749 some global symbols were, in fact, converted to become local.
11750 FIXME: Will this work correctly with the Irix 5 linker? */
11751 eoinfo
.failed
= FALSE
;
11752 eoinfo
.flinfo
= &flinfo
;
11753 eoinfo
.localsyms
= TRUE
;
11754 eoinfo
.file_sym_done
= FALSE
;
11755 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11759 /* If backend needs to output some local symbols not present in the hash
11760 table, do it now. */
11761 if (bed
->elf_backend_output_arch_local_syms
11762 && (info
->strip
!= strip_all
|| emit_relocs
))
11764 typedef int (*out_sym_func
)
11765 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11766 struct elf_link_hash_entry
*);
11768 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11769 (abfd
, info
, &flinfo
,
11770 (out_sym_func
) elf_link_output_symstrtab
)))
11774 /* That wrote out all the local symbols. Finish up the symbol table
11775 with the global symbols. Even if we want to strip everything we
11776 can, we still need to deal with those global symbols that got
11777 converted to local in a version script. */
11779 /* The sh_info field records the index of the first non local symbol. */
11780 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11783 && elf_hash_table (info
)->dynsym
!= NULL
11784 && (elf_hash_table (info
)->dynsym
->output_section
11785 != bfd_abs_section_ptr
))
11787 Elf_Internal_Sym sym
;
11788 bfd_byte
*dynsym
= elf_hash_table (info
)->dynsym
->contents
;
11790 o
= elf_hash_table (info
)->dynsym
->output_section
;
11791 elf_section_data (o
)->this_hdr
.sh_info
11792 = elf_hash_table (info
)->local_dynsymcount
+ 1;
11794 /* Write out the section symbols for the output sections. */
11795 if (bfd_link_pic (info
)
11796 || elf_hash_table (info
)->is_relocatable_executable
)
11802 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11804 sym
.st_target_internal
= 0;
11806 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11812 dynindx
= elf_section_data (s
)->dynindx
;
11815 indx
= elf_section_data (s
)->this_idx
;
11816 BFD_ASSERT (indx
> 0);
11817 sym
.st_shndx
= indx
;
11818 if (! check_dynsym (abfd
, &sym
))
11820 sym
.st_value
= s
->vma
;
11821 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11822 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11826 /* Write out the local dynsyms. */
11827 if (elf_hash_table (info
)->dynlocal
)
11829 struct elf_link_local_dynamic_entry
*e
;
11830 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
11835 /* Copy the internal symbol and turn off visibility.
11836 Note that we saved a word of storage and overwrote
11837 the original st_name with the dynstr_index. */
11839 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11841 s
= bfd_section_from_elf_index (e
->input_bfd
,
11846 elf_section_data (s
->output_section
)->this_idx
;
11847 if (! check_dynsym (abfd
, &sym
))
11849 sym
.st_value
= (s
->output_section
->vma
11851 + e
->isym
.st_value
);
11854 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11855 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11860 /* We get the global symbols from the hash table. */
11861 eoinfo
.failed
= FALSE
;
11862 eoinfo
.localsyms
= FALSE
;
11863 eoinfo
.flinfo
= &flinfo
;
11864 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11868 /* If backend needs to output some symbols not present in the hash
11869 table, do it now. */
11870 if (bed
->elf_backend_output_arch_syms
11871 && (info
->strip
!= strip_all
|| emit_relocs
))
11873 typedef int (*out_sym_func
)
11874 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11875 struct elf_link_hash_entry
*);
11877 if (! ((*bed
->elf_backend_output_arch_syms
)
11878 (abfd
, info
, &flinfo
,
11879 (out_sym_func
) elf_link_output_symstrtab
)))
11883 /* Finalize the .strtab section. */
11884 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11886 /* Swap out the .strtab section. */
11887 if (!elf_link_swap_symbols_out (&flinfo
))
11890 /* Now we know the size of the symtab section. */
11891 if (bfd_get_symcount (abfd
) > 0)
11893 /* Finish up and write out the symbol string table (.strtab)
11895 Elf_Internal_Shdr
*symstrtab_hdr
;
11896 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11898 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11899 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11901 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11902 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11903 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11904 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11905 symtab_shndx_hdr
->sh_size
= amt
;
11907 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11910 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11911 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11915 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11916 /* sh_name was set in prep_headers. */
11917 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11918 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11919 symstrtab_hdr
->sh_addr
= 0;
11920 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11921 symstrtab_hdr
->sh_entsize
= 0;
11922 symstrtab_hdr
->sh_link
= 0;
11923 symstrtab_hdr
->sh_info
= 0;
11924 /* sh_offset is set just below. */
11925 symstrtab_hdr
->sh_addralign
= 1;
11927 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11929 elf_next_file_pos (abfd
) = off
;
11931 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11932 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11936 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11938 (*_bfd_error_handler
) (_("%B: failed to generate import library"),
11939 info
->out_implib_bfd
);
11943 /* Adjust the relocs to have the correct symbol indices. */
11944 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11946 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11948 if ((o
->flags
& SEC_RELOC
) == 0)
11951 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11952 if (esdo
->rel
.hdr
!= NULL
11953 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
11955 if (esdo
->rela
.hdr
!= NULL
11956 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
11959 /* Set the reloc_count field to 0 to prevent write_relocs from
11960 trying to swap the relocs out itself. */
11961 o
->reloc_count
= 0;
11964 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11965 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
11967 /* If we are linking against a dynamic object, or generating a
11968 shared library, finish up the dynamic linking information. */
11971 bfd_byte
*dyncon
, *dynconend
;
11973 /* Fix up .dynamic entries. */
11974 o
= bfd_get_linker_section (dynobj
, ".dynamic");
11975 BFD_ASSERT (o
!= NULL
);
11977 dyncon
= o
->contents
;
11978 dynconend
= o
->contents
+ o
->size
;
11979 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11981 Elf_Internal_Dyn dyn
;
11985 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11992 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
11994 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
11996 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
11997 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12000 dyn
.d_un
.d_val
= relativecount
;
12007 name
= info
->init_function
;
12010 name
= info
->fini_function
;
12013 struct elf_link_hash_entry
*h
;
12015 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
12016 FALSE
, FALSE
, TRUE
);
12018 && (h
->root
.type
== bfd_link_hash_defined
12019 || h
->root
.type
== bfd_link_hash_defweak
))
12021 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12022 o
= h
->root
.u
.def
.section
;
12023 if (o
->output_section
!= NULL
)
12024 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12025 + o
->output_offset
);
12028 /* The symbol is imported from another shared
12029 library and does not apply to this one. */
12030 dyn
.d_un
.d_ptr
= 0;
12037 case DT_PREINIT_ARRAYSZ
:
12038 name
= ".preinit_array";
12040 case DT_INIT_ARRAYSZ
:
12041 name
= ".init_array";
12043 case DT_FINI_ARRAYSZ
:
12044 name
= ".fini_array";
12046 o
= bfd_get_section_by_name (abfd
, name
);
12049 (*_bfd_error_handler
)
12050 (_("could not find section %s"), name
);
12054 (*_bfd_error_handler
)
12055 (_("warning: %s section has zero size"), name
);
12056 dyn
.d_un
.d_val
= o
->size
;
12059 case DT_PREINIT_ARRAY
:
12060 name
= ".preinit_array";
12062 case DT_INIT_ARRAY
:
12063 name
= ".init_array";
12065 case DT_FINI_ARRAY
:
12066 name
= ".fini_array";
12068 o
= bfd_get_section_by_name (abfd
, name
);
12075 name
= ".gnu.hash";
12084 name
= ".gnu.version_d";
12087 name
= ".gnu.version_r";
12090 name
= ".gnu.version";
12092 o
= bfd_get_linker_section (dynobj
, name
);
12096 (*_bfd_error_handler
)
12097 (_("could not find section %s"), name
);
12100 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12102 (*_bfd_error_handler
)
12103 (_("warning: section '%s' is being made into a note"), name
);
12104 bfd_set_error (bfd_error_nonrepresentable_section
);
12107 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12114 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12118 dyn
.d_un
.d_val
= 0;
12119 dyn
.d_un
.d_ptr
= 0;
12120 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12122 Elf_Internal_Shdr
*hdr
;
12124 hdr
= elf_elfsections (abfd
)[i
];
12125 if (hdr
->sh_type
== type
12126 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12128 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12129 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12132 if (dyn
.d_un
.d_ptr
== 0
12133 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12134 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12140 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12144 /* If we have created any dynamic sections, then output them. */
12145 if (dynobj
!= NULL
)
12147 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12150 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12151 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12152 || info
->error_textrel
)
12153 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12155 bfd_byte
*dyncon
, *dynconend
;
12157 dyncon
= o
->contents
;
12158 dynconend
= o
->contents
+ o
->size
;
12159 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12161 Elf_Internal_Dyn dyn
;
12163 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12165 if (dyn
.d_tag
== DT_TEXTREL
)
12167 if (info
->error_textrel
)
12168 info
->callbacks
->einfo
12169 (_("%P%X: read-only segment has dynamic relocations.\n"));
12171 info
->callbacks
->einfo
12172 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12178 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12180 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12182 || o
->output_section
== bfd_abs_section_ptr
)
12184 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12186 /* At this point, we are only interested in sections
12187 created by _bfd_elf_link_create_dynamic_sections. */
12190 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
12192 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
12194 if (strcmp (o
->name
, ".dynstr") != 0)
12196 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12198 (file_ptr
) o
->output_offset
12199 * bfd_octets_per_byte (abfd
),
12205 /* The contents of the .dynstr section are actually in a
12209 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12210 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12211 || ! _bfd_elf_strtab_emit (abfd
,
12212 elf_hash_table (info
)->dynstr
))
12218 if (bfd_link_relocatable (info
))
12220 bfd_boolean failed
= FALSE
;
12222 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12227 /* If we have optimized stabs strings, output them. */
12228 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
12230 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
12234 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12237 elf_final_link_free (abfd
, &flinfo
);
12239 elf_linker (abfd
) = TRUE
;
12243 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12244 if (contents
== NULL
)
12245 return FALSE
; /* Bail out and fail. */
12246 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12247 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12254 elf_final_link_free (abfd
, &flinfo
);
12258 /* Initialize COOKIE for input bfd ABFD. */
12261 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12262 struct bfd_link_info
*info
, bfd
*abfd
)
12264 Elf_Internal_Shdr
*symtab_hdr
;
12265 const struct elf_backend_data
*bed
;
12267 bed
= get_elf_backend_data (abfd
);
12268 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12270 cookie
->abfd
= abfd
;
12271 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12272 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12273 if (cookie
->bad_symtab
)
12275 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12276 cookie
->extsymoff
= 0;
12280 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12281 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12284 if (bed
->s
->arch_size
== 32)
12285 cookie
->r_sym_shift
= 8;
12287 cookie
->r_sym_shift
= 32;
12289 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12290 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12292 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12293 cookie
->locsymcount
, 0,
12295 if (cookie
->locsyms
== NULL
)
12297 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12300 if (info
->keep_memory
)
12301 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12306 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12309 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12311 Elf_Internal_Shdr
*symtab_hdr
;
12313 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12314 if (cookie
->locsyms
!= NULL
12315 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12316 free (cookie
->locsyms
);
12319 /* Initialize the relocation information in COOKIE for input section SEC
12320 of input bfd ABFD. */
12323 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12324 struct bfd_link_info
*info
, bfd
*abfd
,
12327 const struct elf_backend_data
*bed
;
12329 if (sec
->reloc_count
== 0)
12331 cookie
->rels
= NULL
;
12332 cookie
->relend
= NULL
;
12336 bed
= get_elf_backend_data (abfd
);
12338 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12339 info
->keep_memory
);
12340 if (cookie
->rels
== NULL
)
12342 cookie
->rel
= cookie
->rels
;
12343 cookie
->relend
= (cookie
->rels
12344 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12346 cookie
->rel
= cookie
->rels
;
12350 /* Free the memory allocated by init_reloc_cookie_rels,
12354 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12357 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12358 free (cookie
->rels
);
12361 /* Initialize the whole of COOKIE for input section SEC. */
12364 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12365 struct bfd_link_info
*info
,
12368 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12370 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12375 fini_reloc_cookie (cookie
, sec
->owner
);
12380 /* Free the memory allocated by init_reloc_cookie_for_section,
12384 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12387 fini_reloc_cookie_rels (cookie
, sec
);
12388 fini_reloc_cookie (cookie
, sec
->owner
);
12391 /* Garbage collect unused sections. */
12393 /* Default gc_mark_hook. */
12396 _bfd_elf_gc_mark_hook (asection
*sec
,
12397 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12398 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12399 struct elf_link_hash_entry
*h
,
12400 Elf_Internal_Sym
*sym
)
12404 switch (h
->root
.type
)
12406 case bfd_link_hash_defined
:
12407 case bfd_link_hash_defweak
:
12408 return h
->root
.u
.def
.section
;
12410 case bfd_link_hash_common
:
12411 return h
->root
.u
.c
.p
->section
;
12418 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12423 /* For undefined __start_<name> and __stop_<name> symbols, return the
12424 first input section matching <name>. Return NULL otherwise. */
12427 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12428 struct elf_link_hash_entry
*h
)
12431 const char *sec_name
;
12433 if (h
->root
.type
!= bfd_link_hash_undefined
12434 && h
->root
.type
!= bfd_link_hash_undefweak
)
12437 s
= h
->root
.u
.undef
.section
;
12440 if (s
== (asection
*) 0 - 1)
12446 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12447 sec_name
= h
->root
.root
.string
+ 8;
12448 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12449 sec_name
= h
->root
.root
.string
+ 7;
12451 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12455 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12457 s
= bfd_get_section_by_name (i
, sec_name
);
12460 h
->root
.u
.undef
.section
= s
;
12467 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12472 /* COOKIE->rel describes a relocation against section SEC, which is
12473 a section we've decided to keep. Return the section that contains
12474 the relocation symbol, or NULL if no section contains it. */
12477 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12478 elf_gc_mark_hook_fn gc_mark_hook
,
12479 struct elf_reloc_cookie
*cookie
,
12480 bfd_boolean
*start_stop
)
12482 unsigned long r_symndx
;
12483 struct elf_link_hash_entry
*h
;
12485 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12486 if (r_symndx
== STN_UNDEF
)
12489 if (r_symndx
>= cookie
->locsymcount
12490 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12492 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12495 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12499 while (h
->root
.type
== bfd_link_hash_indirect
12500 || h
->root
.type
== bfd_link_hash_warning
)
12501 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12503 /* If this symbol is weak and there is a non-weak definition, we
12504 keep the non-weak definition because many backends put
12505 dynamic reloc info on the non-weak definition for code
12506 handling copy relocs. */
12507 if (h
->u
.weakdef
!= NULL
)
12508 h
->u
.weakdef
->mark
= 1;
12510 if (start_stop
!= NULL
)
12512 /* To work around a glibc bug, mark all XXX input sections
12513 when there is an as yet undefined reference to __start_XXX
12514 or __stop_XXX symbols. The linker will later define such
12515 symbols for orphan input sections that have a name
12516 representable as a C identifier. */
12517 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12521 *start_stop
= !s
->gc_mark
;
12526 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12529 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12530 &cookie
->locsyms
[r_symndx
]);
12533 /* COOKIE->rel describes a relocation against section SEC, which is
12534 a section we've decided to keep. Mark the section that contains
12535 the relocation symbol. */
12538 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12540 elf_gc_mark_hook_fn gc_mark_hook
,
12541 struct elf_reloc_cookie
*cookie
)
12544 bfd_boolean start_stop
= FALSE
;
12546 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12547 while (rsec
!= NULL
)
12549 if (!rsec
->gc_mark
)
12551 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12552 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12554 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12559 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12564 /* The mark phase of garbage collection. For a given section, mark
12565 it and any sections in this section's group, and all the sections
12566 which define symbols to which it refers. */
12569 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12571 elf_gc_mark_hook_fn gc_mark_hook
)
12574 asection
*group_sec
, *eh_frame
;
12578 /* Mark all the sections in the group. */
12579 group_sec
= elf_section_data (sec
)->next_in_group
;
12580 if (group_sec
&& !group_sec
->gc_mark
)
12581 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12584 /* Look through the section relocs. */
12586 eh_frame
= elf_eh_frame_section (sec
->owner
);
12587 if ((sec
->flags
& SEC_RELOC
) != 0
12588 && sec
->reloc_count
> 0
12589 && sec
!= eh_frame
)
12591 struct elf_reloc_cookie cookie
;
12593 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12597 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12598 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12603 fini_reloc_cookie_for_section (&cookie
, sec
);
12607 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12609 struct elf_reloc_cookie cookie
;
12611 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12615 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12616 gc_mark_hook
, &cookie
))
12618 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12622 eh_frame
= elf_section_eh_frame_entry (sec
);
12623 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12624 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12630 /* Scan and mark sections in a special or debug section group. */
12633 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12635 /* Point to first section of section group. */
12637 /* Used to iterate the section group. */
12640 bfd_boolean is_special_grp
= TRUE
;
12641 bfd_boolean is_debug_grp
= TRUE
;
12643 /* First scan to see if group contains any section other than debug
12644 and special section. */
12645 ssec
= msec
= elf_next_in_group (grp
);
12648 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12649 is_debug_grp
= FALSE
;
12651 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12652 is_special_grp
= FALSE
;
12654 msec
= elf_next_in_group (msec
);
12656 while (msec
!= ssec
);
12658 /* If this is a pure debug section group or pure special section group,
12659 keep all sections in this group. */
12660 if (is_debug_grp
|| is_special_grp
)
12665 msec
= elf_next_in_group (msec
);
12667 while (msec
!= ssec
);
12671 /* Keep debug and special sections. */
12674 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12675 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12679 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12682 bfd_boolean some_kept
;
12683 bfd_boolean debug_frag_seen
;
12685 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12688 /* Ensure all linker created sections are kept,
12689 see if any other section is already marked,
12690 and note if we have any fragmented debug sections. */
12691 debug_frag_seen
= some_kept
= FALSE
;
12692 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12694 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12696 else if (isec
->gc_mark
)
12699 if (debug_frag_seen
== FALSE
12700 && (isec
->flags
& SEC_DEBUGGING
)
12701 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12702 debug_frag_seen
= TRUE
;
12705 /* If no section in this file will be kept, then we can
12706 toss out the debug and special sections. */
12710 /* Keep debug and special sections like .comment when they are
12711 not part of a group. Also keep section groups that contain
12712 just debug sections or special sections. */
12713 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12715 if ((isec
->flags
& SEC_GROUP
) != 0)
12716 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12717 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12718 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12719 && elf_next_in_group (isec
) == NULL
)
12723 if (! debug_frag_seen
)
12726 /* Look for CODE sections which are going to be discarded,
12727 and find and discard any fragmented debug sections which
12728 are associated with that code section. */
12729 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12730 if ((isec
->flags
& SEC_CODE
) != 0
12731 && isec
->gc_mark
== 0)
12736 ilen
= strlen (isec
->name
);
12738 /* Association is determined by the name of the debug section
12739 containing the name of the code section as a suffix. For
12740 example .debug_line.text.foo is a debug section associated
12742 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12746 if (dsec
->gc_mark
== 0
12747 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12750 dlen
= strlen (dsec
->name
);
12753 && strncmp (dsec
->name
+ (dlen
- ilen
),
12754 isec
->name
, ilen
) == 0)
12764 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12766 struct elf_gc_sweep_symbol_info
12768 struct bfd_link_info
*info
;
12769 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12774 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12777 && (((h
->root
.type
== bfd_link_hash_defined
12778 || h
->root
.type
== bfd_link_hash_defweak
)
12779 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12780 && h
->root
.u
.def
.section
->gc_mark
))
12781 || h
->root
.type
== bfd_link_hash_undefined
12782 || h
->root
.type
== bfd_link_hash_undefweak
))
12784 struct elf_gc_sweep_symbol_info
*inf
;
12786 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12787 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12788 h
->def_regular
= 0;
12789 h
->ref_regular
= 0;
12790 h
->ref_regular_nonweak
= 0;
12796 /* The sweep phase of garbage collection. Remove all garbage sections. */
12798 typedef bfd_boolean (*gc_sweep_hook_fn
)
12799 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12802 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12805 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12806 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12807 unsigned long section_sym_count
;
12808 struct elf_gc_sweep_symbol_info sweep_info
;
12810 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12814 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12815 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12818 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12820 /* When any section in a section group is kept, we keep all
12821 sections in the section group. If the first member of
12822 the section group is excluded, we will also exclude the
12824 if (o
->flags
& SEC_GROUP
)
12826 asection
*first
= elf_next_in_group (o
);
12827 o
->gc_mark
= first
->gc_mark
;
12833 /* Skip sweeping sections already excluded. */
12834 if (o
->flags
& SEC_EXCLUDE
)
12837 /* Since this is early in the link process, it is simple
12838 to remove a section from the output. */
12839 o
->flags
|= SEC_EXCLUDE
;
12841 if (info
->print_gc_sections
&& o
->size
!= 0)
12842 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12844 /* But we also have to update some of the relocation
12845 info we collected before. */
12847 && (o
->flags
& SEC_RELOC
) != 0
12848 && o
->reloc_count
!= 0
12849 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12850 && (o
->flags
& SEC_DEBUGGING
) != 0)
12851 && !bfd_is_abs_section (o
->output_section
))
12853 Elf_Internal_Rela
*internal_relocs
;
12857 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12858 info
->keep_memory
);
12859 if (internal_relocs
== NULL
)
12862 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12864 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12865 free (internal_relocs
);
12873 /* Remove the symbols that were in the swept sections from the dynamic
12874 symbol table. GCFIXME: Anyone know how to get them out of the
12875 static symbol table as well? */
12876 sweep_info
.info
= info
;
12877 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12878 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12881 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12885 /* Propagate collected vtable information. This is called through
12886 elf_link_hash_traverse. */
12889 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12891 /* Those that are not vtables. */
12892 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12895 /* Those vtables that do not have parents, we cannot merge. */
12896 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12899 /* If we've already been done, exit. */
12900 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12903 /* Make sure the parent's table is up to date. */
12904 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12906 if (h
->vtable
->used
== NULL
)
12908 /* None of this table's entries were referenced. Re-use the
12910 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12911 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12916 bfd_boolean
*cu
, *pu
;
12918 /* Or the parent's entries into ours. */
12919 cu
= h
->vtable
->used
;
12921 pu
= h
->vtable
->parent
->vtable
->used
;
12924 const struct elf_backend_data
*bed
;
12925 unsigned int log_file_align
;
12927 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12928 log_file_align
= bed
->s
->log_file_align
;
12929 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12944 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12947 bfd_vma hstart
, hend
;
12948 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12949 const struct elf_backend_data
*bed
;
12950 unsigned int log_file_align
;
12952 /* Take care of both those symbols that do not describe vtables as
12953 well as those that are not loaded. */
12954 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12957 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
12958 || h
->root
.type
== bfd_link_hash_defweak
);
12960 sec
= h
->root
.u
.def
.section
;
12961 hstart
= h
->root
.u
.def
.value
;
12962 hend
= hstart
+ h
->size
;
12964 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
12966 return *(bfd_boolean
*) okp
= FALSE
;
12967 bed
= get_elf_backend_data (sec
->owner
);
12968 log_file_align
= bed
->s
->log_file_align
;
12970 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
12972 for (rel
= relstart
; rel
< relend
; ++rel
)
12973 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
12975 /* If the entry is in use, do nothing. */
12976 if (h
->vtable
->used
12977 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
12979 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
12980 if (h
->vtable
->used
[entry
])
12983 /* Otherwise, kill it. */
12984 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
12990 /* Mark sections containing dynamically referenced symbols. When
12991 building shared libraries, we must assume that any visible symbol is
12995 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
12997 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
12998 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13000 if ((h
->root
.type
== bfd_link_hash_defined
13001 || h
->root
.type
== bfd_link_hash_defweak
)
13003 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13004 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13005 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13006 && (!bfd_link_executable (info
)
13007 || info
->export_dynamic
13010 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13011 && (h
->versioned
>= versioned
13012 || !bfd_hide_sym_by_version (info
->version_info
,
13013 h
->root
.root
.string
)))))
13014 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13019 /* Keep all sections containing symbols undefined on the command-line,
13020 and the section containing the entry symbol. */
13023 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13025 struct bfd_sym_chain
*sym
;
13027 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13029 struct elf_link_hash_entry
*h
;
13031 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13032 FALSE
, FALSE
, FALSE
);
13035 && (h
->root
.type
== bfd_link_hash_defined
13036 || h
->root
.type
== bfd_link_hash_defweak
)
13037 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
13038 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13043 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13044 struct bfd_link_info
*info
)
13046 bfd
*ibfd
= info
->input_bfds
;
13048 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13051 struct elf_reloc_cookie cookie
;
13053 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13056 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13059 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13061 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13062 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13064 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13065 fini_reloc_cookie_rels (&cookie
, sec
);
13072 /* Do mark and sweep of unused sections. */
13075 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13077 bfd_boolean ok
= TRUE
;
13079 elf_gc_mark_hook_fn gc_mark_hook
;
13080 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13081 struct elf_link_hash_table
*htab
;
13083 if (!bed
->can_gc_sections
13084 || !is_elf_hash_table (info
->hash
))
13086 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
13090 bed
->gc_keep (info
);
13091 htab
= elf_hash_table (info
);
13093 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13094 at the .eh_frame section if we can mark the FDEs individually. */
13095 for (sub
= info
->input_bfds
;
13096 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13097 sub
= sub
->link
.next
)
13100 struct elf_reloc_cookie cookie
;
13102 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13103 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13105 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13106 if (elf_section_data (sec
)->sec_info
13107 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13108 elf_eh_frame_section (sub
) = sec
;
13109 fini_reloc_cookie_for_section (&cookie
, sec
);
13110 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13114 /* Apply transitive closure to the vtable entry usage info. */
13115 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13119 /* Kill the vtable relocations that were not used. */
13120 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13124 /* Mark dynamically referenced symbols. */
13125 if (htab
->dynamic_sections_created
)
13126 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13128 /* Grovel through relocs to find out who stays ... */
13129 gc_mark_hook
= bed
->gc_mark_hook
;
13130 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13134 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13135 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13138 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13139 Also treat note sections as a root, if the section is not part
13141 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13143 && (o
->flags
& SEC_EXCLUDE
) == 0
13144 && ((o
->flags
& SEC_KEEP
) != 0
13145 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13146 && elf_next_in_group (o
) == NULL
)))
13148 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13153 /* Allow the backend to mark additional target specific sections. */
13154 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13156 /* ... and mark SEC_EXCLUDE for those that go. */
13157 return elf_gc_sweep (abfd
, info
);
13160 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13163 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13165 struct elf_link_hash_entry
*h
,
13168 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13169 struct elf_link_hash_entry
**search
, *child
;
13170 size_t extsymcount
;
13171 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13173 /* The sh_info field of the symtab header tells us where the
13174 external symbols start. We don't care about the local symbols at
13176 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13177 if (!elf_bad_symtab (abfd
))
13178 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13180 sym_hashes
= elf_sym_hashes (abfd
);
13181 sym_hashes_end
= sym_hashes
+ extsymcount
;
13183 /* Hunt down the child symbol, which is in this section at the same
13184 offset as the relocation. */
13185 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13187 if ((child
= *search
) != NULL
13188 && (child
->root
.type
== bfd_link_hash_defined
13189 || child
->root
.type
== bfd_link_hash_defweak
)
13190 && child
->root
.u
.def
.section
== sec
13191 && child
->root
.u
.def
.value
== offset
)
13195 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
13196 abfd
, sec
, (unsigned long) offset
);
13197 bfd_set_error (bfd_error_invalid_operation
);
13201 if (!child
->vtable
)
13203 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13204 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13205 if (!child
->vtable
)
13210 /* This *should* only be the absolute section. It could potentially
13211 be that someone has defined a non-global vtable though, which
13212 would be bad. It isn't worth paging in the local symbols to be
13213 sure though; that case should simply be handled by the assembler. */
13215 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13218 child
->vtable
->parent
= h
;
13223 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13226 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13227 asection
*sec ATTRIBUTE_UNUSED
,
13228 struct elf_link_hash_entry
*h
,
13231 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13232 unsigned int log_file_align
= bed
->s
->log_file_align
;
13236 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13237 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13242 if (addend
>= h
->vtable
->size
)
13244 size_t size
, bytes
, file_align
;
13245 bfd_boolean
*ptr
= h
->vtable
->used
;
13247 /* While the symbol is undefined, we have to be prepared to handle
13249 file_align
= 1 << log_file_align
;
13250 if (h
->root
.type
== bfd_link_hash_undefined
)
13251 size
= addend
+ file_align
;
13255 if (addend
>= size
)
13257 /* Oops! We've got a reference past the defined end of
13258 the table. This is probably a bug -- shall we warn? */
13259 size
= addend
+ file_align
;
13262 size
= (size
+ file_align
- 1) & -file_align
;
13264 /* Allocate one extra entry for use as a "done" flag for the
13265 consolidation pass. */
13266 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13270 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13276 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13277 * sizeof (bfd_boolean
));
13278 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13282 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13287 /* And arrange for that done flag to be at index -1. */
13288 h
->vtable
->used
= ptr
+ 1;
13289 h
->vtable
->size
= size
;
13292 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13297 /* Map an ELF section header flag to its corresponding string. */
13301 flagword flag_value
;
13302 } elf_flags_to_name_table
;
13304 static elf_flags_to_name_table elf_flags_to_names
[] =
13306 { "SHF_WRITE", SHF_WRITE
},
13307 { "SHF_ALLOC", SHF_ALLOC
},
13308 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13309 { "SHF_MERGE", SHF_MERGE
},
13310 { "SHF_STRINGS", SHF_STRINGS
},
13311 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13312 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13313 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13314 { "SHF_GROUP", SHF_GROUP
},
13315 { "SHF_TLS", SHF_TLS
},
13316 { "SHF_MASKOS", SHF_MASKOS
},
13317 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13320 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13322 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13323 struct flag_info
*flaginfo
,
13326 const bfd_vma sh_flags
= elf_section_flags (section
);
13328 if (!flaginfo
->flags_initialized
)
13330 bfd
*obfd
= info
->output_bfd
;
13331 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13332 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13334 int without_hex
= 0;
13336 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13339 flagword (*lookup
) (char *);
13341 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13342 if (lookup
!= NULL
)
13344 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13348 if (tf
->with
== with_flags
)
13349 with_hex
|= hexval
;
13350 else if (tf
->with
== without_flags
)
13351 without_hex
|= hexval
;
13356 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13358 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13360 if (tf
->with
== with_flags
)
13361 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13362 else if (tf
->with
== without_flags
)
13363 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13370 info
->callbacks
->einfo
13371 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13375 flaginfo
->flags_initialized
= TRUE
;
13376 flaginfo
->only_with_flags
|= with_hex
;
13377 flaginfo
->not_with_flags
|= without_hex
;
13380 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13383 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13389 struct alloc_got_off_arg
{
13391 struct bfd_link_info
*info
;
13394 /* We need a special top-level link routine to convert got reference counts
13395 to real got offsets. */
13398 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13400 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13401 bfd
*obfd
= gofarg
->info
->output_bfd
;
13402 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13404 if (h
->got
.refcount
> 0)
13406 h
->got
.offset
= gofarg
->gotoff
;
13407 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13410 h
->got
.offset
= (bfd_vma
) -1;
13415 /* And an accompanying bit to work out final got entry offsets once
13416 we're done. Should be called from final_link. */
13419 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13420 struct bfd_link_info
*info
)
13423 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13425 struct alloc_got_off_arg gofarg
;
13427 BFD_ASSERT (abfd
== info
->output_bfd
);
13429 if (! is_elf_hash_table (info
->hash
))
13432 /* The GOT offset is relative to the .got section, but the GOT header is
13433 put into the .got.plt section, if the backend uses it. */
13434 if (bed
->want_got_plt
)
13437 gotoff
= bed
->got_header_size
;
13439 /* Do the local .got entries first. */
13440 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13442 bfd_signed_vma
*local_got
;
13443 size_t j
, locsymcount
;
13444 Elf_Internal_Shdr
*symtab_hdr
;
13446 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13449 local_got
= elf_local_got_refcounts (i
);
13453 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13454 if (elf_bad_symtab (i
))
13455 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13457 locsymcount
= symtab_hdr
->sh_info
;
13459 for (j
= 0; j
< locsymcount
; ++j
)
13461 if (local_got
[j
] > 0)
13463 local_got
[j
] = gotoff
;
13464 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13467 local_got
[j
] = (bfd_vma
) -1;
13471 /* Then the global .got entries. .plt refcounts are handled by
13472 adjust_dynamic_symbol */
13473 gofarg
.gotoff
= gotoff
;
13474 gofarg
.info
= info
;
13475 elf_link_hash_traverse (elf_hash_table (info
),
13476 elf_gc_allocate_got_offsets
,
13481 /* Many folk need no more in the way of final link than this, once
13482 got entry reference counting is enabled. */
13485 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13487 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13490 /* Invoke the regular ELF backend linker to do all the work. */
13491 return bfd_elf_final_link (abfd
, info
);
13495 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13497 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13499 if (rcookie
->bad_symtab
)
13500 rcookie
->rel
= rcookie
->rels
;
13502 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13504 unsigned long r_symndx
;
13506 if (! rcookie
->bad_symtab
)
13507 if (rcookie
->rel
->r_offset
> offset
)
13509 if (rcookie
->rel
->r_offset
!= offset
)
13512 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13513 if (r_symndx
== STN_UNDEF
)
13516 if (r_symndx
>= rcookie
->locsymcount
13517 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13519 struct elf_link_hash_entry
*h
;
13521 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13523 while (h
->root
.type
== bfd_link_hash_indirect
13524 || h
->root
.type
== bfd_link_hash_warning
)
13525 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13527 if ((h
->root
.type
== bfd_link_hash_defined
13528 || h
->root
.type
== bfd_link_hash_defweak
)
13529 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13530 || h
->root
.u
.def
.section
->kept_section
!= NULL
13531 || discarded_section (h
->root
.u
.def
.section
)))
13536 /* It's not a relocation against a global symbol,
13537 but it could be a relocation against a local
13538 symbol for a discarded section. */
13540 Elf_Internal_Sym
*isym
;
13542 /* Need to: get the symbol; get the section. */
13543 isym
= &rcookie
->locsyms
[r_symndx
];
13544 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13546 && (isec
->kept_section
!= NULL
13547 || discarded_section (isec
)))
13555 /* Discard unneeded references to discarded sections.
13556 Returns -1 on error, 1 if any section's size was changed, 0 if
13557 nothing changed. This function assumes that the relocations are in
13558 sorted order, which is true for all known assemblers. */
13561 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13563 struct elf_reloc_cookie cookie
;
13568 if (info
->traditional_format
13569 || !is_elf_hash_table (info
->hash
))
13572 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13577 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13580 || i
->reloc_count
== 0
13581 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13585 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13588 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13591 if (_bfd_discard_section_stabs (abfd
, i
,
13592 elf_section_data (i
)->sec_info
,
13593 bfd_elf_reloc_symbol_deleted_p
,
13597 fini_reloc_cookie_for_section (&cookie
, i
);
13602 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13603 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13608 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13614 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13617 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13620 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13621 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13622 bfd_elf_reloc_symbol_deleted_p
,
13626 fini_reloc_cookie_for_section (&cookie
, i
);
13630 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13632 const struct elf_backend_data
*bed
;
13634 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13637 bed
= get_elf_backend_data (abfd
);
13639 if (bed
->elf_backend_discard_info
!= NULL
)
13641 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13644 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13647 fini_reloc_cookie (&cookie
, abfd
);
13651 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13652 _bfd_elf_end_eh_frame_parsing (info
);
13654 if (info
->eh_frame_hdr_type
13655 && !bfd_link_relocatable (info
)
13656 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13663 _bfd_elf_section_already_linked (bfd
*abfd
,
13665 struct bfd_link_info
*info
)
13668 const char *name
, *key
;
13669 struct bfd_section_already_linked
*l
;
13670 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13672 if (sec
->output_section
== bfd_abs_section_ptr
)
13675 flags
= sec
->flags
;
13677 /* Return if it isn't a linkonce section. A comdat group section
13678 also has SEC_LINK_ONCE set. */
13679 if ((flags
& SEC_LINK_ONCE
) == 0)
13682 /* Don't put group member sections on our list of already linked
13683 sections. They are handled as a group via their group section. */
13684 if (elf_sec_group (sec
) != NULL
)
13687 /* For a SHT_GROUP section, use the group signature as the key. */
13689 if ((flags
& SEC_GROUP
) != 0
13690 && elf_next_in_group (sec
) != NULL
13691 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13692 key
= elf_group_name (elf_next_in_group (sec
));
13695 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13696 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13697 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13700 /* Must be a user linkonce section that doesn't follow gcc's
13701 naming convention. In this case we won't be matching
13702 single member groups. */
13706 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13708 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13710 /* We may have 2 different types of sections on the list: group
13711 sections with a signature of <key> (<key> is some string),
13712 and linkonce sections named .gnu.linkonce.<type>.<key>.
13713 Match like sections. LTO plugin sections are an exception.
13714 They are always named .gnu.linkonce.t.<key> and match either
13715 type of section. */
13716 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13717 && ((flags
& SEC_GROUP
) != 0
13718 || strcmp (name
, l
->sec
->name
) == 0))
13719 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13721 /* The section has already been linked. See if we should
13722 issue a warning. */
13723 if (!_bfd_handle_already_linked (sec
, l
, info
))
13726 if (flags
& SEC_GROUP
)
13728 asection
*first
= elf_next_in_group (sec
);
13729 asection
*s
= first
;
13733 s
->output_section
= bfd_abs_section_ptr
;
13734 /* Record which group discards it. */
13735 s
->kept_section
= l
->sec
;
13736 s
= elf_next_in_group (s
);
13737 /* These lists are circular. */
13747 /* A single member comdat group section may be discarded by a
13748 linkonce section and vice versa. */
13749 if ((flags
& SEC_GROUP
) != 0)
13751 asection
*first
= elf_next_in_group (sec
);
13753 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13754 /* Check this single member group against linkonce sections. */
13755 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13756 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13757 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13759 first
->output_section
= bfd_abs_section_ptr
;
13760 first
->kept_section
= l
->sec
;
13761 sec
->output_section
= bfd_abs_section_ptr
;
13766 /* Check this linkonce section against single member groups. */
13767 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13768 if (l
->sec
->flags
& SEC_GROUP
)
13770 asection
*first
= elf_next_in_group (l
->sec
);
13773 && elf_next_in_group (first
) == first
13774 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13776 sec
->output_section
= bfd_abs_section_ptr
;
13777 sec
->kept_section
= first
;
13782 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13783 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13784 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13785 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13786 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13787 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13788 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13789 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13790 The reverse order cannot happen as there is never a bfd with only the
13791 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13792 matter as here were are looking only for cross-bfd sections. */
13794 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13795 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13796 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13797 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13799 if (abfd
!= l
->sec
->owner
)
13800 sec
->output_section
= bfd_abs_section_ptr
;
13804 /* This is the first section with this name. Record it. */
13805 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13806 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13807 return sec
->output_section
== bfd_abs_section_ptr
;
13811 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13813 return sym
->st_shndx
== SHN_COMMON
;
13817 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13823 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13825 return bfd_com_section_ptr
;
13829 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13830 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13831 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13832 bfd
*ibfd ATTRIBUTE_UNUSED
,
13833 unsigned long symndx ATTRIBUTE_UNUSED
)
13835 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13836 return bed
->s
->arch_size
/ 8;
13839 /* Routines to support the creation of dynamic relocs. */
13841 /* Returns the name of the dynamic reloc section associated with SEC. */
13843 static const char *
13844 get_dynamic_reloc_section_name (bfd
* abfd
,
13846 bfd_boolean is_rela
)
13849 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13850 const char *prefix
= is_rela
? ".rela" : ".rel";
13852 if (old_name
== NULL
)
13855 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13856 sprintf (name
, "%s%s", prefix
, old_name
);
13861 /* Returns the dynamic reloc section associated with SEC.
13862 If necessary compute the name of the dynamic reloc section based
13863 on SEC's name (looked up in ABFD's string table) and the setting
13867 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13869 bfd_boolean is_rela
)
13871 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13873 if (reloc_sec
== NULL
)
13875 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13879 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13881 if (reloc_sec
!= NULL
)
13882 elf_section_data (sec
)->sreloc
= reloc_sec
;
13889 /* Returns the dynamic reloc section associated with SEC. If the
13890 section does not exist it is created and attached to the DYNOBJ
13891 bfd and stored in the SRELOC field of SEC's elf_section_data
13894 ALIGNMENT is the alignment for the newly created section and
13895 IS_RELA defines whether the name should be .rela.<SEC's name>
13896 or .rel.<SEC's name>. The section name is looked up in the
13897 string table associated with ABFD. */
13900 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13902 unsigned int alignment
,
13904 bfd_boolean is_rela
)
13906 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13908 if (reloc_sec
== NULL
)
13910 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13915 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13917 if (reloc_sec
== NULL
)
13919 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13920 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13921 if ((sec
->flags
& SEC_ALLOC
) != 0)
13922 flags
|= SEC_ALLOC
| SEC_LOAD
;
13924 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13925 if (reloc_sec
!= NULL
)
13927 /* _bfd_elf_get_sec_type_attr chooses a section type by
13928 name. Override as it may be wrong, eg. for a user
13929 section named "auto" we'll get ".relauto" which is
13930 seen to be a .rela section. */
13931 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13932 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13937 elf_section_data (sec
)->sreloc
= reloc_sec
;
13943 /* Copy the ELF symbol type and other attributes for a linker script
13944 assignment from HSRC to HDEST. Generally this should be treated as
13945 if we found a strong non-dynamic definition for HDEST (except that
13946 ld ignores multiple definition errors). */
13948 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
13949 struct bfd_link_hash_entry
*hdest
,
13950 struct bfd_link_hash_entry
*hsrc
)
13952 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
13953 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
13954 Elf_Internal_Sym isym
;
13956 ehdest
->type
= ehsrc
->type
;
13957 ehdest
->target_internal
= ehsrc
->target_internal
;
13959 isym
.st_other
= ehsrc
->other
;
13960 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
13963 /* Append a RELA relocation REL to section S in BFD. */
13966 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13968 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13969 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
13970 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
13971 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
13974 /* Append a REL relocation REL to section S in BFD. */
13977 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13979 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13980 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
13981 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
13982 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);